]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/auth.c
d6382c84118bda9b89f4b801af9b520805494840
[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     if (afp_version >= 30 && obj->proto != AFPPROTO_DSI)
659         return AFPERR_BADVERS ;
660
661     /* FIXME Hack */
662     if (afp_version >= 30 && sizeof(off_t) != 8) {
663         LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
664         return AFPERR_BADVERS ;
665     }
666
667     return 0;
668 }
669
670 /* ---------------------- */
671 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
672 {
673     struct passwd *pwd = NULL;
674     size_t len;
675     int     i;
676
677     *rbuflen = 0;
678
679     if ( nologin & 1)
680         return send_reply(obj, AFPERR_SHUTDOWN );
681
682     if (ibuflen < 2)
683         return send_reply(obj, AFPERR_BADVERS );
684
685     ibuf++;
686     len = (unsigned char) *ibuf++;
687     ibuflen -= 2;
688
689     i = get_version(obj, ibuf, ibuflen, len);
690     if (i)
691         return send_reply(obj, i );
692
693     if (ibuflen <= len)
694         return send_reply(obj, AFPERR_BADUAM);
695
696     ibuf += len;
697     ibuflen -= len;
698
699     len = (unsigned char) *ibuf++;
700     ibuflen--;
701
702     if (!len || len > ibuflen)
703         return send_reply(obj, AFPERR_BADUAM);
704
705     if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
706         return send_reply(obj, AFPERR_BADUAM);
707     ibuf += len;
708     ibuflen -= len;
709
710     if (AFP_OK != (i = create_session_key(obj)) )
711         return send_reply(obj, i);
712
713     i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
714
715     if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
716         return send_reply(obj, i);
717
718     return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
719 }
720
721 /* ---------------------- */
722 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
723 {
724     struct passwd *pwd = NULL;
725     size_t  len;
726     int     i;
727     char        type;
728     uint16_t   len16;
729     char        *username;
730
731     *rbuflen = 0;
732
733     if ( nologin & 1)
734         return send_reply(obj, AFPERR_SHUTDOWN );
735
736     if (ibuflen < 5)
737         return send_reply(obj, AFPERR_BADVERS );
738
739     ibuf++;
740     ibuf++;     /* pad  */
741     ibuf +=2;   /* flag */
742
743     len = (unsigned char) *ibuf;
744     ibuf++;
745     ibuflen -= 5;
746
747     i = get_version(obj, ibuf, ibuflen, len);
748     if (i)
749         return send_reply(obj, i );
750
751     if (ibuflen <= len)
752         return send_reply(obj, AFPERR_BADUAM);
753
754     ibuf    += len;
755     ibuflen -= len;
756
757     len = (unsigned char) *ibuf;
758     ibuf++;
759     ibuflen--;
760
761     if (!len || len > ibuflen)
762         return send_reply(obj, AFPERR_BADUAM);
763
764     if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
765         return send_reply(obj, AFPERR_BADUAM);
766     ibuf    += len;
767     ibuflen -= len;
768
769     if (!afp_uam->u.uam_login.login_ext) {
770         LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
771         return send_reply(obj, AFPERR_BADUAM);
772     }
773     /* user name */
774     if (ibuflen <= 1 +sizeof(len16))
775         return send_reply(obj, AFPERR_PARAM);
776     type = *ibuf;
777     username = ibuf;
778     ibuf++;
779     ibuflen--;
780     if (type != 3)
781         return send_reply(obj, AFPERR_PARAM);
782
783     memcpy(&len16, ibuf, sizeof(len16));
784     ibuf += sizeof(len16);
785     ibuflen -= sizeof(len16);
786     len = ntohs(len16);
787     if (len > ibuflen)
788         return send_reply(obj, AFPERR_PARAM);
789     ibuf += len;
790     ibuflen -= len;
791
792     /* directory service name */
793     if (!ibuflen)
794         return send_reply(obj, AFPERR_PARAM);
795     type = *ibuf;
796     ibuf++;
797     ibuflen--;
798
799     switch(type) {
800     case 1:
801     case 2:
802         if (!ibuflen)
803             return send_reply(obj, AFPERR_PARAM);
804         len = (unsigned char) *ibuf;
805         ibuf++;
806         ibuflen--;
807         break;
808     case 3:
809         /* With "No User Authen" it is equal */
810         if (ibuflen < sizeof(len16))
811             return send_reply(obj, AFPERR_PARAM);
812         memcpy(&len16, ibuf, sizeof(len16));
813         ibuf += sizeof(len16);
814         ibuflen -= sizeof(len16);
815         len = ntohs(len16);
816         break;
817     default:
818         return send_reply(obj, AFPERR_PARAM);
819     }
820 #if 0
821     if (len != 0) {
822         LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
823         return send_reply(obj, AFPERR_PARAM);
824     }
825 #endif
826     ibuf += len;
827     ibuflen -= len;
828
829     /* Pad */
830     if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
831         ibuf++;
832         ibuflen--;
833     }
834
835     if (AFP_OK != (i = create_session_key(obj)) ) {
836         return send_reply(obj, i);
837     }
838
839     /* FIXME user name are in UTF8 */
840     i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
841
842     if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
843         return send_reply(obj, i);
844
845     return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
846 }
847
848 /* ---------------------- */
849 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
850 {
851     struct passwd *pwd = NULL;
852     int err;
853
854     if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
855         *rbuflen = 0;
856         return send_reply(obj, AFPERR_NOTAUTH );
857     }
858
859     ibuf += 2; ibuflen -= 2;
860     err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
861                                          rbuf, rbuflen);
862     if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
863         return send_reply(obj, err);
864
865     return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
866 }
867
868
869 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen  _U_, char *rbuf  _U_, size_t *rbuflen)
870 {
871     DSI *dsi = (DSI *)(obj->dsi);
872
873     LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
874     of_close_all_forks();
875     close_all_vol();
876     dsi->flags = DSI_AFP_LOGGED_OUT;
877     *rbuflen = 0;
878     return AFP_OK;
879 }
880
881
882
883 /* change password  --
884  * NOTE: an FPLogin must already have completed successfully for this
885  *       to work. this also does a little pre-processing before it hands
886  *       it off to the uam.
887  */
888 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
889 {
890     char username[MACFILELEN + 1], *start = ibuf;
891     struct uam_obj *uam;
892     struct passwd *pwd;
893     size_t len;
894     int    ret;
895
896     *rbuflen = 0;
897     ibuf += 2;
898
899     /* check if password change is allowed, OS-X ignores the flag.
900      * we shouldn't trust the client on this anyway.
901      * not sure about the "right" error code, NOOP for now */
902     if (!(obj->options.passwdbits & PASSWD_SET))
903         return AFPERR_NOOP;
904
905     /* make sure we can deal w/ this uam */
906     len = (unsigned char) *ibuf++;
907     if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
908         return AFPERR_BADUAM;
909
910     ibuf += len;
911     if ((len + 1) & 1) /* pad byte */
912         ibuf++;
913
914     if ( afp_version < 30) {
915         len = (unsigned char) *ibuf++;
916         if ( len > sizeof(username) - 1) {
917             return AFPERR_PARAM;
918         }
919         memcpy(username, ibuf, len);
920         username[ len ] = '\0';
921         ibuf += len;
922         if ((len + 1) & 1) /* pad byte */
923             ibuf++;
924     } else {
925         /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
926         if ( ibuf[0] != '\0' || ibuf[1] != '\0')
927             return AFPERR_PARAM;
928         ibuf += 2;
929         len = MIN(sizeof(username), strlen(obj->username));
930         memcpy(username, obj->username, len);
931         username[ len ] = '\0';
932     }
933
934
935     LOG(log_info, logtype_afpd, "changing password for <%s>", username);
936
937     if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
938         return AFPERR_PARAM;
939
940     /* send it off to the uam. we really don't use ibuflen right now. */
941     if (ibuflen < (size_t)(ibuf - start)) 
942         return AFPERR_PARAM;
943     
944     ibuflen -= (ibuf - start);
945     ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
946                               rbuf, rbuflen);
947     LOG(log_info, logtype_afpd, "password change %s.",
948         (ret == AFPERR_AUTHCONT) ? "continued" :
949         (ret ? "failed" : "succeeded"));
950     if ( ret == AFP_OK )
951         set_auth_switch(0);
952
953     return ret;
954 }
955
956
957 /* FPGetUserInfo */
958 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
959 {
960     uint8_t  thisuser;
961     uint32_t id;
962     uint16_t bitmap;
963     char *bitmapp;
964
965     LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
966
967     *rbuflen = 0;
968     ibuf++;
969     thisuser = *ibuf++;
970     ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
971     memcpy(&bitmap, ibuf, sizeof(bitmap));
972     bitmap = ntohs(bitmap);
973
974     /* deal with error cases. we don't have to worry about
975      * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
976      * succeed. */
977     if (!thisuser)
978         return AFPERR_PARAM;
979     if ((bitmap & USERIBIT_ALL) != bitmap)
980         return AFPERR_BITMAP;
981
982     /* remember place where we store the possibly modified bitmap later */
983     memcpy(rbuf, ibuf, sizeof(bitmap));
984     bitmapp = rbuf;
985     rbuf += sizeof(bitmap);
986     *rbuflen = sizeof(bitmap);
987
988     /* copy the user/group info */
989     if (bitmap & USERIBIT_USER) {
990         id = htonl(geteuid());
991         memcpy(rbuf, &id, sizeof(id));
992         rbuf += sizeof(id);
993         *rbuflen += sizeof(id);
994     }
995
996     if (bitmap & USERIBIT_GROUP) {
997         id = htonl(getegid());
998         memcpy(rbuf, &id, sizeof(id));
999         rbuf += sizeof(id);
1000         *rbuflen += sizeof(id);
1001     }
1002
1003     if (bitmap & USERIBIT_UUID) {
1004         if ( ! (obj->options.flags & OPTION_UUID)) {
1005             bitmap &= ~USERIBIT_UUID;
1006             bitmap = htons(bitmap);
1007             memcpy(bitmapp, &bitmap, sizeof(bitmap));
1008         } else {
1009             LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
1010             int ret;
1011             atalk_uuid_t uuid;
1012             ret = getuuidfromname( obj->username, UUID_USER, uuid);
1013             if (ret != 0) {
1014                 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
1015                 return AFPERR_NOITEM;
1016             }
1017             LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
1018
1019             memcpy(rbuf, uuid, UUID_BINSIZE);
1020             rbuf += UUID_BINSIZE;
1021             *rbuflen += UUID_BINSIZE;
1022         }
1023     }
1024
1025     LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
1026     return AFP_OK;
1027 }
1028
1029 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
1030                         (((type) == UAM_SERVER_CHANGEPW) ?              \
1031                          &uam_changepw : NULL))
1032
1033 /* just do a linked list search. this could be sped up with a hashed
1034  * list, but i doubt anyone's going to have enough uams to matter. */
1035 struct uam_obj *auth_uamfind(const int type, const char *name,
1036                              const int len)
1037 {
1038     struct uam_obj *prev, *start;
1039
1040     if (!name || !(start = UAM_LIST(type)))
1041         return NULL;
1042
1043     prev = start;
1044     while ((prev = prev->uam_prev) != start)
1045         if (strndiacasecmp(prev->uam_name, name, len) == 0)
1046             return prev;
1047
1048     return NULL;
1049 }
1050
1051 int auth_register(const int type, struct uam_obj *uam)
1052 {
1053     struct uam_obj *start;
1054
1055     if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1056         return -1;
1057
1058     if (!(start = UAM_LIST(type)))
1059         return 1; /* we don't know what to do with it, caller must free it */
1060
1061     uam_attach(start, uam);
1062     return 0;
1063 }
1064
1065 /* load all of the modules */
1066 int auth_load(const char *path, const char *list)
1067 {
1068     char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
1069     struct uam_mod *mod;
1070     struct stat st;
1071     size_t len;
1072
1073     if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1074         return -1;
1075
1076     strlcpy(buf, list, sizeof(buf));
1077     if ((p = strtok(buf, ",")) == NULL)
1078         return -1;
1079
1080     strcpy(name, path);
1081     if (name[len - 1] != '/') {
1082         strcat(name, "/");
1083         len++;
1084     }
1085
1086     while (p) {
1087         strlcpy(name + len, p, sizeof(name) - len);
1088         LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1089         /*
1090           if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1091         */
1092         if (stat(name, &st) == 0) {
1093             if ((mod = uam_load(name, p))) {
1094                 uam_attach(&uam_modules, mod);
1095                 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1096             } else {
1097                 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1098             }
1099         } else {
1100             LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1101         }
1102         p = strtok(NULL, ",");
1103     }
1104
1105     return 0;
1106 }
1107
1108 /* get rid of all of the uams */
1109 void auth_unload(void)
1110 {
1111     struct uam_mod *mod, *prev, *start = &uam_modules;
1112
1113     prev = start->uam_prev;
1114     while ((mod = prev) != start) {
1115         prev = prev->uam_prev;
1116         uam_detach(mod);
1117         uam_unload(mod);
1118     }
1119 }