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 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 if (!obj->sinfo.clientid) {
542 obj->sinfo.clientid = malloc(idlen + 8);
543 memcpy(obj->sinfo.clientid, p, idlen + 8);
544 obj->sinfo.clientid_len = idlen + 8;
546 if (ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p) != 0)
548 tklen = obj->sinfo.sessiontoken_len;
549 token = obj->sinfo.sessiontoken;
552 case 8: /* Panther Kerberos Token */
553 tklen = obj->sinfo.cryptedkey_len;
554 token = obj->sinfo.cryptedkey;
566 memcpy(rbuf, &tp, sizeof(tklen));
567 rbuf += sizeof(tklen);
568 *rbuflen += sizeof(tklen);
570 memcpy(rbuf, token, tklen);
576 /* ---------------------- */
577 int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
579 DSI *dsi = (DSI *)obj->handle;
589 /* check for guest user */
590 if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
595 memcpy(&type, ibuf, sizeof(type));
597 ibuf += sizeof(type);
599 memcpy(&tklen, ibuf, sizeof(tklen));
600 tklen = ntohl(tklen);
601 ibuf += sizeof(tklen);
603 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
604 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
607 if (tklen != SESSIONTOKEN_LEN) {
610 tklen = sizeof(pid_t);
611 memcpy(&token, ibuf, tklen);
613 /* our stuff is pid + zero pad */
615 for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
621 LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
622 dsi->flags |= DSI_RECONINPROG;
624 /* Deactivate tickle timer */
625 const struct itimerval none = {{0, 0}, {0, 0}};
626 setitimer(ITIMER_REAL, &none, NULL);
628 /* check for old session, possibly transfering session from here to there */
629 if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) != 0)
631 /* write uint16_t DSI request ID */
632 if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
633 LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
636 /* now send our connected AFP client socket */
637 if (send_fd(obj->ipc_fd, dsi->socket) != 0)
639 /* Now see what happens: either afpd master sends us SIGTERM because our session */
640 /* has been transfered to a old disconnected session, or we continue */
643 if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
644 /* Reconnect succeeded, we exit now after sleeping some more */
645 sleep(2); /* sleep some more to give the recon. session time */
646 LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
651 /* Reinstall tickle timer */
652 setitimer(ITIMER_REAL, &dsi->timer, NULL);
654 LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
658 /* ---------------------- */
659 static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
663 if (!len || len > ibuflen)
664 return AFPERR_BADVERS;
666 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
667 for ( i = 0; i < num; i++ ) {
668 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
669 afp_version = afp_versions[ i ].av_number;
670 afp_version_index = i;
674 if ( i == num ) /* An inappropo version */
675 return AFPERR_BADVERS ;
677 if (afp_version >= 30 && obj->proto != AFPPROTO_DSI)
678 return AFPERR_BADVERS ;
681 if (afp_version >= 30 && sizeof(off_t) != 8) {
682 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
683 return AFPERR_BADVERS ;
689 /* ---------------------- */
690 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
692 struct passwd *pwd = NULL;
699 return send_reply(obj, AFPERR_SHUTDOWN );
702 return send_reply(obj, AFPERR_BADVERS );
705 len = (unsigned char) *ibuf++;
708 i = get_version(obj, ibuf, ibuflen, len);
710 return send_reply(obj, i );
713 return send_reply(obj, AFPERR_BADUAM);
718 len = (unsigned char) *ibuf++;
721 if (!len || len > ibuflen)
722 return send_reply(obj, AFPERR_BADUAM);
724 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
725 return send_reply(obj, AFPERR_BADUAM);
729 if (AFP_OK != (i = create_session_key(obj)) )
730 return send_reply(obj, i);
732 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
734 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
735 return send_reply(obj, i);
737 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
740 /* ---------------------- */
741 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
743 struct passwd *pwd = NULL;
753 return send_reply(obj, AFPERR_SHUTDOWN );
756 return send_reply(obj, AFPERR_BADVERS );
762 len = (unsigned char) *ibuf;
766 i = get_version(obj, ibuf, ibuflen, len);
768 return send_reply(obj, i );
771 return send_reply(obj, AFPERR_BADUAM);
776 len = (unsigned char) *ibuf;
780 if (!len || len > ibuflen)
781 return send_reply(obj, AFPERR_BADUAM);
783 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
784 return send_reply(obj, AFPERR_BADUAM);
788 if (!afp_uam->u.uam_login.login_ext) {
789 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
790 return send_reply(obj, AFPERR_BADUAM);
793 if (ibuflen <= 1 +sizeof(len16))
794 return send_reply(obj, AFPERR_PARAM);
800 return send_reply(obj, AFPERR_PARAM);
802 memcpy(&len16, ibuf, sizeof(len16));
803 ibuf += sizeof(len16);
804 ibuflen -= sizeof(len16);
807 return send_reply(obj, AFPERR_PARAM);
811 /* directory service name */
813 return send_reply(obj, AFPERR_PARAM);
822 return send_reply(obj, AFPERR_PARAM);
823 len = (unsigned char) *ibuf;
828 /* With "No User Authen" it is equal */
829 if (ibuflen < sizeof(len16))
830 return send_reply(obj, AFPERR_PARAM);
831 memcpy(&len16, ibuf, sizeof(len16));
832 ibuf += sizeof(len16);
833 ibuflen -= sizeof(len16);
837 return send_reply(obj, AFPERR_PARAM);
841 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
842 return send_reply(obj, AFPERR_PARAM);
849 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
854 if (AFP_OK != (i = create_session_key(obj)) ) {
855 return send_reply(obj, i);
858 /* FIXME user name are in UTF8 */
859 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
861 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
862 return send_reply(obj, i);
864 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
867 /* ---------------------- */
868 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
870 struct passwd *pwd = NULL;
873 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
875 return send_reply(obj, AFPERR_NOTAUTH );
878 ibuf += 2; ibuflen -= 2;
879 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
881 if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
882 return send_reply(obj, err);
884 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
888 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
890 DSI *dsi = (DSI *)(obj->handle);
892 LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
893 of_close_all_forks();
895 dsi->flags = DSI_AFP_LOGGED_OUT;
902 /* change password --
903 * NOTE: an FPLogin must already have completed successfully for this
904 * to work. this also does a little pre-processing before it hands
907 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
909 char username[MACFILELEN + 1], *start = ibuf;
918 /* check if password change is allowed, OS-X ignores the flag.
919 * we shouldn't trust the client on this anyway.
920 * not sure about the "right" error code, NOOP for now */
921 if (!(obj->options.passwdbits & PASSWD_SET))
924 /* make sure we can deal w/ this uam */
925 len = (unsigned char) *ibuf++;
926 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
927 return AFPERR_BADUAM;
930 if ((len + 1) & 1) /* pad byte */
933 if ( afp_version < 30) {
934 len = (unsigned char) *ibuf++;
935 if ( len > sizeof(username) - 1) {
938 memcpy(username, ibuf, len);
939 username[ len ] = '\0';
941 if ((len + 1) & 1) /* pad byte */
944 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
945 if ( ibuf[0] != '\0' || ibuf[1] != '\0')
948 len = MIN(sizeof(username), strlen(obj->username));
949 memcpy(username, obj->username, len);
950 username[ len ] = '\0';
954 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
956 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
959 /* send it off to the uam. we really don't use ibuflen right now. */
960 if (ibuflen < (size_t)(ibuf - start))
963 ibuflen -= (ibuf - start);
964 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
966 LOG(log_info, logtype_afpd, "password change %s.",
967 (ret == AFPERR_AUTHCONT) ? "continued" :
968 (ret ? "failed" : "succeeded"));
977 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
984 LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
989 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
990 memcpy(&bitmap, ibuf, sizeof(bitmap));
991 bitmap = ntohs(bitmap);
993 /* deal with error cases. we don't have to worry about
994 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
998 if ((bitmap & USERIBIT_ALL) != bitmap)
999 return AFPERR_BITMAP;
1001 /* remember place where we store the possibly modified bitmap later */
1002 memcpy(rbuf, ibuf, sizeof(bitmap));
1004 rbuf += sizeof(bitmap);
1005 *rbuflen = sizeof(bitmap);
1007 /* copy the user/group info */
1008 if (bitmap & USERIBIT_USER) {
1009 id = htonl(geteuid());
1010 memcpy(rbuf, &id, sizeof(id));
1012 *rbuflen += sizeof(id);
1015 if (bitmap & USERIBIT_GROUP) {
1016 id = htonl(getegid());
1017 memcpy(rbuf, &id, sizeof(id));
1019 *rbuflen += sizeof(id);
1022 if (bitmap & USERIBIT_UUID) {
1023 if ( ! (obj->options.flags & OPTION_UUID)) {
1024 bitmap &= ~USERIBIT_UUID;
1025 bitmap = htons(bitmap);
1026 memcpy(bitmapp, &bitmap, sizeof(bitmap));
1028 LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
1031 ret = getuuidfromname( obj->username, UUID_USER, uuid);
1033 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
1034 return AFPERR_NOITEM;
1036 LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
1038 memcpy(rbuf, uuid, UUID_BINSIZE);
1039 rbuf += UUID_BINSIZE;
1040 *rbuflen += UUID_BINSIZE;
1044 LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
1048 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
1049 (((type) == UAM_SERVER_CHANGEPW) ? \
1050 &uam_changepw : NULL))
1052 /* just do a linked list search. this could be sped up with a hashed
1053 * list, but i doubt anyone's going to have enough uams to matter. */
1054 struct uam_obj *auth_uamfind(const int type, const char *name,
1057 struct uam_obj *prev, *start;
1059 if (!name || !(start = UAM_LIST(type)))
1063 while ((prev = prev->uam_prev) != start)
1064 if (strndiacasecmp(prev->uam_name, name, len) == 0)
1070 int auth_register(const int type, struct uam_obj *uam)
1072 struct uam_obj *start;
1074 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1077 if (!(start = UAM_LIST(type)))
1078 return 1; /* we don't know what to do with it, caller must free it */
1080 uam_attach(start, uam);
1084 /* load all of the modules */
1085 int auth_load(const char *path, const char *list)
1087 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
1088 struct uam_mod *mod;
1092 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1095 strlcpy(buf, list, sizeof(buf));
1096 if ((p = strtok(buf, ",")) == NULL)
1100 if (name[len - 1] != '/') {
1106 strlcpy(name + len, p, sizeof(name) - len);
1107 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1109 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1111 if (stat(name, &st) == 0) {
1112 if ((mod = uam_load(name, p))) {
1113 uam_attach(&uam_modules, mod);
1114 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1116 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1119 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1121 p = strtok(NULL, ",");
1127 /* get rid of all of the uams */
1128 void auth_unload(void)
1130 struct uam_mod *mod, *prev, *start = &uam_modules;
1132 prev = start->uam_prev;
1133 while ((mod = prev) != start) {
1134 prev = prev->uam_prev;