2 Unix SMB/CIFS implementation.
4 PAC Glue between Samba and the KDC
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "include/ads.h"
27 #include "lib/ldb/include/ldb.h"
28 #include "heimdal/lib/krb5/krb5_locl.h"
29 #include "librpc/gen_ndr/krb5pac.h"
30 #include "auth/auth.h"
31 #include "auth/auth_sam.h"
33 /* Given the right private pointer from hdb_ldb, get a PAC from the attached ldb messages */
34 static krb5_error_code samba_get_pac(krb5_context context,
35 struct hdb_ldb_private *private,
36 krb5_principal client,
37 krb5_keyblock *krbtgt_keyblock,
38 krb5_keyblock *server_keyblock,
44 struct auth_serversupplied_info *server_info;
46 TALLOC_CTX *mem_ctx = talloc_named(private, 0, "samba_get_pac context");
52 nt_status = authsam_make_server_info(mem_ctx, private->samdb,
54 private->realm_ref_msg,
58 if (!NT_STATUS_IS_OK(nt_status)) {
59 DEBUG(0, ("Getting user info for PAC failed: %s\n",
60 nt_errstr(nt_status)));
64 ret = kerberos_create_pac(mem_ctx, server_info,
73 DEBUG(1, ("PAC encoding failed: %s\n",
74 smb_get_krb5_error_message(context, ret, mem_ctx)));
79 ret = krb5_data_copy(pac, tmp_blob.data, tmp_blob.length);
84 /* Wrap the PAC in the right ASN.1. Will always free 'pac', on success or failure */
85 static krb5_error_code wrap_pac(krb5_context context, krb5_data *pac, AuthorizationData **out)
93 AD_IF_RELEVANT if_relevant;
94 AuthorizationData *auth_data;
97 if_relevant.val = malloc(sizeof(*if_relevant.val));
98 if (!if_relevant.val) {
104 if_relevant.val[0].ad_type = KRB5_AUTHDATA_WIN2K_PAC;
105 if_relevant.val[0].ad_data.data = NULL;
106 if_relevant.val[0].ad_data.length = 0;
108 /* pac.data will be freed with this */
109 if_relevant.val[0].ad_data.data = pac->data;
110 if_relevant.val[0].ad_data.length = pac->length;
112 ASN1_MALLOC_ENCODE(AuthorizationData, buf, buf_size, &if_relevant, &len, ret);
113 free_AuthorizationData(&if_relevant);
119 auth_data = malloc(sizeof(*auth_data));
126 auth_data->val = malloc(sizeof(*auth_data->val));
127 if (!auth_data->val) {
133 auth_data->val[0].ad_type = KRB5_AUTHDATA_IF_RELEVANT;
134 auth_data->val[0].ad_data.length = len;
135 auth_data->val[0].ad_data.data = buf;
142 /* Given a hdb_entry, create a PAC out of the private data
144 Don't create it if the client has the UF_NO_AUTH_DATA_REQUIRED bit
145 set, or if they specificaly asked not to get it.
148 krb5_error_code hdb_ldb_authz_data_as_req(krb5_context context, struct hdb_entry_ex *entry_ex,
149 METHOD_DATA* pa_data_seq,
151 EncryptionKey *tgtkey,
152 EncryptionKey *sessionkey,
153 AuthorizationData **out)
158 krb5_boolean pac_wanted = TRUE;
159 unsigned int userAccountControl;
160 struct PA_PAC_REQUEST pac_request;
161 struct hdb_ldb_private *private = talloc_get_type(entry_ex->ctx, struct hdb_ldb_private);
163 /* The user account may be set not to want the PAC */
164 userAccountControl = ldb_msg_find_uint(private->msg, "userAccountControl", 0);
165 if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
170 /* The user may not want a PAC */
171 for (i=0; i<pa_data_seq->len; i++) {
172 if (pa_data_seq->val[i].padata_type == KRB5_PADATA_PA_PAC_REQUEST) {
173 ret = decode_PA_PAC_REQUEST(pa_data_seq->val[i].padata_value.data,
174 pa_data_seq->val[i].padata_value.length,
177 pac_wanted = !!pac_request.include_pac;
179 free_PA_PAC_REQUEST(&pac_request);
189 /* Get PAC from Samba */
190 ret = samba_get_pac(context,
192 entry_ex->entry.principal,
203 return wrap_pac(context, &pac, out);
206 /* Resign (and reform, including possibly new groups) a PAC */
208 krb5_error_code hdb_ldb_authz_data_tgs_req(krb5_context context, struct hdb_entry_ex *entry_ex,
209 krb5_principal client,
210 AuthorizationData *in,
212 EncryptionKey *tgtkey,
213 EncryptionKey *servicekey,
214 EncryptionKey *sessionkey,
215 AuthorizationData **out)
220 unsigned int userAccountControl;
222 struct hdb_ldb_private *private = talloc_get_type(entry_ex->ctx, struct hdb_ldb_private);
223 krb5_data k5pac_in, k5pac_out;
224 DATA_BLOB pac_in, pac_out;
226 struct PAC_LOGON_INFO *logon_info;
227 union netr_Validation validation;
228 struct auth_serversupplied_info *server_info_out;
230 krb5_boolean found = FALSE;
233 /* The service account may be set not to want the PAC */
234 userAccountControl = ldb_msg_find_uint(private->msg, "userAccountControl", 0);
235 if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
240 ret = _krb5_find_type_in_ad(context, KRB5_AUTHDATA_WIN2K_PAC,
241 &k5pac_in, &found, sessionkey, in);
247 mem_ctx = talloc_new(private);
249 krb5_data_free(&k5pac_in);
254 pac_in = data_blob_talloc(mem_ctx, k5pac_in.data, k5pac_in.length);
255 krb5_data_free(&k5pac_in);
257 talloc_free(mem_ctx);
262 /* Parse the PAC again, for the logon info */
263 nt_status = kerberos_pac_logon_info(mem_ctx, &logon_info,
271 if (!NT_STATUS_IS_OK(nt_status)) {
272 DEBUG(1, ("Failed to parse PAC in TGT: %s/%s\n",
273 nt_errstr(nt_status), error_message(ret)));
274 talloc_free(mem_ctx);
279 /* Pull this right into the normal auth sysstem structures */
280 validation.sam3 = &logon_info->info3;
281 nt_status = make_server_info_netlogon_validation(mem_ctx,
285 if (!NT_STATUS_IS_OK(nt_status)) {
286 talloc_free(mem_ctx);
291 /* And make a new PAC, possibly containing new groups */
292 ret = kerberos_create_pac(mem_ctx,
302 talloc_free(mem_ctx);
307 ret = krb5_data_copy(&k5pac_out, pac_out.data, pac_out.length);
309 talloc_free(mem_ctx);
314 return wrap_pac(context, &k5pac_out, out);
317 /* Given an hdb entry (and in particular it's private member), consult
318 * the account_ok routine in auth/auth_sam.c for consistancy */
320 krb5_error_code hdb_ldb_check_client_access(krb5_context context, hdb_entry_ex *entry_ex,
321 HostAddresses *addresses)
325 TALLOC_CTX *tmp_ctx = talloc_new(entry_ex->ctx);
326 struct hdb_ldb_private *private = talloc_get_type(entry_ex->ctx, struct hdb_ldb_private);
327 char *name, *workstation = NULL;
334 ret = krb5_unparse_name(context, entry_ex->entry.principal, &name);
336 talloc_free(tmp_ctx);
341 for (i=0; i < addresses->len; i++) {
342 if (addresses->val->addr_type == KRB5_ADDRESS_NETBIOS) {
343 workstation = talloc_strndup(tmp_ctx, addresses->val->address.data, MIN(addresses->val->address.length, 15));
351 /* Strip space padding */
353 i = MIN(strlen(workstation), 15);
354 for (; i > 0 && workstation[i - 1] == ' '; i--) {
355 workstation[i - 1] = '\0';
359 nt_status = authsam_account_ok(tmp_ctx,
361 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
363 private->realm_ref_msg,
368 /* TODO: Need a more complete mapping of NTSTATUS to krb5kdc errors */
370 if (!NT_STATUS_IS_OK(nt_status)) {
371 return KRB5KDC_ERR_POLICY;