2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
14 #include <sys/types.h>
15 #include <sys/param.h>
17 #include <netatalk/endian.h>
18 #include <atalk/afp.h>
19 #include <atalk/compat.h>
20 #include <atalk/util.h>
30 #include <arpa/inet.h>
34 extern void afp_get_cmdline( int *ac, char ***av );
37 #include <atalk/logger.h>
38 #include <atalk/server_ipc.h>
39 #include <atalk/uuid.h>
53 static int afp_version_index;
57 #if defined( sun ) && !defined( __svr4__ ) || defined( ultrix )
60 #define GROUPS_SIZE sizeof(int)
62 #else /* sun __svr4__ ultrix */
65 #define GROUPS_SIZE sizeof(gid_t)
66 #endif /* sun ultrix */
71 * These numbers are scattered throughout the code.
73 static struct afp_versions afp_versions[] = {
75 { "AFPVersion 1.1", 11 },
76 { "AFPVersion 2.0", 20 },
77 { "AFPVersion 2.1", 21 },
86 static struct uam_mod uam_modules = {NULL, NULL, &uam_modules, &uam_modules};
87 static struct uam_obj uam_login = {"", "", 0, {{NULL, NULL, NULL, NULL }}, &uam_login,
89 static struct uam_obj uam_changepw = {"", "", 0, {{NULL, NULL, NULL, NULL}}, &uam_changepw,
92 static struct uam_obj *afp_uam = NULL;
95 void status_versions( char *data, const ASP asp, const DSI *dsi)
99 int len, num, i, count = 0;
101 memcpy(&status, start + AFPSTATUS_VERSOFF, sizeof(status));
102 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ] );
104 for ( i = 0; i < num; i++ ) {
106 if ( !asp && (afp_versions[ i ].av_number <= 21)) continue;
107 #endif /* ! NO_DDP */
108 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
111 data += ntohs( status );
114 for ( i = 0; i < num; i++ ) {
116 if ( !asp && (afp_versions[ i ].av_number <= 21)) continue;
117 #endif /* ! NO_DDP */
118 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
119 len = strlen( afp_versions[ i ].av_name );
121 memcpy( data, afp_versions[ i ].av_name , len );
124 status = htons( data - start );
125 memcpy(start + AFPSTATUS_UAMSOFF, &status, sizeof(status));
128 void status_uams(char *data, const char *authlist)
132 struct uam_obj *uams;
135 memcpy(&status, start + AFPSTATUS_UAMSOFF, sizeof(status));
137 while ((uams = uams->uam_prev) != &uam_login) {
138 if (strstr(authlist, uams->uam_path))
142 data += ntohs( status );
144 while ((uams = uams->uam_prev) != &uam_login) {
145 if (strstr(authlist, uams->uam_path)) {
146 LOG(log_info, logtype_afpd, "uam: \"%s\" available", uams->uam_name);
147 len = strlen( uams->uam_name);
149 memcpy( data, uams->uam_name, len );
155 status = htons(data - start);
156 memcpy(start + AFPSTATUS_ICONOFF, &status, sizeof(status));
159 /* handle errors by closing the connection. this is only needed
160 * by the afp_* functions. */
161 static int send_reply(const AFPObj *obj, const int err)
163 if ((err == AFP_OK) || (err == AFPERR_AUTHCONT))
166 obj->reply(obj->handle, err);
172 static int afp_errpwdexpired(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
173 char *rbuf _U_, size_t *rbuflen)
176 return AFPERR_PWDEXPR;
179 static int afp_null_nolog(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
180 char *rbuf _U_, size_t *rbuflen)
183 return( AFPERR_NOOP );
186 static int set_auth_switch(int expired)
192 * BF: expired password handling
193 * to allow the user to change his/her password we have to allow login
194 * but every following call except for FPChangePassword will be thrown
195 * away with an AFPERR_PWDEXPR error. (thanks to Leland Wallace from Apple
196 * for clarifying this)
199 for (i=0; i<=0xff; i++) {
200 uam_afpserver_action(i, UAM_AFPSERVER_PREAUTH, afp_errpwdexpired, NULL);
202 uam_afpserver_action(AFP_LOGOUT, UAM_AFPSERVER_PREAUTH, afp_logout, NULL);
203 uam_afpserver_action(AFP_CHANGEPW, UAM_AFPSERVER_PREAUTH, afp_changepw, NULL);
206 afp_switch = postauth_switch;
207 switch (afp_version) {
212 uam_afpserver_action(AFP_GETACL, UAM_AFPSERVER_POSTAUTH, afp_getacl, NULL);
213 uam_afpserver_action(AFP_SETACL, UAM_AFPSERVER_POSTAUTH, afp_setacl, NULL);
214 uam_afpserver_action(AFP_ACCESS, UAM_AFPSERVER_POSTAUTH, afp_access, NULL);
215 #endif /* HAVE_ACLS */
216 uam_afpserver_action(AFP_GETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_getextattr, NULL);
217 uam_afpserver_action(AFP_SETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_setextattr, NULL);
218 uam_afpserver_action(AFP_REMOVEATTR, UAM_AFPSERVER_POSTAUTH, afp_remextattr, NULL);
219 uam_afpserver_action(AFP_LISTEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_listextattr, NULL);
222 uam_afpserver_action(AFP_SYNCDIR, UAM_AFPSERVER_POSTAUTH, afp_syncdir, NULL);
223 uam_afpserver_action(AFP_SYNCFORK, UAM_AFPSERVER_POSTAUTH, afp_syncfork, NULL);
224 uam_afpserver_action(AFP_SPOTLIGHT_PRIVATE, UAM_AFPSERVER_POSTAUTH, afp_null_nolog, NULL);
225 uam_afpserver_action(AFP_ENUMERATE_EXT2, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext2, NULL);
228 uam_afpserver_action(AFP_ENUMERATE_EXT, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext, NULL);
229 uam_afpserver_action(AFP_BYTELOCK_EXT, UAM_AFPSERVER_POSTAUTH, afp_bytelock_ext, NULL);
230 /* catsearch_ext uses the same packet as catsearch FIXME double check this, it wasn't true for enue
232 uam_afpserver_action(AFP_CATSEARCH_EXT, UAM_AFPSERVER_POSTAUTH, afp_catsearch_ext, NULL);
233 uam_afpserver_action(AFP_GETSESSTOKEN, UAM_AFPSERVER_POSTAUTH, afp_getsession, NULL);
234 uam_afpserver_action(AFP_READ_EXT, UAM_AFPSERVER_POSTAUTH, afp_read_ext, NULL);
235 uam_afpserver_action(AFP_WRITE_EXT, UAM_AFPSERVER_POSTAUTH, afp_write_ext, NULL);
236 uam_afpserver_action(AFP_DISCTOLDSESS, UAM_AFPSERVER_POSTAUTH, afp_disconnect, NULL);
240 * If first connection to a server is done in classic AFP2.2 version is used
241 * but OSX uses AFP3.x FPzzz command !
243 uam_afpserver_action(AFP_ZZZ, UAM_AFPSERVER_POSTAUTH, afp_zzz, NULL);
251 static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void), int expired)
255 #endif /* ADMIN_GRP */
258 set_processname("afpd");
261 if ( pwd->pw_uid == 0 ) { /* don't allow root login */
262 LOG(log_error, logtype_afpd, "login: root login denied!" );
263 return AFPERR_NOTAUTH;
266 LOG(log_note, logtype_afpd, "%s Login by %s",
267 afp_versions[afp_version_index].av_name, pwd->pw_name);
270 if (obj->proto == AFPPROTO_ASP) {
271 ASP asp = obj->handle;
272 int addr_net = ntohs( asp->asp_sat.sat_addr.s_net );
273 int addr_node = asp->asp_sat.sat_addr.s_node;
275 if (obj->options.authprintdir) {
276 if(addr_net && addr_node) { /* Do we have a valid Appletalk address? */
279 int mypid = getpid();
280 struct stat stat_buf;
282 sprintf(nodename, "%s/net%d.%dnode%d", obj->options.authprintdir,
283 addr_net / 256, addr_net % 256, addr_node);
284 LOG(log_info, logtype_afpd, "registering %s (uid %d) on %u.%u as %s",
285 pwd->pw_name, pwd->pw_uid, addr_net, addr_node, nodename);
287 if (stat(nodename, &stat_buf) == 0) { /* file exists */
288 if (S_ISREG(stat_buf.st_mode)) { /* normal file */
290 fp = fopen(nodename, "w");
291 fprintf(fp, "%s:%d\n", pwd->pw_name, mypid);
293 chown( nodename, pwd->pw_uid, -1 );
294 } else { /* somebody is messing with us */
295 LOG(log_error, logtype_afpd, "print authfile %s is not a normal file, it will not be modified", nodename );
297 } else { /* file 'nodename' does not exist */
298 fp = fopen(nodename, "w");
299 fprintf(fp, "%s:%d\n", pwd->pw_name, mypid);
301 chown( nodename, pwd->pw_uid, -1 );
303 } /* if (addr_net && addr_node ) */
304 } /* if (options->authprintdir) */
305 } /* if (obj->proto == AFPPROTO_ASP) */
308 if (initgroups( pwd->pw_name, pwd->pw_gid ) < 0) {
310 LOG(log_info, logtype_afpd, "running with uid %d", geteuid());
311 #else /* RUN_AS_USER */
312 LOG(log_error, logtype_afpd, "login: %s", strerror(errno));
313 return AFPERR_BADUAM;
314 #endif /* RUN_AS_USER */
318 /* Basically if the user is in the admin group, we stay root */
320 if (( ngroups = getgroups( 0, NULL )) < 0 ) {
321 LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
322 return AFPERR_BADUAM;
325 if ( NULL == (groups = calloc(ngroups, GROUPS_SIZE)) ) {
326 LOG(log_error, logtype_afpd, "login: %s calloc: %d", ngroups);
327 return AFPERR_BADUAM;
330 if (( ngroups = getgroups( ngroups, groups )) < 0 ) {
331 LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
332 return AFPERR_BADUAM;
336 LOG(log_debug, logtype_afpd, "obj->options.admingid == %d", obj->options.admingid);
338 if (obj->options.admingid != 0) {
340 for (i = 0; i < ngroups; i++) {
341 if (groups[i] == obj->options.admingid) admin = 1;
346 LOG(log_info, logtype_afpd, "admin login -- %s", pwd->pw_name );
349 #endif /* ADMIN_GRP */
352 struct DSI *dsi = obj->handle;
359 afp_get_cmdline( &argc, &argv );
361 hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
362 sizeof( struct in_addr ),
363 dsi->client.sin_family );
366 clientname = hp->h_name;
368 clientname = inet_ntoa( dsi->client.sin_addr );
370 sprintf( hostname, "%s@%s", pwd->pw_name, clientname );
372 if( sia_become_user( NULL, argc, argv, hostname, pwd->pw_name,
373 NULL, FALSE, NULL, NULL,
374 SIA_BEU_REALLOGIN ) != SIASUCCESS )
375 return AFPERR_BADUAM;
377 LOG(log_info, logtype_afpd, "session from %s (%s)", hostname,
378 inet_ntoa( dsi->client.sin_addr ) );
380 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
381 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
382 return AFPERR_BADUAM;
386 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
387 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
388 return AFPERR_BADUAM;
393 #define GROUPSTR_BUFSIZE 1024
394 char groupsstr[GROUPSTR_BUFSIZE];
396 int j = GROUPSTR_BUFSIZE;
398 int n = snprintf(groupsstr, GROUPSTR_BUFSIZE, "%u", groups[0]);
402 for (int i = 1; i < ngroups; i++) {
403 n = snprintf(s, j, ", %u", groups[i]);
406 LOG(log_debug, logtype_afpd, "login: group string buffer overflow");
412 LOG(log_debug, logtype_afpd, "login: %u supplementary groups: %s", ngroups, groupsstr);
415 /* There's probably a better way to do this, but for now, we just play root */
420 #endif /* ADMIN_GRP */
423 set_auth_switch(expired);
424 /* save our euid, we need it for preexec_close */
425 obj->uid = geteuid();
426 obj->logout = logout;
430 save_uidgid ( &obj->uidgid );
436 /* ---------------------- */
437 int afp_zzz ( /* Function 122 */
439 char *ibuf _U_, size_t ibuflen _U_,
440 char *rbuf, size_t *rbuflen)
446 retdata = obj->options.sleep /120;
450 *rbuflen = sizeof(retdata);
451 retdata = htonl(retdata);
452 memcpy(rbuf, &retdata, sizeof(retdata));
455 rbuf += sizeof(retdata);
459 /* ---------------------- */
460 static int create_session_token(AFPObj *obj)
464 /* use 8 bytes for token as OSX, don't know if it helps */
465 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
466 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
470 if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
473 memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
474 obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
476 memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
481 static int create_session_key(AFPObj *obj)
483 /* create session key */
484 if (obj->sinfo.sessionkey == NULL) {
485 if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
487 uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
488 obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
494 /* ---------------------- */
497 char *ibuf, size_t ibuflen,
498 char *rbuf, size_t *rbuflen)
510 if (ibuflen < 2 + sizeof(type)) {
517 memcpy(&type, ibuf, sizeof(type));
519 ibuf += sizeof(type);
520 ibuflen -= sizeof(type);
522 if ( obj->sinfo.sessiontoken == NULL ) {
523 if ( create_session_token( obj ) )
531 case 0: /* old version ?*/
532 tklen = obj->sinfo.sessiontoken_len;
533 token = obj->sinfo.sessiontoken;
535 case 1: /* disconnect */
536 case 2: /* reconnect update id */
537 if (ibuflen >= sizeof(idlen)) {
538 memcpy(&idlen, ibuf, sizeof(idlen));
539 idlen = ntohl(idlen);
540 ibuf += sizeof(idlen);
541 ibuflen -= sizeof(idlen);
542 if (ibuflen < idlen) {
545 /* memcpy (id, ibuf, idlen) */
546 tklen = obj->sinfo.sessiontoken_len;
547 token = obj->sinfo.sessiontoken;
554 memcpy( &idlen, ibuf, sizeof(idlen));
555 idlen = ntohl(idlen);
556 ibuf += sizeof(idlen);
557 ibuflen -= sizeof(idlen);
558 ibuf += sizeof(boottime);
559 ibuflen -= sizeof(boottime);
560 if (ibuflen < idlen || idlen > (90-10)) {
563 ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p);
564 tklen = obj->sinfo.sessiontoken_len;
565 token = obj->sinfo.sessiontoken;
568 case 8: /* Panther Kerberos Token */
569 tklen = obj->sinfo.cryptedkey_len;
570 token = obj->sinfo.cryptedkey;
582 memcpy(rbuf, &tp, sizeof(tklen));
583 rbuf += sizeof(tklen);
584 *rbuflen += sizeof(tklen);
586 memcpy(rbuf, token, tklen);
592 /* ---------------------- */
593 int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
595 DSI *dsi = (DSI *)obj->handle;
605 /* check for guest user */
606 if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
611 memcpy(&type, ibuf, sizeof(type));
613 ibuf += sizeof(type);
615 memcpy(&tklen, ibuf, sizeof(tklen));
616 tklen = ntohl(tklen);
617 ibuf += sizeof(tklen);
619 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
620 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
623 if (tklen != SESSIONTOKEN_LEN) {
626 tklen = sizeof(pid_t);
627 memcpy(&token, ibuf, tklen);
629 /* our stuff is pid + zero pad */
631 for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
637 LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
638 dsi->flags |= DSI_RECONINPROG;
640 /* Deactivate tickle timer */
641 const struct itimerval none = {{0, 0}, {0, 0}};
642 setitimer(ITIMER_REAL, &none, NULL);
644 /* check for old session, possibly transfering session from here to there */
645 if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) == -1)
647 /* write uint16_t DSI request ID */
648 if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
649 LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
652 /* now send our connected AFP client socket */
653 if (send_fd(obj->ipc_fd, dsi->socket) != 0)
655 /* Now see what happens: either afpd master sends us SIGTERM because our session */
656 /* has been transfered to a old disconnected session, or we continue */
659 if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
660 /* Reconnect succeeded, we exit now after sleeping some more */
661 sleep(2); /* sleep some more to give the recon. session time */
662 LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
667 /* Reinstall tickle timer */
668 setitimer(ITIMER_REAL, &dsi->timer, NULL);
670 LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
674 /* ---------------------- */
675 static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
679 if (!len || len > ibuflen)
680 return AFPERR_BADVERS;
682 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
683 for ( i = 0; i < num; i++ ) {
684 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
685 afp_version = afp_versions[ i ].av_number;
686 afp_version_index = i;
690 if ( i == num ) /* An inappropo version */
691 return AFPERR_BADVERS ;
693 if (afp_version >= 30 && obj->proto != AFPPROTO_DSI)
694 return AFPERR_BADVERS ;
697 if (afp_version >= 30 && sizeof(off_t) != 8) {
698 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
699 return AFPERR_BADVERS ;
705 /* ---------------------- */
706 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
708 struct passwd *pwd = NULL;
715 return send_reply(obj, AFPERR_SHUTDOWN );
718 return send_reply(obj, AFPERR_BADVERS );
721 len = (unsigned char) *ibuf++;
724 i = get_version(obj, ibuf, ibuflen, len);
726 return send_reply(obj, i );
729 return send_reply(obj, AFPERR_BADUAM);
734 len = (unsigned char) *ibuf++;
737 if (!len || len > ibuflen)
738 return send_reply(obj, AFPERR_BADUAM);
740 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
741 return send_reply(obj, AFPERR_BADUAM);
745 if (AFP_OK != (i = create_session_key(obj)) )
746 return send_reply(obj, i);
748 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
750 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
751 return send_reply(obj, i);
753 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
756 /* ---------------------- */
757 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
759 struct passwd *pwd = NULL;
769 return send_reply(obj, AFPERR_SHUTDOWN );
772 return send_reply(obj, AFPERR_BADVERS );
778 len = (unsigned char) *ibuf;
782 i = get_version(obj, ibuf, ibuflen, len);
784 return send_reply(obj, i );
787 return send_reply(obj, AFPERR_BADUAM);
792 len = (unsigned char) *ibuf;
796 if (!len || len > ibuflen)
797 return send_reply(obj, AFPERR_BADUAM);
799 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
800 return send_reply(obj, AFPERR_BADUAM);
804 if (!afp_uam->u.uam_login.login_ext) {
805 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
806 return send_reply(obj, AFPERR_BADUAM);
809 if (ibuflen <= 1 +sizeof(len16))
810 return send_reply(obj, AFPERR_PARAM);
816 return send_reply(obj, AFPERR_PARAM);
818 memcpy(&len16, ibuf, sizeof(len16));
819 ibuf += sizeof(len16);
820 ibuflen -= sizeof(len16);
823 return send_reply(obj, AFPERR_PARAM);
827 /* directory service name */
829 return send_reply(obj, AFPERR_PARAM);
838 return send_reply(obj, AFPERR_PARAM);
839 len = (unsigned char) *ibuf;
844 /* With "No User Authen" it is equal */
845 if (ibuflen < sizeof(len16))
846 return send_reply(obj, AFPERR_PARAM);
847 memcpy(&len16, ibuf, sizeof(len16));
848 ibuf += sizeof(len16);
849 ibuflen -= sizeof(len16);
853 return send_reply(obj, AFPERR_PARAM);
857 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
858 return send_reply(obj, AFPERR_PARAM);
865 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
870 if (AFP_OK != (i = create_session_key(obj)) ) {
871 return send_reply(obj, i);
874 /* FIXME user name are in UTF8 */
875 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
877 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
878 return send_reply(obj, i);
880 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
883 /* ---------------------- */
884 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
886 struct passwd *pwd = NULL;
889 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
891 return send_reply(obj, AFPERR_NOTAUTH );
894 ibuf += 2; ibuflen -= 2;
895 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
897 if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
898 return send_reply(obj, err);
900 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
904 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen _U_)
906 LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
914 /* change password --
915 * NOTE: an FPLogin must already have completed successfully for this
916 * to work. this also does a little pre-processing before it hands
919 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
921 char username[MACFILELEN + 1], *start = ibuf;
930 /* check if password change is allowed, OS-X ignores the flag.
931 * we shouldn't trust the client on this anyway.
932 * not sure about the "right" error code, NOOP for now */
933 if (!(obj->options.passwdbits & PASSWD_SET))
936 /* make sure we can deal w/ this uam */
937 len = (unsigned char) *ibuf++;
938 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
939 return AFPERR_BADUAM;
942 if ((len + 1) & 1) /* pad byte */
945 if ( afp_version < 30) {
946 len = (unsigned char) *ibuf++;
947 if ( len > sizeof(username) - 1) {
950 memcpy(username, ibuf, len);
951 username[ len ] = '\0';
953 if ((len + 1) & 1) /* pad byte */
956 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
957 if ( ibuf[0] != '\0' || ibuf[1] != '\0')
960 len = MIN(sizeof(username), strlen(obj->username));
961 memcpy(username, obj->username, len);
962 username[ len ] = '\0';
966 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
968 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
971 /* send it off to the uam. we really don't use ibuflen right now. */
972 if (ibuflen < (size_t)(ibuf - start))
975 ibuflen -= (ibuf - start);
976 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
978 LOG(log_info, logtype_afpd, "password change %s.",
979 (ret == AFPERR_AUTHCONT) ? "continued" :
980 (ret ? "failed" : "succeeded"));
989 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
996 LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
1001 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
1002 memcpy(&bitmap, ibuf, sizeof(bitmap));
1003 bitmap = ntohs(bitmap);
1005 /* deal with error cases. we don't have to worry about
1006 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
1009 return AFPERR_PARAM;
1010 if ((bitmap & USERIBIT_ALL) != bitmap)
1011 return AFPERR_BITMAP;
1013 /* remember place where we store the possibly modified bitmap later */
1014 memcpy(rbuf, ibuf, sizeof(bitmap));
1016 rbuf += sizeof(bitmap);
1017 *rbuflen = sizeof(bitmap);
1019 /* copy the user/group info */
1020 if (bitmap & USERIBIT_USER) {
1021 id = htonl(geteuid());
1022 memcpy(rbuf, &id, sizeof(id));
1024 *rbuflen += sizeof(id);
1027 if (bitmap & USERIBIT_GROUP) {
1028 id = htonl(getegid());
1029 memcpy(rbuf, &id, sizeof(id));
1031 *rbuflen += sizeof(id);
1034 if (bitmap & USERIBIT_UUID) {
1035 if ( ! (obj->options.flags & OPTION_UUID)) {
1036 bitmap &= ~USERIBIT_UUID;
1037 bitmap = htons(bitmap);
1038 memcpy(bitmapp, &bitmap, sizeof(bitmap));
1040 LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
1043 ret = getuuidfromname( obj->username, UUID_USER, uuid);
1045 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
1046 return AFPERR_NOITEM;
1048 LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
1050 memcpy(rbuf, uuid, UUID_BINSIZE);
1051 rbuf += UUID_BINSIZE;
1052 *rbuflen += UUID_BINSIZE;
1056 LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
1060 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
1061 (((type) == UAM_SERVER_CHANGEPW) ? \
1062 &uam_changepw : NULL))
1064 /* just do a linked list search. this could be sped up with a hashed
1065 * list, but i doubt anyone's going to have enough uams to matter. */
1066 struct uam_obj *auth_uamfind(const int type, const char *name,
1069 struct uam_obj *prev, *start;
1071 if (!name || !(start = UAM_LIST(type)))
1075 while ((prev = prev->uam_prev) != start)
1076 if (strndiacasecmp(prev->uam_name, name, len) == 0)
1082 int auth_register(const int type, struct uam_obj *uam)
1084 struct uam_obj *start;
1086 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1089 if (!(start = UAM_LIST(type)))
1090 return 1; /* we don't know what to do with it, caller must free it */
1092 uam_attach(start, uam);
1096 /* load all of the modules */
1097 int auth_load(const char *path, const char *list)
1099 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
1100 struct uam_mod *mod;
1104 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1107 strlcpy(buf, list, sizeof(buf));
1108 if ((p = strtok(buf, ",")) == NULL)
1112 if (name[len - 1] != '/') {
1118 strlcpy(name + len, p, sizeof(name) - len);
1119 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1121 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1123 if (stat(name, &st) == 0) {
1124 if ((mod = uam_load(name, p))) {
1125 uam_attach(&uam_modules, mod);
1126 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1128 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1131 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1133 p = strtok(NULL, ",");
1139 /* get rid of all of the uams */
1140 void auth_unload(void)
1142 struct uam_mod *mod, *prev, *start = &uam_modules;
1144 prev = start->uam_prev;
1145 while ((mod = prev) != start) {
1146 prev = prev->uam_prev;