s4:kdc Streamline client access verification call
[ira/wip.git] / source4 / kdc / pac-glue.c
index 1a7ef29ad1d4cddbc24f3beed3976f1fdfc742db..a311777f2b03b2d5792ca757f9e74439e3f4f007 100644 (file)
-/* 
+/*
    Unix SMB/CIFS implementation.
 
    PAC Glue between Samba and the KDC
-   
-   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
+
+   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2009
+   Copyright (C) Simo Sorce <idra@samba.org> 2010
 
    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,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    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 "kdc/kdc.h"
-#include "include/ads.h"
+#include "../libds/common/flags.h"
 #include "lib/ldb/include/ldb.h"
+#include "librpc/gen_ndr/ndr_krb5pac.h"
 #include "librpc/gen_ndr/krb5pac.h"
 #include "auth/auth.h"
 #include "auth/auth_sam.h"
+#include "auth/auth_sam_reply.h"
+#include "kdc/kdc.h"
+#include "param/param.h"
 
-struct krb5_dh_moduli;
-struct _krb5_krb_auth_data;
-
-#include "heimdal/lib/krb5/krb5-private.h"
-
-/* Given the right private pointer from hdb_ldb, get a PAC from the attached ldb messages */
-static krb5_error_code samba_get_pac(krb5_context context, 
-                                    struct hdb_ldb_private *private,
-                                    krb5_principal client, 
-                                    krb5_keyblock *krbtgt_keyblock, 
-                                    krb5_keyblock *server_keyblock, 
-                                    time_t tgs_authtime,
-                                    krb5_data *pac)
+NTSTATUS samba_get_logon_info_pac_blob(TALLOC_CTX *mem_ctx,
+                                      struct smb_iconv_convenience *ic,
+                                      struct auth_serversupplied_info *info,
+                                      DATA_BLOB *pac_data)
 {
-       krb5_error_code ret;
+       struct netr_SamInfo3 *info3;
+       union PAC_INFO pac_info;
+       enum ndr_err_code ndr_err;
        NTSTATUS nt_status;
-       struct auth_serversupplied_info *server_info;
-       DATA_BLOB tmp_blob;
-       TALLOC_CTX *mem_ctx = talloc_named(private, 0, "samba_get_pac context");
 
-       if (!mem_ctx) {
-               return ENOMEM;
-       }
+       ZERO_STRUCT(pac_info);
 
-       nt_status = authsam_make_server_info(mem_ctx, private->samdb, 
-                                            private->msg, 
-                                            private->realm_ref_msg,
-                                            data_blob(NULL, 0),
-                                            data_blob(NULL, 0),
-                                            &server_info);
+       nt_status = auth_convert_server_info_saminfo3(mem_ctx, info, &info3);
        if (!NT_STATUS_IS_OK(nt_status)) {
-               DEBUG(0, ("Getting user info for PAC failed: %s\n",
+               DEBUG(1, ("Getting Samba info failed: %s\n",
                          nt_errstr(nt_status)));
-               return ENOMEM;
+               return nt_status;
        }
 
-       ret = kerberos_create_pac(mem_ctx, server_info, 
-                                 context, 
-                                 krbtgt_keyblock,
-                                 server_keyblock,
-                                 client,
-                                 tgs_authtime,
-                                 &tmp_blob);
-
-       if (ret) {
-               DEBUG(1, ("PAC encoding failed: %s\n", 
-                         smb_get_krb5_error_message(context, ret, mem_ctx)));
-               talloc_free(mem_ctx);
-               return ret;
+       pac_info.logon_info.info = talloc_zero(mem_ctx, struct PAC_LOGON_INFO);
+       if (!mem_ctx) {
+               return NT_STATUS_NO_MEMORY;
        }
 
-       ret = krb5_data_copy(pac, tmp_blob.data, tmp_blob.length);
-       talloc_free(mem_ctx);
-       return ret;
-}
-
-/* Wrap the PAC in the right ASN.1.  Will always free 'pac', on success or failure */
-static krb5_error_code wrap_pac(krb5_context context, krb5_data *pac, AuthorizationData **out) 
-{
-       krb5_error_code ret;
-
-       unsigned char *buf;
-       size_t buf_size;
-       size_t len;
-       
-       AD_IF_RELEVANT if_relevant;
-       AuthorizationData *auth_data;
-
-       if_relevant.len = 1;
-       if_relevant.val = malloc(sizeof(*if_relevant.val));
-       if (!if_relevant.val) {
-               krb5_data_free(pac);
-               *out = NULL;
-               return ENOMEM;
-       }
+       pac_info.logon_info.info->info3 = *info3;
 
-       if_relevant.val[0].ad_type = KRB5_AUTHDATA_WIN2K_PAC;
-       if_relevant.val[0].ad_data.data = NULL;
-       if_relevant.val[0].ad_data.length = 0;
-       
-       /* pac.data will be freed with this */
-       if_relevant.val[0].ad_data.data = pac->data;
-       if_relevant.val[0].ad_data.length = pac->length;
-       
-       ASN1_MALLOC_ENCODE(AuthorizationData, buf, buf_size, &if_relevant, &len, ret);
-       free_AuthorizationData(&if_relevant);
-       if (ret) {
-               *out = NULL;
-               return ret;
-       }               
-       
-       auth_data = malloc(sizeof(*auth_data));
-       if (!auth_data) {
-               free(buf);
-               *out = NULL;
-               return ret;
-       }               
-       auth_data->len = 1;
-       auth_data->val = malloc(sizeof(*auth_data->val));
-       if (!auth_data->val) {
-               free(buf);
-               free(auth_data);
-               *out = NULL;
-               return ret;
+       ndr_err = ndr_push_union_blob(pac_data, mem_ctx, ic, &pac_info,
+                                     PAC_TYPE_LOGON_INFO,
+                                     (ndr_push_flags_fn_t)ndr_push_PAC_INFO);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               nt_status = ndr_map_error2ntstatus(ndr_err);
+               DEBUG(1, ("PAC (presig) push failed: %s\n",
+                         nt_errstr(nt_status)));
+               return nt_status;
        }
-       auth_data->val[0].ad_type = KRB5_AUTHDATA_IF_RELEVANT;
-       auth_data->val[0].ad_data.length = len;
-       auth_data->val[0].ad_data.data = buf;
 
-       *out = auth_data;
-       return 0;
+       return NT_STATUS_OK;
 }
 
-
-/* Given a hdb_entry, create a PAC out of the private data 
-
-   Don't create it if the client has the UF_NO_AUTH_DATA_REQUIRED bit
-   set, or if they specificaly asked not to get it.
-*/
-
-krb5_error_code hdb_ldb_authz_data_as_req(krb5_context context, struct hdb_entry_ex *entry_ex, 
-                                          METHOD_DATA* pa_data_seq,
-                                          time_t authtime,
-                                          EncryptionKey *tgtkey,
-                                          EncryptionKey *sessionkey,
-                                          AuthorizationData **out)
+krb5_error_code samba_make_krb5_pac(krb5_context context,
+                                   DATA_BLOB *pac_blob,
+                                   krb5_pac *pac)
 {
+       krb5_data pac_data;
        krb5_error_code ret;
-       int i;
-       krb5_data pac;
-       krb5_boolean pac_wanted = TRUE;
-       unsigned int userAccountControl;
-       struct PA_PAC_REQUEST pac_request;
-       struct hdb_ldb_private *private = talloc_get_type(entry_ex->ctx, struct hdb_ldb_private);
-       
-       /* The user account may be set not to want the PAC */
-       userAccountControl = ldb_msg_find_uint(private->msg, "userAccountControl", 0);
-       if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
-               *out = NULL;
-               return 0;
-       }
 
-       /* The user may not want a PAC */
-       for (i=0; i<pa_data_seq->len; i++) {
-               if (pa_data_seq->val[i].padata_type == KRB5_PADATA_PA_PAC_REQUEST) {
-                       ret = decode_PA_PAC_REQUEST(pa_data_seq->val[i].padata_value.data, 
-                                                   pa_data_seq->val[i].padata_value.length, 
-                                                   &pac_request, NULL);
-                       if (ret == 0) {
-                               pac_wanted = !!pac_request.include_pac;
-                       }
-                       free_PA_PAC_REQUEST(&pac_request);
-                       break;
-               }
+       ret = krb5_data_copy(&pac_data, pac_blob->data, pac_blob->length);
+       if (ret != 0) {
+               return ret;
        }
 
-       if (!pac_wanted) {
-               *out = NULL;
-               return 0;
-       }       
-
-       /* Get PAC from Samba */
-       ret = samba_get_pac(context,
-                           private,
-                           entry_ex->entry.principal,
-                           tgtkey,
-                           tgtkey,
-                           authtime,
-                           &pac);
+       ret = krb5_pac_init(context, pac);
+       if (ret != 0) {
+               krb5_data_free(&pac_data);
+               return ret;
+       }
 
-       if (ret) {
-               *out = NULL;
+       ret = krb5_pac_add_buffer(context, *pac, PAC_TYPE_LOGON_INFO, &pac_data);
+       krb5_data_free(&pac_data);
+       if (ret != 0) {
                return ret;
        }
-       
-       return wrap_pac(context, &pac, out);
-}
 
-/* Resign (and reform, including possibly new groups) a PAC */
+       return ret;
+}
 
-krb5_error_code hdb_ldb_authz_data_tgs_req(krb5_context context, struct hdb_entry_ex *entry_ex, 
-                                           krb5_principal client, 
-                                           AuthorizationData *in, 
-                                           time_t authtime,
-                                           EncryptionKey *tgtkey,
-                                           EncryptionKey *servicekey,
-                                           EncryptionKey *sessionkey,
-                                           AuthorizationData **out)
+bool samba_princ_needs_pac(struct hdb_entry_ex *princ)
 {
-       NTSTATUS nt_status;
-       krb5_error_code ret;
 
+       struct samba_kdc_entry *p = talloc_get_type(princ->ctx, struct samba_kdc_entry);
        unsigned int userAccountControl;
 
-       struct hdb_ldb_private *private = talloc_get_type(entry_ex->ctx, struct hdb_ldb_private);
-       krb5_data k5pac_in, k5pac_out;
-       DATA_BLOB pac_in, pac_out;
 
-       struct PAC_LOGON_INFO *logon_info;
-       union netr_Validation validation;
-       struct auth_serversupplied_info *server_info_out;
-
-       krb5_boolean found = FALSE;
-       TALLOC_CTX *mem_ctx;
-       
        /* The service account may be set not to want the PAC */
-       userAccountControl = ldb_msg_find_uint(private->msg, "userAccountControl", 0);
+       userAccountControl = ldb_msg_find_attr_as_uint(p->msg, "userAccountControl", 0);
        if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
-               *out = NULL;
-               return 0;
+               return false;
        }
 
-       ret = _krb5_find_type_in_ad(context, KRB5_AUTHDATA_WIN2K_PAC,
-                                   &k5pac_in, &found, sessionkey, in);
-       if (ret || !found) {
-               *out = NULL;
-               return 0;
-       }
+       return true;
+}
 
-       mem_ctx = talloc_new(private);
-       if (!mem_ctx) {
-               krb5_data_free(&k5pac_in);
-               *out = NULL;
-               return ENOMEM;
-       }
+NTSTATUS samba_kdc_get_pac_blob(TALLOC_CTX *mem_ctx,
+                               struct hdb_entry_ex *client,
+                               DATA_BLOB **_pac_blob)
+{
+       struct samba_kdc_entry *p = talloc_get_type(client->ctx, struct samba_kdc_entry);
+       struct auth_serversupplied_info *server_info;
+       DATA_BLOB *pac_blob;
+       NTSTATUS nt_status;
 
-       pac_in = data_blob_talloc(mem_ctx, k5pac_in.data, k5pac_in.length);
-       krb5_data_free(&k5pac_in);
-       if (!pac_in.data) {
-               talloc_free(mem_ctx);
-               *out = NULL;
-               return ENOMEM;
+       /* The user account may be set not to want the PAC */
+       if ( ! samba_princ_needs_pac(client)) {
+               *_pac_blob = NULL;
+               return NT_STATUS_OK;
        }
-               
-       /* Parse the PAC again, for the logon info */
-       nt_status = kerberos_pac_logon_info(mem_ctx, &logon_info,
-                                           pac_in,
-                                           context,
-                                           tgtkey, 
-                                           tgtkey, 
-                                           client, authtime, 
-                                           &ret);
 
-       if (!NT_STATUS_IS_OK(nt_status)) {
-               DEBUG(1, ("Failed to parse PAC in TGT: %s/%s\n", 
-                         nt_errstr(nt_status), error_message(ret)));
-               talloc_free(mem_ctx);
-               *out = NULL;
-               return ret;
+       pac_blob = talloc_zero(mem_ctx, DATA_BLOB);
+       if (!pac_blob) {
+               return NT_STATUS_NO_MEMORY;
        }
 
-       /* Pull this right into the normal auth sysstem structures */
-       validation.sam3 = &logon_info->info3;
-       nt_status = make_server_info_netlogon_validation(mem_ctx,
-                                                        "",
-                                                        3, &validation,
-                                                        &server_info_out); 
+       nt_status = authsam_make_server_info(mem_ctx, p->kdc_db_ctx->samdb,
+                                            lp_netbios_name(p->kdc_db_ctx->lp_ctx),
+                                            lp_sam_name(p->kdc_db_ctx->lp_ctx),
+                                            p->realm_dn,
+                                            p->msg,
+                                            data_blob(NULL, 0),
+                                            data_blob(NULL, 0),
+                                            &server_info);
        if (!NT_STATUS_IS_OK(nt_status)) {
-               talloc_free(mem_ctx);
-               *out = NULL;
-               return ENOMEM;
-       }
-
-       /* And make a new PAC, possibly containing new groups */
-       ret = kerberos_create_pac(mem_ctx, 
-                                 server_info_out,
-                                 context,
-                                 tgtkey,
-                                 servicekey,
-                                 client,
-                                 authtime,
-                                 &pac_out);
-
-       if (ret != 0) {
-               talloc_free(mem_ctx);
-               *out = NULL;
-               return ret;
+               DEBUG(0, ("Getting user info for PAC failed: %s\n",
+                         nt_errstr(nt_status)));
+               return nt_status;
        }
 
-       ret = krb5_data_copy(&k5pac_out, pac_out.data, pac_out.length);
-       if (ret != 0) {
-               talloc_free(mem_ctx);
-               *out = NULL;
-               return ret;
+       nt_status = samba_get_logon_info_pac_blob(mem_ctx,
+                                                 p->kdc_db_ctx->ic_ctx,
+                                                 server_info, pac_blob);
+       if (!NT_STATUS_IS_OK(nt_status)) {
+               DEBUG(0, ("Building PAC failed: %s\n",
+                         nt_errstr(nt_status)));
+               return nt_status;
        }
 
-       return wrap_pac(context, &k5pac_out, out);
+       *_pac_blob = pac_blob;
+       return NT_STATUS_OK;
 }
 
-/* Given an hdb entry (and in particular it's private member), consult
- * the account_ok routine in auth/auth_sam.c for consistancy */
-
-krb5_error_code hdb_ldb_check_client_access(krb5_context context, hdb_entry_ex *entry_ex, 
-                                           HostAddresses *addresses)
+NTSTATUS samba_kdc_update_pac_blob(TALLOC_CTX *mem_ctx,
+                                  krb5_context context,
+                                  struct smb_iconv_convenience *ic,
+                                  krb5_pac *pac, DATA_BLOB *pac_blob)
 {
+       struct auth_serversupplied_info *server_info;
        krb5_error_code ret;
        NTSTATUS nt_status;
-       TALLOC_CTX *tmp_ctx = talloc_new(entry_ex->ctx);
-       struct hdb_ldb_private *private = talloc_get_type(entry_ex->ctx, struct hdb_ldb_private);
-       char *name, *workstation = NULL;
-       int i;
 
-       if (!tmp_ctx) {
-               return ENOMEM;
-       }
-       
-       ret = krb5_unparse_name(context, entry_ex->entry.principal, &name);
-       if (ret != 0) {
-               talloc_free(tmp_ctx);
-               return ret;
+       ret = kerberos_pac_to_server_info(mem_ctx, ic, *pac,
+                                         context, &server_info);
+       if (ret) {
+               return NT_STATUS_UNSUCCESSFUL;
        }
 
-       if (addresses) {
-               for (i=0; i < addresses->len; i++) {
-                       if (addresses->val->addr_type == KRB5_ADDRESS_NETBIOS) {
-                               workstation = talloc_strndup(tmp_ctx, addresses->val->address.data, MIN(addresses->val->address.length, 15));
-                               if (workstation) {
-                                       break;
-                               }
-                       }
-               }
-       }
+       nt_status = samba_get_logon_info_pac_blob(mem_ctx, ic,
+                                                 server_info, pac_blob);
+
+       return nt_status;
+}
 
-       /* Strip space padding */
-       if (workstation) {
-               i = MIN(strlen(workstation), 15);
-               for (; i > 0 && workstation[i - 1] == ' '; i--) {
-                       workstation[i - 1] = '\0';
-               }
+/* this function allocates 'data' using malloc.
+ * The caller is responsible for freeing it */
+void samba_kdc_build_edata_reply(NTSTATUS nt_status, DATA_BLOB *e_data)
+{
+       PA_DATA pa;
+       unsigned char *buf;
+       size_t len;
+       krb5_error_code ret = 0;
+
+       if (!e_data)
+               return;
+
+       pa.padata_type          = KRB5_PADATA_PW_SALT;
+       pa.padata_value.length  = 12;
+       pa.padata_value.data    = malloc(pa.padata_value.length);
+       if (!pa.padata_value.data) {
+               e_data->length = 0;
+               e_data->data = NULL;
+               return;
        }
 
-       nt_status = authsam_account_ok(tmp_ctx, 
-                                      private->samdb, 
-                                      MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
-                                      private->msg,
-                                      private->realm_ref_msg,
-                                      workstation,
-                                      name);
-       free(name);
+       SIVAL(pa.padata_value.data, 0, NT_STATUS_V(nt_status));
+       SIVAL(pa.padata_value.data, 4, 0);
+       SIVAL(pa.padata_value.data, 8, 1);
 
-       /* TODO:  Need a more complete mapping of NTSTATUS to krb5kdc errors */
+       ASN1_MALLOC_ENCODE(PA_DATA, buf, len, &pa, &len, ret);
+       free(pa.padata_value.data);
 
-       if (!NT_STATUS_IS_OK(nt_status)) {
-               return KRB5KDC_ERR_POLICY;
+       e_data->data   = buf;
+       e_data->length = len;
+
+       return;
+}
+
+/* function to map policy errors */
+krb5_error_code samba_kdc_map_policy_err(NTSTATUS nt_status)
+{
+       krb5_error_code ret;
+
+       if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_MUST_CHANGE))
+               ret = KRB5KDC_ERR_KEY_EXPIRED;
+       else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_EXPIRED))
+               ret = KRB5KDC_ERR_KEY_EXPIRED;
+       else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_EXPIRED))
+               ret = KRB5KDC_ERR_CLIENT_REVOKED;
+       else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_DISABLED))
+               ret = KRB5KDC_ERR_CLIENT_REVOKED;
+       else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_LOGON_HOURS))
+               ret = KRB5KDC_ERR_CLIENT_REVOKED;
+       else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_LOCKED_OUT))
+               ret = KRB5KDC_ERR_CLIENT_REVOKED;
+       else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_WORKSTATION))
+               ret = KRB5KDC_ERR_POLICY;
+       else
+               ret = KRB5KDC_ERR_POLICY;
+
+       return ret;
+}
+
+/* Given a kdc entry, consult the account_ok routine in auth/auth_sam.c
+ * for consistency */
+NTSTATUS samba_kdc_check_client_access(struct samba_kdc_entry *kdc_entry,
+                                      const char *client_name,
+                                      const char *workstation,
+                                      bool password_change)
+{
+       TALLOC_CTX *tmp_ctx;
+       NTSTATUS nt_status;
+
+       tmp_ctx = talloc_named(NULL, 0, "samba_kdc_check_client_access");
+       if (!tmp_ctx) {
+               return NT_STATUS_NO_MEMORY;
        }
-       return 0;
+
+       /* we allow all kinds of trusts here */
+       nt_status = authsam_account_ok(tmp_ctx,
+                                      kdc_entry->kdc_db_ctx->samdb,
+                                      MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
+                                      MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
+                                      kdc_entry->realm_dn, kdc_entry->msg,
+                                      workstation, client_name,
+                                      true, password_change);
+
+       talloc_free(tmp_ctx);
+       return nt_status;
 }