2 * -----------------------------------------------------------------------------
4 * Author: Markus Moeller (markus_moeller at compuserve.com)
6 * Copyright (C) 2007 Markus Moeller. All rights reserved.
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.
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.
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.
22 * -----------------------------------------------------------------------------
25 * Hosted at http://sourceforge.net/projects/squidkerbauth
30 #if HAVE_KRB5 && HAVE_GSSAPI
34 #include "peer_proxy_negotiate_auth.h"
42 #endif /* HAVE_PROFILE_H */
44 #if HAVE_BROKEN_SOLARIS_KRB5_H
45 #if defined(__cplusplus)
46 #define KRB5INT_BEGIN_DECLS extern "C" {
47 #define KRB5INT_END_DECLS
52 #elif HAVE_ET_COM_ERR_H
53 #include <et/com_err.h>
54 #endif /* HAVE_COM_ERR_H */
57 #endif /* HAVE_COM_ERR_H */
59 #if HAVE_GSSAPI_GSSAPI_H
60 #include <gssapi/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 */
74 #ifndef gss_nt_service_name
75 #define gss_nt_service_name GSS_C_NT_HOSTBASED_SERVICE
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
);
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
;
98 #include <ibm_svc/krb5_svc.h>
99 const char *KRB5_CALLCONV
error_message(long code
) {
101 krb5_svc_get_msg(code
, &msg
);
107 * Kerberos context and cache structure
108 * Caches authentication details to reduce
109 * number of authentication requests to kdc
111 static struct kstruct
{
112 krb5_context context
;
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.
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.
125 int krb5_create_cache(char *keytab_filename
, char *principal_name
);
128 * krb5_cleanup clears used Keberos memory
130 void krb5_cleanup(void);
133 * check_gss_err checks for gssapi error codes, extracts the error message
136 int check_gss_err(OM_uint32 major_status
, OM_uint32 minor_status
,
137 const char *function
);
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
;
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
;
160 gss_release_buffer(&min_stat
, &status_string
);
163 gss_release_buffer(&min_stat
, &status_string
);
165 if (sizeof(buf
) > len
+ 2) {
166 strcpy(buf
+ len
, ". ");
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
;
180 gss_release_buffer(&min_stat
, &status_string
);
183 gss_release_buffer(&min_stat
, &status_string
);
185 debugs(11, 5, HERE
<< function
<< "failed: " << buf
);
191 void krb5_cleanup() {
192 debugs(11, 5, HERE
<< "Cleanup kerberos context");
193 if (kparam
.context
) {
195 krb5_cc_destroy(kparam
.context
, kparam
.cc
);
197 krb5_free_context(kparam
.context
);
198 kparam
.context
= NULL
;
202 int krb5_create_cache(char *kf
, char *pn
) {
204 #define KT_PATH_MAX 256
205 #define MAX_RENEW_TIME "365d"
206 #define DEFAULT_SKEW (krb5_deltat) 600
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
;
216 static krb5_principal principal
= NULL
;
217 static krb5_deltat skew
;
219 krb5_get_init_creds_opt options
;
220 krb5_error_code code
= 0;
222 #if HAVE_PROFILE_H && HAVE_KRB5_GET_PROFILE && HAVE_PROFILE_GET_INTEGER && HAVE_PROFILE_RELEASE
225 #if HAVE_HEIMDAL_KERBEROS
226 krb5_kdc_flags flags
;
227 krb5_realm
*client_realm
;
233 * Check if credentials need to be renewed
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
242 krb5_get_renewed_creds(kparam
.context
, creds
, principal
,
247 flags
.b
.renewable
= flags
.b
.renew
= 1;
250 krb5_cc_get_principal(kparam
.context
, kparam
.cc
,
255 "Error while getting principal from credential cache : "
256 << error_message(code
));
259 client_realm
= krb5_princ_realm(kparam
.context
, creds2
.client
);
261 krb5_make_principal(kparam
.context
, &creds2
.server
,
262 *client_realm
, KRB5_TGS_NAME
, *client_realm
, NULL
);
265 HERE
<< "Error while getting krbtgt principal : " <<
266 error_message(code
));
270 krb5_get_kdc_cred(kparam
.context
, kparam
.cc
, flags
, NULL
,
271 NULL
, &creds2
, &creds
);
272 krb5_free_creds(kparam
.context
, &creds2
);
275 if (code
== KRB5KRB_AP_ERR_TKT_EXPIRED
) {
276 krb5_free_creds(kparam
.context
, creds
);
278 /* this can happen because of clock skew */
282 HERE
<< "Error while get credentials : " <<
283 error_message(code
));
289 if (!kparam
.context
) {
290 code
= krb5_init_context(&kparam
.context
);
293 HERE
<< "Error while initialising Kerberos library : "
294 << error_message(code
));
298 #if HAVE_PROFILE_H && HAVE_KRB5_GET_PROFILE && HAVE_PROFILE_GET_INTEGER && HAVE_PROFILE_RELEASE
299 code
= krb5_get_profile(kparam
.context
, &profile
);
302 profile_release(profile
);
304 HERE
<< "Error while getting profile : " <<
305 error_message(code
));
309 profile_get_integer(profile
, "libdefaults", "clockskew", 0,
312 profile_release(profile
);
315 HERE
<< "Error while getting clockskew : " <<
316 error_message(code
));
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
;
328 char buf
[KT_PATH_MAX
], *p
;
330 krb5_kt_default_name(kparam
.context
, buf
, KT_PATH_MAX
);
331 p
= strchr(buf
, ':');
335 xfree(keytab_filename
);
336 keytab_filename
= xstrdup(p
? p
: buf
);
338 keytab_filename
= xstrdup(kf
);
341 code
= krb5_kt_resolve(kparam
.context
, keytab_filename
, &keytab
);
344 HERE
<< "Error while resolving keytab filename " <<
345 keytab_filename
<< " : " << error_message(code
));
350 code
= krb5_kt_start_seq_get(kparam
.context
, keytab
, &cursor
);
353 HERE
<< "Error while starting keytab scan : " <<
354 error_message(code
));
358 krb5_kt_next_entry(kparam
.context
, keytab
, &entry
, &cursor
);
359 krb5_copy_principal(kparam
.context
, entry
.principal
,
361 if (code
&& code
!= KRB5_KT_END
) {
363 HERE
<< "Error while scanning keytab : " <<
364 error_message(code
));
368 code
= krb5_kt_end_seq_get(kparam
.context
, keytab
, &cursor
);
371 HERE
<< "Error while ending keytab scan : " <<
372 error_message(code
));
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
);
378 code
= krb5_free_keytab_entry_contents(kparam
.context
, &entry
);
382 HERE
<< "Error while freeing keytab entry : " <<
383 error_message(code
));
388 principal_name
= xstrdup(pn
);
393 krb5_parse_name(kparam
.context
, principal_name
, &principal
);
396 HERE
<< "Error while parsing principal name " <<
397 principal_name
<< " : " << error_message(code
));
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) {
408 HERE
<< "Error bad lifetime value " << MAX_RENEW_TIME
<<
409 " : " << error_message(code
));
412 krb5_get_init_creds_opt_set_renew_life(&options
, rlife
);
415 krb5_get_init_creds_keytab(kparam
.context
, creds
, principal
,
416 keytab
, 0, NULL
, &options
);
420 "Error while initializing credentials from keytab : " <<
421 error_message(code
));
424 #if !HAVE_KRB5_MEMORY_CACHE
426 (char *) xmalloc(strlen("FILE:/tmp/peer_proxy_negotiate_auth_")
429 strlen("FILE:/tmp/peer_proxy_negotiate_auth_") + 16,
430 "FILE:/tmp/peer_proxy_negotiate_auth_%d", (int) getpid());
433 (char *) xmalloc(strlen("MEMORY:peer_proxy_negotiate_auth_") +
436 strlen("MEMORY:peer_proxy_negotiate_auth_") + 16,
437 "MEMORY:peer_proxy_negotiate_auth_%d", (int) getpid());
440 setenv("KRB5CCNAME", mem_cache
, 1);
441 code
= krb5_cc_resolve(kparam
.context
, mem_cache
, &kparam
.cc
);
446 HERE
<< "Error while resolving memory credential cache : "
447 << error_message(code
));
450 code
= krb5_cc_initialize(kparam
.context
, kparam
.cc
, principal
);
454 "Error while initializing memory credential cache : " <<
455 error_message(code
));
458 code
= krb5_cc_store_cred(kparam
.context
, kparam
.cc
, creds
);
461 HERE
<< "Error while storing credentials : " <<
462 error_message(code
));
466 if (!creds
->times
.starttime
)
467 creds
->times
.starttime
= creds
->times
.authtime
;
473 * peer_proxy_negotiate_auth gets a GSSAPI token for principal_name
474 * and base64 encodes it.
476 char *peer_proxy_negotiate_auth(char *principal_name
, char *proxy
) {
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
;
486 setbuf(stdout
, NULL
);
490 debugs(11, 5, HERE
<< "Error : No proxy server name");
496 HERE
<< "Creating credential cache for " << principal_name
);
498 debugs(11, 5, HERE
<< "Creating credential cache");
499 rc
= krb5_create_cache(NULL
, principal_name
);
501 debugs(11, 5, HERE
<< "Error : Failed to create Kerberos cache");
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
);
511 debugs(11, 5, HERE
<< "Import gss name");
512 major_status
= gss_import_name(&minor_status
, &service
,
513 gss_nt_service_name
, &server_name
);
515 if (check_gss_err(major_status
, minor_status
, "gss_import_name()"))
518 debugs(11, 5, HERE
<< "Initialize gss security context");
519 major_status
= gss_init_sec_context(&minor_status
,
526 GSS_C_NO_CHANNEL_BINDINGS
,
527 &input_token
, NULL
, &output_token
, NULL
, NULL
);
529 if (check_gss_err(major_status
, minor_status
, "gss_init_sec_context()"))
532 debugs(11, 5, HERE
<< "Got token with length " << output_token
.length
);
533 if (output_token
.length
) {
536 (char *) base64_encode_bin((const char *) output_token
.value
,
537 output_token
.length
);
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
);
553 #endif /* HAVE_KRB5 && HAVE_GSSAPI */