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>
53 static int afp_version_index;
54 static struct uam_mod uam_modules = {NULL, NULL, &uam_modules, &uam_modules};
55 static struct uam_obj uam_login = {"", "", 0, {{NULL, NULL, NULL, NULL }}, &uam_login,
57 static struct uam_obj uam_changepw = {"", "", 0, {{NULL, NULL, NULL, NULL}}, &uam_changepw,
60 static struct uam_obj *afp_uam = NULL;
63 void status_versions( char *data, const DSI *dsi)
67 int len, num, i, count = 0;
69 memcpy(&status, start + AFPSTATUS_VERSOFF, sizeof(status));
70 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ] );
72 for ( i = 0; i < num; i++ ) {
73 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
76 data += ntohs( status );
79 for ( i = 0; i < num; i++ ) {
80 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
81 len = strlen( afp_versions[ i ].av_name );
83 memcpy( data, afp_versions[ i ].av_name , len );
86 status = htons( data - start );
87 memcpy(start + AFPSTATUS_UAMSOFF, &status, sizeof(status));
90 void status_uams(char *data, const char *authlist)
97 memcpy(&status, start + AFPSTATUS_UAMSOFF, sizeof(status));
99 while ((uams = uams->uam_prev) != &uam_login) {
100 if (strstr(authlist, uams->uam_path))
104 data += ntohs( status );
106 while ((uams = uams->uam_prev) != &uam_login) {
107 if (strstr(authlist, uams->uam_path)) {
108 LOG(log_info, logtype_afpd, "uam: \"%s\" available", uams->uam_name);
109 len = strlen( uams->uam_name);
111 memcpy( data, uams->uam_name, len );
117 status = htons(data - start);
118 memcpy(start + AFPSTATUS_ICONOFF, &status, sizeof(status));
121 /* handle errors by closing the connection. this is only needed
122 * by the afp_* functions. */
123 static int send_reply(const AFPObj *obj, const int err)
125 if ((err == AFP_OK) || (err == AFPERR_AUTHCONT))
128 obj->reply(obj->dsi, err);
134 static int afp_errpwdexpired(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
135 char *rbuf _U_, size_t *rbuflen)
138 return AFPERR_PWDEXPR;
141 static int afp_null_nolog(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
142 char *rbuf _U_, size_t *rbuflen)
145 return( AFPERR_NOOP );
148 static int set_auth_switch(const AFPObj *obj, int expired)
154 * BF: expired password handling
155 * to allow the user to change his/her password we have to allow login
156 * but every following call except for FPChangePassword will be thrown
157 * away with an AFPERR_PWDEXPR error. (thanks to Leland Wallace from Apple
158 * for clarifying this)
161 for (i=0; i<=0xff; i++) {
162 uam_afpserver_action(i, UAM_AFPSERVER_PREAUTH, afp_errpwdexpired, NULL);
164 uam_afpserver_action(AFP_LOGOUT, UAM_AFPSERVER_PREAUTH, afp_logout, NULL);
165 uam_afpserver_action(AFP_CHANGEPW, UAM_AFPSERVER_PREAUTH, afp_changepw, NULL);
168 afp_switch = postauth_switch;
169 switch (obj->afp_version) {
174 uam_afpserver_action(AFP_GETACL, UAM_AFPSERVER_POSTAUTH, afp_getacl, NULL);
175 uam_afpserver_action(AFP_SETACL, UAM_AFPSERVER_POSTAUTH, afp_setacl, NULL);
176 uam_afpserver_action(AFP_ACCESS, UAM_AFPSERVER_POSTAUTH, afp_access, NULL);
177 #endif /* HAVE_ACLS */
178 uam_afpserver_action(AFP_GETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_getextattr, NULL);
179 uam_afpserver_action(AFP_SETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_setextattr, NULL);
180 uam_afpserver_action(AFP_REMOVEATTR, UAM_AFPSERVER_POSTAUTH, afp_remextattr, NULL);
181 uam_afpserver_action(AFP_LISTEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_listextattr, NULL);
184 uam_afpserver_action(AFP_SYNCDIR, UAM_AFPSERVER_POSTAUTH, afp_syncdir, NULL);
185 uam_afpserver_action(AFP_SYNCFORK, UAM_AFPSERVER_POSTAUTH, afp_syncfork, NULL);
186 uam_afpserver_action(AFP_SPOTLIGHT_PRIVATE, UAM_AFPSERVER_POSTAUTH, afp_null_nolog, NULL);
187 uam_afpserver_action(AFP_ENUMERATE_EXT2, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext2, NULL);
190 uam_afpserver_action(AFP_ENUMERATE_EXT, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext, NULL);
191 uam_afpserver_action(AFP_BYTELOCK_EXT, UAM_AFPSERVER_POSTAUTH, afp_bytelock_ext, NULL);
192 /* catsearch_ext uses the same packet as catsearch FIXME double check this, it wasn't true for enue
194 uam_afpserver_action(AFP_CATSEARCH_EXT, UAM_AFPSERVER_POSTAUTH, afp_catsearch_ext, NULL);
195 uam_afpserver_action(AFP_GETSESSTOKEN, UAM_AFPSERVER_POSTAUTH, afp_getsession, NULL);
196 uam_afpserver_action(AFP_READ_EXT, UAM_AFPSERVER_POSTAUTH, afp_read_ext, NULL);
197 uam_afpserver_action(AFP_WRITE_EXT, UAM_AFPSERVER_POSTAUTH, afp_write_ext, NULL);
198 uam_afpserver_action(AFP_DISCTOLDSESS, UAM_AFPSERVER_POSTAUTH, afp_disconnect, NULL);
202 * If first connection to a server is done in classic AFP2.2 version is used
203 * but OSX uses AFP3.x FPzzz command !
205 uam_afpserver_action(AFP_ZZZ, UAM_AFPSERVER_POSTAUTH, afp_zzz, NULL);
213 #define GROUPSTR_BUFSIZE 1024
214 static const char *print_groups(int ngroups, gid_t *groups)
216 static char groupsstr[GROUPSTR_BUFSIZE];
223 for (i = 0; (i < ngroups) && (s < &groupsstr[GROUPSTR_BUFSIZE]); i++) {
224 s += snprintf(s, &groupsstr[GROUPSTR_BUFSIZE] - s, " %u", groups[i]);
230 static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void), int expired)
234 #endif /* ADMIN_GRP */
236 if ( pwd->pw_uid == 0 ) { /* don't allow root login */
237 LOG(log_error, logtype_afpd, "login: root login denied!" );
238 return AFPERR_NOTAUTH;
241 LOG(log_note, logtype_afpd, "%s Login by %s",
242 afp_versions[afp_version_index].av_name, pwd->pw_name);
244 if (initgroups( pwd->pw_name, pwd->pw_gid ) < 0) {
246 LOG(log_info, logtype_afpd, "running with uid %d", geteuid());
247 #else /* RUN_AS_USER */
248 LOG(log_error, logtype_afpd, "login: %s", strerror(errno));
249 return AFPERR_BADUAM;
250 #endif /* RUN_AS_USER */
254 /* Basically if the user is in the admin group, we stay root */
256 if ((obj->ngroups = getgroups( 0, NULL )) < 0 ) {
257 LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
258 return AFPERR_BADUAM;
261 if ( NULL == (obj->groups = calloc(obj->ngroups, sizeof(gid_t))) ) {
262 LOG(log_error, logtype_afpd, "login: %s calloc: %d", obj->ngroups);
263 return AFPERR_BADUAM;
266 if (( obj->ngroups = getgroups(obj->ngroups, obj->groups )) < 0 ) {
267 LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
268 return AFPERR_BADUAM;
272 LOG(log_debug, logtype_afpd, "obj->options.admingid == %d", obj->options.admingid);
274 if (obj->options.admingid != 0) {
276 for (i = 0; i < obj->ngroups; i++) {
277 if (obj->groups[i] == obj->options.admingid) admin = 1;
282 LOG(log_info, logtype_afpd, "admin login -- %s", pwd->pw_name );
285 #endif /* ADMIN_GRP */
288 struct DSI *dsi = obj->handle;
295 afp_get_cmdline( &argc, &argv );
297 hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
298 sizeof( struct in_addr ),
299 dsi->client.sin_family );
302 clientname = hp->h_name;
304 clientname = inet_ntoa( dsi->client.sin_addr );
306 sprintf( hostname, "%s@%s", pwd->pw_name, clientname );
308 if( sia_become_user( NULL, argc, argv, hostname, pwd->pw_name,
309 NULL, FALSE, NULL, NULL,
310 SIA_BEU_REALLOGIN ) != SIASUCCESS )
311 return AFPERR_BADUAM;
313 LOG(log_info, logtype_afpd, "session from %s (%s)", hostname,
314 inet_ntoa( dsi->client.sin_addr ) );
316 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
317 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
318 return AFPERR_BADUAM;
322 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
323 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
324 return AFPERR_BADUAM;
328 LOG(log_debug, logtype_afpd, "login: supplementary groups: %s", print_groups(obj->ngroups, obj->groups));
330 /* There's probably a better way to do this, but for now, we just play root */
335 #endif /* ADMIN_GRP */
336 obj->uid = geteuid();
338 set_auth_switch(obj, expired);
339 /* save our euid, we need it for preexec_close */
340 obj->uid = geteuid();
341 obj->logout = logout;
343 /* pam_umask or similar might have changed our umask */
344 (void)umask(obj->options.umask);
349 /* ---------------------- */
350 int afp_zzz(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
353 DSI *dsi = (DSI *)AFPobj->dsi;
361 memcpy(&data, ibuf, 4); /* flag */
365 * Possible sleeping states:
366 * 1) normal sleep: DSI_SLEEPING (up to 10.3)
367 * 2) extended sleep: DSI_SLEEPING | DSI_EXTSLEEP (starting with 10.4)
370 if (data & AFPZZZ_EXT_WAKEUP) {
371 /* wakeup request from exetended sleep */
372 if (dsi->flags & DSI_EXTSLEEP) {
373 LOG(log_note, logtype_afpd, "afp_zzz: waking up from extended sleep");
374 dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP);
378 dsi->flags |= DSI_SLEEPING;
379 if (data & AFPZZZ_EXT_SLEEP) {
380 LOG(log_note, logtype_afpd, "afp_zzz: entering extended sleep");
381 dsi->flags |= DSI_EXTSLEEP;
383 LOG(log_note, logtype_afpd, "afp_zzz: entering normal sleep");
388 * According to AFP 3.3 spec we should not return anything,
389 * but eg 10.5.8 server still returns the numbers of hours
390 * the server is keeping the sessino (ie max sleeptime).
392 data = obj->options.sleep / 120; /* hours */
396 *rbuflen = sizeof(data);
398 memcpy(rbuf, &data, sizeof(data));
399 rbuf += sizeof(data);
404 /* ---------------------- */
405 static int create_session_token(AFPObj *obj)
409 /* use 8 bytes for token as OSX, don't know if it helps */
410 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
411 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
415 if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
418 memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
419 obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
421 memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
426 static int create_session_key(AFPObj *obj)
428 /* create session key */
429 if (obj->sinfo.sessionkey == NULL) {
430 if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
432 uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
433 obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
439 /* ---------------------- */
442 char *ibuf, size_t ibuflen,
443 char *rbuf, size_t *rbuflen)
455 if (ibuflen < 2 + sizeof(type)) {
462 memcpy(&type, ibuf, sizeof(type));
464 ibuf += sizeof(type);
465 ibuflen -= sizeof(type);
467 if ( obj->sinfo.sessiontoken == NULL ) {
468 if ( create_session_token( obj ) )
476 case 0: /* old version ?*/
477 tklen = obj->sinfo.sessiontoken_len;
478 token = obj->sinfo.sessiontoken;
480 case 1: /* disconnect */
481 case 2: /* reconnect update id */
482 if (ibuflen >= sizeof(idlen)) {
483 memcpy(&idlen, ibuf, sizeof(idlen));
484 idlen = ntohl(idlen);
485 ibuf += sizeof(idlen);
486 ibuflen -= sizeof(idlen);
487 if (ibuflen < idlen) {
490 /* memcpy (id, ibuf, idlen) */
491 tklen = obj->sinfo.sessiontoken_len;
492 token = obj->sinfo.sessiontoken;
499 memcpy( &idlen, ibuf, sizeof(idlen));
500 idlen = ntohl(idlen);
501 ibuf += sizeof(idlen);
502 ibuflen -= sizeof(idlen);
503 ibuf += sizeof(boottime);
504 ibuflen -= sizeof(boottime);
505 if (ibuflen < idlen || idlen > (90-10)) {
508 if (!obj->sinfo.clientid) {
509 obj->sinfo.clientid = malloc(idlen + 8);
510 memcpy(obj->sinfo.clientid, p, idlen + 8);
511 obj->sinfo.clientid_len = idlen + 8;
513 if (ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p) != 0)
515 tklen = obj->sinfo.sessiontoken_len;
516 token = obj->sinfo.sessiontoken;
519 case 8: /* Panther Kerberos Token */
520 tklen = obj->sinfo.cryptedkey_len;
521 token = obj->sinfo.cryptedkey;
533 memcpy(rbuf, &tp, sizeof(tklen));
534 rbuf += sizeof(tklen);
535 *rbuflen += sizeof(tklen);
537 memcpy(rbuf, token, tklen);
543 /* ---------------------- */
544 int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
546 DSI *dsi = (DSI *)obj->dsi;
556 /* check for guest user */
557 if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
562 memcpy(&type, ibuf, sizeof(type));
564 ibuf += sizeof(type);
566 memcpy(&tklen, ibuf, sizeof(tklen));
567 tklen = ntohl(tklen);
568 ibuf += sizeof(tklen);
570 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
571 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
574 if (tklen != SESSIONTOKEN_LEN) {
577 tklen = sizeof(pid_t);
578 memcpy(&token, ibuf, tklen);
580 /* our stuff is pid + zero pad */
582 for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
588 LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
589 dsi->flags |= DSI_RECONINPROG;
591 /* Deactivate tickle timer */
592 const struct itimerval none = {{0, 0}, {0, 0}};
593 setitimer(ITIMER_REAL, &none, NULL);
595 /* check for old session, possibly transfering session from here to there */
596 if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) != 0)
598 /* write uint16_t DSI request ID */
599 if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
600 LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
603 /* now send our connected AFP client socket */
604 if (send_fd(obj->ipc_fd, dsi->socket) != 0)
606 /* Now see what happens: either afpd master sends us SIGTERM because our session */
607 /* has been transfered to a old disconnected session, or we continue */
610 if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
611 /* Reconnect succeeded, we exit now after sleeping some more */
612 sleep(2); /* sleep some more to give the recon. session time */
613 LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
618 /* Reinstall tickle timer */
619 setitimer(ITIMER_REAL, &dsi->timer, NULL);
621 LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
625 /* ---------------------- */
626 static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
630 if (!len || len > ibuflen)
631 return AFPERR_BADVERS;
633 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
634 for ( i = 0; i < num; i++ ) {
635 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
636 obj->afp_version = afp_versions[ i ].av_number;
637 afp_version_index = i;
641 if ( i == num ) /* An inappropo version */
642 return AFPERR_BADVERS ;
645 if (obj->afp_version >= 30 && sizeof(off_t) != 8) {
646 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
647 return AFPERR_BADVERS ;
653 /* ---------------------- */
654 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
656 struct passwd *pwd = NULL;
663 return send_reply(obj, AFPERR_SHUTDOWN );
666 return send_reply(obj, AFPERR_BADVERS );
669 len = (unsigned char) *ibuf++;
672 i = get_version(obj, ibuf, ibuflen, len);
674 return send_reply(obj, i );
677 return send_reply(obj, AFPERR_BADUAM);
682 len = (unsigned char) *ibuf++;
685 if (!len || len > ibuflen)
686 return send_reply(obj, AFPERR_BADUAM);
688 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
689 return send_reply(obj, AFPERR_BADUAM);
693 if (AFP_OK != (i = create_session_key(obj)) )
694 return send_reply(obj, i);
696 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
698 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
699 return send_reply(obj, i);
701 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
704 /* ---------------------- */
705 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
707 struct passwd *pwd = NULL;
717 return send_reply(obj, AFPERR_SHUTDOWN );
720 return send_reply(obj, AFPERR_BADVERS );
726 len = (unsigned char) *ibuf;
730 i = get_version(obj, ibuf, ibuflen, len);
732 return send_reply(obj, i );
735 return send_reply(obj, AFPERR_BADUAM);
740 len = (unsigned char) *ibuf;
744 if (!len || len > ibuflen)
745 return send_reply(obj, AFPERR_BADUAM);
747 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
748 return send_reply(obj, AFPERR_BADUAM);
752 if (!afp_uam->u.uam_login.login_ext) {
753 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
754 return send_reply(obj, AFPERR_BADUAM);
757 if (ibuflen <= 1 +sizeof(len16))
758 return send_reply(obj, AFPERR_PARAM);
764 return send_reply(obj, AFPERR_PARAM);
766 memcpy(&len16, ibuf, sizeof(len16));
767 ibuf += sizeof(len16);
768 ibuflen -= sizeof(len16);
771 return send_reply(obj, AFPERR_PARAM);
775 /* directory service name */
777 return send_reply(obj, AFPERR_PARAM);
786 return send_reply(obj, AFPERR_PARAM);
787 len = (unsigned char) *ibuf;
792 /* With "No User Authen" it is equal */
793 if (ibuflen < sizeof(len16))
794 return send_reply(obj, AFPERR_PARAM);
795 memcpy(&len16, ibuf, sizeof(len16));
796 ibuf += sizeof(len16);
797 ibuflen -= sizeof(len16);
801 return send_reply(obj, AFPERR_PARAM);
805 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
806 return send_reply(obj, AFPERR_PARAM);
813 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
818 if (AFP_OK != (i = create_session_key(obj)) ) {
819 return send_reply(obj, i);
822 /* FIXME user name are in UTF8 */
823 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
825 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
826 return send_reply(obj, i);
828 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
831 /* ---------------------- */
832 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
834 struct passwd *pwd = NULL;
837 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
839 return send_reply(obj, AFPERR_NOTAUTH );
842 ibuf += 2; ibuflen -= 2;
843 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
845 if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
846 return send_reply(obj, err);
848 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
852 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
854 DSI *dsi = (DSI *)(obj->dsi);
856 LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
857 of_close_all_forks(obj);
859 dsi->flags = DSI_AFP_LOGGED_OUT;
866 /* change password --
867 * NOTE: an FPLogin must already have completed successfully for this
868 * to work. this also does a little pre-processing before it hands
871 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
873 char username[MACFILELEN + 1], *start = ibuf;
882 /* check if password change is allowed, OS-X ignores the flag.
883 * we shouldn't trust the client on this anyway.
884 * not sure about the "right" error code, NOOP for now */
885 if (!(obj->options.passwdbits & PASSWD_SET))
888 /* make sure we can deal w/ this uam */
889 len = (unsigned char) *ibuf++;
890 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
891 return AFPERR_BADUAM;
894 if ((len + 1) & 1) /* pad byte */
897 if (obj->afp_version < 30) {
898 len = (unsigned char) *ibuf++;
899 if ( len > sizeof(username) - 1) {
902 memcpy(username, ibuf, len);
903 username[ len ] = '\0';
905 if ((len + 1) & 1) /* pad byte */
908 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
909 if ( ibuf[0] != '\0' || ibuf[1] != '\0')
912 len = MIN(sizeof(username), strlen(obj->username));
913 memcpy(username, obj->username, len);
914 username[ len ] = '\0';
918 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
920 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
923 /* send it off to the uam. we really don't use ibuflen right now. */
924 if (ibuflen < (size_t)(ibuf - start))
927 ibuflen -= (ibuf - start);
928 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
930 LOG(log_info, logtype_afpd, "password change %s.",
931 (ret == AFPERR_AUTHCONT) ? "continued" :
932 (ret ? "failed" : "succeeded"));
934 set_auth_switch(obj, 0);
941 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
948 LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
953 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
954 memcpy(&bitmap, ibuf, sizeof(bitmap));
955 bitmap = ntohs(bitmap);
957 /* deal with error cases. we don't have to worry about
958 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
962 if ((bitmap & USERIBIT_ALL) != bitmap)
963 return AFPERR_BITMAP;
965 /* remember place where we store the possibly modified bitmap later */
966 memcpy(rbuf, ibuf, sizeof(bitmap));
968 rbuf += sizeof(bitmap);
969 *rbuflen = sizeof(bitmap);
971 /* copy the user/group info */
972 if (bitmap & USERIBIT_USER) {
973 id = htonl(geteuid());
974 memcpy(rbuf, &id, sizeof(id));
976 *rbuflen += sizeof(id);
979 if (bitmap & USERIBIT_GROUP) {
980 id = htonl(getegid());
981 memcpy(rbuf, &id, sizeof(id));
983 *rbuflen += sizeof(id);
986 if (bitmap & USERIBIT_UUID) {
987 if ( ! (obj->options.flags & OPTION_UUID)) {
988 bitmap &= ~USERIBIT_UUID;
989 bitmap = htons(bitmap);
990 memcpy(bitmapp, &bitmap, sizeof(bitmap));
992 LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
995 ret = getuuidfromname( obj->username, UUID_USER, uuid);
997 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
998 return AFPERR_NOITEM;
1000 LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
1002 memcpy(rbuf, uuid, UUID_BINSIZE);
1003 rbuf += UUID_BINSIZE;
1004 *rbuflen += UUID_BINSIZE;
1008 LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
1012 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
1013 (((type) == UAM_SERVER_CHANGEPW) ? \
1014 &uam_changepw : NULL))
1016 /* just do a linked list search. this could be sped up with a hashed
1017 * list, but i doubt anyone's going to have enough uams to matter. */
1018 struct uam_obj *auth_uamfind(const int type, const char *name,
1021 struct uam_obj *prev, *start;
1023 if (!name || !(start = UAM_LIST(type)))
1027 while ((prev = prev->uam_prev) != start)
1028 if (strndiacasecmp(prev->uam_name, name, len) == 0)
1034 int auth_register(const int type, struct uam_obj *uam)
1036 struct uam_obj *start;
1038 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1041 if (!(start = UAM_LIST(type)))
1042 return 1; /* we don't know what to do with it, caller must free it */
1044 uam_attach(start, uam);
1048 /* load all of the modules */
1049 int auth_load(const char *path, const char *list)
1051 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
1052 struct uam_mod *mod;
1056 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1059 strlcpy(buf, list, sizeof(buf));
1060 if ((p = strtok(buf, ", ")) == NULL)
1064 if (name[len - 1] != '/') {
1070 strlcpy(name + len, p, sizeof(name) - len);
1071 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1073 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1075 if (stat(name, &st) == 0) {
1076 if ((mod = uam_load(name, p))) {
1077 uam_attach(&uam_modules, mod);
1078 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1080 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1083 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1085 p = strtok(NULL, ", ");
1091 /* get rid of all of the uams */
1092 void auth_unload(void)
1094 struct uam_mod *mod, *prev, *start = &uam_modules;
1096 prev = start->uam_prev;
1097 while ((mod = prev) != start) {
1098 prev = prev->uam_prev;