1 /* Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
2 * All Rights Reserved. See COPYRIGHT.
12 #include <sys/param.h>
19 #include <netatalk/endian.h>
20 #include <atalk/asp.h>
21 #include <atalk/dsi.h>
22 #include <atalk/afp.h>
23 #include <atalk/util.h>
30 /* --- server uam functions -- */
32 /* uam_load. uams must have a uam_setup function. */
33 struct uam_mod *uam_load(const char *path, const char *name)
35 char buf[MAXPATHLEN + 1], *p;
39 if ((module = mod_open(path)) == NULL) {
40 syslog(LOG_ERR, "uam_load(%s): failed to load: %s", name, mod_error());
44 if ((mod = (struct uam_mod *) malloc(sizeof(struct uam_mod))) == NULL) {
45 syslog(LOG_ERR, "uam_load(%s): malloc failed", name);
49 strncpy(buf, name, sizeof(buf));
50 if ((p = strchr(buf, '.')))
52 if ((mod->uam_fcn = mod_symbol(module, buf)) == NULL) {
56 if (mod->uam_fcn->uam_type != UAM_MODULE_SERVER) {
57 syslog(LOG_ERR, "uam_load(%s): attempted to load a non-server module",
62 /* version check would go here */
64 if (!mod->uam_fcn->uam_setup ||
65 ((*mod->uam_fcn->uam_setup)(name) < 0)) {
66 syslog(LOG_ERR, "uam_load(%s): uam_setup failed", name);
70 mod->uam_module = module;
80 /* unload the module. we check for a cleanup function, but we don't
81 * die if one doesn't exist. however, things are likely to leak without one.
83 void uam_unload(struct uam_mod *mod)
85 if (mod->uam_fcn->uam_cleanup)
86 (*mod->uam_fcn->uam_cleanup)();
87 mod_close(mod->uam_module);
91 /* -- client-side uam functions -- */
93 /* set up stuff for this uam. */
94 int uam_register(const int type, const char *path, const char *name, ...)
102 /* see if it already exists. */
103 if ((uam = auth_uamfind(type, name, strlen(name)))) {
104 if (strcmp(uam->uam_path, path)) {
105 /* it exists, but it's not the same module. */
106 syslog(LOG_ERR, "uam_register: \"%s\" already loaded by %s",
114 /* allocate space for uam */
115 if ((uam = calloc(1, sizeof(struct uam_obj))) == NULL)
118 uam->uam_name = name;
119 uam->uam_path = strdup(path);
124 case UAM_SERVER_LOGIN: /* expect three arguments */
125 uam->u.uam_login.login = va_arg(ap, void *);
126 uam->u.uam_login.logincont = va_arg(ap, void *);
127 uam->u.uam_login.logout = va_arg(ap, void *);
129 case UAM_SERVER_CHANGEPW: /* one argument */
130 uam->u.uam_changepw = va_arg(ap, void *);
132 case UAM_SERVER_PRINTAUTH: /* x arguments */
138 /* attach to other uams */
139 if (auth_register(type, uam) < 0) {
148 void uam_unregister(const int type, const char *name)
155 uam = auth_uamfind(type, name, strlen(name));
156 if (!uam || --uam->uam_count > 0)
159 auth_unregister(uam);
164 /* --- helper functions for plugin uams --- */
166 struct passwd *uam_getname(char *name, const int len)
168 struct passwd *pwent;
172 if ((pwent = getpwnam(name)))
175 #ifndef NO_REAL_USER_NAME
176 for (i = 0; i < len; i++)
177 name[i] = tolower(name[i]);
180 while ((pwent = getpwent())) {
181 if (user = strchr(pwent->pw_gecos, ','))
183 user = pwent->pw_gecos;
185 /* check against both the gecos and the name fields. the user
186 * might have just used a different capitalization. */
187 if ((strncasecmp(user, name, len) == 0) ||
188 (strncasecmp(pwent->pw_name, name, len) == 0)) {
189 strncpy(name, pwent->pw_name, len);
196 /* os x server doesn't keep anything useful if we do getpwent */
197 return pwent ? getpwnam(name) : NULL;
200 int uam_checkuser(const struct passwd *pwd)
204 if (!pwd || !pwd->pw_shell || (*pwd->pw_shell == '\0'))
207 while ((p = getusershell())) {
208 if ( strcmp( p, pwd->pw_shell ) == 0 )
214 syslog( LOG_INFO, "illegal shell %s for %s", pwd->pw_shell, pwd->pw_name);
221 /* afp-specific functions */
222 int uam_afpserver_option(void *private, const int what, void *option,
225 AFPObj *obj = private;
226 char **buf = (char **) option; /* most of the options are this */
234 case UAM_OPTION_USERNAME:
235 *buf = (void *) obj->username;
237 *len = sizeof(obj->username) - 1;
240 case UAM_OPTION_GUEST:
241 *buf = (void *) obj->options.guest;
243 *len = strlen(obj->options.guest);
246 case UAM_OPTION_PASSWDOPT:
251 case UAM_PASSWD_FILENAME:
252 *buf = (void *) obj->options.passwdfile;
253 *len = strlen(obj->options.passwdfile);
256 case UAM_PASSWD_MINLENGTH:
257 *((int *) option) = obj->options.passwdminlen;
258 *len = sizeof(obj->options.passwdminlen);
261 case UAM_PASSWD_MAXFAIL:
262 *((int *) option) = obj->options.loginmaxfail;
263 *len = sizeof(obj->options.loginmaxfail);
266 case UAM_PASSWD_EXPIRETIME: /* not implemented */
273 case UAM_OPTION_SIGNATURE:
274 *buf = (void *) (((AFPConfig *)obj->config)->signature);
279 case UAM_OPTION_RANDNUM: /* returns a random number in 4-byte units. */
280 if (!len || (*len < 0) || (*len % sizeof(result)))
283 /* construct a random number */
284 if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
287 char *randnum = (char *) option;
290 if (gettimeofday(&tv, &tz) < 0)
292 srandom(tv.tv_sec + (unsigned long) obj + (unsigned long) obj->handle);
293 for (i = 0; i < *len; i += sizeof(result)) {
295 memcpy(randnum + i, &result, sizeof(result));
298 result = read(fd, option, *len);
305 case UAM_OPTION_HOSTNAME:
306 *buf = (void *) obj->options.hostname;
308 *len = strlen(obj->options.hostname);
311 case UAM_OPTION_COOKIE:
312 /* it's up to the uam to actually store something useful here.
313 * this just passes back a handle to the cookie. the uam side
314 * needs to do something like **buf = (void *) cookie to store
316 *buf = (void *) &obj->uam_cookie;
327 /* if we need to maintain a connection, this is how we do it.
328 * because an action pointer gets passed in, we can stream
330 int uam_afp_read(void *handle, char *buf, int *buflen,
331 int (*action)(void *, void *, const int))
333 AFPObj *obj = handle;
339 switch (obj->proto) {
341 if ((len = asp_wrtcont(obj->handle, buf, buflen )) < 0)
342 goto uam_afp_read_err;
343 return action(handle, buf, *buflen);
347 len = dsi_writeinit(obj->handle, buf, *buflen);
348 if (!len || ((len = action(handle, buf, len)) < 0)) {
349 dsi_writeflush(obj->handle);
350 goto uam_afp_read_err;
353 while (len = (dsi_write(obj->handle, buf, *buflen))) {
354 if ((len = action(handle, buf, len)) < 0) {
355 dsi_writeflush(obj->handle);
356 goto uam_afp_read_err;
368 /* --- papd-specific functions (just placeholders) --- */
369 void append(void *pf, char *data, int len)