2 * $Id: uams_randnum.c,v 1.11 2002-09-29 23:30:20 sibaz Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu)
6 * All Rights Reserved. See COPYRIGHT.
11 #endif /* HAVE_CONFIG_H */
19 #else /* STDC_HEADERS */
23 #endif /* HAVE_STRCHR */
24 char *strchr (), *strrchr ();
26 #define memcpy(d,s,n) bcopy ((s), (d), (n))
27 #define memmove(d,s,n) bcopy ((s), (d), (n))
28 #endif /* ! HAVE_MEMCPY */
29 #endif /* STDC_HEADERS */
33 #endif /* HAVE_UNISTD_H */
36 #endif /* HAVE_FCNTL_H */
40 #include <sys/param.h>
42 #include <atalk/logger.h>
44 #include <netatalk/endian.h>
46 #include <atalk/afp.h>
47 #include <atalk/uam.h>
54 #endif /* USE_CRACKLIB */
58 #endif /* __inline__ */
62 static C_Block seskey;
63 static Key_schedule seskeysched;
64 static struct passwd *randpwd;
65 static u_int8_t randbuf[8];
67 /* hash to a 16-bit number. this will generate completely harmless
68 * warnings on 64-bit machines. */
69 #define randhash(a) (((((unsigned long) a) >> 8) ^ \
70 ((unsigned long)a)) & 0xffff)
73 /* handle ~/.passwd. courtesy of shirsch@ibm.net. */
74 static __inline__ int home_passwd(const struct passwd *pwd,
75 const char *path, const int pathlen,
76 char *passwd, const int len,
82 if ( (fd = open(path, (set) ? O_WRONLY : O_RDONLY)) < 0 ) {
83 LOG(log_error, logtype_uams, "Failed to open %s", path);
87 if ( fstat( fd, &st ) < 0 )
88 goto home_passwd_fail;
90 /* If any of these are true, disallow login:
91 * - not a regular file
92 * - gid or uid don't match user
93 * - anyone else has permissions of any sort
95 if (!S_ISREG(st.st_mode) || (pwd->pw_uid != st.st_uid) ||
96 (pwd->pw_gid != st.st_gid) ||
97 (st.st_mode & ( S_IRWXG | S_IRWXO )) ) {
98 LOG(log_info, logtype_uams, "Insecure permissions found for %s.", path);
99 goto home_passwd_fail;
102 /* get the password */
104 if (write(fd, passwd, len) < 0) {
105 LOG(log_error, logtype_uams, "Failed to write to %s", path );
106 goto home_passwd_fail;
109 if (read(fd, passwd, len) < 0) {
110 LOG(log_error, logtype_uams, "Failed to read from %s", path );
111 goto home_passwd_fail;
114 /* get rid of pesky characters */
115 for (i = 0; i < len; i++)
116 if ((passwd[i] != ' ') && isspace(passwd[i]))
125 return AFPERR_ACCESS;
131 * handle /path/afppasswd with an optional key file. we're a lot more
132 * trusting of this file. NOTE: we use our own password entry writing
133 * bits as we want to avoid tromping over global variables. in addition,
134 * we look for a key file and use that if it's there. here are the
137 * username:password:last login date:failedcount
139 * password is just the hex equivalent of either the ASCII password
140 * (if the key file doesn't exist) or the des encrypted password.
144 #define PASSWD_ILLEGAL '*'
145 #define unhex(x) (isdigit(x) ? (x) - '0' : toupper(x) + 10 - 'A')
146 static int afppasswd(const struct passwd *pwd,
147 const char *path, const int pathlen,
148 char *passwd, int len,
151 u_int8_t key[DES_KEY_SZ*2];
152 char buf[MAXPATHLEN + 1], *p;
153 Key_schedule schedule;
155 int i, j, keyfd = -1, err = 0;
158 if ((fp = fopen(path, (set) ? "r+" : "r")) == NULL) {
159 LOG(log_error, logtype_uams, "Failed to open %s", path);
160 return AFPERR_ACCESS;
163 /* open the key file if it exists */
165 if (pathlen < sizeof(buf) - 5) {
167 keyfd = open(buf, O_RDONLY);
171 memset(buf, 0, sizeof(buf));
172 while (fgets(buf, sizeof(buf), fp)) {
173 if ((p = strchr(buf, ':'))) {
174 if (strncmp(buf, pwd->pw_name, p - buf) == 0) {
176 if (*p == PASSWD_ILLEGAL) {
177 LOG(log_info, logtype_uams, "invalid password entry for %s", pwd->pw_name);
181 goto afppasswd_found;
185 memset(buf, 0, sizeof(buf));
192 /* convert to binary. */
193 for (i = j = 0; i < sizeof(key); i += 2, j++)
194 p[j] = (unhex(p[i]) << 4) | unhex(p[i + 1]);
196 memset(p + j, 0, sizeof(key) - j);
200 /* read in the hex representation of an 8-byte key */
201 read(keyfd, key, sizeof(key));
203 /* convert to binary key */
204 for (i = j = 0; i < strlen((char *) key); i += 2, j++)
205 key[j] = (unhex(key[i]) << 4) | unhex(key[i + 1]);
207 memset(key + j, 0, sizeof(key) - j);
208 key_sched((C_Block *) key, schedule);
209 memset(key, 0, sizeof(key));
212 /* NOTE: this takes advantage of the fact that passwd doesn't
213 * get used after this call if it's being set. */
214 ecb_encrypt((C_Block *) passwd, (C_Block *) passwd, schedule,
217 /* decrypt the password */
218 ecb_encrypt((C_Block *) p, (C_Block *) p, schedule, DES_DECRYPT);
220 memset(schedule, 0, sizeof(schedule));
224 const unsigned char hextable[] = "0123456789ABCDEF";
228 /* convert to hex password */
229 for (i = j = 0; i < DES_KEY_SZ; i++, j += 2) {
230 key[j] = hextable[(passwd[i] & 0xF0) >> 4];
231 key[j + 1] = hextable[passwd[i] & 0x0F];
233 memcpy(p, key, sizeof(key));
235 /* get exclusive access to the user's password entry. we don't
236 * worry so much on reads. in the worse possible case there, the
237 * user will just need to re-enter their password. */
238 lock.l_type = F_WRLCK;
241 lock.l_whence = SEEK_SET;
243 fseek(fp, pos, SEEK_SET);
244 fcntl(fd, F_SETLKW, &lock);
245 fwrite(buf, p - buf + sizeof(key), 1, fp);
246 lock.l_type = F_UNLCK;
247 fcntl(fd, F_SETLK, &lock);
249 memcpy(passwd, p, len);
251 memset(buf, 0, sizeof(buf));
261 /* this sets the uid. it needs to do slightly different things
262 * depending upon whether or not the password is in ~/.passwd
263 * or in a global location */
264 static int randpass(const struct passwd *pwd, const char *file,
265 char *passwd, const int len, const int set)
268 uid_t uid = geteuid();
270 /* Build pathname to user's '.passwd' file */
273 char path[MAXPATHLEN + 1];
275 if ( (strlen(pwd->pw_dir) + i - 1) > MAXPATHLEN)
278 strcpy(path, pwd->pw_dir );
280 strcat(path, file + 2);
282 seteuid(pwd->pw_uid); /* change ourselves to the user */
283 i = home_passwd(pwd, path, i, passwd, len, set);
285 seteuid(0); /* change ourselves back to root */
292 /* handle afppasswd file. we need to make sure that we're root
293 * when we do this. */
296 i = afppasswd(pwd, file, i, passwd, len, set);
303 /* randnum sends an 8-byte number and uses the user's password to
304 * check against the encrypted reply. */
305 static int randnum_login(void *obj, struct passwd **uam_pwd,
306 char *ibuf, int ibuflen,
307 char *rbuf, int *rbuflen)
309 char *username, *passwdfile;
315 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
316 (void *) &username, &ulen) < 0)
319 len = UAM_PASSWD_FILENAME;
320 if (uam_afpserver_option(obj, UAM_OPTION_PASSWDOPT,
321 (void *) &passwdfile, &len) < 0)
324 len = (unsigned char) *ibuf++;
328 memcpy(username, ibuf, len );
330 username[ len ] = '\0';
331 if ((unsigned long) ibuf & 1) /* padding */
334 if (( randpwd = uam_getname(username, ulen)) == NULL )
335 return AFPERR_PARAM; /* unknown user */
337 LOG(log_info, logtype_uams, "randnum/rand2num login: %s", username);
338 if (uam_checkuser(randpwd) < 0)
339 return AFPERR_NOTAUTH;
341 if ((err = randpass(randpwd, passwdfile, seskey,
342 sizeof(seskey), 0)) != AFP_OK)
345 /* get a random number */
346 len = sizeof(randbuf);
347 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM,
348 (void *) randbuf, &len) < 0)
351 /* session id is a hashed version of the obj pointer */
352 sessid = randhash(obj);
353 memcpy(rbuf, &sessid, sizeof(sessid));
354 rbuf += sizeof(sessid);
355 *rbuflen = sizeof(sessid);
357 /* send the random number off */
358 memcpy(rbuf, randbuf, sizeof(randbuf));
359 *rbuflen += sizeof(randbuf);
360 return AFPERR_AUTHCONT;
364 /* check encrypted reply. we actually setup the encryption stuff
365 * here as the first part of randnum and rand2num are identical. */
366 static int randnum_logincont(void *obj, struct passwd **uam_pwd,
367 char *ibuf, int ibuflen,
368 char *rbuf, int *rbuflen)
374 memcpy(&sessid, ibuf, sizeof(sessid));
375 if (sessid != randhash(obj))
378 ibuf += sizeof(sessid);
380 /* encrypt. this saves a little space by using the fact that
381 * des can encrypt in-place without side-effects. */
382 key_sched((C_Block *) seskey, seskeysched);
383 memset(seskey, 0, sizeof(seskey));
384 ecb_encrypt((C_Block *) randbuf, (C_Block *) randbuf,
385 seskeysched, DES_ENCRYPT);
386 memset(seskeysched, 0, sizeof(seskeysched));
388 /* test against what the client sent */
389 if (memcmp( randbuf, ibuf, sizeof(randbuf) )) { /* != */
390 memset(randbuf, 0, sizeof(randbuf));
391 return AFPERR_NOTAUTH;
394 memset(randbuf, 0, sizeof(randbuf));
400 /* differences from randnum:
401 * 1) each byte of the key is shifted left one bit
402 * 2) client sends the server a 64-bit number. the server encrypts it
403 * and sends it back as part of the reply.
405 static int rand2num_logincont(void *obj, struct passwd **uam_pwd,
406 char *ibuf, int ibuflen,
407 char *rbuf, int *rbuflen)
414 /* compare session id */
415 memcpy(&sessid, ibuf, sizeof(sessid));
416 if (sessid != randhash(obj))
419 ibuf += sizeof(sessid);
421 /* shift key elements left one bit */
422 for (i = 0; i < sizeof(seskey); i++)
425 /* encrypt randbuf */
426 key_sched((C_Block *) seskey, seskeysched);
427 memset(seskey, 0, sizeof(seskey));
428 ecb_encrypt( (C_Block *) randbuf, (C_Block *) randbuf,
429 seskeysched, DES_ENCRYPT);
431 /* test against client's reply */
432 if (memcmp(randbuf, ibuf, sizeof(randbuf))) { /* != */
433 memset(randbuf, 0, sizeof(randbuf));
434 memset(seskeysched, 0, sizeof(seskeysched));
435 return AFPERR_NOTAUTH;
437 ibuf += sizeof(randbuf);
438 memset(randbuf, 0, sizeof(randbuf));
440 /* encrypt client's challenge and send back */
441 ecb_encrypt( (C_Block *) ibuf, (C_Block *) rbuf,
442 seskeysched, DES_ENCRYPT);
443 memset(seskeysched, 0, sizeof(seskeysched));
444 *rbuflen = sizeof(randbuf);
450 /* change password --
451 * NOTE: an FPLogin must already have completed successfully for this
454 static int randnum_changepw(void *obj, const char *username,
455 struct passwd *pwd, char *ibuf,
456 int ibuflen, char *rbuf, int *rbuflen)
461 if (uam_checkuser(pwd) < 0)
462 return AFPERR_ACCESS;
464 len = UAM_PASSWD_FILENAME;
465 if (uam_afpserver_option(obj, UAM_OPTION_PASSWDOPT,
466 (void *) &passwdfile, &len) < 0)
469 /* old password is encrypted with new password and new password is
470 * encrypted with old. */
471 if ((err = randpass(pwd, passwdfile, seskey,
472 sizeof(seskey), 0)) != AFP_OK)
475 /* use old passwd to decrypt new passwd */
476 key_sched((C_Block *) seskey, seskeysched);
477 ibuf += PASSWDLEN; /* new passwd */
478 ibuf[PASSWDLEN] = '\0';
479 ecb_encrypt( (C_Block *) ibuf, (C_Block *) ibuf, seskeysched, DES_DECRYPT);
481 /* now use new passwd to decrypt old passwd */
482 key_sched((C_Block *) ibuf, seskeysched);
483 ibuf -= PASSWDLEN; /* old passwd */
484 ecb_encrypt((C_Block *) ibuf, (C_Block *) ibuf, seskeysched, DES_DECRYPT);
485 if (memcmp(seskey, ibuf, sizeof(seskey)))
486 err = AFPERR_NOTAUTH;
487 else if (memcmp(seskey, ibuf + PASSWDLEN, sizeof(seskey)) == 0)
488 err = AFPERR_PWDSAME;
490 else if (FascistCheck(ibuf + PASSWDLEN, _PATH_CRACKLIB))
491 err = AFPERR_PWDPOLCY;
492 #endif /* USE_CRACKLIB */
495 err = randpass(pwd, passwdfile, ibuf + PASSWDLEN, sizeof(seskey), 1);
497 /* zero out some fields */
498 memset(seskeysched, 0, sizeof(seskeysched));
499 memset(seskey, 0, sizeof(seskey));
500 memset(ibuf, 0, sizeof(seskey)); /* old passwd */
501 memset(ibuf + PASSWDLEN, 0, sizeof(seskey)); /* new passwd */
509 static int uam_setup(const char *path)
511 if (uam_register(UAM_SERVER_LOGIN, path, "Randnum exchange",
512 randnum_login, randnum_logincont, NULL) < 0)
514 if (uam_register(UAM_SERVER_LOGIN, path, "2-Way Randnum exchange",
515 randnum_login, rand2num_logincont, NULL) < 0) {
516 uam_unregister(UAM_SERVER_LOGIN, "Randnum exchange");
520 if (uam_register(UAM_SERVER_CHANGEPW, path, "Randnum Exchange",
521 randnum_changepw) < 0) {
522 uam_unregister(UAM_SERVER_LOGIN, "Randnum exchange");
523 uam_unregister(UAM_SERVER_LOGIN, "2-Way Randnum exchange");
526 /*uam_register(UAM_SERVER_PRINTAUTH, path, "Randnum Exchange",
532 static void uam_cleanup(void)
534 uam_unregister(UAM_SERVER_LOGIN, "Randnum exchange");
535 uam_unregister(UAM_SERVER_LOGIN, "2-Way Randnum exchange");
536 uam_unregister(UAM_SERVER_CHANGEPW, "Randnum Exchange");
537 /*uam_unregister(UAM_SERVER_PRINTAUTH, "Randnum Exchange");*/
540 UAM_MODULE_EXPORT struct uam_export uams_randnum = {
543 uam_setup, uam_cleanup