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>
53 #include <atalk/globals.h>
55 #include "afp_config.h"
59 #define utf8_encoding() (afp_version >= 30)
68 /* --- server uam functions -- */
70 /* uam_load. uams must have a uam_setup function. */
71 struct uam_mod *uam_load(const char *path, const char *name)
73 char buf[MAXPATHLEN + 1], *p;
77 if ((module = mod_open(path)) == NULL) {
78 LOG(log_error, logtype_afpd, "uam_load(%s): failed to load: %s", name, mod_error());
82 if ((mod = (struct uam_mod *) malloc(sizeof(struct uam_mod))) == NULL) {
83 LOG(log_error, logtype_afpd, "uam_load(%s): malloc failed", name);
87 strlcpy(buf, name, sizeof(buf));
88 if ((p = strchr(buf, '.')))
91 if ((mod->uam_fcn = mod_symbol(module, buf)) == NULL) {
92 LOG(log_error, logtype_afpd, "uam_load(%s): mod_symbol error for symbol %s",
98 if (mod->uam_fcn->uam_type != UAM_MODULE_SERVER) {
99 LOG(log_error, logtype_afpd, "uam_load(%s): attempted to load a non-server module",
104 /* version check would go here */
106 if (!mod->uam_fcn->uam_setup ||
107 ((*mod->uam_fcn->uam_setup)(name) < 0)) {
108 LOG(log_error, logtype_afpd, "uam_load(%s): uam_setup failed", name);
112 mod->uam_module = module;
122 /* unload the module. we check for a cleanup function, but we don't
123 * die if one doesn't exist. however, things are likely to leak without one.
125 void uam_unload(struct uam_mod *mod)
127 if (mod->uam_fcn->uam_cleanup)
128 (*mod->uam_fcn->uam_cleanup)();
130 mod_close(mod->uam_module);
134 /* -- client-side uam functions -- */
135 /* set up stuff for this uam. */
136 int uam_register(const int type, const char *path, const char *name, ...)
145 /* see if it already exists. */
146 if ((uam = auth_uamfind(type, name, strlen(name)))) {
147 if (strcmp(uam->uam_path, path)) {
148 /* it exists, but it's not the same module. */
149 LOG(log_error, logtype_afpd, "uam_register: \"%s\" already loaded by %s",
157 /* allocate space for uam */
158 if ((uam = calloc(1, sizeof(struct uam_obj))) == NULL)
161 uam->uam_name = name;
162 uam->uam_path = strdup(path);
167 case UAM_SERVER_LOGIN_EXT: /* expect four arguments */
168 uam->u.uam_login.login = va_arg(ap, void *);
169 uam->u.uam_login.logincont = va_arg(ap, void *);
170 uam->u.uam_login.logout = va_arg(ap, void *);
171 uam->u.uam_login.login_ext = va_arg(ap, void *);
174 case UAM_SERVER_LOGIN: /* expect three arguments */
175 uam->u.uam_login.login_ext = NULL;
176 uam->u.uam_login.login = va_arg(ap, void *);
177 uam->u.uam_login.logincont = va_arg(ap, void *);
178 uam->u.uam_login.logout = va_arg(ap, void *);
180 case UAM_SERVER_CHANGEPW: /* one argument */
181 uam->u.uam_changepw = va_arg(ap, void *);
183 case UAM_SERVER_PRINTAUTH: /* x arguments */
189 /* attach to other uams */
190 ret = auth_register(type, uam);
199 void uam_unregister(const int type, const char *name)
206 uam = auth_uamfind(type, name, strlen(name));
207 if (!uam || --uam->uam_count > 0)
210 auth_unregister(uam);
215 /* --- helper functions for plugin uams ---
217 * len: size of name buffer.
220 struct passwd *uam_getname(void *private, char *name, const int len)
222 AFPObj *obj = private;
223 struct passwd *pwent;
224 static char username[256];
225 static char user[256];
226 static char pwname[256];
228 size_t namelen, gecoslen = 0, pwnamelen = 0;
230 if ((pwent = getpwnam(name)))
233 /* if we have a NT domain name try with it */
234 if (obj->options.ntdomain && obj->options.ntseparator) {
235 /* FIXME What about charset ? */
236 size_t ulen = strlen(obj->options.ntdomain) + strlen(obj->options.ntseparator) + strlen(name);
237 if ((p = malloc(ulen +1))) {
238 strcpy(p, obj->options.ntdomain);
239 strcat(p, obj->options.ntseparator);
244 int len = strlen(pwent->pw_name);
245 if (len < MAXUSERLEN) {
246 strncpy(name,pwent->pw_name, MAXUSERLEN);
248 LOG(log_error, logtype_uams, "MAJOR:The name %s is longer than %d",pwent->pw_name,MAXUSERLEN);
255 #ifndef NO_REAL_USER_NAME
257 if ( (size_t) -1 == (namelen = convert_string((utf8_encoding())?CH_UTF8_MAC:obj->options.maccharset,
258 CH_UCS2, name, -1, username, sizeof(username))))
262 while ((pwent = getpwent())) {
263 if ((p = strchr(pwent->pw_gecos, ',')))
266 gecoslen = convert_string(obj->options.unixcharset, CH_UCS2,
267 pwent->pw_gecos, -1, user, sizeof(username));
268 pwnamelen = convert_string(obj->options.unixcharset, CH_UCS2,
269 pwent->pw_name, -1, pwname, sizeof(username));
270 if ((size_t)-1 == gecoslen && (size_t)-1 == pwnamelen)
274 /* check against both the gecos and the name fields. the user
275 * might have just used a different capitalization. */
277 if ( (namelen == gecoslen && strncasecmp_w((ucs2_t*)user, (ucs2_t*)username, len) == 0) ||
278 ( namelen == pwnamelen && strncasecmp_w ( (ucs2_t*) pwname, (ucs2_t*) username, len) == 0)) {
279 strlcpy(name, pwent->pw_name, len);
284 #endif /* ! NO_REAL_USER_NAME */
286 /* os x server doesn't keep anything useful if we do getpwent */
287 return pwent ? getpwnam(name) : NULL;
290 int uam_checkuser(const struct passwd *pwd)
297 #ifndef DISABLE_SHELLCHECK
298 if (!pwd->pw_shell || (*pwd->pw_shell == '\0')) {
299 LOG(log_info, logtype_afpd, "uam_checkuser: User %s does not have a shell", pwd->pw_name);
303 while ((p = getusershell())) {
304 if ( strcmp( p, pwd->pw_shell ) == 0 )
310 LOG(log_info, logtype_afpd, "illegal shell %s for %s", pwd->pw_shell, pwd->pw_name);
313 #endif /* DISABLE_SHELLCHECK */
318 int uam_random_string (AFPObj *obj, char *buf, int len)
324 if ( (len <= 0) || (len % sizeof(result)))
327 /* construct a random number */
328 if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
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(buf + i, &result, sizeof(result));
341 ret = read(fd, buf, len);
349 /* afp-specific functions */
350 int uam_afpserver_option(void *private, const int what, void *option,
353 AFPObj *obj = private;
354 const char **buf = (const char **) option; /* most of the options are this */
355 struct session_info **sinfo = (struct session_info **) option;
361 case UAM_OPTION_USERNAME:
362 *buf = obj->username;
364 *len = sizeof(obj->username) - 1;
367 case UAM_OPTION_GUEST:
368 *buf = obj->options.guest;
370 *len = strlen(obj->options.guest);
373 case UAM_OPTION_PASSWDOPT:
378 case UAM_PASSWD_FILENAME:
379 *buf = obj->options.passwdfile;
380 *len = strlen(obj->options.passwdfile);
383 case UAM_PASSWD_MINLENGTH:
384 *((int *) option) = obj->options.passwdminlen;
385 *len = sizeof(obj->options.passwdminlen);
388 case UAM_PASSWD_MAXFAIL:
389 *((int *) option) = obj->options.loginmaxfail;
390 *len = sizeof(obj->options.loginmaxfail);
393 case UAM_PASSWD_EXPIRETIME: /* not implemented */
400 case UAM_OPTION_SIGNATURE:
401 *buf = (void *) (((AFPConfig *)obj->config)->signature);
406 case UAM_OPTION_RANDNUM: /* returns a random number in 4-byte units. */
410 return uam_random_string(obj, option, *len);
413 case UAM_OPTION_HOSTNAME:
414 *buf = obj->options.hostname;
416 *len = strlen(obj->options.hostname);
419 case UAM_OPTION_PROTOCOL:
420 *((int *) option) = obj->proto;
423 case UAM_OPTION_CLIENTNAME:
425 struct DSI *dsi = obj->handle;
426 const struct sockaddr *sa;
427 static char hbuf[NI_MAXHOST];
429 sa = (struct sockaddr *)&dsi->client;
430 if (getnameinfo(sa, sizeof(dsi->client), hbuf, sizeof(hbuf), NULL, 0, 0) == 0)
433 *buf = getip_string((struct sockaddr *)&dsi->client);
437 case UAM_OPTION_COOKIE:
438 /* it's up to the uam to actually store something useful here.
439 * this just passes back a handle to the cookie. the uam side
440 * needs to do something like **buf = (void *) cookie to store
442 *buf = (void *) &obj->uam_cookie;
444 case UAM_OPTION_KRB5SERVICE:
445 *buf = obj->options.k5service;
447 *len = (*buf)?strlen(*buf):0;
449 case UAM_OPTION_KRB5REALM:
450 *buf = obj->options.k5realm;
452 *len = (*buf)?strlen(*buf):0;
454 case UAM_OPTION_FQDN:
455 *buf = obj->options.fqdn;
457 *len = (*buf)?strlen(*buf):0;
459 case UAM_OPTION_MACCHARSET:
460 *((int *) option) = obj->options.maccharset;
461 *len = sizeof(obj->options.maccharset);
463 case UAM_OPTION_UNIXCHARSET:
464 *((int *) option) = obj->options.unixcharset;
465 *len = sizeof(obj->options.unixcharset);
467 case UAM_OPTION_SESSIONINFO:
468 *sinfo = &(obj->sinfo);
478 /* if we need to maintain a connection, this is how we do it.
479 * because an action pointer gets passed in, we can stream
481 int uam_afp_read(void *handle, char *buf, size_t *buflen,
482 int (*action)(void *, void *, const int))
484 AFPObj *obj = handle;
490 switch (obj->proto) {
493 if ((len = asp_wrtcont(obj->handle, buf, buflen )) < 0)
494 goto uam_afp_read_err;
495 return action(handle, buf, *buflen);
499 len = dsi_writeinit(obj->handle, buf, *buflen);
500 if (!len || ((len = action(handle, buf, len)) < 0)) {
501 dsi_writeflush(obj->handle);
502 goto uam_afp_read_err;
505 while ((len = (dsi_write(obj->handle, buf, *buflen)))) {
506 if ((len = action(handle, buf, len)) < 0) {
507 dsi_writeflush(obj->handle);
508 goto uam_afp_read_err;
523 void uam_afp_getcmdline( int *ac, char ***av )
525 afp_get_cmdline( ac, av );
528 int uam_sia_validate_user(sia_collect_func_t * collect, int argc, char **argv,
529 char *hostname, char *username, char *tty,
530 int colinput, char *gssapi, char *passphrase)
531 /* A clone of the Tru64 system function sia_validate_user() that calls
532 * sia_ses_authent() rather than sia_ses_reauthent()
533 * Added extra code to take into account suspected SIA bug whereby it clobbers
534 * the signal handler on SIGALRM (tickle) installed by Netatalk/afpd
537 SIAENTITY *entity = NULL;
538 struct sigaction act;
541 if ((rc=sia_ses_init(&entity, argc, argv, hostname, username, tty,
542 colinput, gssapi)) != SIASUCCESS) {
543 LOG(log_error, logtype_afpd, "cannot initialise SIA");
547 /* save old action for restoration later */
548 if (sigaction(SIGALRM, NULL, &act))
549 LOG(log_error, logtype_afpd, "cannot save SIGALRM handler");
551 if ((rc=sia_ses_authent(collect, passphrase, entity)) != SIASUCCESS) {
552 /* restore old action after clobbering by sia_ses_authent() */
553 if (sigaction(SIGALRM, &act, NULL))
554 LOG(log_error, logtype_afpd, "cannot restore SIGALRM handler");
556 LOG(log_info, logtype_afpd, "unsuccessful login for %s",
557 (hostname?hostname:"(null)"));
560 LOG(log_info, logtype_afpd, "successful login for %s",
561 (hostname?hostname:"(null)"));
563 /* restore old action after clobbering by sia_ses_authent() */
564 if (sigaction(SIGALRM, &act, NULL))
565 LOG(log_error, logtype_afpd, "cannot restore SIGALRM handler");
567 sia_ses_release(&entity);
573 /* --- papd-specific functions (just placeholders) --- */
576 UAM_MODULE_EXPORT void append(struct papfile *pf _U_, const char *data _U_, int len _U_)