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 LOG(log_note, logtype_afpd, "%s Login by %s",
226 afp_versions[afp_version_index].av_name, pwd->pw_name);
228 if (set_groups(obj, pwd) != 0)
229 return AFPERR_BADUAM;
232 LOG(log_debug, logtype_afpd, "obj->options.admingid == %d", obj->options.admingid);
234 if (obj->options.admingid != 0) {
236 for (i = 0; i < obj->ngroups; i++) {
237 if (obj->groups[i] == obj->options.admingid) admin = 1;
242 LOG(log_info, logtype_afpd, "admin login -- %s", pwd->pw_name );
245 #endif /* ADMIN_GRP */
248 struct DSI *dsi = obj->handle;
255 afp_get_cmdline( &argc, &argv );
257 hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
258 sizeof( struct in_addr ),
259 dsi->client.sin_family );
262 clientname = hp->h_name;
264 clientname = inet_ntoa( dsi->client.sin_addr );
266 sprintf( hostname, "%s@%s", pwd->pw_name, clientname );
268 if( sia_become_user( NULL, argc, argv, hostname, pwd->pw_name,
269 NULL, FALSE, NULL, NULL,
270 SIA_BEU_REALLOGIN ) != SIASUCCESS )
271 return AFPERR_BADUAM;
273 LOG(log_info, logtype_afpd, "session from %s (%s)", hostname,
274 inet_ntoa( dsi->client.sin_addr ) );
276 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
277 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
278 return AFPERR_BADUAM;
282 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
283 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
284 return AFPERR_BADUAM;
288 LOG(log_debug, logtype_afpd, "login: supplementary groups: %s", print_groups(obj->ngroups, obj->groups));
290 /* There's probably a better way to do this, but for now, we just play root */
295 #endif /* ADMIN_GRP */
296 obj->uid = geteuid();
298 set_auth_switch(obj, expired);
299 /* save our euid, we need it for preexec_close */
300 obj->uid = geteuid();
301 obj->logout = logout;
303 /* pam_umask or similar might have changed our umask */
304 (void)umask(obj->options.umask);
306 /* Some PAM module might have reset our signal handlers and timer, so we need to reestablish them */
307 afp_over_dsi_sighandlers(obj);
312 /* ---------------------- */
313 int afp_zzz(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
316 DSI *dsi = (DSI *)AFPobj->dsi;
324 memcpy(&data, ibuf, 4); /* flag */
328 * Possible sleeping states:
329 * 1) normal sleep: DSI_SLEEPING (up to 10.3)
330 * 2) extended sleep: DSI_SLEEPING | DSI_EXTSLEEP (starting with 10.4)
333 if (data & AFPZZZ_EXT_WAKEUP) {
334 /* wakeup request from exetended sleep */
335 if (dsi->flags & DSI_EXTSLEEP) {
336 LOG(log_note, logtype_afpd, "afp_zzz: waking up from extended sleep");
337 dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP);
338 ipc_child_state(obj, DSI_RUNNING);
342 dsi->flags |= DSI_SLEEPING;
343 if (data & AFPZZZ_EXT_SLEEP) {
344 LOG(log_note, logtype_afpd, "afp_zzz: entering extended sleep");
345 dsi->flags |= DSI_EXTSLEEP;
346 ipc_child_state(obj, DSI_EXTSLEEP);
348 LOG(log_note, logtype_afpd, "afp_zzz: entering normal sleep");
349 ipc_child_state(obj, DSI_SLEEPING);
354 * According to AFP 3.3 spec we should not return anything,
355 * but eg 10.5.8 server still returns the numbers of hours
356 * the server is keeping the sessino (ie max sleeptime).
358 data = obj->options.sleep / 120; /* hours */
362 *rbuflen = sizeof(data);
364 memcpy(rbuf, &data, sizeof(data));
365 rbuf += sizeof(data);
370 /* ---------------------- */
371 static int create_session_token(AFPObj *obj)
375 /* use 8 bytes for token as OSX, don't know if it helps */
376 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
377 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
381 if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
384 memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
385 obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
387 memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
392 static int create_session_key(AFPObj *obj)
394 /* create session key */
395 if (obj->sinfo.sessionkey == NULL) {
396 if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
398 uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
399 obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
405 /* ---------------------- */
408 char *ibuf, size_t ibuflen,
409 char *rbuf, size_t *rbuflen)
421 if (ibuflen < 2 + sizeof(type)) {
428 memcpy(&type, ibuf, sizeof(type));
430 ibuf += sizeof(type);
431 ibuflen -= sizeof(type);
433 if ( obj->sinfo.sessiontoken == NULL ) {
434 if ( create_session_token( obj ) )
442 case 0: /* old version ?*/
443 tklen = obj->sinfo.sessiontoken_len;
444 token = obj->sinfo.sessiontoken;
446 case 1: /* disconnect */
447 case 2: /* reconnect update id */
448 if (ibuflen >= sizeof(idlen)) {
449 memcpy(&idlen, ibuf, sizeof(idlen));
450 idlen = ntohl(idlen);
451 ibuf += sizeof(idlen);
452 ibuflen -= sizeof(idlen);
453 if (ibuflen < idlen) {
456 /* memcpy (id, ibuf, idlen) */
457 tklen = obj->sinfo.sessiontoken_len;
458 token = obj->sinfo.sessiontoken;
465 memcpy( &idlen, ibuf, sizeof(idlen));
466 idlen = ntohl(idlen);
467 ibuf += sizeof(idlen);
468 ibuflen -= sizeof(idlen);
469 ibuf += sizeof(boottime);
470 ibuflen -= sizeof(boottime);
471 if (ibuflen < idlen || idlen > (90-10)) {
474 if (!obj->sinfo.clientid) {
475 obj->sinfo.clientid = malloc(idlen + 8);
476 memcpy(obj->sinfo.clientid, p, idlen + 8);
477 obj->sinfo.clientid_len = idlen + 8;
479 if (ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p) != 0)
481 tklen = obj->sinfo.sessiontoken_len;
482 token = obj->sinfo.sessiontoken;
485 case 8: /* Panther Kerberos Token */
486 tklen = obj->sinfo.cryptedkey_len;
487 token = obj->sinfo.cryptedkey;
499 memcpy(rbuf, &tp, sizeof(tklen));
500 rbuf += sizeof(tklen);
501 *rbuflen += sizeof(tklen);
503 memcpy(rbuf, token, tklen);
509 /* ---------------------- */
510 int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
512 DSI *dsi = (DSI *)obj->dsi;
522 /* check for guest user */
523 if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
528 memcpy(&type, ibuf, sizeof(type));
530 ibuf += sizeof(type);
532 memcpy(&tklen, ibuf, sizeof(tklen));
533 tklen = ntohl(tklen);
534 ibuf += sizeof(tklen);
536 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
537 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
540 if (tklen != SESSIONTOKEN_LEN) {
543 tklen = sizeof(pid_t);
544 memcpy(&token, ibuf, tklen);
546 /* our stuff is pid + zero pad */
548 for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
554 LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
555 dsi->flags |= DSI_RECONINPROG;
557 /* Deactivate tickle timer */
558 const struct itimerval none = {{0, 0}, {0, 0}};
559 setitimer(ITIMER_REAL, &none, NULL);
561 /* check for old session, possibly transfering session from here to there */
562 if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) != 0)
564 /* write uint16_t DSI request ID */
565 if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
566 LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
569 /* now send our connected AFP client socket */
570 if (send_fd(obj->ipc_fd, dsi->socket) != 0)
572 /* Now see what happens: either afpd master sends us SIGTERM because our session */
573 /* has been transfered to a old disconnected session, or we continue */
576 if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
577 /* Reconnect succeeded, we exit now after sleeping some more */
578 sleep(2); /* sleep some more to give the recon. session time */
579 LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
584 /* Reinstall tickle timer */
585 setitimer(ITIMER_REAL, &dsi->timer, NULL);
587 LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
591 /* ---------------------- */
592 static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
596 if (!len || len > ibuflen)
597 return AFPERR_BADVERS;
599 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
600 for ( i = 0; i < num; i++ ) {
601 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
602 obj->afp_version = afp_versions[ i ].av_number;
603 afp_version_index = i;
607 if ( i == num ) /* An inappropo version */
608 return AFPERR_BADVERS ;
611 if (obj->afp_version >= 30 && sizeof(off_t) != 8) {
612 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
613 return AFPERR_BADVERS ;
619 /* ---------------------- */
620 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
622 struct passwd *pwd = NULL;
629 return send_reply(obj, AFPERR_SHUTDOWN );
632 return send_reply(obj, AFPERR_BADVERS );
635 len = (unsigned char) *ibuf++;
638 i = get_version(obj, ibuf, ibuflen, len);
640 return send_reply(obj, i );
643 return send_reply(obj, AFPERR_BADUAM);
648 len = (unsigned char) *ibuf++;
651 if (!len || len > ibuflen)
652 return send_reply(obj, AFPERR_BADUAM);
654 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
655 return send_reply(obj, AFPERR_BADUAM);
659 if (AFP_OK != (i = create_session_key(obj)) )
660 return send_reply(obj, i);
662 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
664 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
665 return send_reply(obj, i);
667 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
670 /* ---------------------- */
671 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
673 struct passwd *pwd = NULL;
683 return send_reply(obj, AFPERR_SHUTDOWN );
686 return send_reply(obj, AFPERR_BADVERS );
692 len = (unsigned char) *ibuf;
696 i = get_version(obj, ibuf, ibuflen, len);
698 return send_reply(obj, i );
701 return send_reply(obj, AFPERR_BADUAM);
706 len = (unsigned char) *ibuf;
710 if (!len || len > ibuflen)
711 return send_reply(obj, AFPERR_BADUAM);
713 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
714 return send_reply(obj, AFPERR_BADUAM);
718 if (!afp_uam->u.uam_login.login_ext) {
719 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
720 return send_reply(obj, AFPERR_BADUAM);
723 if (ibuflen <= 1 +sizeof(len16))
724 return send_reply(obj, AFPERR_PARAM);
730 return send_reply(obj, AFPERR_PARAM);
732 memcpy(&len16, ibuf, sizeof(len16));
733 ibuf += sizeof(len16);
734 ibuflen -= sizeof(len16);
737 return send_reply(obj, AFPERR_PARAM);
741 /* directory service name */
743 return send_reply(obj, AFPERR_PARAM);
752 return send_reply(obj, AFPERR_PARAM);
753 len = (unsigned char) *ibuf;
758 /* With "No User Authen" it is equal */
759 if (ibuflen < sizeof(len16))
760 return send_reply(obj, AFPERR_PARAM);
761 memcpy(&len16, ibuf, sizeof(len16));
762 ibuf += sizeof(len16);
763 ibuflen -= sizeof(len16);
767 return send_reply(obj, AFPERR_PARAM);
771 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
772 return send_reply(obj, AFPERR_PARAM);
779 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
784 if (AFP_OK != (i = create_session_key(obj)) ) {
785 return send_reply(obj, i);
788 /* FIXME user name are in UTF8 */
789 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
791 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
792 return send_reply(obj, i);
794 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
797 /* ---------------------- */
798 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
800 struct passwd *pwd = NULL;
803 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
805 return send_reply(obj, AFPERR_NOTAUTH );
808 ibuf += 2; ibuflen -= 2;
809 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
811 if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
812 return send_reply(obj, err);
814 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
818 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
820 DSI *dsi = (DSI *)(obj->dsi);
822 LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
823 of_close_all_forks(obj);
825 dsi->flags = DSI_AFP_LOGGED_OUT;
832 /* change password --
833 * NOTE: an FPLogin must already have completed successfully for this
834 * to work. this also does a little pre-processing before it hands
837 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
839 char username[MACFILELEN + 1], *start = ibuf;
848 /* check if password change is allowed, OS-X ignores the flag.
849 * we shouldn't trust the client on this anyway.
850 * not sure about the "right" error code, NOOP for now */
851 if (!(obj->options.passwdbits & PASSWD_SET))
854 /* make sure we can deal w/ this uam */
855 len = (unsigned char) *ibuf++;
856 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
857 return AFPERR_BADUAM;
860 if ((len + 1) & 1) /* pad byte */
863 if (obj->afp_version < 30) {
864 len = (unsigned char) *ibuf++;
865 if ( len > sizeof(username) - 1) {
868 memcpy(username, ibuf, len);
869 username[ len ] = '\0';
871 if ((len + 1) & 1) /* pad byte */
874 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
875 if ( ibuf[0] != '\0' || ibuf[1] != '\0')
878 len = MIN(sizeof(username) - 1, strlen(obj->username));
879 memcpy(username, obj->username, len);
880 username[ len ] = '\0';
884 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
886 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
889 /* send it off to the uam. we really don't use ibuflen right now. */
890 if (ibuflen < (size_t)(ibuf - start))
893 ibuflen -= (ibuf - start);
894 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
896 LOG(log_info, logtype_afpd, "password change %s.",
897 (ret == AFPERR_AUTHCONT) ? "continued" :
898 (ret ? "failed" : "succeeded"));
900 set_auth_switch(obj, 0);
907 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
914 LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
919 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
920 memcpy(&bitmap, ibuf, sizeof(bitmap));
921 bitmap = ntohs(bitmap);
923 /* deal with error cases. we don't have to worry about
924 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
928 if ((bitmap & USERIBIT_ALL) != bitmap)
929 return AFPERR_BITMAP;
931 /* remember place where we store the possibly modified bitmap later */
932 memcpy(rbuf, ibuf, sizeof(bitmap));
934 rbuf += sizeof(bitmap);
935 *rbuflen = sizeof(bitmap);
937 /* copy the user/group info */
938 if (bitmap & USERIBIT_USER) {
939 id = htonl(geteuid());
940 memcpy(rbuf, &id, sizeof(id));
942 *rbuflen += sizeof(id);
945 if (bitmap & USERIBIT_GROUP) {
946 id = htonl(getegid());
947 memcpy(rbuf, &id, sizeof(id));
949 *rbuflen += sizeof(id);
952 if (bitmap & USERIBIT_UUID) {
953 if ( ! (obj->options.flags & OPTION_UUID)) {
954 bitmap &= ~USERIBIT_UUID;
955 bitmap = htons(bitmap);
956 memcpy(bitmapp, &bitmap, sizeof(bitmap));
958 LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
961 ret = getuuidfromname( obj->username, UUID_USER, uuid);
963 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
964 return AFPERR_NOITEM;
966 LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
968 memcpy(rbuf, uuid, UUID_BINSIZE);
969 rbuf += UUID_BINSIZE;
970 *rbuflen += UUID_BINSIZE;
974 LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
978 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
979 (((type) == UAM_SERVER_CHANGEPW) ? \
980 &uam_changepw : NULL))
982 /* just do a linked list search. this could be sped up with a hashed
983 * list, but i doubt anyone's going to have enough uams to matter. */
984 struct uam_obj *auth_uamfind(const int type, const char *name,
987 struct uam_obj *prev, *start;
989 if (!name || !(start = UAM_LIST(type)))
993 while ((prev = prev->uam_prev) != start)
994 if (strndiacasecmp(prev->uam_name, name, len) == 0)
1000 int auth_register(const int type, struct uam_obj *uam)
1002 struct uam_obj *start;
1004 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1007 if (!(start = UAM_LIST(type)))
1008 return 1; /* we don't know what to do with it, caller must free it */
1010 uam_attach(start, uam);
1014 /* load all of the modules */
1015 int auth_load(const char *path, const char *list)
1017 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
1018 struct uam_mod *mod;
1022 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1025 strlcpy(buf, list, sizeof(buf));
1026 if ((p = strtok(buf, ", ")) == NULL)
1030 if (name[len - 1] != '/') {
1036 strlcpy(name + len, p, sizeof(name) - len);
1037 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1039 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1041 if (stat(name, &st) == 0) {
1042 if ((mod = uam_load(name, p))) {
1043 uam_attach(&uam_modules, mod);
1044 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1046 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1049 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1051 p = strtok(NULL, ", ");
1057 /* get rid of all of the uams */
1058 void auth_unload(void)
1060 struct uam_mod *mod, *prev, *start = &uam_modules;
1062 prev = start->uam_prev;
1063 while ((mod = prev) != start) {
1064 prev = prev->uam_prev;