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