s4:kdc: hook into heimdal's windc.pac_pk_generate hook
[Samba.git] / source4 / kdc / wdc-samba4.c
blob6fd55df152d0244e47f0e00ab936ff714840b53f
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"
29 * Given the right private pointer from hdb_samba4,
30 * get a PAC from the attached ldb messages.
32 * For PKINIT we also get pk_reply_key and can add PAC_CREDENTIAL_INFO.
34 static krb5_error_code samba_wdc_get_pac(void *priv, krb5_context context,
35 struct hdb_entry_ex *client,
36 const krb5_keyblock *pk_reply_key,
37 krb5_pac *pac)
39 TALLOC_CTX *mem_ctx;
40 DATA_BLOB *pac_blob;
41 krb5_error_code ret;
42 NTSTATUS nt_status;
43 struct samba_kdc_entry *skdc_entry =
44 talloc_get_type_abort(client->ctx,
45 struct samba_kdc_entry);
47 mem_ctx = talloc_named(client->ctx, 0, "samba_get_pac context");
48 if (!mem_ctx) {
49 return ENOMEM;
52 nt_status = samba_kdc_get_pac_blob(mem_ctx, skdc_entry, &pac_blob);
53 if (!NT_STATUS_IS_OK(nt_status)) {
54 talloc_free(mem_ctx);
55 return EINVAL;
58 ret = samba_make_krb5_pac(context, pac_blob, NULL, pac);
60 talloc_free(mem_ctx);
61 return ret;
64 static krb5_error_code samba_wdc_get_pac_compat(void *priv, krb5_context context,
65 struct hdb_entry_ex *client,
66 krb5_pac *pac)
68 return samba_wdc_get_pac(priv, context, client, NULL, pac);
71 /* Resign (and reform, including possibly new groups) a PAC */
73 static krb5_error_code samba_wdc_reget_pac(void *priv, krb5_context context,
74 const krb5_principal client_principal,
75 const krb5_principal delegated_proxy_principal,
76 struct hdb_entry_ex *client,
77 struct hdb_entry_ex *server,
78 struct hdb_entry_ex *krbtgt,
79 krb5_pac *pac)
81 struct samba_kdc_entry *p =
82 talloc_get_type_abort(server->ctx,
83 struct samba_kdc_entry);
84 struct samba_kdc_entry *krbtgt_skdc_entry =
85 talloc_get_type_abort(krbtgt->ctx,
86 struct samba_kdc_entry);
87 TALLOC_CTX *mem_ctx = talloc_named(p, 0, "samba_kdc_reget_pac context");
88 DATA_BLOB *pac_blob;
89 DATA_BLOB *deleg_blob = NULL;
90 krb5_error_code ret;
91 NTSTATUS nt_status;
92 struct PAC_SIGNATURE_DATA *pac_srv_sig;
93 struct PAC_SIGNATURE_DATA *pac_kdc_sig;
94 bool is_in_db, is_untrusted;
96 if (!mem_ctx) {
97 return ENOMEM;
100 /* The user account may be set not to want the PAC */
101 if (!samba_princ_needs_pac(p)) {
102 talloc_free(mem_ctx);
103 return EINVAL;
106 /* If the krbtgt was generated by an RODC, and we are not that
107 * RODC, then we need to regenerate the PAC - we can't trust
108 * it */
109 ret = samba_krbtgt_is_in_db(krbtgt_skdc_entry, &is_in_db, &is_untrusted);
110 if (ret != 0) {
111 talloc_free(mem_ctx);
112 return ret;
115 if (is_untrusted) {
116 struct samba_kdc_entry *client_skdc_entry = NULL;
118 if (client == NULL) {
119 return KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
122 client_skdc_entry = talloc_get_type_abort(client->ctx,
123 struct samba_kdc_entry);
125 nt_status = samba_kdc_get_pac_blob(mem_ctx, client_skdc_entry, &pac_blob);
126 if (!NT_STATUS_IS_OK(nt_status)) {
127 talloc_free(mem_ctx);
128 return EINVAL;
130 } else {
131 pac_blob = talloc_zero(mem_ctx, DATA_BLOB);
132 if (!pac_blob) {
133 talloc_free(mem_ctx);
134 return ENOMEM;
137 pac_srv_sig = talloc_zero(mem_ctx, struct PAC_SIGNATURE_DATA);
138 if (!pac_srv_sig) {
139 talloc_free(mem_ctx);
140 return ENOMEM;
143 pac_kdc_sig = talloc_zero(mem_ctx, struct PAC_SIGNATURE_DATA);
144 if (!pac_kdc_sig) {
145 talloc_free(mem_ctx);
146 return ENOMEM;
149 nt_status = samba_kdc_update_pac_blob(mem_ctx, context,
150 *pac, pac_blob,
151 pac_srv_sig, pac_kdc_sig);
152 if (!NT_STATUS_IS_OK(nt_status)) {
153 DEBUG(0, ("Building PAC failed: %s\n",
154 nt_errstr(nt_status)));
155 talloc_free(mem_ctx);
156 return EINVAL;
159 if (is_in_db) {
160 /* Now check the KDC signature, fetching the correct key based on the enc type */
161 ret = kdc_check_pac(context, pac_srv_sig->signature, pac_kdc_sig, krbtgt);
162 if (ret != 0) {
163 DEBUG(1, ("PAC KDC signature failed to verify\n"));
164 talloc_free(mem_ctx);
165 return ret;
170 if (delegated_proxy_principal) {
171 deleg_blob = talloc_zero(mem_ctx, DATA_BLOB);
172 if (!deleg_blob) {
173 talloc_free(mem_ctx);
174 return ENOMEM;
177 nt_status = samba_kdc_update_delegation_info_blob(mem_ctx,
178 context, *pac,
179 server->entry.principal,
180 delegated_proxy_principal,
181 deleg_blob);
182 if (!NT_STATUS_IS_OK(nt_status)) {
183 DEBUG(0, ("Building PAC failed: %s\n",
184 nt_errstr(nt_status)));
185 talloc_free(mem_ctx);
186 return EINVAL;
190 /* We now completely regenerate this pac */
191 krb5_pac_free(context, *pac);
193 ret = samba_make_krb5_pac(context, pac_blob, deleg_blob, pac);
195 talloc_free(mem_ctx);
196 return ret;
199 static char *get_netbios_name(TALLOC_CTX *mem_ctx, HostAddresses *addrs)
201 char *nb_name = NULL;
202 size_t len;
203 unsigned int i;
205 for (i = 0; addrs && i < addrs->len; i++) {
206 if (addrs->val[i].addr_type != KRB5_ADDRESS_NETBIOS) {
207 continue;
209 len = MIN(addrs->val[i].address.length, 15);
210 nb_name = talloc_strndup(mem_ctx,
211 addrs->val[i].address.data, len);
212 if (nb_name) {
213 break;
217 if ((nb_name == NULL) || (nb_name[0] == '\0')) {
218 return NULL;
221 /* Strip space padding */
222 for (len = strlen(nb_name) - 1;
223 (len > 0) && (nb_name[len] == ' ');
224 --len) {
225 nb_name[len] = '\0';
228 return nb_name;
231 static krb5_data fill_krb5_data(void *data, size_t length)
233 krb5_data kdata;
235 kdata.data = data;
236 kdata.length = length;
238 return kdata;
241 /* this function allocates 'data' using malloc.
242 * The caller is responsible for freeing it */
243 static void samba_kdc_build_edata_reply(NTSTATUS nt_status, DATA_BLOB *e_data)
245 krb5_error_code ret = 0;
246 PA_DATA pa;
247 unsigned char *buf;
248 size_t len;
250 if (!e_data)
251 return;
253 e_data->data = NULL;
254 e_data->length = 0;
256 pa.padata_type = KRB5_PADATA_PW_SALT;
257 pa.padata_value.length = 12;
258 pa.padata_value.data = malloc(pa.padata_value.length);
259 if (!pa.padata_value.data) {
260 e_data->length = 0;
261 e_data->data = NULL;
262 return;
265 SIVAL(pa.padata_value.data, 0, NT_STATUS_V(nt_status));
266 SIVAL(pa.padata_value.data, 4, 0);
267 SIVAL(pa.padata_value.data, 8, 1);
269 ASN1_MALLOC_ENCODE(PA_DATA, buf, len, &pa, &len, ret);
270 free(pa.padata_value.data);
271 if (ret) {
272 return;
275 e_data->data = buf;
276 e_data->length = len;
278 return;
282 static krb5_error_code samba_wdc_check_client_access(void *priv,
283 krb5_context context,
284 krb5_kdc_configuration *config,
285 hdb_entry_ex *client_ex, const char *client_name,
286 hdb_entry_ex *server_ex, const char *server_name,
287 KDC_REQ *req,
288 krb5_data *e_data)
290 struct samba_kdc_entry *kdc_entry;
291 bool password_change;
292 char *workstation;
293 NTSTATUS nt_status;
295 kdc_entry = talloc_get_type(client_ex->ctx, struct samba_kdc_entry);
296 password_change = (server_ex && server_ex->entry.flags.change_pw);
297 workstation = get_netbios_name((TALLOC_CTX *)client_ex->ctx,
298 req->req_body.addresses);
300 nt_status = samba_kdc_check_client_access(kdc_entry,
301 client_name,
302 workstation,
303 password_change);
305 if (!NT_STATUS_IS_OK(nt_status)) {
306 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
307 return ENOMEM;
310 if (e_data) {
311 DATA_BLOB data;
313 samba_kdc_build_edata_reply(nt_status, &data);
314 *e_data = fill_krb5_data(data.data, data.length);
317 return samba_kdc_map_policy_err(nt_status);
320 /* Now do the standard Heimdal check */
321 return kdc_check_flags(context, config,
322 client_ex, client_name,
323 server_ex, server_name,
324 req->msg_type == krb_as_req);
327 static krb5_error_code samba_wdc_plugin_init(krb5_context context, void **ptr)
329 *ptr = NULL;
330 return 0;
333 static void samba_wdc_plugin_fini(void *ptr)
335 return;
338 struct krb5plugin_windc_ftable windc_plugin_table = {
339 .minor_version = KRB5_WINDC_PLUGIN_MINOR,
340 .init = samba_wdc_plugin_init,
341 .fini = samba_wdc_plugin_fini,
342 .pac_generate = samba_wdc_get_pac_compat,
343 .pac_verify = samba_wdc_reget_pac,
344 .client_access = samba_wdc_check_client_access,
345 .pac_pk_generate = samba_wdc_get_pac,