2 * $Id: auth.c,v 1.47 2003-06-09 15:09:19 srittau Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
17 #endif /* HAVE_UNISTD_H */
18 #include <sys/types.h>
19 #include <sys/param.h>
21 #include <netatalk/endian.h>
22 #include <atalk/afp.h>
23 #include <atalk/compat.h>
24 #include <atalk/util.h>
35 #include <atalk/logger.h>
36 #include <atalk/server_ipc.h>
40 #include <arpa/inet.h>
44 extern void afp_get_cmdline( int *ac, char ***av );
56 static int afp_version_index;
59 #if defined( __svr4__ ) && !defined( NGROUPS )
60 #define NGROUPS NGROUPS_MAX
61 #endif /* __svr4__ NGROUPS */
62 #if defined( sun ) && !defined( __svr4__ ) || defined( ultrix )
63 int groups[ NGROUPS ];
64 #else /* sun __svr4__ ultrix */
65 #if defined( __svr4__ ) && !defined( NGROUPS )
66 #define NGROUPS NGROUPS_MAX
67 #endif /* __svr4__ NGROUPS */
68 gid_t groups[ NGROUPS ];
69 #endif /* sun ultrix */
73 * These numbers are scattered throughout the code.
75 static struct afp_versions afp_versions[] = {
76 { "AFPVersion 1.1", 11 },
77 { "AFPVersion 2.0", 20 },
78 { "AFPVersion 2.1", 21 },
86 static struct uam_mod uam_modules = {NULL, NULL, &uam_modules, &uam_modules};
87 static struct uam_obj uam_login = {"", "", 0, {{NULL}}, &uam_login,
89 static struct uam_obj uam_changepw = {"", "", 0, {{NULL}}, &uam_changepw,
92 static struct uam_obj *afp_uam = NULL;
95 void status_versions( data )
102 memcpy(&status, start + AFPSTATUS_VERSOFF, sizeof(status));
103 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ] );
104 data += ntohs( status );
106 for ( i = 0; i < num; i++ ) {
107 len = strlen( afp_versions[ i ].av_name );
109 memcpy( data, afp_versions[ i ].av_name , len );
112 status = htons( data - start );
113 memcpy(start + AFPSTATUS_UAMSOFF, &status, sizeof(status));
116 void status_uams(char *data, const char *authlist)
120 struct uam_obj *uams;
123 memcpy(&status, start + AFPSTATUS_UAMSOFF, sizeof(status));
125 while ((uams = uams->uam_prev) != &uam_login) {
126 if (strstr(authlist, uams->uam_path))
130 data += ntohs( status );
132 while ((uams = uams->uam_prev) != &uam_login) {
133 if (strstr(authlist, uams->uam_path)) {
134 LOG(log_info, logtype_afpd, "uam: \"%s\" available", uams->uam_name);
135 len = strlen( uams->uam_name);
137 memcpy( data, uams->uam_name, len );
143 status = htons(data - start);
144 memcpy(start + AFPSTATUS_ICONOFF, &status, sizeof(status));
147 /* handle errors by closing the connection. this is only needed
148 * by the afp_* functions. */
149 static int send_reply(const AFPObj *obj, const int err)
151 if ((err == AFP_OK) || (err == AFPERR_AUTHCONT))
154 obj->reply(obj->handle, err);
160 static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void))
164 #endif /* ADMIN_GRP */
166 /* UAM had syslog control; afpd needs to reassert itself */
167 set_processname("afpd");
168 syslog_setup(log_debug, logtype_default, logoption_ndelay|logoption_pid, logfacility_daemon);
170 if ( pwd->pw_uid == 0 ) { /* don't allow root login */
171 LOG(log_error, logtype_afpd, "login: root login denied!" );
172 return AFPERR_NOTAUTH;
175 LOG(log_info, logtype_afpd, "login %s (uid %d, gid %d) %s", pwd->pw_name,
176 pwd->pw_uid, pwd->pw_gid , afp_versions[afp_version_index].av_name);
178 if (obj->proto == AFPPROTO_ASP) {
179 ASP asp = obj->handle;
180 int addr_net = ntohs( asp->asp_sat.sat_addr.s_net );
181 int addr_node = asp->asp_sat.sat_addr.s_node;
183 if (obj->options.authprintdir) {
184 if(addr_net && addr_node) { /* Do we have a valid Appletalk address? */
187 int mypid = getpid();
188 struct stat stat_buf;
190 sprintf(nodename, "%s/net%d.%dnode%d", obj->options.authprintdir,
191 addr_net / 256, addr_net % 256, addr_node);
192 LOG(log_info, logtype_afpd, "registering %s (uid %d) on %u.%u as %s",
193 pwd->pw_name, pwd->pw_uid, addr_net, addr_node, nodename);
195 if (stat(nodename, &stat_buf) == 0) { /* file exists */
196 if (S_ISREG(stat_buf.st_mode)) { /* normal file */
198 fp = fopen(nodename, "w");
199 fprintf(fp, "%s:%d\n", pwd->pw_name, mypid);
201 chown( nodename, pwd->pw_uid, -1 );
202 } else { /* somebody is messing with us */
203 LOG(log_error, logtype_afpd, "print authfile %s is not a normal file, it will not be modified", nodename );
205 } else { /* file 'nodename' does not exist */
206 fp = fopen(nodename, "w");
207 fprintf(fp, "%s:%d\n", pwd->pw_name, mypid);
209 chown( nodename, pwd->pw_uid, -1 );
211 } /* if (addr_net && addr_node ) */
212 } /* if (options->authprintdir) */
213 } /* if (obj->proto == AFPPROTO_ASP) */
215 if (initgroups( pwd->pw_name, pwd->pw_gid ) < 0) {
217 LOG(log_info, logtype_afpd, "running with uid %d", geteuid());
218 #else /* RUN_AS_USER */
219 LOG(log_error, logtype_afpd, "login: %s", strerror(errno));
220 return AFPERR_BADUAM;
221 #endif /* RUN_AS_USER */
225 /* Basically if the user is in the admin group, we stay root */
227 if (( ngroups = getgroups( NGROUPS, groups )) < 0 ) {
228 LOG(log_error, logtype_afpd, "login: getgroups: %s", strerror(errno) );
229 return AFPERR_BADUAM;
233 LOG(log_info, logtype_afpd, "obj->options.admingid == %d", obj->options.admingid);
235 if (obj->options.admingid != 0) {
237 for (i = 0; i < ngroups; i++) {
238 if (groups[i] == obj->options.admingid) admin = 1;
243 LOG(log_info, logtype_afpd, "admin login -- %s", pwd->pw_name );
249 struct DSI *dsi = obj->handle;
256 afp_get_cmdline( &argc, &argv );
258 hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
259 sizeof( struct in_addr ),
260 dsi->client.sin_family );
263 clientname = hp->h_name;
265 clientname = inet_ntoa( dsi->client.sin_addr );
267 sprintf( hostname, "%s@%s", pwd->pw_name, clientname );
269 if( sia_become_user( NULL, argc, argv, hostname, pwd->pw_name,
270 NULL, FALSE, NULL, NULL,
271 SIA_BEU_REALLOGIN ) != SIASUCCESS )
272 return AFPERR_BADUAM;
274 LOG(log_info, logtype_afpd, "session from %s (%s)", hostname,
275 inet_ntoa( dsi->client.sin_addr ) );
277 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
278 LOG(log_error, logtype_afpd, "login: %s", strerror(errno) );
279 return AFPERR_BADUAM;
284 if (setregid(pwd->pw_gid, pwd->pw_gid ) < 0 || setreuid(pwd->pw_uid,pwd->pw_uid ) < 0) {
286 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
287 LOG(log_error, logtype_afpd, "login: %s", strerror(errno) );
288 return AFPERR_BADUAM;
292 /* There's probably a better way to do this, but for now, we just
298 #endif /* ADMIN_GRP */
301 afp_switch = postauth_switch;
302 switch (afp_version) {
304 uam_afpserver_action(AFP_ENUMERATE_EXT2, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext2, NULL);
306 uam_afpserver_action(AFP_ENUMERATE_EXT, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext, NULL);
307 uam_afpserver_action(AFP_BYTELOCK_EXT, UAM_AFPSERVER_POSTAUTH, afp_bytelock_ext, NULL);
308 /* catsearch_ext uses the same packet as catsearch FIXME double check this, it wasn't true for enue
310 uam_afpserver_action(AFP_CATSEARCH_EXT, UAM_AFPSERVER_POSTAUTH, afp_catsearch_ext, NULL);
311 uam_afpserver_action(AFP_GETSESSTOKEN, UAM_AFPSERVER_POSTAUTH, afp_getsession, NULL);
312 uam_afpserver_action(AFP_READ_EXT, UAM_AFPSERVER_POSTAUTH, afp_read_ext, NULL);
313 uam_afpserver_action(AFP_WRITE_EXT, UAM_AFPSERVER_POSTAUTH, afp_write_ext, NULL);
314 uam_afpserver_action(AFP_DISCTOLDSESS, UAM_AFPSERVER_POSTAUTH, afp_disconnect, NULL);
315 uam_afpserver_action(AFP_ZZZ, UAM_AFPSERVER_POSTAUTH, afp_zzz, NULL);
319 obj->logout = logout;
323 save_uidgid ( &obj->uidgid );
329 /* ---------------------- */
330 int afp_zzz (obj, ibuf, ibuflen, rbuf, rbuflen ) /* Function 122 */
333 unsigned int ibuflen, *rbuflen;
339 retdata = obj->options.sleep /120;
343 *rbuflen = sizeof(retdata);
344 retdata = htonl(retdata);
345 memcpy(rbuf, &retdata, sizeof(retdata));
348 rbuf += sizeof(retdata);
352 /* ---------------------- */
353 int afp_getsession(obj, ibuf, ibuflen, rbuf, rbuflen )
356 unsigned int ibuflen, *rbuflen;
362 u_int16_t tklen; /* FIXME: spec u_int32_t? */
371 memcpy(&type, ibuf, sizeof(type));
373 ibuf += sizeof(type);
374 ibuflen -= sizeof(type);
379 case 0: /* old version ?*/
381 case 1: /* disconnect */
382 case 2: /* reconnect update id */
383 if (ibuflen >= sizeof(idlen)) {
384 memcpy(&idlen, ibuf, sizeof(idlen));
385 idlen = ntohl(idlen);
386 ibuf += sizeof(idlen);
387 ibuflen -= sizeof(idlen);
388 if (ibuflen < idlen) {
391 /* memcpy (id, ibuf, idlen) */
398 memcpy( &idlen, ibuf, sizeof(idlen));
399 idlen = ntohl(idlen);
400 ibuf += sizeof(idlen);
401 ibuflen -= sizeof(idlen);
402 ibuf += sizeof(boottime);
403 ibuflen -= sizeof(boottime);
404 if (ibuflen < idlen || idlen > (90-10)) {
407 server_ipc_write(IPC_GETSESSION, idlen+8, p );
412 *rbuflen = sizeof(type);
414 memcpy(rbuf, &type, sizeof(type));
415 rbuf += sizeof(type);
417 *rbuflen += sizeof(tklen);
418 tklen = htons(sizeof(pid_t));
419 memcpy(rbuf, &tklen, sizeof(tklen));
420 rbuf += sizeof(tklen);
422 *rbuflen += sizeof(pid_t);
424 memcpy(rbuf, &token, sizeof(pid_t));
428 /* ---------------------- */
429 int afp_disconnect(obj, ibuf, ibuflen, rbuf, rbuflen )
432 int ibuflen, *rbuflen;
442 memcpy(&type, ibuf, sizeof(type));
444 ibuf += sizeof(type);
446 memcpy(&tklen, ibuf, sizeof(tklen));
447 tklen = ntohl(tklen);
448 ibuf += sizeof(tklen);
450 if (tklen != sizeof(pid_t)) {
453 memcpy(&token, ibuf, tklen);
454 /* killed old session, not easy */
455 server_ipc_write(IPC_KILLTOKEN, tklen, &token);
458 return AFPERR_SESSCLOS; /* was AFP_OK */
461 /* ---------------------- */
462 static int get_version(obj, ibuf, ibuflen, len)
470 if (!len || len > ibuflen)
471 return AFPERR_BADVERS;
473 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
474 for ( i = 0; i < num; i++ ) {
475 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
476 afp_version = afp_versions[ i ].av_number;
477 afp_version_index = i;
481 if ( i == num ) /* An inappropo version */
482 return AFPERR_BADVERS ;
484 if (afp_version >= 30 && obj->proto != AFPPROTO_DSI)
485 return AFPERR_BADVERS ;
488 if (afp_version >= 30 && sizeof(off_t) != 8) {
489 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
490 return AFPERR_BADVERS ;
496 /* ---------------------- */
497 int afp_login(obj, ibuf, ibuflen, rbuf, rbuflen )
500 int ibuflen, *rbuflen;
502 struct passwd *pwd = NULL;
508 return send_reply(obj, AFPERR_SHUTDOWN );
511 return send_reply(obj, AFPERR_BADVERS );
514 len = (unsigned char) *ibuf++;
517 i = get_version(obj, ibuf, ibuflen, len);
519 return send_reply(obj, i );
525 return send_reply(obj, AFPERR_BADUAM);
527 len = (unsigned char) *ibuf++;
530 if (!len || len > ibuflen)
531 return send_reply(obj, AFPERR_BADUAM);
533 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
534 return send_reply(obj, AFPERR_BADUAM);
538 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
540 return send_reply(obj, i);
542 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout));
545 /* ---------------------- */
546 int afp_login_ext(obj, ibuf, ibuflen, rbuf, rbuflen )
549 unsigned int ibuflen, *rbuflen;
551 struct passwd *pwd = NULL;
561 return send_reply(obj, AFPERR_SHUTDOWN );
564 return send_reply(obj, AFPERR_BADVERS );
570 len = (unsigned char) *ibuf;
574 i = get_version(obj, ibuf, ibuflen, len);
576 return send_reply(obj, i );
582 return send_reply(obj, AFPERR_BADUAM);
584 len = (unsigned char) *ibuf;
588 if (!len || len > ibuflen)
589 return send_reply(obj, AFPERR_BADUAM);
591 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
592 return send_reply(obj, AFPERR_BADUAM);
596 if (!afp_uam->u.uam_login.login_ext) {
597 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
598 return send_reply(obj, AFPERR_BADUAM);
601 if (len <= 1 +sizeof(len16))
602 return send_reply(obj, AFPERR_PARAM);
608 return send_reply(obj, AFPERR_PARAM);
610 memcpy(&len16, ibuf, sizeof(len16));
611 ibuf += sizeof(len16);
612 ibuflen -= sizeof(len16);
615 return send_reply(obj, AFPERR_PARAM);
619 /* directory service name */
621 return send_reply(obj, AFPERR_PARAM);
630 return send_reply(obj, AFPERR_PARAM);
631 len = (unsigned char) *ibuf;
636 /* With "No User Authen" it is equal */
637 if (ibuflen < sizeof(len16))
638 return send_reply(obj, AFPERR_PARAM);
639 memcpy(&len16, ibuf, sizeof(len16));
640 ibuf += sizeof(len16);
641 ibuflen -= sizeof(len16);
645 return send_reply(obj, AFPERR_PARAM);
649 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
650 return send_reply(obj, AFPERR_PARAM);
657 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
662 /* FIXME user name are in UTF8 */
663 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
665 return send_reply(obj, i);
667 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout));
670 /* ---------------------- */
671 int afp_logincont(obj, ibuf, ibuflen, rbuf, rbuflen)
674 int ibuflen, *rbuflen;
676 struct passwd *pwd = NULL;
679 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL ) {
681 return send_reply(obj, AFPERR_NOTAUTH );
685 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
688 return send_reply(obj, err);
690 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout));
694 int afp_logout(obj, ibuf, ibuflen, rbuf, rbuflen)
697 int ibuflen, *rbuflen;
699 LOG(log_info, logtype_afpd, "logout %s", obj->username);
706 /* change password --
707 * NOTE: an FPLogin must already have completed successfully for this
708 * to work. this also does a little pre-processing before it hands
711 int afp_changepw(obj, ibuf, ibuflen, rbuf, rbuflen )
714 int ibuflen, *rbuflen;
716 char username[MACFILELEN + 1], *start = ibuf;
725 /* make sure we can deal w/ this uam */
726 len = (unsigned char) *ibuf++;
727 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
728 return AFPERR_BADUAM;
731 if ((len + 1) & 1) /* pad byte */
734 len = (unsigned char) *ibuf++;
735 if ( len > sizeof(username) - 1) {
738 memcpy(username, ibuf, len);
739 username[ len ] = '\0';
741 if ((len + 1) & 1) /* pad byte */
744 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
746 if (( pwd = uam_getname( username, sizeof(username))) == NULL )
749 /* send it off to the uam. we really don't use ibuflen right now. */
750 ibuflen -= (ibuf - start);
751 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
753 LOG(log_info, logtype_afpd, "password change %s.",
754 (ret == AFPERR_AUTHCONT) ? "continued" :
755 (ret ? "failed" : "succeeded"));
761 int afp_getuserinfo(obj, ibuf, ibuflen, rbuf, rbuflen )
764 int ibuflen, *rbuflen;
773 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
774 memcpy(&bitmap, ibuf, sizeof(bitmap));
775 bitmap = ntohs(bitmap);
777 /* deal with error cases. we don't have to worry about
778 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
782 if ((bitmap & USERIBIT_ALL) != bitmap)
783 return AFPERR_BITMAP;
785 /* copy the bitmap back to reply buffer */
786 memcpy(rbuf, ibuf, sizeof(bitmap));
787 rbuf += sizeof(bitmap);
788 *rbuflen = sizeof(bitmap);
790 /* copy the user/group info */
791 if (bitmap & USERIBIT_USER) {
792 id = htonl(geteuid());
793 memcpy(rbuf, &id, sizeof(id));
795 *rbuflen += sizeof(id);
798 if (bitmap & USERIBIT_GROUP) {
799 id = htonl(getegid());
800 memcpy(rbuf, &id, sizeof(id));
802 *rbuflen += sizeof(id);
808 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
809 (((type) == UAM_SERVER_CHANGEPW) ? \
810 &uam_changepw : NULL))
812 /* just do a linked list search. this could be sped up with a hashed
813 * list, but i doubt anyone's going to have enough uams to matter. */
814 struct uam_obj *auth_uamfind(const int type, const char *name,
817 struct uam_obj *prev, *start;
819 if (!name || !(start = UAM_LIST(type)))
823 while ((prev = prev->uam_prev) != start)
824 if (strndiacasecmp(prev->uam_name, name, len) == 0)
830 int auth_register(const int type, struct uam_obj *uam)
832 struct uam_obj *start;
834 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
837 if (!(start = UAM_LIST(type)))
838 return 0; /* silently fail */
840 uam_attach(start, uam);
844 /* load all of the modules */
845 int auth_load(const char *path, const char *list)
847 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
852 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
855 strncpy(buf, list, sizeof(buf));
856 if ((p = strtok(buf, ",")) == NULL)
860 if (name[len - 1] != '/') {
866 strncpy(name + len, p, sizeof(name) - len);
867 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
869 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
871 if (stat(name, &st) == 0) {
872 if ((mod = uam_load(name, p))) {
873 uam_attach(&uam_modules, mod);
874 LOG(log_info, logtype_afpd, "uam: %s loaded", p);
876 LOG(log_info, logtype_afpd, "uam: %s load failure",p);
879 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
881 p = strtok(NULL, ",");
887 /* get rid of all of the uams */
890 struct uam_mod *mod, *prev, *start = &uam_modules;
892 prev = start->uam_prev;
893 while ((mod = prev) != start) {
894 prev = prev->uam_prev;