]> git.ipfire.org Git - thirdparty/squid.git/blob - src/peer_proxy_negotiate_auth.cc
Merged from trunk
[thirdparty/squid.git] / src / peer_proxy_negotiate_auth.cc
1 /*
2 * -----------------------------------------------------------------------------
3 *
4 * Author: Markus Moeller (markus_moeller at compuserve.com)
5 *
6 * Copyright (C) 2007 Markus Moeller. All rights reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21 *
22 * -----------------------------------------------------------------------------
23 */
24 /*
25 * Hosted at http://sourceforge.net/projects/squidkerbauth
26 */
27
28 #include "squid.h"
29
30 #if HAVE_KRB5 && HAVE_GSSAPI
31
32 #include "base64.h"
33 #include "Debug.h"
34 #include "peer_proxy_negotiate_auth.h"
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40 #if HAVE_PROFILE_H
41 #include <profile.h>
42 #endif /* HAVE_PROFILE_H */
43 #if HAVE_KRB5_H
44 #if HAVE_BROKEN_SOLARIS_KRB5_H
45 #if defined(__cplusplus)
46 #define KRB5INT_BEGIN_DECLS extern "C" {
47 #define KRB5INT_END_DECLS
48 KRB5INT_BEGIN_DECLS
49 #endif
50 #endif
51 #include <krb5.h>
52 #elif HAVE_ET_COM_ERR_H
53 #include <et/com_err.h>
54 #endif /* HAVE_COM_ERR_H */
55 #if HAVE_COM_ERR_H
56 #include <com_err.h>
57 #endif /* HAVE_COM_ERR_H */
58
59 #if HAVE_GSSAPI_GSSAPI_H
60 #include <gssapi/gssapi.h>
61 #elif HAVE_GSSAPI_H
62 #include <gssapi.h>
63 #endif /* HAVE_GSSAPI_H */
64 #if HAVE_GSSAPI_GSSAPI_EXT_H
65 #include <gssapi/gssapi_ext.h>
66 #endif /* HAVE_GSSAPI_GSSAPI_EXT_H */
67 #if HAVE_GSSAPI_GSSAPI_KRB5_H
68 #include <gssapi/gssapi_krb5.h>
69 #endif /* HAVE_GSSAPI_GSSAPI_KRB5_H */
70 #if HAVE_GSSAPI_GSSAPI_GENERIC_H
71 #include <gssapi/gssapi_generic.h>
72 #endif /* HAVE_GSSAPI_GSSAPI_GENERIC_H */
73
74 #ifndef gss_nt_service_name
75 #define gss_nt_service_name GSS_C_NT_HOSTBASED_SERVICE
76 #endif
77
78 #if !HAVE_ERROR_MESSAGE && HAVE_KRB5_GET_ERR_TEXT
79 #define error_message(code) krb5_get_err_text(kparam.context,code)
80 #elif !HAVE_ERROR_MESSAGE && HAVE_KRB5_GET_ERROR_MESSAGE
81 #define error_message(code) krb5_get_error_message(kparam.context,code)
82 #elif !HAVE_ERROR_MESSAGE
83 static char err_code[17];
84 const char *KRB5_CALLCONV
85 error_message(long code) {
86 snprintf(err_code,16,"%ld",code);
87 return err_code;
88 }
89 #endif
90
91 #ifndef gss_mech_spnego
92 static gss_OID_desc _gss_mech_spnego =
93 { 6, (void *) "\x2b\x06\x01\x05\x05\x02" };
94 gss_OID gss_mech_spnego = &_gss_mech_spnego;
95 #endif
96
97 #if HAVE_NAS_KERBEROS
98 #include <ibm_svc/krb5_svc.h>
99 const char *KRB5_CALLCONV error_message(long code) {
100 char *msg = NULL;
101 krb5_svc_get_msg(code, &msg);
102 return msg;
103 }
104 #endif
105
106 /*
107 * Kerberos context and cache structure
108 * Caches authentication details to reduce
109 * number of authentication requests to kdc
110 */
111 static struct kstruct {
112 krb5_context context;
113 krb5_ccache cc;
114 } kparam = {
115 NULL, NULL};
116
117 /*
118 * krb5_create_cache creates a Kerberos file credential cache or a memory
119 * credential cache if supported. The initial key for the principal
120 * principal_name is extracted from the keytab keytab_filename.
121 *
122 * If keytab_filename is NULL the default will be used.
123 * If principal_name is NULL the first working entry of the keytab will be used.
124 */
125 int krb5_create_cache(char *keytab_filename, char *principal_name);
126
127 /*
128 * krb5_cleanup clears used Keberos memory
129 */
130 void krb5_cleanup(void);
131
132 /*
133 * check_gss_err checks for gssapi error codes, extracts the error message
134 * and prints it.
135 */
136 int check_gss_err(OM_uint32 major_status, OM_uint32 minor_status,
137 const char *function);
138
139 int check_gss_err(OM_uint32 major_status, OM_uint32 minor_status,
140 const char *function) {
141 if (GSS_ERROR(major_status)) {
142 OM_uint32 maj_stat, min_stat;
143 OM_uint32 msg_ctx = 0;
144 gss_buffer_desc status_string;
145 char buf[1024];
146 size_t len;
147
148 len = 0;
149 msg_ctx = 0;
150 while (!msg_ctx) {
151 /* convert major status code (GSS-API error) to text */
152 maj_stat = gss_display_status(&min_stat, major_status,
153 GSS_C_GSS_CODE, GSS_C_NULL_OID, &msg_ctx, &status_string);
154 if (maj_stat == GSS_S_COMPLETE) {
155 if (sizeof(buf) > len + status_string.length + 1) {
156 memcpy(buf + len, status_string.value,
157 status_string.length);
158 len += status_string.length;
159 }
160 gss_release_buffer(&min_stat, &status_string);
161 break;
162 }
163 gss_release_buffer(&min_stat, &status_string);
164 }
165 if (sizeof(buf) > len + 2) {
166 strcpy(buf + len, ". ");
167 len += 2;
168 }
169 msg_ctx = 0;
170 while (!msg_ctx) {
171 /* convert minor status code (underlying routine error) to text */
172 maj_stat = gss_display_status(&min_stat, minor_status,
173 GSS_C_MECH_CODE, GSS_C_NULL_OID, &msg_ctx, &status_string);
174 if (maj_stat == GSS_S_COMPLETE) {
175 if (sizeof(buf) > len + status_string.length) {
176 memcpy(buf + len, status_string.value,
177 status_string.length);
178 len += status_string.length;
179 }
180 gss_release_buffer(&min_stat, &status_string);
181 break;
182 }
183 gss_release_buffer(&min_stat, &status_string);
184 }
185 debugs(11, 5, HERE << function << "failed: " << buf);
186 return (1);
187 }
188 return (0);
189 }
190
191 void krb5_cleanup() {
192 debugs(11, 5, HERE << "Cleanup kerberos context");
193 if (kparam.context) {
194 if (kparam.cc)
195 krb5_cc_destroy(kparam.context, kparam.cc);
196 kparam.cc = NULL;
197 krb5_free_context(kparam.context);
198 kparam.context = NULL;
199 }
200 }
201
202 int krb5_create_cache(char *kf, char *pn) {
203
204 #define KT_PATH_MAX 256
205 #define MAX_RENEW_TIME "365d"
206 #define DEFAULT_SKEW (krb5_deltat) 600
207
208 static char *keytab_filename = NULL, *principal_name = NULL;
209 static krb5_keytab keytab = 0;
210 static krb5_keytab_entry entry;
211 static krb5_kt_cursor cursor;
212 static krb5_creds *creds = NULL;
213 #if HAVE_HEIMDAL_KERBEROS
214 static krb5_creds creds2;
215 #endif
216 static krb5_principal principal = NULL;
217 static krb5_deltat skew;
218
219 krb5_get_init_creds_opt options;
220 krb5_error_code code = 0;
221 krb5_deltat rlife;
222 #if HAVE_PROFILE_H && HAVE_KRB5_GET_PROFILE && HAVE_PROFILE_GET_INTEGER && HAVE_PROFILE_RELEASE
223 profile_t profile;
224 #endif
225 #if HAVE_HEIMDAL_KERBEROS
226 krb5_kdc_flags flags;
227 krb5_realm *client_realm;
228 #endif
229 char *mem_cache;
230
231 restart:
232 /*
233 * Check if credentials need to be renewed
234 */
235 if (creds &&
236 (creds->times.endtime - time(0) > skew) &&
237 (creds->times.renew_till - time(0) > 2 * skew)) {
238 if (creds->times.endtime - time(0) < 2 * skew) {
239 #if !HAVE_HEIMDAL_KERBEROS
240 /* renew ticket */
241 code =
242 krb5_get_renewed_creds(kparam.context, creds, principal,
243 kparam.cc, NULL);
244 #else
245 /* renew ticket */
246 flags.i = 0;
247 flags.b.renewable = flags.b.renew = 1;
248
249 code =
250 krb5_cc_get_principal(kparam.context, kparam.cc,
251 &creds2.client);
252 if (code) {
253 debugs(11, 5,
254 HERE <<
255 "Error while getting principal from credential cache : "
256 << error_message(code));
257 return (1);
258 }
259 client_realm = krb5_princ_realm(kparam.context, creds2.client);
260 code =
261 krb5_make_principal(kparam.context, &creds2.server,
262 *client_realm, KRB5_TGS_NAME, *client_realm, NULL);
263 if (code) {
264 debugs(11, 5,
265 HERE << "Error while getting krbtgt principal : " <<
266 error_message(code));
267 return (1);
268 }
269 code =
270 krb5_get_kdc_cred(kparam.context, kparam.cc, flags, NULL,
271 NULL, &creds2, &creds);
272 krb5_free_creds(kparam.context, &creds2);
273 #endif
274 if (code) {
275 if (code == KRB5KRB_AP_ERR_TKT_EXPIRED) {
276 krb5_free_creds(kparam.context, creds);
277 creds = NULL;
278 /* this can happen because of clock skew */
279 goto restart;
280 }
281 debugs(11, 5,
282 HERE << "Error while get credentials : " <<
283 error_message(code));
284 return (1);
285 }
286 }
287 } else {
288 /* reinit */
289 if (!kparam.context) {
290 code = krb5_init_context(&kparam.context);
291 if (code) {
292 debugs(11, 5,
293 HERE << "Error while initialising Kerberos library : "
294 << error_message(code));
295 return (1);
296 }
297 }
298 #if HAVE_PROFILE_H && HAVE_KRB5_GET_PROFILE && HAVE_PROFILE_GET_INTEGER && HAVE_PROFILE_RELEASE
299 code = krb5_get_profile(kparam.context, &profile);
300 if (code) {
301 if (profile)
302 profile_release(profile);
303 debugs(11, 5,
304 HERE << "Error while getting profile : " <<
305 error_message(code));
306 return (1);
307 }
308 code =
309 profile_get_integer(profile, "libdefaults", "clockskew", 0,
310 5 * 60, &skew);
311 if (profile)
312 profile_release(profile);
313 if (code) {
314 debugs(11, 5,
315 HERE << "Error while getting clockskew : " <<
316 error_message(code));
317 return (1);
318 }
319 #elif HAVE_KRB5_GET_MAX_TIME_SKEW && HAVE_HEIMDAL_KERBEROS
320 skew = krb5_get_max_time_skew(kparam.context);
321 #elif HAVE_MAX_SKEW_IN_KRB5_CONTEXT && HAVE_HEIMDAL_KERBEROS
322 skew = kparam.context->max_skew;
323 #else
324 skew = DEFAULT_SKEW;
325 #endif
326
327 if (!kf) {
328 char buf[KT_PATH_MAX], *p;
329
330 krb5_kt_default_name(kparam.context, buf, KT_PATH_MAX);
331 p = strchr(buf, ':');
332 if (p)
333 ++p;
334 if (keytab_filename)
335 xfree(keytab_filename);
336 keytab_filename = xstrdup(p ? p : buf);
337 } else {
338 keytab_filename = xstrdup(kf);
339 }
340
341 code = krb5_kt_resolve(kparam.context, keytab_filename, &keytab);
342 if (code) {
343 debugs(11, 5,
344 HERE << "Error while resolving keytab filename " <<
345 keytab_filename << " : " << error_message(code));
346 return (1);
347 }
348
349 if (!pn) {
350 code = krb5_kt_start_seq_get(kparam.context, keytab, &cursor);
351 if (code) {
352 debugs(11, 5,
353 HERE << "Error while starting keytab scan : " <<
354 error_message(code));
355 return (1);
356 }
357 code =
358 krb5_kt_next_entry(kparam.context, keytab, &entry, &cursor);
359 krb5_copy_principal(kparam.context, entry.principal,
360 &principal);
361 if (code && code != KRB5_KT_END) {
362 debugs(11, 5,
363 HERE << "Error while scanning keytab : " <<
364 error_message(code));
365 return (1);
366 }
367
368 code = krb5_kt_end_seq_get(kparam.context, keytab, &cursor);
369 if (code) {
370 debugs(11, 5,
371 HERE << "Error while ending keytab scan : " <<
372 error_message(code));
373 return (1);
374 }
375 #if HAVE_HEIMDAL_KERBEROS || ( HAVE_KRB5_KT_FREE_ENTRY && HAVE_DECL_KRB5_KT_FREE_ENTRY)
376 code = krb5_kt_free_entry(kparam.context, &entry);
377 #else
378 code = krb5_free_keytab_entry_contents(kparam.context, &entry);
379 #endif
380 if (code) {
381 debugs(11, 5,
382 HERE << "Error while freeing keytab entry : " <<
383 error_message(code));
384 return (1);
385 }
386
387 } else {
388 principal_name = xstrdup(pn);
389 }
390
391 if (!principal) {
392 code =
393 krb5_parse_name(kparam.context, principal_name, &principal);
394 if (code) {
395 debugs(11, 5,
396 HERE << "Error while parsing principal name " <<
397 principal_name << " : " << error_message(code));
398 return (1);
399 }
400 }
401
402 creds = (krb5_creds *) xmalloc(sizeof(*creds));
403 memset(creds, 0, sizeof(*creds));
404 krb5_get_init_creds_opt_init(&options);
405 code = krb5_string_to_deltat((char *) MAX_RENEW_TIME, &rlife);
406 if (code != 0 || rlife == 0) {
407 debugs(11, 5,
408 HERE << "Error bad lifetime value " << MAX_RENEW_TIME <<
409 " : " << error_message(code));
410 return (1);
411 }
412 krb5_get_init_creds_opt_set_renew_life(&options, rlife);
413
414 code =
415 krb5_get_init_creds_keytab(kparam.context, creds, principal,
416 keytab, 0, NULL, &options);
417 if (code) {
418 debugs(11, 5,
419 HERE <<
420 "Error while initializing credentials from keytab : " <<
421 error_message(code));
422 return (1);
423 }
424 #if !HAVE_KRB5_MEMORY_CACHE
425 mem_cache =
426 (char *) xmalloc(strlen("FILE:/tmp/peer_proxy_negotiate_auth_")
427 + 16);
428 snprintf(mem_cache,
429 strlen("FILE:/tmp/peer_proxy_negotiate_auth_") + 16,
430 "FILE:/tmp/peer_proxy_negotiate_auth_%d", (int) getpid());
431 #else
432 mem_cache =
433 (char *) xmalloc(strlen("MEMORY:peer_proxy_negotiate_auth_") +
434 16);
435 snprintf(mem_cache,
436 strlen("MEMORY:peer_proxy_negotiate_auth_") + 16,
437 "MEMORY:peer_proxy_negotiate_auth_%d", (int) getpid());
438 #endif
439
440 setenv("KRB5CCNAME", mem_cache, 1);
441 code = krb5_cc_resolve(kparam.context, mem_cache, &kparam.cc);
442 if (mem_cache)
443 xfree(mem_cache);
444 if (code) {
445 debugs(11, 5,
446 HERE << "Error while resolving memory credential cache : "
447 << error_message(code));
448 return (1);
449 }
450 code = krb5_cc_initialize(kparam.context, kparam.cc, principal);
451 if (code) {
452 debugs(11, 5,
453 HERE <<
454 "Error while initializing memory credential cache : " <<
455 error_message(code));
456 return (1);
457 }
458 code = krb5_cc_store_cred(kparam.context, kparam.cc, creds);
459 if (code) {
460 debugs(11, 5,
461 HERE << "Error while storing credentials : " <<
462 error_message(code));
463 return (1);
464 }
465
466 if (!creds->times.starttime)
467 creds->times.starttime = creds->times.authtime;
468 }
469 return (0);
470 }
471
472 /*
473 * peer_proxy_negotiate_auth gets a GSSAPI token for principal_name
474 * and base64 encodes it.
475 */
476 char *peer_proxy_negotiate_auth(char *principal_name, char *proxy) {
477 int rc = 0;
478 OM_uint32 major_status, minor_status;
479 gss_ctx_id_t gss_context = GSS_C_NO_CONTEXT;
480 gss_name_t server_name = GSS_C_NO_NAME;
481 gss_buffer_desc service = GSS_C_EMPTY_BUFFER;
482 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
483 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
484 char *token = NULL;
485
486 setbuf(stdout, NULL);
487 setbuf(stdin, NULL);
488
489 if (!proxy) {
490 debugs(11, 5, HERE << "Error : No proxy server name");
491 return NULL;
492 }
493
494 if (principal_name)
495 debugs(11, 5,
496 HERE << "Creating credential cache for " << principal_name);
497 else
498 debugs(11, 5, HERE << "Creating credential cache");
499 rc = krb5_create_cache(NULL, principal_name);
500 if (rc) {
501 debugs(11, 5, HERE << "Error : Failed to create Kerberos cache");
502 krb5_cleanup();
503 return NULL;
504 }
505
506 service.value = (void *) xmalloc(strlen("HTTP") + strlen(proxy) + 2);
507 snprintf((char *) service.value, strlen("HTTP") + strlen(proxy) + 2,
508 "%s@%s", "HTTP", proxy);
509 service.length = strlen((char *) service.value);
510
511 debugs(11, 5, HERE << "Import gss name");
512 major_status = gss_import_name(&minor_status, &service,
513 gss_nt_service_name, &server_name);
514
515 if (check_gss_err(major_status, minor_status, "gss_import_name()"))
516 goto cleanup;
517
518 debugs(11, 5, HERE << "Initialize gss security context");
519 major_status = gss_init_sec_context(&minor_status,
520 GSS_C_NO_CREDENTIAL,
521 &gss_context,
522 server_name,
523 gss_mech_spnego,
524 0,
525 0,
526 GSS_C_NO_CHANNEL_BINDINGS,
527 &input_token, NULL, &output_token, NULL, NULL);
528
529 if (check_gss_err(major_status, minor_status, "gss_init_sec_context()"))
530 goto cleanup;
531
532 debugs(11, 5, HERE << "Got token with length " << output_token.length);
533 if (output_token.length) {
534
535 token =
536 (char *) base64_encode_bin((const char *) output_token.value,
537 output_token.length);
538 }
539
540 cleanup:
541 gss_delete_sec_context(&minor_status, &gss_context, NULL);
542 gss_release_buffer(&minor_status, &service);
543 gss_release_buffer(&minor_status, &input_token);
544 gss_release_buffer(&minor_status, &output_token);
545 gss_release_name(&minor_status, &server_name);
546
547 return token;
548 }
549
550 #ifdef __cplusplus
551 }
552 #endif
553 #endif /* HAVE_KRB5 && HAVE_GSSAPI */