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>
42 #include <atalk/spotlight.h>
43 #include <atalk/unix.h>
55 static int afp_version_index;
56 static struct uam_mod uam_modules = {NULL, NULL, &uam_modules, &uam_modules};
57 static struct uam_obj uam_login = {"", "", 0, {{NULL, NULL, NULL, NULL }}, &uam_login,
59 static struct uam_obj uam_changepw = {"", "", 0, {{NULL, NULL, NULL, NULL}}, &uam_changepw,
62 static struct uam_obj *afp_uam = NULL;
65 void status_versions( char *data, const DSI *dsi)
69 int len, num, i, count = 0;
71 memcpy(&status, start + AFPSTATUS_VERSOFF, sizeof(status));
72 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ] );
74 for ( i = 0; i < num; i++ ) {
75 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
78 data += ntohs( status );
81 for ( i = 0; i < num; i++ ) {
82 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
83 len = strlen( afp_versions[ i ].av_name );
85 memcpy( data, afp_versions[ i ].av_name , len );
88 status = htons( data - start );
89 memcpy(start + AFPSTATUS_UAMSOFF, &status, sizeof(status));
92 void status_uams(char *data, const char *authlist)
99 memcpy(&status, start + AFPSTATUS_UAMSOFF, sizeof(status));
101 while ((uams = uams->uam_prev) != &uam_login) {
102 if (strstr(authlist, uams->uam_path))
106 data += ntohs( status );
108 while ((uams = uams->uam_prev) != &uam_login) {
109 if (strstr(authlist, uams->uam_path)) {
110 LOG(log_info, logtype_afpd, "uam: \"%s\" available", uams->uam_name);
111 len = strlen( uams->uam_name);
113 memcpy( data, uams->uam_name, len );
119 status = htons(data - start);
120 memcpy(start + AFPSTATUS_ICONOFF, &status, sizeof(status));
123 /* handle errors by closing the connection. this is only needed
124 * by the afp_* functions. */
125 static int send_reply(const AFPObj *obj, const int err)
127 if ((err == AFP_OK) || (err == AFPERR_AUTHCONT))
130 obj->reply(obj->dsi, err);
136 static int afp_errpwdexpired(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
137 char *rbuf _U_, size_t *rbuflen)
140 return AFPERR_PWDEXPR;
143 static int afp_null_nolog(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
144 char *rbuf _U_, size_t *rbuflen)
147 return( AFPERR_NOOP );
150 static int set_auth_switch(const AFPObj *obj, int expired)
156 * BF: expired password handling
157 * to allow the user to change his/her password we have to allow login
158 * but every following call except for FPChangePassword will be thrown
159 * away with an AFPERR_PWDEXPR error. (thanks to Leland Wallace from Apple
160 * for clarifying this)
163 for (i=0; i<=0xff; i++) {
164 uam_afpserver_action(i, UAM_AFPSERVER_PREAUTH, afp_errpwdexpired, NULL);
166 uam_afpserver_action(AFP_LOGOUT, UAM_AFPSERVER_PREAUTH, afp_logout, NULL);
167 uam_afpserver_action(AFP_CHANGEPW, UAM_AFPSERVER_PREAUTH, afp_changepw, NULL);
170 afp_switch = postauth_switch;
171 switch (obj->afp_version) {
177 uam_afpserver_action(AFP_GETACL, UAM_AFPSERVER_POSTAUTH, afp_getacl, NULL);
178 uam_afpserver_action(AFP_SETACL, UAM_AFPSERVER_POSTAUTH, afp_setacl, NULL);
179 uam_afpserver_action(AFP_ACCESS, UAM_AFPSERVER_POSTAUTH, afp_access, NULL);
180 #endif /* HAVE_ACLS */
181 uam_afpserver_action(AFP_GETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_getextattr, NULL);
182 uam_afpserver_action(AFP_SETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_setextattr, NULL);
183 uam_afpserver_action(AFP_REMOVEATTR, UAM_AFPSERVER_POSTAUTH, afp_remextattr, NULL);
184 uam_afpserver_action(AFP_LISTEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_listextattr, NULL);
187 uam_afpserver_action(AFP_SYNCDIR, UAM_AFPSERVER_POSTAUTH, afp_syncdir, NULL);
188 uam_afpserver_action(AFP_SYNCFORK, UAM_AFPSERVER_POSTAUTH, afp_syncfork, NULL);
189 uam_afpserver_action(AFP_SPOTLIGHT_PRIVATE, UAM_AFPSERVER_POSTAUTH, afp_spotlight_rpc, NULL);
190 uam_afpserver_action(AFP_ENUMERATE_EXT2, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext2, NULL);
193 uam_afpserver_action(AFP_ENUMERATE_EXT, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext, NULL);
194 uam_afpserver_action(AFP_BYTELOCK_EXT, UAM_AFPSERVER_POSTAUTH, afp_bytelock_ext, NULL);
195 /* catsearch_ext uses the same packet as catsearch FIXME double check this, it wasn't true for enue
197 uam_afpserver_action(AFP_CATSEARCH_EXT, UAM_AFPSERVER_POSTAUTH, afp_catsearch_ext, NULL);
198 uam_afpserver_action(AFP_GETSESSTOKEN, UAM_AFPSERVER_POSTAUTH, afp_getsession, NULL);
199 uam_afpserver_action(AFP_READ_EXT, UAM_AFPSERVER_POSTAUTH, afp_read_ext, NULL);
200 uam_afpserver_action(AFP_WRITE_EXT, UAM_AFPSERVER_POSTAUTH, afp_write_ext, NULL);
201 uam_afpserver_action(AFP_DISCTOLDSESS, UAM_AFPSERVER_POSTAUTH, afp_disconnect, NULL);
205 * If first connection to a server is done in classic AFP2.2 version is used
206 * but OSX uses AFP3.x FPzzz command !
208 uam_afpserver_action(AFP_ZZZ, UAM_AFPSERVER_POSTAUTH, afp_zzz, NULL);
216 static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void), int expired)
220 #endif /* ADMIN_GRP */
222 if ( pwd->pw_uid == 0 ) { /* don't allow root login */
223 LOG(log_error, logtype_afpd, "login: root login denied!" );
224 return AFPERR_NOTAUTH;
227 if (obj->cnx_cnt > obj->cnx_max) {
228 LOG(log_error, logtype_dsi, "login: too many connections, limit: %d", obj->cnx_max);
229 return AFPERR_MAXSESS;
232 LOG(log_note, logtype_afpd, "Login by %s (%s)",
233 pwd->pw_name, afp_versions[afp_version_index].av_name);
235 if (set_groups(obj, pwd) != 0)
236 return AFPERR_BADUAM;
239 LOG(log_debug, logtype_afpd, "obj->options.admingid == %d", obj->options.admingid);
241 if (obj->options.admingid != 0) {
243 for (i = 0; i < obj->ngroups; i++) {
244 if (obj->groups[i] == obj->options.admingid) admin = 1;
249 LOG(log_info, logtype_afpd, "admin login -- %s", pwd->pw_name );
252 #endif /* ADMIN_GRP */
255 struct DSI *dsi = obj->handle;
262 afp_get_cmdline( &argc, &argv );
264 hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
265 sizeof( struct in_addr ),
266 dsi->client.sin_family );
269 clientname = hp->h_name;
271 clientname = inet_ntoa( dsi->client.sin_addr );
273 sprintf( hostname, "%s@%s", pwd->pw_name, clientname );
275 if( sia_become_user( NULL, argc, argv, hostname, pwd->pw_name,
276 NULL, FALSE, NULL, NULL,
277 SIA_BEU_REALLOGIN ) != SIASUCCESS )
278 return AFPERR_BADUAM;
280 LOG(log_info, logtype_afpd, "session from %s (%s)", hostname,
281 inet_ntoa( dsi->client.sin_addr ) );
283 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
284 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
285 return AFPERR_BADUAM;
289 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
290 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
291 return AFPERR_BADUAM;
295 LOG(log_debug, logtype_afpd, "login: supplementary groups: %s", print_groups(obj->ngroups, obj->groups));
297 /* There's probably a better way to do this, but for now, we just play root */
302 #endif /* ADMIN_GRP */
303 obj->uid = geteuid();
305 set_auth_switch(obj, expired);
306 /* save our euid, we need it for preexec_close */
307 obj->uid = geteuid();
308 obj->logout = logout;
310 /* pam_umask or similar might have changed our umask */
311 (void)umask(obj->options.umask);
313 /* Some PAM module might have reset our signal handlers and timer, so we need to reestablish them */
314 afp_over_dsi_sighandlers(obj);
319 /* ---------------------- */
320 int afp_zzz(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
323 DSI *dsi = (DSI *)AFPobj->dsi;
331 memcpy(&data, ibuf, 4); /* flag */
335 * Possible sleeping states:
336 * 1) normal sleep: DSI_SLEEPING (up to 10.3)
337 * 2) extended sleep: DSI_SLEEPING | DSI_EXTSLEEP (starting with 10.4)
340 if (data & AFPZZZ_EXT_WAKEUP) {
341 /* wakeup request from exetended sleep */
342 if (dsi->flags & DSI_EXTSLEEP) {
343 LOG(log_note, logtype_afpd, "afp_zzz: waking up from extended sleep");
344 dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP);
345 ipc_child_state(obj, DSI_RUNNING);
349 dsi->flags |= DSI_SLEEPING;
350 if (data & AFPZZZ_EXT_SLEEP) {
351 LOG(log_note, logtype_afpd, "afp_zzz: entering extended sleep");
352 dsi->flags |= DSI_EXTSLEEP;
353 ipc_child_state(obj, DSI_EXTSLEEP);
355 LOG(log_note, logtype_afpd, "afp_zzz: entering normal sleep");
356 ipc_child_state(obj, DSI_SLEEPING);
361 * According to AFP 3.3 spec we should not return anything,
362 * but eg 10.5.8 server still returns the numbers of hours
363 * the server is keeping the sessino (ie max sleeptime).
365 data = obj->options.sleep / 120; /* hours */
369 *rbuflen = sizeof(data);
371 memcpy(rbuf, &data, sizeof(data));
372 rbuf += sizeof(data);
377 /* ---------------------- */
378 static int create_session_token(AFPObj *obj)
382 /* use 8 bytes for token as OSX, don't know if it helps */
383 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
384 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
388 if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
391 memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
392 obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
394 memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
399 static int create_session_key(AFPObj *obj)
401 /* create session key */
402 if (obj->sinfo.sessionkey == NULL) {
403 if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
405 uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
406 obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
412 /* ---------------------- */
415 char *ibuf, size_t ibuflen,
416 char *rbuf, size_t *rbuflen)
428 if (ibuflen < 2 + sizeof(type)) {
435 memcpy(&type, ibuf, sizeof(type));
437 ibuf += sizeof(type);
438 ibuflen -= sizeof(type);
440 if ( obj->sinfo.sessiontoken == NULL ) {
441 if ( create_session_token( obj ) )
449 case 0: /* old version ?*/
450 tklen = obj->sinfo.sessiontoken_len;
451 token = obj->sinfo.sessiontoken;
453 case 1: /* disconnect */
454 case 2: /* reconnect update id */
455 if (ibuflen >= sizeof(idlen)) {
456 memcpy(&idlen, ibuf, sizeof(idlen));
457 idlen = ntohl(idlen);
458 ibuf += sizeof(idlen);
459 ibuflen -= sizeof(idlen);
460 if (ibuflen < idlen) {
463 /* memcpy (id, ibuf, idlen) */
464 tklen = obj->sinfo.sessiontoken_len;
465 token = obj->sinfo.sessiontoken;
472 memcpy( &idlen, ibuf, sizeof(idlen));
473 idlen = ntohl(idlen);
474 ibuf += sizeof(idlen);
475 ibuflen -= sizeof(idlen);
476 ibuf += sizeof(boottime);
477 ibuflen -= sizeof(boottime);
478 if (ibuflen < idlen || idlen > (90-10)) {
481 if (!obj->sinfo.clientid) {
482 obj->sinfo.clientid = malloc(idlen + 8);
483 memcpy(obj->sinfo.clientid, p, idlen + 8);
484 obj->sinfo.clientid_len = idlen + 8;
486 if (ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p) != 0)
488 tklen = obj->sinfo.sessiontoken_len;
489 token = obj->sinfo.sessiontoken;
492 case 8: /* Panther Kerberos Token */
493 tklen = obj->sinfo.cryptedkey_len;
494 token = obj->sinfo.cryptedkey;
506 memcpy(rbuf, &tp, sizeof(tklen));
507 rbuf += sizeof(tklen);
508 *rbuflen += sizeof(tklen);
510 memcpy(rbuf, token, tklen);
516 /* ---------------------- */
517 int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
519 DSI *dsi = (DSI *)obj->dsi;
529 /* check for guest user */
530 if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
535 memcpy(&type, ibuf, sizeof(type));
537 ibuf += sizeof(type);
539 memcpy(&tklen, ibuf, sizeof(tklen));
540 tklen = ntohl(tklen);
541 ibuf += sizeof(tklen);
543 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
544 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
547 if (tklen != SESSIONTOKEN_LEN) {
550 tklen = sizeof(pid_t);
551 memcpy(&token, ibuf, tklen);
553 /* our stuff is pid + zero pad */
555 for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
561 LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
562 dsi->flags |= DSI_RECONINPROG;
564 /* Deactivate tickle timer */
565 const struct itimerval none = {{0, 0}, {0, 0}};
566 setitimer(ITIMER_REAL, &none, NULL);
568 /* check for old session, possibly transfering session from here to there */
569 if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) != 0)
571 /* write uint16_t DSI request ID */
572 if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
573 LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
576 /* now send our connected AFP client socket */
577 if (send_fd(obj->ipc_fd, dsi->socket) != 0)
579 /* Now see what happens: either afpd master sends us SIGTERM because our session */
580 /* has been transfered to a old disconnected session, or we continue */
583 if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
584 /* Reconnect succeeded, we exit now after sleeping some more */
585 sleep(2); /* sleep some more to give the recon. session time */
586 LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
591 /* Reinstall tickle timer */
592 setitimer(ITIMER_REAL, &dsi->timer, NULL);
594 LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
598 /* ---------------------- */
599 static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
603 if (!len || len > ibuflen)
604 return AFPERR_BADVERS;
606 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
607 for ( i = 0; i < num; i++ ) {
608 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
609 obj->afp_version = afp_versions[ i ].av_number;
610 afp_version_index = i;
614 if ( i == num ) /* An inappropo version */
615 return AFPERR_BADVERS ;
618 if (obj->afp_version >= 30 && sizeof(off_t) != 8) {
619 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
620 return AFPERR_BADVERS ;
626 /* ---------------------- */
627 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
629 struct passwd *pwd = NULL;
636 return send_reply(obj, AFPERR_SHUTDOWN );
639 return send_reply(obj, AFPERR_BADVERS );
642 len = (unsigned char) *ibuf++;
645 i = get_version(obj, ibuf, ibuflen, len);
647 return send_reply(obj, i );
650 return send_reply(obj, AFPERR_BADUAM);
655 len = (unsigned char) *ibuf++;
658 if (!len || len > ibuflen)
659 return send_reply(obj, AFPERR_BADUAM);
661 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
662 return send_reply(obj, AFPERR_BADUAM);
666 if (AFP_OK != (i = create_session_key(obj)) )
667 return send_reply(obj, i);
669 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
671 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
672 return send_reply(obj, i);
674 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
677 /* ---------------------- */
678 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
680 struct passwd *pwd = NULL;
690 return send_reply(obj, AFPERR_SHUTDOWN );
693 return send_reply(obj, AFPERR_BADVERS );
699 len = (unsigned char) *ibuf;
703 i = get_version(obj, ibuf, ibuflen, len);
705 return send_reply(obj, i );
708 return send_reply(obj, AFPERR_BADUAM);
713 len = (unsigned char) *ibuf;
717 if (!len || len > ibuflen)
718 return send_reply(obj, AFPERR_BADUAM);
720 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
721 return send_reply(obj, AFPERR_BADUAM);
725 if (!afp_uam->u.uam_login.login_ext) {
726 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
727 return send_reply(obj, AFPERR_BADUAM);
730 if (ibuflen <= 1 +sizeof(len16))
731 return send_reply(obj, AFPERR_PARAM);
737 return send_reply(obj, AFPERR_PARAM);
739 memcpy(&len16, ibuf, sizeof(len16));
740 ibuf += sizeof(len16);
741 ibuflen -= sizeof(len16);
744 return send_reply(obj, AFPERR_PARAM);
748 /* directory service name */
750 return send_reply(obj, AFPERR_PARAM);
759 return send_reply(obj, AFPERR_PARAM);
760 len = (unsigned char) *ibuf;
765 /* With "No User Authen" it is equal */
766 if (ibuflen < sizeof(len16))
767 return send_reply(obj, AFPERR_PARAM);
768 memcpy(&len16, ibuf, sizeof(len16));
769 ibuf += sizeof(len16);
770 ibuflen -= sizeof(len16);
774 return send_reply(obj, AFPERR_PARAM);
778 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
779 return send_reply(obj, AFPERR_PARAM);
786 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
791 if (AFP_OK != (i = create_session_key(obj)) ) {
792 return send_reply(obj, i);
795 /* FIXME user name are in UTF8 */
796 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
798 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
799 return send_reply(obj, i);
801 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
804 /* ---------------------- */
805 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
807 struct passwd *pwd = NULL;
810 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
812 return send_reply(obj, AFPERR_NOTAUTH );
815 ibuf += 2; ibuflen -= 2;
816 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
818 if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
819 return send_reply(obj, err);
821 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
825 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
827 DSI *dsi = (DSI *)(obj->dsi);
829 LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
830 of_close_all_forks(obj);
832 dsi->flags = DSI_AFP_LOGGED_OUT;
839 /* change password --
840 * NOTE: an FPLogin must already have completed successfully for this
841 * to work. this also does a little pre-processing before it hands
844 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
846 char username[MACFILELEN + 1], *start = ibuf;
855 /* check if password change is allowed, OS-X ignores the flag.
856 * we shouldn't trust the client on this anyway.
857 * not sure about the "right" error code, NOOP for now */
858 if (!(obj->options.passwdbits & PASSWD_SET))
861 /* make sure we can deal w/ this uam */
862 len = (unsigned char) *ibuf++;
863 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
864 return AFPERR_BADUAM;
867 if ((len + 1) & 1) /* pad byte */
870 if (obj->afp_version < 30) {
871 len = (unsigned char) *ibuf++;
872 if ( len > sizeof(username) - 1) {
875 memcpy(username, ibuf, len);
876 username[ len ] = '\0';
878 if ((len + 1) & 1) /* pad byte */
881 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
882 if ( ibuf[0] != '\0' || ibuf[1] != '\0')
885 len = MIN(sizeof(username) - 1, strlen(obj->username));
886 memcpy(username, obj->username, len);
887 username[ len ] = '\0';
891 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
893 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
896 /* send it off to the uam. we really don't use ibuflen right now. */
897 if (ibuflen < (size_t)(ibuf - start))
900 ibuflen -= (ibuf - start);
901 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
903 LOG(log_info, logtype_afpd, "password change %s.",
904 (ret == AFPERR_AUTHCONT) ? "continued" :
905 (ret ? "failed" : "succeeded"));
907 set_auth_switch(obj, 0);
914 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
921 LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
926 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
927 memcpy(&bitmap, ibuf, sizeof(bitmap));
928 bitmap = ntohs(bitmap);
930 /* deal with error cases. we don't have to worry about
931 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
935 if ((bitmap & USERIBIT_ALL) != bitmap)
936 return AFPERR_BITMAP;
938 /* remember place where we store the possibly modified bitmap later */
939 memcpy(rbuf, ibuf, sizeof(bitmap));
941 rbuf += sizeof(bitmap);
942 *rbuflen = sizeof(bitmap);
944 /* copy the user/group info */
945 if (bitmap & USERIBIT_USER) {
946 id = htonl(geteuid());
947 memcpy(rbuf, &id, sizeof(id));
949 *rbuflen += sizeof(id);
952 if (bitmap & USERIBIT_GROUP) {
953 id = htonl(getegid());
954 memcpy(rbuf, &id, sizeof(id));
956 *rbuflen += sizeof(id);
959 if (bitmap & USERIBIT_UUID) {
960 if ( ! (obj->options.flags & OPTION_UUID)) {
961 bitmap &= ~USERIBIT_UUID;
962 bitmap = htons(bitmap);
963 memcpy(bitmapp, &bitmap, sizeof(bitmap));
965 LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
968 ret = getuuidfromname( obj->username, UUID_USER, uuid);
970 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID!");
971 return AFPERR_NOITEM;
973 LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
975 memcpy(rbuf, uuid, UUID_BINSIZE);
976 rbuf += UUID_BINSIZE;
977 *rbuflen += UUID_BINSIZE;
981 LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
985 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
986 (((type) == UAM_SERVER_CHANGEPW) ? \
987 &uam_changepw : NULL))
989 /* just do a linked list search. this could be sped up with a hashed
990 * list, but i doubt anyone's going to have enough uams to matter. */
991 struct uam_obj *auth_uamfind(const int type, const char *name,
994 struct uam_obj *prev, *start;
996 if (!name || !(start = UAM_LIST(type)))
1000 while ((prev = prev->uam_prev) != start)
1001 if (strndiacasecmp(prev->uam_name, name, len) == 0)
1007 int auth_register(const int type, struct uam_obj *uam)
1009 struct uam_obj *start;
1011 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1014 if (!(start = UAM_LIST(type)))
1015 return 1; /* we don't know what to do with it, caller must free it */
1017 uam_attach(start, uam);
1021 /* load all of the modules */
1022 int auth_load(AFPObj *obj, const char *path, const char *list)
1024 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p, *last;
1025 struct uam_mod *mod;
1029 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1032 LOG(log_debug, logtype_afpd, "auth_load: %s, %s", path, list);
1034 strlcpy(buf, list, sizeof(buf));
1035 if ((p = strtok_r(buf, ", ", &last)) == NULL)
1039 if (name[len - 1] != '/') {
1045 strlcpy(name + len, p, sizeof(name) - len);
1046 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1048 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1050 if (stat(name, &st) == 0) {
1051 if ((mod = uam_load(obj, name, p))) {
1052 uam_attach(&uam_modules, mod);
1053 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1055 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1058 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1060 p = strtok_r(NULL, ", ", &last);
1066 /* get rid of all of the uams */
1067 void auth_unload(void)
1069 struct uam_mod *mod, *prev, *start = &uam_modules;
1071 prev = start->uam_prev;
1072 while ((mod = prev) != start) {
1073 prev = prev->uam_prev;