test ldap sign/seal
[metze/samba/wip.git] / source4 / libcli / ldap / ldap_bind.c
index 1f6ef77631fb954ad88ec88f34df4fe6ee294c80..5c0b1b4ac78fc1614ea88f5d033e4c9b8622c13a 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/ldap_proto.h"
 #include "libcli/ldap/ldap_client.h"
-#include "auth/auth.h"
+#include "lib/tls/tls.h"
+#include "auth/gensec/gensec.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;
+};
+
+_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,
+                                       conn->lp_ctx);
+               break;
+               
+       case LDAP_BIND_SIMPLE:
+               creds = (struct ldap_simple_creds *)conn->bind.creds;
+
+               if (creds == NULL) {
+                       return NT_STATUS_UNSUCCESSFUL;
+               }
+
+               status = ldap_bind_simple(conn, creds->dn, creds->pw);
+               break;
+
+       default:
+               return NT_STATUS_UNSUCCESSFUL;
+       }
+
+       return status;
+}
+
 
 static struct ldap_message *new_ldap_simple_bind_msg(struct ldap_connection *conn, 
                                                     const char *dn, const char *pw)
@@ -51,7 +89,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;
@@ -109,6 +147,20 @@ NTSTATUS ldap_bind_simple(struct ldap_connection *conn,
 
        talloc_free(req);
 
+       if (NT_STATUS_IS_OK(status)) {
+               struct ldap_simple_creds *creds = talloc(conn, struct ldap_simple_creds);
+               if (creds == NULL) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+               creds->dn = talloc_strdup(creds, dn);
+               creds->pw = talloc_strdup(creds, pw);
+               if (creds->dn == NULL || creds->pw == NULL) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+               conn->bind.type = LDAP_BIND_SIMPLE;
+               conn->bind.creds = creds;
+       }
+
        return status;
 }
 
@@ -129,7 +181,16 @@ static struct ldap_message *new_ldap_sasl_bind_msg(struct ldap_connection *conn,
        res->r.BindRequest.dn = "";
        res->r.BindRequest.mechanism = LDAP_AUTH_MECH_SASL;
        res->r.BindRequest.creds.SASL.mechanism = talloc_strdup(res, sasl_mechanism);
-       res->r.BindRequest.creds.SASL.secblob = *secblob;
+       if (secblob) {
+               res->r.BindRequest.creds.SASL.secblob = talloc(res, DATA_BLOB);
+               if (!res->r.BindRequest.creds.SASL.secblob) {
+                       talloc_free(res);
+                       return NULL;
+               }
+               *res->r.BindRequest.creds.SASL.secblob = *secblob;
+       } else {
+               res->r.BindRequest.creds.SASL.secblob = NULL;
+       }
        res->controls = NULL;
 
        return res;
@@ -139,7 +200,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;
@@ -152,21 +215,36 @@ NTSTATUS ldap_bind_sasl(struct ldap_connection *conn, struct cli_credentials *cr
        int count, i;
 
        const char **sasl_names;
-       const struct gensec_security_ops **mechs;
-       
+       uint32_t old_gensec_features;
        static const char *supported_sasl_mech_attrs[] = {
                "supportedSASLMechanisms", 
                NULL 
        };
 
-       status = gensec_client_start(conn, &conn->gensec, NULL);
+       gensec_init(lp_ctx);
+
+       status = gensec_client_start(conn, &conn->gensec,
+                                    conn->event.event_ctx, 
+                                    lp_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;
        }
 
-       gensec_want_feature(conn->gensec, 0 | GENSEC_FEATURE_SIGN | GENSEC_FEATURE_SEAL);
+       /* require Kerberos SIGN/SEAL only if we don't use SSL
+        * Windows seem not to like double encryption */
+       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));
+       }
+
+       cli_credentials_set_gensec_features(creds,
+       (old_gensec_features & ~(GENSEC_FEATURE_SIGN|GENSEC_FEATURE_SEAL)) & GENSEC_FEATURE_SIGN);
 
+/*     cli_credentials_set_gensec_features(creds,
+       (old_gensec_features & ~(GENSEC_FEATURE_SIGN|GENSEC_FEATURE_SEAL)) & (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", 
@@ -174,11 +252,20 @@ NTSTATUS ldap_bind_sasl(struct ldap_connection *conn, struct cli_credentials *cr
                goto failed;
        }
 
-       status = gensec_set_target_hostname(conn->gensec, conn->host);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1, ("Failed to set GENSEC target hostname: %s\n", 
-                         nt_errstr(status)));
-               goto failed;
+       gensec_want_feature(conn->gensec, GENSEC_FEATURE_SIGN);
+       //gensec_want_feature(conn->gensec, GENSEC_FEATURE_SEAL);
+
+       /* 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)) {
+                       DEBUG(1, ("Failed to set GENSEC target hostname: %s\n", 
+                                 nt_errstr(status)));
+                       goto failed;
+               }
        }
 
        status = gensec_set_target_service(conn->gensec, "ldap");
@@ -189,7 +276,7 @@ NTSTATUS ldap_bind_sasl(struct ldap_connection *conn, struct cli_credentials *cr
        }
 
        status = ildap_search(conn, "", LDAP_SEARCH_SCOPE_BASE, "", supported_sasl_mech_attrs, 
-                             False, NULL, NULL, &sasl_mechs_msgs);
+                             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)));
@@ -208,7 +295,7 @@ NTSTATUS ldap_bind_sasl(struct ldap_connection *conn, struct cli_credentials *cr
 
        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",
+               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;
        }
@@ -225,17 +312,10 @@ NTSTATUS ldap_bind_sasl(struct ldap_connection *conn, struct cli_credentials *cr
        }
        sasl_names[i] = NULL;
        
-       mechs = gensec_security_by_sasl(conn->gensec, tmp_ctx, sasl_names);
-       if (!mechs || !mechs[0]) {
-               DEBUG(1, ("None of the %d proposed SASL mechs were acceptable\n",
-                         count));
-               goto failed;
-       }
-
-       status = gensec_start_mech_by_ops(conn->gensec, mechs[0]);
+       status = gensec_start_mech_by_sasl_list(conn->gensec, sasl_names);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1, ("Failed to set GENSEC client mechanism: %s/%s %s\n",
-                         mechs[0]->name, mechs[0]->sasl_name, nt_errstr(status)));
+               DEBUG(1, ("None of the %d proposed SASL mechs were acceptable: %s\n",
+                         count, nt_errstr(status)));
                goto failed;
        }
 
@@ -265,11 +345,12 @@ NTSTATUS ldap_bind_sasl(struct ldap_connection *conn, struct cli_credentials *cr
                    !NT_STATUS_IS_OK(status)) {
                        break;
                }
-               if (output.length == 0) {
+               if (NT_STATUS_IS_OK(status) && output.length == 0) {
                        break;
                }
 
-               msg = new_ldap_sasl_bind_msg(tmp_ctx, "GSS-SPNEGO", &output);
+               /* Perhaps we should make gensec_start_mech_by_sasl_list() return the name we got? */
+               msg = new_ldap_sasl_bind_msg(tmp_ctx, conn->gensec->ops->sasl_name, (output.data?&output:NULL));
                if (msg == NULL) {
                        status = NT_STATUS_NO_MEMORY;
                        goto failed;
@@ -304,16 +385,33 @@ NTSTATUS ldap_bind_sasl(struct ldap_connection *conn, struct cli_credentials *cr
                if (!NT_STATUS_EQUAL(gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
                        break;
                }
-               input = response->r.BindResponse.SASL.secblob;
+               if (response->r.BindResponse.SASL.secblob) {
+                       input = *response->r.BindResponse.SASL.secblob;
+               } else {
+                       input = data_blob(NULL, 0);
+               }
        }
 
-       if (NT_STATUS_IS_OK(status) &&
-           (gensec_have_feature(conn->gensec, GENSEC_FEATURE_SEAL) ||
-            gensec_have_feature(conn->gensec, GENSEC_FEATURE_SIGN))) {
-               conn->enable_wrap = True;
+       talloc_free(tmp_ctx);
+
+       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,
+                                           conn,
+                                           &sasl_socket);
+               if (!NT_STATUS_IS_OK(status)) goto failed;
+
+               conn->sock = sasl_socket;
+               packet_set_socket(conn->packet, conn->sock);
+
+               conn->bind.type = LDAP_BIND_SASL;
+               conn->bind.creds = creds;
        }
 
-       talloc_free(tmp_ctx);
        return status;
 
 failed: