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) {
57 syslog(LOG_ERR, "uam_load(%s): mod_symbol error for symbol %s",
63 if (mod->uam_fcn->uam_type != UAM_MODULE_SERVER) {
64 syslog(LOG_ERR, "uam_load(%s): attempted to load a non-server module",
69 /* version check would go here */
71 if (!mod->uam_fcn->uam_setup ||
72 ((*mod->uam_fcn->uam_setup)(name) < 0)) {
73 syslog(LOG_ERR, "uam_load(%s): uam_setup failed", name);
77 mod->uam_module = module;
87 /* unload the module. we check for a cleanup function, but we don't
88 * die if one doesn't exist. however, things are likely to leak without one.
90 void uam_unload(struct uam_mod *mod)
92 if (mod->uam_fcn->uam_cleanup)
93 (*mod->uam_fcn->uam_cleanup)();
94 mod_close(mod->uam_module);
98 /* -- client-side uam functions -- */
100 /* set up stuff for this uam. */
101 int uam_register(const int type, const char *path, const char *name, ...)
109 /* see if it already exists. */
110 if ((uam = auth_uamfind(type, name, strlen(name)))) {
111 if (strcmp(uam->uam_path, path)) {
112 /* it exists, but it's not the same module. */
113 syslog(LOG_ERR, "uam_register: \"%s\" already loaded by %s",
121 /* allocate space for uam */
122 if ((uam = calloc(1, sizeof(struct uam_obj))) == NULL)
125 uam->uam_name = name;
126 uam->uam_path = strdup(path);
131 case UAM_SERVER_LOGIN: /* expect three arguments */
132 uam->u.uam_login.login = va_arg(ap, void *);
133 uam->u.uam_login.logincont = va_arg(ap, void *);
134 uam->u.uam_login.logout = va_arg(ap, void *);
136 case UAM_SERVER_CHANGEPW: /* one argument */
137 uam->u.uam_changepw = va_arg(ap, void *);
139 case UAM_SERVER_PRINTAUTH: /* x arguments */
145 /* attach to other uams */
146 if (auth_register(type, uam) < 0) {
155 void uam_unregister(const int type, const char *name)
162 uam = auth_uamfind(type, name, strlen(name));
163 if (!uam || --uam->uam_count > 0)
166 auth_unregister(uam);
171 /* --- helper functions for plugin uams --- */
173 struct passwd *uam_getname(char *name, const int len)
175 struct passwd *pwent;
179 if ((pwent = getpwnam(name)))
182 #ifndef NO_REAL_USER_NAME
183 for (i = 0; i < len; i++)
184 name[i] = tolower(name[i]);
187 while ((pwent = getpwent())) {
188 if (user = strchr(pwent->pw_gecos, ','))
190 user = pwent->pw_gecos;
192 /* check against both the gecos and the name fields. the user
193 * might have just used a different capitalization. */
194 if ((strncasecmp(user, name, len) == 0) ||
195 (strncasecmp(pwent->pw_name, name, len) == 0)) {
196 strncpy(name, pwent->pw_name, len);
203 /* os x server doesn't keep anything useful if we do getpwent */
204 return pwent ? getpwnam(name) : NULL;
207 int uam_checkuser(const struct passwd *pwd)
211 if (!pwd || !pwd->pw_shell || (*pwd->pw_shell == '\0'))
214 while ((p = getusershell())) {
215 if ( strcmp( p, pwd->pw_shell ) == 0 )
221 syslog( LOG_INFO, "illegal shell %s for %s", pwd->pw_shell, pwd->pw_name);
228 /* afp-specific functions */
229 int uam_afpserver_option(void *private, const int what, void *option,
232 AFPObj *obj = private;
233 char **buf = (char **) option; /* most of the options are this */
241 case UAM_OPTION_USERNAME:
242 *buf = (void *) obj->username;
244 *len = sizeof(obj->username) - 1;
247 case UAM_OPTION_GUEST:
248 *buf = (void *) obj->options.guest;
250 *len = strlen(obj->options.guest);
253 case UAM_OPTION_PASSWDOPT:
258 case UAM_PASSWD_FILENAME:
259 *buf = (void *) obj->options.passwdfile;
260 *len = strlen(obj->options.passwdfile);
263 case UAM_PASSWD_MINLENGTH:
264 *((int *) option) = obj->options.passwdminlen;
265 *len = sizeof(obj->options.passwdminlen);
268 case UAM_PASSWD_MAXFAIL:
269 *((int *) option) = obj->options.loginmaxfail;
270 *len = sizeof(obj->options.loginmaxfail);
273 case UAM_PASSWD_EXPIRETIME: /* not implemented */
280 case UAM_OPTION_SIGNATURE:
281 *buf = (void *) (((AFPConfig *)obj->config)->signature);
286 case UAM_OPTION_RANDNUM: /* returns a random number in 4-byte units. */
287 if (!len || (*len < 0) || (*len % sizeof(result)))
290 /* construct a random number */
291 if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
294 char *randnum = (char *) option;
297 if (gettimeofday(&tv, &tz) < 0)
299 srandom(tv.tv_sec + (unsigned long) obj + (unsigned long) obj->handle);
300 for (i = 0; i < *len; i += sizeof(result)) {
302 memcpy(randnum + i, &result, sizeof(result));
305 result = read(fd, option, *len);
312 case UAM_OPTION_HOSTNAME:
313 *buf = (void *) obj->options.hostname;
315 *len = strlen(obj->options.hostname);
318 case UAM_OPTION_PROTOCOL:
322 case UAM_OPTION_COOKIE:
323 /* it's up to the uam to actually store something useful here.
324 * this just passes back a handle to the cookie. the uam side
325 * needs to do something like **buf = (void *) cookie to store
327 *buf = (void *) &obj->uam_cookie;
338 /* if we need to maintain a connection, this is how we do it.
339 * because an action pointer gets passed in, we can stream
341 int uam_afp_read(void *handle, char *buf, int *buflen,
342 int (*action)(void *, void *, const int))
344 AFPObj *obj = handle;
350 switch (obj->proto) {
352 if ((len = asp_wrtcont(obj->handle, buf, buflen )) < 0)
353 goto uam_afp_read_err;
354 return action(handle, buf, *buflen);
358 len = dsi_writeinit(obj->handle, buf, *buflen);
359 if (!len || ((len = action(handle, buf, len)) < 0)) {
360 dsi_writeflush(obj->handle);
361 goto uam_afp_read_err;
364 while (len = (dsi_write(obj->handle, buf, *buflen))) {
365 if ((len = action(handle, buf, len)) < 0) {
366 dsi_writeflush(obj->handle);
367 goto uam_afp_read_err;
379 /* --- papd-specific functions (just placeholders) --- */
380 void append(void *pf, char *data, int len)