]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/uam.c
Joe Clarke's fix for linux/quota.h checking, removed NO_DLFCN_H define
[netatalk.git] / etc / afpd / uam.c
1 /*
2  * $Id: uam.c,v 1.8 2001-05-31 18:48:32 srittau Exp $
3  *
4  * Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
5  * All Rights Reserved.  See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <ctype.h>
18 #include <syslog.h>
19 #include <sys/param.h>
20 #include <sys/time.h>
21
22 #ifdef SHADOWPW
23 #include <shadow.h>
24 #endif SHADOWPW
25
26 #include <netatalk/endian.h>
27 #include <atalk/asp.h>
28 #include <atalk/dsi.h>
29 #include <atalk/afp.h>
30 #include <atalk/util.h>
31
32 #include "globals.h"
33 #include "afp_config.h"
34 #include "auth.h"
35 #include "uam_auth.h"
36
37 /* --- server uam functions -- */
38
39 /* uam_load. uams must have a uam_setup function. */
40 struct uam_mod *uam_load(const char *path, const char *name)
41 {
42   char buf[MAXPATHLEN + 1], *p;
43   struct uam_mod *mod;
44   void *module;
45
46   if ((module = mod_open(path)) == NULL) {
47     syslog(LOG_ERR, "uam_load(%s): failed to load: %s", name, mod_error());
48     return NULL;
49   }
50
51   if ((mod = (struct uam_mod *) malloc(sizeof(struct uam_mod))) == NULL) {
52     syslog(LOG_ERR, "uam_load(%s): malloc failed", name);
53     goto uam_load_fail;
54   }
55
56   strncpy(buf, name, sizeof(buf));
57   if ((p = strchr(buf, '.')))
58     *p = '\0';
59   if ((mod->uam_fcn = mod_symbol(module, buf)) == NULL) {
60     syslog(LOG_ERR, "uam_load(%s): mod_symbol error for symbol %s",
61            name,
62            buf);
63     goto uam_load_err;
64   }
65
66   if (mod->uam_fcn->uam_type != UAM_MODULE_SERVER) {
67     syslog(LOG_ERR, "uam_load(%s): attempted to load a non-server module",
68            name);
69     goto uam_load_err;
70   }
71
72   /* version check would go here */
73
74   if (!mod->uam_fcn->uam_setup || 
75       ((*mod->uam_fcn->uam_setup)(name) < 0)) {
76     syslog(LOG_ERR, "uam_load(%s): uam_setup failed", name);
77     goto uam_load_err;
78   }
79
80   mod->uam_module = module;
81   return mod;
82
83 uam_load_err:
84   free(mod);
85 uam_load_fail:
86   mod_close(module);
87   return NULL;
88 }
89
90 /* unload the module. we check for a cleanup function, but we don't
91  * die if one doesn't exist. however, things are likely to leak without one.
92  */
93 void uam_unload(struct uam_mod *mod)
94 {
95   if (mod->uam_fcn->uam_cleanup)
96     (*mod->uam_fcn->uam_cleanup)();
97   mod_close(mod->uam_module);
98   free(mod);
99 }
100
101 /* -- client-side uam functions -- */
102
103 /* set up stuff for this uam. */
104 int uam_register(const int type, const char *path, const char *name, ...)
105 {
106   va_list ap;
107   struct uam_obj *uam;
108
109   if (!name)
110     return -1;
111
112   /* see if it already exists. */
113   if ((uam = auth_uamfind(type, name, strlen(name)))) {
114     if (strcmp(uam->uam_path, path)) {
115       /* it exists, but it's not the same module. */
116       syslog(LOG_ERR, "uam_register: \"%s\" already loaded by %s",
117              name, path);
118       return -1;
119     }
120     uam->uam_count++;
121     return 0;
122   }
123   
124   /* allocate space for uam */
125   if ((uam = calloc(1, sizeof(struct uam_obj))) == NULL)
126     return -1;
127
128   uam->uam_name = name;
129   uam->uam_path = strdup(path);
130   uam->uam_count++;
131
132   va_start(ap, name);
133   switch (type) {
134   case UAM_SERVER_LOGIN: /* expect three arguments */
135     uam->u.uam_login.login = va_arg(ap, void *);
136     uam->u.uam_login.logincont = va_arg(ap, void *);
137     uam->u.uam_login.logout = va_arg(ap, void *);
138     break;
139   case UAM_SERVER_CHANGEPW: /* one argument */
140     uam->u.uam_changepw = va_arg(ap, void *);
141     break;
142   case UAM_SERVER_PRINTAUTH: /* x arguments */
143   default:
144     break;
145   }
146   va_end(ap);
147
148   /* attach to other uams */
149   if (auth_register(type, uam) < 0) {
150     free(uam->uam_path);
151     free(uam);
152     return -1;
153   }
154
155   return 0;
156 }
157
158 void uam_unregister(const int type, const char *name)
159 {
160   struct uam_obj *uam;
161
162   if (!name)
163     return;
164
165   uam = auth_uamfind(type, name, strlen(name));
166   if (!uam || --uam->uam_count > 0)
167     return;
168
169   auth_unregister(uam);
170   free(uam->uam_path);
171   free(uam);
172 }
173
174 /* --- helper functions for plugin uams --- */
175
176 struct passwd *uam_getname(char *name, const int len)
177 {
178   struct passwd *pwent;
179   char *user;
180   int i;
181
182   if ((pwent = getpwnam(name)))
183     return pwent;
184
185 #ifndef NO_REAL_USER_NAME
186   for (i = 0; i < len; i++)
187     name[i] = tolower(name[i]);
188
189   setpwent();
190   while ((pwent = getpwent())) {
191     if ((user = strchr(pwent->pw_gecos, ',')))
192       *user = '\0';
193     user = pwent->pw_gecos;
194
195     /* check against both the gecos and the name fields. the user
196      * might have just used a different capitalization. */
197     if ((strncasecmp(user, name, len) == 0) ||
198         (strncasecmp(pwent->pw_name, name, len) == 0)) {
199       strncpy(name, pwent->pw_name, len);
200       break;
201     }
202   }
203   endpwent();
204 #endif
205
206   /* os x server doesn't keep anything useful if we do getpwent */
207   return pwent ? getpwnam(name) : NULL;
208 }
209
210 int uam_checkuser(const struct passwd *pwd)
211 {
212   char *p;
213
214   if (!pwd || !pwd->pw_shell || (*pwd->pw_shell == '\0')) 
215     return -1;
216
217   while ((p = getusershell())) {
218     if ( strcmp( p, pwd->pw_shell ) == 0 ) 
219       break;
220   }
221   endusershell();
222
223 #ifdef DISABLE_SHELLCHECK
224   if (!p) {
225     syslog( LOG_INFO, "illegal shell %s for %s", pwd->pw_shell, pwd->pw_name);
226     return -1;
227   }
228 #endif /* DISABLE_SHELLCHECK */
229
230   return 0;
231 }
232
233 /* afp-specific functions */
234 int uam_afpserver_option(void *private, const int what, void *option,
235                          int *len)
236 {
237   AFPObj *obj = private;
238   char **buf = (char **) option; /* most of the options are this */
239   int32_t result;
240   int fd;
241
242   if (!obj || !option)
243     return -1;
244
245   switch (what) {
246   case UAM_OPTION_USERNAME:
247     *buf = (void *) obj->username;
248     if (len) 
249       *len = sizeof(obj->username) - 1;
250     break;
251
252   case UAM_OPTION_GUEST:
253     *buf = (void *) obj->options.guest;
254     if (len) 
255       *len = strlen(obj->options.guest);
256     break;
257
258   case UAM_OPTION_PASSWDOPT:
259     if (!len)
260       return -1;
261
262     switch (*len) {
263     case UAM_PASSWD_FILENAME:
264       *buf = (void *) obj->options.passwdfile;
265       *len = strlen(obj->options.passwdfile);
266       break;
267
268     case UAM_PASSWD_MINLENGTH:
269       *((int *) option) = obj->options.passwdminlen;
270       *len = sizeof(obj->options.passwdminlen);
271       break;
272
273     case UAM_PASSWD_MAXFAIL: 
274       *((int *) option) = obj->options.loginmaxfail;
275       *len = sizeof(obj->options.loginmaxfail);
276       break;
277       
278     case UAM_PASSWD_EXPIRETIME: /* not implemented */
279     default:
280       return -1;
281     break;
282     }
283     break;
284
285   case UAM_OPTION_SIGNATURE:
286     *buf = (void *) (((AFPConfig *)obj->config)->signature);
287     if (len)
288       *len = 16;
289     break;
290
291   case UAM_OPTION_RANDNUM: /* returns a random number in 4-byte units. */
292     if (!len || (*len < 0) || (*len % sizeof(result)))
293       return -1;
294
295     /* construct a random number */
296     if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
297       struct timeval tv;
298       struct timezone tz;
299       char *randnum = (char *) option;
300       int i;
301     
302       if (gettimeofday(&tv, &tz) < 0)
303         return -1;
304       srandom(tv.tv_sec + (unsigned long) obj + (unsigned long) obj->handle);
305       for (i = 0; i < *len; i += sizeof(result)) {
306         result = random();
307         memcpy(randnum + i, &result, sizeof(result));
308       }
309     } else {
310       result = read(fd, option, *len);
311       close(fd);
312       if (result < 0)
313         return -1;
314     }
315     break;
316
317   case UAM_OPTION_HOSTNAME:
318     *buf = (void *) obj->options.hostname;
319     if (len) 
320       *len = strlen(obj->options.hostname);
321     break;
322
323   case UAM_OPTION_PROTOCOL:
324     *buf = (void *) obj->proto;
325     break;
326
327   case UAM_OPTION_COOKIE: 
328     /* it's up to the uam to actually store something useful here.
329      * this just passes back a handle to the cookie. the uam side
330      * needs to do something like **buf = (void *) cookie to store
331      * the cookie. */
332     *buf = (void *) &obj->uam_cookie;
333     break;
334
335   default:
336     return -1;
337     break;
338   }
339
340   return 0;
341 }
342
343 /* if we need to maintain a connection, this is how we do it. 
344  * because an action pointer gets passed in, we can stream 
345  * DSI connections */
346 int uam_afp_read(void *handle, char *buf, int *buflen, 
347                  int (*action)(void *, void *, const int))
348 {
349   AFPObj *obj = handle;
350   int len;
351
352   if (!obj)
353     return AFPERR_PARAM;
354
355   switch (obj->proto) {
356     case AFPPROTO_ASP:
357       if ((len = asp_wrtcont(obj->handle, buf, buflen )) < 0) 
358         goto uam_afp_read_err;
359       return action(handle, buf, *buflen);
360       break;
361
362     case AFPPROTO_DSI:
363       len = dsi_writeinit(obj->handle, buf, *buflen);
364       if (!len || ((len = action(handle, buf, len)) < 0)) {
365         dsi_writeflush(obj->handle);
366         goto uam_afp_read_err;
367       }
368         
369       while ((len = (dsi_write(obj->handle, buf, *buflen)))) {
370         if ((len = action(handle, buf, len)) < 0) {
371           dsi_writeflush(obj->handle);
372           goto uam_afp_read_err;
373         }
374       }
375       break;
376   }
377   return 0;
378
379 uam_afp_read_err:
380   *buflen = 0;
381   return len;
382 }
383
384 /* --- papd-specific functions (just placeholders) --- */
385 void append(void *pf, char *data, int len)
386 {
387         return;
388 }