s3: Add sock_packet_read
[Samba/vl.git] / source3 / libads / authdata.c
blob0d877ddef89ad62284a8e96223344277d0fd6f38
1 /*
2 Unix SMB/CIFS implementation.
3 kerberos authorization data (PAC) utility library
4 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
6 Copyright (C) Andrew Tridgell 2001
7 Copyright (C) Luke Howard 2002-2003
8 Copyright (C) Stefan Metzmacher 2004-2005
9 Copyright (C) Guenther Deschner 2005,2007,2008
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
26 #include "librpc/gen_ndr/ndr_krb5pac.h"
27 #include "smb_krb5.h"
28 #include "libads/kerberos_proto.h"
30 #ifdef HAVE_KRB5
32 /****************************************************************
33 ****************************************************************/
35 static krb5_error_code check_pac_checksum(TALLOC_CTX *mem_ctx,
36 DATA_BLOB pac_data,
37 struct PAC_SIGNATURE_DATA *sig,
38 krb5_context context,
39 krb5_keyblock *keyblock)
41 krb5_error_code ret;
42 krb5_checksum cksum;
43 krb5_keyusage usage = 0;
45 smb_krb5_checksum_from_pac_sig(&cksum, sig);
47 #ifdef HAVE_KRB5_KU_OTHER_CKSUM /* Heimdal */
48 usage = KRB5_KU_OTHER_CKSUM;
49 #elif defined(HAVE_KRB5_KEYUSAGE_APP_DATA_CKSUM) /* MIT */
50 usage = KRB5_KEYUSAGE_APP_DATA_CKSUM;
51 #else
52 #error UNKNOWN_KRB5_KEYUSAGE
53 #endif
55 ret = smb_krb5_verify_checksum(context,
56 keyblock,
57 usage,
58 &cksum,
59 pac_data.data,
60 pac_data.length);
62 if (ret) {
63 DEBUG(2,("check_pac_checksum: PAC Verification failed: %s (%d)\n",
64 error_message(ret), ret));
65 return ret;
68 return ret;
71 /**
72 * @brief Decode a blob containing a NDR envoded PAC structure
74 * @param mem_ctx - The memory context
75 * @param pac_data_blob - The data blob containing the NDR encoded data
76 * @param context - The Kerberos Context
77 * @param service_keyblock - The Service Key used to verify the checksum
78 * @param client_principal - The client principal
79 * @param tgs_authtime - The ticket timestamp
80 * @param pac_data_out - [out] The decoded PAC
82 * @return - A NTSTATUS error code
84 NTSTATUS decode_pac_data(TALLOC_CTX *mem_ctx,
85 DATA_BLOB *pac_data_blob,
86 krb5_context context,
87 krb5_keyblock *service_keyblock,
88 krb5_const_principal client_principal,
89 time_t tgs_authtime,
90 struct PAC_DATA **pac_data_out)
92 NTSTATUS status;
93 enum ndr_err_code ndr_err;
94 krb5_error_code ret;
95 DATA_BLOB modified_pac_blob;
97 NTTIME tgs_authtime_nttime;
98 krb5_principal client_principal_pac = NULL;
99 int i;
101 struct PAC_SIGNATURE_DATA *srv_sig_ptr = NULL;
102 struct PAC_SIGNATURE_DATA *kdc_sig_ptr = NULL;
103 struct PAC_SIGNATURE_DATA *srv_sig_wipe = NULL;
104 struct PAC_SIGNATURE_DATA *kdc_sig_wipe = NULL;
105 struct PAC_LOGON_NAME *logon_name = NULL;
106 struct PAC_LOGON_INFO *logon_info = NULL;
107 struct PAC_DATA *pac_data = NULL;
108 struct PAC_DATA_RAW *pac_data_raw = NULL;
110 DATA_BLOB *srv_sig_blob = NULL;
111 DATA_BLOB *kdc_sig_blob = NULL;
113 bool bool_ret;
115 *pac_data_out = NULL;
117 pac_data = TALLOC_ZERO_P(mem_ctx, struct PAC_DATA);
118 pac_data_raw = TALLOC_ZERO_P(mem_ctx, struct PAC_DATA_RAW);
119 kdc_sig_wipe = TALLOC_ZERO_P(mem_ctx, struct PAC_SIGNATURE_DATA);
120 srv_sig_wipe = TALLOC_ZERO_P(mem_ctx, struct PAC_SIGNATURE_DATA);
121 if (!pac_data_raw || !pac_data || !kdc_sig_wipe || !srv_sig_wipe) {
122 return NT_STATUS_NO_MEMORY;
125 ndr_err = ndr_pull_struct_blob(pac_data_blob, pac_data, pac_data,
126 (ndr_pull_flags_fn_t)ndr_pull_PAC_DATA);
127 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
128 status = ndr_map_error2ntstatus(ndr_err);
129 DEBUG(0,("can't parse the PAC: %s\n",
130 nt_errstr(status)));
131 return status;
134 if (pac_data->num_buffers < 4) {
135 /* we need logon_ingo, service_key and kdc_key */
136 DEBUG(0,("less than 4 PAC buffers\n"));
137 return NT_STATUS_INVALID_PARAMETER;
140 ndr_err = ndr_pull_struct_blob(
141 pac_data_blob, pac_data_raw, pac_data_raw,
142 (ndr_pull_flags_fn_t)ndr_pull_PAC_DATA_RAW);
143 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
144 status = ndr_map_error2ntstatus(ndr_err);
145 DEBUG(0,("can't parse the PAC: %s\n",
146 nt_errstr(status)));
147 return status;
150 if (pac_data_raw->num_buffers < 4) {
151 /* we need logon_ingo, service_key and kdc_key */
152 DEBUG(0,("less than 4 PAC buffers\n"));
153 return NT_STATUS_INVALID_PARAMETER;
156 if (pac_data->num_buffers != pac_data_raw->num_buffers) {
157 /* we need logon_ingo, service_key and kdc_key */
158 DEBUG(0, ("misparse! PAC_DATA has %d buffers while "
159 "PAC_DATA_RAW has %d\n", pac_data->num_buffers,
160 pac_data_raw->num_buffers));
161 return NT_STATUS_INVALID_PARAMETER;
164 for (i=0; i < pac_data->num_buffers; i++) {
165 struct PAC_BUFFER *data_buf = &pac_data->buffers[i];
166 struct PAC_BUFFER_RAW *raw_buf = &pac_data_raw->buffers[i];
168 if (data_buf->type != raw_buf->type) {
169 DEBUG(0, ("misparse! PAC_DATA buffer %d has type "
170 "%d while PAC_DATA_RAW has %d\n", i,
171 data_buf->type, raw_buf->type));
172 return NT_STATUS_INVALID_PARAMETER;
174 switch (data_buf->type) {
175 case PAC_TYPE_LOGON_INFO:
176 if (!data_buf->info) {
177 break;
179 logon_info = data_buf->info->logon_info.info;
180 break;
181 case PAC_TYPE_SRV_CHECKSUM:
182 if (!data_buf->info) {
183 break;
185 srv_sig_ptr = &data_buf->info->srv_cksum;
186 srv_sig_blob = &raw_buf->info->remaining;
187 break;
188 case PAC_TYPE_KDC_CHECKSUM:
189 if (!data_buf->info) {
190 break;
192 kdc_sig_ptr = &data_buf->info->kdc_cksum;
193 kdc_sig_blob = &raw_buf->info->remaining;
194 break;
195 case PAC_TYPE_LOGON_NAME:
196 logon_name = &data_buf->info->logon_name;
197 break;
198 default:
199 break;
203 if (!logon_info) {
204 DEBUG(0,("PAC no logon_info\n"));
205 return NT_STATUS_INVALID_PARAMETER;
208 if (!logon_name) {
209 DEBUG(0,("PAC no logon_name\n"));
210 return NT_STATUS_INVALID_PARAMETER;
213 if (!srv_sig_ptr || !srv_sig_blob) {
214 DEBUG(0,("PAC no srv_key\n"));
215 return NT_STATUS_INVALID_PARAMETER;
218 if (!kdc_sig_ptr || !kdc_sig_blob) {
219 DEBUG(0,("PAC no kdc_key\n"));
220 return NT_STATUS_INVALID_PARAMETER;
223 /* Find and zero out the signatures,
224 * as required by the signing algorithm */
226 /* We find the data blobs above,
227 * now we parse them to get at the exact portion we should zero */
228 ndr_err = ndr_pull_struct_blob(
229 kdc_sig_blob, kdc_sig_wipe, kdc_sig_wipe,
230 (ndr_pull_flags_fn_t)ndr_pull_PAC_SIGNATURE_DATA);
231 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
232 status = ndr_map_error2ntstatus(ndr_err);
233 DEBUG(0,("can't parse the KDC signature: %s\n",
234 nt_errstr(status)));
235 return status;
238 ndr_err = ndr_pull_struct_blob(
239 srv_sig_blob, srv_sig_wipe, srv_sig_wipe,
240 (ndr_pull_flags_fn_t)ndr_pull_PAC_SIGNATURE_DATA);
241 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
242 status = ndr_map_error2ntstatus(ndr_err);
243 DEBUG(0,("can't parse the SRV signature: %s\n",
244 nt_errstr(status)));
245 return status;
248 /* Now zero the decoded structure */
249 memset(kdc_sig_wipe->signature.data,
250 '\0', kdc_sig_wipe->signature.length);
251 memset(srv_sig_wipe->signature.data,
252 '\0', srv_sig_wipe->signature.length);
254 /* and reencode, back into the same place it came from */
255 ndr_err = ndr_push_struct_blob(
256 kdc_sig_blob, pac_data_raw, kdc_sig_wipe,
257 (ndr_push_flags_fn_t)ndr_push_PAC_SIGNATURE_DATA);
258 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
259 status = ndr_map_error2ntstatus(ndr_err);
260 DEBUG(0,("can't repack the KDC signature: %s\n",
261 nt_errstr(status)));
262 return status;
264 ndr_err = ndr_push_struct_blob(
265 srv_sig_blob, pac_data_raw, srv_sig_wipe,
266 (ndr_push_flags_fn_t)ndr_push_PAC_SIGNATURE_DATA);
267 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
268 status = ndr_map_error2ntstatus(ndr_err);
269 DEBUG(0,("can't repack the SRV signature: %s\n",
270 nt_errstr(status)));
271 return status;
274 /* push out the whole structure, but now with zero'ed signatures */
275 ndr_err = ndr_push_struct_blob(
276 &modified_pac_blob, pac_data_raw, pac_data_raw,
277 (ndr_push_flags_fn_t)ndr_push_PAC_DATA_RAW);
278 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
279 status = ndr_map_error2ntstatus(ndr_err);
280 DEBUG(0,("can't repack the RAW PAC: %s\n",
281 nt_errstr(status)));
282 return status;
285 /* verify by service_key */
286 ret = check_pac_checksum(mem_ctx,
287 modified_pac_blob, srv_sig_ptr,
288 context,
289 service_keyblock);
290 if (ret) {
291 DEBUG(1, ("PAC Decode: Failed to verify the service "
292 "signature: %s\n", error_message(ret)));
293 return NT_STATUS_ACCESS_DENIED;
296 /* Convert to NT time, so as not to loose accuracy in comparison */
297 unix_to_nt_time(&tgs_authtime_nttime, tgs_authtime);
299 if (tgs_authtime_nttime != logon_name->logon_time) {
300 DEBUG(2, ("PAC Decode: "
301 "Logon time mismatch between ticket and PAC!\n"));
302 DEBUG(2, ("PAC Decode: PAC: %s\n",
303 nt_time_string(mem_ctx, logon_name->logon_time)));
304 DEBUG(2, ("PAC Decode: Ticket: %s\n",
305 nt_time_string(mem_ctx, tgs_authtime_nttime)));
306 return NT_STATUS_ACCESS_DENIED;
309 ret = smb_krb5_parse_name_norealm(context,
310 logon_name->account_name,
311 &client_principal_pac);
312 if (ret) {
313 DEBUG(2, ("Could not parse name from PAC: [%s]:%s\n",
314 logon_name->account_name, error_message(ret)));
315 return NT_STATUS_INVALID_PARAMETER;
318 bool_ret = smb_krb5_principal_compare_any_realm(context,
319 client_principal,
320 client_principal_pac);
322 krb5_free_principal(context, client_principal_pac);
324 if (!bool_ret) {
325 DEBUG(2, ("Name in PAC [%s] does not match principal name "
326 "in ticket\n", logon_name->account_name));
327 return NT_STATUS_ACCESS_DENIED;
330 DEBUG(3,("Found account name from PAC: %s [%s]\n",
331 logon_info->info3.base.account_name.string,
332 logon_info->info3.base.full_name.string));
334 DEBUG(10,("Successfully validated Kerberos PAC\n"));
336 if (DEBUGLEVEL >= 10) {
337 const char *s;
338 s = NDR_PRINT_STRUCT_STRING(mem_ctx, PAC_DATA, pac_data);
339 if (s) {
340 DEBUGADD(10,("%s\n", s));
344 *pac_data_out = pac_data;
346 return NT_STATUS_OK;
349 /****************************************************************
350 Given a username, password and other details, return the
351 PAC_LOGON_INFO (the structure containing the important user
352 information such as groups).
353 ****************************************************************/
355 NTSTATUS kerberos_return_pac(TALLOC_CTX *mem_ctx,
356 const char *name,
357 const char *pass,
358 time_t time_offset,
359 time_t *expire_time,
360 time_t *renew_till_time,
361 const char *cache_name,
362 bool request_pac,
363 bool add_netbios_addr,
364 time_t renewable_time,
365 const char *impersonate_princ_s,
366 struct PAC_LOGON_INFO **logon_info)
368 krb5_error_code ret;
369 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
370 DATA_BLOB tkt, ap_rep, sesskey1, sesskey2;
371 char *client_princ_out = NULL;
372 const char *auth_princ = NULL;
373 const char *local_service = NULL;
374 const char *cc = "MEMORY:kerberos_return_pac";
376 ZERO_STRUCT(tkt);
377 ZERO_STRUCT(ap_rep);
378 ZERO_STRUCT(sesskey1);
379 ZERO_STRUCT(sesskey2);
381 if (!name || !pass) {
382 return NT_STATUS_INVALID_PARAMETER;
385 if (cache_name) {
386 cc = cache_name;
389 if (!strchr_m(name, '@')) {
390 auth_princ = talloc_asprintf(mem_ctx, "%s@%s", name,
391 lp_realm());
392 } else {
393 auth_princ = name;
395 NT_STATUS_HAVE_NO_MEMORY(auth_princ);
397 local_service = talloc_asprintf(mem_ctx, "%s$@%s",
398 global_myname(), lp_realm());
399 NT_STATUS_HAVE_NO_MEMORY(local_service);
401 ret = kerberos_kinit_password_ext(auth_princ,
402 pass,
403 time_offset,
404 expire_time,
405 renew_till_time,
407 request_pac,
408 add_netbios_addr,
409 renewable_time,
410 &status);
411 if (ret) {
412 DEBUG(1,("kinit failed for '%s' with: %s (%d)\n",
413 auth_princ, error_message(ret), ret));
414 /* status already set */
415 goto out;
418 DEBUG(10,("got TGT for %s in %s\n", auth_princ, cc));
419 if (expire_time) {
420 DEBUGADD(10,("\tvalid until: %s (%d)\n",
421 http_timestring(talloc_tos(), *expire_time),
422 (int)*expire_time));
424 if (renew_till_time) {
425 DEBUGADD(10,("\trenewable till: %s (%d)\n",
426 http_timestring(talloc_tos(), *renew_till_time),
427 (int)*renew_till_time));
430 /* we cannot continue with krb5 when UF_DONT_REQUIRE_PREAUTH is set,
431 * in that case fallback to NTLM - gd */
433 if (expire_time && renew_till_time &&
434 (*expire_time == 0) && (*renew_till_time == 0)) {
435 return NT_STATUS_INVALID_LOGON_TYPE;
438 ret = cli_krb5_get_ticket(mem_ctx,
439 local_service,
440 time_offset,
441 &tkt,
442 &sesskey1,
445 NULL,
446 impersonate_princ_s);
447 if (ret) {
448 DEBUG(1,("failed to get ticket for %s: %s\n",
449 local_service, error_message(ret)));
450 if (impersonate_princ_s) {
451 DEBUGADD(1,("tried S4U2SELF impersonation as: %s\n",
452 impersonate_princ_s));
454 status = krb5_to_nt_status(ret);
455 goto out;
457 status = ads_verify_ticket(mem_ctx,
458 lp_realm(),
459 time_offset,
460 &tkt,
461 &client_princ_out,
462 logon_info,
463 &ap_rep,
464 &sesskey2,
465 False);
466 if (!NT_STATUS_IS_OK(status)) {
467 DEBUG(1,("ads_verify_ticket failed: %s\n",
468 nt_errstr(status)));
469 goto out;
472 if (!*logon_info) {
473 DEBUG(1,("no PAC\n"));
474 status = NT_STATUS_INVALID_PARAMETER;
475 goto out;
478 out:
479 if (cc != cache_name) {
480 ads_kdestroy(cc);
483 data_blob_free(&tkt);
484 data_blob_free(&ap_rep);
485 data_blob_free(&sesskey1);
486 data_blob_free(&sesskey2);
488 TALLOC_FREE(client_princ_out);
490 return status;
493 #endif