s3:selftest: run samba3.blackbox.acl_xattr with NT1 and SMB3
[Samba.git] / source4 / kdc / wdc-samba4.c
blobb90578c85084b943777b786ee41f3cbd5ee87801
1 /*
2 Unix SMB/CIFS implementation.
4 PAC Glue between Samba and the KDC
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2009
7 Copyright (C) Simo Sorce <idra@samba.org> 2010
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "kdc/kdc-glue.h"
26 #include "kdc/pac-glue.h"
29 * Given the right private pointer from hdb_samba4,
30 * get a PAC from the attached ldb messages.
32 * For PKINIT we also get pk_reply_key and can add PAC_CREDENTIAL_INFO.
34 static krb5_error_code samba_wdc_get_pac(void *priv, krb5_context context,
35 struct hdb_entry_ex *client,
36 const krb5_keyblock *pk_reply_key,
37 krb5_pac *pac)
39 TALLOC_CTX *mem_ctx;
40 DATA_BLOB *logon_blob = NULL;
41 DATA_BLOB *cred_ndr = NULL;
42 DATA_BLOB **cred_ndr_ptr = NULL;
43 DATA_BLOB _cred_blob = data_blob_null;
44 DATA_BLOB *cred_blob = NULL;
45 DATA_BLOB *upn_blob = NULL;
46 krb5_error_code ret;
47 NTSTATUS nt_status;
48 struct samba_kdc_entry *skdc_entry =
49 talloc_get_type_abort(client->ctx,
50 struct samba_kdc_entry);
52 mem_ctx = talloc_named(client->ctx, 0, "samba_get_pac context");
53 if (!mem_ctx) {
54 return ENOMEM;
57 if (pk_reply_key != NULL) {
58 cred_ndr_ptr = &cred_ndr;
61 nt_status = samba_kdc_get_pac_blobs(mem_ctx, skdc_entry,
62 &logon_blob,
63 cred_ndr_ptr,
64 &upn_blob);
65 if (!NT_STATUS_IS_OK(nt_status)) {
66 talloc_free(mem_ctx);
67 return EINVAL;
70 if (pk_reply_key != NULL && cred_ndr != NULL) {
71 ret = samba_kdc_encrypt_pac_credentials(context,
72 pk_reply_key,
73 cred_ndr,
74 mem_ctx,
75 &_cred_blob);
76 if (ret != 0) {
77 talloc_free(mem_ctx);
78 return ret;
80 cred_blob = &_cred_blob;
83 ret = samba_make_krb5_pac(context, logon_blob, cred_blob,
84 upn_blob, NULL, pac);
86 talloc_free(mem_ctx);
87 return ret;
90 static krb5_error_code samba_wdc_get_pac_compat(void *priv, krb5_context context,
91 struct hdb_entry_ex *client,
92 krb5_pac *pac)
94 return samba_wdc_get_pac(priv, context, client, NULL, pac);
97 /* Resign (and reform, including possibly new groups) a PAC */
99 static krb5_error_code samba_wdc_reget_pac(void *priv, krb5_context context,
100 const krb5_principal client_principal,
101 const krb5_principal delegated_proxy_principal,
102 struct hdb_entry_ex *client,
103 struct hdb_entry_ex *server,
104 struct hdb_entry_ex *krbtgt,
105 krb5_pac *pac)
107 struct samba_kdc_entry *p =
108 talloc_get_type_abort(server->ctx,
109 struct samba_kdc_entry);
110 struct samba_kdc_entry *krbtgt_skdc_entry =
111 talloc_get_type_abort(krbtgt->ctx,
112 struct samba_kdc_entry);
113 TALLOC_CTX *mem_ctx = talloc_named(p, 0, "samba_kdc_reget_pac context");
114 krb5_pac new_pac = NULL;
115 DATA_BLOB *pac_blob = NULL;
116 DATA_BLOB *upn_blob = NULL;
117 DATA_BLOB *deleg_blob = NULL;
118 krb5_error_code ret;
119 NTSTATUS nt_status;
120 struct PAC_SIGNATURE_DATA *pac_srv_sig;
121 struct PAC_SIGNATURE_DATA *pac_kdc_sig;
122 bool is_in_db, is_untrusted;
123 size_t num_types = 0;
124 uint32_t *types = NULL;
125 uint32_t forced_next_type = 0;
126 size_t i = 0;
127 ssize_t logon_info_idx = -1;
128 ssize_t delegation_idx = -1;
129 ssize_t logon_name_idx = -1;
130 ssize_t upn_dns_info_idx = -1;
131 ssize_t srv_checksum_idx = -1;
132 ssize_t kdc_checksum_idx = -1;
134 if (!mem_ctx) {
135 return ENOMEM;
138 /* The user account may be set not to want the PAC */
139 if (!samba_princ_needs_pac(p)) {
140 talloc_free(mem_ctx);
141 return EINVAL;
144 /* If the krbtgt was generated by an RODC, and we are not that
145 * RODC, then we need to regenerate the PAC - we can't trust
146 * it */
147 ret = samba_krbtgt_is_in_db(krbtgt_skdc_entry, &is_in_db, &is_untrusted);
148 if (ret != 0) {
149 talloc_free(mem_ctx);
150 return ret;
153 if (is_untrusted) {
154 struct samba_kdc_entry *client_skdc_entry = NULL;
156 if (client == NULL) {
157 return KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
160 client_skdc_entry = talloc_get_type_abort(client->ctx,
161 struct samba_kdc_entry);
163 nt_status = samba_kdc_get_pac_blobs(mem_ctx, client_skdc_entry,
164 &pac_blob, NULL, &upn_blob);
165 if (!NT_STATUS_IS_OK(nt_status)) {
166 talloc_free(mem_ctx);
167 return EINVAL;
169 } else {
170 pac_blob = talloc_zero(mem_ctx, DATA_BLOB);
171 if (!pac_blob) {
172 talloc_free(mem_ctx);
173 return ENOMEM;
176 pac_srv_sig = talloc_zero(mem_ctx, struct PAC_SIGNATURE_DATA);
177 if (!pac_srv_sig) {
178 talloc_free(mem_ctx);
179 return ENOMEM;
182 pac_kdc_sig = talloc_zero(mem_ctx, struct PAC_SIGNATURE_DATA);
183 if (!pac_kdc_sig) {
184 talloc_free(mem_ctx);
185 return ENOMEM;
188 nt_status = samba_kdc_update_pac_blob(mem_ctx, context,
189 *pac, pac_blob,
190 pac_srv_sig, pac_kdc_sig);
191 if (!NT_STATUS_IS_OK(nt_status)) {
192 DEBUG(0, ("Building PAC failed: %s\n",
193 nt_errstr(nt_status)));
194 talloc_free(mem_ctx);
195 return EINVAL;
198 if (is_in_db) {
199 /* Now check the KDC signature, fetching the correct key based on the enc type */
200 ret = kdc_check_pac(context, pac_srv_sig->signature, pac_kdc_sig, krbtgt);
201 if (ret != 0) {
202 DEBUG(1, ("PAC KDC signature failed to verify\n"));
203 talloc_free(mem_ctx);
204 return ret;
209 if (delegated_proxy_principal) {
210 deleg_blob = talloc_zero(mem_ctx, DATA_BLOB);
211 if (!deleg_blob) {
212 talloc_free(mem_ctx);
213 return ENOMEM;
216 nt_status = samba_kdc_update_delegation_info_blob(mem_ctx,
217 context, *pac,
218 server->entry.principal,
219 delegated_proxy_principal,
220 deleg_blob);
221 if (!NT_STATUS_IS_OK(nt_status)) {
222 DEBUG(0, ("Building PAC failed: %s\n",
223 nt_errstr(nt_status)));
224 talloc_free(mem_ctx);
225 return EINVAL;
229 /* Check the types of the given PAC */
230 ret = krb5_pac_get_types(context, *pac, &num_types, &types);
231 if (ret != 0) {
232 talloc_free(mem_ctx);
233 return ret;
236 for (i = 0; i < num_types; i++) {
237 switch (types[i]) {
238 case PAC_TYPE_LOGON_INFO:
239 if (logon_info_idx != -1) {
240 DEBUG(1, ("logon type[%d] twice [%d] and [%d]: \n",
241 (int)types[i],
242 (int)logon_info_idx,
243 (int)i));
244 SAFE_FREE(types);
245 talloc_free(mem_ctx);
246 return EINVAL;
248 logon_info_idx = i;
249 break;
250 case PAC_TYPE_CONSTRAINED_DELEGATION:
251 if (delegation_idx != -1) {
252 DEBUG(1, ("logon type[%d] twice [%d] and [%d]: \n",
253 (int)types[i],
254 (int)logon_info_idx,
255 (int)i));
256 SAFE_FREE(types);
257 talloc_free(mem_ctx);
258 return EINVAL;
260 delegation_idx = i;
261 break;
262 case PAC_TYPE_LOGON_NAME:
263 if (logon_name_idx != -1) {
264 DEBUG(1, ("logon type[%d] twice [%d] and [%d]: \n",
265 (int)types[i],
266 (int)logon_info_idx,
267 (int)i));
268 SAFE_FREE(types);
269 talloc_free(mem_ctx);
270 return EINVAL;
272 logon_name_idx = i;
273 break;
274 case PAC_TYPE_UPN_DNS_INFO:
275 if (upn_dns_info_idx != -1) {
276 DEBUG(1, ("logon type[%d] twice [%d] and [%d]: \n",
277 (int)types[i],
278 (int)logon_info_idx,
279 (int)i));
280 SAFE_FREE(types);
281 talloc_free(mem_ctx);
282 return EINVAL;
284 upn_dns_info_idx = i;
285 break;
286 case PAC_TYPE_SRV_CHECKSUM:
287 if (srv_checksum_idx != -1) {
288 DEBUG(1, ("logon type[%d] twice [%d] and [%d]: \n",
289 (int)types[i],
290 (int)logon_info_idx,
291 (int)i));
292 SAFE_FREE(types);
293 talloc_free(mem_ctx);
294 return EINVAL;
296 srv_checksum_idx = i;
297 break;
298 case PAC_TYPE_KDC_CHECKSUM:
299 if (kdc_checksum_idx != -1) {
300 DEBUG(1, ("logon type[%d] twice [%d] and [%d]: \n",
301 (int)types[i],
302 (int)logon_info_idx,
303 (int)i));
304 SAFE_FREE(types);
305 talloc_free(mem_ctx);
306 return EINVAL;
308 kdc_checksum_idx = i;
309 break;
310 default:
311 continue;
315 if (logon_info_idx == -1) {
316 DEBUG(1, ("PAC_TYPE_LOGON_INFO missing\n"));
317 SAFE_FREE(types);
318 talloc_free(mem_ctx);
319 return EINVAL;
321 if (logon_name_idx == -1) {
322 DEBUG(1, ("PAC_TYPE_LOGON_NAME missing\n"));
323 SAFE_FREE(types);
324 talloc_free(mem_ctx);
325 return EINVAL;
327 if (srv_checksum_idx == -1) {
328 DEBUG(1, ("PAC_TYPE_SRV_CHECKSUM missing\n"));
329 SAFE_FREE(types);
330 talloc_free(mem_ctx);
331 return EINVAL;
333 if (kdc_checksum_idx == -1) {
334 DEBUG(1, ("PAC_TYPE_KDC_CHECKSUM missing\n"));
335 SAFE_FREE(types);
336 talloc_free(mem_ctx);
337 return EINVAL;
340 /* Build an updated PAC */
341 ret = krb5_pac_init(context, &new_pac);
342 if (ret != 0) {
343 SAFE_FREE(types);
344 talloc_free(mem_ctx);
345 return ret;
348 for (i = 0;;) {
349 const uint8_t zero_byte = 0;
350 krb5_data type_data;
351 DATA_BLOB type_blob = data_blob_null;
352 uint32_t type;
354 if (forced_next_type != 0) {
356 * We need to inject possible missing types
358 type = forced_next_type;
359 forced_next_type = 0;
360 } else if (i < num_types) {
361 type = types[i];
362 i++;
363 } else {
364 break;
367 switch (type) {
368 case PAC_TYPE_LOGON_INFO:
369 type_blob = *pac_blob;
371 if (delegation_idx == -1 && deleg_blob != NULL) {
372 /* inject CONSTRAINED_DELEGATION behind */
373 forced_next_type = PAC_TYPE_CONSTRAINED_DELEGATION;
375 break;
376 case PAC_TYPE_CONSTRAINED_DELEGATION:
377 if (deleg_blob != NULL) {
378 type_blob = *deleg_blob;
380 break;
381 case PAC_TYPE_CREDENTIAL_INFO:
383 * Note that we copy the credential blob,
384 * as it's only usable with the PKINIT based
385 * AS-REP reply key, it's only available on the
386 * host which did the AS-REQ/AS-REP exchange.
388 * This matches Windows 2008R2...
390 break;
391 case PAC_TYPE_LOGON_NAME:
393 * this is generated in the main KDC code
394 * we just add a place holder here.
396 type_blob = data_blob_const(&zero_byte, 1);
398 if (upn_dns_info_idx == -1 && upn_blob != NULL) {
399 /* inject UPN_DNS_INFO behind */
400 forced_next_type = PAC_TYPE_UPN_DNS_INFO;
402 break;
403 case PAC_TYPE_UPN_DNS_INFO:
405 * Replace in the RODC case, otherwise
406 * upn_blob is NULL and we just copy.
408 if (upn_blob != NULL) {
409 type_blob = *upn_blob;
411 break;
412 case PAC_TYPE_SRV_CHECKSUM:
414 * this are generated in the main KDC code
415 * we just add a place holder here.
417 type_blob = data_blob_const(&zero_byte, 1);
418 break;
419 case PAC_TYPE_KDC_CHECKSUM:
421 * this are generated in the main KDC code
422 * we just add a place holders here.
424 type_blob = data_blob_const(&zero_byte, 1);
425 break;
426 default:
427 /* just copy... */
428 break;
431 if (type_blob.length != 0) {
432 ret = smb_krb5_copy_data_contents(&type_data,
433 type_blob.data,
434 type_blob.length);
435 if (ret != 0) {
436 SAFE_FREE(types);
437 krb5_pac_free(context, new_pac);
438 talloc_free(mem_ctx);
439 return ret;
441 } else {
442 ret = krb5_pac_get_buffer(context, *pac,
443 type, &type_data);
444 if (ret != 0) {
445 SAFE_FREE(types);
446 krb5_pac_free(context, new_pac);
447 talloc_free(mem_ctx);
448 return ret;
452 ret = krb5_pac_add_buffer(context, new_pac,
453 type, &type_data);
454 smb_krb5_free_data_contents(context, &type_data);
455 if (ret != 0) {
456 SAFE_FREE(types);
457 krb5_pac_free(context, new_pac);
458 talloc_free(mem_ctx);
459 return ret;
463 SAFE_FREE(types);
465 /* We now replace the pac */
466 krb5_pac_free(context, *pac);
467 *pac = new_pac;
469 talloc_free(mem_ctx);
470 return ret;
473 static char *get_netbios_name(TALLOC_CTX *mem_ctx, HostAddresses *addrs)
475 char *nb_name = NULL;
476 size_t len;
477 unsigned int i;
479 for (i = 0; addrs && i < addrs->len; i++) {
480 if (addrs->val[i].addr_type != KRB5_ADDRESS_NETBIOS) {
481 continue;
483 len = MIN(addrs->val[i].address.length, 15);
484 nb_name = talloc_strndup(mem_ctx,
485 addrs->val[i].address.data, len);
486 if (nb_name) {
487 break;
491 if ((nb_name == NULL) || (nb_name[0] == '\0')) {
492 return NULL;
495 /* Strip space padding */
496 for (len = strlen(nb_name) - 1;
497 (len > 0) && (nb_name[len] == ' ');
498 --len) {
499 nb_name[len] = '\0';
502 return nb_name;
505 static krb5_data fill_krb5_data(void *data, size_t length)
507 krb5_data kdata;
509 kdata.data = data;
510 kdata.length = length;
512 return kdata;
515 /* this function allocates 'data' using malloc.
516 * The caller is responsible for freeing it */
517 static void samba_kdc_build_edata_reply(NTSTATUS nt_status, DATA_BLOB *e_data)
519 krb5_error_code ret = 0;
520 PA_DATA pa;
521 unsigned char *buf;
522 size_t len;
524 if (!e_data)
525 return;
527 e_data->data = NULL;
528 e_data->length = 0;
530 pa.padata_type = KRB5_PADATA_PW_SALT;
531 pa.padata_value.length = 12;
532 pa.padata_value.data = malloc(pa.padata_value.length);
533 if (!pa.padata_value.data) {
534 e_data->length = 0;
535 e_data->data = NULL;
536 return;
539 SIVAL(pa.padata_value.data, 0, NT_STATUS_V(nt_status));
540 SIVAL(pa.padata_value.data, 4, 0);
541 SIVAL(pa.padata_value.data, 8, 1);
543 ASN1_MALLOC_ENCODE(PA_DATA, buf, len, &pa, &len, ret);
544 free(pa.padata_value.data);
545 if (ret) {
546 return;
549 e_data->data = buf;
550 e_data->length = len;
552 return;
556 static krb5_error_code samba_wdc_check_client_access(void *priv,
557 krb5_context context,
558 krb5_kdc_configuration *config,
559 hdb_entry_ex *client_ex, const char *client_name,
560 hdb_entry_ex *server_ex, const char *server_name,
561 KDC_REQ *req,
562 krb5_data *e_data)
564 struct samba_kdc_entry *kdc_entry;
565 bool password_change;
566 char *workstation;
567 NTSTATUS nt_status;
569 kdc_entry = talloc_get_type(client_ex->ctx, struct samba_kdc_entry);
570 password_change = (server_ex && server_ex->entry.flags.change_pw);
571 workstation = get_netbios_name((TALLOC_CTX *)client_ex->ctx,
572 req->req_body.addresses);
574 nt_status = samba_kdc_check_client_access(kdc_entry,
575 client_name,
576 workstation,
577 password_change);
579 if (!NT_STATUS_IS_OK(nt_status)) {
580 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
581 return ENOMEM;
584 if (e_data) {
585 DATA_BLOB data;
587 samba_kdc_build_edata_reply(nt_status, &data);
588 *e_data = fill_krb5_data(data.data, data.length);
591 return samba_kdc_map_policy_err(nt_status);
594 /* Now do the standard Heimdal check */
595 return kdc_check_flags(context, config,
596 client_ex, client_name,
597 server_ex, server_name,
598 req->msg_type == krb_as_req);
601 static krb5_error_code samba_wdc_plugin_init(krb5_context context, void **ptr)
603 *ptr = NULL;
604 return 0;
607 static void samba_wdc_plugin_fini(void *ptr)
609 return;
612 struct krb5plugin_windc_ftable windc_plugin_table = {
613 .minor_version = KRB5_WINDC_PLUGIN_MINOR,
614 .init = samba_wdc_plugin_init,
615 .fini = samba_wdc_plugin_fini,
616 .pac_generate = samba_wdc_get_pac_compat,
617 .pac_verify = samba_wdc_reget_pac,
618 .client_access = samba_wdc_check_client_access,
619 .pac_pk_generate = samba_wdc_get_pac,