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 <arpa/inet.h>
19 #include <atalk/afp.h>
20 #include <atalk/compat.h>
21 #include <atalk/util.h>
31 #include <arpa/inet.h>
35 extern void afp_get_cmdline( int *ac, char ***av );
38 #include <atalk/logger.h>
39 #include <atalk/server_ipc.h>
40 #include <atalk/uuid.h>
41 #include <atalk/globals.h>
54 static int afp_version_index;
58 #if defined( sun ) && !defined( __svr4__ ) || defined( ultrix )
61 #define GROUPS_SIZE sizeof(int)
63 #else /* sun __svr4__ ultrix */
66 #define GROUPS_SIZE sizeof(gid_t)
67 #endif /* sun ultrix */
72 * These numbers are scattered throughout the code.
74 static struct afp_versions afp_versions[] = {
82 static struct uam_mod uam_modules = {NULL, NULL, &uam_modules, &uam_modules};
83 static struct uam_obj uam_login = {"", "", 0, {{NULL, NULL, NULL, NULL }}, &uam_login,
85 static struct uam_obj uam_changepw = {"", "", 0, {{NULL, NULL, NULL, NULL}}, &uam_changepw,
88 static struct uam_obj *afp_uam = NULL;
91 void status_versions( char *data, const DSI *dsi)
95 int len, num, i, count = 0;
97 memcpy(&status, start + AFPSTATUS_VERSOFF, sizeof(status));
98 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ] );
100 for ( i = 0; i < num; i++ ) {
101 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
104 data += ntohs( status );
107 for ( i = 0; i < num; i++ ) {
108 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
109 len = strlen( afp_versions[ i ].av_name );
111 memcpy( data, afp_versions[ i ].av_name , len );
114 status = htons( data - start );
115 memcpy(start + AFPSTATUS_UAMSOFF, &status, sizeof(status));
118 void status_uams(char *data, const char *authlist)
122 struct uam_obj *uams;
125 memcpy(&status, start + AFPSTATUS_UAMSOFF, sizeof(status));
127 while ((uams = uams->uam_prev) != &uam_login) {
128 if (strstr(authlist, uams->uam_path))
132 data += ntohs( status );
134 while ((uams = uams->uam_prev) != &uam_login) {
135 if (strstr(authlist, uams->uam_path)) {
136 LOG(log_info, logtype_afpd, "uam: \"%s\" available", uams->uam_name);
137 len = strlen( uams->uam_name);
139 memcpy( data, uams->uam_name, len );
145 status = htons(data - start);
146 memcpy(start + AFPSTATUS_ICONOFF, &status, sizeof(status));
149 /* handle errors by closing the connection. this is only needed
150 * by the afp_* functions. */
151 static int send_reply(const AFPObj *obj, const int err)
153 if ((err == AFP_OK) || (err == AFPERR_AUTHCONT))
156 obj->reply(obj->handle, err);
162 static int afp_errpwdexpired(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
163 char *rbuf _U_, size_t *rbuflen)
166 return AFPERR_PWDEXPR;
169 static int afp_null_nolog(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
170 char *rbuf _U_, size_t *rbuflen)
173 return( AFPERR_NOOP );
176 static int set_auth_switch(int expired)
182 * BF: expired password handling
183 * to allow the user to change his/her password we have to allow login
184 * but every following call except for FPChangePassword will be thrown
185 * away with an AFPERR_PWDEXPR error. (thanks to Leland Wallace from Apple
186 * for clarifying this)
189 for (i=0; i<=0xff; i++) {
190 uam_afpserver_action(i, UAM_AFPSERVER_PREAUTH, afp_errpwdexpired, NULL);
192 uam_afpserver_action(AFP_LOGOUT, UAM_AFPSERVER_PREAUTH, afp_logout, NULL);
193 uam_afpserver_action(AFP_CHANGEPW, UAM_AFPSERVER_PREAUTH, afp_changepw, NULL);
196 afp_switch = postauth_switch;
197 switch (afp_version) {
202 uam_afpserver_action(AFP_GETACL, UAM_AFPSERVER_POSTAUTH, afp_getacl, NULL);
203 uam_afpserver_action(AFP_SETACL, UAM_AFPSERVER_POSTAUTH, afp_setacl, NULL);
204 uam_afpserver_action(AFP_ACCESS, UAM_AFPSERVER_POSTAUTH, afp_access, NULL);
205 #endif /* HAVE_ACLS */
206 uam_afpserver_action(AFP_GETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_getextattr, NULL);
207 uam_afpserver_action(AFP_SETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_setextattr, NULL);
208 uam_afpserver_action(AFP_REMOVEATTR, UAM_AFPSERVER_POSTAUTH, afp_remextattr, NULL);
209 uam_afpserver_action(AFP_LISTEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_listextattr, NULL);
212 uam_afpserver_action(AFP_SYNCDIR, UAM_AFPSERVER_POSTAUTH, afp_syncdir, NULL);
213 uam_afpserver_action(AFP_SYNCFORK, UAM_AFPSERVER_POSTAUTH, afp_syncfork, NULL);
214 uam_afpserver_action(AFP_SPOTLIGHT_PRIVATE, UAM_AFPSERVER_POSTAUTH, afp_null_nolog, NULL);
215 uam_afpserver_action(AFP_ENUMERATE_EXT2, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext2, NULL);
218 uam_afpserver_action(AFP_ENUMERATE_EXT, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext, NULL);
219 uam_afpserver_action(AFP_BYTELOCK_EXT, UAM_AFPSERVER_POSTAUTH, afp_bytelock_ext, NULL);
220 /* catsearch_ext uses the same packet as catsearch FIXME double check this, it wasn't true for enue
222 uam_afpserver_action(AFP_CATSEARCH_EXT, UAM_AFPSERVER_POSTAUTH, afp_catsearch_ext, NULL);
223 uam_afpserver_action(AFP_GETSESSTOKEN, UAM_AFPSERVER_POSTAUTH, afp_getsession, NULL);
224 uam_afpserver_action(AFP_READ_EXT, UAM_AFPSERVER_POSTAUTH, afp_read_ext, NULL);
225 uam_afpserver_action(AFP_WRITE_EXT, UAM_AFPSERVER_POSTAUTH, afp_write_ext, NULL);
226 uam_afpserver_action(AFP_DISCTOLDSESS, UAM_AFPSERVER_POSTAUTH, afp_disconnect, NULL);
230 * If first connection to a server is done in classic AFP2.2 version is used
231 * but OSX uses AFP3.x FPzzz command !
233 uam_afpserver_action(AFP_ZZZ, UAM_AFPSERVER_POSTAUTH, afp_zzz, NULL);
241 #define GROUPSTR_BUFSIZE 1024
242 static const char *print_groups(int ngroups, gid_t *groups)
244 static char groupsstr[GROUPSTR_BUFSIZE];
251 for (i = 0; (i < ngroups) && (s < &groupsstr[GROUPSTR_BUFSIZE]); i++) {
252 s += snprintf(s, &groupsstr[GROUPSTR_BUFSIZE] - s, " %u", groups[i]);
258 static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void), int expired)
262 #endif /* ADMIN_GRP */
264 if ( pwd->pw_uid == 0 ) { /* don't allow root login */
265 LOG(log_error, logtype_afpd, "login: root login denied!" );
266 return AFPERR_NOTAUTH;
269 LOG(log_note, logtype_afpd, "%s Login by %s",
270 afp_versions[afp_version_index].av_name, pwd->pw_name);
272 if (initgroups( pwd->pw_name, pwd->pw_gid ) < 0) {
274 LOG(log_info, logtype_afpd, "running with uid %d", geteuid());
275 #else /* RUN_AS_USER */
276 LOG(log_error, logtype_afpd, "login: %s", strerror(errno));
277 return AFPERR_BADUAM;
278 #endif /* RUN_AS_USER */
282 /* Basically if the user is in the admin group, we stay root */
284 if (( ngroups = getgroups( 0, NULL )) < 0 ) {
285 LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
286 return AFPERR_BADUAM;
289 if ( NULL == (groups = calloc(ngroups, GROUPS_SIZE)) ) {
290 LOG(log_error, logtype_afpd, "login: %s calloc: %d", ngroups);
291 return AFPERR_BADUAM;
294 if (( ngroups = getgroups( ngroups, groups )) < 0 ) {
295 LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
296 return AFPERR_BADUAM;
300 LOG(log_debug, logtype_afpd, "obj->options.admingid == %d", obj->options.admingid);
302 if (obj->options.admingid != 0) {
304 for (i = 0; i < ngroups; i++) {
305 if (groups[i] == obj->options.admingid) admin = 1;
310 LOG(log_info, logtype_afpd, "admin login -- %s", pwd->pw_name );
313 #endif /* ADMIN_GRP */
316 struct DSI *dsi = obj->handle;
323 afp_get_cmdline( &argc, &argv );
325 hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
326 sizeof( struct in_addr ),
327 dsi->client.sin_family );
330 clientname = hp->h_name;
332 clientname = inet_ntoa( dsi->client.sin_addr );
334 sprintf( hostname, "%s@%s", pwd->pw_name, clientname );
336 if( sia_become_user( NULL, argc, argv, hostname, pwd->pw_name,
337 NULL, FALSE, NULL, NULL,
338 SIA_BEU_REALLOGIN ) != SIASUCCESS )
339 return AFPERR_BADUAM;
341 LOG(log_info, logtype_afpd, "session from %s (%s)", hostname,
342 inet_ntoa( dsi->client.sin_addr ) );
344 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
345 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
346 return AFPERR_BADUAM;
350 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
351 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
352 return AFPERR_BADUAM;
356 LOG(log_debug, logtype_afpd, "login: supplementary groups: %s", print_groups(ngroups, groups));
358 /* There's probably a better way to do this, but for now, we just play root */
363 #endif /* ADMIN_GRP */
366 set_auth_switch(expired);
367 /* save our euid, we need it for preexec_close */
368 obj->uid = geteuid();
369 obj->logout = logout;
374 /* ---------------------- */
375 int afp_zzz(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
378 DSI *dsi = (DSI *)AFPobj->handle;
386 memcpy(&data, ibuf, 4); /* flag */
390 * Possible sleeping states:
391 * 1) normal sleep: DSI_SLEEPING (up to 10.3)
392 * 2) extended sleep: DSI_SLEEPING | DSI_EXTSLEEP (starting with 10.4)
395 if (data & AFPZZZ_EXT_WAKEUP) {
396 /* wakeup request from exetended sleep */
397 if (dsi->flags & DSI_EXTSLEEP) {
398 LOG(log_note, logtype_afpd, "afp_zzz: waking up from extended sleep");
399 dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP);
403 dsi->flags |= DSI_SLEEPING;
404 if (data & AFPZZZ_EXT_SLEEP) {
405 LOG(log_note, logtype_afpd, "afp_zzz: entering extended sleep");
406 dsi->flags |= DSI_EXTSLEEP;
408 LOG(log_note, logtype_afpd, "afp_zzz: entering normal sleep");
413 * According to AFP 3.3 spec we should not return anything,
414 * but eg 10.5.8 server still returns the numbers of hours
415 * the server is keeping the sessino (ie max sleeptime).
417 data = obj->options.sleep / 120; /* hours */
421 *rbuflen = sizeof(data);
423 memcpy(rbuf, &data, sizeof(data));
424 rbuf += sizeof(data);
429 /* ---------------------- */
430 static int create_session_token(AFPObj *obj)
434 /* use 8 bytes for token as OSX, don't know if it helps */
435 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
436 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
440 if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
443 memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
444 obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
446 memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
451 static int create_session_key(AFPObj *obj)
453 /* create session key */
454 if (obj->sinfo.sessionkey == NULL) {
455 if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
457 uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
458 obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
464 /* ---------------------- */
467 char *ibuf, size_t ibuflen,
468 char *rbuf, size_t *rbuflen)
480 if (ibuflen < 2 + sizeof(type)) {
487 memcpy(&type, ibuf, sizeof(type));
489 ibuf += sizeof(type);
490 ibuflen -= sizeof(type);
492 if ( obj->sinfo.sessiontoken == NULL ) {
493 if ( create_session_token( obj ) )
501 case 0: /* old version ?*/
502 tklen = obj->sinfo.sessiontoken_len;
503 token = obj->sinfo.sessiontoken;
505 case 1: /* disconnect */
506 case 2: /* reconnect update id */
507 if (ibuflen >= sizeof(idlen)) {
508 memcpy(&idlen, ibuf, sizeof(idlen));
509 idlen = ntohl(idlen);
510 ibuf += sizeof(idlen);
511 ibuflen -= sizeof(idlen);
512 if (ibuflen < idlen) {
515 /* memcpy (id, ibuf, idlen) */
516 tklen = obj->sinfo.sessiontoken_len;
517 token = obj->sinfo.sessiontoken;
524 memcpy( &idlen, ibuf, sizeof(idlen));
525 idlen = ntohl(idlen);
526 ibuf += sizeof(idlen);
527 ibuflen -= sizeof(idlen);
528 ibuf += sizeof(boottime);
529 ibuflen -= sizeof(boottime);
530 if (ibuflen < idlen || idlen > (90-10)) {
533 if (!obj->sinfo.clientid) {
534 obj->sinfo.clientid = malloc(idlen + 8);
535 memcpy(obj->sinfo.clientid, p, idlen + 8);
536 obj->sinfo.clientid_len = idlen + 8;
538 if (ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p) != 0)
540 tklen = obj->sinfo.sessiontoken_len;
541 token = obj->sinfo.sessiontoken;
544 case 8: /* Panther Kerberos Token */
545 tklen = obj->sinfo.cryptedkey_len;
546 token = obj->sinfo.cryptedkey;
558 memcpy(rbuf, &tp, sizeof(tklen));
559 rbuf += sizeof(tklen);
560 *rbuflen += sizeof(tklen);
562 memcpy(rbuf, token, tklen);
568 /* ---------------------- */
569 int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
571 DSI *dsi = (DSI *)obj->handle;
581 /* check for guest user */
582 if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
587 memcpy(&type, ibuf, sizeof(type));
589 ibuf += sizeof(type);
591 memcpy(&tklen, ibuf, sizeof(tklen));
592 tklen = ntohl(tklen);
593 ibuf += sizeof(tklen);
595 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
596 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
599 if (tklen != SESSIONTOKEN_LEN) {
602 tklen = sizeof(pid_t);
603 memcpy(&token, ibuf, tklen);
605 /* our stuff is pid + zero pad */
607 for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
613 LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
614 dsi->flags |= DSI_RECONINPROG;
616 /* Deactivate tickle timer */
617 const struct itimerval none = {{0, 0}, {0, 0}};
618 setitimer(ITIMER_REAL, &none, NULL);
620 /* check for old session, possibly transfering session from here to there */
621 if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) != 0)
623 /* write uint16_t DSI request ID */
624 if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
625 LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
628 /* now send our connected AFP client socket */
629 if (send_fd(obj->ipc_fd, dsi->socket) != 0)
631 /* Now see what happens: either afpd master sends us SIGTERM because our session */
632 /* has been transfered to a old disconnected session, or we continue */
635 if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
636 /* Reconnect succeeded, we exit now after sleeping some more */
637 sleep(2); /* sleep some more to give the recon. session time */
638 LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
643 /* Reinstall tickle timer */
644 setitimer(ITIMER_REAL, &dsi->timer, NULL);
646 LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
650 /* ---------------------- */
651 static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
655 if (!len || len > ibuflen)
656 return AFPERR_BADVERS;
658 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
659 for ( i = 0; i < num; i++ ) {
660 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
661 afp_version = afp_versions[ i ].av_number;
662 afp_version_index = i;
666 if ( i == num ) /* An inappropo version */
667 return AFPERR_BADVERS ;
669 if (afp_version >= 30 && obj->proto != AFPPROTO_DSI)
670 return AFPERR_BADVERS ;
673 if (afp_version >= 30 && sizeof(off_t) != 8) {
674 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
675 return AFPERR_BADVERS ;
681 /* ---------------------- */
682 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
684 struct passwd *pwd = NULL;
691 return send_reply(obj, AFPERR_SHUTDOWN );
694 return send_reply(obj, AFPERR_BADVERS );
697 len = (unsigned char) *ibuf++;
700 i = get_version(obj, ibuf, ibuflen, len);
702 return send_reply(obj, i );
705 return send_reply(obj, AFPERR_BADUAM);
710 len = (unsigned char) *ibuf++;
713 if (!len || len > ibuflen)
714 return send_reply(obj, AFPERR_BADUAM);
716 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
717 return send_reply(obj, AFPERR_BADUAM);
721 if (AFP_OK != (i = create_session_key(obj)) )
722 return send_reply(obj, i);
724 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
726 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
727 return send_reply(obj, i);
729 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
732 /* ---------------------- */
733 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
735 struct passwd *pwd = NULL;
745 return send_reply(obj, AFPERR_SHUTDOWN );
748 return send_reply(obj, AFPERR_BADVERS );
754 len = (unsigned char) *ibuf;
758 i = get_version(obj, ibuf, ibuflen, len);
760 return send_reply(obj, i );
763 return send_reply(obj, AFPERR_BADUAM);
768 len = (unsigned char) *ibuf;
772 if (!len || len > ibuflen)
773 return send_reply(obj, AFPERR_BADUAM);
775 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
776 return send_reply(obj, AFPERR_BADUAM);
780 if (!afp_uam->u.uam_login.login_ext) {
781 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
782 return send_reply(obj, AFPERR_BADUAM);
785 if (ibuflen <= 1 +sizeof(len16))
786 return send_reply(obj, AFPERR_PARAM);
792 return send_reply(obj, AFPERR_PARAM);
794 memcpy(&len16, ibuf, sizeof(len16));
795 ibuf += sizeof(len16);
796 ibuflen -= sizeof(len16);
799 return send_reply(obj, AFPERR_PARAM);
803 /* directory service name */
805 return send_reply(obj, AFPERR_PARAM);
814 return send_reply(obj, AFPERR_PARAM);
815 len = (unsigned char) *ibuf;
820 /* With "No User Authen" it is equal */
821 if (ibuflen < sizeof(len16))
822 return send_reply(obj, AFPERR_PARAM);
823 memcpy(&len16, ibuf, sizeof(len16));
824 ibuf += sizeof(len16);
825 ibuflen -= sizeof(len16);
829 return send_reply(obj, AFPERR_PARAM);
833 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
834 return send_reply(obj, AFPERR_PARAM);
841 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
846 if (AFP_OK != (i = create_session_key(obj)) ) {
847 return send_reply(obj, i);
850 /* FIXME user name are in UTF8 */
851 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
853 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
854 return send_reply(obj, i);
856 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
859 /* ---------------------- */
860 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
862 struct passwd *pwd = NULL;
865 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
867 return send_reply(obj, AFPERR_NOTAUTH );
870 ibuf += 2; ibuflen -= 2;
871 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
873 if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
874 return send_reply(obj, err);
876 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
880 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
882 DSI *dsi = (DSI *)(obj->handle);
884 LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
885 of_close_all_forks();
887 dsi->flags = DSI_AFP_LOGGED_OUT;
894 /* change password --
895 * NOTE: an FPLogin must already have completed successfully for this
896 * to work. this also does a little pre-processing before it hands
899 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
901 char username[MACFILELEN + 1], *start = ibuf;
910 /* check if password change is allowed, OS-X ignores the flag.
911 * we shouldn't trust the client on this anyway.
912 * not sure about the "right" error code, NOOP for now */
913 if (!(obj->options.passwdbits & PASSWD_SET))
916 /* make sure we can deal w/ this uam */
917 len = (unsigned char) *ibuf++;
918 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
919 return AFPERR_BADUAM;
922 if ((len + 1) & 1) /* pad byte */
925 if ( afp_version < 30) {
926 len = (unsigned char) *ibuf++;
927 if ( len > sizeof(username) - 1) {
930 memcpy(username, ibuf, len);
931 username[ len ] = '\0';
933 if ((len + 1) & 1) /* pad byte */
936 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
937 if ( ibuf[0] != '\0' || ibuf[1] != '\0')
940 len = MIN(sizeof(username), strlen(obj->username));
941 memcpy(username, obj->username, len);
942 username[ len ] = '\0';
946 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
948 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
951 /* send it off to the uam. we really don't use ibuflen right now. */
952 if (ibuflen < (size_t)(ibuf - start))
955 ibuflen -= (ibuf - start);
956 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
958 LOG(log_info, logtype_afpd, "password change %s.",
959 (ret == AFPERR_AUTHCONT) ? "continued" :
960 (ret ? "failed" : "succeeded"));
969 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
976 LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
981 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
982 memcpy(&bitmap, ibuf, sizeof(bitmap));
983 bitmap = ntohs(bitmap);
985 /* deal with error cases. we don't have to worry about
986 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
990 if ((bitmap & USERIBIT_ALL) != bitmap)
991 return AFPERR_BITMAP;
993 /* remember place where we store the possibly modified bitmap later */
994 memcpy(rbuf, ibuf, sizeof(bitmap));
996 rbuf += sizeof(bitmap);
997 *rbuflen = sizeof(bitmap);
999 /* copy the user/group info */
1000 if (bitmap & USERIBIT_USER) {
1001 id = htonl(geteuid());
1002 memcpy(rbuf, &id, sizeof(id));
1004 *rbuflen += sizeof(id);
1007 if (bitmap & USERIBIT_GROUP) {
1008 id = htonl(getegid());
1009 memcpy(rbuf, &id, sizeof(id));
1011 *rbuflen += sizeof(id);
1014 if (bitmap & USERIBIT_UUID) {
1015 if ( ! (obj->options.flags & OPTION_UUID)) {
1016 bitmap &= ~USERIBIT_UUID;
1017 bitmap = htons(bitmap);
1018 memcpy(bitmapp, &bitmap, sizeof(bitmap));
1020 LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
1023 ret = getuuidfromname( obj->username, UUID_USER, uuid);
1025 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
1026 return AFPERR_NOITEM;
1028 LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
1030 memcpy(rbuf, uuid, UUID_BINSIZE);
1031 rbuf += UUID_BINSIZE;
1032 *rbuflen += UUID_BINSIZE;
1036 LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
1040 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
1041 (((type) == UAM_SERVER_CHANGEPW) ? \
1042 &uam_changepw : NULL))
1044 /* just do a linked list search. this could be sped up with a hashed
1045 * list, but i doubt anyone's going to have enough uams to matter. */
1046 struct uam_obj *auth_uamfind(const int type, const char *name,
1049 struct uam_obj *prev, *start;
1051 if (!name || !(start = UAM_LIST(type)))
1055 while ((prev = prev->uam_prev) != start)
1056 if (strndiacasecmp(prev->uam_name, name, len) == 0)
1062 int auth_register(const int type, struct uam_obj *uam)
1064 struct uam_obj *start;
1066 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1069 if (!(start = UAM_LIST(type)))
1070 return 1; /* we don't know what to do with it, caller must free it */
1072 uam_attach(start, uam);
1076 /* load all of the modules */
1077 int auth_load(const char *path, const char *list)
1079 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
1080 struct uam_mod *mod;
1084 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1087 strlcpy(buf, list, sizeof(buf));
1088 if ((p = strtok(buf, ",")) == NULL)
1092 if (name[len - 1] != '/') {
1098 strlcpy(name + len, p, sizeof(name) - len);
1099 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1101 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1103 if (stat(name, &st) == 0) {
1104 if ((mod = uam_load(name, p))) {
1105 uam_attach(&uam_modules, mod);
1106 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1108 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1111 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1113 p = strtok(NULL, ",");
1119 /* get rid of all of the uams */
1120 void auth_unload(void)
1122 struct uam_mod *mod, *prev, *start = &uam_modules;
1124 prev = start->uam_prev;
1125 while ((mod = prev) != start) {
1126 prev = prev->uam_prev;