2 * $Id: uam.c,v 1.8 2001-05-31 18:48:32 srittau Exp $
4 * Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
5 * All Rights Reserved. See COPYRIGHT.
19 #include <sys/param.h>
26 #include <netatalk/endian.h>
27 #include <atalk/asp.h>
28 #include <atalk/dsi.h>
29 #include <atalk/afp.h>
30 #include <atalk/util.h>
33 #include "afp_config.h"
37 /* --- server uam functions -- */
39 /* uam_load. uams must have a uam_setup function. */
40 struct uam_mod *uam_load(const char *path, const char *name)
42 char buf[MAXPATHLEN + 1], *p;
46 if ((module = mod_open(path)) == NULL) {
47 syslog(LOG_ERR, "uam_load(%s): failed to load: %s", name, mod_error());
51 if ((mod = (struct uam_mod *) malloc(sizeof(struct uam_mod))) == NULL) {
52 syslog(LOG_ERR, "uam_load(%s): malloc failed", name);
56 strncpy(buf, name, sizeof(buf));
57 if ((p = strchr(buf, '.')))
59 if ((mod->uam_fcn = mod_symbol(module, buf)) == NULL) {
60 syslog(LOG_ERR, "uam_load(%s): mod_symbol error for symbol %s",
66 if (mod->uam_fcn->uam_type != UAM_MODULE_SERVER) {
67 syslog(LOG_ERR, "uam_load(%s): attempted to load a non-server module",
72 /* version check would go here */
74 if (!mod->uam_fcn->uam_setup ||
75 ((*mod->uam_fcn->uam_setup)(name) < 0)) {
76 syslog(LOG_ERR, "uam_load(%s): uam_setup failed", name);
80 mod->uam_module = module;
90 /* unload the module. we check for a cleanup function, but we don't
91 * die if one doesn't exist. however, things are likely to leak without one.
93 void uam_unload(struct uam_mod *mod)
95 if (mod->uam_fcn->uam_cleanup)
96 (*mod->uam_fcn->uam_cleanup)();
97 mod_close(mod->uam_module);
101 /* -- client-side uam functions -- */
103 /* set up stuff for this uam. */
104 int uam_register(const int type, const char *path, const char *name, ...)
112 /* see if it already exists. */
113 if ((uam = auth_uamfind(type, name, strlen(name)))) {
114 if (strcmp(uam->uam_path, path)) {
115 /* it exists, but it's not the same module. */
116 syslog(LOG_ERR, "uam_register: \"%s\" already loaded by %s",
124 /* allocate space for uam */
125 if ((uam = calloc(1, sizeof(struct uam_obj))) == NULL)
128 uam->uam_name = name;
129 uam->uam_path = strdup(path);
134 case UAM_SERVER_LOGIN: /* expect three arguments */
135 uam->u.uam_login.login = va_arg(ap, void *);
136 uam->u.uam_login.logincont = va_arg(ap, void *);
137 uam->u.uam_login.logout = va_arg(ap, void *);
139 case UAM_SERVER_CHANGEPW: /* one argument */
140 uam->u.uam_changepw = va_arg(ap, void *);
142 case UAM_SERVER_PRINTAUTH: /* x arguments */
148 /* attach to other uams */
149 if (auth_register(type, uam) < 0) {
158 void uam_unregister(const int type, const char *name)
165 uam = auth_uamfind(type, name, strlen(name));
166 if (!uam || --uam->uam_count > 0)
169 auth_unregister(uam);
174 /* --- helper functions for plugin uams --- */
176 struct passwd *uam_getname(char *name, const int len)
178 struct passwd *pwent;
182 if ((pwent = getpwnam(name)))
185 #ifndef NO_REAL_USER_NAME
186 for (i = 0; i < len; i++)
187 name[i] = tolower(name[i]);
190 while ((pwent = getpwent())) {
191 if ((user = strchr(pwent->pw_gecos, ',')))
193 user = pwent->pw_gecos;
195 /* check against both the gecos and the name fields. the user
196 * might have just used a different capitalization. */
197 if ((strncasecmp(user, name, len) == 0) ||
198 (strncasecmp(pwent->pw_name, name, len) == 0)) {
199 strncpy(name, pwent->pw_name, len);
206 /* os x server doesn't keep anything useful if we do getpwent */
207 return pwent ? getpwnam(name) : NULL;
210 int uam_checkuser(const struct passwd *pwd)
214 if (!pwd || !pwd->pw_shell || (*pwd->pw_shell == '\0'))
217 while ((p = getusershell())) {
218 if ( strcmp( p, pwd->pw_shell ) == 0 )
223 #ifdef DISABLE_SHELLCHECK
225 syslog( LOG_INFO, "illegal shell %s for %s", pwd->pw_shell, pwd->pw_name);
228 #endif /* DISABLE_SHELLCHECK */
233 /* afp-specific functions */
234 int uam_afpserver_option(void *private, const int what, void *option,
237 AFPObj *obj = private;
238 char **buf = (char **) option; /* most of the options are this */
246 case UAM_OPTION_USERNAME:
247 *buf = (void *) obj->username;
249 *len = sizeof(obj->username) - 1;
252 case UAM_OPTION_GUEST:
253 *buf = (void *) obj->options.guest;
255 *len = strlen(obj->options.guest);
258 case UAM_OPTION_PASSWDOPT:
263 case UAM_PASSWD_FILENAME:
264 *buf = (void *) obj->options.passwdfile;
265 *len = strlen(obj->options.passwdfile);
268 case UAM_PASSWD_MINLENGTH:
269 *((int *) option) = obj->options.passwdminlen;
270 *len = sizeof(obj->options.passwdminlen);
273 case UAM_PASSWD_MAXFAIL:
274 *((int *) option) = obj->options.loginmaxfail;
275 *len = sizeof(obj->options.loginmaxfail);
278 case UAM_PASSWD_EXPIRETIME: /* not implemented */
285 case UAM_OPTION_SIGNATURE:
286 *buf = (void *) (((AFPConfig *)obj->config)->signature);
291 case UAM_OPTION_RANDNUM: /* returns a random number in 4-byte units. */
292 if (!len || (*len < 0) || (*len % sizeof(result)))
295 /* construct a random number */
296 if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
299 char *randnum = (char *) option;
302 if (gettimeofday(&tv, &tz) < 0)
304 srandom(tv.tv_sec + (unsigned long) obj + (unsigned long) obj->handle);
305 for (i = 0; i < *len; i += sizeof(result)) {
307 memcpy(randnum + i, &result, sizeof(result));
310 result = read(fd, option, *len);
317 case UAM_OPTION_HOSTNAME:
318 *buf = (void *) obj->options.hostname;
320 *len = strlen(obj->options.hostname);
323 case UAM_OPTION_PROTOCOL:
324 *buf = (void *) obj->proto;
327 case UAM_OPTION_COOKIE:
328 /* it's up to the uam to actually store something useful here.
329 * this just passes back a handle to the cookie. the uam side
330 * needs to do something like **buf = (void *) cookie to store
332 *buf = (void *) &obj->uam_cookie;
343 /* if we need to maintain a connection, this is how we do it.
344 * because an action pointer gets passed in, we can stream
346 int uam_afp_read(void *handle, char *buf, int *buflen,
347 int (*action)(void *, void *, const int))
349 AFPObj *obj = handle;
355 switch (obj->proto) {
357 if ((len = asp_wrtcont(obj->handle, buf, buflen )) < 0)
358 goto uam_afp_read_err;
359 return action(handle, buf, *buflen);
363 len = dsi_writeinit(obj->handle, buf, *buflen);
364 if (!len || ((len = action(handle, buf, len)) < 0)) {
365 dsi_writeflush(obj->handle);
366 goto uam_afp_read_err;
369 while ((len = (dsi_write(obj->handle, buf, *buflen)))) {
370 if ((len = action(handle, buf, len)) < 0) {
371 dsi_writeflush(obj->handle);
372 goto uam_afp_read_err;
384 /* --- papd-specific functions (just placeholders) --- */
385 void append(void *pf, char *data, int len)