2 * $Id: uam.c,v 1.20 2002-02-13 16:44:59 srittau Exp $
4 * Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
18 #else /* STDC_HEADERS */
22 #endif /* HAVE_STRCHR */
23 char *strchr (), *strrchr ();
25 #define memcpy(d,s,n) bcopy ((s), (d), (n))
26 #define memmove(d,s,n) bcopy ((s), (d), (n))
27 #endif /* ! HAVE_MEMCPY */
28 #endif /* STDC_HEADERS */
32 #endif /* HAVE_UNISTD_H */
35 #endif /* HAVE_FCNTL_H */
37 #include <atalk/logger.h>
38 #include <sys/param.h>
39 #include <sys/socket.h>
43 #endif /* HAVE_DLFCN_H */
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
48 #include <netatalk/endian.h>
49 #include <atalk/asp.h>
50 #include <atalk/dsi.h>
51 #include <atalk/afp.h>
52 #include <atalk/util.h>
55 #include "afp_config.h"
66 /* --- server uam functions -- */
68 /* uam_load. uams must have a uam_setup function. */
69 struct uam_mod *uam_load(const char *path, const char *name)
71 char buf[MAXPATHLEN + 1], *p;
75 if ((module = mod_open(path)) == NULL) {
76 LOG(log_error, logtype_default, "uam_load(%s): failed to load: %s", name, mod_error());
80 if ((mod = (struct uam_mod *) malloc(sizeof(struct uam_mod))) == NULL) {
81 LOG(log_error, logtype_default, "uam_load(%s): malloc failed", name);
85 strncpy(buf, name, sizeof(buf));
86 buf[sizeof(buf) - 1] = '\0';
87 if ((p = strchr(buf, '.')))
89 if ((mod->uam_fcn = mod_symbol(module, buf)) == NULL) {
90 LOG(log_error, logtype_default, "uam_load(%s): mod_symbol error for symbol %s",
96 if (mod->uam_fcn->uam_type != UAM_MODULE_SERVER) {
97 LOG(log_error, logtype_default, "uam_load(%s): attempted to load a non-server module",
102 /* version check would go here */
104 if (!mod->uam_fcn->uam_setup ||
105 ((*mod->uam_fcn->uam_setup)(name) < 0)) {
106 LOG(log_error, logtype_default, "uam_load(%s): uam_setup failed", name);
110 mod->uam_module = module;
120 /* unload the module. we check for a cleanup function, but we don't
121 * die if one doesn't exist. however, things are likely to leak without one.
123 void uam_unload(struct uam_mod *mod)
125 if (mod->uam_fcn->uam_cleanup)
126 (*mod->uam_fcn->uam_cleanup)();
127 mod_close(mod->uam_module);
131 /* -- client-side uam functions -- */
133 /* set up stuff for this uam. */
134 int uam_register(const int type, const char *path, const char *name, ...)
142 /* see if it already exists. */
143 if ((uam = auth_uamfind(type, name, strlen(name)))) {
144 if (strcmp(uam->uam_path, path)) {
145 /* it exists, but it's not the same module. */
146 LOG(log_error, logtype_default, "uam_register: \"%s\" already loaded by %s",
154 /* allocate space for uam */
155 if ((uam = calloc(1, sizeof(struct uam_obj))) == NULL)
158 uam->uam_name = name;
159 uam->uam_path = strdup(path);
164 case UAM_SERVER_LOGIN: /* expect three arguments */
165 uam->u.uam_login.login = va_arg(ap, void *);
166 uam->u.uam_login.logincont = va_arg(ap, void *);
167 uam->u.uam_login.logout = va_arg(ap, void *);
169 case UAM_SERVER_CHANGEPW: /* one argument */
170 uam->u.uam_changepw = va_arg(ap, void *);
172 case UAM_SERVER_PRINTAUTH: /* x arguments */
178 /* attach to other uams */
179 if (auth_register(type, uam) < 0) {
188 void uam_unregister(const int type, const char *name)
195 uam = auth_uamfind(type, name, strlen(name));
196 if (!uam || --uam->uam_count > 0)
199 auth_unregister(uam);
204 /* --- helper functions for plugin uams --- */
206 struct passwd *uam_getname(char *name, const int len)
208 struct passwd *pwent;
212 if ((pwent = getpwnam(name)))
215 #ifndef NO_REAL_USER_NAME
216 for (i = 0; i < len; i++)
217 name[i] = tolower(name[i]);
220 while ((pwent = getpwent())) {
221 if ((user = strchr(pwent->pw_gecos, ',')))
223 user = pwent->pw_gecos;
225 /* check against both the gecos and the name fields. the user
226 * might have just used a different capitalization. */
227 if ((strncasecmp(user, name, len) == 0) ||
228 (strncasecmp(pwent->pw_name, name, len) == 0)) {
229 strncpy(name, pwent->pw_name, len);
230 name[len - 1] = '\0';
235 #endif /* ! NO_REAL_USER_NAME */
237 /* os x server doesn't keep anything useful if we do getpwent */
238 return pwent ? getpwnam(name) : NULL;
241 int uam_checkuser(const struct passwd *pwd)
245 if (!pwd || !pwd->pw_shell || (*pwd->pw_shell == '\0'))
248 while ((p = getusershell())) {
249 if ( strcmp( p, pwd->pw_shell ) == 0 )
254 #ifndef DISABLE_SHELLCHECK
256 LOG(log_info, logtype_default, "illegal shell %s for %s", pwd->pw_shell, pwd->pw_name);
259 #endif /* DISABLE_SHELLCHECK */
264 /* afp-specific functions */
265 int uam_afpserver_option(void *private, const int what, void *option,
268 AFPObj *obj = private;
269 char **buf = (char **) option; /* most of the options are this */
277 case UAM_OPTION_USERNAME:
278 *buf = (void *) obj->username;
280 *len = sizeof(obj->username) - 1;
283 case UAM_OPTION_GUEST:
284 *buf = (void *) obj->options.guest;
286 *len = strlen(obj->options.guest);
289 case UAM_OPTION_PASSWDOPT:
294 case UAM_PASSWD_FILENAME:
295 *buf = (void *) obj->options.passwdfile;
296 *len = strlen(obj->options.passwdfile);
299 case UAM_PASSWD_MINLENGTH:
300 *((int *) option) = obj->options.passwdminlen;
301 *len = sizeof(obj->options.passwdminlen);
304 case UAM_PASSWD_MAXFAIL:
305 *((int *) option) = obj->options.loginmaxfail;
306 *len = sizeof(obj->options.loginmaxfail);
309 case UAM_PASSWD_EXPIRETIME: /* not implemented */
316 case UAM_OPTION_SIGNATURE:
317 *buf = (void *) (((AFPConfig *)obj->config)->signature);
322 case UAM_OPTION_RANDNUM: /* returns a random number in 4-byte units. */
323 if (!len || (*len < 0) || (*len % sizeof(result)))
326 /* construct a random number */
327 if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
330 char *randnum = (char *) option;
333 if (gettimeofday(&tv, &tz) < 0)
335 srandom(tv.tv_sec + (unsigned long) obj + (unsigned long) obj->handle);
336 for (i = 0; i < *len; i += sizeof(result)) {
338 memcpy(randnum + i, &result, sizeof(result));
341 result = read(fd, option, *len);
348 case UAM_OPTION_HOSTNAME:
349 *buf = (void *) obj->options.hostname;
351 *len = strlen(obj->options.hostname);
354 case UAM_OPTION_PROTOCOL:
355 *buf = (void *) obj->proto;
357 case UAM_OPTION_CLIENTNAME:
359 struct DSI *dsi = obj->handle;
362 hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
363 sizeof( struct in_addr ),
364 dsi->client.sin_family );
366 *buf = (void *) hp->h_name;
368 *buf = (void *) inet_ntoa( dsi->client.sin_addr );
371 case UAM_OPTION_COOKIE:
372 /* it's up to the uam to actually store something useful here.
373 * this just passes back a handle to the cookie. the uam side
374 * needs to do something like **buf = (void *) cookie to store
376 *buf = (void *) &obj->uam_cookie;
387 /* if we need to maintain a connection, this is how we do it.
388 * because an action pointer gets passed in, we can stream
390 int uam_afp_read(void *handle, char *buf, int *buflen,
391 int (*action)(void *, void *, const int))
393 AFPObj *obj = handle;
399 switch (obj->proto) {
401 if ((len = asp_wrtcont(obj->handle, buf, buflen )) < 0)
402 goto uam_afp_read_err;
403 return action(handle, buf, *buflen);
407 len = dsi_writeinit(obj->handle, buf, *buflen);
408 if (!len || ((len = action(handle, buf, len)) < 0)) {
409 dsi_writeflush(obj->handle);
410 goto uam_afp_read_err;
413 while ((len = (dsi_write(obj->handle, buf, *buflen)))) {
414 if ((len = action(handle, buf, len)) < 0) {
415 dsi_writeflush(obj->handle);
416 goto uam_afp_read_err;
429 void uam_afp_getcmdline( int *ac, char ***av )
431 afp_get_cmdline( ac, av );
434 int uam_sia_validate_user(sia_collect_func_t * collect, int argc, char **argv,
435 char *hostname, char *username, char *tty,
436 int colinput, char *gssapi, char *passphrase)
437 /* A clone of the Tru64 system function sia_validate_user() that calls
438 * sia_ses_authent() rather than sia_ses_reauthent()
439 * Added extra code to take into account suspected SIA bug whereby it clobbers
440 * the signal handler on SIGALRM (tickle) installed by Netatalk/afpd
443 SIAENTITY *entity = NULL;
444 struct sigaction act;
447 if ((rc=sia_ses_init(&entity, argc, argv, hostname, username, tty,
448 colinput, gssapi)) != SIASUCCESS) {
449 LOG(log_error, logtype_default, "cannot initialise SIA");
453 /* save old action for restoration later */
454 if (sigaction(SIGALRM, NULL, &act))
455 LOG(log_error, logtype_default, "cannot save SIGALRM handler");
457 if ((rc=sia_ses_authent(collect, passphrase, entity)) != SIASUCCESS) {
458 /* restore old action after clobbering by sia_ses_authent() */
459 if (sigaction(SIGALRM, &act, NULL))
460 LOG(log_error, logtype_default, "cannot restore SIGALRM handler");
462 LOG(log_info, logtype_default, "unsuccessful login for %s",
463 (hostname?hostname:"(null)"));
466 LOG(log_info, logtype_default, "successful login for %s",
467 (hostname?hostname:"(null)"));
469 /* restore old action after clobbering by sia_ses_authent() */
470 if (sigaction(SIGALRM, &act, NULL))
471 LOG(log_error, logtype_default, "cannot restore SIGALRM handler");
473 sia_ses_release(&entity);
479 /* --- papd-specific functions (just placeholders) --- */
480 void append(void *pf, char *data, int len)