2 * squid_ldap_auth: authentication via ldap for squid proxy server
12 * National Research Council
14 * with contributions from others mentioned in the Changes section below
16 * Usage: squid_ldap_auth -b basedn [-s searchscope]
17 * [-f searchfilter] [-D binddn -w bindpasswd]
18 * [-u attr] [-h host] [-p port] [-P] [-R] [ldap_server_name[:port]] ...
20 * Dependencies: You need to get the OpenLDAP libraries
21 * from http://www.openldap.org or another compatible LDAP C-API
24 * If you want to make a TLS enabled connection you will also need the
25 * OpenSSL libraries linked into openldap. See http://www.openssl.org/
27 * License: squid_ldap_auth is free software; you can redistribute it
28 * and/or modify it under the terms of the GNU General Public License
29 * as published by the Free Software Foundation; either version 2,
30 * or (at your option) any later version.
33 * 2005-01-07: Henrik Nordstrom <hno@squid-cache.org>
34 * - Added some sanity checks on login names to avoid
35 * users bypassing equality checks by exploring the
36 * overly helpful match capabilities of LDAP
37 * 2004-07-17: Henrik Nordstrom <hno@squid-cache.org>
38 * - Corrected non-persistent mode to only issue one
39 * ldap_bind per connection.
40 * - -U option to compare the users password rather
42 * 2004-03-01: Henrik Nordstrom <hno@squid-cache.org>
43 * - corrected building of search filters to escape
45 * - -d option for "debug" like squid_ldap_group
46 * 2004-01-05: Henrik Nordstrom <hno@squid-cache.org>
47 * - Corrected TLS mode
48 * 2003-03-01: David J N Begley
49 * - Support for Netscape API method of ldap over SSL
51 * - Timeout option for better recovery when using
52 * multiple LDAP servers
53 * 2003-03-01: Christoph Lechleitner <lech@ibcl.at>
54 * - Added -W option to read bindpasswd from file
55 * 2003-03-01: Juerg Michel
56 * - Added support for ldap URI via the -H option
58 * 2001-12-12: Michael Cunningham <m.cunningham@xpedite.com>
59 * - Added TLS support and partial ldap version 3 support.
60 * 2001-10-04: Henrik Nordstrom <hno@squid-cache.org>
61 * - Be consistent with the other helpers in how
62 * spaces are managed. If there is space characters
63 * then these are assumed to be part of the password
64 * 2001-09-05: Henrik Nordstrom <hno@squid-cache.org>
65 * - Added ability to specify another default LDAP port to
66 * connect to. Persistent connections moved to -P
67 * 2001-05-02: Henrik Nordstrom <hno@squid-cache.org>
68 * - Support newer OpenLDAP 2.x libraries using the
69 * revised Internet Draft API which unfortunately
70 * is not backwards compatible with RFC1823..
71 * 2001-04-15: Henrik Nordstrom <hno@squid-cache.org>
72 * - Added command line option for basedn
73 * - Added the ability to search for the user DN
74 * 2001-04-16: Henrik Nordstrom <hno@squid-cache.org>
75 * - Added -D binddn -w bindpasswd.
76 * 2001-04-17: Henrik Nordstrom <hno@squid-cache.org>
77 * - Added -R to disable referrals
78 * - Added -a to control alias dereferencing
79 * 2001-04-17: Henrik Nordstrom <hno@squid-cache.org>
80 * - Added -u, DN username attribute name
81 * 2001-04-18: Henrik Nordstrom <hno@squid-cache.org>
82 * - Allow full filter specifications in -f
87 #define LDAP_DEPRECATED 1
96 #if _SQUID_MSWIN_ /* Native Windows port and MinGW */
98 #define snprintf _snprintf
102 #define LDAPAPI __cdecl
105 #ifndef LDAP_OPT_X_TLS
106 #define LDAP_OPT_X_TLS 0x6000
108 /* Some tricks to allow dynamic bind with ldap_start_tls_s entry point at
111 #undef ldap_start_tls_s
113 #define LDAP_START_TLS_S "ldap_start_tls_sW"
114 typedef WINLDAPAPI
ULONG(LDAPAPI
* PFldap_start_tls_s
) (IN PLDAP
, OUT PULONG
, OUT LDAPMessage
**, IN PLDAPControlW
*, IN PLDAPControlW
*);
116 #define LDAP_START_TLS_S "ldap_start_tls_sA"
117 typedef WINLDAPAPI
ULONG(LDAPAPI
* PFldap_start_tls_s
) (IN PLDAP
, OUT PULONG
, OUT LDAPMessage
**, IN PLDAPControlA
*, IN PLDAPControlA
*);
118 #endif /* LDAP_UNICODE */
119 PFldap_start_tls_s Win32_ldap_start_tls_s
;
120 #define ldap_start_tls_s(l,s,c) Win32_ldap_start_tls_s(l,NULL,NULL,s,c)
121 #endif /* LDAP_VERSION3 */
130 #define PROGRAM_NAME "basic_ldap_auth"
133 static const char *basedn
;
134 static const char *searchfilter
= NULL
;
135 static const char *binddn
= NULL
;
136 static const char *bindpasswd
= NULL
;
137 static const char *userattr
= "uid";
138 static const char *passwdattr
= NULL
;
139 static int searchscope
= LDAP_SCOPE_SUBTREE
;
140 static int persistent
= 0;
141 static int bind_once
= 0;
142 static int noreferrals
= 0;
143 static int aliasderef
= LDAP_DEREF_NEVER
;
144 #if defined(NETSCAPE_SSL)
145 static const char *sslpath
= NULL
;
146 static int sslinit
= 0;
148 static int connect_timeout
= 0;
149 static int timelimit
= LDAP_NO_LIMIT
;
151 /* Added for TLS support and version 3 */
152 static int use_tls
= 0;
153 static int version
= -1;
155 static int checkLDAP(LDAP
* ld
, const char *userid
, const char *password
, const char *server
, int port
);
156 static int readSecret(const char *filename
);
158 /* Yuck.. we need to glue to different versions of the API */
160 #ifndef LDAP_NO_ATTRS
161 #define LDAP_NO_ATTRS "1.1"
164 #if defined(LDAP_API_VERSION) && LDAP_API_VERSION > 1823
166 squid_ldap_errno(LDAP
* ld
)
169 ldap_get_option(ld
, LDAP_OPT_ERROR_NUMBER
, &err
);
173 squid_ldap_set_aliasderef(LDAP
* ld
, int deref
)
175 ldap_set_option(ld
, LDAP_OPT_DEREF
, &deref
);
178 squid_ldap_set_referrals(LDAP
* ld
, int referrals
)
180 int *value
= static_cast<int*>(referrals
? LDAP_OPT_ON
:LDAP_OPT_OFF
);
181 ldap_set_option(ld
, LDAP_OPT_REFERRALS
, value
);
184 squid_ldap_set_timelimit(LDAP
* ld
, int aTimeLimit
)
186 ldap_set_option(ld
, LDAP_OPT_TIMELIMIT
, &aTimeLimit
);
189 squid_ldap_set_connect_timeout(LDAP
* ld
, int aTimeLimit
)
191 #if defined(LDAP_OPT_NETWORK_TIMEOUT)
193 tv
.tv_sec
= aTimeLimit
;
195 ldap_set_option(ld
, LDAP_OPT_NETWORK_TIMEOUT
, &tv
);
196 #elif defined(LDAP_X_OPT_CONNECT_TIMEOUT)
198 ldap_set_option(ld
, LDAP_X_OPT_CONNECT_TIMEOUT
, &aTimeLimit
);
202 squid_ldap_memfree(char *p
)
209 squid_ldap_errno(LDAP
* ld
)
214 squid_ldap_set_aliasderef(LDAP
* ld
, int deref
)
216 ld
->ld_deref
= deref
;
219 squid_ldap_set_referrals(LDAP
* ld
, int referrals
)
222 ld
->ld_options
|= ~LDAP_OPT_REFERRALS
;
224 ld
->ld_options
&= ~LDAP_OPT_REFERRALS
;
227 squid_ldap_set_timelimit(LDAP
* ld
, int timelimit
)
229 ld
->ld_timelimit
= timelimit
;
232 squid_ldap_set_connect_timeout(LDAP
* ld
, int timelimit
)
234 fprintf(stderr
, "Connect timeouts not supported in your LDAP library\n");
237 squid_ldap_memfree(char *p
)
244 #ifdef LDAP_API_FEATURE_X_OPENLDAP
245 #if LDAP_VENDOR_VERSION > 194
246 #define HAS_URI_SUPPORT 1
251 open_ldap_connection(const char *ldapServer
, int port
)
255 if (strstr(ldapServer
, "://") != NULL
) {
256 int rc
= ldap_initialize(&ld
, ldapServer
);
257 if (rc
!= LDAP_SUCCESS
) {
258 fprintf(stderr
, "\nUnable to connect to LDAPURI:%s\n", ldapServer
);
265 if (!sslinit
&& (ldapssl_client_init(sslpath
, NULL
) != LDAP_SUCCESS
)) {
266 fprintf(stderr
, "\nUnable to initialise SSL with cert path %s\n",
272 if ((ld
= ldapssl_init(ldapServer
, port
, 1)) == NULL
) {
273 fprintf(stderr
, "\nUnable to connect to SSL LDAP server: %s port:%d\n",
279 if ((ld
= ldap_init(ldapServer
, port
)) == NULL
) {
280 fprintf(stderr
, "\nUnable to connect to LDAP server:%s port:%d\n",
285 squid_ldap_set_connect_timeout(ld
, connect_timeout
);
289 version
= LDAP_VERSION3
;
291 if (ldap_set_option(ld
, LDAP_OPT_PROTOCOL_VERSION
, &version
) != LDAP_SUCCESS
) {
292 fprintf(stderr
, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
297 #ifdef LDAP_OPT_X_TLS
298 if (version
!= LDAP_VERSION3
) {
299 fprintf(stderr
, "TLS requires LDAP version 3\n");
301 } else if (ldap_start_tls_s(ld
, NULL
, NULL
) != LDAP_SUCCESS
) {
302 fprintf(stderr
, "Could not Activate TLS connection\n");
306 fprintf(stderr
, "TLS not supported with your LDAP library\n");
311 squid_ldap_set_timelimit(ld
, timelimit
);
312 squid_ldap_set_referrals(ld
, !noreferrals
);
313 squid_ldap_set_aliasderef(ld
, aliasderef
);
317 /* Make a sanity check on the username to reject oddly typed names */
319 validUsername(const char *user
)
321 const unsigned char *p
= (const unsigned char *) user
;
323 /* Leading whitespace? */
326 while (p
[0] && p
[1]) {
327 if (xisspace(p
[0])) {
328 /* More than one consequitive space? */
331 /* or odd space type character used? */
337 /* Trailing whitespace? */
344 main(int argc
, char **argv
)
348 char *ldapServer
= NULL
;
351 int port
= LDAP_PORT
;
353 setbuf(stdout
, NULL
);
355 while (argc
> 1 && argv
[1][0] == '-') {
356 const char *value
= "";
357 char option
= argv
[1][1];
367 if (strlen(argv
[1]) > 2) {
369 } else if (argc
> 2) {
382 fprintf(stderr
, "ERROR: Your LDAP library does not have URI support\n");
385 /* Fall thru to -h */
388 int len
= strlen(ldapServer
) + 1 + strlen(value
) + 1;
389 char *newhost
= static_cast<char*>(malloc(len
));
390 snprintf(newhost
, len
, "%s %s", ldapServer
, value
);
392 ldapServer
= newhost
;
394 ldapServer
= xstrdup(value
);
401 searchfilter
= value
;
410 if (strcmp(value
, "base") == 0)
411 searchscope
= LDAP_SCOPE_BASE
;
412 else if (strcmp(value
, "one") == 0)
413 searchscope
= LDAP_SCOPE_ONELEVEL
;
414 else if (strcmp(value
, "sub") == 0)
415 searchscope
= LDAP_SCOPE_SUBTREE
;
417 fprintf(stderr
, PROGRAM_NAME
": ERROR: Unknown search scope '%s'\n", value
);
422 #if defined(NETSCAPE_SSL)
424 if (port
== LDAP_PORT
)
427 fprintf(stderr
, PROGRAM_NAME
" ERROR: -E unsupported with this LDAP library\n");
432 connect_timeout
= atoi(value
);
435 timelimit
= atoi(value
);
438 if (strcmp(value
, "never") == 0)
439 aliasderef
= LDAP_DEREF_NEVER
;
440 else if (strcmp(value
, "always") == 0)
441 aliasderef
= LDAP_DEREF_ALWAYS
;
442 else if (strcmp(value
, "search") == 0)
443 aliasderef
= LDAP_DEREF_SEARCHING
;
444 else if (strcmp(value
, "find") == 0)
445 aliasderef
= LDAP_DEREF_FINDING
;
447 fprintf(stderr
, PROGRAM_NAME
": ERROR: Unknown alias dereference method '%s'\n", value
);
461 persistent
= !persistent
;
464 bind_once
= !bind_once
;
470 noreferrals
= !noreferrals
;
474 switch (atoi(value
)) {
476 version
= LDAP_VERSION2
;
479 version
= LDAP_VERSION3
;
482 fprintf(stderr
, "Protocol version should be 2 or 3\n");
487 if (version
== LDAP_VERSION2
) {
488 fprintf(stderr
, "TLS (-Z) is incompatible with version %d\n",
492 version
= LDAP_VERSION3
;
500 fprintf(stderr
, PROGRAM_NAME
": ERROR: Unknown command line option '%c'\n", option
);
506 char *value
= argv
[1];
508 int len
= strlen(ldapServer
) + 1 + strlen(value
) + 1;
509 char *newhost
= static_cast<char*>(malloc(len
));
510 snprintf(newhost
, len
, "%s %s", ldapServer
, value
);
512 ldapServer
= newhost
;
514 ldapServer
= xstrdup(value
);
520 ldapServer
= xstrdup("localhost");
523 fprintf(stderr
, "Usage: " PROGRAM_NAME
" -b basedn [options] [ldap_server_name[:port]]...\n\n");
524 fprintf(stderr
, "\t-b basedn (REQUIRED)\tbase dn under which to search\n");
525 fprintf(stderr
, "\t-f filter\t\tsearch filter to locate user DN\n");
526 fprintf(stderr
, "\t-u userattr\t\tusername DN attribute\n");
527 fprintf(stderr
, "\t-s base|one|sub\t\tsearch scope\n");
528 fprintf(stderr
, "\t-D binddn\t\tDN to bind as to perform searches\n");
529 fprintf(stderr
, "\t-w bindpasswd\t\tpassword for binddn\n");
530 fprintf(stderr
, "\t-W secretfile\t\tread password for binddn from file secretfile\n");
532 fprintf(stderr
, "\t-H URI\t\t\tLDAPURI (defaults to ldap://localhost)\n");
534 fprintf(stderr
, "\t-h server\t\tLDAP server (defaults to localhost)\n");
535 fprintf(stderr
, "\t-p port\t\t\tLDAP server port\n");
536 fprintf(stderr
, "\t-P\t\t\tpersistent LDAP connection\n");
537 #if defined(NETSCAPE_SSL)
538 fprintf(stderr
, "\t-E sslcertpath\t\tenable LDAP over SSL\n");
540 fprintf(stderr
, "\t-c timeout\t\tconnect timeout\n");
541 fprintf(stderr
, "\t-t timelimit\t\tsearch time limit\n");
542 fprintf(stderr
, "\t-R\t\t\tdo not follow referrals\n");
543 fprintf(stderr
, "\t-a never|always|search|find\n\t\t\t\twhen to dereference aliases\n");
545 fprintf(stderr
, "\t-v 2|3\t\t\tLDAP version\n");
546 fprintf(stderr
, "\t-Z\t\t\tTLS encrypt the LDAP connection, requires LDAP version 3\n");
548 fprintf(stderr
, "\t-d\t\t\tenable debug mode\n");
549 fprintf(stderr
, "\n");
550 fprintf(stderr
, "\tIf no search filter is specified, then the dn <userattr>=user,basedn\n\twill be used (same as specifying a search filter of '<userattr>=',\n\tbut quicker as as there is no need to search for the user DN)\n\n");
551 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");
554 /* On Windows ldap_start_tls_s is available starting from Windows XP,
555 * so we need to bind at run-time with the function entry point
560 HMODULE WLDAP32Handle
;
562 WLDAP32Handle
= GetModuleHandle("wldap32");
563 if ((Win32_ldap_start_tls_s
= (PFldap_start_tls_s
) GetProcAddress(WLDAP32Handle
, LDAP_START_TLS_S
)) == NULL
) {
564 fprintf(stderr
, PROGRAM_NAME
": ERROR: TLS (-Z) not supported on this platform.\n");
570 while (fgets(buf
, sizeof(buf
), stdin
) != NULL
) {
571 user
= strtok(buf
, " \r\n");
572 passwd
= strtok(NULL
, "\r\n");
575 printf("ERR Missing username\n");
578 if (!passwd
|| !passwd
[0]) {
579 printf("ERR Missing password '%s'\n", user
);
582 rfc1738_unescape(user
);
583 rfc1738_unescape(passwd
);
584 if (!validUsername(user
)) {
585 printf("ERR No such user '%s':'%s'\n",user
, passwd
);
588 tryagain
= (ld
!= NULL
);
590 if (ld
== NULL
&& persistent
)
591 ld
= open_ldap_connection(ldapServer
, port
);
592 if (checkLDAP(ld
, user
, passwd
, ldapServer
, port
) != 0) {
593 if (tryagain
&& squid_ldap_errno(ld
) != LDAP_INVALID_CREDENTIALS
) {
599 printf("ERR %s\n", ldap_err2string(squid_ldap_errno(ld
)));
603 if (ld
&& (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
) {
627 snprintf(escaped
, 3, "%02x", (unsigned char) *src
++);
641 /* Check the userid & password.
642 * Return 0 on success, 1 on failure
645 checkLDAP(LDAP
* persistent_ld
, const char *userid
, const char *password
, const char *ldapServer
, int port
)
649 LDAP
*bind_ld
= NULL
;
652 /* LDAP can't bind with a blank password. Seen as "anonymous"
653 * and always granted access
655 debug("Blank password given\n");
660 char escaped_login
[1024];
661 LDAPMessage
*res
= NULL
;
663 char *searchattr
[] = {(char *)LDAP_NO_ATTRS
, NULL
};
666 LDAP
*search_ld
= persistent_ld
;
669 search_ld
= open_ldap_connection(ldapServer
, port
);
671 ldap_escape_value(escaped_login
, sizeof(escaped_login
), userid
);
673 rc
= ldap_simple_bind_s(search_ld
, binddn
, bindpasswd
);
674 if (rc
!= LDAP_SUCCESS
) {
675 fprintf(stderr
, PROGRAM_NAME
": WARNING, could not bind to binddn '%s'\n", ldap_err2string(rc
));
680 snprintf(filter
, sizeof(filter
), searchfilter
, 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
);
681 debug("user filter '%s', searchbase '%s'\n", filter
, basedn
);
682 rc
= ldap_search_s(search_ld
, basedn
, searchscope
, filter
, searchattr
, 1, &res
);
683 if (rc
!= LDAP_SUCCESS
) {
684 if (noreferrals
&& rc
== LDAP_PARTIAL_RESULTS
) {
685 /* Everything is fine. This is expected when referrals
688 debug("noreferrals && rc == LDAP_PARTIAL_RESULTS\n");
690 fprintf(stderr
, PROGRAM_NAME
": WARNING, LDAP search error '%s'\n", ldap_err2string(rc
));
691 #if defined(NETSCAPE_SSL)
692 if (sslpath
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
))) {
693 int sslerr
= PORT_GetError();
694 fprintf(stderr
, PROGRAM_NAME
": WARNING, SSL error %d (%s)\n", sslerr
, ldapssl_err2string(sslerr
));
701 entry
= ldap_first_entry(search_ld
, res
);
703 debug("Ldap search returned nothing\n");
707 userdn
= ldap_get_dn(search_ld
, entry
);
709 fprintf(stderr
, PROGRAM_NAME
": ERROR, could not get user DN for '%s'\n", userid
);
713 snprintf(dn
, sizeof(dn
), "%s", userdn
);
714 squid_ldap_memfree(userdn
);
716 if (ret
== 0 && (!binddn
|| !bind_once
|| passwdattr
)) {
717 /* Reuse the search connection for comparing the user password attribute */
726 if (search_ld
&& search_ld
!= persistent_ld
) {
727 ldap_unbind(search_ld
);
733 snprintf(dn
, sizeof(dn
), "%s=%s,%s", userattr
, userid
, basedn
);
736 debug("attempting to authenticate user '%s'\n", dn
);
737 if (!bind_ld
&& !bind_once
)
738 bind_ld
= persistent_ld
;
740 bind_ld
= open_ldap_connection(ldapServer
, port
);
742 if (ldap_compare_s(bind_ld
, dn
, passwdattr
, password
) != LDAP_COMPARE_TRUE
) {
745 } else if (ldap_simple_bind_s(bind_ld
, dn
, password
) != LDAP_SUCCESS
)
747 if (bind_ld
!= persistent_ld
) {
748 ldap_unbind(bind_ld
);
755 readSecret(const char *filename
)
762 if (!(f
= fopen(filename
, "r"))) {
763 fprintf(stderr
, PROGRAM_NAME
" ERROR: Can not read secret file %s\n", filename
);
766 if (!fgets(buf
, sizeof(buf
) - 1, f
)) {
767 fprintf(stderr
, PROGRAM_NAME
" ERROR: Secret file %s is empty\n", filename
);
771 /* strip whitespaces on end */
772 if ((e
= strrchr(buf
, '\n')))
774 if ((e
= strrchr(buf
, '\r')))
777 passwd
= (char *) calloc(sizeof(char), strlen(buf
) + 1);
779 fprintf(stderr
, PROGRAM_NAME
" ERROR: can not allocate memory\n");