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 <atalk/dsi.h>
30 #include <atalk/afp.h>
31 #include <atalk/util.h>
32 #include <atalk/globals.h>
34 #include "afp_config.h"
38 #define utf8_encoding() (afp_version >= 30)
47 /* --- server uam functions -- */
49 /* uam_load. uams must have a uam_setup function. */
50 struct uam_mod *uam_load(const char *path, const char *name)
52 char buf[MAXPATHLEN + 1], *p;
56 if ((module = mod_open(path)) == NULL) {
57 LOG(log_error, logtype_afpd, "uam_load(%s): failed to load: %s", name, mod_error());
61 if ((mod = (struct uam_mod *) malloc(sizeof(struct uam_mod))) == NULL) {
62 LOG(log_error, logtype_afpd, "uam_load(%s): malloc failed", name);
66 strlcpy(buf, name, sizeof(buf));
67 if ((p = strchr(buf, '.')))
70 if ((mod->uam_fcn = mod_symbol(module, buf)) == NULL) {
71 LOG(log_error, logtype_afpd, "uam_load(%s): mod_symbol error for symbol %s",
77 if (mod->uam_fcn->uam_type != UAM_MODULE_SERVER) {
78 LOG(log_error, logtype_afpd, "uam_load(%s): attempted to load a non-server module",
83 /* version check would go here */
85 if (!mod->uam_fcn->uam_setup ||
86 ((*mod->uam_fcn->uam_setup)(name) < 0)) {
87 LOG(log_error, logtype_afpd, "uam_load(%s): uam_setup failed", name);
91 mod->uam_module = module;
101 /* unload the module. we check for a cleanup function, but we don't
102 * die if one doesn't exist. however, things are likely to leak without one.
104 void uam_unload(struct uam_mod *mod)
106 if (mod->uam_fcn->uam_cleanup)
107 (*mod->uam_fcn->uam_cleanup)();
109 mod_close(mod->uam_module);
113 /* -- client-side uam functions -- */
114 /* set up stuff for this uam. */
115 int uam_register(const int type, const char *path, const char *name, ...)
124 /* see if it already exists. */
125 if ((uam = auth_uamfind(type, name, strlen(name)))) {
126 if (strcmp(uam->uam_path, path)) {
127 /* it exists, but it's not the same module. */
128 LOG(log_error, logtype_afpd, "uam_register: \"%s\" already loaded by %s",
136 /* allocate space for uam */
137 if ((uam = calloc(1, sizeof(struct uam_obj))) == NULL)
140 uam->uam_name = name;
141 uam->uam_path = strdup(path);
146 case UAM_SERVER_LOGIN_EXT: /* expect four arguments */
147 uam->u.uam_login.login = va_arg(ap, void *);
148 uam->u.uam_login.logincont = va_arg(ap, void *);
149 uam->u.uam_login.logout = va_arg(ap, void *);
150 uam->u.uam_login.login_ext = va_arg(ap, void *);
153 case UAM_SERVER_LOGIN: /* expect three arguments */
154 uam->u.uam_login.login_ext = NULL;
155 uam->u.uam_login.login = va_arg(ap, void *);
156 uam->u.uam_login.logincont = va_arg(ap, void *);
157 uam->u.uam_login.logout = va_arg(ap, void *);
159 case UAM_SERVER_CHANGEPW: /* one argument */
160 uam->u.uam_changepw = va_arg(ap, void *);
162 case UAM_SERVER_PRINTAUTH: /* x arguments */
168 /* attach to other uams */
169 ret = auth_register(type, uam);
178 void uam_unregister(const int type, const char *name)
185 uam = auth_uamfind(type, name, strlen(name));
186 if (!uam || --uam->uam_count > 0)
189 auth_unregister(uam);
194 /* --- helper functions for plugin uams ---
196 * len: size of name buffer.
199 struct passwd *uam_getname(void *private, char *name, const int len)
201 AFPObj *obj = private;
202 struct passwd *pwent;
203 static char username[256];
204 static char user[256];
205 static char pwname[256];
207 size_t namelen, gecoslen = 0, pwnamelen = 0;
209 if ((pwent = getpwnam(name)))
212 /* if we have a NT domain name try with it */
213 if (obj->options.ntdomain && obj->options.ntseparator) {
214 /* FIXME What about charset ? */
215 size_t ulen = strlen(obj->options.ntdomain) + strlen(obj->options.ntseparator) + strlen(name);
216 if ((p = malloc(ulen +1))) {
217 strcpy(p, obj->options.ntdomain);
218 strcat(p, obj->options.ntseparator);
223 int len = strlen(pwent->pw_name);
224 if (len < MAXUSERLEN) {
225 strncpy(name,pwent->pw_name, MAXUSERLEN);
227 LOG(log_error, logtype_uams, "MAJOR:The name %s is longer than %d",pwent->pw_name,MAXUSERLEN);
234 #ifndef NO_REAL_USER_NAME
236 if ( (size_t) -1 == (namelen = convert_string((utf8_encoding())?CH_UTF8_MAC:obj->options.maccharset,
237 CH_UCS2, name, -1, username, sizeof(username))))
241 while ((pwent = getpwent())) {
242 if ((p = strchr(pwent->pw_gecos, ',')))
245 gecoslen = convert_string(obj->options.unixcharset, CH_UCS2,
246 pwent->pw_gecos, -1, user, sizeof(username));
247 pwnamelen = convert_string(obj->options.unixcharset, CH_UCS2,
248 pwent->pw_name, -1, pwname, sizeof(username));
249 if ((size_t)-1 == gecoslen && (size_t)-1 == pwnamelen)
253 /* check against both the gecos and the name fields. the user
254 * might have just used a different capitalization. */
256 if ( (namelen == gecoslen && strncasecmp_w((ucs2_t*)user, (ucs2_t*)username, len) == 0) ||
257 ( namelen == pwnamelen && strncasecmp_w ( (ucs2_t*) pwname, (ucs2_t*) username, len) == 0)) {
258 strlcpy(name, pwent->pw_name, len);
263 #endif /* ! NO_REAL_USER_NAME */
265 /* os x server doesn't keep anything useful if we do getpwent */
266 return pwent ? getpwnam(name) : NULL;
269 int uam_checkuser(const struct passwd *pwd)
276 #ifndef DISABLE_SHELLCHECK
277 if (!pwd->pw_shell || (*pwd->pw_shell == '\0')) {
278 LOG(log_info, logtype_afpd, "uam_checkuser: User %s does not have a shell", pwd->pw_name);
282 while ((p = getusershell())) {
283 if ( strcmp( p, pwd->pw_shell ) == 0 )
289 LOG(log_info, logtype_afpd, "illegal shell %s for %s", pwd->pw_shell, pwd->pw_name);
292 #endif /* DISABLE_SHELLCHECK */
297 int uam_random_string (AFPObj *obj, char *buf, int len)
303 if ( (len <= 0) || (len % sizeof(result)))
306 /* construct a random number */
307 if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
312 if (gettimeofday(&tv, &tz) < 0)
314 srandom(tv.tv_sec + (unsigned long) obj + (unsigned long) obj->dsi);
315 for (i = 0; i < len; i += sizeof(result)) {
317 memcpy(buf + i, &result, sizeof(result));
320 ret = read(fd, buf, len);
328 /* afp-specific functions */
329 int uam_afpserver_option(void *private, const int what, void *option,
332 AFPObj *obj = private;
333 const char **buf = (const char **) option; /* most of the options are this */
334 struct session_info **sinfo = (struct session_info **) option;
340 case UAM_OPTION_USERNAME:
341 *buf = &(obj->username[0]);
343 *len = sizeof(obj->username) - 1;
346 case UAM_OPTION_GUEST:
347 *buf = obj->options.guest;
349 *len = strlen(obj->options.guest);
352 case UAM_OPTION_PASSWDOPT:
357 case UAM_PASSWD_FILENAME:
358 *buf = obj->options.passwdfile;
359 *len = strlen(obj->options.passwdfile);
362 case UAM_PASSWD_MINLENGTH:
363 *((int *) option) = obj->options.passwdminlen;
364 *len = sizeof(obj->options.passwdminlen);
367 case UAM_PASSWD_EXPIRETIME: /* not implemented */
374 case UAM_OPTION_SIGNATURE:
375 *buf = (void *) (((AFPConfig *)obj->config)->signature);
380 case UAM_OPTION_RANDNUM: /* returns a random number in 4-byte units. */
384 return uam_random_string(obj, option, *len);
387 case UAM_OPTION_HOSTNAME:
388 *buf = obj->options.hostname;
390 *len = strlen(obj->options.hostname);
393 case UAM_OPTION_PROTOCOL:
394 *((int *) option) = obj->proto;
397 case UAM_OPTION_CLIENTNAME:
399 struct DSI *dsi = obj->dsi;
400 const struct sockaddr *sa;
401 static char hbuf[NI_MAXHOST];
403 sa = (struct sockaddr *)&dsi->client;
404 if (getnameinfo(sa, sizeof(dsi->client), hbuf, sizeof(hbuf), NULL, 0, 0) == 0)
407 *buf = getip_string((struct sockaddr *)&dsi->client);
411 case UAM_OPTION_COOKIE:
412 /* it's up to the uam to actually store something useful here.
413 * this just passes back a handle to the cookie. the uam side
414 * needs to do something like **buf = (void *) cookie to store
416 *buf = (void *) &obj->uam_cookie;
418 case UAM_OPTION_KRB5SERVICE:
419 *buf = obj->options.k5service;
421 *len = (*buf)?strlen(*buf):0;
423 case UAM_OPTION_KRB5REALM:
424 *buf = obj->options.k5realm;
426 *len = (*buf)?strlen(*buf):0;
428 case UAM_OPTION_FQDN:
429 *buf = obj->options.fqdn;
431 *len = (*buf)?strlen(*buf):0;
433 case UAM_OPTION_MACCHARSET:
434 *((int *) option) = obj->options.maccharset;
435 *len = sizeof(obj->options.maccharset);
437 case UAM_OPTION_UNIXCHARSET:
438 *((int *) option) = obj->options.unixcharset;
439 *len = sizeof(obj->options.unixcharset);
441 case UAM_OPTION_SESSIONINFO:
442 *sinfo = &(obj->sinfo);
452 /* if we need to maintain a connection, this is how we do it.
453 * because an action pointer gets passed in, we can stream
455 int uam_afp_read(void *handle, char *buf, size_t *buflen,
456 int (*action)(void *, void *, const int))
458 AFPObj *obj = handle;
464 len = dsi_writeinit(obj->dsi, buf, *buflen);
465 if (!len || ((len = action(handle, buf, len)) < 0)) {
466 dsi_writeflush(obj->dsi);
467 goto uam_afp_read_err;
470 while ((len = (dsi_write(obj->dsi, buf, *buflen)))) {
471 if ((len = action(handle, buf, len)) < 0) {
472 dsi_writeflush(obj->dsi);
473 goto uam_afp_read_err;
484 void uam_afp_getcmdline( int *ac, char ***av )
486 afp_get_cmdline( ac, av );
489 int uam_sia_validate_user(sia_collect_func_t * collect, int argc, char **argv,
490 char *hostname, char *username, char *tty,
491 int colinput, char *gssapi, char *passphrase)
492 /* A clone of the Tru64 system function sia_validate_user() that calls
493 * sia_ses_authent() rather than sia_ses_reauthent()
494 * Added extra code to take into account suspected SIA bug whereby it clobbers
495 * the signal handler on SIGALRM (tickle) installed by Netatalk/afpd
498 SIAENTITY *entity = NULL;
499 struct sigaction act;
502 if ((rc=sia_ses_init(&entity, argc, argv, hostname, username, tty,
503 colinput, gssapi)) != SIASUCCESS) {
504 LOG(log_error, logtype_afpd, "cannot initialise SIA");
508 /* save old action for restoration later */
509 if (sigaction(SIGALRM, NULL, &act))
510 LOG(log_error, logtype_afpd, "cannot save SIGALRM handler");
512 if ((rc=sia_ses_authent(collect, passphrase, entity)) != SIASUCCESS) {
513 /* restore old action after clobbering by sia_ses_authent() */
514 if (sigaction(SIGALRM, &act, NULL))
515 LOG(log_error, logtype_afpd, "cannot restore SIGALRM handler");
517 LOG(log_info, logtype_afpd, "unsuccessful login for %s",
518 (hostname?hostname:"(null)"));
521 LOG(log_info, logtype_afpd, "successful login for %s",
522 (hostname?hostname:"(null)"));
524 /* restore old action after clobbering by sia_ses_authent() */
525 if (sigaction(SIGALRM, &act, NULL))
526 LOG(log_error, logtype_afpd, "cannot restore SIGALRM handler");
528 sia_ses_release(&entity);
534 /* --- papd-specific functions (just placeholders) --- */
537 UAM_MODULE_EXPORT void append(struct papfile *pf _U_, const char *data _U_, int len _U_)