s3:test: don't rely on pyhton being in /usr/bin/python in the sids2xids test
[Samba/gebeck_regimport.git] / source4 / kdc / pac-glue.c
blob38db41d837551f6436762733940e0bf33055c06d
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"
34 static
35 NTSTATUS samba_get_logon_info_pac_blob(TALLOC_CTX *mem_ctx,
36 struct auth_user_info_dc *info,
37 DATA_BLOB *pac_data)
39 struct netr_SamInfo3 *info3;
40 union PAC_INFO pac_info;
41 enum ndr_err_code ndr_err;
42 NTSTATUS nt_status;
44 ZERO_STRUCT(pac_info);
46 nt_status = auth_convert_user_info_dc_saminfo3(mem_ctx, info, &info3);
47 if (!NT_STATUS_IS_OK(nt_status)) {
48 DEBUG(1, ("Getting Samba info failed: %s\n",
49 nt_errstr(nt_status)));
50 return nt_status;
53 pac_info.logon_info.info = talloc_zero(mem_ctx, struct PAC_LOGON_INFO);
54 if (!mem_ctx) {
55 return NT_STATUS_NO_MEMORY;
58 pac_info.logon_info.info->info3 = *info3;
60 ndr_err = ndr_push_union_blob(pac_data, mem_ctx, &pac_info,
61 PAC_TYPE_LOGON_INFO,
62 (ndr_push_flags_fn_t)ndr_push_PAC_INFO);
63 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
64 nt_status = ndr_map_error2ntstatus(ndr_err);
65 DEBUG(1, ("PAC (presig) push failed: %s\n",
66 nt_errstr(nt_status)));
67 return nt_status;
70 return NT_STATUS_OK;
73 krb5_error_code samba_make_krb5_pac(krb5_context context,
74 DATA_BLOB *pac_blob,
75 krb5_pac *pac)
77 krb5_data pac_data;
78 krb5_error_code ret;
80 /* The user account may be set not to want the PAC */
81 if (!pac_blob) {
82 return 0;
85 ret = krb5_data_copy(&pac_data, pac_blob->data, pac_blob->length);
86 if (ret != 0) {
87 return ret;
90 ret = krb5_pac_init(context, pac);
91 if (ret != 0) {
92 krb5_data_free(&pac_data);
93 return ret;
96 ret = krb5_pac_add_buffer(context, *pac, PAC_TYPE_LOGON_INFO, &pac_data);
97 krb5_data_free(&pac_data);
98 if (ret != 0) {
99 return ret;
102 return ret;
105 bool samba_princ_needs_pac(struct hdb_entry_ex *princ)
108 struct samba_kdc_entry *p = talloc_get_type(princ->ctx, struct samba_kdc_entry);
109 uint32_t userAccountControl;
112 /* The service account may be set not to want the PAC */
113 userAccountControl = ldb_msg_find_attr_as_uint(p->msg, "userAccountControl", 0);
114 if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
115 return false;
118 return true;
121 /* Was the krbtgt an RODC (and we are not) */
122 bool samba_krbtgt_was_untrusted_rodc(struct hdb_entry_ex *princ)
125 struct samba_kdc_entry *p = talloc_get_type(princ->ctx, struct samba_kdc_entry);
126 int rodc_krbtgt_number;
128 /* Determine if this was printed by an RODC */
129 rodc_krbtgt_number = ldb_msg_find_attr_as_int(p->msg, "msDS-SecondaryKrbTgtNumber", -1);
130 if (rodc_krbtgt_number == -1) {
131 return false;
132 } else if (rodc_krbtgt_number != p->kdc_db_ctx->my_krbtgt_number) {
133 return true;
136 return false;
139 NTSTATUS samba_kdc_get_pac_blob(TALLOC_CTX *mem_ctx,
140 struct hdb_entry_ex *client,
141 DATA_BLOB **_pac_blob)
143 struct samba_kdc_entry *p = talloc_get_type(client->ctx, struct samba_kdc_entry);
144 struct auth_user_info_dc *user_info_dc;
145 DATA_BLOB *pac_blob;
146 NTSTATUS nt_status;
148 /* The user account may be set not to want the PAC */
149 if ( ! samba_princ_needs_pac(client)) {
150 *_pac_blob = NULL;
151 return NT_STATUS_OK;
154 pac_blob = talloc_zero(mem_ctx, DATA_BLOB);
155 if (!pac_blob) {
156 return NT_STATUS_NO_MEMORY;
159 nt_status = authsam_make_user_info_dc(mem_ctx, p->kdc_db_ctx->samdb,
160 lpcfg_netbios_name(p->kdc_db_ctx->lp_ctx),
161 lpcfg_sam_name(p->kdc_db_ctx->lp_ctx),
162 p->realm_dn,
163 p->msg,
164 data_blob(NULL, 0),
165 data_blob(NULL, 0),
166 &user_info_dc);
167 if (!NT_STATUS_IS_OK(nt_status)) {
168 DEBUG(0, ("Getting user info for PAC failed: %s\n",
169 nt_errstr(nt_status)));
170 return nt_status;
173 nt_status = samba_get_logon_info_pac_blob(mem_ctx, user_info_dc, pac_blob);
174 if (!NT_STATUS_IS_OK(nt_status)) {
175 DEBUG(0, ("Building PAC failed: %s\n",
176 nt_errstr(nt_status)));
177 return nt_status;
180 *_pac_blob = pac_blob;
181 return NT_STATUS_OK;
184 NTSTATUS samba_kdc_update_pac_blob(TALLOC_CTX *mem_ctx,
185 krb5_context context,
186 krb5_pac *pac, DATA_BLOB *pac_blob)
188 struct auth_user_info_dc *user_info_dc;
189 krb5_error_code ret;
190 NTSTATUS nt_status;
192 ret = kerberos_pac_to_user_info_dc(mem_ctx, *pac,
193 context, &user_info_dc, NULL, NULL);
194 if (ret) {
195 return NT_STATUS_UNSUCCESSFUL;
198 nt_status = samba_get_logon_info_pac_blob(mem_ctx,
199 user_info_dc, pac_blob);
201 return nt_status;
204 /* this function allocates 'data' using malloc.
205 * The caller is responsible for freeing it */
206 void samba_kdc_build_edata_reply(NTSTATUS nt_status, DATA_BLOB *e_data)
208 PA_DATA pa;
209 unsigned char *buf;
210 size_t len;
211 krb5_error_code ret = 0;
213 if (!e_data)
214 return;
216 pa.padata_type = KRB5_PADATA_PW_SALT;
217 pa.padata_value.length = 12;
218 pa.padata_value.data = malloc(pa.padata_value.length);
219 if (!pa.padata_value.data) {
220 e_data->length = 0;
221 e_data->data = NULL;
222 return;
225 SIVAL(pa.padata_value.data, 0, NT_STATUS_V(nt_status));
226 SIVAL(pa.padata_value.data, 4, 0);
227 SIVAL(pa.padata_value.data, 8, 1);
229 ASN1_MALLOC_ENCODE(PA_DATA, buf, len, &pa, &len, ret);
230 free(pa.padata_value.data);
232 e_data->data = buf;
233 e_data->length = len;
235 return;
238 /* function to map policy errors */
239 krb5_error_code samba_kdc_map_policy_err(NTSTATUS nt_status)
241 krb5_error_code ret;
243 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_MUST_CHANGE))
244 ret = KRB5KDC_ERR_KEY_EXPIRED;
245 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_EXPIRED))
246 ret = KRB5KDC_ERR_KEY_EXPIRED;
247 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_EXPIRED))
248 ret = KRB5KDC_ERR_CLIENT_REVOKED;
249 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_DISABLED))
250 ret = KRB5KDC_ERR_CLIENT_REVOKED;
251 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_LOGON_HOURS))
252 ret = KRB5KDC_ERR_CLIENT_REVOKED;
253 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_LOCKED_OUT))
254 ret = KRB5KDC_ERR_CLIENT_REVOKED;
255 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_WORKSTATION))
256 ret = KRB5KDC_ERR_POLICY;
257 else
258 ret = KRB5KDC_ERR_POLICY;
260 return ret;
263 /* Given a kdc entry, consult the account_ok routine in auth/auth_sam.c
264 * for consistency */
265 NTSTATUS samba_kdc_check_client_access(struct samba_kdc_entry *kdc_entry,
266 const char *client_name,
267 const char *workstation,
268 bool password_change)
270 TALLOC_CTX *tmp_ctx;
271 NTSTATUS nt_status;
273 tmp_ctx = talloc_named(NULL, 0, "samba_kdc_check_client_access");
274 if (!tmp_ctx) {
275 return NT_STATUS_NO_MEMORY;
278 /* we allow all kinds of trusts here */
279 nt_status = authsam_account_ok(tmp_ctx,
280 kdc_entry->kdc_db_ctx->samdb,
281 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
282 MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
283 kdc_entry->realm_dn, kdc_entry->msg,
284 workstation, client_name,
285 true, password_change);
287 talloc_free(tmp_ctx);
288 return nt_status;