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"
32 /* Given the right private pointer from hdb_ldb, get a PAC from the attached ldb messages */
33 static krb5_error_code samba_get_pac(krb5_context context,
34 struct hdb_ldb_private *private,
35 krb5_principal client,
36 krb5_keyblock *krbtgt_keyblock,
37 krb5_keyblock *server_keyblock,
43 struct auth_serversupplied_info *server_info;
45 TALLOC_CTX *mem_ctx = talloc_named(private, 0, "samba_get_pac context");
51 nt_status = authsam_make_server_info(mem_ctx, private->samdb,
53 private->realm_ref_msg,
57 if (!NT_STATUS_IS_OK(nt_status)) {
58 DEBUG(0, ("Getting user info for PAC failed: %s\n",
59 nt_errstr(nt_status)));
63 ret = kerberos_create_pac(mem_ctx, server_info,
72 DEBUG(1, ("PAC encoding failed: %s\n",
73 smb_get_krb5_error_message(context, ret, mem_ctx)));
78 ret = krb5_data_copy(pac, tmp_blob.data, tmp_blob.length);
83 /* Wrap the PAC in the right ASN.1. Will always free 'pac', on success or failure */
84 static krb5_error_code wrap_pac(krb5_context context, krb5_data *pac, AuthorizationData **out)
92 AD_IF_RELEVANT if_relevant;
93 AuthorizationData *auth_data;
96 if_relevant.val = malloc(sizeof(*if_relevant.val));
97 if (!if_relevant.val) {
103 if_relevant.val[0].ad_type = KRB5_AUTHDATA_WIN2K_PAC;
104 if_relevant.val[0].ad_data.data = NULL;
105 if_relevant.val[0].ad_data.length = 0;
107 /* pac.data will be freed with this */
108 if_relevant.val[0].ad_data.data = pac->data;
109 if_relevant.val[0].ad_data.length = pac->length;
111 ASN1_MALLOC_ENCODE(AuthorizationData, buf, buf_size, &if_relevant, &len, ret);
112 free_AuthorizationData(&if_relevant);
118 auth_data = malloc(sizeof(*auth_data));
125 auth_data->val = malloc(sizeof(*auth_data->val));
126 if (!auth_data->val) {
132 auth_data->val[0].ad_type = KRB5_AUTHDATA_IF_RELEVANT;
133 auth_data->val[0].ad_data.length = len;
134 auth_data->val[0].ad_data.data = buf;
141 /* Given a hdb_entry, create a PAC out of the private data
143 Don't create it if the client has the UF_NO_AUTH_DATA_REQUIRED bit
144 set, or if they specificaly asked not to get it.
147 krb5_error_code hdb_ldb_authz_data_as_req(krb5_context context, struct hdb_entry_ex *entry_ex,
148 METHOD_DATA* pa_data_seq,
150 EncryptionKey *tgtkey,
151 EncryptionKey *sessionkey,
152 AuthorizationData **out)
157 krb5_boolean pac_wanted = TRUE;
158 unsigned int userAccountControl;
159 struct PA_PAC_REQUEST pac_request;
160 struct hdb_ldb_private *private = talloc_get_type(entry_ex->ctx, struct hdb_ldb_private);
162 /* The user account may be set not to want the PAC */
163 userAccountControl = ldb_msg_find_uint(private->msg, "userAccountControl", 0);
164 if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
169 /* The user may not want a PAC */
170 for (i=0; i<pa_data_seq->len; i++) {
171 if (pa_data_seq->val[i].padata_type == KRB5_PADATA_PA_PAC_REQUEST) {
172 ret = decode_PA_PAC_REQUEST(pa_data_seq->val[i].padata_value.data,
173 pa_data_seq->val[i].padata_value.length,
176 pac_wanted = !!pac_request.include_pac;
178 free_PA_PAC_REQUEST(&pac_request);
188 /* Get PAC from Samba */
189 ret = samba_get_pac(context,
191 entry_ex->entry.principal,
202 return wrap_pac(context, &pac, out);
205 /* Resign (and reform, including possibly new groups) a PAC */
207 krb5_error_code hdb_ldb_authz_data_tgs_req(krb5_context context, struct hdb_entry_ex *entry_ex,
208 krb5_principal client,
209 AuthorizationData *in,
211 EncryptionKey *tgtkey,
212 EncryptionKey *servicekey,
213 EncryptionKey *sessionkey,
214 AuthorizationData **out)
219 unsigned int userAccountControl;
221 struct hdb_ldb_private *private = talloc_get_type(entry_ex->ctx, struct hdb_ldb_private);
222 krb5_data k5pac_in, k5pac_out;
223 DATA_BLOB pac_in, pac_out;
225 struct PAC_LOGON_INFO *logon_info;
226 union netr_Validation validation;
227 struct auth_serversupplied_info *server_info_out;
229 krb5_boolean found = FALSE;
232 /* The service account may be set not to want the PAC */
233 userAccountControl = ldb_msg_find_uint(private->msg, "userAccountControl", 0);
234 if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
239 ret = _krb5_find_type_in_ad(context, KRB5_AUTHDATA_WIN2K_PAC,
240 &k5pac_in, &found, sessionkey, in);
246 mem_ctx = talloc_new(private);
248 krb5_data_free(&k5pac_in);
253 pac_in = data_blob_talloc(mem_ctx, k5pac_in.data, k5pac_in.length);
254 krb5_data_free(&k5pac_in);
256 talloc_free(mem_ctx);
261 /* Parse the PAC again, for the logon info */
262 nt_status = kerberos_pac_logon_info(mem_ctx, &logon_info,
270 if (!NT_STATUS_IS_OK(nt_status)) {
271 DEBUG(1, ("Failed to parse PAC in TGT: %s/%s\n",
272 nt_errstr(nt_status), error_message(ret)));
273 talloc_free(mem_ctx);
278 /* Pull this right into the normal auth sysstem structures */
279 validation.sam3 = &logon_info->info3;
280 nt_status = make_server_info_netlogon_validation(mem_ctx,
284 if (!NT_STATUS_IS_OK(nt_status)) {
285 talloc_free(mem_ctx);
290 /* And make a new PAC, possibly containing new groups */
291 ret = kerberos_create_pac(mem_ctx,
301 talloc_free(mem_ctx);
306 ret = krb5_data_copy(&k5pac_out, pac_out.data, pac_out.length);
308 talloc_free(mem_ctx);
313 return wrap_pac(context, &k5pac_out, out);
316 /* Given an hdb entry (and in particular it's private member), consult
317 * the account_ok routine in auth/auth_sam.c for consistancy */
319 krb5_error_code hdb_ldb_check_client_access(krb5_context context, hdb_entry_ex *entry_ex,
320 HostAddresses *addresses)
324 TALLOC_CTX *tmp_ctx = talloc_new(entry_ex->ctx);
325 struct hdb_ldb_private *private = talloc_get_type(entry_ex->ctx, struct hdb_ldb_private);
326 char *name, *workstation = NULL;
333 ret = krb5_unparse_name(context, entry_ex->entry.principal, &name);
335 talloc_free(tmp_ctx);
340 for (i=0; i < addresses->len; i++) {
341 if (addresses->val->addr_type == KRB5_ADDRESS_NETBIOS) {
342 workstation = talloc_strndup(tmp_ctx, addresses->val->address.data, MIN(addresses->val->address.length, 15));
350 /* Strip space padding */
352 i = MIN(strlen(workstation), 15);
353 for (; i > 0 && workstation[i - 1] == ' '; i--) {
354 workstation[i - 1] = '\0';
358 nt_status = authsam_account_ok(tmp_ctx,
360 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
362 private->realm_ref_msg,
367 /* TODO: Need a more complete mapping of NTSTATUS to krb5kdc errors */
369 if (!NT_STATUS_IS_OK(nt_status)) {
370 return KRB5KDC_ERR_POLICY;