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>
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_spotlight_rpc, 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 #define GROUPSTR_BUFSIZE 1024
215 static const char *print_groups(int ngroups, gid_t *groups)
217 static char groupsstr[GROUPSTR_BUFSIZE];
224 for (i = 0; (i < ngroups) && (s < &groupsstr[GROUPSTR_BUFSIZE]); i++) {
225 s += snprintf(s, &groupsstr[GROUPSTR_BUFSIZE] - s, " %u", groups[i]);
231 static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void), int expired)
235 #endif /* ADMIN_GRP */
237 if ( pwd->pw_uid == 0 ) { /* don't allow root login */
238 LOG(log_error, logtype_afpd, "login: root login denied!" );
239 return AFPERR_NOTAUTH;
242 LOG(log_note, logtype_afpd, "%s Login by %s",
243 afp_versions[afp_version_index].av_name, pwd->pw_name);
245 if (initgroups( pwd->pw_name, pwd->pw_gid ) < 0) {
247 LOG(log_info, logtype_afpd, "running with uid %d", geteuid());
248 #else /* RUN_AS_USER */
249 LOG(log_error, logtype_afpd, "login: %s", strerror(errno));
250 return AFPERR_BADUAM;
251 #endif /* RUN_AS_USER */
255 /* Basically if the user is in the admin group, we stay root */
257 if ((obj->ngroups = getgroups( 0, NULL )) < 0 ) {
258 LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
259 return AFPERR_BADUAM;
262 if ( NULL == (obj->groups = calloc(obj->ngroups, sizeof(gid_t))) ) {
263 LOG(log_error, logtype_afpd, "login: %s calloc: %d", obj->ngroups);
264 return AFPERR_BADUAM;
267 if (( obj->ngroups = getgroups(obj->ngroups, obj->groups )) < 0 ) {
268 LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
269 return AFPERR_BADUAM;
273 LOG(log_debug, logtype_afpd, "obj->options.admingid == %d", obj->options.admingid);
275 if (obj->options.admingid != 0) {
277 for (i = 0; i < obj->ngroups; i++) {
278 if (obj->groups[i] == obj->options.admingid) admin = 1;
283 LOG(log_info, logtype_afpd, "admin login -- %s", pwd->pw_name );
286 #endif /* ADMIN_GRP */
289 struct DSI *dsi = obj->handle;
296 afp_get_cmdline( &argc, &argv );
298 hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
299 sizeof( struct in_addr ),
300 dsi->client.sin_family );
303 clientname = hp->h_name;
305 clientname = inet_ntoa( dsi->client.sin_addr );
307 sprintf( hostname, "%s@%s", pwd->pw_name, clientname );
309 if( sia_become_user( NULL, argc, argv, hostname, pwd->pw_name,
310 NULL, FALSE, NULL, NULL,
311 SIA_BEU_REALLOGIN ) != SIASUCCESS )
312 return AFPERR_BADUAM;
314 LOG(log_info, logtype_afpd, "session from %s (%s)", hostname,
315 inet_ntoa( dsi->client.sin_addr ) );
317 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
318 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
319 return AFPERR_BADUAM;
323 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
324 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
325 return AFPERR_BADUAM;
329 LOG(log_debug, logtype_afpd, "login: supplementary groups: %s", print_groups(obj->ngroups, obj->groups));
331 /* There's probably a better way to do this, but for now, we just play root */
336 #endif /* ADMIN_GRP */
337 obj->uid = geteuid();
339 set_auth_switch(obj, expired);
340 /* save our euid, we need it for preexec_close */
341 obj->uid = geteuid();
342 obj->logout = logout;
344 /* pam_umask or similar might have changed our umask */
345 (void)umask(obj->options.umask);
347 /* Some PAM module might have reset our signal handlers and timer, so we need to reestablish them */
348 afp_over_dsi_sighandlers(obj);
353 /* ---------------------- */
354 int afp_zzz(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
357 DSI *dsi = (DSI *)AFPobj->dsi;
365 memcpy(&data, ibuf, 4); /* flag */
369 * Possible sleeping states:
370 * 1) normal sleep: DSI_SLEEPING (up to 10.3)
371 * 2) extended sleep: DSI_SLEEPING | DSI_EXTSLEEP (starting with 10.4)
374 if (data & AFPZZZ_EXT_WAKEUP) {
375 /* wakeup request from exetended sleep */
376 if (dsi->flags & DSI_EXTSLEEP) {
377 LOG(log_note, logtype_afpd, "afp_zzz: waking up from extended sleep");
378 dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP);
382 dsi->flags |= DSI_SLEEPING;
383 if (data & AFPZZZ_EXT_SLEEP) {
384 LOG(log_note, logtype_afpd, "afp_zzz: entering extended sleep");
385 dsi->flags |= DSI_EXTSLEEP;
387 LOG(log_note, logtype_afpd, "afp_zzz: entering normal sleep");
392 * According to AFP 3.3 spec we should not return anything,
393 * but eg 10.5.8 server still returns the numbers of hours
394 * the server is keeping the sessino (ie max sleeptime).
396 data = obj->options.sleep / 120; /* hours */
400 *rbuflen = sizeof(data);
402 memcpy(rbuf, &data, sizeof(data));
403 rbuf += sizeof(data);
408 /* ---------------------- */
409 static int create_session_token(AFPObj *obj)
413 /* use 8 bytes for token as OSX, don't know if it helps */
414 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
415 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
419 if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
422 memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
423 obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
425 memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
430 static int create_session_key(AFPObj *obj)
432 /* create session key */
433 if (obj->sinfo.sessionkey == NULL) {
434 if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
436 uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
437 obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
443 /* ---------------------- */
446 char *ibuf, size_t ibuflen,
447 char *rbuf, size_t *rbuflen)
459 if (ibuflen < 2 + sizeof(type)) {
466 memcpy(&type, ibuf, sizeof(type));
468 ibuf += sizeof(type);
469 ibuflen -= sizeof(type);
471 if ( obj->sinfo.sessiontoken == NULL ) {
472 if ( create_session_token( obj ) )
480 case 0: /* old version ?*/
481 tklen = obj->sinfo.sessiontoken_len;
482 token = obj->sinfo.sessiontoken;
484 case 1: /* disconnect */
485 case 2: /* reconnect update id */
486 if (ibuflen >= sizeof(idlen)) {
487 memcpy(&idlen, ibuf, sizeof(idlen));
488 idlen = ntohl(idlen);
489 ibuf += sizeof(idlen);
490 ibuflen -= sizeof(idlen);
491 if (ibuflen < idlen) {
494 /* memcpy (id, ibuf, idlen) */
495 tklen = obj->sinfo.sessiontoken_len;
496 token = obj->sinfo.sessiontoken;
503 memcpy( &idlen, ibuf, sizeof(idlen));
504 idlen = ntohl(idlen);
505 ibuf += sizeof(idlen);
506 ibuflen -= sizeof(idlen);
507 ibuf += sizeof(boottime);
508 ibuflen -= sizeof(boottime);
509 if (ibuflen < idlen || idlen > (90-10)) {
512 if (!obj->sinfo.clientid) {
513 obj->sinfo.clientid = malloc(idlen + 8);
514 memcpy(obj->sinfo.clientid, p, idlen + 8);
515 obj->sinfo.clientid_len = idlen + 8;
517 if (ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p) != 0)
519 tklen = obj->sinfo.sessiontoken_len;
520 token = obj->sinfo.sessiontoken;
523 case 8: /* Panther Kerberos Token */
524 tklen = obj->sinfo.cryptedkey_len;
525 token = obj->sinfo.cryptedkey;
537 memcpy(rbuf, &tp, sizeof(tklen));
538 rbuf += sizeof(tklen);
539 *rbuflen += sizeof(tklen);
541 memcpy(rbuf, token, tklen);
547 /* ---------------------- */
548 int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
550 DSI *dsi = (DSI *)obj->dsi;
560 /* check for guest user */
561 if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
566 memcpy(&type, ibuf, sizeof(type));
568 ibuf += sizeof(type);
570 memcpy(&tklen, ibuf, sizeof(tklen));
571 tklen = ntohl(tklen);
572 ibuf += sizeof(tklen);
574 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
575 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
578 if (tklen != SESSIONTOKEN_LEN) {
581 tklen = sizeof(pid_t);
582 memcpy(&token, ibuf, tklen);
584 /* our stuff is pid + zero pad */
586 for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
592 LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
593 dsi->flags |= DSI_RECONINPROG;
595 /* Deactivate tickle timer */
596 const struct itimerval none = {{0, 0}, {0, 0}};
597 setitimer(ITIMER_REAL, &none, NULL);
599 /* check for old session, possibly transfering session from here to there */
600 if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) != 0)
602 /* write uint16_t DSI request ID */
603 if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
604 LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
607 /* now send our connected AFP client socket */
608 if (send_fd(obj->ipc_fd, dsi->socket) != 0)
610 /* Now see what happens: either afpd master sends us SIGTERM because our session */
611 /* has been transfered to a old disconnected session, or we continue */
614 if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
615 /* Reconnect succeeded, we exit now after sleeping some more */
616 sleep(2); /* sleep some more to give the recon. session time */
617 LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
622 /* Reinstall tickle timer */
623 setitimer(ITIMER_REAL, &dsi->timer, NULL);
625 LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
629 /* ---------------------- */
630 static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
634 if (!len || len > ibuflen)
635 return AFPERR_BADVERS;
637 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
638 for ( i = 0; i < num; i++ ) {
639 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
640 obj->afp_version = afp_versions[ i ].av_number;
641 afp_version_index = i;
645 if ( i == num ) /* An inappropo version */
646 return AFPERR_BADVERS ;
649 if (obj->afp_version >= 30 && sizeof(off_t) != 8) {
650 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
651 return AFPERR_BADVERS ;
657 /* ---------------------- */
658 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
660 struct passwd *pwd = NULL;
667 return send_reply(obj, AFPERR_SHUTDOWN );
670 return send_reply(obj, AFPERR_BADVERS );
673 len = (unsigned char) *ibuf++;
676 i = get_version(obj, ibuf, ibuflen, len);
678 return send_reply(obj, i );
681 return send_reply(obj, AFPERR_BADUAM);
686 len = (unsigned char) *ibuf++;
689 if (!len || len > ibuflen)
690 return send_reply(obj, AFPERR_BADUAM);
692 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
693 return send_reply(obj, AFPERR_BADUAM);
697 if (AFP_OK != (i = create_session_key(obj)) )
698 return send_reply(obj, i);
700 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
702 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
703 return send_reply(obj, i);
705 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
708 /* ---------------------- */
709 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
711 struct passwd *pwd = NULL;
721 return send_reply(obj, AFPERR_SHUTDOWN );
724 return send_reply(obj, AFPERR_BADVERS );
730 len = (unsigned char) *ibuf;
734 i = get_version(obj, ibuf, ibuflen, len);
736 return send_reply(obj, i );
739 return send_reply(obj, AFPERR_BADUAM);
744 len = (unsigned char) *ibuf;
748 if (!len || len > ibuflen)
749 return send_reply(obj, AFPERR_BADUAM);
751 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
752 return send_reply(obj, AFPERR_BADUAM);
756 if (!afp_uam->u.uam_login.login_ext) {
757 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
758 return send_reply(obj, AFPERR_BADUAM);
761 if (ibuflen <= 1 +sizeof(len16))
762 return send_reply(obj, AFPERR_PARAM);
768 return send_reply(obj, AFPERR_PARAM);
770 memcpy(&len16, ibuf, sizeof(len16));
771 ibuf += sizeof(len16);
772 ibuflen -= sizeof(len16);
775 return send_reply(obj, AFPERR_PARAM);
779 /* directory service name */
781 return send_reply(obj, AFPERR_PARAM);
790 return send_reply(obj, AFPERR_PARAM);
791 len = (unsigned char) *ibuf;
796 /* With "No User Authen" it is equal */
797 if (ibuflen < sizeof(len16))
798 return send_reply(obj, AFPERR_PARAM);
799 memcpy(&len16, ibuf, sizeof(len16));
800 ibuf += sizeof(len16);
801 ibuflen -= sizeof(len16);
805 return send_reply(obj, AFPERR_PARAM);
809 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
810 return send_reply(obj, AFPERR_PARAM);
817 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
822 if (AFP_OK != (i = create_session_key(obj)) ) {
823 return send_reply(obj, i);
826 /* FIXME user name are in UTF8 */
827 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
829 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
830 return send_reply(obj, i);
832 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
835 /* ---------------------- */
836 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
838 struct passwd *pwd = NULL;
841 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
843 return send_reply(obj, AFPERR_NOTAUTH );
846 ibuf += 2; ibuflen -= 2;
847 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
849 if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
850 return send_reply(obj, err);
852 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
856 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
858 DSI *dsi = (DSI *)(obj->dsi);
860 LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
861 of_close_all_forks(obj);
863 dsi->flags = DSI_AFP_LOGGED_OUT;
870 /* change password --
871 * NOTE: an FPLogin must already have completed successfully for this
872 * to work. this also does a little pre-processing before it hands
875 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
877 char username[MACFILELEN + 1], *start = ibuf;
886 /* check if password change is allowed, OS-X ignores the flag.
887 * we shouldn't trust the client on this anyway.
888 * not sure about the "right" error code, NOOP for now */
889 if (!(obj->options.passwdbits & PASSWD_SET))
892 /* make sure we can deal w/ this uam */
893 len = (unsigned char) *ibuf++;
894 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
895 return AFPERR_BADUAM;
898 if ((len + 1) & 1) /* pad byte */
901 if (obj->afp_version < 30) {
902 len = (unsigned char) *ibuf++;
903 if ( len > sizeof(username) - 1) {
906 memcpy(username, ibuf, len);
907 username[ len ] = '\0';
909 if ((len + 1) & 1) /* pad byte */
912 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
913 if ( ibuf[0] != '\0' || ibuf[1] != '\0')
916 len = MIN(sizeof(username), strlen(obj->username));
917 memcpy(username, obj->username, len);
918 username[ len ] = '\0';
922 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
924 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
927 /* send it off to the uam. we really don't use ibuflen right now. */
928 if (ibuflen < (size_t)(ibuf - start))
931 ibuflen -= (ibuf - start);
932 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
934 LOG(log_info, logtype_afpd, "password change %s.",
935 (ret == AFPERR_AUTHCONT) ? "continued" :
936 (ret ? "failed" : "succeeded"));
938 set_auth_switch(obj, 0);
945 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
952 LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
957 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
958 memcpy(&bitmap, ibuf, sizeof(bitmap));
959 bitmap = ntohs(bitmap);
961 /* deal with error cases. we don't have to worry about
962 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
966 if ((bitmap & USERIBIT_ALL) != bitmap)
967 return AFPERR_BITMAP;
969 /* remember place where we store the possibly modified bitmap later */
970 memcpy(rbuf, ibuf, sizeof(bitmap));
972 rbuf += sizeof(bitmap);
973 *rbuflen = sizeof(bitmap);
975 /* copy the user/group info */
976 if (bitmap & USERIBIT_USER) {
977 id = htonl(geteuid());
978 memcpy(rbuf, &id, sizeof(id));
980 *rbuflen += sizeof(id);
983 if (bitmap & USERIBIT_GROUP) {
984 id = htonl(getegid());
985 memcpy(rbuf, &id, sizeof(id));
987 *rbuflen += sizeof(id);
990 if (bitmap & USERIBIT_UUID) {
991 if ( ! (obj->options.flags & OPTION_UUID)) {
992 bitmap &= ~USERIBIT_UUID;
993 bitmap = htons(bitmap);
994 memcpy(bitmapp, &bitmap, sizeof(bitmap));
996 LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
999 ret = getuuidfromname( obj->username, UUID_USER, uuid);
1001 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
1002 return AFPERR_NOITEM;
1004 LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
1006 memcpy(rbuf, uuid, UUID_BINSIZE);
1007 rbuf += UUID_BINSIZE;
1008 *rbuflen += UUID_BINSIZE;
1012 LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
1016 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
1017 (((type) == UAM_SERVER_CHANGEPW) ? \
1018 &uam_changepw : NULL))
1020 /* just do a linked list search. this could be sped up with a hashed
1021 * list, but i doubt anyone's going to have enough uams to matter. */
1022 struct uam_obj *auth_uamfind(const int type, const char *name,
1025 struct uam_obj *prev, *start;
1027 if (!name || !(start = UAM_LIST(type)))
1031 while ((prev = prev->uam_prev) != start)
1032 if (strndiacasecmp(prev->uam_name, name, len) == 0)
1038 int auth_register(const int type, struct uam_obj *uam)
1040 struct uam_obj *start;
1042 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1045 if (!(start = UAM_LIST(type)))
1046 return 1; /* we don't know what to do with it, caller must free it */
1048 uam_attach(start, uam);
1052 /* load all of the modules */
1053 int auth_load(const char *path, const char *list)
1055 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
1056 struct uam_mod *mod;
1060 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1063 strlcpy(buf, list, sizeof(buf));
1064 if ((p = strtok(buf, ", ")) == NULL)
1068 if (name[len - 1] != '/') {
1074 strlcpy(name + len, p, sizeof(name) - len);
1075 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1077 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1079 if (stat(name, &st) == 0) {
1080 if ((mod = uam_load(name, p))) {
1081 uam_attach(&uam_modules, mod);
1082 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1084 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1087 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1089 p = strtok(NULL, ", ");
1095 /* get rid of all of the uams */
1096 void auth_unload(void)
1098 struct uam_mod *mod, *prev, *start = &uam_modules;
1100 prev = start->uam_prev;
1101 while ((mod = prev) != start) {
1102 prev = prev->uam_prev;