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