2 * $Id: uam.c,v 1.16 2001-12-03 05:03:38 jmarcus 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 */
38 #include <sys/param.h>
42 #endif /* HAVE_DLFCN_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"
61 #include <arpa/inet.h>
64 /* --- server uam functions -- */
66 /* uam_load. uams must have a uam_setup function. */
67 struct uam_mod *uam_load(const char *path, const char *name)
69 char buf[MAXPATHLEN + 1], *p;
73 if ((module = mod_open(path)) == NULL) {
74 syslog(LOG_ERR, "uam_load(%s): failed to load: %s", name, mod_error());
78 if ((mod = (struct uam_mod *) malloc(sizeof(struct uam_mod))) == NULL) {
79 syslog(LOG_ERR, "uam_load(%s): malloc failed", name);
83 strncpy(buf, name, sizeof(buf));
84 buf[sizeof(buf) - 1] = '\0';
85 if ((p = strchr(buf, '.')))
87 if ((mod->uam_fcn = mod_symbol(module, buf)) == NULL) {
88 syslog(LOG_ERR, "uam_load(%s): mod_symbol error for symbol %s",
94 if (mod->uam_fcn->uam_type != UAM_MODULE_SERVER) {
95 syslog(LOG_ERR, "uam_load(%s): attempted to load a non-server module",
100 /* version check would go here */
102 if (!mod->uam_fcn->uam_setup ||
103 ((*mod->uam_fcn->uam_setup)(name) < 0)) {
104 syslog(LOG_ERR, "uam_load(%s): uam_setup failed", name);
108 mod->uam_module = module;
118 /* unload the module. we check for a cleanup function, but we don't
119 * die if one doesn't exist. however, things are likely to leak without one.
121 void uam_unload(struct uam_mod *mod)
123 if (mod->uam_fcn->uam_cleanup)
124 (*mod->uam_fcn->uam_cleanup)();
125 mod_close(mod->uam_module);
129 /* -- client-side uam functions -- */
131 /* set up stuff for this uam. */
132 int uam_register(const int type, const char *path, const char *name, ...)
140 /* see if it already exists. */
141 if ((uam = auth_uamfind(type, name, strlen(name)))) {
142 if (strcmp(uam->uam_path, path)) {
143 /* it exists, but it's not the same module. */
144 syslog(LOG_ERR, "uam_register: \"%s\" already loaded by %s",
152 /* allocate space for uam */
153 if ((uam = calloc(1, sizeof(struct uam_obj))) == NULL)
156 uam->uam_name = name;
157 uam->uam_path = strdup(path);
162 case UAM_SERVER_LOGIN: /* expect three arguments */
163 uam->u.uam_login.login = va_arg(ap, void *);
164 uam->u.uam_login.logincont = va_arg(ap, void *);
165 uam->u.uam_login.logout = va_arg(ap, void *);
167 case UAM_SERVER_CHANGEPW: /* one argument */
168 uam->u.uam_changepw = va_arg(ap, void *);
170 case UAM_SERVER_PRINTAUTH: /* x arguments */
176 /* attach to other uams */
177 if (auth_register(type, uam) < 0) {
186 void uam_unregister(const int type, const char *name)
193 uam = auth_uamfind(type, name, strlen(name));
194 if (!uam || --uam->uam_count > 0)
197 auth_unregister(uam);
202 /* --- helper functions for plugin uams --- */
204 struct passwd *uam_getname(char *name, const int len)
206 struct passwd *pwent;
210 if ((pwent = getpwnam(name)))
213 #ifndef NO_REAL_USER_NAME
214 for (i = 0; i < len; i++)
215 name[i] = tolower(name[i]);
218 while ((pwent = getpwent())) {
219 if ((user = strchr(pwent->pw_gecos, ',')))
221 user = pwent->pw_gecos;
223 /* check against both the gecos and the name fields. the user
224 * might have just used a different capitalization. */
225 if ((strncasecmp(user, name, len) == 0) ||
226 (strncasecmp(pwent->pw_name, name, len) == 0)) {
227 strncpy(name, pwent->pw_name, len);
228 name[len - 1] = '\0';
233 #endif /* ! NO_REAL_USER_NAME */
235 /* os x server doesn't keep anything useful if we do getpwent */
236 return pwent ? getpwnam(name) : NULL;
239 int uam_checkuser(const struct passwd *pwd)
243 if (!pwd || !pwd->pw_shell || (*pwd->pw_shell == '\0'))
246 while ((p = getusershell())) {
247 if ( strcmp( p, pwd->pw_shell ) == 0 )
252 #ifndef DISABLE_SHELLCHECK
254 syslog( LOG_INFO, "illegal shell %s for %s", pwd->pw_shell, pwd->pw_name);
257 #endif /* DISABLE_SHELLCHECK */
262 /* afp-specific functions */
263 int uam_afpserver_option(void *private, const int what, void *option,
266 AFPObj *obj = private;
267 char **buf = (char **) option; /* most of the options are this */
275 case UAM_OPTION_USERNAME:
276 *buf = (void *) obj->username;
278 *len = sizeof(obj->username) - 1;
281 case UAM_OPTION_GUEST:
282 *buf = (void *) obj->options.guest;
284 *len = strlen(obj->options.guest);
287 case UAM_OPTION_PASSWDOPT:
292 case UAM_PASSWD_FILENAME:
293 *buf = (void *) obj->options.passwdfile;
294 *len = strlen(obj->options.passwdfile);
297 case UAM_PASSWD_MINLENGTH:
298 *((int *) option) = obj->options.passwdminlen;
299 *len = sizeof(obj->options.passwdminlen);
302 case UAM_PASSWD_MAXFAIL:
303 *((int *) option) = obj->options.loginmaxfail;
304 *len = sizeof(obj->options.loginmaxfail);
307 case UAM_PASSWD_EXPIRETIME: /* not implemented */
314 case UAM_OPTION_SIGNATURE:
315 *buf = (void *) (((AFPConfig *)obj->config)->signature);
320 case UAM_OPTION_RANDNUM: /* returns a random number in 4-byte units. */
321 if (!len || (*len < 0) || (*len % sizeof(result)))
324 /* construct a random number */
325 if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
328 char *randnum = (char *) option;
331 if (gettimeofday(&tv, &tz) < 0)
333 srandom(tv.tv_sec + (unsigned long) obj + (unsigned long) obj->handle);
334 for (i = 0; i < *len; i += sizeof(result)) {
336 memcpy(randnum + i, &result, sizeof(result));
339 result = read(fd, option, *len);
346 case UAM_OPTION_HOSTNAME:
347 *buf = (void *) obj->options.hostname;
349 *len = strlen(obj->options.hostname);
352 case UAM_OPTION_PROTOCOL:
353 *buf = (void *) obj->proto;
355 case UAM_OPTION_CLIENTNAME:
357 struct DSI *dsi = obj->handle;
360 hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
361 sizeof( struct in_addr ),
362 dsi->client.sin_family );
364 *buf = (void *) hp->h_name;
366 *buf = (void *) inet_ntoa( dsi->client.sin_addr );
369 case UAM_OPTION_COOKIE:
370 /* it's up to the uam to actually store something useful here.
371 * this just passes back a handle to the cookie. the uam side
372 * needs to do something like **buf = (void *) cookie to store
374 *buf = (void *) &obj->uam_cookie;
385 /* if we need to maintain a connection, this is how we do it.
386 * because an action pointer gets passed in, we can stream
388 int uam_afp_read(void *handle, char *buf, int *buflen,
389 int (*action)(void *, void *, const int))
391 AFPObj *obj = handle;
397 switch (obj->proto) {
399 if ((len = asp_wrtcont(obj->handle, buf, buflen )) < 0)
400 goto uam_afp_read_err;
401 return action(handle, buf, *buflen);
405 len = dsi_writeinit(obj->handle, buf, *buflen);
406 if (!len || ((len = action(handle, buf, len)) < 0)) {
407 dsi_writeflush(obj->handle);
408 goto uam_afp_read_err;
411 while ((len = (dsi_write(obj->handle, buf, *buflen)))) {
412 if ((len = action(handle, buf, len)) < 0) {
413 dsi_writeflush(obj->handle);
414 goto uam_afp_read_err;
427 void uam_afp_getcmdline( int *ac, char ***av )
429 afp_get_cmdline( ac, av );
433 /* --- papd-specific functions (just placeholders) --- */
434 void append(void *pf, char *data, int len)