s4:kdc: Fix debugging messages
[Samba.git] / source4 / kdc / wdc-samba4.c
blob037db40ce465b7520492eb935acd626b41919865
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 krbtgt_skdc_entry->kdc_db_ctx->samdb,
190 *pac, pac_blob,
191 pac_srv_sig, pac_kdc_sig);
192 if (!NT_STATUS_IS_OK(nt_status)) {
193 DEBUG(0, ("Building PAC failed: %s\n",
194 nt_errstr(nt_status)));
195 talloc_free(mem_ctx);
196 return EINVAL;
199 if (is_in_db) {
200 /* Now check the KDC signature, fetching the correct key based on the enc type */
201 ret = kdc_check_pac(context, pac_srv_sig->signature, pac_kdc_sig, krbtgt);
202 if (ret != 0) {
203 DEBUG(1, ("PAC KDC signature failed to verify\n"));
204 talloc_free(mem_ctx);
205 return ret;
210 if (delegated_proxy_principal) {
211 deleg_blob = talloc_zero(mem_ctx, DATA_BLOB);
212 if (!deleg_blob) {
213 talloc_free(mem_ctx);
214 return ENOMEM;
217 nt_status = samba_kdc_update_delegation_info_blob(mem_ctx,
218 context, *pac,
219 server->entry.principal,
220 delegated_proxy_principal,
221 deleg_blob);
222 if (!NT_STATUS_IS_OK(nt_status)) {
223 DEBUG(0, ("Building PAC failed: %s\n",
224 nt_errstr(nt_status)));
225 talloc_free(mem_ctx);
226 return EINVAL;
230 /* Check the types of the given PAC */
231 ret = krb5_pac_get_types(context, *pac, &num_types, &types);
232 if (ret != 0) {
233 talloc_free(mem_ctx);
234 return ret;
237 for (i = 0; i < num_types; i++) {
238 switch (types[i]) {
239 case PAC_TYPE_LOGON_INFO:
240 if (logon_info_idx != -1) {
241 DEBUG(1, ("logon info type[%"PRIu32"] twice [%zd] and [%zu]: \n",
242 types[i],
243 logon_info_idx,
244 i));
245 SAFE_FREE(types);
246 talloc_free(mem_ctx);
247 return EINVAL;
249 logon_info_idx = i;
250 break;
251 case PAC_TYPE_CONSTRAINED_DELEGATION:
252 if (delegation_idx != -1) {
253 DEBUG(1, ("constrained delegation type[%"PRIu32"] twice [%zd] and [%zu]: \n",
254 types[i],
255 delegation_idx,
256 i));
257 SAFE_FREE(types);
258 talloc_free(mem_ctx);
259 return EINVAL;
261 delegation_idx = i;
262 break;
263 case PAC_TYPE_LOGON_NAME:
264 if (logon_name_idx != -1) {
265 DEBUG(1, ("logon name type[%"PRIu32"] twice [%zd] and [%zu]: \n",
266 types[i],
267 logon_name_idx,
268 i));
269 SAFE_FREE(types);
270 talloc_free(mem_ctx);
271 return EINVAL;
273 logon_name_idx = i;
274 break;
275 case PAC_TYPE_UPN_DNS_INFO:
276 if (upn_dns_info_idx != -1) {
277 DEBUG(1, ("upn dns info type[%"PRIu32"] twice [%zd] and [%zu]: \n",
278 types[i],
279 upn_dns_info_idx,
280 i));
281 SAFE_FREE(types);
282 talloc_free(mem_ctx);
283 return EINVAL;
285 upn_dns_info_idx = i;
286 break;
287 case PAC_TYPE_SRV_CHECKSUM:
288 if (srv_checksum_idx != -1) {
289 DEBUG(1, ("server checksum type[%"PRIu32"] twice [%zd] and [%zu]: \n",
290 types[i],
291 srv_checksum_idx,
292 i));
293 SAFE_FREE(types);
294 talloc_free(mem_ctx);
295 return EINVAL;
297 srv_checksum_idx = i;
298 break;
299 case PAC_TYPE_KDC_CHECKSUM:
300 if (kdc_checksum_idx != -1) {
301 DEBUG(1, ("kdc checksum type[%"PRIu32"] twice [%zd] and [%zu]: \n",
302 types[i],
303 kdc_checksum_idx,
304 i));
305 SAFE_FREE(types);
306 talloc_free(mem_ctx);
307 return EINVAL;
309 kdc_checksum_idx = i;
310 break;
311 default:
312 continue;
316 if (logon_info_idx == -1) {
317 DEBUG(1, ("PAC_TYPE_LOGON_INFO missing\n"));
318 SAFE_FREE(types);
319 talloc_free(mem_ctx);
320 return EINVAL;
322 if (logon_name_idx == -1) {
323 DEBUG(1, ("PAC_TYPE_LOGON_NAME missing\n"));
324 SAFE_FREE(types);
325 talloc_free(mem_ctx);
326 return EINVAL;
328 if (srv_checksum_idx == -1) {
329 DEBUG(1, ("PAC_TYPE_SRV_CHECKSUM missing\n"));
330 SAFE_FREE(types);
331 talloc_free(mem_ctx);
332 return EINVAL;
334 if (kdc_checksum_idx == -1) {
335 DEBUG(1, ("PAC_TYPE_KDC_CHECKSUM missing\n"));
336 SAFE_FREE(types);
337 talloc_free(mem_ctx);
338 return EINVAL;
341 /* Build an updated PAC */
342 ret = krb5_pac_init(context, &new_pac);
343 if (ret != 0) {
344 SAFE_FREE(types);
345 talloc_free(mem_ctx);
346 return ret;
349 for (i = 0;;) {
350 const uint8_t zero_byte = 0;
351 krb5_data type_data;
352 DATA_BLOB type_blob = data_blob_null;
353 uint32_t type;
355 if (forced_next_type != 0) {
357 * We need to inject possible missing types
359 type = forced_next_type;
360 forced_next_type = 0;
361 } else if (i < num_types) {
362 type = types[i];
363 i++;
364 } else {
365 break;
368 switch (type) {
369 case PAC_TYPE_LOGON_INFO:
370 type_blob = *pac_blob;
372 if (delegation_idx == -1 && deleg_blob != NULL) {
373 /* inject CONSTRAINED_DELEGATION behind */
374 forced_next_type = PAC_TYPE_CONSTRAINED_DELEGATION;
376 break;
377 case PAC_TYPE_CONSTRAINED_DELEGATION:
378 if (deleg_blob != NULL) {
379 type_blob = *deleg_blob;
381 break;
382 case PAC_TYPE_CREDENTIAL_INFO:
384 * Note that we copy the credential blob,
385 * as it's only usable with the PKINIT based
386 * AS-REP reply key, it's only available on the
387 * host which did the AS-REQ/AS-REP exchange.
389 * This matches Windows 2008R2...
391 break;
392 case PAC_TYPE_LOGON_NAME:
394 * this is generated in the main KDC code
395 * we just add a place holder here.
397 type_blob = data_blob_const(&zero_byte, 1);
399 if (upn_dns_info_idx == -1 && upn_blob != NULL) {
400 /* inject UPN_DNS_INFO behind */
401 forced_next_type = PAC_TYPE_UPN_DNS_INFO;
403 break;
404 case PAC_TYPE_UPN_DNS_INFO:
406 * Replace in the RODC case, otherwise
407 * upn_blob is NULL and we just copy.
409 if (upn_blob != NULL) {
410 type_blob = *upn_blob;
412 break;
413 case PAC_TYPE_SRV_CHECKSUM:
415 * this are generated in the main KDC code
416 * we just add a place holder here.
418 type_blob = data_blob_const(&zero_byte, 1);
419 break;
420 case PAC_TYPE_KDC_CHECKSUM:
422 * this are generated in the main KDC code
423 * we just add a place holders here.
425 type_blob = data_blob_const(&zero_byte, 1);
426 break;
427 default:
428 /* just copy... */
429 break;
432 if (type_blob.length != 0) {
433 ret = smb_krb5_copy_data_contents(&type_data,
434 type_blob.data,
435 type_blob.length);
436 if (ret != 0) {
437 SAFE_FREE(types);
438 krb5_pac_free(context, new_pac);
439 talloc_free(mem_ctx);
440 return ret;
442 } else {
443 ret = krb5_pac_get_buffer(context, *pac,
444 type, &type_data);
445 if (ret != 0) {
446 SAFE_FREE(types);
447 krb5_pac_free(context, new_pac);
448 talloc_free(mem_ctx);
449 return ret;
453 ret = krb5_pac_add_buffer(context, new_pac,
454 type, &type_data);
455 smb_krb5_free_data_contents(context, &type_data);
456 if (ret != 0) {
457 SAFE_FREE(types);
458 krb5_pac_free(context, new_pac);
459 talloc_free(mem_ctx);
460 return ret;
464 SAFE_FREE(types);
466 /* We now replace the pac */
467 krb5_pac_free(context, *pac);
468 *pac = new_pac;
470 talloc_free(mem_ctx);
471 return ret;
474 static char *get_netbios_name(TALLOC_CTX *mem_ctx, HostAddresses *addrs)
476 char *nb_name = NULL;
477 size_t len;
478 unsigned int i;
480 for (i = 0; addrs && i < addrs->len; i++) {
481 if (addrs->val[i].addr_type != KRB5_ADDRESS_NETBIOS) {
482 continue;
484 len = MIN(addrs->val[i].address.length, 15);
485 nb_name = talloc_strndup(mem_ctx,
486 addrs->val[i].address.data, len);
487 if (nb_name) {
488 break;
492 if ((nb_name == NULL) || (nb_name[0] == '\0')) {
493 return NULL;
496 /* Strip space padding */
497 for (len = strlen(nb_name) - 1;
498 (len > 0) && (nb_name[len] == ' ');
499 --len) {
500 nb_name[len] = '\0';
503 return nb_name;
506 static krb5_data fill_krb5_data(void *data, size_t length)
508 krb5_data kdata;
510 kdata.data = data;
511 kdata.length = length;
513 return kdata;
516 /* this function allocates 'data' using malloc.
517 * The caller is responsible for freeing it */
518 static void samba_kdc_build_edata_reply(NTSTATUS nt_status, DATA_BLOB *e_data)
520 krb5_error_code ret = 0;
521 PA_DATA pa;
522 unsigned char *buf;
523 size_t len;
525 if (!e_data)
526 return;
528 e_data->data = NULL;
529 e_data->length = 0;
531 pa.padata_type = KRB5_PADATA_PW_SALT;
532 pa.padata_value.length = 12;
533 pa.padata_value.data = malloc(pa.padata_value.length);
534 if (!pa.padata_value.data) {
535 e_data->length = 0;
536 e_data->data = NULL;
537 return;
540 SIVAL(pa.padata_value.data, 0, NT_STATUS_V(nt_status));
541 SIVAL(pa.padata_value.data, 4, 0);
542 SIVAL(pa.padata_value.data, 8, 1);
544 ASN1_MALLOC_ENCODE(PA_DATA, buf, len, &pa, &len, ret);
545 free(pa.padata_value.data);
546 if (ret) {
547 return;
550 e_data->data = buf;
551 e_data->length = len;
553 return;
557 static krb5_error_code samba_wdc_check_client_access(void *priv,
558 krb5_context context,
559 krb5_kdc_configuration *config,
560 hdb_entry_ex *client_ex, const char *client_name,
561 hdb_entry_ex *server_ex, const char *server_name,
562 KDC_REQ *req,
563 krb5_data *e_data)
565 struct samba_kdc_entry *kdc_entry;
566 bool password_change;
567 char *workstation;
568 NTSTATUS nt_status;
570 kdc_entry = talloc_get_type(client_ex->ctx, struct samba_kdc_entry);
571 password_change = (server_ex && server_ex->entry.flags.change_pw);
572 workstation = get_netbios_name((TALLOC_CTX *)client_ex->ctx,
573 req->req_body.addresses);
575 nt_status = samba_kdc_check_client_access(kdc_entry,
576 client_name,
577 workstation,
578 password_change);
580 if (!NT_STATUS_IS_OK(nt_status)) {
581 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
582 return ENOMEM;
585 if (e_data) {
586 DATA_BLOB data;
588 samba_kdc_build_edata_reply(nt_status, &data);
589 *e_data = fill_krb5_data(data.data, data.length);
592 return samba_kdc_map_policy_err(nt_status);
595 /* Now do the standard Heimdal check */
596 return kdc_check_flags(context, config,
597 client_ex, client_name,
598 server_ex, server_name,
599 req->msg_type == krb_as_req);
602 static krb5_error_code samba_wdc_plugin_init(krb5_context context, void **ptr)
604 *ptr = NULL;
605 return 0;
608 static void samba_wdc_plugin_fini(void *ptr)
610 return;
613 struct krb5plugin_windc_ftable windc_plugin_table = {
614 .minor_version = KRB5_WINDC_PLUGIN_MINOR,
615 .init = samba_wdc_plugin_init,
616 .fini = samba_wdc_plugin_fini,
617 .pac_generate = samba_wdc_get_pac_compat,
618 .pac_verify = samba_wdc_reget_pac,
619 .client_access = samba_wdc_check_client_access,
620 .pac_pk_generate = samba_wdc_get_pac,