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 */
17 #endif /* HAVE_UNISTD_H */
20 #endif /* HAVE_FCNTL_H */
22 #include <atalk/logger.h>
23 #include <sys/param.h>
24 #include <sys/socket.h>
28 #endif /* HAVE_DLFCN_H */
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
33 #include <netatalk/endian.h>
34 #include <atalk/asp.h>
35 #include <atalk/dsi.h>
36 #include <atalk/afp.h>
37 #include <atalk/util.h>
40 #include "afp_config.h"
45 #define utf8_encoding() (afp_version >= 30)
47 #define utf8_encoding() (0)
57 /* --- server uam functions -- */
59 /* uam_load. uams must have a uam_setup function. */
60 struct uam_mod *uam_load(const char *path, const char *name)
62 char buf[MAXPATHLEN + 1], *p;
66 if ((module = mod_open(path)) == NULL) {
67 LOG(log_error, logtype_afpd, "uam_load(%s): failed to load: %s", name, mod_error());
71 if ((mod = (struct uam_mod *) malloc(sizeof(struct uam_mod))) == NULL) {
72 LOG(log_error, logtype_afpd, "uam_load(%s): malloc failed", name);
76 strlcpy(buf, name, sizeof(buf));
77 if ((p = strchr(buf, '.')))
80 if ((mod->uam_fcn = mod_symbol(module, buf)) == NULL) {
81 LOG(log_error, logtype_afpd, "uam_load(%s): mod_symbol error for symbol %s",
87 if (mod->uam_fcn->uam_type != UAM_MODULE_SERVER) {
88 LOG(log_error, logtype_afpd, "uam_load(%s): attempted to load a non-server module",
93 /* version check would go here */
95 if (!mod->uam_fcn->uam_setup ||
96 ((*mod->uam_fcn->uam_setup)(name) < 0)) {
97 LOG(log_error, logtype_afpd, "uam_load(%s): uam_setup failed", name);
101 mod->uam_module = module;
111 /* unload the module. we check for a cleanup function, but we don't
112 * die if one doesn't exist. however, things are likely to leak without one.
114 void uam_unload(struct uam_mod *mod)
116 if (mod->uam_fcn->uam_cleanup)
117 (*mod->uam_fcn->uam_cleanup)();
119 mod_close(mod->uam_module);
123 /* -- client-side uam functions -- */
124 /* set up stuff for this uam. */
125 int uam_register(const int type, const char *path, const char *name, ...)
134 /* see if it already exists. */
135 if ((uam = auth_uamfind(type, name, strlen(name)))) {
136 if (strcmp(uam->uam_path, path)) {
137 /* it exists, but it's not the same module. */
138 LOG(log_error, logtype_afpd, "uam_register: \"%s\" already loaded by %s",
146 /* allocate space for uam */
147 if ((uam = calloc(1, sizeof(struct uam_obj))) == NULL)
150 uam->uam_name = name;
151 uam->uam_path = strdup(path);
156 case UAM_SERVER_LOGIN_EXT: /* expect four arguments */
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 *);
160 uam->u.uam_login.login_ext = va_arg(ap, void *);
163 case UAM_SERVER_LOGIN: /* expect three arguments */
164 uam->u.uam_login.login_ext = NULL;
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 ret = auth_register(type, uam);
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 * len: size of name buffer.
209 struct passwd *uam_getname(void *private, char *name, const int len)
211 AFPObj *obj = private;
212 struct passwd *pwent;
213 static char username[256];
214 static char user[256];
215 static char pwname[256];
217 size_t namelen, gecoslen = 0, pwnamelen = 0;
219 if ((pwent = getpwnam(name)))
222 /* if we have a NT domain name try with it */
223 if (obj->options.ntdomain && obj->options.ntseparator) {
224 /* FIXME What about charset ? */
225 size_t ulen = strlen(obj->options.ntdomain) + strlen(obj->options.ntseparator) + strlen(name);
226 if ((p = malloc(ulen +1))) {
227 strcpy(p, obj->options.ntdomain);
228 strcat(p, obj->options.ntseparator);
233 int len = strlen(pwent->pw_name);
234 if (len < MAXUSERLEN) {
235 strncpy(name,pwent->pw_name, MAXUSERLEN);
237 LOG(log_error, logtype_uams, "MAJOR:The name %s is longer than %d",pwent->pw_name,MAXUSERLEN);
244 #ifndef NO_REAL_USER_NAME
246 if ( (size_t) -1 == (namelen = convert_string((utf8_encoding())?CH_UTF8_MAC:obj->options.maccharset,
247 CH_UCS2, name, -1, username, sizeof(username))))
251 while ((pwent = getpwent())) {
252 if ((p = strchr(pwent->pw_gecos, ',')))
255 if ((size_t)-1 == ( gecoslen = convert_string(obj->options.unixcharset, CH_UCS2,
256 pwent->pw_gecos, -1, user, sizeof(username))) )
258 if ((size_t)-1 == ( pwnamelen = convert_string(obj->options.unixcharset, CH_UCS2,
259 pwent->pw_name, -1, pwname, sizeof(username))) )
263 /* check against both the gecos and the name fields. the user
264 * might have just used a different capitalization. */
266 if ( (namelen == gecoslen && strncasecmp_w((ucs2_t*)user, (ucs2_t*)username, len) == 0) ||
267 ( namelen == pwnamelen && strncasecmp_w ( (ucs2_t*) pwname, (ucs2_t*) username, len) == 0)) {
268 strlcpy(name, pwent->pw_name, len);
273 #endif /* ! NO_REAL_USER_NAME */
275 /* os x server doesn't keep anything useful if we do getpwent */
276 return pwent ? getpwnam(name) : NULL;
279 int uam_checkuser(const struct passwd *pwd)
286 #ifndef DISABLE_SHELLCHECK
287 if (!pwd->pw_shell || (*pwd->pw_shell == '\0')) {
288 LOG(log_info, logtype_afpd, "uam_checkuser: User %s does not have a shell", pwd->pw_name);
292 while ((p = getusershell())) {
293 if ( strcmp( p, pwd->pw_shell ) == 0 )
299 LOG(log_info, logtype_afpd, "illegal shell %s for %s", pwd->pw_shell, pwd->pw_name);
302 #endif /* DISABLE_SHELLCHECK */
307 int uam_random_string (AFPObj *obj, char *buf, int len)
313 if ( (len <= 0) || (len % sizeof(result)))
316 /* construct a random number */
317 if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
322 if (gettimeofday(&tv, &tz) < 0)
324 srandom(tv.tv_sec + (unsigned long) obj + (unsigned long) obj->handle);
325 for (i = 0; i < len; i += sizeof(result)) {
327 memcpy(buf + i, &result, sizeof(result));
330 ret = read(fd, buf, len);
338 /* afp-specific functions */
339 int uam_afpserver_option(void *private, const int what, void *option,
342 AFPObj *obj = private;
343 const char **buf = (const char **) option; /* most of the options are this */
344 struct session_info **sinfo = (struct session_info **) option;
350 case UAM_OPTION_USERNAME:
351 *buf = &(obj->username[0]);
353 *len = sizeof(obj->username) - 1;
356 case UAM_OPTION_GUEST:
357 *buf = obj->options.guest;
359 *len = strlen(obj->options.guest);
362 case UAM_OPTION_PASSWDOPT:
367 case UAM_PASSWD_FILENAME:
368 *buf = obj->options.passwdfile;
369 *len = strlen(obj->options.passwdfile);
372 case UAM_PASSWD_MINLENGTH:
373 *((int *) option) = obj->options.passwdminlen;
374 *len = sizeof(obj->options.passwdminlen);
377 case UAM_PASSWD_MAXFAIL:
378 *((int *) option) = obj->options.loginmaxfail;
379 *len = sizeof(obj->options.loginmaxfail);
382 case UAM_PASSWD_EXPIRETIME: /* not implemented */
389 case UAM_OPTION_SIGNATURE:
390 *buf = (void *) (((AFPConfig *)obj->config)->signature);
395 case UAM_OPTION_RANDNUM: /* returns a random number in 4-byte units. */
399 return uam_random_string(obj, option, *len);
402 case UAM_OPTION_HOSTNAME:
403 *buf = obj->options.hostname;
405 *len = strlen(obj->options.hostname);
408 case UAM_OPTION_PROTOCOL:
409 *((int *) option) = obj->proto;
412 case UAM_OPTION_CLIENTNAME:
414 struct DSI *dsi = obj->handle;
415 const struct sockaddr *sa;
416 static char hbuf[NI_MAXHOST];
418 sa = (struct sockaddr *)&dsi->client;
419 if (getnameinfo(sa, sizeof(dsi->client), hbuf, sizeof(hbuf), NULL, 0, 0) == 0)
422 *buf = getip_string((struct sockaddr *)&dsi->client);
426 case UAM_OPTION_COOKIE:
427 /* it's up to the uam to actually store something useful here.
428 * this just passes back a handle to the cookie. the uam side
429 * needs to do something like **buf = (void *) cookie to store
431 *buf = (void *) &obj->uam_cookie;
433 case UAM_OPTION_KRB5SERVICE:
434 *buf = obj->options.k5service;
436 *len = (*buf)?strlen(*buf):0;
438 case UAM_OPTION_KRB5REALM:
439 *buf = obj->options.k5realm;
441 *len = (*buf)?strlen(*buf):0;
443 case UAM_OPTION_FQDN:
444 *buf = obj->options.fqdn;
446 *len = (*buf)?strlen(*buf):0;
448 case UAM_OPTION_MACCHARSET:
449 *((int *) option) = obj->options.maccharset;
450 *len = sizeof(obj->options.maccharset);
452 case UAM_OPTION_UNIXCHARSET:
453 *((int *) option) = obj->options.unixcharset;
454 *len = sizeof(obj->options.unixcharset);
456 case UAM_OPTION_SESSIONINFO:
457 *sinfo = &(obj->sinfo);
467 /* if we need to maintain a connection, this is how we do it.
468 * because an action pointer gets passed in, we can stream
470 int uam_afp_read(void *handle, char *buf, size_t *buflen,
471 int (*action)(void *, void *, const int))
473 AFPObj *obj = handle;
480 switch (obj->proto) {
482 if ((len = asp_wrtcont(obj->handle, buf, buflen )) < 0)
483 goto uam_afp_read_err;
484 return action(handle, buf, *buflen);
489 len = dsi_writeinit(obj->handle, buf, *buflen);
490 if (!len || ((len = action(handle, buf, len)) < 0)) {
491 dsi_writeflush(obj->handle);
492 goto uam_afp_read_err;
495 while ((len = (dsi_write(obj->handle, buf, *buflen)))) {
496 if ((len = action(handle, buf, len)) < 0) {
497 dsi_writeflush(obj->handle);
498 goto uam_afp_read_err;
513 void uam_afp_getcmdline( int *ac, char ***av )
515 afp_get_cmdline( ac, av );
518 int uam_sia_validate_user(sia_collect_func_t * collect, int argc, char **argv,
519 char *hostname, char *username, char *tty,
520 int colinput, char *gssapi, char *passphrase)
521 /* A clone of the Tru64 system function sia_validate_user() that calls
522 * sia_ses_authent() rather than sia_ses_reauthent()
523 * Added extra code to take into account suspected SIA bug whereby it clobbers
524 * the signal handler on SIGALRM (tickle) installed by Netatalk/afpd
527 SIAENTITY *entity = NULL;
528 struct sigaction act;
531 if ((rc=sia_ses_init(&entity, argc, argv, hostname, username, tty,
532 colinput, gssapi)) != SIASUCCESS) {
533 LOG(log_error, logtype_afpd, "cannot initialise SIA");
537 /* save old action for restoration later */
538 if (sigaction(SIGALRM, NULL, &act))
539 LOG(log_error, logtype_afpd, "cannot save SIGALRM handler");
541 if ((rc=sia_ses_authent(collect, passphrase, entity)) != SIASUCCESS) {
542 /* restore old action after clobbering by sia_ses_authent() */
543 if (sigaction(SIGALRM, &act, NULL))
544 LOG(log_error, logtype_afpd, "cannot restore SIGALRM handler");
546 LOG(log_info, logtype_afpd, "unsuccessful login for %s",
547 (hostname?hostname:"(null)"));
550 LOG(log_info, logtype_afpd, "successful login for %s",
551 (hostname?hostname:"(null)"));
553 /* restore old action after clobbering by sia_ses_authent() */
554 if (sigaction(SIGALRM, &act, NULL))
555 LOG(log_error, logtype_afpd, "cannot restore SIGALRM handler");
557 sia_ses_release(&entity);
563 /* --- papd-specific functions (just placeholders) --- */
566 UAM_MODULE_EXPORT void append(struct papfile *pf _U_, const char *data _U_, int len _U_)