2 * $Id: uam.c,v 1.31 2009-10-29 11:35:58 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 extern int uam_setup(const char *path);
77 /* uam_load. uams must have a uam_setup function. */
78 struct uam_mod *uam_load(const char *path, const char *name)
80 char buf[MAXPATHLEN + 1], *p;
85 if ((module = mod_open(path)) == NULL) {
86 LOG(log_error, logtype_afpd, "uam_load(%s): failed to load: %s", name, mod_error());
91 if ((mod = (struct uam_mod *) malloc(sizeof(struct uam_mod))) == NULL) {
92 LOG(log_error, logtype_afpd, "uam_load(%s): malloc failed", name);
96 strlcpy(buf, name, sizeof(buf));
97 if ((p = strchr(buf, '.')))
101 if ((mod->uam_fcn = mod_symbol(module, buf)) == NULL) {
102 LOG(log_error, logtype_afpd, "uam_load(%s): mod_symbol error for symbol %s",
108 if (mod->uam_fcn->uam_type != UAM_MODULE_SERVER) {
109 LOG(log_error, logtype_afpd, "uam_load(%s): attempted to load a non-server module",
114 /* version check would go here */
116 if (!mod->uam_fcn->uam_setup ||
117 ((*mod->uam_fcn->uam_setup)(name) < 0)) {
118 LOG(log_error, logtype_afpd, "uam_load(%s): uam_setup failed", name);
125 mod->uam_module = module;
135 /* unload the module. we check for a cleanup function, but we don't
136 * die if one doesn't exist. however, things are likely to leak without one.
138 void uam_unload(struct uam_mod *mod)
140 if (mod->uam_fcn->uam_cleanup)
141 (*mod->uam_fcn->uam_cleanup)();
144 mod_close(mod->uam_module);
149 /* -- client-side uam functions -- */
150 /* set up stuff for this uam. */
151 int uam_register(const int type, const char *path, const char *name, ...)
160 /* see if it already exists. */
161 if ((uam = auth_uamfind(type, name, strlen(name)))) {
162 if (strcmp(uam->uam_path, path)) {
163 /* it exists, but it's not the same module. */
164 LOG(log_error, logtype_afpd, "uam_register: \"%s\" already loaded by %s",
172 /* allocate space for uam */
173 if ((uam = calloc(1, sizeof(struct uam_obj))) == NULL)
176 uam->uam_name = name;
177 uam->uam_path = strdup(path);
182 case UAM_SERVER_LOGIN_EXT: /* expect four arguments */
183 uam->u.uam_login.login = va_arg(ap, void *);
184 uam->u.uam_login.logincont = va_arg(ap, void *);
185 uam->u.uam_login.logout = va_arg(ap, void *);
186 uam->u.uam_login.login_ext = va_arg(ap, void *);
189 case UAM_SERVER_LOGIN: /* expect three arguments */
190 uam->u.uam_login.login_ext = NULL;
191 uam->u.uam_login.login = va_arg(ap, void *);
192 uam->u.uam_login.logincont = va_arg(ap, void *);
193 uam->u.uam_login.logout = va_arg(ap, void *);
195 case UAM_SERVER_CHANGEPW: /* one argument */
196 uam->u.uam_changepw = va_arg(ap, void *);
198 case UAM_SERVER_PRINTAUTH: /* x arguments */
204 /* attach to other uams */
205 ret = auth_register(type, uam);
214 void uam_unregister(const int type, const char *name)
221 uam = auth_uamfind(type, name, strlen(name));
222 if (!uam || --uam->uam_count > 0)
225 auth_unregister(uam);
230 /* --- helper functions for plugin uams ---
232 * len: size of name buffer.
235 struct passwd *uam_getname(void *private, char *name, const int len)
237 AFPObj *obj = private;
238 struct passwd *pwent;
239 static char username[256];
240 static char user[256];
241 static char pwname[256];
243 size_t namelen, gecoslen = 0, pwnamelen = 0;
245 if ((pwent = getpwnam(name)))
248 /* if we have a NT domain name try with it */
249 if (obj->options.ntdomain && obj->options.ntseparator) {
250 /* FIXME What about charset ? */
251 size_t ulen = strlen(obj->options.ntdomain) + strlen(obj->options.ntseparator) + strlen(name);
252 if ((p = malloc(ulen +1))) {
253 strcpy(p, obj->options.ntdomain);
254 strcat(p, obj->options.ntseparator);
259 int len = strlen(pwent->pw_name);
260 if (len < MAXUSERLEN) {
261 strncpy(name,pwent->pw_name, MAXUSERLEN);
263 LOG(log_error, logtype_uams, "MAJOR:The name %s is longer than %d",pwent->pw_name,MAXUSERLEN);
270 #ifndef NO_REAL_USER_NAME
272 if ( (size_t) -1 == (namelen = convert_string((utf8_encoding())?CH_UTF8_MAC:obj->options.maccharset,
273 CH_UCS2, name, -1, username, sizeof(username))))
277 while ((pwent = getpwent())) {
278 if ((p = strchr(pwent->pw_gecos, ',')))
281 if ((size_t)-1 == ( gecoslen = convert_string(obj->options.unixcharset, CH_UCS2,
282 pwent->pw_gecos, -1, user, sizeof(username))) )
284 if ((size_t)-1 == ( pwnamelen = convert_string(obj->options.unixcharset, CH_UCS2,
285 pwent->pw_name, -1, pwname, sizeof(username))) )
289 /* check against both the gecos and the name fields. the user
290 * might have just used a different capitalization. */
292 if ( (namelen == gecoslen && strncasecmp_w((ucs2_t*)user, (ucs2_t*)username, len) == 0) ||
293 ( namelen == pwnamelen && strncasecmp_w ( (ucs2_t*) pwname, (ucs2_t*) username, len) == 0)) {
294 strlcpy(name, pwent->pw_name, len);
299 #endif /* ! NO_REAL_USER_NAME */
301 /* os x server doesn't keep anything useful if we do getpwent */
302 return pwent ? getpwnam(name) : NULL;
305 int uam_checkuser(const struct passwd *pwd)
312 #ifndef DISABLE_SHELLCHECK
313 if (!pwd->pw_shell || (*pwd->pw_shell == '\0')) {
314 LOG(log_info, logtype_afpd, "uam_checkuser: User %s does not have a shell", pwd->pw_name);
318 while ((p = getusershell())) {
319 if ( strcmp( p, pwd->pw_shell ) == 0 )
325 LOG(log_info, logtype_afpd, "illegal shell %s for %s", pwd->pw_shell, pwd->pw_name);
328 #endif /* DISABLE_SHELLCHECK */
333 int uam_random_string (AFPObj *obj, char *buf, int len)
339 if ( (len <= 0) || (len % sizeof(result)))
342 /* construct a random number */
343 if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
348 if (gettimeofday(&tv, &tz) < 0)
350 srandom(tv.tv_sec + (unsigned long) obj + (unsigned long) obj->handle);
351 for (i = 0; i < len; i += sizeof(result)) {
353 memcpy(buf + i, &result, sizeof(result));
356 ret = read(fd, buf, len);
364 /* afp-specific functions */
365 int uam_afpserver_option(void *private, const int what, void *option,
368 AFPObj *obj = private;
369 char **buf = (char **) option; /* most of the options are this */
370 struct session_info **sinfo = (struct session_info **) option;
376 case UAM_OPTION_USERNAME:
377 *buf = obj->username;
379 *len = sizeof(obj->username) - 1;
382 case UAM_OPTION_GUEST:
383 *buf = obj->options.guest;
385 *len = strlen(obj->options.guest);
388 case UAM_OPTION_PASSWDOPT:
393 case UAM_PASSWD_FILENAME:
394 *buf = obj->options.passwdfile;
395 *len = strlen(obj->options.passwdfile);
398 case UAM_PASSWD_MINLENGTH:
399 *((int *) option) = obj->options.passwdminlen;
400 *len = sizeof(obj->options.passwdminlen);
403 case UAM_PASSWD_MAXFAIL:
404 *((int *) option) = obj->options.loginmaxfail;
405 *len = sizeof(obj->options.loginmaxfail);
408 case UAM_PASSWD_EXPIRETIME: /* not implemented */
415 case UAM_OPTION_SIGNATURE:
416 *buf = (void *) (((AFPConfig *)obj->config)->signature);
421 case UAM_OPTION_RANDNUM: /* returns a random number in 4-byte units. */
425 return uam_random_string(obj, option, *len);
428 case UAM_OPTION_HOSTNAME:
429 *buf = obj->options.hostname;
431 *len = strlen(obj->options.hostname);
434 case UAM_OPTION_PROTOCOL:
435 *((int *) option) = obj->proto;
438 case UAM_OPTION_CLIENTNAME:
440 struct DSI *dsi = obj->handle;
443 hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
444 sizeof( struct in_addr ),
445 dsi->client.sin_family );
449 *buf = inet_ntoa( dsi->client.sin_addr );
452 case UAM_OPTION_COOKIE:
453 /* it's up to the uam to actually store something useful here.
454 * this just passes back a handle to the cookie. the uam side
455 * needs to do something like **buf = (void *) cookie to store
457 *buf = (void *) &obj->uam_cookie;
459 case UAM_OPTION_KRB5SERVICE:
460 *buf = obj->options.k5service;
462 *len = (*buf)?strlen(*buf):0;
464 case UAM_OPTION_KRB5REALM:
465 *buf = obj->options.k5realm;
467 *len = (*buf)?strlen(*buf):0;
469 case UAM_OPTION_FQDN:
470 *buf = obj->options.fqdn;
472 *len = (*buf)?strlen(*buf):0;
474 case UAM_OPTION_MACCHARSET:
475 *((int *) option) = obj->options.maccharset;
476 *len = sizeof(obj->options.maccharset);
478 case UAM_OPTION_UNIXCHARSET:
479 *((int *) option) = obj->options.unixcharset;
480 *len = sizeof(obj->options.unixcharset);
482 case UAM_OPTION_SESSIONINFO:
483 *sinfo = &(obj->sinfo);
493 /* if we need to maintain a connection, this is how we do it.
494 * because an action pointer gets passed in, we can stream
496 int uam_afp_read(void *handle, char *buf, size_t *buflen,
497 int (*action)(void *, void *, const int))
499 AFPObj *obj = handle;
505 switch (obj->proto) {
507 if ((len = asp_wrtcont(obj->handle, buf, buflen )) < 0)
508 goto uam_afp_read_err;
509 return action(handle, buf, *buflen);
513 len = dsi_writeinit(obj->handle, buf, *buflen);
514 if (!len || ((len = action(handle, buf, len)) < 0)) {
515 dsi_writeflush(obj->handle);
516 goto uam_afp_read_err;
519 while ((len = (dsi_write(obj->handle, buf, *buflen)))) {
520 if ((len = action(handle, buf, len)) < 0) {
521 dsi_writeflush(obj->handle);
522 goto uam_afp_read_err;
535 void uam_afp_getcmdline( int *ac, char ***av )
537 afp_get_cmdline( ac, av );
540 int uam_sia_validate_user(sia_collect_func_t * collect, int argc, char **argv,
541 char *hostname, char *username, char *tty,
542 int colinput, char *gssapi, char *passphrase)
543 /* A clone of the Tru64 system function sia_validate_user() that calls
544 * sia_ses_authent() rather than sia_ses_reauthent()
545 * Added extra code to take into account suspected SIA bug whereby it clobbers
546 * the signal handler on SIGALRM (tickle) installed by Netatalk/afpd
549 SIAENTITY *entity = NULL;
550 struct sigaction act;
553 if ((rc=sia_ses_init(&entity, argc, argv, hostname, username, tty,
554 colinput, gssapi)) != SIASUCCESS) {
555 LOG(log_error, logtype_afpd, "cannot initialise SIA");
559 /* save old action for restoration later */
560 if (sigaction(SIGALRM, NULL, &act))
561 LOG(log_error, logtype_afpd, "cannot save SIGALRM handler");
563 if ((rc=sia_ses_authent(collect, passphrase, entity)) != SIASUCCESS) {
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 LOG(log_info, logtype_afpd, "unsuccessful login for %s",
569 (hostname?hostname:"(null)"));
572 LOG(log_info, logtype_afpd, "successful login for %s",
573 (hostname?hostname:"(null)"));
575 /* restore old action after clobbering by sia_ses_authent() */
576 if (sigaction(SIGALRM, &act, NULL))
577 LOG(log_error, logtype_afpd, "cannot restore SIGALRM handler");
579 sia_ses_release(&entity);
585 /* --- papd-specific functions (just placeholders) --- */
586 void append(void *pf _U_, char *data _U_, int len _U_)