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 #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"
60 #define utf8_encoding() (afp_version >= 30)
62 #define utf8_encoding() (0)
72 /* --- server uam functions -- */
74 /* uam_load. uams must have a uam_setup function. */
75 struct uam_mod *uam_load(const char *path, const char *name)
77 char buf[MAXPATHLEN + 1], *p;
81 if ((module = mod_open(path)) == NULL) {
82 LOG(log_error, logtype_afpd, "uam_load(%s): failed to load: %s", name, mod_error());
86 if ((mod = (struct uam_mod *) malloc(sizeof(struct uam_mod))) == NULL) {
87 LOG(log_error, logtype_afpd, "uam_load(%s): malloc failed", name);
91 strlcpy(buf, name, sizeof(buf));
92 if ((p = strchr(buf, '.')))
95 if ((mod->uam_fcn = mod_symbol(module, buf)) == NULL) {
96 LOG(log_error, logtype_afpd, "uam_load(%s): mod_symbol error for symbol %s",
102 if (mod->uam_fcn->uam_type != UAM_MODULE_SERVER) {
103 LOG(log_error, logtype_afpd, "uam_load(%s): attempted to load a non-server module",
108 /* version check would go here */
110 if (!mod->uam_fcn->uam_setup ||
111 ((*mod->uam_fcn->uam_setup)(name) < 0)) {
112 LOG(log_error, logtype_afpd, "uam_load(%s): uam_setup failed", name);
116 mod->uam_module = module;
126 /* unload the module. we check for a cleanup function, but we don't
127 * die if one doesn't exist. however, things are likely to leak without one.
129 void uam_unload(struct uam_mod *mod)
131 if (mod->uam_fcn->uam_cleanup)
132 (*mod->uam_fcn->uam_cleanup)();
134 mod_close(mod->uam_module);
138 /* -- client-side uam functions -- */
139 /* set up stuff for this uam. */
140 int uam_register(const int type, const char *path, const char *name, ...)
149 /* see if it already exists. */
150 if ((uam = auth_uamfind(type, name, strlen(name)))) {
151 if (strcmp(uam->uam_path, path)) {
152 /* it exists, but it's not the same module. */
153 LOG(log_error, logtype_afpd, "uam_register: \"%s\" already loaded by %s",
161 /* allocate space for uam */
162 if ((uam = calloc(1, sizeof(struct uam_obj))) == NULL)
165 uam->uam_name = name;
166 uam->uam_path = strdup(path);
171 case UAM_SERVER_LOGIN_EXT: /* expect four arguments */
172 uam->u.uam_login.login = va_arg(ap, void *);
173 uam->u.uam_login.logincont = va_arg(ap, void *);
174 uam->u.uam_login.logout = va_arg(ap, void *);
175 uam->u.uam_login.login_ext = va_arg(ap, void *);
178 case UAM_SERVER_LOGIN: /* expect three arguments */
179 uam->u.uam_login.login_ext = NULL;
180 uam->u.uam_login.login = va_arg(ap, void *);
181 uam->u.uam_login.logincont = va_arg(ap, void *);
182 uam->u.uam_login.logout = va_arg(ap, void *);
184 case UAM_SERVER_CHANGEPW: /* one argument */
185 uam->u.uam_changepw = va_arg(ap, void *);
187 case UAM_SERVER_PRINTAUTH: /* x arguments */
193 /* attach to other uams */
194 ret = auth_register(type, uam);
203 void uam_unregister(const int type, const char *name)
210 uam = auth_uamfind(type, name, strlen(name));
211 if (!uam || --uam->uam_count > 0)
214 auth_unregister(uam);
219 /* --- helper functions for plugin uams ---
221 * len: size of name buffer.
224 struct passwd *uam_getname(void *private, char *name, const int len)
226 AFPObj *obj = private;
227 struct passwd *pwent;
228 static char username[256];
229 static char user[256];
230 static char pwname[256];
232 size_t namelen, gecoslen = 0, pwnamelen = 0;
234 if ((pwent = getpwnam(name)))
237 /* if we have a NT domain name try with it */
238 if (obj->options.ntdomain && obj->options.ntseparator) {
239 /* FIXME What about charset ? */
240 size_t ulen = strlen(obj->options.ntdomain) + strlen(obj->options.ntseparator) + strlen(name);
241 if ((p = malloc(ulen +1))) {
242 strcpy(p, obj->options.ntdomain);
243 strcat(p, obj->options.ntseparator);
248 int len = strlen(pwent->pw_name);
249 if (len < MAXUSERLEN) {
250 strncpy(name,pwent->pw_name, MAXUSERLEN);
252 LOG(log_error, logtype_uams, "MAJOR:The name %s is longer than %d",pwent->pw_name,MAXUSERLEN);
259 #ifndef NO_REAL_USER_NAME
261 if ( (size_t) -1 == (namelen = convert_string((utf8_encoding())?CH_UTF8_MAC:obj->options.maccharset,
262 CH_UCS2, name, -1, username, sizeof(username))))
266 while ((pwent = getpwent())) {
267 if ((p = strchr(pwent->pw_gecos, ',')))
270 if ((size_t)-1 == ( gecoslen = convert_string(obj->options.unixcharset, CH_UCS2,
271 pwent->pw_gecos, -1, user, sizeof(username))) )
273 if ((size_t)-1 == ( pwnamelen = convert_string(obj->options.unixcharset, CH_UCS2,
274 pwent->pw_name, -1, pwname, sizeof(username))) )
278 /* check against both the gecos and the name fields. the user
279 * might have just used a different capitalization. */
281 if ( (namelen == gecoslen && strncasecmp_w((ucs2_t*)user, (ucs2_t*)username, len) == 0) ||
282 ( namelen == pwnamelen && strncasecmp_w ( (ucs2_t*) pwname, (ucs2_t*) username, len) == 0)) {
283 strlcpy(name, pwent->pw_name, len);
288 #endif /* ! NO_REAL_USER_NAME */
290 /* os x server doesn't keep anything useful if we do getpwent */
291 return pwent ? getpwnam(name) : NULL;
294 int uam_checkuser(const struct passwd *pwd)
301 #ifndef DISABLE_SHELLCHECK
302 if (!pwd->pw_shell || (*pwd->pw_shell == '\0')) {
303 LOG(log_info, logtype_afpd, "uam_checkuser: User %s does not have a shell", pwd->pw_name);
307 while ((p = getusershell())) {
308 if ( strcmp( p, pwd->pw_shell ) == 0 )
314 LOG(log_info, logtype_afpd, "illegal shell %s for %s", pwd->pw_shell, pwd->pw_name);
317 #endif /* DISABLE_SHELLCHECK */
322 int uam_random_string (AFPObj *obj, char *buf, int len)
328 if ( (len <= 0) || (len % sizeof(result)))
331 /* construct a random number */
332 if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
337 if (gettimeofday(&tv, &tz) < 0)
339 srandom(tv.tv_sec + (unsigned long) obj + (unsigned long) obj->handle);
340 for (i = 0; i < len; i += sizeof(result)) {
342 memcpy(buf + i, &result, sizeof(result));
345 ret = read(fd, buf, len);
353 /* afp-specific functions */
354 int uam_afpserver_option(void *private, const int what, void *option,
357 AFPObj *obj = private;
358 const char **buf = (const char **) option; /* most of the options are this */
359 struct session_info **sinfo = (struct session_info **) option;
365 case UAM_OPTION_USERNAME:
366 *buf = obj->username;
368 *len = sizeof(obj->username) - 1;
371 case UAM_OPTION_GUEST:
372 *buf = obj->options.guest;
374 *len = strlen(obj->options.guest);
377 case UAM_OPTION_PASSWDOPT:
382 case UAM_PASSWD_FILENAME:
383 *buf = obj->options.passwdfile;
384 *len = strlen(obj->options.passwdfile);
387 case UAM_PASSWD_MINLENGTH:
388 *((int *) option) = obj->options.passwdminlen;
389 *len = sizeof(obj->options.passwdminlen);
392 case UAM_PASSWD_MAXFAIL:
393 *((int *) option) = obj->options.loginmaxfail;
394 *len = sizeof(obj->options.loginmaxfail);
397 case UAM_PASSWD_EXPIRETIME: /* not implemented */
404 case UAM_OPTION_SIGNATURE:
405 *buf = (void *) (((AFPConfig *)obj->config)->signature);
410 case UAM_OPTION_RANDNUM: /* returns a random number in 4-byte units. */
414 return uam_random_string(obj, option, *len);
417 case UAM_OPTION_HOSTNAME:
418 *buf = obj->options.hostname;
420 *len = strlen(obj->options.hostname);
423 case UAM_OPTION_PROTOCOL:
424 *((int *) option) = obj->proto;
427 case UAM_OPTION_CLIENTNAME:
429 struct DSI *dsi = obj->handle;
430 const struct sockaddr *sa;
431 static char hbuf[NI_MAXHOST];
433 sa = (struct sockaddr *)&dsi->client;
434 if (getnameinfo(sa, sizeof(dsi->client), hbuf, sizeof(hbuf), NULL, 0, 0) == 0)
437 *buf = getip_string((struct sockaddr *)&dsi->client);
441 case UAM_OPTION_COOKIE:
442 /* it's up to the uam to actually store something useful here.
443 * this just passes back a handle to the cookie. the uam side
444 * needs to do something like **buf = (void *) cookie to store
446 *buf = (void *) &obj->uam_cookie;
448 case UAM_OPTION_KRB5SERVICE:
449 *buf = obj->options.k5service;
451 *len = (*buf)?strlen(*buf):0;
453 case UAM_OPTION_KRB5REALM:
454 *buf = obj->options.k5realm;
456 *len = (*buf)?strlen(*buf):0;
458 case UAM_OPTION_FQDN:
459 *buf = obj->options.fqdn;
461 *len = (*buf)?strlen(*buf):0;
463 case UAM_OPTION_MACCHARSET:
464 *((int *) option) = obj->options.maccharset;
465 *len = sizeof(obj->options.maccharset);
467 case UAM_OPTION_UNIXCHARSET:
468 *((int *) option) = obj->options.unixcharset;
469 *len = sizeof(obj->options.unixcharset);
471 case UAM_OPTION_SESSIONINFO:
472 *sinfo = &(obj->sinfo);
482 /* if we need to maintain a connection, this is how we do it.
483 * because an action pointer gets passed in, we can stream
485 int uam_afp_read(void *handle, char *buf, size_t *buflen,
486 int (*action)(void *, void *, const int))
488 AFPObj *obj = handle;
494 switch (obj->proto) {
496 if ((len = asp_wrtcont(obj->handle, buf, buflen )) < 0)
497 goto uam_afp_read_err;
498 return action(handle, buf, *buflen);
502 len = dsi_writeinit(obj->handle, buf, *buflen);
503 if (!len || ((len = action(handle, buf, len)) < 0)) {
504 dsi_writeflush(obj->handle);
505 goto uam_afp_read_err;
508 while ((len = (dsi_write(obj->handle, buf, *buflen)))) {
509 if ((len = action(handle, buf, len)) < 0) {
510 dsi_writeflush(obj->handle);
511 goto uam_afp_read_err;
524 void uam_afp_getcmdline( int *ac, char ***av )
526 afp_get_cmdline( ac, av );
529 int uam_sia_validate_user(sia_collect_func_t * collect, int argc, char **argv,
530 char *hostname, char *username, char *tty,
531 int colinput, char *gssapi, char *passphrase)
532 /* A clone of the Tru64 system function sia_validate_user() that calls
533 * sia_ses_authent() rather than sia_ses_reauthent()
534 * Added extra code to take into account suspected SIA bug whereby it clobbers
535 * the signal handler on SIGALRM (tickle) installed by Netatalk/afpd
538 SIAENTITY *entity = NULL;
539 struct sigaction act;
542 if ((rc=sia_ses_init(&entity, argc, argv, hostname, username, tty,
543 colinput, gssapi)) != SIASUCCESS) {
544 LOG(log_error, logtype_afpd, "cannot initialise SIA");
548 /* save old action for restoration later */
549 if (sigaction(SIGALRM, NULL, &act))
550 LOG(log_error, logtype_afpd, "cannot save SIGALRM handler");
552 if ((rc=sia_ses_authent(collect, passphrase, entity)) != SIASUCCESS) {
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 LOG(log_info, logtype_afpd, "unsuccessful login for %s",
558 (hostname?hostname:"(null)"));
561 LOG(log_info, logtype_afpd, "successful login for %s",
562 (hostname?hostname:"(null)"));
564 /* restore old action after clobbering by sia_ses_authent() */
565 if (sigaction(SIGALRM, &act, NULL))
566 LOG(log_error, logtype_afpd, "cannot restore SIGALRM handler");
568 sia_ses_release(&entity);
574 /* --- papd-specific functions (just placeholders) --- */
577 UAM_MODULE_EXPORT void append(struct papfile *pf _U_, const char *data _U_, int len _U_)