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