2 * $Id: uam.c,v 1.15.2.3 2002-03-11 17:54:44 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>
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)
245 #ifndef DISABLE_SHELLCHECK
246 if (!pwd->pw_shell || (*pwd->pw_shell == '\0')) {
247 syslog(LOG_INFO, "uam_checkuser: User %s does not have a shell", pwd->pw_name);
252 while ((p = getusershell())) {
253 if ( strcmp( p, pwd->pw_shell ) == 0 )
258 #ifndef DISABLE_SHELLCHECK
260 syslog( LOG_INFO, "illegal shell %s for %s", pwd->pw_shell, pwd->pw_name);
263 #endif /* DISABLE_SHELLCHECK */
268 /* afp-specific functions */
269 int uam_afpserver_option(void *private, const int what, void *option,
272 AFPObj *obj = private;
273 char **buf = (char **) option; /* most of the options are this */
281 case UAM_OPTION_USERNAME:
282 *buf = (void *) obj->username;
284 *len = sizeof(obj->username) - 1;
287 case UAM_OPTION_GUEST:
288 *buf = (void *) obj->options.guest;
290 *len = strlen(obj->options.guest);
293 case UAM_OPTION_PASSWDOPT:
298 case UAM_PASSWD_FILENAME:
299 *buf = (void *) obj->options.passwdfile;
300 *len = strlen(obj->options.passwdfile);
303 case UAM_PASSWD_MINLENGTH:
304 *((int *) option) = obj->options.passwdminlen;
305 *len = sizeof(obj->options.passwdminlen);
308 case UAM_PASSWD_MAXFAIL:
309 *((int *) option) = obj->options.loginmaxfail;
310 *len = sizeof(obj->options.loginmaxfail);
313 case UAM_PASSWD_EXPIRETIME: /* not implemented */
320 case UAM_OPTION_SIGNATURE:
321 *buf = (void *) (((AFPConfig *)obj->config)->signature);
326 case UAM_OPTION_RANDNUM: /* returns a random number in 4-byte units. */
327 if (!len || (*len < 0) || (*len % sizeof(result)))
330 /* construct a random number */
331 if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
334 char *randnum = (char *) option;
337 if (gettimeofday(&tv, &tz) < 0)
339 srandom(tv.tv_sec + (unsigned long) obj + (unsigned long) obj->handle);
340 for (i = 0; i < *len; i += sizeof(result)) {
342 memcpy(randnum + i, &result, sizeof(result));
345 result = read(fd, option, *len);
352 case UAM_OPTION_HOSTNAME:
353 *buf = (void *) obj->options.hostname;
355 *len = strlen(obj->options.hostname);
358 case UAM_OPTION_PROTOCOL:
359 *buf = (void *) obj->proto;
361 case UAM_OPTION_CLIENTNAME:
363 struct DSI *dsi = obj->handle;
366 hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
367 sizeof( struct in_addr ),
368 dsi->client.sin_family );
370 *buf = (void *) hp->h_name;
372 *buf = (void *) inet_ntoa( dsi->client.sin_addr );
375 case UAM_OPTION_COOKIE:
376 /* it's up to the uam to actually store something useful here.
377 * this just passes back a handle to the cookie. the uam side
378 * needs to do something like **buf = (void *) cookie to store
380 *buf = (void *) &obj->uam_cookie;
391 /* if we need to maintain a connection, this is how we do it.
392 * because an action pointer gets passed in, we can stream
394 int uam_afp_read(void *handle, char *buf, int *buflen,
395 int (*action)(void *, void *, const int))
397 AFPObj *obj = handle;
403 switch (obj->proto) {
405 if ((len = asp_wrtcont(obj->handle, buf, buflen )) < 0)
406 goto uam_afp_read_err;
407 return action(handle, buf, *buflen);
411 len = dsi_writeinit(obj->handle, buf, *buflen);
412 if (!len || ((len = action(handle, buf, len)) < 0)) {
413 dsi_writeflush(obj->handle);
414 goto uam_afp_read_err;
417 while ((len = (dsi_write(obj->handle, buf, *buflen)))) {
418 if ((len = action(handle, buf, len)) < 0) {
419 dsi_writeflush(obj->handle);
420 goto uam_afp_read_err;
433 void uam_afp_getcmdline( int *ac, char ***av )
435 afp_get_cmdline( ac, av );
439 /* --- papd-specific functions (just placeholders) --- */
440 void append(void *pf, char *data, int len)