lib: Remove unused parmlist code
[Samba.git] / source4 / kdc / pac-glue.c
blobcdb0ac1fa5647b134f6c3f3d9ba1a0ec87fc9a15
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 "system/kerberos.h"
30 #include "auth/kerberos/kerberos.h"
31 #include "kdc/samba_kdc.h"
32 #include "kdc/pac-glue.h"
33 #include "param/param.h"
34 #include "librpc/gen_ndr/ndr_krb5pac.h"
35 #include "libcli/security/security.h"
36 #include "dsdb/samdb/samdb.h"
37 #include "auth/kerberos/pac_utils.h"
39 static
40 NTSTATUS samba_get_logon_info_pac_blob(TALLOC_CTX *mem_ctx,
41 struct auth_user_info_dc *info,
42 DATA_BLOB *pac_data)
44 struct netr_SamInfo3 *info3;
45 union PAC_INFO pac_info;
46 enum ndr_err_code ndr_err;
47 NTSTATUS nt_status;
49 ZERO_STRUCT(pac_info);
51 nt_status = auth_convert_user_info_dc_saminfo3(mem_ctx, info, &info3);
52 if (!NT_STATUS_IS_OK(nt_status)) {
53 DEBUG(1, ("Getting Samba info failed: %s\n",
54 nt_errstr(nt_status)));
55 return nt_status;
58 pac_info.logon_info.info = talloc_zero(mem_ctx, struct PAC_LOGON_INFO);
59 if (!pac_info.logon_info.info) {
60 return NT_STATUS_NO_MEMORY;
63 pac_info.logon_info.info->info3 = *info3;
65 ndr_err = ndr_push_union_blob(pac_data, mem_ctx, &pac_info,
66 PAC_TYPE_LOGON_INFO,
67 (ndr_push_flags_fn_t)ndr_push_PAC_INFO);
68 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
69 nt_status = ndr_map_error2ntstatus(ndr_err);
70 DEBUG(1, ("PAC (presig) push failed: %s\n",
71 nt_errstr(nt_status)));
72 return nt_status;
75 return NT_STATUS_OK;
78 krb5_error_code samba_make_krb5_pac(krb5_context context,
79 DATA_BLOB *pac_blob,
80 DATA_BLOB *deleg_blob,
81 krb5_pac *pac)
83 krb5_data pac_data;
84 krb5_data deleg_data;
85 krb5_error_code ret;
87 /* The user account may be set not to want the PAC */
88 if (!pac_blob) {
89 return 0;
92 ret = krb5_copy_data_contents(&pac_data,
93 pac_blob->data,
94 pac_blob->length);
95 if (ret != 0) {
96 return ret;
99 ZERO_STRUCT(deleg_data);
100 if (deleg_blob) {
101 ret = krb5_copy_data_contents(&deleg_data,
102 deleg_blob->data,
103 deleg_blob->length);
104 if (ret != 0) {
105 kerberos_free_data_contents(context, &pac_data);
106 return ret;
110 ret = krb5_pac_init(context, pac);
111 if (ret != 0) {
112 kerberos_free_data_contents(context, &pac_data);
113 kerberos_free_data_contents(context, &deleg_data);
114 return ret;
117 ret = krb5_pac_add_buffer(context, *pac, PAC_TYPE_LOGON_INFO, &pac_data);
118 kerberos_free_data_contents(context, &pac_data);
119 if (ret != 0) {
120 kerberos_free_data_contents(context, &deleg_data);
121 return ret;
124 if (deleg_blob) {
125 ret = krb5_pac_add_buffer(context, *pac,
126 PAC_TYPE_CONSTRAINED_DELEGATION,
127 &deleg_data);
128 kerberos_free_data_contents(context, &deleg_data);
129 if (ret != 0) {
130 return ret;
134 return ret;
137 bool samba_princ_needs_pac(struct samba_kdc_entry *skdc_entry)
140 uint32_t userAccountControl;
142 /* The service account may be set not to want the PAC */
143 userAccountControl = ldb_msg_find_attr_as_uint(skdc_entry->msg, "userAccountControl", 0);
144 if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
145 return false;
148 return true;
151 /* Was the krbtgt in this DB (ie, should we check the incoming signature) and was it an RODC */
152 int samba_krbtgt_is_in_db(struct samba_kdc_entry *p,
153 bool *is_in_db,
154 bool *is_untrusted)
156 NTSTATUS status;
157 int rodc_krbtgt_number, trust_direction;
158 uint32_t rid;
160 TALLOC_CTX *mem_ctx = talloc_new(NULL);
161 if (!mem_ctx) {
162 return ENOMEM;
165 trust_direction = ldb_msg_find_attr_as_int(p->msg, "trustDirection", 0);
167 if (trust_direction != 0) {
168 /* Domain trust - we cannot check the sig, but we trust it for a correct PAC
170 This is exactly where we should flag for SID
171 validation when we do inter-foreest trusts
173 talloc_free(mem_ctx);
174 *is_untrusted = false;
175 *is_in_db = false;
176 return 0;
179 /* The lack of password controls etc applies to krbtgt by
180 * virtue of being that particular RID */
181 status = dom_sid_split_rid(NULL, samdb_result_dom_sid(mem_ctx, p->msg, "objectSid"), NULL, &rid);
183 if (!NT_STATUS_IS_OK(status)) {
184 talloc_free(mem_ctx);
185 return EINVAL;
188 rodc_krbtgt_number = ldb_msg_find_attr_as_int(p->msg, "msDS-SecondaryKrbTgtNumber", -1);
190 if (p->kdc_db_ctx->my_krbtgt_number == 0) {
191 if (rid == DOMAIN_RID_KRBTGT) {
192 *is_untrusted = false;
193 *is_in_db = true;
194 talloc_free(mem_ctx);
195 return 0;
196 } else if (rodc_krbtgt_number != -1) {
197 *is_in_db = true;
198 *is_untrusted = true;
199 talloc_free(mem_ctx);
200 return 0;
202 } else if ((rid != DOMAIN_RID_KRBTGT) && (rodc_krbtgt_number == p->kdc_db_ctx->my_krbtgt_number)) {
203 talloc_free(mem_ctx);
204 *is_untrusted = false;
205 *is_in_db = true;
206 return 0;
207 } else if (rid == DOMAIN_RID_KRBTGT) {
208 /* krbtgt viewed from an RODC */
209 talloc_free(mem_ctx);
210 *is_untrusted = false;
211 *is_in_db = false;
212 return 0;
215 /* Another RODC */
216 talloc_free(mem_ctx);
217 *is_untrusted = true;
218 *is_in_db = false;
219 return 0;
222 NTSTATUS samba_kdc_get_pac_blob(TALLOC_CTX *mem_ctx,
223 struct samba_kdc_entry *p,
224 DATA_BLOB **_pac_blob)
226 struct auth_user_info_dc *user_info_dc;
227 DATA_BLOB *pac_blob;
228 NTSTATUS nt_status;
230 /* The user account may be set not to want the PAC */
231 if ( ! samba_princ_needs_pac(p)) {
232 *_pac_blob = NULL;
233 return NT_STATUS_OK;
236 pac_blob = talloc_zero(mem_ctx, DATA_BLOB);
237 if (!pac_blob) {
238 return NT_STATUS_NO_MEMORY;
241 nt_status = authsam_make_user_info_dc(mem_ctx, p->kdc_db_ctx->samdb,
242 lpcfg_netbios_name(p->kdc_db_ctx->lp_ctx),
243 lpcfg_sam_name(p->kdc_db_ctx->lp_ctx),
244 p->realm_dn,
245 p->msg,
246 data_blob(NULL, 0),
247 data_blob(NULL, 0),
248 &user_info_dc);
249 if (!NT_STATUS_IS_OK(nt_status)) {
250 DEBUG(0, ("Getting user info for PAC failed: %s\n",
251 nt_errstr(nt_status)));
252 return nt_status;
255 nt_status = samba_get_logon_info_pac_blob(mem_ctx, user_info_dc, pac_blob);
256 if (!NT_STATUS_IS_OK(nt_status)) {
257 DEBUG(0, ("Building PAC failed: %s\n",
258 nt_errstr(nt_status)));
259 return nt_status;
262 *_pac_blob = pac_blob;
263 return NT_STATUS_OK;
266 NTSTATUS samba_kdc_update_pac_blob(TALLOC_CTX *mem_ctx,
267 krb5_context context,
268 const krb5_pac pac, DATA_BLOB *pac_blob,
269 struct PAC_SIGNATURE_DATA *pac_srv_sig,
270 struct PAC_SIGNATURE_DATA *pac_kdc_sig)
272 struct auth_user_info_dc *user_info_dc;
273 krb5_error_code ret;
274 NTSTATUS nt_status;
276 ret = kerberos_pac_to_user_info_dc(mem_ctx, pac,
277 context, &user_info_dc, pac_srv_sig, pac_kdc_sig);
278 if (ret) {
279 return NT_STATUS_UNSUCCESSFUL;
282 nt_status = samba_get_logon_info_pac_blob(mem_ctx,
283 user_info_dc, pac_blob);
285 return nt_status;
288 NTSTATUS samba_kdc_update_delegation_info_blob(TALLOC_CTX *mem_ctx,
289 krb5_context context,
290 const krb5_pac pac,
291 const krb5_principal server_principal,
292 const krb5_principal proxy_principal,
293 DATA_BLOB *new_blob)
295 krb5_data old_data;
296 DATA_BLOB old_blob;
297 krb5_error_code ret;
298 NTSTATUS nt_status;
299 enum ndr_err_code ndr_err;
300 union PAC_INFO info;
301 struct PAC_CONSTRAINED_DELEGATION _d;
302 struct PAC_CONSTRAINED_DELEGATION *d = NULL;
303 char *server = NULL;
304 char *proxy = NULL;
305 uint32_t i;
306 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
308 if (tmp_ctx == NULL) {
309 return NT_STATUS_NO_MEMORY;
312 ret = krb5_pac_get_buffer(context, pac, PAC_TYPE_CONSTRAINED_DELEGATION, &old_data);
313 if (ret == ENOENT) {
314 ZERO_STRUCT(old_data);
315 } else if (ret) {
316 talloc_free(tmp_ctx);
317 return NT_STATUS_UNSUCCESSFUL;
320 old_blob.length = old_data.length;
321 old_blob.data = (uint8_t *)old_data.data;
323 ZERO_STRUCT(info);
324 if (old_blob.length > 0) {
325 ndr_err = ndr_pull_union_blob(&old_blob, mem_ctx,
326 &info, PAC_TYPE_CONSTRAINED_DELEGATION,
327 (ndr_pull_flags_fn_t)ndr_pull_PAC_INFO);
328 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
329 kerberos_free_data_contents(context, &old_data);
330 nt_status = ndr_map_error2ntstatus(ndr_err);
331 DEBUG(0,("can't parse the PAC LOGON_INFO: %s\n", nt_errstr(nt_status)));
332 talloc_free(tmp_ctx);
333 return nt_status;
335 } else {
336 ZERO_STRUCT(_d);
337 info.constrained_delegation.info = &_d;
339 kerberos_free_data_contents(context, &old_data);
341 ret = krb5_unparse_name(context, server_principal, &server);
342 if (ret) {
343 talloc_free(tmp_ctx);
344 return NT_STATUS_INTERNAL_ERROR;
347 ret = krb5_unparse_name_flags(context, proxy_principal,
348 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &proxy);
349 if (ret) {
350 SAFE_FREE(server);
351 talloc_free(tmp_ctx);
352 return NT_STATUS_INTERNAL_ERROR;
355 d = info.constrained_delegation.info;
356 i = d->num_transited_services;
357 d->proxy_target.string = server;
358 d->transited_services = talloc_realloc(mem_ctx, d->transited_services,
359 struct lsa_String, i + 1);
360 d->transited_services[i].string = proxy;
361 d->num_transited_services = i + 1;
363 ndr_err = ndr_push_union_blob(new_blob, mem_ctx,
364 &info, PAC_TYPE_CONSTRAINED_DELEGATION,
365 (ndr_push_flags_fn_t)ndr_push_PAC_INFO);
366 SAFE_FREE(server);
367 SAFE_FREE(proxy);
368 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
369 kerberos_free_data_contents(context, &old_data);
370 nt_status = ndr_map_error2ntstatus(ndr_err);
371 DEBUG(0,("can't parse the PAC LOGON_INFO: %s\n", nt_errstr(nt_status)));
372 talloc_free(tmp_ctx);
373 return nt_status;
376 talloc_free(tmp_ctx);
377 return NT_STATUS_OK;
380 /* function to map policy errors */
381 krb5_error_code samba_kdc_map_policy_err(NTSTATUS nt_status)
383 krb5_error_code ret;
385 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_MUST_CHANGE))
386 ret = KRB5KDC_ERR_KEY_EXP;
387 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_EXPIRED))
388 ret = KRB5KDC_ERR_KEY_EXP;
389 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_EXPIRED))
390 ret = KRB5KDC_ERR_CLIENT_REVOKED;
391 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_DISABLED))
392 ret = KRB5KDC_ERR_CLIENT_REVOKED;
393 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_LOGON_HOURS))
394 ret = KRB5KDC_ERR_CLIENT_REVOKED;
395 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_LOCKED_OUT))
396 ret = KRB5KDC_ERR_CLIENT_REVOKED;
397 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_WORKSTATION))
398 ret = KRB5KDC_ERR_POLICY;
399 else
400 ret = KRB5KDC_ERR_POLICY;
402 return ret;
405 /* Given a kdc entry, consult the account_ok routine in auth/auth_sam.c
406 * for consistency */
407 NTSTATUS samba_kdc_check_client_access(struct samba_kdc_entry *kdc_entry,
408 const char *client_name,
409 const char *workstation,
410 bool password_change)
412 TALLOC_CTX *tmp_ctx;
413 NTSTATUS nt_status;
415 tmp_ctx = talloc_named(NULL, 0, "samba_kdc_check_client_access");
416 if (!tmp_ctx) {
417 return NT_STATUS_NO_MEMORY;
420 /* we allow all kinds of trusts here */
421 nt_status = authsam_account_ok(tmp_ctx,
422 kdc_entry->kdc_db_ctx->samdb,
423 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
424 MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
425 kdc_entry->realm_dn, kdc_entry->msg,
426 workstation, client_name,
427 true, password_change);
429 talloc_free(tmp_ctx);
430 return nt_status;