6 * AUTHOR: Flavio Pescuma, MARA Systems AB <flavio@marasystems.com>
10 #define LDAP_DEPRECATED 1
12 #include "ldap_backend.h"
14 #if _SQUID_MSWIN_ /* Native Windows port and MinGW */
16 #define snprintf _snprintf
20 #define LDAPAPI __cdecl
23 #ifndef LDAP_OPT_X_TLS
24 #define LDAP_OPT_X_TLS 0x6000
26 /* Some tricks to allow dynamic bind with ldap_start_tls_s entry point at
29 #undef ldap_start_tls_s
31 #define LDAP_START_TLS_S "ldap_start_tls_sW"
32 typedef WINLDAPAPI
ULONG(LDAPAPI
* PFldap_start_tls_s
) (IN PLDAP
, OUT PULONG
, OUT LDAPMessage
**, IN PLDAPControlW
*, IN PLDAPControlW
*);
34 #define LDAP_START_TLS_S "ldap_start_tls_sA"
35 typedef WINLDAPAPI
ULONG(LDAPAPI
* PFldap_start_tls_s
) (IN PLDAP
, OUT PULONG
, OUT LDAPMessage
**, IN PLDAPControlA
*, IN PLDAPControlA
*);
36 #endif /* LDAP_UNICODE */
37 PFldap_start_tls_s Win32_ldap_start_tls_s
;
38 #define ldap_start_tls_s(l,s,c) Win32_ldap_start_tls_s(l,NULL,NULL,s,c)
39 #endif /* LDAP_VERSION3 */
47 #include "edir_ldapext.h"
48 #define PROGRAM_NAME "digest_pw_auth(LDAP_backend)"
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 edir_universal_passwd
= 0;
68 static int aliasderef
= LDAP_DEREF_NEVER
;
69 #if defined(NETSCAPE_SSL)
70 static char *sslpath
= NULL
;
71 static int sslinit
= 0;
73 static int connect_timeout
= 0;
74 static int timelimit
= LDAP_NO_LIMIT
;
77 /* Added for TLS support and version 3 */
78 static int use_tls
= 0;
79 static int version
= -1;
82 static void ldapconnect(void);
83 static int readSecret(const char *filename
);
85 /* Yuck.. we need to glue to different versions of the API */
87 #if defined(LDAP_API_VERSION) && LDAP_API_VERSION > 1823
89 squid_ldap_set_aliasderef(int deref
)
91 ldap_set_option(ld
, LDAP_OPT_DEREF
, &deref
);
94 squid_ldap_set_referrals(int referrals
)
96 int *value
= static_cast<int*>(referrals
? LDAP_OPT_ON
:LDAP_OPT_OFF
);
97 ldap_set_option(ld
, LDAP_OPT_REFERRALS
, value
);
100 squid_ldap_set_timelimit(int aTimeLimit
)
102 ldap_set_option(ld
, LDAP_OPT_TIMELIMIT
, &aTimeLimit
);
105 squid_ldap_set_connect_timeout(int aTimeLimit
)
107 #if defined(LDAP_OPT_NETWORK_TIMEOUT)
109 tv
.tv_sec
= aTimeLimit
;
111 ldap_set_option(ld
, LDAP_OPT_NETWORK_TIMEOUT
, &tv
);
112 #elif defined(LDAP_X_OPT_CONNECT_TIMEOUT)
114 ldap_set_option(ld
, LDAP_X_OPT_CONNECT_TIMEOUT
, &aTimeLimit
);
120 squid_ldap_errno(LDAP
* ld
)
125 squid_ldap_set_aliasderef(int deref
)
127 ld
->ld_deref
= deref
;
130 squid_ldap_set_referrals(int referrals
)
133 ld
->ld_options
|= ~LDAP_OPT_REFERRALS
;
135 ld
->ld_options
&= ~LDAP_OPT_REFERRALS
;
138 squid_ldap_set_timelimit(int aTimeLimit
)
140 ld
->ld_timelimit
= aTimeLimit
;
143 squid_ldap_set_connect_timeout(int aTimeLimit
)
145 fprintf(stderr
, "ERROR: Connect timeouts not supported in your LDAP library\n");
148 squid_ldap_memfree(char *p
)
155 #ifdef LDAP_API_FEATURE_X_OPENLDAP
156 #if LDAP_VENDOR_VERSION > 194
157 #define HAS_URI_SUPPORT 1
162 ldap_escape_value(char *escaped
, int size
, const char *src
)
165 while (size
> 4 && *src
) {
175 snprintf(escaped
, 3, "%02x", (int) *src
++);
190 getpassword(char *login
, char *realm
)
192 LDAPMessage
*res
= NULL
;
194 char **values
= NULL
;
196 char *password
= NULL
;
199 char searchbase
[8192];
200 char *universal_password
= NULL
;
201 size_t universal_password_len
= 256;
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
);
212 debug("user filter '%s', searchbase '%s'\n", filter
, searchbase
);
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
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
));
229 fprintf(stderr
, PROGRAM_NAME
" WARNING, LDAP search error, trying to recover'%s'\n", ldap_err2string(rc
));
231 /* try to connect to the LDAP server agin, maybe my persisten conexion failed. */
243 } else if (userdnattr
) {
244 snprintf(searchbase
, 8192, "%s=%s, %s", userdnattr
, login
, userbasedn
);
247 debug("searchbase '%s'\n", searchbase
);
248 rc
= ldap_search_s(ld
, searchbase
, searchscope
, NULL
, NULL
, 0, &res
);
250 if (rc
== LDAP_SUCCESS
) {
251 entry
= ldap_first_entry(ld
, res
);
253 debug("ldap dn: %s\n", ldap_get_dn(ld
, entry
));
254 if (edir_universal_passwd
) {
256 /* allocate some memory for the universal password returned by NMAS */
257 universal_password
= (char*)calloc(1, universal_password_len
);
258 values
= (char**)calloc(1, sizeof(char *));
260 /* actually talk to NMAS to get a password */
261 nmas_res
= nds_get_password(ld
, ldap_get_dn(ld
, entry
), &universal_password_len
, universal_password
);
262 if (nmas_res
== LDAP_SUCCESS
&& universal_password
) {
263 debug("NMAS returned value %s\n", universal_password
);
264 values
[0] = universal_password
;
266 debug("Error reading Universal Password: %d = %s\n", nmas_res
, ldap_err2string(nmas_res
));
269 values
= ldap_get_values(ld
, entry
, passattr
);
276 debug("No attribute value found\n");
277 if (edir_universal_passwd
)
278 free(universal_password
);
285 if (strcmp(strtok(*value
, delimiter
), realm
) == 0) {
286 password
= strtok(NULL
, delimiter
);
295 debug("password: %s\n", password
);
297 password
= xstrdup(password
);
298 if (edir_universal_passwd
) {
300 free(universal_password
);
302 ldap_value_free(values
);
307 fprintf(stderr
, PROGRAM_NAME
" WARNING, LDAP error '%s'\n", ldap_err2string(rc
));
308 /* try to connect to the LDAP server agin, maybe my persisten conexion failed. */
329 /* On Windows ldap_start_tls_s is available starting from Windows XP,
330 * so we need to bind at run-time with the function entry point
335 HMODULE WLDAP32Handle
;
337 WLDAP32Handle
= GetModuleHandle("wldap32");
338 if ((Win32_ldap_start_tls_s
= (PFldap_start_tls_s
) GetProcAddress(WLDAP32Handle
, LDAP_START_TLS_S
)) == NULL
) {
339 fprintf(stderr
, PROGRAM_NAME
": ERROR: TLS (-Z) not supported on this platform.\n");
347 if (strstr(ldapServer
, "://") != NULL
) {
348 rc
= ldap_initialize(&ld
, ldapServer
);
349 if (rc
!= LDAP_SUCCESS
) {
350 fprintf(stderr
, "\nUnable to connect to LDAPURI:%s\n", ldapServer
);
356 if (!sslinit
&& (ldapssl_client_init(sslpath
, NULL
) != LDAP_SUCCESS
)) {
357 fprintf(stderr
, "\nUnable to initialise SSL with cert path %s\n",
363 if ((ld
= ldapssl_init(ldapServer
, port
, 1)) == NULL
) {
364 fprintf(stderr
, "\nUnable to connect to SSL LDAP server: %s port:%d\n",
370 if ((ld
= ldap_init(ldapServer
, port
)) == NULL
) {
371 fprintf(stderr
, "\nUnable to connect to LDAP server:%s port:%d\n", ldapServer
, port
);
374 squid_ldap_set_connect_timeout(connect_timeout
);
378 version
= LDAP_VERSION2
;
380 if (ldap_set_option(ld
, LDAP_OPT_PROTOCOL_VERSION
, &version
)
382 fprintf(stderr
, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
388 #ifdef LDAP_OPT_X_TLS
389 if ((version
== LDAP_VERSION3
) && (ldap_start_tls_s(ld
, NULL
, NULL
) == LDAP_SUCCESS
)) {
390 fprintf(stderr
, "Could not Activate TLS connection\n");
395 fprintf(stderr
, "TLS not supported with your LDAP library\n");
401 squid_ldap_set_timelimit(timelimit
);
402 squid_ldap_set_referrals(!noreferrals
);
403 squid_ldap_set_aliasderef(aliasderef
);
404 if (binddn
&& bindpasswd
&& *binddn
&& *bindpasswd
) {
405 rc
= ldap_simple_bind_s(ld
, binddn
, bindpasswd
);
406 if (rc
!= LDAP_SUCCESS
) {
407 fprintf(stderr
, PROGRAM_NAME
" WARNING, could not bind to binddn '%s'\n", ldap_err2string(rc
));
412 debug("Connected OK\n");
416 LDAPArguments(int argc
, char **argv
)
418 setbuf(stdout
, NULL
);
420 while (argc
> 1 && argv
[1][0] == '-') {
421 const char *value
= "";
422 char option
= argv
[1][1];
435 if (strlen(argv
[1]) > 2) {
437 } else if (argc
> 2) {
450 fprintf(stderr
, "ERROR: Your LDAP library does not have URI support\n");
453 /* Fall thru to -h */
456 int len
= strlen(ldapServer
) + 1 + strlen(value
) + 1;
457 char *newhost
= (char*)malloc(len
);
458 snprintf(newhost
, len
, "%s %s", ldapServer
, value
);
460 ldapServer
= newhost
;
462 ldapServer
= xstrdup(value
);
478 usersearchfilter
= value
;
484 if (strcmp(value
, "base") == 0)
485 searchscope
= LDAP_SCOPE_BASE
;
486 else if (strcmp(value
, "one") == 0)
487 searchscope
= LDAP_SCOPE_ONELEVEL
;
488 else if (strcmp(value
, "sub") == 0)
489 searchscope
= LDAP_SCOPE_SUBTREE
;
491 fprintf(stderr
, PROGRAM_NAME
" ERROR: Unknown search scope '%s'\n", value
);
496 #if defined(NETSCAPE_SSL)
498 if (port
== LDAP_PORT
)
501 fprintf(stderr
, PROGRAM_NAME
" ERROR: -E unsupported with this LDAP library\n");
506 connect_timeout
= atoi(value
);
509 timelimit
= atoi(value
);
512 if (strcmp(value
, "never") == 0)
513 aliasderef
= LDAP_DEREF_NEVER
;
514 else if (strcmp(value
, "always") == 0)
515 aliasderef
= LDAP_DEREF_ALWAYS
;
516 else if (strcmp(value
, "search") == 0)
517 aliasderef
= LDAP_DEREF_SEARCHING
;
518 else if (strcmp(value
, "find") == 0)
519 aliasderef
= LDAP_DEREF_FINDING
;
521 fprintf(stderr
, PROGRAM_NAME
" ERROR: Unknown alias dereference method '%s'\n", value
);
535 persistent
= !persistent
;
541 noreferrals
= !noreferrals
;
545 switch (atoi(value
)) {
547 version
= LDAP_VERSION2
;
550 version
= LDAP_VERSION3
;
553 fprintf(stderr
, "Protocol version should be 2 or 3\n");
558 if (version
== LDAP_VERSION2
) {
559 fprintf(stderr
, "TLS (-Z) is incompatible with version %d\n",
563 version
= LDAP_VERSION3
;
574 edir_universal_passwd
= 1;
577 fprintf(stderr
, PROGRAM_NAME
" ERROR: Unknown command line option '%c'\n", option
);
583 char *value
= argv
[1];
585 int len
= strlen(ldapServer
) + 1 + strlen(value
) + 1;
586 char *newhost
= (char*)malloc(len
);
587 snprintf(newhost
, len
, "%s %s", ldapServer
, value
);
589 ldapServer
= newhost
;
591 ldapServer
= xstrdup(value
);
598 ldapServer
= (char *) "localhost";
600 if (!userbasedn
|| !((passattr
!= NULL
) || (edir_universal_passwd
&& usersearchfilter
&& version
== LDAP_VERSION3
&& use_tls
))) {
601 fprintf(stderr
, "Usage: " PROGRAM_NAME
" -b basedn -f filter [options] ldap_server_name\n\n");
602 fprintf(stderr
, "\t-A password attribute(REQUIRED)\t\tUser attribute that contains the password\n");
603 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");
604 fprintf(stderr
, "\t-b basedn (REQUIRED)\t\t\tbase dn under where to search for users\n");
605 fprintf(stderr
, "\t-e Encrypted passwords(REQUIRED)\tPassword are stored encrypted using HHA1\n");
606 fprintf(stderr
, "\t-F filter\t\t\t\tuser search filter pattern. %%s = login\n");
607 fprintf(stderr
, "\t-u attribute\t\t\t\tattribute to use in combination with the basedn to create the user DN\n");
608 fprintf(stderr
, "\t-s base|one|sub\t\t\t\tsearch scope\n");
609 fprintf(stderr
, "\t-D binddn\t\t\t\tDN to bind as to perform searches\n");
610 fprintf(stderr
, "\t-w bindpasswd\t\t\t\tpassword for binddn\n");
611 fprintf(stderr
, "\t-W secretfile\t\t\t\tread password for binddn from file secretfile\n");
613 fprintf(stderr
, "\t-H URI\t\t\t\t\tLDAPURI (defaults to ldap://localhost)\n");
615 fprintf(stderr
, "\t-h server\t\t\t\tLDAP server (defaults to localhost)\n");
616 fprintf(stderr
, "\t-p port\t\t\t\t\tLDAP server port (defaults to %d)\n", LDAP_PORT
);
617 fprintf(stderr
, "\t-P\t\t\t\t\tpersistent LDAP connection\n");
618 #if defined(NETSCAPE_SSL)
619 fprintf(stderr
, "\t-E sslcertpath\t\t\t\tenable LDAP over SSL\n");
621 fprintf(stderr
, "\t-c timeout\t\t\t\tconnect timeout\n");
622 fprintf(stderr
, "\t-t timelimit\t\t\t\tsearch time limit\n");
623 fprintf(stderr
, "\t-R\t\t\t\t\tdo not follow referrals\n");
624 fprintf(stderr
, "\t-a never|always|search|find\t\twhen to dereference aliases\n");
626 fprintf(stderr
, "\t-v 2|3\t\t\t\t\tLDAP version\n");
627 fprintf(stderr
, "\t-Z\t\t\t\t\tTLS encrypt the LDAP connection, requires\n\t\t\t\tLDAP version 3\n");
629 fprintf(stderr
, "\t-S\t\t\t\t\tStrip NT domain from usernames\n");
630 fprintf(stderr
, "\t-n\t\t\t\t\tGet an eDirectory Universal Password from Novell NMAS\n\t\t\t\t\t\t(requires bind credentials, version 3, TLS, and a search filter)\n");
631 fprintf(stderr
, "\n");
632 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");
638 readSecret(const char *filename
)
644 if (!(f
= fopen(filename
, "r"))) {
645 fprintf(stderr
, PROGRAM_NAME
" ERROR: Can not read secret file %s\n", filename
);
648 if (!fgets(buf
, sizeof(buf
) - 1, f
)) {
649 fprintf(stderr
, PROGRAM_NAME
" ERROR: Secret file %s is empty\n", filename
);
653 /* strip whitespaces on end */
654 if ((e
= strrchr(buf
, '\n')))
656 if ((e
= strrchr(buf
, '\r')))
659 bindpasswd
= xstrdup(buf
);
661 fprintf(stderr
, PROGRAM_NAME
" ERROR: can not allocate memory\n");
669 LDAPHHA1(RequestData
* requestData
)
673 password
= getpassword(requestData
->user
, requestData
->realm
);
674 if (password
!= NULL
) {
676 xstrncpy(requestData
->HHA1
, password
, sizeof(requestData
->HHA1
));
679 DigestCalcHA1("md5", requestData
->user
, requestData
->realm
, password
, NULL
, NULL
, HA1
, requestData
->HHA1
);
683 requestData
->error
= -1;