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>
42 #include <atalk/globals.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 */
72 static struct uam_mod uam_modules = {NULL, NULL, &uam_modules, &uam_modules};
73 static struct uam_obj uam_login = {"", "", 0, {{NULL, NULL, NULL, NULL }}, &uam_login,
75 static struct uam_obj uam_changepw = {"", "", 0, {{NULL, NULL, NULL, NULL}}, &uam_changepw,
78 static struct uam_obj *afp_uam = NULL;
81 void status_versions(char *data,
89 int len, num, i, count = 0;
91 memcpy(&status, start + AFPSTATUS_VERSOFF, sizeof(status));
92 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ] );
94 for ( i = 0; i < num; i++ ) {
96 if ( !asp && (afp_versions[ i ].av_number <= 21)) continue;
98 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
101 data += ntohs( status );
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;
109 len = strlen( afp_versions[ i ].av_name );
111 memcpy( data, afp_versions[ i ].av_name , len );
114 status = htons( data - start );
115 memcpy(start + AFPSTATUS_UAMSOFF, &status, sizeof(status));
118 void status_uams(char *data, const char *authlist)
122 struct uam_obj *uams;
125 memcpy(&status, start + AFPSTATUS_UAMSOFF, sizeof(status));
127 while ((uams = uams->uam_prev) != &uam_login) {
128 if (strstr(authlist, uams->uam_path))
132 data += ntohs( status );
134 while ((uams = uams->uam_prev) != &uam_login) {
135 if (strstr(authlist, uams->uam_path)) {
136 LOG(log_info, logtype_afpd, "uam: \"%s\" available", uams->uam_name);
137 len = strlen( uams->uam_name);
139 memcpy( data, uams->uam_name, len );
145 status = htons(data - start);
146 memcpy(start + AFPSTATUS_ICONOFF, &status, sizeof(status));
149 /* handle errors by closing the connection. this is only needed
150 * by the afp_* functions. */
151 static int send_reply(const AFPObj *obj, const int err)
153 if ((err == AFP_OK) || (err == AFPERR_AUTHCONT))
156 obj->reply(obj->handle, err);
162 static int afp_errpwdexpired(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
163 char *rbuf _U_, size_t *rbuflen)
166 return AFPERR_PWDEXPR;
169 static int afp_null_nolog(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
170 char *rbuf _U_, size_t *rbuflen)
173 return( AFPERR_NOOP );
176 static int set_auth_switch(int expired)
182 * BF: expired password handling
183 * to allow the user to change his/her password we have to allow login
184 * but every following call except for FPChangePassword will be thrown
185 * away with an AFPERR_PWDEXPR error. (thanks to Leland Wallace from Apple
186 * for clarifying this)
189 for (i=0; i<=0xff; i++) {
190 uam_afpserver_action(i, UAM_AFPSERVER_PREAUTH, afp_errpwdexpired, NULL);
192 uam_afpserver_action(AFP_LOGOUT, UAM_AFPSERVER_PREAUTH, afp_logout, NULL);
193 uam_afpserver_action(AFP_CHANGEPW, UAM_AFPSERVER_PREAUTH, afp_changepw, NULL);
196 afp_switch = postauth_switch;
197 switch (afp_version) {
202 uam_afpserver_action(AFP_GETACL, UAM_AFPSERVER_POSTAUTH, afp_getacl, NULL);
203 uam_afpserver_action(AFP_SETACL, UAM_AFPSERVER_POSTAUTH, afp_setacl, NULL);
204 uam_afpserver_action(AFP_ACCESS, UAM_AFPSERVER_POSTAUTH, afp_access, NULL);
205 #endif /* HAVE_ACLS */
206 uam_afpserver_action(AFP_GETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_getextattr, NULL);
207 uam_afpserver_action(AFP_SETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_setextattr, NULL);
208 uam_afpserver_action(AFP_REMOVEATTR, UAM_AFPSERVER_POSTAUTH, afp_remextattr, NULL);
209 uam_afpserver_action(AFP_LISTEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_listextattr, NULL);
212 uam_afpserver_action(AFP_SYNCDIR, UAM_AFPSERVER_POSTAUTH, afp_syncdir, NULL);
213 uam_afpserver_action(AFP_SYNCFORK, UAM_AFPSERVER_POSTAUTH, afp_syncfork, NULL);
214 uam_afpserver_action(AFP_SPOTLIGHT_PRIVATE, UAM_AFPSERVER_POSTAUTH, afp_null_nolog, NULL);
215 uam_afpserver_action(AFP_ENUMERATE_EXT2, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext2, NULL);
218 uam_afpserver_action(AFP_ENUMERATE_EXT, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext, NULL);
219 uam_afpserver_action(AFP_BYTELOCK_EXT, UAM_AFPSERVER_POSTAUTH, afp_bytelock_ext, NULL);
220 /* catsearch_ext uses the same packet as catsearch FIXME double check this, it wasn't true for enue
222 uam_afpserver_action(AFP_CATSEARCH_EXT, UAM_AFPSERVER_POSTAUTH, afp_catsearch_ext, NULL);
223 uam_afpserver_action(AFP_GETSESSTOKEN, UAM_AFPSERVER_POSTAUTH, afp_getsession, NULL);
224 uam_afpserver_action(AFP_READ_EXT, UAM_AFPSERVER_POSTAUTH, afp_read_ext, NULL);
225 uam_afpserver_action(AFP_WRITE_EXT, UAM_AFPSERVER_POSTAUTH, afp_write_ext, NULL);
226 uam_afpserver_action(AFP_DISCTOLDSESS, UAM_AFPSERVER_POSTAUTH, afp_disconnect, NULL);
230 * If first connection to a server is done in classic AFP2.2 version is used
231 * but OSX uses AFP3.x FPzzz command !
233 uam_afpserver_action(AFP_ZZZ, UAM_AFPSERVER_POSTAUTH, afp_zzz, NULL);
241 #define GROUPSTR_BUFSIZE 1024
242 static const char *print_groups(int ngroups, gid_t *groups)
244 static char groupsstr[GROUPSTR_BUFSIZE];
251 for (i = 0; (i < ngroups) && (s < &groupsstr[GROUPSTR_BUFSIZE]); i++) {
252 s += snprintf(s, &groupsstr[GROUPSTR_BUFSIZE] - s, " %u", groups[i]);
258 static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void), int expired)
262 #endif /* ADMIN_GRP */
264 if ( pwd->pw_uid == 0 ) { /* don't allow root login */
265 LOG(log_error, logtype_afpd, "login: root login denied!" );
266 return AFPERR_NOTAUTH;
269 LOG(log_note, logtype_afpd, "%s Login by %s",
270 afp_versions[afp_version_index].av_name, pwd->pw_name);
273 if (obj->proto == AFPPROTO_ASP) {
274 ASP asp = obj->handle;
275 int addr_net = ntohs( asp->asp_sat.sat_addr.s_net );
276 int addr_node = asp->asp_sat.sat_addr.s_node;
278 if (obj->options.authprintdir) {
279 if(addr_net && addr_node) { /* Do we have a valid Appletalk address? */
282 int mypid = getpid();
283 struct stat stat_buf;
285 sprintf(nodename, "%s/net%d.%dnode%d", obj->options.authprintdir,
286 addr_net / 256, addr_net % 256, addr_node);
287 LOG(log_info, logtype_afpd, "registering %s (uid %d) on %u.%u as %s",
288 pwd->pw_name, pwd->pw_uid, addr_net, addr_node, nodename);
290 if (stat(nodename, &stat_buf) == 0) { /* file exists */
291 if (S_ISREG(stat_buf.st_mode)) { /* normal file */
293 fp = fopen(nodename, "w");
294 fprintf(fp, "%s:%d\n", pwd->pw_name, mypid);
296 chown( nodename, pwd->pw_uid, -1 );
297 } else { /* somebody is messing with us */
298 LOG(log_error, logtype_afpd, "print authfile %s is not a normal file, it will not be modified", nodename );
300 } else { /* file 'nodename' does not exist */
301 fp = fopen(nodename, "w");
302 fprintf(fp, "%s:%d\n", pwd->pw_name, mypid);
304 chown( nodename, pwd->pw_uid, -1 );
306 } /* if (addr_net && addr_node ) */
307 } /* if (options->authprintdir) */
308 } /* if (obj->proto == AFPPROTO_ASP) */
311 if (initgroups( pwd->pw_name, pwd->pw_gid ) < 0) {
313 LOG(log_info, logtype_afpd, "running with uid %d", geteuid());
314 #else /* RUN_AS_USER */
315 LOG(log_error, logtype_afpd, "login: %s", strerror(errno));
316 return AFPERR_BADUAM;
317 #endif /* RUN_AS_USER */
321 /* Basically if the user is in the admin group, we stay root */
323 if (( ngroups = getgroups( 0, NULL )) < 0 ) {
324 LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
325 return AFPERR_BADUAM;
328 if ( NULL == (groups = calloc(ngroups, GROUPS_SIZE)) ) {
329 LOG(log_error, logtype_afpd, "login: %s calloc: %d", ngroups);
330 return AFPERR_BADUAM;
333 if (( ngroups = getgroups( ngroups, groups )) < 0 ) {
334 LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
335 return AFPERR_BADUAM;
339 LOG(log_debug, logtype_afpd, "obj->options.admingid == %d", obj->options.admingid);
341 if (obj->options.admingid != 0) {
343 for (i = 0; i < ngroups; i++) {
344 if (groups[i] == obj->options.admingid) admin = 1;
349 LOG(log_info, logtype_afpd, "admin login -- %s", pwd->pw_name );
352 #endif /* ADMIN_GRP */
355 struct DSI *dsi = obj->handle;
362 afp_get_cmdline( &argc, &argv );
364 hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
365 sizeof( struct in_addr ),
366 dsi->client.sin_family );
369 clientname = hp->h_name;
371 clientname = inet_ntoa( dsi->client.sin_addr );
373 sprintf( hostname, "%s@%s", pwd->pw_name, clientname );
375 if( sia_become_user( NULL, argc, argv, hostname, pwd->pw_name,
376 NULL, FALSE, NULL, NULL,
377 SIA_BEU_REALLOGIN ) != SIASUCCESS )
378 return AFPERR_BADUAM;
380 LOG(log_info, logtype_afpd, "session from %s (%s)", hostname,
381 inet_ntoa( dsi->client.sin_addr ) );
383 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
384 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
385 return AFPERR_BADUAM;
389 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
390 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
391 return AFPERR_BADUAM;
395 LOG(log_debug, logtype_afpd, "login: supplementary groups: %s", print_groups(ngroups, groups));
397 /* There's probably a better way to do this, but for now, we just play root */
402 #endif /* ADMIN_GRP */
405 set_auth_switch(expired);
406 /* save our euid, we need it for preexec_close */
407 obj->uid = geteuid();
408 obj->logout = logout;
412 save_uidgid ( &obj->uidgid );
415 /* pam_umask or similar might have changed our umask */
416 (void)umask(obj->options.umask);
421 /* ---------------------- */
422 int afp_zzz(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
425 DSI *dsi = (DSI *)AFPobj->handle;
433 memcpy(&data, ibuf, 4); /* flag */
437 * Possible sleeping states:
438 * 1) normal sleep: DSI_SLEEPING (up to 10.3)
439 * 2) extended sleep: DSI_SLEEPING | DSI_EXTSLEEP (starting with 10.4)
442 if (data & AFPZZZ_EXT_WAKEUP) {
443 /* wakeup request from exetended sleep */
444 if (dsi->flags & DSI_EXTSLEEP) {
445 LOG(log_note, logtype_afpd, "afp_zzz: waking up from extended sleep");
446 dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP);
450 dsi->flags |= DSI_SLEEPING;
451 if (data & AFPZZZ_EXT_SLEEP) {
452 LOG(log_note, logtype_afpd, "afp_zzz: entering extended sleep");
453 dsi->flags |= DSI_EXTSLEEP;
455 LOG(log_note, logtype_afpd, "afp_zzz: entering normal sleep");
460 * According to AFP 3.3 spec we should not return anything,
461 * but eg 10.5.8 server still returns the numbers of hours
462 * the server is keeping the sessino (ie max sleeptime).
464 data = obj->options.sleep / 120; /* hours */
468 *rbuflen = sizeof(data);
470 memcpy(rbuf, &data, sizeof(data));
471 rbuf += sizeof(data);
476 /* ---------------------- */
477 static int create_session_token(AFPObj *obj)
481 /* use 8 bytes for token as OSX, don't know if it helps */
482 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
483 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
487 if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
490 memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
491 obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
493 memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
498 static int create_session_key(AFPObj *obj)
500 /* create session key */
501 if (obj->sinfo.sessionkey == NULL) {
502 if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
504 uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
505 obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
511 /* ---------------------- */
514 char *ibuf, size_t ibuflen,
515 char *rbuf, size_t *rbuflen)
527 if (ibuflen < 2 + sizeof(type)) {
534 memcpy(&type, ibuf, sizeof(type));
536 ibuf += sizeof(type);
537 ibuflen -= sizeof(type);
539 if ( obj->sinfo.sessiontoken == NULL ) {
540 if ( create_session_token( obj ) )
548 case 0: /* old version ?*/
549 tklen = obj->sinfo.sessiontoken_len;
550 token = obj->sinfo.sessiontoken;
552 case 1: /* disconnect */
553 case 2: /* reconnect update id */
554 if (ibuflen >= sizeof(idlen)) {
555 memcpy(&idlen, ibuf, sizeof(idlen));
556 idlen = ntohl(idlen);
557 ibuf += sizeof(idlen);
558 ibuflen -= sizeof(idlen);
559 if (ibuflen < idlen) {
562 /* memcpy (id, ibuf, idlen) */
563 tklen = obj->sinfo.sessiontoken_len;
564 token = obj->sinfo.sessiontoken;
571 memcpy( &idlen, ibuf, sizeof(idlen));
572 idlen = ntohl(idlen);
573 ibuf += sizeof(idlen);
574 ibuflen -= sizeof(idlen);
575 ibuf += sizeof(boottime);
576 ibuflen -= sizeof(boottime);
577 if (ibuflen < idlen || idlen > (90-10)) {
580 if (!obj->sinfo.clientid) {
581 obj->sinfo.clientid = malloc(idlen + 8);
582 memcpy(obj->sinfo.clientid, p, idlen + 8);
583 obj->sinfo.clientid_len = idlen + 8;
585 if (ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p) != 0)
587 tklen = obj->sinfo.sessiontoken_len;
588 token = obj->sinfo.sessiontoken;
591 case 8: /* Panther Kerberos Token */
592 tklen = obj->sinfo.cryptedkey_len;
593 token = obj->sinfo.cryptedkey;
605 memcpy(rbuf, &tp, sizeof(tklen));
606 rbuf += sizeof(tklen);
607 *rbuflen += sizeof(tklen);
609 memcpy(rbuf, token, tklen);
615 /* ---------------------- */
616 int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
618 DSI *dsi = (DSI *)obj->handle;
628 /* check for guest user */
629 if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
634 memcpy(&type, ibuf, sizeof(type));
636 ibuf += sizeof(type);
638 memcpy(&tklen, ibuf, sizeof(tklen));
639 tklen = ntohl(tklen);
640 ibuf += sizeof(tklen);
642 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
643 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
646 if (tklen != SESSIONTOKEN_LEN) {
649 tklen = sizeof(pid_t);
650 memcpy(&token, ibuf, tklen);
652 /* our stuff is pid + zero pad */
654 for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
660 LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
661 dsi->flags |= DSI_RECONINPROG;
663 /* Deactivate tickle timer */
664 const struct itimerval none = {{0, 0}, {0, 0}};
665 setitimer(ITIMER_REAL, &none, NULL);
667 /* check for old session, possibly transfering session from here to there */
668 if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) != 0)
670 /* write uint16_t DSI request ID */
671 if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
672 LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
675 /* now send our connected AFP client socket */
676 if (send_fd(obj->ipc_fd, dsi->socket) != 0)
678 /* Now see what happens: either afpd master sends us SIGTERM because our session */
679 /* has been transfered to a old disconnected session, or we continue */
682 if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
683 /* Reconnect succeeded, we exit now after sleeping some more */
684 sleep(2); /* sleep some more to give the recon. session time */
685 LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
690 /* Reinstall tickle timer */
691 setitimer(ITIMER_REAL, &dsi->timer, NULL);
693 LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
697 /* ---------------------- */
698 static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
702 if (!len || len > ibuflen)
703 return AFPERR_BADVERS;
705 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
706 for ( i = 0; i < num; i++ ) {
707 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
708 afp_version = afp_versions[ i ].av_number;
709 afp_version_index = i;
713 if ( i == num ) /* An inappropo version */
714 return AFPERR_BADVERS ;
716 if (afp_version >= 30 && obj->proto != AFPPROTO_DSI)
717 return AFPERR_BADVERS ;
720 if (afp_version >= 30 && sizeof(off_t) != 8) {
721 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
722 return AFPERR_BADVERS ;
728 /* ---------------------- */
729 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
731 struct passwd *pwd = NULL;
738 return send_reply(obj, AFPERR_SHUTDOWN );
741 return send_reply(obj, AFPERR_BADVERS );
744 len = (unsigned char) *ibuf++;
747 i = get_version(obj, ibuf, ibuflen, len);
749 return send_reply(obj, i );
752 return send_reply(obj, AFPERR_BADUAM);
757 len = (unsigned char) *ibuf++;
760 if (!len || len > ibuflen)
761 return send_reply(obj, AFPERR_BADUAM);
763 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
764 return send_reply(obj, AFPERR_BADUAM);
768 if (AFP_OK != (i = create_session_key(obj)) )
769 return send_reply(obj, i);
771 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
773 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
774 return send_reply(obj, i);
776 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
779 /* ---------------------- */
780 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
782 struct passwd *pwd = NULL;
792 return send_reply(obj, AFPERR_SHUTDOWN );
795 return send_reply(obj, AFPERR_BADVERS );
801 len = (unsigned char) *ibuf;
805 i = get_version(obj, ibuf, ibuflen, len);
807 return send_reply(obj, i );
810 return send_reply(obj, AFPERR_BADUAM);
815 len = (unsigned char) *ibuf;
819 if (!len || len > ibuflen)
820 return send_reply(obj, AFPERR_BADUAM);
822 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
823 return send_reply(obj, AFPERR_BADUAM);
827 if (!afp_uam->u.uam_login.login_ext) {
828 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
829 return send_reply(obj, AFPERR_BADUAM);
832 if (ibuflen <= 1 +sizeof(len16))
833 return send_reply(obj, AFPERR_PARAM);
839 return send_reply(obj, AFPERR_PARAM);
841 memcpy(&len16, ibuf, sizeof(len16));
842 ibuf += sizeof(len16);
843 ibuflen -= sizeof(len16);
846 return send_reply(obj, AFPERR_PARAM);
850 /* directory service name */
852 return send_reply(obj, AFPERR_PARAM);
861 return send_reply(obj, AFPERR_PARAM);
862 len = (unsigned char) *ibuf;
867 /* With "No User Authen" it is equal */
868 if (ibuflen < sizeof(len16))
869 return send_reply(obj, AFPERR_PARAM);
870 memcpy(&len16, ibuf, sizeof(len16));
871 ibuf += sizeof(len16);
872 ibuflen -= sizeof(len16);
876 return send_reply(obj, AFPERR_PARAM);
880 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
881 return send_reply(obj, AFPERR_PARAM);
888 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
893 if (AFP_OK != (i = create_session_key(obj)) ) {
894 return send_reply(obj, i);
897 /* FIXME user name are in UTF8 */
898 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
900 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
901 return send_reply(obj, i);
903 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
906 /* ---------------------- */
907 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
909 struct passwd *pwd = NULL;
912 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
914 return send_reply(obj, AFPERR_NOTAUTH );
917 ibuf += 2; ibuflen -= 2;
918 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
920 if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
921 return send_reply(obj, err);
923 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
927 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
929 DSI *dsi = (DSI *)(obj->handle);
931 LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
932 of_close_all_forks();
934 dsi->flags = DSI_AFP_LOGGED_OUT;
941 /* change password --
942 * NOTE: an FPLogin must already have completed successfully for this
943 * to work. this also does a little pre-processing before it hands
946 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
948 char username[MACFILELEN + 1], *start = ibuf;
957 /* check if password change is allowed, OS-X ignores the flag.
958 * we shouldn't trust the client on this anyway.
959 * not sure about the "right" error code, NOOP for now */
960 if (!(obj->options.passwdbits & PASSWD_SET))
963 /* make sure we can deal w/ this uam */
964 len = (unsigned char) *ibuf++;
965 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
966 return AFPERR_BADUAM;
969 if ((len + 1) & 1) /* pad byte */
972 if ( afp_version < 30) {
973 len = (unsigned char) *ibuf++;
974 if ( len > sizeof(username) - 1) {
977 memcpy(username, ibuf, len);
978 username[ len ] = '\0';
980 if ((len + 1) & 1) /* pad byte */
983 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
984 if ( ibuf[0] != '\0' || ibuf[1] != '\0')
987 len = MIN(sizeof(username), strlen(obj->username));
988 memcpy(username, obj->username, len);
989 username[ len ] = '\0';
993 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
995 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
998 /* send it off to the uam. we really don't use ibuflen right now. */
999 if (ibuflen < (size_t)(ibuf - start))
1000 return AFPERR_PARAM;
1002 ibuflen -= (ibuf - start);
1003 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
1005 LOG(log_info, logtype_afpd, "password change %s.",
1006 (ret == AFPERR_AUTHCONT) ? "continued" :
1007 (ret ? "failed" : "succeeded"));
1008 if ( ret == AFP_OK )
1016 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1023 LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
1028 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
1029 memcpy(&bitmap, ibuf, sizeof(bitmap));
1030 bitmap = ntohs(bitmap);
1032 /* deal with error cases. we don't have to worry about
1033 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
1036 return AFPERR_PARAM;
1037 if ((bitmap & USERIBIT_ALL) != bitmap)
1038 return AFPERR_BITMAP;
1040 /* remember place where we store the possibly modified bitmap later */
1041 memcpy(rbuf, ibuf, sizeof(bitmap));
1043 rbuf += sizeof(bitmap);
1044 *rbuflen = sizeof(bitmap);
1046 /* copy the user/group info */
1047 if (bitmap & USERIBIT_USER) {
1048 id = htonl(geteuid());
1049 memcpy(rbuf, &id, sizeof(id));
1051 *rbuflen += sizeof(id);
1054 if (bitmap & USERIBIT_GROUP) {
1055 id = htonl(getegid());
1056 memcpy(rbuf, &id, sizeof(id));
1058 *rbuflen += sizeof(id);
1061 if (bitmap & USERIBIT_UUID) {
1062 if ( ! (obj->options.flags & OPTION_UUID)) {
1063 bitmap &= ~USERIBIT_UUID;
1064 bitmap = htons(bitmap);
1065 memcpy(bitmapp, &bitmap, sizeof(bitmap));
1067 LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
1070 ret = getuuidfromname( obj->username, UUID_USER, uuid);
1072 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
1073 return AFPERR_NOITEM;
1075 LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
1077 memcpy(rbuf, uuid, UUID_BINSIZE);
1078 rbuf += UUID_BINSIZE;
1079 *rbuflen += UUID_BINSIZE;
1083 LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
1087 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
1088 (((type) == UAM_SERVER_CHANGEPW) ? \
1089 &uam_changepw : NULL))
1091 /* just do a linked list search. this could be sped up with a hashed
1092 * list, but i doubt anyone's going to have enough uams to matter. */
1093 struct uam_obj *auth_uamfind(const int type, const char *name,
1096 struct uam_obj *prev, *start;
1098 if (!name || !(start = UAM_LIST(type)))
1102 while ((prev = prev->uam_prev) != start)
1103 if (strndiacasecmp(prev->uam_name, name, len) == 0)
1109 int auth_register(const int type, struct uam_obj *uam)
1111 struct uam_obj *start;
1113 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1116 if (!(start = UAM_LIST(type)))
1117 return 1; /* we don't know what to do with it, caller must free it */
1119 uam_attach(start, uam);
1123 /* load all of the modules */
1124 int auth_load(const char *path, const char *list)
1126 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
1127 struct uam_mod *mod;
1131 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1134 strlcpy(buf, list, sizeof(buf));
1135 if ((p = strtok(buf, ",")) == NULL)
1139 if (name[len - 1] != '/') {
1145 strlcpy(name + len, p, sizeof(name) - len);
1146 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1148 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1150 if (stat(name, &st) == 0) {
1151 if ((mod = uam_load(name, p))) {
1152 uam_attach(&uam_modules, mod);
1153 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1155 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1158 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1160 p = strtok(NULL, ",");
1166 /* get rid of all of the uams */
1167 void auth_unload(void)
1169 struct uam_mod *mod, *prev, *start = &uam_modules;
1171 prev = start->uam_prev;
1172 while ((mod = prev) != start) {
1173 prev = prev->uam_prev;