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