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>
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 static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void), int expired)
245 #endif /* ADMIN_GRP */
248 set_processname("afpd");
251 if ( pwd->pw_uid == 0 ) { /* don't allow root login */
252 LOG(log_error, logtype_afpd, "login: root login denied!" );
253 return AFPERR_NOTAUTH;
256 LOG(log_note, logtype_afpd, "%s Login by %s",
257 afp_versions[afp_version_index].av_name, pwd->pw_name);
259 if (initgroups( pwd->pw_name, pwd->pw_gid ) < 0) {
261 LOG(log_info, logtype_afpd, "running with uid %d", geteuid());
262 #else /* RUN_AS_USER */
263 LOG(log_error, logtype_afpd, "login: %s", strerror(errno));
264 return AFPERR_BADUAM;
265 #endif /* RUN_AS_USER */
269 /* Basically if the user is in the admin group, we stay root */
271 if (( ngroups = getgroups( 0, NULL )) < 0 ) {
272 LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
273 return AFPERR_BADUAM;
276 if ( NULL == (groups = calloc(ngroups, GROUPS_SIZE)) ) {
277 LOG(log_error, logtype_afpd, "login: %s calloc: %d", ngroups);
278 return AFPERR_BADUAM;
281 if (( ngroups = getgroups( ngroups, groups )) < 0 ) {
282 LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
283 return AFPERR_BADUAM;
287 LOG(log_debug, logtype_afpd, "obj->options.admingid == %d", obj->options.admingid);
289 if (obj->options.admingid != 0) {
291 for (i = 0; i < ngroups; i++) {
292 if (groups[i] == obj->options.admingid) admin = 1;
297 LOG(log_info, logtype_afpd, "admin login -- %s", pwd->pw_name );
300 #endif /* ADMIN_GRP */
303 struct DSI *dsi = obj->handle;
310 afp_get_cmdline( &argc, &argv );
312 hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
313 sizeof( struct in_addr ),
314 dsi->client.sin_family );
317 clientname = hp->h_name;
319 clientname = inet_ntoa( dsi->client.sin_addr );
321 sprintf( hostname, "%s@%s", pwd->pw_name, clientname );
323 if( sia_become_user( NULL, argc, argv, hostname, pwd->pw_name,
324 NULL, FALSE, NULL, NULL,
325 SIA_BEU_REALLOGIN ) != SIASUCCESS )
326 return AFPERR_BADUAM;
328 LOG(log_info, logtype_afpd, "session from %s (%s)", hostname,
329 inet_ntoa( dsi->client.sin_addr ) );
331 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
332 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
333 return AFPERR_BADUAM;
337 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
338 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
339 return AFPERR_BADUAM;
344 #define GROUPSTR_BUFSIZE 1024
345 char groupsstr[GROUPSTR_BUFSIZE];
347 int j = GROUPSTR_BUFSIZE;
349 int n = snprintf(groupsstr, GROUPSTR_BUFSIZE, "%u", groups[0]);
353 for (int i = 1; i < ngroups; i++) {
354 n = snprintf(s, j, ", %u", groups[i]);
357 LOG(log_debug, logtype_afpd, "login: group string buffer overflow");
363 LOG(log_debug, logtype_afpd, "login: %u supplementary groups: %s", ngroups, groupsstr);
366 /* There's probably a better way to do this, but for now, we just play root */
371 #endif /* ADMIN_GRP */
374 set_auth_switch(expired);
375 /* save our euid, we need it for preexec_close */
376 obj->uid = geteuid();
377 obj->logout = logout;
382 /* ---------------------- */
383 int afp_zzz(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
386 DSI *dsi = (DSI *)AFPobj->handle;
394 memcpy(&data, ibuf, 4); /* flag */
398 * Possible sleeping states:
399 * 1) normal sleep: DSI_SLEEPING (up to 10.3)
400 * 2) extended sleep: DSI_SLEEPING | DSI_EXTSLEEP (starting with 10.4)
403 if (data & AFPZZZ_EXT_WAKEUP) {
404 /* wakeup request from exetended sleep */
405 if (dsi->flags & DSI_EXTSLEEP) {
406 LOG(log_note, logtype_afpd, "afp_zzz: waking up from extended sleep");
407 dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP);
411 dsi->flags |= DSI_SLEEPING;
412 if (data & AFPZZZ_EXT_SLEEP) {
413 LOG(log_note, logtype_afpd, "afp_zzz: entering extended sleep");
414 dsi->flags |= DSI_EXTSLEEP;
416 LOG(log_note, logtype_afpd, "afp_zzz: entering normal sleep");
421 * According to AFP 3.3 spec we should not return anything,
422 * but eg 10.5.8 server still returns the numbers of hours
423 * the server is keeping the sessino (ie max sleeptime).
425 data = obj->options.sleep / 120; /* hours */
429 *rbuflen = sizeof(data);
431 memcpy(rbuf, &data, sizeof(data));
432 rbuf += sizeof(data);
437 /* ---------------------- */
438 static int create_session_token(AFPObj *obj)
442 /* use 8 bytes for token as OSX, don't know if it helps */
443 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
444 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
448 if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
451 memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
452 obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
454 memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
459 static int create_session_key(AFPObj *obj)
461 /* create session key */
462 if (obj->sinfo.sessionkey == NULL) {
463 if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
465 uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
466 obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
472 /* ---------------------- */
475 char *ibuf, size_t ibuflen,
476 char *rbuf, size_t *rbuflen)
488 if (ibuflen < 2 + sizeof(type)) {
495 memcpy(&type, ibuf, sizeof(type));
497 ibuf += sizeof(type);
498 ibuflen -= sizeof(type);
500 if ( obj->sinfo.sessiontoken == NULL ) {
501 if ( create_session_token( obj ) )
509 case 0: /* old version ?*/
510 tklen = obj->sinfo.sessiontoken_len;
511 token = obj->sinfo.sessiontoken;
513 case 1: /* disconnect */
514 case 2: /* reconnect update id */
515 if (ibuflen >= sizeof(idlen)) {
516 memcpy(&idlen, ibuf, sizeof(idlen));
517 idlen = ntohl(idlen);
518 ibuf += sizeof(idlen);
519 ibuflen -= sizeof(idlen);
520 if (ibuflen < idlen) {
523 /* memcpy (id, ibuf, idlen) */
524 tklen = obj->sinfo.sessiontoken_len;
525 token = obj->sinfo.sessiontoken;
532 memcpy( &idlen, ibuf, sizeof(idlen));
533 idlen = ntohl(idlen);
534 ibuf += sizeof(idlen);
535 ibuflen -= sizeof(idlen);
536 ibuf += sizeof(boottime);
537 ibuflen -= sizeof(boottime);
538 if (ibuflen < idlen || idlen > (90-10)) {
541 ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p);
542 tklen = obj->sinfo.sessiontoken_len;
543 token = obj->sinfo.sessiontoken;
546 case 8: /* Panther Kerberos Token */
547 tklen = obj->sinfo.cryptedkey_len;
548 token = obj->sinfo.cryptedkey;
560 memcpy(rbuf, &tp, sizeof(tklen));
561 rbuf += sizeof(tklen);
562 *rbuflen += sizeof(tklen);
564 memcpy(rbuf, token, tklen);
570 /* ---------------------- */
571 int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
573 DSI *dsi = (DSI *)obj->handle;
583 /* check for guest user */
584 if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
589 memcpy(&type, ibuf, sizeof(type));
591 ibuf += sizeof(type);
593 memcpy(&tklen, ibuf, sizeof(tklen));
594 tklen = ntohl(tklen);
595 ibuf += sizeof(tklen);
597 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
598 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
601 if (tklen != SESSIONTOKEN_LEN) {
604 tklen = sizeof(pid_t);
605 memcpy(&token, ibuf, tklen);
607 /* our stuff is pid + zero pad */
609 for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
615 LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
616 dsi->flags |= DSI_RECONINPROG;
618 /* Deactivate tickle timer */
619 const struct itimerval none = {{0, 0}, {0, 0}};
620 setitimer(ITIMER_REAL, &none, NULL);
622 /* check for old session, possibly transfering session from here to there */
623 if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) == -1)
625 /* write uint16_t DSI request ID */
626 if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
627 LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
630 /* now send our connected AFP client socket */
631 if (send_fd(obj->ipc_fd, dsi->socket) != 0)
633 /* Now see what happens: either afpd master sends us SIGTERM because our session */
634 /* has been transfered to a old disconnected session, or we continue */
637 if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
638 /* Reconnect succeeded, we exit now after sleeping some more */
639 sleep(2); /* sleep some more to give the recon. session time */
640 LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
645 /* Reinstall tickle timer */
646 setitimer(ITIMER_REAL, &dsi->timer, NULL);
648 LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
652 /* ---------------------- */
653 static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
657 if (!len || len > ibuflen)
658 return AFPERR_BADVERS;
660 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
661 for ( i = 0; i < num; i++ ) {
662 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
663 afp_version = afp_versions[ i ].av_number;
664 afp_version_index = i;
668 if ( i == num ) /* An inappropo version */
669 return AFPERR_BADVERS ;
671 if (afp_version >= 30 && obj->proto != AFPPROTO_DSI)
672 return AFPERR_BADVERS ;
675 if (afp_version >= 30 && sizeof(off_t) != 8) {
676 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
677 return AFPERR_BADVERS ;
683 /* ---------------------- */
684 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
686 struct passwd *pwd = NULL;
693 return send_reply(obj, AFPERR_SHUTDOWN );
696 return send_reply(obj, AFPERR_BADVERS );
699 len = (unsigned char) *ibuf++;
702 i = get_version(obj, ibuf, ibuflen, len);
704 return send_reply(obj, i );
707 return send_reply(obj, AFPERR_BADUAM);
712 len = (unsigned char) *ibuf++;
715 if (!len || len > ibuflen)
716 return send_reply(obj, AFPERR_BADUAM);
718 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
719 return send_reply(obj, AFPERR_BADUAM);
723 if (AFP_OK != (i = create_session_key(obj)) )
724 return send_reply(obj, i);
726 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
728 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
729 return send_reply(obj, i);
731 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
734 /* ---------------------- */
735 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
737 struct passwd *pwd = NULL;
747 return send_reply(obj, AFPERR_SHUTDOWN );
750 return send_reply(obj, AFPERR_BADVERS );
756 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;
774 if (!len || len > ibuflen)
775 return send_reply(obj, AFPERR_BADUAM);
777 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
778 return send_reply(obj, AFPERR_BADUAM);
782 if (!afp_uam->u.uam_login.login_ext) {
783 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
784 return send_reply(obj, AFPERR_BADUAM);
787 if (ibuflen <= 1 +sizeof(len16))
788 return send_reply(obj, AFPERR_PARAM);
794 return send_reply(obj, AFPERR_PARAM);
796 memcpy(&len16, ibuf, sizeof(len16));
797 ibuf += sizeof(len16);
798 ibuflen -= sizeof(len16);
801 return send_reply(obj, AFPERR_PARAM);
805 /* directory service name */
807 return send_reply(obj, AFPERR_PARAM);
816 return send_reply(obj, AFPERR_PARAM);
817 len = (unsigned char) *ibuf;
822 /* With "No User Authen" it is equal */
823 if (ibuflen < sizeof(len16))
824 return send_reply(obj, AFPERR_PARAM);
825 memcpy(&len16, ibuf, sizeof(len16));
826 ibuf += sizeof(len16);
827 ibuflen -= sizeof(len16);
831 return send_reply(obj, AFPERR_PARAM);
835 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
836 return send_reply(obj, AFPERR_PARAM);
843 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
848 if (AFP_OK != (i = create_session_key(obj)) ) {
849 return send_reply(obj, i);
852 /* FIXME user name are in UTF8 */
853 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
855 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
856 return send_reply(obj, i);
858 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
861 /* ---------------------- */
862 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
864 struct passwd *pwd = NULL;
867 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
869 return send_reply(obj, AFPERR_NOTAUTH );
872 ibuf += 2; ibuflen -= 2;
873 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
875 if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
876 return send_reply(obj, err);
878 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
882 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
884 DSI *dsi = (DSI *)(obj->handle);
886 LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
887 of_close_all_forks();
889 dsi->flags = DSI_AFP_LOGGED_OUT;
896 /* change password --
897 * NOTE: an FPLogin must already have completed successfully for this
898 * to work. this also does a little pre-processing before it hands
901 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
903 char username[MACFILELEN + 1], *start = ibuf;
912 /* check if password change is allowed, OS-X ignores the flag.
913 * we shouldn't trust the client on this anyway.
914 * not sure about the "right" error code, NOOP for now */
915 if (!(obj->options.passwdbits & PASSWD_SET))
918 /* make sure we can deal w/ this uam */
919 len = (unsigned char) *ibuf++;
920 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
921 return AFPERR_BADUAM;
924 if ((len + 1) & 1) /* pad byte */
927 if ( afp_version < 30) {
928 len = (unsigned char) *ibuf++;
929 if ( len > sizeof(username) - 1) {
932 memcpy(username, ibuf, len);
933 username[ len ] = '\0';
935 if ((len + 1) & 1) /* pad byte */
938 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
939 if ( ibuf[0] != '\0' || ibuf[1] != '\0')
942 len = MIN(sizeof(username), strlen(obj->username));
943 memcpy(username, obj->username, len);
944 username[ len ] = '\0';
948 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
950 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
953 /* send it off to the uam. we really don't use ibuflen right now. */
954 if (ibuflen < (size_t)(ibuf - start))
957 ibuflen -= (ibuf - start);
958 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
960 LOG(log_info, logtype_afpd, "password change %s.",
961 (ret == AFPERR_AUTHCONT) ? "continued" :
962 (ret ? "failed" : "succeeded"));
971 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
978 LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
983 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
984 memcpy(&bitmap, ibuf, sizeof(bitmap));
985 bitmap = ntohs(bitmap);
987 /* deal with error cases. we don't have to worry about
988 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
992 if ((bitmap & USERIBIT_ALL) != bitmap)
993 return AFPERR_BITMAP;
995 /* remember place where we store the possibly modified bitmap later */
996 memcpy(rbuf, ibuf, sizeof(bitmap));
998 rbuf += sizeof(bitmap);
999 *rbuflen = sizeof(bitmap);
1001 /* copy the user/group info */
1002 if (bitmap & USERIBIT_USER) {
1003 id = htonl(geteuid());
1004 memcpy(rbuf, &id, sizeof(id));
1006 *rbuflen += sizeof(id);
1009 if (bitmap & USERIBIT_GROUP) {
1010 id = htonl(getegid());
1011 memcpy(rbuf, &id, sizeof(id));
1013 *rbuflen += sizeof(id);
1016 if (bitmap & USERIBIT_UUID) {
1017 if ( ! (obj->options.flags & OPTION_UUID)) {
1018 bitmap &= ~USERIBIT_UUID;
1019 bitmap = htons(bitmap);
1020 memcpy(bitmapp, &bitmap, sizeof(bitmap));
1022 LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
1025 ret = getuuidfromname( obj->username, UUID_USER, uuid);
1027 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
1028 return AFPERR_NOITEM;
1030 LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
1032 memcpy(rbuf, uuid, UUID_BINSIZE);
1033 rbuf += UUID_BINSIZE;
1034 *rbuflen += UUID_BINSIZE;
1038 LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
1042 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
1043 (((type) == UAM_SERVER_CHANGEPW) ? \
1044 &uam_changepw : NULL))
1046 /* just do a linked list search. this could be sped up with a hashed
1047 * list, but i doubt anyone's going to have enough uams to matter. */
1048 struct uam_obj *auth_uamfind(const int type, const char *name,
1051 struct uam_obj *prev, *start;
1053 if (!name || !(start = UAM_LIST(type)))
1057 while ((prev = prev->uam_prev) != start)
1058 if (strndiacasecmp(prev->uam_name, name, len) == 0)
1064 int auth_register(const int type, struct uam_obj *uam)
1066 struct uam_obj *start;
1068 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1071 if (!(start = UAM_LIST(type)))
1072 return 1; /* we don't know what to do with it, caller must free it */
1074 uam_attach(start, uam);
1078 /* load all of the modules */
1079 int auth_load(const char *path, const char *list)
1081 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
1082 struct uam_mod *mod;
1086 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1089 strlcpy(buf, list, sizeof(buf));
1090 if ((p = strtok(buf, ",")) == NULL)
1094 if (name[len - 1] != '/') {
1100 strlcpy(name + len, p, sizeof(name) - len);
1101 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1103 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1105 if (stat(name, &st) == 0) {
1106 if ((mod = uam_load(name, p))) {
1107 uam_attach(&uam_modules, mod);
1108 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1110 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1113 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1115 p = strtok(NULL, ",");
1121 /* get rid of all of the uams */
1122 void auth_unload(void)
1124 struct uam_mod *mod, *prev, *start = &uam_modules;
1126 prev = start->uam_prev;
1127 while ((mod = prev) != start) {
1128 prev = prev->uam_prev;