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"
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"
35 NTSTATUS
samba_get_logon_info_pac_blob(TALLOC_CTX
*mem_ctx
,
36 struct auth_user_info_dc
*info
,
39 struct netr_SamInfo3
*info3
;
40 union PAC_INFO pac_info
;
41 enum ndr_err_code ndr_err
;
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
)));
53 pac_info
.logon_info
.info
= talloc_zero(mem_ctx
, struct PAC_LOGON_INFO
);
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
,
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
)));
73 krb5_error_code
samba_make_krb5_pac(krb5_context context
,
80 /* The user account may be set not to want the PAC */
85 ret
= krb5_data_copy(&pac_data
, pac_blob
->data
, pac_blob
->length
);
90 ret
= krb5_pac_init(context
, pac
);
92 krb5_data_free(&pac_data
);
96 ret
= krb5_pac_add_buffer(context
, *pac
, PAC_TYPE_LOGON_INFO
, &pac_data
);
97 krb5_data_free(&pac_data
);
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
) {
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) {
132 } else if (rodc_krbtgt_number
!= p
->kdc_db_ctx
->my_krbtgt_number
) {
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
;
148 /* The user account may be set not to want the PAC */
149 if ( ! samba_princ_needs_pac(client
)) {
154 pac_blob
= talloc_zero(mem_ctx
, DATA_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
),
167 if (!NT_STATUS_IS_OK(nt_status
)) {
168 DEBUG(0, ("Getting user info for PAC failed: %s\n",
169 nt_errstr(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
)));
180 *_pac_blob
= pac_blob
;
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
;
192 ret
= kerberos_pac_to_user_info_dc(mem_ctx
, *pac
,
193 context
, &user_info_dc
, NULL
, NULL
);
195 return NT_STATUS_UNSUCCESSFUL
;
198 nt_status
= samba_get_logon_info_pac_blob(mem_ctx
,
199 user_info_dc
, pac_blob
);
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
)
211 krb5_error_code ret
= 0;
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
) {
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
);
233 e_data
->length
= len
;
238 /* function to map policy errors */
239 krb5_error_code
samba_kdc_map_policy_err(NTSTATUS nt_status
)
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
;
258 ret
= KRB5KDC_ERR_POLICY
;
263 /* Given a kdc entry, consult the account_ok routine in auth/auth_sam.c
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
)
273 tmp_ctx
= talloc_named(NULL
, 0, "samba_kdc_check_client_access");
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
);