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