]> git.ipfire.org Git - thirdparty/squid.git/blob - helpers/digest_auth/LDAP/ldap_backend.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[thirdparty/squid.git] / helpers / digest_auth / LDAP / ldap_backend.cc
1 /*
2 *
3 *
4 *
5 * ldap_backend.c
6 * AUTHOR: Flavio Pescuma, MARA Systems AB <flavio@marasystems.com>
7 */
8 #include "squid.h"
9 #include "util.h"
10
11 #define LDAP_DEPRECATED 1
12
13 #include "ldap_backend.h"
14
15 #if _SQUID_MSWIN_ /* Native Windows port and MinGW */
16
17 #define snprintf _snprintf
18 #include <windows.h>
19 #include <winldap.h>
20 #ifndef LDAPAPI
21 #define LDAPAPI __cdecl
22 #endif
23 #ifdef LDAP_VERSION3
24 #ifndef LDAP_OPT_X_TLS
25 #define LDAP_OPT_X_TLS 0x6000
26 #endif
27 /* Some tricks to allow dynamic bind with ldap_start_tls_s entry point at
28 * run time.
29 */
30 #undef ldap_start_tls_s
31 #if LDAP_UNICODE
32 #define LDAP_START_TLS_S "ldap_start_tls_sW"
33 typedef WINLDAPAPI ULONG(LDAPAPI * PFldap_start_tls_s) (IN PLDAP, OUT PULONG, OUT LDAPMessage **, IN PLDAPControlW *, IN PLDAPControlW *);
34 #else
35 #define LDAP_START_TLS_S "ldap_start_tls_sA"
36 typedef WINLDAPAPI ULONG(LDAPAPI * PFldap_start_tls_s) (IN PLDAP, OUT PULONG, OUT LDAPMessage **, IN PLDAPControlA *, IN PLDAPControlA *);
37 #endif /* LDAP_UNICODE */
38 PFldap_start_tls_s Win32_ldap_start_tls_s;
39 #define ldap_start_tls_s(l,s,c) Win32_ldap_start_tls_s(l,NULL,NULL,s,c)
40 #endif /* LDAP_VERSION3 */
41
42 #else
43
44 #include <lber.h>
45 #include <ldap.h>
46
47 #endif
48 #define PROGRAM_NAME "digest_pw_auth(LDAP_backend)"
49
50 /* Globals */
51
52 static LDAP *ld = NULL;
53 static const char *passattr = NULL;
54 static char *ldapServer = NULL;
55 static const char *userbasedn = NULL;
56 static const char *userdnattr = NULL;
57 static const char *usersearchfilter = NULL;
58 static const char *binddn = NULL;
59 static const char *bindpasswd = NULL;
60 static const char *delimiter = ":";
61 static int encrpass = 0;
62 static int searchscope = LDAP_SCOPE_SUBTREE;
63 static int persistent = 0;
64 static int noreferrals = 0;
65 static int port = LDAP_PORT;
66 static int strip_nt_domain = 0;
67 static int aliasderef = LDAP_DEREF_NEVER;
68 #if defined(NETSCAPE_SSL)
69 static char *sslpath = NULL;
70 static int sslinit = 0;
71 #endif
72 static int connect_timeout = 0;
73 static int timelimit = LDAP_NO_LIMIT;
74
75 #ifdef LDAP_VERSION3
76 /* Added for TLS support and version 3 */
77 static int use_tls = 0;
78 static int version = -1;
79 #endif
80
81 static void ldapconnect(void);
82 static int readSecret(const char *filename);
83
84 /* Yuck.. we need to glue to different versions of the API */
85
86 #if defined(LDAP_API_VERSION) && LDAP_API_VERSION > 1823
87 static void
88 squid_ldap_set_aliasderef(int deref)
89 {
90 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
91 }
92 static void
93 squid_ldap_set_referrals(int referrals)
94 {
95 int *value = static_cast<int*>(referrals ? LDAP_OPT_ON :LDAP_OPT_OFF);
96 ldap_set_option(ld, LDAP_OPT_REFERRALS, value);
97 }
98 static void
99 squid_ldap_set_timelimit(int aTimeLimit)
100 {
101 ldap_set_option(ld, LDAP_OPT_TIMELIMIT, &aTimeLimit);
102 }
103 static void
104 squid_ldap_set_connect_timeout(int aTimeLimit)
105 {
106 #if defined(LDAP_OPT_NETWORK_TIMEOUT)
107 struct timeval tv;
108 tv.tv_sec = aTimeLimit;
109 tv.tv_usec = 0;
110 ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT, &tv);
111 #elif defined(LDAP_X_OPT_CONNECT_TIMEOUT)
112 aTimeLimit *= 1000;
113 ldap_set_option(ld, LDAP_X_OPT_CONNECT_TIMEOUT, &aTimeLimit);
114 #endif
115 }
116
117 #else
118 static int
119 squid_ldap_errno(LDAP * ld)
120 {
121 return ld->ld_errno;
122 }
123 static void
124 squid_ldap_set_aliasderef(int deref)
125 {
126 ld->ld_deref = deref;
127 }
128 static void
129 squid_ldap_set_referrals(int referrals)
130 {
131 if (referrals)
132 ld->ld_options |= ~LDAP_OPT_REFERRALS;
133 else
134 ld->ld_options &= ~LDAP_OPT_REFERRALS;
135 }
136 static void
137 squid_ldap_set_timelimit(int aTimeLimit)
138 {
139 ld->ld_timelimit = aTimeLimit;
140 }
141 static void
142 squid_ldap_set_connect_timeout(int aTimeLimit)
143 {
144 fprintf(stderr, "Connect timeouts not supported in your LDAP library\n");
145 }
146 static void
147 squid_ldap_memfree(char *p)
148 {
149 free(p);
150 }
151
152 #endif
153
154 #ifdef LDAP_API_FEATURE_X_OPENLDAP
155 #if LDAP_VENDOR_VERSION > 194
156 #define HAS_URI_SUPPORT 1
157 #endif
158 #endif
159
160 static int
161 ldap_escape_value(char *escaped, int size, const char *src)
162 {
163 int n = 0;
164 while (size > 4 && *src) {
165 switch (*src) {
166 case '*':
167 case '(':
168 case ')':
169 case '\\':
170 n += 3;
171 size -= 3;
172 if (size > 0) {
173 *escaped++ = '\\';
174 snprintf(escaped, 3, "%02x", (int) *src++);
175 escaped += 2;
176 }
177 break;
178 default:
179 *escaped++ = *src++;
180 n++;
181 size--;
182 }
183 }
184 *escaped = '\0';
185 return n;
186 }
187
188 static char *
189 getpassword(char *login, char *realm)
190 {
191 LDAPMessage *res = NULL;
192 LDAPMessage *entry;
193 char **values = NULL;
194 char **value = NULL;
195 char *password = NULL;
196 int retry = 0;
197 char filter[8192];
198 char searchbase[8192];
199 int rc = -1;
200 if (ld) {
201 if (usersearchfilter) {
202 char escaped_login[1024];
203 snprintf(searchbase, sizeof(searchbase), "%s", userbasedn);
204 ldap_escape_value(escaped_login, sizeof(escaped_login), login);
205 snprintf(filter, sizeof(filter), usersearchfilter, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login, escaped_login);
206
207 retrysrch:
208 debug("user filter '%s', searchbase '%s'\n", filter, searchbase);
209
210 rc = ldap_search_s(ld, searchbase, searchscope, filter, NULL, 0, &res);
211 if (rc != LDAP_SUCCESS) {
212 if (noreferrals && rc == LDAP_PARTIAL_RESULTS) {
213 /* Everything is fine. This is expected when referrals
214 * are disabled.
215 */
216 rc = LDAP_SUCCESS;
217 } else {
218 fprintf(stderr, PROGRAM_NAME " WARNING, LDAP search error '%s'\n", ldap_err2string(rc));
219 #if defined(NETSCAPE_SSL)
220 if (sslpath && ((rc == LDAP_SERVER_DOWN) || (rc == LDAP_CONNECT_ERROR))) {
221 int sslerr = PORT_GetError();
222 fprintf(stderr, PROGRAM_NAME ": WARNING, SSL error %d (%s)\n", sslerr, ldapssl_err2string(sslerr));
223 }
224 #endif
225 fprintf(stderr, PROGRAM_NAME " WARNING, LDAP search error, trying to recover'%s'\n", ldap_err2string(rc));
226 ldap_msgfree(res);
227 /* try to connect to the LDAP server agin, maybe my persisten conexion failed. */
228 if (!retry) {
229 retry++;
230 ldap_unbind(ld);
231 ld = NULL;
232 ldapconnect();
233 goto retrysrch;
234 }
235 return NULL;
236
237 }
238 }
239 } else if (userdnattr) {
240 snprintf(filter,8192,"%s=%s",userdnattr,login);
241
242 retrydnattr:
243 debug("searchbase '%s'\n", userbasedn);
244 rc = ldap_search_s(ld, userbasedn, searchscope, filter, NULL, 0, &res);
245 }
246 if (rc == LDAP_SUCCESS) {
247 entry = ldap_first_entry(ld, res);
248 if (entry)
249 values = ldap_get_values(ld, entry, passattr);
250 else {
251 ldap_msgfree(res);
252 return NULL;
253 }
254 if (!values) {
255 debug("No attribute value found\n");
256 ldap_msgfree(res);
257 return NULL;
258 }
259 value = values;
260 while (*value) {
261 if (encrpass) {
262 if (strcmp(strtok(*value, delimiter), realm) == 0) {
263 password = strtok(NULL, delimiter);
264 break;
265 }
266 } else {
267 password = *value;
268 break;
269 }
270 value++;
271 }
272 debug("password: %s\n", password);
273 if (password)
274 password = xstrdup(password);
275 ldap_value_free(values);
276 ldap_msgfree(res);
277 return password;
278 } else {
279 fprintf(stderr, PROGRAM_NAME " WARNING, LDAP error '%s'\n", ldap_err2string(rc));
280 /* try to connect to the LDAP server agin, maybe my persisten conexion failed. */
281 if (!retry) {
282 retry++;
283 ldap_unbind(ld);
284 ld = NULL;
285 ldapconnect();
286 goto retrydnattr;
287 }
288 return NULL;
289 }
290 }
291 return NULL;
292 }
293
294
295
296 static void
297 ldapconnect(void)
298 {
299 int rc;
300
301 /* On Windows ldap_start_tls_s is available starting from Windows XP,
302 * so we need to bind at run-time with the function entry point
303 */
304 #if _SQUID_MSWIN_
305 if (use_tls) {
306
307 HMODULE WLDAP32Handle;
308
309 WLDAP32Handle = GetModuleHandle("wldap32");
310 if ((Win32_ldap_start_tls_s = (PFldap_start_tls_s) GetProcAddress(WLDAP32Handle, LDAP_START_TLS_S)) == NULL) {
311 fprintf(stderr, PROGRAM_NAME ": ERROR: TLS (-Z) not supported on this platform.\n");
312 exit(1);
313 }
314 }
315 #endif
316
317 if (ld == NULL) {
318 #if HAS_URI_SUPPORT
319 if (strstr(ldapServer, "://") != NULL) {
320 rc = ldap_initialize(&ld, ldapServer);
321 if (rc != LDAP_SUCCESS) {
322 fprintf(stderr, "\nUnable to connect to LDAPURI:%s\n", ldapServer);
323 }
324 } else
325 #endif
326 #if NETSCAPE_SSL
327 if (sslpath) {
328 if (!sslinit && (ldapssl_client_init(sslpath, NULL) != LDAP_SUCCESS)) {
329 fprintf(stderr, "\nUnable to initialise SSL with cert path %s\n",
330 sslpath);
331 exit(1);
332 } else {
333 sslinit++;
334 }
335 if ((ld = ldapssl_init(ldapServer, port, 1)) == NULL) {
336 fprintf(stderr, "\nUnable to connect to SSL LDAP server: %s port:%d\n",
337 ldapServer, port);
338 exit(1);
339 }
340 } else
341 #endif
342 if ((ld = ldap_init(ldapServer, port)) == NULL) {
343 fprintf(stderr, "\nUnable to connect to LDAP server:%s port:%d\n", ldapServer, port);
344 }
345 if (connect_timeout)
346 squid_ldap_set_connect_timeout(connect_timeout);
347
348 #ifdef LDAP_VERSION3
349 if (version == -1) {
350 version = LDAP_VERSION2;
351 }
352 if (ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version)
353 != LDAP_SUCCESS) {
354 fprintf(stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
355 version);
356 ldap_unbind(ld);
357 ld = NULL;
358 }
359 if (use_tls) {
360 #ifdef LDAP_OPT_X_TLS
361 if (version != LDAP_VERSION3) {
362 fprintf(stderr, "TLS requires LDAP version 3\n");
363 exit(1);
364 } else if (ldap_start_tls_s(ld, NULL, NULL) != LDAP_SUCCESS) {
365 fprintf(stderr, "Could not Activate TLS connection\n");
366 exit(1);
367 }
368 #else
369 fprintf(stderr, "TLS not supported with your LDAP library\n");
370 ldap_unbind(ld);
371 ld = NULL;
372 #endif
373 }
374 #endif
375 squid_ldap_set_timelimit(timelimit);
376 squid_ldap_set_referrals(!noreferrals);
377 squid_ldap_set_aliasderef(aliasderef);
378 if (binddn && bindpasswd && *binddn && *bindpasswd) {
379 rc = ldap_simple_bind_s(ld, binddn, bindpasswd);
380 if (rc != LDAP_SUCCESS) {
381 fprintf(stderr, PROGRAM_NAME " WARNING, could not bind to binddn '%s'\n", ldap_err2string(rc));
382 ldap_unbind(ld);
383 ld = NULL;
384 }
385 }
386 debug("Connected OK\n");
387 }
388 }
389 int
390 LDAPArguments(int argc, char **argv)
391 {
392 setbuf(stdout, NULL);
393
394 while (argc > 1 && argv[1][0] == '-') {
395 const char *value = "";
396 char option = argv[1][1];
397 switch (option) {
398 case 'P':
399 case 'R':
400 case 'z':
401 case 'Z':
402 case 'g':
403 case 'e':
404 case 'S':
405 break;
406 default:
407 if (strlen(argv[1]) > 2) {
408 value = argv[1] + 2;
409 } else if (argc > 2) {
410 value = argv[2];
411 argv++;
412 argc--;
413 } else
414 value = "";
415 break;
416 }
417 argv++;
418 argc--;
419 switch (option) {
420 case 'H':
421 #if !HAS_URI_SUPPORT
422 fprintf(stderr, "ERROR: Your LDAP library does not have URI support\n");
423 return 1;
424 #endif
425 /* Fall thru to -h */
426 case 'h':
427 if (ldapServer) {
428 int len = strlen(ldapServer) + 1 + strlen(value) + 1;
429 char *newhost = (char*)malloc(len);
430 snprintf(newhost, len, "%s %s", ldapServer, value);
431 free(ldapServer);
432 ldapServer = newhost;
433 } else {
434 ldapServer = xstrdup(value);
435 }
436 break;
437 case 'A':
438 passattr = value;
439 break;
440 case 'e':
441 encrpass = 1;
442 break;
443 case 'l':
444 delimiter = value;
445 break;
446 case 'b':
447 userbasedn = value;
448 break;
449 case 'F':
450 usersearchfilter = value;
451 break;
452 case 'u':
453 userdnattr = value;
454 break;
455 case 's':
456 if (strcmp(value, "base") == 0)
457 searchscope = LDAP_SCOPE_BASE;
458 else if (strcmp(value, "one") == 0)
459 searchscope = LDAP_SCOPE_ONELEVEL;
460 else if (strcmp(value, "sub") == 0)
461 searchscope = LDAP_SCOPE_SUBTREE;
462 else {
463 fprintf(stderr, PROGRAM_NAME " ERROR: Unknown search scope '%s'\n", value);
464 return 1;
465 }
466 break;
467 case 'S':
468 #if defined(NETSCAPE_SSL)
469 sslpath = value;
470 if (port == LDAP_PORT)
471 port = LDAPS_PORT;
472 #else
473 fprintf(stderr, PROGRAM_NAME " ERROR: -E unsupported with this LDAP library\n");
474 return 1;
475 #endif
476 break;
477 case 'c':
478 connect_timeout = atoi(value);
479 break;
480 case 't':
481 timelimit = atoi(value);
482 break;
483 case 'a':
484 if (strcmp(value, "never") == 0)
485 aliasderef = LDAP_DEREF_NEVER;
486 else if (strcmp(value, "always") == 0)
487 aliasderef = LDAP_DEREF_ALWAYS;
488 else if (strcmp(value, "search") == 0)
489 aliasderef = LDAP_DEREF_SEARCHING;
490 else if (strcmp(value, "find") == 0)
491 aliasderef = LDAP_DEREF_FINDING;
492 else {
493 fprintf(stderr, PROGRAM_NAME " ERROR: Unknown alias dereference method '%s'\n", value);
494 return 1;
495 }
496 break;
497 case 'D':
498 binddn = value;
499 break;
500 case 'w':
501 bindpasswd = value;
502 break;
503 case 'W':
504 readSecret(value);
505 break;
506 case 'P':
507 persistent = !persistent;
508 break;
509 case 'p':
510 port = atoi(value);
511 break;
512 case 'R':
513 noreferrals = !noreferrals;
514 break;
515 #ifdef LDAP_VERSION3
516 case 'v':
517 switch (atoi(value)) {
518 case 2:
519 version = LDAP_VERSION2;
520 break;
521 case 3:
522 version = LDAP_VERSION3;
523 break;
524 default:
525 fprintf(stderr, "Protocol version should be 2 or 3\n");
526 return 1;
527 }
528 break;
529 case 'Z':
530 if (version == LDAP_VERSION2) {
531 fprintf(stderr, "TLS (-Z) is incompatible with version %d\n",
532 version);
533 return 1;
534 }
535 version = LDAP_VERSION3;
536 use_tls = 1;
537 break;
538 #endif
539 case 'd':
540 debug_enabled = 1;
541 break;
542 case 'E':
543 strip_nt_domain = 1;
544 break;
545 default:
546 fprintf(stderr, PROGRAM_NAME " ERROR: Unknown command line option '%c'\n", option);
547 return 1;
548 }
549 }
550
551 while (argc > 1) {
552 char *value = argv[1];
553 if (ldapServer) {
554 int len = strlen(ldapServer) + 1 + strlen(value) + 1;
555 char *newhost = (char*)malloc(len);
556 snprintf(newhost, len, "%s %s", ldapServer, value);
557 free(ldapServer);
558 ldapServer = newhost;
559 } else {
560 ldapServer = xstrdup(value);
561 }
562 argc--;
563 argv++;
564 }
565
566 if (!ldapServer)
567 ldapServer = (char *) "localhost";
568
569 if (!userbasedn || !passattr) {
570 fprintf(stderr, "Usage: " PROGRAM_NAME " -b basedn -f filter [options] ldap_server_name\n\n");
571 fprintf(stderr, "\t-A password attribute(REQUIRED)\t\tUser attribute that contains the password\n");
572 fprintf(stderr, "\t-l password realm delimiter(REQUIRED)\tCharater(s) that devides the password attribute\n\t\t\t\t\t\tin realm and password tokens, default ':' realm:password\n");
573 fprintf(stderr, "\t-b basedn (REQUIRED)\t\t\tbase dn under where to search for users\n");
574 fprintf(stderr, "\t-e Encrypted passwords(REQUIRED)\tPassword are stored encrypted using HHA1\n");
575 fprintf(stderr, "\t-F filter\t\t\t\tuser search filter pattern. %%s = login\n");
576 fprintf(stderr, "\t-u attribute\t\t\t\tattribute to use in combination with the basedn to create the user DN\n");
577 fprintf(stderr, "\t-s base|one|sub\t\t\t\tsearch scope\n");
578 fprintf(stderr, "\t-D binddn\t\t\t\tDN to bind as to perform searches\n");
579 fprintf(stderr, "\t-w bindpasswd\t\t\t\tpassword for binddn\n");
580 fprintf(stderr, "\t-W secretfile\t\t\t\tread password for binddn from file secretfile\n");
581 #if HAS_URI_SUPPORT
582 fprintf(stderr, "\t-H URI\t\t\t\t\tLDAPURI (defaults to ldap://localhost)\n");
583 #endif
584 fprintf(stderr, "\t-h server\t\t\t\tLDAP server (defaults to localhost)\n");
585 fprintf(stderr, "\t-p port\t\t\t\t\tLDAP server port (defaults to %d)\n", LDAP_PORT);
586 fprintf(stderr, "\t-P\t\t\t\t\tpersistent LDAP connection\n");
587 #if defined(NETSCAPE_SSL)
588 fprintf(stderr, "\t-E sslcertpath\t\t\t\tenable LDAP over SSL\n");
589 #endif
590 fprintf(stderr, "\t-c timeout\t\t\t\tconnect timeout\n");
591 fprintf(stderr, "\t-t timelimit\t\t\t\tsearch time limit\n");
592 fprintf(stderr, "\t-R\t\t\t\t\tdo not follow referrals\n");
593 fprintf(stderr, "\t-a never|always|search|find\t\twhen to dereference aliases\n");
594 #ifdef LDAP_VERSION3
595 fprintf(stderr, "\t-v 2|3\t\t\t\t\tLDAP version\n");
596 fprintf(stderr, "\t-Z\t\t\t\t\tTLS encrypt the LDAP connection, requires\n\t\t\t\tLDAP version 3\n");
597 #endif
598 fprintf(stderr, "\t-S\t\t\t\t\tStrip NT domain from usernames\n");
599 fprintf(stderr, "\n");
600 fprintf(stderr, "\tIf you need to bind as a user to perform searches then use the\n\t-D binddn -w bindpasswd or -D binddn -W secretfile options\n\n");
601 return -1;
602 }
603 return 0;
604 }
605 static int
606 readSecret(const char *filename)
607 {
608 char buf[BUFSIZ];
609 char *e = 0;
610 FILE *f;
611
612 if (!(f = fopen(filename, "r"))) {
613 fprintf(stderr, PROGRAM_NAME " ERROR: Can not read secret file %s\n", filename);
614 return 1;
615 }
616 if (!fgets(buf, sizeof(buf) - 1, f)) {
617 fprintf(stderr, PROGRAM_NAME " ERROR: Secret file %s is empty\n", filename);
618 fclose(f);
619 return 1;
620 }
621 /* strip whitespaces on end */
622 if ((e = strrchr(buf, '\n')))
623 *e = 0;
624 if ((e = strrchr(buf, '\r')))
625 *e = 0;
626
627 bindpasswd = xstrdup(buf);
628 if (!bindpasswd) {
629 fprintf(stderr, PROGRAM_NAME " ERROR: can not allocate memory\n");
630 }
631 fclose(f);
632
633 return 0;
634 }
635
636 void
637 LDAPHHA1(RequestData * requestData)
638 {
639 char *password;
640 ldapconnect();
641 password = getpassword(requestData->user, requestData->realm);
642 if (password != NULL) {
643 if (encrpass)
644 xstrncpy(requestData->HHA1, password, sizeof(requestData->HHA1));
645 else {
646 HASH HA1;
647 DigestCalcHA1("md5", requestData->user, requestData->realm, password, NULL, NULL, HA1, requestData->HHA1);
648 }
649 free(password);
650 } else {
651 requestData->error = -1;
652 }
653
654 }