s4:PAC Streamline pac-glue step 2
[Samba/fernandojvsilva.git] / source4 / kdc / pac-glue.c
blobeb5f284d83d05be9cbe501872e627bce84b7a37d
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 struct krb5_dh_moduli;
36 struct _krb5_krb_auth_data;
38 static krb5_error_code samba_kdc_plugin_init(krb5_context context, void **ptr)
40 *ptr = NULL;
41 return 0;
44 static void samba_kdc_plugin_fini(void *ptr)
46 return;
49 static NTSTATUS
50 get_logon_info_pac_blob(TALLOC_CTX *mem_ctx,
51 struct smb_iconv_convenience *ic,
52 struct auth_serversupplied_info *info,
53 DATA_BLOB *pac_data)
55 struct netr_SamInfo3 *info3;
56 union PAC_INFO pac_info;
57 enum ndr_err_code ndr_err;
58 NTSTATUS nt_status;
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)));
66 return nt_status;
69 pac_info.logon_info.info = talloc_zero(mem_ctx, struct PAC_LOGON_INFO);
70 if (!mem_ctx) {
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,
77 PAC_TYPE_LOGON_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)));
83 return nt_status;
86 return NT_STATUS_OK;
89 static krb5_error_code make_krb5_pac(krb5_context context,
90 DATA_BLOB *pac_blob,
91 krb5_pac *pac)
93 krb5_data pac_data;
94 krb5_error_code ret;
96 ret = krb5_data_copy(&pac_data, pac_blob->data, pac_blob->length);
97 if (ret != 0) {
98 return ret;
101 ret = krb5_pac_init(context, pac);
102 if (ret != 0) {
103 krb5_data_free(&pac_data);
104 return ret;
107 ret = krb5_pac_add_buffer(context, *pac, PAC_TYPE_LOGON_INFO, &pac_data);
108 krb5_data_free(&pac_data);
109 if (ret != 0) {
110 return ret;
113 return ret;
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) {
126 return false;
129 return true;
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;
138 DATA_BLOB *pac_blob;
139 NTSTATUS nt_status;
141 /* The user account may be set not to want the PAC */
142 if ( ! princ_needs_pac(client)) {
143 *_pac_blob = NULL;
144 return NT_STATUS_OK;
147 pac_blob = talloc_zero(mem_ctx, DATA_BLOB);
148 if (!pac_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),
155 p->realm_dn,
156 p->msg,
157 data_blob(NULL, 0),
158 data_blob(NULL, 0),
159 &server_info);
160 if (!NT_STATUS_IS_OK(nt_status)) {
161 DEBUG(0, ("Getting user info for PAC failed: %s\n",
162 nt_errstr(nt_status)));
163 return 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)));
172 return nt_status;
175 *_pac_blob = pac_blob;
176 return NT_STATUS_OK;
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;
185 krb5_error_code ret;
186 NTSTATUS nt_status;
188 ret = kerberos_pac_to_server_info(mem_ctx, ic, *pac,
189 context, &server_info);
190 if (ret) {
191 return NT_STATUS_UNSUCCESSFUL;
194 nt_status = samba_get_logon_info_pac_blob(mem_ctx, ic,
195 server_info, pac_blob);
197 return nt_status;
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,
203 krb5_pac *pac)
205 TALLOC_CTX *mem_ctx;
206 DATA_BLOB *pac_blob;
207 krb5_error_code ret;
208 NTSTATUS nt_status;
210 mem_ctx = talloc_named(client->ctx, 0, "samba_get_pac context");
211 if (!mem_ctx) {
212 return ENOMEM;
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);
218 return EINVAL;
221 ret = make_krb5_pac(context, pac_blob, pac);
223 talloc_free(mem_ctx);
224 return ret;
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");
236 DATA_BLOB *pac_blob;
237 krb5_error_code ret;
238 NTSTATUS nt_status;
240 if (!mem_ctx) {
241 return ENOMEM;
244 pac_blob = talloc_zero(mem_ctx, DATA_BLOB);
245 if (!pac_blob) {
246 talloc_free(mem_ctx);
247 return ENOMEM;
250 /* The user account may be set not to want the PAC */
251 if ( ! princ_needs_pac(server)) {
252 talloc_free(mem_ctx);
253 return EINVAL;
256 nt_status = samba_kdc_update_pac_blob(mem_ctx, context,
257 p->iconv_convenience,
258 pac, pac_blob);
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);
263 return EINVAL;
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);
272 return ret;
275 static void samba_kdc_build_edata_reply(TALLOC_CTX *tmp_ctx, krb5_data *e_data,
276 NTSTATUS nt_status)
278 PA_DATA pa;
279 unsigned char *buf;
280 size_t len;
281 krb5_error_code ret = 0;
283 if (!e_data)
284 return;
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) {
290 e_data->length = 0;
291 e_data->data = NULL;
292 return;
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);
302 e_data->data = buf;
303 e_data->length = len;
305 return;
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,
317 KDC_REQ *req,
318 krb5_data *e_data)
320 krb5_error_code ret;
321 NTSTATUS nt_status;
322 TALLOC_CTX *tmp_ctx;
323 struct hdb_samba4_private *p;
324 char *workstation = NULL;
325 HostAddresses *addresses = req->req_body.addresses;
326 int i;
327 bool password_change;
329 tmp_ctx = talloc_new(client_ex->ctx);
330 p = talloc_get_type(client_ex->ctx, struct hdb_samba4_private);
332 if (!tmp_ctx) {
333 return ENOMEM;
336 if (addresses) {
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));
340 if (workstation) {
341 break;
347 /* Strip space padding */
348 if (workstation) {
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,
359 p->samdb,
360 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
361 p->realm_dn,
362 p->msg,
363 workstation,
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);
372 } else {
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;
387 else
388 ret = KRB5KDC_ERR_POLICY;
390 samba_kdc_build_edata_reply(tmp_ctx, e_data, nt_status);
393 return ret;
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,