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