2 * $Id: auth.c,v 1.44.2.3.2.3 2003-09-25 12:23:53 didg 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, tp; /* 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);
419 /* use at least 8 bytes for token as OSX, don't know if it helps */
420 tklen = sizeof(pid_t);
425 memcpy(rbuf, &tp, sizeof(tklen));
426 rbuf += sizeof(tklen);
429 memset(rbuf, 0, tklen);
431 memcpy(rbuf, &token, sizeof(pid_t));
435 /* ---------------------- */
436 int afp_disconnect(obj, ibuf, ibuflen, rbuf, rbuflen )
439 int ibuflen, *rbuflen;
449 memcpy(&type, ibuf, sizeof(type));
451 ibuf += sizeof(type);
453 memcpy(&tklen, ibuf, sizeof(tklen));
454 tklen = ntohl(tklen);
455 ibuf += sizeof(tklen);
457 if (tklen != sizeof(pid_t)) {
460 memcpy(&token, ibuf, tklen);
461 /* killed old session, not easy */
462 server_ipc_write(IPC_KILLTOKEN, tklen, &token);
465 return AFPERR_SESSCLOS; /* was AFP_OK */
468 /* ---------------------- */
469 static int get_version(obj, ibuf, ibuflen, len)
477 if (!len || len > ibuflen)
478 return AFPERR_BADVERS;
480 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
481 for ( i = 0; i < num; i++ ) {
482 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
483 afp_version = afp_versions[ i ].av_number;
484 afp_version_index = i;
488 if ( i == num ) /* An inappropo version */
489 return AFPERR_BADVERS ;
491 if (afp_version >= 30 && obj->proto != AFPPROTO_DSI)
492 return AFPERR_BADVERS ;
495 if (afp_version >= 30 && sizeof(off_t) != 8) {
496 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
497 return AFPERR_BADVERS ;
503 /* ---------------------- */
504 int afp_login(obj, ibuf, ibuflen, rbuf, rbuflen )
507 int ibuflen, *rbuflen;
509 struct passwd *pwd = NULL;
515 return send_reply(obj, AFPERR_SHUTDOWN );
518 return send_reply(obj, AFPERR_BADVERS );
521 len = (unsigned char) *ibuf++;
524 i = get_version(obj, ibuf, ibuflen, len);
526 return send_reply(obj, i );
532 return send_reply(obj, AFPERR_BADUAM);
534 len = (unsigned char) *ibuf++;
537 if (!len || len > ibuflen)
538 return send_reply(obj, AFPERR_BADUAM);
540 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
541 return send_reply(obj, AFPERR_BADUAM);
545 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
547 return send_reply(obj, i);
549 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout));
552 /* ---------------------- */
553 int afp_login_ext(obj, ibuf, ibuflen, rbuf, rbuflen )
556 unsigned int ibuflen, *rbuflen;
558 struct passwd *pwd = NULL;
568 return send_reply(obj, AFPERR_SHUTDOWN );
571 return send_reply(obj, AFPERR_BADVERS );
577 len = (unsigned char) *ibuf;
581 i = get_version(obj, ibuf, ibuflen, len);
583 return send_reply(obj, i );
589 return send_reply(obj, AFPERR_BADUAM);
591 len = (unsigned char) *ibuf;
595 if (!len || len > ibuflen)
596 return send_reply(obj, AFPERR_BADUAM);
598 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
599 return send_reply(obj, AFPERR_BADUAM);
603 if (!afp_uam->u.uam_login.login_ext) {
604 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
605 return send_reply(obj, AFPERR_BADUAM);
608 if (len <= 1 +sizeof(len16))
609 return send_reply(obj, AFPERR_PARAM);
615 return send_reply(obj, AFPERR_PARAM);
617 memcpy(&len16, ibuf, sizeof(len16));
618 ibuf += sizeof(len16);
619 ibuflen -= sizeof(len16);
622 return send_reply(obj, AFPERR_PARAM);
626 /* directory service name */
628 return send_reply(obj, AFPERR_PARAM);
637 return send_reply(obj, AFPERR_PARAM);
638 len = (unsigned char) *ibuf;
643 /* With "No User Authen" it is equal */
644 if (ibuflen < sizeof(len16))
645 return send_reply(obj, AFPERR_PARAM);
646 memcpy(&len16, ibuf, sizeof(len16));
647 ibuf += sizeof(len16);
648 ibuflen -= sizeof(len16);
652 return send_reply(obj, AFPERR_PARAM);
656 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
657 return send_reply(obj, AFPERR_PARAM);
664 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
669 /* FIXME user name are in UTF8 */
670 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
672 return send_reply(obj, i);
674 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout));
677 /* ---------------------- */
678 int afp_logincont(obj, ibuf, ibuflen, rbuf, rbuflen)
681 int ibuflen, *rbuflen;
683 struct passwd *pwd = NULL;
686 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL ) {
688 return send_reply(obj, AFPERR_NOTAUTH );
692 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
695 return send_reply(obj, err);
697 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout));
701 int afp_logout(obj, ibuf, ibuflen, rbuf, rbuflen)
704 int ibuflen, *rbuflen;
706 LOG(log_info, logtype_afpd, "logout %s", obj->username);
714 /* change password --
715 * NOTE: an FPLogin must already have completed successfully for this
716 * to work. this also does a little pre-processing before it hands
719 int afp_changepw(obj, ibuf, ibuflen, rbuf, rbuflen )
722 int ibuflen, *rbuflen;
724 char username[MACFILELEN + 1], *start = ibuf;
733 /* make sure we can deal w/ this uam */
734 len = (unsigned char) *ibuf++;
735 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
736 return AFPERR_BADUAM;
739 if ((len + 1) & 1) /* pad byte */
742 if ( afp_version < 30) {
743 len = (unsigned char) *ibuf++;
744 if ( len > sizeof(username) - 1) {
747 memcpy(username, ibuf, len);
748 username[ len ] = '\0';
750 if ((len + 1) & 1) /* pad byte */
753 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
754 if ( ibuf[0] != '\0' || ibuf[1] != '\0')
757 len = MIN(sizeof(username), strlen(obj->username));
758 memcpy(username, obj->username, len);
759 username[ len ] = '\0';
763 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
765 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
768 /* send it off to the uam. we really don't use ibuflen right now. */
769 ibuflen -= (ibuf - start);
770 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
772 LOG(log_info, logtype_afpd, "password change %s.",
773 (ret == AFPERR_AUTHCONT) ? "continued" :
774 (ret ? "failed" : "succeeded"));
780 int afp_getuserinfo(obj, ibuf, ibuflen, rbuf, rbuflen )
783 int ibuflen, *rbuflen;
792 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
793 memcpy(&bitmap, ibuf, sizeof(bitmap));
794 bitmap = ntohs(bitmap);
796 /* deal with error cases. we don't have to worry about
797 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
801 if ((bitmap & USERIBIT_ALL) != bitmap)
802 return AFPERR_BITMAP;
804 /* copy the bitmap back to reply buffer */
805 memcpy(rbuf, ibuf, sizeof(bitmap));
806 rbuf += sizeof(bitmap);
807 *rbuflen = sizeof(bitmap);
809 /* copy the user/group info */
810 if (bitmap & USERIBIT_USER) {
811 id = htonl(geteuid());
812 memcpy(rbuf, &id, sizeof(id));
814 *rbuflen += sizeof(id);
817 if (bitmap & USERIBIT_GROUP) {
818 id = htonl(getegid());
819 memcpy(rbuf, &id, sizeof(id));
821 *rbuflen += sizeof(id);
827 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
828 (((type) == UAM_SERVER_CHANGEPW) ? \
829 &uam_changepw : NULL))
831 /* just do a linked list search. this could be sped up with a hashed
832 * list, but i doubt anyone's going to have enough uams to matter. */
833 struct uam_obj *auth_uamfind(const int type, const char *name,
836 struct uam_obj *prev, *start;
838 if (!name || !(start = UAM_LIST(type)))
842 while ((prev = prev->uam_prev) != start)
843 if (strndiacasecmp(prev->uam_name, name, len) == 0)
849 int auth_register(const int type, struct uam_obj *uam)
851 struct uam_obj *start;
853 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
856 if (!(start = UAM_LIST(type)))
857 return 0; /* silently fail */
859 uam_attach(start, uam);
863 /* load all of the modules */
864 int auth_load(const char *path, const char *list)
866 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
871 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
874 strncpy(buf, list, sizeof(buf));
875 if ((p = strtok(buf, ",")) == NULL)
879 if (name[len - 1] != '/') {
885 strncpy(name + len, p, sizeof(name) - len);
886 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
888 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
890 if (stat(name, &st) == 0) {
891 if ((mod = uam_load(name, p))) {
892 uam_attach(&uam_modules, mod);
893 LOG(log_info, logtype_afpd, "uam: %s loaded", p);
895 LOG(log_info, logtype_afpd, "uam: %s load failure",p);
898 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
900 p = strtok(NULL, ",");
906 /* get rid of all of the uams */
909 struct uam_mod *mod, *prev, *start = &uam_modules;
911 prev = start->uam_prev;
912 while ((mod = prev) != start) {
913 prev = prev->uam_prev;