]> arthur.barton.de Git - netatalk.git/blob - etc/uams/uams_randnum.c
make randnum UAM AFP3 ready
[netatalk.git] / etc / uams / uams_randnum.c
1 /* 
2  * $Id: uams_randnum.c,v 1.12.6.3 2004-02-25 00:37:19 bfernhomberg 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   unsigned int i, j;
156   int keyfd = -1, err = 0;
157   off_t pos;
158   
159   if ((fp = fopen(path, (set) ? "r+" : "r")) == NULL) {
160     LOG(log_error, logtype_uams, "Failed to open %s", path);
161     return AFPERR_ACCESS;
162   }
163   
164   /* open the key file if it exists */
165   strcpy(buf, path);
166   if (pathlen < (int) sizeof(buf) - 5) {
167     strcat(buf, ".key");
168     keyfd = open(buf, O_RDONLY);
169   } 
170   
171   pos = ftell(fp);
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) {
176         p++;
177         if (*p == PASSWD_ILLEGAL) {
178           LOG(log_info, logtype_uams, "invalid password entry for %s", pwd->pw_name);
179           err = AFPERR_ACCESS;
180           goto afppasswd_done;
181         }
182         goto afppasswd_found;
183       }
184     }
185     pos = ftell(fp);
186     memset(buf, 0, sizeof(buf));
187   }
188   err = AFPERR_PARAM;
189   goto afppasswd_done;
190
191 afppasswd_found:
192   if (!set) {
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]);
196     if (j <= DES_KEY_SZ)
197       memset(p + j, 0, sizeof(key) - j);
198   }
199
200   if (keyfd > -1) {
201       /* read in the hex representation of an 8-byte key */
202       read(keyfd, key, sizeof(key));
203
204       /* convert to binary key */
205       for (i = j = 0; i < strlen((char *) key); i += 2, j++)
206         key[j] = (unhex(key[i]) << 4) | unhex(key[i + 1]);
207       if (j <= DES_KEY_SZ)
208         memset(key + j, 0, sizeof(key) - j);
209       key_sched((C_Block *) key, schedule);
210       memset(key, 0, sizeof(key));
211
212       if (set) {
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,
216                     DES_ENCRYPT);
217       } else {
218         /* decrypt the password */
219         ecb_encrypt((C_Block *) p, (C_Block *) p, schedule, DES_DECRYPT);
220       }
221       memset(&schedule, 0, sizeof(schedule));
222   }
223
224   if (set) {
225     const unsigned char hextable[] = "0123456789ABCDEF";
226     struct flock lock;
227     int fd = fileno(fp);
228
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];
233     }
234     memcpy(p, key, sizeof(key));
235
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;
240     lock.l_start = pos;
241     lock.l_len = 1;
242     lock.l_whence = SEEK_SET;
243
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);
249   } else 
250     memcpy(passwd, p, len);
251
252   memset(buf, 0, sizeof(buf));
253
254 afppasswd_done:
255   if (keyfd > -1)
256     close(keyfd);
257   fclose(fp);
258   return err;
259 }
260
261
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) 
267 {
268   int i;
269   uid_t uid = geteuid();
270
271   /* Build pathname to user's '.passwd' file */
272   i = strlen(file);
273   if (*file == '~') {
274     char path[MAXPATHLEN + 1];
275
276     if ( (strlen(pwd->pw_dir) + i - 1) > MAXPATHLEN)
277     return AFPERR_PARAM;
278   
279     strcpy(path,  pwd->pw_dir );
280     strcat(path, "/" );
281     strcat(path, file + 2);
282     if (!uid)
283       seteuid(pwd->pw_uid); /* change ourselves to the user */
284     i = home_passwd(pwd, path, i, passwd, len, set);
285     if (!uid)
286       seteuid(0); /* change ourselves back to root */
287     return i;
288   } 
289
290   if (i > MAXPATHLEN)
291     return AFPERR_PARAM;
292
293   /* handle afppasswd file. we need to make sure that we're root
294    * when we do this. */
295   if (uid)
296     seteuid(0);
297   i = afppasswd(pwd, file, i, passwd, len, set);
298   if (uid)
299     seteuid(uid);
300   return i;
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 rand_login(void *obj, char *username, int ulen, struct passwd **uam_pwd,
306                         char *ibuf, int ibuflen,
307                         char *rbuf, int *rbuflen)
308 {
309
310   char *passwdfile;
311   u_int16_t sessid;
312   int len, err;
313  
314   if (( randpwd = uam_getname(obj, username, ulen)) == NULL )
315     return AFPERR_PARAM; /* unknown user */
316   
317   LOG(log_info, logtype_uams, "randnum/rand2num login: %s", username);
318   if (uam_checkuser(randpwd) < 0)
319     return AFPERR_NOTAUTH;
320
321   len = UAM_PASSWD_FILENAME;
322   if (uam_afpserver_option(obj, UAM_OPTION_PASSWDOPT,
323                              (void *) &passwdfile, &len) < 0)
324     return AFPERR_PARAM;
325
326   if ((err = randpass(randpwd, passwdfile, seskey,
327                       sizeof(seskey), 0)) != AFP_OK)
328     return err;
329
330   /* get a random number */
331   len = sizeof(randbuf);
332   if (uam_afpserver_option(obj, UAM_OPTION_RANDNUM,
333                            (void *) randbuf, &len) < 0)
334     return AFPERR_PARAM;
335
336   /* session id is a hashed version of the obj pointer */
337   sessid = randhash(obj);
338   memcpy(rbuf, &sessid, sizeof(sessid));
339   rbuf += sizeof(sessid);
340   *rbuflen = sizeof(sessid);
341   
342   /* send the random number off */
343   memcpy(rbuf, randbuf, sizeof(randbuf));
344   *rbuflen += sizeof(randbuf);
345   return AFPERR_AUTHCONT;
346 }
347
348
349 /* check encrypted reply. we actually setup the encryption stuff
350  * here as the first part of randnum and rand2num are identical. */
351 static int randnum_logincont(void *obj, struct passwd **uam_pwd,
352                              char *ibuf, int ibuflen, 
353                              char *rbuf, int *rbuflen)
354 {
355   u_int16_t sessid;
356
357   *rbuflen = 0;
358
359   memcpy(&sessid, ibuf, sizeof(sessid));
360   if (sessid != randhash(obj))
361     return AFPERR_PARAM;
362
363   ibuf += sizeof(sessid);
364
365   /* encrypt. this saves a little space by using the fact that
366    * des can encrypt in-place without side-effects. */
367   key_sched((C_Block *) seskey, seskeysched);
368   memset(seskey, 0, sizeof(seskey));
369   ecb_encrypt((C_Block *) randbuf, (C_Block *) randbuf,
370                seskeysched, DES_ENCRYPT);
371   memset(&seskeysched, 0, sizeof(seskeysched));
372
373   /* test against what the client sent */
374   if (memcmp( randbuf, ibuf, sizeof(randbuf) )) { /* != */
375     memset(randbuf, 0, sizeof(randbuf));
376     return AFPERR_NOTAUTH;
377   }
378
379   memset(randbuf, 0, sizeof(randbuf));
380   *uam_pwd = randpwd;
381   return AFP_OK;
382 }
383
384
385 /* differences from randnum:
386  * 1) each byte of the key is shifted left one bit
387  * 2) client sends the server a 64-bit number. the server encrypts it
388  *    and sends it back as part of the reply.
389  */
390 static int rand2num_logincont(void *obj, struct passwd **uam_pwd,
391                               char *ibuf, int ibuflen, 
392                               char *rbuf, int *rbuflen)
393 {
394   u_int16_t sessid;
395   unsigned int i;
396
397   *rbuflen = 0;
398
399   /* compare session id */
400   memcpy(&sessid, ibuf, sizeof(sessid));
401   if (sessid != randhash(obj))
402     return AFPERR_PARAM;
403
404   ibuf += sizeof(sessid);
405
406   /* shift key elements left one bit */
407   for (i = 0; i < sizeof(seskey); i++)
408     seskey[i] <<= 1;
409
410   /* encrypt randbuf */
411   key_sched((C_Block *) seskey, seskeysched);
412   memset(seskey, 0, sizeof(seskey));
413   ecb_encrypt( (C_Block *) randbuf, (C_Block *) randbuf,
414                seskeysched, DES_ENCRYPT);
415
416   /* test against client's reply */
417   if (memcmp(randbuf, ibuf, sizeof(randbuf))) { /* != */
418     memset(randbuf, 0, sizeof(randbuf));
419     memset(&seskeysched, 0, sizeof(seskeysched));
420     return AFPERR_NOTAUTH;
421   }
422   ibuf += sizeof(randbuf);
423   memset(randbuf, 0, sizeof(randbuf));
424
425   /* encrypt client's challenge and send back */
426   ecb_encrypt( (C_Block *) ibuf, (C_Block *) rbuf,
427                seskeysched, DES_ENCRYPT);
428   memset(&seskeysched, 0, sizeof(seskeysched));
429   *rbuflen = sizeof(randbuf);
430   
431   *uam_pwd = randpwd;
432   return AFP_OK;
433 }
434
435 /* change password  --
436  * NOTE: an FPLogin must already have completed successfully for this
437  *       to work. 
438  */
439 static int randnum_changepw(void *obj, const char *username, 
440                             struct passwd *pwd, char *ibuf,
441                             int ibuflen, char *rbuf, int *rbuflen)
442 {
443     char *passwdfile;
444     int err, len;
445
446     if (uam_checkuser(pwd) < 0)
447       return AFPERR_ACCESS;
448
449     len = UAM_PASSWD_FILENAME;
450     if (uam_afpserver_option(obj, UAM_OPTION_PASSWDOPT, 
451                              (void *) &passwdfile, &len) < 0)
452       return AFPERR_PARAM;
453
454     /* old password is encrypted with new password and new password is
455      * encrypted with old. */
456     if ((err = randpass(pwd, passwdfile, seskey, 
457                         sizeof(seskey), 0)) != AFP_OK)
458       return err;
459
460     /* use old passwd to decrypt new passwd */
461     key_sched((C_Block *) seskey, seskeysched);
462     ibuf += PASSWDLEN; /* new passwd */
463     ibuf[PASSWDLEN] = '\0';
464     ecb_encrypt( (C_Block *) ibuf, (C_Block *) ibuf, seskeysched, DES_DECRYPT);
465
466     /* now use new passwd to decrypt old passwd */
467     key_sched((C_Block *) ibuf, seskeysched);
468     ibuf -= PASSWDLEN; /* old passwd */
469     ecb_encrypt((C_Block *) ibuf, (C_Block *) ibuf, seskeysched, DES_DECRYPT);
470     if (memcmp(seskey, ibuf, sizeof(seskey))) 
471         err = AFPERR_NOTAUTH;
472     else if (memcmp(seskey, ibuf + PASSWDLEN, sizeof(seskey)) == 0)
473         err = AFPERR_PWDSAME;
474 #ifdef USE_CRACKLIB
475     else if (FascistCheck(ibuf + PASSWDLEN, _PATH_CRACKLIB))
476         err = AFPERR_PWDPOLCY;
477 #endif /* USE_CRACKLIB */
478
479     if (!err) 
480       err = randpass(pwd, passwdfile, ibuf + PASSWDLEN, sizeof(seskey), 1);
481
482     /* zero out some fields */
483     memset(&seskeysched, 0, sizeof(seskeysched));
484     memset(seskey, 0, sizeof(seskey));
485     memset(ibuf, 0, sizeof(seskey)); /* old passwd */
486     memset(ibuf + PASSWDLEN, 0, sizeof(seskey)); /* new passwd */
487
488     if (err)
489       return err;
490
491   return( AFP_OK );
492 }
493
494 /* randnum login */
495 static int randnum_login(void *obj, struct passwd **uam_pwd,
496                         char *ibuf, int ibuflen,
497                         char *rbuf, int *rbuflen)
498 {
499     char *username;
500     int len, ulen;
501
502     *rbuflen = 0;
503
504     if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
505                              (void *) &username, &ulen) < 0)
506         return AFPERR_MISC;
507
508     if (ibuflen <= 1) {
509         return( AFPERR_PARAM );
510     }
511
512     len = (unsigned char) *ibuf++;
513     ibuflen--;
514     if (!len || len > ibuflen || len > ulen ) {
515         return( AFPERR_PARAM );
516     }
517     memcpy(username, ibuf, len );
518     ibuf += len;
519     ibuflen -=len;
520     username[ len ] = '\0';
521
522     if ((unsigned long) ibuf & 1) { /* pad character */
523         ++ibuf;
524         ibuflen--;
525     }
526     return (rand_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
527 }
528
529 /* randnum login ext */
530 static int randnum_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
531                         char *ibuf, int ibuflen,
532                         char *rbuf, int *rbuflen)
533 {
534     char       *username;
535     int        len, ulen;
536     u_int16_t  temp16;
537
538     *rbuflen = 0;
539
540     if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
541                              (void *) &username, &ulen) < 0)
542         return AFPERR_MISC;
543
544     if (*uname != 3)
545         return AFPERR_PARAM;
546     uname++;
547     memcpy(&temp16, uname, sizeof(temp16));
548     len = ntohs(temp16);
549     if (!len || len > ulen ) {
550         return( AFPERR_PARAM );
551     }
552     memcpy(username, uname +2, len );
553     username[ len ] = '\0';
554     return (rand_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
555 }
556
557 static int uam_setup(const char *path)
558 {
559   if (uam_register(UAM_SERVER_LOGIN_EXT, path, "Randnum exchange", 
560                    randnum_login, randnum_logincont, NULL, randnum_login_ext) < 0)
561     return -1;
562
563   if (uam_register(UAM_SERVER_LOGIN_EXT, path, "2-Way Randnum exchange",
564                    randnum_login, rand2num_logincont, NULL, randnum_login_ext) < 0) {
565     uam_unregister(UAM_SERVER_LOGIN, "Randnum exchange");
566     return -1;
567   }
568
569   if (uam_register(UAM_SERVER_CHANGEPW, path, "Randnum Exchange", 
570                    randnum_changepw) < 0) {
571     uam_unregister(UAM_SERVER_LOGIN, "Randnum exchange");
572     uam_unregister(UAM_SERVER_LOGIN, "2-Way Randnum exchange");
573     return -1;
574   }
575   /*uam_register(UAM_SERVER_PRINTAUTH, path, "Randnum Exchange",
576     pam_printer);*/
577
578   return 0;
579 }
580
581 static void uam_cleanup(void)
582 {
583   uam_unregister(UAM_SERVER_LOGIN, "Randnum exchange");
584   uam_unregister(UAM_SERVER_LOGIN, "2-Way Randnum exchange");
585   uam_unregister(UAM_SERVER_CHANGEPW, "Randnum Exchange");
586   /*uam_unregister(UAM_SERVER_PRINTAUTH, "Randnum Exchange");*/
587 }
588
589 UAM_MODULE_EXPORT struct uam_export uams_randnum = {
590   UAM_MODULE_SERVER,
591   UAM_MODULE_VERSION,
592   uam_setup, uam_cleanup
593 };