2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
10 * ext_ldap_group_acl: lookup group membership in LDAP
14 * (C)2002,2003 MARA Systems AB
16 * License: squid_ldap_group is free software; you can redistribute it
17 * and/or modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2,
19 * or (at your option) any later version.
22 * Flavio Pescuma <flavio@marasystems.com>
23 * Henrik Nordstrom <hno@marasystems.com>
24 * MARA Systems AB, Sweden <http://www.marasystems.com>
26 * With contributions from others mentioned in the ChangeLog file
28 * In part based on squid_ldap_auth by Glen Newton and Henrik Nordstrom.
30 * Latest version of this program can always be found from MARA Systems
31 * at http://marasystems.com/download/LDAP_Group/
33 * Dependencies: You need to get the OpenLDAP libraries
34 * from http://www.openldap.org or use another compatible
37 * If you want to make a TLS enabled connection you will also need the
38 * OpenSSL libraries linked into openldap. See http://www.openssl.org/
41 #include "helpers/defines.h"
45 #define LDAP_DEPRECATED 1
50 #if _SQUID_WINDOWS_ && !_SQUID_CYGWIN_
52 #define snprintf _snprintf
56 #define LDAPAPI __cdecl
59 #ifndef LDAP_OPT_X_TLS
60 #define LDAP_OPT_X_TLS 0x6000
62 /* Some tricks to allow dynamic bind with ldap_start_tls_s entry point at
65 #undef ldap_start_tls_s
67 #define LDAP_START_TLS_S "ldap_start_tls_sW"
68 typedef WINLDAPAPI
ULONG(LDAPAPI
* PFldap_start_tls_s
) (IN PLDAP
, OUT PULONG
, OUT LDAPMessage
**, IN PLDAPControlW
*, IN PLDAPControlW
*);
70 #define LDAP_START_TLS_S "ldap_start_tls_sA"
71 typedef WINLDAPAPI
ULONG(LDAPAPI
* PFldap_start_tls_s
) (IN PLDAP
, OUT PULONG
, OUT LDAPMessage
**, IN PLDAPControlA
*, IN PLDAPControlA
*);
72 #endif /* LDAP_UNICODE */
73 PFldap_start_tls_s Win32_ldap_start_tls_s
;
74 #define ldap_start_tls_s(l,s,c) Win32_ldap_start_tls_s(l,NULL,NULL,s,c)
75 #endif /* LDAP_VERSION3 */
88 #define PROGRAM_NAME "ext_ldap_group_acl"
89 #define PROGRAM_VERSION "2.18"
93 static const char *basedn
= NULL
;
94 static const char *searchfilter
= NULL
;
95 static const char *userbasedn
= NULL
;
96 static const char *userdnattr
= NULL
;
97 static const char *usersearchfilter
= NULL
;
98 static const char *binddn
= NULL
;
99 static const char *bindpasswd
= NULL
;
100 static int searchscope
= LDAP_SCOPE_SUBTREE
;
101 static int persistent
= 0;
102 static int noreferrals
= 0;
103 static int aliasderef
= LDAP_DEREF_NEVER
;
104 #if defined(NETSCAPE_SSL)
105 static char *sslpath
= NULL
;
106 static int sslinit
= 0;
108 static int connect_timeout
= 0;
109 static int timelimit
= LDAP_NO_LIMIT
;
112 /* Added for TLS support and version 3 */
113 static int use_tls
= 0;
114 static int version
= -1;
117 static int searchLDAP(LDAP
* ld
, char *group
, char *user
, char *extension_dn
);
119 static int readSecret(const char *filename
);
121 /* Yuck.. we need to glue to different versions of the API */
123 #ifndef LDAP_NO_ATTRS
124 #define LDAP_NO_ATTRS "1.1"
127 #if defined(LDAP_API_VERSION) && LDAP_API_VERSION > 1823
129 squid_ldap_errno(LDAP
* ld
)
132 ldap_get_option(ld
, LDAP_OPT_ERROR_NUMBER
, &err
);
136 squid_ldap_set_aliasderef(LDAP
* ld
, int deref
)
138 ldap_set_option(ld
, LDAP_OPT_DEREF
, &deref
);
141 squid_ldap_set_referrals(LDAP
* ld
, int referrals
)
143 int *value
= static_cast<int*>(referrals
? LDAP_OPT_ON
:LDAP_OPT_OFF
);
144 ldap_set_option(ld
, LDAP_OPT_REFERRALS
, value
);
147 squid_ldap_set_timelimit(LDAP
* ld
, int aTimeLimit
)
149 ldap_set_option(ld
, LDAP_OPT_TIMELIMIT
, &aTimeLimit
);
152 squid_ldap_set_connect_timeout(LDAP
* ld
, int aTimeLimit
)
154 #if defined(LDAP_OPT_NETWORK_TIMEOUT)
156 tv
.tv_sec
= aTimeLimit
;
158 ldap_set_option(ld
, LDAP_OPT_NETWORK_TIMEOUT
, &tv
);
159 #elif defined(LDAP_X_OPT_CONNECT_TIMEOUT)
161 ldap_set_option(ld
, LDAP_X_OPT_CONNECT_TIMEOUT
, &aTimeLimit
);
165 squid_ldap_memfree(char *p
)
172 squid_ldap_errno(LDAP
* ld
)
177 squid_ldap_set_aliasderef(LDAP
* ld
, int deref
)
179 ld
->ld_deref
= deref
;
182 squid_ldap_set_referrals(LDAP
* ld
, int referrals
)
185 ld
->ld_options
|= ~LDAP_OPT_REFERRALS
;
187 ld
->ld_options
&= ~LDAP_OPT_REFERRALS
;
190 squid_ldap_set_timelimit(LDAP
* ld
, int timelimit
)
192 ld
->ld_timelimit
= timelimit
;
195 squid_ldap_set_connect_timeout(LDAP
* ld
, int timelimit
)
197 fprintf(stderr
, "WARNING: Connect timeouts not supported in your LDAP library\n");
200 squid_ldap_memfree(char *p
)
207 #ifdef LDAP_API_FEATURE_X_OPENLDAP
208 #if LDAP_VENDOR_VERSION > 194
209 #define HAS_URI_SUPPORT 1
214 main(int argc
, char **argv
)
216 char buf
[HELPER_INPUT_BUFFER
];
217 char *user
, *group
, *extension_dn
= NULL
;
218 char *ldapServer
= NULL
;
220 int tryagain
= 0, rc
;
221 int port
= LDAP_PORT
;
222 int use_extension_dn
= 0;
223 int strip_nt_domain
= 0;
224 int strip_kerberos_realm
= 0;
226 setbuf(stdout
, NULL
);
228 while (argc
> 1 && argv
[1][0] == '-') {
229 const char *value
= "";
230 char option
= argv
[1][1];
242 if (strlen(argv
[1]) > 2) {
244 } else if (argc
> 2) {
257 fprintf(stderr
, "FATAL: Your LDAP library does not have URI support\n");
260 /* Fall thru to -h */
263 int len
= strlen(ldapServer
) + 1 + strlen(value
) + 1;
264 char *newhost
= (char*)malloc(len
);
265 snprintf(newhost
, len
, "%s %s", ldapServer
, value
);
267 ldapServer
= newhost
;
269 ldapServer
= xstrdup(value
);
276 searchfilter
= value
;
282 usersearchfilter
= value
;
288 if (strcmp(value
, "base") == 0)
289 searchscope
= LDAP_SCOPE_BASE
;
290 else if (strcmp(value
, "one") == 0)
291 searchscope
= LDAP_SCOPE_ONELEVEL
;
292 else if (strcmp(value
, "sub") == 0)
293 searchscope
= LDAP_SCOPE_SUBTREE
;
295 fprintf(stderr
, PROGRAM_NAME
": FATAL: Unknown search scope '%s'\n", value
);
300 #if defined(NETSCAPE_SSL)
302 if (port
== LDAP_PORT
)
305 fprintf(stderr
, PROGRAM_NAME
": FATAL: -E unsupported with this LDAP library\n");
310 connect_timeout
= atoi(value
);
313 timelimit
= atoi(value
);
316 if (strcmp(value
, "never") == 0)
317 aliasderef
= LDAP_DEREF_NEVER
;
318 else if (strcmp(value
, "always") == 0)
319 aliasderef
= LDAP_DEREF_ALWAYS
;
320 else if (strcmp(value
, "search") == 0)
321 aliasderef
= LDAP_DEREF_SEARCHING
;
322 else if (strcmp(value
, "find") == 0)
323 aliasderef
= LDAP_DEREF_FINDING
;
325 fprintf(stderr
, PROGRAM_NAME
": FATAL: Unknown alias dereference method '%s'\n", value
);
339 persistent
= !persistent
;
345 noreferrals
= !noreferrals
;
349 switch (atoi(value
)) {
351 version
= LDAP_VERSION2
;
354 version
= LDAP_VERSION3
;
357 fprintf(stderr
, "FATAL: Protocol version should be 2 or 3\n");
362 if (version
== LDAP_VERSION2
) {
363 fprintf(stderr
, "FATAL: TLS (-Z) is incompatible with version %d\n",
367 version
= LDAP_VERSION3
;
375 use_extension_dn
= 1;
381 strip_kerberos_realm
= 1;
384 fprintf(stderr
, PROGRAM_NAME
": FATAL: Unknown command line option '%c'\n", option
);
390 char *value
= argv
[1];
392 int len
= strlen(ldapServer
) + 1 + strlen(value
) + 1;
393 char *newhost
= (char*)malloc(len
);
394 snprintf(newhost
, len
, "%s %s", ldapServer
, value
);
396 ldapServer
= newhost
;
398 ldapServer
= xstrdup(value
);
405 ldapServer
= (char *) "localhost";
407 if (!basedn
|| !searchfilter
) {
408 fprintf(stderr
, "\n" PROGRAM_NAME
" version " PROGRAM_VERSION
"\n\n");
409 fprintf(stderr
, "Usage: " PROGRAM_NAME
" -b basedn -f filter [options] ldap_server_name\n\n");
410 fprintf(stderr
, "\t-b basedn (REQUIRED)\tbase dn under where to search for groups\n");
411 fprintf(stderr
, "\t-f filter (REQUIRED)\tgroup search filter pattern. %%u = user,\n\t\t\t\t%%v = group\n");
412 fprintf(stderr
, "\t-B basedn (REQUIRED)\tbase dn under where to search for users\n");
413 fprintf(stderr
, "\t-F filter (REQUIRED)\tuser search filter pattern. %%s = login\n");
414 fprintf(stderr
, "\t-s base|one|sub\t\tsearch scope\n");
415 fprintf(stderr
, "\t-D binddn\t\tDN to bind as to perform searches\n");
416 fprintf(stderr
, "\t-w bindpasswd\t\tpassword for binddn\n");
417 fprintf(stderr
, "\t-W secretfile\t\tread password for binddn from file secretfile\n");
419 fprintf(stderr
, "\t-H URI\t\t\tLDAPURI (defaults to ldap://localhost)\n");
421 fprintf(stderr
, "\t-h server\t\tLDAP server (defaults to localhost)\n");
422 fprintf(stderr
, "\t-p port\t\t\tLDAP server port (defaults to %d)\n", LDAP_PORT
);
423 fprintf(stderr
, "\t-P\t\t\tpersistent LDAP connection\n");
424 #if defined(NETSCAPE_SSL)
425 fprintf(stderr
, "\t-E sslcertpath\t\tenable LDAP over SSL\n");
427 fprintf(stderr
, "\t-c timeout\t\tconnect timeout\n");
428 fprintf(stderr
, "\t-t timelimit\t\tsearch time limit\n");
429 fprintf(stderr
, "\t-R\t\t\tdo not follow referrals\n");
430 fprintf(stderr
, "\t-a never|always|search|find\n\t\t\t\twhen to dereference aliases\n");
432 fprintf(stderr
, "\t-v 2|3\t\t\tLDAP version\n");
433 fprintf(stderr
, "\t-Z\t\t\tTLS encrypt the LDAP connection, requires\n\t\t\t\tLDAP version 3\n");
435 fprintf(stderr
, "\t-g\t\t\tfirst query parameter is base DN extension\n\t\t\t\tfor this query\n");
436 fprintf(stderr
, "\t-S\t\t\tStrip NT domain from usernames\n");
437 fprintf(stderr
, "\t-K\t\t\tStrip Kerberos realm from usernames\n");
438 fprintf(stderr
, "\t-d\t\t\tenable debug mode\n");
439 fprintf(stderr
, "\n");
440 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");
443 /* On Windows ldap_start_tls_s is available starting from Windows XP,
444 * so we need to bind at run-time with the function entry point
449 HMODULE WLDAP32Handle
;
451 WLDAP32Handle
= GetModuleHandle("wldap32");
452 if ((Win32_ldap_start_tls_s
= (PFldap_start_tls_s
) GetProcAddress(WLDAP32Handle
, LDAP_START_TLS_S
)) == NULL
) {
453 fprintf(stderr
, PROGRAM_NAME
": FATAL: TLS (-Z) not supported on this platform.\n");
459 while (fgets(buf
, HELPER_INPUT_BUFFER
, stdin
) != NULL
) {
461 if (!strchr(buf
, '\n')) {
462 /* too large message received.. skip and deny */
463 fprintf(stderr
, "%s: ERROR: Input Too large: %s\n", argv
[0], buf
);
464 while (fgets(buf
, sizeof(buf
), stdin
)) {
465 fprintf(stderr
, "%s: ERROR: Input Too large..: %s\n", argv
[0], buf
);
466 if (strchr(buf
, '\n') != NULL
)
472 user
= strtok(buf
, " \n");
474 debug("%s: Invalid request: No Username given\n", argv
[0]);
475 SEND_ERR("Invalid request. No Username");
478 rfc1738_unescape(user
);
479 if (strip_nt_domain
) {
480 char *u
= strrchr(user
, '\\');
482 u
= strrchr(user
, '/');
484 u
= strrchr(user
, '+');
488 if (strip_kerberos_realm
) {
489 char *u
= strchr(user
, '@');
494 if (use_extension_dn
) {
495 extension_dn
= strtok(NULL
, " \n");
497 debug("%s: Invalid request: Extension DN configured, but none sent.\n", argv
[0]);
498 SEND_ERR("Invalid Request. Extension DN required.");
501 rfc1738_unescape(extension_dn
);
503 while (!found
&& user
&& (group
= strtok(NULL
, " \n")) != NULL
) {
504 rfc1738_unescape(group
);
509 if (strstr(ldapServer
, "://") != NULL
) {
510 rc
= ldap_initialize(&ld
, ldapServer
);
511 if (rc
!= LDAP_SUCCESS
) {
512 fprintf(stderr
, "%s: ERROR: Unable to connect to LDAPURI:%s\n", argv
[0], ldapServer
);
519 if (!sslinit
&& (ldapssl_client_init(sslpath
, NULL
) != LDAP_SUCCESS
)) {
520 fprintf(stderr
, "FATAL: Unable to initialise SSL with cert path %s\n", sslpath
);
525 if ((ld
= ldapssl_init(ldapServer
, port
, 1)) == NULL
) {
526 fprintf(stderr
, "FATAL: Unable to connect to SSL LDAP server: %s port:%d\n",
532 if ((ld
= ldap_init(ldapServer
, port
)) == NULL
) {
533 fprintf(stderr
, "ERROR: Unable to connect to LDAP server:%s port:%d\n", ldapServer
, port
);
537 squid_ldap_set_connect_timeout(ld
, connect_timeout
);
541 version
= LDAP_VERSION3
;
543 if (ldap_set_option(ld
, LDAP_OPT_PROTOCOL_VERSION
, &version
) != LDAP_SUCCESS
) {
544 fprintf(stderr
, "ERROR: Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
551 #ifdef LDAP_OPT_X_TLS
552 if (version
!= LDAP_VERSION3
) {
553 fprintf(stderr
, "FATAL: TLS requires LDAP version 3\n");
555 } else if (ldap_start_tls_s(ld
, NULL
, NULL
) != LDAP_SUCCESS
) {
556 fprintf(stderr
, "ERROR: Could not Activate TLS connection\n");
562 fprintf(stderr
, "FATAL: TLS not supported with your LDAP library\n");
567 squid_ldap_set_timelimit(ld
, timelimit
);
568 squid_ldap_set_referrals(ld
, !noreferrals
);
569 squid_ldap_set_aliasderef(ld
, aliasderef
);
570 if (binddn
&& bindpasswd
&& *binddn
&& *bindpasswd
) {
571 rc
= ldap_simple_bind_s(ld
, binddn
, bindpasswd
);
572 if (rc
!= LDAP_SUCCESS
) {
573 fprintf(stderr
, PROGRAM_NAME
": WARNING: could not bind to binddn '%s'\n", ldap_err2string(rc
));
579 debug("Connected OK\n");
581 if (searchLDAP(ld
, group
, user
, extension_dn
) == 0) {
600 if (!persistent
|| (squid_ldap_errno(ld
) != LDAP_SUCCESS
&& squid_ldap_errno(ld
) != LDAP_INVALID_CREDENTIALS
)) {
614 ldap_escape_value(char *escaped
, int size
, const char *src
)
617 while (size
> 4 && *src
) {
628 snprintf(escaped
, 3, "%02x", (unsigned char) *src
);
646 build_filter(char *filter
, int size
, const char *templ
, const char *user
, const char *group
)
649 while (*templ
&& size
> 0) {
657 n
= ldap_escape_value(filter
, size
, user
);
664 n
= ldap_escape_value(filter
, size
, group
);
669 fprintf(stderr
, "ERROR: Unknown filter template string %%%c\n", *templ
);
692 fprintf(stderr
, "ERROR: Filter too large\n");
700 searchLDAPGroup(LDAP
* ld
, char *group
, char *member
, char *extension_dn
)
703 static char searchbase
[256];
704 LDAPMessage
*res
= NULL
;
707 char *searchattr
[] = {(char *) LDAP_NO_ATTRS
, NULL
};
709 if (extension_dn
&& *extension_dn
)
710 snprintf(searchbase
, sizeof(searchbase
), "%s,%s", extension_dn
, basedn
);
712 snprintf(searchbase
, sizeof(searchbase
), "%s", basedn
);
714 if (build_filter(filter
, sizeof(filter
), searchfilter
, member
, group
) != 0) {
715 fprintf(stderr
, PROGRAM_NAME
": ERROR: Failed to construct LDAP search filter. filter=\"%s\", user=\"%s\", group=\"%s\"\n", filter
, member
, group
);
718 debug("group filter '%s', searchbase '%s'\n", filter
, searchbase
);
720 rc
= ldap_search_s(ld
, searchbase
, searchscope
, filter
, searchattr
, 1, &res
);
721 if (rc
!= LDAP_SUCCESS
) {
722 if (noreferrals
&& rc
== LDAP_PARTIAL_RESULTS
) {
723 /* Everything is fine. This is expected when referrals
727 fprintf(stderr
, PROGRAM_NAME
": WARNING: LDAP search error '%s'\n", ldap_err2string(rc
));
728 #if defined(NETSCAPE_SSL)
729 if (sslpath
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
))) {
730 int sslerr
= PORT_GetError();
731 fprintf(stderr
, PROGRAM_NAME
": WARNING: SSL error %d (%s)\n", sslerr
, ldapssl_err2string(sslerr
));
738 entry
= ldap_first_entry(ld
, res
);
748 searchLDAP(LDAP
* ld
, char *group
, char *login
, char *extension_dn
)
751 if (usersearchfilter
) {
753 char searchbase
[8192];
754 char escaped_login
[1024];
755 LDAPMessage
*res
= NULL
;
759 char *searchattr
[] = {(char *) LDAP_NO_ATTRS
, NULL
};
760 if (extension_dn
&& *extension_dn
)
761 snprintf(searchbase
, sizeof(searchbase
), "%s,%s", extension_dn
, userbasedn
? userbasedn
: basedn
);
763 snprintf(searchbase
, sizeof(searchbase
), "%s", userbasedn
? userbasedn
: basedn
);
764 ldap_escape_value(escaped_login
, sizeof(escaped_login
), login
);
765 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
);
766 debug("user filter '%s', searchbase '%s'\n", filter
, searchbase
);
767 rc
= ldap_search_s(ld
, searchbase
, searchscope
, filter
, searchattr
, 1, &res
);
768 if (rc
!= LDAP_SUCCESS
) {
769 if (noreferrals
&& rc
== LDAP_PARTIAL_RESULTS
) {
770 /* Everything is fine. This is expected when referrals
774 fprintf(stderr
, PROGRAM_NAME
": WARNING: LDAP search error '%s'\n", ldap_err2string(rc
));
775 #if defined(NETSCAPE_SSL)
776 if (sslpath
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
))) {
777 int sslerr
= PORT_GetError();
778 fprintf(stderr
, PROGRAM_NAME
": WARNING: SSL error %d (%s)\n", sslerr
, ldapssl_err2string(sslerr
));
785 entry
= ldap_first_entry(ld
, res
);
787 fprintf(stderr
, PROGRAM_NAME
": WARNING: User '%s' not found in '%s'\n", login
, searchbase
);
791 userdn
= ldap_get_dn(ld
, entry
);
792 rc
= searchLDAPGroup(ld
, group
, userdn
, extension_dn
);
793 squid_ldap_memfree(userdn
);
796 } else if (userdnattr
) {
798 if (extension_dn
&& *extension_dn
)
799 snprintf(dn
, 8192, "%s=%s, %s, %s", userdnattr
, login
, extension_dn
, userbasedn
? userbasedn
: basedn
);
801 snprintf(dn
, 8192, "%s=%s, %s", userdnattr
, login
, userbasedn
? userbasedn
: basedn
);
802 return searchLDAPGroup(ld
, group
, dn
, extension_dn
);
804 return searchLDAPGroup(ld
, group
, login
, extension_dn
);
809 readSecret(const char *filename
)
815 if (!(f
= fopen(filename
, "r"))) {
816 fprintf(stderr
, PROGRAM_NAME
": ERROR: Can not read secret file %s\n", filename
);
819 if (!fgets(buf
, sizeof(buf
) - 1, f
)) {
820 fprintf(stderr
, PROGRAM_NAME
": ERROR: Secret file %s is empty\n", filename
);
824 /* strip whitespaces on end */
825 if ((e
= strrchr(buf
, '\n')))
827 if ((e
= strrchr(buf
, '\r')))
830 bindpasswd
= xstrdup(buf
);
832 fprintf(stderr
, PROGRAM_NAME
": ERROR: can not allocate memory\n");