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 <netatalk/endian.h>
18 #include <atalk/afp.h>
19 #include <atalk/compat.h>
20 #include <atalk/util.h>
30 #include <arpa/inet.h>
34 extern void afp_get_cmdline( int *ac, char ***av );
37 #include <atalk/logger.h>
38 #include <atalk/server_ipc.h>
39 #include <atalk/uuid.h>
53 static int afp_version_index;
57 #if defined( sun ) && !defined( __svr4__ ) || defined( ultrix )
60 #define GROUPS_SIZE sizeof(int)
62 #else /* sun __svr4__ ultrix */
65 #define GROUPS_SIZE sizeof(gid_t)
66 #endif /* sun ultrix */
71 * These numbers are scattered throughout the code.
73 static struct afp_versions afp_versions[] = {
75 { "AFPVersion 1.1", 11 },
76 { "AFPVersion 2.0", 20 },
77 { "AFPVersion 2.1", 21 },
86 static struct uam_mod uam_modules = {NULL, NULL, &uam_modules, &uam_modules};
87 static struct uam_obj uam_login = {"", "", 0, {{NULL, NULL, NULL, NULL }}, &uam_login,
89 static struct uam_obj uam_changepw = {"", "", 0, {{NULL, NULL, NULL, NULL}}, &uam_changepw,
92 static struct uam_obj *afp_uam = NULL;
95 void status_versions( char *data, const ASP asp, const DSI *dsi)
99 int len, num, i, count = 0;
101 memcpy(&status, start + AFPSTATUS_VERSOFF, sizeof(status));
102 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ] );
104 for ( i = 0; i < num; i++ ) {
106 if ( !asp && (afp_versions[ i ].av_number <= 21)) continue;
107 #endif /* ! NO_DDP */
108 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
111 data += ntohs( status );
114 for ( i = 0; i < num; i++ ) {
116 if ( !asp && (afp_versions[ i ].av_number <= 21)) continue;
117 #endif /* ! NO_DDP */
118 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
119 len = strlen( afp_versions[ i ].av_name );
121 memcpy( data, afp_versions[ i ].av_name , len );
124 status = htons( data - start );
125 memcpy(start + AFPSTATUS_UAMSOFF, &status, sizeof(status));
128 void status_uams(char *data, const char *authlist)
132 struct uam_obj *uams;
135 memcpy(&status, start + AFPSTATUS_UAMSOFF, sizeof(status));
137 while ((uams = uams->uam_prev) != &uam_login) {
138 if (strstr(authlist, uams->uam_path))
142 data += ntohs( status );
144 while ((uams = uams->uam_prev) != &uam_login) {
145 if (strstr(authlist, uams->uam_path)) {
146 LOG(log_info, logtype_afpd, "uam: \"%s\" available", uams->uam_name);
147 len = strlen( uams->uam_name);
149 memcpy( data, uams->uam_name, len );
155 status = htons(data - start);
156 memcpy(start + AFPSTATUS_ICONOFF, &status, sizeof(status));
159 /* handle errors by closing the connection. this is only needed
160 * by the afp_* functions. */
161 static int send_reply(const AFPObj *obj, const int err)
163 if ((err == AFP_OK) || (err == AFPERR_AUTHCONT))
166 obj->reply(obj->handle, err);
172 static int afp_errpwdexpired(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
173 char *rbuf _U_, size_t *rbuflen)
176 return AFPERR_PWDEXPR;
179 static int afp_null_nolog(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
180 char *rbuf _U_, size_t *rbuflen)
183 return( AFPERR_NOOP );
186 static int set_auth_switch(int expired)
192 * BF: expired password handling
193 * to allow the user to change his/her password we have to allow login
194 * but every following call except for FPChangePassword will be thrown
195 * away with an AFPERR_PWDEXPR error. (thanks to Leland Wallace from Apple
196 * for clarifying this)
199 for (i=0; i<=0xff; i++) {
200 uam_afpserver_action(i, UAM_AFPSERVER_PREAUTH, afp_errpwdexpired, NULL);
202 uam_afpserver_action(AFP_LOGOUT, UAM_AFPSERVER_PREAUTH, afp_logout, NULL);
203 uam_afpserver_action(AFP_CHANGEPW, UAM_AFPSERVER_PREAUTH, afp_changepw, NULL);
206 afp_switch = postauth_switch;
207 switch (afp_version) {
212 uam_afpserver_action(AFP_GETACL, UAM_AFPSERVER_POSTAUTH, afp_getacl, NULL);
213 uam_afpserver_action(AFP_SETACL, UAM_AFPSERVER_POSTAUTH, afp_setacl, NULL);
214 uam_afpserver_action(AFP_ACCESS, UAM_AFPSERVER_POSTAUTH, afp_access, NULL);
215 #endif /* HAVE_ACLS */
216 uam_afpserver_action(AFP_GETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_getextattr, NULL);
217 uam_afpserver_action(AFP_SETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_setextattr, NULL);
218 uam_afpserver_action(AFP_REMOVEATTR, UAM_AFPSERVER_POSTAUTH, afp_remextattr, NULL);
219 uam_afpserver_action(AFP_LISTEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_listextattr, NULL);
222 uam_afpserver_action(AFP_SYNCDIR, UAM_AFPSERVER_POSTAUTH, afp_syncdir, NULL);
223 uam_afpserver_action(AFP_SYNCFORK, UAM_AFPSERVER_POSTAUTH, afp_syncfork, NULL);
224 uam_afpserver_action(AFP_SPOTLIGHT_PRIVATE, UAM_AFPSERVER_POSTAUTH, afp_null_nolog, NULL);
225 uam_afpserver_action(AFP_ENUMERATE_EXT2, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext2, NULL);
228 uam_afpserver_action(AFP_ENUMERATE_EXT, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext, NULL);
229 uam_afpserver_action(AFP_BYTELOCK_EXT, UAM_AFPSERVER_POSTAUTH, afp_bytelock_ext, NULL);
230 /* catsearch_ext uses the same packet as catsearch FIXME double check this, it wasn't true for enue
232 uam_afpserver_action(AFP_CATSEARCH_EXT, UAM_AFPSERVER_POSTAUTH, afp_catsearch_ext, NULL);
233 uam_afpserver_action(AFP_GETSESSTOKEN, UAM_AFPSERVER_POSTAUTH, afp_getsession, NULL);
234 uam_afpserver_action(AFP_READ_EXT, UAM_AFPSERVER_POSTAUTH, afp_read_ext, NULL);
235 uam_afpserver_action(AFP_WRITE_EXT, UAM_AFPSERVER_POSTAUTH, afp_write_ext, NULL);
236 uam_afpserver_action(AFP_DISCTOLDSESS, UAM_AFPSERVER_POSTAUTH, afp_disconnect, NULL);
240 * If first connection to a server is done in classic AFP2.2 version is used
241 * but OSX uses AFP3.x FPzzz command !
243 uam_afpserver_action(AFP_ZZZ, UAM_AFPSERVER_POSTAUTH, afp_zzz, NULL);
251 static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void), int expired)
255 #endif /* ADMIN_GRP */
258 set_processname("afpd");
261 if ( pwd->pw_uid == 0 ) { /* don't allow root login */
262 LOG(log_error, logtype_afpd, "login: root login denied!" );
263 return AFPERR_NOTAUTH;
266 LOG(log_note, logtype_afpd, "%s Login by %s",
267 afp_versions[afp_version_index].av_name, pwd->pw_name);
270 if (obj->proto == AFPPROTO_ASP) {
271 ASP asp = obj->handle;
272 int addr_net = ntohs( asp->asp_sat.sat_addr.s_net );
273 int addr_node = asp->asp_sat.sat_addr.s_node;
275 if (obj->options.authprintdir) {
276 if(addr_net && addr_node) { /* Do we have a valid Appletalk address? */
279 int mypid = getpid();
280 struct stat stat_buf;
282 sprintf(nodename, "%s/net%d.%dnode%d", obj->options.authprintdir,
283 addr_net / 256, addr_net % 256, addr_node);
284 LOG(log_info, logtype_afpd, "registering %s (uid %d) on %u.%u as %s",
285 pwd->pw_name, pwd->pw_uid, addr_net, addr_node, nodename);
287 if (stat(nodename, &stat_buf) == 0) { /* file exists */
288 if (S_ISREG(stat_buf.st_mode)) { /* normal file */
290 fp = fopen(nodename, "w");
291 fprintf(fp, "%s:%d\n", pwd->pw_name, mypid);
293 chown( nodename, pwd->pw_uid, -1 );
294 } else { /* somebody is messing with us */
295 LOG(log_error, logtype_afpd, "print authfile %s is not a normal file, it will not be modified", nodename );
297 } else { /* file 'nodename' does not exist */
298 fp = fopen(nodename, "w");
299 fprintf(fp, "%s:%d\n", pwd->pw_name, mypid);
301 chown( nodename, pwd->pw_uid, -1 );
303 } /* if (addr_net && addr_node ) */
304 } /* if (options->authprintdir) */
305 } /* if (obj->proto == AFPPROTO_ASP) */
308 if (initgroups( pwd->pw_name, pwd->pw_gid ) < 0) {
310 LOG(log_info, logtype_afpd, "running with uid %d", geteuid());
311 #else /* RUN_AS_USER */
312 LOG(log_error, logtype_afpd, "login: %s", strerror(errno));
313 return AFPERR_BADUAM;
314 #endif /* RUN_AS_USER */
318 /* Basically if the user is in the admin group, we stay root */
320 if (( ngroups = getgroups( 0, NULL )) < 0 ) {
321 LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
322 return AFPERR_BADUAM;
325 if ( NULL == (groups = calloc(ngroups, GROUPS_SIZE)) ) {
326 LOG(log_error, logtype_afpd, "login: %s calloc: %d", ngroups);
327 return AFPERR_BADUAM;
330 if (( ngroups = getgroups( ngroups, groups )) < 0 ) {
331 LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
332 return AFPERR_BADUAM;
336 LOG(log_debug, logtype_afpd, "obj->options.admingid == %d", obj->options.admingid);
338 if (obj->options.admingid != 0) {
340 for (i = 0; i < ngroups; i++) {
341 if (groups[i] == obj->options.admingid) admin = 1;
346 LOG(log_info, logtype_afpd, "admin login -- %s", pwd->pw_name );
349 #endif /* ADMIN_GRP */
352 struct DSI *dsi = obj->handle;
359 afp_get_cmdline( &argc, &argv );
361 hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
362 sizeof( struct in_addr ),
363 dsi->client.sin_family );
366 clientname = hp->h_name;
368 clientname = inet_ntoa( dsi->client.sin_addr );
370 sprintf( hostname, "%s@%s", pwd->pw_name, clientname );
372 if( sia_become_user( NULL, argc, argv, hostname, pwd->pw_name,
373 NULL, FALSE, NULL, NULL,
374 SIA_BEU_REALLOGIN ) != SIASUCCESS )
375 return AFPERR_BADUAM;
377 LOG(log_info, logtype_afpd, "session from %s (%s)", hostname,
378 inet_ntoa( dsi->client.sin_addr ) );
380 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
381 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
382 return AFPERR_BADUAM;
386 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
387 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
388 return AFPERR_BADUAM;
393 #define GROUPSTR_BUFSIZE 1024
394 char groupsstr[GROUPSTR_BUFSIZE];
396 int j = GROUPSTR_BUFSIZE;
398 int n = snprintf(groupsstr, GROUPSTR_BUFSIZE, "%u", groups[0]);
402 for (int i = 1; i < ngroups; i++) {
403 n = snprintf(s, j, ", %u", groups[i]);
406 LOG(log_debug, logtype_afpd, "login: group string buffer overflow");
412 LOG(log_debug, logtype_afpd, "login: %u supplementary groups: %s", ngroups, groupsstr);
415 /* There's probably a better way to do this, but for now, we just play root */
420 #endif /* ADMIN_GRP */
423 set_auth_switch(expired);
424 /* save our euid, we need it for preexec_close */
425 obj->uid = geteuid();
426 obj->logout = logout;
431 /* ---------------------- */
432 int afp_zzz ( /* Function 122 */
434 char *ibuf _U_, size_t ibuflen _U_,
435 char *rbuf, size_t *rbuflen)
441 retdata = obj->options.sleep /120;
445 *rbuflen = sizeof(retdata);
446 retdata = htonl(retdata);
447 memcpy(rbuf, &retdata, sizeof(retdata));
450 rbuf += sizeof(retdata);
454 /* ---------------------- */
455 static int create_session_token(AFPObj *obj)
459 /* use 8 bytes for token as OSX, don't know if it helps */
460 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
461 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
465 if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
468 memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
469 obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
471 memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
476 static int create_session_key(AFPObj *obj)
478 /* create session key */
479 if (obj->sinfo.sessionkey == NULL) {
480 if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
482 uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
483 obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
489 /* ---------------------- */
492 char *ibuf, size_t ibuflen,
493 char *rbuf, size_t *rbuflen)
505 if (ibuflen < 2 + sizeof(type)) {
512 memcpy(&type, ibuf, sizeof(type));
514 ibuf += sizeof(type);
515 ibuflen -= sizeof(type);
517 if ( obj->sinfo.sessiontoken == NULL ) {
518 if ( create_session_token( obj ) )
526 case 0: /* old version ?*/
527 tklen = obj->sinfo.sessiontoken_len;
528 token = obj->sinfo.sessiontoken;
530 case 1: /* disconnect */
531 case 2: /* reconnect update id */
532 if (ibuflen >= sizeof(idlen)) {
533 memcpy(&idlen, ibuf, sizeof(idlen));
534 idlen = ntohl(idlen);
535 ibuf += sizeof(idlen);
536 ibuflen -= sizeof(idlen);
537 if (ibuflen < idlen) {
540 /* memcpy (id, ibuf, idlen) */
541 tklen = obj->sinfo.sessiontoken_len;
542 token = obj->sinfo.sessiontoken;
549 memcpy( &idlen, ibuf, sizeof(idlen));
550 idlen = ntohl(idlen);
551 ibuf += sizeof(idlen);
552 ibuflen -= sizeof(idlen);
553 ibuf += sizeof(boottime);
554 ibuflen -= sizeof(boottime);
555 if (ibuflen < idlen || idlen > (90-10)) {
558 ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p);
559 tklen = obj->sinfo.sessiontoken_len;
560 token = obj->sinfo.sessiontoken;
563 case 8: /* Panther Kerberos Token */
564 tklen = obj->sinfo.cryptedkey_len;
565 token = obj->sinfo.cryptedkey;
577 memcpy(rbuf, &tp, sizeof(tklen));
578 rbuf += sizeof(tklen);
579 *rbuflen += sizeof(tklen);
581 memcpy(rbuf, token, tklen);
587 /* ---------------------- */
588 int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
590 DSI *dsi = (DSI *)obj->handle;
600 /* check for guest user */
601 if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
606 memcpy(&type, ibuf, sizeof(type));
608 ibuf += sizeof(type);
610 memcpy(&tklen, ibuf, sizeof(tklen));
611 tklen = ntohl(tklen);
612 ibuf += sizeof(tklen);
614 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
615 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
618 if (tklen != SESSIONTOKEN_LEN) {
621 tklen = sizeof(pid_t);
622 memcpy(&token, ibuf, tklen);
624 /* our stuff is pid + zero pad */
626 for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
632 LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
633 dsi->flags |= DSI_RECONINPROG;
635 /* Deactivate tickle timer */
636 const struct itimerval none = {{0, 0}, {0, 0}};
637 setitimer(ITIMER_REAL, &none, NULL);
639 /* check for old session, possibly transfering session from here to there */
640 if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) == -1)
642 /* write uint16_t DSI request ID */
643 if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
644 LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
647 /* now send our connected AFP client socket */
648 if (send_fd(obj->ipc_fd, dsi->socket) != 0)
650 /* Now see what happens: either afpd master sends us SIGTERM because our session */
651 /* has been transfered to a old disconnected session, or we continue */
654 if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
655 /* Reconnect succeeded, we exit now after sleeping some more */
656 sleep(2); /* sleep some more to give the recon. session time */
657 LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
662 /* Reinstall tickle timer */
663 setitimer(ITIMER_REAL, &dsi->timer, NULL);
665 LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
669 /* ---------------------- */
670 static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
674 if (!len || len > ibuflen)
675 return AFPERR_BADVERS;
677 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
678 for ( i = 0; i < num; i++ ) {
679 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
680 afp_version = afp_versions[ i ].av_number;
681 afp_version_index = i;
685 if ( i == num ) /* An inappropo version */
686 return AFPERR_BADVERS ;
688 if (afp_version >= 30 && obj->proto != AFPPROTO_DSI)
689 return AFPERR_BADVERS ;
692 if (afp_version >= 30 && sizeof(off_t) != 8) {
693 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
694 return AFPERR_BADVERS ;
700 /* ---------------------- */
701 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
703 struct passwd *pwd = NULL;
710 return send_reply(obj, AFPERR_SHUTDOWN );
713 return send_reply(obj, AFPERR_BADVERS );
716 len = (unsigned char) *ibuf++;
719 i = get_version(obj, ibuf, ibuflen, len);
721 return send_reply(obj, i );
724 return send_reply(obj, AFPERR_BADUAM);
729 len = (unsigned char) *ibuf++;
732 if (!len || len > ibuflen)
733 return send_reply(obj, AFPERR_BADUAM);
735 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
736 return send_reply(obj, AFPERR_BADUAM);
740 if (AFP_OK != (i = create_session_key(obj)) )
741 return send_reply(obj, i);
743 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
745 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
746 return send_reply(obj, i);
748 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
751 /* ---------------------- */
752 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
754 struct passwd *pwd = NULL;
764 return send_reply(obj, AFPERR_SHUTDOWN );
767 return send_reply(obj, AFPERR_BADVERS );
773 len = (unsigned char) *ibuf;
777 i = get_version(obj, ibuf, ibuflen, len);
779 return send_reply(obj, i );
782 return send_reply(obj, AFPERR_BADUAM);
787 len = (unsigned char) *ibuf;
791 if (!len || len > ibuflen)
792 return send_reply(obj, AFPERR_BADUAM);
794 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
795 return send_reply(obj, AFPERR_BADUAM);
799 if (!afp_uam->u.uam_login.login_ext) {
800 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
801 return send_reply(obj, AFPERR_BADUAM);
804 if (ibuflen <= 1 +sizeof(len16))
805 return send_reply(obj, AFPERR_PARAM);
811 return send_reply(obj, AFPERR_PARAM);
813 memcpy(&len16, ibuf, sizeof(len16));
814 ibuf += sizeof(len16);
815 ibuflen -= sizeof(len16);
818 return send_reply(obj, AFPERR_PARAM);
822 /* directory service name */
824 return send_reply(obj, AFPERR_PARAM);
833 return send_reply(obj, AFPERR_PARAM);
834 len = (unsigned char) *ibuf;
839 /* With "No User Authen" it is equal */
840 if (ibuflen < sizeof(len16))
841 return send_reply(obj, AFPERR_PARAM);
842 memcpy(&len16, ibuf, sizeof(len16));
843 ibuf += sizeof(len16);
844 ibuflen -= sizeof(len16);
848 return send_reply(obj, AFPERR_PARAM);
852 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
853 return send_reply(obj, AFPERR_PARAM);
860 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
865 if (AFP_OK != (i = create_session_key(obj)) ) {
866 return send_reply(obj, i);
869 /* FIXME user name are in UTF8 */
870 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
872 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
873 return send_reply(obj, i);
875 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
878 /* ---------------------- */
879 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
881 struct passwd *pwd = NULL;
884 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
886 return send_reply(obj, AFPERR_NOTAUTH );
889 ibuf += 2; ibuflen -= 2;
890 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
892 if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
893 return send_reply(obj, err);
895 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
899 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen _U_)
901 LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
909 /* change password --
910 * NOTE: an FPLogin must already have completed successfully for this
911 * to work. this also does a little pre-processing before it hands
914 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
916 char username[MACFILELEN + 1], *start = ibuf;
925 /* check if password change is allowed, OS-X ignores the flag.
926 * we shouldn't trust the client on this anyway.
927 * not sure about the "right" error code, NOOP for now */
928 if (!(obj->options.passwdbits & PASSWD_SET))
931 /* make sure we can deal w/ this uam */
932 len = (unsigned char) *ibuf++;
933 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
934 return AFPERR_BADUAM;
937 if ((len + 1) & 1) /* pad byte */
940 if ( afp_version < 30) {
941 len = (unsigned char) *ibuf++;
942 if ( len > sizeof(username) - 1) {
945 memcpy(username, ibuf, len);
946 username[ len ] = '\0';
948 if ((len + 1) & 1) /* pad byte */
951 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
952 if ( ibuf[0] != '\0' || ibuf[1] != '\0')
955 len = MIN(sizeof(username), strlen(obj->username));
956 memcpy(username, obj->username, len);
957 username[ len ] = '\0';
961 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
963 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
966 /* send it off to the uam. we really don't use ibuflen right now. */
967 if (ibuflen < (size_t)(ibuf - start))
970 ibuflen -= (ibuf - start);
971 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
973 LOG(log_info, logtype_afpd, "password change %s.",
974 (ret == AFPERR_AUTHCONT) ? "continued" :
975 (ret ? "failed" : "succeeded"));
984 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
991 LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
996 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
997 memcpy(&bitmap, ibuf, sizeof(bitmap));
998 bitmap = ntohs(bitmap);
1000 /* deal with error cases. we don't have to worry about
1001 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
1004 return AFPERR_PARAM;
1005 if ((bitmap & USERIBIT_ALL) != bitmap)
1006 return AFPERR_BITMAP;
1008 /* remember place where we store the possibly modified bitmap later */
1009 memcpy(rbuf, ibuf, sizeof(bitmap));
1011 rbuf += sizeof(bitmap);
1012 *rbuflen = sizeof(bitmap);
1014 /* copy the user/group info */
1015 if (bitmap & USERIBIT_USER) {
1016 id = htonl(geteuid());
1017 memcpy(rbuf, &id, sizeof(id));
1019 *rbuflen += sizeof(id);
1022 if (bitmap & USERIBIT_GROUP) {
1023 id = htonl(getegid());
1024 memcpy(rbuf, &id, sizeof(id));
1026 *rbuflen += sizeof(id);
1029 if (bitmap & USERIBIT_UUID) {
1030 if ( ! (obj->options.flags & OPTION_UUID)) {
1031 bitmap &= ~USERIBIT_UUID;
1032 bitmap = htons(bitmap);
1033 memcpy(bitmapp, &bitmap, sizeof(bitmap));
1035 LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
1038 ret = getuuidfromname( obj->username, UUID_USER, uuid);
1040 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
1041 return AFPERR_NOITEM;
1043 LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
1045 memcpy(rbuf, uuid, UUID_BINSIZE);
1046 rbuf += UUID_BINSIZE;
1047 *rbuflen += UUID_BINSIZE;
1051 LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
1055 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
1056 (((type) == UAM_SERVER_CHANGEPW) ? \
1057 &uam_changepw : NULL))
1059 /* just do a linked list search. this could be sped up with a hashed
1060 * list, but i doubt anyone's going to have enough uams to matter. */
1061 struct uam_obj *auth_uamfind(const int type, const char *name,
1064 struct uam_obj *prev, *start;
1066 if (!name || !(start = UAM_LIST(type)))
1070 while ((prev = prev->uam_prev) != start)
1071 if (strndiacasecmp(prev->uam_name, name, len) == 0)
1077 int auth_register(const int type, struct uam_obj *uam)
1079 struct uam_obj *start;
1081 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1084 if (!(start = UAM_LIST(type)))
1085 return 1; /* we don't know what to do with it, caller must free it */
1087 uam_attach(start, uam);
1091 /* load all of the modules */
1092 int auth_load(const char *path, const char *list)
1094 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
1095 struct uam_mod *mod;
1099 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1102 strlcpy(buf, list, sizeof(buf));
1103 if ((p = strtok(buf, ",")) == NULL)
1107 if (name[len - 1] != '/') {
1113 strlcpy(name + len, p, sizeof(name) - len);
1114 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1116 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1118 if (stat(name, &st) == 0) {
1119 if ((mod = uam_load(name, p))) {
1120 uam_attach(&uam_modules, mod);
1121 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1123 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1126 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1128 p = strtok(NULL, ",");
1134 /* get rid of all of the uams */
1135 void auth_unload(void)
1137 struct uam_mod *mod, *prev, *start = &uam_modules;
1139 prev = start->uam_prev;
1140 while ((mod = prev) != start) {
1141 prev = prev->uam_prev;