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 );
418 /* ---------------------- */
419 int afp_zzz(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
422 DSI *dsi = (DSI *)AFPobj->handle;
430 memcpy(&data, ibuf, 4); /* flag */
434 * Possible sleeping states:
435 * 1) normal sleep: DSI_SLEEPING (up to 10.3)
436 * 2) extended sleep: DSI_SLEEPING | DSI_EXTSLEEP (starting with 10.4)
439 if (data & AFPZZZ_EXT_WAKEUP) {
440 /* wakeup request from exetended sleep */
441 if (dsi->flags & DSI_EXTSLEEP) {
442 LOG(log_note, logtype_afpd, "afp_zzz: waking up from extended sleep");
443 dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP);
447 dsi->flags |= DSI_SLEEPING;
448 if (data & AFPZZZ_EXT_SLEEP) {
449 LOG(log_note, logtype_afpd, "afp_zzz: entering extended sleep");
450 dsi->flags |= DSI_EXTSLEEP;
452 LOG(log_note, logtype_afpd, "afp_zzz: entering normal sleep");
457 * According to AFP 3.3 spec we should not return anything,
458 * but eg 10.5.8 server still returns the numbers of hours
459 * the server is keeping the sessino (ie max sleeptime).
461 data = obj->options.sleep / 120; /* hours */
465 *rbuflen = sizeof(data);
467 memcpy(rbuf, &data, sizeof(data));
468 rbuf += sizeof(data);
473 /* ---------------------- */
474 static int create_session_token(AFPObj *obj)
478 /* use 8 bytes for token as OSX, don't know if it helps */
479 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
480 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
484 if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
487 memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
488 obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
490 memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
495 static int create_session_key(AFPObj *obj)
497 /* create session key */
498 if (obj->sinfo.sessionkey == NULL) {
499 if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
501 uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
502 obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
508 /* ---------------------- */
511 char *ibuf, size_t ibuflen,
512 char *rbuf, size_t *rbuflen)
524 if (ibuflen < 2 + sizeof(type)) {
531 memcpy(&type, ibuf, sizeof(type));
533 ibuf += sizeof(type);
534 ibuflen -= sizeof(type);
536 if ( obj->sinfo.sessiontoken == NULL ) {
537 if ( create_session_token( obj ) )
545 case 0: /* old version ?*/
546 tklen = obj->sinfo.sessiontoken_len;
547 token = obj->sinfo.sessiontoken;
549 case 1: /* disconnect */
550 case 2: /* reconnect update id */
551 if (ibuflen >= sizeof(idlen)) {
552 memcpy(&idlen, ibuf, sizeof(idlen));
553 idlen = ntohl(idlen);
554 ibuf += sizeof(idlen);
555 ibuflen -= sizeof(idlen);
556 if (ibuflen < idlen) {
559 /* memcpy (id, ibuf, idlen) */
560 tklen = obj->sinfo.sessiontoken_len;
561 token = obj->sinfo.sessiontoken;
568 memcpy( &idlen, ibuf, sizeof(idlen));
569 idlen = ntohl(idlen);
570 ibuf += sizeof(idlen);
571 ibuflen -= sizeof(idlen);
572 ibuf += sizeof(boottime);
573 ibuflen -= sizeof(boottime);
574 if (ibuflen < idlen || idlen > (90-10)) {
577 if (!obj->sinfo.clientid) {
578 obj->sinfo.clientid = malloc(idlen + 8);
579 memcpy(obj->sinfo.clientid, p, idlen + 8);
580 obj->sinfo.clientid_len = idlen + 8;
582 if (ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p) != 0)
584 tklen = obj->sinfo.sessiontoken_len;
585 token = obj->sinfo.sessiontoken;
588 case 8: /* Panther Kerberos Token */
589 tklen = obj->sinfo.cryptedkey_len;
590 token = obj->sinfo.cryptedkey;
602 memcpy(rbuf, &tp, sizeof(tklen));
603 rbuf += sizeof(tklen);
604 *rbuflen += sizeof(tklen);
606 memcpy(rbuf, token, tklen);
612 /* ---------------------- */
613 int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
615 DSI *dsi = (DSI *)obj->handle;
625 /* check for guest user */
626 if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
631 memcpy(&type, ibuf, sizeof(type));
633 ibuf += sizeof(type);
635 memcpy(&tklen, ibuf, sizeof(tklen));
636 tklen = ntohl(tklen);
637 ibuf += sizeof(tklen);
639 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
640 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
643 if (tklen != SESSIONTOKEN_LEN) {
646 tklen = sizeof(pid_t);
647 memcpy(&token, ibuf, tklen);
649 /* our stuff is pid + zero pad */
651 for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
657 LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
658 dsi->flags |= DSI_RECONINPROG;
660 /* Deactivate tickle timer */
661 const struct itimerval none = {{0, 0}, {0, 0}};
662 setitimer(ITIMER_REAL, &none, NULL);
664 /* check for old session, possibly transfering session from here to there */
665 if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) != 0)
667 /* write uint16_t DSI request ID */
668 if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
669 LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
672 /* now send our connected AFP client socket */
673 if (send_fd(obj->ipc_fd, dsi->socket) != 0)
675 /* Now see what happens: either afpd master sends us SIGTERM because our session */
676 /* has been transfered to a old disconnected session, or we continue */
679 if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
680 /* Reconnect succeeded, we exit now after sleeping some more */
681 sleep(2); /* sleep some more to give the recon. session time */
682 LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
687 /* Reinstall tickle timer */
688 setitimer(ITIMER_REAL, &dsi->timer, NULL);
690 LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
694 /* ---------------------- */
695 static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
699 if (!len || len > ibuflen)
700 return AFPERR_BADVERS;
702 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
703 for ( i = 0; i < num; i++ ) {
704 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
705 afp_version = afp_versions[ i ].av_number;
706 afp_version_index = i;
710 if ( i == num ) /* An inappropo version */
711 return AFPERR_BADVERS ;
713 if (afp_version >= 30 && obj->proto != AFPPROTO_DSI)
714 return AFPERR_BADVERS ;
717 if (afp_version >= 30 && sizeof(off_t) != 8) {
718 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
719 return AFPERR_BADVERS ;
725 /* ---------------------- */
726 int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
728 struct passwd *pwd = NULL;
735 return send_reply(obj, AFPERR_SHUTDOWN );
738 return send_reply(obj, AFPERR_BADVERS );
741 len = (unsigned char) *ibuf++;
744 i = get_version(obj, ibuf, ibuflen, len);
746 return send_reply(obj, i );
749 return send_reply(obj, AFPERR_BADUAM);
754 len = (unsigned char) *ibuf++;
757 if (!len || len > ibuflen)
758 return send_reply(obj, AFPERR_BADUAM);
760 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
761 return send_reply(obj, AFPERR_BADUAM);
765 if (AFP_OK != (i = create_session_key(obj)) )
766 return send_reply(obj, i);
768 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
770 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
771 return send_reply(obj, i);
773 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
776 /* ---------------------- */
777 int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
779 struct passwd *pwd = NULL;
789 return send_reply(obj, AFPERR_SHUTDOWN );
792 return send_reply(obj, AFPERR_BADVERS );
798 len = (unsigned char) *ibuf;
802 i = get_version(obj, ibuf, ibuflen, len);
804 return send_reply(obj, i );
807 return send_reply(obj, AFPERR_BADUAM);
812 len = (unsigned char) *ibuf;
816 if (!len || len > ibuflen)
817 return send_reply(obj, AFPERR_BADUAM);
819 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
820 return send_reply(obj, AFPERR_BADUAM);
824 if (!afp_uam->u.uam_login.login_ext) {
825 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
826 return send_reply(obj, AFPERR_BADUAM);
829 if (ibuflen <= 1 +sizeof(len16))
830 return send_reply(obj, AFPERR_PARAM);
836 return send_reply(obj, AFPERR_PARAM);
838 memcpy(&len16, ibuf, sizeof(len16));
839 ibuf += sizeof(len16);
840 ibuflen -= sizeof(len16);
843 return send_reply(obj, AFPERR_PARAM);
847 /* directory service name */
849 return send_reply(obj, AFPERR_PARAM);
858 return send_reply(obj, AFPERR_PARAM);
859 len = (unsigned char) *ibuf;
864 /* With "No User Authen" it is equal */
865 if (ibuflen < sizeof(len16))
866 return send_reply(obj, AFPERR_PARAM);
867 memcpy(&len16, ibuf, sizeof(len16));
868 ibuf += sizeof(len16);
869 ibuflen -= sizeof(len16);
873 return send_reply(obj, AFPERR_PARAM);
877 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
878 return send_reply(obj, AFPERR_PARAM);
885 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
890 if (AFP_OK != (i = create_session_key(obj)) ) {
891 return send_reply(obj, i);
894 /* FIXME user name are in UTF8 */
895 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
897 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
898 return send_reply(obj, i);
900 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
903 /* ---------------------- */
904 int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
906 struct passwd *pwd = NULL;
909 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
911 return send_reply(obj, AFPERR_NOTAUTH );
914 ibuf += 2; ibuflen -= 2;
915 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
917 if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
918 return send_reply(obj, err);
920 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
924 int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
926 DSI *dsi = (DSI *)(obj->handle);
928 LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
929 of_close_all_forks();
931 dsi->flags = DSI_AFP_LOGGED_OUT;
938 /* change password --
939 * NOTE: an FPLogin must already have completed successfully for this
940 * to work. this also does a little pre-processing before it hands
943 int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
945 char username[MACFILELEN + 1], *start = ibuf;
954 /* check if password change is allowed, OS-X ignores the flag.
955 * we shouldn't trust the client on this anyway.
956 * not sure about the "right" error code, NOOP for now */
957 if (!(obj->options.passwdbits & PASSWD_SET))
960 /* make sure we can deal w/ this uam */
961 len = (unsigned char) *ibuf++;
962 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
963 return AFPERR_BADUAM;
966 if ((len + 1) & 1) /* pad byte */
969 if ( afp_version < 30) {
970 len = (unsigned char) *ibuf++;
971 if ( len > sizeof(username) - 1) {
974 memcpy(username, ibuf, len);
975 username[ len ] = '\0';
977 if ((len + 1) & 1) /* pad byte */
980 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
981 if ( ibuf[0] != '\0' || ibuf[1] != '\0')
984 len = MIN(sizeof(username), strlen(obj->username));
985 memcpy(username, obj->username, len);
986 username[ len ] = '\0';
990 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
992 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
995 /* send it off to the uam. we really don't use ibuflen right now. */
996 if (ibuflen < (size_t)(ibuf - start))
999 ibuflen -= (ibuf - start);
1000 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
1002 LOG(log_info, logtype_afpd, "password change %s.",
1003 (ret == AFPERR_AUTHCONT) ? "continued" :
1004 (ret ? "failed" : "succeeded"));
1005 if ( ret == AFP_OK )
1013 int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1020 LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
1025 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
1026 memcpy(&bitmap, ibuf, sizeof(bitmap));
1027 bitmap = ntohs(bitmap);
1029 /* deal with error cases. we don't have to worry about
1030 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
1033 return AFPERR_PARAM;
1034 if ((bitmap & USERIBIT_ALL) != bitmap)
1035 return AFPERR_BITMAP;
1037 /* remember place where we store the possibly modified bitmap later */
1038 memcpy(rbuf, ibuf, sizeof(bitmap));
1040 rbuf += sizeof(bitmap);
1041 *rbuflen = sizeof(bitmap);
1043 /* copy the user/group info */
1044 if (bitmap & USERIBIT_USER) {
1045 id = htonl(geteuid());
1046 memcpy(rbuf, &id, sizeof(id));
1048 *rbuflen += sizeof(id);
1051 if (bitmap & USERIBIT_GROUP) {
1052 id = htonl(getegid());
1053 memcpy(rbuf, &id, sizeof(id));
1055 *rbuflen += sizeof(id);
1058 if (bitmap & USERIBIT_UUID) {
1059 if ( ! (obj->options.flags & OPTION_UUID)) {
1060 bitmap &= ~USERIBIT_UUID;
1061 bitmap = htons(bitmap);
1062 memcpy(bitmapp, &bitmap, sizeof(bitmap));
1064 LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
1067 ret = getuuidfromname( obj->username, UUID_USER, uuid);
1069 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
1070 return AFPERR_NOITEM;
1072 LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
1074 memcpy(rbuf, uuid, UUID_BINSIZE);
1075 rbuf += UUID_BINSIZE;
1076 *rbuflen += UUID_BINSIZE;
1080 LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
1084 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
1085 (((type) == UAM_SERVER_CHANGEPW) ? \
1086 &uam_changepw : NULL))
1088 /* just do a linked list search. this could be sped up with a hashed
1089 * list, but i doubt anyone's going to have enough uams to matter. */
1090 struct uam_obj *auth_uamfind(const int type, const char *name,
1093 struct uam_obj *prev, *start;
1095 if (!name || !(start = UAM_LIST(type)))
1099 while ((prev = prev->uam_prev) != start)
1100 if (strndiacasecmp(prev->uam_name, name, len) == 0)
1106 int auth_register(const int type, struct uam_obj *uam)
1108 struct uam_obj *start;
1110 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
1113 if (!(start = UAM_LIST(type)))
1114 return 1; /* we don't know what to do with it, caller must free it */
1116 uam_attach(start, uam);
1120 /* load all of the modules */
1121 int auth_load(const char *path, const char *list)
1123 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
1124 struct uam_mod *mod;
1128 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
1131 strlcpy(buf, list, sizeof(buf));
1132 if ((p = strtok(buf, ",")) == NULL)
1136 if (name[len - 1] != '/') {
1142 strlcpy(name + len, p, sizeof(name) - len);
1143 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
1145 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
1147 if (stat(name, &st) == 0) {
1148 if ((mod = uam_load(name, p))) {
1149 uam_attach(&uam_modules, mod);
1150 LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
1152 LOG(log_error, logtype_afpd, "uam: %s load failure",p);
1155 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
1157 p = strtok(NULL, ",");
1163 /* get rid of all of the uams */
1164 void auth_unload(void)
1166 struct uam_mod *mod, *prev, *start = &uam_modules;
1168 prev = start->uam_prev;
1169 while ((mod = prev) != start) {
1170 prev = prev->uam_prev;