]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/auth.c
Merge remote branch 'sf/branch-allea' into branch-allea
[netatalk.git] / etc / afpd / auth.c
1 /*
2  * Copyright (c) 1990,1993 Regents of The University of Michigan.
3  * All Rights Reserved.  See COPYRIGHT.
4  */
5
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif /* HAVE_CONFIG_H */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <errno.h>
13 #include <unistd.h>
14 #include <sys/types.h>
15 #include <sys/param.h>
16 #include <sys/stat.h>
17 #include <arpa/inet.h>
18
19 #include <atalk/afp.h>
20 #include <atalk/compat.h>
21 #include <atalk/util.h>
22 #include <limits.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <time.h>
26 #include <pwd.h>
27 #include <grp.h>
28
29 #ifdef TRU64
30 #include <netdb.h>
31 #include <arpa/inet.h>
32 #include <sia.h>
33 #include <siad.h>
34
35 extern void afp_get_cmdline( int *ac, char ***av );
36 #endif /* TRU64 */
37
38 #include <atalk/logger.h>
39 #include <atalk/server_ipc.h>
40 #include <atalk/uuid.h>
41 #include <atalk/globals.h>
42
43 #include "auth.h"
44 #include "uam_auth.h"
45 #include "switch.h"
46 #include "status.h"
47 #include "fork.h"
48 #include "extattrs.h"
49 #ifdef HAVE_ACLS
50 #include "acls.h"
51 #endif
52
53 static int afp_version_index;
54 static struct uam_mod uam_modules = {NULL, NULL, &uam_modules, &uam_modules};
55 static struct uam_obj uam_login = {"", "", 0, {{NULL, NULL, NULL, NULL }}, &uam_login,
56                                    &uam_login};
57 static struct uam_obj uam_changepw = {"", "", 0, {{NULL, NULL, NULL, NULL}}, &uam_changepw,
58                                       &uam_changepw};
59
60 static struct uam_obj *afp_uam = NULL;
61
62
63 void status_versions( char *data, const DSI *dsi)
64 {
65     char                *start = data;
66     uint16_t           status;
67     int         len, num, i, count = 0;
68
69     memcpy(&status, start + AFPSTATUS_VERSOFF, sizeof(status));
70     num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ] );
71
72     for ( i = 0; i < num; i++ ) {
73         if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
74         count++;
75     }
76     data += ntohs( status );
77     *data++ = count;
78
79     for ( i = 0; i < num; i++ ) {
80         if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
81         len = strlen( afp_versions[ i ].av_name );
82         *data++ = len;
83         memcpy( data, afp_versions[ i ].av_name , len );
84         data += len;
85     }
86     status = htons( data - start );
87     memcpy(start + AFPSTATUS_UAMSOFF, &status, sizeof(status));
88 }
89
90 void status_uams(char *data, const char *authlist)
91 {
92     char                *start = data;
93     uint16_t           status;
94     struct uam_obj      *uams;
95     int         len, num = 0;
96
97     memcpy(&status, start + AFPSTATUS_UAMSOFF, sizeof(status));
98     uams = &uam_login;
99     while ((uams = uams->uam_prev) != &uam_login) {
100         if (strstr(authlist, uams->uam_path))
101             num++;
102     }
103
104     data += ntohs( status );
105     *data++ = num;
106     while ((uams = uams->uam_prev) != &uam_login) {
107         if (strstr(authlist, uams->uam_path)) {
108             LOG(log_info, logtype_afpd, "uam: \"%s\" available", uams->uam_name);
109             len = strlen( uams->uam_name);
110             *data++ = len;
111             memcpy( data, uams->uam_name, len );
112             data += len;
113         }
114     }
115
116     /* icon offset */
117     status = htons(data - start);
118     memcpy(start + AFPSTATUS_ICONOFF, &status, sizeof(status));
119 }
120
121 /* handle errors by closing the connection. this is only needed
122  * by the afp_* functions. */
123 static int send_reply(const AFPObj *obj, const int err)
124 {
125     if ((err == AFP_OK) || (err == AFPERR_AUTHCONT))
126         return err;
127
128     obj->reply(obj->dsi, err);
129     obj->exit(0);
130
131     return AFP_OK;
132 }
133
134 static int afp_errpwdexpired(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, 
135                              char *rbuf _U_, size_t *rbuflen)
136 {
137     *rbuflen = 0;
138     return AFPERR_PWDEXPR;
139 }
140
141 static int afp_null_nolog(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, 
142                           char *rbuf _U_, size_t *rbuflen)
143 {
144     *rbuflen = 0;
145     return( AFPERR_NOOP );
146 }
147
148 static int set_auth_switch(const AFPObj *obj, int expired)
149 {
150     int i;
151
152     if (expired) {
153         /*
154          * BF: expired password handling
155          * to allow the user to change his/her password we have to allow login
156          * but every following call except for FPChangePassword will be thrown
157          * away with an AFPERR_PWDEXPR error. (thanks to Leland Wallace from Apple
158          * for clarifying this)
159          */
160
161         for (i=0; i<=0xff; i++) {
162             uam_afpserver_action(i, UAM_AFPSERVER_PREAUTH, afp_errpwdexpired, NULL);
163         }
164         uam_afpserver_action(AFP_LOGOUT, UAM_AFPSERVER_PREAUTH, afp_logout, NULL);
165         uam_afpserver_action(AFP_CHANGEPW, UAM_AFPSERVER_PREAUTH, afp_changepw, NULL);
166     }
167     else {
168         afp_switch = postauth_switch;
169         switch (obj->afp_version) {
170
171         case 33:
172         case 32:
173 #ifdef HAVE_ACLS
174             uam_afpserver_action(AFP_GETACL, UAM_AFPSERVER_POSTAUTH, afp_getacl, NULL);
175             uam_afpserver_action(AFP_SETACL, UAM_AFPSERVER_POSTAUTH, afp_setacl, NULL);
176             uam_afpserver_action(AFP_ACCESS, UAM_AFPSERVER_POSTAUTH, afp_access, NULL);
177 #endif /* HAVE_ACLS */
178             uam_afpserver_action(AFP_GETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_getextattr, NULL);
179             uam_afpserver_action(AFP_SETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_setextattr, NULL);
180             uam_afpserver_action(AFP_REMOVEATTR, UAM_AFPSERVER_POSTAUTH, afp_remextattr, NULL);
181             uam_afpserver_action(AFP_LISTEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_listextattr, NULL);
182
183         case 31:
184             uam_afpserver_action(AFP_SYNCDIR, UAM_AFPSERVER_POSTAUTH, afp_syncdir, NULL);
185             uam_afpserver_action(AFP_SYNCFORK, UAM_AFPSERVER_POSTAUTH, afp_syncfork, NULL);
186             uam_afpserver_action(AFP_SPOTLIGHT_PRIVATE, UAM_AFPSERVER_POSTAUTH, afp_null_nolog, NULL);
187             uam_afpserver_action(AFP_ENUMERATE_EXT2, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext2, NULL);
188
189         case 30:
190             uam_afpserver_action(AFP_ENUMERATE_EXT, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext, NULL);
191             uam_afpserver_action(AFP_BYTELOCK_EXT,  UAM_AFPSERVER_POSTAUTH, afp_bytelock_ext, NULL);
192             /* catsearch_ext uses the same packet as catsearch FIXME double check this, it wasn't true for enue
193                enumerate_ext */
194             uam_afpserver_action(AFP_CATSEARCH_EXT, UAM_AFPSERVER_POSTAUTH, afp_catsearch_ext, NULL);
195             uam_afpserver_action(AFP_GETSESSTOKEN,  UAM_AFPSERVER_POSTAUTH, afp_getsession, NULL);
196             uam_afpserver_action(AFP_READ_EXT,      UAM_AFPSERVER_POSTAUTH, afp_read_ext, NULL);
197             uam_afpserver_action(AFP_WRITE_EXT,     UAM_AFPSERVER_POSTAUTH, afp_write_ext, NULL);
198             uam_afpserver_action(AFP_DISCTOLDSESS,  UAM_AFPSERVER_POSTAUTH, afp_disconnect, NULL);
199
200         case 22:
201             /*
202              * If first connection to a server is done in classic AFP2.2 version is used
203              * but OSX uses AFP3.x FPzzz command !
204              */
205             uam_afpserver_action(AFP_ZZZ,  UAM_AFPSERVER_POSTAUTH, afp_zzz, NULL);
206             break;
207         }
208     }
209
210     return AFP_OK;
211 }
212
213 #define GROUPSTR_BUFSIZE 1024
214 static const char *print_groups(int ngroups, gid_t *groups)
215 {
216     static char groupsstr[GROUPSTR_BUFSIZE];
217     int i;
218     char *s = groupsstr;
219
220     if (ngroups == 0)
221         return "-";
222
223     for (i = 0; (i < ngroups) && (s < &groupsstr[GROUPSTR_BUFSIZE]); i++) {
224         s += snprintf(s, &groupsstr[GROUPSTR_BUFSIZE] - s, " %u", groups[i]);
225     }
226
227     return groupsstr;
228 }
229
230 static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void), int expired)
231 {
232 #ifdef ADMIN_GRP
233     int admin = 0;
234 #endif /* ADMIN_GRP */
235
236     if ( pwd->pw_uid == 0 ) {   /* don't allow root login */
237         LOG(log_error, logtype_afpd, "login: root login denied!" );
238         return AFPERR_NOTAUTH;
239     }
240
241     LOG(log_note, logtype_afpd, "%s Login by %s",
242         afp_versions[afp_version_index].av_name, pwd->pw_name);
243
244     if (initgroups( pwd->pw_name, pwd->pw_gid ) < 0) {
245 #ifdef RUN_AS_USER
246         LOG(log_info, logtype_afpd, "running with uid %d", geteuid());
247 #else /* RUN_AS_USER */
248         LOG(log_error, logtype_afpd, "login: %s", strerror(errno));
249         return AFPERR_BADUAM;
250 #endif /* RUN_AS_USER */
251
252     }
253
254     /* Basically if the user is in the admin group, we stay root */
255
256     if ((obj->ngroups = getgroups( 0, NULL )) < 0 ) {
257         LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
258         return AFPERR_BADUAM;
259     }
260
261     if ( NULL == (obj->groups = calloc(obj->ngroups, sizeof(gid_t))) ) {
262         LOG(log_error, logtype_afpd, "login: %s calloc: %d", obj->ngroups);
263         return AFPERR_BADUAM;
264     }
265
266     if (( obj->ngroups = getgroups(obj->ngroups, obj->groups )) < 0 ) {
267         LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
268         return AFPERR_BADUAM;
269     }
270
271 #ifdef ADMIN_GRP
272     LOG(log_debug, logtype_afpd, "obj->options.admingid == %d", obj->options.admingid);
273
274     if (obj->options.admingid != 0) {
275         int i;
276         for (i = 0; i < obj->ngroups; i++) {
277             if (obj->groups[i] == obj->options.admingid) admin = 1;
278         }
279     }
280     if (admin) {
281         ad_setfuid(0);
282         LOG(log_info, logtype_afpd, "admin login -- %s", pwd->pw_name );
283     }
284     if (!admin)
285 #endif /* ADMIN_GRP */
286 #ifdef TRU64
287     {
288         struct DSI *dsi = obj->handle;
289         struct hostent *hp;
290         char *clientname;
291         int argc;
292         char **argv;
293         char hostname[256];
294
295         afp_get_cmdline( &argc, &argv );
296
297         hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
298                             sizeof( struct in_addr ),
299                             dsi->client.sin_family );
300
301         if( hp )
302             clientname = hp->h_name;
303         else
304             clientname = inet_ntoa( dsi->client.sin_addr );
305
306         sprintf( hostname, "%s@%s", pwd->pw_name, clientname );
307
308         if( sia_become_user( NULL, argc, argv, hostname, pwd->pw_name,
309                              NULL, FALSE, NULL, NULL,
310                              SIA_BEU_REALLOGIN ) != SIASUCCESS )
311             return AFPERR_BADUAM;
312
313         LOG(log_info, logtype_afpd, "session from %s (%s)", hostname,
314             inet_ntoa( dsi->client.sin_addr ) );
315
316         if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
317             LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
318             return AFPERR_BADUAM;
319         }
320     }
321 #else /* TRU64 */
322     if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
323         LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
324         return AFPERR_BADUAM;
325     }
326 #endif /* TRU64 */
327
328     LOG(log_debug, logtype_afpd, "login: supplementary groups: %s", print_groups(obj->ngroups, obj->groups));
329
330     /* There's probably a better way to do this, but for now, we just play root */
331 #ifdef ADMIN_GRP
332     if (admin)
333         obj->uid = 0;
334     else
335 #endif /* ADMIN_GRP */
336         obj->uid = geteuid();
337
338     set_auth_switch(obj, expired);
339     /* save our euid, we need it for preexec_close */
340     obj->uid = geteuid();
341     obj->logout = logout;
342
343     return( AFP_OK );
344 }
345
346 /* ---------------------- */
347 int afp_zzz(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
348 {
349     uint32_t data;
350     DSI *dsi = (DSI *)AFPobj->dsi;
351
352     *rbuflen = 0;
353     ibuf += 2;
354     ibuflen -= 2;
355
356     if (ibuflen < 4)
357         return AFPERR_MISC;
358     memcpy(&data, ibuf, 4); /* flag */
359     data = ntohl(data);
360
361     /*
362      * Possible sleeping states:
363      * 1) normal sleep: DSI_SLEEPING (up to 10.3)
364      * 2) extended sleep: DSI_SLEEPING | DSI_EXTSLEEP (starting with 10.4)
365      */
366
367     if (data & AFPZZZ_EXT_WAKEUP) {
368         /* wakeup request from exetended sleep */
369         if (dsi->flags & DSI_EXTSLEEP) {
370             LOG(log_note, logtype_afpd, "afp_zzz: waking up from extended sleep");
371             dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP);
372         }
373     } else {
374         /* sleep request */
375         dsi->flags |= DSI_SLEEPING;
376         if (data & AFPZZZ_EXT_SLEEP) {
377             LOG(log_note, logtype_afpd, "afp_zzz: entering extended sleep");
378             dsi->flags |= DSI_EXTSLEEP;
379         } else {
380             LOG(log_note, logtype_afpd, "afp_zzz: entering normal sleep");
381         }
382     }
383
384     /*
385      * According to AFP 3.3 spec we should not return anything,
386      * but eg 10.5.8 server still returns the numbers of hours
387      * the server is keeping the sessino (ie max sleeptime).
388      */
389     data = obj->options.sleep / 120; /* hours */
390     if (!data) {
391         data = 1;
392     }
393     *rbuflen = sizeof(data);
394     data = htonl(data);
395     memcpy(rbuf, &data, sizeof(data));
396     rbuf += sizeof(data);
397
398     return AFP_OK;
399 }
400
401 /* ---------------------- */
402 static int create_session_token(AFPObj *obj)
403 {
404     pid_t pid;
405
406     /* use 8 bytes for token as OSX, don't know if it helps */
407     if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
408         LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
409         return AFPERR_MISC;
410     }
411
412     if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
413         return AFPERR_MISC;
414
415     memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
416     obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
417     pid = getpid();
418     memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
419
420     return 0;
421 }
422
423 static int create_session_key(AFPObj *obj)
424 {
425     /* create session key */
426     if (obj->sinfo.sessionkey == NULL) {
427         if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
428             return AFPERR_MISC;
429         uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
430         obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
431     }
432     return AFP_OK;
433 }
434
435
436 /* ---------------------- */
437 int afp_getsession(
438     AFPObj *obj,
439     char   *ibuf, size_t ibuflen, 
440     char   *rbuf, size_t *rbuflen)
441 {
442     uint16_t           type;
443     uint32_t           idlen = 0;
444     uint32_t       boottime;
445     uint32_t           tklen, tp;
446     char                *token;
447     char                *p;
448
449     *rbuflen = 0;
450     tklen = 0;
451
452     if (ibuflen < 2 + sizeof(type)) {
453         return AFPERR_PARAM;
454     }
455
456     ibuf += 2;
457     ibuflen -= 2;
458
459     memcpy(&type, ibuf, sizeof(type));
460     type = ntohs(type);
461     ibuf += sizeof(type);
462     ibuflen -= sizeof(type);
463
464     if ( obj->sinfo.sessiontoken == NULL ) {
465         if ( create_session_token( obj ) )
466             return AFPERR_MISC;
467     }
468
469     /*
470      *
471      */
472     switch (type) {
473     case 0: /* old version ?*/
474         tklen = obj->sinfo.sessiontoken_len;
475         token = obj->sinfo.sessiontoken;
476         break;
477     case 1: /* disconnect */
478     case 2: /* reconnect update id */
479         if (ibuflen >= sizeof(idlen)) {
480             memcpy(&idlen, ibuf, sizeof(idlen));
481             idlen = ntohl(idlen);
482             ibuf += sizeof(idlen);
483             ibuflen -= sizeof(idlen);
484             if (ibuflen < idlen) {
485                 return AFPERR_PARAM;
486             }
487             /* memcpy (id, ibuf, idlen) */
488             tklen = obj->sinfo.sessiontoken_len;
489             token = obj->sinfo.sessiontoken;
490         }
491         break;
492     case 3:
493     case 4:
494         if (ibuflen >= 8 ) {
495             p = ibuf;
496             memcpy( &idlen, ibuf, sizeof(idlen));
497             idlen = ntohl(idlen);
498             ibuf += sizeof(idlen);
499             ibuflen -= sizeof(idlen);
500             ibuf += sizeof(boottime);
501             ibuflen -= sizeof(boottime);
502             if (ibuflen < idlen || idlen > (90-10)) {
503                 return AFPERR_PARAM;
504             }
505             if (!obj->sinfo.clientid) {
506                 obj->sinfo.clientid = malloc(idlen + 8);
507                 memcpy(obj->sinfo.clientid, p, idlen + 8);
508                 obj->sinfo.clientid_len = idlen + 8;
509             }
510             if (ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p) != 0)
511                 return AFPERR_MISC;
512             tklen = obj->sinfo.sessiontoken_len;
513             token = obj->sinfo.sessiontoken;
514         }
515         break;
516     case 8: /* Panther Kerberos Token */
517         tklen = obj->sinfo.cryptedkey_len;
518         token = obj->sinfo.cryptedkey;
519         break;
520     default:
521         return AFPERR_NOOP;
522         break;
523
524     }
525
526     if (tklen == 0)
527         return AFPERR_MISC;
528
529     tp = htonl(tklen);
530     memcpy(rbuf, &tp, sizeof(tklen));
531     rbuf += sizeof(tklen);
532     *rbuflen += sizeof(tklen);
533
534     memcpy(rbuf, token, tklen);
535     *rbuflen += tklen;
536
537     return AFP_OK;
538 }
539
540 /* ---------------------- */
541 int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
542 {
543     DSI                 *dsi = (DSI *)obj->dsi;
544     uint16_t           type;
545     uint32_t           tklen;
546     pid_t               token;
547     int                 i;
548
549     *rbuflen = 0;
550     ibuf += 2;
551
552 #if 0
553     /* check for guest user */
554     if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
555         return AFPERR_MISC;
556     }
557 #endif
558
559     memcpy(&type, ibuf, sizeof(type));
560     type = ntohs(type);
561     ibuf += sizeof(type);
562
563     memcpy(&tklen, ibuf, sizeof(tklen));
564     tklen = ntohl(tklen);
565     ibuf += sizeof(tklen);
566
567     if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
568         LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
569         return AFPERR_MISC;
570     }
571     if (tklen != SESSIONTOKEN_LEN) {
572         return AFPERR_MISC;
573     }
574     tklen = sizeof(pid_t);
575     memcpy(&token, ibuf, tklen);
576
577     /* our stuff is pid + zero pad */
578     ibuf += tklen;
579     for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
580         if (*ibuf != 0) {
581             return AFPERR_MISC;
582         }
583     }
584
585     LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
586     dsi->flags |= DSI_RECONINPROG;
587
588     /* Deactivate tickle timer */
589     const struct itimerval none = {{0, 0}, {0, 0}};
590     setitimer(ITIMER_REAL, &none, NULL);
591
592     /* check for old session, possibly transfering session from here to there */
593     if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) != 0)
594         goto exit;
595     /* write uint16_t DSI request ID */
596     if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
597         LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
598         goto exit;
599     }
600     /* now send our connected AFP client socket */
601     if (send_fd(obj->ipc_fd, dsi->socket) != 0)
602         goto exit;
603     /* Now see what happens: either afpd master sends us SIGTERM because our session */
604     /* has been transfered to a old disconnected session, or we continue    */
605     sleep(5);
606
607     if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
608         /* Reconnect succeeded, we exit now after sleeping some more */
609         sleep(2); /* sleep some more to give the recon. session time */
610         LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
611         exit(0);
612     }
613
614 exit:
615     /* Reinstall tickle timer */
616     setitimer(ITIMER_REAL, &dsi->timer, NULL);
617
618     LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
619     return AFPERR_MISC;
620 }
621
622 /* ---------------------- */
623 static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
624 {
625     int num,i;
626
627     if (!len || len > ibuflen)
628         return AFPERR_BADVERS;
629
630     num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
631     for ( i = 0; i < num; i++ ) {
632         if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
633             obj->afp_version = afp_versions[ i ].av_number;
634             afp_version_index = i;
635             break;
636         }
637     }
638     if ( i == num )                 /* An inappropo version */
639         return AFPERR_BADVERS ;
640
641     /* FIXME Hack */
642     if (obj->afp_version >= 30 && sizeof(off_t) != 8) {
643         LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
644         return AFPERR_BADVERS ;
645     }
646
647     return 0;
648 }
649
650 /* ---------------------- */
651 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
652 {
653     struct passwd *pwd = NULL;
654     size_t len;
655     int     i;
656
657     *rbuflen = 0;
658
659     if ( nologin & 1)
660         return send_reply(obj, AFPERR_SHUTDOWN );
661
662     if (ibuflen < 2)
663         return send_reply(obj, AFPERR_BADVERS );
664
665     ibuf++;
666     len = (unsigned char) *ibuf++;
667     ibuflen -= 2;
668
669     i = get_version(obj, ibuf, ibuflen, len);
670     if (i)
671         return send_reply(obj, i );
672
673     if (ibuflen <= len)
674         return send_reply(obj, AFPERR_BADUAM);
675
676     ibuf += len;
677     ibuflen -= len;
678
679     len = (unsigned char) *ibuf++;
680     ibuflen--;
681
682     if (!len || len > ibuflen)
683         return send_reply(obj, AFPERR_BADUAM);
684
685     if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
686         return send_reply(obj, AFPERR_BADUAM);
687     ibuf += len;
688     ibuflen -= len;
689
690     if (AFP_OK != (i = create_session_key(obj)) )
691         return send_reply(obj, i);
692
693     i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
694
695     if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
696         return send_reply(obj, i);
697
698     return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
699 }
700
701 /* ---------------------- */
702 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
703 {
704     struct passwd *pwd = NULL;
705     size_t  len;
706     int     i;
707     char        type;
708     uint16_t   len16;
709     char        *username;
710
711     *rbuflen = 0;
712
713     if ( nologin & 1)
714         return send_reply(obj, AFPERR_SHUTDOWN );
715
716     if (ibuflen < 5)
717         return send_reply(obj, AFPERR_BADVERS );
718
719     ibuf++;
720     ibuf++;     /* pad  */
721     ibuf +=2;   /* flag */
722
723     len = (unsigned char) *ibuf;
724     ibuf++;
725     ibuflen -= 5;
726
727     i = get_version(obj, ibuf, ibuflen, len);
728     if (i)
729         return send_reply(obj, i );
730
731     if (ibuflen <= len)
732         return send_reply(obj, AFPERR_BADUAM);
733
734     ibuf    += len;
735     ibuflen -= len;
736
737     len = (unsigned char) *ibuf;
738     ibuf++;
739     ibuflen--;
740
741     if (!len || len > ibuflen)
742         return send_reply(obj, AFPERR_BADUAM);
743
744     if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
745         return send_reply(obj, AFPERR_BADUAM);
746     ibuf    += len;
747     ibuflen -= len;
748
749     if (!afp_uam->u.uam_login.login_ext) {
750         LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
751         return send_reply(obj, AFPERR_BADUAM);
752     }
753     /* user name */
754     if (ibuflen <= 1 +sizeof(len16))
755         return send_reply(obj, AFPERR_PARAM);
756     type = *ibuf;
757     username = ibuf;
758     ibuf++;
759     ibuflen--;
760     if (type != 3)
761         return send_reply(obj, AFPERR_PARAM);
762
763     memcpy(&len16, ibuf, sizeof(len16));
764     ibuf += sizeof(len16);
765     ibuflen -= sizeof(len16);
766     len = ntohs(len16);
767     if (len > ibuflen)
768         return send_reply(obj, AFPERR_PARAM);
769     ibuf += len;
770     ibuflen -= len;
771
772     /* directory service name */
773     if (!ibuflen)
774         return send_reply(obj, AFPERR_PARAM);
775     type = *ibuf;
776     ibuf++;
777     ibuflen--;
778
779     switch(type) {
780     case 1:
781     case 2:
782         if (!ibuflen)
783             return send_reply(obj, AFPERR_PARAM);
784         len = (unsigned char) *ibuf;
785         ibuf++;
786         ibuflen--;
787         break;
788     case 3:
789         /* With "No User Authen" it is equal */
790         if (ibuflen < sizeof(len16))
791             return send_reply(obj, AFPERR_PARAM);
792         memcpy(&len16, ibuf, sizeof(len16));
793         ibuf += sizeof(len16);
794         ibuflen -= sizeof(len16);
795         len = ntohs(len16);
796         break;
797     default:
798         return send_reply(obj, AFPERR_PARAM);
799     }
800 #if 0
801     if (len != 0) {
802         LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
803         return send_reply(obj, AFPERR_PARAM);
804     }
805 #endif
806     ibuf += len;
807     ibuflen -= len;
808
809     /* Pad */
810     if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
811         ibuf++;
812         ibuflen--;
813     }
814
815     if (AFP_OK != (i = create_session_key(obj)) ) {
816         return send_reply(obj, i);
817     }
818
819     /* FIXME user name are in UTF8 */
820     i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
821
822     if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
823         return send_reply(obj, i);
824
825     return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
826 }
827
828 /* ---------------------- */
829 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
830 {
831     struct passwd *pwd = NULL;
832     int err;
833
834     if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
835         *rbuflen = 0;
836         return send_reply(obj, AFPERR_NOTAUTH );
837     }
838
839     ibuf += 2; ibuflen -= 2;
840     err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
841                                          rbuf, rbuflen);
842     if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
843         return send_reply(obj, err);
844
845     return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
846 }
847
848
849 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen  _U_, char *rbuf  _U_, size_t *rbuflen)
850 {
851     DSI *dsi = (DSI *)(obj->dsi);
852
853     LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
854     of_close_all_forks();
855     close_all_vol();
856     dsi->flags = DSI_AFP_LOGGED_OUT;
857     *rbuflen = 0;
858     return AFP_OK;
859 }
860
861
862
863 /* change password  --
864  * NOTE: an FPLogin must already have completed successfully for this
865  *       to work. this also does a little pre-processing before it hands
866  *       it off to the uam.
867  */
868 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
869 {
870     char username[MACFILELEN + 1], *start = ibuf;
871     struct uam_obj *uam;
872     struct passwd *pwd;
873     size_t len;
874     int    ret;
875
876     *rbuflen = 0;
877     ibuf += 2;
878
879     /* check if password change is allowed, OS-X ignores the flag.
880      * we shouldn't trust the client on this anyway.
881      * not sure about the "right" error code, NOOP for now */
882     if (!(obj->options.passwdbits & PASSWD_SET))
883         return AFPERR_NOOP;
884
885     /* make sure we can deal w/ this uam */
886     len = (unsigned char) *ibuf++;
887     if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
888         return AFPERR_BADUAM;
889
890     ibuf += len;
891     if ((len + 1) & 1) /* pad byte */
892         ibuf++;
893
894     if (obj->afp_version < 30) {
895         len = (unsigned char) *ibuf++;
896         if ( len > sizeof(username) - 1) {
897             return AFPERR_PARAM;
898         }
899         memcpy(username, ibuf, len);
900         username[ len ] = '\0';
901         ibuf += len;
902         if ((len + 1) & 1) /* pad byte */
903             ibuf++;
904     } else {
905         /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
906         if ( ibuf[0] != '\0' || ibuf[1] != '\0')
907             return AFPERR_PARAM;
908         ibuf += 2;
909         len = MIN(sizeof(username), strlen(obj->username));
910         memcpy(username, obj->username, len);
911         username[ len ] = '\0';
912     }
913
914
915     LOG(log_info, logtype_afpd, "changing password for <%s>", username);
916
917     if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
918         return AFPERR_PARAM;
919
920     /* send it off to the uam. we really don't use ibuflen right now. */
921     if (ibuflen < (size_t)(ibuf - start)) 
922         return AFPERR_PARAM;
923     
924     ibuflen -= (ibuf - start);
925     ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
926                               rbuf, rbuflen);
927     LOG(log_info, logtype_afpd, "password change %s.",
928         (ret == AFPERR_AUTHCONT) ? "continued" :
929         (ret ? "failed" : "succeeded"));
930     if ( ret == AFP_OK )
931         set_auth_switch(obj, 0);
932
933     return ret;
934 }
935
936
937 /* FPGetUserInfo */
938 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
939 {
940     uint8_t  thisuser;
941     uint32_t id;
942     uint16_t bitmap;
943     char *bitmapp;
944
945     LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
946
947     *rbuflen = 0;
948     ibuf++;
949     thisuser = *ibuf++;
950     ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
951     memcpy(&bitmap, ibuf, sizeof(bitmap));
952     bitmap = ntohs(bitmap);
953
954     /* deal with error cases. we don't have to worry about
955      * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
956      * succeed. */
957     if (!thisuser)
958         return AFPERR_PARAM;
959     if ((bitmap & USERIBIT_ALL) != bitmap)
960         return AFPERR_BITMAP;
961
962     /* remember place where we store the possibly modified bitmap later */
963     memcpy(rbuf, ibuf, sizeof(bitmap));
964     bitmapp = rbuf;
965     rbuf += sizeof(bitmap);
966     *rbuflen = sizeof(bitmap);
967
968     /* copy the user/group info */
969     if (bitmap & USERIBIT_USER) {
970         id = htonl(geteuid());
971         memcpy(rbuf, &id, sizeof(id));
972         rbuf += sizeof(id);
973         *rbuflen += sizeof(id);
974     }
975
976     if (bitmap & USERIBIT_GROUP) {
977         id = htonl(getegid());
978         memcpy(rbuf, &id, sizeof(id));
979         rbuf += sizeof(id);
980         *rbuflen += sizeof(id);
981     }
982
983     if (bitmap & USERIBIT_UUID) {
984         if ( ! (obj->options.flags & OPTION_UUID)) {
985             bitmap &= ~USERIBIT_UUID;
986             bitmap = htons(bitmap);
987             memcpy(bitmapp, &bitmap, sizeof(bitmap));
988         } else {
989             LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
990             int ret;
991             atalk_uuid_t uuid;
992             ret = getuuidfromname( obj->username, UUID_USER, uuid);
993             if (ret != 0) {
994                 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
995                 return AFPERR_NOITEM;
996             }
997             LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
998
999             memcpy(rbuf, uuid, UUID_BINSIZE);
1000             rbuf += UUID_BINSIZE;
1001             *rbuflen += UUID_BINSIZE;
1002         }
1003     }
1004
1005     LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
1006     return AFP_OK;
1007 }
1008
1009 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
1010                         (((type) == UAM_SERVER_CHANGEPW) ?              \
1011                          &uam_changepw : NULL))
1012
1013 /* just do a linked list search. this could be sped up with a hashed
1014  * list, but i doubt anyone's going to have enough uams to matter. */
1015 struct uam_obj *auth_uamfind(const int type, const char *name,
1016                              const int len)
1017 {
1018     struct uam_obj *prev, *start;
1019
1020     if (!name || !(start = UAM_LIST(type)))
1021         return NULL;
1022
1023     prev = start;
1024     while ((prev = prev->uam_prev) != start)
1025         if (strndiacasecmp(prev->uam_name, name, len) == 0)
1026             return prev;
1027
1028     return NULL;
1029 }
1030
1031 int auth_register(const int type, struct uam_obj *uam)
1032 {
1033     struct uam_obj *start;
1034
1035     if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1036         return -1;
1037
1038     if (!(start = UAM_LIST(type)))
1039         return 1; /* we don't know what to do with it, caller must free it */
1040
1041     uam_attach(start, uam);
1042     return 0;
1043 }
1044
1045 /* load all of the modules */
1046 int auth_load(const char *path, const char *list)
1047 {
1048     char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
1049     struct uam_mod *mod;
1050     struct stat st;
1051     size_t len;
1052
1053     if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1054         return -1;
1055
1056     strlcpy(buf, list, sizeof(buf));
1057     if ((p = strtok(buf, ",")) == NULL)
1058         return -1;
1059
1060     strcpy(name, path);
1061     if (name[len - 1] != '/') {
1062         strcat(name, "/");
1063         len++;
1064     }
1065
1066     while (p) {
1067         strlcpy(name + len, p, sizeof(name) - len);
1068         LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1069         /*
1070           if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1071         */
1072         if (stat(name, &st) == 0) {
1073             if ((mod = uam_load(name, p))) {
1074                 uam_attach(&uam_modules, mod);
1075                 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1076             } else {
1077                 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1078             }
1079         } else {
1080             LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1081         }
1082         p = strtok(NULL, ",");
1083     }
1084
1085     return 0;
1086 }
1087
1088 /* get rid of all of the uams */
1089 void auth_unload(void)
1090 {
1091     struct uam_mod *mod, *prev, *start = &uam_modules;
1092
1093     prev = start->uam_prev;
1094     while ((mod = prev) != start) {
1095         prev = prev->uam_prev;
1096         uam_detach(mod);
1097         uam_unload(mod);
1098     }
1099 }