JunkBox_Lib  1.10.2
password.c
Go to the documentation of this file.
1 
7 #ifdef CPLUSPLUS
8  #undef CPLUSPLUS
9 #endif
10 
11 #include "password.h"
12 
13 
25 char* get_passwd(char* user_id)
26 {
27  struct passwd* pw;
28  struct spwd* sp;
29  char* pass;
30 
31  pass = (char*)malloc(LPASS+1);
32  if (pass==NULL) return NULL;
33  memset(pass, 0, LPASS+1);
34 
35  // for /etc/passwd
36  pw = getpwnam((const char*)user_id);
37  if (pw==NULL) {
38  free(pass);
39  return NULL;
40  }
41  strncpy(pass, pw->pw_passwd, LPASS);
42  if (strcmp(pass, "*") && strcmp(pass, "x") && strcmp(pass, "!")) return pass;
43 
44 #ifdef HAVE_GETSPNAM
45  // for /etc/shadow
46  sp = getspnam((const char*)user_id);
47  if (sp!=NULL) {
48  strncpy(pass, sp->sp_pwdp, LPASS);
49  return pass;
50  }
51 #endif
52 
53 #ifdef HAVE_RPCSVC_YPCLNT_H
54  // for NIS
55  pw = getnisnam(user_id);
56  if (pw!=NULL) {
57  strncpy(pass, pw->pw_passwd, LPASS);
58  free_pw(pw);
59  return pass;
60  }
61 #endif
62 
63  return pass; // *, x, !
64 }
65 
66 
67 #ifdef HAVE_RPCSVC_YPCLNT_H
68 
77 char* get_nis_passwdf(char* usrid)
78 {
79  const char* inmap="passwd.byname";
80  int ret, keylen, vallen, usrlen;
81  char* domainname;
82  char* key;
83  char* val;
84  char* uname;
85  char* nis;
86 
87  usrlen = strlen(usrid);
88  uname = (char*)malloc(usrlen + 2);
89  if (uname==NULL) return NULL;
90  strncpy(uname, usrid, usrlen);
91  uname[usrlen] = ':';
92  uname[usrlen+1] = '\0';
93 
94  yp_get_default_domain(&domainname);
95 
96  ret = yp_first(domainname, inmap, &key, &keylen, &val, &vallen);
97  while(ret==0){
98  if (!strncmp(val, uname, usrlen+1)) {
99  free(uname);
100  nis = (char*)malloc(strlen(val)+1);
101  if (nis==NULL) return NULL;
102  strncpy(nis, val, strlen(val)+1);
103  return nis;
104  }
105  ret = yp_next(domainname, inmap, key, keylen, &key, &keylen, &val, &vallen);
106  }
107 
108  free(uname);
109  return NULL;
110 }
111 
112 
122 struct passwd* getnisnam(char* usrid)
123 {
124  struct passwd* pw;
125  char* buf;
126  char* nis;
127 
128  nis = get_nis_passwdf(usrid);
129  if (nis==NULL) return NULL;
130  pw = (struct passwd*)malloc(sizeof(struct passwd));
131  if (pw==NULL) {
132  free(nis);
133  return NULL;
134  }
135 
136  pw->pw_name = awk(nis, ':', 1);
137  pw->pw_passwd = awk(nis, ':', 2);
138  pw->pw_gecos = awk(nis, ':', 5);
139  pw->pw_dir = awk(nis, ':', 6);
140  pw->pw_shell = awk(nis, ':', 7);
141 
142  buf = awk(nis, ':', 3);
143  if (buf!=NULL) pw->pw_uid = atoi(buf);
144  free(buf);
145 
146  buf = awk(nis, ':', 4);
147  if (buf!=NULL) pw->pw_gid = atoi(buf);
148  free(buf);
149 
150  free(nis);
151  return pw;
152 }
153 
154 #endif // HAVE_RPCSVC_YPCLNT_H
155 
156 
167 void free_pw(struct passwd* pw)
168 {
169  if (pw==NULL) return;
170 
171  freeNull(pw->pw_name);
172  freeNull(pw->pw_passwd);
173  freeNull(pw->pw_gecos);
174  freeNull(pw->pw_dir);
175  freeNull(pw->pw_shell);
176  freeNull(pw);
177  return;
178 }
179 
180 
194 char* x2crypt(char* pass, char* bsalt)
195 {
196  char* cpass;
197  char* dpass = NULL;
198  char* csalt;
199 
200  if (pass==NULL || bsalt==NULL) return NULL;
201 
202  csalt = get_line(bsalt, 1);
203  cpass = crypt(pass, csalt);
204  freeNull(csalt);
205  csalt = get_line(bsalt, 2);
206  if (csalt==NULL) return NULL;
207 
208  if (cpass[0]=='$') {
209  int lsalt = 0;
210  if (cpass[1]=='1') lsalt = LEN_DOLLAR_SALT;
211  else if (cpass[1]=='2') lsalt = LEN_DOLLAR2_SALT;
212  else if (cpass[1]=='5') lsalt = LEN_DOLLAR5_SALT;
213  else if (cpass[1]=='6') lsalt = LEN_DOLLAR6_SALT;
214 
215  if (lsalt!=0) {
216  int passlen = strlen(cpass);
217  dpass = cut_str(cpass, lsalt, passlen-1);
218  cpass = crypt(dpass, csalt);
219  freeNull(dpass);
220  dpass = cut_str(cpass, lsalt, passlen-1);
221  }
222  }
223  else if (strlen(cpass)==LEN_DESPASS) {
224  dpass = cut_str(cpass, LEN_DESSALT, LEN_DESPASS-1);
225  freeNull(cpass);
226  cpass = crypt(dpass, csalt);
227  freeNull(dpass);
228  dpass = cut_str(cpass, LEN_DESSALT, LEN_DESPASS-1);
229  }
230 
231  freeNull(csalt);
232  return dpass;
233 }
234 
235 
248 int check_passwd(char* passwd, char* cryptpass)
249 {
250  int samepass=FALSE;
251  char* salt;
252 
253  if (passwd==NULL || cryptpass==NULL) return FALSE;
254 
255 // if (!strncmp("$1$", cryptpass, 3) && strlen(cryptpass)==LEN_MD5PASS) {
256 // if (cryptpass[0]=='$' && cryptpass[2]=='$' && cryptpass[11]=='$') {
257  if (cryptpass[0]=='$') {
258  int lsalt = 0;
259  if (cryptpass[1]=='1') lsalt = LEN_DOLLAR_SALT;
260  else if (cryptpass[1]=='2') lsalt = LEN_DOLLAR2_SALT;
261  else if (cryptpass[1]=='5') lsalt = LEN_DOLLAR5_SALT;
262  else if (cryptpass[1]=='6') lsalt = LEN_DOLLAR6_SALT;
263 
264  if (lsalt!=0) {
265  salt = cut_str(cryptpass, 0, lsalt-1);
266  if (!strcmp(crypt(passwd, salt), cryptpass)) {
267  samepass = TRUE;
268  }
269  free(salt);
270  }
271  }
272  else if (strlen(cryptpass)==LEN_DESPASS) {
273  // DES
274  salt = cut_str(cryptpass, 0, LEN_DESSALT-1);
275  if (!strcmp(crypt(passwd, salt), cryptpass)) {
276  samepass = TRUE;
277  }
278  free(salt);
279  }
280 
281  return samepass;
282 }
283 
284 
301 int check_salt(char* passwd)
302 {
303  int ret = -1;
304  int len = strlen(passwd);
305 
306  if (LEN_DESPASS<len) {
307  if (passwd[0]=='$' && passwd[2]=='$' && passwd[11]=='$') {
308  ret = (short int)passwd[1] - (short int)'1' + 1;
309  }
310  }
311  else if (LEN_DESPASS==len) {
312  ret = 0;
313  }
314 
315  return ret;
316 }
317 
#define LPASS
$6$ -> 98文字
Definition: common.h:155
#define TRUE
Definition: common.h:226
#define FALSE
Definition: common.h:223
unsigned char ** buf
Definition: jpeg_tool.h:96
unsigned char unsigned long * len
Definition: jpeg_tool.h:96
int check_passwd(char *passwd, char *cryptpass)
Definition: password.c:248
void free_pw(struct passwd *pw)
Definition: password.c:167
char * get_passwd(char *user_id)
Definition: password.c:25
char * x2crypt(char *pass, char *bsalt)
Definition: password.c:194
int check_salt(char *passwd)
Definition: password.c:301
パスワードライブラリ ヘッダ
#define LEN_DOLLAR5_SALT
Definition: password.h:46
#define LEN_DOLLAR2_SALT
Definition: password.h:45
#define LEN_DOLLAR_SALT
Definition: password.h:44
#define LEN_DESPASS
Definition: password.h:51
#define LEN_DOLLAR6_SALT
Definition: password.h:47
#define LEN_DESSALT
Definition: password.h:52
char * awk(char *buf, char cc, int n)
ccを区切り記号として, strのバッファ内の n番目の項目を返す.要 free()
Definition: tools.c:567
char * cut_str(char *buf, int ls, int le)
buf[ls]〜buf[le] を切り出す.要 free()
Definition: tools.c:1338
char * get_line(char *buf, int n)
文字型データbufのバッファ内の n行目を取り出す.改行コードは削除される.要 free()
Definition: tools.c:484
#define freeNull(p)
Definition: tools.h:201