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);
191 uam_afpserver_action(AFP_SPOTLIGHT_PRIVATE, UAM_AFPSERVER_POSTAUTH, afp_spotlight_rpc, NULL);
193 uam_afpserver_action(AFP_ENUMERATE_EXT2, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext2, NULL);
196 uam_afpserver_action(AFP_ENUMERATE_EXT, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext, NULL);
197 uam_afpserver_action(AFP_BYTELOCK_EXT, UAM_AFPSERVER_POSTAUTH, afp_bytelock_ext, NULL);
198 /* catsearch_ext uses the same packet as catsearch FIXME double check this, it wasn't true for enue
200 uam_afpserver_action(AFP_CATSEARCH_EXT, UAM_AFPSERVER_POSTAUTH, afp_catsearch_ext, NULL);
201 uam_afpserver_action(AFP_GETSESSTOKEN, UAM_AFPSERVER_POSTAUTH, afp_getsession, NULL);
202 uam_afpserver_action(AFP_READ_EXT, UAM_AFPSERVER_POSTAUTH, afp_read_ext, NULL);
203 uam_afpserver_action(AFP_WRITE_EXT, UAM_AFPSERVER_POSTAUTH, afp_write_ext, NULL);
204 uam_afpserver_action(AFP_DISCTOLDSESS, UAM_AFPSERVER_POSTAUTH, afp_disconnect, NULL);
208 * If first connection to a server is done in classic AFP2.2 version is used
209 * but OSX uses AFP3.x FPzzz command !
211 uam_afpserver_action(AFP_ZZZ, UAM_AFPSERVER_POSTAUTH, afp_zzz, NULL);
219 static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void), int expired)
223 #endif /* ADMIN_GRP */
225 if ( pwd->pw_uid == 0 ) { /* don't allow root login */
226 LOG(log_error, logtype_afpd, "login: root login denied!" );
227 return AFPERR_NOTAUTH;
230 if (obj->cnx_cnt > obj->cnx_max) {
231 LOG(log_error, logtype_dsi, "login: too many connections, limit: %d", obj->cnx_max);
232 return AFPERR_MAXSESS;
235 LOG(log_note, logtype_afpd, "Login by %s (%s)",
236 pwd->pw_name, afp_versions[afp_version_index].av_name);
238 if (set_groups(obj, pwd) != 0)
239 return AFPERR_BADUAM;
242 LOG(log_debug, logtype_afpd, "obj->options.admingid == %d", obj->options.admingid);
244 if (obj->options.admingid != 0) {
246 for (i = 0; i < obj->ngroups; i++) {
247 if (obj->groups[i] == obj->options.admingid) admin = 1;
252 LOG(log_info, logtype_afpd, "admin login -- %s", pwd->pw_name );
255 #endif /* ADMIN_GRP */
258 struct DSI *dsi = obj->handle;
265 afp_get_cmdline( &argc, &argv );
267 hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
268 sizeof( struct in_addr ),
269 dsi->client.sin_family );
272 clientname = hp->h_name;
274 clientname = inet_ntoa( dsi->client.sin_addr );
276 sprintf( hostname, "%s@%s", pwd->pw_name, clientname );
278 if( sia_become_user( NULL, argc, argv, hostname, pwd->pw_name,
279 NULL, FALSE, NULL, NULL,
280 SIA_BEU_REALLOGIN ) != SIASUCCESS )
281 return AFPERR_BADUAM;
283 LOG(log_info, logtype_afpd, "session from %s (%s)", hostname,
284 inet_ntoa( dsi->client.sin_addr ) );
286 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
287 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
288 return AFPERR_BADUAM;
292 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
293 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
294 return AFPERR_BADUAM;
298 LOG(log_debug, logtype_afpd, "login: supplementary groups: %s", print_groups(obj->ngroups, obj->groups));
300 /* There's probably a better way to do this, but for now, we just play root */
305 #endif /* ADMIN_GRP */
306 obj->uid = geteuid();
308 set_auth_switch(obj, expired);
309 /* save our euid, we need it for preexec_close */
310 obj->uid = geteuid();
311 obj->logout = logout;
313 /* pam_umask or similar might have changed our umask */
314 (void)umask(obj->options.umask);
316 /* Some PAM module might have reset our signal handlers and timer, so we need to reestablish them */
317 afp_over_dsi_sighandlers(obj);
319 /* Send FCE login event */
320 fce_register(obj, FCE_LOGIN, "", NULL);
325 /* ---------------------- */
326 int afp_zzz(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
329 DSI *dsi = (DSI *)AFPobj->dsi;
337 memcpy(&data, ibuf, 4); /* flag */
341 * Possible sleeping states:
342 * 1) normal sleep: DSI_SLEEPING (up to 10.3)
343 * 2) extended sleep: DSI_SLEEPING | DSI_EXTSLEEP (starting with 10.4)
346 if (data & AFPZZZ_EXT_WAKEUP) {
347 /* wakeup request from exetended sleep */
348 if (dsi->flags & DSI_EXTSLEEP) {
349 LOG(log_note, logtype_afpd, "afp_zzz: waking up from extended sleep");
350 dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP);
351 ipc_child_state(obj, DSI_RUNNING);
355 dsi->flags |= DSI_SLEEPING;
356 if (data & AFPZZZ_EXT_SLEEP) {
357 LOG(log_note, logtype_afpd, "afp_zzz: entering extended sleep");
358 dsi->flags |= DSI_EXTSLEEP;
359 ipc_child_state(obj, DSI_EXTSLEEP);
361 LOG(log_note, logtype_afpd, "afp_zzz: entering normal sleep");
362 ipc_child_state(obj, DSI_SLEEPING);
367 * According to AFP 3.3 spec we should not return anything,
368 * but eg 10.5.8 server still returns the numbers of hours
369 * the server is keeping the sessino (ie max sleeptime).
371 data = obj->options.sleep / 120; /* hours */
375 *rbuflen = sizeof(data);
377 memcpy(rbuf, &data, sizeof(data));
378 rbuf += sizeof(data);
383 /* ---------------------- */
384 static int create_session_token(AFPObj *obj)
388 /* use 8 bytes for token as OSX, don't know if it helps */
389 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
390 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
394 if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
397 memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
398 obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
400 memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
405 static int create_session_key(AFPObj *obj)
407 /* create session key */
408 if (obj->sinfo.sessionkey == NULL) {
409 if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
411 uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
412 obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
418 /* ---------------------- */
421 char *ibuf, size_t ibuflen,
422 char *rbuf, size_t *rbuflen)
434 if (ibuflen < 2 + sizeof(type)) {
441 memcpy(&type, ibuf, sizeof(type));
443 ibuf += sizeof(type);
444 ibuflen -= sizeof(type);
446 if ( obj->sinfo.sessiontoken == NULL ) {
447 if ( create_session_token( obj ) )
455 case 0: /* old version ?*/
456 tklen = obj->sinfo.sessiontoken_len;
457 token = obj->sinfo.sessiontoken;
459 case 1: /* disconnect */
460 case 2: /* reconnect update id */
461 if (ibuflen >= sizeof(idlen)) {
462 memcpy(&idlen, ibuf, sizeof(idlen));
463 idlen = ntohl(idlen);
464 ibuf += sizeof(idlen);
465 ibuflen -= sizeof(idlen);
466 if (ibuflen < idlen) {
469 /* memcpy (id, ibuf, idlen) */
470 tklen = obj->sinfo.sessiontoken_len;
471 token = obj->sinfo.sessiontoken;
478 memcpy( &idlen, ibuf, sizeof(idlen));
479 idlen = ntohl(idlen);
480 ibuf += sizeof(idlen);
481 ibuflen -= sizeof(idlen);
482 ibuf += sizeof(boottime);
483 ibuflen -= sizeof(boottime);
484 if (ibuflen < idlen || idlen > (90-10)) {
487 if (!obj->sinfo.clientid) {
488 obj->sinfo.clientid = malloc(idlen + 8);
489 memcpy(obj->sinfo.clientid, p, idlen + 8);
490 obj->sinfo.clientid_len = idlen + 8;
492 if (ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p) != 0)
494 tklen = obj->sinfo.sessiontoken_len;
495 token = obj->sinfo.sessiontoken;
498 case 8: /* Panther Kerberos Token */
499 tklen = obj->sinfo.cryptedkey_len;
500 token = obj->sinfo.cryptedkey;
512 memcpy(rbuf, &tp, sizeof(tklen));
513 rbuf += sizeof(tklen);
514 *rbuflen += sizeof(tklen);
516 memcpy(rbuf, token, tklen);
522 /* ---------------------- */
523 int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
525 DSI *dsi = (DSI *)obj->dsi;
535 /* check for guest user */
536 if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
541 memcpy(&type, ibuf, sizeof(type));
543 ibuf += sizeof(type);
545 memcpy(&tklen, ibuf, sizeof(tklen));
546 tklen = ntohl(tklen);
547 ibuf += sizeof(tklen);
549 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
550 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
553 if (tklen != SESSIONTOKEN_LEN) {
556 tklen = sizeof(pid_t);
557 memcpy(&token, ibuf, tklen);
559 /* our stuff is pid + zero pad */
561 for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
567 LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
568 dsi->flags |= DSI_RECONINPROG;
570 /* Deactivate tickle timer */
571 const struct itimerval none = {{0, 0}, {0, 0}};
572 setitimer(ITIMER_REAL, &none, NULL);
574 /* check for old session, possibly transfering session from here to there */
575 if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) != 0)
577 /* write uint16_t DSI request ID */
578 if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
579 LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
582 /* now send our connected AFP client socket */
583 if (send_fd(obj->ipc_fd, dsi->socket) != 0)
585 /* Now see what happens: either afpd master sends us SIGTERM because our session */
586 /* has been transfered to a old disconnected session, or we continue */
589 if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
590 /* Reconnect succeeded, we exit now after sleeping some more */
591 sleep(2); /* sleep some more to give the recon. session time */
592 LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
597 /* Reinstall tickle timer */
598 setitimer(ITIMER_REAL, &dsi->timer, NULL);
600 LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
604 /* ---------------------- */
605 static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
609 if (!len || len > ibuflen)
610 return AFPERR_BADVERS;
612 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
613 for ( i = 0; i < num; i++ ) {
614 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
615 obj->afp_version = afp_versions[ i ].av_number;
616 afp_version_index = i;
620 if ( i == num ) /* An inappropo version */
621 return AFPERR_BADVERS ;
624 if (obj->afp_version >= 30 && sizeof(off_t) != 8) {
625 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
626 return AFPERR_BADVERS ;
632 /* ---------------------- */
633 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
635 struct passwd *pwd = NULL;
642 return send_reply(obj, AFPERR_SHUTDOWN );
645 return send_reply(obj, AFPERR_BADVERS );
648 len = (unsigned char) *ibuf++;
651 i = get_version(obj, ibuf, ibuflen, len);
653 return send_reply(obj, i );
656 return send_reply(obj, AFPERR_BADUAM);
661 len = (unsigned char) *ibuf++;
664 if (!len || len > ibuflen)
665 return send_reply(obj, AFPERR_BADUAM);
667 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
668 return send_reply(obj, AFPERR_BADUAM);
672 if (AFP_OK != (i = create_session_key(obj)) )
673 return send_reply(obj, i);
675 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
677 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
678 return send_reply(obj, i);
680 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
683 /* ---------------------- */
684 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
686 struct passwd *pwd = NULL;
696 return send_reply(obj, AFPERR_SHUTDOWN );
699 return send_reply(obj, AFPERR_BADVERS );
705 len = (unsigned char) *ibuf;
709 i = get_version(obj, ibuf, ibuflen, len);
711 return send_reply(obj, i );
714 return send_reply(obj, AFPERR_BADUAM);
719 len = (unsigned char) *ibuf;
723 if (!len || len > ibuflen)
724 return send_reply(obj, AFPERR_BADUAM);
726 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
727 return send_reply(obj, AFPERR_BADUAM);
731 if (!afp_uam->u.uam_login.login_ext) {
732 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
733 return send_reply(obj, AFPERR_BADUAM);
736 if (ibuflen <= 1 +sizeof(len16))
737 return send_reply(obj, AFPERR_PARAM);
743 return send_reply(obj, AFPERR_PARAM);
745 memcpy(&len16, ibuf, sizeof(len16));
746 ibuf += sizeof(len16);
747 ibuflen -= sizeof(len16);
750 return send_reply(obj, AFPERR_PARAM);
754 /* directory service name */
756 return send_reply(obj, AFPERR_PARAM);
765 return send_reply(obj, AFPERR_PARAM);
766 len = (unsigned char) *ibuf;
771 /* With "No User Authen" it is equal */
772 if (ibuflen < sizeof(len16))
773 return send_reply(obj, AFPERR_PARAM);
774 memcpy(&len16, ibuf, sizeof(len16));
775 ibuf += sizeof(len16);
776 ibuflen -= sizeof(len16);
780 return send_reply(obj, AFPERR_PARAM);
784 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
785 return send_reply(obj, AFPERR_PARAM);
792 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
797 if (AFP_OK != (i = create_session_key(obj)) ) {
798 return send_reply(obj, i);
801 /* FIXME user name are in UTF8 */
802 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
804 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
805 return send_reply(obj, i);
807 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
810 /* ---------------------- */
811 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
813 struct passwd *pwd = NULL;
816 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
818 return send_reply(obj, AFPERR_NOTAUTH );
821 ibuf += 2; ibuflen -= 2;
822 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
824 if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
825 return send_reply(obj, err);
827 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
831 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
833 DSI *dsi = (DSI *)(obj->dsi);
835 LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
836 of_close_all_forks(obj);
838 dsi->flags = DSI_AFP_LOGGED_OUT;
841 /* Send FCE login event */
842 fce_register(obj, FCE_LOGOUT, "", NULL);
849 /* change password --
850 * NOTE: an FPLogin must already have completed successfully for this
851 * to work. this also does a little pre-processing before it hands
854 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
856 char username[MACFILELEN + 1], *start = ibuf;
865 /* check if password change is allowed, OS-X ignores the flag.
866 * we shouldn't trust the client on this anyway.
867 * not sure about the "right" error code, NOOP for now */
868 if (!(obj->options.passwdbits & PASSWD_SET))
871 /* make sure we can deal w/ this uam */
872 len = (unsigned char) *ibuf++;
873 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
874 return AFPERR_BADUAM;
877 if ((len + 1) & 1) /* pad byte */
880 if (obj->afp_version < 30) {
881 len = (unsigned char) *ibuf++;
882 if ( len > sizeof(username) - 1) {
885 memcpy(username, ibuf, len);
886 username[ len ] = '\0';
888 if ((len + 1) & 1) /* pad byte */
891 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
892 if ( ibuf[0] != '\0' || ibuf[1] != '\0')
895 len = MIN(sizeof(username) - 1, strlen(obj->username));
896 memcpy(username, obj->username, len);
897 username[ len ] = '\0';
901 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
903 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
906 /* send it off to the uam. we really don't use ibuflen right now. */
907 if (ibuflen < (size_t)(ibuf - start))
910 ibuflen -= (ibuf - start);
911 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
913 LOG(log_info, logtype_afpd, "password change %s.",
914 (ret == AFPERR_AUTHCONT) ? "continued" :
915 (ret ? "failed" : "succeeded"));
917 set_auth_switch(obj, 0);
924 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
931 LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
936 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
937 memcpy(&bitmap, ibuf, sizeof(bitmap));
938 bitmap = ntohs(bitmap);
940 /* deal with error cases. we don't have to worry about
941 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
945 if ((bitmap & USERIBIT_ALL) != bitmap)
946 return AFPERR_BITMAP;
948 /* remember place where we store the possibly modified bitmap later */
949 memcpy(rbuf, ibuf, sizeof(bitmap));
951 rbuf += sizeof(bitmap);
952 *rbuflen = sizeof(bitmap);
954 /* copy the user/group info */
955 if (bitmap & USERIBIT_USER) {
956 id = htonl(geteuid());
957 memcpy(rbuf, &id, sizeof(id));
959 *rbuflen += sizeof(id);
962 if (bitmap & USERIBIT_GROUP) {
963 id = htonl(getegid());
964 memcpy(rbuf, &id, sizeof(id));
966 *rbuflen += sizeof(id);
969 if (bitmap & USERIBIT_UUID) {
970 if ( ! (obj->options.flags & OPTION_UUID)) {
971 bitmap &= ~USERIBIT_UUID;
972 bitmap = htons(bitmap);
973 memcpy(bitmapp, &bitmap, sizeof(bitmap));
975 LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
978 ret = getuuidfromname( obj->username, UUID_USER, uuid);
980 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
981 return AFPERR_NOITEM;
983 LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
985 memcpy(rbuf, uuid, UUID_BINSIZE);
986 rbuf += UUID_BINSIZE;
987 *rbuflen += UUID_BINSIZE;
991 LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
995 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
996 (((type) == UAM_SERVER_CHANGEPW) ? \
997 &uam_changepw : NULL))
999 /* just do a linked list search. this could be sped up with a hashed
1000 * list, but i doubt anyone's going to have enough uams to matter. */
1001 struct uam_obj *auth_uamfind(const int type, const char *name,
1004 struct uam_obj *prev, *start;
1006 if (!name || !(start = UAM_LIST(type)))
1010 while ((prev = prev->uam_prev) != start)
1011 if (strndiacasecmp(prev->uam_name, name, len) == 0)
1017 int auth_register(const int type, struct uam_obj *uam)
1019 struct uam_obj *start;
1021 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1024 if (!(start = UAM_LIST(type)))
1025 return 1; /* we don't know what to do with it, caller must free it */
1027 uam_attach(start, uam);
1031 /* load all of the modules */
1032 int auth_load(AFPObj *obj, const char *path, const char *list)
1034 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p, *last;
1035 struct uam_mod *mod;
1039 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1042 LOG(log_debug, logtype_afpd, "auth_load: %s, %s", path, list);
1044 strlcpy(buf, list, sizeof(buf));
1045 if ((p = strtok_r(buf, ", ", &last)) == NULL)
1049 if (name[len - 1] != '/') {
1055 strlcpy(name + len, p, sizeof(name) - len);
1056 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1058 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1060 if (stat(name, &st) == 0) {
1061 if ((mod = uam_load(obj, name, p))) {
1062 uam_attach(&uam_modules, mod);
1063 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1065 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1068 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1070 p = strtok_r(NULL, ", ", &last);
1076 /* get rid of all of the uams */
1077 void auth_unload(void)
1079 struct uam_mod *mod, *prev, *start = &uam_modules;
1081 prev = start->uam_prev;
1082 while ((mod = prev) != start) {
1083 prev = prev->uam_prev;