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/unix.h>
54 static int afp_version_index;
55 static struct uam_mod uam_modules = {NULL, NULL, &uam_modules, &uam_modules};
56 static struct uam_obj uam_login = {"", "", 0, {{NULL, NULL, NULL, NULL }}, &uam_login,
58 static struct uam_obj uam_changepw = {"", "", 0, {{NULL, NULL, NULL, NULL}}, &uam_changepw,
61 static struct uam_obj *afp_uam = NULL;
64 void status_versions( char *data, const DSI *dsi)
68 int len, num, i, count = 0;
70 memcpy(&status, start + AFPSTATUS_VERSOFF, sizeof(status));
71 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ] );
73 for ( i = 0; i < num; i++ ) {
74 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
77 data += ntohs( status );
80 for ( i = 0; i < num; i++ ) {
81 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
82 len = strlen( afp_versions[ i ].av_name );
84 memcpy( data, afp_versions[ i ].av_name , len );
87 status = htons( data - start );
88 memcpy(start + AFPSTATUS_UAMSOFF, &status, sizeof(status));
91 void status_uams(char *data, const char *authlist)
98 memcpy(&status, start + AFPSTATUS_UAMSOFF, sizeof(status));
100 while ((uams = uams->uam_prev) != &uam_login) {
101 if (strstr(authlist, uams->uam_path))
105 data += ntohs( status );
107 while ((uams = uams->uam_prev) != &uam_login) {
108 if (strstr(authlist, uams->uam_path)) {
109 LOG(log_info, logtype_afpd, "uam: \"%s\" available", uams->uam_name);
110 len = strlen( uams->uam_name);
112 memcpy( data, uams->uam_name, len );
118 status = htons(data - start);
119 memcpy(start + AFPSTATUS_ICONOFF, &status, sizeof(status));
122 /* handle errors by closing the connection. this is only needed
123 * by the afp_* functions. */
124 static int send_reply(const AFPObj *obj, const int err)
126 if ((err == AFP_OK) || (err == AFPERR_AUTHCONT))
129 obj->reply(obj->dsi, err);
135 static int afp_errpwdexpired(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
136 char *rbuf _U_, size_t *rbuflen)
139 return AFPERR_PWDEXPR;
142 static int afp_null_nolog(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
143 char *rbuf _U_, size_t *rbuflen)
146 return( AFPERR_NOOP );
149 static int set_auth_switch(const AFPObj *obj, int expired)
155 * BF: expired password handling
156 * to allow the user to change his/her password we have to allow login
157 * but every following call except for FPChangePassword will be thrown
158 * away with an AFPERR_PWDEXPR error. (thanks to Leland Wallace from Apple
159 * for clarifying this)
162 for (i=0; i<=0xff; i++) {
163 uam_afpserver_action(i, UAM_AFPSERVER_PREAUTH, afp_errpwdexpired, NULL);
165 uam_afpserver_action(AFP_LOGOUT, UAM_AFPSERVER_PREAUTH, afp_logout, NULL);
166 uam_afpserver_action(AFP_CHANGEPW, UAM_AFPSERVER_PREAUTH, afp_changepw, NULL);
169 afp_switch = postauth_switch;
170 switch (obj->afp_version) {
175 uam_afpserver_action(AFP_GETACL, UAM_AFPSERVER_POSTAUTH, afp_getacl, NULL);
176 uam_afpserver_action(AFP_SETACL, UAM_AFPSERVER_POSTAUTH, afp_setacl, NULL);
177 uam_afpserver_action(AFP_ACCESS, UAM_AFPSERVER_POSTAUTH, afp_access, NULL);
178 #endif /* HAVE_ACLS */
179 uam_afpserver_action(AFP_GETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_getextattr, NULL);
180 uam_afpserver_action(AFP_SETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_setextattr, NULL);
181 uam_afpserver_action(AFP_REMOVEATTR, UAM_AFPSERVER_POSTAUTH, afp_remextattr, NULL);
182 uam_afpserver_action(AFP_LISTEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_listextattr, NULL);
185 uam_afpserver_action(AFP_SYNCDIR, UAM_AFPSERVER_POSTAUTH, afp_syncdir, NULL);
186 uam_afpserver_action(AFP_SYNCFORK, UAM_AFPSERVER_POSTAUTH, afp_syncfork, NULL);
187 uam_afpserver_action(AFP_SPOTLIGHT_PRIVATE, UAM_AFPSERVER_POSTAUTH, afp_null_nolog, NULL);
188 uam_afpserver_action(AFP_ENUMERATE_EXT2, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext2, NULL);
191 uam_afpserver_action(AFP_ENUMERATE_EXT, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext, NULL);
192 uam_afpserver_action(AFP_BYTELOCK_EXT, UAM_AFPSERVER_POSTAUTH, afp_bytelock_ext, NULL);
193 /* catsearch_ext uses the same packet as catsearch FIXME double check this, it wasn't true for enue
195 uam_afpserver_action(AFP_CATSEARCH_EXT, UAM_AFPSERVER_POSTAUTH, afp_catsearch_ext, NULL);
196 uam_afpserver_action(AFP_GETSESSTOKEN, UAM_AFPSERVER_POSTAUTH, afp_getsession, NULL);
197 uam_afpserver_action(AFP_READ_EXT, UAM_AFPSERVER_POSTAUTH, afp_read_ext, NULL);
198 uam_afpserver_action(AFP_WRITE_EXT, UAM_AFPSERVER_POSTAUTH, afp_write_ext, NULL);
199 uam_afpserver_action(AFP_DISCTOLDSESS, UAM_AFPSERVER_POSTAUTH, afp_disconnect, NULL);
203 * If first connection to a server is done in classic AFP2.2 version is used
204 * but OSX uses AFP3.x FPzzz command !
206 uam_afpserver_action(AFP_ZZZ, UAM_AFPSERVER_POSTAUTH, afp_zzz, NULL);
214 static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void), int expired)
218 #endif /* ADMIN_GRP */
220 if ( pwd->pw_uid == 0 ) { /* don't allow root login */
221 LOG(log_error, logtype_afpd, "login: root login denied!" );
222 return AFPERR_NOTAUTH;
225 if (obj->cnx_cnt >= obj->cnx_max) {
226 LOG(log_error, logtype_dsi, "login: too many connections, limit: %d", obj->cnx_max);
227 return AFPERR_MAXSESS;
230 LOG(log_note, logtype_afpd, "%s Login by %s",
231 afp_versions[afp_version_index].av_name, pwd->pw_name);
233 if (set_groups(obj, pwd) != 0)
234 return AFPERR_BADUAM;
237 LOG(log_debug, logtype_afpd, "obj->options.admingid == %d", obj->options.admingid);
239 if (obj->options.admingid != 0) {
241 for (i = 0; i < obj->ngroups; i++) {
242 if (obj->groups[i] == obj->options.admingid) admin = 1;
247 LOG(log_info, logtype_afpd, "admin login -- %s", pwd->pw_name );
250 #endif /* ADMIN_GRP */
253 struct DSI *dsi = obj->handle;
260 afp_get_cmdline( &argc, &argv );
262 hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
263 sizeof( struct in_addr ),
264 dsi->client.sin_family );
267 clientname = hp->h_name;
269 clientname = inet_ntoa( dsi->client.sin_addr );
271 sprintf( hostname, "%s@%s", pwd->pw_name, clientname );
273 if( sia_become_user( NULL, argc, argv, hostname, pwd->pw_name,
274 NULL, FALSE, NULL, NULL,
275 SIA_BEU_REALLOGIN ) != SIASUCCESS )
276 return AFPERR_BADUAM;
278 LOG(log_info, logtype_afpd, "session from %s (%s)", hostname,
279 inet_ntoa( dsi->client.sin_addr ) );
281 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
282 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
283 return AFPERR_BADUAM;
287 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
288 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
289 return AFPERR_BADUAM;
293 LOG(log_debug, logtype_afpd, "login: supplementary groups: %s", print_groups(obj->ngroups, obj->groups));
295 /* There's probably a better way to do this, but for now, we just play root */
300 #endif /* ADMIN_GRP */
301 obj->uid = geteuid();
303 set_auth_switch(obj, expired);
304 /* save our euid, we need it for preexec_close */
305 obj->uid = geteuid();
306 obj->logout = logout;
308 /* pam_umask or similar might have changed our umask */
309 (void)umask(obj->options.umask);
311 /* Some PAM module might have reset our signal handlers and timer, so we need to reestablish them */
312 afp_over_dsi_sighandlers(obj);
317 /* ---------------------- */
318 int afp_zzz(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
321 DSI *dsi = (DSI *)AFPobj->dsi;
329 memcpy(&data, ibuf, 4); /* flag */
333 * Possible sleeping states:
334 * 1) normal sleep: DSI_SLEEPING (up to 10.3)
335 * 2) extended sleep: DSI_SLEEPING | DSI_EXTSLEEP (starting with 10.4)
338 if (data & AFPZZZ_EXT_WAKEUP) {
339 /* wakeup request from exetended sleep */
340 if (dsi->flags & DSI_EXTSLEEP) {
341 LOG(log_note, logtype_afpd, "afp_zzz: waking up from extended sleep");
342 dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP);
343 ipc_child_state(obj, DSI_RUNNING);
347 dsi->flags |= DSI_SLEEPING;
348 if (data & AFPZZZ_EXT_SLEEP) {
349 LOG(log_note, logtype_afpd, "afp_zzz: entering extended sleep");
350 dsi->flags |= DSI_EXTSLEEP;
351 ipc_child_state(obj, DSI_EXTSLEEP);
353 LOG(log_note, logtype_afpd, "afp_zzz: entering normal sleep");
354 ipc_child_state(obj, DSI_SLEEPING);
359 * According to AFP 3.3 spec we should not return anything,
360 * but eg 10.5.8 server still returns the numbers of hours
361 * the server is keeping the sessino (ie max sleeptime).
363 data = obj->options.sleep / 120; /* hours */
367 *rbuflen = sizeof(data);
369 memcpy(rbuf, &data, sizeof(data));
370 rbuf += sizeof(data);
375 /* ---------------------- */
376 static int create_session_token(AFPObj *obj)
380 /* use 8 bytes for token as OSX, don't know if it helps */
381 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
382 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
386 if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
389 memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
390 obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
392 memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
397 static int create_session_key(AFPObj *obj)
399 /* create session key */
400 if (obj->sinfo.sessionkey == NULL) {
401 if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
403 uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
404 obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
410 /* ---------------------- */
413 char *ibuf, size_t ibuflen,
414 char *rbuf, size_t *rbuflen)
426 if (ibuflen < 2 + sizeof(type)) {
433 memcpy(&type, ibuf, sizeof(type));
435 ibuf += sizeof(type);
436 ibuflen -= sizeof(type);
438 if ( obj->sinfo.sessiontoken == NULL ) {
439 if ( create_session_token( obj ) )
447 case 0: /* old version ?*/
448 tklen = obj->sinfo.sessiontoken_len;
449 token = obj->sinfo.sessiontoken;
451 case 1: /* disconnect */
452 case 2: /* reconnect update id */
453 if (ibuflen >= sizeof(idlen)) {
454 memcpy(&idlen, ibuf, sizeof(idlen));
455 idlen = ntohl(idlen);
456 ibuf += sizeof(idlen);
457 ibuflen -= sizeof(idlen);
458 if (ibuflen < idlen) {
461 /* memcpy (id, ibuf, idlen) */
462 tklen = obj->sinfo.sessiontoken_len;
463 token = obj->sinfo.sessiontoken;
470 memcpy( &idlen, ibuf, sizeof(idlen));
471 idlen = ntohl(idlen);
472 ibuf += sizeof(idlen);
473 ibuflen -= sizeof(idlen);
474 ibuf += sizeof(boottime);
475 ibuflen -= sizeof(boottime);
476 if (ibuflen < idlen || idlen > (90-10)) {
479 if (!obj->sinfo.clientid) {
480 obj->sinfo.clientid = malloc(idlen + 8);
481 memcpy(obj->sinfo.clientid, p, idlen + 8);
482 obj->sinfo.clientid_len = idlen + 8;
484 if (ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p) != 0)
486 tklen = obj->sinfo.sessiontoken_len;
487 token = obj->sinfo.sessiontoken;
490 case 8: /* Panther Kerberos Token */
491 tklen = obj->sinfo.cryptedkey_len;
492 token = obj->sinfo.cryptedkey;
504 memcpy(rbuf, &tp, sizeof(tklen));
505 rbuf += sizeof(tklen);
506 *rbuflen += sizeof(tklen);
508 memcpy(rbuf, token, tklen);
514 /* ---------------------- */
515 int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
517 DSI *dsi = (DSI *)obj->dsi;
527 /* check for guest user */
528 if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
533 memcpy(&type, ibuf, sizeof(type));
535 ibuf += sizeof(type);
537 memcpy(&tklen, ibuf, sizeof(tklen));
538 tklen = ntohl(tklen);
539 ibuf += sizeof(tklen);
541 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
542 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
545 if (tklen != SESSIONTOKEN_LEN) {
548 tklen = sizeof(pid_t);
549 memcpy(&token, ibuf, tklen);
551 /* our stuff is pid + zero pad */
553 for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
559 LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
560 dsi->flags |= DSI_RECONINPROG;
562 /* Deactivate tickle timer */
563 const struct itimerval none = {{0, 0}, {0, 0}};
564 setitimer(ITIMER_REAL, &none, NULL);
566 /* check for old session, possibly transfering session from here to there */
567 if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) != 0)
569 /* write uint16_t DSI request ID */
570 if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
571 LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
574 /* now send our connected AFP client socket */
575 if (send_fd(obj->ipc_fd, dsi->socket) != 0)
577 /* Now see what happens: either afpd master sends us SIGTERM because our session */
578 /* has been transfered to a old disconnected session, or we continue */
581 if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
582 /* Reconnect succeeded, we exit now after sleeping some more */
583 sleep(2); /* sleep some more to give the recon. session time */
584 LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
589 /* Reinstall tickle timer */
590 setitimer(ITIMER_REAL, &dsi->timer, NULL);
592 LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
596 /* ---------------------- */
597 static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
601 if (!len || len > ibuflen)
602 return AFPERR_BADVERS;
604 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
605 for ( i = 0; i < num; i++ ) {
606 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
607 obj->afp_version = afp_versions[ i ].av_number;
608 afp_version_index = i;
612 if ( i == num ) /* An inappropo version */
613 return AFPERR_BADVERS ;
616 if (obj->afp_version >= 30 && sizeof(off_t) != 8) {
617 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
618 return AFPERR_BADVERS ;
624 /* ---------------------- */
625 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
627 struct passwd *pwd = NULL;
634 return send_reply(obj, AFPERR_SHUTDOWN );
637 return send_reply(obj, AFPERR_BADVERS );
640 len = (unsigned char) *ibuf++;
643 i = get_version(obj, ibuf, ibuflen, len);
645 return send_reply(obj, i );
648 return send_reply(obj, AFPERR_BADUAM);
653 len = (unsigned char) *ibuf++;
656 if (!len || len > ibuflen)
657 return send_reply(obj, AFPERR_BADUAM);
659 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
660 return send_reply(obj, AFPERR_BADUAM);
664 if (AFP_OK != (i = create_session_key(obj)) )
665 return send_reply(obj, i);
667 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
669 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
670 return send_reply(obj, i);
672 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
675 /* ---------------------- */
676 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
678 struct passwd *pwd = NULL;
688 return send_reply(obj, AFPERR_SHUTDOWN );
691 return send_reply(obj, AFPERR_BADVERS );
697 len = (unsigned char) *ibuf;
701 i = get_version(obj, ibuf, ibuflen, len);
703 return send_reply(obj, i );
706 return send_reply(obj, AFPERR_BADUAM);
711 len = (unsigned char) *ibuf;
715 if (!len || len > ibuflen)
716 return send_reply(obj, AFPERR_BADUAM);
718 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
719 return send_reply(obj, AFPERR_BADUAM);
723 if (!afp_uam->u.uam_login.login_ext) {
724 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
725 return send_reply(obj, AFPERR_BADUAM);
728 if (ibuflen <= 1 +sizeof(len16))
729 return send_reply(obj, AFPERR_PARAM);
735 return send_reply(obj, AFPERR_PARAM);
737 memcpy(&len16, ibuf, sizeof(len16));
738 ibuf += sizeof(len16);
739 ibuflen -= sizeof(len16);
742 return send_reply(obj, AFPERR_PARAM);
746 /* directory service name */
748 return send_reply(obj, AFPERR_PARAM);
757 return send_reply(obj, AFPERR_PARAM);
758 len = (unsigned char) *ibuf;
763 /* With "No User Authen" it is equal */
764 if (ibuflen < sizeof(len16))
765 return send_reply(obj, AFPERR_PARAM);
766 memcpy(&len16, ibuf, sizeof(len16));
767 ibuf += sizeof(len16);
768 ibuflen -= sizeof(len16);
772 return send_reply(obj, AFPERR_PARAM);
776 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
777 return send_reply(obj, AFPERR_PARAM);
784 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
789 if (AFP_OK != (i = create_session_key(obj)) ) {
790 return send_reply(obj, i);
793 /* FIXME user name are in UTF8 */
794 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
796 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
797 return send_reply(obj, i);
799 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
802 /* ---------------------- */
803 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
805 struct passwd *pwd = NULL;
808 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
810 return send_reply(obj, AFPERR_NOTAUTH );
813 ibuf += 2; ibuflen -= 2;
814 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
816 if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
817 return send_reply(obj, err);
819 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
823 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
825 DSI *dsi = (DSI *)(obj->dsi);
827 LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
828 of_close_all_forks(obj);
830 dsi->flags = DSI_AFP_LOGGED_OUT;
837 /* change password --
838 * NOTE: an FPLogin must already have completed successfully for this
839 * to work. this also does a little pre-processing before it hands
842 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
844 char username[MACFILELEN + 1], *start = ibuf;
853 /* check if password change is allowed, OS-X ignores the flag.
854 * we shouldn't trust the client on this anyway.
855 * not sure about the "right" error code, NOOP for now */
856 if (!(obj->options.passwdbits & PASSWD_SET))
859 /* make sure we can deal w/ this uam */
860 len = (unsigned char) *ibuf++;
861 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
862 return AFPERR_BADUAM;
865 if ((len + 1) & 1) /* pad byte */
868 if (obj->afp_version < 30) {
869 len = (unsigned char) *ibuf++;
870 if ( len > sizeof(username) - 1) {
873 memcpy(username, ibuf, len);
874 username[ len ] = '\0';
876 if ((len + 1) & 1) /* pad byte */
879 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
880 if ( ibuf[0] != '\0' || ibuf[1] != '\0')
883 len = MIN(sizeof(username) - 1, strlen(obj->username));
884 memcpy(username, obj->username, len);
885 username[ len ] = '\0';
889 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
891 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
894 /* send it off to the uam. we really don't use ibuflen right now. */
895 if (ibuflen < (size_t)(ibuf - start))
898 ibuflen -= (ibuf - start);
899 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
901 LOG(log_info, logtype_afpd, "password change %s.",
902 (ret == AFPERR_AUTHCONT) ? "continued" :
903 (ret ? "failed" : "succeeded"));
905 set_auth_switch(obj, 0);
912 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
919 LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
924 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
925 memcpy(&bitmap, ibuf, sizeof(bitmap));
926 bitmap = ntohs(bitmap);
928 /* deal with error cases. we don't have to worry about
929 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
933 if ((bitmap & USERIBIT_ALL) != bitmap)
934 return AFPERR_BITMAP;
936 /* remember place where we store the possibly modified bitmap later */
937 memcpy(rbuf, ibuf, sizeof(bitmap));
939 rbuf += sizeof(bitmap);
940 *rbuflen = sizeof(bitmap);
942 /* copy the user/group info */
943 if (bitmap & USERIBIT_USER) {
944 id = htonl(geteuid());
945 memcpy(rbuf, &id, sizeof(id));
947 *rbuflen += sizeof(id);
950 if (bitmap & USERIBIT_GROUP) {
951 id = htonl(getegid());
952 memcpy(rbuf, &id, sizeof(id));
954 *rbuflen += sizeof(id);
957 if (bitmap & USERIBIT_UUID) {
958 if ( ! (obj->options.flags & OPTION_UUID)) {
959 bitmap &= ~USERIBIT_UUID;
960 bitmap = htons(bitmap);
961 memcpy(bitmapp, &bitmap, sizeof(bitmap));
963 LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
966 ret = getuuidfromname( obj->username, UUID_USER, uuid);
968 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
969 return AFPERR_NOITEM;
971 LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
973 memcpy(rbuf, uuid, UUID_BINSIZE);
974 rbuf += UUID_BINSIZE;
975 *rbuflen += UUID_BINSIZE;
979 LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
983 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
984 (((type) == UAM_SERVER_CHANGEPW) ? \
985 &uam_changepw : NULL))
987 /* just do a linked list search. this could be sped up with a hashed
988 * list, but i doubt anyone's going to have enough uams to matter. */
989 struct uam_obj *auth_uamfind(const int type, const char *name,
992 struct uam_obj *prev, *start;
994 if (!name || !(start = UAM_LIST(type)))
998 while ((prev = prev->uam_prev) != start)
999 if (strndiacasecmp(prev->uam_name, name, len) == 0)
1005 int auth_register(const int type, struct uam_obj *uam)
1007 struct uam_obj *start;
1009 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1012 if (!(start = UAM_LIST(type)))
1013 return 1; /* we don't know what to do with it, caller must free it */
1015 uam_attach(start, uam);
1019 /* load all of the modules */
1020 int auth_load(const char *path, const char *list)
1022 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
1023 struct uam_mod *mod;
1027 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1030 strlcpy(buf, list, sizeof(buf));
1031 if ((p = strtok(buf, ", ")) == NULL)
1035 if (name[len - 1] != '/') {
1041 strlcpy(name + len, p, sizeof(name) - len);
1042 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1044 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1046 if (stat(name, &st) == 0) {
1047 if ((mod = uam_load(name, p))) {
1048 uam_attach(&uam_modules, mod);
1049 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1051 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1054 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1056 p = strtok(NULL, ", ");
1062 /* get rid of all of the uams */
1063 void auth_unload(void)
1065 struct uam_mod *mod, *prev, *start = &uam_modules;
1067 prev = start->uam_prev;
1068 while ((mod = prev) != start) {
1069 prev = prev->uam_prev;