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