2 * $Id: uam.c,v 1.35 2009-11-08 01:15:31 didg Exp $
4 * Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
18 #include <atalk/logger.h>
19 #include <sys/param.h>
20 #include <sys/socket.h>
24 #endif /* HAVE_DLFCN_H */
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
29 #include <netatalk/endian.h>
30 #include <atalk/asp.h>
31 #include <atalk/dsi.h>
32 #include <atalk/afp.h>
33 #include <atalk/util.h>
36 #include "afp_config.h"
40 #define utf8_encoding() (afp_version >= 30)
49 /* --- server uam functions -- */
51 /* uam_load. uams must have a uam_setup function. */
52 struct uam_mod *uam_load(const char *path, const char *name)
54 char buf[MAXPATHLEN + 1], *p;
58 if ((module = mod_open(path)) == NULL) {
59 LOG(log_error, logtype_afpd, "uam_load(%s): failed to load: %s", name, mod_error());
63 if ((mod = (struct uam_mod *) malloc(sizeof(struct uam_mod))) == NULL) {
64 LOG(log_error, logtype_afpd, "uam_load(%s): malloc failed", name);
68 strlcpy(buf, name, sizeof(buf));
69 if ((p = strchr(buf, '.')))
72 if ((mod->uam_fcn = mod_symbol(module, buf)) == NULL) {
73 LOG(log_error, logtype_afpd, "uam_load(%s): mod_symbol error for symbol %s",
79 if (mod->uam_fcn->uam_type != UAM_MODULE_SERVER) {
80 LOG(log_error, logtype_afpd, "uam_load(%s): attempted to load a non-server module",
85 /* version check would go here */
87 if (!mod->uam_fcn->uam_setup ||
88 ((*mod->uam_fcn->uam_setup)(name) < 0)) {
89 LOG(log_error, logtype_afpd, "uam_load(%s): uam_setup failed", name);
93 mod->uam_module = module;
103 /* unload the module. we check for a cleanup function, but we don't
104 * die if one doesn't exist. however, things are likely to leak without one.
106 void uam_unload(struct uam_mod *mod)
108 if (mod->uam_fcn->uam_cleanup)
109 (*mod->uam_fcn->uam_cleanup)();
111 mod_close(mod->uam_module);
115 /* -- client-side uam functions -- */
116 /* set up stuff for this uam. */
117 int uam_register(const int type, const char *path, const char *name, ...)
126 /* see if it already exists. */
127 if ((uam = auth_uamfind(type, name, strlen(name)))) {
128 if (strcmp(uam->uam_path, path)) {
129 /* it exists, but it's not the same module. */
130 LOG(log_error, logtype_afpd, "uam_register: \"%s\" already loaded by %s",
138 /* allocate space for uam */
139 if ((uam = calloc(1, sizeof(struct uam_obj))) == NULL)
142 uam->uam_name = name;
143 uam->uam_path = strdup(path);
148 case UAM_SERVER_LOGIN_EXT: /* expect four arguments */
149 uam->u.uam_login.login = va_arg(ap, void *);
150 uam->u.uam_login.logincont = va_arg(ap, void *);
151 uam->u.uam_login.logout = va_arg(ap, void *);
152 uam->u.uam_login.login_ext = va_arg(ap, void *);
155 case UAM_SERVER_LOGIN: /* expect three arguments */
156 uam->u.uam_login.login_ext = NULL;
157 uam->u.uam_login.login = va_arg(ap, void *);
158 uam->u.uam_login.logincont = va_arg(ap, void *);
159 uam->u.uam_login.logout = va_arg(ap, void *);
161 case UAM_SERVER_CHANGEPW: /* one argument */
162 uam->u.uam_changepw = va_arg(ap, void *);
164 case UAM_SERVER_PRINTAUTH: /* x arguments */
170 /* attach to other uams */
171 ret = auth_register(type, uam);
180 void uam_unregister(const int type, const char *name)
187 uam = auth_uamfind(type, name, strlen(name));
188 if (!uam || --uam->uam_count > 0)
191 auth_unregister(uam);
196 /* --- helper functions for plugin uams ---
198 * len: size of name buffer.
201 struct passwd *uam_getname(void *private, char *name, const int len)
203 AFPObj *obj = private;
204 struct passwd *pwent;
205 static char username[256];
206 static char user[256];
207 static char pwname[256];
209 size_t namelen, gecoslen = 0, pwnamelen = 0;
211 if ((pwent = getpwnam(name)))
214 /* if we have a NT domain name try with it */
215 if (obj->options.ntdomain && obj->options.ntseparator) {
216 /* FIXME What about charset ? */
217 size_t ulen = strlen(obj->options.ntdomain) + strlen(obj->options.ntseparator) + strlen(name);
218 if ((p = malloc(ulen +1))) {
219 strcpy(p, obj->options.ntdomain);
220 strcat(p, obj->options.ntseparator);
225 int len = strlen(pwent->pw_name);
226 if (len < MAXUSERLEN) {
227 strncpy(name,pwent->pw_name, MAXUSERLEN);
229 LOG(log_error, logtype_uams, "MAJOR:The name %s is longer than %d",pwent->pw_name,MAXUSERLEN);
236 #ifndef NO_REAL_USER_NAME
238 if ( (size_t) -1 == (namelen = convert_string((utf8_encoding())?CH_UTF8_MAC:obj->options.maccharset,
239 CH_UCS2, name, -1, username, sizeof(username))))
243 while ((pwent = getpwent())) {
244 if ((p = strchr(pwent->pw_gecos, ',')))
247 if ((size_t)-1 == ( gecoslen = convert_string(obj->options.unixcharset, CH_UCS2,
248 pwent->pw_gecos, -1, user, sizeof(username))) )
250 if ((size_t)-1 == ( pwnamelen = convert_string(obj->options.unixcharset, CH_UCS2,
251 pwent->pw_name, -1, pwname, sizeof(username))) )
255 /* check against both the gecos and the name fields. the user
256 * might have just used a different capitalization. */
258 if ( (namelen == gecoslen && strncasecmp_w((ucs2_t*)user, (ucs2_t*)username, len) == 0) ||
259 ( namelen == pwnamelen && strncasecmp_w ( (ucs2_t*) pwname, (ucs2_t*) username, len) == 0)) {
260 strlcpy(name, pwent->pw_name, len);
265 #endif /* ! NO_REAL_USER_NAME */
267 /* os x server doesn't keep anything useful if we do getpwent */
268 return pwent ? getpwnam(name) : NULL;
271 int uam_checkuser(const struct passwd *pwd)
278 #ifndef DISABLE_SHELLCHECK
279 if (!pwd->pw_shell || (*pwd->pw_shell == '\0')) {
280 LOG(log_info, logtype_afpd, "uam_checkuser: User %s does not have a shell", pwd->pw_name);
284 while ((p = getusershell())) {
285 if ( strcmp( p, pwd->pw_shell ) == 0 )
291 LOG(log_info, logtype_afpd, "illegal shell %s for %s", pwd->pw_shell, pwd->pw_name);
294 #endif /* DISABLE_SHELLCHECK */
299 int uam_random_string (AFPObj *obj, char *buf, int len)
305 if ( (len <= 0) || (len % sizeof(result)))
308 /* construct a random number */
309 if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
314 if (gettimeofday(&tv, &tz) < 0)
316 srandom(tv.tv_sec + (unsigned long) obj + (unsigned long) obj->handle);
317 for (i = 0; i < len; i += sizeof(result)) {
319 memcpy(buf + i, &result, sizeof(result));
322 ret = read(fd, buf, len);
330 /* afp-specific functions */
331 int uam_afpserver_option(void *private, const int what, void *option,
334 AFPObj *obj = private;
335 const char **buf = (const char **) option; /* most of the options are this */
336 struct session_info **sinfo = (struct session_info **) option;
342 case UAM_OPTION_USERNAME:
343 *buf = &(obj->username[0]);
345 *len = sizeof(obj->username) - 1;
348 case UAM_OPTION_GUEST:
349 *buf = obj->options.guest;
351 *len = strlen(obj->options.guest);
354 case UAM_OPTION_PASSWDOPT:
359 case UAM_PASSWD_FILENAME:
360 *buf = obj->options.passwdfile;
361 *len = strlen(obj->options.passwdfile);
364 case UAM_PASSWD_MINLENGTH:
365 *((int *) option) = obj->options.passwdminlen;
366 *len = sizeof(obj->options.passwdminlen);
369 case UAM_PASSWD_MAXFAIL:
370 *((int *) option) = obj->options.loginmaxfail;
371 *len = sizeof(obj->options.loginmaxfail);
374 case UAM_PASSWD_EXPIRETIME: /* not implemented */
381 case UAM_OPTION_SIGNATURE:
382 *buf = (void *) (((AFPConfig *)obj->config)->signature);
387 case UAM_OPTION_RANDNUM: /* returns a random number in 4-byte units. */
391 return uam_random_string(obj, option, *len);
394 case UAM_OPTION_HOSTNAME:
395 *buf = obj->options.hostname;
397 *len = strlen(obj->options.hostname);
400 case UAM_OPTION_PROTOCOL:
401 *((int *) option) = obj->proto;
404 case UAM_OPTION_CLIENTNAME:
406 struct DSI *dsi = obj->handle;
407 const struct sockaddr *sa;
408 static char hbuf[NI_MAXHOST];
410 sa = (struct sockaddr *)&dsi->client;
411 if (getnameinfo(sa, sizeof(dsi->client), hbuf, sizeof(hbuf), NULL, 0, 0) == 0)
414 *buf = getip_string((struct sockaddr *)&dsi->client);
418 case UAM_OPTION_COOKIE:
419 /* it's up to the uam to actually store something useful here.
420 * this just passes back a handle to the cookie. the uam side
421 * needs to do something like **buf = (void *) cookie to store
423 *buf = (void *) &obj->uam_cookie;
425 case UAM_OPTION_KRB5SERVICE:
426 *buf = obj->options.k5service;
428 *len = (*buf)?strlen(*buf):0;
430 case UAM_OPTION_KRB5REALM:
431 *buf = obj->options.k5realm;
433 *len = (*buf)?strlen(*buf):0;
435 case UAM_OPTION_FQDN:
436 *buf = obj->options.fqdn;
438 *len = (*buf)?strlen(*buf):0;
440 case UAM_OPTION_MACCHARSET:
441 *((int *) option) = obj->options.maccharset;
442 *len = sizeof(obj->options.maccharset);
444 case UAM_OPTION_UNIXCHARSET:
445 *((int *) option) = obj->options.unixcharset;
446 *len = sizeof(obj->options.unixcharset);
448 case UAM_OPTION_SESSIONINFO:
449 *sinfo = &(obj->sinfo);
459 /* if we need to maintain a connection, this is how we do it.
460 * because an action pointer gets passed in, we can stream
462 int uam_afp_read(void *handle, char *buf, size_t *buflen,
463 int (*action)(void *, void *, const int))
465 AFPObj *obj = handle;
472 switch (obj->proto) {
474 if ((len = asp_wrtcont(obj->handle, buf, buflen )) < 0)
475 goto uam_afp_read_err;
476 return action(handle, buf, *buflen);
481 len = dsi_writeinit(obj->handle, buf, *buflen);
482 if (!len || ((len = action(handle, buf, len)) < 0)) {
483 dsi_writeflush(obj->handle);
484 goto uam_afp_read_err;
487 while ((len = (dsi_write(obj->handle, buf, *buflen)))) {
488 if ((len = action(handle, buf, len)) < 0) {
489 dsi_writeflush(obj->handle);
490 goto uam_afp_read_err;
505 void uam_afp_getcmdline( int *ac, char ***av )
507 afp_get_cmdline( ac, av );
510 int uam_sia_validate_user(sia_collect_func_t * collect, int argc, char **argv,
511 char *hostname, char *username, char *tty,
512 int colinput, char *gssapi, char *passphrase)
513 /* A clone of the Tru64 system function sia_validate_user() that calls
514 * sia_ses_authent() rather than sia_ses_reauthent()
515 * Added extra code to take into account suspected SIA bug whereby it clobbers
516 * the signal handler on SIGALRM (tickle) installed by Netatalk/afpd
519 SIAENTITY *entity = NULL;
520 struct sigaction act;
523 if ((rc=sia_ses_init(&entity, argc, argv, hostname, username, tty,
524 colinput, gssapi)) != SIASUCCESS) {
525 LOG(log_error, logtype_afpd, "cannot initialise SIA");
529 /* save old action for restoration later */
530 if (sigaction(SIGALRM, NULL, &act))
531 LOG(log_error, logtype_afpd, "cannot save SIGALRM handler");
533 if ((rc=sia_ses_authent(collect, passphrase, entity)) != SIASUCCESS) {
534 /* restore old action after clobbering by sia_ses_authent() */
535 if (sigaction(SIGALRM, &act, NULL))
536 LOG(log_error, logtype_afpd, "cannot restore SIGALRM handler");
538 LOG(log_info, logtype_afpd, "unsuccessful login for %s",
539 (hostname?hostname:"(null)"));
542 LOG(log_info, logtype_afpd, "successful login for %s",
543 (hostname?hostname:"(null)"));
545 /* restore old action after clobbering by sia_ses_authent() */
546 if (sigaction(SIGALRM, &act, NULL))
547 LOG(log_error, logtype_afpd, "cannot restore SIGALRM handler");
549 sia_ses_release(&entity);
555 /* --- papd-specific functions (just placeholders) --- */
558 UAM_MODULE_EXPORT void append(struct papfile *pf _U_, const char *data _U_, int len _U_)