idl: wmi and dcom don't need python bindings
[Samba.git] / source4 / kdc / pac-glue.c
blob3b0f00f850352ba0fc4fcc9b7711f9052efa5b07
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 "../libds/common/flags.h"
26 #include <ldb.h>
27 #include "auth/auth.h"
28 #include "auth/auth_sam_reply.h"
29 #include "kdc/kdc-glue.h"
30 #include "kdc/pac-glue.h"
31 #include "param/param.h"
32 #include "librpc/gen_ndr/ndr_krb5pac.h"
33 #include "libcli/security/security.h"
34 #include "dsdb/samdb/samdb.h"
36 static
37 NTSTATUS samba_get_logon_info_pac_blob(TALLOC_CTX *mem_ctx,
38 struct auth_user_info_dc *info,
39 DATA_BLOB *pac_data)
41 struct netr_SamInfo3 *info3;
42 union PAC_INFO pac_info;
43 enum ndr_err_code ndr_err;
44 NTSTATUS nt_status;
46 ZERO_STRUCT(pac_info);
48 nt_status = auth_convert_user_info_dc_saminfo3(mem_ctx, info, &info3);
49 if (!NT_STATUS_IS_OK(nt_status)) {
50 DEBUG(1, ("Getting Samba info failed: %s\n",
51 nt_errstr(nt_status)));
52 return nt_status;
55 pac_info.logon_info.info = talloc_zero(mem_ctx, struct PAC_LOGON_INFO);
56 if (!pac_info.logon_info.info) {
57 return NT_STATUS_NO_MEMORY;
60 pac_info.logon_info.info->info3 = *info3;
62 ndr_err = ndr_push_union_blob(pac_data, mem_ctx, &pac_info,
63 PAC_TYPE_LOGON_INFO,
64 (ndr_push_flags_fn_t)ndr_push_PAC_INFO);
65 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
66 nt_status = ndr_map_error2ntstatus(ndr_err);
67 DEBUG(1, ("PAC (presig) push failed: %s\n",
68 nt_errstr(nt_status)));
69 return nt_status;
72 return NT_STATUS_OK;
75 krb5_error_code samba_make_krb5_pac(krb5_context context,
76 DATA_BLOB *pac_blob,
77 DATA_BLOB *deleg_blob,
78 krb5_pac *pac)
80 krb5_data pac_data;
81 krb5_data deleg_data;
82 krb5_error_code ret;
84 /* The user account may be set not to want the PAC */
85 if (!pac_blob) {
86 return 0;
89 ret = krb5_data_copy(&pac_data, pac_blob->data, pac_blob->length);
90 if (ret != 0) {
91 return ret;
94 ZERO_STRUCT(deleg_data);
95 if (deleg_blob) {
96 ret = krb5_data_copy(&deleg_data,
97 deleg_blob->data,
98 deleg_blob->length);
99 if (ret != 0) {
100 krb5_data_free(&pac_data);
101 return ret;
105 ret = krb5_pac_init(context, pac);
106 if (ret != 0) {
107 krb5_data_free(&pac_data);
108 krb5_data_free(&deleg_data);
109 return ret;
112 ret = krb5_pac_add_buffer(context, *pac, PAC_TYPE_LOGON_INFO, &pac_data);
113 krb5_data_free(&pac_data);
114 if (ret != 0) {
115 krb5_data_free(&deleg_data);
116 return ret;
119 if (deleg_blob) {
120 ret = krb5_pac_add_buffer(context, *pac,
121 PAC_TYPE_CONSTRAINED_DELEGATION,
122 &deleg_data);
123 krb5_data_free(&deleg_data);
124 if (ret != 0) {
125 return ret;
129 return ret;
132 bool samba_princ_needs_pac(struct hdb_entry_ex *princ)
135 struct samba_kdc_entry *p = talloc_get_type(princ->ctx, struct samba_kdc_entry);
136 uint32_t userAccountControl;
139 /* The service account may be set not to want the PAC */
140 userAccountControl = ldb_msg_find_attr_as_uint(p->msg, "userAccountControl", 0);
141 if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
142 return false;
145 return true;
148 /* Was the krbtgt in this DB (ie, should we check the incoming signature) and was it an RODC */
149 int samba_krbtgt_is_in_db(struct hdb_entry_ex *princ, bool *is_in_db, bool *is_untrusted)
151 NTSTATUS status;
152 struct samba_kdc_entry *p = talloc_get_type(princ->ctx, struct samba_kdc_entry);
153 int rodc_krbtgt_number, trust_direction;
154 uint32_t rid;
156 TALLOC_CTX *mem_ctx = talloc_new(NULL);
157 if (!mem_ctx) {
158 return ENOMEM;
161 trust_direction = ldb_msg_find_attr_as_int(p->msg, "trustDirection", 0);
163 if (trust_direction != 0) {
164 /* Domain trust - we cannot check the sig, but we trust it for a correct PAC
166 This is exactly where we should flag for SID
167 validation when we do inter-foreest trusts
169 talloc_free(mem_ctx);
170 *is_untrusted = false;
171 *is_in_db = false;
172 return 0;
175 /* The lack of password controls etc applies to krbtgt by
176 * virtue of being that particular RID */
177 status = dom_sid_split_rid(NULL, samdb_result_dom_sid(mem_ctx, p->msg, "objectSid"), NULL, &rid);
179 if (!NT_STATUS_IS_OK(status)) {
180 talloc_free(mem_ctx);
181 return EINVAL;
184 rodc_krbtgt_number = ldb_msg_find_attr_as_int(p->msg, "msDS-SecondaryKrbTgtNumber", -1);
186 if (p->kdc_db_ctx->my_krbtgt_number == 0) {
187 if (rid == DOMAIN_RID_KRBTGT) {
188 *is_untrusted = false;
189 *is_in_db = true;
190 talloc_free(mem_ctx);
191 return 0;
192 } else if (rodc_krbtgt_number != -1) {
193 *is_in_db = true;
194 *is_untrusted = true;
195 talloc_free(mem_ctx);
196 return 0;
198 } else if ((rid != DOMAIN_RID_KRBTGT) && (rodc_krbtgt_number == p->kdc_db_ctx->my_krbtgt_number)) {
199 talloc_free(mem_ctx);
200 *is_untrusted = false;
201 *is_in_db = true;
202 return 0;
203 } else if (rid == DOMAIN_RID_KRBTGT) {
204 /* krbtgt viewed from an RODC */
205 talloc_free(mem_ctx);
206 *is_untrusted = false;
207 *is_in_db = false;
208 return 0;
211 /* Another RODC */
212 talloc_free(mem_ctx);
213 *is_untrusted = true;
214 *is_in_db = false;
215 return 0;
218 NTSTATUS samba_kdc_get_pac_blob(TALLOC_CTX *mem_ctx,
219 struct hdb_entry_ex *client,
220 DATA_BLOB **_pac_blob)
222 struct samba_kdc_entry *p = talloc_get_type(client->ctx, struct samba_kdc_entry);
223 struct auth_user_info_dc *user_info_dc;
224 DATA_BLOB *pac_blob;
225 NTSTATUS nt_status;
227 /* The user account may be set not to want the PAC */
228 if ( ! samba_princ_needs_pac(client)) {
229 *_pac_blob = NULL;
230 return NT_STATUS_OK;
233 pac_blob = talloc_zero(mem_ctx, DATA_BLOB);
234 if (!pac_blob) {
235 return NT_STATUS_NO_MEMORY;
238 nt_status = authsam_make_user_info_dc(mem_ctx, p->kdc_db_ctx->samdb,
239 lpcfg_netbios_name(p->kdc_db_ctx->lp_ctx),
240 lpcfg_sam_name(p->kdc_db_ctx->lp_ctx),
241 p->realm_dn,
242 p->msg,
243 data_blob(NULL, 0),
244 data_blob(NULL, 0),
245 &user_info_dc);
246 if (!NT_STATUS_IS_OK(nt_status)) {
247 DEBUG(0, ("Getting user info for PAC failed: %s\n",
248 nt_errstr(nt_status)));
249 return nt_status;
252 nt_status = samba_get_logon_info_pac_blob(mem_ctx, user_info_dc, pac_blob);
253 if (!NT_STATUS_IS_OK(nt_status)) {
254 DEBUG(0, ("Building PAC failed: %s\n",
255 nt_errstr(nt_status)));
256 return nt_status;
259 *_pac_blob = pac_blob;
260 return NT_STATUS_OK;
263 NTSTATUS samba_kdc_update_pac_blob(TALLOC_CTX *mem_ctx,
264 krb5_context context,
265 const krb5_pac pac, DATA_BLOB *pac_blob,
266 struct PAC_SIGNATURE_DATA *pac_srv_sig,
267 struct PAC_SIGNATURE_DATA *pac_kdc_sig)
269 struct auth_user_info_dc *user_info_dc;
270 krb5_error_code ret;
271 NTSTATUS nt_status;
273 ret = kerberos_pac_to_user_info_dc(mem_ctx, pac,
274 context, &user_info_dc, pac_srv_sig, pac_kdc_sig);
275 if (ret) {
276 return NT_STATUS_UNSUCCESSFUL;
279 nt_status = samba_get_logon_info_pac_blob(mem_ctx,
280 user_info_dc, pac_blob);
282 return nt_status;
285 NTSTATUS samba_kdc_update_delegation_info_blob(TALLOC_CTX *mem_ctx,
286 krb5_context context,
287 const krb5_pac pac,
288 const krb5_principal server_principal,
289 const krb5_principal proxy_principal,
290 DATA_BLOB *new_blob)
292 krb5_data old_data;
293 DATA_BLOB old_blob;
294 krb5_error_code ret;
295 NTSTATUS nt_status;
296 enum ndr_err_code ndr_err;
297 union PAC_INFO info;
298 struct PAC_CONSTRAINED_DELEGATION _d;
299 struct PAC_CONSTRAINED_DELEGATION *d = NULL;
300 char *server = NULL;
301 char *proxy = NULL;
302 uint32_t i;
303 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
305 if (tmp_ctx == NULL) {
306 return NT_STATUS_NO_MEMORY;
309 ret = krb5_pac_get_buffer(context, pac, PAC_TYPE_CONSTRAINED_DELEGATION, &old_data);
310 if (ret == ENOENT) {
311 ZERO_STRUCT(old_data);
312 } else if (ret) {
313 talloc_free(tmp_ctx);
314 return NT_STATUS_UNSUCCESSFUL;
317 old_blob.length = old_data.length;
318 old_blob.data = (uint8_t *)old_data.data;
320 ZERO_STRUCT(info);
321 if (old_blob.length > 0) {
322 ndr_err = ndr_pull_union_blob(&old_blob, mem_ctx,
323 &info, PAC_TYPE_CONSTRAINED_DELEGATION,
324 (ndr_pull_flags_fn_t)ndr_pull_PAC_INFO);
325 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
326 krb5_data_free(&old_data);
327 nt_status = ndr_map_error2ntstatus(ndr_err);
328 DEBUG(0,("can't parse the PAC LOGON_INFO: %s\n", nt_errstr(nt_status)));
329 talloc_free(tmp_ctx);
330 return nt_status;
332 } else {
333 ZERO_STRUCT(_d);
334 info.constrained_delegation.info = &_d;
336 krb5_data_free(&old_data);
338 ret = krb5_unparse_name(context, server_principal, &server);
339 if (ret) {
340 talloc_free(tmp_ctx);
341 return NT_STATUS_INTERNAL_ERROR;
344 ret = krb5_unparse_name_flags(context, proxy_principal,
345 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &proxy);
346 if (ret) {
347 SAFE_FREE(server);
348 talloc_free(tmp_ctx);
349 return NT_STATUS_INTERNAL_ERROR;
352 d = info.constrained_delegation.info;
353 i = d->num_transited_services;
354 d->proxy_target.string = server;
355 d->transited_services = talloc_realloc(mem_ctx, d->transited_services,
356 struct lsa_String, i + 1);
357 d->transited_services[i].string = proxy;
358 d->num_transited_services = i + 1;
360 ndr_err = ndr_push_union_blob(new_blob, mem_ctx,
361 &info, PAC_TYPE_CONSTRAINED_DELEGATION,
362 (ndr_push_flags_fn_t)ndr_push_PAC_INFO);
363 SAFE_FREE(server);
364 SAFE_FREE(proxy);
365 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
366 krb5_data_free(&old_data);
367 nt_status = ndr_map_error2ntstatus(ndr_err);
368 DEBUG(0,("can't parse the PAC LOGON_INFO: %s\n", nt_errstr(nt_status)));
369 talloc_free(tmp_ctx);
370 return nt_status;
373 talloc_free(tmp_ctx);
374 return NT_STATUS_OK;
377 /* this function allocates 'data' using malloc.
378 * The caller is responsible for freeing it */
379 void samba_kdc_build_edata_reply(NTSTATUS nt_status, DATA_BLOB *e_data)
381 PA_DATA pa;
382 unsigned char *buf;
383 size_t len;
384 krb5_error_code ret = 0;
386 if (!e_data)
387 return;
389 pa.padata_type = KRB5_PADATA_PW_SALT;
390 pa.padata_value.length = 12;
391 pa.padata_value.data = malloc(pa.padata_value.length);
392 if (!pa.padata_value.data) {
393 e_data->length = 0;
394 e_data->data = NULL;
395 return;
398 SIVAL(pa.padata_value.data, 0, NT_STATUS_V(nt_status));
399 SIVAL(pa.padata_value.data, 4, 0);
400 SIVAL(pa.padata_value.data, 8, 1);
402 ASN1_MALLOC_ENCODE(PA_DATA, buf, len, &pa, &len, ret);
403 free(pa.padata_value.data);
405 e_data->data = buf;
406 e_data->length = len;
408 return;
411 /* function to map policy errors */
412 krb5_error_code samba_kdc_map_policy_err(NTSTATUS nt_status)
414 krb5_error_code ret;
416 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_MUST_CHANGE))
417 ret = KRB5KDC_ERR_KEY_EXPIRED;
418 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_EXPIRED))
419 ret = KRB5KDC_ERR_KEY_EXPIRED;
420 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_EXPIRED))
421 ret = KRB5KDC_ERR_CLIENT_REVOKED;
422 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_DISABLED))
423 ret = KRB5KDC_ERR_CLIENT_REVOKED;
424 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_LOGON_HOURS))
425 ret = KRB5KDC_ERR_CLIENT_REVOKED;
426 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_LOCKED_OUT))
427 ret = KRB5KDC_ERR_CLIENT_REVOKED;
428 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_WORKSTATION))
429 ret = KRB5KDC_ERR_POLICY;
430 else
431 ret = KRB5KDC_ERR_POLICY;
433 return ret;
436 /* Given a kdc entry, consult the account_ok routine in auth/auth_sam.c
437 * for consistency */
438 NTSTATUS samba_kdc_check_client_access(struct samba_kdc_entry *kdc_entry,
439 const char *client_name,
440 const char *workstation,
441 bool password_change)
443 TALLOC_CTX *tmp_ctx;
444 NTSTATUS nt_status;
446 tmp_ctx = talloc_named(NULL, 0, "samba_kdc_check_client_access");
447 if (!tmp_ctx) {
448 return NT_STATUS_NO_MEMORY;
451 /* we allow all kinds of trusts here */
452 nt_status = authsam_account_ok(tmp_ctx,
453 kdc_entry->kdc_db_ctx->samdb,
454 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
455 MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
456 kdc_entry->realm_dn, kdc_entry->msg,
457 workstation, client_name,
458 true, password_change);
460 talloc_free(tmp_ctx);
461 return nt_status;
464 int kdc_check_pac(krb5_context context,
465 DATA_BLOB srv_sig,
466 struct PAC_SIGNATURE_DATA *kdc_sig,
467 hdb_entry_ex *ent)
469 krb5_enctype etype;
470 int ret;
471 krb5_keyblock keyblock;
472 Key *key;
473 if (kdc_sig->type == CKSUMTYPE_HMAC_MD5) {
474 etype = ETYPE_ARCFOUR_HMAC_MD5;
475 } else {
476 ret = krb5_cksumtype_to_enctype(context,
477 kdc_sig->type,
478 &etype);
479 if (ret != 0) {
480 return ret;
484 ret = hdb_enctype2key(context, &ent->entry, etype, &key);
486 if (ret != 0) {
487 return ret;
490 keyblock = key->key;
492 return check_pac_checksum(srv_sig, kdc_sig,
493 context, &keyblock);