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