]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/uam.c
implemented config.h
[netatalk.git] / etc / afpd / uam.c
1 /* Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
2  * All Rights Reserved.  See COPYRIGHT.
3  */
4
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <fcntl.h>
14 #include <ctype.h>
15 #include <syslog.h>
16 #include <sys/param.h>
17 #include <sys/time.h>
18
19 #ifdef SHADOWPW
20 #include <shadow.h>
21 #endif SHADOWPW
22
23 #include <netatalk/endian.h>
24 #include <atalk/asp.h>
25 #include <atalk/dsi.h>
26 #include <atalk/afp.h>
27 #include <atalk/util.h>
28
29 #include "globals.h"
30 #include "afp_config.h"
31 #include "auth.h"
32 #include "uam_auth.h"
33
34 /* --- server uam functions -- */
35
36 /* uam_load. uams must have a uam_setup function. */
37 struct uam_mod *uam_load(const char *path, const char *name)
38 {
39   char buf[MAXPATHLEN + 1], *p;
40   struct uam_mod *mod;
41   void *module;
42
43   if ((module = mod_open(path)) == NULL) {
44     syslog(LOG_ERR, "uam_load(%s): failed to load: %s", name, mod_error());
45     return NULL;
46   }
47
48   if ((mod = (struct uam_mod *) malloc(sizeof(struct uam_mod))) == NULL) {
49     syslog(LOG_ERR, "uam_load(%s): malloc failed", name);
50     goto uam_load_fail;
51   }
52
53   strncpy(buf, name, sizeof(buf));
54   if ((p = strchr(buf, '.')))
55     *p = '\0';
56   if ((mod->uam_fcn = mod_symbol(module, buf)) == NULL) {
57     goto uam_load_err;
58   }
59
60   if (mod->uam_fcn->uam_type != UAM_MODULE_SERVER) {
61     syslog(LOG_ERR, "uam_load(%s): attempted to load a non-server module",
62            name);
63     goto uam_load_err;
64   }
65
66   /* version check would go here */
67
68   if (!mod->uam_fcn->uam_setup || 
69       ((*mod->uam_fcn->uam_setup)(name) < 0)) {
70     syslog(LOG_ERR, "uam_load(%s): uam_setup failed", name);
71     goto uam_load_err;
72   }
73
74   mod->uam_module = module;
75   return mod;
76
77 uam_load_err:
78   free(mod);
79 uam_load_fail:
80   mod_close(module);
81   return NULL;
82 }
83
84 /* unload the module. we check for a cleanup function, but we don't
85  * die if one doesn't exist. however, things are likely to leak without one.
86  */
87 void uam_unload(struct uam_mod *mod)
88 {
89   if (mod->uam_fcn->uam_cleanup)
90     (*mod->uam_fcn->uam_cleanup)();
91   mod_close(mod->uam_module);
92   free(mod);
93 }
94
95 /* -- client-side uam functions -- */
96
97 /* set up stuff for this uam. */
98 int uam_register(const int type, const char *path, const char *name, ...)
99 {
100   va_list ap;
101   struct uam_obj *uam;
102
103   if (!name)
104     return -1;
105
106   /* see if it already exists. */
107   if ((uam = auth_uamfind(type, name, strlen(name)))) {
108     if (strcmp(uam->uam_path, path)) {
109       /* it exists, but it's not the same module. */
110       syslog(LOG_ERR, "uam_register: \"%s\" already loaded by %s",
111              name, path);
112       return -1;
113     }
114     uam->uam_count++;
115     return 0;
116   }
117   
118   /* allocate space for uam */
119   if ((uam = calloc(1, sizeof(struct uam_obj))) == NULL)
120     return -1;
121
122   uam->uam_name = name;
123   uam->uam_path = strdup(path);
124   uam->uam_count++;
125
126   va_start(ap, name);
127   switch (type) {
128   case UAM_SERVER_LOGIN: /* expect three arguments */
129     uam->u.uam_login.login = va_arg(ap, void *);
130     uam->u.uam_login.logincont = va_arg(ap, void *);
131     uam->u.uam_login.logout = va_arg(ap, void *);
132     break;
133   case UAM_SERVER_CHANGEPW: /* one argument */
134     uam->u.uam_changepw = va_arg(ap, void *);
135     break;
136   case UAM_SERVER_PRINTAUTH: /* x arguments */
137   default:
138     break;
139   }
140   va_end(ap);
141
142   /* attach to other uams */
143   if (auth_register(type, uam) < 0) {
144     free(uam->uam_path);
145     free(uam);
146     return -1;
147   }
148
149   return 0;
150 }
151
152 void uam_unregister(const int type, const char *name)
153 {
154   struct uam_obj *uam;
155
156   if (!name)
157     return;
158
159   uam = auth_uamfind(type, name, strlen(name));
160   if (!uam || --uam->uam_count > 0)
161     return;
162
163   auth_unregister(uam);
164   free(uam->uam_path);
165   free(uam);
166 }
167
168 /* --- helper functions for plugin uams --- */
169
170 struct passwd *uam_getname(char *name, const int len)
171 {
172   struct passwd *pwent;
173   char *user;
174   int i;
175
176   if ((pwent = getpwnam(name)))
177     return pwent;
178
179 #ifndef NO_REAL_USER_NAME
180   for (i = 0; i < len; i++)
181     name[i] = tolower(name[i]);
182
183   setpwent();
184   while ((pwent = getpwent())) {
185     if (user = strchr(pwent->pw_gecos, ','))
186       *user = '\0';
187     user = pwent->pw_gecos;
188
189     /* check against both the gecos and the name fields. the user
190      * might have just used a different capitalization. */
191     if ((strncasecmp(user, name, len) == 0) ||
192         (strncasecmp(pwent->pw_name, name, len) == 0)) {
193       strncpy(name, pwent->pw_name, len);
194       break;
195     }
196   }
197   endpwent();
198 #endif
199
200   /* os x server doesn't keep anything useful if we do getpwent */
201   return pwent ? getpwnam(name) : NULL;
202 }
203
204 int uam_checkuser(const struct passwd *pwd)
205 {
206   char *p;
207
208   if (!pwd || !pwd->pw_shell || (*pwd->pw_shell == '\0')) 
209     return -1;
210
211   while ((p = getusershell())) {
212     if ( strcmp( p, pwd->pw_shell ) == 0 ) 
213       break;
214   }
215   endusershell();
216
217   if (!p) {
218     syslog( LOG_INFO, "illegal shell %s for %s", pwd->pw_shell, pwd->pw_name);
219     return -1;
220   }
221
222   return 0;
223 }
224
225 /* afp-specific functions */
226 int uam_afpserver_option(void *private, const int what, void *option,
227                          int *len)
228 {
229   AFPObj *obj = private;
230   char **buf = (char **) option; /* most of the options are this */
231   int32_t result;
232   int fd;
233
234   if (!obj || !option)
235     return -1;
236
237   switch (what) {
238   case UAM_OPTION_USERNAME:
239     *buf = (void *) obj->username;
240     if (len) 
241       *len = sizeof(obj->username) - 1;
242     break;
243
244   case UAM_OPTION_GUEST:
245     *buf = (void *) obj->options.guest;
246     if (len) 
247       *len = strlen(obj->options.guest);
248     break;
249
250   case UAM_OPTION_PASSWDOPT:
251     if (!len)
252       return -1;
253
254     switch (*len) {
255     case UAM_PASSWD_FILENAME:
256       *buf = (void *) obj->options.passwdfile;
257       *len = strlen(obj->options.passwdfile);
258       break;
259
260     case UAM_PASSWD_MINLENGTH:
261       *((int *) option) = obj->options.passwdminlen;
262       *len = sizeof(obj->options.passwdminlen);
263       break;
264
265     case UAM_PASSWD_MAXFAIL: 
266       *((int *) option) = obj->options.loginmaxfail;
267       *len = sizeof(obj->options.loginmaxfail);
268       break;
269       
270     case UAM_PASSWD_EXPIRETIME: /* not implemented */
271     default:
272       return -1;
273     break;
274     }
275     break;
276
277   case UAM_OPTION_SIGNATURE:
278     *buf = (void *) (((AFPConfig *)obj->config)->signature);
279     if (len)
280       *len = 16;
281     break;
282
283   case UAM_OPTION_RANDNUM: /* returns a random number in 4-byte units. */
284     if (!len || (*len < 0) || (*len % sizeof(result)))
285       return -1;
286
287     /* construct a random number */
288     if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
289       struct timeval tv;
290       struct timezone tz;
291       char *randnum = (char *) option;
292       int i;
293     
294       if (gettimeofday(&tv, &tz) < 0)
295         return -1;
296       srandom(tv.tv_sec + (unsigned long) obj + (unsigned long) obj->handle);
297       for (i = 0; i < *len; i += sizeof(result)) {
298         result = random();
299         memcpy(randnum + i, &result, sizeof(result));
300       }
301     } else {
302       result = read(fd, option, *len);
303       close(fd);
304       if (result < 0)
305         return -1;
306     }
307     break;
308
309   case UAM_OPTION_HOSTNAME:
310     *buf = (void *) obj->options.hostname;
311     if (len) 
312       *len = strlen(obj->options.hostname);
313     break;
314
315   case UAM_OPTION_COOKIE: 
316     /* it's up to the uam to actually store something useful here.
317      * this just passes back a handle to the cookie. the uam side
318      * needs to do something like **buf = (void *) cookie to store
319      * the cookie. */
320     *buf = (void *) &obj->uam_cookie;
321     break;
322
323   default:
324     return -1;
325     break;
326   }
327
328   return 0;
329 }
330
331 /* if we need to maintain a connection, this is how we do it. 
332  * because an action pointer gets passed in, we can stream 
333  * DSI connections */
334 int uam_afp_read(void *handle, char *buf, int *buflen, 
335                  int (*action)(void *, void *, const int))
336 {
337   AFPObj *obj = handle;
338   int len;
339
340   if (!obj)
341     return AFPERR_PARAM;
342
343   switch (obj->proto) {
344     case AFPPROTO_ASP:
345       if ((len = asp_wrtcont(obj->handle, buf, buflen )) < 0) 
346         goto uam_afp_read_err;
347       return action(handle, buf, *buflen);
348       break;
349
350     case AFPPROTO_DSI:
351       len = dsi_writeinit(obj->handle, buf, *buflen);
352       if (!len || ((len = action(handle, buf, len)) < 0)) {
353         dsi_writeflush(obj->handle);
354         goto uam_afp_read_err;
355       }
356         
357       while (len = (dsi_write(obj->handle, buf, *buflen))) {
358         if ((len = action(handle, buf, len)) < 0) {
359           dsi_writeflush(obj->handle);
360           goto uam_afp_read_err;
361         }
362       }
363       break;
364   }
365   return 0;
366
367 uam_afp_read_err:
368   *buflen = 0;
369   return len;
370 }
371
372 /* --- papd-specific functions (just placeholders) --- */
373 void append(void *pf, char *data, int len)
374 {
375         return;
376 }