2 * $Id: uams_randnum.c,v 1.13 2003-06-11 06:29:30 srittau 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 */
60 #endif /* __inline__ */
64 static C_Block seskey;
65 static Key_schedule seskeysched;
66 static struct passwd *randpwd;
67 static u_int8_t randbuf[8];
69 /* hash to a 16-bit number. this will generate completely harmless
70 * warnings on 64-bit machines. */
71 #define randhash(a) (((((unsigned long) a) >> 8) ^ \
72 ((unsigned long)a)) & 0xffff)
75 /* handle ~/.passwd. courtesy of shirsch@ibm.net. */
76 static __inline__ int home_passwd(const struct passwd *pwd,
77 const char *path, const int pathlen,
78 char *passwd, const int len,
84 if ( (fd = open(path, (set) ? O_WRONLY : O_RDONLY)) < 0 ) {
85 LOG(log_error, logtype_uams, "Failed to open %s", path);
89 if ( fstat( fd, &st ) < 0 )
90 goto home_passwd_fail;
92 /* If any of these are true, disallow login:
93 * - not a regular file
94 * - gid or uid don't match user
95 * - anyone else has permissions of any sort
97 if (!S_ISREG(st.st_mode) || (pwd->pw_uid != st.st_uid) ||
98 (pwd->pw_gid != st.st_gid) ||
99 (st.st_mode & ( S_IRWXG | S_IRWXO )) ) {
100 LOG(log_info, logtype_uams, "Insecure permissions found for %s.", path);
101 goto home_passwd_fail;
104 /* get the password */
106 if (write(fd, passwd, len) < 0) {
107 LOG(log_error, logtype_uams, "Failed to write to %s", path );
108 goto home_passwd_fail;
111 if (read(fd, passwd, len) < 0) {
112 LOG(log_error, logtype_uams, "Failed to read from %s", path );
113 goto home_passwd_fail;
116 /* get rid of pesky characters */
117 for (i = 0; i < len; i++)
118 if ((passwd[i] != ' ') && isspace(passwd[i]))
127 return AFPERR_ACCESS;
133 * handle /path/afppasswd with an optional key file. we're a lot more
134 * trusting of this file. NOTE: we use our own password entry writing
135 * bits as we want to avoid tromping over global variables. in addition,
136 * we look for a key file and use that if it's there. here are the
139 * username:password:last login date:failedcount
141 * password is just the hex equivalent of either the ASCII password
142 * (if the key file doesn't exist) or the des encrypted password.
146 #define PASSWD_ILLEGAL '*'
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;
155 int 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 unhexify(p, sizeof(key), p, sizeof(key));
197 /* read in the hex representation of an 8-byte key */
198 read(keyfd, key, sizeof(key));
200 /* convert to binary key */
201 len = strlen((char *) key);
202 unhexify(key, len, key, len);
205 /* NOTE: this takes advantage of the fact that passwd doesn't
206 * get used after this call if it's being set. */
207 err = encrypt(key, passwd, passwd);
209 err = decrypt(key, p, p);
211 memset(key, 0, sizeof(key));
221 /* convert to hex password */
222 hexify(key, sizeof(key), passwd, DES_KEY_SZ);
223 memcpy(p, key, sizeof(key));
225 /* get exclusive access to the user's password entry. we don't
226 * worry so much on reads. in the worse possible case there, the
227 * user will just need to re-enter their password. */
228 lock.l_type = F_WRLCK;
231 lock.l_whence = SEEK_SET;
233 fseek(fp, pos, SEEK_SET);
234 fcntl(fd, F_SETLKW, &lock);
235 fwrite(buf, p - buf + sizeof(key), 1, fp);
236 lock.l_type = F_UNLCK;
237 fcntl(fd, F_SETLK, &lock);
239 memcpy(passwd, p, len);
241 memset(buf, 0, sizeof(buf));
251 /* this sets the uid. it needs to do slightly different things
252 * depending upon whether or not the password is in ~/.passwd
253 * or in a global location */
254 static int randpass(const struct passwd *pwd, const char *file,
255 char *passwd, const int len, const int set)
258 uid_t uid = geteuid();
260 /* Build pathname to user's '.passwd' file */
263 char path[MAXPATHLEN + 1];
265 if ( (strlen(pwd->pw_dir) + i - 1) > MAXPATHLEN)
268 strcpy(path, pwd->pw_dir );
270 strcat(path, file + 2);
272 seteuid(pwd->pw_uid); /* change ourselves to the user */
273 i = home_passwd(pwd, path, i, passwd, len, set);
275 seteuid(0); /* change ourselves back to root */
282 /* handle afppasswd file. we need to make sure that we're root
283 * when we do this. */
286 i = afppasswd(pwd, file, i, passwd, len, set);
293 /* randnum sends an 8-byte number and uses the user's password to
294 * check against the encrypted reply. */
295 static int randnum_login(void *obj, struct passwd **uam_pwd,
296 char *ibuf, int ibuflen,
297 char *rbuf, int *rbuflen)
299 char *username, *passwdfile;
305 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
306 (void *) &username, &ulen) < 0)
309 len = UAM_PASSWD_FILENAME;
310 if (uam_afpserver_option(obj, UAM_OPTION_PASSWDOPT,
311 (void *) &passwdfile, &len) < 0)
314 len = (unsigned char) *ibuf++;
318 memcpy(username, ibuf, len );
320 username[ len ] = '\0';
321 if ((unsigned long) ibuf & 1) /* padding */
324 if (( randpwd = uam_getname(username, ulen)) == NULL )
325 return AFPERR_PARAM; /* unknown user */
327 LOG(log_info, logtype_uams, "randnum/rand2num login: %s", username);
328 if (uam_checkuser(randpwd) < 0)
329 return AFPERR_NOTAUTH;
331 if ((err = randpass(randpwd, passwdfile, seskey,
332 sizeof(seskey), 0)) != AFP_OK)
335 /* get a random number */
336 len = sizeof(randbuf);
337 if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM,
338 (void *) randbuf, &len) < 0)
341 /* session id is a hashed version of the obj pointer */
342 sessid = randhash(obj);
343 memcpy(rbuf, &sessid, sizeof(sessid));
344 rbuf += sizeof(sessid);
345 *rbuflen = sizeof(sessid);
347 /* send the random number off */
348 memcpy(rbuf, randbuf, sizeof(randbuf));
349 *rbuflen += sizeof(randbuf);
350 return AFPERR_AUTHCONT;
354 /* check encrypted reply. we actually setup the encryption stuff
355 * here as the first part of randnum and rand2num are identical. */
356 static int randnum_logincont(void *obj, struct passwd **uam_pwd,
357 char *ibuf, int ibuflen,
358 char *rbuf, int *rbuflen)
365 memcpy(&sessid, ibuf, sizeof(sessid));
366 if (sessid != randhash(obj))
369 ibuf += sizeof(sessid);
371 err = encrypt(seskey, randbuf, randbuf);
372 memset(seskey, 0, sizeof(seskey));
376 /* test against what the client sent */
377 if (memcmp( randbuf, ibuf, sizeof(randbuf) )) { /* != */
378 memset(randbuf, 0, sizeof(randbuf));
379 return AFPERR_NOTAUTH;
382 memset(randbuf, 0, sizeof(randbuf));
388 /* differences from randnum:
389 * 1) each byte of the key is shifted left one bit
390 * 2) client sends the server a 64-bit number. the server encrypts it
391 * and sends it back as part of the reply.
393 static int rand2num_logincont(void *obj, struct passwd **uam_pwd,
394 char *ibuf, int ibuflen,
395 char *rbuf, int *rbuflen)
398 CryptHandle crypt_handle;
404 /* compare session id */
405 memcpy(&sessid, ibuf, sizeof(sessid));
406 if (sessid != randhash(obj))
409 ibuf += sizeof(sessid);
411 /* shift key elements left one bit */
412 for (i = 0; i < sizeof(seskey); i++)
415 /* encrypt randbuf */
416 err = encrypt_start(&crypt_handle, seskey);
417 encrypt_do(crypt_handle, randbuf, randbuf);
419 /* test against client's reply */
420 if (memcmp(randbuf, ibuf, sizeof(randbuf))) { /* != */
421 memset(randbuf, 0, sizeof(randbuf));
422 memset(&seskeysched, 0, sizeof(seskeysched));
423 return AFPERR_NOTAUTH;
425 ibuf += sizeof(randbuf);
426 memset(randbuf, 0, sizeof(randbuf));
428 /* encrypt client's challenge and send back */
429 encrypt_do(crypt_handle, rbuf, ibuf);
430 encrypt_end(crypt_handle);
431 memset(seskey, 0, sizeof(seskey));
433 *rbuflen = sizeof(randbuf);
439 /* change password --
440 * NOTE: an FPLogin must already have completed successfully for this
443 static int randnum_changepw(void *obj, const char *username,
444 struct passwd *pwd, char *ibuf,
445 int ibuflen, char *rbuf, int *rbuflen)
450 if (uam_checkuser(pwd) < 0)
451 return AFPERR_ACCESS;
453 len = UAM_PASSWD_FILENAME;
454 if (uam_afpserver_option(obj, UAM_OPTION_PASSWDOPT,
455 (void *) &passwdfile, &len) < 0)
458 /* old password is encrypted with new password and new password is
459 * encrypted with old. */
460 if ((err = randpass(pwd, passwdfile, seskey,
461 sizeof(seskey), 0)) != AFP_OK)
464 /* use old passwd to decrypt new passwd */
465 key_sched((C_Block *) seskey, seskeysched);
466 ibuf += PASSWDLEN; /* new passwd */
467 ibuf[PASSWDLEN] = '\0';
468 ecb_encrypt( (C_Block *) ibuf, (C_Block *) ibuf, seskeysched, DES_DECRYPT);
470 /* now use new passwd to decrypt old passwd */
471 key_sched((C_Block *) ibuf, seskeysched);
472 ibuf -= PASSWDLEN; /* old passwd */
473 ecb_encrypt((C_Block *) ibuf, (C_Block *) ibuf, seskeysched, DES_DECRYPT);
474 if (memcmp(seskey, ibuf, sizeof(seskey)))
475 err = AFPERR_NOTAUTH;
476 else if (memcmp(seskey, ibuf + PASSWDLEN, sizeof(seskey)) == 0)
477 err = AFPERR_PWDSAME;
479 else if (FascistCheck(ibuf + PASSWDLEN, _PATH_CRACKLIB))
480 err = AFPERR_PWDPOLCY;
481 #endif /* USE_CRACKLIB */
484 err = randpass(pwd, passwdfile, ibuf + PASSWDLEN, sizeof(seskey), 1);
486 /* zero out some fields */
487 memset(&seskeysched, 0, sizeof(seskeysched));
488 memset(seskey, 0, sizeof(seskey));
489 memset(ibuf, 0, sizeof(seskey)); /* old passwd */
490 memset(ibuf + PASSWDLEN, 0, sizeof(seskey)); /* new passwd */
498 static int uam_setup(const char *path)
500 if (uam_register(UAM_SERVER_LOGIN, path, "Randnum exchange",
501 randnum_login, randnum_logincont, NULL) < 0)
503 if (uam_register(UAM_SERVER_LOGIN, path, "2-Way Randnum exchange",
504 randnum_login, rand2num_logincont, NULL) < 0) {
505 uam_unregister(UAM_SERVER_LOGIN, "Randnum exchange");
509 if (uam_register(UAM_SERVER_CHANGEPW, path, "Randnum Exchange",
510 randnum_changepw) < 0) {
511 uam_unregister(UAM_SERVER_LOGIN, "Randnum exchange");
512 uam_unregister(UAM_SERVER_LOGIN, "2-Way Randnum exchange");
515 /*uam_register(UAM_SERVER_PRINTAUTH, path, "Randnum Exchange",
521 static void uam_cleanup(void)
523 uam_unregister(UAM_SERVER_LOGIN, "Randnum exchange");
524 uam_unregister(UAM_SERVER_LOGIN, "2-Way Randnum exchange");
525 uam_unregister(UAM_SERVER_CHANGEPW, "Randnum Exchange");
526 /*uam_unregister(UAM_SERVER_PRINTAUTH, "Randnum Exchange");*/
529 UAM_MODULE_EXPORT struct uam_export uams_randnum = {
532 uam_setup, uam_cleanup