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