1*76404edcSAsim Jamshed #include "plugin.h"
2*76404edcSAsim Jamshed #include "http_auth.h"
3*76404edcSAsim Jamshed #include "log.h"
4*76404edcSAsim Jamshed #include "response.h"
5*76404edcSAsim Jamshed
6*76404edcSAsim Jamshed #include <sys/types.h>
7*76404edcSAsim Jamshed #include <sys/stat.h>
8*76404edcSAsim Jamshed
9*76404edcSAsim Jamshed #include <stdlib.h>
10*76404edcSAsim Jamshed #include <string.h>
11*76404edcSAsim Jamshed #include <errno.h>
12*76404edcSAsim Jamshed #include <fcntl.h>
13*76404edcSAsim Jamshed #include <unistd.h>
14*76404edcSAsim Jamshed
15*76404edcSAsim Jamshed handler_t auth_ldap_init(server *srv, mod_auth_plugin_config *s);
16*76404edcSAsim Jamshed
17*76404edcSAsim Jamshed
18*76404edcSAsim Jamshed /**
19*76404edcSAsim Jamshed * the basic and digest auth framework
20*76404edcSAsim Jamshed *
21*76404edcSAsim Jamshed * - config handling
22*76404edcSAsim Jamshed * - protocol handling
23*76404edcSAsim Jamshed *
24*76404edcSAsim Jamshed * http_auth.c
25*76404edcSAsim Jamshed * http_auth_digest.c
26*76404edcSAsim Jamshed *
27*76404edcSAsim Jamshed * do the real work
28*76404edcSAsim Jamshed */
29*76404edcSAsim Jamshed
INIT_FUNC(mod_auth_init)30*76404edcSAsim Jamshed INIT_FUNC(mod_auth_init) {
31*76404edcSAsim Jamshed mod_auth_plugin_data *p;
32*76404edcSAsim Jamshed
33*76404edcSAsim Jamshed p = calloc(1, sizeof(*p));
34*76404edcSAsim Jamshed
35*76404edcSAsim Jamshed p->tmp_buf = buffer_init();
36*76404edcSAsim Jamshed
37*76404edcSAsim Jamshed p->auth_user = buffer_init();
38*76404edcSAsim Jamshed #ifdef USE_LDAP
39*76404edcSAsim Jamshed p->ldap_filter = buffer_init();
40*76404edcSAsim Jamshed #endif
41*76404edcSAsim Jamshed
42*76404edcSAsim Jamshed return p;
43*76404edcSAsim Jamshed }
44*76404edcSAsim Jamshed
FREE_FUNC(mod_auth_free)45*76404edcSAsim Jamshed FREE_FUNC(mod_auth_free) {
46*76404edcSAsim Jamshed mod_auth_plugin_data *p = p_d;
47*76404edcSAsim Jamshed
48*76404edcSAsim Jamshed UNUSED(srv);
49*76404edcSAsim Jamshed
50*76404edcSAsim Jamshed if (!p) return HANDLER_GO_ON;
51*76404edcSAsim Jamshed
52*76404edcSAsim Jamshed buffer_free(p->tmp_buf);
53*76404edcSAsim Jamshed buffer_free(p->auth_user);
54*76404edcSAsim Jamshed #ifdef USE_LDAP
55*76404edcSAsim Jamshed buffer_free(p->ldap_filter);
56*76404edcSAsim Jamshed #endif
57*76404edcSAsim Jamshed
58*76404edcSAsim Jamshed if (p->config_storage) {
59*76404edcSAsim Jamshed size_t i;
60*76404edcSAsim Jamshed for (i = 0; i < srv->config_context->used; i++) {
61*76404edcSAsim Jamshed mod_auth_plugin_config *s = p->config_storage[i];
62*76404edcSAsim Jamshed
63*76404edcSAsim Jamshed if (!s) continue;
64*76404edcSAsim Jamshed
65*76404edcSAsim Jamshed array_free(s->auth_require);
66*76404edcSAsim Jamshed buffer_free(s->auth_plain_groupfile);
67*76404edcSAsim Jamshed buffer_free(s->auth_plain_userfile);
68*76404edcSAsim Jamshed buffer_free(s->auth_htdigest_userfile);
69*76404edcSAsim Jamshed buffer_free(s->auth_htpasswd_userfile);
70*76404edcSAsim Jamshed buffer_free(s->auth_backend_conf);
71*76404edcSAsim Jamshed
72*76404edcSAsim Jamshed buffer_free(s->auth_ldap_hostname);
73*76404edcSAsim Jamshed buffer_free(s->auth_ldap_basedn);
74*76404edcSAsim Jamshed buffer_free(s->auth_ldap_binddn);
75*76404edcSAsim Jamshed buffer_free(s->auth_ldap_bindpw);
76*76404edcSAsim Jamshed buffer_free(s->auth_ldap_filter);
77*76404edcSAsim Jamshed buffer_free(s->auth_ldap_cafile);
78*76404edcSAsim Jamshed
79*76404edcSAsim Jamshed #ifdef USE_LDAP
80*76404edcSAsim Jamshed buffer_free(s->ldap_filter_pre);
81*76404edcSAsim Jamshed buffer_free(s->ldap_filter_post);
82*76404edcSAsim Jamshed
83*76404edcSAsim Jamshed if (s->ldap) ldap_unbind_s(s->ldap);
84*76404edcSAsim Jamshed #endif
85*76404edcSAsim Jamshed
86*76404edcSAsim Jamshed free(s);
87*76404edcSAsim Jamshed }
88*76404edcSAsim Jamshed free(p->config_storage);
89*76404edcSAsim Jamshed }
90*76404edcSAsim Jamshed
91*76404edcSAsim Jamshed free(p);
92*76404edcSAsim Jamshed
93*76404edcSAsim Jamshed return HANDLER_GO_ON;
94*76404edcSAsim Jamshed }
95*76404edcSAsim Jamshed
96*76404edcSAsim Jamshed #define PATCH(x) \
97*76404edcSAsim Jamshed p->conf.x = s->x;
mod_auth_patch_connection(server * srv,connection * con,mod_auth_plugin_data * p)98*76404edcSAsim Jamshed static int mod_auth_patch_connection(server *srv, connection *con, mod_auth_plugin_data *p) {
99*76404edcSAsim Jamshed size_t i, j;
100*76404edcSAsim Jamshed mod_auth_plugin_config *s = p->config_storage[0];
101*76404edcSAsim Jamshed
102*76404edcSAsim Jamshed PATCH(auth_backend);
103*76404edcSAsim Jamshed PATCH(auth_plain_groupfile);
104*76404edcSAsim Jamshed PATCH(auth_plain_userfile);
105*76404edcSAsim Jamshed PATCH(auth_htdigest_userfile);
106*76404edcSAsim Jamshed PATCH(auth_htpasswd_userfile);
107*76404edcSAsim Jamshed PATCH(auth_require);
108*76404edcSAsim Jamshed PATCH(auth_debug);
109*76404edcSAsim Jamshed PATCH(auth_ldap_hostname);
110*76404edcSAsim Jamshed PATCH(auth_ldap_basedn);
111*76404edcSAsim Jamshed PATCH(auth_ldap_binddn);
112*76404edcSAsim Jamshed PATCH(auth_ldap_bindpw);
113*76404edcSAsim Jamshed PATCH(auth_ldap_filter);
114*76404edcSAsim Jamshed PATCH(auth_ldap_cafile);
115*76404edcSAsim Jamshed PATCH(auth_ldap_starttls);
116*76404edcSAsim Jamshed PATCH(auth_ldap_allow_empty_pw);
117*76404edcSAsim Jamshed #ifdef USE_LDAP
118*76404edcSAsim Jamshed p->anon_conf = s;
119*76404edcSAsim Jamshed PATCH(ldap_filter_pre);
120*76404edcSAsim Jamshed PATCH(ldap_filter_post);
121*76404edcSAsim Jamshed #endif
122*76404edcSAsim Jamshed
123*76404edcSAsim Jamshed /* skip the first, the global context */
124*76404edcSAsim Jamshed for (i = 1; i < srv->config_context->used; i++) {
125*76404edcSAsim Jamshed data_config *dc = (data_config *)srv->config_context->data[i];
126*76404edcSAsim Jamshed s = p->config_storage[i];
127*76404edcSAsim Jamshed
128*76404edcSAsim Jamshed /* condition didn't match */
129*76404edcSAsim Jamshed if (!config_check_cond(srv, con, dc)) continue;
130*76404edcSAsim Jamshed
131*76404edcSAsim Jamshed /* merge config */
132*76404edcSAsim Jamshed for (j = 0; j < dc->value->used; j++) {
133*76404edcSAsim Jamshed data_unset *du = dc->value->data[j];
134*76404edcSAsim Jamshed
135*76404edcSAsim Jamshed if (buffer_is_equal_string(du->key, CONST_STR_LEN("auth.backend"))) {
136*76404edcSAsim Jamshed PATCH(auth_backend);
137*76404edcSAsim Jamshed } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("auth.backend.plain.groupfile"))) {
138*76404edcSAsim Jamshed PATCH(auth_plain_groupfile);
139*76404edcSAsim Jamshed } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("auth.backend.plain.userfile"))) {
140*76404edcSAsim Jamshed PATCH(auth_plain_userfile);
141*76404edcSAsim Jamshed } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("auth.backend.htdigest.userfile"))) {
142*76404edcSAsim Jamshed PATCH(auth_htdigest_userfile);
143*76404edcSAsim Jamshed } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("auth.backend.htpasswd.userfile"))) {
144*76404edcSAsim Jamshed PATCH(auth_htpasswd_userfile);
145*76404edcSAsim Jamshed } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("auth.require"))) {
146*76404edcSAsim Jamshed PATCH(auth_require);
147*76404edcSAsim Jamshed } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("auth.debug"))) {
148*76404edcSAsim Jamshed PATCH(auth_debug);
149*76404edcSAsim Jamshed } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("auth.backend.ldap.hostname"))) {
150*76404edcSAsim Jamshed PATCH(auth_ldap_hostname);
151*76404edcSAsim Jamshed #ifdef USE_LDAP
152*76404edcSAsim Jamshed p->anon_conf = s;
153*76404edcSAsim Jamshed #endif
154*76404edcSAsim Jamshed } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("auth.backend.ldap.base-dn"))) {
155*76404edcSAsim Jamshed PATCH(auth_ldap_basedn);
156*76404edcSAsim Jamshed } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("auth.backend.ldap.filter"))) {
157*76404edcSAsim Jamshed PATCH(auth_ldap_filter);
158*76404edcSAsim Jamshed #ifdef USE_LDAP
159*76404edcSAsim Jamshed PATCH(ldap_filter_pre);
160*76404edcSAsim Jamshed PATCH(ldap_filter_post);
161*76404edcSAsim Jamshed #endif
162*76404edcSAsim Jamshed } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("auth.backend.ldap.ca-file"))) {
163*76404edcSAsim Jamshed PATCH(auth_ldap_cafile);
164*76404edcSAsim Jamshed } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("auth.backend.ldap.starttls"))) {
165*76404edcSAsim Jamshed PATCH(auth_ldap_starttls);
166*76404edcSAsim Jamshed } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("auth.backend.ldap.bind-dn"))) {
167*76404edcSAsim Jamshed PATCH(auth_ldap_binddn);
168*76404edcSAsim Jamshed } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("auth.backend.ldap.bind-pw"))) {
169*76404edcSAsim Jamshed PATCH(auth_ldap_bindpw);
170*76404edcSAsim Jamshed } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("auth.backend.ldap.allow-empty-pw"))) {
171*76404edcSAsim Jamshed PATCH(auth_ldap_allow_empty_pw);
172*76404edcSAsim Jamshed }
173*76404edcSAsim Jamshed }
174*76404edcSAsim Jamshed }
175*76404edcSAsim Jamshed
176*76404edcSAsim Jamshed return 0;
177*76404edcSAsim Jamshed }
178*76404edcSAsim Jamshed #undef PATCH
179*76404edcSAsim Jamshed
mod_auth_uri_handler(server * srv,connection * con,void * p_d)180*76404edcSAsim Jamshed static handler_t mod_auth_uri_handler(server *srv, connection *con, void *p_d) {
181*76404edcSAsim Jamshed size_t k;
182*76404edcSAsim Jamshed int auth_required = 0, auth_satisfied = 0;
183*76404edcSAsim Jamshed char *http_authorization = NULL;
184*76404edcSAsim Jamshed const char *auth_type = NULL;
185*76404edcSAsim Jamshed data_string *ds;
186*76404edcSAsim Jamshed mod_auth_plugin_data *p = p_d;
187*76404edcSAsim Jamshed array *req;
188*76404edcSAsim Jamshed
189*76404edcSAsim Jamshed /* select the right config */
190*76404edcSAsim Jamshed mod_auth_patch_connection(srv, con, p);
191*76404edcSAsim Jamshed
192*76404edcSAsim Jamshed if (p->conf.auth_require == NULL) return HANDLER_GO_ON;
193*76404edcSAsim Jamshed
194*76404edcSAsim Jamshed /*
195*76404edcSAsim Jamshed * AUTH
196*76404edcSAsim Jamshed *
197*76404edcSAsim Jamshed */
198*76404edcSAsim Jamshed
199*76404edcSAsim Jamshed /* do we have to ask for auth ? */
200*76404edcSAsim Jamshed
201*76404edcSAsim Jamshed auth_required = 0;
202*76404edcSAsim Jamshed auth_satisfied = 0;
203*76404edcSAsim Jamshed
204*76404edcSAsim Jamshed /* search auth-directives for path */
205*76404edcSAsim Jamshed for (k = 0; k < p->conf.auth_require->used; k++) {
206*76404edcSAsim Jamshed buffer *require = p->conf.auth_require->data[k]->key;
207*76404edcSAsim Jamshed
208*76404edcSAsim Jamshed if (require->used == 0) continue;
209*76404edcSAsim Jamshed if (con->uri.path->used < require->used) continue;
210*76404edcSAsim Jamshed
211*76404edcSAsim Jamshed /* if we have a case-insensitive FS we have to lower-case the URI here too */
212*76404edcSAsim Jamshed
213*76404edcSAsim Jamshed if (con->conf.force_lowercase_filenames) {
214*76404edcSAsim Jamshed if (0 == strncasecmp(con->uri.path->ptr, require->ptr, require->used - 1)) {
215*76404edcSAsim Jamshed auth_required = 1;
216*76404edcSAsim Jamshed break;
217*76404edcSAsim Jamshed }
218*76404edcSAsim Jamshed } else {
219*76404edcSAsim Jamshed if (0 == strncmp(con->uri.path->ptr, require->ptr, require->used - 1)) {
220*76404edcSAsim Jamshed auth_required = 1;
221*76404edcSAsim Jamshed break;
222*76404edcSAsim Jamshed }
223*76404edcSAsim Jamshed }
224*76404edcSAsim Jamshed }
225*76404edcSAsim Jamshed
226*76404edcSAsim Jamshed /* nothing to do for us */
227*76404edcSAsim Jamshed if (auth_required == 0) return HANDLER_GO_ON;
228*76404edcSAsim Jamshed
229*76404edcSAsim Jamshed req = ((data_array *)(p->conf.auth_require->data[k]))->value;
230*76404edcSAsim Jamshed
231*76404edcSAsim Jamshed /* try to get Authorization-header */
232*76404edcSAsim Jamshed
233*76404edcSAsim Jamshed if (NULL != (ds = (data_string *)array_get_element(con->request.headers, "Authorization"))) {
234*76404edcSAsim Jamshed http_authorization = ds->value->ptr;
235*76404edcSAsim Jamshed }
236*76404edcSAsim Jamshed
237*76404edcSAsim Jamshed if (ds && ds->value && ds->value->used) {
238*76404edcSAsim Jamshed char *auth_realm;
239*76404edcSAsim Jamshed data_string *method;
240*76404edcSAsim Jamshed
241*76404edcSAsim Jamshed method = (data_string *)array_get_element(req, "method");
242*76404edcSAsim Jamshed
243*76404edcSAsim Jamshed /* parse auth-header */
244*76404edcSAsim Jamshed if (NULL != (auth_realm = strchr(http_authorization, ' '))) {
245*76404edcSAsim Jamshed int auth_type_len = auth_realm - http_authorization;
246*76404edcSAsim Jamshed
247*76404edcSAsim Jamshed if ((auth_type_len == 5) &&
248*76404edcSAsim Jamshed (0 == strncasecmp(http_authorization, "Basic", auth_type_len))) {
249*76404edcSAsim Jamshed auth_type = "Basic";
250*76404edcSAsim Jamshed
251*76404edcSAsim Jamshed if (0 == strcmp(method->value->ptr, "basic")) {
252*76404edcSAsim Jamshed auth_satisfied = http_auth_basic_check(srv, con, p, req, con->uri.path, auth_realm+1);
253*76404edcSAsim Jamshed }
254*76404edcSAsim Jamshed } else if ((auth_type_len == 6) &&
255*76404edcSAsim Jamshed (0 == strncasecmp(http_authorization, "Digest", auth_type_len))) {
256*76404edcSAsim Jamshed auth_type = "Digest";
257*76404edcSAsim Jamshed if (0 == strcmp(method->value->ptr, "digest")) {
258*76404edcSAsim Jamshed if (-1 == (auth_satisfied = http_auth_digest_check(srv, con, p, req, con->uri.path, auth_realm+1))) {
259*76404edcSAsim Jamshed con->http_status = 400;
260*76404edcSAsim Jamshed con->mode = DIRECT;
261*76404edcSAsim Jamshed
262*76404edcSAsim Jamshed /* a field was missing */
263*76404edcSAsim Jamshed
264*76404edcSAsim Jamshed return HANDLER_FINISHED;
265*76404edcSAsim Jamshed }
266*76404edcSAsim Jamshed }
267*76404edcSAsim Jamshed } else {
268*76404edcSAsim Jamshed log_error_write(srv, __FILE__, __LINE__, "ss",
269*76404edcSAsim Jamshed "unknown authentification type:",
270*76404edcSAsim Jamshed http_authorization);
271*76404edcSAsim Jamshed }
272*76404edcSAsim Jamshed }
273*76404edcSAsim Jamshed }
274*76404edcSAsim Jamshed
275*76404edcSAsim Jamshed if (!auth_satisfied) {
276*76404edcSAsim Jamshed data_string *method, *realm;
277*76404edcSAsim Jamshed method = (data_string *)array_get_element(req, "method");
278*76404edcSAsim Jamshed realm = (data_string *)array_get_element(req, "realm");
279*76404edcSAsim Jamshed
280*76404edcSAsim Jamshed con->http_status = 401;
281*76404edcSAsim Jamshed con->mode = DIRECT;
282*76404edcSAsim Jamshed
283*76404edcSAsim Jamshed if (0 == strcmp(method->value->ptr, "basic")) {
284*76404edcSAsim Jamshed buffer_copy_string_len(p->tmp_buf, CONST_STR_LEN("Basic realm=\""));
285*76404edcSAsim Jamshed buffer_append_string_buffer(p->tmp_buf, realm->value);
286*76404edcSAsim Jamshed buffer_append_string_len(p->tmp_buf, CONST_STR_LEN("\""));
287*76404edcSAsim Jamshed
288*76404edcSAsim Jamshed response_header_insert(srv, con, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(p->tmp_buf));
289*76404edcSAsim Jamshed } else if (0 == strcmp(method->value->ptr, "digest")) {
290*76404edcSAsim Jamshed char hh[33];
291*76404edcSAsim Jamshed http_auth_digest_generate_nonce(srv, p, srv->tmp_buf, hh);
292*76404edcSAsim Jamshed
293*76404edcSAsim Jamshed buffer_copy_string_len(p->tmp_buf, CONST_STR_LEN("Digest realm=\""));
294*76404edcSAsim Jamshed buffer_append_string_buffer(p->tmp_buf, realm->value);
295*76404edcSAsim Jamshed buffer_append_string_len(p->tmp_buf, CONST_STR_LEN("\", nonce=\""));
296*76404edcSAsim Jamshed buffer_append_string(p->tmp_buf, hh);
297*76404edcSAsim Jamshed buffer_append_string_len(p->tmp_buf, CONST_STR_LEN("\", qop=\"auth\""));
298*76404edcSAsim Jamshed
299*76404edcSAsim Jamshed response_header_insert(srv, con, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(p->tmp_buf));
300*76404edcSAsim Jamshed } else {
301*76404edcSAsim Jamshed /* evil */
302*76404edcSAsim Jamshed }
303*76404edcSAsim Jamshed return HANDLER_FINISHED;
304*76404edcSAsim Jamshed } else {
305*76404edcSAsim Jamshed /* the REMOTE_USER header */
306*76404edcSAsim Jamshed
307*76404edcSAsim Jamshed buffer_copy_string_buffer(con->authed_user, p->auth_user);
308*76404edcSAsim Jamshed
309*76404edcSAsim Jamshed /* AUTH_TYPE environment */
310*76404edcSAsim Jamshed
311*76404edcSAsim Jamshed if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
312*76404edcSAsim Jamshed ds = data_string_init();
313*76404edcSAsim Jamshed }
314*76404edcSAsim Jamshed
315*76404edcSAsim Jamshed buffer_copy_string(ds->key, "AUTH_TYPE");
316*76404edcSAsim Jamshed buffer_copy_string(ds->value, auth_type);
317*76404edcSAsim Jamshed
318*76404edcSAsim Jamshed array_insert_unique(con->environment, (data_unset *)ds);
319*76404edcSAsim Jamshed }
320*76404edcSAsim Jamshed
321*76404edcSAsim Jamshed return HANDLER_GO_ON;
322*76404edcSAsim Jamshed }
323*76404edcSAsim Jamshed
SETDEFAULTS_FUNC(mod_auth_set_defaults)324*76404edcSAsim Jamshed SETDEFAULTS_FUNC(mod_auth_set_defaults) {
325*76404edcSAsim Jamshed mod_auth_plugin_data *p = p_d;
326*76404edcSAsim Jamshed size_t i;
327*76404edcSAsim Jamshed
328*76404edcSAsim Jamshed config_values_t cv[] = {
329*76404edcSAsim Jamshed { "auth.backend", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
330*76404edcSAsim Jamshed { "auth.backend.plain.groupfile", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
331*76404edcSAsim Jamshed { "auth.backend.plain.userfile", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
332*76404edcSAsim Jamshed { "auth.require", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
333*76404edcSAsim Jamshed { "auth.backend.ldap.hostname", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 4 */
334*76404edcSAsim Jamshed { "auth.backend.ldap.base-dn", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 5 */
335*76404edcSAsim Jamshed { "auth.backend.ldap.filter", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 6 */
336*76404edcSAsim Jamshed { "auth.backend.ldap.ca-file", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 7 */
337*76404edcSAsim Jamshed { "auth.backend.ldap.starttls", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 8 */
338*76404edcSAsim Jamshed { "auth.backend.ldap.bind-dn", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 9 */
339*76404edcSAsim Jamshed { "auth.backend.ldap.bind-pw", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 10 */
340*76404edcSAsim Jamshed { "auth.backend.ldap.allow-empty-pw", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 11 */
341*76404edcSAsim Jamshed { "auth.backend.htdigest.userfile", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 12 */
342*76404edcSAsim Jamshed { "auth.backend.htpasswd.userfile", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 13 */
343*76404edcSAsim Jamshed { "auth.debug", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 14 */
344*76404edcSAsim Jamshed { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
345*76404edcSAsim Jamshed };
346*76404edcSAsim Jamshed
347*76404edcSAsim Jamshed p->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *));
348*76404edcSAsim Jamshed
349*76404edcSAsim Jamshed for (i = 0; i < srv->config_context->used; i++) {
350*76404edcSAsim Jamshed mod_auth_plugin_config *s;
351*76404edcSAsim Jamshed size_t n;
352*76404edcSAsim Jamshed data_array *da;
353*76404edcSAsim Jamshed array *ca;
354*76404edcSAsim Jamshed
355*76404edcSAsim Jamshed s = calloc(1, sizeof(mod_auth_plugin_config));
356*76404edcSAsim Jamshed s->auth_plain_groupfile = buffer_init();
357*76404edcSAsim Jamshed s->auth_plain_userfile = buffer_init();
358*76404edcSAsim Jamshed s->auth_htdigest_userfile = buffer_init();
359*76404edcSAsim Jamshed s->auth_htpasswd_userfile = buffer_init();
360*76404edcSAsim Jamshed s->auth_backend_conf = buffer_init();
361*76404edcSAsim Jamshed
362*76404edcSAsim Jamshed s->auth_ldap_hostname = buffer_init();
363*76404edcSAsim Jamshed s->auth_ldap_basedn = buffer_init();
364*76404edcSAsim Jamshed s->auth_ldap_binddn = buffer_init();
365*76404edcSAsim Jamshed s->auth_ldap_bindpw = buffer_init();
366*76404edcSAsim Jamshed s->auth_ldap_filter = buffer_init();
367*76404edcSAsim Jamshed s->auth_ldap_cafile = buffer_init();
368*76404edcSAsim Jamshed s->auth_ldap_starttls = 0;
369*76404edcSAsim Jamshed s->auth_debug = 0;
370*76404edcSAsim Jamshed
371*76404edcSAsim Jamshed s->auth_require = array_init();
372*76404edcSAsim Jamshed
373*76404edcSAsim Jamshed #ifdef USE_LDAP
374*76404edcSAsim Jamshed s->ldap_filter_pre = buffer_init();
375*76404edcSAsim Jamshed s->ldap_filter_post = buffer_init();
376*76404edcSAsim Jamshed s->ldap = NULL;
377*76404edcSAsim Jamshed #endif
378*76404edcSAsim Jamshed
379*76404edcSAsim Jamshed cv[0].destination = s->auth_backend_conf;
380*76404edcSAsim Jamshed cv[1].destination = s->auth_plain_groupfile;
381*76404edcSAsim Jamshed cv[2].destination = s->auth_plain_userfile;
382*76404edcSAsim Jamshed cv[3].destination = s->auth_require;
383*76404edcSAsim Jamshed cv[4].destination = s->auth_ldap_hostname;
384*76404edcSAsim Jamshed cv[5].destination = s->auth_ldap_basedn;
385*76404edcSAsim Jamshed cv[6].destination = s->auth_ldap_filter;
386*76404edcSAsim Jamshed cv[7].destination = s->auth_ldap_cafile;
387*76404edcSAsim Jamshed cv[8].destination = &(s->auth_ldap_starttls);
388*76404edcSAsim Jamshed cv[9].destination = s->auth_ldap_binddn;
389*76404edcSAsim Jamshed cv[10].destination = s->auth_ldap_bindpw;
390*76404edcSAsim Jamshed cv[11].destination = &(s->auth_ldap_allow_empty_pw);
391*76404edcSAsim Jamshed cv[12].destination = s->auth_htdigest_userfile;
392*76404edcSAsim Jamshed cv[13].destination = s->auth_htpasswd_userfile;
393*76404edcSAsim Jamshed cv[14].destination = &(s->auth_debug);
394*76404edcSAsim Jamshed
395*76404edcSAsim Jamshed p->config_storage[i] = s;
396*76404edcSAsim Jamshed ca = ((data_config *)srv->config_context->data[i])->value;
397*76404edcSAsim Jamshed
398*76404edcSAsim Jamshed if (0 != config_insert_values_global(srv, ca, cv)) {
399*76404edcSAsim Jamshed return HANDLER_ERROR;
400*76404edcSAsim Jamshed }
401*76404edcSAsim Jamshed
402*76404edcSAsim Jamshed if (s->auth_backend_conf->used) {
403*76404edcSAsim Jamshed if (0 == strcmp(s->auth_backend_conf->ptr, "htpasswd")) {
404*76404edcSAsim Jamshed s->auth_backend = AUTH_BACKEND_HTPASSWD;
405*76404edcSAsim Jamshed } else if (0 == strcmp(s->auth_backend_conf->ptr, "htdigest")) {
406*76404edcSAsim Jamshed s->auth_backend = AUTH_BACKEND_HTDIGEST;
407*76404edcSAsim Jamshed } else if (0 == strcmp(s->auth_backend_conf->ptr, "plain")) {
408*76404edcSAsim Jamshed s->auth_backend = AUTH_BACKEND_PLAIN;
409*76404edcSAsim Jamshed } else if (0 == strcmp(s->auth_backend_conf->ptr, "ldap")) {
410*76404edcSAsim Jamshed s->auth_backend = AUTH_BACKEND_LDAP;
411*76404edcSAsim Jamshed } else {
412*76404edcSAsim Jamshed log_error_write(srv, __FILE__, __LINE__, "sb", "auth.backend not supported:", s->auth_backend_conf);
413*76404edcSAsim Jamshed
414*76404edcSAsim Jamshed return HANDLER_ERROR;
415*76404edcSAsim Jamshed }
416*76404edcSAsim Jamshed }
417*76404edcSAsim Jamshed
418*76404edcSAsim Jamshed #ifdef USE_LDAP
419*76404edcSAsim Jamshed if (s->auth_ldap_filter->used) {
420*76404edcSAsim Jamshed char *dollar;
421*76404edcSAsim Jamshed
422*76404edcSAsim Jamshed /* parse filter */
423*76404edcSAsim Jamshed
424*76404edcSAsim Jamshed if (NULL == (dollar = strchr(s->auth_ldap_filter->ptr, '$'))) {
425*76404edcSAsim Jamshed log_error_write(srv, __FILE__, __LINE__, "s", "ldap: auth.backend.ldap.filter is missing a replace-operator '$'");
426*76404edcSAsim Jamshed
427*76404edcSAsim Jamshed return HANDLER_ERROR;
428*76404edcSAsim Jamshed }
429*76404edcSAsim Jamshed
430*76404edcSAsim Jamshed buffer_copy_string_len(s->ldap_filter_pre, s->auth_ldap_filter->ptr, dollar - s->auth_ldap_filter->ptr);
431*76404edcSAsim Jamshed buffer_copy_string(s->ldap_filter_post, dollar+1);
432*76404edcSAsim Jamshed }
433*76404edcSAsim Jamshed #endif
434*76404edcSAsim Jamshed
435*76404edcSAsim Jamshed /* no auth.require for this section */
436*76404edcSAsim Jamshed if (NULL == (da = (data_array *)array_get_element(ca, "auth.require"))) continue;
437*76404edcSAsim Jamshed
438*76404edcSAsim Jamshed if (da->type != TYPE_ARRAY) continue;
439*76404edcSAsim Jamshed
440*76404edcSAsim Jamshed for (n = 0; n < da->value->used; n++) {
441*76404edcSAsim Jamshed size_t m;
442*76404edcSAsim Jamshed data_array *da_file = (data_array *)da->value->data[n];
443*76404edcSAsim Jamshed const char *method, *realm, *require;
444*76404edcSAsim Jamshed
445*76404edcSAsim Jamshed if (da->value->data[n]->type != TYPE_ARRAY) {
446*76404edcSAsim Jamshed log_error_write(srv, __FILE__, __LINE__, "ss",
447*76404edcSAsim Jamshed "auth.require should contain an array as in:",
448*76404edcSAsim Jamshed "auth.require = ( \"...\" => ( ..., ...) )");
449*76404edcSAsim Jamshed
450*76404edcSAsim Jamshed return HANDLER_ERROR;
451*76404edcSAsim Jamshed }
452*76404edcSAsim Jamshed
453*76404edcSAsim Jamshed method = realm = require = NULL;
454*76404edcSAsim Jamshed
455*76404edcSAsim Jamshed for (m = 0; m < da_file->value->used; m++) {
456*76404edcSAsim Jamshed if (da_file->value->data[m]->type == TYPE_STRING) {
457*76404edcSAsim Jamshed if (0 == strcmp(da_file->value->data[m]->key->ptr, "method")) {
458*76404edcSAsim Jamshed method = ((data_string *)(da_file->value->data[m]))->value->ptr;
459*76404edcSAsim Jamshed } else if (0 == strcmp(da_file->value->data[m]->key->ptr, "realm")) {
460*76404edcSAsim Jamshed realm = ((data_string *)(da_file->value->data[m]))->value->ptr;
461*76404edcSAsim Jamshed } else if (0 == strcmp(da_file->value->data[m]->key->ptr, "require")) {
462*76404edcSAsim Jamshed require = ((data_string *)(da_file->value->data[m]))->value->ptr;
463*76404edcSAsim Jamshed } else {
464*76404edcSAsim Jamshed log_error_write(srv, __FILE__, __LINE__, "ssbs",
465*76404edcSAsim Jamshed "the field is unknown in:",
466*76404edcSAsim Jamshed "auth.require = ( \"...\" => ( ..., -> \"",
467*76404edcSAsim Jamshed da_file->value->data[m]->key,
468*76404edcSAsim Jamshed "\" <- => \"...\" ) )");
469*76404edcSAsim Jamshed
470*76404edcSAsim Jamshed return HANDLER_ERROR;
471*76404edcSAsim Jamshed }
472*76404edcSAsim Jamshed } else {
473*76404edcSAsim Jamshed log_error_write(srv, __FILE__, __LINE__, "ssbs",
474*76404edcSAsim Jamshed "a string was expected for:",
475*76404edcSAsim Jamshed "auth.require = ( \"...\" => ( ..., -> \"",
476*76404edcSAsim Jamshed da_file->value->data[m]->key,
477*76404edcSAsim Jamshed "\" <- => \"...\" ) )");
478*76404edcSAsim Jamshed
479*76404edcSAsim Jamshed return HANDLER_ERROR;
480*76404edcSAsim Jamshed }
481*76404edcSAsim Jamshed }
482*76404edcSAsim Jamshed
483*76404edcSAsim Jamshed if (method == NULL) {
484*76404edcSAsim Jamshed log_error_write(srv, __FILE__, __LINE__, "ss",
485*76404edcSAsim Jamshed "the method field is missing in:",
486*76404edcSAsim Jamshed "auth.require = ( \"...\" => ( ..., \"method\" => \"...\" ) )");
487*76404edcSAsim Jamshed return HANDLER_ERROR;
488*76404edcSAsim Jamshed } else {
489*76404edcSAsim Jamshed if (0 != strcmp(method, "basic") &&
490*76404edcSAsim Jamshed 0 != strcmp(method, "digest")) {
491*76404edcSAsim Jamshed log_error_write(srv, __FILE__, __LINE__, "ss",
492*76404edcSAsim Jamshed "method has to be either \"basic\" or \"digest\" in",
493*76404edcSAsim Jamshed "auth.require = ( \"...\" => ( ..., \"method\" => \"...\") )");
494*76404edcSAsim Jamshed return HANDLER_ERROR;
495*76404edcSAsim Jamshed }
496*76404edcSAsim Jamshed }
497*76404edcSAsim Jamshed
498*76404edcSAsim Jamshed if (realm == NULL) {
499*76404edcSAsim Jamshed log_error_write(srv, __FILE__, __LINE__, "ss",
500*76404edcSAsim Jamshed "the realm field is missing in:",
501*76404edcSAsim Jamshed "auth.require = ( \"...\" => ( ..., \"realm\" => \"...\" ) )");
502*76404edcSAsim Jamshed return HANDLER_ERROR;
503*76404edcSAsim Jamshed }
504*76404edcSAsim Jamshed
505*76404edcSAsim Jamshed if (require == NULL) {
506*76404edcSAsim Jamshed log_error_write(srv, __FILE__, __LINE__, "ss",
507*76404edcSAsim Jamshed "the require field is missing in:",
508*76404edcSAsim Jamshed "auth.require = ( \"...\" => ( ..., \"require\" => \"...\" ) )");
509*76404edcSAsim Jamshed return HANDLER_ERROR;
510*76404edcSAsim Jamshed }
511*76404edcSAsim Jamshed
512*76404edcSAsim Jamshed if (method && realm && require) {
513*76404edcSAsim Jamshed data_string *ds;
514*76404edcSAsim Jamshed data_array *a;
515*76404edcSAsim Jamshed
516*76404edcSAsim Jamshed a = data_array_init();
517*76404edcSAsim Jamshed buffer_copy_string_buffer(a->key, da_file->key);
518*76404edcSAsim Jamshed
519*76404edcSAsim Jamshed ds = data_string_init();
520*76404edcSAsim Jamshed
521*76404edcSAsim Jamshed buffer_copy_string_len(ds->key, CONST_STR_LEN("method"));
522*76404edcSAsim Jamshed buffer_copy_string(ds->value, method);
523*76404edcSAsim Jamshed
524*76404edcSAsim Jamshed array_insert_unique(a->value, (data_unset *)ds);
525*76404edcSAsim Jamshed
526*76404edcSAsim Jamshed ds = data_string_init();
527*76404edcSAsim Jamshed
528*76404edcSAsim Jamshed buffer_copy_string_len(ds->key, CONST_STR_LEN("realm"));
529*76404edcSAsim Jamshed buffer_copy_string(ds->value, realm);
530*76404edcSAsim Jamshed
531*76404edcSAsim Jamshed array_insert_unique(a->value, (data_unset *)ds);
532*76404edcSAsim Jamshed
533*76404edcSAsim Jamshed ds = data_string_init();
534*76404edcSAsim Jamshed
535*76404edcSAsim Jamshed buffer_copy_string_len(ds->key, CONST_STR_LEN("require"));
536*76404edcSAsim Jamshed buffer_copy_string(ds->value, require);
537*76404edcSAsim Jamshed
538*76404edcSAsim Jamshed array_insert_unique(a->value, (data_unset *)ds);
539*76404edcSAsim Jamshed
540*76404edcSAsim Jamshed array_insert_unique(s->auth_require, (data_unset *)a);
541*76404edcSAsim Jamshed }
542*76404edcSAsim Jamshed }
543*76404edcSAsim Jamshed
544*76404edcSAsim Jamshed switch(s->auth_ldap_hostname->used) {
545*76404edcSAsim Jamshed case AUTH_BACKEND_LDAP: {
546*76404edcSAsim Jamshed handler_t ret = auth_ldap_init(srv, s);
547*76404edcSAsim Jamshed if (ret == HANDLER_ERROR)
548*76404edcSAsim Jamshed return (ret);
549*76404edcSAsim Jamshed break;
550*76404edcSAsim Jamshed }
551*76404edcSAsim Jamshed default:
552*76404edcSAsim Jamshed break;
553*76404edcSAsim Jamshed }
554*76404edcSAsim Jamshed }
555*76404edcSAsim Jamshed
556*76404edcSAsim Jamshed return HANDLER_GO_ON;
557*76404edcSAsim Jamshed }
558*76404edcSAsim Jamshed
auth_ldap_init(server * srv,mod_auth_plugin_config * s)559*76404edcSAsim Jamshed handler_t auth_ldap_init(server *srv, mod_auth_plugin_config *s) {
560*76404edcSAsim Jamshed #ifdef USE_LDAP
561*76404edcSAsim Jamshed int ret;
562*76404edcSAsim Jamshed #if 0
563*76404edcSAsim Jamshed if (s->auth_ldap_basedn->used == 0) {
564*76404edcSAsim Jamshed log_error_write(srv, __FILE__, __LINE__, "s", "ldap: auth.backend.ldap.base-dn has to be set");
565*76404edcSAsim Jamshed
566*76404edcSAsim Jamshed return HANDLER_ERROR;
567*76404edcSAsim Jamshed }
568*76404edcSAsim Jamshed #endif
569*76404edcSAsim Jamshed
570*76404edcSAsim Jamshed if (s->auth_ldap_hostname->used) {
571*76404edcSAsim Jamshed /* free old context */
572*76404edcSAsim Jamshed if (NULL != s->ldap) ldap_unbind_s(s->ldap);
573*76404edcSAsim Jamshed
574*76404edcSAsim Jamshed if (NULL == (s->ldap = ldap_init(s->auth_ldap_hostname->ptr, LDAP_PORT))) {
575*76404edcSAsim Jamshed log_error_write(srv, __FILE__, __LINE__, "ss", "ldap ...", strerror(errno));
576*76404edcSAsim Jamshed
577*76404edcSAsim Jamshed return HANDLER_ERROR;
578*76404edcSAsim Jamshed }
579*76404edcSAsim Jamshed
580*76404edcSAsim Jamshed ret = LDAP_VERSION3;
581*76404edcSAsim Jamshed if (LDAP_OPT_SUCCESS != (ret = ldap_set_option(s->ldap, LDAP_OPT_PROTOCOL_VERSION, &ret))) {
582*76404edcSAsim Jamshed log_error_write(srv, __FILE__, __LINE__, "ss", "ldap:", ldap_err2string(ret));
583*76404edcSAsim Jamshed
584*76404edcSAsim Jamshed return HANDLER_ERROR;
585*76404edcSAsim Jamshed }
586*76404edcSAsim Jamshed
587*76404edcSAsim Jamshed if (s->auth_ldap_starttls) {
588*76404edcSAsim Jamshed /* if no CA file is given, it is ok, as we will use encryption
589*76404edcSAsim Jamshed * if the server requires a CAfile it will tell us */
590*76404edcSAsim Jamshed if (!buffer_is_empty(s->auth_ldap_cafile)) {
591*76404edcSAsim Jamshed if (LDAP_OPT_SUCCESS != (ret = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE,
592*76404edcSAsim Jamshed s->auth_ldap_cafile->ptr))) {
593*76404edcSAsim Jamshed log_error_write(srv, __FILE__, __LINE__, "ss",
594*76404edcSAsim Jamshed "Loading CA certificate failed:", ldap_err2string(ret));
595*76404edcSAsim Jamshed
596*76404edcSAsim Jamshed return HANDLER_ERROR;
597*76404edcSAsim Jamshed }
598*76404edcSAsim Jamshed }
599*76404edcSAsim Jamshed
600*76404edcSAsim Jamshed if (LDAP_OPT_SUCCESS != (ret = ldap_start_tls_s(s->ldap, NULL, NULL))) {
601*76404edcSAsim Jamshed log_error_write(srv, __FILE__, __LINE__, "ss", "ldap startTLS failed:", ldap_err2string(ret));
602*76404edcSAsim Jamshed
603*76404edcSAsim Jamshed return HANDLER_ERROR;
604*76404edcSAsim Jamshed }
605*76404edcSAsim Jamshed }
606*76404edcSAsim Jamshed
607*76404edcSAsim Jamshed
608*76404edcSAsim Jamshed /* 1. */
609*76404edcSAsim Jamshed if (s->auth_ldap_binddn->used) {
610*76404edcSAsim Jamshed if (LDAP_SUCCESS != (ret = ldap_simple_bind_s(s->ldap, s->auth_ldap_binddn->ptr, s->auth_ldap_bindpw->ptr))) {
611*76404edcSAsim Jamshed log_error_write(srv, __FILE__, __LINE__, "ss", "ldap:", ldap_err2string(ret));
612*76404edcSAsim Jamshed
613*76404edcSAsim Jamshed return HANDLER_ERROR;
614*76404edcSAsim Jamshed }
615*76404edcSAsim Jamshed } else {
616*76404edcSAsim Jamshed if (LDAP_SUCCESS != (ret = ldap_simple_bind_s(s->ldap, NULL, NULL))) {
617*76404edcSAsim Jamshed log_error_write(srv, __FILE__, __LINE__, "ss", "ldap:", ldap_err2string(ret));
618*76404edcSAsim Jamshed
619*76404edcSAsim Jamshed return HANDLER_ERROR;
620*76404edcSAsim Jamshed }
621*76404edcSAsim Jamshed }
622*76404edcSAsim Jamshed }
623*76404edcSAsim Jamshed return HANDLER_GO_ON;
624*76404edcSAsim Jamshed #else
625*76404edcSAsim Jamshed UNUSED(s);
626*76404edcSAsim Jamshed log_error_write(srv, __FILE__, __LINE__, "s", "no ldap support available");
627*76404edcSAsim Jamshed return HANDLER_ERROR;
628*76404edcSAsim Jamshed #endif
629*76404edcSAsim Jamshed }
630*76404edcSAsim Jamshed
631*76404edcSAsim Jamshed int mod_auth_plugin_init(plugin *p);
mod_auth_plugin_init(plugin * p)632*76404edcSAsim Jamshed int mod_auth_plugin_init(plugin *p) {
633*76404edcSAsim Jamshed p->version = LIGHTTPD_VERSION_ID;
634*76404edcSAsim Jamshed p->name = buffer_init_string("auth");
635*76404edcSAsim Jamshed p->init = mod_auth_init;
636*76404edcSAsim Jamshed p->set_defaults = mod_auth_set_defaults;
637*76404edcSAsim Jamshed p->handle_uri_clean = mod_auth_uri_handler;
638*76404edcSAsim Jamshed p->cleanup = mod_auth_free;
639*76404edcSAsim Jamshed
640*76404edcSAsim Jamshed p->data = NULL;
641*76404edcSAsim Jamshed
642*76404edcSAsim Jamshed return 0;
643*76404edcSAsim Jamshed }
644