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