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