Revert "ldap sign|seal"
[metze/samba/wip.git] / source4 / libcli / ldap / ldap_bind.c
index c4c731e4f5401939b55431a60bad0ae491b3fc0a..b355e18e0d31ba9b50e74dc396a5d84720d91fc1 100644 (file)
@@ -8,7 +8,7 @@
     
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
 */
 
 #include "includes.h"
-#include "libcli/ldap/ldap.h"
+#include "libcli/ldap/libcli_ldap.h"
+#include "libcli/ldap/ldap_proto.h"
 #include "libcli/ldap/ldap_client.h"
 #include "lib/tls/tls.h"
 #include "auth/gensec/gensec.h"
-#include "auth/gensec/socket.h"
+#include "auth/gensec/gensec_socket.h"
+#include "auth/credentials/credentials.h"
 #include "lib/stream/packet.h"
+#include "param/param.h"
 
 struct ldap_simple_creds {
        const char *dn;
        const char *pw;
 };
 
-NTSTATUS ldap_rebind(struct ldap_connection *conn)
+_PUBLIC_ NTSTATUS ldap_rebind(struct ldap_connection *conn)
 {
        NTSTATUS status;
        struct ldap_simple_creds *creds;
 
        switch (conn->bind.type) {
        case LDAP_BIND_SASL:
-               status = ldap_bind_sasl(conn, (struct cli_credentials *)conn->bind.creds);
+               status = ldap_bind_sasl(conn, (struct cli_credentials *)conn->bind.creds,
+                                       conn->lp_ctx);
                break;
                
        case LDAP_BIND_SIMPLE:
@@ -87,7 +90,7 @@ static struct ldap_message *new_ldap_simple_bind_msg(struct ldap_connection *con
 /*
   perform a simple username/password bind
 */
-NTSTATUS ldap_bind_simple(struct ldap_connection *conn, 
+_PUBLIC_ NTSTATUS ldap_bind_simple(struct ldap_connection *conn, 
                          const char *userdn, const char *password)
 {
        struct ldap_request *req;
@@ -198,7 +201,9 @@ static struct ldap_message *new_ldap_sasl_bind_msg(struct ldap_connection *conn,
 /*
   perform a sasl bind using the given credentials
 */
-NTSTATUS ldap_bind_sasl(struct ldap_connection *conn, struct cli_credentials *creds)
+_PUBLIC_ NTSTATUS ldap_bind_sasl(struct ldap_connection *conn,
+                       struct cli_credentials *creds,
+                       struct loadparm_context *lp_ctx)
 {
        NTSTATUS status;
        TALLOC_CTX *tmp_ctx = NULL;
@@ -211,13 +216,61 @@ NTSTATUS ldap_bind_sasl(struct ldap_connection *conn, struct cli_credentials *cr
        int count, i;
 
        const char **sasl_names;
-       
+       uint32_t old_gensec_features;
        static const char *supported_sasl_mech_attrs[] = {
                "supportedSASLMechanisms", 
                NULL 
        };
+       unsigned int logon_retries = 0;
 
-       status = gensec_client_start(conn, &conn->gensec, NULL);
+       status = ildap_search(conn, "", LDAP_SEARCH_SCOPE_BASE, "", supported_sasl_mech_attrs,
+                             false, NULL, NULL, &sasl_mechs_msgs);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("Failed to inquire of target's available sasl mechs in rootdse search: %s\n",
+                         nt_errstr(status)));
+               goto failed;
+       }
+
+       count = ildap_count_entries(conn, sasl_mechs_msgs);
+       if (count != 1) {
+               DEBUG(1, ("Failed to inquire of target's available sasl mechs in rootdse search: wrong number of replies: %d\n",
+                         count));
+               goto failed;
+       }
+
+       tmp_ctx = talloc_new(conn);
+       if (tmp_ctx == NULL) goto failed;
+
+       search = &sasl_mechs_msgs[0]->r.SearchResultEntry;
+       if (search->num_attributes != 1) {
+               DEBUG(1, ("Failed to inquire of target's available sasl mechs in rootdse search: wrong number of attributes: %d != 1\n",
+                         search->num_attributes));
+               goto failed;
+       }
+
+       sasl_names = talloc_array(tmp_ctx, const char *, search->attributes[0].num_values + 1);
+       if (!sasl_names) {
+               DEBUG(1, ("talloc_arry(char *, %d) failed\n",
+                         count));
+               goto failed;
+       }
+
+       for (i=0; i<search->attributes[0].num_values; i++) {
+               sasl_names[i] = (const char *)search->attributes[0].values[i].data;
+       }
+       sasl_names[i] = NULL;
+
+       gensec_init();
+
+try_logon_again:
+       /*
+         we loop back here on a logon failure, and re-create the
+         gensec session. The logon_retries counter ensures we don't
+         loop forever.
+        */
+
+       status = gensec_client_start(conn, &conn->gensec,
+                                    lpcfg_gensec_settings(conn, lp_ctx));
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("Failed to start GENSEC engine (%s)\n", nt_errstr(status)));
                goto failed;
@@ -225,10 +278,12 @@ NTSTATUS ldap_bind_sasl(struct ldap_connection *conn, struct cli_credentials *cr
 
        /* require Kerberos SIGN/SEAL only if we don't use SSL
         * Windows seem not to like double encryption */
-       if (!tls_enabled(conn->sock)) {
-               gensec_want_feature(conn->gensec, 0 | GENSEC_FEATURE_SIGN | GENSEC_FEATURE_SEAL);
+       old_gensec_features = cli_credentials_get_gensec_features(creds);
+       if (tls_enabled(conn->sock)) {
+               cli_credentials_set_gensec_features(creds, old_gensec_features & ~(GENSEC_FEATURE_SIGN|GENSEC_FEATURE_SEAL));
        }
 
+       /* this call also sets the gensec_want_features */
        status = gensec_set_credentials(conn->gensec, creds);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(1, ("Failed to set GENSEC creds: %s\n", 
@@ -236,6 +291,10 @@ NTSTATUS ldap_bind_sasl(struct ldap_connection *conn, struct cli_credentials *cr
                goto failed;
        }
 
+       /* reset the original gensec_features (on the credentials
+        * context, so we don't tatoo it ) */
+       cli_credentials_set_gensec_features(creds, old_gensec_features);
+
        if (conn->host) {
                status = gensec_set_target_hostname(conn->gensec, conn->host);
                if (!NT_STATUS_IS_OK(status)) {
@@ -252,43 +311,6 @@ NTSTATUS ldap_bind_sasl(struct ldap_connection *conn, struct cli_credentials *cr
                goto failed;
        }
 
-       status = ildap_search(conn, "", LDAP_SEARCH_SCOPE_BASE, "", supported_sasl_mech_attrs, 
-                             False, NULL, NULL, &sasl_mechs_msgs);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1, ("Failed to inquire of target's available sasl mechs in rootdse search: %s\n", 
-                         nt_errstr(status)));
-               goto failed;
-       }
-       
-       count = ildap_count_entries(conn, sasl_mechs_msgs);
-       if (count != 1) {
-               DEBUG(1, ("Failed to inquire of target's available sasl mechs in rootdse search: wrong number of replies: %d\n",
-                         count));
-               goto failed;
-       }
-
-       tmp_ctx = talloc_new(conn);
-       if (tmp_ctx == NULL) goto failed;
-
-       search = &sasl_mechs_msgs[0]->r.SearchResultEntry;
-       if (search->num_attributes != 1) {
-               DEBUG(1, ("Failed to inquire of target's available sasl mechs in rootdse search: wrong number of attributes: %d\n",
-                         search->num_attributes));
-               goto failed;
-       }
-
-       sasl_names = talloc_array(tmp_ctx, const char *, search->attributes[0].num_values + 1);
-       if (!sasl_names) {
-               DEBUG(1, ("talloc_arry(char *, %d) failed\n",
-                         count));
-               goto failed;
-       }
-               
-       for (i=0; i<search->attributes[0].num_values; i++) {
-               sasl_names[i] = (const char *)search->attributes[0].values[i].data;
-       }
-       sasl_names[i] = NULL;
-       
        status = gensec_start_mech_by_sasl_list(conn->gensec, sasl_names);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(1, ("None of the %d proposed SASL mechs were acceptable: %s\n",
@@ -304,6 +326,7 @@ NTSTATUS ldap_bind_sasl(struct ldap_connection *conn, struct cli_credentials *cr
                int result = LDAP_OTHER;
        
                status = gensec_update(conn->gensec, tmp_ctx,
+                                      conn->event.event_ctx,
                                       input,
                                       &output);
                /* The status value here, from GENSEC is vital to the security
@@ -338,7 +361,7 @@ NTSTATUS ldap_bind_sasl(struct ldap_connection *conn, struct cli_credentials *cr
                        status = NT_STATUS_NO_MEMORY;
                        goto failed;
                }
-               talloc_steal(tmp_ctx, req);
+               talloc_reparent(conn, tmp_ctx, req);
 
                status = ldap_result_n(req, 0, &response);
                if (!NT_STATUS_IS_OK(status)) {
@@ -352,6 +375,40 @@ NTSTATUS ldap_bind_sasl(struct ldap_connection *conn, struct cli_credentials *cr
 
                result = response->r.BindResponse.response.resultcode;
 
+               if (result == LDAP_INVALID_CREDENTIALS) {
+                       /*
+                         try a second time on invalid credentials, to
+                         give the user a chance to re-enter the
+                         password and to handle the case where our
+                         kerberos ticket is invalid as the server
+                         password has changed
+                       */
+                       const char *principal;
+
+                       principal = gensec_get_target_principal(conn->gensec);
+                       if (principal == NULL) {
+                               const char *hostname = gensec_get_target_hostname(conn->gensec);
+                               const char *service  = gensec_get_target_service(conn->gensec);
+                               if (hostname != NULL && service != NULL) {
+                                       principal = talloc_asprintf(tmp_ctx, "%s/%s", service, hostname);
+                               }
+                       }
+
+                       if (cli_credentials_failed_kerberos_login(creds, principal, &logon_retries) ||
+                           cli_credentials_wrong_password(creds)) {
+                               /*
+                                 destroy our gensec session and loop
+                                 back up to the top to retry,
+                                 offering the user a chance to enter
+                                 new credentials, or get a new ticket
+                                 if using kerberos
+                                */
+                               talloc_free(conn->gensec);
+                               conn->gensec = NULL;
+                               goto try_logon_again;
+                       }
+               }
+
                if (result != LDAP_SUCCESS && result != LDAP_SASL_BIND_IN_PROGRESS) {
                        status = ldap_check_response(conn, 
                                                     &response->r.BindResponse.response);
@@ -374,6 +431,7 @@ NTSTATUS ldap_bind_sasl(struct ldap_connection *conn, struct cli_credentials *cr
        if (NT_STATUS_IS_OK(status)) {
                struct socket_context *sasl_socket;
                status = gensec_socket_init(conn->gensec, 
+                                           conn,
                                            conn->sock,
                                            conn->event.event_ctx, 
                                            ldap_read_io_handler,
@@ -381,8 +439,6 @@ NTSTATUS ldap_bind_sasl(struct ldap_connection *conn, struct cli_credentials *cr
                                            &sasl_socket);
                if (!NT_STATUS_IS_OK(status)) goto failed;
 
-               talloc_steal(conn->sock, sasl_socket);
-               talloc_unlink(conn, conn->sock);
                conn->sock = sasl_socket;
                packet_set_socket(conn->packet, conn->sock);