2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
15 #endif /* HAVE_UNISTD_H */
16 #include <sys/types.h>
17 #include <sys/param.h>
19 #include <netatalk/endian.h>
20 #include <atalk/afp.h>
21 #include <atalk/compat.h>
22 #include <atalk/util.h>
32 #include <arpa/inet.h>
36 extern void afp_get_cmdline( int *ac, char ***av );
39 #include <atalk/logger.h>
40 #include <atalk/server_ipc.h>
41 #include <atalk/uuid.h>
55 static int afp_version_index;
59 #if defined( sun ) && !defined( __svr4__ ) || defined( ultrix )
62 #define GROUPS_SIZE sizeof(int)
64 #else /* sun __svr4__ ultrix */
67 #define GROUPS_SIZE sizeof(gid_t)
68 #endif /* sun ultrix */
73 * These numbers are scattered throughout the code.
75 static struct afp_versions afp_versions[] = {
77 { "AFPVersion 1.1", 11 },
78 { "AFPVersion 2.0", 20 },
79 { "AFPVersion 2.1", 21 },
88 static struct uam_mod uam_modules = {NULL, NULL, &uam_modules, &uam_modules};
89 static struct uam_obj uam_login = {"", "", 0, {{NULL, NULL, NULL, NULL }}, &uam_login,
91 static struct uam_obj uam_changepw = {"", "", 0, {{NULL, NULL, NULL, NULL}}, &uam_changepw,
94 static struct uam_obj *afp_uam = NULL;
97 void status_versions( char *data, const ASP asp, const DSI *dsi)
101 int len, num, i, count = 0;
103 memcpy(&status, start + AFPSTATUS_VERSOFF, sizeof(status));
104 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ] );
106 for ( i = 0; i < num; i++ ) {
108 if ( !asp && (afp_versions[ i ].av_number <= 21)) continue;
109 #endif /* ! NO_DDP */
110 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
113 data += ntohs( status );
116 for ( i = 0; i < num; i++ ) {
118 if ( !asp && (afp_versions[ i ].av_number <= 21)) continue;
119 #endif /* ! NO_DDP */
120 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
121 len = strlen( afp_versions[ i ].av_name );
123 memcpy( data, afp_versions[ i ].av_name , len );
126 status = htons( data - start );
127 memcpy(start + AFPSTATUS_UAMSOFF, &status, sizeof(status));
130 void status_uams(char *data, const char *authlist)
134 struct uam_obj *uams;
137 memcpy(&status, start + AFPSTATUS_UAMSOFF, sizeof(status));
139 while ((uams = uams->uam_prev) != &uam_login) {
140 if (strstr(authlist, uams->uam_path))
144 data += ntohs( status );
146 while ((uams = uams->uam_prev) != &uam_login) {
147 if (strstr(authlist, uams->uam_path)) {
148 LOG(log_info, logtype_afpd, "uam: \"%s\" available", uams->uam_name);
149 len = strlen( uams->uam_name);
151 memcpy( data, uams->uam_name, len );
157 status = htons(data - start);
158 memcpy(start + AFPSTATUS_ICONOFF, &status, sizeof(status));
161 /* handle errors by closing the connection. this is only needed
162 * by the afp_* functions. */
163 static int send_reply(const AFPObj *obj, const int err)
165 if ((err == AFP_OK) || (err == AFPERR_AUTHCONT))
168 obj->reply(obj->handle, err);
174 static int afp_errpwdexpired(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
175 char *rbuf _U_, size_t *rbuflen)
178 return AFPERR_PWDEXPR;
181 static int afp_null_nolog(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
182 char *rbuf _U_, size_t *rbuflen)
185 return( AFPERR_NOOP );
188 static int set_auth_switch(int expired)
194 * BF: expired password handling
195 * to allow the user to change his/her password we have to allow login
196 * but every following call except for FPChangePassword will be thrown
197 * away with an AFPERR_PWDEXPR error. (thanks to Leland Wallace from Apple
198 * for clarifying this)
201 for (i=0; i<=0xff; i++) {
202 uam_afpserver_action(i, UAM_AFPSERVER_PREAUTH, afp_errpwdexpired, NULL);
204 uam_afpserver_action(AFP_LOGOUT, UAM_AFPSERVER_PREAUTH, afp_logout, NULL);
205 uam_afpserver_action(AFP_CHANGEPW, UAM_AFPSERVER_PREAUTH, afp_changepw, NULL);
208 afp_switch = postauth_switch;
209 switch (afp_version) {
214 uam_afpserver_action(AFP_GETACL, UAM_AFPSERVER_POSTAUTH, afp_getacl, NULL);
215 uam_afpserver_action(AFP_SETACL, UAM_AFPSERVER_POSTAUTH, afp_setacl, NULL);
216 uam_afpserver_action(AFP_ACCESS, UAM_AFPSERVER_POSTAUTH, afp_access, NULL);
217 #endif /* HAVE_ACLS */
218 uam_afpserver_action(AFP_GETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_getextattr, NULL);
219 uam_afpserver_action(AFP_SETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_setextattr, NULL);
220 uam_afpserver_action(AFP_REMOVEATTR, UAM_AFPSERVER_POSTAUTH, afp_remextattr, NULL);
221 uam_afpserver_action(AFP_LISTEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_listextattr, NULL);
224 uam_afpserver_action(AFP_SYNCDIR, UAM_AFPSERVER_POSTAUTH, afp_syncdir, NULL);
225 uam_afpserver_action(AFP_SYNCFORK, UAM_AFPSERVER_POSTAUTH, afp_syncfork, NULL);
226 uam_afpserver_action(AFP_SPOTLIGHT_PRIVATE, UAM_AFPSERVER_POSTAUTH, afp_null_nolog, NULL);
227 uam_afpserver_action(AFP_ENUMERATE_EXT2, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext2, NULL);
230 uam_afpserver_action(AFP_ENUMERATE_EXT, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext, NULL);
231 uam_afpserver_action(AFP_BYTELOCK_EXT, UAM_AFPSERVER_POSTAUTH, afp_bytelock_ext, NULL);
232 /* catsearch_ext uses the same packet as catsearch FIXME double check this, it wasn't true for enue
234 uam_afpserver_action(AFP_CATSEARCH_EXT, UAM_AFPSERVER_POSTAUTH, afp_catsearch_ext, NULL);
235 uam_afpserver_action(AFP_GETSESSTOKEN, UAM_AFPSERVER_POSTAUTH, afp_getsession, NULL);
236 uam_afpserver_action(AFP_READ_EXT, UAM_AFPSERVER_POSTAUTH, afp_read_ext, NULL);
237 uam_afpserver_action(AFP_WRITE_EXT, UAM_AFPSERVER_POSTAUTH, afp_write_ext, NULL);
238 uam_afpserver_action(AFP_DISCTOLDSESS, UAM_AFPSERVER_POSTAUTH, afp_disconnect, NULL);
242 * If first connection to a server is done in classic AFP2.2 version is used
243 * but OSX uses AFP3.x FPzzz command !
245 uam_afpserver_action(AFP_ZZZ, UAM_AFPSERVER_POSTAUTH, afp_zzz, NULL);
253 static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void), int expired)
257 #endif /* ADMIN_GRP */
260 set_processname("afpd");
263 if ( pwd->pw_uid == 0 ) { /* don't allow root login */
264 LOG(log_error, logtype_afpd, "login: root login denied!" );
265 return AFPERR_NOTAUTH;
268 LOG(log_note, logtype_afpd, "%s Login by %s",
269 afp_versions[afp_version_index].av_name, pwd->pw_name);
272 if (obj->proto == AFPPROTO_ASP) {
273 ASP asp = obj->handle;
274 int addr_net = ntohs( asp->asp_sat.sat_addr.s_net );
275 int addr_node = asp->asp_sat.sat_addr.s_node;
277 if (obj->options.authprintdir) {
278 if(addr_net && addr_node) { /* Do we have a valid Appletalk address? */
281 int mypid = getpid();
282 struct stat stat_buf;
284 sprintf(nodename, "%s/net%d.%dnode%d", obj->options.authprintdir,
285 addr_net / 256, addr_net % 256, addr_node);
286 LOG(log_info, logtype_afpd, "registering %s (uid %d) on %u.%u as %s",
287 pwd->pw_name, pwd->pw_uid, addr_net, addr_node, nodename);
289 if (stat(nodename, &stat_buf) == 0) { /* file exists */
290 if (S_ISREG(stat_buf.st_mode)) { /* normal file */
292 fp = fopen(nodename, "w");
293 fprintf(fp, "%s:%d\n", pwd->pw_name, mypid);
295 chown( nodename, pwd->pw_uid, -1 );
296 } else { /* somebody is messing with us */
297 LOG(log_error, logtype_afpd, "print authfile %s is not a normal file, it will not be modified", nodename );
299 } else { /* file 'nodename' does not exist */
300 fp = fopen(nodename, "w");
301 fprintf(fp, "%s:%d\n", pwd->pw_name, mypid);
303 chown( nodename, pwd->pw_uid, -1 );
305 } /* if (addr_net && addr_node ) */
306 } /* if (options->authprintdir) */
307 } /* if (obj->proto == AFPPROTO_ASP) */
310 if (initgroups( pwd->pw_name, pwd->pw_gid ) < 0) {
312 LOG(log_info, logtype_afpd, "running with uid %d", geteuid());
313 #else /* RUN_AS_USER */
314 LOG(log_error, logtype_afpd, "login: %s", strerror(errno));
315 return AFPERR_BADUAM;
316 #endif /* RUN_AS_USER */
320 /* Basically if the user is in the admin group, we stay root */
322 if (( ngroups = getgroups( 0, NULL )) < 0 ) {
323 LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
324 return AFPERR_BADUAM;
327 if ( NULL == (groups = calloc(ngroups, GROUPS_SIZE)) ) {
328 LOG(log_error, logtype_afpd, "login: %s calloc: %d", ngroups);
329 return AFPERR_BADUAM;
332 if (( ngroups = getgroups( ngroups, groups )) < 0 ) {
333 LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
334 return AFPERR_BADUAM;
338 LOG(log_debug, logtype_afpd, "obj->options.admingid == %d", obj->options.admingid);
340 if (obj->options.admingid != 0) {
342 for (i = 0; i < ngroups; i++) {
343 if (groups[i] == obj->options.admingid) admin = 1;
348 LOG(log_info, logtype_afpd, "admin login -- %s", pwd->pw_name );
351 #endif /* ADMIN_GRP */
354 struct DSI *dsi = obj->handle;
361 afp_get_cmdline( &argc, &argv );
363 hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
364 sizeof( struct in_addr ),
365 dsi->client.sin_family );
368 clientname = hp->h_name;
370 clientname = inet_ntoa( dsi->client.sin_addr );
372 sprintf( hostname, "%s@%s", pwd->pw_name, clientname );
374 if( sia_become_user( NULL, argc, argv, hostname, pwd->pw_name,
375 NULL, FALSE, NULL, NULL,
376 SIA_BEU_REALLOGIN ) != SIASUCCESS )
377 return AFPERR_BADUAM;
379 LOG(log_info, logtype_afpd, "session from %s (%s)", hostname,
380 inet_ntoa( dsi->client.sin_addr ) );
382 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
383 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
384 return AFPERR_BADUAM;
388 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
389 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
390 return AFPERR_BADUAM;
395 #define GROUPSTR_BUFSIZE 1024
396 char groupsstr[GROUPSTR_BUFSIZE];
398 int j = GROUPSTR_BUFSIZE;
400 int n = snprintf(groupsstr, GROUPSTR_BUFSIZE, "%u", groups[0]);
404 for (int i = 1; i < ngroups; i++) {
405 n = snprintf(s, j, ", %u", groups[i]);
408 LOG(log_debug, logtype_afpd, "login: group string buffer overflow");
414 LOG(log_debug, logtype_afpd, "login: %u supplementary groups: %s", ngroups, groupsstr);
417 /* There's probably a better way to do this, but for now, we just play root */
422 #endif /* ADMIN_GRP */
425 set_auth_switch(expired);
426 /* save our euid, we need it for preexec_close */
427 obj->uid = geteuid();
428 obj->logout = logout;
432 save_uidgid ( &obj->uidgid );
438 /* ---------------------- */
439 int afp_zzz(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
442 DSI *dsi = (DSI *)AFPobj->handle;
448 memcpy(&data, ibuf, 4); /* flag */
452 * Possible sleeping states:
453 * 1) normal sleep: DSI_SLEEPING (up to 10.3)
454 * 2) extended sleep: DSI_SLEEPING | DSI_EXTSLEEP (starting with 10.4)
457 if (data & AFPZZZ_EXT_WAKEUP) {
458 /* wakeup request from exetended sleep */
459 if (dsi->flags & DSI_EXTSLEEP) {
460 LOG(log_debug, logtype_afpd, "afp_zzz: waking up from extended sleep");
461 dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP);
465 dsi->flags |= DSI_SLEEPING;
466 if (data & AFPZZZ_EXT_SLEEP) {
467 LOG(log_debug, logtype_afpd, "afp_zzz: entering extended sleep");
468 dsi->flags |= DSI_EXTSLEEP;
470 LOG(log_debug, logtype_afpd, "afp_zzz: entering normal sleep");
474 * According to AFP 3.3 spec we should not return anything,
475 * but eg 10.5.8 server still returns the numbers of hours
476 * the server is keeping the sessino (ie max sleeptime).
478 data = obj->options.sleep / 120; /* hours */
482 *rbuflen = sizeof(data);
484 memcpy(rbuf, &data, sizeof(data));
485 rbuf += sizeof(data);
490 /* ---------------------- */
491 static int create_session_token(AFPObj *obj)
495 /* use 8 bytes for token as OSX, don't know if it helps */
496 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
497 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
501 if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
504 memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
505 obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
507 memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
512 static int create_session_key(AFPObj *obj)
514 /* create session key */
515 if (obj->sinfo.sessionkey == NULL) {
516 if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
518 uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
519 obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
525 /* ---------------------- */
528 char *ibuf, size_t ibuflen,
529 char *rbuf, size_t *rbuflen)
541 if (ibuflen < 2 + sizeof(type)) {
548 memcpy(&type, ibuf, sizeof(type));
550 ibuf += sizeof(type);
551 ibuflen -= sizeof(type);
553 if ( obj->sinfo.sessiontoken == NULL ) {
554 if ( create_session_token( obj ) )
562 case 0: /* old version ?*/
563 tklen = obj->sinfo.sessiontoken_len;
564 token = obj->sinfo.sessiontoken;
566 case 1: /* disconnect */
567 case 2: /* reconnect update id */
568 if (ibuflen >= sizeof(idlen)) {
569 memcpy(&idlen, ibuf, sizeof(idlen));
570 idlen = ntohl(idlen);
571 ibuf += sizeof(idlen);
572 ibuflen -= sizeof(idlen);
573 if (ibuflen < idlen) {
576 /* memcpy (id, ibuf, idlen) */
577 tklen = obj->sinfo.sessiontoken_len;
578 token = obj->sinfo.sessiontoken;
585 memcpy( &idlen, ibuf, sizeof(idlen));
586 idlen = ntohl(idlen);
587 ibuf += sizeof(idlen);
588 ibuflen -= sizeof(idlen);
589 ibuf += sizeof(boottime);
590 ibuflen -= sizeof(boottime);
591 if (ibuflen < idlen || idlen > (90-10)) {
594 ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p);
595 tklen = obj->sinfo.sessiontoken_len;
596 token = obj->sinfo.sessiontoken;
599 case 8: /* Panther Kerberos Token */
600 tklen = obj->sinfo.cryptedkey_len;
601 token = obj->sinfo.cryptedkey;
613 memcpy(rbuf, &tp, sizeof(tklen));
614 rbuf += sizeof(tklen);
615 *rbuflen += sizeof(tklen);
617 memcpy(rbuf, token, tklen);
623 /* ---------------------- */
624 int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
626 DSI *dsi = (DSI *)obj->handle;
636 /* check for guest user */
637 if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
642 memcpy(&type, ibuf, sizeof(type));
644 ibuf += sizeof(type);
646 memcpy(&tklen, ibuf, sizeof(tklen));
647 tklen = ntohl(tklen);
648 ibuf += sizeof(tklen);
650 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
651 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
654 if (tklen != SESSIONTOKEN_LEN) {
657 tklen = sizeof(pid_t);
658 memcpy(&token, ibuf, tklen);
660 /* our stuff is pid + zero pad */
662 for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
668 LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
669 dsi->flags |= DSI_RECONINPROG;
671 /* Deactivate tickle timer */
672 const struct itimerval none = {{0, 0}, {0, 0}};
673 setitimer(ITIMER_REAL, &none, NULL);
675 /* check for old session, possibly transfering session from here to there */
676 if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) == -1)
678 /* write uint16_t DSI request ID */
679 if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
680 LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
683 /* now send our connected AFP client socket */
684 if (send_fd(obj->ipc_fd, dsi->socket) != 0)
686 /* Now see what happens: either afpd master sends us SIGTERM because our session */
687 /* has been transfered to a old disconnected session, or we continue */
690 if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
691 /* Reconnect succeeded, we exit now after sleeping some more */
692 sleep(2); /* sleep some more to give the recon. session time */
693 LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
698 /* Reinstall tickle timer */
699 setitimer(ITIMER_REAL, &dsi->timer, NULL);
701 LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
705 /* ---------------------- */
706 static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
710 if (!len || len > ibuflen)
711 return AFPERR_BADVERS;
713 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
714 for ( i = 0; i < num; i++ ) {
715 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
716 afp_version = afp_versions[ i ].av_number;
717 afp_version_index = i;
721 if ( i == num ) /* An inappropo version */
722 return AFPERR_BADVERS ;
724 if (afp_version >= 30 && obj->proto != AFPPROTO_DSI)
725 return AFPERR_BADVERS ;
728 if (afp_version >= 30 && sizeof(off_t) != 8) {
729 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
730 return AFPERR_BADVERS ;
736 /* ---------------------- */
737 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
739 struct passwd *pwd = NULL;
746 return send_reply(obj, AFPERR_SHUTDOWN );
749 return send_reply(obj, AFPERR_BADVERS );
752 len = (unsigned char) *ibuf++;
755 i = get_version(obj, ibuf, ibuflen, len);
757 return send_reply(obj, i );
760 return send_reply(obj, AFPERR_BADUAM);
765 len = (unsigned char) *ibuf++;
768 if (!len || len > ibuflen)
769 return send_reply(obj, AFPERR_BADUAM);
771 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
772 return send_reply(obj, AFPERR_BADUAM);
776 if (AFP_OK != (i = create_session_key(obj)) )
777 return send_reply(obj, i);
779 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
781 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
782 return send_reply(obj, i);
784 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
787 /* ---------------------- */
788 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
790 struct passwd *pwd = NULL;
800 return send_reply(obj, AFPERR_SHUTDOWN );
803 return send_reply(obj, AFPERR_BADVERS );
809 len = (unsigned char) *ibuf;
813 i = get_version(obj, ibuf, ibuflen, len);
815 return send_reply(obj, i );
818 return send_reply(obj, AFPERR_BADUAM);
823 len = (unsigned char) *ibuf;
827 if (!len || len > ibuflen)
828 return send_reply(obj, AFPERR_BADUAM);
830 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
831 return send_reply(obj, AFPERR_BADUAM);
835 if (!afp_uam->u.uam_login.login_ext) {
836 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
837 return send_reply(obj, AFPERR_BADUAM);
840 if (ibuflen <= 1 +sizeof(len16))
841 return send_reply(obj, AFPERR_PARAM);
847 return send_reply(obj, AFPERR_PARAM);
849 memcpy(&len16, ibuf, sizeof(len16));
850 ibuf += sizeof(len16);
851 ibuflen -= sizeof(len16);
854 return send_reply(obj, AFPERR_PARAM);
858 /* directory service name */
860 return send_reply(obj, AFPERR_PARAM);
869 return send_reply(obj, AFPERR_PARAM);
870 len = (unsigned char) *ibuf;
875 /* With "No User Authen" it is equal */
876 if (ibuflen < sizeof(len16))
877 return send_reply(obj, AFPERR_PARAM);
878 memcpy(&len16, ibuf, sizeof(len16));
879 ibuf += sizeof(len16);
880 ibuflen -= sizeof(len16);
884 return send_reply(obj, AFPERR_PARAM);
888 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
889 return send_reply(obj, AFPERR_PARAM);
896 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
901 if (AFP_OK != (i = create_session_key(obj)) ) {
902 return send_reply(obj, i);
905 /* FIXME user name are in UTF8 */
906 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
908 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
909 return send_reply(obj, i);
911 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
914 /* ---------------------- */
915 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
917 struct passwd *pwd = NULL;
920 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
922 return send_reply(obj, AFPERR_NOTAUTH );
925 ibuf += 2; ibuflen -= 2;
926 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
928 if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
929 return send_reply(obj, err);
931 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
935 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen _U_)
937 LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
945 /* change password --
946 * NOTE: an FPLogin must already have completed successfully for this
947 * to work. this also does a little pre-processing before it hands
950 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
952 char username[MACFILELEN + 1], *start = ibuf;
961 /* check if password change is allowed, OS-X ignores the flag.
962 * we shouldn't trust the client on this anyway.
963 * not sure about the "right" error code, NOOP for now */
964 if (!(obj->options.passwdbits & PASSWD_SET))
967 /* make sure we can deal w/ this uam */
968 len = (unsigned char) *ibuf++;
969 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
970 return AFPERR_BADUAM;
973 if ((len + 1) & 1) /* pad byte */
976 if ( afp_version < 30) {
977 len = (unsigned char) *ibuf++;
978 if ( len > sizeof(username) - 1) {
981 memcpy(username, ibuf, len);
982 username[ len ] = '\0';
984 if ((len + 1) & 1) /* pad byte */
987 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
988 if ( ibuf[0] != '\0' || ibuf[1] != '\0')
991 len = MIN(sizeof(username), strlen(obj->username));
992 memcpy(username, obj->username, len);
993 username[ len ] = '\0';
997 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
999 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
1000 return AFPERR_PARAM;
1002 /* send it off to the uam. we really don't use ibuflen right now. */
1003 if (ibuflen < (size_t)(ibuf - start))
1004 return AFPERR_PARAM;
1006 ibuflen -= (ibuf - start);
1007 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
1009 LOG(log_info, logtype_afpd, "password change %s.",
1010 (ret == AFPERR_AUTHCONT) ? "continued" :
1011 (ret ? "failed" : "succeeded"));
1012 if ( ret == AFP_OK )
1020 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1027 LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
1032 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
1033 memcpy(&bitmap, ibuf, sizeof(bitmap));
1034 bitmap = ntohs(bitmap);
1036 /* deal with error cases. we don't have to worry about
1037 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
1040 return AFPERR_PARAM;
1041 if ((bitmap & USERIBIT_ALL) != bitmap)
1042 return AFPERR_BITMAP;
1044 /* remember place where we store the possibly modified bitmap later */
1045 memcpy(rbuf, ibuf, sizeof(bitmap));
1047 rbuf += sizeof(bitmap);
1048 *rbuflen = sizeof(bitmap);
1050 /* copy the user/group info */
1051 if (bitmap & USERIBIT_USER) {
1052 id = htonl(geteuid());
1053 memcpy(rbuf, &id, sizeof(id));
1055 *rbuflen += sizeof(id);
1058 if (bitmap & USERIBIT_GROUP) {
1059 id = htonl(getegid());
1060 memcpy(rbuf, &id, sizeof(id));
1062 *rbuflen += sizeof(id);
1065 if (bitmap & USERIBIT_UUID) {
1066 if ( ! (obj->options.flags & OPTION_UUID)) {
1067 bitmap &= ~USERIBIT_UUID;
1068 bitmap = htons(bitmap);
1069 memcpy(bitmapp, &bitmap, sizeof(bitmap));
1071 LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
1074 ret = getuuidfromname( obj->username, UUID_USER, uuid);
1076 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
1077 return AFPERR_NOITEM;
1079 LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
1081 memcpy(rbuf, uuid, UUID_BINSIZE);
1082 rbuf += UUID_BINSIZE;
1083 *rbuflen += UUID_BINSIZE;
1087 LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
1091 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
1092 (((type) == UAM_SERVER_CHANGEPW) ? \
1093 &uam_changepw : NULL))
1095 /* just do a linked list search. this could be sped up with a hashed
1096 * list, but i doubt anyone's going to have enough uams to matter. */
1097 struct uam_obj *auth_uamfind(const int type, const char *name,
1100 struct uam_obj *prev, *start;
1102 if (!name || !(start = UAM_LIST(type)))
1106 while ((prev = prev->uam_prev) != start)
1107 if (strndiacasecmp(prev->uam_name, name, len) == 0)
1113 int auth_register(const int type, struct uam_obj *uam)
1115 struct uam_obj *start;
1117 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1120 if (!(start = UAM_LIST(type)))
1121 return 1; /* we don't know what to do with it, caller must free it */
1123 uam_attach(start, uam);
1127 /* load all of the modules */
1128 int auth_load(const char *path, const char *list)
1130 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
1131 struct uam_mod *mod;
1135 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1138 strlcpy(buf, list, sizeof(buf));
1139 if ((p = strtok(buf, ",")) == NULL)
1143 if (name[len - 1] != '/') {
1149 strlcpy(name + len, p, sizeof(name) - len);
1150 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1152 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1154 if (stat(name, &st) == 0) {
1155 if ((mod = uam_load(name, p))) {
1156 uam_attach(&uam_modules, mod);
1157 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1159 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1162 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1164 p = strtok(NULL, ",");
1170 /* get rid of all of the uams */
1171 void auth_unload(void)
1173 struct uam_mod *mod, *prev, *start = &uam_modules;
1175 prev = start->uam_prev;
1176 while ((mod = prev) != start) {
1177 prev = prev->uam_prev;