2 * $Id: uam.c,v 1.15.2.2 2002-01-14 02:54:47 srittau 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>
39 #include <sys/socket.h>
43 #endif /* HAVE_DLFCN_H */
45 #include <netinet/in.h>
46 #include <arpa/inet.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"
63 /* --- server uam functions -- */
65 /* uam_load. uams must have a uam_setup function. */
66 struct uam_mod *uam_load(const char *path, const char *name)
68 char buf[MAXPATHLEN + 1], *p;
72 if ((module = mod_open(path)) == NULL) {
73 syslog(LOG_ERR, "uam_load(%s): failed to load: %s", name, mod_error());
77 if ((mod = (struct uam_mod *) malloc(sizeof(struct uam_mod))) == NULL) {
78 syslog(LOG_ERR, "uam_load(%s): malloc failed", name);
82 strncpy(buf, name, sizeof(buf));
83 buf[sizeof(buf) - 1] = '\0';
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);
227 name[len - 1] = '\0';
232 #endif /* ! NO_REAL_USER_NAME */
234 /* os x server doesn't keep anything useful if we do getpwent */
235 return pwent ? getpwnam(name) : NULL;
238 int uam_checkuser(const struct passwd *pwd)
242 if (!pwd || !pwd->pw_shell || (*pwd->pw_shell == '\0'))
245 while ((p = getusershell())) {
246 if ( strcmp( p, pwd->pw_shell ) == 0 )
251 #ifndef DISABLE_SHELLCHECK
253 syslog( LOG_INFO, "illegal shell %s for %s", pwd->pw_shell, pwd->pw_name);
256 #endif /* DISABLE_SHELLCHECK */
261 /* afp-specific functions */
262 int uam_afpserver_option(void *private, const int what, void *option,
265 AFPObj *obj = private;
266 char **buf = (char **) option; /* most of the options are this */
274 case UAM_OPTION_USERNAME:
275 *buf = (void *) obj->username;
277 *len = sizeof(obj->username) - 1;
280 case UAM_OPTION_GUEST:
281 *buf = (void *) obj->options.guest;
283 *len = strlen(obj->options.guest);
286 case UAM_OPTION_PASSWDOPT:
291 case UAM_PASSWD_FILENAME:
292 *buf = (void *) obj->options.passwdfile;
293 *len = strlen(obj->options.passwdfile);
296 case UAM_PASSWD_MINLENGTH:
297 *((int *) option) = obj->options.passwdminlen;
298 *len = sizeof(obj->options.passwdminlen);
301 case UAM_PASSWD_MAXFAIL:
302 *((int *) option) = obj->options.loginmaxfail;
303 *len = sizeof(obj->options.loginmaxfail);
306 case UAM_PASSWD_EXPIRETIME: /* not implemented */
313 case UAM_OPTION_SIGNATURE:
314 *buf = (void *) (((AFPConfig *)obj->config)->signature);
319 case UAM_OPTION_RANDNUM: /* returns a random number in 4-byte units. */
320 if (!len || (*len < 0) || (*len % sizeof(result)))
323 /* construct a random number */
324 if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
327 char *randnum = (char *) option;
330 if (gettimeofday(&tv, &tz) < 0)
332 srandom(tv.tv_sec + (unsigned long) obj + (unsigned long) obj->handle);
333 for (i = 0; i < *len; i += sizeof(result)) {
335 memcpy(randnum + i, &result, sizeof(result));
338 result = read(fd, option, *len);
345 case UAM_OPTION_HOSTNAME:
346 *buf = (void *) obj->options.hostname;
348 *len = strlen(obj->options.hostname);
351 case UAM_OPTION_PROTOCOL:
352 *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 );
368 case UAM_OPTION_COOKIE:
369 /* it's up to the uam to actually store something useful here.
370 * this just passes back a handle to the cookie. the uam side
371 * needs to do something like **buf = (void *) cookie to store
373 *buf = (void *) &obj->uam_cookie;
384 /* if we need to maintain a connection, this is how we do it.
385 * because an action pointer gets passed in, we can stream
387 int uam_afp_read(void *handle, char *buf, int *buflen,
388 int (*action)(void *, void *, const int))
390 AFPObj *obj = handle;
396 switch (obj->proto) {
398 if ((len = asp_wrtcont(obj->handle, buf, buflen )) < 0)
399 goto uam_afp_read_err;
400 return action(handle, buf, *buflen);
404 len = dsi_writeinit(obj->handle, buf, *buflen);
405 if (!len || ((len = action(handle, buf, len)) < 0)) {
406 dsi_writeflush(obj->handle);
407 goto uam_afp_read_err;
410 while ((len = (dsi_write(obj->handle, buf, *buflen)))) {
411 if ((len = action(handle, buf, len)) < 0) {
412 dsi_writeflush(obj->handle);
413 goto uam_afp_read_err;
426 void uam_afp_getcmdline( int *ac, char ***av )
428 afp_get_cmdline( ac, av );
432 /* --- papd-specific functions (just placeholders) --- */
433 void append(void *pf, char *data, int len)