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 "../libds/common/flags.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "librpc/gen_ndr/ndr_krb5pac.h"
28 #include "librpc/gen_ndr/krb5pac.h"
29 #include "auth/auth.h"
30 #include "auth/auth_sam.h"
31 #include "auth/auth_sam_reply.h"
33 #include "param/param.h"
35 struct krb5_dh_moduli
;
36 struct _krb5_krb_auth_data
;
38 static krb5_error_code
samba_kdc_plugin_init(krb5_context context
, void **ptr
)
44 static void samba_kdc_plugin_fini(void *ptr
)
50 get_logon_info_pac_blob(TALLOC_CTX
*mem_ctx
,
51 struct smb_iconv_convenience
*ic
,
52 struct auth_serversupplied_info
*info
,
55 struct netr_SamInfo3
*info3
;
56 union PAC_INFO pac_info
;
57 enum ndr_err_code ndr_err
;
60 ZERO_STRUCT(pac_info
);
62 nt_status
= auth_convert_server_info_saminfo3(mem_ctx
, info
, &info3
);
63 if (!NT_STATUS_IS_OK(nt_status
)) {
64 DEBUG(1, ("Getting Samba info failed: %s\n",
65 nt_errstr(nt_status
)));
69 pac_info
.logon_info
.info
= talloc_zero(mem_ctx
, struct PAC_LOGON_INFO
);
71 return NT_STATUS_NO_MEMORY
;
74 pac_info
.logon_info
.info
->info3
= *info3
;
76 ndr_err
= ndr_push_union_blob(pac_data
, mem_ctx
, ic
, &pac_info
,
78 (ndr_push_flags_fn_t
)ndr_push_PAC_INFO
);
79 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
80 nt_status
= ndr_map_error2ntstatus(ndr_err
);
81 DEBUG(1, ("PAC (presig) push failed: %s\n",
82 nt_errstr(nt_status
)));
89 static krb5_error_code
make_krb5_pac(krb5_context context
,
96 ret
= krb5_data_copy(&pac_data
, pac_blob
->data
, pac_blob
->length
);
101 ret
= krb5_pac_init(context
, pac
);
103 krb5_data_free(&pac_data
);
107 ret
= krb5_pac_add_buffer(context
, *pac
, PAC_TYPE_LOGON_INFO
, &pac_data
);
108 krb5_data_free(&pac_data
);
116 static bool princ_needs_pac(struct hdb_entry_ex
*princ
)
119 struct hdb_samba4_private
*p
= talloc_get_type(princ
->ctx
, struct hdb_samba4_private
);
120 unsigned int userAccountControl
;
123 /* The service account may be set not to want the PAC */
124 userAccountControl
= ldb_msg_find_attr_as_uint(p
->msg
, "userAccountControl", 0);
125 if (userAccountControl
& UF_NO_AUTH_DATA_REQUIRED
) {
132 static NTSTATUS
samba_kdc_get_pac_blob(TALLOC_CTX
*mem_ctx
,
133 struct hdb_entry_ex
*client
,
134 DATA_BLOB
**_pac_blob
)
136 struct hdb_samba4_private
*p
= talloc_get_type(client
->ctx
, struct hdb_samba4_private
);
137 struct auth_serversupplied_info
*server_info
;
141 /* The user account may be set not to want the PAC */
142 if ( ! princ_needs_pac(client
)) {
147 pac_blob
= talloc_zero(mem_ctx
, DATA_BLOB
);
149 return NT_STATUS_NO_MEMORY
;
152 nt_status
= authsam_make_server_info(mem_ctx
, p
->samdb
,
153 lp_netbios_name(p
->lp_ctx
),
154 lp_sam_name(p
->lp_ctx
),
160 if (!NT_STATUS_IS_OK(nt_status
)) {
161 DEBUG(0, ("Getting user info for PAC failed: %s\n",
162 nt_errstr(nt_status
)));
166 nt_status
= get_logon_info_pac_blob(mem_ctx
,
167 p
->iconv_convenience
,
168 server_info
, pac_blob
);
169 if (!NT_STATUS_IS_OK(nt_status
)) {
170 DEBUG(0, ("Building PAC failed: %s\n",
171 nt_errstr(nt_status
)));
175 *_pac_blob
= pac_blob
;
179 static NTSTATUS
samba_kdc_update_pac_blob(TALLOC_CTX
*mem_ctx
,
180 krb5_context context
,
181 struct smb_iconv_convenience
*ic
,
182 krb5_pac
*pac
, DATA_BLOB
*pac_blob
)
184 struct auth_serversupplied_info
*server_info
;
188 ret
= kerberos_pac_to_server_info(mem_ctx
, ic
, *pac
,
189 context
, &server_info
);
191 return NT_STATUS_UNSUCCESSFUL
;
194 nt_status
= samba_get_logon_info_pac_blob(mem_ctx
, ic
,
195 server_info
, pac_blob
);
200 /* Given the right private pointer from hdb_samba4, get a PAC from the attached ldb messages */
201 static krb5_error_code
samba_kdc_get_pac(void *priv
, krb5_context context
,
202 struct hdb_entry_ex
*client
,
210 mem_ctx
= talloc_named(client
->ctx
, 0, "samba_get_pac context");
215 nt_status
= samba_kdc_get_pac_blob(mem_ctx
, client
, &pac_blob
);
216 if (!NT_STATUS_IS_OK(nt_status
)) {
217 talloc_free(mem_ctx
);
221 ret
= make_krb5_pac(context
, pac_blob
, pac
);
223 talloc_free(mem_ctx
);
227 /* Resign (and reform, including possibly new groups) a PAC */
229 static krb5_error_code
samba_kdc_reget_pac(void *priv
, krb5_context context
,
230 const krb5_principal client_principal
,
231 struct hdb_entry_ex
*client
,
232 struct hdb_entry_ex
*server
, krb5_pac
*pac
)
234 struct hdb_samba4_private
*p
= talloc_get_type(server
->ctx
, struct hdb_samba4_private
);
235 TALLOC_CTX
*mem_ctx
= talloc_named(p
, 0, "samba_kdc_reget_pac context");
244 pac_blob
= talloc_zero(mem_ctx
, DATA_BLOB
);
246 talloc_free(mem_ctx
);
250 /* The user account may be set not to want the PAC */
251 if ( ! princ_needs_pac(server
)) {
252 talloc_free(mem_ctx
);
256 nt_status
= samba_kdc_update_pac_blob(mem_ctx
, context
,
257 p
->iconv_convenience
,
259 if (!NT_STATUS_IS_OK(nt_status
)) {
260 DEBUG(0, ("Building PAC failed: %s\n",
261 nt_errstr(nt_status
)));
262 talloc_free(mem_ctx
);
266 /* We now completly regenerate this pac */
267 krb5_pac_free(context
, *pac
);
269 ret
= make_krb5_pac(context
, pac_blob
, pac
);
271 talloc_free(mem_ctx
);
275 static void samba_kdc_build_edata_reply(TALLOC_CTX
*tmp_ctx
, krb5_data
*e_data
,
281 krb5_error_code ret
= 0;
286 pa
.padata_type
= KRB5_PADATA_PW_SALT
;
287 pa
.padata_value
.length
= 12;
288 pa
.padata_value
.data
= malloc(pa
.padata_value
.length
);
289 if (!pa
.padata_value
.data
) {
295 SIVAL(pa
.padata_value
.data
, 0, NT_STATUS_V(nt_status
));
296 SIVAL(pa
.padata_value
.data
, 4, 0);
297 SIVAL(pa
.padata_value
.data
, 8, 1);
299 ASN1_MALLOC_ENCODE(PA_DATA
, buf
, len
, &pa
, &len
, ret
);
300 free(pa
.padata_value
.data
);
303 e_data
->length
= len
;
308 /* Given an hdb entry (and in particular it's private member), consult
309 * the account_ok routine in auth/auth_sam.c for consistancy */
312 static krb5_error_code
samba_kdc_check_client_access(void *priv
,
313 krb5_context context
,
314 krb5_kdc_configuration
*config
,
315 hdb_entry_ex
*client_ex
, const char *client_name
,
316 hdb_entry_ex
*server_ex
, const char *server_name
,
323 struct hdb_samba4_private
*p
;
324 char *workstation
= NULL
;
325 HostAddresses
*addresses
= req
->req_body
.addresses
;
327 bool password_change
;
329 tmp_ctx
= talloc_new(client_ex
->ctx
);
330 p
= talloc_get_type(client_ex
->ctx
, struct hdb_samba4_private
);
337 for (i
=0; i
< addresses
->len
; i
++) {
338 if (addresses
->val
->addr_type
== KRB5_ADDRESS_NETBIOS
) {
339 workstation
= talloc_strndup(tmp_ctx
, addresses
->val
->address
.data
, MIN(addresses
->val
->address
.length
, 15));
347 /* Strip space padding */
349 i
= MIN(strlen(workstation
), 15);
350 for (; i
> 0 && workstation
[i
- 1] == ' '; i
--) {
351 workstation
[i
- 1] = '\0';
355 password_change
= (server_ex
&& server_ex
->entry
.flags
.change_pw
);
357 /* we allow all kinds of trusts here */
358 nt_status
= authsam_account_ok(tmp_ctx
,
360 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT
| MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
,
364 client_name
, true, password_change
);
366 if (NT_STATUS_IS_OK(nt_status
)) {
367 /* Now do the standard Heimdal check */
368 ret
= kdc_check_flags(context
, config
,
369 client_ex
, client_name
,
370 server_ex
, server_name
,
371 req
->msg_type
== krb_as_req
);
373 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_PASSWORD_MUST_CHANGE
))
374 ret
= KRB5KDC_ERR_KEY_EXPIRED
;
375 else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_PASSWORD_EXPIRED
))
376 ret
= KRB5KDC_ERR_KEY_EXPIRED
;
377 else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCOUNT_EXPIRED
))
378 ret
= KRB5KDC_ERR_CLIENT_REVOKED
;
379 else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCOUNT_DISABLED
))
380 ret
= KRB5KDC_ERR_CLIENT_REVOKED
;
381 else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_INVALID_LOGON_HOURS
))
382 ret
= KRB5KDC_ERR_CLIENT_REVOKED
;
383 else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCOUNT_LOCKED_OUT
))
384 ret
= KRB5KDC_ERR_CLIENT_REVOKED
;
385 else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_INVALID_WORKSTATION
))
386 ret
= KRB5KDC_ERR_POLICY
;
388 ret
= KRB5KDC_ERR_POLICY
;
390 samba_kdc_build_edata_reply(tmp_ctx
, e_data
, nt_status
);
396 struct krb5plugin_windc_ftable windc_plugin_table
= {
397 .minor_version
= KRB5_WINDC_PLUGING_MINOR
,
398 .init
= samba_kdc_plugin_init
,
399 .fini
= samba_kdc_plugin_fini
,
400 .pac_generate
= samba_kdc_get_pac
,
401 .pac_verify
= samba_kdc_reget_pac
,
402 .client_access
= samba_kdc_check_client_access
,