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