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, const ASP asp, const DSI *dsi)
85 int len, num, i, count = 0;
87 memcpy(&status, start + AFPSTATUS_VERSOFF, sizeof(status));
88 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ] );
90 for ( i = 0; i < num; i++ ) {
92 if ( !asp && (afp_versions[ i ].av_number <= 21)) continue;
94 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
97 data += ntohs( status );
100 for ( i = 0; i < num; i++ ) {
102 if ( !asp && (afp_versions[ i ].av_number <= 21)) continue;
103 #endif /* ! NO_DDP */
104 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
105 len = strlen( afp_versions[ i ].av_name );
107 memcpy( data, afp_versions[ i ].av_name , len );
110 status = htons( data - start );
111 memcpy(start + AFPSTATUS_UAMSOFF, &status, sizeof(status));
114 void status_uams(char *data, const char *authlist)
118 struct uam_obj *uams;
121 memcpy(&status, start + AFPSTATUS_UAMSOFF, sizeof(status));
123 while ((uams = uams->uam_prev) != &uam_login) {
124 if (strstr(authlist, uams->uam_path))
128 data += ntohs( status );
130 while ((uams = uams->uam_prev) != &uam_login) {
131 if (strstr(authlist, uams->uam_path)) {
132 LOG(log_info, logtype_afpd, "uam: \"%s\" available", uams->uam_name);
133 len = strlen( uams->uam_name);
135 memcpy( data, uams->uam_name, len );
141 status = htons(data - start);
142 memcpy(start + AFPSTATUS_ICONOFF, &status, sizeof(status));
145 /* handle errors by closing the connection. this is only needed
146 * by the afp_* functions. */
147 static int send_reply(const AFPObj *obj, const int err)
149 if ((err == AFP_OK) || (err == AFPERR_AUTHCONT))
152 obj->reply(obj->handle, err);
158 static int afp_errpwdexpired(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
159 char *rbuf _U_, size_t *rbuflen)
162 return AFPERR_PWDEXPR;
165 static int afp_null_nolog(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
166 char *rbuf _U_, size_t *rbuflen)
169 return( AFPERR_NOOP );
172 static int set_auth_switch(int expired)
178 * BF: expired password handling
179 * to allow the user to change his/her password we have to allow login
180 * but every following call except for FPChangePassword will be thrown
181 * away with an AFPERR_PWDEXPR error. (thanks to Leland Wallace from Apple
182 * for clarifying this)
185 for (i=0; i<=0xff; i++) {
186 uam_afpserver_action(i, UAM_AFPSERVER_PREAUTH, afp_errpwdexpired, NULL);
188 uam_afpserver_action(AFP_LOGOUT, UAM_AFPSERVER_PREAUTH, afp_logout, NULL);
189 uam_afpserver_action(AFP_CHANGEPW, UAM_AFPSERVER_PREAUTH, afp_changepw, NULL);
192 afp_switch = postauth_switch;
193 switch (afp_version) {
198 uam_afpserver_action(AFP_GETACL, UAM_AFPSERVER_POSTAUTH, afp_getacl, NULL);
199 uam_afpserver_action(AFP_SETACL, UAM_AFPSERVER_POSTAUTH, afp_setacl, NULL);
200 uam_afpserver_action(AFP_ACCESS, UAM_AFPSERVER_POSTAUTH, afp_access, NULL);
201 #endif /* HAVE_ACLS */
202 uam_afpserver_action(AFP_GETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_getextattr, NULL);
203 uam_afpserver_action(AFP_SETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_setextattr, NULL);
204 uam_afpserver_action(AFP_REMOVEATTR, UAM_AFPSERVER_POSTAUTH, afp_remextattr, NULL);
205 uam_afpserver_action(AFP_LISTEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_listextattr, NULL);
208 uam_afpserver_action(AFP_SYNCDIR, UAM_AFPSERVER_POSTAUTH, afp_syncdir, NULL);
209 uam_afpserver_action(AFP_SYNCFORK, UAM_AFPSERVER_POSTAUTH, afp_syncfork, NULL);
210 uam_afpserver_action(AFP_SPOTLIGHT_PRIVATE, UAM_AFPSERVER_POSTAUTH, afp_null_nolog, NULL);
211 uam_afpserver_action(AFP_ENUMERATE_EXT2, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext2, NULL);
214 uam_afpserver_action(AFP_ENUMERATE_EXT, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext, NULL);
215 uam_afpserver_action(AFP_BYTELOCK_EXT, UAM_AFPSERVER_POSTAUTH, afp_bytelock_ext, NULL);
216 /* catsearch_ext uses the same packet as catsearch FIXME double check this, it wasn't true for enue
218 uam_afpserver_action(AFP_CATSEARCH_EXT, UAM_AFPSERVER_POSTAUTH, afp_catsearch_ext, NULL);
219 uam_afpserver_action(AFP_GETSESSTOKEN, UAM_AFPSERVER_POSTAUTH, afp_getsession, NULL);
220 uam_afpserver_action(AFP_READ_EXT, UAM_AFPSERVER_POSTAUTH, afp_read_ext, NULL);
221 uam_afpserver_action(AFP_WRITE_EXT, UAM_AFPSERVER_POSTAUTH, afp_write_ext, NULL);
222 uam_afpserver_action(AFP_DISCTOLDSESS, UAM_AFPSERVER_POSTAUTH, afp_disconnect, NULL);
226 * If first connection to a server is done in classic AFP2.2 version is used
227 * but OSX uses AFP3.x FPzzz command !
229 uam_afpserver_action(AFP_ZZZ, UAM_AFPSERVER_POSTAUTH, afp_zzz, NULL);
237 #define GROUPSTR_BUFSIZE 1024
238 static const char *print_groups(int ngroups, gid_t *groups)
240 static char groupsstr[GROUPSTR_BUFSIZE];
247 for (i = 0; (i < ngroups) && (s < &groupsstr[GROUPSTR_BUFSIZE]); i++) {
248 s += snprintf(s, &groupsstr[GROUPSTR_BUFSIZE] - s, " %u", groups[i]);
254 static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void), int expired)
258 #endif /* ADMIN_GRP */
260 if ( pwd->pw_uid == 0 ) { /* don't allow root login */
261 LOG(log_error, logtype_afpd, "login: root login denied!" );
262 return AFPERR_NOTAUTH;
265 LOG(log_note, logtype_afpd, "%s Login by %s",
266 afp_versions[afp_version_index].av_name, pwd->pw_name);
269 if (obj->proto == AFPPROTO_ASP) {
270 ASP asp = obj->handle;
271 int addr_net = ntohs( asp->asp_sat.sat_addr.s_net );
272 int addr_node = asp->asp_sat.sat_addr.s_node;
274 if (obj->options.authprintdir) {
275 if(addr_net && addr_node) { /* Do we have a valid Appletalk address? */
278 int mypid = getpid();
279 struct stat stat_buf;
281 sprintf(nodename, "%s/net%d.%dnode%d", obj->options.authprintdir,
282 addr_net / 256, addr_net % 256, addr_node);
283 LOG(log_info, logtype_afpd, "registering %s (uid %d) on %u.%u as %s",
284 pwd->pw_name, pwd->pw_uid, addr_net, addr_node, nodename);
286 if (stat(nodename, &stat_buf) == 0) { /* file exists */
287 if (S_ISREG(stat_buf.st_mode)) { /* normal file */
289 fp = fopen(nodename, "w");
290 fprintf(fp, "%s:%d\n", pwd->pw_name, mypid);
292 chown( nodename, pwd->pw_uid, -1 );
293 } else { /* somebody is messing with us */
294 LOG(log_error, logtype_afpd, "print authfile %s is not a normal file, it will not be modified", nodename );
296 } else { /* file 'nodename' does not exist */
297 fp = fopen(nodename, "w");
298 fprintf(fp, "%s:%d\n", pwd->pw_name, mypid);
300 chown( nodename, pwd->pw_uid, -1 );
302 } /* if (addr_net && addr_node ) */
303 } /* if (options->authprintdir) */
304 } /* if (obj->proto == AFPPROTO_ASP) */
307 if (initgroups( pwd->pw_name, pwd->pw_gid ) < 0) {
309 LOG(log_info, logtype_afpd, "running with uid %d", geteuid());
310 #else /* RUN_AS_USER */
311 LOG(log_error, logtype_afpd, "login: %s", strerror(errno));
312 return AFPERR_BADUAM;
313 #endif /* RUN_AS_USER */
317 /* Basically if the user is in the admin group, we stay root */
319 if (( ngroups = getgroups( 0, NULL )) < 0 ) {
320 LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
321 return AFPERR_BADUAM;
324 if ( NULL == (groups = calloc(ngroups, GROUPS_SIZE)) ) {
325 LOG(log_error, logtype_afpd, "login: %s calloc: %d", ngroups);
326 return AFPERR_BADUAM;
329 if (( ngroups = getgroups( ngroups, groups )) < 0 ) {
330 LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
331 return AFPERR_BADUAM;
335 LOG(log_debug, logtype_afpd, "obj->options.admingid == %d", obj->options.admingid);
337 if (obj->options.admingid != 0) {
339 for (i = 0; i < ngroups; i++) {
340 if (groups[i] == obj->options.admingid) admin = 1;
345 LOG(log_info, logtype_afpd, "admin login -- %s", pwd->pw_name );
348 #endif /* ADMIN_GRP */
351 struct DSI *dsi = obj->handle;
358 afp_get_cmdline( &argc, &argv );
360 hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
361 sizeof( struct in_addr ),
362 dsi->client.sin_family );
365 clientname = hp->h_name;
367 clientname = inet_ntoa( dsi->client.sin_addr );
369 sprintf( hostname, "%s@%s", pwd->pw_name, clientname );
371 if( sia_become_user( NULL, argc, argv, hostname, pwd->pw_name,
372 NULL, FALSE, NULL, NULL,
373 SIA_BEU_REALLOGIN ) != SIASUCCESS )
374 return AFPERR_BADUAM;
376 LOG(log_info, logtype_afpd, "session from %s (%s)", hostname,
377 inet_ntoa( dsi->client.sin_addr ) );
379 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
380 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
381 return AFPERR_BADUAM;
385 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
386 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
387 return AFPERR_BADUAM;
391 LOG(log_debug, logtype_afpd, "login: supplementary groups: %s", print_groups(ngroups, groups));
393 /* There's probably a better way to do this, but for now, we just play root */
398 #endif /* ADMIN_GRP */
401 set_auth_switch(expired);
402 /* save our euid, we need it for preexec_close */
403 obj->uid = geteuid();
404 obj->logout = logout;
408 save_uidgid ( &obj->uidgid );
414 /* ---------------------- */
415 int afp_zzz(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
418 DSI *dsi = (DSI *)AFPobj->handle;
426 memcpy(&data, ibuf, 4); /* flag */
430 * Possible sleeping states:
431 * 1) normal sleep: DSI_SLEEPING (up to 10.3)
432 * 2) extended sleep: DSI_SLEEPING | DSI_EXTSLEEP (starting with 10.4)
435 if (data & AFPZZZ_EXT_WAKEUP) {
436 /* wakeup request from exetended sleep */
437 if (dsi->flags & DSI_EXTSLEEP) {
438 LOG(log_note, logtype_afpd, "afp_zzz: waking up from extended sleep");
439 dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP);
443 dsi->flags |= DSI_SLEEPING;
444 if (data & AFPZZZ_EXT_SLEEP) {
445 LOG(log_note, logtype_afpd, "afp_zzz: entering extended sleep");
446 dsi->flags |= DSI_EXTSLEEP;
448 LOG(log_note, logtype_afpd, "afp_zzz: entering normal sleep");
453 * According to AFP 3.3 spec we should not return anything,
454 * but eg 10.5.8 server still returns the numbers of hours
455 * the server is keeping the sessino (ie max sleeptime).
457 data = obj->options.sleep / 120; /* hours */
461 *rbuflen = sizeof(data);
463 memcpy(rbuf, &data, sizeof(data));
464 rbuf += sizeof(data);
469 /* ---------------------- */
470 static int create_session_token(AFPObj *obj)
474 /* use 8 bytes for token as OSX, don't know if it helps */
475 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
476 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
480 if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
483 memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
484 obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
486 memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
491 static int create_session_key(AFPObj *obj)
493 /* create session key */
494 if (obj->sinfo.sessionkey == NULL) {
495 if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
497 uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
498 obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
504 /* ---------------------- */
507 char *ibuf, size_t ibuflen,
508 char *rbuf, size_t *rbuflen)
520 if (ibuflen < 2 + sizeof(type)) {
527 memcpy(&type, ibuf, sizeof(type));
529 ibuf += sizeof(type);
530 ibuflen -= sizeof(type);
532 if ( obj->sinfo.sessiontoken == NULL ) {
533 if ( create_session_token( obj ) )
541 case 0: /* old version ?*/
542 tklen = obj->sinfo.sessiontoken_len;
543 token = obj->sinfo.sessiontoken;
545 case 1: /* disconnect */
546 case 2: /* reconnect update id */
547 if (ibuflen >= sizeof(idlen)) {
548 memcpy(&idlen, ibuf, sizeof(idlen));
549 idlen = ntohl(idlen);
550 ibuf += sizeof(idlen);
551 ibuflen -= sizeof(idlen);
552 if (ibuflen < idlen) {
555 /* memcpy (id, ibuf, idlen) */
556 tklen = obj->sinfo.sessiontoken_len;
557 token = obj->sinfo.sessiontoken;
564 memcpy( &idlen, ibuf, sizeof(idlen));
565 idlen = ntohl(idlen);
566 ibuf += sizeof(idlen);
567 ibuflen -= sizeof(idlen);
568 ibuf += sizeof(boottime);
569 ibuflen -= sizeof(boottime);
570 if (ibuflen < idlen || idlen > (90-10)) {
573 if (!obj->sinfo.clientid) {
574 obj->sinfo.clientid = malloc(idlen + 8);
575 memcpy(obj->sinfo.clientid, p, idlen + 8);
576 obj->sinfo.clientid_len = idlen + 8;
578 if (ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p) != 0)
580 tklen = obj->sinfo.sessiontoken_len;
581 token = obj->sinfo.sessiontoken;
584 case 8: /* Panther Kerberos Token */
585 tklen = obj->sinfo.cryptedkey_len;
586 token = obj->sinfo.cryptedkey;
598 memcpy(rbuf, &tp, sizeof(tklen));
599 rbuf += sizeof(tklen);
600 *rbuflen += sizeof(tklen);
602 memcpy(rbuf, token, tklen);
608 /* ---------------------- */
609 int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
611 DSI *dsi = (DSI *)obj->handle;
621 /* check for guest user */
622 if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
627 memcpy(&type, ibuf, sizeof(type));
629 ibuf += sizeof(type);
631 memcpy(&tklen, ibuf, sizeof(tklen));
632 tklen = ntohl(tklen);
633 ibuf += sizeof(tklen);
635 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
636 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
639 if (tklen != SESSIONTOKEN_LEN) {
642 tklen = sizeof(pid_t);
643 memcpy(&token, ibuf, tklen);
645 /* our stuff is pid + zero pad */
647 for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
653 LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
654 dsi->flags |= DSI_RECONINPROG;
656 /* Deactivate tickle timer */
657 const struct itimerval none = {{0, 0}, {0, 0}};
658 setitimer(ITIMER_REAL, &none, NULL);
660 /* check for old session, possibly transfering session from here to there */
661 if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) != 0)
663 /* write uint16_t DSI request ID */
664 if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
665 LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
668 /* now send our connected AFP client socket */
669 if (send_fd(obj->ipc_fd, dsi->socket) != 0)
671 /* Now see what happens: either afpd master sends us SIGTERM because our session */
672 /* has been transfered to a old disconnected session, or we continue */
675 if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
676 /* Reconnect succeeded, we exit now after sleeping some more */
677 sleep(2); /* sleep some more to give the recon. session time */
678 LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
683 /* Reinstall tickle timer */
684 setitimer(ITIMER_REAL, &dsi->timer, NULL);
686 LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
690 /* ---------------------- */
691 static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
695 if (!len || len > ibuflen)
696 return AFPERR_BADVERS;
698 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
699 for ( i = 0; i < num; i++ ) {
700 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
701 afp_version = afp_versions[ i ].av_number;
702 afp_version_index = i;
706 if ( i == num ) /* An inappropo version */
707 return AFPERR_BADVERS ;
709 if (afp_version >= 30 && obj->proto != AFPPROTO_DSI)
710 return AFPERR_BADVERS ;
713 if (afp_version >= 30 && sizeof(off_t) != 8) {
714 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
715 return AFPERR_BADVERS ;
721 /* ---------------------- */
722 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
724 struct passwd *pwd = NULL;
731 return send_reply(obj, AFPERR_SHUTDOWN );
734 return send_reply(obj, AFPERR_BADVERS );
737 len = (unsigned char) *ibuf++;
740 i = get_version(obj, ibuf, ibuflen, len);
742 return send_reply(obj, i );
745 return send_reply(obj, AFPERR_BADUAM);
750 len = (unsigned char) *ibuf++;
753 if (!len || len > ibuflen)
754 return send_reply(obj, AFPERR_BADUAM);
756 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
757 return send_reply(obj, AFPERR_BADUAM);
761 if (AFP_OK != (i = create_session_key(obj)) )
762 return send_reply(obj, i);
764 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
766 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
767 return send_reply(obj, i);
769 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
772 /* ---------------------- */
773 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
775 struct passwd *pwd = NULL;
785 return send_reply(obj, AFPERR_SHUTDOWN );
788 return send_reply(obj, AFPERR_BADVERS );
794 len = (unsigned char) *ibuf;
798 i = get_version(obj, ibuf, ibuflen, len);
800 return send_reply(obj, i );
803 return send_reply(obj, AFPERR_BADUAM);
808 len = (unsigned char) *ibuf;
812 if (!len || len > ibuflen)
813 return send_reply(obj, AFPERR_BADUAM);
815 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
816 return send_reply(obj, AFPERR_BADUAM);
820 if (!afp_uam->u.uam_login.login_ext) {
821 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
822 return send_reply(obj, AFPERR_BADUAM);
825 if (ibuflen <= 1 +sizeof(len16))
826 return send_reply(obj, AFPERR_PARAM);
832 return send_reply(obj, AFPERR_PARAM);
834 memcpy(&len16, ibuf, sizeof(len16));
835 ibuf += sizeof(len16);
836 ibuflen -= sizeof(len16);
839 return send_reply(obj, AFPERR_PARAM);
843 /* directory service name */
845 return send_reply(obj, AFPERR_PARAM);
854 return send_reply(obj, AFPERR_PARAM);
855 len = (unsigned char) *ibuf;
860 /* With "No User Authen" it is equal */
861 if (ibuflen < sizeof(len16))
862 return send_reply(obj, AFPERR_PARAM);
863 memcpy(&len16, ibuf, sizeof(len16));
864 ibuf += sizeof(len16);
865 ibuflen -= sizeof(len16);
869 return send_reply(obj, AFPERR_PARAM);
873 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
874 return send_reply(obj, AFPERR_PARAM);
881 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
886 if (AFP_OK != (i = create_session_key(obj)) ) {
887 return send_reply(obj, i);
890 /* FIXME user name are in UTF8 */
891 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
893 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
894 return send_reply(obj, i);
896 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
899 /* ---------------------- */
900 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
902 struct passwd *pwd = NULL;
905 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
907 return send_reply(obj, AFPERR_NOTAUTH );
910 ibuf += 2; ibuflen -= 2;
911 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
913 if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
914 return send_reply(obj, err);
916 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
920 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
922 DSI *dsi = (DSI *)(obj->handle);
924 LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
925 of_close_all_forks();
927 dsi->flags = DSI_AFP_LOGGED_OUT;
934 /* change password --
935 * NOTE: an FPLogin must already have completed successfully for this
936 * to work. this also does a little pre-processing before it hands
939 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
941 char username[MACFILELEN + 1], *start = ibuf;
950 /* check if password change is allowed, OS-X ignores the flag.
951 * we shouldn't trust the client on this anyway.
952 * not sure about the "right" error code, NOOP for now */
953 if (!(obj->options.passwdbits & PASSWD_SET))
956 /* make sure we can deal w/ this uam */
957 len = (unsigned char) *ibuf++;
958 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
959 return AFPERR_BADUAM;
962 if ((len + 1) & 1) /* pad byte */
965 if ( afp_version < 30) {
966 len = (unsigned char) *ibuf++;
967 if ( len > sizeof(username) - 1) {
970 memcpy(username, ibuf, len);
971 username[ len ] = '\0';
973 if ((len + 1) & 1) /* pad byte */
976 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
977 if ( ibuf[0] != '\0' || ibuf[1] != '\0')
980 len = MIN(sizeof(username), strlen(obj->username));
981 memcpy(username, obj->username, len);
982 username[ len ] = '\0';
986 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
988 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
991 /* send it off to the uam. we really don't use ibuflen right now. */
992 if (ibuflen < (size_t)(ibuf - start))
995 ibuflen -= (ibuf - start);
996 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
998 LOG(log_info, logtype_afpd, "password change %s.",
999 (ret == AFPERR_AUTHCONT) ? "continued" :
1000 (ret ? "failed" : "succeeded"));
1001 if ( ret == AFP_OK )
1009 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1016 LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
1021 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
1022 memcpy(&bitmap, ibuf, sizeof(bitmap));
1023 bitmap = ntohs(bitmap);
1025 /* deal with error cases. we don't have to worry about
1026 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
1029 return AFPERR_PARAM;
1030 if ((bitmap & USERIBIT_ALL) != bitmap)
1031 return AFPERR_BITMAP;
1033 /* remember place where we store the possibly modified bitmap later */
1034 memcpy(rbuf, ibuf, sizeof(bitmap));
1036 rbuf += sizeof(bitmap);
1037 *rbuflen = sizeof(bitmap);
1039 /* copy the user/group info */
1040 if (bitmap & USERIBIT_USER) {
1041 id = htonl(geteuid());
1042 memcpy(rbuf, &id, sizeof(id));
1044 *rbuflen += sizeof(id);
1047 if (bitmap & USERIBIT_GROUP) {
1048 id = htonl(getegid());
1049 memcpy(rbuf, &id, sizeof(id));
1051 *rbuflen += sizeof(id);
1054 if (bitmap & USERIBIT_UUID) {
1055 if ( ! (obj->options.flags & OPTION_UUID)) {
1056 bitmap &= ~USERIBIT_UUID;
1057 bitmap = htons(bitmap);
1058 memcpy(bitmapp, &bitmap, sizeof(bitmap));
1060 LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
1063 ret = getuuidfromname( obj->username, UUID_USER, uuid);
1065 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
1066 return AFPERR_NOITEM;
1068 LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
1070 memcpy(rbuf, uuid, UUID_BINSIZE);
1071 rbuf += UUID_BINSIZE;
1072 *rbuflen += UUID_BINSIZE;
1076 LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
1080 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
1081 (((type) == UAM_SERVER_CHANGEPW) ? \
1082 &uam_changepw : NULL))
1084 /* just do a linked list search. this could be sped up with a hashed
1085 * list, but i doubt anyone's going to have enough uams to matter. */
1086 struct uam_obj *auth_uamfind(const int type, const char *name,
1089 struct uam_obj *prev, *start;
1091 if (!name || !(start = UAM_LIST(type)))
1095 while ((prev = prev->uam_prev) != start)
1096 if (strndiacasecmp(prev->uam_name, name, len) == 0)
1102 int auth_register(const int type, struct uam_obj *uam)
1104 struct uam_obj *start;
1106 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1109 if (!(start = UAM_LIST(type)))
1110 return 1; /* we don't know what to do with it, caller must free it */
1112 uam_attach(start, uam);
1116 /* load all of the modules */
1117 int auth_load(const char *path, const char *list)
1119 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
1120 struct uam_mod *mod;
1124 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1127 strlcpy(buf, list, sizeof(buf));
1128 if ((p = strtok(buf, ",")) == NULL)
1132 if (name[len - 1] != '/') {
1138 strlcpy(name + len, p, sizeof(name) - len);
1139 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1141 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1143 if (stat(name, &st) == 0) {
1144 if ((mod = uam_load(name, p))) {
1145 uam_attach(&uam_modules, mod);
1146 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1148 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1151 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1153 p = strtok(NULL, ",");
1159 /* get rid of all of the uams */
1160 void auth_unload(void)
1162 struct uam_mod *mod, *prev, *start = &uam_modules;
1164 prev = start->uam_prev;
1165 while ((mod = prev) != start) {
1166 prev = prev->uam_prev;