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, char *rbuf, size_t *rbuflen)
442 DSI *dsi = (DSI *)AFPobj->handle;
450 memcpy(&data, ibuf, 4); /* flag */
454 * Possible sleeping states:
455 * 1) normal sleep: DSI_SLEEPING (up to 10.3)
456 * 2) extended sleep: DSI_SLEEPING | DSI_EXTSLEEP (starting with 10.4)
459 if (data & AFPZZZ_EXT_WAKEUP) {
460 /* wakeup request from exetended sleep */
461 if (dsi->flags & DSI_EXTSLEEP) {
462 LOG(log_note, logtype_afpd, "afp_zzz: waking up from extended sleep");
463 dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP);
467 dsi->flags |= DSI_SLEEPING;
468 if (data & AFPZZZ_EXT_SLEEP) {
469 LOG(log_note, logtype_afpd, "afp_zzz: entering extended sleep");
470 dsi->flags |= DSI_EXTSLEEP;
472 LOG(log_note, logtype_afpd, "afp_zzz: entering normal sleep");
477 * According to AFP 3.3 spec we should not return anything,
478 * but eg 10.5.8 server still returns the numbers of hours
479 * the server is keeping the sessino (ie max sleeptime).
481 data = obj->options.sleep / 120; /* hours */
485 *rbuflen = sizeof(data);
487 memcpy(rbuf, &data, sizeof(data));
488 rbuf += sizeof(data);
493 /* ---------------------- */
494 static int create_session_token(AFPObj *obj)
498 /* use 8 bytes for token as OSX, don't know if it helps */
499 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
500 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
504 if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
507 memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
508 obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
510 memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
515 static int create_session_key(AFPObj *obj)
517 /* create session key */
518 if (obj->sinfo.sessionkey == NULL) {
519 if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
521 uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
522 obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
528 /* ---------------------- */
531 char *ibuf, size_t ibuflen,
532 char *rbuf, size_t *rbuflen)
544 if (ibuflen < 2 + sizeof(type)) {
551 memcpy(&type, ibuf, sizeof(type));
553 ibuf += sizeof(type);
554 ibuflen -= sizeof(type);
556 if ( obj->sinfo.sessiontoken == NULL ) {
557 if ( create_session_token( obj ) )
565 case 0: /* old version ?*/
566 tklen = obj->sinfo.sessiontoken_len;
567 token = obj->sinfo.sessiontoken;
569 case 1: /* disconnect */
570 case 2: /* reconnect update id */
571 if (ibuflen >= sizeof(idlen)) {
572 memcpy(&idlen, ibuf, sizeof(idlen));
573 idlen = ntohl(idlen);
574 ibuf += sizeof(idlen);
575 ibuflen -= sizeof(idlen);
576 if (ibuflen < idlen) {
579 /* memcpy (id, ibuf, idlen) */
580 tklen = obj->sinfo.sessiontoken_len;
581 token = obj->sinfo.sessiontoken;
588 memcpy( &idlen, ibuf, sizeof(idlen));
589 idlen = ntohl(idlen);
590 ibuf += sizeof(idlen);
591 ibuflen -= sizeof(idlen);
592 ibuf += sizeof(boottime);
593 ibuflen -= sizeof(boottime);
594 if (ibuflen < idlen || idlen > (90-10)) {
597 ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p);
598 tklen = obj->sinfo.sessiontoken_len;
599 token = obj->sinfo.sessiontoken;
602 case 8: /* Panther Kerberos Token */
603 tklen = obj->sinfo.cryptedkey_len;
604 token = obj->sinfo.cryptedkey;
616 memcpy(rbuf, &tp, sizeof(tklen));
617 rbuf += sizeof(tklen);
618 *rbuflen += sizeof(tklen);
620 memcpy(rbuf, token, tklen);
626 /* ---------------------- */
627 int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
629 DSI *dsi = (DSI *)obj->handle;
639 /* check for guest user */
640 if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
645 memcpy(&type, ibuf, sizeof(type));
647 ibuf += sizeof(type);
649 memcpy(&tklen, ibuf, sizeof(tklen));
650 tklen = ntohl(tklen);
651 ibuf += sizeof(tklen);
653 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
654 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
657 if (tklen != SESSIONTOKEN_LEN) {
660 tklen = sizeof(pid_t);
661 memcpy(&token, ibuf, tklen);
663 /* our stuff is pid + zero pad */
665 for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
671 LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
672 dsi->flags |= DSI_RECONINPROG;
674 /* Deactivate tickle timer */
675 const struct itimerval none = {{0, 0}, {0, 0}};
676 setitimer(ITIMER_REAL, &none, NULL);
678 /* check for old session, possibly transfering session from here to there */
679 if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) == -1)
681 /* write uint16_t DSI request ID */
682 if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
683 LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
686 /* now send our connected AFP client socket */
687 if (send_fd(obj->ipc_fd, dsi->socket) != 0)
689 /* Now see what happens: either afpd master sends us SIGTERM because our session */
690 /* has been transfered to a old disconnected session, or we continue */
693 if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
694 /* Reconnect succeeded, we exit now after sleeping some more */
695 sleep(2); /* sleep some more to give the recon. session time */
696 LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
701 /* Reinstall tickle timer */
702 setitimer(ITIMER_REAL, &dsi->timer, NULL);
704 LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
708 /* ---------------------- */
709 static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
713 if (!len || len > ibuflen)
714 return AFPERR_BADVERS;
716 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
717 for ( i = 0; i < num; i++ ) {
718 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
719 afp_version = afp_versions[ i ].av_number;
720 afp_version_index = i;
724 if ( i == num ) /* An inappropo version */
725 return AFPERR_BADVERS ;
727 if (afp_version >= 30 && obj->proto != AFPPROTO_DSI)
728 return AFPERR_BADVERS ;
731 if (afp_version >= 30 && sizeof(off_t) != 8) {
732 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
733 return AFPERR_BADVERS ;
739 /* ---------------------- */
740 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
742 struct passwd *pwd = NULL;
749 return send_reply(obj, AFPERR_SHUTDOWN );
752 return send_reply(obj, AFPERR_BADVERS );
755 len = (unsigned char) *ibuf++;
758 i = get_version(obj, ibuf, ibuflen, len);
760 return send_reply(obj, i );
763 return send_reply(obj, AFPERR_BADUAM);
768 len = (unsigned char) *ibuf++;
771 if (!len || len > ibuflen)
772 return send_reply(obj, AFPERR_BADUAM);
774 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
775 return send_reply(obj, AFPERR_BADUAM);
779 if (AFP_OK != (i = create_session_key(obj)) )
780 return send_reply(obj, i);
782 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
784 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
785 return send_reply(obj, i);
787 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
790 /* ---------------------- */
791 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
793 struct passwd *pwd = NULL;
803 return send_reply(obj, AFPERR_SHUTDOWN );
806 return send_reply(obj, AFPERR_BADVERS );
812 len = (unsigned char) *ibuf;
816 i = get_version(obj, ibuf, ibuflen, len);
818 return send_reply(obj, i );
821 return send_reply(obj, AFPERR_BADUAM);
826 len = (unsigned char) *ibuf;
830 if (!len || len > ibuflen)
831 return send_reply(obj, AFPERR_BADUAM);
833 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
834 return send_reply(obj, AFPERR_BADUAM);
838 if (!afp_uam->u.uam_login.login_ext) {
839 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
840 return send_reply(obj, AFPERR_BADUAM);
843 if (ibuflen <= 1 +sizeof(len16))
844 return send_reply(obj, AFPERR_PARAM);
850 return send_reply(obj, AFPERR_PARAM);
852 memcpy(&len16, ibuf, sizeof(len16));
853 ibuf += sizeof(len16);
854 ibuflen -= sizeof(len16);
857 return send_reply(obj, AFPERR_PARAM);
861 /* directory service name */
863 return send_reply(obj, AFPERR_PARAM);
872 return send_reply(obj, AFPERR_PARAM);
873 len = (unsigned char) *ibuf;
878 /* With "No User Authen" it is equal */
879 if (ibuflen < sizeof(len16))
880 return send_reply(obj, AFPERR_PARAM);
881 memcpy(&len16, ibuf, sizeof(len16));
882 ibuf += sizeof(len16);
883 ibuflen -= sizeof(len16);
887 return send_reply(obj, AFPERR_PARAM);
891 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
892 return send_reply(obj, AFPERR_PARAM);
899 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
904 if (AFP_OK != (i = create_session_key(obj)) ) {
905 return send_reply(obj, i);
908 /* FIXME user name are in UTF8 */
909 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
911 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
912 return send_reply(obj, i);
914 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
917 /* ---------------------- */
918 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
920 struct passwd *pwd = NULL;
923 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
925 return send_reply(obj, AFPERR_NOTAUTH );
928 ibuf += 2; ibuflen -= 2;
929 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
931 if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
932 return send_reply(obj, err);
934 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
938 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
940 DSI *dsi = (DSI *)(obj->handle);
942 LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
943 of_close_all_forks();
945 dsi->flags = DSI_AFP_LOGGED_OUT;
952 /* change password --
953 * NOTE: an FPLogin must already have completed successfully for this
954 * to work. this also does a little pre-processing before it hands
957 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
959 char username[MACFILELEN + 1], *start = ibuf;
968 /* check if password change is allowed, OS-X ignores the flag.
969 * we shouldn't trust the client on this anyway.
970 * not sure about the "right" error code, NOOP for now */
971 if (!(obj->options.passwdbits & PASSWD_SET))
974 /* make sure we can deal w/ this uam */
975 len = (unsigned char) *ibuf++;
976 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
977 return AFPERR_BADUAM;
980 if ((len + 1) & 1) /* pad byte */
983 if ( afp_version < 30) {
984 len = (unsigned char) *ibuf++;
985 if ( len > sizeof(username) - 1) {
988 memcpy(username, ibuf, len);
989 username[ len ] = '\0';
991 if ((len + 1) & 1) /* pad byte */
994 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
995 if ( ibuf[0] != '\0' || ibuf[1] != '\0')
998 len = MIN(sizeof(username), strlen(obj->username));
999 memcpy(username, obj->username, len);
1000 username[ len ] = '\0';
1004 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
1006 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
1007 return AFPERR_PARAM;
1009 /* send it off to the uam. we really don't use ibuflen right now. */
1010 if (ibuflen < (size_t)(ibuf - start))
1011 return AFPERR_PARAM;
1013 ibuflen -= (ibuf - start);
1014 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
1016 LOG(log_info, logtype_afpd, "password change %s.",
1017 (ret == AFPERR_AUTHCONT) ? "continued" :
1018 (ret ? "failed" : "succeeded"));
1019 if ( ret == AFP_OK )
1027 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1034 LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
1039 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
1040 memcpy(&bitmap, ibuf, sizeof(bitmap));
1041 bitmap = ntohs(bitmap);
1043 /* deal with error cases. we don't have to worry about
1044 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
1047 return AFPERR_PARAM;
1048 if ((bitmap & USERIBIT_ALL) != bitmap)
1049 return AFPERR_BITMAP;
1051 /* remember place where we store the possibly modified bitmap later */
1052 memcpy(rbuf, ibuf, sizeof(bitmap));
1054 rbuf += sizeof(bitmap);
1055 *rbuflen = sizeof(bitmap);
1057 /* copy the user/group info */
1058 if (bitmap & USERIBIT_USER) {
1059 id = htonl(geteuid());
1060 memcpy(rbuf, &id, sizeof(id));
1062 *rbuflen += sizeof(id);
1065 if (bitmap & USERIBIT_GROUP) {
1066 id = htonl(getegid());
1067 memcpy(rbuf, &id, sizeof(id));
1069 *rbuflen += sizeof(id);
1072 if (bitmap & USERIBIT_UUID) {
1073 if ( ! (obj->options.flags & OPTION_UUID)) {
1074 bitmap &= ~USERIBIT_UUID;
1075 bitmap = htons(bitmap);
1076 memcpy(bitmapp, &bitmap, sizeof(bitmap));
1078 LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
1081 ret = getuuidfromname( obj->username, UUID_USER, uuid);
1083 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
1084 return AFPERR_NOITEM;
1086 LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
1088 memcpy(rbuf, uuid, UUID_BINSIZE);
1089 rbuf += UUID_BINSIZE;
1090 *rbuflen += UUID_BINSIZE;
1094 LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
1098 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
1099 (((type) == UAM_SERVER_CHANGEPW) ? \
1100 &uam_changepw : NULL))
1102 /* just do a linked list search. this could be sped up with a hashed
1103 * list, but i doubt anyone's going to have enough uams to matter. */
1104 struct uam_obj *auth_uamfind(const int type, const char *name,
1107 struct uam_obj *prev, *start;
1109 if (!name || !(start = UAM_LIST(type)))
1113 while ((prev = prev->uam_prev) != start)
1114 if (strndiacasecmp(prev->uam_name, name, len) == 0)
1120 int auth_register(const int type, struct uam_obj *uam)
1122 struct uam_obj *start;
1124 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1127 if (!(start = UAM_LIST(type)))
1128 return 1; /* we don't know what to do with it, caller must free it */
1130 uam_attach(start, uam);
1134 /* load all of the modules */
1135 int auth_load(const char *path, const char *list)
1137 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
1138 struct uam_mod *mod;
1142 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1145 strlcpy(buf, list, sizeof(buf));
1146 if ((p = strtok(buf, ",")) == NULL)
1150 if (name[len - 1] != '/') {
1156 strlcpy(name + len, p, sizeof(name) - len);
1157 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1159 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1161 if (stat(name, &st) == 0) {
1162 if ((mod = uam_load(name, p))) {
1163 uam_attach(&uam_modules, mod);
1164 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1166 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1169 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1171 p = strtok(NULL, ",");
1177 /* get rid of all of the uams */
1178 void auth_unload(void)
1180 struct uam_mod *mod, *prev, *start = &uam_modules;
1182 prev = start->uam_prev;
1183 while ((mod = prev) != start) {
1184 prev = prev->uam_prev;