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 */
71 static struct uam_mod uam_modules = {NULL, NULL, &uam_modules, &uam_modules};
72 static struct uam_obj uam_login = {"", "", 0, {{NULL, NULL, NULL, NULL }}, &uam_login,
74 static struct uam_obj uam_changepw = {"", "", 0, {{NULL, NULL, NULL, NULL}}, &uam_changepw,
77 static struct uam_obj *afp_uam = NULL;
80 void status_versions( char *data, const DSI *dsi)
84 int len, num, i, count = 0;
86 memcpy(&status, start + AFPSTATUS_VERSOFF, sizeof(status));
87 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ] );
89 for ( i = 0; i < num; i++ ) {
90 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
93 data += ntohs( status );
96 for ( i = 0; i < num; i++ ) {
97 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
98 len = strlen( afp_versions[ i ].av_name );
100 memcpy( data, afp_versions[ i ].av_name , len );
103 status = htons( data - start );
104 memcpy(start + AFPSTATUS_UAMSOFF, &status, sizeof(status));
107 void status_uams(char *data, const char *authlist)
111 struct uam_obj *uams;
114 memcpy(&status, start + AFPSTATUS_UAMSOFF, sizeof(status));
116 while ((uams = uams->uam_prev) != &uam_login) {
117 if (strstr(authlist, uams->uam_path))
121 data += ntohs( status );
123 while ((uams = uams->uam_prev) != &uam_login) {
124 if (strstr(authlist, uams->uam_path)) {
125 LOG(log_info, logtype_afpd, "uam: \"%s\" available", uams->uam_name);
126 len = strlen( uams->uam_name);
128 memcpy( data, uams->uam_name, len );
134 status = htons(data - start);
135 memcpy(start + AFPSTATUS_ICONOFF, &status, sizeof(status));
138 /* handle errors by closing the connection. this is only needed
139 * by the afp_* functions. */
140 static int send_reply(const AFPObj *obj, const int err)
142 if ((err == AFP_OK) || (err == AFPERR_AUTHCONT))
145 obj->reply(obj->dsi, err);
151 static int afp_errpwdexpired(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
152 char *rbuf _U_, size_t *rbuflen)
155 return AFPERR_PWDEXPR;
158 static int afp_null_nolog(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
159 char *rbuf _U_, size_t *rbuflen)
162 return( AFPERR_NOOP );
165 static int set_auth_switch(int expired)
171 * BF: expired password handling
172 * to allow the user to change his/her password we have to allow login
173 * but every following call except for FPChangePassword will be thrown
174 * away with an AFPERR_PWDEXPR error. (thanks to Leland Wallace from Apple
175 * for clarifying this)
178 for (i=0; i<=0xff; i++) {
179 uam_afpserver_action(i, UAM_AFPSERVER_PREAUTH, afp_errpwdexpired, NULL);
181 uam_afpserver_action(AFP_LOGOUT, UAM_AFPSERVER_PREAUTH, afp_logout, NULL);
182 uam_afpserver_action(AFP_CHANGEPW, UAM_AFPSERVER_PREAUTH, afp_changepw, NULL);
185 afp_switch = postauth_switch;
186 switch (afp_version) {
191 uam_afpserver_action(AFP_GETACL, UAM_AFPSERVER_POSTAUTH, afp_getacl, NULL);
192 uam_afpserver_action(AFP_SETACL, UAM_AFPSERVER_POSTAUTH, afp_setacl, NULL);
193 uam_afpserver_action(AFP_ACCESS, UAM_AFPSERVER_POSTAUTH, afp_access, NULL);
194 #endif /* HAVE_ACLS */
195 uam_afpserver_action(AFP_GETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_getextattr, NULL);
196 uam_afpserver_action(AFP_SETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_setextattr, NULL);
197 uam_afpserver_action(AFP_REMOVEATTR, UAM_AFPSERVER_POSTAUTH, afp_remextattr, NULL);
198 uam_afpserver_action(AFP_LISTEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_listextattr, NULL);
201 uam_afpserver_action(AFP_SYNCDIR, UAM_AFPSERVER_POSTAUTH, afp_syncdir, NULL);
202 uam_afpserver_action(AFP_SYNCFORK, UAM_AFPSERVER_POSTAUTH, afp_syncfork, NULL);
203 uam_afpserver_action(AFP_SPOTLIGHT_PRIVATE, UAM_AFPSERVER_POSTAUTH, afp_null_nolog, NULL);
204 uam_afpserver_action(AFP_ENUMERATE_EXT2, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext2, NULL);
207 uam_afpserver_action(AFP_ENUMERATE_EXT, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext, NULL);
208 uam_afpserver_action(AFP_BYTELOCK_EXT, UAM_AFPSERVER_POSTAUTH, afp_bytelock_ext, NULL);
209 /* catsearch_ext uses the same packet as catsearch FIXME double check this, it wasn't true for enue
211 uam_afpserver_action(AFP_CATSEARCH_EXT, UAM_AFPSERVER_POSTAUTH, afp_catsearch_ext, NULL);
212 uam_afpserver_action(AFP_GETSESSTOKEN, UAM_AFPSERVER_POSTAUTH, afp_getsession, NULL);
213 uam_afpserver_action(AFP_READ_EXT, UAM_AFPSERVER_POSTAUTH, afp_read_ext, NULL);
214 uam_afpserver_action(AFP_WRITE_EXT, UAM_AFPSERVER_POSTAUTH, afp_write_ext, NULL);
215 uam_afpserver_action(AFP_DISCTOLDSESS, UAM_AFPSERVER_POSTAUTH, afp_disconnect, NULL);
219 * If first connection to a server is done in classic AFP2.2 version is used
220 * but OSX uses AFP3.x FPzzz command !
222 uam_afpserver_action(AFP_ZZZ, UAM_AFPSERVER_POSTAUTH, afp_zzz, NULL);
230 #define GROUPSTR_BUFSIZE 1024
231 static const char *print_groups(int ngroups, gid_t *groups)
233 static char groupsstr[GROUPSTR_BUFSIZE];
240 for (i = 0; (i < ngroups) && (s < &groupsstr[GROUPSTR_BUFSIZE]); i++) {
241 s += snprintf(s, &groupsstr[GROUPSTR_BUFSIZE] - s, " %u", groups[i]);
247 static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void), int expired)
251 #endif /* ADMIN_GRP */
253 if ( pwd->pw_uid == 0 ) { /* don't allow root login */
254 LOG(log_error, logtype_afpd, "login: root login denied!" );
255 return AFPERR_NOTAUTH;
258 LOG(log_note, logtype_afpd, "%s Login by %s",
259 afp_versions[afp_version_index].av_name, pwd->pw_name);
261 if (initgroups( pwd->pw_name, pwd->pw_gid ) < 0) {
263 LOG(log_info, logtype_afpd, "running with uid %d", geteuid());
264 #else /* RUN_AS_USER */
265 LOG(log_error, logtype_afpd, "login: %s", strerror(errno));
266 return AFPERR_BADUAM;
267 #endif /* RUN_AS_USER */
271 /* Basically if the user is in the admin group, we stay root */
273 if (( ngroups = getgroups( 0, NULL )) < 0 ) {
274 LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
275 return AFPERR_BADUAM;
278 if ( NULL == (groups = calloc(ngroups, GROUPS_SIZE)) ) {
279 LOG(log_error, logtype_afpd, "login: %s calloc: %d", ngroups);
280 return AFPERR_BADUAM;
283 if (( ngroups = getgroups( ngroups, groups )) < 0 ) {
284 LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
285 return AFPERR_BADUAM;
289 LOG(log_debug, logtype_afpd, "obj->options.admingid == %d", obj->options.admingid);
291 if (obj->options.admingid != 0) {
293 for (i = 0; i < ngroups; i++) {
294 if (groups[i] == obj->options.admingid) admin = 1;
299 LOG(log_info, logtype_afpd, "admin login -- %s", pwd->pw_name );
302 #endif /* ADMIN_GRP */
305 struct DSI *dsi = obj->handle;
312 afp_get_cmdline( &argc, &argv );
314 hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
315 sizeof( struct in_addr ),
316 dsi->client.sin_family );
319 clientname = hp->h_name;
321 clientname = inet_ntoa( dsi->client.sin_addr );
323 sprintf( hostname, "%s@%s", pwd->pw_name, clientname );
325 if( sia_become_user( NULL, argc, argv, hostname, pwd->pw_name,
326 NULL, FALSE, NULL, NULL,
327 SIA_BEU_REALLOGIN ) != SIASUCCESS )
328 return AFPERR_BADUAM;
330 LOG(log_info, logtype_afpd, "session from %s (%s)", hostname,
331 inet_ntoa( dsi->client.sin_addr ) );
333 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
334 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
335 return AFPERR_BADUAM;
339 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
340 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
341 return AFPERR_BADUAM;
345 LOG(log_debug, logtype_afpd, "login: supplementary groups: %s", print_groups(ngroups, groups));
347 /* There's probably a better way to do this, but for now, we just play root */
352 #endif /* ADMIN_GRP */
355 set_auth_switch(expired);
356 /* save our euid, we need it for preexec_close */
357 obj->uid = geteuid();
358 obj->logout = logout;
363 /* ---------------------- */
364 int afp_zzz(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
367 DSI *dsi = (DSI *)AFPobj->dsi;
375 memcpy(&data, ibuf, 4); /* flag */
379 * Possible sleeping states:
380 * 1) normal sleep: DSI_SLEEPING (up to 10.3)
381 * 2) extended sleep: DSI_SLEEPING | DSI_EXTSLEEP (starting with 10.4)
384 if (data & AFPZZZ_EXT_WAKEUP) {
385 /* wakeup request from exetended sleep */
386 if (dsi->flags & DSI_EXTSLEEP) {
387 LOG(log_note, logtype_afpd, "afp_zzz: waking up from extended sleep");
388 dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP);
392 dsi->flags |= DSI_SLEEPING;
393 if (data & AFPZZZ_EXT_SLEEP) {
394 LOG(log_note, logtype_afpd, "afp_zzz: entering extended sleep");
395 dsi->flags |= DSI_EXTSLEEP;
397 LOG(log_note, logtype_afpd, "afp_zzz: entering normal sleep");
402 * According to AFP 3.3 spec we should not return anything,
403 * but eg 10.5.8 server still returns the numbers of hours
404 * the server is keeping the sessino (ie max sleeptime).
406 data = obj->options.sleep / 120; /* hours */
410 *rbuflen = sizeof(data);
412 memcpy(rbuf, &data, sizeof(data));
413 rbuf += sizeof(data);
418 /* ---------------------- */
419 static int create_session_token(AFPObj *obj)
423 /* use 8 bytes for token as OSX, don't know if it helps */
424 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
425 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
429 if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
432 memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
433 obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
435 memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
440 static int create_session_key(AFPObj *obj)
442 /* create session key */
443 if (obj->sinfo.sessionkey == NULL) {
444 if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
446 uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
447 obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
453 /* ---------------------- */
456 char *ibuf, size_t ibuflen,
457 char *rbuf, size_t *rbuflen)
469 if (ibuflen < 2 + sizeof(type)) {
476 memcpy(&type, ibuf, sizeof(type));
478 ibuf += sizeof(type);
479 ibuflen -= sizeof(type);
481 if ( obj->sinfo.sessiontoken == NULL ) {
482 if ( create_session_token( obj ) )
490 case 0: /* old version ?*/
491 tklen = obj->sinfo.sessiontoken_len;
492 token = obj->sinfo.sessiontoken;
494 case 1: /* disconnect */
495 case 2: /* reconnect update id */
496 if (ibuflen >= sizeof(idlen)) {
497 memcpy(&idlen, ibuf, sizeof(idlen));
498 idlen = ntohl(idlen);
499 ibuf += sizeof(idlen);
500 ibuflen -= sizeof(idlen);
501 if (ibuflen < idlen) {
504 /* memcpy (id, ibuf, idlen) */
505 tklen = obj->sinfo.sessiontoken_len;
506 token = obj->sinfo.sessiontoken;
513 memcpy( &idlen, ibuf, sizeof(idlen));
514 idlen = ntohl(idlen);
515 ibuf += sizeof(idlen);
516 ibuflen -= sizeof(idlen);
517 ibuf += sizeof(boottime);
518 ibuflen -= sizeof(boottime);
519 if (ibuflen < idlen || idlen > (90-10)) {
522 if (!obj->sinfo.clientid) {
523 obj->sinfo.clientid = malloc(idlen + 8);
524 memcpy(obj->sinfo.clientid, p, idlen + 8);
525 obj->sinfo.clientid_len = idlen + 8;
527 if (ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p) != 0)
529 tklen = obj->sinfo.sessiontoken_len;
530 token = obj->sinfo.sessiontoken;
533 case 8: /* Panther Kerberos Token */
534 tklen = obj->sinfo.cryptedkey_len;
535 token = obj->sinfo.cryptedkey;
547 memcpy(rbuf, &tp, sizeof(tklen));
548 rbuf += sizeof(tklen);
549 *rbuflen += sizeof(tklen);
551 memcpy(rbuf, token, tklen);
557 /* ---------------------- */
558 int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
560 DSI *dsi = (DSI *)obj->dsi;
570 /* check for guest user */
571 if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
576 memcpy(&type, ibuf, sizeof(type));
578 ibuf += sizeof(type);
580 memcpy(&tklen, ibuf, sizeof(tklen));
581 tklen = ntohl(tklen);
582 ibuf += sizeof(tklen);
584 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
585 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
588 if (tklen != SESSIONTOKEN_LEN) {
591 tklen = sizeof(pid_t);
592 memcpy(&token, ibuf, tklen);
594 /* our stuff is pid + zero pad */
596 for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
602 LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
603 dsi->flags |= DSI_RECONINPROG;
605 /* Deactivate tickle timer */
606 const struct itimerval none = {{0, 0}, {0, 0}};
607 setitimer(ITIMER_REAL, &none, NULL);
609 /* check for old session, possibly transfering session from here to there */
610 if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) != 0)
612 /* write uint16_t DSI request ID */
613 if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
614 LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
617 /* now send our connected AFP client socket */
618 if (send_fd(obj->ipc_fd, dsi->socket) != 0)
620 /* Now see what happens: either afpd master sends us SIGTERM because our session */
621 /* has been transfered to a old disconnected session, or we continue */
624 if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
625 /* Reconnect succeeded, we exit now after sleeping some more */
626 sleep(2); /* sleep some more to give the recon. session time */
627 LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
632 /* Reinstall tickle timer */
633 setitimer(ITIMER_REAL, &dsi->timer, NULL);
635 LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
639 /* ---------------------- */
640 static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
644 if (!len || len > ibuflen)
645 return AFPERR_BADVERS;
647 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
648 for ( i = 0; i < num; i++ ) {
649 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
650 afp_version = afp_versions[ i ].av_number;
651 afp_version_index = i;
655 if ( i == num ) /* An inappropo version */
656 return AFPERR_BADVERS ;
658 if (afp_version >= 30 && obj->proto != AFPPROTO_DSI)
659 return AFPERR_BADVERS ;
662 if (afp_version >= 30 && sizeof(off_t) != 8) {
663 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
664 return AFPERR_BADVERS ;
670 /* ---------------------- */
671 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
673 struct passwd *pwd = NULL;
680 return send_reply(obj, AFPERR_SHUTDOWN );
683 return send_reply(obj, AFPERR_BADVERS );
686 len = (unsigned char) *ibuf++;
689 i = get_version(obj, ibuf, ibuflen, len);
691 return send_reply(obj, i );
694 return send_reply(obj, AFPERR_BADUAM);
699 len = (unsigned char) *ibuf++;
702 if (!len || len > ibuflen)
703 return send_reply(obj, AFPERR_BADUAM);
705 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
706 return send_reply(obj, AFPERR_BADUAM);
710 if (AFP_OK != (i = create_session_key(obj)) )
711 return send_reply(obj, i);
713 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
715 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
716 return send_reply(obj, i);
718 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
721 /* ---------------------- */
722 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
724 struct passwd *pwd = NULL;
734 return send_reply(obj, AFPERR_SHUTDOWN );
737 return send_reply(obj, AFPERR_BADVERS );
743 len = (unsigned char) *ibuf;
747 i = get_version(obj, ibuf, ibuflen, len);
749 return send_reply(obj, i );
752 return send_reply(obj, AFPERR_BADUAM);
757 len = (unsigned char) *ibuf;
761 if (!len || len > ibuflen)
762 return send_reply(obj, AFPERR_BADUAM);
764 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
765 return send_reply(obj, AFPERR_BADUAM);
769 if (!afp_uam->u.uam_login.login_ext) {
770 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
771 return send_reply(obj, AFPERR_BADUAM);
774 if (ibuflen <= 1 +sizeof(len16))
775 return send_reply(obj, AFPERR_PARAM);
781 return send_reply(obj, AFPERR_PARAM);
783 memcpy(&len16, ibuf, sizeof(len16));
784 ibuf += sizeof(len16);
785 ibuflen -= sizeof(len16);
788 return send_reply(obj, AFPERR_PARAM);
792 /* directory service name */
794 return send_reply(obj, AFPERR_PARAM);
803 return send_reply(obj, AFPERR_PARAM);
804 len = (unsigned char) *ibuf;
809 /* With "No User Authen" it is equal */
810 if (ibuflen < sizeof(len16))
811 return send_reply(obj, AFPERR_PARAM);
812 memcpy(&len16, ibuf, sizeof(len16));
813 ibuf += sizeof(len16);
814 ibuflen -= sizeof(len16);
818 return send_reply(obj, AFPERR_PARAM);
822 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
823 return send_reply(obj, AFPERR_PARAM);
830 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
835 if (AFP_OK != (i = create_session_key(obj)) ) {
836 return send_reply(obj, i);
839 /* FIXME user name are in UTF8 */
840 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
842 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
843 return send_reply(obj, i);
845 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
848 /* ---------------------- */
849 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
851 struct passwd *pwd = NULL;
854 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
856 return send_reply(obj, AFPERR_NOTAUTH );
859 ibuf += 2; ibuflen -= 2;
860 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
862 if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
863 return send_reply(obj, err);
865 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
869 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
871 DSI *dsi = (DSI *)(obj->dsi);
873 LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
874 of_close_all_forks();
876 dsi->flags = DSI_AFP_LOGGED_OUT;
883 /* change password --
884 * NOTE: an FPLogin must already have completed successfully for this
885 * to work. this also does a little pre-processing before it hands
888 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
890 char username[MACFILELEN + 1], *start = ibuf;
899 /* check if password change is allowed, OS-X ignores the flag.
900 * we shouldn't trust the client on this anyway.
901 * not sure about the "right" error code, NOOP for now */
902 if (!(obj->options.passwdbits & PASSWD_SET))
905 /* make sure we can deal w/ this uam */
906 len = (unsigned char) *ibuf++;
907 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
908 return AFPERR_BADUAM;
911 if ((len + 1) & 1) /* pad byte */
914 if ( afp_version < 30) {
915 len = (unsigned char) *ibuf++;
916 if ( len > sizeof(username) - 1) {
919 memcpy(username, ibuf, len);
920 username[ len ] = '\0';
922 if ((len + 1) & 1) /* pad byte */
925 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
926 if ( ibuf[0] != '\0' || ibuf[1] != '\0')
929 len = MIN(sizeof(username), strlen(obj->username));
930 memcpy(username, obj->username, len);
931 username[ len ] = '\0';
935 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
937 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
940 /* send it off to the uam. we really don't use ibuflen right now. */
941 if (ibuflen < (size_t)(ibuf - start))
944 ibuflen -= (ibuf - start);
945 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
947 LOG(log_info, logtype_afpd, "password change %s.",
948 (ret == AFPERR_AUTHCONT) ? "continued" :
949 (ret ? "failed" : "succeeded"));
958 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
965 LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
970 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
971 memcpy(&bitmap, ibuf, sizeof(bitmap));
972 bitmap = ntohs(bitmap);
974 /* deal with error cases. we don't have to worry about
975 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
979 if ((bitmap & USERIBIT_ALL) != bitmap)
980 return AFPERR_BITMAP;
982 /* remember place where we store the possibly modified bitmap later */
983 memcpy(rbuf, ibuf, sizeof(bitmap));
985 rbuf += sizeof(bitmap);
986 *rbuflen = sizeof(bitmap);
988 /* copy the user/group info */
989 if (bitmap & USERIBIT_USER) {
990 id = htonl(geteuid());
991 memcpy(rbuf, &id, sizeof(id));
993 *rbuflen += sizeof(id);
996 if (bitmap & USERIBIT_GROUP) {
997 id = htonl(getegid());
998 memcpy(rbuf, &id, sizeof(id));
1000 *rbuflen += sizeof(id);
1003 if (bitmap & USERIBIT_UUID) {
1004 if ( ! (obj->options.flags & OPTION_UUID)) {
1005 bitmap &= ~USERIBIT_UUID;
1006 bitmap = htons(bitmap);
1007 memcpy(bitmapp, &bitmap, sizeof(bitmap));
1009 LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
1012 ret = getuuidfromname( obj->username, UUID_USER, uuid);
1014 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
1015 return AFPERR_NOITEM;
1017 LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
1019 memcpy(rbuf, uuid, UUID_BINSIZE);
1020 rbuf += UUID_BINSIZE;
1021 *rbuflen += UUID_BINSIZE;
1025 LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
1029 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
1030 (((type) == UAM_SERVER_CHANGEPW) ? \
1031 &uam_changepw : NULL))
1033 /* just do a linked list search. this could be sped up with a hashed
1034 * list, but i doubt anyone's going to have enough uams to matter. */
1035 struct uam_obj *auth_uamfind(const int type, const char *name,
1038 struct uam_obj *prev, *start;
1040 if (!name || !(start = UAM_LIST(type)))
1044 while ((prev = prev->uam_prev) != start)
1045 if (strndiacasecmp(prev->uam_name, name, len) == 0)
1051 int auth_register(const int type, struct uam_obj *uam)
1053 struct uam_obj *start;
1055 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1058 if (!(start = UAM_LIST(type)))
1059 return 1; /* we don't know what to do with it, caller must free it */
1061 uam_attach(start, uam);
1065 /* load all of the modules */
1066 int auth_load(const char *path, const char *list)
1068 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
1069 struct uam_mod *mod;
1073 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1076 strlcpy(buf, list, sizeof(buf));
1077 if ((p = strtok(buf, ",")) == NULL)
1081 if (name[len - 1] != '/') {
1087 strlcpy(name + len, p, sizeof(name) - len);
1088 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1090 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1092 if (stat(name, &st) == 0) {
1093 if ((mod = uam_load(name, p))) {
1094 uam_attach(&uam_modules, mod);
1095 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1097 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1100 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1102 p = strtok(NULL, ",");
1108 /* get rid of all of the uams */
1109 void auth_unload(void)
1111 struct uam_mod *mod, *prev, *start = &uam_modules;
1113 prev = start->uam_prev;
1114 while ((mod = prev) != start) {
1115 prev = prev->uam_prev;