]> arthur.barton.de Git - netatalk.git/blob - etc/papd/uam.c
changed all LOG messages using logtype_default to use logtype_papd
[netatalk.git] / etc / papd / uam.c
1 /*
2  * $Id: uam.c,v 1.8 2002-09-29 23:29:14 sibaz 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 #include <fcntl.h>
34 #include <ctype.h>
35 #include <atalk/logger.h>
36 #include <sys/param.h>
37 #include <sys/time.h>
38
39 #include <netatalk/endian.h>
40 #include <atalk/asp.h>
41 #include <atalk/dsi.h>
42 #include <atalk/afp.h>
43 #include <atalk/util.h>
44
45 #include "uam_auth.h"
46
47 /* --- server uam functions -- */
48
49 /* uam_load. uams must have a uam_setup function. */
50 struct uam_mod *uam_load(const char *path, const char *name)
51 {
52   char buf[MAXPATHLEN + 1], *p;
53   struct uam_mod *mod;
54   void *module;
55
56   if ((module = mod_open(path)) == NULL) {
57     LOG(log_error, logtype_papd, "uam_load(%s): failed to load.", name);
58     LOG(log_error, logtype_papd, dlerror());
59     return NULL;
60   }
61
62   if ((mod = (struct uam_mod *) malloc(sizeof(struct uam_mod))) == NULL) {
63     LOG(log_error, logtype_papd, "uam_load(%s): malloc failed", name);
64     goto uam_load_fail;
65   }
66
67   strncpy(buf, name, sizeof(buf));
68   if ((p = strchr(buf, '.')))
69     *p = '\0';
70   if ((mod->uam_fcn = mod_symbol(module, buf)) == NULL) {
71     goto uam_load_err;
72   }
73
74   if (mod->uam_fcn->uam_type != UAM_MODULE_SERVER) {
75     LOG(log_error, logtype_papd, "uam_load(%s): attempted to load a non-server module",
76            name);
77     goto uam_load_err;
78   }
79
80   /* version check would go here */
81
82   if (!mod->uam_fcn->uam_setup || 
83       ((*mod->uam_fcn->uam_setup)(name) < 0)) {
84     LOG(log_error, logtype_papd, "uam_load(%s): uam_setup failed", name);
85     goto uam_load_err;
86   }
87
88   mod->uam_module = module;
89   return mod;
90
91 uam_load_err:
92   free(mod);
93 uam_load_fail:
94   mod_close(module);
95   return NULL;
96 }
97
98 /* unload the module. we check for a cleanup function, but we don't
99  * die if one doesn't exist. however, things are likely to leak without one.
100  */
101 void uam_unload(struct uam_mod *mod)
102 {
103   if (mod->uam_fcn->uam_cleanup)
104     (*mod->uam_fcn->uam_cleanup)();
105   mod_close(mod->uam_module);
106   free(mod);
107 }
108
109 /* -- client-side uam functions -- */
110
111 /* set up stuff for this uam. */
112 int uam_register(const int type, const char *path, const char *name, ...)
113 {
114   va_list ap;
115   struct uam_obj *uam;
116
117   if (!name)
118     return -1;
119
120   /* see if it already exists. */
121   if ((uam = auth_uamfind(type, name, strlen(name)))) {
122     if (strcmp(uam->uam_path, path)) {
123       /* it exists, but it's not the same module. */
124       LOG(log_error, logtype_papd, "uam_register: \"%s\" already loaded by %s",
125              name, path);
126       return -1;
127     }
128     uam->uam_count++;
129     return 0;
130   }
131   
132   /* allocate space for uam */
133   if ((uam = calloc(1, sizeof(struct uam_obj))) == NULL)
134     return -1;
135
136   uam->uam_name = name;
137   uam->uam_path = strdup(path);
138   uam->uam_count++;
139
140   va_start(ap, name);
141   switch (type) {
142   case UAM_SERVER_LOGIN: /* expect three arguments */
143     uam->u.uam_login.login = va_arg(ap, void *);
144     uam->u.uam_login.logincont = va_arg(ap, void *);
145     uam->u.uam_login.logout = va_arg(ap, void *);
146     break;
147   case UAM_SERVER_CHANGEPW: /* one argument */
148     uam->u.uam_changepw = va_arg(ap, void *);
149     break;
150   case UAM_SERVER_PRINTAUTH: /* x arguments */
151     uam->u.uam_printer = va_arg(ap, void *);
152     break;
153   default:
154     break;
155   }
156   va_end(ap);
157
158   /* attach to other uams */
159   if (auth_register(type, uam) < 0) {
160     free(uam->uam_path);
161     free(uam);
162     return -1;
163   }
164
165   return 0;
166 }
167
168 void uam_unregister(const int type, const char *name)
169 {
170   struct uam_obj *uam;
171
172   if (!name)
173     return;
174
175   uam = auth_uamfind(type, name, strlen(name));
176   if (!uam || --uam->uam_count > 0)
177     return;
178
179   auth_unregister(uam);
180   free(uam->uam_path);
181   free(uam);
182 }
183
184 /* Crap to support uams which call this afpd function */
185 int uam_afpserver_option(void *private, const int what, void *option,
186                          int *len)
187 {
188         return(0);
189 }
190
191 /* --- helper functions for plugin uams --- */
192
193 struct passwd *uam_getname(char *name, const int len)
194 {
195   struct passwd *pwent;
196   char *user;
197   int i;
198
199   if ((pwent = getpwnam(name)))
200     return pwent;
201
202 #ifndef NO_REAL_USER_NAME
203   for (i = 0; i < len; i++)
204     name[i] = tolower(name[i]);
205
206   setpwent();
207   while ((pwent = getpwent())) {
208     if ((user = strchr(pwent->pw_gecos, ','))) *user = '\0';
209     user = pwent->pw_gecos;
210
211     /* check against both the gecos and the name fields. the user
212      * might have just used a different capitalization. */
213     if ((strncasecmp(user, name, len) == 0) ||
214         (strncasecmp(pwent->pw_name, name, len) == 0)) {
215       strncpy(name, pwent->pw_name, len);
216       break;
217     }
218   }
219   endpwent();
220 #endif /* NO_REAL_USER_NAME */
221
222   /* os x server doesn't keep anything useful if we do getpwent */
223   return pwent ? getpwnam(name) : NULL;
224 }
225
226
227 int uam_checkuser(const struct passwd *pwd)
228 {
229   char *p;
230
231   if (!pwd || !pwd->pw_shell || (*pwd->pw_shell == '\0')) 
232     return -1;
233
234   while ((p = getusershell())) {
235     if ( strcmp( p, pwd->pw_shell ) == 0 ) 
236       break;
237   }
238   endusershell();
239
240 #ifndef DISABLE_SHELLCHECK
241   if (!p) {
242     LOG(log_info, logtype_papd, "illegal shell %s for %s",pwd->pw_shell,pwd->pw_name);
243     return -1;
244   }
245 #endif /* DISABLE_SHELLCHECK */
246
247   return 0;
248 }
249
250