2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
15 #endif /* HAVE_UNISTD_H */
16 #include <sys/types.h>
17 #include <sys/param.h>
19 #include <netatalk/endian.h>
20 #include <atalk/afp.h>
21 #include <atalk/compat.h>
22 #include <atalk/util.h>
32 #include <arpa/inet.h>
36 extern void afp_get_cmdline( int *ac, char ***av );
39 #include <atalk/logger.h>
40 #include <atalk/server_ipc.h>
41 #include <atalk/uuid.h>
55 static int afp_version_index;
59 #if defined( sun ) && !defined( __svr4__ ) || defined( ultrix )
62 #define GROUPS_SIZE sizeof(int)
64 #else /* sun __svr4__ ultrix */
67 #define GROUPS_SIZE sizeof(gid_t)
68 #endif /* sun ultrix */
73 * These numbers are scattered throughout the code.
75 static struct afp_versions afp_versions[] = {
77 { "AFPVersion 1.1", 11 },
78 { "AFPVersion 2.0", 20 },
79 { "AFPVersion 2.1", 21 },
88 static struct uam_mod uam_modules = {NULL, NULL, &uam_modules, &uam_modules};
89 static struct uam_obj uam_login = {"", "", 0, {{NULL, NULL, NULL, NULL }}, &uam_login,
91 static struct uam_obj uam_changepw = {"", "", 0, {{NULL, NULL, NULL, NULL}}, &uam_changepw,
94 static struct uam_obj *afp_uam = NULL;
97 void status_versions( char *data, const ASP asp, const DSI *dsi)
101 int len, num, i, count = 0;
103 memcpy(&status, start + AFPSTATUS_VERSOFF, sizeof(status));
104 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ] );
106 for ( i = 0; i < num; i++ ) {
108 if ( !asp && (afp_versions[ i ].av_number <= 21)) continue;
109 #endif /* ! NO_DDP */
110 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
113 data += ntohs( status );
116 for ( i = 0; i < num; i++ ) {
118 if ( !asp && (afp_versions[ i ].av_number <= 21)) continue;
119 #endif /* ! NO_DDP */
120 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
121 len = strlen( afp_versions[ i ].av_name );
123 memcpy( data, afp_versions[ i ].av_name , len );
126 status = htons( data - start );
127 memcpy(start + AFPSTATUS_UAMSOFF, &status, sizeof(status));
130 void status_uams(char *data, const char *authlist)
134 struct uam_obj *uams;
137 memcpy(&status, start + AFPSTATUS_UAMSOFF, sizeof(status));
139 while ((uams = uams->uam_prev) != &uam_login) {
140 if (strstr(authlist, uams->uam_path))
144 data += ntohs( status );
146 while ((uams = uams->uam_prev) != &uam_login) {
147 if (strstr(authlist, uams->uam_path)) {
148 LOG(log_info, logtype_afpd, "uam: \"%s\" available", uams->uam_name);
149 len = strlen( uams->uam_name);
151 memcpy( data, uams->uam_name, len );
157 status = htons(data - start);
158 memcpy(start + AFPSTATUS_ICONOFF, &status, sizeof(status));
161 /* handle errors by closing the connection. this is only needed
162 * by the afp_* functions. */
163 static int send_reply(const AFPObj *obj, const int err)
165 if ((err == AFP_OK) || (err == AFPERR_AUTHCONT))
168 obj->reply(obj->handle, err);
174 static int afp_errpwdexpired(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
175 char *rbuf _U_, size_t *rbuflen)
178 return AFPERR_PWDEXPR;
181 static int afp_null_nolog(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
182 char *rbuf _U_, size_t *rbuflen)
185 return( AFPERR_NOOP );
188 static int set_auth_switch(int expired)
194 * BF: expired password handling
195 * to allow the user to change his/her password we have to allow login
196 * but every following call except for FPChangePassword will be thrown
197 * away with an AFPERR_PWDEXPR error. (thanks to Leland Wallace from Apple
198 * for clarifying this)
201 for (i=0; i<=0xff; i++) {
202 uam_afpserver_action(i, UAM_AFPSERVER_PREAUTH, afp_errpwdexpired, NULL);
204 uam_afpserver_action(AFP_LOGOUT, UAM_AFPSERVER_PREAUTH, afp_logout, NULL);
205 uam_afpserver_action(AFP_CHANGEPW, UAM_AFPSERVER_PREAUTH, afp_changepw, NULL);
208 afp_switch = postauth_switch;
209 switch (afp_version) {
214 uam_afpserver_action(AFP_GETACL, UAM_AFPSERVER_POSTAUTH, afp_getacl, NULL);
215 uam_afpserver_action(AFP_SETACL, UAM_AFPSERVER_POSTAUTH, afp_setacl, NULL);
216 uam_afpserver_action(AFP_ACCESS, UAM_AFPSERVER_POSTAUTH, afp_access, NULL);
217 #endif /* HAVE_ACLS */
218 uam_afpserver_action(AFP_GETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_getextattr, NULL);
219 uam_afpserver_action(AFP_SETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_setextattr, NULL);
220 uam_afpserver_action(AFP_REMOVEATTR, UAM_AFPSERVER_POSTAUTH, afp_remextattr, NULL);
221 uam_afpserver_action(AFP_LISTEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_listextattr, NULL);
224 uam_afpserver_action(AFP_SYNCDIR, UAM_AFPSERVER_POSTAUTH, afp_syncdir, NULL);
225 uam_afpserver_action(AFP_SYNCFORK, UAM_AFPSERVER_POSTAUTH, afp_syncfork, NULL);
226 uam_afpserver_action(AFP_SPOTLIGHT_PRIVATE, UAM_AFPSERVER_POSTAUTH, afp_null_nolog, NULL);
227 uam_afpserver_action(AFP_ENUMERATE_EXT2, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext2, NULL);
230 uam_afpserver_action(AFP_ENUMERATE_EXT, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext, NULL);
231 uam_afpserver_action(AFP_BYTELOCK_EXT, UAM_AFPSERVER_POSTAUTH, afp_bytelock_ext, NULL);
232 /* catsearch_ext uses the same packet as catsearch FIXME double check this, it wasn't true for enue
234 uam_afpserver_action(AFP_CATSEARCH_EXT, UAM_AFPSERVER_POSTAUTH, afp_catsearch_ext, NULL);
235 uam_afpserver_action(AFP_GETSESSTOKEN, UAM_AFPSERVER_POSTAUTH, afp_getsession, NULL);
236 uam_afpserver_action(AFP_READ_EXT, UAM_AFPSERVER_POSTAUTH, afp_read_ext, NULL);
237 uam_afpserver_action(AFP_WRITE_EXT, UAM_AFPSERVER_POSTAUTH, afp_write_ext, NULL);
238 uam_afpserver_action(AFP_DISCTOLDSESS, UAM_AFPSERVER_POSTAUTH, afp_disconnect, NULL);
242 * If first connection to a server is done in classic AFP2.2 version is used
243 * but OSX uses AFP3.x FPzzz command !
245 uam_afpserver_action(AFP_ZZZ, UAM_AFPSERVER_POSTAUTH, afp_zzz, NULL);
253 #define GROUPSTR_BUFSIZE 1024
254 static const char *print_groups(int ngroups, gid_t *groups)
256 static char groupsstr[GROUPSTR_BUFSIZE];
263 for (i = 0; (i < ngroups) && (s < &groupsstr[GROUPSTR_BUFSIZE]); i++) {
264 s += snprintf(s, &groupsstr[GROUPSTR_BUFSIZE] - s, " %u", groups[i]);
270 static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void), int expired)
274 #endif /* ADMIN_GRP */
277 set_processname("afpd");
280 if ( pwd->pw_uid == 0 ) { /* don't allow root login */
281 LOG(log_error, logtype_afpd, "login: root login denied!" );
282 return AFPERR_NOTAUTH;
285 LOG(log_note, logtype_afpd, "%s Login by %s",
286 afp_versions[afp_version_index].av_name, pwd->pw_name);
289 if (obj->proto == AFPPROTO_ASP) {
290 ASP asp = obj->handle;
291 int addr_net = ntohs( asp->asp_sat.sat_addr.s_net );
292 int addr_node = asp->asp_sat.sat_addr.s_node;
294 if (obj->options.authprintdir) {
295 if(addr_net && addr_node) { /* Do we have a valid Appletalk address? */
298 int mypid = getpid();
299 struct stat stat_buf;
301 sprintf(nodename, "%s/net%d.%dnode%d", obj->options.authprintdir,
302 addr_net / 256, addr_net % 256, addr_node);
303 LOG(log_info, logtype_afpd, "registering %s (uid %d) on %u.%u as %s",
304 pwd->pw_name, pwd->pw_uid, addr_net, addr_node, nodename);
306 if (stat(nodename, &stat_buf) == 0) { /* file exists */
307 if (S_ISREG(stat_buf.st_mode)) { /* normal file */
309 fp = fopen(nodename, "w");
310 fprintf(fp, "%s:%d\n", pwd->pw_name, mypid);
312 chown( nodename, pwd->pw_uid, -1 );
313 } else { /* somebody is messing with us */
314 LOG(log_error, logtype_afpd, "print authfile %s is not a normal file, it will not be modified", nodename );
316 } else { /* file 'nodename' does not exist */
317 fp = fopen(nodename, "w");
318 fprintf(fp, "%s:%d\n", pwd->pw_name, mypid);
320 chown( nodename, pwd->pw_uid, -1 );
322 } /* if (addr_net && addr_node ) */
323 } /* if (options->authprintdir) */
324 } /* if (obj->proto == AFPPROTO_ASP) */
327 if (initgroups( pwd->pw_name, pwd->pw_gid ) < 0) {
329 LOG(log_info, logtype_afpd, "running with uid %d", geteuid());
330 #else /* RUN_AS_USER */
331 LOG(log_error, logtype_afpd, "login: %s", strerror(errno));
332 return AFPERR_BADUAM;
333 #endif /* RUN_AS_USER */
337 /* Basically if the user is in the admin group, we stay root */
339 if (( ngroups = getgroups( 0, NULL )) < 0 ) {
340 LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
341 return AFPERR_BADUAM;
344 if ( NULL == (groups = calloc(ngroups, GROUPS_SIZE)) ) {
345 LOG(log_error, logtype_afpd, "login: %s calloc: %d", ngroups);
346 return AFPERR_BADUAM;
349 if (( ngroups = getgroups( ngroups, groups )) < 0 ) {
350 LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
351 return AFPERR_BADUAM;
355 LOG(log_debug, logtype_afpd, "obj->options.admingid == %d", obj->options.admingid);
357 if (obj->options.admingid != 0) {
359 for (i = 0; i < ngroups; i++) {
360 if (groups[i] == obj->options.admingid) admin = 1;
365 LOG(log_info, logtype_afpd, "admin login -- %s", pwd->pw_name );
368 #endif /* ADMIN_GRP */
371 struct DSI *dsi = obj->handle;
378 afp_get_cmdline( &argc, &argv );
380 hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
381 sizeof( struct in_addr ),
382 dsi->client.sin_family );
385 clientname = hp->h_name;
387 clientname = inet_ntoa( dsi->client.sin_addr );
389 sprintf( hostname, "%s@%s", pwd->pw_name, clientname );
391 if( sia_become_user( NULL, argc, argv, hostname, pwd->pw_name,
392 NULL, FALSE, NULL, NULL,
393 SIA_BEU_REALLOGIN ) != SIASUCCESS )
394 return AFPERR_BADUAM;
396 LOG(log_info, logtype_afpd, "session from %s (%s)", hostname,
397 inet_ntoa( dsi->client.sin_addr ) );
399 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
400 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
401 return AFPERR_BADUAM;
405 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
406 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
407 return AFPERR_BADUAM;
411 LOG(log_debug, logtype_afpd, "login: supplementary groups: %s", print_groups(ngroups, groups));
413 /* There's probably a better way to do this, but for now, we just play root */
418 #endif /* ADMIN_GRP */
421 set_auth_switch(expired);
422 /* save our euid, we need it for preexec_close */
423 obj->uid = geteuid();
424 obj->logout = logout;
428 save_uidgid ( &obj->uidgid );
434 /* ---------------------- */
435 int afp_zzz(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
438 DSI *dsi = (DSI *)AFPobj->handle;
446 memcpy(&data, ibuf, 4); /* flag */
450 * Possible sleeping states:
451 * 1) normal sleep: DSI_SLEEPING (up to 10.3)
452 * 2) extended sleep: DSI_SLEEPING | DSI_EXTSLEEP (starting with 10.4)
455 if (data & AFPZZZ_EXT_WAKEUP) {
456 /* wakeup request from exetended sleep */
457 if (dsi->flags & DSI_EXTSLEEP) {
458 LOG(log_note, logtype_afpd, "afp_zzz: waking up from extended sleep");
459 dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP);
463 dsi->flags |= DSI_SLEEPING;
464 if (data & AFPZZZ_EXT_SLEEP) {
465 LOG(log_note, logtype_afpd, "afp_zzz: entering extended sleep");
466 dsi->flags |= DSI_EXTSLEEP;
468 LOG(log_note, logtype_afpd, "afp_zzz: entering normal sleep");
473 * According to AFP 3.3 spec we should not return anything,
474 * but eg 10.5.8 server still returns the numbers of hours
475 * the server is keeping the sessino (ie max sleeptime).
477 data = obj->options.sleep / 120; /* hours */
481 *rbuflen = sizeof(data);
483 memcpy(rbuf, &data, sizeof(data));
484 rbuf += sizeof(data);
489 /* ---------------------- */
490 static int create_session_token(AFPObj *obj)
494 /* use 8 bytes for token as OSX, don't know if it helps */
495 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
496 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
500 if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
503 memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
504 obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
506 memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
511 static int create_session_key(AFPObj *obj)
513 /* create session key */
514 if (obj->sinfo.sessionkey == NULL) {
515 if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
517 uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
518 obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
524 /* ---------------------- */
527 char *ibuf, size_t ibuflen,
528 char *rbuf, size_t *rbuflen)
540 if (ibuflen < 2 + sizeof(type)) {
547 memcpy(&type, ibuf, sizeof(type));
549 ibuf += sizeof(type);
550 ibuflen -= sizeof(type);
552 if ( obj->sinfo.sessiontoken == NULL ) {
553 if ( create_session_token( obj ) )
561 case 0: /* old version ?*/
562 tklen = obj->sinfo.sessiontoken_len;
563 token = obj->sinfo.sessiontoken;
565 case 1: /* disconnect */
566 case 2: /* reconnect update id */
567 if (ibuflen >= sizeof(idlen)) {
568 memcpy(&idlen, ibuf, sizeof(idlen));
569 idlen = ntohl(idlen);
570 ibuf += sizeof(idlen);
571 ibuflen -= sizeof(idlen);
572 if (ibuflen < idlen) {
575 /* memcpy (id, ibuf, idlen) */
576 tklen = obj->sinfo.sessiontoken_len;
577 token = obj->sinfo.sessiontoken;
584 memcpy( &idlen, ibuf, sizeof(idlen));
585 idlen = ntohl(idlen);
586 ibuf += sizeof(idlen);
587 ibuflen -= sizeof(idlen);
588 ibuf += sizeof(boottime);
589 ibuflen -= sizeof(boottime);
590 if (ibuflen < idlen || idlen > (90-10)) {
593 ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p);
594 tklen = obj->sinfo.sessiontoken_len;
595 token = obj->sinfo.sessiontoken;
598 case 8: /* Panther Kerberos Token */
599 tklen = obj->sinfo.cryptedkey_len;
600 token = obj->sinfo.cryptedkey;
612 memcpy(rbuf, &tp, sizeof(tklen));
613 rbuf += sizeof(tklen);
614 *rbuflen += sizeof(tklen);
616 memcpy(rbuf, token, tklen);
622 /* ---------------------- */
623 int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
625 DSI *dsi = (DSI *)obj->handle;
635 /* check for guest user */
636 if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
641 memcpy(&type, ibuf, sizeof(type));
643 ibuf += sizeof(type);
645 memcpy(&tklen, ibuf, sizeof(tklen));
646 tklen = ntohl(tklen);
647 ibuf += sizeof(tklen);
649 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
650 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
653 if (tklen != SESSIONTOKEN_LEN) {
656 tklen = sizeof(pid_t);
657 memcpy(&token, ibuf, tklen);
659 /* our stuff is pid + zero pad */
661 for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
667 LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
668 dsi->flags |= DSI_RECONINPROG;
670 /* Deactivate tickle timer */
671 const struct itimerval none = {{0, 0}, {0, 0}};
672 setitimer(ITIMER_REAL, &none, NULL);
674 /* check for old session, possibly transfering session from here to there */
675 if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) == -1)
677 /* write uint16_t DSI request ID */
678 if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
679 LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
682 /* now send our connected AFP client socket */
683 if (send_fd(obj->ipc_fd, dsi->socket) != 0)
685 /* Now see what happens: either afpd master sends us SIGTERM because our session */
686 /* has been transfered to a old disconnected session, or we continue */
689 if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
690 /* Reconnect succeeded, we exit now after sleeping some more */
691 sleep(2); /* sleep some more to give the recon. session time */
692 LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
697 /* Reinstall tickle timer */
698 setitimer(ITIMER_REAL, &dsi->timer, NULL);
700 LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
704 /* ---------------------- */
705 static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
709 if (!len || len > ibuflen)
710 return AFPERR_BADVERS;
712 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
713 for ( i = 0; i < num; i++ ) {
714 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
715 afp_version = afp_versions[ i ].av_number;
716 afp_version_index = i;
720 if ( i == num ) /* An inappropo version */
721 return AFPERR_BADVERS ;
723 if (afp_version >= 30 && obj->proto != AFPPROTO_DSI)
724 return AFPERR_BADVERS ;
727 if (afp_version >= 30 && sizeof(off_t) != 8) {
728 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
729 return AFPERR_BADVERS ;
735 /* ---------------------- */
736 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
738 struct passwd *pwd = NULL;
745 return send_reply(obj, AFPERR_SHUTDOWN );
748 return send_reply(obj, AFPERR_BADVERS );
751 len = (unsigned char) *ibuf++;
754 i = get_version(obj, ibuf, ibuflen, len);
756 return send_reply(obj, i );
759 return send_reply(obj, AFPERR_BADUAM);
764 len = (unsigned char) *ibuf++;
767 if (!len || len > ibuflen)
768 return send_reply(obj, AFPERR_BADUAM);
770 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
771 return send_reply(obj, AFPERR_BADUAM);
775 if (AFP_OK != (i = create_session_key(obj)) )
776 return send_reply(obj, i);
778 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
780 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
781 return send_reply(obj, i);
783 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
786 /* ---------------------- */
787 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
789 struct passwd *pwd = NULL;
799 return send_reply(obj, AFPERR_SHUTDOWN );
802 return send_reply(obj, AFPERR_BADVERS );
808 len = (unsigned char) *ibuf;
812 i = get_version(obj, ibuf, ibuflen, len);
814 return send_reply(obj, i );
817 return send_reply(obj, AFPERR_BADUAM);
822 len = (unsigned char) *ibuf;
826 if (!len || len > ibuflen)
827 return send_reply(obj, AFPERR_BADUAM);
829 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
830 return send_reply(obj, AFPERR_BADUAM);
834 if (!afp_uam->u.uam_login.login_ext) {
835 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
836 return send_reply(obj, AFPERR_BADUAM);
839 if (ibuflen <= 1 +sizeof(len16))
840 return send_reply(obj, AFPERR_PARAM);
846 return send_reply(obj, AFPERR_PARAM);
848 memcpy(&len16, ibuf, sizeof(len16));
849 ibuf += sizeof(len16);
850 ibuflen -= sizeof(len16);
853 return send_reply(obj, AFPERR_PARAM);
857 /* directory service name */
859 return send_reply(obj, AFPERR_PARAM);
868 return send_reply(obj, AFPERR_PARAM);
869 len = (unsigned char) *ibuf;
874 /* With "No User Authen" it is equal */
875 if (ibuflen < sizeof(len16))
876 return send_reply(obj, AFPERR_PARAM);
877 memcpy(&len16, ibuf, sizeof(len16));
878 ibuf += sizeof(len16);
879 ibuflen -= sizeof(len16);
883 return send_reply(obj, AFPERR_PARAM);
887 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
888 return send_reply(obj, AFPERR_PARAM);
895 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
900 if (AFP_OK != (i = create_session_key(obj)) ) {
901 return send_reply(obj, i);
904 /* FIXME user name are in UTF8 */
905 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
907 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
908 return send_reply(obj, i);
910 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
913 /* ---------------------- */
914 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
916 struct passwd *pwd = NULL;
919 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
921 return send_reply(obj, AFPERR_NOTAUTH );
924 ibuf += 2; ibuflen -= 2;
925 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
927 if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
928 return send_reply(obj, err);
930 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
934 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
936 DSI *dsi = (DSI *)(obj->handle);
938 LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
939 of_close_all_forks();
941 dsi->flags = DSI_AFP_LOGGED_OUT;
948 /* change password --
949 * NOTE: an FPLogin must already have completed successfully for this
950 * to work. this also does a little pre-processing before it hands
953 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
955 char username[MACFILELEN + 1], *start = ibuf;
964 /* check if password change is allowed, OS-X ignores the flag.
965 * we shouldn't trust the client on this anyway.
966 * not sure about the "right" error code, NOOP for now */
967 if (!(obj->options.passwdbits & PASSWD_SET))
970 /* make sure we can deal w/ this uam */
971 len = (unsigned char) *ibuf++;
972 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
973 return AFPERR_BADUAM;
976 if ((len + 1) & 1) /* pad byte */
979 if ( afp_version < 30) {
980 len = (unsigned char) *ibuf++;
981 if ( len > sizeof(username) - 1) {
984 memcpy(username, ibuf, len);
985 username[ len ] = '\0';
987 if ((len + 1) & 1) /* pad byte */
990 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
991 if ( ibuf[0] != '\0' || ibuf[1] != '\0')
994 len = MIN(sizeof(username), strlen(obj->username));
995 memcpy(username, obj->username, len);
996 username[ len ] = '\0';
1000 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
1002 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
1003 return AFPERR_PARAM;
1005 /* send it off to the uam. we really don't use ibuflen right now. */
1006 if (ibuflen < (size_t)(ibuf - start))
1007 return AFPERR_PARAM;
1009 ibuflen -= (ibuf - start);
1010 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
1012 LOG(log_info, logtype_afpd, "password change %s.",
1013 (ret == AFPERR_AUTHCONT) ? "continued" :
1014 (ret ? "failed" : "succeeded"));
1015 if ( ret == AFP_OK )
1023 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1030 LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
1035 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
1036 memcpy(&bitmap, ibuf, sizeof(bitmap));
1037 bitmap = ntohs(bitmap);
1039 /* deal with error cases. we don't have to worry about
1040 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
1043 return AFPERR_PARAM;
1044 if ((bitmap & USERIBIT_ALL) != bitmap)
1045 return AFPERR_BITMAP;
1047 /* remember place where we store the possibly modified bitmap later */
1048 memcpy(rbuf, ibuf, sizeof(bitmap));
1050 rbuf += sizeof(bitmap);
1051 *rbuflen = sizeof(bitmap);
1053 /* copy the user/group info */
1054 if (bitmap & USERIBIT_USER) {
1055 id = htonl(geteuid());
1056 memcpy(rbuf, &id, sizeof(id));
1058 *rbuflen += sizeof(id);
1061 if (bitmap & USERIBIT_GROUP) {
1062 id = htonl(getegid());
1063 memcpy(rbuf, &id, sizeof(id));
1065 *rbuflen += sizeof(id);
1068 if (bitmap & USERIBIT_UUID) {
1069 if ( ! (obj->options.flags & OPTION_UUID)) {
1070 bitmap &= ~USERIBIT_UUID;
1071 bitmap = htons(bitmap);
1072 memcpy(bitmapp, &bitmap, sizeof(bitmap));
1074 LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
1077 ret = getuuidfromname( obj->username, UUID_USER, uuid);
1079 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
1080 return AFPERR_NOITEM;
1082 LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
1084 memcpy(rbuf, uuid, UUID_BINSIZE);
1085 rbuf += UUID_BINSIZE;
1086 *rbuflen += UUID_BINSIZE;
1090 LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
1094 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
1095 (((type) == UAM_SERVER_CHANGEPW) ? \
1096 &uam_changepw : NULL))
1098 /* just do a linked list search. this could be sped up with a hashed
1099 * list, but i doubt anyone's going to have enough uams to matter. */
1100 struct uam_obj *auth_uamfind(const int type, const char *name,
1103 struct uam_obj *prev, *start;
1105 if (!name || !(start = UAM_LIST(type)))
1109 while ((prev = prev->uam_prev) != start)
1110 if (strndiacasecmp(prev->uam_name, name, len) == 0)
1116 int auth_register(const int type, struct uam_obj *uam)
1118 struct uam_obj *start;
1120 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1123 if (!(start = UAM_LIST(type)))
1124 return 1; /* we don't know what to do with it, caller must free it */
1126 uam_attach(start, uam);
1130 /* load all of the modules */
1131 int auth_load(const char *path, const char *list)
1133 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
1134 struct uam_mod *mod;
1138 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1141 strlcpy(buf, list, sizeof(buf));
1142 if ((p = strtok(buf, ",")) == NULL)
1146 if (name[len - 1] != '/') {
1152 strlcpy(name + len, p, sizeof(name) - len);
1153 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1155 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1157 if (stat(name, &st) == 0) {
1158 if ((mod = uam_load(name, p))) {
1159 uam_attach(&uam_modules, mod);
1160 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1162 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1165 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1167 p = strtok(NULL, ",");
1173 /* get rid of all of the uams */
1174 void auth_unload(void)
1176 struct uam_mod *mod, *prev, *start = &uam_modules;
1178 prev = start->uam_prev;
1179 while ((mod = prev) != start) {
1180 prev = prev->uam_prev;