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);
418 /* Some PAM module might have reset our signal handlers and timer, so we need to reestablish them */
419 afp_over_dsi_sighandlers(obj);
424 /* ---------------------- */
425 int afp_zzz(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
428 DSI *dsi = (DSI *)AFPobj->handle;
436 memcpy(&data, ibuf, 4); /* flag */
440 * Possible sleeping states:
441 * 1) normal sleep: DSI_SLEEPING (up to 10.3)
442 * 2) extended sleep: DSI_SLEEPING | DSI_EXTSLEEP (starting with 10.4)
445 if (data & AFPZZZ_EXT_WAKEUP) {
446 /* wakeup request from exetended sleep */
447 if (dsi->flags & DSI_EXTSLEEP) {
448 LOG(log_note, logtype_afpd, "afp_zzz: waking up from extended sleep");
449 dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP);
453 dsi->flags |= DSI_SLEEPING;
454 if (data & AFPZZZ_EXT_SLEEP) {
455 LOG(log_note, logtype_afpd, "afp_zzz: entering extended sleep");
456 dsi->flags |= DSI_EXTSLEEP;
458 LOG(log_note, logtype_afpd, "afp_zzz: entering normal sleep");
463 * According to AFP 3.3 spec we should not return anything,
464 * but eg 10.5.8 server still returns the numbers of hours
465 * the server is keeping the sessino (ie max sleeptime).
467 data = obj->options.sleep / 120; /* hours */
471 *rbuflen = sizeof(data);
473 memcpy(rbuf, &data, sizeof(data));
474 rbuf += sizeof(data);
479 /* ---------------------- */
480 static int create_session_token(AFPObj *obj)
484 /* use 8 bytes for token as OSX, don't know if it helps */
485 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
486 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
490 if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
493 memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
494 obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
496 memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
501 static int create_session_key(AFPObj *obj)
503 /* create session key */
504 if (obj->sinfo.sessionkey == NULL) {
505 if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
507 uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
508 obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
514 /* ---------------------- */
517 char *ibuf, size_t ibuflen,
518 char *rbuf, size_t *rbuflen)
530 if (ibuflen < 2 + sizeof(type)) {
537 memcpy(&type, ibuf, sizeof(type));
539 ibuf += sizeof(type);
540 ibuflen -= sizeof(type);
542 if ( obj->sinfo.sessiontoken == NULL ) {
543 if ( create_session_token( obj ) )
551 case 0: /* old version ?*/
552 tklen = obj->sinfo.sessiontoken_len;
553 token = obj->sinfo.sessiontoken;
555 case 1: /* disconnect */
556 case 2: /* reconnect update id */
557 if (ibuflen >= sizeof(idlen)) {
558 memcpy(&idlen, ibuf, sizeof(idlen));
559 idlen = ntohl(idlen);
560 ibuf += sizeof(idlen);
561 ibuflen -= sizeof(idlen);
562 if (ibuflen < idlen) {
565 /* memcpy (id, ibuf, idlen) */
566 tklen = obj->sinfo.sessiontoken_len;
567 token = obj->sinfo.sessiontoken;
574 memcpy( &idlen, ibuf, sizeof(idlen));
575 idlen = ntohl(idlen);
576 ibuf += sizeof(idlen);
577 ibuflen -= sizeof(idlen);
578 ibuf += sizeof(boottime);
579 ibuflen -= sizeof(boottime);
580 if (ibuflen < idlen || idlen > (90-10)) {
583 if (!obj->sinfo.clientid) {
584 obj->sinfo.clientid = malloc(idlen + 8);
585 memcpy(obj->sinfo.clientid, p, idlen + 8);
586 obj->sinfo.clientid_len = idlen + 8;
588 if (ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p) != 0)
590 tklen = obj->sinfo.sessiontoken_len;
591 token = obj->sinfo.sessiontoken;
594 case 8: /* Panther Kerberos Token */
595 tklen = obj->sinfo.cryptedkey_len;
596 token = obj->sinfo.cryptedkey;
608 memcpy(rbuf, &tp, sizeof(tklen));
609 rbuf += sizeof(tklen);
610 *rbuflen += sizeof(tklen);
612 memcpy(rbuf, token, tklen);
618 /* ---------------------- */
619 int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
621 DSI *dsi = (DSI *)obj->handle;
631 /* check for guest user */
632 if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
637 memcpy(&type, ibuf, sizeof(type));
639 ibuf += sizeof(type);
641 memcpy(&tklen, ibuf, sizeof(tklen));
642 tklen = ntohl(tklen);
643 ibuf += sizeof(tklen);
645 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
646 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
649 if (tklen != SESSIONTOKEN_LEN) {
652 tklen = sizeof(pid_t);
653 memcpy(&token, ibuf, tklen);
655 /* our stuff is pid + zero pad */
657 for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
663 LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
664 dsi->flags |= DSI_RECONINPROG;
666 /* Deactivate tickle timer */
667 const struct itimerval none = {{0, 0}, {0, 0}};
668 setitimer(ITIMER_REAL, &none, NULL);
670 /* check for old session, possibly transfering session from here to there */
671 if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) != 0)
673 /* write uint16_t DSI request ID */
674 if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
675 LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
678 /* now send our connected AFP client socket */
679 if (send_fd(obj->ipc_fd, dsi->socket) != 0)
681 /* Now see what happens: either afpd master sends us SIGTERM because our session */
682 /* has been transfered to a old disconnected session, or we continue */
685 if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
686 /* Reconnect succeeded, we exit now after sleeping some more */
687 sleep(2); /* sleep some more to give the recon. session time */
688 LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
693 /* Reinstall tickle timer */
694 setitimer(ITIMER_REAL, &dsi->timer, NULL);
696 LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
700 /* ---------------------- */
701 static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
705 if (!len || len > ibuflen)
706 return AFPERR_BADVERS;
708 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
709 for ( i = 0; i < num; i++ ) {
710 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
711 afp_version = afp_versions[ i ].av_number;
712 afp_version_index = i;
716 if ( i == num ) /* An inappropo version */
717 return AFPERR_BADVERS ;
719 if (afp_version >= 30 && obj->proto != AFPPROTO_DSI)
720 return AFPERR_BADVERS ;
723 if (afp_version >= 30 && sizeof(off_t) != 8) {
724 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
725 return AFPERR_BADVERS ;
731 /* ---------------------- */
732 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
734 struct passwd *pwd = NULL;
741 return send_reply(obj, AFPERR_SHUTDOWN );
744 return send_reply(obj, AFPERR_BADVERS );
747 len = (unsigned char) *ibuf++;
750 i = get_version(obj, ibuf, ibuflen, len);
752 return send_reply(obj, i );
755 return send_reply(obj, AFPERR_BADUAM);
760 len = (unsigned char) *ibuf++;
763 if (!len || len > ibuflen)
764 return send_reply(obj, AFPERR_BADUAM);
766 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
767 return send_reply(obj, AFPERR_BADUAM);
771 if (AFP_OK != (i = create_session_key(obj)) )
772 return send_reply(obj, i);
774 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
776 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
777 return send_reply(obj, i);
779 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
782 /* ---------------------- */
783 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
785 struct passwd *pwd = NULL;
795 return send_reply(obj, AFPERR_SHUTDOWN );
798 return send_reply(obj, AFPERR_BADVERS );
804 len = (unsigned char) *ibuf;
808 i = get_version(obj, ibuf, ibuflen, len);
810 return send_reply(obj, i );
813 return send_reply(obj, AFPERR_BADUAM);
818 len = (unsigned char) *ibuf;
822 if (!len || len > ibuflen)
823 return send_reply(obj, AFPERR_BADUAM);
825 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
826 return send_reply(obj, AFPERR_BADUAM);
830 if (!afp_uam->u.uam_login.login_ext) {
831 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
832 return send_reply(obj, AFPERR_BADUAM);
835 if (ibuflen <= 1 +sizeof(len16))
836 return send_reply(obj, AFPERR_PARAM);
842 return send_reply(obj, AFPERR_PARAM);
844 memcpy(&len16, ibuf, sizeof(len16));
845 ibuf += sizeof(len16);
846 ibuflen -= sizeof(len16);
849 return send_reply(obj, AFPERR_PARAM);
853 /* directory service name */
855 return send_reply(obj, AFPERR_PARAM);
864 return send_reply(obj, AFPERR_PARAM);
865 len = (unsigned char) *ibuf;
870 /* With "No User Authen" it is equal */
871 if (ibuflen < sizeof(len16))
872 return send_reply(obj, AFPERR_PARAM);
873 memcpy(&len16, ibuf, sizeof(len16));
874 ibuf += sizeof(len16);
875 ibuflen -= sizeof(len16);
879 return send_reply(obj, AFPERR_PARAM);
883 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
884 return send_reply(obj, AFPERR_PARAM);
891 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
896 if (AFP_OK != (i = create_session_key(obj)) ) {
897 return send_reply(obj, i);
900 /* FIXME user name are in UTF8 */
901 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
903 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
904 return send_reply(obj, i);
906 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
909 /* ---------------------- */
910 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
912 struct passwd *pwd = NULL;
915 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
917 return send_reply(obj, AFPERR_NOTAUTH );
920 ibuf += 2; ibuflen -= 2;
921 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
923 if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
924 return send_reply(obj, err);
926 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
930 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
932 DSI *dsi = (DSI *)(obj->handle);
934 LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
935 of_close_all_forks();
937 dsi->flags = DSI_AFP_LOGGED_OUT;
944 /* change password --
945 * NOTE: an FPLogin must already have completed successfully for this
946 * to work. this also does a little pre-processing before it hands
949 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
951 char username[MACFILELEN + 1], *start = ibuf;
960 /* check if password change is allowed, OS-X ignores the flag.
961 * we shouldn't trust the client on this anyway.
962 * not sure about the "right" error code, NOOP for now */
963 if (!(obj->options.passwdbits & PASSWD_SET))
966 /* make sure we can deal w/ this uam */
967 len = (unsigned char) *ibuf++;
968 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
969 return AFPERR_BADUAM;
972 if ((len + 1) & 1) /* pad byte */
975 if ( afp_version < 30) {
976 len = (unsigned char) *ibuf++;
977 if ( len > sizeof(username) - 1) {
980 memcpy(username, ibuf, len);
981 username[ len ] = '\0';
983 if ((len + 1) & 1) /* pad byte */
986 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
987 if ( ibuf[0] != '\0' || ibuf[1] != '\0')
990 len = MIN(sizeof(username), strlen(obj->username));
991 memcpy(username, obj->username, len);
992 username[ len ] = '\0';
996 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
998 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
1001 /* send it off to the uam. we really don't use ibuflen right now. */
1002 if (ibuflen < (size_t)(ibuf - start))
1003 return AFPERR_PARAM;
1005 ibuflen -= (ibuf - start);
1006 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
1008 LOG(log_info, logtype_afpd, "password change %s.",
1009 (ret == AFPERR_AUTHCONT) ? "continued" :
1010 (ret ? "failed" : "succeeded"));
1011 if ( ret == AFP_OK )
1019 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1026 LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
1031 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
1032 memcpy(&bitmap, ibuf, sizeof(bitmap));
1033 bitmap = ntohs(bitmap);
1035 /* deal with error cases. we don't have to worry about
1036 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
1039 return AFPERR_PARAM;
1040 if ((bitmap & USERIBIT_ALL) != bitmap)
1041 return AFPERR_BITMAP;
1043 /* remember place where we store the possibly modified bitmap later */
1044 memcpy(rbuf, ibuf, sizeof(bitmap));
1046 rbuf += sizeof(bitmap);
1047 *rbuflen = sizeof(bitmap);
1049 /* copy the user/group info */
1050 if (bitmap & USERIBIT_USER) {
1051 id = htonl(geteuid());
1052 memcpy(rbuf, &id, sizeof(id));
1054 *rbuflen += sizeof(id);
1057 if (bitmap & USERIBIT_GROUP) {
1058 id = htonl(getegid());
1059 memcpy(rbuf, &id, sizeof(id));
1061 *rbuflen += sizeof(id);
1064 if (bitmap & USERIBIT_UUID) {
1065 if ( ! (obj->options.flags & OPTION_UUID)) {
1066 bitmap &= ~USERIBIT_UUID;
1067 bitmap = htons(bitmap);
1068 memcpy(bitmapp, &bitmap, sizeof(bitmap));
1070 LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
1073 ret = getuuidfromname( obj->username, UUID_USER, uuid);
1075 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
1076 return AFPERR_NOITEM;
1078 LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
1080 memcpy(rbuf, uuid, UUID_BINSIZE);
1081 rbuf += UUID_BINSIZE;
1082 *rbuflen += UUID_BINSIZE;
1086 LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
1090 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
1091 (((type) == UAM_SERVER_CHANGEPW) ? \
1092 &uam_changepw : NULL))
1094 /* just do a linked list search. this could be sped up with a hashed
1095 * list, but i doubt anyone's going to have enough uams to matter. */
1096 struct uam_obj *auth_uamfind(const int type, const char *name,
1099 struct uam_obj *prev, *start;
1101 if (!name || !(start = UAM_LIST(type)))
1105 while ((prev = prev->uam_prev) != start)
1106 if (strndiacasecmp(prev->uam_name, name, len) == 0)
1112 int auth_register(const int type, struct uam_obj *uam)
1114 struct uam_obj *start;
1116 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1119 if (!(start = UAM_LIST(type)))
1120 return 1; /* we don't know what to do with it, caller must free it */
1122 uam_attach(start, uam);
1126 /* load all of the modules */
1127 int auth_load(const char *path, const char *list)
1129 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
1130 struct uam_mod *mod;
1134 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1137 strlcpy(buf, list, sizeof(buf));
1138 if ((p = strtok(buf, ",")) == NULL)
1142 if (name[len - 1] != '/') {
1148 strlcpy(name + len, p, sizeof(name) - len);
1149 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1151 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1153 if (stat(name, &st) == 0) {
1154 if ((mod = uam_load(name, p))) {
1155 uam_attach(&uam_modules, mod);
1156 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1158 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1161 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1163 p = strtok(NULL, ",");
1169 /* get rid of all of the uams */
1170 void auth_unload(void)
1172 struct uam_mod *mod, *prev, *start = &uam_modules;
1174 prev = start->uam_prev;
1175 while ((mod = prev) != start) {
1176 prev = prev->uam_prev;