1 /* Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
2 * All Rights Reserved. See COPYRIGHT.
16 #include <sys/param.h>
23 #include <netatalk/endian.h>
24 #include <atalk/asp.h>
25 #include <atalk/dsi.h>
26 #include <atalk/afp.h>
27 #include <atalk/util.h>
30 #include "afp_config.h"
34 /* --- server uam functions -- */
36 /* uam_load. uams must have a uam_setup function. */
37 struct uam_mod *uam_load(const char *path, const char *name)
39 char buf[MAXPATHLEN + 1], *p;
43 if ((module = mod_open(path)) == NULL) {
44 syslog(LOG_ERR, "uam_load(%s): failed to load: %s", name, mod_error());
48 if ((mod = (struct uam_mod *) malloc(sizeof(struct uam_mod))) == NULL) {
49 syslog(LOG_ERR, "uam_load(%s): malloc failed", name);
53 strncpy(buf, name, sizeof(buf));
54 if ((p = strchr(buf, '.')))
56 if ((mod->uam_fcn = mod_symbol(module, buf)) == NULL) {
60 if (mod->uam_fcn->uam_type != UAM_MODULE_SERVER) {
61 syslog(LOG_ERR, "uam_load(%s): attempted to load a non-server module",
66 /* version check would go here */
68 if (!mod->uam_fcn->uam_setup ||
69 ((*mod->uam_fcn->uam_setup)(name) < 0)) {
70 syslog(LOG_ERR, "uam_load(%s): uam_setup failed", name);
74 mod->uam_module = module;
84 /* unload the module. we check for a cleanup function, but we don't
85 * die if one doesn't exist. however, things are likely to leak without one.
87 void uam_unload(struct uam_mod *mod)
89 if (mod->uam_fcn->uam_cleanup)
90 (*mod->uam_fcn->uam_cleanup)();
91 mod_close(mod->uam_module);
95 /* -- client-side uam functions -- */
97 /* set up stuff for this uam. */
98 int uam_register(const int type, const char *path, const char *name, ...)
106 /* see if it already exists. */
107 if ((uam = auth_uamfind(type, name, strlen(name)))) {
108 if (strcmp(uam->uam_path, path)) {
109 /* it exists, but it's not the same module. */
110 syslog(LOG_ERR, "uam_register: \"%s\" already loaded by %s",
118 /* allocate space for uam */
119 if ((uam = calloc(1, sizeof(struct uam_obj))) == NULL)
122 uam->uam_name = name;
123 uam->uam_path = strdup(path);
128 case UAM_SERVER_LOGIN: /* expect three arguments */
129 uam->u.uam_login.login = va_arg(ap, void *);
130 uam->u.uam_login.logincont = va_arg(ap, void *);
131 uam->u.uam_login.logout = va_arg(ap, void *);
133 case UAM_SERVER_CHANGEPW: /* one argument */
134 uam->u.uam_changepw = va_arg(ap, void *);
136 case UAM_SERVER_PRINTAUTH: /* x arguments */
142 /* attach to other uams */
143 if (auth_register(type, uam) < 0) {
152 void uam_unregister(const int type, const char *name)
159 uam = auth_uamfind(type, name, strlen(name));
160 if (!uam || --uam->uam_count > 0)
163 auth_unregister(uam);
168 /* --- helper functions for plugin uams --- */
170 struct passwd *uam_getname(char *name, const int len)
172 struct passwd *pwent;
176 if ((pwent = getpwnam(name)))
179 #ifndef NO_REAL_USER_NAME
180 for (i = 0; i < len; i++)
181 name[i] = tolower(name[i]);
184 while ((pwent = getpwent())) {
185 if (user = strchr(pwent->pw_gecos, ','))
187 user = pwent->pw_gecos;
189 /* check against both the gecos and the name fields. the user
190 * might have just used a different capitalization. */
191 if ((strncasecmp(user, name, len) == 0) ||
192 (strncasecmp(pwent->pw_name, name, len) == 0)) {
193 strncpy(name, pwent->pw_name, len);
200 /* os x server doesn't keep anything useful if we do getpwent */
201 return pwent ? getpwnam(name) : NULL;
204 int uam_checkuser(const struct passwd *pwd)
208 if (!pwd || !pwd->pw_shell || (*pwd->pw_shell == '\0'))
211 while ((p = getusershell())) {
212 if ( strcmp( p, pwd->pw_shell ) == 0 )
218 syslog( LOG_INFO, "illegal shell %s for %s", pwd->pw_shell, pwd->pw_name);
225 /* afp-specific functions */
226 int uam_afpserver_option(void *private, const int what, void *option,
229 AFPObj *obj = private;
230 char **buf = (char **) option; /* most of the options are this */
238 case UAM_OPTION_USERNAME:
239 *buf = (void *) obj->username;
241 *len = sizeof(obj->username) - 1;
244 case UAM_OPTION_GUEST:
245 *buf = (void *) obj->options.guest;
247 *len = strlen(obj->options.guest);
250 case UAM_OPTION_PASSWDOPT:
255 case UAM_PASSWD_FILENAME:
256 *buf = (void *) obj->options.passwdfile;
257 *len = strlen(obj->options.passwdfile);
260 case UAM_PASSWD_MINLENGTH:
261 *((int *) option) = obj->options.passwdminlen;
262 *len = sizeof(obj->options.passwdminlen);
265 case UAM_PASSWD_MAXFAIL:
266 *((int *) option) = obj->options.loginmaxfail;
267 *len = sizeof(obj->options.loginmaxfail);
270 case UAM_PASSWD_EXPIRETIME: /* not implemented */
277 case UAM_OPTION_SIGNATURE:
278 *buf = (void *) (((AFPConfig *)obj->config)->signature);
283 case UAM_OPTION_RANDNUM: /* returns a random number in 4-byte units. */
284 if (!len || (*len < 0) || (*len % sizeof(result)))
287 /* construct a random number */
288 if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
291 char *randnum = (char *) option;
294 if (gettimeofday(&tv, &tz) < 0)
296 srandom(tv.tv_sec + (unsigned long) obj + (unsigned long) obj->handle);
297 for (i = 0; i < *len; i += sizeof(result)) {
299 memcpy(randnum + i, &result, sizeof(result));
302 result = read(fd, option, *len);
309 case UAM_OPTION_HOSTNAME:
310 *buf = (void *) obj->options.hostname;
312 *len = strlen(obj->options.hostname);
315 case UAM_OPTION_COOKIE:
316 /* it's up to the uam to actually store something useful here.
317 * this just passes back a handle to the cookie. the uam side
318 * needs to do something like **buf = (void *) cookie to store
320 *buf = (void *) &obj->uam_cookie;
331 /* if we need to maintain a connection, this is how we do it.
332 * because an action pointer gets passed in, we can stream
334 int uam_afp_read(void *handle, char *buf, int *buflen,
335 int (*action)(void *, void *, const int))
337 AFPObj *obj = handle;
343 switch (obj->proto) {
345 if ((len = asp_wrtcont(obj->handle, buf, buflen )) < 0)
346 goto uam_afp_read_err;
347 return action(handle, buf, *buflen);
351 len = dsi_writeinit(obj->handle, buf, *buflen);
352 if (!len || ((len = action(handle, buf, len)) < 0)) {
353 dsi_writeflush(obj->handle);
354 goto uam_afp_read_err;
357 while (len = (dsi_write(obj->handle, buf, *buflen))) {
358 if ((len = action(handle, buf, len)) < 0) {
359 dsi_writeflush(obj->handle);
360 goto uam_afp_read_err;
372 /* --- papd-specific functions (just placeholders) --- */
373 void append(void *pf, char *data, int len)