s3: auth: Use wbcAuthenticateUserEx to prime the caches.
[metze/samba/wip.git] / source3 / auth / auth_generic.c
index b76dcd7e8fe244412e2b7d60a42a65b45c33c477..f9b918496fd381f15dd7c1c13a84f73651a8d054 100644 (file)
 #include "auth/gensec/gensec.h"
 #include "lib/param/param.h"
 #ifdef HAVE_KRB5
-#include "libcli/auth/krb5_wrap.h"
+#include "auth/kerberos/pac_utils.h"
+#include "nsswitch/libwbclient/wbclient.h"
 #endif
 #include "librpc/crypto/gse.h"
 #include "auth/credentials/credentials.h"
+#include "lib/param/loadparm.h"
+#include "librpc/gen_ndr/dcerpc.h"
 
 static NTSTATUS auth3_generate_session_info_pac(struct auth4_context *auth_ctx,
                                                TALLOC_CTX *mem_ctx,
@@ -42,9 +45,8 @@ static NTSTATUS auth3_generate_session_info_pac(struct auth4_context *auth_ctx,
                                                struct auth_session_info **session_info)
 {
        TALLOC_CTX *tmp_ctx;
-       struct PAC_DATA *pac_data = NULL;
        struct PAC_LOGON_INFO *logon_info = NULL;
-       unsigned int i;
+       struct netr_SamInfo3 *info3_copy = NULL;
        bool is_mapped;
        bool is_guest;
        char *ntuser;
@@ -62,34 +64,57 @@ static NTSTATUS auth3_generate_session_info_pac(struct auth4_context *auth_ctx,
 
        if (pac_blob) {
 #ifdef HAVE_KRB5
-               status = kerberos_decode_pac(tmp_ctx,
-                                    *pac_blob,
-                                    NULL, NULL, NULL, NULL, 0, &pac_data);
-#else
-               status = NT_STATUS_ACCESS_DENIED;
-#endif
-               if (!NT_STATUS_IS_OK(status)) {
-                       goto done;
-               }
+               struct wbcAuthUserParams params = {};
+               struct wbcAuthUserInfo *info = NULL;
+               struct wbcAuthErrorInfo *err = NULL;
+               wbcErr wbc_err;
 
-               /* get logon name and logon info */
-               for (i = 0; i < pac_data->num_buffers; i++) {
-                       struct PAC_BUFFER *data_buf = &pac_data->buffers[i];
+               /*
+                * Let winbind decode the PAC.
+                * This will also store the user
+                * data in the netsamlogon cache.
+                *
+                * We need to do this *before* we
+                * call get_user_from_kerberos_info()
+                * as that does a user lookup that
+                * expects info in the netsamlogon cache.
+                *
+                * See BUG: https://bugzilla.samba.org/show_bug.cgi?id=11259
+                */
+               params.level = WBC_AUTH_USER_LEVEL_PAC;
+               params.password.pac.data = pac_blob->data;
+               params.password.pac.length = pac_blob->length;
 
-                       switch (data_buf->type) {
-                       case PAC_TYPE_LOGON_INFO:
-                               if (!data_buf->info) {
-                                       break;
-                               }
-                               logon_info = data_buf->info->logon_info.info;
+               become_root();
+               wbc_err = wbcAuthenticateUserEx(&params, &info, &err);
+               unbecome_root();
+
+               /*
+                * As this is merely a cache prime
+                * WBC_ERR_WINBIND_NOT_AVAILABLE
+                * is not a fatal error, treat it
+                * as success.
+                */
+
+               switch (wbc_err) {
+                       case WBC_ERR_WINBIND_NOT_AVAILABLE:
+                       case WBC_ERR_SUCCESS:
                                break;
+                       case WBC_ERR_AUTH_ERROR:
+                               status = NT_STATUS(err->nt_status);
+                               wbcFreeMemory(err);
+                               goto done;
                        default:
-                               break;
-                       }
+                               status = NT_STATUS_LOGON_FAILURE;
+                               goto done;
                }
-               if (!logon_info) {
-                       DEBUG(1, ("Invalid PAC data, missing logon info!\n"));
-                       status = NT_STATUS_NOT_FOUND;
+
+               status = kerberos_pac_logon_info(tmp_ctx, *pac_blob, NULL, NULL,
+                                                NULL, NULL, 0, &logon_info);
+#else
+               status = NT_STATUS_ACCESS_DENIED;
+#endif
+               if (!NT_STATUS_IS_OK(status)) {
                        goto done;
                }
        }
@@ -116,26 +141,31 @@ static NTSTATUS auth3_generate_session_info_pac(struct auth4_context *auth_ctx,
                                             &ntuser, &ntdomain,
                                             &username, &pw);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1, ("Failed to map kerberos principal to system user "
-                         "(%s)\n", nt_errstr(status)));
+               DBG_NOTICE("Failed to map kerberos principal to system user "
+                         "(%s)\n", nt_errstr(status));
                status = NT_STATUS_ACCESS_DENIED;
                goto done;
        }
 
-       /* save the PAC data if we have it */
+       /* Get the info3 from the PAC data if we have it */
        if (logon_info) {
-               netsamlogon_cache_store(ntuser, &logon_info->info3);
+               status = create_info3_from_pac_logon_info(tmp_ctx,
+                                       logon_info,
+                                       &info3_copy);
+               if (!NT_STATUS_IS_OK(status)) {
+                       goto done;
+               }
        }
 
        /* setup the string used by %U */
        sub_set_smb_name(username);
 
        /* reload services so that the new %U is taken into account */
-       lp_load(get_dyn_CONFIGFILE(), false, false, true, true);
+       lp_load_with_shares(get_dyn_CONFIGFILE());
 
        status = make_session_info_krb5(mem_ctx,
                                        ntuser, ntdomain, username, pw,
-                                       logon_info, is_guest, is_mapped, NULL /* No session key for now, caller will sort it out */,
+                                       info3_copy, is_guest, is_mapped, NULL /* No session key for now, caller will sort it out */,
                                        session_info);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(1, ("Failed to map kerberos pac to server info (%s)\n",
@@ -154,6 +184,53 @@ done:
        return status;
 }
 
+static struct auth4_context *make_auth4_context_s3(TALLOC_CTX *mem_ctx, struct auth_context *auth_context)
+{
+       struct auth4_context *auth4_context = talloc_zero(mem_ctx, struct auth4_context);
+       if (auth4_context == NULL) {
+               DEBUG(10, ("failed to allocate auth4_context failed\n"));
+               return NULL;
+       }
+       auth4_context->generate_session_info_pac = auth3_generate_session_info_pac;
+       auth4_context->generate_session_info = auth3_generate_session_info;
+       auth4_context->get_ntlm_challenge = auth3_get_challenge;
+       auth4_context->set_ntlm_challenge = auth3_set_challenge;
+       auth4_context->check_ntlm_password = auth3_check_password;
+       auth4_context->private_data = talloc_steal(auth4_context, auth_context);
+       return auth4_context;
+}
+
+NTSTATUS make_auth4_context(TALLOC_CTX *mem_ctx, struct auth4_context **auth4_context_out)
+{
+       struct auth_context *auth_context;
+       NTSTATUS nt_status;
+
+       TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
+       NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
+
+       nt_status = make_auth_context_subsystem(tmp_ctx, &auth_context);
+       if (!NT_STATUS_IS_OK(nt_status)) {
+               TALLOC_FREE(tmp_ctx);
+               return nt_status;
+       }
+
+       if (auth_context->make_auth4_context) {
+               nt_status = auth_context->make_auth4_context(auth_context, mem_ctx, auth4_context_out);
+               TALLOC_FREE(tmp_ctx);
+               return nt_status;
+
+       } else {
+               struct auth4_context *auth4_context = make_auth4_context_s3(tmp_ctx, auth_context);
+               if (auth4_context == NULL) {
+                       TALLOC_FREE(tmp_ctx);
+                       return NT_STATUS_NO_MEMORY;
+               }
+               *auth4_context_out = talloc_steal(mem_ctx, auth4_context);
+               TALLOC_FREE(tmp_ctx);
+               return NT_STATUS_OK;
+       }
+}
+
 NTSTATUS auth_generic_prepare(TALLOC_CTX *mem_ctx,
                              const struct tsocket_address *remote_address,
                              struct gensec_security **gensec_security_out)
@@ -172,32 +249,27 @@ NTSTATUS auth_generic_prepare(TALLOC_CTX *mem_ctx,
        }
 
        if (auth_context->prepare_gensec) {
-               nt_status = auth_context->prepare_gensec(tmp_ctx,
+               nt_status = auth_context->prepare_gensec(auth_context, tmp_ctx,
                                                         &gensec_security);
                if (!NT_STATUS_IS_OK(nt_status)) {
                        TALLOC_FREE(tmp_ctx);
                        return nt_status;
                }
        } else {
+               const struct gensec_security_ops **backends = NULL;
                struct gensec_settings *gensec_settings;
                struct loadparm_context *lp_ctx;
                size_t idx = 0;
                struct cli_credentials *server_credentials;
-               struct auth4_context *auth4_context = talloc_zero(tmp_ctx, struct auth4_context);
+               const char *dns_name;
+               const char *dns_domain;
+               struct auth4_context *auth4_context = make_auth4_context_s3(tmp_ctx, auth_context);
                if (auth4_context == NULL) {
-                       DEBUG(10, ("failed to allocate auth4_context failed\n"));
                        TALLOC_FREE(tmp_ctx);
                        return NT_STATUS_NO_MEMORY;
                }
-               auth4_context->generate_session_info_pac = auth3_generate_session_info_pac;
-               auth4_context->generate_session_info = auth3_generate_session_info;
-               auth4_context->get_challenge = auth3_get_challenge;
-               auth4_context->set_challenge = auth3_set_challenge;
-               auth4_context->challenge_may_be_modified = auth3_may_set_challenge;
-               auth4_context->check_password = auth3_check_password;
-               auth4_context->private_data = talloc_steal(auth4_context, auth_context);
-
-               lp_ctx = loadparm_init_s3(tmp_ctx, loadparm_s3_context());
+
+               lp_ctx = loadparm_init_s3(tmp_ctx, loadparm_s3_helpers());
                if (lp_ctx == NULL) {
                        DEBUG(10, ("loadparm_init_s3 failed\n"));
                        TALLOC_FREE(tmp_ctx);
@@ -211,22 +283,58 @@ NTSTATUS auth_generic_prepare(TALLOC_CTX *mem_ctx,
                        return NT_STATUS_NO_MEMORY;
                }
 
-               gensec_settings->backends = talloc_zero_array(gensec_settings,
-                                               struct gensec_security_ops *, 4);
-               if (gensec_settings->backends == NULL) {
+               /*
+                * This should be a 'netbios domain -> DNS domain'
+                * mapping, and can currently validly return NULL on
+                * poorly configured systems.
+                *
+                * This is used for the NTLMSSP server
+                *
+                */
+               dns_name = get_mydnsfullname();
+               if (dns_name == NULL) {
+                       dns_name = "";
+               }
+
+               dns_domain = get_mydnsdomname(tmp_ctx);
+               if (dns_domain == NULL) {
+                       dns_domain = "";
+               }
+
+               gensec_settings->server_dns_name = strlower_talloc(gensec_settings, dns_name);
+               if (gensec_settings->server_dns_name == NULL) {
                        TALLOC_FREE(tmp_ctx);
                        return NT_STATUS_NO_MEMORY;
                }
 
-               gensec_settings->backends[idx++] = &gensec_ntlmssp3_server_ops;
+               gensec_settings->server_dns_domain = strlower_talloc(gensec_settings, dns_domain);
+               if (gensec_settings->server_dns_domain == NULL) {
+                       TALLOC_FREE(tmp_ctx);
+                       return NT_STATUS_NO_MEMORY;
+               }
 
-#if defined(HAVE_KRB5) && defined(HAVE_GSS_WRAP_IOV)
-               gensec_settings->backends[idx++] = &gensec_gse_krb5_security_ops;
-#endif
+               backends = talloc_zero_array(gensec_settings,
+                                            const struct gensec_security_ops *, 6);
+               if (backends == NULL) {
+                       TALLOC_FREE(tmp_ctx);
+                       return NT_STATUS_NO_MEMORY;
+               }
+               gensec_settings->backends = backends;
 
                gensec_init();
-               gensec_settings->backends[idx++] = gensec_security_by_oid(NULL,
-                                                       GENSEC_OID_SPNEGO);
+
+               /* These need to be in priority order, krb5 before NTLMSSP */
+#if defined(HAVE_KRB5)
+               backends[idx++] = &gensec_gse_krb5_security_ops;
+#endif
+
+               backends[idx++] = gensec_security_by_oid(NULL, GENSEC_OID_NTLMSSP);
+
+               backends[idx++] = gensec_security_by_oid(NULL, GENSEC_OID_SPNEGO);
+
+               backends[idx++] = gensec_security_by_auth_type(NULL, DCERPC_AUTH_TYPE_SCHANNEL);
+
+               backends[idx++] = gensec_security_by_auth_type(NULL, DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM);
 
                /*
                 * This is anonymous for now, because we just use it
@@ -273,3 +381,29 @@ NTSTATUS auth_generic_prepare(TALLOC_CTX *mem_ctx,
        TALLOC_FREE(tmp_ctx);
        return NT_STATUS_OK;
 }
+
+NTSTATUS auth_check_password_session_info(struct auth4_context *auth_context,
+                                         TALLOC_CTX *mem_ctx,
+                                         struct auth_usersupplied_info *user_info,
+                                         struct auth_session_info **session_info)
+{
+       NTSTATUS nt_status;
+       void *server_info;
+
+       nt_status = auth_context->check_ntlm_password(auth_context,
+                                                     talloc_tos(),
+                                                     user_info,
+                                                     &server_info, NULL, NULL);
+
+       if (NT_STATUS_IS_OK(nt_status)) {
+               nt_status = auth_context->generate_session_info(auth_context,
+                                                               mem_ctx,
+                                                               server_info,
+                                                               user_info->client.account_name,
+                                                               AUTH_SESSION_INFO_UNIX_TOKEN |
+                                                               AUTH_SESSION_INFO_DEFAULT_GROUPS,
+                                                               session_info);
+               TALLOC_FREE(server_info);
+       }
+       return nt_status;
+}