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 "param/param.h"
31 #include "librpc/gen_ndr/ndr_krb5pac.h"
34 NTSTATUS
samba_get_logon_info_pac_blob(TALLOC_CTX
*mem_ctx
,
35 struct auth_user_info_dc
*info
,
38 struct netr_SamInfo3
*info3
;
39 union PAC_INFO pac_info
;
40 enum ndr_err_code ndr_err
;
43 ZERO_STRUCT(pac_info
);
45 nt_status
= auth_convert_user_info_dc_saminfo3(mem_ctx
, info
, &info3
);
46 if (!NT_STATUS_IS_OK(nt_status
)) {
47 DEBUG(1, ("Getting Samba info failed: %s\n",
48 nt_errstr(nt_status
)));
52 pac_info
.logon_info
.info
= talloc_zero(mem_ctx
, struct PAC_LOGON_INFO
);
54 return NT_STATUS_NO_MEMORY
;
57 pac_info
.logon_info
.info
->info3
= *info3
;
59 ndr_err
= ndr_push_union_blob(pac_data
, mem_ctx
, &pac_info
,
61 (ndr_push_flags_fn_t
)ndr_push_PAC_INFO
);
62 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
63 nt_status
= ndr_map_error2ntstatus(ndr_err
);
64 DEBUG(1, ("PAC (presig) push failed: %s\n",
65 nt_errstr(nt_status
)));
72 krb5_error_code
samba_make_krb5_pac(krb5_context context
,
79 /* The user account may be set not to want the PAC */
84 ret
= krb5_data_copy(&pac_data
, pac_blob
->data
, pac_blob
->length
);
89 ret
= krb5_pac_init(context
, pac
);
91 krb5_data_free(&pac_data
);
95 ret
= krb5_pac_add_buffer(context
, *pac
, PAC_TYPE_LOGON_INFO
, &pac_data
);
96 krb5_data_free(&pac_data
);
104 bool samba_princ_needs_pac(struct hdb_entry_ex
*princ
)
107 struct samba_kdc_entry
*p
= talloc_get_type(princ
->ctx
, struct samba_kdc_entry
);
108 uint32_t userAccountControl
;
111 /* The service account may be set not to want the PAC */
112 userAccountControl
= ldb_msg_find_attr_as_uint(p
->msg
, "userAccountControl", 0);
113 if (userAccountControl
& UF_NO_AUTH_DATA_REQUIRED
) {
120 /* Was the krbtgt an RODC (and we are not) */
121 bool samba_krbtgt_was_untrusted_rodc(struct hdb_entry_ex
*princ
)
124 struct samba_kdc_entry
*p
= talloc_get_type(princ
->ctx
, struct samba_kdc_entry
);
125 int rodc_krbtgt_number
;
127 /* Determine if this was printed by an RODC */
128 rodc_krbtgt_number
= ldb_msg_find_attr_as_int(p
->msg
, "msDS-SecondaryKrbTgtNumber", -1);
129 if (rodc_krbtgt_number
== -1) {
131 } else if (rodc_krbtgt_number
!= p
->kdc_db_ctx
->my_krbtgt_number
) {
138 NTSTATUS
samba_kdc_get_pac_blob(TALLOC_CTX
*mem_ctx
,
139 struct hdb_entry_ex
*client
,
140 DATA_BLOB
**_pac_blob
)
142 struct samba_kdc_entry
*p
= talloc_get_type(client
->ctx
, struct samba_kdc_entry
);
143 struct auth_user_info_dc
*user_info_dc
;
147 /* The user account may be set not to want the PAC */
148 if ( ! samba_princ_needs_pac(client
)) {
153 pac_blob
= talloc_zero(mem_ctx
, DATA_BLOB
);
155 return NT_STATUS_NO_MEMORY
;
158 nt_status
= authsam_make_user_info_dc(mem_ctx
, p
->kdc_db_ctx
->samdb
,
159 lpcfg_netbios_name(p
->kdc_db_ctx
->lp_ctx
),
160 lpcfg_sam_name(p
->kdc_db_ctx
->lp_ctx
),
166 if (!NT_STATUS_IS_OK(nt_status
)) {
167 DEBUG(0, ("Getting user info for PAC failed: %s\n",
168 nt_errstr(nt_status
)));
172 nt_status
= samba_get_logon_info_pac_blob(mem_ctx
, user_info_dc
, pac_blob
);
173 if (!NT_STATUS_IS_OK(nt_status
)) {
174 DEBUG(0, ("Building PAC failed: %s\n",
175 nt_errstr(nt_status
)));
179 *_pac_blob
= pac_blob
;
183 NTSTATUS
samba_kdc_update_pac_blob(TALLOC_CTX
*mem_ctx
,
184 krb5_context context
,
185 krb5_pac
*pac
, DATA_BLOB
*pac_blob
)
187 struct auth_user_info_dc
*user_info_dc
;
191 ret
= kerberos_pac_to_user_info_dc(mem_ctx
, *pac
,
192 context
, &user_info_dc
, NULL
, NULL
);
194 return NT_STATUS_UNSUCCESSFUL
;
197 nt_status
= samba_get_logon_info_pac_blob(mem_ctx
,
198 user_info_dc
, pac_blob
);
203 /* this function allocates 'data' using malloc.
204 * The caller is responsible for freeing it */
205 void samba_kdc_build_edata_reply(NTSTATUS nt_status
, DATA_BLOB
*e_data
)
210 krb5_error_code ret
= 0;
215 pa
.padata_type
= KRB5_PADATA_PW_SALT
;
216 pa
.padata_value
.length
= 12;
217 pa
.padata_value
.data
= malloc(pa
.padata_value
.length
);
218 if (!pa
.padata_value
.data
) {
224 SIVAL(pa
.padata_value
.data
, 0, NT_STATUS_V(nt_status
));
225 SIVAL(pa
.padata_value
.data
, 4, 0);
226 SIVAL(pa
.padata_value
.data
, 8, 1);
228 ASN1_MALLOC_ENCODE(PA_DATA
, buf
, len
, &pa
, &len
, ret
);
229 free(pa
.padata_value
.data
);
232 e_data
->length
= len
;
237 /* function to map policy errors */
238 krb5_error_code
samba_kdc_map_policy_err(NTSTATUS nt_status
)
242 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_PASSWORD_MUST_CHANGE
))
243 ret
= KRB5KDC_ERR_KEY_EXPIRED
;
244 else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_PASSWORD_EXPIRED
))
245 ret
= KRB5KDC_ERR_KEY_EXPIRED
;
246 else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCOUNT_EXPIRED
))
247 ret
= KRB5KDC_ERR_CLIENT_REVOKED
;
248 else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCOUNT_DISABLED
))
249 ret
= KRB5KDC_ERR_CLIENT_REVOKED
;
250 else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_INVALID_LOGON_HOURS
))
251 ret
= KRB5KDC_ERR_CLIENT_REVOKED
;
252 else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCOUNT_LOCKED_OUT
))
253 ret
= KRB5KDC_ERR_CLIENT_REVOKED
;
254 else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_INVALID_WORKSTATION
))
255 ret
= KRB5KDC_ERR_POLICY
;
257 ret
= KRB5KDC_ERR_POLICY
;
262 /* Given a kdc entry, consult the account_ok routine in auth/auth_sam.c
264 NTSTATUS
samba_kdc_check_client_access(struct samba_kdc_entry
*kdc_entry
,
265 const char *client_name
,
266 const char *workstation
,
267 bool password_change
)
272 tmp_ctx
= talloc_named(NULL
, 0, "samba_kdc_check_client_access");
274 return NT_STATUS_NO_MEMORY
;
277 /* we allow all kinds of trusts here */
278 nt_status
= authsam_account_ok(tmp_ctx
,
279 kdc_entry
->kdc_db_ctx
->samdb
,
280 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT
|
281 MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
,
282 kdc_entry
->realm_dn
, kdc_entry
->msg
,
283 workstation
, client_name
,
284 true, password_change
);
286 talloc_free(tmp_ctx
);