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/fce_api.h>
43 #include <atalk/spotlight.h>
44 #include <atalk/unix.h>
56 static int afp_version_index;
57 static struct uam_mod uam_modules = {NULL, NULL, &uam_modules, &uam_modules};
58 static struct uam_obj uam_login = {"", "", 0, {{NULL, NULL, NULL, NULL }}, &uam_login,
60 static struct uam_obj uam_changepw = {"", "", 0, {{NULL, NULL, NULL, NULL}}, &uam_changepw,
63 static struct uam_obj *afp_uam = NULL;
66 void status_versions( char *data, const DSI *dsi)
70 int len, num, i, count = 0;
72 memcpy(&status, start + AFPSTATUS_VERSOFF, sizeof(status));
73 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ] );
75 for ( i = 0; i < num; i++ ) {
76 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
79 data += ntohs( status );
82 for ( i = 0; i < num; i++ ) {
83 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
84 len = strlen( afp_versions[ i ].av_name );
86 memcpy( data, afp_versions[ i ].av_name , len );
89 status = htons( data - start );
90 memcpy(start + AFPSTATUS_UAMSOFF, &status, sizeof(status));
93 void status_uams(char *data, const char *authlist)
100 memcpy(&status, start + AFPSTATUS_UAMSOFF, sizeof(status));
102 while ((uams = uams->uam_prev) != &uam_login) {
103 if (strstr(authlist, uams->uam_path))
107 data += ntohs( status );
109 while ((uams = uams->uam_prev) != &uam_login) {
110 if (strstr(authlist, uams->uam_path)) {
111 LOG(log_info, logtype_afpd, "uam: \"%s\" available", uams->uam_name);
112 len = strlen( uams->uam_name);
114 memcpy( data, uams->uam_name, len );
120 status = htons(data - start);
121 memcpy(start + AFPSTATUS_ICONOFF, &status, sizeof(status));
124 /* handle errors by closing the connection. this is only needed
125 * by the afp_* functions. */
126 static int send_reply(const AFPObj *obj, const int err)
128 if ((err == AFP_OK) || (err == AFPERR_AUTHCONT))
131 obj->reply(obj->dsi, err);
137 static int afp_errpwdexpired(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
138 char *rbuf _U_, size_t *rbuflen)
141 return AFPERR_PWDEXPR;
144 static int afp_null_nolog(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
145 char *rbuf _U_, size_t *rbuflen)
148 return( AFPERR_NOOP );
151 static int set_auth_switch(const AFPObj *obj, int expired)
157 * BF: expired password handling
158 * to allow the user to change his/her password we have to allow login
159 * but every following call except for FPChangePassword will be thrown
160 * away with an AFPERR_PWDEXPR error. (thanks to Leland Wallace from Apple
161 * for clarifying this)
164 for (i=0; i<=0xff; i++) {
165 uam_afpserver_action(i, UAM_AFPSERVER_PREAUTH, afp_errpwdexpired, NULL);
167 uam_afpserver_action(AFP_LOGOUT, UAM_AFPSERVER_PREAUTH, afp_logout, NULL);
168 uam_afpserver_action(AFP_CHANGEPW, UAM_AFPSERVER_PREAUTH, afp_changepw, NULL);
171 afp_switch = postauth_switch;
172 switch (obj->afp_version) {
178 uam_afpserver_action(AFP_GETACL, UAM_AFPSERVER_POSTAUTH, afp_getacl, NULL);
179 uam_afpserver_action(AFP_SETACL, UAM_AFPSERVER_POSTAUTH, afp_setacl, NULL);
180 uam_afpserver_action(AFP_ACCESS, UAM_AFPSERVER_POSTAUTH, afp_access, NULL);
181 #endif /* HAVE_ACLS */
182 uam_afpserver_action(AFP_GETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_getextattr, NULL);
183 uam_afpserver_action(AFP_SETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_setextattr, NULL);
184 uam_afpserver_action(AFP_REMOVEATTR, UAM_AFPSERVER_POSTAUTH, afp_remextattr, NULL);
185 uam_afpserver_action(AFP_LISTEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_listextattr, NULL);
188 uam_afpserver_action(AFP_SYNCDIR, UAM_AFPSERVER_POSTAUTH, afp_syncdir, NULL);
189 uam_afpserver_action(AFP_SYNCFORK, UAM_AFPSERVER_POSTAUTH, afp_syncfork, NULL);
190 uam_afpserver_action(AFP_SPOTLIGHT_PRIVATE, UAM_AFPSERVER_POSTAUTH, afp_spotlight_rpc, NULL);
191 uam_afpserver_action(AFP_ENUMERATE_EXT2, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext2, NULL);
194 uam_afpserver_action(AFP_ENUMERATE_EXT, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext, NULL);
195 uam_afpserver_action(AFP_BYTELOCK_EXT, UAM_AFPSERVER_POSTAUTH, afp_bytelock_ext, NULL);
196 /* catsearch_ext uses the same packet as catsearch FIXME double check this, it wasn't true for enue
198 uam_afpserver_action(AFP_CATSEARCH_EXT, UAM_AFPSERVER_POSTAUTH, afp_catsearch_ext, NULL);
199 uam_afpserver_action(AFP_GETSESSTOKEN, UAM_AFPSERVER_POSTAUTH, afp_getsession, NULL);
200 uam_afpserver_action(AFP_READ_EXT, UAM_AFPSERVER_POSTAUTH, afp_read_ext, NULL);
201 uam_afpserver_action(AFP_WRITE_EXT, UAM_AFPSERVER_POSTAUTH, afp_write_ext, NULL);
202 uam_afpserver_action(AFP_DISCTOLDSESS, UAM_AFPSERVER_POSTAUTH, afp_disconnect, NULL);
206 * If first connection to a server is done in classic AFP2.2 version is used
207 * but OSX uses AFP3.x FPzzz command !
209 uam_afpserver_action(AFP_ZZZ, UAM_AFPSERVER_POSTAUTH, afp_zzz, NULL);
217 static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void), int expired)
221 #endif /* ADMIN_GRP */
223 if ( pwd->pw_uid == 0 ) { /* don't allow root login */
224 LOG(log_error, logtype_afpd, "login: root login denied!" );
225 return AFPERR_NOTAUTH;
228 if (obj->cnx_cnt > obj->cnx_max) {
229 LOG(log_error, logtype_dsi, "login: too many connections, limit: %d", obj->cnx_max);
230 return AFPERR_MAXSESS;
233 LOG(log_note, logtype_afpd, "Login by %s (%s)",
234 pwd->pw_name, afp_versions[afp_version_index].av_name);
236 if (set_groups(obj, pwd) != 0)
237 return AFPERR_BADUAM;
240 LOG(log_debug, logtype_afpd, "obj->options.admingid == %d", obj->options.admingid);
242 if (obj->options.admingid != 0) {
244 for (i = 0; i < obj->ngroups; i++) {
245 if (obj->groups[i] == obj->options.admingid) admin = 1;
250 LOG(log_info, logtype_afpd, "admin login -- %s", pwd->pw_name );
253 #endif /* ADMIN_GRP */
256 struct DSI *dsi = obj->handle;
263 afp_get_cmdline( &argc, &argv );
265 hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
266 sizeof( struct in_addr ),
267 dsi->client.sin_family );
270 clientname = hp->h_name;
272 clientname = inet_ntoa( dsi->client.sin_addr );
274 sprintf( hostname, "%s@%s", pwd->pw_name, clientname );
276 if( sia_become_user( NULL, argc, argv, hostname, pwd->pw_name,
277 NULL, FALSE, NULL, NULL,
278 SIA_BEU_REALLOGIN ) != SIASUCCESS )
279 return AFPERR_BADUAM;
281 LOG(log_info, logtype_afpd, "session from %s (%s)", hostname,
282 inet_ntoa( dsi->client.sin_addr ) );
284 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
285 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
286 return AFPERR_BADUAM;
290 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
291 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
292 return AFPERR_BADUAM;
296 LOG(log_debug, logtype_afpd, "login: supplementary groups: %s", print_groups(obj->ngroups, obj->groups));
298 /* There's probably a better way to do this, but for now, we just play root */
303 #endif /* ADMIN_GRP */
304 obj->uid = geteuid();
306 set_auth_switch(obj, expired);
307 /* save our euid, we need it for preexec_close */
308 obj->uid = geteuid();
309 obj->logout = logout;
311 /* pam_umask or similar might have changed our umask */
312 (void)umask(obj->options.umask);
314 /* Some PAM module might have reset our signal handlers and timer, so we need to reestablish them */
315 afp_over_dsi_sighandlers(obj);
317 /* Send FCE login event */
318 fce_register(obj, FCE_LOGIN, "", NULL);
323 /* ---------------------- */
324 int afp_zzz(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
327 DSI *dsi = (DSI *)AFPobj->dsi;
335 memcpy(&data, ibuf, 4); /* flag */
339 * Possible sleeping states:
340 * 1) normal sleep: DSI_SLEEPING (up to 10.3)
341 * 2) extended sleep: DSI_SLEEPING | DSI_EXTSLEEP (starting with 10.4)
344 if (data & AFPZZZ_EXT_WAKEUP) {
345 /* wakeup request from exetended sleep */
346 if (dsi->flags & DSI_EXTSLEEP) {
347 LOG(log_note, logtype_afpd, "afp_zzz: waking up from extended sleep");
348 dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP);
349 ipc_child_state(obj, DSI_RUNNING);
353 dsi->flags |= DSI_SLEEPING;
354 if (data & AFPZZZ_EXT_SLEEP) {
355 LOG(log_note, logtype_afpd, "afp_zzz: entering extended sleep");
356 dsi->flags |= DSI_EXTSLEEP;
357 ipc_child_state(obj, DSI_EXTSLEEP);
359 LOG(log_note, logtype_afpd, "afp_zzz: entering normal sleep");
360 ipc_child_state(obj, DSI_SLEEPING);
365 * According to AFP 3.3 spec we should not return anything,
366 * but eg 10.5.8 server still returns the numbers of hours
367 * the server is keeping the sessino (ie max sleeptime).
369 data = obj->options.sleep / 120; /* hours */
373 *rbuflen = sizeof(data);
375 memcpy(rbuf, &data, sizeof(data));
376 rbuf += sizeof(data);
381 /* ---------------------- */
382 static int create_session_token(AFPObj *obj)
386 /* use 8 bytes for token as OSX, don't know if it helps */
387 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
388 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
392 if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
395 memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
396 obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
398 memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
403 static int create_session_key(AFPObj *obj)
405 /* create session key */
406 if (obj->sinfo.sessionkey == NULL) {
407 if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
409 uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
410 obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
416 /* ---------------------- */
419 char *ibuf, size_t ibuflen,
420 char *rbuf, size_t *rbuflen)
432 if (ibuflen < 2 + sizeof(type)) {
439 memcpy(&type, ibuf, sizeof(type));
441 ibuf += sizeof(type);
442 ibuflen -= sizeof(type);
444 if ( obj->sinfo.sessiontoken == NULL ) {
445 if ( create_session_token( obj ) )
453 case 0: /* old version ?*/
454 tklen = obj->sinfo.sessiontoken_len;
455 token = obj->sinfo.sessiontoken;
457 case 1: /* disconnect */
458 case 2: /* reconnect update id */
459 if (ibuflen >= sizeof(idlen)) {
460 memcpy(&idlen, ibuf, sizeof(idlen));
461 idlen = ntohl(idlen);
462 ibuf += sizeof(idlen);
463 ibuflen -= sizeof(idlen);
464 if (ibuflen < idlen) {
467 /* memcpy (id, ibuf, idlen) */
468 tklen = obj->sinfo.sessiontoken_len;
469 token = obj->sinfo.sessiontoken;
476 memcpy( &idlen, ibuf, sizeof(idlen));
477 idlen = ntohl(idlen);
478 ibuf += sizeof(idlen);
479 ibuflen -= sizeof(idlen);
480 ibuf += sizeof(boottime);
481 ibuflen -= sizeof(boottime);
482 if (ibuflen < idlen || idlen > (90-10)) {
485 if (!obj->sinfo.clientid) {
486 obj->sinfo.clientid = malloc(idlen + 8);
487 memcpy(obj->sinfo.clientid, p, idlen + 8);
488 obj->sinfo.clientid_len = idlen + 8;
490 if (ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p) != 0)
492 tklen = obj->sinfo.sessiontoken_len;
493 token = obj->sinfo.sessiontoken;
496 case 8: /* Panther Kerberos Token */
497 tklen = obj->sinfo.cryptedkey_len;
498 token = obj->sinfo.cryptedkey;
510 memcpy(rbuf, &tp, sizeof(tklen));
511 rbuf += sizeof(tklen);
512 *rbuflen += sizeof(tklen);
514 memcpy(rbuf, token, tklen);
520 /* ---------------------- */
521 int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
523 DSI *dsi = (DSI *)obj->dsi;
533 /* check for guest user */
534 if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
539 memcpy(&type, ibuf, sizeof(type));
541 ibuf += sizeof(type);
543 memcpy(&tklen, ibuf, sizeof(tklen));
544 tklen = ntohl(tklen);
545 ibuf += sizeof(tklen);
547 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
548 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
551 if (tklen != SESSIONTOKEN_LEN) {
554 tklen = sizeof(pid_t);
555 memcpy(&token, ibuf, tklen);
557 /* our stuff is pid + zero pad */
559 for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
565 LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
566 dsi->flags |= DSI_RECONINPROG;
568 /* Deactivate tickle timer */
569 const struct itimerval none = {{0, 0}, {0, 0}};
570 setitimer(ITIMER_REAL, &none, NULL);
572 /* check for old session, possibly transfering session from here to there */
573 if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) != 0)
575 /* write uint16_t DSI request ID */
576 if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
577 LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
580 /* now send our connected AFP client socket */
581 if (send_fd(obj->ipc_fd, dsi->socket) != 0)
583 /* Now see what happens: either afpd master sends us SIGTERM because our session */
584 /* has been transfered to a old disconnected session, or we continue */
587 if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
588 /* Reconnect succeeded, we exit now after sleeping some more */
589 sleep(2); /* sleep some more to give the recon. session time */
590 LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
595 /* Reinstall tickle timer */
596 setitimer(ITIMER_REAL, &dsi->timer, NULL);
598 LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
602 /* ---------------------- */
603 static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
607 if (!len || len > ibuflen)
608 return AFPERR_BADVERS;
610 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
611 for ( i = 0; i < num; i++ ) {
612 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
613 obj->afp_version = afp_versions[ i ].av_number;
614 afp_version_index = i;
618 if ( i == num ) /* An inappropo version */
619 return AFPERR_BADVERS ;
622 if (obj->afp_version >= 30 && sizeof(off_t) != 8) {
623 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
624 return AFPERR_BADVERS ;
630 /* ---------------------- */
631 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
633 struct passwd *pwd = NULL;
640 return send_reply(obj, AFPERR_SHUTDOWN );
643 return send_reply(obj, AFPERR_BADVERS );
646 len = (unsigned char) *ibuf++;
649 i = get_version(obj, ibuf, ibuflen, len);
651 return send_reply(obj, i );
654 return send_reply(obj, AFPERR_BADUAM);
659 len = (unsigned char) *ibuf++;
662 if (!len || len > ibuflen)
663 return send_reply(obj, AFPERR_BADUAM);
665 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
666 return send_reply(obj, AFPERR_BADUAM);
670 if (AFP_OK != (i = create_session_key(obj)) )
671 return send_reply(obj, i);
673 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
675 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
676 return send_reply(obj, i);
678 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
681 /* ---------------------- */
682 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
684 struct passwd *pwd = NULL;
694 return send_reply(obj, AFPERR_SHUTDOWN );
697 return send_reply(obj, AFPERR_BADVERS );
703 len = (unsigned char) *ibuf;
707 i = get_version(obj, ibuf, ibuflen, len);
709 return send_reply(obj, i );
712 return send_reply(obj, AFPERR_BADUAM);
717 len = (unsigned char) *ibuf;
721 if (!len || len > ibuflen)
722 return send_reply(obj, AFPERR_BADUAM);
724 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
725 return send_reply(obj, AFPERR_BADUAM);
729 if (!afp_uam->u.uam_login.login_ext) {
730 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
731 return send_reply(obj, AFPERR_BADUAM);
734 if (ibuflen <= 1 +sizeof(len16))
735 return send_reply(obj, AFPERR_PARAM);
741 return send_reply(obj, AFPERR_PARAM);
743 memcpy(&len16, ibuf, sizeof(len16));
744 ibuf += sizeof(len16);
745 ibuflen -= sizeof(len16);
748 return send_reply(obj, AFPERR_PARAM);
752 /* directory service name */
754 return send_reply(obj, AFPERR_PARAM);
763 return send_reply(obj, AFPERR_PARAM);
764 len = (unsigned char) *ibuf;
769 /* With "No User Authen" it is equal */
770 if (ibuflen < sizeof(len16))
771 return send_reply(obj, AFPERR_PARAM);
772 memcpy(&len16, ibuf, sizeof(len16));
773 ibuf += sizeof(len16);
774 ibuflen -= sizeof(len16);
778 return send_reply(obj, AFPERR_PARAM);
782 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
783 return send_reply(obj, AFPERR_PARAM);
790 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
795 if (AFP_OK != (i = create_session_key(obj)) ) {
796 return send_reply(obj, i);
799 /* FIXME user name are in UTF8 */
800 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
802 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
803 return send_reply(obj, i);
805 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
808 /* ---------------------- */
809 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
811 struct passwd *pwd = NULL;
814 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
816 return send_reply(obj, AFPERR_NOTAUTH );
819 ibuf += 2; ibuflen -= 2;
820 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
822 if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
823 return send_reply(obj, err);
825 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
829 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
831 DSI *dsi = (DSI *)(obj->dsi);
833 LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
834 of_close_all_forks(obj);
836 dsi->flags = DSI_AFP_LOGGED_OUT;
839 /* Send FCE login event */
840 fce_register(obj, FCE_LOGOUT, "", NULL);
847 /* change password --
848 * NOTE: an FPLogin must already have completed successfully for this
849 * to work. this also does a little pre-processing before it hands
852 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
854 char username[MACFILELEN + 1], *start = ibuf;
863 /* check if password change is allowed, OS-X ignores the flag.
864 * we shouldn't trust the client on this anyway.
865 * not sure about the "right" error code, NOOP for now */
866 if (!(obj->options.passwdbits & PASSWD_SET))
869 /* make sure we can deal w/ this uam */
870 len = (unsigned char) *ibuf++;
871 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
872 return AFPERR_BADUAM;
875 if ((len + 1) & 1) /* pad byte */
878 if (obj->afp_version < 30) {
879 len = (unsigned char) *ibuf++;
880 if ( len > sizeof(username) - 1) {
883 memcpy(username, ibuf, len);
884 username[ len ] = '\0';
886 if ((len + 1) & 1) /* pad byte */
889 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
890 if ( ibuf[0] != '\0' || ibuf[1] != '\0')
893 len = MIN(sizeof(username) - 1, strlen(obj->username));
894 memcpy(username, obj->username, len);
895 username[ len ] = '\0';
899 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
901 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
904 /* send it off to the uam. we really don't use ibuflen right now. */
905 if (ibuflen < (size_t)(ibuf - start))
908 ibuflen -= (ibuf - start);
909 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
911 LOG(log_info, logtype_afpd, "password change %s.",
912 (ret == AFPERR_AUTHCONT) ? "continued" :
913 (ret ? "failed" : "succeeded"));
915 set_auth_switch(obj, 0);
922 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
929 LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
934 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
935 memcpy(&bitmap, ibuf, sizeof(bitmap));
936 bitmap = ntohs(bitmap);
938 /* deal with error cases. we don't have to worry about
939 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
943 if ((bitmap & USERIBIT_ALL) != bitmap)
944 return AFPERR_BITMAP;
946 /* remember place where we store the possibly modified bitmap later */
947 memcpy(rbuf, ibuf, sizeof(bitmap));
949 rbuf += sizeof(bitmap);
950 *rbuflen = sizeof(bitmap);
952 /* copy the user/group info */
953 if (bitmap & USERIBIT_USER) {
954 id = htonl(geteuid());
955 memcpy(rbuf, &id, sizeof(id));
957 *rbuflen += sizeof(id);
960 if (bitmap & USERIBIT_GROUP) {
961 id = htonl(getegid());
962 memcpy(rbuf, &id, sizeof(id));
964 *rbuflen += sizeof(id);
967 if (bitmap & USERIBIT_UUID) {
968 if ( ! (obj->options.flags & OPTION_UUID)) {
969 bitmap &= ~USERIBIT_UUID;
970 bitmap = htons(bitmap);
971 memcpy(bitmapp, &bitmap, sizeof(bitmap));
973 LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
976 ret = getuuidfromname( obj->username, UUID_USER, uuid);
978 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
979 return AFPERR_NOITEM;
981 LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
983 memcpy(rbuf, uuid, UUID_BINSIZE);
984 rbuf += UUID_BINSIZE;
985 *rbuflen += UUID_BINSIZE;
989 LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
993 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
994 (((type) == UAM_SERVER_CHANGEPW) ? \
995 &uam_changepw : NULL))
997 /* just do a linked list search. this could be sped up with a hashed
998 * list, but i doubt anyone's going to have enough uams to matter. */
999 struct uam_obj *auth_uamfind(const int type, const char *name,
1002 struct uam_obj *prev, *start;
1004 if (!name || !(start = UAM_LIST(type)))
1008 while ((prev = prev->uam_prev) != start)
1009 if (strndiacasecmp(prev->uam_name, name, len) == 0)
1015 int auth_register(const int type, struct uam_obj *uam)
1017 struct uam_obj *start;
1019 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1022 if (!(start = UAM_LIST(type)))
1023 return 1; /* we don't know what to do with it, caller must free it */
1025 uam_attach(start, uam);
1029 /* load all of the modules */
1030 int auth_load(AFPObj *obj, const char *path, const char *list)
1032 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p, *last;
1033 struct uam_mod *mod;
1037 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1040 LOG(log_debug, logtype_afpd, "auth_load: %s, %s", path, list);
1042 strlcpy(buf, list, sizeof(buf));
1043 if ((p = strtok_r(buf, ", ", &last)) == NULL)
1047 if (name[len - 1] != '/') {
1053 strlcpy(name + len, p, sizeof(name) - len);
1054 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1056 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1058 if (stat(name, &st) == 0) {
1059 if ((mod = uam_load(obj, name, p))) {
1060 uam_attach(&uam_modules, mod);
1061 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1063 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1066 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1068 p = strtok_r(NULL, ", ", &last);
1074 /* get rid of all of the uams */
1075 void auth_unload(void)
1077 struct uam_mod *mod, *prev, *start = &uam_modules;
1079 prev = start->uam_prev;
1080 while ((mod = prev) != start) {
1081 prev = prev->uam_prev;