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