diff --git a/README.md b/README.md index 06bd0f2..81fd171 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,30 @@ # LDAP Authentication module for nginx + LDAP module for nginx which supports authentication against multiple LDAP servers. -# How to install +## Project history + +This project is a clone of [nginx-auth-ldap](https://github.com/kvspb/nginx-auth-ldap) original module from [kvspb](https://github.com/kvspb). + +The reasons for this fork are: + +* The original project seems abondonned (no commit since 2 years). +* Inherit from other contributors fixes/features: + * [Pull request #237](https://github.com/kvspb/nginx-auth-ldap/pull/237) from [mmguero-dev](https://github.com/mmguero-dev/nginx-auth-ldap). + * Compatible with Nginx 1.23.0 (http headers are now linked). +* Add new features: + * Add the use of `resolver` to resolve hostname of the LDAP server. + * Support LDAP attributes fecthing during search. + * Added an `encoding` attribute to the binddn_passwd parameter. + * Manage connections waiting a reconnect delay in a specific queue, so that we can + cancel the reconnect delay when a new request ask for an authentication and no free + connection is available, but some are waiting to re-connect. + * Fix the usage of `max_down_retries` parameter + * Add the `clean_on_timeout` option + +## How to install -## FreeBSD +### FreeBSD ```bash cd /usr/ports/www/nginx && make config install clean @@ -11,15 +32,14 @@ cd /usr/ports/www/nginx && make config install clean Check HTTP_AUTH_LDAP options - -``` +```text [*] HTTP_AUTH_LDAP 3rd party http_auth_ldap module ``` -## Linux +### Linux ```bash -cd ~ && git clone https://github.com/kvspb/nginx-auth-ldap.git +cd ~ && git clone https://github.com/Ericbla/nginx-auth-ldap.git ``` in nginx source folder @@ -29,11 +49,14 @@ in nginx source folder make install ``` -# Example configuration +## Example configuration + Define list of your LDAP servers with required user/group requirements: ```bash http { + auth_ldap_resolver 8.8.8.8; + ldap_server test1 { url ldap://192.168.0.1:3268/DC=test,DC=local?sAMAccountName?sub?(objectClass=person); binddn "TEST\\LDAPUSER"; @@ -55,6 +78,7 @@ Define list of your LDAP servers with required user/group requirements: ``` And add required servers in correct order into your location/server directive: + ```bash server { listen 8000; @@ -62,7 +86,7 @@ And add required servers in correct order into your location/server directive: auth_ldap "Forbidden"; auth_ldap_servers test1; - auth_ldap_servers test2; + auth_ldap_servers test2; location / { root html; @@ -72,49 +96,151 @@ And add required servers in correct order into your location/server directive: } ``` -# Available config parameters +## Available config parameters + +### auth_ldap_cache_enabled + +* Syntax: auth_ldap_cache_enabled on | off; +* Default: auth_ldap_cache_enabled off; +* Context: http + +### auth_ldap_cache_expiration_time + +* Syntax: auth_ldap_cache_expiration_time time; +* Default: auth_ldap_cache_expiration_time 10s; +* Context: http + +Cache expiration time (see for time intervals syntax). + +### auth_ldap_cache_size + +* Syntax: auth_ldap_cache_size size; +* Default: auth_ldap_cache_size 100; +* Context: http + +Number of cached LDAP authentications (min 100) + +### auth_ldap_servers_size + +* Syntax: auth_ldap_servers_size size; +* Syntax: auth_ldap_servers_size 7; +* Context: http + +Maximum number of `ldap_server` elements to support + +### auth_ldap + +* Syntax: auth_ldap off | _realm_; +* Default: -- +* Context: http, server, loc, limit_expect + +Set the _realm_ to be used with the `WWW-Authenticate` response header when authentication failed or is missing. + +### auth_ldap_servers + +* Syntax: auth_ldap_servers _name_; +* Default: -- +* Context: http, server, loc, limit_expect + +Select the server _name_ to work with user authentication + +### auth_ldap_resolver + +* Syntax: auth_ldap_resolver _address_ ... [valid=time] [ipv4=on|off] [ipv6=on|off] [status_zone=zone]; +* Default: -- +* Context: http + +The resolver to use as a fallback when the system hostname resolution +(gethostbyname()) can't resolve the LDAP server hostname. +See the `resolver` directive of the **ngx_http_core_module** + +### auth_ldap_resolver_timeout + +* Syntax: auth_ldap_resolver_timeout time; +* Default: auth_ldap_resolver_timeout 10s; +* Context: http + +Resolver requests timeout (see for time intervals syntax). -## url -expected value: string +### ldap_server -Available URL schemes: ldap://, ldaps:// +* Syntax: ldap_server _name_ { ... } +* Default: none +* Context: http -## binddn -expected value: string +## Configuration parameters for the `ldap_server` block -## binddn_passwd -expected value: string +### url -## group_attribute -expected value: string +* Syntax: url _url_; +* Default: -- +* Context: `ldap_server` block -## group_attribute_is_dn -expected value: on or off, default off +url format: ldap[s]://host[:port]/dn?attrs?scope?filter[?exts] -## require -expected value: valid_user, user, group +### binddn -## satisfy -expected value: all, any +* Syntax: binddn _dn_; +* Default: -- +* Context: `ldap_server` block -## max_down_retries -expected value: a number, default 0 +The DN for the initial bind + +### binddn_passwd + +* Syntax: binddn_passwd _password_ [text | base64 | hex]; +* Default: -- +* Context: `ldap_server` block + +The initial bind password. can be encoded in clear text (the default) or be encoded in base64 or HEX representation + +### group_attribute + +* Syntax: group attr; +* Default: -- +* Context: `ldap_server` block + +### group_attribute_is_dn + +* Syntax: group_attribute_is_dn on | off; +* Default: group_attribute_is_dn off; +* Context: `ldap_server` block + +Tell to search for full DN in member object. + +### require + +* Syntax: require valid_user | user | group; +* Default: --; +* Context: `ldap_server` block + +### satisfy + +* Syntax: satisfy all | any; +* Default: --; +* Context: `ldap_server` block + +### max_down_retries + +* Syntax: max_down_retries _number_; +* Default: max_down_retries 0; +* Context: `ldap_server` block Retry count for attempting to reconnect to an LDAP server if it is considered -"DOWN". This may happen if a KEEP-ALIVE connection to an LDAP server times +"DOWN". This may happen if a KEEP-ALIVE connection to an LDAP server times out or is terminated by the server end after some amount of time. This can usually help with the following error: -``` +```text http_auth_ldap: ldap_result() failed (-1: Can't contact LDAP server) ``` -## connections -expected value: a number greater than 0 +### ssl_check_cert -## ssl_check_cert -expected value: on or off, default off +* Syntax: ssl_check_cert on | chain | off; +* Default: ssl_check_cert off; +* Context: `ldap_server` block Verify the remote certificate for LDAPs connections. If disabled, any remote certificate will be accepted which exposes you to possible man-in-the-middle attacks. Note that the server's @@ -123,23 +249,74 @@ See below how to trust CAs without installing them system-wide. This options needs OpenSSL >= 1.0.2; it is unavailable if compiled with older versions. -## ssl_ca_file -expected value: file path +When `chain` is given, verify cert chain but not hostname/IP in SAN + +### ssl_ca_file + +* Syntax: ssl_ca_file _file-path_; +* Default: --; +* Context: `ldap_server` block Trust the CA certificate in this file (see ssl_check_cert above). -## ssl_ca_dir -expected value: directory path +### ssl_ca_dir + +* Syntax: ssl_ca_file _dir-path_; +* Default: --; +* Context: `ldap_server` block Trust all CA certificates in this directory (see ssl_check_cert above). Note that you need to provide hash-based symlinks in the directory for this to work; you'll basically need to run OpenSSL's c_rehash command in this directory. -## referral -expected value: on, off +### referral + +* Syntax: referral on | off; +* Default: referral on; +* Context: `ldap_server` block LDAP library default is on. This option disables usage of referral messages from LDAP server. Usefull for authenticating against read only AD server without access to read write. +### attribute_header_prefix + +* Syntax: attribute_header_prefix _string_; +* Default: attribute_header_prefix X-LDAP-ATTRS-; +* Context: `ldap_server` block + +The prefix for the HEADER names used to carry the feteched attributes (default: "X-LDAP-ATTRS-") + +### search_attributes + +* Syntax: search_attributes _attr1_ [ [ _attr2_ ] ... [ _attrN_ ] ]; +* Default: -- +* Context: `ldap_server` block + +Space delimited list of LDAP attribute descriptions to include in the search (require valid-user or require user). Each attribute value will be return as a HTTP header () in the authentication response. + +### reconnect_timeout + +* Syntax: reconnect_timeout _timespec_; +* Default: reconnect_timeout 10s; +* Context: `ldap_server` block + +The delay before reconnection attempts (see for _timespec_ syntax) + +### connections + +* Syntax: connections _count_; +* Default: connections 1; +* Context: `ldap_server` block + +The number of connections to the server use in // + +### clean_on_timeout + +* Syntax: clean_on_timeout on | off; +* Default: clean_on_timeout off; +* Context: `ldap_server` block + +Tell the module to shutdown an re-connect a LDAP server connection after a +send timeout detected (instead of just marking the connection as free again). diff --git a/config b/config index 2da8d99..8d49eed 100644 --- a/config +++ b/config @@ -13,6 +13,7 @@ if test -n "$ngx_module_link"; then ngx_module_name=ngx_http_auth_ldap_module ngx_module_incs= ngx_module_deps= + ngx_module_order="ngx_http_auth_ldap_module ngx_http_access_module" ngx_module_srcs="$ngx_addon_dir/ngx_http_auth_ldap_module.c" ngx_module_libs="$LDAP_REQUIRED_LIBS" . auto/module diff --git a/example.conf b/example.conf index 1512b2d..2cee536 100644 --- a/example.conf +++ b/example.conf @@ -11,6 +11,9 @@ http { sendfile on; keepalive_timeout 65; + auth_ldap_resolver 8.8.8.8; + auth_ldap_cache_enabled on; + # define ldap server ldap_server ad_1 { # user search base. @@ -18,7 +21,11 @@ http { # bind as binddn "CN=Operator,OU=Service Accounts,DC=company,DC=com"; # bind pw - binddn_passwd ; + binddn_passwd ""; + # Or binddn_passwd "base64()" base64; + # Or binddn_passwd "hex()" hex; + # Select attributes to be retrieved during the search (space separated list of attributes) + search_attributes mail sn givenName; # group attribute name which contains member object group_attribute member; # search for full DN in member object diff --git a/ngx_http_auth_ldap_module.c b/ngx_http_auth_ldap_module.c index 2f4e592..55355bd 100644 --- a/ngx_http_auth_ldap_module.c +++ b/ngx_http_auth_ldap_module.c @@ -32,6 +32,7 @@ #include #include #include +//#include // used for manual warnings #define XSTR(x) STR(x) @@ -69,6 +70,26 @@ extern int ldap_init_fd(ber_socket_t fd, int proto, const char *url, LDAP **ld); #define NGX_HTTP_AUTH_LDAP_MAX_SERVERS_SIZE 7 +#define SSL_CERT_VERIFY_OFF 0 +#define SSL_CERT_VERIFY_FULL 1 +#define SSL_CERT_VERIFY_CHAIN 2 + +#define LDAP_ATTR_HEADER_DEFAULT_PREFIX "X-LDAP-ATTR-" + +#define SANITIZE_NO_CONV 0 +#define SANITIZE_TO_UPPER 1 +#define SANITIZE_TO_LOWER 2 + +#define ENCODING_TEXT 0 +#define ENCODING_B64 1 +#define ENCODING_HEX 2 + +#define DEFAULT_ATTRS_COUNT 3 /* The default number of search attributes */ +#define MAX_ATTRS_COUNT 5 /* Maximum search attributes to display in log */ + +#define RECONNECT_ASAP_MS 1000 /* Delay (in ms) for LDAP reconnection (when we want ASAP reconnect) */ + + typedef struct { LDAPURLDesc *ludpp; @@ -92,6 +113,7 @@ typedef struct { ngx_http_complex_value_t require_valid_user_dn; ngx_flag_t satisfy_all; ngx_flag_t referral; + ngx_flag_t clean_on_timeout; ngx_uint_t connections; ngx_uint_t max_down_retries; @@ -100,8 +122,12 @@ typedef struct { ngx_msec_t reconnect_timeout; ngx_msec_t bind_timeout; ngx_msec_t request_timeout; - ngx_queue_t free_connections; - ngx_queue_t waiting_requests; + ngx_queue_t free_connections; /* Queue of free (ready) connections */ + ngx_queue_t waiting_requests; /* Queue of ctx with not finished requests */ + + ngx_queue_t pending_reconnections; /* Queue of pending connections (waiting re-connect) */ + char **attrs; /* Search attributes formated for ldap_search_ext() */ + ngx_str_t attribute_header_prefix; } ngx_http_auth_ldap_server_t; typedef struct { @@ -113,6 +139,9 @@ typedef struct { #if (NGX_OPENSSL) ngx_ssl_t ssl; #endif + ngx_msec_t resolver_timeout; /* resolver_timeout */ + ngx_resolver_t *resolver; /* resolver */ + ngx_pool_t *cnf_pool; } ngx_http_auth_ldap_main_conf_t; typedef struct { @@ -120,11 +149,18 @@ typedef struct { ngx_array_t *servers; /* array of ngx_http_auth_ldap_server_t* */ } ngx_http_auth_ldap_loc_conf_t; + +typedef struct { + ngx_str_t attr_name; + ngx_str_t attr_value; +} ldap_search_attribute_t; + typedef struct { uint32_t small_hash; /* murmur2 hash of username ^ &server */ uint32_t outcome; /* OUTCOME_DENY or OUTCOME_ALLOW */ ngx_msec_t time; /* ngx_current_msec when created */ u_char big_hash[16]; /* md5 hash of (username, server, password) */ + ngx_array_t attributes; /* Attributes (ldap_search_attribute_t) retreived during the search */ } ngx_http_auth_ldap_cache_elt_t; typedef struct { @@ -132,6 +168,7 @@ typedef struct { ngx_uint_t num_buckets; ngx_uint_t elts_per_bucket; ngx_msec_t expiration_time; + ngx_pool_t *pool; } ngx_http_auth_ldap_cache_t; typedef enum { @@ -151,9 +188,10 @@ typedef struct { ngx_http_auth_ldap_request_phase_t phase; unsigned int iteration; int outcome; + ngx_array_t attributes; /* Attributes (ldap_search_attribute_t) retreived during the search */ struct ngx_http_auth_ldap_connection *c; - ngx_queue_t queue; + ngx_queue_t queue; /* Queue element to be chained in server->waiting_requests queue */ int replied; int error_code; ngx_str_t error_msg; @@ -173,11 +211,13 @@ typedef enum { STATE_READY, STATE_BINDING, STATE_SEARCHING, - STATE_COMPARING + STATE_COMPARING, + STATE_DISCONNECTING } ngx_http_auth_ldap_connection_state_t; typedef struct ngx_http_auth_ldap_connection { ngx_log_t *log; + ngx_http_auth_ldap_main_conf_t *main_cnf; ngx_http_auth_ldap_server_t *server; ngx_peer_connection_t conn; ngx_event_t reconnect_event; @@ -187,19 +227,24 @@ typedef struct ngx_http_auth_ldap_connection { ngx_ssl_t *ssl; #endif - ngx_queue_t queue; + ngx_queue_t queue; /* Queue element to be chained in server->free_connections queue */ + ngx_queue_t queue_pending; /* Queue element to be chained in server->pending_reconnections queue */ ngx_http_auth_ldap_ctx_t *rctx; LDAP* ld; ngx_http_auth_ldap_connection_state_t state; int msgid; + ngx_resolver_ctx_t *resolver_ctx; + ngx_uint_t cnx_idx; /* index of the connection from 0 to server->connections -1 */ } ngx_http_auth_ldap_connection_t; static char * ngx_http_auth_ldap_ldap_server_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static char * ngx_http_auth_ldap_ldap_server(ngx_conf_t *cf, ngx_command_t *dummy, void *conf); static char * ngx_http_auth_ldap(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static char * ngx_http_auth_ldap_servers(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); +static char * ngx_http_auth_ldap_resolver(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static char * ngx_http_auth_ldap_parse_url(ngx_conf_t *cf, ngx_http_auth_ldap_server_t *server); +static char * ngx_http_auth_ldap_parse_binddn_passwd(ngx_conf_t *cf, ngx_http_auth_ldap_server_t *server); static char * ngx_http_auth_ldap_parse_require(ngx_conf_t *cf, ngx_http_auth_ldap_server_t *server); static char * ngx_http_auth_ldap_parse_satisfy(ngx_conf_t *cf, ngx_http_auth_ldap_server_t *server); static char * ngx_http_auth_ldap_parse_referral(ngx_conf_t *cf, ngx_http_auth_ldap_server_t *server); @@ -210,9 +255,15 @@ static char * ngx_http_auth_ldap_merge_loc_conf(ngx_conf_t *, void *, void *); static ngx_int_t ngx_http_auth_ldap_init_worker(ngx_cycle_t *cycle); static ngx_int_t ngx_http_auth_ldap_init(ngx_conf_t *cf); static ngx_int_t ngx_http_auth_ldap_init_cache(ngx_cycle_t *cycle); -static void ngx_http_auth_ldap_close_connection(ngx_http_auth_ldap_connection_t *c); +static void ngx_http_auth_ldap_close_connection(ngx_http_auth_ldap_connection_t *c, int retry_asap); +static void ngx_http_auth_ldap_set_pending_reconnection(ngx_http_auth_ldap_connection_t *c, ngx_msec_t reconnect_delay); static void ngx_http_auth_ldap_read_handler(ngx_event_t *rev); +static void ngx_http_auth_ldap_connect(ngx_http_auth_ldap_connection_t *c); +static void ngx_http_auth_ldap_connect_continue(ngx_http_auth_ldap_connection_t *c); static void ngx_http_auth_ldap_reconnect_handler(ngx_event_t *); +static void ngx_http_auth_ldap_reconnect_from_connection(ngx_http_auth_ldap_connection_t *c); +static void ngx_http_auth_ldap_resolve_handler(ngx_resolver_ctx_t *ctx); +static ngx_int_t ngx_http_auth_ldap_init_servers(ngx_cycle_t *cycle); static ngx_int_t ngx_http_auth_ldap_init_connections(ngx_cycle_t *cycle); static ngx_int_t ngx_http_auth_ldap_handler(ngx_http_request_t *r); static ngx_int_t ngx_http_auth_ldap_authenticate(ngx_http_request_t *r, ngx_http_auth_ldap_ctx_t *ctx, @@ -225,6 +276,10 @@ static ngx_int_t ngx_http_auth_ldap_recover_bind(ngx_http_request_t *r, ngx_http #if (NGX_OPENSSL) static ngx_int_t ngx_http_auth_ldap_restore_handlers(ngx_connection_t *conn); #endif +static u_char * santitize_str(u_char *str, ngx_uint_t conv); +static ngx_uint_t my_hex_digit_value(u_char c); +static ngx_uint_t my_hex_decode(ngx_str_t *dst, ngx_str_t *src); +static void my_free_addrs_from_url(ngx_pool_t *pool, ngx_url_t *u); ngx_http_auth_ldap_cache_t ngx_http_auth_ldap_cache; @@ -269,6 +324,22 @@ static ngx_command_t ngx_http_auth_ldap_commands[] = { offsetof(ngx_http_auth_ldap_main_conf_t, servers_size), NULL }, + { + ngx_string("auth_ldap_resolver"), + NGX_HTTP_MAIN_CONF | NGX_CONF_1MORE, + ngx_http_auth_ldap_resolver, + NGX_HTTP_MAIN_CONF_OFFSET, + 0, + NULL + }, + { + ngx_string("auth_ldap_resolver_timeout"), + NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE1, + ngx_conf_set_msec_slot, + NGX_HTTP_MAIN_CONF_OFFSET, + offsetof(ngx_http_auth_ldap_main_conf_t, resolver_timeout), + NULL + }, { ngx_string("auth_ldap"), NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LMT_CONF | NGX_CONF_TAKE1, @@ -315,6 +386,71 @@ ngx_module_t ngx_http_auth_ldap_module = { }; + +/*** Tools ***/ + +static u_char * santitize_str(u_char *str, ngx_uint_t conv) { + static u_char sanitizeTable[256] = {0xff}; + ngx_uint_t i; + u_char *p; + + if (str == NULL || *str == '\0') { + return str; + } + + // Initialize un-initialized table at first use + if (sanitizeTable[0] == 0xff) { + // Initialize un initialized table once + for (i = 0; i < sizeof(sanitizeTable); i++) { + // Convert any char other than ALPHA / DIGIT / "-" / "_" to the "_" char + sanitizeTable[i] = (isalnum(i) || i == '-' || i == '_') ? i : '_'; + } + } + + for (p = str; *p; p++) { + if (conv == SANITIZE_TO_UPPER && *p >= 97 && *p <= 122) { + // lowercase to uppercase + *p -= 32; + } else if (conv == SANITIZE_TO_LOWER && *p >= 65 && *p <= 90) { + // lowercase to uppercase + *p += 32; + } else { + *p = sanitizeTable[(int)*p]; + } + } + return str; // Fluent interface +} + + +static ngx_uint_t +my_hex_digit_value(u_char c) +{ + if (c >= '0' && c <= '9') { + return (ngx_uint_t)(c - '0'); + } + if (c >= 'a' && c <= 'f') { + return (ngx_uint_t)(c - 'a' + 10); + } + if (c >= 'A' && c <= 'F') { + return (ngx_uint_t)(c - 'A' + 10); + } + return 0; // Not an hex digit +} + +static ngx_uint_t +my_hex_decode(ngx_str_t *dst, ngx_str_t *src) +{ + u_char *s, *d; + ngx_uint_t i; + + for (i = 0, s = src->data, d = dst->data ; i < src->len -1; i += 2, s += 2) { + *d++ = (u_char)(16 * my_hex_digit_value(*s) + my_hex_digit_value(*(s + 1))); + } + + dst->len = (d - dst->data); + return (ngx_uint_t)dst->len; +} + /*** Configuration and initialization ***/ /** @@ -338,7 +474,7 @@ ngx_http_auth_ldap_ldap_server_block(ngx_conf_t *cf, ngx_command_t *cmd, void *c return NGX_CONF_ERROR; } - if (cnf->servers == NULL) { + if (cnf->servers == NULL) { if (cnf->servers_size == NGX_CONF_UNSET) { cnf->servers_size = NGX_HTTP_AUTH_LDAP_MAX_SERVERS_SIZE; } @@ -356,11 +492,15 @@ ngx_http_auth_ldap_ldap_server_block(ngx_conf_t *cf, ngx_command_t *cmd, void *c ngx_memzero(server, sizeof(*server)); server->connect_timeout = 10000; server->reconnect_timeout = 10000; + server->max_down_retries = 0; server->bind_timeout = 5000; server->request_timeout = 10000; server->alias = name; server->referral = 1; - + server->clean_on_timeout = 0; + server->attribute_header_prefix.len = sizeof(LDAP_ATTR_HEADER_DEFAULT_PREFIX) -1; + server->attribute_header_prefix.data = (u_char *)LDAP_ATTR_HEADER_DEFAULT_PREFIX; + server->attrs = NULL; save = *cf; cf->handler = ngx_http_auth_ldap_ldap_server; cf->handler_conf = conf; @@ -406,17 +546,33 @@ ngx_http_auth_ldap_ldap_server(ngx_conf_t *cf, ngx_command_t *dummy, void *conf) } else if (ngx_strcmp(value[0].data, "binddn") == 0) { server->bind_dn = value[1]; } else if (ngx_strcmp(value[0].data, "binddn_passwd") == 0) { - server->bind_dn_passwd = value[1]; + return ngx_http_auth_ldap_parse_binddn_passwd(cf, server); } else if (ngx_strcmp(value[0].data, "group_attribute") == 0) { server->group_attribute = value[1]; } else if (ngx_strcmp(value[0].data, "group_attribute_is_dn") == 0 && ngx_strcmp(value[1].data, "on") == 0) { server->group_attribute_dn = 1; + } else if (ngx_strcmp(value[0].data, "search_attributes") == 0 && cf->args->nelts >= 2) { + ngx_uint_t j, attrs_count = cf->args->nelts -1; + server->attrs = ngx_palloc(cf->pool, (attrs_count + 1) * sizeof(char *)); + for (j = 0; j < attrs_count; j++) { + server->attrs[j] = (char *)value[j + 1].data; + } + server->attrs[attrs_count] = NULL; // Last element of the list + } else if (ngx_strcmp(value[0].data, "attribute_header_prefix") == 0 && cf->args->nelts >= 2) { + santitize_str(value[1].data, SANITIZE_NO_CONV); // The prefix is sanitized + server->attribute_header_prefix = value[1]; } else if (ngx_strcmp(value[0].data, "require") == 0) { return ngx_http_auth_ldap_parse_require(cf, server); } else if (ngx_strcmp(value[0].data, "satisfy") == 0) { return ngx_http_auth_ldap_parse_satisfy(cf, server); } else if (ngx_strcmp(value[0].data, "referral") == 0) { return ngx_http_auth_ldap_parse_referral(cf, server); + } else if (ngx_strcmp(value[0].data, "clean_on_timeout") == 0) { + if (ngx_strcasecmp(value[1].data, (u_char *) "on") == 0) { + server->clean_on_timeout = 1; + } else { + server->clean_on_timeout = 0; + } } else if (ngx_strcmp(value[0].data, "max_down_retries") == 0) { i = ngx_atoi(value[1].data, value[1].len); if (i == NGX_ERROR) { @@ -431,18 +587,24 @@ ngx_http_auth_ldap_ldap_server(ngx_conf_t *cf, ngx_command_t *dummy, void *conf) return NGX_CONF_ERROR; } server->connections = i; - } else if (ngx_strcmp(value[0].data, "ssl_check_cert") == 0 && ngx_strcmp(value[1].data, "on") == 0) { - #if OPENSSL_VERSION_NUMBER >= 0x10002000 - server->ssl_check_cert = 1; - #else - #if GNUC > 4 - #warning "http_auth_ldap: Compiling with OpenSSL < 1.0.2, certificate verification will be unavailable. OPENSSL_VERSION_NUMBER == " XSTR(OPENSSL_VERSION_NUMBER) - #endif - ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, - "http_auth_ldap: 'ssl_cert_check': cannot verify remote certificate's domain name because " - "your version of OpenSSL is too old. " - "Please install OpenSSL >= 1.02 and recompile nginx."); - #endif + } else if (ngx_strcmp(value[0].data, "ssl_check_cert") == 0) { + #if OPENSSL_VERSION_NUMBER >= 0x10002000 + if ((ngx_strcmp(value[1].data, "on") == 0) || (ngx_strcmp(value[1].data, "full") == 0)) { + server->ssl_check_cert = SSL_CERT_VERIFY_FULL; + } else if (ngx_strcmp(value[1].data, "chain") == 0) { + server->ssl_check_cert = SSL_CERT_VERIFY_CHAIN; + } else { + server->ssl_check_cert = SSL_CERT_VERIFY_OFF; + } + #else + #if GNUC > 4 + #warning "http_auth_ldap: Compiling with OpenSSL < 1.0.2, certificate verification will be unavailable. OPENSSL_VERSION_NUMBER == " XSTR(OPENSSL_VERSION_NUMBER) + #endif + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, + "http_auth_ldap: 'ssl_cert_check': cannot verify remote certificate's domain name because " + "your version of OpenSSL is too old. " + "Please install OpenSSL >= 1.0.2 and recompile nginx."); + #endif } else if (ngx_strcmp(value[0].data, "ssl_ca_dir") == 0) { server->ssl_ca_dir = value[1]; } else if (ngx_strcmp(value[0].data, "ssl_ca_file") == 0) { @@ -507,12 +669,11 @@ ngx_http_auth_ldap_servers(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) for (i = 1; i < cf->args->nelts; i++) { value = &((ngx_str_t *) cf->args->elts)[i]; server = NULL; - - if (mconf->servers == NULL) { + if (mconf->servers == NULL) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "http_auth_ldap: Using \"auth_ldap_servers\" when no \"ldap_server\" has been previously defined" - " (make sure that \"auth_ldap_servers\" goes after \"ldap_server\"s in your configuration file)", value); + " (make sure that \"auth_ldap_servers\" goes after \"ldap_server\"s in your configuration file)", value); return NGX_CONF_ERROR; - } + } for (j = 0; j < mconf->servers->nelts; j++) { s = &((ngx_http_auth_ldap_server_t *) mconf->servers->elts)[j]; @@ -546,6 +707,33 @@ ngx_http_auth_ldap_servers(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_OK; } +/** + * Parse resolver directive + */ +static char * +ngx_http_auth_ldap_resolver(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) +{ + ngx_http_auth_ldap_main_conf_t *cnf = conf; + ngx_str_t *value; + + if (cnf->resolver) { + return "is duplicate"; + } + + value = cf->args->elts; + + cnf->resolver = ngx_resolver_create(cf, &value[1], cf->args->nelts - 1); + if (cnf->resolver == NULL) { + return NGX_CONF_ERROR; + } + + ngx_conf_log_error(NGX_LOG_DEBUG, cf, 0, + "ngx_http_auth_ldap_resolver: Configured resolver %V", + &value[1]); + + return NGX_CONF_OK; +} + /** * Parse URL conf parameter */ @@ -618,6 +806,7 @@ ngx_http_auth_ldap_parse_url(ngx_conf_t *cf, ngx_http_auth_ldap_server_t *server server->parsed_url.url.data = (u_char *) server->ludpp->lud_host; server->parsed_url.url.len = ngx_strlen(server->ludpp->lud_host); server->parsed_url.default_port = server->ludpp->lud_port; + server->parsed_url.no_resolve = 1; // Do not use hostanme resolution checking during configuration parsing if (ngx_parse_url(cf->pool, &server->parsed_url) != NGX_OK) { if (server->parsed_url.err) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "http_auth_ldap: %s in LDAP hostname \"%V\"", @@ -625,7 +814,7 @@ ngx_http_auth_ldap_parse_url(ngx_conf_t *cf, ngx_http_auth_ldap_server_t *server } return NGX_CONF_ERROR; } - + server->parsed_url.no_resolve = 0; // Reset the no_resolve flag if (ngx_strcmp(server->ludpp->lud_scheme, "ldap") == 0) { return NGX_CONF_OK; #if (NGX_OPENSSL) @@ -646,6 +835,55 @@ ngx_http_auth_ldap_parse_url(ngx_conf_t *cf, ngx_http_auth_ldap_server_t *server } } +static char * +ngx_http_auth_ldap_parse_binddn_passwd(ngx_conf_t *cf, ngx_http_auth_ldap_server_t *server) +{ + ngx_str_t *value; + ngx_int_t encoding = ENCODING_TEXT; + + value = cf->args->elts; + if (cf->args->nelts < 2 || cf->args->nelts > 3) { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "http_auth_ldap: Bad number of parameters for binddn_passwd"); + return NGX_CONF_ERROR; + } + + if (cf->args->nelts == 3 && value[1].len != 0) { + // Non empty password and have a second parameter value (encoding) + if (ngx_strcmp(value[2].data, "text") == 0) { + encoding = ENCODING_TEXT; + } else if (ngx_strcmp(value[2].data, "base64") == 0) { + encoding = ENCODING_B64; + } else if (ngx_strcmp(value[2].data, "hex") == 0) { + encoding = ENCODING_HEX; + } else { + ngx_conf_log_error(NGX_LOG_WARN, cf, 0, + "http_auth_ldap: Unknown encoding ('%V') for binddn_password. (assumming clear text)", + value[2]); + } + } + + if (encoding == ENCODING_TEXT) { + // Just copy reference + server->bind_dn_passwd = value[1]; + } else if (encoding == ENCODING_B64) { + // Base 64 decode + server->bind_dn_passwd.len = 0; + int decoded_length = 3 * value[1].len / 4; + server->bind_dn_passwd.data = ngx_pnalloc(cf->pool, decoded_length + 1); + ngx_decode_base64(&server->bind_dn_passwd, &value[1]); + server->bind_dn_passwd.data[decoded_length] = '\0'; + } else if (encoding == ENCODING_HEX) { + // Hex decode + server->bind_dn_passwd.len = 0; + int decoded_length = value[1].len / 2; + server->bind_dn_passwd.data = ngx_pnalloc(cf->pool, decoded_length + 1); + my_hex_decode(&server->bind_dn_passwd, &value[1]); + server->bind_dn_passwd.data[decoded_length] = '\0'; + } + + return NGX_CONF_OK; +} + /** * Parse "require" conf parameter */ @@ -657,7 +895,7 @@ ngx_http_auth_ldap_parse_require(ngx_conf_t *cf, ngx_http_auth_ldap_server_t *se ngx_http_compile_complex_value_t ccv; value = cf->args->elts; - ngx_conf_log_error(NGX_LOG_NOTICE, cf, 0, "http_auth_ldap: parse_require"); + //ngx_conf_log_error(NGX_LOG_DEBUG | NGX_LOG_DEBUG_HTTP, cf, 0, "http_auth_ldap: parse_require"); if (ngx_strcmp(value[1].data, "valid_user") == 0) { server->require_valid_user = 1; @@ -749,6 +987,8 @@ ngx_http_auth_ldap_parse_referral(ngx_conf_t *cf, ngx_http_auth_ldap_server_t *s return NGX_CONF_ERROR; } + + /** * Create main config which will store ldap_servers array */ @@ -762,10 +1002,13 @@ ngx_http_auth_ldap_create_main_conf(ngx_conf_t *cf) return NULL; } + conf->cnf_pool = cf->pool; conf->cache_enabled = NGX_CONF_UNSET; conf->cache_expiration_time = NGX_CONF_UNSET_MSEC; conf->cache_size = NGX_CONF_UNSET_SIZE; conf->servers_size = NGX_CONF_UNSET; + conf->resolver_timeout = NGX_CONF_UNSET_MSEC; + conf->resolver = NULL; return conf; } @@ -775,6 +1018,10 @@ ngx_http_auth_ldap_init_main_conf(ngx_conf_t *cf, void *parent) { ngx_http_auth_ldap_main_conf_t *conf = parent; + if (conf->resolver_timeout == NGX_CONF_UNSET_MSEC) { + conf->resolver_timeout = 10000; + } + if (conf->cache_enabled == NGX_CONF_UNSET) { conf->cache_enabled = 0; } @@ -843,6 +1090,13 @@ ngx_http_auth_ldap_init_worker(ngx_cycle_t *cycle) return NGX_OK; } + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cycle->log, 0, "ngx_http_auth_ldap_init_worker:"); + + rc = ngx_http_auth_ldap_init_servers(cycle); + if (rc != NGX_OK) { + return rc; + } + rc = ngx_http_auth_ldap_init_cache(cycle); if (rc != NGX_OK) { return rc; @@ -890,6 +1144,9 @@ ngx_http_auth_ldap_init_cache(ngx_cycle_t *cycle) 577, 641, 701, 769, 839, 911, 983, 1049, 1109 }; + cache = &ngx_http_auth_ldap_cache; + cache->pool = cycle->pool; + conf = (ngx_http_auth_ldap_main_conf_t *) ngx_http_cycle_get_module_main_conf(cycle, ngx_http_auth_ldap_module); if (conf == NULL || !conf->cache_enabled) { return NGX_OK; @@ -904,13 +1161,13 @@ ngx_http_auth_ldap_init_cache(ngx_cycle_t *cycle) } } - cache = &ngx_http_auth_ldap_cache; + cache->expiration_time = conf->cache_expiration_time; cache->num_buckets = count; cache->elts_per_bucket = 8; - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cycle->log, 0, "http_auth_ldap: Allocating %ud bytes of LDAP cache.", - cache->num_buckets * cache->elts_per_bucket * sizeof(ngx_http_auth_ldap_cache_elt_t)); + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cycle->log, 0, "http_auth_ldap: Allocating %ud bytes of LDAP cache (ttl=%dms).", + cache->num_buckets * cache->elts_per_bucket * sizeof(ngx_http_auth_ldap_cache_elt_t), cache->expiration_time); cache->buckets = (ngx_http_auth_ldap_cache_elt_t *) ngx_calloc(count * 8 * sizeof(ngx_http_auth_ldap_cache_elt_t), cycle->log); if (cache->buckets == NULL) { @@ -918,6 +1175,12 @@ ngx_http_auth_ldap_init_cache(ngx_cycle_t *cycle) return NGX_ERROR; } + /* Initialize the attributes array in each cache entry */ + ngx_http_auth_ldap_cache_elt_t *cache_entry = cache->buckets; + for (i = 0; i < cache->num_buckets * cache->elts_per_bucket; i++, cache_entry++) { + ngx_array_init(&cache_entry->attributes, cache->pool, DEFAULT_ATTRS_COUNT, sizeof(ldap_search_attribute_t)); + } + return NGX_OK; } @@ -946,6 +1209,17 @@ ngx_http_auth_ldap_check_cache(ngx_http_request_t *r, ngx_http_auth_ldap_ctx_t * if (elt->small_hash == ctx->cache_small_hash && elt->time > time_limit && memcmp(elt->big_hash, ctx->cache_big_hash, 16) == 0) { + if (elt->outcome == OUTCOME_ALLOW || elt->outcome == OUTCOME_CACHED_ALLOW) { + /* Restore the cached attributes to the current context */ + ctx->attributes.nelts = 0; + for (i = 0; i < elt->attributes.nelts; i++) { + ldap_search_attribute_t *ctx_attr = ngx_array_push(&ctx->attributes); + *ctx_attr = *((ldap_search_attribute_t *)elt->attributes.elts + i); + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "http_auth_ldap: ngx_http_auth_ldap_check_cache: restoring attribute '%V' = '%V' from cache", + &ctx_attr->attr_name, &ctx_attr->attr_value); + } + } return elt->outcome; } } @@ -972,6 +1246,15 @@ ngx_http_auth_ldap_update_cache(ngx_http_auth_ldap_ctx_t *ctx, oldest_elt->outcome = outcome; oldest_elt->small_hash = ctx->cache_small_hash; ngx_memcpy(oldest_elt->big_hash, ctx->cache_big_hash, 16); + /* save (copy) each attribute from the context to the cache */ + oldest_elt->attributes.nelts = 0; + for (i = 0; i < ctx->attributes.nelts; i++) { + ldap_search_attribute_t *cache_attr = ngx_array_push(&oldest_elt->attributes); + *cache_attr = *((ldap_search_attribute_t *)ctx->attributes.elts + i); + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, ctx->r->connection->log, 0, + "http_auth_ldap: ngx_http_auth_ldap_update_cache: saving '%V' = '%V' to cache", + &cache_attr->attr_name, &cache_attr->attr_value); + } } @@ -989,7 +1272,7 @@ ngx_http_auth_ldap_sb_remove(Sockbuf_IO_Desc *sbiod) { ngx_http_auth_ldap_connection_t *c = (ngx_http_auth_ldap_connection_t *)sbiod->sbiod_pvt; - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_sb_remove()"); + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_sb_remove() Cnx[%d]", c->cnx_idx); (void)c; /* 'c' would be left unused on debug builds */ sbiod->sbiod_pvt = NULL; @@ -1001,12 +1284,13 @@ ngx_http_auth_ldap_sb_close(Sockbuf_IO_Desc *sbiod) { ngx_http_auth_ldap_connection_t *c = (ngx_http_auth_ldap_connection_t *)sbiod->sbiod_pvt; - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_sb_close()"); + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_sb_close() Cnx[%d]", c->cnx_idx); if (!c->conn.connection->read->error && !c->conn.connection->read->eof) { if (ngx_shutdown_socket(c->conn.connection->fd, SHUT_RDWR) == -1) { ngx_connection_error(c->conn.connection, ngx_socket_errno, ngx_shutdown_socket_n " failed"); - ngx_http_auth_ldap_close_connection(c); + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_sb_close() Cnx[%d] shutdown failed", c->cnx_idx); + ngx_http_auth_ldap_close_connection(c, 0); return -1; } } @@ -1019,7 +1303,7 @@ ngx_http_auth_ldap_sb_ctrl(Sockbuf_IO_Desc *sbiod, int opt, void *arg) { ngx_http_auth_ldap_connection_t *c = (ngx_http_auth_ldap_connection_t *)sbiod->sbiod_pvt; - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_sb_ctrl(opt=%d)", opt); + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_sb_ctrl(opt=%d) Cnx[%d]", opt, c->cnx_idx); switch (opt) { case LBER_SB_OPT_DATA_READY: @@ -1038,9 +1322,10 @@ ngx_http_auth_ldap_sb_read(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len) ngx_http_auth_ldap_connection_t *c = (ngx_http_auth_ldap_connection_t *)sbiod->sbiod_pvt; ber_slen_t ret; - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_sb_read(len=%d)", len); + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_sb_read(len=%d) Cnx[%d]", len, c->cnx_idx); ret = c->conn.connection->recv(c->conn.connection, buf, len); + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_sb_read Cnx[%d] recv ret=%d", c->cnx_idx, ret); if (ret < 0) { errno = (ret == NGX_AGAIN) ? NGX_EAGAIN : NGX_ECONNRESET; return -1; @@ -1055,9 +1340,10 @@ ngx_http_auth_ldap_sb_write(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len) ngx_http_auth_ldap_connection_t *c = (ngx_http_auth_ldap_connection_t *)sbiod->sbiod_pvt; ber_slen_t ret; - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_sb_write(len=%d)", len); + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_sb_write(len=%d) Cnx[%d]", len, c->cnx_idx); ret = c->conn.connection->send(c->conn.connection, buf, len); + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_sb_write Cnx[%d] ret=%d", c->cnx_idx, ret); if (ret < 0) { errno = (ret == NGX_AGAIN) ? NGX_EAGAIN : NGX_ECONNRESET; return 0; @@ -1080,21 +1366,35 @@ static Sockbuf_IO ngx_http_auth_ldap_sbio = /*** Asynchronous LDAP connection handling ***/ static void -ngx_http_auth_ldap_close_connection(ngx_http_auth_ldap_connection_t *c) +ngx_http_auth_ldap_close_connection(ngx_http_auth_ldap_connection_t *c, int retry_asap) { ngx_queue_t *q; + ngx_msec_t reconnect_delay = retry_asap ? RECONNECT_ASAP_MS : c->server->reconnect_timeout; // Default reconnect delay + ngx_http_auth_ldap_connection_state_t saved_state; + + ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_close_connection: Cnx[%d] retry_asap=%d state=%d", c->cnx_idx, retry_asap, c->state); + + if (c->state == STATE_DISCONNECTING) { + // Already in DISCONNECTING state. break here to avoid recuse in ngx_http_auth_ldap_close_connection + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_close_connection: Cnx[%d] Already in DISCONNECTING state", c->cnx_idx); + return; + } + + // Temporary DISCONNECTING state to avoid loops in ngx_http_auth_ldap_close_connection + saved_state = c->state; + c->state = STATE_DISCONNECTING; if (c->ld) { - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http_auth_ldap: Unbinding from the server \"%V\")", - &c->server->url); + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_close_connection: Cnx[%d] Unbinding from the server \"%V\")", + c->cnx_idx, &c->server->url); ldap_unbind_ext(c->ld, NULL, NULL); /* Unbind is always synchronous, even though the function name does not end with an '_s'. */ c->ld = NULL; } if (c->conn.connection) { - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http_auth_ldap: Closing connection (fd=%d)", - c->conn.connection->fd); + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_close_connection: Cnx[%d] Closing connection (fd=%d)", + c->cnx_idx, c->conn.connection->fd); #if (NGX_OPENSSL) if (c->conn.connection->ssl) { @@ -1107,9 +1407,15 @@ ngx_http_auth_ldap_close_connection(ngx_http_auth_ldap_connection_t *c) c->conn.connection = NULL; } + ngx_log_error(NGX_LOG_INFO, c->log, 0, + "ngx_http_auth_ldap_close_connection: Cnx[%d] Closed", c->cnx_idx); + q = ngx_queue_head(&c->server->free_connections); while (q != ngx_queue_sentinel(&c->server->free_connections)) { if (q == &c->queue) { + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "ngx_http_auth_ldap_close_connection: removing cnx [%d] from free queue", + c->cnx_idx); ngx_queue_remove(q); break; } @@ -1117,17 +1423,20 @@ ngx_http_auth_ldap_close_connection(ngx_http_auth_ldap_connection_t *c) } c->rctx = NULL; + c->state = saved_state; // Restore initial state if (c->state != STATE_DISCONNECTED) { c->state = STATE_DISCONNECTED; - ngx_add_timer(&c->reconnect_event, c->server->reconnect_timeout); - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http_auth_ldap: Connection scheduled for reconnection in %d ms", c->server->reconnect_timeout); + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "ngx_http_auth_ldap_close_connection: Cnx[%d] set pending reconnection", + c->cnx_idx); + ngx_http_auth_ldap_set_pending_reconnection(c, reconnect_delay); } } static void ngx_http_auth_ldap_wake_request(ngx_http_request_t *r) { - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http_auth_ldap: Waking authentication request \"%V\"", + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_auth_ldap_wake_request: Waking authentication request \"%V\"", &r->request_line); ngx_http_core_run_phases(r); } @@ -1147,29 +1456,42 @@ ngx_http_auth_ldap_get_connection(ngx_http_auth_ldap_ctx_t *ctx) server = ctx->server; - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->r->connection->log, 0, "http_auth_ldap: Wants a free connection to \"%V\"", - &server->alias); + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->r->connection->log, 0, + "ngx_http_auth_ldap_get_connection: Wants a free connection to \"%V\"", &server->alias); if (!ngx_queue_empty(&server->free_connections)) { q = ngx_queue_last(&server->free_connections); ngx_queue_remove(q); c = ngx_queue_data(q, ngx_http_auth_ldap_connection_t, queue); + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->r->connection->log, 0, + "ngx_http_auth_ldap_get_connection: Got cnx [%d] from free queue", c->cnx_idx); c->rctx = ctx; ctx->c = c; ctx->replied = 0; return 1; } + /* Check if we have pending (waiting reconnect) connection */ + if (!ngx_queue_empty(&server->pending_reconnections)) { + q = ngx_queue_head(&server->pending_reconnections); + c = ngx_queue_data(q, ngx_http_auth_ldap_connection_t, queue_pending); + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->r->connection->log, 0, + "ngx_http_auth_ldap_get_connection: Got cnx [%d] from pending queue -> shorten reconnect timer", c->cnx_idx); + /* Use the shortest the reconnection delay as we really need a new connection here */ + ngx_del_timer(&c->reconnect_event); // Cancel the reconnect timer + ngx_add_timer(&c->reconnect_event, RECONNECT_ASAP_MS); + } + q = ngx_queue_next(&server->waiting_requests); while (q != ngx_queue_sentinel(&server->waiting_requests)) { if (q == &ctx->queue) { - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ctx->r->connection->log, 0, "http_auth_ldap: Tried to insert a same request"); + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ctx->r->connection->log, 0, "ngx_http_auth_ldap_get_connection: Tried to insert a same request"); return 0; } q = ngx_queue_next(q); } ngx_queue_insert_head(&server->waiting_requests, &ctx->queue); - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ctx->r->connection->log, 0, "http_auth_ldap: No connection available at the moment, waiting..."); + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ctx->r->connection->log, 0, "ngx_http_auth_ldap_get_connection: No connection available at the moment, waiting..."); return 0; } @@ -1178,8 +1500,9 @@ ngx_http_auth_ldap_return_connection(ngx_http_auth_ldap_connection_t *c) { ngx_queue_t *q; - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http_auth_ldap: Marking the connection to \"%V\" as free", - &c->server->alias); + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, + "ngx_http_auth_ldap_return_connection: Marking the connection [%d] to \"%V\" as free", + c->cnx_idx, &c->server->alias); if (c->rctx != NULL) { c->rctx->c = NULL; @@ -1189,20 +1512,49 @@ ngx_http_auth_ldap_return_connection(ngx_http_auth_ldap_connection_t *c) } ngx_queue_insert_head(&c->server->free_connections, &c->queue); + ngx_log_error(NGX_LOG_INFO, c->log, 0, + "ngx_http_auth_ldap_return_connectionn: Cnx[%d] Ready", c->cnx_idx); if (!ngx_queue_empty(&c->server->waiting_requests)) { q = ngx_queue_last(&c->server->waiting_requests); ngx_queue_remove(q); + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "ngx_http_auth_ldap_return_connection: Cnx[%d] Wakeup a waiting request", c->cnx_idx); ngx_http_auth_ldap_wake_request((ngx_queue_data(q, ngx_http_auth_ldap_ctx_t, queue))->r); } } +static void +ngx_http_auth_ldap_set_pending_reconnection(ngx_http_auth_ldap_connection_t *c, ngx_msec_t reconnect_delay) +{ + ngx_queue_t *q; + + ngx_log_error(NGX_LOG_INFO, c->log, 0, + "ngx_http_auth_ldap_set_pending_reconnection: Cnx[%d] reconnection scheduled in %d ms", c->cnx_idx, reconnect_delay); + ngx_add_timer(&c->reconnect_event, reconnect_delay); + + /* Check if connection is already in the pending queue */ + for (q = ngx_queue_head(&c->server->pending_reconnections); + q != ngx_queue_sentinel(&c->server->pending_reconnections); + q = ngx_queue_next(q)) + { + if (q == &c->queue_pending) { + ngx_log_error(NGX_LOG_WARN, c->log, 0, + "http_auth_ldap: ngx_http_auth_ldap_set_pending_reconnection: Connection already in pending queue"); + return; + } + } + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "ngx_http_auth_ldap_set_pending_reconnection: Connection [%d] inserted in pending queue", c->cnx_idx); + ngx_queue_insert_tail(&c->server->pending_reconnections, &c->queue_pending); +} + static void ngx_http_auth_ldap_reply_connection(ngx_http_auth_ldap_connection_t *c, int error_code, char* error_msg) { ngx_http_auth_ldap_ctx_t *ctx = c->rctx; - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http_auth_ldap: LDAP request to \"%V\" has finished", - &c->server->alias); + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_reply_connection: cnx[%d] LDAP request to \"%V\" has finished", + c->cnx_idx, &c->server->alias); ctx->replied = 1; ctx->error_code = error_code; @@ -1224,7 +1576,7 @@ ngx_http_auth_ldap_dummy_write_handler(ngx_event_t *wev) ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, "http_auth_ldap: Dummy write handler"); if (ngx_handle_write_event(wev, 0) != NGX_OK) { - ngx_http_auth_ldap_close_connection(((ngx_connection_t *) wev->data)->data); + ngx_http_auth_ldap_close_connection(((ngx_connection_t *) wev->data)->data, 0); } } @@ -1265,6 +1617,8 @@ ngx_http_auth_ldap_connection_established(ngx_http_auth_ldap_connection_t *c) ngx_int_t rc; struct berval cred; + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_connection_established: Cnx[%d] established", c->cnx_idx); + conn = c->conn.connection; ngx_del_timer(conn->read); conn->write->handler = ngx_http_auth_ldap_dummy_write_handler; @@ -1272,52 +1626,60 @@ ngx_http_auth_ldap_connection_established(ngx_http_auth_ldap_connection_t *c) /* Initialize OpenLDAP on the connection */ - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http_auth_ldap: Initializing connection using URL \"%V\"", &c->server->url); - + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_connection_established: Cnx[%d] initializing LDAP using URL \"%V\"", + c->cnx_idx, &c->server->url); rc = ldap_init_fd(c->conn.connection->fd, LDAP_PROTO_EXT, (const char *) c->server->url.data, &c->ld); if (rc != LDAP_SUCCESS) { - ngx_log_error(NGX_LOG_ERR, c->log, errno, "http_auth_ldap: ldap_init_fd() failed (%d: %s)", rc, ldap_err2string(rc)); - ngx_http_auth_ldap_close_connection(c); + ngx_log_error(NGX_LOG_ERR, c->log, errno, "ngx_http_auth_ldap_connection_established: ldap_init_fd() failed (%d: %s)", rc, ldap_err2string(rc)); + ngx_http_auth_ldap_close_connection(c, 0); return; } if (c->server->referral == 0) { rc = ldap_set_option(c->ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF); if (rc != LDAP_OPT_SUCCESS) { - ngx_log_error(NGX_LOG_ERR, c->log, 0, "http_auth_ldap: ldap_set_option() failed (%d: %s)", rc, ldap_err2string(rc)); - ngx_http_auth_ldap_close_connection(c); + ngx_log_error(NGX_LOG_ERR, c->log, 0, "ngx_http_auth_ldap_connection_established: ldap_set_option() failed (%d: %s)", rc, ldap_err2string(rc)); + ngx_http_auth_ldap_close_connection(c, 0); return; } } rc = ldap_get_option(c->ld, LDAP_OPT_SOCKBUF, (void *) &sb); if (rc != LDAP_OPT_SUCCESS) { - ngx_log_error(NGX_LOG_ERR, c->log, 0, "http_auth_ldap: ldap_get_option() failed (%d: %s)", rc, ldap_err2string(rc)); - ngx_http_auth_ldap_close_connection(c); + ngx_log_error(NGX_LOG_ERR, c->log, 0, "ngx_http_auth_ldap_connection_established: ldap_get_option() failed (%d: %s)", rc, ldap_err2string(rc)); + ngx_http_auth_ldap_close_connection(c, 0); return; } ber_sockbuf_add_io(sb, &ngx_http_auth_ldap_sbio, LBER_SBIOD_LEVEL_PROVIDER, (void *) c); - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http_auth_ldap: Connection initialized"); + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "ngx_http_auth_ldap_connection_established: Cnx[%d] LDAP initialized", c->cnx_idx); /* Perform initial bind to the server */ cred.bv_val = (char *) c->server->bind_dn_passwd.data; cred.bv_len = c->server->bind_dn_passwd.len; + ngx_log_error(NGX_LOG_INFO, c->log, 0, + "ngx_http_auth_ldap_connection_established: Cnx[%d] Initial binding ...", c->cnx_idx); rc = ldap_sasl_bind(c->ld, (const char *) c->server->bind_dn.data, LDAP_SASL_SIMPLE, &cred, NULL, NULL, &c->msgid); if (rc != LDAP_SUCCESS) { - ngx_log_error(NGX_LOG_ERR, c->log, 0, "http_auth_ldap: ldap_sasl_bind() failed (%d: %s)", - rc, ldap_err2string(rc)); - ngx_http_auth_ldap_close_connection(c); + ngx_log_error(NGX_LOG_ERR, c->log, 0, + "ngx_http_auth_ldap_connection_established: [%d] initial ldap_sasl_bind() failed (%d: %s)", + c->cnx_idx, rc, ldap_err2string(rc)); + ngx_http_auth_ldap_close_connection(c, 0); return; } - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http_auth_ldap: ldap_sasl_bind() -> msgid=%d", c->msgid); + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, + "ngx_http_auth_ldap_connection_established: [%d] initial ldap_sasl_bind() -> msgid=%d", + c->cnx_idx, c->msgid); c->state = STATE_INITIAL_BINDING; ngx_add_timer(c->conn.connection->read, c->server->bind_timeout); - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http_auth_ldap: bind_timeout=%d", c->server->bind_timeout); + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, + "ngx_http_auth_ldap_connection_established: [%d] waiting initial bind response (timeout=%d)", + c->cnx_idx, c->server->bind_timeout); } #if (NGX_OPENSSL) @@ -1327,6 +1689,8 @@ ngx_http_auth_ldap_ssl_handshake_handler(ngx_connection_t *conn, ngx_flag_t vali ngx_http_auth_ldap_connection_t *c; c = conn->data; + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http_auth_ldap: SSL handshake handler (validate=%d)", validate); + if (conn->ssl->handshaked) { #if OPENSSL_VERSION_NUMBER >= 0x10002000 if (validate) { // verify remote certificate if requested @@ -1334,18 +1698,31 @@ ngx_http_auth_ldap_ssl_handshake_handler(ngx_connection_t *conn, ngx_flag_t vali long chain_verified = SSL_get_verify_result(conn->ssl->connection); int addr_verified; - char *hostname = c->server->ludpp->lud_host; - addr_verified = X509_check_host(cert, hostname, 0, 0, 0); - - if (!addr_verified) { // domain not in cert? try IP - size_t len; // get IP length - if (conn->sockaddr->sa_family == 4) len = 4; - else if (conn->sockaddr->sa_family == 6) len = 16; - else { // very unlikely indeed - ngx_http_auth_ldap_close_connection(c); - return; + if (c->server->ssl_check_cert == SSL_CERT_VERIFY_CHAIN) { + // chain_verified is enough, not requiring full name/IP verification + addr_verified = 1; + + } else { + // verify hostname/IP + char *hostname = c->server->ludpp->lud_host; + addr_verified = X509_check_host(cert, hostname, 0, 0, 0); + + if (!addr_verified) { // domain not in cert? try IP + size_t len; // get IP length + + struct sockaddr *conn_sockaddr = NULL; + if (conn->sockaddr != NULL) conn_sockaddr = conn->sockaddr; + else if (c->conn.sockaddr != NULL) conn_sockaddr = c->conn.sockaddr; + else conn_sockaddr = &c->server->parsed_url.sockaddr.sockaddr; + + if (conn_sockaddr->sa_family == AF_INET) len = 4; + else if (conn_sockaddr->sa_family == AF_INET6) len = 16; + else { // very unlikely indeed + ngx_http_auth_ldap_close_connection(c, 0); + return; + } + addr_verified = X509_check_ip(cert, (const unsigned char*)conn_sockaddr->sa_data, len, 0); } - addr_verified = X509_check_ip(cert, (const unsigned char*)conn->sockaddr->sa_data, len, 0); } // Find anything fishy? @@ -1360,7 +1737,7 @@ ngx_http_auth_ldap_ssl_handshake_handler(ngx_connection_t *conn, ngx_flag_t vali "http_auth_ldap: Remote side presented invalid SSL certificate: error %l, %s", chain_verified, X509_verify_cert_error_string(chain_verified)); } - ngx_http_auth_ldap_close_connection(c); + ngx_http_auth_ldap_close_connection(c, 0); return; } } @@ -1374,7 +1751,7 @@ ngx_http_auth_ldap_ssl_handshake_handler(ngx_connection_t *conn, ngx_flag_t vali } else { // handshake failed ngx_log_error(NGX_LOG_ERR, c->log, 0, "http_auth_ldap: SSL handshake failed"); - ngx_http_auth_ldap_close_connection(c); + ngx_http_auth_ldap_close_connection(c, 0); } } @@ -1393,11 +1770,13 @@ ngx_http_auth_ldap_ssl_handshake(ngx_http_auth_ldap_connection_t *c) { ngx_int_t rc; + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http_auth_ldap: SSL handshake"); + c->conn.connection->pool = c->pool; rc = ngx_ssl_create_connection(c->ssl, c->conn.connection, NGX_SSL_BUFFER | NGX_SSL_CLIENT); if (rc != NGX_OK) { ngx_log_error(NGX_LOG_ERR, c->log, 0, "http_auth_ldap: SSL initialization failed"); - ngx_http_auth_ldap_close_connection(c); + ngx_http_auth_ldap_close_connection(c, 0); return; } @@ -1461,20 +1840,20 @@ ngx_http_auth_ldap_connect_handler(ngx_event_t *wev) ngx_http_auth_ldap_connection_t *c; int keepalive; - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, "http_auth_ldap: Connect handler"); - conn = wev->data; c = conn->data; + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_connect_handler: Cnx[%d]", c->cnx_idx); + if (ngx_handle_write_event(wev, 0) != NGX_OK) { - ngx_http_auth_ldap_close_connection(c); + ngx_http_auth_ldap_close_connection(c, 0); return; } keepalive = 1; if (setsockopt(conn->fd, SOL_SOCKET, SO_KEEPALIVE, (const void *) &keepalive, sizeof(int)) == -1) { - ngx_log_error(NGX_LOG_ALERT, c->log, ngx_socket_errno, "http_auth_ldap: setsockopt(SO_KEEPALIVE) failed"); + ngx_log_error(NGX_LOG_ALERT, c->log, ngx_socket_errno, "ngx_http_auth_ldap_connect_handler: setsockopt(SO_KEEPALIVE) failed"); } #if (NGX_OPENSSL) @@ -1499,21 +1878,22 @@ ngx_http_auth_ldap_read_handler(ngx_event_t *rev) char *error_msg; char *dn; - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0, "http_auth_ldap: Read handler"); - conn = rev->data; c = conn->data; + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, rev->log, 0, "ngx_http_auth_ldap_read_handler: Cnx[%d]", c->cnx_idx); + if (c->ld == NULL) { - ngx_log_error(NGX_LOG_ERR, c->log, 0, "http_auth_ldap: Could not connect"); - ngx_http_auth_ldap_close_connection(c); + ngx_log_error(NGX_LOG_WARN, rev->log, 0, "ngx_http_auth_ldap_read_handler: Cnx[%d] No ldap handler (already closed)", c->cnx_idx); + ngx_http_auth_ldap_close_connection(c, 0); return; } if (rev->timedout) { - ngx_log_error(NGX_LOG_ERR, c->log, NGX_ETIMEDOUT, "http_auth_ldap: Request timed out (state=%d)", c->state); + ngx_log_error(NGX_LOG_ERR, c->log, NGX_ETIMEDOUT, + "ngx_http_auth_ldap_read_handler: Cnx[%d] Read timed out (state=%d)", c->cnx_idx, c->state); conn->timedout = 1; - ngx_http_auth_ldap_close_connection(c); + ngx_http_auth_ldap_close_connection(c, 1); return; } @@ -1522,39 +1902,42 @@ ngx_http_auth_ldap_read_handler(ngx_event_t *rev) for (;;) { rc = ldap_result(c->ld, LDAP_RES_ANY, 0, &timeout, &result); if (rc < 0) { - ngx_log_error(NGX_LOG_ERR, c->log, 0, "http_auth_ldap: ldap_result() failed (%d: %s)", - rc, ldap_err2string(rc)); - ngx_http_auth_ldap_close_connection(c); - + int reconnect_asap = 0; // if LDAP_SERVER_DOWN (usually timeouts or server disconnects) - if (rc == LDAP_SERVER_DOWN && \ - c->server->max_down_retries_count < c->server->max_down_retries) { - /** - update counter (this is always reset in - ngx_http_auth_ldap_connect() for a successful ldap - connection - **/ - c->server->max_down_retries_count++; - ngx_log_error(NGX_LOG_ERR, c->log, 0, "http_auth_ldap: LDAP_SERVER_DOWN: retry count: %d", - c->server->max_down_retries_count); - c->state = STATE_DISCONNECTED; - // immediate reconnect synchronously, this schedules another - // timer call to this read handler again - ngx_http_auth_ldap_reconnect_handler(rev); - return; - } + if (rc == LDAP_SERVER_DOWN) { + if (c->server->max_down_retries_count < c->server->max_down_retries) { + /** + update counter (this is always reset in + ngx_http_auth_ldap_connect() for a successful ldap + connection + **/ + c->server->max_down_retries_count++; + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_read_handler: Cnx[%d] LDAP_SERVER_DOWN (retry count: %d)", + c->cnx_idx, c->server->max_down_retries_count); + reconnect_asap = 1; + } else { + ngx_log_error(NGX_LOG_ERR, c->log, 0, + "ngx_http_auth_ldap_read_handler: Cnx[%d] LDAP_SERVER_DOWN: No more reconnect retry", c->cnx_idx); + } + ngx_http_auth_ldap_close_connection(c, reconnect_asap); + } else { + ngx_log_error(NGX_LOG_ERR, c->log, 0, "ngx_http_auth_ldap_read_handler: Cnx[%d] ldap_result() failed (%d: %s)", + c->cnx_idx, rc, ldap_err2string(rc)); + } return; } if (rc == 0) { - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http_auth_ldap: ldap_result() -> rc=0"); + // Timeout ({0, 0}) => No result message) + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_read_handler: Cnx[%d] ldap_result() -> rc=0", c->cnx_idx); break; } - ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0, "http_auth_ldap: ldap_result() -> rc=%d, msgid=%d, msgtype=%d", - rc, ldap_msgid(result), ldap_msgtype(result)); + ngx_log_debug4(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_read_handler: Cnx[%d] ldap_result() -> rc=%d, msgid=%d, msgtype=%d", + c->cnx_idx, rc, ldap_msgid(result), ldap_msgtype(result)); if (ldap_msgid(result) != c->msgid) { - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http_auth_ldap: Message with unknown ID received, ignoring."); + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "ngx_http_auth_ldap_read_handler: Cnx[%d] Message with unknown ID received, ignoring.", c->cnx_idx); ldap_msgfree(result); continue; } @@ -1564,10 +1947,10 @@ ngx_http_auth_ldap_read_handler(ngx_event_t *rev) error_code = LDAP_NO_RESULTS_RETURNED; error_msg = NULL; } else if (rc != LDAP_SUCCESS) { - ngx_log_error(NGX_LOG_ERR, c->log, 0, "http_auth_ldap: ldap_parse_result() failed (%d: %s)", - rc, ldap_err2string(rc)); + ngx_log_error(NGX_LOG_ERR, c->log, 0, "ngx_http_auth_ldap_read_handler: Cnx[%d] ldap_parse_result() failed (%d: %s)", + c->cnx_idx, rc, ldap_err2string(rc)); ldap_msgfree(result); - ngx_http_auth_ldap_close_connection(c); + ngx_http_auth_ldap_close_connection(c, 1); return; } @@ -1578,15 +1961,15 @@ ngx_http_auth_ldap_read_handler(ngx_event_t *rev) } ngx_del_timer(conn->read); if (error_code == LDAP_SUCCESS) { - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http_auth_ldap: Initial bind successful"); + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_read_handler: Cnx[%d] Initial bind successful", c->cnx_idx); c->state = STATE_READY; ngx_http_auth_ldap_return_connection(c); } else { - ngx_log_error(NGX_LOG_ERR, c->log, 0, "http_auth_ldap: Initial bind failed (%d: %s [%s])", - error_code, ldap_err2string(error_code), error_msg ? error_msg : "-"); + ngx_log_error(NGX_LOG_ERR, c->log, 0, "ngx_http_auth_ldap_read_handler: Cnx[%d] Initial bind failed (%d: %s [%s])", + c->cnx_idx, error_code, ldap_err2string(error_code), error_msg ? error_msg : "-"); ldap_memfree(error_msg); ldap_msgfree(result); - ngx_http_auth_ldap_close_connection(c); + ngx_http_auth_ldap_close_connection(c, 0); return; } break; @@ -1595,27 +1978,60 @@ ngx_http_auth_ldap_read_handler(ngx_event_t *rev) if (ldap_msgtype(result) != LDAP_RES_BIND) { break; } - ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0, "http_auth_ldap: Received bind response (%d: %s [%s])", - error_code, ldap_err2string(error_code), error_msg ? error_msg : "-"); + ngx_log_error(NGX_LOG_INFO, c->log, 0, "ngx_http_auth_ldap_read_handler: Cnx[%d] Received bind response (%d: %s [%s])", + c->cnx_idx, error_code, ldap_err2string(error_code), error_msg ? error_msg : "-"); ngx_http_auth_ldap_reply_connection(c, error_code, error_msg); break; case STATE_SEARCHING: if (ldap_msgtype(result) == LDAP_RES_SEARCH_ENTRY) { - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http_auth_ldap: Received a search entry"); + ngx_log_error(NGX_LOG_INFO, c->log, 0, "ngx_http_auth_ldap_read_handler: Cnx[%d] Received a search entry", c->cnx_idx); if (c->rctx->dn.data == NULL) { dn = ldap_get_dn(c->ld, result); if (dn != NULL) { - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http_auth_ldap: Found entry with DN \"%s\"", dn); + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, + "ngx_http_auth_ldap_read_handler: Cnx[%d] Found entry with DN \"%s\"", c->cnx_idx, dn); c->rctx->dn.len = ngx_strlen(dn); c->rctx->dn.data = (u_char *) ngx_palloc(c->rctx->r->pool, c->rctx->dn.len + 1); ngx_memcpy(c->rctx->dn.data, dn, c->rctx->dn.len + 1); ldap_memfree(dn); } } + /* Iterate through each attribute in the entry. */ + BerElement *ber = NULL; + char *attr = NULL; + struct berval **vals = NULL; + for (attr = ldap_first_attribute(c->ld, result, &ber); + attr != NULL; + attr = ldap_next_attribute(c->ld, result, ber)) { + /* Get only first value for each attribute. */ + if ((vals = ldap_get_values_len(c->ld, result, attr)) != NULL ) { + if(vals[0] != NULL) { + ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_read_handler: Cnx[%d] Received attribute %s: %s", + c->cnx_idx, attr, vals[0]->bv_val); + /* Save attribute name and value in the context */ + ldap_search_attribute_t * elt = ngx_array_push(&c->rctx->attributes); + santitize_str((u_char *)attr, SANITIZE_NO_CONV); + int attr_len = strlen(attr); + elt->attr_name.len = c->server->attribute_header_prefix.len + attr_len; + /* Use the pool of global cache to allocate strings, so that they can be used everywhere */ + elt->attr_name.data = ngx_pnalloc(ngx_http_auth_ldap_cache.pool, elt->attr_name.len); + unsigned char *p = ngx_cpymem(elt->attr_name.data, c->server->attribute_header_prefix.data, c->server->attribute_header_prefix.len); + p = ngx_cpymem(p, attr, attr_len); + elt->attr_value.len = vals[0]->bv_len; + elt->attr_value.data = ngx_pnalloc(ngx_http_auth_ldap_cache.pool, elt->attr_value.len); + ngx_memcpy(elt->attr_value.data, vals[0]->bv_val, elt->attr_value.len); + } + ldap_value_free_len(vals); + } + ldap_memfree(attr); + } + if (ber != NULL) { + ber_free(ber, 0); + } } else if (ldap_msgtype(result) == LDAP_RES_SEARCH_RESULT) { - ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0, "http_auth_ldap: Received search result (%d: %s [%s])", - error_code, ldap_err2string(error_code), error_msg ? error_msg : "-"); + ngx_log_error(NGX_LOG_INFO, c->log, 0, "ngx_http_auth_ldap_read_handler: Cnx[%d] Received search result (%d: %s [%s])", + c->cnx_idx, error_code, ldap_err2string(error_code), error_msg ? error_msg : "-"); ngx_http_auth_ldap_reply_connection(c, error_code, error_msg); } break; @@ -1624,8 +2040,8 @@ ngx_http_auth_ldap_read_handler(ngx_event_t *rev) if (ldap_msgtype(result) != LDAP_RES_COMPARE) { break; } - ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0, "http_auth_ldap: Received comparison result (%d: %s [%s])", - error_code, ldap_err2string(error_code), error_msg ? error_msg : "-"); + ngx_log_error(NGX_LOG_INFO, c->log, 0, "ngx_http_auth_ldap_read_handler: Cnx[%d] Received comparison result (%d: %s [%s])", + c->cnx_idx, error_code, ldap_err2string(error_code), error_msg ? error_msg : "-"); ngx_http_auth_ldap_reply_connection(c, error_code, error_msg); break; @@ -1638,23 +2054,86 @@ ngx_http_auth_ldap_read_handler(ngx_event_t *rev) } if (ngx_handle_read_event(rev, 0) != NGX_OK) { - ngx_http_auth_ldap_close_connection(c); + ngx_http_auth_ldap_close_connection(c, 1); return; } } static void ngx_http_auth_ldap_connect(ngx_http_auth_ldap_connection_t *c) +{ + ngx_log_error(NGX_LOG_INFO, c->log, 0, + "ngx_http_auth_ldap_connect: Cnx[%d] Server \"%V\" connecting ...", + c->cnx_idx, &c->server->alias); + + // Clear and free any previous addrs from parsed_url, so that we can resolve again the LDAP server hostname + my_free_addrs_from_url(c->main_cnf->cnf_pool, &c->server->parsed_url); + + c->server->parsed_url.no_resolve = 0; // Try to resolve this time + if (ngx_parse_url(c->pool, &c->server->parsed_url) != NGX_OK) { + ngx_log_error(NGX_LOG_WARN, c->log, 0, + "ngx_http_auth_ldap_connect: Hostname \"%V\" not found with system resolver, try with DNS", + &c->server->parsed_url.host); + // Try to resolve the hostname through the resolver + if (c->main_cnf->resolver == NULL) { + ngx_log_error(NGX_LOG_ERR, c->log, 0, + "ngx_http_auth_ldap_connect: No resolver configured"); + return; + } + ngx_resolver_ctx_t *resolver_ctx, temp; + temp.name = c->server->parsed_url.host; + resolver_ctx = ngx_resolve_start(c->main_cnf->resolver, &temp); + if (resolver_ctx == NULL) { + ngx_log_error(NGX_LOG_ERR, c->log, 0, "ngx_http_auth_ldap_connect: Unable to start the resolver"); + return; + } + if (resolver_ctx == NGX_NO_RESOLVER) { + ngx_log_error(NGX_LOG_ERR, c->log, 0, + "ngx_http_auth_ldap_connect: No resolver defined to resolve %V", + c->server->parsed_url.host); + return; + } + resolver_ctx->name = c->server->parsed_url.host; + resolver_ctx->handler = ngx_http_auth_ldap_resolve_handler; + resolver_ctx->data = c; + resolver_ctx->timeout = c->main_cnf->resolver_timeout; + c->resolver_ctx = resolver_ctx; + if (ngx_resolve_name(resolver_ctx) != NGX_OK) { + c->resolver_ctx = NULL; + ngx_log_error(NGX_LOG_ERR, c->log, 0, + "ngx_http_auth_ldap_connect: Resolve %V failed", c->server->parsed_url.host); + return; + } + // The DNS Querry has been triggered. Let the ngx_http_auth_ldap_resolve_handler now take the control of the flow. + return; + } + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, + "ngx_http_auth_ldap_connect: server \"%V\" (naddrs is now %d).", + &c->server->alias, c->server->parsed_url.naddrs); + + // Continue with the rest of the connection establishement + ngx_http_auth_ldap_connect_continue(c); +} + +static void +ngx_http_auth_ldap_connect_continue(ngx_http_auth_ldap_connection_t *c) { ngx_peer_connection_t *pconn; ngx_connection_t *conn; ngx_addr_t *addr; ngx_int_t rc; + if (c->server->parsed_url.naddrs == 0) { + ngx_log_error(NGX_LOG_ERR, c->log, 0, + "ngx_http_auth_ldap_connect_continue: Cnx[%d] No addr for server", c->cnx_idx); + return; + } + addr = &c->server->parsed_url.addrs[ngx_random() % c->server->parsed_url.naddrs]; - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http_auth_ldap: Connecting to LDAP server \"%V\".", - &addr->name); + ngx_log_error(NGX_LOG_INFO, c->log, 0, + "ngx_http_auth_ldap_connect_continue: Cnx[%d] Connecting to LDAP server IP@ %V ...", + c->cnx_idx, &addr->name); pconn = &c->conn; pconn->sockaddr = addr->sockaddr; @@ -1665,11 +2144,11 @@ ngx_http_auth_ldap_connect(ngx_http_auth_ldap_connection_t *c) pconn->log_error = NGX_ERROR_ERR; rc = ngx_event_connect_peer(pconn); - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http_auth_ldap: ngx_event_connect_peer() -> %d.", rc); + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "ngx_http_auth_ldap_connect_continue: ngx_event_connect_peer() -> %d.", rc); if (rc == NGX_ERROR || rc == NGX_BUSY || rc == NGX_DECLINED) { - ngx_log_error(NGX_LOG_ERR, c->log, 0, "http_auth_ldap: Unable to connect to LDAP server \"%V\".", - &addr->name); - ngx_add_timer(&c->reconnect_event, c->server->reconnect_timeout); + ngx_log_error(NGX_LOG_ERR, c->log, 0, "ngx_http_auth_ldap_connect_continue: Cnx[%d] Unable to connect to LDAP server \"%V\".", + c->cnx_idx, &addr->name); + ngx_http_auth_ldap_set_pending_reconnection(c, c->server->reconnect_timeout); return; } @@ -1681,7 +2160,9 @@ ngx_http_auth_ldap_connect(ngx_http_auth_ldap_connection_t *c) conn->write->handler = ngx_http_auth_ldap_connect_handler; conn->read->handler = ngx_http_auth_ldap_read_handler; ngx_add_timer(conn->read, c->server->connect_timeout); - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http_auth_ldap: connect_timeout=%d.", c->server->connect_timeout); + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, + "ngx_http_auth_ldap_connect_continue: [%d] Waiting connection response (timeout=%d)", + c->cnx_idx, c->server->connect_timeout); c->server->max_down_retries_count = 0; /* reset retries count */ c->state = STATE_CONNECTING; @@ -1690,7 +2171,7 @@ ngx_http_auth_ldap_connect(ngx_http_auth_ldap_connection_t *c) static void ngx_http_auth_ldap_connection_cleanup(void *data) { - ngx_http_auth_ldap_close_connection((ngx_http_auth_ldap_connection_t *) data); + ngx_http_auth_ldap_close_connection((ngx_http_auth_ldap_connection_t *) data, 0); } static void @@ -1698,9 +2179,192 @@ ngx_http_auth_ldap_reconnect_handler(ngx_event_t *ev) { ngx_connection_t *conn = ev->data; ngx_http_auth_ldap_connection_t *c = conn->data; + + ngx_log_debug3(NGX_LOG_DEBUG_HTTP, ev->log, 0, + "ngx_http_auth_ldap_reconnect_handler: ev=0x%p, conn=0x%p, c=0x%p", ev, conn, c); + + ngx_http_auth_ldap_reconnect_from_connection(c); +} + +static void +ngx_http_auth_ldap_reconnect_from_connection(ngx_http_auth_ldap_connection_t *c) +{ + ngx_queue_t *q; + + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, + "ngx_http_auth_ldap_reconnect_from_connection: Cnx[%d] c=0x%p", c->cnx_idx, c); + + /* Remove this connection from the pending reconnection queue */ + for (q = ngx_queue_head(&c->server->pending_reconnections); + q != ngx_queue_sentinel(&c->server->pending_reconnections); + q = ngx_queue_next(q)) + { + if (q == &c->queue_pending) { + ngx_queue_remove(q); + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "ngx_http_auth_ldap_reconnect_from_connection: Cnx[%d] removed from pending reconnection queue", c->cnx_idx); + break; + } + } ngx_http_auth_ldap_connect(c); } +static void +my_free_addrs_from_url(ngx_pool_t *pool, ngx_url_t *u) +{ + ngx_addr_t *addr; + ngx_uint_t i; + + if (u == NULL || u->addrs == NULL) { + return; + } + + for (i = 0; i < u->naddrs; i++) { + addr = &u->addrs[i]; + if (addr != NULL) { + if (addr->name.data != NULL) { + ngx_pfree(pool, addr->name.data); + addr->name.data = NULL; + addr->name.len = 0; + } + if (addr->sockaddr != NULL) { + ngx_pfree(pool, addr->sockaddr); + addr->sockaddr = NULL; + addr->socklen = 0; + } + } + } + + if (u->addrs != NULL) { + ngx_pfree(pool, u->addrs); + u->addrs = NULL; + u->naddrs = 0; + } +} + +/* Duplicated from ngnx_inet.c (as it is a static function in Nginx) */ +static ngx_int_t +my_ngx_inet_add_addr(ngx_pool_t *pool, ngx_url_t *u, struct sockaddr *sockaddr, + socklen_t socklen, ngx_uint_t total) +{ + u_char *p; + size_t len; + ngx_uint_t i, nports; + ngx_addr_t *addr; + struct sockaddr *sa; + + nports = u->last_port ? u->last_port - u->port + 1 : 1; + if (u->addrs == NULL) { + u->addrs = ngx_palloc(pool, total * nports * sizeof(ngx_addr_t)); + if (u->addrs == NULL) { + return NGX_ERROR; + } + } + for (i = 0; i < nports; i++) { + sa = ngx_pcalloc(pool, socklen); + if (sa == NULL) { + return NGX_ERROR; + } + ngx_memcpy(sa, sockaddr, socklen); + ngx_inet_set_port(sa, u->port + i); + switch (sa->sa_family) { + +#if (NGX_HAVE_INET6) + case AF_INET6: + len = NGX_INET6_ADDRSTRLEN + sizeof("[]:65536") - 1; + break; +#endif + + default: /* AF_INET */ + len = NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1; + } + p = ngx_pnalloc(pool, len); + if (p == NULL) { + return NGX_ERROR; + } + len = ngx_sock_ntop(sa, socklen, p, len, 1); + addr = &u->addrs[u->naddrs++]; + addr->sockaddr = sa; + addr->socklen = socklen; + addr->name.len = len; + addr->name.data = p; + } + return NGX_OK; +} + +static void ngx_http_auth_ldap_resolve_handler(ngx_resolver_ctx_t *ctx) +{ + ngx_http_auth_ldap_connection_t *c = ctx->data; + ngx_uint_t i; + ngx_resolver_addr_t *res_addr; + + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, + "ngx_http_auth_ldap_resolve_handler: Cnx[%d] server \"%V\"", + c->cnx_idx, &c->server->alias); + + if (ctx->state) { + ngx_log_error(NGX_LOG_ERR, c->log, 0, + "http_auth_ldap: ngx_http_auth_ldap_resolve_handler: %V could not be resolved (%i: %s)", + &ctx->name, ctx->state, ngx_resolver_strerror(ctx->state)); + return; + } + ngx_resolve_name_done(ctx); + c->resolver_ctx = NULL; + + // Clear and free any previous addrs in parsed_url + my_free_addrs_from_url(c->main_cnf->cnf_pool, &c->server->parsed_url); + + u_char ip_addr_str[INET6_ADDRSTRLEN +1]; // Buffer for IPv4 or IPv6 address strings + // Update the parsed_url with the addresses resolved by DNS + for (i = 0, res_addr = ctx->addrs; res_addr != NULL && i < ctx->naddrs; i++, res_addr++) { + bzero(ip_addr_str, sizeof(ip_addr_str)); + ngx_sock_ntop(res_addr->sockaddr, res_addr->socklen, ip_addr_str, sizeof(ip_addr_str) -1, 0); + ngx_log_debug4(NGX_LOG_DEBUG_HTTP, c->log, 0, + "ngx_http_auth_ldap_resolve_handler: Cnx[%d] Found [%d] %V -> %s", + c->cnx_idx, i, &ctx->name, ip_addr_str); + my_ngx_inet_add_addr(c->main_cnf->cnf_pool, &c->server->parsed_url, + res_addr->sockaddr, res_addr->socklen, ctx->naddrs); + } + + // Go on the the rest of the connection establishment + ngx_http_auth_ldap_connect_continue(c); +} + +/** + * Finalize servers configuration (at worker initialization) + */ +static ngx_int_t +ngx_http_auth_ldap_init_servers(ngx_cycle_t *cycle) +{ + ngx_http_auth_ldap_main_conf_t *halmcf; + ngx_http_auth_ldap_server_t *server; + ngx_uint_t i, j; + + halmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_auth_ldap_module); + if (halmcf == NULL || halmcf->servers == NULL) { + return NGX_OK; + } + + for (i = 0; i < halmcf->servers->nelts; i++) { + server = &((ngx_http_auth_ldap_server_t *) halmcf->servers->elts)[i]; + + if (server->attrs == NULL) { + // No search attributes specified, so setup an empty attributes list + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cycle->log, 0, "ngx_http_auth_ldap_init_servers: Server '%V': No search_attributes", &server->alias); + server->attrs = ngx_palloc(cycle->pool, 2 * sizeof(char *)); + server->attrs[0] = LDAP_NO_ATTRS; + server->attrs[1] = NULL; + } else { + for (j = 0; server->attrs[j] != NULL && j < MAX_ATTRS_COUNT; j++) { + ngx_log_debug3(NGX_LOG_DEBUG_HTTP, cycle->log, 0, "ngx_http_auth_ldap_init_servers: Server '%V': search_attribute[%d] = '%s'", &server->alias, j, server->attrs[j]); + } + } + } + + return NGX_OK; +} + + static ngx_int_t ngx_http_auth_ldap_init_connections(ngx_cycle_t *cycle) { @@ -1714,7 +2378,7 @@ ngx_http_auth_ldap_init_connections(ngx_cycle_t *cycle) halmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_auth_ldap_module); if (halmcf == NULL || halmcf->servers == NULL) { - return NGX_OK; + return NGX_OK; } option = LDAP_VERSION3; @@ -1724,6 +2388,7 @@ ngx_http_auth_ldap_init_connections(ngx_cycle_t *cycle) server = &((ngx_http_auth_ldap_server_t *) halmcf->servers->elts)[i]; ngx_queue_init(&server->free_connections); ngx_queue_init(&server->waiting_requests); + ngx_queue_init(&server->pending_reconnections); if (server->connections <= 1) { server->connections = 1; } @@ -1740,8 +2405,10 @@ ngx_http_auth_ldap_init_connections(ngx_cycle_t *cycle) cleanup->data = c; c->log = cycle->log; + c->main_cnf = halmcf; c->server = server; c->state = STATE_DISCONNECTED; + c->cnx_idx = j; /* Various debug logging around timer management assume that the field 'data' in ngx_event_t is a pointer to ngx_connection_t, therefore we @@ -1779,6 +2446,9 @@ ngx_http_auth_ldap_set_realm(ngx_http_request_t *r, ngx_str_t *realm) } r->headers_out.www_authenticate->hash = 1; +#if (nginx_version >= 1023000) + r->headers_out.www_authenticate->next = NULL; +#endif r->headers_out.www_authenticate->key.len = sizeof("WWW-Authenticate") - 1; r->headers_out.www_authenticate->key.data = (u_char *) "WWW-Authenticate"; r->headers_out.www_authenticate->value = *realm; @@ -1831,6 +2501,10 @@ ngx_http_auth_ldap_handler(ngx_http_request_t *r) return NGX_HTTP_INTERNAL_SERVER_ERROR; } ctx->r = r; + + /* Initialize the attributes array */ + ngx_array_init(&ctx->attributes, r->pool, DEFAULT_ATTRS_COUNT, sizeof(ldap_search_attribute_t)); + /* Other fields have been initialized to zero/NULL */ ngx_http_set_ctx(r, ctx, ngx_http_auth_ldap_module); } @@ -1850,9 +2524,21 @@ ngx_http_auth_ldap_authenticate(ngx_http_request_t *r, ngx_http_auth_ldap_ctx_t ngx_http_auth_ldap_server_t *s; if (r->connection->write->timedout) { - ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "http_auth_ldap: Authentication timed out"); + ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "ngx_http_auth_ldap_authenticate: Authentication timed out"); if (ctx->c != NULL) { - ngx_http_auth_ldap_return_connection(ctx->c); + if (ctx->server && ctx->server->clean_on_timeout) { + // Authentication response timeouted => Close and clean the corresponding LDAP connection + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "ngx_http_auth_ldap_authenticate: Close timeouted Cnx[%d]", ctx->c->cnx_idx); + ngx_http_auth_ldap_close_connection(ctx->c, 1); + // Clean the connection + ctx->c->msgid = -1; + ctx->c = NULL; + } else { + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "ngx_http_auth_ldap_authenticate: Return old timedout Cnx[%d]", ctx->c->cnx_idx); + ngx_http_auth_ldap_return_connection(ctx->c); + } } // Remove ctx from waiting_requests queue if it was added. @@ -1872,13 +2558,13 @@ ngx_http_auth_ldap_authenticate(ngx_http_request_t *r, ngx_http_auth_ldap_ctx_t * this happens when we are trying to get an LDAP connection but all of them are busy right now. */ if (ctx->iteration > 0 && !ctx->replied && ctx->phase != PHASE_START) { - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http_auth_ldap: The LDAP operation did not finish yet"); + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_auth_ldap_authenticate: The LDAP operation did not finish yet"); return NGX_AGAIN; } for (;;) { loop: - ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http_auth_ldap: Authentication loop (phase=%d, iteration=%d)", + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_auth_ldap_authenticate: Authentication loop (phase=%d, iteration=%d)", ctx->phase, ctx->iteration); switch (ctx->phase) { @@ -1887,7 +2573,7 @@ ngx_http_auth_ldap_authenticate(ngx_http_request_t *r, ngx_http_auth_ldap_ctx_t ctx->outcome = OUTCOME_UNCERTAIN; ngx_add_timer(r->connection->write, ctx->server->request_timeout); - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http_auth_ldap: request_timeout=%d",ctx->server->request_timeout); + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_auth_ldap_authenticate: Set request watchdog (timeout=%d)",ctx->server->request_timeout); /* Check cache if enabled */ @@ -1895,7 +2581,7 @@ ngx_http_auth_ldap_authenticate(ngx_http_request_t *r, ngx_http_auth_ldap_ctx_t for (i = 0; i < conf->servers->nelts; i++) { s = &((ngx_http_auth_ldap_server_t *) conf->servers->elts)[i]; rc = ngx_http_auth_ldap_check_cache(r, ctx, &ngx_http_auth_ldap_cache, s); - ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http_auth_ldap: Using cached outcome %d from server %d", rc, i); + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_auth_ldap_authenticate: Using cached outcome %d from server %d", rc, i); if (rc == OUTCOME_DENY || rc == OUTCOME_ALLOW) { ctx->outcome = (rc == OUTCOME_DENY ? OUTCOME_CACHED_DENY : OUTCOME_CACHED_ALLOW); ctx->phase = PHASE_NEXT; @@ -1936,13 +2622,13 @@ ngx_http_auth_ldap_authenticate(ngx_http_request_t *r, ngx_http_auth_ldap_ctx_t break; case PHASE_CHECK_USER: - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http_auth_ldap: User DN is \"%V\"", + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_auth_ldap_authenticate: User DN is \"%V\"", &ctx->user_dn); if (ctx->server->require_user != NULL) { rc = ngx_http_auth_ldap_check_user(r, ctx); if (rc != NGX_OK) { - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http_auth_ldap: Not ok", &ctx->user_dn); + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_auth_ldap_authenticate: Not ok", &ctx->user_dn); /* User check failed, try next server */ ctx->phase = PHASE_NEXT; break; @@ -1952,7 +2638,7 @@ ngx_http_auth_ldap_authenticate(ngx_http_request_t *r, ngx_http_auth_ldap_ctx_t /* User not yet fully authenticated, check group next */ if ((ctx->outcome == OUTCOME_UNCERTAIN) && (ctx->server->require_group != NULL)) { - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http_auth_ldap: Moving to group check", &ctx->user_dn); + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_auth_ldap_authenticate: Moving to group check", &ctx->user_dn); ctx->phase = PHASE_CHECK_GROUP; ctx->iteration = 0; break; @@ -1964,7 +2650,7 @@ ngx_http_auth_ldap_authenticate(ngx_http_request_t *r, ngx_http_auth_ldap_ctx_t break; case PHASE_CHECK_GROUP: - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http_auth_ldap: Checking group", &ctx->user_dn); + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_auth_ldap_authenticate: Checking group", &ctx->user_dn); rc = ngx_http_auth_ldap_check_group(r, ctx); if (rc == NGX_AGAIN) { /* LDAP operation in progress, wait for the results */ @@ -1990,7 +2676,7 @@ ngx_http_auth_ldap_authenticate(ngx_http_request_t *r, ngx_http_auth_ldap_ctx_t if ((ctx->server->satisfy_all == 0) && ( (ctx->server->require_user != NULL) || (ctx->server->require_group != NULL))){ - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http_auth_ldap: no requirement satisfied"); + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_auth_ldap_authenticate: no requirement satisfied"); ctx->outcome = OUTCOME_DENY; ctx->phase = PHASE_NEXT; /*rc = NGX_DECLINED;*/ @@ -2036,11 +2722,27 @@ ngx_http_auth_ldap_authenticate(ngx_http_request_t *r, ngx_http_auth_ldap_ctx_t if (ngx_http_auth_ldap_cache.buckets != NULL && (ctx->outcome == OUTCOME_DENY || ctx->outcome == OUTCOME_ALLOW)) { - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http_auth_ldap: Caching outcome %d", ctx->outcome); + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_auth_ldap_authenticate: Caching outcome %d", ctx->outcome); ngx_http_auth_ldap_update_cache(ctx, &ngx_http_auth_ldap_cache, ctx->outcome); } if (ctx->outcome == OUTCOME_ALLOW || ctx->outcome == OUTCOME_CACHED_ALLOW) { + /* Create response headers for each search attributes found in context */ + for (i = 0; i < ctx->attributes.nelts; i++) { + ldap_search_attribute_t *elt = (ldap_search_attribute_t *)ctx->attributes.elts + i; + ngx_table_elt_t *h = ngx_list_push(&r->headers_out.headers); + if (h != NULL) { + h->hash = 1; +#if (nginx_version >= 1023000) + h->next = NULL; +#endif + h->key = elt->attr_name; + h->value = elt->attr_value; + } + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "ngx_http_auth_ldap_authenticate: Set response header %V : %V", + &elt->attr_name, &elt->attr_value); + } return NGX_OK; } @@ -2060,7 +2762,6 @@ ngx_http_auth_ldap_search(ngx_http_request_t *r, ngx_http_auth_ldap_ctx_t *ctx) { LDAPURLDesc *ludpp; u_char *filter; - char *attrs[2]; ngx_int_t rc; /* On the first call, initiate the LDAP search operation */ @@ -2077,22 +2778,19 @@ ngx_http_auth_ldap_search(ngx_http_request_t *r, ngx_http_auth_ldap_ctx_t *ctx) ngx_sprintf(filter, "(&%s(%s=%V))%Z", ludpp->lud_filter != NULL ? ludpp->lud_filter : "(objectClass=*)", ludpp->lud_attrs[0], &r->headers_in.user); - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http_auth_ldap: Search filter is \"%s\"", - (const char *) filter); + ngx_log_error(NGX_LOG_INFO, ctx->c->log, 0, "ngx_http_auth_ldap_search: Cnx[%d] Searching (with filter \"%s\") ...", + ctx->c->cnx_idx, (const char *) filter); - attrs[0] = LDAP_NO_ATTRS; - attrs[1] = NULL; - - rc = ldap_search_ext(ctx->c->ld, ludpp->lud_dn, ludpp->lud_scope, (const char *) filter, attrs, 0, NULL, NULL, NULL, 0, &ctx->c->msgid); + rc = ldap_search_ext(ctx->c->ld, ludpp->lud_dn, ludpp->lud_scope, (const char *) filter, ctx->server->attrs, 0, NULL, NULL, NULL, 0, &ctx->c->msgid); if (rc != LDAP_SUCCESS) { - ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "http_auth_ldap: ldap_search_ext() failed (%d, %s)", - rc, ldap_err2string(rc)); + ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "ngx_http_auth_ldap_search: ldap_search_ext() Cnx[%d] failed (%d, %s)", + ctx->c->cnx_idx, rc, ldap_err2string(rc)); ngx_http_auth_ldap_return_connection(ctx->c); return NGX_ERROR; } - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http_auth_ldap: ldap_search_ext() -> msgid=%d", - ctx->c->msgid); + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_auth_ldap_search: ldap_search_ext() Cnx[%d] -> msgid=%d", + ctx->c->cnx_idx, ctx->c->msgid); ctx->c->state = STATE_SEARCHING; ctx->iteration++; return NGX_AGAIN; @@ -2100,15 +2798,17 @@ ngx_http_auth_ldap_search(ngx_http_request_t *r, ngx_http_auth_ldap_ctx_t *ctx) /* On the second call, handle the search results */ if (ctx->error_code != LDAP_SUCCESS) { - ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "http_auth_ldap: ldap_search_ext() request failed (%d: %s)", - ctx->error_code, ldap_err2string(ctx->error_code)); + ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "ngx_http_auth_ldap_search: ldap_search_ext() Cnx[%d] request failed (%d: %s)", + ctx->c->cnx_idx, ctx->error_code, ldap_err2string(ctx->error_code)); return NGX_ERROR; } if (ctx->dn.data == NULL) { - ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "http_auth_ldap: Could not find user DN"); + ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "ngx_http_auth_ldap_search: Cnx[%d] Could not find user DN", ctx->c->cnx_idx); return NGX_ERROR; } else { + ngx_log_error(NGX_LOG_INFO, ctx->c->log, 0, "ngx_http_auth_ldap_search: Cnx[%d] Found dn: \"%s\")", + ctx->c->cnx_idx, ctx->dn.data); ctx->user_dn.len = ngx_strlen(ctx->dn.data); ctx->user_dn.data = (u_char *) ngx_palloc(ctx->r->pool, ctx->user_dn.len + 1); ngx_memcpy(ctx->user_dn.data, ctx->dn.data, ctx->user_dn.len + 1); @@ -2288,17 +2988,19 @@ ngx_http_auth_ldap_check_bind(ngx_http_request_t *r, ngx_http_auth_ldap_ctx_t *c cred.bv_val = (char *) r->headers_in.passwd.data; cred.bv_len = r->headers_in.passwd.len; + ngx_log_error(NGX_LOG_INFO, ctx->c->log, 0, "ngx_http_auth_ldap_check_bind: Cnx[%d] Binding (user dn: %s) ...", + ctx->c->cnx_idx, ctx->user_dn.data); rc = ldap_sasl_bind(ctx->c->ld, (const char *) ctx->user_dn.data, LDAP_SASL_SIMPLE, &cred, NULL, NULL, &ctx->c->msgid); if (rc != LDAP_SUCCESS) { - ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "http_auth_ldap: ldap_sasl_bind() failed (%d: %s)", - rc, ldap_err2string(rc)); + ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "ngx_http_auth_ldap_check_bind: Cnx[%d] ldap_sasl_bind() failed (%d: %s)", + ctx->c->cnx_idx, rc, ldap_err2string(rc)); ctx->outcome = OUTCOME_ERROR; ngx_http_auth_ldap_return_connection(ctx->c); return NGX_ERROR; } - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http_auth_ldap: ldap_sasl_bind() -> msgid=%d", - ctx->c->msgid); + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_auth_ldap_check_bind: Cnx[%d] ldap_sasl_bind() -> msgid=%d", + ctx->c->cnx_idx, ctx->c->msgid); ctx->c->state = STATE_BINDING; ctx->iteration++; @@ -2307,11 +3009,12 @@ ngx_http_auth_ldap_check_bind(ngx_http_request_t *r, ngx_http_auth_ldap_ctx_t *c /* On the second call, process the operation result */ if (ctx->error_code != LDAP_SUCCESS) { - ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "http_auth_ldap: User bind failed (%d: %s)", - ctx->error_code, ldap_err2string(ctx->error_code)); + ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "ngx_http_auth_ldap_check_bind: Cnx[%d] User (dn: %s) bind failed (%d: %s)", + ctx->c->cnx_idx, ctx->user_dn.data, ctx->error_code, ldap_err2string(ctx->error_code)); ctx->outcome = OUTCOME_DENY; } else { - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http_auth_ldap: User bind successful"); + ngx_log_error(NGX_LOG_INFO, ctx->c->log, 0, "ngx_http_auth_ldap_check_bind: Cnx[%d] User (dn: %s) bind successfull", + ctx->c->cnx_idx, ctx->user_dn.data); ctx->outcome = OUTCOME_ALLOW; } return NGX_OK; @@ -2329,20 +3032,21 @@ ngx_http_auth_ldap_recover_bind(ngx_http_request_t *r, ngx_http_auth_ldap_ctx_t return NGX_AGAIN; } - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http_auth_ldap: Rebinding to binddn"); cred.bv_val = (char *) ctx->server->bind_dn_passwd.data; cred.bv_len = ctx->server->bind_dn_passwd.len; + ngx_log_error(NGX_LOG_INFO, ctx->c->log, 0, "ngx_http_auth_ldap_recover_bind: Cnx[%d] Rebinding to binddn ...", + ctx->c->cnx_idx); rc = ldap_sasl_bind(ctx->c->ld, (const char *) ctx->server->bind_dn.data, LDAP_SASL_SIMPLE, &cred, NULL, NULL, &ctx->c->msgid); if (rc != LDAP_SUCCESS) { - ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "http_auth_ldap: ldap_sasl_bind() failed (%d: %s)", - rc, ldap_err2string(rc)); + ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "ngx_http_auth_ldap_recover_bind: Cnx[%d] ldap_sasl_bind() failed (%d: %s)", + ctx->c->cnx_idx, rc, ldap_err2string(rc)); ctx->outcome = OUTCOME_ERROR; ngx_http_auth_ldap_return_connection(ctx->c); return NGX_ERROR; } - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http_auth_ldap: ldap_sasl_bind() -> msgid=%d", - ctx->c->msgid); + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_auth_ldap_recover_bind: Cnx[%d] ldap_sasl_bind() -> msgid=%d", + ctx->c->cnx_idx, ctx->c->msgid); ctx->c->state = STATE_BINDING; ctx->iteration++; return NGX_AGAIN; @@ -2350,10 +3054,12 @@ ngx_http_auth_ldap_recover_bind(ngx_http_request_t *r, ngx_http_auth_ldap_ctx_t /* On the second call, process the operation result */ if (ctx->error_code != LDAP_SUCCESS) { - ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "http_auth_ldap: binddn bind failed (%d: %s)", - ctx->error_code, ldap_err2string(ctx->error_code)); + ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "ngx_http_auth_ldap_recover_bind: Cnx[%d] Rebinding to binddn failed (%d: %s)", + ctx->c->cnx_idx, ctx->error_code, ldap_err2string(ctx->error_code)); } else { - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http_auth_ldap: binddn bind successful"); + + ngx_log_error(NGX_LOG_INFO, ctx->c->log, 0, "ngx_http_auth_ldap_recover_bind: Cnx[%d] Rebinding to binddn successful", + ctx->c->cnx_idx); } return NGX_OK; }