s4:winreg RPC - don't crash when incoming data wasn't correctly specified
[Samba/nascimento.git] / source4 / kdc / pac-glue.c
blob97f741653728b6edb9ed005d337878d2c2de1937
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 "../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"
32 #include "kdc/kdc.h"
33 #include "param/param.h"
35 static
36 NTSTATUS samba_get_logon_info_pac_blob(TALLOC_CTX *mem_ctx,
37 struct smb_iconv_convenience *ic,
38 struct auth_serversupplied_info *info,
39 DATA_BLOB *pac_data)
41 struct netr_SamInfo3 *info3;
42 union PAC_INFO pac_info;
43 enum ndr_err_code ndr_err;
44 NTSTATUS nt_status;
46 ZERO_STRUCT(pac_info);
48 nt_status = auth_convert_server_info_saminfo3(mem_ctx, info, &info3);
49 if (!NT_STATUS_IS_OK(nt_status)) {
50 DEBUG(1, ("Getting Samba info failed: %s\n",
51 nt_errstr(nt_status)));
52 return nt_status;
55 pac_info.logon_info.info = talloc_zero(mem_ctx, struct PAC_LOGON_INFO);
56 if (!mem_ctx) {
57 return NT_STATUS_NO_MEMORY;
60 pac_info.logon_info.info->info3 = *info3;
62 ndr_err = ndr_push_union_blob(pac_data, mem_ctx, ic, &pac_info,
63 PAC_TYPE_LOGON_INFO,
64 (ndr_push_flags_fn_t)ndr_push_PAC_INFO);
65 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
66 nt_status = ndr_map_error2ntstatus(ndr_err);
67 DEBUG(1, ("PAC (presig) push failed: %s\n",
68 nt_errstr(nt_status)));
69 return nt_status;
72 return NT_STATUS_OK;
75 krb5_error_code samba_make_krb5_pac(krb5_context context,
76 DATA_BLOB *pac_blob,
77 krb5_pac *pac)
79 krb5_data pac_data;
80 krb5_error_code ret;
82 ret = krb5_data_copy(&pac_data, pac_blob->data, pac_blob->length);
83 if (ret != 0) {
84 return ret;
87 ret = krb5_pac_init(context, pac);
88 if (ret != 0) {
89 krb5_data_free(&pac_data);
90 return ret;
93 ret = krb5_pac_add_buffer(context, *pac, PAC_TYPE_LOGON_INFO, &pac_data);
94 krb5_data_free(&pac_data);
95 if (ret != 0) {
96 return ret;
99 return ret;
102 bool samba_princ_needs_pac(struct hdb_entry_ex *princ)
105 struct samba_kdc_entry *p = talloc_get_type(princ->ctx, struct samba_kdc_entry);
106 unsigned int userAccountControl;
109 /* The service account may be set not to want the PAC */
110 userAccountControl = ldb_msg_find_attr_as_uint(p->msg, "userAccountControl", 0);
111 if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
112 return false;
115 return true;
118 NTSTATUS samba_kdc_get_pac_blob(TALLOC_CTX *mem_ctx,
119 struct hdb_entry_ex *client,
120 DATA_BLOB **_pac_blob)
122 struct samba_kdc_entry *p = talloc_get_type(client->ctx, struct samba_kdc_entry);
123 struct auth_serversupplied_info *server_info;
124 DATA_BLOB *pac_blob;
125 NTSTATUS nt_status;
127 /* The user account may be set not to want the PAC */
128 if ( ! samba_princ_needs_pac(client)) {
129 *_pac_blob = NULL;
130 return NT_STATUS_OK;
133 pac_blob = talloc_zero(mem_ctx, DATA_BLOB);
134 if (!pac_blob) {
135 return NT_STATUS_NO_MEMORY;
138 nt_status = authsam_make_server_info(mem_ctx, p->kdc_db_ctx->samdb,
139 lp_netbios_name(p->kdc_db_ctx->lp_ctx),
140 lp_sam_name(p->kdc_db_ctx->lp_ctx),
141 p->realm_dn,
142 p->msg,
143 data_blob(NULL, 0),
144 data_blob(NULL, 0),
145 &server_info);
146 if (!NT_STATUS_IS_OK(nt_status)) {
147 DEBUG(0, ("Getting user info for PAC failed: %s\n",
148 nt_errstr(nt_status)));
149 return nt_status;
152 nt_status = samba_get_logon_info_pac_blob(mem_ctx,
153 p->kdc_db_ctx->ic_ctx,
154 server_info, pac_blob);
155 if (!NT_STATUS_IS_OK(nt_status)) {
156 DEBUG(0, ("Building PAC failed: %s\n",
157 nt_errstr(nt_status)));
158 return nt_status;
161 *_pac_blob = pac_blob;
162 return NT_STATUS_OK;
165 NTSTATUS samba_kdc_update_pac_blob(TALLOC_CTX *mem_ctx,
166 krb5_context context,
167 struct smb_iconv_convenience *ic,
168 krb5_pac *pac, DATA_BLOB *pac_blob)
170 struct auth_serversupplied_info *server_info;
171 krb5_error_code ret;
172 NTSTATUS nt_status;
174 ret = kerberos_pac_to_server_info(mem_ctx, ic, *pac,
175 context, &server_info);
176 if (ret) {
177 return NT_STATUS_UNSUCCESSFUL;
180 nt_status = samba_get_logon_info_pac_blob(mem_ctx, ic,
181 server_info, pac_blob);
183 return nt_status;
186 /* this function allocates 'data' using malloc.
187 * The caller is responsible for freeing it */
188 void samba_kdc_build_edata_reply(NTSTATUS nt_status, DATA_BLOB *e_data)
190 PA_DATA pa;
191 unsigned char *buf;
192 size_t len;
193 krb5_error_code ret = 0;
195 if (!e_data)
196 return;
198 pa.padata_type = KRB5_PADATA_PW_SALT;
199 pa.padata_value.length = 12;
200 pa.padata_value.data = malloc(pa.padata_value.length);
201 if (!pa.padata_value.data) {
202 e_data->length = 0;
203 e_data->data = NULL;
204 return;
207 SIVAL(pa.padata_value.data, 0, NT_STATUS_V(nt_status));
208 SIVAL(pa.padata_value.data, 4, 0);
209 SIVAL(pa.padata_value.data, 8, 1);
211 ASN1_MALLOC_ENCODE(PA_DATA, buf, len, &pa, &len, ret);
212 free(pa.padata_value.data);
214 e_data->data = buf;
215 e_data->length = len;
217 return;
220 /* function to map policy errors */
221 krb5_error_code samba_kdc_map_policy_err(NTSTATUS nt_status)
223 krb5_error_code ret;
225 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_MUST_CHANGE))
226 ret = KRB5KDC_ERR_KEY_EXPIRED;
227 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_EXPIRED))
228 ret = KRB5KDC_ERR_KEY_EXPIRED;
229 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_EXPIRED))
230 ret = KRB5KDC_ERR_CLIENT_REVOKED;
231 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_DISABLED))
232 ret = KRB5KDC_ERR_CLIENT_REVOKED;
233 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_LOGON_HOURS))
234 ret = KRB5KDC_ERR_CLIENT_REVOKED;
235 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_LOCKED_OUT))
236 ret = KRB5KDC_ERR_CLIENT_REVOKED;
237 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_WORKSTATION))
238 ret = KRB5KDC_ERR_POLICY;
239 else
240 ret = KRB5KDC_ERR_POLICY;
242 return ret;
245 /* Given a kdc entry, consult the account_ok routine in auth/auth_sam.c
246 * for consistency */
247 NTSTATUS samba_kdc_check_client_access(struct samba_kdc_entry *kdc_entry,
248 const char *client_name,
249 const char *workstation,
250 bool password_change)
252 TALLOC_CTX *tmp_ctx;
253 NTSTATUS nt_status;
255 tmp_ctx = talloc_named(NULL, 0, "samba_kdc_check_client_access");
256 if (!tmp_ctx) {
257 return NT_STATUS_NO_MEMORY;
260 /* we allow all kinds of trusts here */
261 nt_status = authsam_account_ok(tmp_ctx,
262 kdc_entry->kdc_db_ctx->samdb,
263 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
264 MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
265 kdc_entry->realm_dn, kdc_entry->msg,
266 workstation, client_name,
267 true, password_change);
269 talloc_free(tmp_ctx);
270 return nt_status;