2 * $Id: uams_randnum.c,v 1.4 2001-02-27 17:07:43 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.
21 #include <sys/param.h>
25 #include <netatalk/endian.h>
27 #include <atalk/afp.h>
28 #include <atalk/uam.h>
44 static C_Block seskey;
45 static Key_schedule seskeysched;
46 static struct passwd *randpwd;
47 static u_int8_t randbuf[8];
49 /* hash to a 16-bit number. this will generate completely harmless
50 * warnings on 64-bit machines. */
51 #define randhash(a) (((((unsigned long) a) >> 8) ^ \
52 ((unsigned long)a)) & 0xffff)
55 /* handle ~/.passwd. courtesy of shirsch@ibm.net. */
56 static __inline__ int home_passwd(const struct passwd *pwd,
57 const char *path, const int pathlen,
58 char *passwd, const int len,
64 if ( (fd = open(path, (set) ? O_WRONLY : O_RDONLY)) < 0 ) {
65 syslog( LOG_ERR, "Failed to open %s", path);
69 if ( fstat( fd, &st ) < 0 )
70 goto home_passwd_fail;
72 /* If any of these are true, disallow login:
73 * - not a regular file
74 * - gid or uid don't match user
75 * - anyone else has permissions of any sort
77 if (!S_ISREG(st.st_mode) || (pwd->pw_uid != st.st_uid) ||
78 (pwd->pw_gid != st.st_gid) ||
79 (st.st_mode & ( S_IRWXG | S_IRWXO )) ) {
80 syslog( LOG_INFO, "Insecure permissions found for %s.", path);
81 goto home_passwd_fail;
84 /* get the password */
86 if (write(fd, passwd, len) < 0) {
87 syslog( LOG_ERR, "Failed to write to %s", path );
88 goto home_passwd_fail;
91 if (read(fd, passwd, len) < 0) {
92 syslog( LOG_ERR, "Failed to read from %s", path );
93 goto home_passwd_fail;
96 /* get rid of pesky characters */
97 for (i = 0; i < len; i++)
98 if ((passwd[i] != ' ') && isspace(passwd[i]))
107 return AFPERR_ACCESS;
113 * handle /path/afppasswd with an optional key file. we're a lot more
114 * trusting of this file. NOTE: we use our own password entry writing
115 * bits as we want to avoid tromping over global variables. in addition,
116 * we look for a key file and use that if it's there. here are the
119 * username:password:last login date:failedcount
121 * password is just the hex equivalent of either the ASCII password
122 * (if the key file doesn't exist) or the des encrypted password.
126 #define PASSWD_ILLEGAL '*'
127 #define unhex(x) (isdigit(x) ? (x) - '0' : toupper(x) + 10 - 'A')
128 static int afppasswd(const struct passwd *pwd,
129 const char *path, const int pathlen,
130 char *passwd, int len,
133 u_int8_t key[DES_KEY_SZ*2];
134 char buf[MAXPATHLEN + 1], *p;
135 Key_schedule schedule;
137 int i, j, keyfd = -1, err = 0;
140 if ((fp = fopen(path, (set) ? "r+" : "r")) <= 0) {
141 syslog( LOG_ERR, "Failed to open %s", path);
142 return AFPERR_ACCESS;
145 /* open the key file if it exists */
147 if (pathlen < sizeof(buf) - 5) {
149 keyfd = open(buf, O_RDONLY);
153 memset(buf, 0, sizeof(buf));
154 while (fgets(buf, sizeof(buf), fp)) {
155 if ((p = strchr(buf, ':'))) {
156 if (strncmp(buf, pwd->pw_name, p - buf) == 0) {
158 if (*p == PASSWD_ILLEGAL) {
159 syslog(LOG_INFO, "invalid password entry for %s", pwd->pw_name);
163 goto afppasswd_found;
167 memset(buf, 0, sizeof(buf));
174 /* convert to binary. */
175 for (i = j = 0; i < sizeof(key); i += 2, j++)
176 p[j] = (unhex(p[i]) << 4) | unhex(p[i + 1]);
178 memset(p + j, 0, sizeof(key) - j);
182 /* read in the hex representation of an 8-byte key */
183 read(keyfd, key, sizeof(key));
185 /* convert to binary key */
186 for (i = j = 0; i < strlen(key); i += 2, j++)
187 key[j] = (unhex(key[i]) << 4) | unhex(key[i + 1]);
189 memset(key + j, 0, sizeof(key) - j);
190 key_sched((C_Block *) key, schedule);
191 memset(key, 0, sizeof(key));
194 /* NOTE: this takes advantage of the fact that passwd doesn't
195 * get used after this call if it's being set. */
196 ecb_encrypt((C_Block *) passwd, (C_Block *) passwd, schedule,
199 /* decrypt the password */
200 ecb_encrypt((C_Block *) p, (C_Block *) p, schedule, DES_DECRYPT);
202 memset(schedule, 0, sizeof(schedule));
206 const unsigned char hextable[] = "0123456789ABCDEF";
210 /* convert to hex password */
211 for (i = j = 0; i < DES_KEY_SZ; i++, j += 2) {
212 key[j] = hextable[(passwd[i] & 0xF0) >> 4];
213 key[j + 1] = hextable[passwd[i] & 0x0F];
215 memcpy(p, key, sizeof(key));
217 /* get exclusive access to the user's password entry. we don't
218 * worry so much on reads. in the worse possible case there, the
219 * user will just need to re-enter their password. */
220 lock.l_type = F_WRLCK;
223 lock.l_whence = SEEK_SET;
225 fseek(fp, pos, SEEK_SET);
226 fcntl(fd, F_SETLKW, &lock);
227 fwrite(buf, p - buf + sizeof(key), 1, fp);
228 lock.l_type = F_UNLCK;
229 fcntl(fd, F_SETLK, &lock);
231 memcpy(passwd, p, len);
233 memset(buf, 0, sizeof(buf));
243 /* this sets the uid. it needs to do slightly different things
244 * depending upon whether or not the password is in ~/.passwd
245 * or in a global location */
246 static int randpass(const struct passwd *pwd, const char *file,
247 char *passwd, const int len, const int set)
250 uid_t uid = geteuid();
252 /* Build pathname to user's '.passwd' file */
255 char path[MAXPATHLEN + 1];
257 if ( (strlen(pwd->pw_dir) + i - 1) > MAXPATHLEN)
260 strcpy(path, pwd->pw_dir );
262 strcat(path, file + 2);
264 seteuid(pwd->pw_uid); /* change ourselves to the user */
265 i = home_passwd(pwd, path, i, passwd, len, set);
267 seteuid(0); /* change ourselves back to root */
274 /* handle afppasswd file. we need to make sure that we're root
275 * when we do this. */
278 i = afppasswd(pwd, file, i, passwd, len, set);
285 /* randnum sends an 8-byte number and uses the user's password to
286 * check against the encrypted reply. */
287 static int randnum_login(void *obj, struct passwd **uam_pwd,
288 char *ibuf, int ibuflen,
289 char *rbuf, int *rbuflen)
291 char *username, *passwdfile;
297 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
298 (void *) &username, &ulen) < 0)
301 len = UAM_PASSWD_FILENAME;
302 if (uam_afpserver_option(obj, UAM_OPTION_PASSWDOPT,
303 (void *) &passwdfile, &len) < 0)
306 len = (unsigned char) *ibuf++;
310 memcpy(username, ibuf, len );
312 username[ len ] = '\0';
313 if ((unsigned long) ibuf & 1) /* padding */
316 if (( randpwd = uam_getname(username, ulen)) == NULL )
317 return AFPERR_PARAM; /* unknown user */
319 syslog( LOG_INFO, "randnum/rand2num login: %s", username);
320 if (uam_checkuser(randpwd) < 0)
321 return AFPERR_NOTAUTH;
323 if ((err = randpass(randpwd, passwdfile, seskey,
324 sizeof(seskey), 0)) != AFP_OK)
327 /* get a random number */
328 len = sizeof(randbuf);
329 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM,
330 (void *) randbuf, &len) < 0)
333 /* session id is a hashed version of the obj pointer */
334 sessid = randhash(obj);
335 memcpy(rbuf, &sessid, sizeof(sessid));
336 rbuf += sizeof(sessid);
337 *rbuflen = sizeof(sessid);
339 /* send the random number off */
340 memcpy(rbuf, randbuf, sizeof(randbuf));
341 *rbuflen += sizeof(randbuf);
342 return AFPERR_AUTHCONT;
346 /* check encrypted reply. we actually setup the encryption stuff
347 * here as the first part of randnum and rand2num are identical. */
348 static int randnum_logincont(void *obj, struct passwd **uam_pwd,
349 char *ibuf, int ibuflen,
350 char *rbuf, int *rbuflen)
356 memcpy(&sessid, ibuf, sizeof(sessid));
357 if (sessid != randhash(obj))
360 ibuf += sizeof(sessid);
362 /* encrypt. this saves a little space by using the fact that
363 * des can encrypt in-place without side-effects. */
364 key_sched((C_Block *) seskey, seskeysched);
365 memset(seskey, 0, sizeof(seskey));
366 ecb_encrypt((C_Block *) randbuf, (C_Block *) randbuf,
367 seskeysched, DES_ENCRYPT);
368 memset(seskeysched, 0, sizeof(seskeysched));
370 /* test against what the client sent */
371 if (memcmp( randbuf, ibuf, sizeof(randbuf) )) { /* != */
372 memset(randbuf, 0, sizeof(randbuf));
373 return AFPERR_NOTAUTH;
376 memset(randbuf, 0, sizeof(randbuf));
382 /* differences from randnum:
383 * 1) each byte of the key is shifted left one bit
384 * 2) client sends the server a 64-bit number. the server encrypts it
385 * and sends it back as part of the reply.
387 static int rand2num_logincont(void *obj, struct passwd **uam_pwd,
388 char *ibuf, int ibuflen,
389 char *rbuf, int *rbuflen)
396 /* compare session id */
397 memcpy(&sessid, ibuf, sizeof(sessid));
398 if (sessid != randhash(obj))
401 ibuf += sizeof(sessid);
403 /* shift key elements left one bit */
404 for (i = 0; i < sizeof(seskey); i++)
407 /* encrypt randbuf */
408 key_sched((C_Block *) seskey, seskeysched);
409 memset(seskey, 0, sizeof(seskey));
410 ecb_encrypt( (C_Block *) randbuf, (C_Block *) randbuf,
411 seskeysched, DES_ENCRYPT);
413 /* test against client's reply */
414 if (memcmp(randbuf, ibuf, sizeof(randbuf))) { /* != */
415 memset(randbuf, 0, sizeof(randbuf));
416 memset(seskeysched, 0, sizeof(seskeysched));
417 return AFPERR_NOTAUTH;
419 ibuf += sizeof(randbuf);
420 memset(randbuf, 0, sizeof(randbuf));
422 /* encrypt client's challenge and send back */
423 ecb_encrypt( (C_Block *) ibuf, (C_Block *) rbuf,
424 seskeysched, DES_ENCRYPT);
425 memset(seskeysched, 0, sizeof(seskeysched));
426 *rbuflen = sizeof(randbuf);
432 /* change password --
433 * NOTE: an FPLogin must already have completed successfully for this
436 static int randnum_changepw(void *obj, const char *username,
437 struct passwd *pwd, char *ibuf,
438 int ibuflen, char *rbuf, int *rbuflen)
443 if (uam_checkuser(pwd) < 0)
444 return AFPERR_ACCESS;
446 len = UAM_PASSWD_FILENAME;
447 if (uam_afpserver_option(obj, UAM_OPTION_PASSWDOPT,
448 (void *) &passwdfile, &len) < 0)
451 /* old password is encrypted with new password and new password is
452 * encrypted with old. */
453 if ((err = randpass(pwd, passwdfile, seskey,
454 sizeof(seskey), 0)) != AFP_OK)
457 /* use old passwd to decrypt new passwd */
458 key_sched((C_Block *) seskey, seskeysched);
459 ibuf += PASSWDLEN; /* new passwd */
460 ibuf[PASSWDLEN] = '\0';
461 ecb_encrypt( (C_Block *) ibuf, (C_Block *) ibuf, seskeysched, DES_DECRYPT);
463 /* now use new passwd to decrypt old passwd */
464 key_sched((C_Block *) ibuf, seskeysched);
465 ibuf -= PASSWDLEN; /* old passwd */
466 ecb_encrypt((C_Block *) ibuf, (C_Block *) ibuf, seskeysched, DES_DECRYPT);
467 if (memcmp(seskey, ibuf, sizeof(seskey)))
468 err = AFPERR_NOTAUTH;
469 else if (memcmp(seskey, ibuf + PASSWDLEN, sizeof(seskey)) == 0)
470 err = AFPERR_PWDSAME;
472 else if (FascistCheck(ibuf + PASSWDLEN, _PATH_CRACKLIB))
473 err = AFPERR_PWDPOLCY;
477 err = randpass(pwd, passwdfile, ibuf + PASSWDLEN, sizeof(seskey), 1);
479 /* zero out some fields */
480 memset(seskeysched, 0, sizeof(seskeysched));
481 memset(seskey, 0, sizeof(seskey));
482 memset(ibuf, 0, sizeof(seskey)); /* old passwd */
483 memset(ibuf + PASSWDLEN, 0, sizeof(seskey)); /* new passwd */
491 static int uam_setup(const char *path)
493 if (uam_register(UAM_SERVER_LOGIN, path, "Randnum exchange",
494 randnum_login, randnum_logincont, NULL) < 0)
496 if (uam_register(UAM_SERVER_LOGIN, path, "2-Way Randnum exchange",
497 randnum_login, rand2num_logincont, NULL) < 0) {
498 uam_unregister(UAM_SERVER_LOGIN, "Randnum exchange");
502 if (uam_register(UAM_SERVER_CHANGEPW, path, "Randnum Exchange",
503 randnum_changepw) < 0) {
504 uam_unregister(UAM_SERVER_LOGIN, "Randnum exchange");
505 uam_unregister(UAM_SERVER_LOGIN, "2-Way Randnum exchange");
508 /*uam_register(UAM_SERVER_PRINTAUTH, path, "Randnum Exchange",
514 static void uam_cleanup(void)
516 uam_unregister(UAM_SERVER_LOGIN, "Randnum exchange");
517 uam_unregister(UAM_SERVER_LOGIN, "2-Way Randnum exchange");
518 uam_unregister(UAM_SERVER_CHANGEPW, "Randnum Exchange");
519 /*uam_unregister(UAM_SERVER_PRINTAUTH, "Randnum Exchange");*/
522 UAM_MODULE_EXPORT struct uam_export uams_randnum = {
525 uam_setup, uam_cleanup
528 #endif /* UAM_RNDNUM */