1 /* Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
2 * All Rights Reserved. See COPYRIGHT.
12 #include <sys/param.h>
15 #include <netatalk/endian.h>
16 #include <atalk/asp.h>
17 #include <atalk/dsi.h>
18 #include <atalk/afp.h>
19 #include <atalk/util.h>
26 /* --- server uam functions -- */
28 /* uam_load. uams must have a uam_setup function. */
29 struct uam_mod *uam_load(const char *path, const char *name)
31 char buf[MAXPATHLEN + 1], *p;
35 if ((module = mod_open(path)) == NULL) {
36 syslog(LOG_ERR, "uam_load(%s): failed to load: %s", name, mod_error());
40 if ((mod = (struct uam_mod *) malloc(sizeof(struct uam_mod))) == NULL) {
41 syslog(LOG_ERR, "uam_load(%s): malloc failed", name);
45 strncpy(buf, name, sizeof(buf));
46 if ((p = strchr(buf, '.')))
48 if ((mod->uam_fcn = mod_symbol(module, buf)) == NULL) {
52 if (mod->uam_fcn->uam_type != UAM_MODULE_SERVER) {
53 syslog(LOG_ERR, "uam_load(%s): attempted to load a non-server module",
58 /* version check would go here */
60 if (!mod->uam_fcn->uam_setup ||
61 ((*mod->uam_fcn->uam_setup)(name) < 0)) {
62 syslog(LOG_ERR, "uam_load(%s): uam_setup failed", name);
66 mod->uam_module = module;
76 /* unload the module. we check for a cleanup function, but we don't
77 * die if one doesn't exist. however, things are likely to leak without one.
79 void uam_unload(struct uam_mod *mod)
81 if (mod->uam_fcn->uam_cleanup)
82 (*mod->uam_fcn->uam_cleanup)();
83 mod_close(mod->uam_module);
87 /* -- client-side uam functions -- */
89 /* set up stuff for this uam. */
90 int uam_register(const int type, const char *path, const char *name, ...)
98 /* see if it already exists. */
99 if ((uam = auth_uamfind(type, name, strlen(name)))) {
100 if (strcmp(uam->uam_path, path)) {
101 /* it exists, but it's not the same module. */
102 syslog(LOG_ERR, "uam_register: \"%s\" already loaded by %s",
110 /* allocate space for uam */
111 if ((uam = calloc(1, sizeof(struct uam_obj))) == NULL)
114 uam->uam_name = name;
115 uam->uam_path = strdup(path);
120 case UAM_SERVER_LOGIN: /* expect three arguments */
121 uam->u.uam_login.login = va_arg(ap, void *);
122 uam->u.uam_login.logincont = va_arg(ap, void *);
123 uam->u.uam_login.logout = va_arg(ap, void *);
125 case UAM_SERVER_CHANGEPW: /* one argument */
126 uam->u.uam_changepw = va_arg(ap, void *);
128 case UAM_SERVER_PRINTAUTH: /* x arguments */
134 /* attach to other uams */
135 if (auth_register(type, uam) < 0) {
144 void uam_unregister(const int type, const char *name)
151 uam = auth_uamfind(type, name, strlen(name));
152 if (!uam || --uam->uam_count > 0)
155 auth_unregister(uam);
160 /* --- helper functions for plugin uams --- */
162 struct passwd *uam_getname(char *name, const int len)
164 struct passwd *pwent;
168 if ((pwent = getpwnam(name)))
171 #ifndef NO_REAL_USER_NAME
172 for (i = 0; i < len; i++)
173 name[i] = tolower(name[i]);
176 while ((pwent = getpwent())) {
177 if (user = strchr(pwent->pw_gecos, ','))
179 user = pwent->pw_gecos;
181 /* check against both the gecos and the name fields. the user
182 * might have just used a different capitalization. */
183 if ((strncasecmp(user, name, len) == 0) ||
184 (strncasecmp(pwent->pw_name, name, len) == 0)) {
185 strncpy(name, pwent->pw_name, len);
192 /* os x server doesn't keep anything useful if we do getpwent */
193 return pwent ? getpwnam(name) : NULL;
196 int uam_checkuser(const struct passwd *pwd)
200 if (!pwd || !pwd->pw_shell || (*pwd->pw_shell == '\0'))
203 while ((p = getusershell())) {
204 if ( strcmp( p, pwd->pw_shell ) == 0 )
210 syslog( LOG_INFO, "illegal shell %s for %s", pwd->pw_shell, pwd->pw_name);
217 /* afp-specific functions */
218 int uam_afpserver_option(void *private, const int what, void *option,
221 AFPObj *obj = private;
222 char **buf = (char **) option; /* most of the options are this */
230 case UAM_OPTION_USERNAME:
231 *buf = (void *) obj->username;
233 *len = sizeof(obj->username) - 1;
236 case UAM_OPTION_GUEST:
237 *buf = (void *) obj->options.guest;
239 *len = strlen(obj->options.guest);
242 case UAM_OPTION_PASSWDOPT:
247 case UAM_PASSWD_FILENAME:
248 *buf = (void *) obj->options.passwdfile;
249 *len = strlen(obj->options.passwdfile);
252 case UAM_PASSWD_MINLENGTH:
253 *((int *) option) = obj->options.passwdminlen;
254 *len = sizeof(obj->options.passwdminlen);
257 case UAM_PASSWD_MAXFAIL:
258 *((int *) option) = obj->options.loginmaxfail;
259 *len = sizeof(obj->options.loginmaxfail);
262 case UAM_PASSWD_EXPIRETIME: /* not implemented */
269 case UAM_OPTION_SIGNATURE:
270 *buf = (void *) (((AFPConfig *)obj->config)->signature);
275 case UAM_OPTION_RANDNUM: /* returns a random number in 4-byte units. */
276 if (!len || (*len < 0) || (*len % sizeof(result)))
279 /* construct a random number */
280 if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
283 char *randnum = (char *) option;
286 if (gettimeofday(&tv, &tz) < 0)
288 srandom(tv.tv_sec + (unsigned long) obj + (unsigned long) obj->handle);
289 for (i = 0; i < *len; i += sizeof(result)) {
291 memcpy(randnum + i, &result, sizeof(result));
294 result = read(fd, option, *len);
301 case UAM_OPTION_HOSTNAME:
302 *buf = (void *) obj->options.hostname;
304 *len = strlen(obj->options.hostname);
307 case UAM_OPTION_COOKIE:
308 /* it's up to the uam to actually store something useful here.
309 * this just passes back a handle to the cookie. the uam side
310 * needs to do something like **buf = (void *) cookie to store
312 *buf = (void *) &obj->uam_cookie;
323 /* if we need to maintain a connection, this is how we do it.
324 * because an action pointer gets passed in, we can stream
326 int uam_afp_read(void *handle, char *buf, int *buflen,
327 int (*action)(void *, void *, const int))
329 AFPObj *obj = handle;
335 switch (obj->proto) {
337 if ((len = asp_wrtcont(obj->handle, buf, buflen )) < 0)
338 goto uam_afp_read_err;
339 return action(handle, buf, *buflen);
343 len = dsi_writeinit(obj->handle, buf, *buflen);
344 if (!len || ((len = action(handle, buf, len)) < 0)) {
345 dsi_writeflush(obj->handle);
346 goto uam_afp_read_err;
349 while (len = (dsi_write(obj->handle, buf, *buflen))) {
350 if ((len = action(handle, buf, len)) < 0) {
351 dsi_writeflush(obj->handle);
352 goto uam_afp_read_err;
364 /* --- papd-specific functions (just placeholders) --- */
365 void append(void *pf, char *data, int len)