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