s4:kdc: samba_kdc_fetch() only needs sdb_entry
[Samba.git] / source4 / kdc / wdc-samba4.c
blobe6292696aaf9f7abe462718f06b5dd7556586dd9
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/db-glue.h"
27 #include "kdc/pac-glue.h"
28 #include "sdb.h"
29 #include "sdb_hdb.h"
30 #include "librpc/gen_ndr/auth.h"
31 #include <krb5_locl.h>
34 * Given the right private pointer from hdb_samba4,
35 * get a PAC from the attached ldb messages.
37 * For PKINIT we also get pk_reply_key and can add PAC_CREDENTIAL_INFO.
39 static krb5_error_code samba_wdc_get_pac(void *priv,
40 astgs_request_t r,
41 hdb_entry *client,
42 hdb_entry *server,
43 const krb5_keyblock *pk_reply_key,
44 uint64_t pac_attributes,
45 krb5_pac *pac)
47 krb5_context context = kdc_request_get_context((kdc_request_t)r);
48 TALLOC_CTX *mem_ctx;
49 DATA_BLOB *logon_blob = NULL;
50 DATA_BLOB *cred_ndr = NULL;
51 DATA_BLOB **cred_ndr_ptr = NULL;
52 DATA_BLOB _cred_blob = data_blob_null;
53 DATA_BLOB *cred_blob = NULL;
54 DATA_BLOB *upn_blob = NULL;
55 DATA_BLOB *pac_attrs_blob = NULL;
56 DATA_BLOB *requester_sid_blob = NULL;
57 krb5_error_code ret;
58 NTSTATUS nt_status;
59 struct samba_kdc_entry *skdc_entry =
60 talloc_get_type_abort(client->context,
61 struct samba_kdc_entry);
62 bool is_krbtgt;
64 mem_ctx = talloc_named(client->context, 0, "samba_get_pac context");
65 if (!mem_ctx) {
66 return ENOMEM;
69 if (pk_reply_key != NULL) {
70 cred_ndr_ptr = &cred_ndr;
73 is_krbtgt = krb5_principal_is_krbtgt(context, server->principal);
75 nt_status = samba_kdc_get_pac_blobs(mem_ctx, skdc_entry,
76 &logon_blob,
77 cred_ndr_ptr,
78 &upn_blob,
79 is_krbtgt ? &pac_attrs_blob : NULL,
80 pac_attributes,
81 is_krbtgt ? &requester_sid_blob : NULL);
82 if (!NT_STATUS_IS_OK(nt_status)) {
83 talloc_free(mem_ctx);
84 return EINVAL;
87 if (pk_reply_key != NULL && cred_ndr != NULL) {
88 ret = samba_kdc_encrypt_pac_credentials(context,
89 pk_reply_key,
90 cred_ndr,
91 mem_ctx,
92 &_cred_blob);
93 if (ret != 0) {
94 talloc_free(mem_ctx);
95 return ret;
97 cred_blob = &_cred_blob;
100 ret = krb5_pac_init(context, pac);
101 if (ret != 0) {
102 talloc_free(mem_ctx);
103 return ret;
106 ret = samba_make_krb5_pac(context, logon_blob, cred_blob,
107 upn_blob, pac_attrs_blob,
108 requester_sid_blob, NULL, *pac);
110 talloc_free(mem_ctx);
111 return ret;
114 static krb5_error_code samba_wdc_reget_pac2(krb5_context context,
115 const krb5_principal delegated_proxy_principal,
116 hdb_entry *client,
117 hdb_entry *server,
118 hdb_entry *krbtgt,
119 krb5_pac *pac,
120 krb5_cksumtype ctype)
122 struct samba_kdc_entry *client_skdc_entry = NULL;
123 struct samba_kdc_entry *server_skdc_entry =
124 talloc_get_type_abort(server->context, struct samba_kdc_entry);
125 struct samba_kdc_entry *krbtgt_skdc_entry =
126 talloc_get_type_abort(krbtgt->context, struct samba_kdc_entry);
127 TALLOC_CTX *mem_ctx = NULL;
128 krb5_pac new_pac = NULL;
129 krb5_error_code ret;
130 bool is_in_db = false;
131 bool is_untrusted = false;
132 uint32_t flags = 0;
134 mem_ctx = talloc_named(NULL, 0, "samba_kdc_reget_pac2 context");
135 if (mem_ctx == NULL) {
136 return ENOMEM;
139 if (client != NULL) {
140 client_skdc_entry = talloc_get_type_abort(client->context,
141 struct samba_kdc_entry);
145 * If the krbtgt was generated by an RODC, and we are not that
146 * RODC, then we need to regenerate the PAC - we can't trust
147 * it, and confirm that the RODC was permitted to print this ticket
149 * Becasue of the samba_kdc_validate_pac_blob() step we can be
150 * sure that the record in 'client' matches the SID in the
151 * original PAC.
153 ret = samba_krbtgt_is_in_db(krbtgt_skdc_entry, &is_in_db, &is_untrusted);
154 if (ret != 0) {
155 goto out;
158 if (delegated_proxy_principal != NULL) {
159 krb5_enctype etype;
160 Key *key = NULL;
162 if (!is_in_db) {
164 * The RODC-issued PAC was signed by a KDC entry that we
165 * don't have a key for. The server signature is not
166 * trustworthy, since it could have been created by the
167 * server we got the ticket from. We must not proceed as
168 * otherwise the ticket signature is unchecked.
170 ret = HDB_ERR_NOT_FOUND_HERE;
171 goto out;
174 /* Fetch the correct key depending on the checksum type. */
175 if (ctype == CKSUMTYPE_HMAC_MD5) {
176 etype = ENCTYPE_ARCFOUR_HMAC;
177 } else {
178 ret = krb5_cksumtype_to_enctype(context,
179 ctype,
180 &etype);
181 if (ret != 0) {
182 goto out;
185 ret = hdb_enctype2key(context, krbtgt, NULL, etype, &key);
186 if (ret != 0) {
187 goto out;
190 /* Check the KDC and ticket signatures. */
191 ret = krb5_pac_verify(context,
192 *pac,
194 NULL,
195 NULL,
196 &key->key);
197 if (ret != 0) {
198 DEBUG(1, ("PAC KDC signature failed to verify\n"));
199 goto out;
202 flags |= SAMBA_KDC_FLAG_CONSTRAINED_DELEGATION;
205 if (is_untrusted) {
206 flags |= SAMBA_KDC_FLAG_KRBTGT_IS_UNTRUSTED;
209 if (is_in_db) {
210 flags |= SAMBA_KDC_FLAG_KRBTGT_IN_DB;
213 ret = krb5_pac_init(context, &new_pac);
214 if (ret != 0) {
215 new_pac = NULL;
216 goto out;
219 ret = samba_kdc_update_pac(mem_ctx,
220 context,
221 krbtgt_skdc_entry->kdc_db_ctx->samdb,
222 flags,
223 client_skdc_entry,
224 server->principal,
225 server_skdc_entry,
226 krbtgt_skdc_entry,
227 delegated_proxy_principal,
228 *pac,
229 new_pac);
230 if (ret != 0) {
231 krb5_pac_free(context, new_pac);
232 if (ret == ENODATA) {
233 krb5_pac_free(context, *pac);
234 *pac = NULL;
235 ret = 0;
237 goto out;
240 /* Replace the pac */
241 krb5_pac_free(context, *pac);
242 *pac = new_pac;
244 out:
245 talloc_free(mem_ctx);
246 return ret;
249 /* Resign (and reform, including possibly new groups) a PAC */
251 static krb5_error_code samba_wdc_reget_pac(void *priv, astgs_request_t r,
252 const krb5_principal client_principal,
253 const krb5_principal delegated_proxy_principal,
254 hdb_entry *client,
255 hdb_entry *server,
256 hdb_entry *krbtgt,
257 krb5_pac *pac)
259 krb5_context context = kdc_request_get_context((kdc_request_t)r);
260 krb5_kdc_configuration *config = kdc_request_get_config((kdc_request_t)r);
261 struct samba_kdc_entry *krbtgt_skdc_entry =
262 talloc_get_type_abort(krbtgt->context,
263 struct samba_kdc_entry);
264 krb5_error_code ret;
265 krb5_cksumtype ctype = CKSUMTYPE_NONE;
266 hdb_entry signing_krbtgt_hdb;
268 if (delegated_proxy_principal) {
269 uint16_t rodc_id;
270 unsigned int my_krbtgt_number;
273 * We're using delegated_proxy_principal for the moment to
274 * indicate cases where the ticket was encrypted with the server
275 * key, and not a krbtgt key. This cannot be trusted, so we need
276 * to find a krbtgt key that signs the PAC in order to trust the
277 * ticket.
279 * The krbtgt passed in to this function refers to the krbtgt
280 * used to decrypt the ticket of the server requesting
281 * S4U2Proxy.
283 * When we implement service ticket renewal, we need to check
284 * the PAC, and this will need to be updated.
286 ret = krb5_pac_get_kdc_checksum_info(context,
287 *pac,
288 &ctype,
289 &rodc_id);
290 if (ret != 0) {
291 DEBUG(1, ("Failed to get PAC checksum info\n"));
292 return ret;
296 * We need to check the KDC and ticket signatures, fetching the
297 * correct key based on the enctype.
300 my_krbtgt_number = krbtgt_skdc_entry->kdc_db_ctx->my_krbtgt_number;
302 if (my_krbtgt_number != 0) {
304 * If we are an RODC, and we are not the KDC that signed
305 * the evidence ticket, then we need to proxy the
306 * request.
308 if (rodc_id != my_krbtgt_number) {
309 return HDB_ERR_NOT_FOUND_HERE;
311 } else {
313 * If we are a DC, the ticket may have been signed by a
314 * different KDC than the one that issued the header
315 * ticket.
317 if (rodc_id != krbtgt->kvno >> 16) {
318 struct sdb_entry_ex signing_krbtgt_sdb;
321 * If we didn't sign the ticket, then return an
322 * error.
324 if (rodc_id != 0) {
325 return KRB5KRB_AP_ERR_MODIFIED;
329 * Fetch our key from the database. To support
330 * key rollover, we're going to need to try
331 * multiple keys by trial and error. For now,
332 * krbtgt keys aren't assumed to change.
334 ret = samba_kdc_fetch(context,
335 krbtgt_skdc_entry->kdc_db_ctx,
336 krbtgt->principal,
337 SDB_F_GET_KRBTGT | SDB_F_CANON,
339 &signing_krbtgt_sdb.entry);
340 if (ret != 0) {
341 return ret;
344 ret = sdb_entry_to_hdb_entry(context,
345 &signing_krbtgt_sdb.entry,
346 &signing_krbtgt_hdb);
347 sdb_free_entry(&signing_krbtgt_sdb);
348 if (ret != 0) {
349 return ret;
353 * Replace the krbtgt entry with our own entry
354 * for further processing.
356 krbtgt = &signing_krbtgt_hdb;
361 ret = samba_wdc_reget_pac2(context,
362 delegated_proxy_principal,
363 client,
364 server,
365 krbtgt,
366 pac,
367 ctype);
369 if (krbtgt == &signing_krbtgt_hdb) {
370 hdb_free_entry(context, config->db[0], &signing_krbtgt_hdb);
373 return ret;
376 static char *get_netbios_name(TALLOC_CTX *mem_ctx, HostAddresses *addrs)
378 char *nb_name = NULL;
379 size_t len;
380 unsigned int i;
382 for (i = 0; addrs && i < addrs->len; i++) {
383 if (addrs->val[i].addr_type != KRB5_ADDRESS_NETBIOS) {
384 continue;
386 len = MIN(addrs->val[i].address.length, 15);
387 nb_name = talloc_strndup(mem_ctx,
388 addrs->val[i].address.data, len);
389 if (nb_name) {
390 break;
394 if ((nb_name == NULL) || (nb_name[0] == '\0')) {
395 return NULL;
398 /* Strip space padding */
399 for (len = strlen(nb_name) - 1;
400 (len > 0) && (nb_name[len] == ' ');
401 --len) {
402 nb_name[len] = '\0';
405 return nb_name;
408 /* this function allocates 'data' using malloc.
409 * The caller is responsible for freeing it */
410 static void samba_kdc_build_edata_reply(NTSTATUS nt_status, krb5_data *e_data)
412 e_data->data = malloc(12);
413 if (e_data->data == NULL) {
414 e_data->length = 0;
415 e_data->data = NULL;
416 return;
418 e_data->length = 12;
420 SIVAL(e_data->data, 0, NT_STATUS_V(nt_status));
421 SIVAL(e_data->data, 4, 0);
422 SIVAL(e_data->data, 8, 1);
424 return;
427 static krb5_error_code samba_wdc_check_client_access(void *priv,
428 astgs_request_t r)
430 struct samba_kdc_entry *kdc_entry;
431 bool password_change;
432 char *workstation;
433 NTSTATUS nt_status;
436 kdc_entry = talloc_get_type(kdc_request_get_client(r)->context, struct samba_kdc_entry);
437 password_change = (kdc_request_get_server(r) && kdc_request_get_server(r)->flags.change_pw);
438 workstation = get_netbios_name((TALLOC_CTX *)kdc_request_get_client(r)->context,
439 kdc_request_get_req(r)->req_body.addresses);
441 nt_status = samba_kdc_check_client_access(kdc_entry,
442 kdc_request_get_cname((kdc_request_t)r),
443 workstation,
444 password_change);
446 if (!NT_STATUS_IS_OK(nt_status)) {
447 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
448 return ENOMEM;
451 if (kdc_request_get_rep(r)->padata) {
452 int ret;
453 krb5_data kd;
455 samba_kdc_build_edata_reply(nt_status, &kd);
456 ret = krb5_padata_add(kdc_request_get_context((kdc_request_t)r), kdc_request_get_rep(r)->padata,
457 KRB5_PADATA_PW_SALT,
458 kd.data, kd.length);
459 if (ret != 0) {
461 * So we do not leak the allocated
462 * memory on kd in the error case
464 krb5_data_free(&kd);
468 return samba_kdc_map_policy_err(nt_status);
471 /* Now do the standard Heimdal check */
472 return KRB5_PLUGIN_NO_HANDLE;
475 /* this function allocates 'data' using malloc.
476 * The caller is responsible for freeing it */
477 static krb5_error_code samba_kdc_build_supported_etypes(uint32_t supported_etypes,
478 krb5_data *e_data)
480 e_data->data = malloc(4);
481 if (e_data->data == NULL) {
482 return ENOMEM;
484 e_data->length = 4;
486 PUSH_LE_U32(e_data->data, 0, supported_etypes);
488 return 0;
491 static krb5_error_code samba_wdc_finalize_reply(void *priv,
492 astgs_request_t r)
494 struct samba_kdc_entry *server_kdc_entry;
495 uint32_t supported_enctypes;
497 server_kdc_entry = talloc_get_type(kdc_request_get_server(r)->context, struct samba_kdc_entry);
500 * If the canonicalize flag is set, add PA-SUPPORTED-ENCTYPES padata
501 * type to indicate what encryption types the server supports.
503 supported_enctypes = server_kdc_entry->supported_enctypes;
504 if (kdc_request_get_req(r)->req_body.kdc_options.canonicalize && supported_enctypes != 0) {
505 krb5_error_code ret;
507 PA_DATA md;
509 ret = samba_kdc_build_supported_etypes(supported_enctypes, &md.padata_value);
510 if (ret != 0) {
511 return ret;
514 md.padata_type = KRB5_PADATA_SUPPORTED_ETYPES;
516 ret = kdc_request_add_encrypted_padata(r, &md);
517 if (ret != 0) {
519 * So we do not leak the allocated
520 * memory on kd in the error case
522 krb5_data_free(&md.padata_value);
526 return 0;
529 static krb5_error_code samba_wdc_plugin_init(krb5_context context, void **ptr)
531 *ptr = NULL;
532 return 0;
535 static void samba_wdc_plugin_fini(void *ptr)
537 return;
540 static krb5_error_code samba_wdc_referral_policy(void *priv,
541 astgs_request_t r)
543 return kdc_request_get_error_code((kdc_request_t)r);
546 struct krb5plugin_kdc_ftable kdc_plugin_table = {
547 .minor_version = KRB5_PLUGIN_KDC_VERSION_10,
548 .init = samba_wdc_plugin_init,
549 .fini = samba_wdc_plugin_fini,
550 .pac_verify = samba_wdc_reget_pac,
551 .client_access = samba_wdc_check_client_access,
552 .finalize_reply = samba_wdc_finalize_reply,
553 .pac_generate = samba_wdc_get_pac,
554 .referral_policy = samba_wdc_referral_policy,