2 * $Id: uam.c,v 1.9 2001-06-20 18:33:04 rufustfirefly 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 */
23 #include <sys/param.h>
27 #endif /* HAVE_DLFCN_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"
44 /* --- server uam functions -- */
46 /* uam_load. uams must have a uam_setup function. */
47 struct uam_mod *uam_load(const char *path, const char *name)
49 char buf[MAXPATHLEN + 1], *p;
53 if ((module = mod_open(path)) == NULL) {
54 syslog(LOG_ERR, "uam_load(%s): failed to load: %s", name, mod_error());
58 if ((mod = (struct uam_mod *) malloc(sizeof(struct uam_mod))) == NULL) {
59 syslog(LOG_ERR, "uam_load(%s): malloc failed", name);
63 strncpy(buf, name, sizeof(buf));
64 if ((p = strchr(buf, '.')))
66 if ((mod->uam_fcn = mod_symbol(module, buf)) == NULL) {
67 syslog(LOG_ERR, "uam_load(%s): mod_symbol error for symbol %s",
73 if (mod->uam_fcn->uam_type != UAM_MODULE_SERVER) {
74 syslog(LOG_ERR, "uam_load(%s): attempted to load a non-server module",
79 /* version check would go here */
81 if (!mod->uam_fcn->uam_setup ||
82 ((*mod->uam_fcn->uam_setup)(name) < 0)) {
83 syslog(LOG_ERR, "uam_load(%s): uam_setup failed", name);
87 mod->uam_module = module;
97 /* unload the module. we check for a cleanup function, but we don't
98 * die if one doesn't exist. however, things are likely to leak without one.
100 void uam_unload(struct uam_mod *mod)
102 if (mod->uam_fcn->uam_cleanup)
103 (*mod->uam_fcn->uam_cleanup)();
104 mod_close(mod->uam_module);
108 /* -- client-side uam functions -- */
110 /* set up stuff for this uam. */
111 int uam_register(const int type, const char *path, const char *name, ...)
119 /* see if it already exists. */
120 if ((uam = auth_uamfind(type, name, strlen(name)))) {
121 if (strcmp(uam->uam_path, path)) {
122 /* it exists, but it's not the same module. */
123 syslog(LOG_ERR, "uam_register: \"%s\" already loaded by %s",
131 /* allocate space for uam */
132 if ((uam = calloc(1, sizeof(struct uam_obj))) == NULL)
135 uam->uam_name = name;
136 uam->uam_path = strdup(path);
141 case UAM_SERVER_LOGIN: /* expect three arguments */
142 uam->u.uam_login.login = va_arg(ap, void *);
143 uam->u.uam_login.logincont = va_arg(ap, void *);
144 uam->u.uam_login.logout = va_arg(ap, void *);
146 case UAM_SERVER_CHANGEPW: /* one argument */
147 uam->u.uam_changepw = va_arg(ap, void *);
149 case UAM_SERVER_PRINTAUTH: /* x arguments */
155 /* attach to other uams */
156 if (auth_register(type, uam) < 0) {
165 void uam_unregister(const int type, const char *name)
172 uam = auth_uamfind(type, name, strlen(name));
173 if (!uam || --uam->uam_count > 0)
176 auth_unregister(uam);
181 /* --- helper functions for plugin uams --- */
183 struct passwd *uam_getname(char *name, const int len)
185 struct passwd *pwent;
189 if ((pwent = getpwnam(name)))
192 #ifndef NO_REAL_USER_NAME
193 for (i = 0; i < len; i++)
194 name[i] = tolower(name[i]);
197 while ((pwent = getpwent())) {
198 if ((user = strchr(pwent->pw_gecos, ',')))
200 user = pwent->pw_gecos;
202 /* check against both the gecos and the name fields. the user
203 * might have just used a different capitalization. */
204 if ((strncasecmp(user, name, len) == 0) ||
205 (strncasecmp(pwent->pw_name, name, len) == 0)) {
206 strncpy(name, pwent->pw_name, len);
211 #endif /* ! NO_REAL_USER_NAME */
213 /* os x server doesn't keep anything useful if we do getpwent */
214 return pwent ? getpwnam(name) : NULL;
217 int uam_checkuser(const struct passwd *pwd)
221 if (!pwd || !pwd->pw_shell || (*pwd->pw_shell == '\0'))
224 while ((p = getusershell())) {
225 if ( strcmp( p, pwd->pw_shell ) == 0 )
230 #ifdef DISABLE_SHELLCHECK
232 syslog( LOG_INFO, "illegal shell %s for %s", pwd->pw_shell, pwd->pw_name);
235 #endif /* DISABLE_SHELLCHECK */
240 /* afp-specific functions */
241 int uam_afpserver_option(void *private, const int what, void *option,
244 AFPObj *obj = private;
245 char **buf = (char **) option; /* most of the options are this */
253 case UAM_OPTION_USERNAME:
254 *buf = (void *) obj->username;
256 *len = sizeof(obj->username) - 1;
259 case UAM_OPTION_GUEST:
260 *buf = (void *) obj->options.guest;
262 *len = strlen(obj->options.guest);
265 case UAM_OPTION_PASSWDOPT:
270 case UAM_PASSWD_FILENAME:
271 *buf = (void *) obj->options.passwdfile;
272 *len = strlen(obj->options.passwdfile);
275 case UAM_PASSWD_MINLENGTH:
276 *((int *) option) = obj->options.passwdminlen;
277 *len = sizeof(obj->options.passwdminlen);
280 case UAM_PASSWD_MAXFAIL:
281 *((int *) option) = obj->options.loginmaxfail;
282 *len = sizeof(obj->options.loginmaxfail);
285 case UAM_PASSWD_EXPIRETIME: /* not implemented */
292 case UAM_OPTION_SIGNATURE:
293 *buf = (void *) (((AFPConfig *)obj->config)->signature);
298 case UAM_OPTION_RANDNUM: /* returns a random number in 4-byte units. */
299 if (!len || (*len < 0) || (*len % sizeof(result)))
302 /* construct a random number */
303 if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
306 char *randnum = (char *) option;
309 if (gettimeofday(&tv, &tz) < 0)
311 srandom(tv.tv_sec + (unsigned long) obj + (unsigned long) obj->handle);
312 for (i = 0; i < *len; i += sizeof(result)) {
314 memcpy(randnum + i, &result, sizeof(result));
317 result = read(fd, option, *len);
324 case UAM_OPTION_HOSTNAME:
325 *buf = (void *) obj->options.hostname;
327 *len = strlen(obj->options.hostname);
330 case UAM_OPTION_PROTOCOL:
331 *buf = (void *) obj->proto;
334 case UAM_OPTION_COOKIE:
335 /* it's up to the uam to actually store something useful here.
336 * this just passes back a handle to the cookie. the uam side
337 * needs to do something like **buf = (void *) cookie to store
339 *buf = (void *) &obj->uam_cookie;
350 /* if we need to maintain a connection, this is how we do it.
351 * because an action pointer gets passed in, we can stream
353 int uam_afp_read(void *handle, char *buf, int *buflen,
354 int (*action)(void *, void *, const int))
356 AFPObj *obj = handle;
362 switch (obj->proto) {
364 if ((len = asp_wrtcont(obj->handle, buf, buflen )) < 0)
365 goto uam_afp_read_err;
366 return action(handle, buf, *buflen);
370 len = dsi_writeinit(obj->handle, buf, *buflen);
371 if (!len || ((len = action(handle, buf, len)) < 0)) {
372 dsi_writeflush(obj->handle);
373 goto uam_afp_read_err;
376 while ((len = (dsi_write(obj->handle, buf, *buflen)))) {
377 if ((len = action(handle, buf, len)) < 0) {
378 dsi_writeflush(obj->handle);
379 goto uam_afp_read_err;
391 /* --- papd-specific functions (just placeholders) --- */
392 void append(void *pf, char *data, int len)