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