2 * $Id: auth.c,v 1.46 2003-05-16 15:29:26 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 ) );
276 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
277 LOG(log_error, logtype_afpd, "login: %s", strerror(errno) );
278 return AFPERR_BADUAM;
283 if (setregid(pwd->pw_gid, pwd->pw_gid ) < 0 || setreuid(pwd->pw_uid,pwd->pw_uid ) < 0) {
285 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
286 LOG(log_error, logtype_afpd, "login: %s", strerror(errno) );
287 return AFPERR_BADUAM;
291 /* There's probably a better way to do this, but for now, we just
297 #endif /* ADMIN_GRP */
300 afp_switch = postauth_switch;
301 switch (afp_version) {
303 uam_afpserver_action(AFP_ENUMERATE_EXT2, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext2, NULL);
305 uam_afpserver_action(AFP_ENUMERATE_EXT, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext, NULL);
306 uam_afpserver_action(AFP_BYTELOCK_EXT, UAM_AFPSERVER_POSTAUTH, afp_bytelock_ext, NULL);
307 /* catsearch_ext uses the same packet as catsearch FIXME double check this, it wasn't true for enue
309 uam_afpserver_action(AFP_CATSEARCH_EXT, UAM_AFPSERVER_POSTAUTH, afp_catsearch_ext, NULL);
310 uam_afpserver_action(AFP_GETSESSTOKEN, UAM_AFPSERVER_POSTAUTH, afp_getsession, NULL);
311 uam_afpserver_action(AFP_READ_EXT, UAM_AFPSERVER_POSTAUTH, afp_read_ext, NULL);
312 uam_afpserver_action(AFP_WRITE_EXT, UAM_AFPSERVER_POSTAUTH, afp_write_ext, NULL);
313 uam_afpserver_action(AFP_DISCTOLDSESS, UAM_AFPSERVER_POSTAUTH, afp_disconnect, NULL);
314 uam_afpserver_action(AFP_ZZZ, UAM_AFPSERVER_POSTAUTH, afp_zzz, NULL);
318 obj->logout = logout;
322 save_uidgid ( &obj->uidgid );
328 /* ---------------------- */
329 int afp_zzz (obj, ibuf, ibuflen, rbuf, rbuflen ) /* Function 122 */
332 unsigned int ibuflen, *rbuflen;
338 retdata = obj->options.sleep /120;
342 *rbuflen = sizeof(retdata);
343 retdata = htonl(retdata);
344 memcpy(rbuf, &retdata, sizeof(retdata));
347 rbuf += sizeof(retdata);
351 /* ---------------------- */
352 int afp_getsession(obj, ibuf, ibuflen, rbuf, rbuflen )
355 unsigned int ibuflen, *rbuflen;
361 u_int16_t tklen; /* FIXME: spec u_int32_t? */
370 memcpy(&type, ibuf, sizeof(type));
372 ibuf += sizeof(type);
373 ibuflen -= sizeof(type);
378 case 0: /* old version ?*/
380 case 1: /* disconnect */
381 case 2: /* reconnect update id */
382 if (ibuflen >= sizeof(idlen)) {
383 memcpy(&idlen, ibuf, sizeof(idlen));
384 idlen = ntohl(idlen);
385 ibuf += sizeof(idlen);
386 ibuflen -= sizeof(idlen);
387 if (ibuflen < idlen) {
390 /* memcpy (id, ibuf, idlen) */
397 memcpy( &idlen, ibuf, sizeof(idlen));
398 idlen = ntohl(idlen);
399 ibuf += sizeof(idlen);
400 ibuflen -= sizeof(idlen);
401 ibuf += sizeof(boottime);
402 ibuflen -= sizeof(boottime);
403 if (ibuflen < idlen || idlen > (90-10)) {
406 server_ipc_write(IPC_GETSESSION, idlen+8, p );
411 *rbuflen = sizeof(type);
413 memcpy(rbuf, &type, sizeof(type));
414 rbuf += sizeof(type);
416 *rbuflen += sizeof(tklen);
417 tklen = htons(sizeof(pid_t));
418 memcpy(rbuf, &tklen, sizeof(tklen));
419 rbuf += sizeof(tklen);
421 *rbuflen += sizeof(pid_t);
423 memcpy(rbuf, &token, sizeof(pid_t));
427 /* ---------------------- */
428 int afp_disconnect(obj, ibuf, ibuflen, rbuf, rbuflen )
431 int ibuflen, *rbuflen;
441 memcpy(&type, ibuf, sizeof(type));
443 ibuf += sizeof(type);
445 memcpy(&tklen, ibuf, sizeof(tklen));
446 tklen = ntohl(tklen);
447 ibuf += sizeof(tklen);
449 if (tklen != sizeof(pid_t)) {
452 memcpy(&token, ibuf, tklen);
453 /* killed old session, not easy */
454 server_ipc_write(IPC_KILLTOKEN, tklen, &token);
457 return AFPERR_SESSCLOS; /* was AFP_OK */
460 /* ---------------------- */
461 static int get_version(obj, ibuf, ibuflen, len)
469 if (!len || len > ibuflen)
470 return AFPERR_BADVERS;
472 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
473 for ( i = 0; i < num; i++ ) {
474 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
475 afp_version = afp_versions[ i ].av_number;
476 afp_version_index = i;
480 if ( i == num ) /* An inappropo version */
481 return AFPERR_BADVERS ;
483 if (afp_version >= 30 && obj->proto != AFPPROTO_DSI)
484 return AFPERR_BADVERS ;
487 if (afp_version >= 30 && sizeof(off_t) != 8) {
488 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
489 return AFPERR_BADVERS ;
495 /* ---------------------- */
496 int afp_login(obj, ibuf, ibuflen, rbuf, rbuflen )
499 int ibuflen, *rbuflen;
501 struct passwd *pwd = NULL;
507 return send_reply(obj, AFPERR_SHUTDOWN );
510 return send_reply(obj, AFPERR_BADVERS );
513 len = (unsigned char) *ibuf++;
516 i = get_version(obj, ibuf, ibuflen, len);
518 return send_reply(obj, i );
524 return send_reply(obj, AFPERR_BADUAM);
526 len = (unsigned char) *ibuf++;
529 if (!len || len > ibuflen)
530 return send_reply(obj, AFPERR_BADUAM);
532 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
533 return send_reply(obj, AFPERR_BADUAM);
537 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
539 return send_reply(obj, i);
541 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout));
544 /* ---------------------- */
545 int afp_login_ext(obj, ibuf, ibuflen, rbuf, rbuflen )
548 unsigned int ibuflen, *rbuflen;
550 struct passwd *pwd = NULL;
560 return send_reply(obj, AFPERR_SHUTDOWN );
563 return send_reply(obj, AFPERR_BADVERS );
569 len = (unsigned char) *ibuf;
573 i = get_version(obj, ibuf, ibuflen, len);
575 return send_reply(obj, i );
581 return send_reply(obj, AFPERR_BADUAM);
583 len = (unsigned char) *ibuf;
587 if (!len || len > ibuflen)
588 return send_reply(obj, AFPERR_BADUAM);
590 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
591 return send_reply(obj, AFPERR_BADUAM);
595 if (!afp_uam->u.uam_login.login_ext) {
596 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
597 return send_reply(obj, AFPERR_BADUAM);
600 if (len <= 1 +sizeof(len16))
601 return send_reply(obj, AFPERR_PARAM);
607 return send_reply(obj, AFPERR_PARAM);
609 memcpy(&len16, ibuf, sizeof(len16));
610 ibuf += sizeof(len16);
611 ibuflen -= sizeof(len16);
614 return send_reply(obj, AFPERR_PARAM);
618 /* directory service name */
620 return send_reply(obj, AFPERR_PARAM);
629 return send_reply(obj, AFPERR_PARAM);
630 len = (unsigned char) *ibuf;
635 /* With "No User Authen" it is equal */
636 if (ibuflen < sizeof(len16))
637 return send_reply(obj, AFPERR_PARAM);
638 memcpy(&len16, ibuf, sizeof(len16));
639 ibuf += sizeof(len16);
640 ibuflen -= sizeof(len16);
644 return send_reply(obj, AFPERR_PARAM);
648 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
649 return send_reply(obj, AFPERR_PARAM);
656 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
661 /* FIXME user name are in UTF8 */
662 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
664 return send_reply(obj, i);
666 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout));
669 /* ---------------------- */
670 int afp_logincont(obj, ibuf, ibuflen, rbuf, rbuflen)
673 int ibuflen, *rbuflen;
675 struct passwd *pwd = NULL;
678 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL ) {
680 return send_reply(obj, AFPERR_NOTAUTH );
684 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
687 return send_reply(obj, err);
689 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout));
693 int afp_logout(obj, ibuf, ibuflen, rbuf, rbuflen)
696 int ibuflen, *rbuflen;
698 LOG(log_info, logtype_afpd, "logout %s", obj->username);
705 /* change password --
706 * NOTE: an FPLogin must already have completed successfully for this
707 * to work. this also does a little pre-processing before it hands
710 int afp_changepw(obj, ibuf, ibuflen, rbuf, rbuflen )
713 int ibuflen, *rbuflen;
715 char username[MACFILELEN + 1], *start = ibuf;
724 /* make sure we can deal w/ this uam */
725 len = (unsigned char) *ibuf++;
726 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
727 return AFPERR_BADUAM;
730 if ((len + 1) & 1) /* pad byte */
733 len = (unsigned char) *ibuf++;
734 if ( len > sizeof(username) - 1) {
737 memcpy(username, ibuf, len);
738 username[ len ] = '\0';
740 if ((len + 1) & 1) /* pad byte */
743 LOG(log_info, logtype_afpd, "changing password for <%s>", username);
745 if (( pwd = uam_getname( username, sizeof(username))) == NULL )
748 /* send it off to the uam. we really don't use ibuflen right now. */
749 ibuflen -= (ibuf - start);
750 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
752 LOG(log_info, logtype_afpd, "password change %s.",
753 (ret == AFPERR_AUTHCONT) ? "continued" :
754 (ret ? "failed" : "succeeded"));
760 int afp_getuserinfo(obj, ibuf, ibuflen, rbuf, rbuflen )
763 int ibuflen, *rbuflen;
772 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
773 memcpy(&bitmap, ibuf, sizeof(bitmap));
774 bitmap = ntohs(bitmap);
776 /* deal with error cases. we don't have to worry about
777 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
781 if ((bitmap & USERIBIT_ALL) != bitmap)
782 return AFPERR_BITMAP;
784 /* copy the bitmap back to reply buffer */
785 memcpy(rbuf, ibuf, sizeof(bitmap));
786 rbuf += sizeof(bitmap);
787 *rbuflen = sizeof(bitmap);
789 /* copy the user/group info */
790 if (bitmap & USERIBIT_USER) {
791 id = htonl(geteuid());
792 memcpy(rbuf, &id, sizeof(id));
794 *rbuflen += sizeof(id);
797 if (bitmap & USERIBIT_GROUP) {
798 id = htonl(getegid());
799 memcpy(rbuf, &id, sizeof(id));
801 *rbuflen += sizeof(id);
807 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
808 (((type) == UAM_SERVER_CHANGEPW) ? \
809 &uam_changepw : NULL))
811 /* just do a linked list search. this could be sped up with a hashed
812 * list, but i doubt anyone's going to have enough uams to matter. */
813 struct uam_obj *auth_uamfind(const int type, const char *name,
816 struct uam_obj *prev, *start;
818 if (!name || !(start = UAM_LIST(type)))
822 while ((prev = prev->uam_prev) != start)
823 if (strndiacasecmp(prev->uam_name, name, len) == 0)
829 int auth_register(const int type, struct uam_obj *uam)
831 struct uam_obj *start;
833 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
836 if (!(start = UAM_LIST(type)))
837 return 0; /* silently fail */
839 uam_attach(start, uam);
843 /* load all of the modules */
844 int auth_load(const char *path, const char *list)
846 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
851 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
854 strncpy(buf, list, sizeof(buf));
855 if ((p = strtok(buf, ",")) == NULL)
859 if (name[len - 1] != '/') {
865 strncpy(name + len, p, sizeof(name) - len);
866 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
868 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
870 if (stat(name, &st) == 0) {
871 if ((mod = uam_load(name, p))) {
872 uam_attach(&uam_modules, mod);
873 LOG(log_info, logtype_afpd, "uam: %s loaded", p);
875 LOG(log_info, logtype_afpd, "uam: %s load failure",p);
878 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
880 p = strtok(NULL, ",");
886 /* get rid of all of the uams */
889 struct uam_mod *mod, *prev, *start = &uam_modules;
891 prev = start->uam_prev;
892 while ((mod = prev) != start) {
893 prev = prev->uam_prev;