2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
14 #include <sys/types.h>
15 #include <sys/param.h>
17 #include <arpa/inet.h>
19 #include <atalk/afp.h>
20 #include <atalk/compat.h>
21 #include <atalk/util.h>
31 #include <arpa/inet.h>
35 extern void afp_get_cmdline( int *ac, char ***av );
38 #include <atalk/logger.h>
39 #include <atalk/server_ipc.h>
40 #include <atalk/uuid.h>
41 #include <atalk/globals.h>
42 #include <atalk/spotlight.h>
43 #include <atalk/unix.h>
55 static int afp_version_index;
56 static struct uam_mod uam_modules = {NULL, NULL, &uam_modules, &uam_modules};
57 static struct uam_obj uam_login = {"", "", 0, {{NULL, NULL, NULL, NULL }}, &uam_login,
59 static struct uam_obj uam_changepw = {"", "", 0, {{NULL, NULL, NULL, NULL}}, &uam_changepw,
62 static struct uam_obj *afp_uam = NULL;
65 void status_versions( char *data, const DSI *dsi)
69 int len, num, i, count = 0;
71 memcpy(&status, start + AFPSTATUS_VERSOFF, sizeof(status));
72 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ] );
74 for ( i = 0; i < num; i++ ) {
75 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
78 data += ntohs( status );
81 for ( i = 0; i < num; i++ ) {
82 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
83 len = strlen( afp_versions[ i ].av_name );
85 memcpy( data, afp_versions[ i ].av_name , len );
88 status = htons( data - start );
89 memcpy(start + AFPSTATUS_UAMSOFF, &status, sizeof(status));
92 void status_uams(char *data, const char *authlist)
99 memcpy(&status, start + AFPSTATUS_UAMSOFF, sizeof(status));
101 while ((uams = uams->uam_prev) != &uam_login) {
102 if (strstr(authlist, uams->uam_path))
106 data += ntohs( status );
108 while ((uams = uams->uam_prev) != &uam_login) {
109 if (strstr(authlist, uams->uam_path)) {
110 LOG(log_info, logtype_afpd, "uam: \"%s\" available", uams->uam_name);
111 len = strlen( uams->uam_name);
113 memcpy( data, uams->uam_name, len );
119 status = htons(data - start);
120 memcpy(start + AFPSTATUS_ICONOFF, &status, sizeof(status));
123 /* handle errors by closing the connection. this is only needed
124 * by the afp_* functions. */
125 static int send_reply(const AFPObj *obj, const int err)
127 if ((err == AFP_OK) || (err == AFPERR_AUTHCONT))
130 obj->reply(obj->dsi, err);
136 static int afp_errpwdexpired(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
137 char *rbuf _U_, size_t *rbuflen)
140 return AFPERR_PWDEXPR;
143 static int afp_null_nolog(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
144 char *rbuf _U_, size_t *rbuflen)
147 return( AFPERR_NOOP );
150 static int set_auth_switch(const AFPObj *obj, int expired)
156 * BF: expired password handling
157 * to allow the user to change his/her password we have to allow login
158 * but every following call except for FPChangePassword will be thrown
159 * away with an AFPERR_PWDEXPR error. (thanks to Leland Wallace from Apple
160 * for clarifying this)
163 for (i=0; i<=0xff; i++) {
164 uam_afpserver_action(i, UAM_AFPSERVER_PREAUTH, afp_errpwdexpired, NULL);
166 uam_afpserver_action(AFP_LOGOUT, UAM_AFPSERVER_PREAUTH, afp_logout, NULL);
167 uam_afpserver_action(AFP_CHANGEPW, UAM_AFPSERVER_PREAUTH, afp_changepw, NULL);
170 afp_switch = postauth_switch;
171 switch (obj->afp_version) {
176 uam_afpserver_action(AFP_GETACL, UAM_AFPSERVER_POSTAUTH, afp_getacl, NULL);
177 uam_afpserver_action(AFP_SETACL, UAM_AFPSERVER_POSTAUTH, afp_setacl, NULL);
178 uam_afpserver_action(AFP_ACCESS, UAM_AFPSERVER_POSTAUTH, afp_access, NULL);
179 #endif /* HAVE_ACLS */
180 uam_afpserver_action(AFP_GETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_getextattr, NULL);
181 uam_afpserver_action(AFP_SETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_setextattr, NULL);
182 uam_afpserver_action(AFP_REMOVEATTR, UAM_AFPSERVER_POSTAUTH, afp_remextattr, NULL);
183 uam_afpserver_action(AFP_LISTEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_listextattr, NULL);
186 uam_afpserver_action(AFP_SYNCDIR, UAM_AFPSERVER_POSTAUTH, afp_syncdir, NULL);
187 uam_afpserver_action(AFP_SYNCFORK, UAM_AFPSERVER_POSTAUTH, afp_syncfork, NULL);
188 uam_afpserver_action(AFP_SPOTLIGHT_PRIVATE, UAM_AFPSERVER_POSTAUTH, afp_spotlight_rpc, NULL);
189 uam_afpserver_action(AFP_ENUMERATE_EXT2, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext2, NULL);
192 uam_afpserver_action(AFP_ENUMERATE_EXT, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext, NULL);
193 uam_afpserver_action(AFP_BYTELOCK_EXT, UAM_AFPSERVER_POSTAUTH, afp_bytelock_ext, NULL);
194 /* catsearch_ext uses the same packet as catsearch FIXME double check this, it wasn't true for enue
196 uam_afpserver_action(AFP_CATSEARCH_EXT, UAM_AFPSERVER_POSTAUTH, afp_catsearch_ext, NULL);
197 uam_afpserver_action(AFP_GETSESSTOKEN, UAM_AFPSERVER_POSTAUTH, afp_getsession, NULL);
198 uam_afpserver_action(AFP_READ_EXT, UAM_AFPSERVER_POSTAUTH, afp_read_ext, NULL);
199 uam_afpserver_action(AFP_WRITE_EXT, UAM_AFPSERVER_POSTAUTH, afp_write_ext, NULL);
200 uam_afpserver_action(AFP_DISCTOLDSESS, UAM_AFPSERVER_POSTAUTH, afp_disconnect, NULL);
204 * If first connection to a server is done in classic AFP2.2 version is used
205 * but OSX uses AFP3.x FPzzz command !
207 uam_afpserver_action(AFP_ZZZ, UAM_AFPSERVER_POSTAUTH, afp_zzz, NULL);
215 static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void), int expired)
219 #endif /* ADMIN_GRP */
221 if ( pwd->pw_uid == 0 ) { /* don't allow root login */
222 LOG(log_error, logtype_afpd, "login: root login denied!" );
223 return AFPERR_NOTAUTH;
226 LOG(log_note, logtype_afpd, "%s Login by %s",
227 afp_versions[afp_version_index].av_name, pwd->pw_name);
229 if (set_groups(obj, pwd) != 0)
230 return AFPERR_BADUAM;
233 LOG(log_debug, logtype_afpd, "obj->options.admingid == %d", obj->options.admingid);
235 if (obj->options.admingid != 0) {
237 for (i = 0; i < obj->ngroups; i++) {
238 if (obj->groups[i] == obj->options.admingid) admin = 1;
243 LOG(log_info, logtype_afpd, "admin login -- %s", pwd->pw_name );
246 #endif /* ADMIN_GRP */
249 struct DSI *dsi = obj->handle;
256 afp_get_cmdline( &argc, &argv );
258 hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
259 sizeof( struct in_addr ),
260 dsi->client.sin_family );
263 clientname = hp->h_name;
265 clientname = inet_ntoa( dsi->client.sin_addr );
267 sprintf( hostname, "%s@%s", pwd->pw_name, clientname );
269 if( sia_become_user( NULL, argc, argv, hostname, pwd->pw_name,
270 NULL, FALSE, NULL, NULL,
271 SIA_BEU_REALLOGIN ) != SIASUCCESS )
272 return AFPERR_BADUAM;
274 LOG(log_info, logtype_afpd, "session from %s (%s)", hostname,
275 inet_ntoa( dsi->client.sin_addr ) );
277 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
278 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
279 return AFPERR_BADUAM;
283 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
284 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
285 return AFPERR_BADUAM;
289 LOG(log_debug, logtype_afpd, "login: supplementary groups: %s", print_groups(obj->ngroups, obj->groups));
291 /* There's probably a better way to do this, but for now, we just play root */
296 #endif /* ADMIN_GRP */
297 obj->uid = geteuid();
299 set_auth_switch(obj, expired);
300 /* save our euid, we need it for preexec_close */
301 obj->uid = geteuid();
302 obj->logout = logout;
304 /* pam_umask or similar might have changed our umask */
305 (void)umask(obj->options.umask);
307 /* Some PAM module might have reset our signal handlers and timer, so we need to reestablish them */
308 afp_over_dsi_sighandlers(obj);
313 /* ---------------------- */
314 int afp_zzz(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
317 DSI *dsi = (DSI *)AFPobj->dsi;
325 memcpy(&data, ibuf, 4); /* flag */
329 * Possible sleeping states:
330 * 1) normal sleep: DSI_SLEEPING (up to 10.3)
331 * 2) extended sleep: DSI_SLEEPING | DSI_EXTSLEEP (starting with 10.4)
334 if (data & AFPZZZ_EXT_WAKEUP) {
335 /* wakeup request from exetended sleep */
336 if (dsi->flags & DSI_EXTSLEEP) {
337 LOG(log_note, logtype_afpd, "afp_zzz: waking up from extended sleep");
338 dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP);
339 ipc_child_state(obj, DSI_RUNNING);
343 dsi->flags |= DSI_SLEEPING;
344 if (data & AFPZZZ_EXT_SLEEP) {
345 LOG(log_note, logtype_afpd, "afp_zzz: entering extended sleep");
346 dsi->flags |= DSI_EXTSLEEP;
347 ipc_child_state(obj, DSI_EXTSLEEP);
349 LOG(log_note, logtype_afpd, "afp_zzz: entering normal sleep");
350 ipc_child_state(obj, DSI_SLEEPING);
355 * According to AFP 3.3 spec we should not return anything,
356 * but eg 10.5.8 server still returns the numbers of hours
357 * the server is keeping the sessino (ie max sleeptime).
359 data = obj->options.sleep / 120; /* hours */
363 *rbuflen = sizeof(data);
365 memcpy(rbuf, &data, sizeof(data));
366 rbuf += sizeof(data);
371 /* ---------------------- */
372 static int create_session_token(AFPObj *obj)
376 /* use 8 bytes for token as OSX, don't know if it helps */
377 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
378 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
382 if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
385 memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
386 obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
388 memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
393 static int create_session_key(AFPObj *obj)
395 /* create session key */
396 if (obj->sinfo.sessionkey == NULL) {
397 if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
399 uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
400 obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
406 /* ---------------------- */
409 char *ibuf, size_t ibuflen,
410 char *rbuf, size_t *rbuflen)
422 if (ibuflen < 2 + sizeof(type)) {
429 memcpy(&type, ibuf, sizeof(type));
431 ibuf += sizeof(type);
432 ibuflen -= sizeof(type);
434 if ( obj->sinfo.sessiontoken == NULL ) {
435 if ( create_session_token( obj ) )
443 case 0: /* old version ?*/
444 tklen = obj->sinfo.sessiontoken_len;
445 token = obj->sinfo.sessiontoken;
447 case 1: /* disconnect */
448 case 2: /* reconnect update id */
449 if (ibuflen >= sizeof(idlen)) {
450 memcpy(&idlen, ibuf, sizeof(idlen));
451 idlen = ntohl(idlen);
452 ibuf += sizeof(idlen);
453 ibuflen -= sizeof(idlen);
454 if (ibuflen < idlen) {
457 /* memcpy (id, ibuf, idlen) */
458 tklen = obj->sinfo.sessiontoken_len;
459 token = obj->sinfo.sessiontoken;
466 memcpy( &idlen, ibuf, sizeof(idlen));
467 idlen = ntohl(idlen);
468 ibuf += sizeof(idlen);
469 ibuflen -= sizeof(idlen);
470 ibuf += sizeof(boottime);
471 ibuflen -= sizeof(boottime);
472 if (ibuflen < idlen || idlen > (90-10)) {
475 if (!obj->sinfo.clientid) {
476 obj->sinfo.clientid = malloc(idlen + 8);
477 memcpy(obj->sinfo.clientid, p, idlen + 8);
478 obj->sinfo.clientid_len = idlen + 8;
480 if (ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p) != 0)
482 tklen = obj->sinfo.sessiontoken_len;
483 token = obj->sinfo.sessiontoken;
486 case 8: /* Panther Kerberos Token */
487 tklen = obj->sinfo.cryptedkey_len;
488 token = obj->sinfo.cryptedkey;
500 memcpy(rbuf, &tp, sizeof(tklen));
501 rbuf += sizeof(tklen);
502 *rbuflen += sizeof(tklen);
504 memcpy(rbuf, token, tklen);
510 /* ---------------------- */
511 int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
513 DSI *dsi = (DSI *)obj->dsi;
523 /* check for guest user */
524 if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
529 memcpy(&type, ibuf, sizeof(type));
531 ibuf += sizeof(type);
533 memcpy(&tklen, ibuf, sizeof(tklen));
534 tklen = ntohl(tklen);
535 ibuf += sizeof(tklen);
537 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
538 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
541 if (tklen != SESSIONTOKEN_LEN) {
544 tklen = sizeof(pid_t);
545 memcpy(&token, ibuf, tklen);
547 /* our stuff is pid + zero pad */
549 for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
555 LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
556 dsi->flags |= DSI_RECONINPROG;
558 /* Deactivate tickle timer */
559 const struct itimerval none = {{0, 0}, {0, 0}};
560 setitimer(ITIMER_REAL, &none, NULL);
562 /* check for old session, possibly transfering session from here to there */
563 if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) != 0)
565 /* write uint16_t DSI request ID */
566 if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
567 LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
570 /* now send our connected AFP client socket */
571 if (send_fd(obj->ipc_fd, dsi->socket) != 0)
573 /* Now see what happens: either afpd master sends us SIGTERM because our session */
574 /* has been transfered to a old disconnected session, or we continue */
577 if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
578 /* Reconnect succeeded, we exit now after sleeping some more */
579 sleep(2); /* sleep some more to give the recon. session time */
580 LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
585 /* Reinstall tickle timer */
586 setitimer(ITIMER_REAL, &dsi->timer, NULL);
588 LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
592 /* ---------------------- */
593 static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
597 if (!len || len > ibuflen)
598 return AFPERR_BADVERS;
600 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
601 for ( i = 0; i < num; i++ ) {
602 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
603 obj->afp_version = afp_versions[ i ].av_number;
604 afp_version_index = i;
608 if ( i == num ) /* An inappropo version */
609 return AFPERR_BADVERS ;
612 if (obj->afp_version >= 30 && sizeof(off_t) != 8) {
613 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
614 return AFPERR_BADVERS ;
620 /* ---------------------- */
621 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
623 struct passwd *pwd = NULL;
630 return send_reply(obj, AFPERR_SHUTDOWN );
633 return send_reply(obj, AFPERR_BADVERS );
636 len = (unsigned char) *ibuf++;
639 i = get_version(obj, ibuf, ibuflen, len);
641 return send_reply(obj, i );
644 return send_reply(obj, AFPERR_BADUAM);
649 len = (unsigned char) *ibuf++;
652 if (!len || len > ibuflen)
653 return send_reply(obj, AFPERR_BADUAM);
655 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
656 return send_reply(obj, AFPERR_BADUAM);
660 if (AFP_OK != (i = create_session_key(obj)) )
661 return send_reply(obj, i);
663 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
665 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
666 return send_reply(obj, i);
668 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
671 /* ---------------------- */
672 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
674 struct passwd *pwd = NULL;
684 return send_reply(obj, AFPERR_SHUTDOWN );
687 return send_reply(obj, AFPERR_BADVERS );
693 len = (unsigned char) *ibuf;
697 i = get_version(obj, ibuf, ibuflen, len);
699 return send_reply(obj, i );
702 return send_reply(obj, AFPERR_BADUAM);
707 len = (unsigned char) *ibuf;
711 if (!len || len > ibuflen)
712 return send_reply(obj, AFPERR_BADUAM);
714 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
715 return send_reply(obj, AFPERR_BADUAM);
719 if (!afp_uam->u.uam_login.login_ext) {
720 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
721 return send_reply(obj, AFPERR_BADUAM);
724 if (ibuflen <= 1 +sizeof(len16))
725 return send_reply(obj, AFPERR_PARAM);
731 return send_reply(obj, AFPERR_PARAM);
733 memcpy(&len16, ibuf, sizeof(len16));
734 ibuf += sizeof(len16);
735 ibuflen -= sizeof(len16);
738 return send_reply(obj, AFPERR_PARAM);
742 /* directory service name */
744 return send_reply(obj, AFPERR_PARAM);
753 return send_reply(obj, AFPERR_PARAM);
754 len = (unsigned char) *ibuf;
759 /* With "No User Authen" it is equal */
760 if (ibuflen < sizeof(len16))
761 return send_reply(obj, AFPERR_PARAM);
762 memcpy(&len16, ibuf, sizeof(len16));
763 ibuf += sizeof(len16);
764 ibuflen -= sizeof(len16);
768 return send_reply(obj, AFPERR_PARAM);
772 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
773 return send_reply(obj, AFPERR_PARAM);
780 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
785 if (AFP_OK != (i = create_session_key(obj)) ) {
786 return send_reply(obj, i);
789 /* FIXME user name are in UTF8 */
790 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
792 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
793 return send_reply(obj, i);
795 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
798 /* ---------------------- */
799 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
801 struct passwd *pwd = NULL;
804 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
806 return send_reply(obj, AFPERR_NOTAUTH );
809 ibuf += 2; ibuflen -= 2;
810 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
812 if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
813 return send_reply(obj, err);
815 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
819 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
821 DSI *dsi = (DSI *)(obj->dsi);
823 LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
824 of_close_all_forks(obj);
826 dsi->flags = DSI_AFP_LOGGED_OUT;
833 /* change password --
834 * NOTE: an FPLogin must already have completed successfully for this
835 * to work. this also does a little pre-processing before it hands
838 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
840 char username[MACFILELEN + 1], *start = ibuf;
849 /* check if password change is allowed, OS-X ignores the flag.
850 * we shouldn't trust the client on this anyway.
851 * not sure about the "right" error code, NOOP for now */
852 if (!(obj->options.passwdbits & PASSWD_SET))
855 /* make sure we can deal w/ this uam */
856 len = (unsigned char) *ibuf++;
857 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
858 return AFPERR_BADUAM;
861 if ((len + 1) & 1) /* pad byte */
864 if (obj->afp_version < 30) {
865 len = (unsigned char) *ibuf++;
866 if ( len > sizeof(username) - 1) {
869 memcpy(username, ibuf, len);
870 username[ len ] = '\0';
872 if ((len + 1) & 1) /* pad byte */
875 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
876 if ( ibuf[0] != '\0' || ibuf[1] != '\0')
879 len = MIN(sizeof(username) - 1, strlen(obj->username));
880 memcpy(username, obj->username, len);
881 username[ len ] = '\0';
885 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
887 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
890 /* send it off to the uam. we really don't use ibuflen right now. */
891 if (ibuflen < (size_t)(ibuf - start))
894 ibuflen -= (ibuf - start);
895 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
897 LOG(log_info, logtype_afpd, "password change %s.",
898 (ret == AFPERR_AUTHCONT) ? "continued" :
899 (ret ? "failed" : "succeeded"));
901 set_auth_switch(obj, 0);
908 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
915 LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
920 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
921 memcpy(&bitmap, ibuf, sizeof(bitmap));
922 bitmap = ntohs(bitmap);
924 /* deal with error cases. we don't have to worry about
925 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
929 if ((bitmap & USERIBIT_ALL) != bitmap)
930 return AFPERR_BITMAP;
932 /* remember place where we store the possibly modified bitmap later */
933 memcpy(rbuf, ibuf, sizeof(bitmap));
935 rbuf += sizeof(bitmap);
936 *rbuflen = sizeof(bitmap);
938 /* copy the user/group info */
939 if (bitmap & USERIBIT_USER) {
940 id = htonl(geteuid());
941 memcpy(rbuf, &id, sizeof(id));
943 *rbuflen += sizeof(id);
946 if (bitmap & USERIBIT_GROUP) {
947 id = htonl(getegid());
948 memcpy(rbuf, &id, sizeof(id));
950 *rbuflen += sizeof(id);
953 if (bitmap & USERIBIT_UUID) {
954 if ( ! (obj->options.flags & OPTION_UUID)) {
955 bitmap &= ~USERIBIT_UUID;
956 bitmap = htons(bitmap);
957 memcpy(bitmapp, &bitmap, sizeof(bitmap));
959 LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
962 ret = getuuidfromname( obj->username, UUID_USER, uuid);
964 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
965 return AFPERR_NOITEM;
967 LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
969 memcpy(rbuf, uuid, UUID_BINSIZE);
970 rbuf += UUID_BINSIZE;
971 *rbuflen += UUID_BINSIZE;
975 LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
979 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
980 (((type) == UAM_SERVER_CHANGEPW) ? \
981 &uam_changepw : NULL))
983 /* just do a linked list search. this could be sped up with a hashed
984 * list, but i doubt anyone's going to have enough uams to matter. */
985 struct uam_obj *auth_uamfind(const int type, const char *name,
988 struct uam_obj *prev, *start;
990 if (!name || !(start = UAM_LIST(type)))
994 while ((prev = prev->uam_prev) != start)
995 if (strndiacasecmp(prev->uam_name, name, len) == 0)
1001 int auth_register(const int type, struct uam_obj *uam)
1003 struct uam_obj *start;
1005 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1008 if (!(start = UAM_LIST(type)))
1009 return 1; /* we don't know what to do with it, caller must free it */
1011 uam_attach(start, uam);
1015 /* load all of the modules */
1016 int auth_load(const char *path, const char *list)
1018 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
1019 struct uam_mod *mod;
1023 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1026 strlcpy(buf, list, sizeof(buf));
1027 if ((p = strtok(buf, ", ")) == NULL)
1031 if (name[len - 1] != '/') {
1037 strlcpy(name + len, p, sizeof(name) - len);
1038 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1040 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1042 if (stat(name, &st) == 0) {
1043 if ((mod = uam_load(name, p))) {
1044 uam_attach(&uam_modules, mod);
1045 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1047 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1050 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1052 p = strtok(NULL, ", ");
1058 /* get rid of all of the uams */
1059 void auth_unload(void)
1061 struct uam_mod *mod, *prev, *start = &uam_modules;
1063 prev = start->uam_prev;
1064 while ((mod = prev) != start) {
1065 prev = prev->uam_prev;