s4-kdc/wdc-samba4: add a copy of samba_kdc_build_edata_reply for Heimdal.
[Samba.git] / source4 / kdc / wdc-samba4.c
blob4e7ea4b09071fec58b3085dc5c3db2b5b32bcc1c
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"
28 /* Given the right private pointer from hdb_samba4, get a PAC from the attached ldb messages */
29 static krb5_error_code samba_wdc_get_pac(void *priv, krb5_context context,
30 struct hdb_entry_ex *client,
31 krb5_pac *pac)
33 TALLOC_CTX *mem_ctx;
34 DATA_BLOB *pac_blob;
35 krb5_error_code ret;
36 NTSTATUS nt_status;
37 struct samba_kdc_entry *skdc_entry =
38 talloc_get_type_abort(client->ctx,
39 struct samba_kdc_entry);
41 mem_ctx = talloc_named(client->ctx, 0, "samba_get_pac context");
42 if (!mem_ctx) {
43 return ENOMEM;
46 nt_status = samba_kdc_get_pac_blob(mem_ctx, skdc_entry, &pac_blob);
47 if (!NT_STATUS_IS_OK(nt_status)) {
48 talloc_free(mem_ctx);
49 return EINVAL;
52 ret = samba_make_krb5_pac(context, pac_blob, NULL, pac);
54 talloc_free(mem_ctx);
55 return ret;
58 /* Resign (and reform, including possibly new groups) a PAC */
60 static krb5_error_code samba_wdc_reget_pac(void *priv, krb5_context context,
61 const krb5_principal client_principal,
62 const krb5_principal delegated_proxy_principal,
63 struct hdb_entry_ex *client,
64 struct hdb_entry_ex *server,
65 struct hdb_entry_ex *krbtgt,
66 krb5_pac *pac)
68 struct samba_kdc_entry *p =
69 talloc_get_type_abort(server->ctx,
70 struct samba_kdc_entry);
71 struct samba_kdc_entry *krbtgt_skdc_entry =
72 talloc_get_type_abort(krbtgt->ctx,
73 struct samba_kdc_entry);
74 TALLOC_CTX *mem_ctx = talloc_named(p, 0, "samba_kdc_reget_pac context");
75 DATA_BLOB *pac_blob;
76 DATA_BLOB *deleg_blob = NULL;
77 krb5_error_code ret;
78 NTSTATUS nt_status;
79 struct PAC_SIGNATURE_DATA *pac_srv_sig;
80 struct PAC_SIGNATURE_DATA *pac_kdc_sig;
81 bool is_in_db, is_untrusted;
83 if (!mem_ctx) {
84 return ENOMEM;
87 /* The user account may be set not to want the PAC */
88 if (!samba_princ_needs_pac(p)) {
89 talloc_free(mem_ctx);
90 return EINVAL;
93 /* If the krbtgt was generated by an RODC, and we are not that
94 * RODC, then we need to regenerate the PAC - we can't trust
95 * it */
96 ret = samba_krbtgt_is_in_db(krbtgt_skdc_entry, &is_in_db, &is_untrusted);
97 if (ret != 0) {
98 talloc_free(mem_ctx);
99 return ret;
102 if (is_untrusted) {
103 struct samba_kdc_entry *client_skdc_entry = NULL;
105 if (client == NULL) {
106 return KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
109 client_skdc_entry = talloc_get_type_abort(client->ctx,
110 struct samba_kdc_entry);
112 nt_status = samba_kdc_get_pac_blob(mem_ctx, client_skdc_entry, &pac_blob);
113 if (!NT_STATUS_IS_OK(nt_status)) {
114 talloc_free(mem_ctx);
115 return EINVAL;
117 } else {
118 pac_blob = talloc_zero(mem_ctx, DATA_BLOB);
119 if (!pac_blob) {
120 talloc_free(mem_ctx);
121 return ENOMEM;
124 pac_srv_sig = talloc_zero(mem_ctx, struct PAC_SIGNATURE_DATA);
125 if (!pac_srv_sig) {
126 talloc_free(mem_ctx);
127 return ENOMEM;
130 pac_kdc_sig = talloc_zero(mem_ctx, struct PAC_SIGNATURE_DATA);
131 if (!pac_kdc_sig) {
132 talloc_free(mem_ctx);
133 return ENOMEM;
136 nt_status = samba_kdc_update_pac_blob(mem_ctx, context,
137 *pac, pac_blob,
138 pac_srv_sig, pac_kdc_sig);
139 if (!NT_STATUS_IS_OK(nt_status)) {
140 DEBUG(0, ("Building PAC failed: %s\n",
141 nt_errstr(nt_status)));
142 talloc_free(mem_ctx);
143 return EINVAL;
146 if (is_in_db) {
147 /* Now check the KDC signature, fetching the correct key based on the enc type */
148 ret = kdc_check_pac(context, pac_srv_sig->signature, pac_kdc_sig, krbtgt);
149 if (ret != 0) {
150 DEBUG(1, ("PAC KDC signature failed to verify\n"));
151 talloc_free(mem_ctx);
152 return ret;
157 if (delegated_proxy_principal) {
158 deleg_blob = talloc_zero(mem_ctx, DATA_BLOB);
159 if (!deleg_blob) {
160 talloc_free(mem_ctx);
161 return ENOMEM;
164 nt_status = samba_kdc_update_delegation_info_blob(mem_ctx,
165 context, *pac,
166 server->entry.principal,
167 delegated_proxy_principal,
168 deleg_blob);
169 if (!NT_STATUS_IS_OK(nt_status)) {
170 DEBUG(0, ("Building PAC failed: %s\n",
171 nt_errstr(nt_status)));
172 talloc_free(mem_ctx);
173 return EINVAL;
177 /* We now completely regenerate this pac */
178 krb5_pac_free(context, *pac);
180 ret = samba_make_krb5_pac(context, pac_blob, deleg_blob, pac);
182 talloc_free(mem_ctx);
183 return ret;
186 static char *get_netbios_name(TALLOC_CTX *mem_ctx, HostAddresses *addrs)
188 char *nb_name = NULL;
189 size_t len;
190 unsigned int i;
192 for (i = 0; addrs && i < addrs->len; i++) {
193 if (addrs->val[i].addr_type != KRB5_ADDRESS_NETBIOS) {
194 continue;
196 len = MIN(addrs->val[i].address.length, 15);
197 nb_name = talloc_strndup(mem_ctx,
198 addrs->val[i].address.data, len);
199 if (nb_name) {
200 break;
204 if ((nb_name == NULL) || (nb_name[0] == '\0')) {
205 return NULL;
208 /* Strip space padding */
209 for (len = strlen(nb_name) - 1;
210 (len > 0) && (nb_name[len] == ' ');
211 --len) {
212 nb_name[len] = '\0';
215 return nb_name;
218 static krb5_data fill_krb5_data(void *data, size_t length)
220 krb5_data kdata;
222 kdata.data = data;
223 kdata.length = length;
225 return kdata;
228 /* this function allocates 'data' using malloc.
229 * The caller is responsible for freeing it */
230 static void samba_kdc_build_edata_reply(NTSTATUS nt_status, DATA_BLOB *e_data)
232 krb5_error_code ret = 0;
233 PA_DATA pa;
234 unsigned char *buf;
235 size_t len;
237 if (!e_data)
238 return;
240 e_data->data = NULL;
241 e_data->length = 0;
243 pa.padata_type = KRB5_PADATA_PW_SALT;
244 pa.padata_value.length = 12;
245 pa.padata_value.data = malloc(pa.padata_value.length);
246 if (!pa.padata_value.data) {
247 e_data->length = 0;
248 e_data->data = NULL;
249 return;
252 SIVAL(pa.padata_value.data, 0, NT_STATUS_V(nt_status));
253 SIVAL(pa.padata_value.data, 4, 0);
254 SIVAL(pa.padata_value.data, 8, 1);
256 ASN1_MALLOC_ENCODE(PA_DATA, buf, len, &pa, &len, ret);
257 free(pa.padata_value.data);
258 if (ret) {
259 return;
262 e_data->data = buf;
263 e_data->length = len;
265 return;
269 static krb5_error_code samba_wdc_check_client_access(void *priv,
270 krb5_context context,
271 krb5_kdc_configuration *config,
272 hdb_entry_ex *client_ex, const char *client_name,
273 hdb_entry_ex *server_ex, const char *server_name,
274 KDC_REQ *req,
275 krb5_data *e_data)
277 struct samba_kdc_entry *kdc_entry;
278 bool password_change;
279 char *workstation;
280 NTSTATUS nt_status;
282 kdc_entry = talloc_get_type(client_ex->ctx, struct samba_kdc_entry);
283 password_change = (server_ex && server_ex->entry.flags.change_pw);
284 workstation = get_netbios_name((TALLOC_CTX *)client_ex->ctx,
285 req->req_body.addresses);
287 nt_status = samba_kdc_check_client_access(kdc_entry,
288 client_name,
289 workstation,
290 password_change);
292 if (!NT_STATUS_IS_OK(nt_status)) {
293 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
294 return ENOMEM;
297 if (e_data) {
298 DATA_BLOB data;
300 samba_kdc_build_edata_reply(nt_status, &data);
301 *e_data = fill_krb5_data(data.data, data.length);
304 return samba_kdc_map_policy_err(nt_status);
307 /* Now do the standard Heimdal check */
308 return kdc_check_flags(context, config,
309 client_ex, client_name,
310 server_ex, server_name,
311 req->msg_type == krb_as_req);
314 static krb5_error_code samba_wdc_plugin_init(krb5_context context, void **ptr)
316 *ptr = NULL;
317 return 0;
320 static void samba_wdc_plugin_fini(void *ptr)
322 return;
325 struct krb5plugin_windc_ftable windc_plugin_table = {
326 .minor_version = KRB5_WINDC_PLUGIN_MINOR,
327 .init = samba_wdc_plugin_init,
328 .fini = samba_wdc_plugin_fini,
329 .pac_generate = samba_wdc_get_pac,
330 .pac_verify = samba_wdc_reget_pac,
331 .client_access = samba_wdc_check_client_access,