2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
13 #include <sys/types.h>
14 #include <sys/param.h>
16 #include <netatalk/endian.h>
17 #include <atalk/afp.h>
18 #include <atalk/compat.h>
19 #include <atalk/util.h>
40 #if defined( __svr4__ ) && !defined( NGROUPS )
41 #define NGROUPS NGROUPS_MAX
42 #endif __svr4__ NGROUPS
43 #if defined( sun ) && !defined( __svr4__ ) || defined( ultrix )
44 int groups[ NGROUPS ];
45 #else sun __svr4__ ultrix
46 #if defined( __svr4__ ) && !defined( NGROUPS )
47 #define NGROUPS NGROUPS_MAX
48 #endif __svr4__ NGROUPS
49 gid_t groups[ NGROUPS ];
54 * These numbers are scattered throughout the code.
56 static struct afp_versions afp_versions[] = {
57 { "AFPVersion 1.1", 11 },
58 { "AFPVersion 2.0", 20 },
59 { "AFPVersion 2.1", 21 },
63 static struct uam_mod uam_modules = {NULL, NULL, &uam_modules, &uam_modules};
64 static struct uam_obj uam_login = {"", "", 0, {{NULL}}, &uam_login,
66 static struct uam_obj uam_changepw = {"", "", 0, {{NULL}}, &uam_changepw,
69 static struct uam_obj *afp_uam = NULL;
72 /* Variables for CAP style printer authentication */
74 extern int addr_net, addr_node, addr_uid;
75 extern char addr_name[32];
79 syslog (LOG_INFO, "Connection terminated");
82 void status_versions( data )
89 memcpy(&status, start + AFPSTATUS_VERSOFF, sizeof(status));
90 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ] );
91 data += ntohs( status );
93 for ( i = 0; i < num; i++ ) {
94 len = strlen( afp_versions[ i ].av_name );
96 memcpy( data, afp_versions[ i ].av_name , len );
99 status = htons( data - start );
100 memcpy(start + AFPSTATUS_UAMSOFF, &status, sizeof(status));
103 void status_uams(char *data, const char *authlist)
107 struct uam_obj *uams;
110 memcpy(&status, start + AFPSTATUS_UAMSOFF, sizeof(status));
112 while ((uams = uams->uam_prev) != &uam_login) {
113 if (strstr(authlist, uams->uam_path))
117 data += ntohs( status );
119 while ((uams = uams->uam_prev) != &uam_login) {
120 if (strstr(authlist, uams->uam_path)) {
121 syslog(LOG_INFO, "uam: \"%s\" available", uams->uam_name);
122 len = strlen( uams->uam_name);
124 memcpy( data, uams->uam_name, len );
130 status = htons(data - start);
131 memcpy(start + AFPSTATUS_ICONOFF, &status, sizeof(status));
134 /* handle errors by closing the connection. this is only needed
135 * by the afp_* functions. */
136 static int send_reply(const AFPObj *obj, const int err)
138 if ((err == AFP_OK) || (err == AFPERR_AUTHCONT))
141 obj->reply(obj->handle, err);
145 static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void))
153 struct afp_options *options = &obj->options;
156 /* UAM had syslog control; afpd needs to reassert itself */
157 openlog( "afpd", LOG_NDELAY|LOG_PID, LOG_DAEMON);
159 if ( pwd->pw_uid == 0 ) { /* don't allow root login */
160 syslog( LOG_ERR, "login: root login denied!" );
161 return AFPERR_NOTAUTH;
164 syslog( LOG_INFO, "login %s (uid %d, gid %d)", pwd->pw_name,
165 pwd->pw_uid, pwd->pw_gid );
168 if(addr_net && addr_node) { /* Do we have a valid Appletalk address? */
169 addr_uid = pwd->pw_uid;
170 strncpy(addr_name, pwd->pw_name, 32);
171 sprintf(nodename, "%s/net%d.%dnode%d", CAPDIR, addr_net / 256, addr_net % 256, addr_node);
172 syslog (LOG_INFO, "registering %s (uid %d) on %u.%u as %s",
173 addr_name, addr_uid, addr_net, addr_node, nodename);
174 fp = fopen(nodename, "w");
175 fprintf(fp, "%s\n", addr_name);
180 if (initgroups( pwd->pw_name, pwd->pw_gid ) < 0) {
182 syslog(LOG_INFO, "running with uid %d", geteuid());
184 syslog(LOG_ERR, "login: %m");
185 return AFPERR_BADUAM;
190 /* Basically if the user is in the admin group, we stay root */
192 if (( ngroups = getgroups( NGROUPS, groups )) < 0 ) {
193 syslog( LOG_ERR, "login: getgroups: %m" );
194 return AFPERR_BADUAM;
198 syslog(LOG_INFO, "options->admingid == %d", options->admingid);
200 if (options->admingid != 0) {
202 for (i = 0; i < ngroups; i++) {
203 if (groups[i] == options->admingid) admin = 1;
206 if (admin) syslog( LOG_INFO, "admin login -- %s", pwd->pw_name );
209 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
210 syslog( LOG_ERR, "login: %m" );
211 return AFPERR_BADUAM;
214 /* There's probably a better way to do this, but for now, we just
223 afp_switch = postauth_switch;
224 obj->logout = logout;
226 /* Run logout if the process is terminated */
227 if (atexit((void *) forcedlogout))
228 syslog (LOG_INFO, "Unable to set up logout on kill: %m");
229 /* This is a non-critical error, so afpd doesn't need to abort
230 or return an error value */
235 int afp_login(obj, ibuf, ibuflen, rbuf, rbuflen )
238 int ibuflen, *rbuflen;
240 struct passwd *pwd = NULL;
246 return send_reply(obj, AFPERR_SHUTDOWN );
249 len = (unsigned char) *ibuf++;
250 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
251 for ( i = 0; i < num; i++ ) {
252 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
253 afp_version = afp_versions[ i ].av_number;
257 if ( i == num ) /* An inappropo version */
258 return send_reply(obj, AFPERR_BADVERS );
261 len = (unsigned char) *ibuf++;
262 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
263 return send_reply(obj, AFPERR_BADUAM);
266 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
268 return send_reply(obj, i);
270 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout));
274 int afp_logincont(obj, ibuf, ibuflen, rbuf, rbuflen)
277 int ibuflen, *rbuflen;
279 struct passwd *pwd = NULL;
282 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL ) {
284 return send_reply(obj, AFPERR_NOTAUTH );
288 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
291 return send_reply(obj, err);
293 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout));
297 int afp_logout(obj, ibuf, ibuflen, rbuf, rbuflen)
300 int ibuflen, *rbuflen;
302 syslog(LOG_INFO, "logout %s", obj->username);
308 /* change password --
309 * NOTE: an FPLogin must already have completed successfully for this
310 * to work. this also does a little pre-processing before it hands
313 int afp_changepw(obj, ibuf, ibuflen, rbuf, rbuflen )
316 int ibuflen, *rbuflen;
318 char username[MACFILELEN + 1], *start = ibuf;
326 /* make sure we can deal w/ this uam */
327 len = (unsigned char) *ibuf++;
328 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
329 return AFPERR_BADUAM;
332 if ((len + 1) & 1) /* pad byte */
335 len = (unsigned char) *ibuf++;
336 if ( len > sizeof(username) - 1) {
339 memcpy(username, ibuf, len);
340 username[ len ] = '\0';
342 if ((len + 1) & 1) /* pad byte */
345 syslog(LOG_INFO, "changing password for <%s>", username);
347 if (( pwd = uam_getname( username, sizeof(username))) == NULL )
350 /* send it off to the uam. we really don't use ibuflen right now. */
351 ibuflen -= (ibuf - start);
352 len = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
354 syslog(LOG_INFO, "password change %s.",
355 (len == AFPERR_AUTHCONT) ? "continued" :
356 (len ? "failed" : "succeeded"));
362 int afp_getuserinfo(obj, ibuf, ibuflen, rbuf, rbuflen )
365 int ibuflen, *rbuflen;
374 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
375 memcpy(&bitmap, ibuf, sizeof(bitmap));
376 bitmap = ntohs(bitmap);
378 /* deal with error cases. we don't have to worry about
379 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
383 if ((bitmap & USERIBIT_ALL) != bitmap)
384 return AFPERR_BITMAP;
386 /* copy the bitmap back to reply buffer */
387 memcpy(rbuf, ibuf, sizeof(bitmap));
388 rbuf += sizeof(bitmap);
389 *rbuflen = sizeof(bitmap);
391 /* copy the user/group info */
392 if (bitmap & USERIBIT_USER) {
393 id = htonl(geteuid());
394 memcpy(rbuf, &id, sizeof(id));
396 *rbuflen += sizeof(id);
399 if (bitmap & USERIBIT_GROUP) {
400 id = htonl(getegid());
401 memcpy(rbuf, &id, sizeof(id));
403 *rbuflen += sizeof(id);
409 #define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN) ? &uam_login : \
410 (((type) == UAM_SERVER_CHANGEPW) ? \
411 &uam_changepw : NULL))
413 /* just do a linked list search. this could be sped up with a hashed
414 * list, but i doubt anyone's going to have enough uams to matter. */
415 struct uam_obj *auth_uamfind(const int type, const char *name,
418 struct uam_obj *prev, *start;
420 if (!name || !(start = UAM_LIST(type)))
424 while ((prev = prev->uam_prev) != start)
425 if (strndiacasecmp(prev->uam_name, name, len) == 0)
431 int auth_register(const int type, struct uam_obj *uam)
433 struct uam_obj *start;
435 if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
438 if (!(start = UAM_LIST(type)))
439 return 0; /* silently fail */
441 uam_attach(start, uam);
445 /* load all of the modules */
446 int auth_load(const char *path, const char *list)
448 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
453 if (!path || !list || (len = strlen(path)) > sizeof(name) - 2)
456 strncpy(buf, list, sizeof(buf));
457 if ((p = strtok(buf, ",")) == NULL)
461 if (name[len - 1] != '/') {
467 strncpy(name + len, p, sizeof(name) - len);
468 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
469 uam_attach(&uam_modules, mod);
470 syslog(LOG_INFO, "uam: %s loaded", p);
472 p = strtok(NULL, ",");
476 /* get rid of all of the uams */
479 struct uam_mod *mod, *prev, *start = &uam_modules;
481 prev = start->uam_prev;
482 while ((mod = prev) != start) {
483 prev = prev->uam_prev;