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 _U_, char *rbuf, size_t *rbuflen)
386 DSI *dsi = (DSI *)AFPobj->handle;
392 memcpy(&data, ibuf, 4); /* flag */
396 * Possible sleeping states:
397 * 1) normal sleep: DSI_SLEEPING (up to 10.3)
398 * 2) extended sleep: DSI_SLEEPING | DSI_EXTSLEEP (starting with 10.4)
401 if (data & AFPZZZ_EXT_WAKEUP) {
402 /* wakeup request from exetended sleep */
403 if (dsi->flags & DSI_EXTSLEEP) {
404 LOG(log_debug, logtype_afpd, "afp_zzz: waking up from extended sleep");
405 dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP);
409 dsi->flags |= DSI_SLEEPING;
410 if (data & AFPZZZ_EXT_SLEEP) {
411 LOG(log_debug, logtype_afpd, "afp_zzz: entering extended sleep");
412 dsi->flags |= DSI_EXTSLEEP;
414 LOG(log_debug, logtype_afpd, "afp_zzz: entering normal sleep");
418 * According to AFP 3.3 spec we should not return anything,
419 * but eg 10.5.8 server still returns the numbers of hours
420 * the server is keeping the sessino (ie max sleeptime).
422 data = obj->options.sleep / 120; /* hours */
426 *rbuflen = sizeof(data);
428 memcpy(rbuf, &data, sizeof(data));
429 rbuf += sizeof(data);
434 /* ---------------------- */
435 static int create_session_token(AFPObj *obj)
439 /* use 8 bytes for token as OSX, don't know if it helps */
440 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
441 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
445 if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
448 memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
449 obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
451 memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
456 static int create_session_key(AFPObj *obj)
458 /* create session key */
459 if (obj->sinfo.sessionkey == NULL) {
460 if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
462 uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
463 obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
469 /* ---------------------- */
472 char *ibuf, size_t ibuflen,
473 char *rbuf, size_t *rbuflen)
485 if (ibuflen < 2 + sizeof(type)) {
492 memcpy(&type, ibuf, sizeof(type));
494 ibuf += sizeof(type);
495 ibuflen -= sizeof(type);
497 if ( obj->sinfo.sessiontoken == NULL ) {
498 if ( create_session_token( obj ) )
506 case 0: /* old version ?*/
507 tklen = obj->sinfo.sessiontoken_len;
508 token = obj->sinfo.sessiontoken;
510 case 1: /* disconnect */
511 case 2: /* reconnect update id */
512 if (ibuflen >= sizeof(idlen)) {
513 memcpy(&idlen, ibuf, sizeof(idlen));
514 idlen = ntohl(idlen);
515 ibuf += sizeof(idlen);
516 ibuflen -= sizeof(idlen);
517 if (ibuflen < idlen) {
520 /* memcpy (id, ibuf, idlen) */
521 tklen = obj->sinfo.sessiontoken_len;
522 token = obj->sinfo.sessiontoken;
529 memcpy( &idlen, ibuf, sizeof(idlen));
530 idlen = ntohl(idlen);
531 ibuf += sizeof(idlen);
532 ibuflen -= sizeof(idlen);
533 ibuf += sizeof(boottime);
534 ibuflen -= sizeof(boottime);
535 if (ibuflen < idlen || idlen > (90-10)) {
538 ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p);
539 tklen = obj->sinfo.sessiontoken_len;
540 token = obj->sinfo.sessiontoken;
543 case 8: /* Panther Kerberos Token */
544 tklen = obj->sinfo.cryptedkey_len;
545 token = obj->sinfo.cryptedkey;
557 memcpy(rbuf, &tp, sizeof(tklen));
558 rbuf += sizeof(tklen);
559 *rbuflen += sizeof(tklen);
561 memcpy(rbuf, token, tklen);
567 /* ---------------------- */
568 int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
570 DSI *dsi = (DSI *)obj->handle;
580 /* check for guest user */
581 if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
586 memcpy(&type, ibuf, sizeof(type));
588 ibuf += sizeof(type);
590 memcpy(&tklen, ibuf, sizeof(tklen));
591 tklen = ntohl(tklen);
592 ibuf += sizeof(tklen);
594 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
595 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
598 if (tklen != SESSIONTOKEN_LEN) {
601 tklen = sizeof(pid_t);
602 memcpy(&token, ibuf, tklen);
604 /* our stuff is pid + zero pad */
606 for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
612 LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
613 dsi->flags |= DSI_RECONINPROG;
615 /* Deactivate tickle timer */
616 const struct itimerval none = {{0, 0}, {0, 0}};
617 setitimer(ITIMER_REAL, &none, NULL);
619 /* check for old session, possibly transfering session from here to there */
620 if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) == -1)
622 /* write uint16_t DSI request ID */
623 if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
624 LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
627 /* now send our connected AFP client socket */
628 if (send_fd(obj->ipc_fd, dsi->socket) != 0)
630 /* Now see what happens: either afpd master sends us SIGTERM because our session */
631 /* has been transfered to a old disconnected session, or we continue */
634 if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
635 /* Reconnect succeeded, we exit now after sleeping some more */
636 sleep(2); /* sleep some more to give the recon. session time */
637 LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
642 /* Reinstall tickle timer */
643 setitimer(ITIMER_REAL, &dsi->timer, NULL);
645 LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
649 /* ---------------------- */
650 static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
654 if (!len || len > ibuflen)
655 return AFPERR_BADVERS;
657 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
658 for ( i = 0; i < num; i++ ) {
659 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
660 afp_version = afp_versions[ i ].av_number;
661 afp_version_index = i;
665 if ( i == num ) /* An inappropo version */
666 return AFPERR_BADVERS ;
668 if (afp_version >= 30 && obj->proto != AFPPROTO_DSI)
669 return AFPERR_BADVERS ;
672 if (afp_version >= 30 && sizeof(off_t) != 8) {
673 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
674 return AFPERR_BADVERS ;
680 /* ---------------------- */
681 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
683 struct passwd *pwd = NULL;
690 return send_reply(obj, AFPERR_SHUTDOWN );
693 return send_reply(obj, AFPERR_BADVERS );
696 len = (unsigned char) *ibuf++;
699 i = get_version(obj, ibuf, ibuflen, len);
701 return send_reply(obj, i );
704 return send_reply(obj, AFPERR_BADUAM);
709 len = (unsigned char) *ibuf++;
712 if (!len || len > ibuflen)
713 return send_reply(obj, AFPERR_BADUAM);
715 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
716 return send_reply(obj, AFPERR_BADUAM);
720 if (AFP_OK != (i = create_session_key(obj)) )
721 return send_reply(obj, i);
723 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
725 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
726 return send_reply(obj, i);
728 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
731 /* ---------------------- */
732 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
734 struct passwd *pwd = NULL;
744 return send_reply(obj, AFPERR_SHUTDOWN );
747 return send_reply(obj, AFPERR_BADVERS );
753 len = (unsigned char) *ibuf;
757 i = get_version(obj, ibuf, ibuflen, len);
759 return send_reply(obj, i );
762 return send_reply(obj, AFPERR_BADUAM);
767 len = (unsigned char) *ibuf;
771 if (!len || len > ibuflen)
772 return send_reply(obj, AFPERR_BADUAM);
774 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
775 return send_reply(obj, AFPERR_BADUAM);
779 if (!afp_uam->u.uam_login.login_ext) {
780 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
781 return send_reply(obj, AFPERR_BADUAM);
784 if (ibuflen <= 1 +sizeof(len16))
785 return send_reply(obj, AFPERR_PARAM);
791 return send_reply(obj, AFPERR_PARAM);
793 memcpy(&len16, ibuf, sizeof(len16));
794 ibuf += sizeof(len16);
795 ibuflen -= sizeof(len16);
798 return send_reply(obj, AFPERR_PARAM);
802 /* directory service name */
804 return send_reply(obj, AFPERR_PARAM);
813 return send_reply(obj, AFPERR_PARAM);
814 len = (unsigned char) *ibuf;
819 /* With "No User Authen" it is equal */
820 if (ibuflen < sizeof(len16))
821 return send_reply(obj, AFPERR_PARAM);
822 memcpy(&len16, ibuf, sizeof(len16));
823 ibuf += sizeof(len16);
824 ibuflen -= sizeof(len16);
828 return send_reply(obj, AFPERR_PARAM);
832 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
833 return send_reply(obj, AFPERR_PARAM);
840 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
845 if (AFP_OK != (i = create_session_key(obj)) ) {
846 return send_reply(obj, i);
849 /* FIXME user name are in UTF8 */
850 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
852 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
853 return send_reply(obj, i);
855 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
858 /* ---------------------- */
859 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
861 struct passwd *pwd = NULL;
864 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
866 return send_reply(obj, AFPERR_NOTAUTH );
869 ibuf += 2; ibuflen -= 2;
870 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
872 if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
873 return send_reply(obj, err);
875 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
879 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen _U_)
881 LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
889 /* change password --
890 * NOTE: an FPLogin must already have completed successfully for this
891 * to work. this also does a little pre-processing before it hands
894 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
896 char username[MACFILELEN + 1], *start = ibuf;
905 /* check if password change is allowed, OS-X ignores the flag.
906 * we shouldn't trust the client on this anyway.
907 * not sure about the "right" error code, NOOP for now */
908 if (!(obj->options.passwdbits & PASSWD_SET))
911 /* make sure we can deal w/ this uam */
912 len = (unsigned char) *ibuf++;
913 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
914 return AFPERR_BADUAM;
917 if ((len + 1) & 1) /* pad byte */
920 if ( afp_version < 30) {
921 len = (unsigned char) *ibuf++;
922 if ( len > sizeof(username) - 1) {
925 memcpy(username, ibuf, len);
926 username[ len ] = '\0';
928 if ((len + 1) & 1) /* pad byte */
931 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
932 if ( ibuf[0] != '\0' || ibuf[1] != '\0')
935 len = MIN(sizeof(username), strlen(obj->username));
936 memcpy(username, obj->username, len);
937 username[ len ] = '\0';
941 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
943 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
946 /* send it off to the uam. we really don't use ibuflen right now. */
947 if (ibuflen < (size_t)(ibuf - start))
950 ibuflen -= (ibuf - start);
951 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
953 LOG(log_info, logtype_afpd, "password change %s.",
954 (ret == AFPERR_AUTHCONT) ? "continued" :
955 (ret ? "failed" : "succeeded"));
964 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
971 LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
976 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
977 memcpy(&bitmap, ibuf, sizeof(bitmap));
978 bitmap = ntohs(bitmap);
980 /* deal with error cases. we don't have to worry about
981 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
985 if ((bitmap & USERIBIT_ALL) != bitmap)
986 return AFPERR_BITMAP;
988 /* remember place where we store the possibly modified bitmap later */
989 memcpy(rbuf, ibuf, sizeof(bitmap));
991 rbuf += sizeof(bitmap);
992 *rbuflen = sizeof(bitmap);
994 /* copy the user/group info */
995 if (bitmap & USERIBIT_USER) {
996 id = htonl(geteuid());
997 memcpy(rbuf, &id, sizeof(id));
999 *rbuflen += sizeof(id);
1002 if (bitmap & USERIBIT_GROUP) {
1003 id = htonl(getegid());
1004 memcpy(rbuf, &id, sizeof(id));
1006 *rbuflen += sizeof(id);
1009 if (bitmap & USERIBIT_UUID) {
1010 if ( ! (obj->options.flags & OPTION_UUID)) {
1011 bitmap &= ~USERIBIT_UUID;
1012 bitmap = htons(bitmap);
1013 memcpy(bitmapp, &bitmap, sizeof(bitmap));
1015 LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
1018 ret = getuuidfromname( obj->username, UUID_USER, uuid);
1020 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
1021 return AFPERR_NOITEM;
1023 LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
1025 memcpy(rbuf, uuid, UUID_BINSIZE);
1026 rbuf += UUID_BINSIZE;
1027 *rbuflen += UUID_BINSIZE;
1031 LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
1035 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
1036 (((type) == UAM_SERVER_CHANGEPW) ? \
1037 &uam_changepw : NULL))
1039 /* just do a linked list search. this could be sped up with a hashed
1040 * list, but i doubt anyone's going to have enough uams to matter. */
1041 struct uam_obj *auth_uamfind(const int type, const char *name,
1044 struct uam_obj *prev, *start;
1046 if (!name || !(start = UAM_LIST(type)))
1050 while ((prev = prev->uam_prev) != start)
1051 if (strndiacasecmp(prev->uam_name, name, len) == 0)
1057 int auth_register(const int type, struct uam_obj *uam)
1059 struct uam_obj *start;
1061 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1064 if (!(start = UAM_LIST(type)))
1065 return 1; /* we don't know what to do with it, caller must free it */
1067 uam_attach(start, uam);
1071 /* load all of the modules */
1072 int auth_load(const char *path, const char *list)
1074 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
1075 struct uam_mod *mod;
1079 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1082 strlcpy(buf, list, sizeof(buf));
1083 if ((p = strtok(buf, ",")) == NULL)
1087 if (name[len - 1] != '/') {
1093 strlcpy(name + len, p, sizeof(name) - len);
1094 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1096 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1098 if (stat(name, &st) == 0) {
1099 if ((mod = uam_load(name, p))) {
1100 uam_attach(&uam_modules, mod);
1101 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1103 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1106 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1108 p = strtok(NULL, ",");
1114 /* get rid of all of the uams */
1115 void auth_unload(void)
1117 struct uam_mod *mod, *prev, *start = &uam_modules;
1119 prev = start->uam_prev;
1120 while ((mod = prev) != start) {
1121 prev = prev->uam_prev;