2 * $Id: uam.c,v 1.13 2001-09-06 20:00:59 rufustfirefly 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 if ((p = strchr(buf, '.')))
86 if ((mod->uam_fcn = mod_symbol(module, buf)) == NULL) {
87 syslog(LOG_ERR, "uam_load(%s): mod_symbol error for symbol %s",
93 if (mod->uam_fcn->uam_type != UAM_MODULE_SERVER) {
94 syslog(LOG_ERR, "uam_load(%s): attempted to load a non-server module",
99 /* version check would go here */
101 if (!mod->uam_fcn->uam_setup ||
102 ((*mod->uam_fcn->uam_setup)(name) < 0)) {
103 syslog(LOG_ERR, "uam_load(%s): uam_setup failed", name);
107 mod->uam_module = module;
117 /* unload the module. we check for a cleanup function, but we don't
118 * die if one doesn't exist. however, things are likely to leak without one.
120 void uam_unload(struct uam_mod *mod)
122 if (mod->uam_fcn->uam_cleanup)
123 (*mod->uam_fcn->uam_cleanup)();
124 mod_close(mod->uam_module);
128 /* -- client-side uam functions -- */
130 /* set up stuff for this uam. */
131 int uam_register(const int type, const char *path, const char *name, ...)
139 /* see if it already exists. */
140 if ((uam = auth_uamfind(type, name, strlen(name)))) {
141 if (strcmp(uam->uam_path, path)) {
142 /* it exists, but it's not the same module. */
143 syslog(LOG_ERR, "uam_register: \"%s\" already loaded by %s",
151 /* allocate space for uam */
152 if ((uam = calloc(1, sizeof(struct uam_obj))) == NULL)
155 uam->uam_name = name;
156 uam->uam_path = strdup(path);
161 case UAM_SERVER_LOGIN: /* expect three arguments */
162 uam->u.uam_login.login = va_arg(ap, void *);
163 uam->u.uam_login.logincont = va_arg(ap, void *);
164 uam->u.uam_login.logout = va_arg(ap, void *);
166 case UAM_SERVER_CHANGEPW: /* one argument */
167 uam->u.uam_changepw = va_arg(ap, void *);
169 case UAM_SERVER_PRINTAUTH: /* x arguments */
175 /* attach to other uams */
176 if (auth_register(type, uam) < 0) {
185 void uam_unregister(const int type, const char *name)
192 uam = auth_uamfind(type, name, strlen(name));
193 if (!uam || --uam->uam_count > 0)
196 auth_unregister(uam);
201 /* --- helper functions for plugin uams --- */
203 struct passwd *uam_getname(char *name, const int len)
205 struct passwd *pwent;
209 if ((pwent = getpwnam(name)))
212 #ifndef NO_REAL_USER_NAME
213 for (i = 0; i < len; i++)
214 name[i] = tolower(name[i]);
217 while ((pwent = getpwent())) {
218 if ((user = strchr(pwent->pw_gecos, ',')))
220 user = pwent->pw_gecos;
222 /* check against both the gecos and the name fields. the user
223 * might have just used a different capitalization. */
224 if ((strncasecmp(user, name, len) == 0) ||
225 (strncasecmp(pwent->pw_name, name, len) == 0)) {
226 strncpy(name, pwent->pw_name, len);
231 #endif /* ! NO_REAL_USER_NAME */
233 /* os x server doesn't keep anything useful if we do getpwent */
234 return pwent ? getpwnam(name) : NULL;
237 int uam_checkuser(const struct passwd *pwd)
241 if (!pwd || !pwd->pw_shell || (*pwd->pw_shell == '\0'))
244 while ((p = getusershell())) {
245 if ( strcmp( p, pwd->pw_shell ) == 0 )
250 #ifndef DISABLE_SHELLCHECK
252 syslog( LOG_INFO, "illegal shell %s for %s", pwd->pw_shell, pwd->pw_name);
255 #endif /* DISABLE_SHELLCHECK */
260 /* afp-specific functions */
261 int uam_afpserver_option(void *private, const int what, void *option,
264 AFPObj *obj = private;
265 char **buf = (char **) option; /* most of the options are this */
273 case UAM_OPTION_USERNAME:
274 *buf = (void *) obj->username;
276 *len = sizeof(obj->username) - 1;
279 case UAM_OPTION_GUEST:
280 *buf = (void *) obj->options.guest;
282 *len = strlen(obj->options.guest);
285 case UAM_OPTION_PASSWDOPT:
290 case UAM_PASSWD_FILENAME:
291 *buf = (void *) obj->options.passwdfile;
292 *len = strlen(obj->options.passwdfile);
295 case UAM_PASSWD_MINLENGTH:
296 *((int *) option) = obj->options.passwdminlen;
297 *len = sizeof(obj->options.passwdminlen);
300 case UAM_PASSWD_MAXFAIL:
301 *((int *) option) = obj->options.loginmaxfail;
302 *len = sizeof(obj->options.loginmaxfail);
305 case UAM_PASSWD_EXPIRETIME: /* not implemented */
312 case UAM_OPTION_SIGNATURE:
313 *buf = (void *) (((AFPConfig *)obj->config)->signature);
318 case UAM_OPTION_RANDNUM: /* returns a random number in 4-byte units. */
319 if (!len || (*len < 0) || (*len % sizeof(result)))
322 /* construct a random number */
323 if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
326 char *randnum = (char *) option;
329 if (gettimeofday(&tv, &tz) < 0)
331 srandom(tv.tv_sec + (unsigned long) obj + (unsigned long) obj->handle);
332 for (i = 0; i < *len; i += sizeof(result)) {
334 memcpy(randnum + i, &result, sizeof(result));
337 result = read(fd, option, *len);
344 case UAM_OPTION_HOSTNAME:
345 *buf = (void *) obj->options.hostname;
347 *len = strlen(obj->options.hostname);
350 case UAM_OPTION_PROTOCOL:
351 *buf = (void *) obj->proto;
354 case UAM_OPTION_CLIENTNAME:
356 struct DSI *dsi = obj->handle;
359 hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
360 sizeof( struct in_addr ),
361 dsi->client.sin_family );
363 *buf = (void *) hp->h_name;
365 *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)