2 * $Id: uams_randnum.c,v 1.6 2001-09-06 20:00:59 rufustfirefly 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>
44 #include <netatalk/endian.h>
46 #include <atalk/afp.h>
47 #include <atalk/uam.h>
55 #endif /* USE_CRACKLIB */
59 #endif /* __inline__ */
63 static C_Block seskey;
64 static Key_schedule seskeysched;
65 static struct passwd *randpwd;
66 static u_int8_t randbuf[8];
68 /* hash to a 16-bit number. this will generate completely harmless
69 * warnings on 64-bit machines. */
70 #define randhash(a) (((((unsigned long) a) >> 8) ^ \
71 ((unsigned long)a)) & 0xffff)
74 /* handle ~/.passwd. courtesy of shirsch@ibm.net. */
75 static __inline__ int home_passwd(const struct passwd *pwd,
76 const char *path, const int pathlen,
77 char *passwd, const int len,
83 if ( (fd = open(path, (set) ? O_WRONLY : O_RDONLY)) < 0 ) {
84 syslog( LOG_ERR, "Failed to open %s", path);
88 if ( fstat( fd, &st ) < 0 )
89 goto home_passwd_fail;
91 /* If any of these are true, disallow login:
92 * - not a regular file
93 * - gid or uid don't match user
94 * - anyone else has permissions of any sort
96 if (!S_ISREG(st.st_mode) || (pwd->pw_uid != st.st_uid) ||
97 (pwd->pw_gid != st.st_gid) ||
98 (st.st_mode & ( S_IRWXG | S_IRWXO )) ) {
99 syslog( LOG_INFO, "Insecure permissions found for %s.", path);
100 goto home_passwd_fail;
103 /* get the password */
105 if (write(fd, passwd, len) < 0) {
106 syslog( LOG_ERR, "Failed to write to %s", path );
107 goto home_passwd_fail;
110 if (read(fd, passwd, len) < 0) {
111 syslog( LOG_ERR, "Failed to read from %s", path );
112 goto home_passwd_fail;
115 /* get rid of pesky characters */
116 for (i = 0; i < len; i++)
117 if ((passwd[i] != ' ') && isspace(passwd[i]))
126 return AFPERR_ACCESS;
132 * handle /path/afppasswd with an optional key file. we're a lot more
133 * trusting of this file. NOTE: we use our own password entry writing
134 * bits as we want to avoid tromping over global variables. in addition,
135 * we look for a key file and use that if it's there. here are the
138 * username:password:last login date:failedcount
140 * password is just the hex equivalent of either the ASCII password
141 * (if the key file doesn't exist) or the des encrypted password.
145 #define PASSWD_ILLEGAL '*'
146 #define unhex(x) (isdigit(x) ? (x) - '0' : toupper(x) + 10 - 'A')
147 static int afppasswd(const struct passwd *pwd,
148 const char *path, const int pathlen,
149 char *passwd, int len,
152 u_int8_t key[DES_KEY_SZ*2];
153 char buf[MAXPATHLEN + 1], *p;
154 Key_schedule schedule;
156 int i, j, keyfd = -1, err = 0;
159 if ((fp = fopen(path, (set) ? "r+" : "r")) <= 0) {
160 syslog( LOG_ERR, "Failed to open %s", path);
161 return AFPERR_ACCESS;
164 /* open the key file if it exists */
166 if (pathlen < sizeof(buf) - 5) {
168 keyfd = open(buf, O_RDONLY);
172 memset(buf, 0, sizeof(buf));
173 while (fgets(buf, sizeof(buf), fp)) {
174 if ((p = strchr(buf, ':'))) {
175 if (strncmp(buf, pwd->pw_name, p - buf) == 0) {
177 if (*p == PASSWD_ILLEGAL) {
178 syslog(LOG_INFO, "invalid password entry for %s", pwd->pw_name);
182 goto afppasswd_found;
186 memset(buf, 0, sizeof(buf));
193 /* convert to binary. */
194 for (i = j = 0; i < sizeof(key); i += 2, j++)
195 p[j] = (unhex(p[i]) << 4) | unhex(p[i + 1]);
197 memset(p + j, 0, sizeof(key) - j);
201 /* read in the hex representation of an 8-byte key */
202 read(keyfd, key, sizeof(key));
204 /* convert to binary key */
205 for (i = j = 0; i < strlen(key); i += 2, j++)
206 key[j] = (unhex(key[i]) << 4) | unhex(key[i + 1]);
208 memset(key + j, 0, sizeof(key) - j);
209 key_sched((C_Block *) key, schedule);
210 memset(key, 0, sizeof(key));
213 /* NOTE: this takes advantage of the fact that passwd doesn't
214 * get used after this call if it's being set. */
215 ecb_encrypt((C_Block *) passwd, (C_Block *) passwd, schedule,
218 /* decrypt the password */
219 ecb_encrypt((C_Block *) p, (C_Block *) p, schedule, DES_DECRYPT);
221 memset(schedule, 0, sizeof(schedule));
225 const unsigned char hextable[] = "0123456789ABCDEF";
229 /* convert to hex password */
230 for (i = j = 0; i < DES_KEY_SZ; i++, j += 2) {
231 key[j] = hextable[(passwd[i] & 0xF0) >> 4];
232 key[j + 1] = hextable[passwd[i] & 0x0F];
234 memcpy(p, key, sizeof(key));
236 /* get exclusive access to the user's password entry. we don't
237 * worry so much on reads. in the worse possible case there, the
238 * user will just need to re-enter their password. */
239 lock.l_type = F_WRLCK;
242 lock.l_whence = SEEK_SET;
244 fseek(fp, pos, SEEK_SET);
245 fcntl(fd, F_SETLKW, &lock);
246 fwrite(buf, p - buf + sizeof(key), 1, fp);
247 lock.l_type = F_UNLCK;
248 fcntl(fd, F_SETLK, &lock);
250 memcpy(passwd, p, len);
252 memset(buf, 0, sizeof(buf));
262 /* this sets the uid. it needs to do slightly different things
263 * depending upon whether or not the password is in ~/.passwd
264 * or in a global location */
265 static int randpass(const struct passwd *pwd, const char *file,
266 char *passwd, const int len, const int set)
269 uid_t uid = geteuid();
271 /* Build pathname to user's '.passwd' file */
274 char path[MAXPATHLEN + 1];
276 if ( (strlen(pwd->pw_dir) + i - 1) > MAXPATHLEN)
279 strcpy(path, pwd->pw_dir );
281 strcat(path, file + 2);
283 seteuid(pwd->pw_uid); /* change ourselves to the user */
284 i = home_passwd(pwd, path, i, passwd, len, set);
286 seteuid(0); /* change ourselves back to root */
293 /* handle afppasswd file. we need to make sure that we're root
294 * when we do this. */
297 i = afppasswd(pwd, file, i, passwd, len, set);
304 /* randnum sends an 8-byte number and uses the user's password to
305 * check against the encrypted reply. */
306 static int randnum_login(void *obj, struct passwd **uam_pwd,
307 char *ibuf, int ibuflen,
308 char *rbuf, int *rbuflen)
310 char *username, *passwdfile;
316 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
317 (void *) &username, &ulen) < 0)
320 len = UAM_PASSWD_FILENAME;
321 if (uam_afpserver_option(obj, UAM_OPTION_PASSWDOPT,
322 (void *) &passwdfile, &len) < 0)
325 len = (unsigned char) *ibuf++;
329 memcpy(username, ibuf, len );
331 username[ len ] = '\0';
332 if ((unsigned long) ibuf & 1) /* padding */
335 if (( randpwd = uam_getname(username, ulen)) == NULL )
336 return AFPERR_PARAM; /* unknown user */
338 syslog( LOG_INFO, "randnum/rand2num login: %s", username);
339 if (uam_checkuser(randpwd) < 0)
340 return AFPERR_NOTAUTH;
342 if ((err = randpass(randpwd, passwdfile, seskey,
343 sizeof(seskey), 0)) != AFP_OK)
346 /* get a random number */
347 len = sizeof(randbuf);
348 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM,
349 (void *) randbuf, &len) < 0)
352 /* session id is a hashed version of the obj pointer */
353 sessid = randhash(obj);
354 memcpy(rbuf, &sessid, sizeof(sessid));
355 rbuf += sizeof(sessid);
356 *rbuflen = sizeof(sessid);
358 /* send the random number off */
359 memcpy(rbuf, randbuf, sizeof(randbuf));
360 *rbuflen += sizeof(randbuf);
361 return AFPERR_AUTHCONT;
365 /* check encrypted reply. we actually setup the encryption stuff
366 * here as the first part of randnum and rand2num are identical. */
367 static int randnum_logincont(void *obj, struct passwd **uam_pwd,
368 char *ibuf, int ibuflen,
369 char *rbuf, int *rbuflen)
375 memcpy(&sessid, ibuf, sizeof(sessid));
376 if (sessid != randhash(obj))
379 ibuf += sizeof(sessid);
381 /* encrypt. this saves a little space by using the fact that
382 * des can encrypt in-place without side-effects. */
383 key_sched((C_Block *) seskey, seskeysched);
384 memset(seskey, 0, sizeof(seskey));
385 ecb_encrypt((C_Block *) randbuf, (C_Block *) randbuf,
386 seskeysched, DES_ENCRYPT);
387 memset(seskeysched, 0, sizeof(seskeysched));
389 /* test against what the client sent */
390 if (memcmp( randbuf, ibuf, sizeof(randbuf) )) { /* != */
391 memset(randbuf, 0, sizeof(randbuf));
392 return AFPERR_NOTAUTH;
395 memset(randbuf, 0, sizeof(randbuf));
401 /* differences from randnum:
402 * 1) each byte of the key is shifted left one bit
403 * 2) client sends the server a 64-bit number. the server encrypts it
404 * and sends it back as part of the reply.
406 static int rand2num_logincont(void *obj, struct passwd **uam_pwd,
407 char *ibuf, int ibuflen,
408 char *rbuf, int *rbuflen)
415 /* compare session id */
416 memcpy(&sessid, ibuf, sizeof(sessid));
417 if (sessid != randhash(obj))
420 ibuf += sizeof(sessid);
422 /* shift key elements left one bit */
423 for (i = 0; i < sizeof(seskey); i++)
426 /* encrypt randbuf */
427 key_sched((C_Block *) seskey, seskeysched);
428 memset(seskey, 0, sizeof(seskey));
429 ecb_encrypt( (C_Block *) randbuf, (C_Block *) randbuf,
430 seskeysched, DES_ENCRYPT);
432 /* test against client's reply */
433 if (memcmp(randbuf, ibuf, sizeof(randbuf))) { /* != */
434 memset(randbuf, 0, sizeof(randbuf));
435 memset(seskeysched, 0, sizeof(seskeysched));
436 return AFPERR_NOTAUTH;
438 ibuf += sizeof(randbuf);
439 memset(randbuf, 0, sizeof(randbuf));
441 /* encrypt client's challenge and send back */
442 ecb_encrypt( (C_Block *) ibuf, (C_Block *) rbuf,
443 seskeysched, DES_ENCRYPT);
444 memset(seskeysched, 0, sizeof(seskeysched));
445 *rbuflen = sizeof(randbuf);
451 /* change password --
452 * NOTE: an FPLogin must already have completed successfully for this
455 static int randnum_changepw(void *obj, const char *username,
456 struct passwd *pwd, char *ibuf,
457 int ibuflen, char *rbuf, int *rbuflen)
462 if (uam_checkuser(pwd) < 0)
463 return AFPERR_ACCESS;
465 len = UAM_PASSWD_FILENAME;
466 if (uam_afpserver_option(obj, UAM_OPTION_PASSWDOPT,
467 (void *) &passwdfile, &len) < 0)
470 /* old password is encrypted with new password and new password is
471 * encrypted with old. */
472 if ((err = randpass(pwd, passwdfile, seskey,
473 sizeof(seskey), 0)) != AFP_OK)
476 /* use old passwd to decrypt new passwd */
477 key_sched((C_Block *) seskey, seskeysched);
478 ibuf += PASSWDLEN; /* new passwd */
479 ibuf[PASSWDLEN] = '\0';
480 ecb_encrypt( (C_Block *) ibuf, (C_Block *) ibuf, seskeysched, DES_DECRYPT);
482 /* now use new passwd to decrypt old passwd */
483 key_sched((C_Block *) ibuf, seskeysched);
484 ibuf -= PASSWDLEN; /* old passwd */
485 ecb_encrypt((C_Block *) ibuf, (C_Block *) ibuf, seskeysched, DES_DECRYPT);
486 if (memcmp(seskey, ibuf, sizeof(seskey)))
487 err = AFPERR_NOTAUTH;
488 else if (memcmp(seskey, ibuf + PASSWDLEN, sizeof(seskey)) == 0)
489 err = AFPERR_PWDSAME;
491 else if (FascistCheck(ibuf + PASSWDLEN, _PATH_CRACKLIB))
492 err = AFPERR_PWDPOLCY;
493 #endif /* USE_CRACKLIB */
496 err = randpass(pwd, passwdfile, ibuf + PASSWDLEN, sizeof(seskey), 1);
498 /* zero out some fields */
499 memset(seskeysched, 0, sizeof(seskeysched));
500 memset(seskey, 0, sizeof(seskey));
501 memset(ibuf, 0, sizeof(seskey)); /* old passwd */
502 memset(ibuf + PASSWDLEN, 0, sizeof(seskey)); /* new passwd */
510 static int uam_setup(const char *path)
512 if (uam_register(UAM_SERVER_LOGIN, path, "Randnum exchange",
513 randnum_login, randnum_logincont, NULL) < 0)
515 if (uam_register(UAM_SERVER_LOGIN, path, "2-Way Randnum exchange",
516 randnum_login, rand2num_logincont, NULL) < 0) {
517 uam_unregister(UAM_SERVER_LOGIN, "Randnum exchange");
521 if (uam_register(UAM_SERVER_CHANGEPW, path, "Randnum Exchange",
522 randnum_changepw) < 0) {
523 uam_unregister(UAM_SERVER_LOGIN, "Randnum exchange");
524 uam_unregister(UAM_SERVER_LOGIN, "2-Way Randnum exchange");
527 /*uam_register(UAM_SERVER_PRINTAUTH, path, "Randnum Exchange",
533 static void uam_cleanup(void)
535 uam_unregister(UAM_SERVER_LOGIN, "Randnum exchange");
536 uam_unregister(UAM_SERVER_LOGIN, "2-Way Randnum exchange");
537 uam_unregister(UAM_SERVER_CHANGEPW, "Randnum Exchange");
538 /*uam_unregister(UAM_SERVER_PRINTAUTH, "Randnum Exchange");*/
541 UAM_MODULE_EXPORT struct uam_export uams_randnum = {
544 uam_setup, uam_cleanup
547 #endif /* UAM_RNDNUM */