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