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