]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/uam.c
93cdb8e76396b4e5aad7831ea6a7a37e46049139
[netatalk.git] / etc / afpd / uam.c
1 /*
2  * $Id: uam.c,v 1.24.6.3 2004-01-11 12:31:12 bfernhomberg 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 <atalk/logger.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 AFP3x
60 #define utf8_encoding() (afp_version >= 30)
61 #else
62 #define utf8_encoding() (0)
63 #endif
64
65 #ifdef TRU64
66 #include <netdb.h>
67 #include <sia.h>
68 #include <siad.h>
69 #include <signal.h>
70 #endif /* TRU64 */
71
72 /* --- server uam functions -- */
73 #ifndef NO_LOAD_UAM
74 extern  int uam_setup(const char *path);
75 #endif
76
77 /* uam_load. uams must have a uam_setup function. */
78 struct uam_mod *uam_load(const char *path, const char *name)
79 {
80     char buf[MAXPATHLEN + 1], *p;
81     struct uam_mod *mod;
82     void *module;
83
84 #ifndef NO_LOAD_UAM
85     if ((module = mod_open(path)) == NULL) {
86         LOG(log_error, logtype_afpd, "uam_load(%s): failed to load: %s", name, mod_error());
87         return NULL;
88     }
89 #endif
90
91     if ((mod = (struct uam_mod *) malloc(sizeof(struct uam_mod))) == NULL) {
92         LOG(log_error, logtype_afpd, "uam_load(%s): malloc failed", name);
93         goto uam_load_fail;
94     }
95
96     strncpy(buf, name, sizeof(buf));
97     buf[sizeof(buf) - 1] = '\0';
98     if ((p = strchr(buf, '.')))
99         *p = '\0';
100
101 #ifndef NO_LOAD_UAM
102     if ((mod->uam_fcn = mod_symbol(module, buf)) == NULL) {
103         LOG(log_error, logtype_afpd, "uam_load(%s): mod_symbol error for symbol %s",
104             name,
105             buf);
106         goto uam_load_err;
107     }
108
109     if (mod->uam_fcn->uam_type != UAM_MODULE_SERVER) {
110         LOG(log_error, logtype_afpd, "uam_load(%s): attempted to load a non-server module",
111             name);
112         goto uam_load_err;
113     }
114
115     /* version check would go here */
116
117     if (!mod->uam_fcn->uam_setup ||
118             ((*mod->uam_fcn->uam_setup)(name) < 0)) {
119         LOG(log_error, logtype_afpd, "uam_load(%s): uam_setup failed", name);
120         goto uam_load_err;
121     }
122 #else
123    uam_setup(name);
124 #endif
125
126     mod->uam_module = module;
127     return mod;
128
129 uam_load_err:
130     free(mod);
131 uam_load_fail:
132     mod_close(module);
133     return NULL;
134 }
135
136 /* unload the module. we check for a cleanup function, but we don't
137  * die if one doesn't exist. however, things are likely to leak without one.
138  */
139 void uam_unload(struct uam_mod *mod)
140 {
141     if (mod->uam_fcn->uam_cleanup)
142         (*mod->uam_fcn->uam_cleanup)();
143
144 #ifndef NO_LOAD_UAM
145     mod_close(mod->uam_module);
146 #endif    
147     free(mod);
148 }
149
150 /* -- client-side uam functions -- */
151 #ifndef ATACC
152 /* set up stuff for this uam. */
153 int uam_register(const int type, const char *path, const char *name, ...)
154 {
155     va_list ap;
156     struct uam_obj *uam;
157     int ret;
158
159     if (!name)
160         return -1;
161
162     /* see if it already exists. */
163     if ((uam = auth_uamfind(type, name, strlen(name)))) {
164         if (strcmp(uam->uam_path, path)) {
165             /* it exists, but it's not the same module. */
166             LOG(log_error, logtype_afpd, "uam_register: \"%s\" already loaded by %s",
167                 name, path);
168             return -1;
169         }
170         uam->uam_count++;
171         return 0;
172     }
173
174     /* allocate space for uam */
175     if ((uam = calloc(1, sizeof(struct uam_obj))) == NULL)
176         return -1;
177
178     uam->uam_name = name;
179     uam->uam_path = strdup(path);
180     uam->uam_count++;
181
182     va_start(ap, name);
183     switch (type) {
184     case UAM_SERVER_LOGIN_EXT: /* expect four arguments */
185         uam->u.uam_login.login = va_arg(ap, void *);
186         uam->u.uam_login.logincont = va_arg(ap, void *);
187         uam->u.uam_login.logout = va_arg(ap, void *);
188         uam->u.uam_login.login_ext = va_arg(ap, void *);
189         break;
190     
191     case UAM_SERVER_LOGIN: /* expect three arguments */
192         uam->u.uam_login.login_ext = NULL;
193         uam->u.uam_login.login = va_arg(ap, void *);
194         uam->u.uam_login.logincont = va_arg(ap, void *);
195         uam->u.uam_login.logout = va_arg(ap, void *);
196         break;
197     case UAM_SERVER_CHANGEPW: /* one argument */
198         uam->u.uam_changepw = va_arg(ap, void *);
199         break;
200     case UAM_SERVER_PRINTAUTH: /* x arguments */
201     default:
202         break;
203     }
204     va_end(ap);
205
206     /* attach to other uams */
207     ret = auth_register(type, uam);
208     if ( ret) {
209         free(uam->uam_path);
210         free(uam);
211     }
212
213     return ret;
214 }
215 #endif
216
217 #ifdef ATACC
218 int uam_register_fn(const int type, const char *path, const char *name, void *fn1, void *fn2, 
219                      void *fn3, void *fn4)
220 {
221     va_list ap;
222     struct uam_obj *uam;
223
224     if (!name)
225         return -1;
226
227     /* see if it already exists. */
228     if ((uam = auth_uamfind(type, name, strlen(name)))) {
229         if (strcmp(uam->uam_path, path)) {
230             /* it exists, but it's not the same module. */
231             LOG(log_error, logtype_afpd, "uam_register: \"%s\" already loaded by %s",
232                 name, path);
233             return -1;
234         }
235         uam->uam_count++;
236         return 0;
237     }
238
239     /* allocate space for uam */
240     if ((uam = calloc(1, sizeof(struct uam_obj))) == NULL)
241         return -1;
242
243     uam->uam_name = name;
244     uam->uam_path = strdup(path);
245     uam->uam_count++;
246
247     switch (type) {
248     case UAM_SERVER_LOGIN_EXT: /* expect four arguments */
249         uam->u.uam_login.login_ext = fn4;
250         uam->u.uam_login.login = fn1;
251         uam->u.uam_login.logincont = fn2;
252         uam->u.uam_login.logout = fn3;
253         break;
254     case UAM_SERVER_LOGIN: /* expect three arguments */
255         uam->u.uam_login.login_ext = NULL;
256         uam->u.uam_login.login = fn1;
257         uam->u.uam_login.logincont = fn2;
258         uam->u.uam_login.logout = fn3;
259         break;
260     case UAM_SERVER_CHANGEPW: /* one argument */
261         uam->u.uam_changepw = fn1;
262         break;
263     case UAM_SERVER_PRINTAUTH: /* x arguments */
264     default:
265         break;
266     }
267
268     /* attach to other uams */
269     if (auth_register(type, uam) < 0) {
270         free(uam->uam_path);
271         free(uam);
272         return -1;
273     }
274
275     return 0;
276 }
277 #endif
278
279 void uam_unregister(const int type, const char *name)
280 {
281     struct uam_obj *uam;
282
283     if (!name)
284         return;
285
286     uam = auth_uamfind(type, name, strlen(name));
287     if (!uam || --uam->uam_count > 0)
288         return;
289
290     auth_unregister(uam);
291     free(uam->uam_path);
292     free(uam);
293 }
294
295 /* --- helper functions for plugin uams --- */
296
297 struct passwd *uam_getname(void *private, char *name, const int len)
298 {
299     AFPObj *obj = private;
300     struct passwd *pwent;
301     static char username[256];
302     static char user[256];
303     static char pwname[256];
304     char *p;
305     size_t ulen;
306     u_int16_t flags = CONV_PRECOMPOSE;
307
308     if ((pwent = getpwnam(name)))
309         return pwent;
310
311 #ifndef NO_REAL_USER_NAME
312
313     if ( (size_t) -1 == (ulen = convert_charset((utf8_encoding())?CH_UTF8_MAC:obj->options.maccharset, 0, 
314                                 CH_UCS2, name, len, username, 256, &flags)))
315         return NULL;
316
317     setpwent();
318     while ((pwent = getpwent())) {
319         if ((p = strchr(pwent->pw_gecos, ',')))
320             *p = '\0';
321
322         if ((size_t)-1 == ( ulen = convert_string(obj->options.unixcharset, CH_UCS2, 
323                                 pwent->pw_gecos, strlen(pwent->pw_gecos), user, 256)) )
324                 continue;
325         if ((size_t)-1 == ( ulen = convert_string(obj->options.unixcharset, CH_UCS2, 
326                                 pwent->pw_name, strlen(pwent->pw_name), pwname, 256)) )
327                 continue;
328
329
330         /* check against both the gecos and the name fields. the user
331          * might have just used a different capitalization. */
332
333         if ( (strncasecmp_w((ucs2_t*)user, (ucs2_t*)username, len) == 0) || 
334                 ( strncasecmp_w ( (ucs2_t*) pwname, (ucs2_t*) username, len) == 0)) {
335             strncpy(name, pwent->pw_name, len);
336             name[len - 1] = '\0';
337             break;
338         }
339     }
340     endpwent();
341 #endif /* ! NO_REAL_USER_NAME */
342
343     /* os x server doesn't keep anything useful if we do getpwent */
344     return pwent ? getpwnam(name) : NULL;
345 }
346
347 int uam_checkuser(const struct passwd *pwd)
348 {
349     const char *p;
350
351     if (!pwd)
352         return -1;
353
354 #ifndef DISABLE_SHELLCHECK
355         if (!pwd->pw_shell || (*pwd->pw_shell == '\0')) {
356                 LOG(log_info, logtype_afpd, "uam_checkuser: User %s does not have a shell", pwd->pw_name);
357                 return -1;
358         }
359 #endif
360
361     while ((p = getusershell())) {
362         if ( strcmp( p, pwd->pw_shell ) == 0 )
363             break;
364     }
365     endusershell();
366
367 #ifndef DISABLE_SHELLCHECK
368     if (!p) {
369         LOG(log_info, logtype_afpd, "illegal shell %s for %s", pwd->pw_shell, pwd->pw_name);
370         return -1;
371     }
372 #endif /* DISABLE_SHELLCHECK */
373
374     return 0;
375 }
376
377 /* afp-specific functions */
378 int uam_afpserver_option(void *private, const int what, void *option,
379                          int *len)
380 {
381 AFPObj *obj = private;
382     char **buf = (char **) option; /* most of the options are this */
383     int32_t result;
384     int fd;
385
386     if (!obj || !option)
387         return -1;
388
389     switch (what) {
390     case UAM_OPTION_USERNAME:
391         *buf = (void *) obj->username;
392         if (len)
393             *len = sizeof(obj->username) - 1;
394         break;
395
396     case UAM_OPTION_GUEST:
397         *buf = (void *) obj->options.guest;
398         if (len)
399             *len = strlen(obj->options.guest);
400         break;
401
402     case UAM_OPTION_PASSWDOPT:
403         if (!len)
404             return -1;
405
406         switch (*len) {
407         case UAM_PASSWD_FILENAME:
408             *buf = (void *) obj->options.passwdfile;
409             *len = strlen(obj->options.passwdfile);
410             break;
411
412         case UAM_PASSWD_MINLENGTH:
413             *((int *) option) = obj->options.passwdminlen;
414             *len = sizeof(obj->options.passwdminlen);
415             break;
416
417         case UAM_PASSWD_MAXFAIL:
418             *((int *) option) = obj->options.loginmaxfail;
419             *len = sizeof(obj->options.loginmaxfail);
420             break;
421
422         case UAM_PASSWD_EXPIRETIME: /* not implemented */
423         default:
424             return -1;
425             break;
426         }
427         break;
428
429     case UAM_OPTION_SIGNATURE:
430         *buf = (void *) (((AFPConfig *)obj->config)->signature);
431         if (len)
432             *len = 16;
433         break;
434
435     case UAM_OPTION_RANDNUM: /* returns a random number in 4-byte units. */
436         if (!len || (*len < 0) || (*len % sizeof(result)))
437             return -1;
438
439         /* construct a random number */
440         if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
441             struct timeval tv;
442             struct timezone tz;
443             char *randnum = (char *) option;
444             int i;
445
446             if (gettimeofday(&tv, &tz) < 0)
447                 return -1;
448             srandom(tv.tv_sec + (unsigned long) obj + (unsigned long) obj->handle);
449             for (i = 0; i < *len; i += sizeof(result)) {
450                 result = random();
451                 memcpy(randnum + i, &result, sizeof(result));
452             }
453         } else {
454             result = read(fd, option, *len);
455             close(fd);
456             if (result < 0)
457                 return -1;
458         }
459         break;
460
461     case UAM_OPTION_HOSTNAME:
462         *buf = (void *) obj->options.hostname;
463         if (len)
464             *len = strlen(obj->options.hostname);
465         break;
466
467     case UAM_OPTION_PROTOCOL:
468         *buf = (void *) obj->proto;
469         break;
470     case UAM_OPTION_CLIENTNAME:
471         {
472             struct DSI *dsi = obj->handle;
473             struct hostent *hp;
474
475             hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
476                                 sizeof( struct in_addr ),
477                                 dsi->client.sin_family );
478             if( hp )
479                 *buf = (void *) hp->h_name;
480             else
481                 *buf = (void *) inet_ntoa( dsi->client.sin_addr );
482         }
483         break;
484     case UAM_OPTION_COOKIE:
485         /* it's up to the uam to actually store something useful here.
486          * this just passes back a handle to the cookie. the uam side
487          * needs to do something like **buf = (void *) cookie to store
488          * the cookie. */
489         *buf = (void *) &obj->uam_cookie;
490         break;
491     case UAM_OPTION_KRB5SERVICE:
492         *buf = obj->options.k5service;
493         if (len)
494             *len = (*buf)?strlen(*buf):0;
495         break;
496     case UAM_OPTION_MACCHARSET:
497         *((int *) option) = obj->options.maccharset;
498         *len = sizeof(obj->options.maccharset);
499         break;
500     case UAM_OPTION_UNIXCHARSET:
501         *((int *) option) = obj->options.unixcharset;
502         *len = sizeof(obj->options.unixcharset);
503         break;
504     default:
505         return -1;
506         break;
507     }
508
509     return 0;
510 }
511
512 /* if we need to maintain a connection, this is how we do it.
513  * because an action pointer gets passed in, we can stream 
514  * DSI connections */
515 int uam_afp_read(void *handle, char *buf, int *buflen,
516                  int (*action)(void *, void *, const int))
517 {
518     AFPObj *obj = handle;
519     int len;
520
521     if (!obj)
522         return AFPERR_PARAM;
523
524     switch (obj->proto) {
525     case AFPPROTO_ASP:
526         if ((len = asp_wrtcont(obj->handle, buf, buflen )) < 0)
527             goto uam_afp_read_err;
528         return action(handle, buf, *buflen);
529         break;
530
531     case AFPPROTO_DSI:
532         len = dsi_writeinit(obj->handle, buf, *buflen);
533         if (!len || ((len = action(handle, buf, len)) < 0)) {
534             dsi_writeflush(obj->handle);
535             goto uam_afp_read_err;
536         }
537
538         while ((len = (dsi_write(obj->handle, buf, *buflen)))) {
539             if ((len = action(handle, buf, len)) < 0) {
540                 dsi_writeflush(obj->handle);
541                 goto uam_afp_read_err;
542             }
543         }
544         break;
545     }
546     return 0;
547
548 uam_afp_read_err:
549     *buflen = 0;
550     return len;
551 }
552
553 #ifdef TRU64
554 void uam_afp_getcmdline( int *ac, char ***av )
555 {
556     afp_get_cmdline( ac, av );
557 }
558
559 int uam_sia_validate_user(sia_collect_func_t * collect, int argc, char **argv,
560                          char *hostname, char *username, char *tty,
561                          int colinput, char *gssapi, char *passphrase)
562 /* A clone of the Tru64 system function sia_validate_user() that calls
563  * sia_ses_authent() rather than sia_ses_reauthent()   
564  * Added extra code to take into account suspected SIA bug whereby it clobbers
565  * the signal handler on SIGALRM (tickle) installed by Netatalk/afpd
566  */
567 {
568        SIAENTITY *entity = NULL;
569        struct sigaction act;
570        int rc;
571
572        if ((rc=sia_ses_init(&entity, argc, argv, hostname, username, tty,
573                             colinput, gssapi)) != SIASUCCESS) {
574                LOG(log_error, logtype_afpd, "cannot initialise SIA");
575                return SIAFAIL;
576        }
577
578        /* save old action for restoration later */
579        if (sigaction(SIGALRM, NULL, &act))
580                LOG(log_error, logtype_afpd, "cannot save SIGALRM handler");
581
582        if ((rc=sia_ses_authent(collect, passphrase, entity)) != SIASUCCESS) {
583                /* restore old action after clobbering by sia_ses_authent() */
584                if (sigaction(SIGALRM, &act, NULL))
585                        LOG(log_error, logtype_afpd, "cannot restore SIGALRM handler");
586
587                LOG(log_info, logtype_afpd, "unsuccessful login for %s",
588 (hostname?hostname:"(null)"));
589                return SIAFAIL;
590        }
591        LOG(log_info, logtype_afpd, "successful login for %s",
592 (hostname?hostname:"(null)"));
593
594        /* restore old action after clobbering by sia_ses_authent() */   
595        if (sigaction(SIGALRM, &act, NULL))
596                LOG(log_error, logtype_afpd, "cannot restore SIGALRM handler");
597
598        sia_ses_release(&entity);
599
600        return SIASUCCESS;
601 }
602 #endif /* TRU64 */
603
604 /* --- papd-specific functions (just placeholders) --- */
605 void append(void *pf, char *data, int len)
606 {
607     return;
608 }