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