]> arthur.barton.de Git - netatalk.git/blob - etc/uams/uams_randnum.c
changed all LOG messages using logtype_default to use logtype_uams
[netatalk.git] / etc / uams / uams_randnum.c
1 /* 
2  * $Id: uams_randnum.c,v 1.11 2002-09-29 23:30:20 sibaz Exp $
3  *
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.
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif /* HAVE_CONFIG_H */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15
16 /* STDC check */
17 #if STDC_HEADERS
18 #include <string.h>
19 #else /* STDC_HEADERS */
20 #ifndef HAVE_STRCHR
21 #define strchr index
22 #define strrchr index
23 #endif /* HAVE_STRCHR */
24 char *strchr (), *strrchr ();
25 #ifndef HAVE_MEMCPY
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 */
30
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif /* HAVE_UNISTD_H */
34 #ifdef HAVE_FCNTL_H
35 #include <fcntl.h>
36 #endif /* HAVE_FCNTL_H */
37 #include <ctype.h>
38 #include <pwd.h>
39 #include <sys/stat.h>
40 #include <sys/param.h>
41
42 #include <atalk/logger.h>
43
44 #include <netatalk/endian.h>
45
46 #include <atalk/afp.h>
47 #include <atalk/uam.h>
48
49
50 #include <des.h>
51
52 #ifdef USE_CRACKLIB
53 #include <crack.h>
54 #endif /* USE_CRACKLIB */
55
56 #ifndef __inline__
57 #define __inline__
58 #endif /* __inline__ */
59
60 #define PASSWDLEN 8
61
62 static C_Block          seskey;
63 static Key_schedule     seskeysched;
64 static struct passwd    *randpwd;
65 static u_int8_t         randbuf[8];
66
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)
71
72
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,
77                                    const int set)
78 {
79   struct stat st;
80   int fd, i;
81   
82   if ( (fd = open(path, (set) ? O_WRONLY : O_RDONLY)) < 0 ) {
83     LOG(log_error, logtype_uams, "Failed to open %s", path);
84     return AFPERR_ACCESS;
85   }
86
87   if ( fstat( fd, &st ) < 0 ) 
88     goto home_passwd_fail;
89   
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
94    */
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;
100   }
101
102   /* get the password */
103   if (set) {
104     if (write(fd, passwd, len) < 0) {
105       LOG(log_error, logtype_uams, "Failed to write to %s", path );
106       goto home_passwd_fail;
107     }
108   } else {
109     if (read(fd, passwd, len) < 0) {
110       LOG(log_error, logtype_uams, "Failed to read from %s", path );
111       goto home_passwd_fail;
112     }
113   
114   /* get rid of pesky characters */
115   for (i = 0; i < len; i++)
116     if ((passwd[i] != ' ') && isspace(passwd[i]))
117       passwd[i] = '\0';
118   }
119
120   close(fd);
121   return AFP_OK;
122
123 home_passwd_fail:
124   close(fd);
125   return AFPERR_ACCESS;
126 }
127
128
129
130 /* 
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 
135  * formats: 
136  * password file:
137  * username:password:last login date:failedcount
138  *
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.
141  *
142  * key file: 
143  * key (in hex) */
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, 
149                      const int set)
150 {
151   u_int8_t key[DES_KEY_SZ*2];
152   char buf[MAXPATHLEN + 1], *p;
153   Key_schedule  schedule;
154   FILE *fp;
155   int i, j, keyfd = -1, err = 0;
156   off_t pos;
157   
158   if ((fp = fopen(path, (set) ? "r+" : "r")) == NULL) {
159     LOG(log_error, logtype_uams, "Failed to open %s", path);
160     return AFPERR_ACCESS;
161   }
162   
163   /* open the key file if it exists */
164   strcpy(buf, path);
165   if (pathlen < sizeof(buf) - 5) {
166     strcat(buf, ".key");
167     keyfd = open(buf, O_RDONLY);
168   } 
169   
170   pos = ftell(fp);
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) {
175         p++;
176         if (*p == PASSWD_ILLEGAL) {
177           LOG(log_info, logtype_uams, "invalid password entry for %s", pwd->pw_name);
178           err = AFPERR_ACCESS;
179           goto afppasswd_done;
180         }
181         goto afppasswd_found;
182       }
183     }
184     pos = ftell(fp);
185     memset(buf, 0, sizeof(buf));
186   }
187   err = AFPERR_PARAM;
188   goto afppasswd_done;
189
190 afppasswd_found:
191   if (!set) {
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]);
195     if (j <= DES_KEY_SZ)
196       memset(p + j, 0, sizeof(key) - j);
197   }
198
199   if (keyfd > -1) {
200       /* read in the hex representation of an 8-byte key */
201       read(keyfd, key, sizeof(key));
202
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]);
206       if (j <= DES_KEY_SZ)
207         memset(key + j, 0, sizeof(key) - j);
208       key_sched((C_Block *) key, schedule);
209       memset(key, 0, sizeof(key));
210
211       if (set) {
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,
215                     DES_ENCRYPT);
216       } else {
217         /* decrypt the password */
218         ecb_encrypt((C_Block *) p, (C_Block *) p, schedule, DES_DECRYPT);
219       }
220       memset(schedule, 0, sizeof(schedule));
221   }
222
223   if (set) {
224     const unsigned char hextable[] = "0123456789ABCDEF";
225     struct flock lock;
226     int fd = fileno(fp);
227
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];
232     }
233     memcpy(p, key, sizeof(key));
234
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;
239     lock.l_start = pos;
240     lock.l_len = 1;
241     lock.l_whence = SEEK_SET;
242
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);
248   } else 
249     memcpy(passwd, p, len);
250
251   memset(buf, 0, sizeof(buf));
252
253 afppasswd_done:
254   if (keyfd > -1)
255     close(keyfd);
256   fclose(fp);
257   return err;
258 }
259
260
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) 
266 {
267   int i;
268   uid_t uid = geteuid();
269
270   /* Build pathname to user's '.passwd' file */
271   i = strlen(file);
272   if (*file == '~') {
273     char path[MAXPATHLEN + 1];
274
275     if ( (strlen(pwd->pw_dir) + i - 1) > MAXPATHLEN)
276     return AFPERR_PARAM;
277   
278     strcpy(path,  pwd->pw_dir );
279     strcat(path, "/" );
280     strcat(path, file + 2);
281     if (!uid)
282       seteuid(pwd->pw_uid); /* change ourselves to the user */
283     i = home_passwd(pwd, path, i, passwd, len, set);
284     if (!uid)
285       seteuid(0); /* change ourselves back to root */
286     return i;
287   } 
288
289   if (i > MAXPATHLEN)
290     return AFPERR_PARAM;
291
292   /* handle afppasswd file. we need to make sure that we're root
293    * when we do this. */
294   if (uid)
295     seteuid(0);
296   i = afppasswd(pwd, file, i, passwd, len, set);
297   if (uid)
298     seteuid(uid);
299   return i;
300 }
301
302   
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)
308 {
309   char *username, *passwdfile;
310   u_int16_t sessid;
311   int len, ulen, err;
312   
313   *rbuflen = 0;
314   
315   if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, 
316                            (void *) &username, &ulen) < 0)
317     return AFPERR_PARAM;
318
319   len = UAM_PASSWD_FILENAME;
320   if (uam_afpserver_option(obj, UAM_OPTION_PASSWDOPT, 
321                              (void *) &passwdfile, &len) < 0)
322     return AFPERR_PARAM;
323
324   len = (unsigned char) *ibuf++;
325   if ( len > ulen ) {
326         return AFPERR_PARAM;
327   }
328   memcpy(username, ibuf, len );
329   ibuf += len;
330   username[ len ] = '\0';
331   if ((unsigned long) ibuf & 1) /* padding */
332     ++ibuf;
333   
334   if (( randpwd = uam_getname(username, ulen)) == NULL )
335     return AFPERR_PARAM; /* unknown user */
336   
337   LOG(log_info, logtype_uams, "randnum/rand2num login: %s", username);
338   if (uam_checkuser(randpwd) < 0)
339     return AFPERR_NOTAUTH;
340
341   if ((err = randpass(randpwd, passwdfile, seskey,
342                       sizeof(seskey), 0)) != AFP_OK)
343     return err;
344
345   /* get a random number */
346   len = sizeof(randbuf);
347   if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM,
348                            (void *) randbuf, &len) < 0)
349     return AFPERR_PARAM;
350
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);
356   
357   /* send the random number off */
358   memcpy(rbuf, randbuf, sizeof(randbuf));
359   *rbuflen += sizeof(randbuf);
360   return AFPERR_AUTHCONT;
361 }
362
363
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)
369 {
370   u_int16_t sessid;
371
372   *rbuflen = 0;
373
374   memcpy(&sessid, ibuf, sizeof(sessid));
375   if (sessid != randhash(obj))
376     return AFPERR_PARAM;
377
378   ibuf += sizeof(sessid);
379
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));
387
388   /* test against what the client sent */
389   if (memcmp( randbuf, ibuf, sizeof(randbuf) )) { /* != */
390     memset(randbuf, 0, sizeof(randbuf));
391     return AFPERR_NOTAUTH;
392   }
393
394   memset(randbuf, 0, sizeof(randbuf));
395   *uam_pwd = randpwd;
396   return AFP_OK;
397 }
398
399
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.
404  */
405 static int rand2num_logincont(void *obj, struct passwd **uam_pwd,
406                               char *ibuf, int ibuflen, 
407                               char *rbuf, int *rbuflen)
408 {
409   u_int16_t sessid;
410   int i;
411
412   *rbuflen = 0;
413
414   /* compare session id */
415   memcpy(&sessid, ibuf, sizeof(sessid));
416   if (sessid != randhash(obj))
417     return AFPERR_PARAM;
418
419   ibuf += sizeof(sessid);
420
421   /* shift key elements left one bit */
422   for (i = 0; i < sizeof(seskey); i++)
423     seskey[i] <<= 1;
424
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);
430
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;
436   }
437   ibuf += sizeof(randbuf);
438   memset(randbuf, 0, sizeof(randbuf));
439
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);
445   
446   *uam_pwd = randpwd;
447   return AFP_OK;
448 }
449
450 /* change password  --
451  * NOTE: an FPLogin must already have completed successfully for this
452  *       to work. 
453  */
454 static int randnum_changepw(void *obj, const char *username, 
455                             struct passwd *pwd, char *ibuf,
456                             int ibuflen, char *rbuf, int *rbuflen)
457 {
458     char *passwdfile;
459     int err, len;
460
461     if (uam_checkuser(pwd) < 0)
462       return AFPERR_ACCESS;
463
464     len = UAM_PASSWD_FILENAME;
465     if (uam_afpserver_option(obj, UAM_OPTION_PASSWDOPT, 
466                              (void *) &passwdfile, &len) < 0)
467       return AFPERR_PARAM;
468
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)
473       return err;
474
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);
480
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;
489 #ifdef USE_CRACKLIB
490     else if (FascistCheck(ibuf + PASSWDLEN, _PATH_CRACKLIB))
491         err = AFPERR_PWDPOLCY;
492 #endif /* USE_CRACKLIB */
493
494     if (!err) 
495       err = randpass(pwd, passwdfile, ibuf + PASSWDLEN, sizeof(seskey), 1);
496
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 */
502
503     if (err)
504       return err;
505
506   return( AFP_OK );
507 }
508
509 static int uam_setup(const char *path)
510 {
511   if (uam_register(UAM_SERVER_LOGIN, path, "Randnum exchange", 
512                    randnum_login, randnum_logincont, NULL) < 0)
513     return -1;
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");
517     return -1;
518   }
519     
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");
524     return -1;
525   }
526   /*uam_register(UAM_SERVER_PRINTAUTH, path, "Randnum Exchange",
527     pam_printer);*/
528
529   return 0;
530 }
531
532 static void uam_cleanup(void)
533 {
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");*/
538 }
539
540 UAM_MODULE_EXPORT struct uam_export uams_randnum = {
541   UAM_MODULE_SERVER,
542   UAM_MODULE_VERSION,
543   uam_setup, uam_cleanup
544 };