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>
42 #include <atalk/globals.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 if (!obj->sinfo.clientid) {
594 obj->sinfo.clientid = malloc(idlen + 8);
595 memcpy(obj->sinfo.clientid, p, idlen + 8);
596 obj->sinfo.clientid_len = idlen + 8;
598 if (ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p) != 0)
600 tklen = obj->sinfo.sessiontoken_len;
601 token = obj->sinfo.sessiontoken;
604 case 8: /* Panther Kerberos Token */
605 tklen = obj->sinfo.cryptedkey_len;
606 token = obj->sinfo.cryptedkey;
618 memcpy(rbuf, &tp, sizeof(tklen));
619 rbuf += sizeof(tklen);
620 *rbuflen += sizeof(tklen);
622 memcpy(rbuf, token, tklen);
628 /* ---------------------- */
629 int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
631 DSI *dsi = (DSI *)obj->handle;
641 /* check for guest user */
642 if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
647 memcpy(&type, ibuf, sizeof(type));
649 ibuf += sizeof(type);
651 memcpy(&tklen, ibuf, sizeof(tklen));
652 tklen = ntohl(tklen);
653 ibuf += sizeof(tklen);
655 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
656 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
659 if (tklen != SESSIONTOKEN_LEN) {
662 tklen = sizeof(pid_t);
663 memcpy(&token, ibuf, tklen);
665 /* our stuff is pid + zero pad */
667 for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
673 LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
674 dsi->flags |= DSI_RECONINPROG;
676 /* Deactivate tickle timer */
677 const struct itimerval none = {{0, 0}, {0, 0}};
678 setitimer(ITIMER_REAL, &none, NULL);
680 /* check for old session, possibly transfering session from here to there */
681 if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) != 0)
683 /* write uint16_t DSI request ID */
684 if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
685 LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
688 /* now send our connected AFP client socket */
689 if (send_fd(obj->ipc_fd, dsi->socket) != 0)
691 /* Now see what happens: either afpd master sends us SIGTERM because our session */
692 /* has been transfered to a old disconnected session, or we continue */
695 if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
696 /* Reconnect succeeded, we exit now after sleeping some more */
697 sleep(2); /* sleep some more to give the recon. session time */
698 LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
703 /* Reinstall tickle timer */
704 setitimer(ITIMER_REAL, &dsi->timer, NULL);
706 LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
710 /* ---------------------- */
711 static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
715 if (!len || len > ibuflen)
716 return AFPERR_BADVERS;
718 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
719 for ( i = 0; i < num; i++ ) {
720 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
721 afp_version = afp_versions[ i ].av_number;
722 afp_version_index = i;
726 if ( i == num ) /* An inappropo version */
727 return AFPERR_BADVERS ;
729 if (afp_version >= 30 && obj->proto != AFPPROTO_DSI)
730 return AFPERR_BADVERS ;
733 if (afp_version >= 30 && sizeof(off_t) != 8) {
734 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
735 return AFPERR_BADVERS ;
741 /* ---------------------- */
742 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
744 struct passwd *pwd = NULL;
751 return send_reply(obj, AFPERR_SHUTDOWN );
754 return send_reply(obj, AFPERR_BADVERS );
757 len = (unsigned char) *ibuf++;
760 i = get_version(obj, ibuf, ibuflen, len);
762 return send_reply(obj, i );
765 return send_reply(obj, AFPERR_BADUAM);
770 len = (unsigned char) *ibuf++;
773 if (!len || len > ibuflen)
774 return send_reply(obj, AFPERR_BADUAM);
776 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
777 return send_reply(obj, AFPERR_BADUAM);
781 if (AFP_OK != (i = create_session_key(obj)) )
782 return send_reply(obj, i);
784 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
786 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
787 return send_reply(obj, i);
789 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
792 /* ---------------------- */
793 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
795 struct passwd *pwd = NULL;
805 return send_reply(obj, AFPERR_SHUTDOWN );
808 return send_reply(obj, AFPERR_BADVERS );
814 len = (unsigned char) *ibuf;
818 i = get_version(obj, ibuf, ibuflen, len);
820 return send_reply(obj, i );
823 return send_reply(obj, AFPERR_BADUAM);
828 len = (unsigned char) *ibuf;
832 if (!len || len > ibuflen)
833 return send_reply(obj, AFPERR_BADUAM);
835 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
836 return send_reply(obj, AFPERR_BADUAM);
840 if (!afp_uam->u.uam_login.login_ext) {
841 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
842 return send_reply(obj, AFPERR_BADUAM);
845 if (ibuflen <= 1 +sizeof(len16))
846 return send_reply(obj, AFPERR_PARAM);
852 return send_reply(obj, AFPERR_PARAM);
854 memcpy(&len16, ibuf, sizeof(len16));
855 ibuf += sizeof(len16);
856 ibuflen -= sizeof(len16);
859 return send_reply(obj, AFPERR_PARAM);
863 /* directory service name */
865 return send_reply(obj, AFPERR_PARAM);
874 return send_reply(obj, AFPERR_PARAM);
875 len = (unsigned char) *ibuf;
880 /* With "No User Authen" it is equal */
881 if (ibuflen < sizeof(len16))
882 return send_reply(obj, AFPERR_PARAM);
883 memcpy(&len16, ibuf, sizeof(len16));
884 ibuf += sizeof(len16);
885 ibuflen -= sizeof(len16);
889 return send_reply(obj, AFPERR_PARAM);
893 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
894 return send_reply(obj, AFPERR_PARAM);
901 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
906 if (AFP_OK != (i = create_session_key(obj)) ) {
907 return send_reply(obj, i);
910 /* FIXME user name are in UTF8 */
911 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
913 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
914 return send_reply(obj, i);
916 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
919 /* ---------------------- */
920 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
922 struct passwd *pwd = NULL;
925 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
927 return send_reply(obj, AFPERR_NOTAUTH );
930 ibuf += 2; ibuflen -= 2;
931 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
933 if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
934 return send_reply(obj, err);
936 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
940 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
942 DSI *dsi = (DSI *)(obj->handle);
944 LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
945 of_close_all_forks();
947 dsi->flags = DSI_AFP_LOGGED_OUT;
954 /* change password --
955 * NOTE: an FPLogin must already have completed successfully for this
956 * to work. this also does a little pre-processing before it hands
959 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
961 char username[MACFILELEN + 1], *start = ibuf;
970 /* check if password change is allowed, OS-X ignores the flag.
971 * we shouldn't trust the client on this anyway.
972 * not sure about the "right" error code, NOOP for now */
973 if (!(obj->options.passwdbits & PASSWD_SET))
976 /* make sure we can deal w/ this uam */
977 len = (unsigned char) *ibuf++;
978 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
979 return AFPERR_BADUAM;
982 if ((len + 1) & 1) /* pad byte */
985 if ( afp_version < 30) {
986 len = (unsigned char) *ibuf++;
987 if ( len > sizeof(username) - 1) {
990 memcpy(username, ibuf, len);
991 username[ len ] = '\0';
993 if ((len + 1) & 1) /* pad byte */
996 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
997 if ( ibuf[0] != '\0' || ibuf[1] != '\0')
1000 len = MIN(sizeof(username), strlen(obj->username));
1001 memcpy(username, obj->username, len);
1002 username[ len ] = '\0';
1006 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
1008 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
1009 return AFPERR_PARAM;
1011 /* send it off to the uam. we really don't use ibuflen right now. */
1012 if (ibuflen < (size_t)(ibuf - start))
1013 return AFPERR_PARAM;
1015 ibuflen -= (ibuf - start);
1016 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
1018 LOG(log_info, logtype_afpd, "password change %s.",
1019 (ret == AFPERR_AUTHCONT) ? "continued" :
1020 (ret ? "failed" : "succeeded"));
1021 if ( ret == AFP_OK )
1029 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1036 LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
1041 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
1042 memcpy(&bitmap, ibuf, sizeof(bitmap));
1043 bitmap = ntohs(bitmap);
1045 /* deal with error cases. we don't have to worry about
1046 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
1049 return AFPERR_PARAM;
1050 if ((bitmap & USERIBIT_ALL) != bitmap)
1051 return AFPERR_BITMAP;
1053 /* remember place where we store the possibly modified bitmap later */
1054 memcpy(rbuf, ibuf, sizeof(bitmap));
1056 rbuf += sizeof(bitmap);
1057 *rbuflen = sizeof(bitmap);
1059 /* copy the user/group info */
1060 if (bitmap & USERIBIT_USER) {
1061 id = htonl(geteuid());
1062 memcpy(rbuf, &id, sizeof(id));
1064 *rbuflen += sizeof(id);
1067 if (bitmap & USERIBIT_GROUP) {
1068 id = htonl(getegid());
1069 memcpy(rbuf, &id, sizeof(id));
1071 *rbuflen += sizeof(id);
1074 if (bitmap & USERIBIT_UUID) {
1075 if ( ! (obj->options.flags & OPTION_UUID)) {
1076 bitmap &= ~USERIBIT_UUID;
1077 bitmap = htons(bitmap);
1078 memcpy(bitmapp, &bitmap, sizeof(bitmap));
1080 LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
1083 ret = getuuidfromname( obj->username, UUID_USER, uuid);
1085 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
1086 return AFPERR_NOITEM;
1088 LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
1090 memcpy(rbuf, uuid, UUID_BINSIZE);
1091 rbuf += UUID_BINSIZE;
1092 *rbuflen += UUID_BINSIZE;
1096 LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
1100 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
1101 (((type) == UAM_SERVER_CHANGEPW) ? \
1102 &uam_changepw : NULL))
1104 /* just do a linked list search. this could be sped up with a hashed
1105 * list, but i doubt anyone's going to have enough uams to matter. */
1106 struct uam_obj *auth_uamfind(const int type, const char *name,
1109 struct uam_obj *prev, *start;
1111 if (!name || !(start = UAM_LIST(type)))
1115 while ((prev = prev->uam_prev) != start)
1116 if (strndiacasecmp(prev->uam_name, name, len) == 0)
1122 int auth_register(const int type, struct uam_obj *uam)
1124 struct uam_obj *start;
1126 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1129 if (!(start = UAM_LIST(type)))
1130 return 1; /* we don't know what to do with it, caller must free it */
1132 uam_attach(start, uam);
1136 /* load all of the modules */
1137 int auth_load(const char *path, const char *list)
1139 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
1140 struct uam_mod *mod;
1144 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1147 strlcpy(buf, list, sizeof(buf));
1148 if ((p = strtok(buf, ",")) == NULL)
1152 if (name[len - 1] != '/') {
1158 strlcpy(name + len, p, sizeof(name) - len);
1159 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1161 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1163 if (stat(name, &st) == 0) {
1164 if ((mod = uam_load(name, p))) {
1165 uam_attach(&uam_modules, mod);
1166 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1168 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1171 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1173 p = strtok(NULL, ",");
1179 /* get rid of all of the uams */
1180 void auth_unload(void)
1182 struct uam_mod *mod, *prev, *start = &uam_modules;
1184 prev = start->uam_prev;
1185 while ((mod = prev) != start) {
1186 prev = prev->uam_prev;