]> git.ipfire.org Git - thirdparty/squid.git/blob - helpers/digest_auth/LDAP/ldap_backend.cc
SourceFormat Enforcement
[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 ++escaped;
175 snprintf(escaped, 3, "%02x", (int) *src);
176 ++src;
177 escaped += 2;
178 }
179 break;
180 default:
181 *escaped = *src;
182 ++escaped;
183 ++src;
184 ++n;
185 --size;
186 }
187 }
188 *escaped = '\0';
189 return n;
190 }
191
192 static char *
193 getpassword(char *login, char *realm)
194 {
195 LDAPMessage *res = NULL;
196 LDAPMessage *entry;
197 char **values = NULL;
198 char **value = NULL;
199 char *password = NULL;
200 int retry = 0;
201 char filter[8192];
202 char searchbase[8192];
203 int rc = -1;
204 if (ld) {
205 if (usersearchfilter) {
206 char escaped_login[1024];
207 snprintf(searchbase, sizeof(searchbase), "%s", userbasedn);
208 ldap_escape_value(escaped_login, sizeof(escaped_login), login);
209 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);
210
211 retrysrch:
212 debug("user filter '%s', searchbase '%s'\n", filter, searchbase);
213
214 rc = ldap_search_s(ld, searchbase, searchscope, filter, NULL, 0, &res);
215 if (rc != LDAP_SUCCESS) {
216 if (noreferrals && rc == LDAP_PARTIAL_RESULTS) {
217 /* Everything is fine. This is expected when referrals
218 * are disabled.
219 */
220 rc = LDAP_SUCCESS;
221 } else {
222 fprintf(stderr, PROGRAM_NAME " WARNING, LDAP search error '%s'\n", ldap_err2string(rc));
223 #if defined(NETSCAPE_SSL)
224 if (sslpath && ((rc == LDAP_SERVER_DOWN) || (rc == LDAP_CONNECT_ERROR))) {
225 int sslerr = PORT_GetError();
226 fprintf(stderr, PROGRAM_NAME ": WARNING, SSL error %d (%s)\n", sslerr, ldapssl_err2string(sslerr));
227 }
228 #endif
229 fprintf(stderr, PROGRAM_NAME " WARNING, LDAP search error, trying to recover'%s'\n", ldap_err2string(rc));
230 ldap_msgfree(res);
231 /* try to connect to the LDAP server agin, maybe my persisten conexion failed. */
232 if (!retry) {
233 ++retry;
234 ldap_unbind(ld);
235 ld = NULL;
236 ldapconnect();
237 goto retrysrch;
238 }
239 return NULL;
240
241 }
242 }
243 } else if (userdnattr) {
244 snprintf(filter,8192,"%s=%s",userdnattr,login);
245
246 retrydnattr:
247 debug("searchbase '%s'\n", userbasedn);
248 rc = ldap_search_s(ld, userbasedn, searchscope, filter, NULL, 0, &res);
249 }
250 if (rc == LDAP_SUCCESS) {
251 entry = ldap_first_entry(ld, res);
252 if (entry)
253 values = ldap_get_values(ld, entry, passattr);
254 else {
255 ldap_msgfree(res);
256 return NULL;
257 }
258 if (!values) {
259 debug("No attribute value found\n");
260 ldap_msgfree(res);
261 return NULL;
262 }
263 value = values;
264 while (*value) {
265 if (encrpass) {
266 if (strcmp(strtok(*value, delimiter), realm) == 0) {
267 password = strtok(NULL, delimiter);
268 break;
269 }
270 } else {
271 password = *value;
272 break;
273 }
274 ++value;
275 }
276 debug("password: %s\n", password);
277 if (password)
278 password = xstrdup(password);
279 ldap_value_free(values);
280 ldap_msgfree(res);
281 return password;
282 } else {
283 fprintf(stderr, PROGRAM_NAME " WARNING, LDAP error '%s'\n", ldap_err2string(rc));
284 /* try to connect to the LDAP server agin, maybe my persisten conexion failed. */
285 if (!retry) {
286 ++retry;
287 ldap_unbind(ld);
288 ld = NULL;
289 ldapconnect();
290 goto retrydnattr;
291 }
292 return NULL;
293 }
294 }
295 return NULL;
296 }
297
298 static void
299 ldapconnect(void)
300 {
301 int rc;
302
303 /* On Windows ldap_start_tls_s is available starting from Windows XP,
304 * so we need to bind at run-time with the function entry point
305 */
306 #if _SQUID_MSWIN_
307 if (use_tls) {
308
309 HMODULE WLDAP32Handle;
310
311 WLDAP32Handle = GetModuleHandle("wldap32");
312 if ((Win32_ldap_start_tls_s = (PFldap_start_tls_s) GetProcAddress(WLDAP32Handle, LDAP_START_TLS_S)) == NULL) {
313 fprintf(stderr, PROGRAM_NAME ": ERROR: TLS (-Z) not supported on this platform.\n");
314 exit(1);
315 }
316 }
317 #endif
318
319 if (ld == NULL) {
320 #if HAS_URI_SUPPORT
321 if (strstr(ldapServer, "://") != NULL) {
322 rc = ldap_initialize(&ld, ldapServer);
323 if (rc != LDAP_SUCCESS) {
324 fprintf(stderr, "\nUnable to connect to LDAPURI:%s\n", ldapServer);
325 }
326 } else
327 #endif
328 #if NETSCAPE_SSL
329 if (sslpath) {
330 if (!sslinit && (ldapssl_client_init(sslpath, NULL) != LDAP_SUCCESS)) {
331 fprintf(stderr, "\nUnable to initialise SSL with cert path %s\n",
332 sslpath);
333 exit(1);
334 } else {
335 ++sslinit;
336 }
337 if ((ld = ldapssl_init(ldapServer, port, 1)) == NULL) {
338 fprintf(stderr, "\nUnable to connect to SSL LDAP server: %s port:%d\n",
339 ldapServer, port);
340 exit(1);
341 }
342 } else
343 #endif
344 if ((ld = ldap_init(ldapServer, port)) == NULL) {
345 fprintf(stderr, "\nUnable to connect to LDAP server:%s port:%d\n", ldapServer, port);
346 }
347 if (connect_timeout)
348 squid_ldap_set_connect_timeout(connect_timeout);
349
350 #ifdef LDAP_VERSION3
351 if (version == -1) {
352 version = LDAP_VERSION2;
353 }
354 if (ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version)
355 != LDAP_SUCCESS) {
356 fprintf(stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
357 version);
358 ldap_unbind(ld);
359 ld = NULL;
360 }
361 if (use_tls) {
362 #ifdef LDAP_OPT_X_TLS
363 if (version != LDAP_VERSION3) {
364 fprintf(stderr, "TLS requires LDAP version 3\n");
365 exit(1);
366 } else if (ldap_start_tls_s(ld, NULL, NULL) != LDAP_SUCCESS) {
367 fprintf(stderr, "Could not Activate TLS connection\n");
368 exit(1);
369 }
370 #else
371 fprintf(stderr, "TLS not supported with your LDAP library\n");
372 ldap_unbind(ld);
373 ld = NULL;
374 #endif
375 }
376 #endif
377 squid_ldap_set_timelimit(timelimit);
378 squid_ldap_set_referrals(!noreferrals);
379 squid_ldap_set_aliasderef(aliasderef);
380 if (binddn && bindpasswd && *binddn && *bindpasswd) {
381 rc = ldap_simple_bind_s(ld, binddn, bindpasswd);
382 if (rc != LDAP_SUCCESS) {
383 fprintf(stderr, PROGRAM_NAME " WARNING, could not bind to binddn '%s'\n", ldap_err2string(rc));
384 ldap_unbind(ld);
385 ld = NULL;
386 }
387 }
388 debug("Connected OK\n");
389 }
390 }
391 int
392 LDAPArguments(int argc, char **argv)
393 {
394 setbuf(stdout, NULL);
395
396 while (argc > 1 && argv[1][0] == '-') {
397 const char *value = "";
398 char option = argv[1][1];
399 switch (option) {
400 case 'P':
401 case 'R':
402 case 'z':
403 case 'Z':
404 case 'g':
405 case 'e':
406 case 'S':
407 break;
408 default:
409 if (strlen(argv[1]) > 2) {
410 value = argv[1] + 2;
411 } else if (argc > 2) {
412 value = argv[2];
413 ++argv;
414 --argc;
415 } else
416 value = "";
417 break;
418 }
419 ++argv;
420 --argc;
421 switch (option) {
422 case 'H':
423 #if !HAS_URI_SUPPORT
424 fprintf(stderr, "ERROR: Your LDAP library does not have URI support\n");
425 return 1;
426 #endif
427 /* Fall thru to -h */
428 case 'h':
429 if (ldapServer) {
430 int len = strlen(ldapServer) + 1 + strlen(value) + 1;
431 char *newhost = (char*)malloc(len);
432 snprintf(newhost, len, "%s %s", ldapServer, value);
433 free(ldapServer);
434 ldapServer = newhost;
435 } else {
436 ldapServer = xstrdup(value);
437 }
438 break;
439 case 'A':
440 passattr = value;
441 break;
442 case 'e':
443 encrpass = 1;
444 break;
445 case 'l':
446 delimiter = value;
447 break;
448 case 'b':
449 userbasedn = value;
450 break;
451 case 'F':
452 usersearchfilter = value;
453 break;
454 case 'u':
455 userdnattr = value;
456 break;
457 case 's':
458 if (strcmp(value, "base") == 0)
459 searchscope = LDAP_SCOPE_BASE;
460 else if (strcmp(value, "one") == 0)
461 searchscope = LDAP_SCOPE_ONELEVEL;
462 else if (strcmp(value, "sub") == 0)
463 searchscope = LDAP_SCOPE_SUBTREE;
464 else {
465 fprintf(stderr, PROGRAM_NAME " ERROR: Unknown search scope '%s'\n", value);
466 return 1;
467 }
468 break;
469 case 'S':
470 #if defined(NETSCAPE_SSL)
471 sslpath = value;
472 if (port == LDAP_PORT)
473 port = LDAPS_PORT;
474 #else
475 fprintf(stderr, PROGRAM_NAME " ERROR: -E unsupported with this LDAP library\n");
476 return 1;
477 #endif
478 break;
479 case 'c':
480 connect_timeout = atoi(value);
481 break;
482 case 't':
483 timelimit = atoi(value);
484 break;
485 case 'a':
486 if (strcmp(value, "never") == 0)
487 aliasderef = LDAP_DEREF_NEVER;
488 else if (strcmp(value, "always") == 0)
489 aliasderef = LDAP_DEREF_ALWAYS;
490 else if (strcmp(value, "search") == 0)
491 aliasderef = LDAP_DEREF_SEARCHING;
492 else if (strcmp(value, "find") == 0)
493 aliasderef = LDAP_DEREF_FINDING;
494 else {
495 fprintf(stderr, PROGRAM_NAME " ERROR: Unknown alias dereference method '%s'\n", value);
496 return 1;
497 }
498 break;
499 case 'D':
500 binddn = value;
501 break;
502 case 'w':
503 bindpasswd = value;
504 break;
505 case 'W':
506 readSecret(value);
507 break;
508 case 'P':
509 persistent = !persistent;
510 break;
511 case 'p':
512 port = atoi(value);
513 break;
514 case 'R':
515 noreferrals = !noreferrals;
516 break;
517 #ifdef LDAP_VERSION3
518 case 'v':
519 switch (atoi(value)) {
520 case 2:
521 version = LDAP_VERSION2;
522 break;
523 case 3:
524 version = LDAP_VERSION3;
525 break;
526 default:
527 fprintf(stderr, "Protocol version should be 2 or 3\n");
528 return 1;
529 }
530 break;
531 case 'Z':
532 if (version == LDAP_VERSION2) {
533 fprintf(stderr, "TLS (-Z) is incompatible with version %d\n",
534 version);
535 return 1;
536 }
537 version = LDAP_VERSION3;
538 use_tls = 1;
539 break;
540 #endif
541 case 'd':
542 debug_enabled = 1;
543 break;
544 case 'E':
545 strip_nt_domain = 1;
546 break;
547 default:
548 fprintf(stderr, PROGRAM_NAME " ERROR: Unknown command line option '%c'\n", option);
549 return 1;
550 }
551 }
552
553 while (argc > 1) {
554 char *value = argv[1];
555 if (ldapServer) {
556 int len = strlen(ldapServer) + 1 + strlen(value) + 1;
557 char *newhost = (char*)malloc(len);
558 snprintf(newhost, len, "%s %s", ldapServer, value);
559 free(ldapServer);
560 ldapServer = newhost;
561 } else {
562 ldapServer = xstrdup(value);
563 }
564 --argc;
565 ++argv;
566 }
567
568 if (!ldapServer)
569 ldapServer = (char *) "localhost";
570
571 if (!userbasedn || !passattr) {
572 fprintf(stderr, "Usage: " PROGRAM_NAME " -b basedn -f filter [options] ldap_server_name\n\n");
573 fprintf(stderr, "\t-A password attribute(REQUIRED)\t\tUser attribute that contains the password\n");
574 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");
575 fprintf(stderr, "\t-b basedn (REQUIRED)\t\t\tbase dn under where to search for users\n");
576 fprintf(stderr, "\t-e Encrypted passwords(REQUIRED)\tPassword are stored encrypted using HHA1\n");
577 fprintf(stderr, "\t-F filter\t\t\t\tuser search filter pattern. %%s = login\n");
578 fprintf(stderr, "\t-u attribute\t\t\t\tattribute to use in combination with the basedn to create the user DN\n");
579 fprintf(stderr, "\t-s base|one|sub\t\t\t\tsearch scope\n");
580 fprintf(stderr, "\t-D binddn\t\t\t\tDN to bind as to perform searches\n");
581 fprintf(stderr, "\t-w bindpasswd\t\t\t\tpassword for binddn\n");
582 fprintf(stderr, "\t-W secretfile\t\t\t\tread password for binddn from file secretfile\n");
583 #if HAS_URI_SUPPORT
584 fprintf(stderr, "\t-H URI\t\t\t\t\tLDAPURI (defaults to ldap://localhost)\n");
585 #endif
586 fprintf(stderr, "\t-h server\t\t\t\tLDAP server (defaults to localhost)\n");
587 fprintf(stderr, "\t-p port\t\t\t\t\tLDAP server port (defaults to %d)\n", LDAP_PORT);
588 fprintf(stderr, "\t-P\t\t\t\t\tpersistent LDAP connection\n");
589 #if defined(NETSCAPE_SSL)
590 fprintf(stderr, "\t-E sslcertpath\t\t\t\tenable LDAP over SSL\n");
591 #endif
592 fprintf(stderr, "\t-c timeout\t\t\t\tconnect timeout\n");
593 fprintf(stderr, "\t-t timelimit\t\t\t\tsearch time limit\n");
594 fprintf(stderr, "\t-R\t\t\t\t\tdo not follow referrals\n");
595 fprintf(stderr, "\t-a never|always|search|find\t\twhen to dereference aliases\n");
596 #ifdef LDAP_VERSION3
597 fprintf(stderr, "\t-v 2|3\t\t\t\t\tLDAP version\n");
598 fprintf(stderr, "\t-Z\t\t\t\t\tTLS encrypt the LDAP connection, requires\n\t\t\t\tLDAP version 3\n");
599 #endif
600 fprintf(stderr, "\t-S\t\t\t\t\tStrip NT domain from usernames\n");
601 fprintf(stderr, "\n");
602 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");
603 return -1;
604 }
605 return 0;
606 }
607 static int
608 readSecret(const char *filename)
609 {
610 char buf[BUFSIZ];
611 char *e = 0;
612 FILE *f;
613
614 if (!(f = fopen(filename, "r"))) {
615 fprintf(stderr, PROGRAM_NAME " ERROR: Can not read secret file %s\n", filename);
616 return 1;
617 }
618 if (!fgets(buf, sizeof(buf) - 1, f)) {
619 fprintf(stderr, PROGRAM_NAME " ERROR: Secret file %s is empty\n", filename);
620 fclose(f);
621 return 1;
622 }
623 /* strip whitespaces on end */
624 if ((e = strrchr(buf, '\n')))
625 *e = 0;
626 if ((e = strrchr(buf, '\r')))
627 *e = 0;
628
629 bindpasswd = xstrdup(buf);
630 if (!bindpasswd) {
631 fprintf(stderr, PROGRAM_NAME " ERROR: can not allocate memory\n");
632 }
633 fclose(f);
634
635 return 0;
636 }
637
638 void
639 LDAPHHA1(RequestData * requestData)
640 {
641 char *password;
642 ldapconnect();
643 password = getpassword(requestData->user, requestData->realm);
644 if (password != NULL) {
645 if (encrpass)
646 xstrncpy(requestData->HHA1, password, sizeof(requestData->HHA1));
647 else {
648 HASH HA1;
649 DigestCalcHA1("md5", requestData->user, requestData->realm, password, NULL, NULL, HA1, requestData->HHA1);
650 }
651 free(password);
652 } else {
653 requestData->error = -1;
654 }
655
656 }