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/>.
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
,
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");
52 nt_status
= samba_kdc_get_pac_blob(mem_ctx
, skdc_entry
, &pac_blob
);
53 if (!NT_STATUS_IS_OK(nt_status
)) {
58 ret
= samba_make_krb5_pac(context
, pac_blob
, NULL
, pac
);
64 static krb5_error_code
samba_wdc_get_pac_compat(void *priv
, krb5_context context
,
65 struct hdb_entry_ex
*client
,
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
,
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");
89 DATA_BLOB
*deleg_blob
= NULL
;
92 struct PAC_SIGNATURE_DATA
*pac_srv_sig
;
93 struct PAC_SIGNATURE_DATA
*pac_kdc_sig
;
94 bool is_in_db
, is_untrusted
;
100 /* The user account may be set not to want the PAC */
101 if (!samba_princ_needs_pac(p
)) {
102 talloc_free(mem_ctx
);
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
109 ret
= samba_krbtgt_is_in_db(krbtgt_skdc_entry
, &is_in_db
, &is_untrusted
);
111 talloc_free(mem_ctx
);
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
);
131 pac_blob
= talloc_zero(mem_ctx
, DATA_BLOB
);
133 talloc_free(mem_ctx
);
137 pac_srv_sig
= talloc_zero(mem_ctx
, struct PAC_SIGNATURE_DATA
);
139 talloc_free(mem_ctx
);
143 pac_kdc_sig
= talloc_zero(mem_ctx
, struct PAC_SIGNATURE_DATA
);
145 talloc_free(mem_ctx
);
149 nt_status
= samba_kdc_update_pac_blob(mem_ctx
, context
,
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
);
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
);
163 DEBUG(1, ("PAC KDC signature failed to verify\n"));
164 talloc_free(mem_ctx
);
170 if (delegated_proxy_principal
) {
171 deleg_blob
= talloc_zero(mem_ctx
, DATA_BLOB
);
173 talloc_free(mem_ctx
);
177 nt_status
= samba_kdc_update_delegation_info_blob(mem_ctx
,
179 server
->entry
.principal
,
180 delegated_proxy_principal
,
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
);
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
);
199 static char *get_netbios_name(TALLOC_CTX
*mem_ctx
, HostAddresses
*addrs
)
201 char *nb_name
= NULL
;
205 for (i
= 0; addrs
&& i
< addrs
->len
; i
++) {
206 if (addrs
->val
[i
].addr_type
!= KRB5_ADDRESS_NETBIOS
) {
209 len
= MIN(addrs
->val
[i
].address
.length
, 15);
210 nb_name
= talloc_strndup(mem_ctx
,
211 addrs
->val
[i
].address
.data
, len
);
217 if ((nb_name
== NULL
) || (nb_name
[0] == '\0')) {
221 /* Strip space padding */
222 for (len
= strlen(nb_name
) - 1;
223 (len
> 0) && (nb_name
[len
] == ' ');
231 static krb5_data
fill_krb5_data(void *data
, size_t length
)
236 kdata
.length
= length
;
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;
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
) {
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
);
276 e_data
->length
= len
;
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
,
290 struct samba_kdc_entry
*kdc_entry
;
291 bool password_change
;
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
,
305 if (!NT_STATUS_IS_OK(nt_status
)) {
306 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_MEMORY
)) {
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
)
333 static void samba_wdc_plugin_fini(void *ptr
)
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
,