2 * $Id: uam.c,v 1.10 2001-06-25 15:18:01 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"
46 #include <arpa/inet.h>
49 /* --- server uam functions -- */
51 /* uam_load. uams must have a uam_setup function. */
52 struct uam_mod *uam_load(const char *path, const char *name)
54 char buf[MAXPATHLEN + 1], *p;
58 if ((module = mod_open(path)) == NULL) {
59 syslog(LOG_ERR, "uam_load(%s): failed to load: %s", name, mod_error());
63 if ((mod = (struct uam_mod *) malloc(sizeof(struct uam_mod))) == NULL) {
64 syslog(LOG_ERR, "uam_load(%s): malloc failed", name);
68 strncpy(buf, name, sizeof(buf));
69 if ((p = strchr(buf, '.')))
71 if ((mod->uam_fcn = mod_symbol(module, buf)) == NULL) {
72 syslog(LOG_ERR, "uam_load(%s): mod_symbol error for symbol %s",
78 if (mod->uam_fcn->uam_type != UAM_MODULE_SERVER) {
79 syslog(LOG_ERR, "uam_load(%s): attempted to load a non-server module",
84 /* version check would go here */
86 if (!mod->uam_fcn->uam_setup ||
87 ((*mod->uam_fcn->uam_setup)(name) < 0)) {
88 syslog(LOG_ERR, "uam_load(%s): uam_setup failed", name);
92 mod->uam_module = module;
102 /* unload the module. we check for a cleanup function, but we don't
103 * die if one doesn't exist. however, things are likely to leak without one.
105 void uam_unload(struct uam_mod *mod)
107 if (mod->uam_fcn->uam_cleanup)
108 (*mod->uam_fcn->uam_cleanup)();
109 mod_close(mod->uam_module);
113 /* -- client-side uam functions -- */
115 /* set up stuff for this uam. */
116 int uam_register(const int type, const char *path, const char *name, ...)
124 /* see if it already exists. */
125 if ((uam = auth_uamfind(type, name, strlen(name)))) {
126 if (strcmp(uam->uam_path, path)) {
127 /* it exists, but it's not the same module. */
128 syslog(LOG_ERR, "uam_register: \"%s\" already loaded by %s",
136 /* allocate space for uam */
137 if ((uam = calloc(1, sizeof(struct uam_obj))) == NULL)
140 uam->uam_name = name;
141 uam->uam_path = strdup(path);
146 case UAM_SERVER_LOGIN: /* expect three arguments */
147 uam->u.uam_login.login = va_arg(ap, void *);
148 uam->u.uam_login.logincont = va_arg(ap, void *);
149 uam->u.uam_login.logout = va_arg(ap, void *);
151 case UAM_SERVER_CHANGEPW: /* one argument */
152 uam->u.uam_changepw = va_arg(ap, void *);
154 case UAM_SERVER_PRINTAUTH: /* x arguments */
160 /* attach to other uams */
161 if (auth_register(type, uam) < 0) {
170 void uam_unregister(const int type, const char *name)
177 uam = auth_uamfind(type, name, strlen(name));
178 if (!uam || --uam->uam_count > 0)
181 auth_unregister(uam);
186 /* --- helper functions for plugin uams --- */
188 struct passwd *uam_getname(char *name, const int len)
190 struct passwd *pwent;
194 if ((pwent = getpwnam(name)))
197 #ifndef NO_REAL_USER_NAME
198 for (i = 0; i < len; i++)
199 name[i] = tolower(name[i]);
202 while ((pwent = getpwent())) {
203 if ((user = strchr(pwent->pw_gecos, ',')))
205 user = pwent->pw_gecos;
207 /* check against both the gecos and the name fields. the user
208 * might have just used a different capitalization. */
209 if ((strncasecmp(user, name, len) == 0) ||
210 (strncasecmp(pwent->pw_name, name, len) == 0)) {
211 strncpy(name, pwent->pw_name, len);
216 #endif /* ! NO_REAL_USER_NAME */
218 /* os x server doesn't keep anything useful if we do getpwent */
219 return pwent ? getpwnam(name) : NULL;
222 int uam_checkuser(const struct passwd *pwd)
226 if (!pwd || !pwd->pw_shell || (*pwd->pw_shell == '\0'))
229 while ((p = getusershell())) {
230 if ( strcmp( p, pwd->pw_shell ) == 0 )
235 #ifdef DISABLE_SHELLCHECK
237 syslog( LOG_INFO, "illegal shell %s for %s", pwd->pw_shell, pwd->pw_name);
240 #endif /* DISABLE_SHELLCHECK */
245 /* afp-specific functions */
246 int uam_afpserver_option(void *private, const int what, void *option,
249 AFPObj *obj = private;
250 char **buf = (char **) option; /* most of the options are this */
258 case UAM_OPTION_USERNAME:
259 *buf = (void *) obj->username;
261 *len = sizeof(obj->username) - 1;
264 case UAM_OPTION_GUEST:
265 *buf = (void *) obj->options.guest;
267 *len = strlen(obj->options.guest);
270 case UAM_OPTION_PASSWDOPT:
275 case UAM_PASSWD_FILENAME:
276 *buf = (void *) obj->options.passwdfile;
277 *len = strlen(obj->options.passwdfile);
280 case UAM_PASSWD_MINLENGTH:
281 *((int *) option) = obj->options.passwdminlen;
282 *len = sizeof(obj->options.passwdminlen);
285 case UAM_PASSWD_MAXFAIL:
286 *((int *) option) = obj->options.loginmaxfail;
287 *len = sizeof(obj->options.loginmaxfail);
290 case UAM_PASSWD_EXPIRETIME: /* not implemented */
297 case UAM_OPTION_SIGNATURE:
298 *buf = (void *) (((AFPConfig *)obj->config)->signature);
303 case UAM_OPTION_RANDNUM: /* returns a random number in 4-byte units. */
304 if (!len || (*len < 0) || (*len % sizeof(result)))
307 /* construct a random number */
308 if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
311 char *randnum = (char *) option;
314 if (gettimeofday(&tv, &tz) < 0)
316 srandom(tv.tv_sec + (unsigned long) obj + (unsigned long) obj->handle);
317 for (i = 0; i < *len; i += sizeof(result)) {
319 memcpy(randnum + i, &result, sizeof(result));
322 result = read(fd, option, *len);
329 case UAM_OPTION_HOSTNAME:
330 *buf = (void *) obj->options.hostname;
332 *len = strlen(obj->options.hostname);
335 case UAM_OPTION_PROTOCOL:
336 *buf = (void *) obj->proto;
339 case UAM_OPTION_CLIENTNAME:
341 struct DSI *dsi = obj->handle;
344 hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
345 sizeof( struct in_addr ),
346 dsi->client.sin_family );
348 *buf = (void *) hp->h_name;
350 *buf = (void *) inet_ntoa( dsi->client.sin_addr );
354 case UAM_OPTION_COOKIE:
355 /* it's up to the uam to actually store something useful here.
356 * this just passes back a handle to the cookie. the uam side
357 * needs to do something like **buf = (void *) cookie to store
359 *buf = (void *) &obj->uam_cookie;
370 /* if we need to maintain a connection, this is how we do it.
371 * because an action pointer gets passed in, we can stream
373 int uam_afp_read(void *handle, char *buf, int *buflen,
374 int (*action)(void *, void *, const int))
376 AFPObj *obj = handle;
382 switch (obj->proto) {
384 if ((len = asp_wrtcont(obj->handle, buf, buflen )) < 0)
385 goto uam_afp_read_err;
386 return action(handle, buf, *buflen);
390 len = dsi_writeinit(obj->handle, buf, *buflen);
391 if (!len || ((len = action(handle, buf, len)) < 0)) {
392 dsi_writeflush(obj->handle);
393 goto uam_afp_read_err;
396 while ((len = (dsi_write(obj->handle, buf, *buflen)))) {
397 if ((len = action(handle, buf, len)) < 0) {
398 dsi_writeflush(obj->handle);
399 goto uam_afp_read_err;
412 void uam_afp_getcmdline( int *ac, char ***av )
414 afp_get_cmdline( ac, av );
418 /* --- papd-specific functions (just placeholders) --- */
419 void append(void *pf, char *data, int len)