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
38 #endif /* HAVE_PROFILE_H */
41 #elif HAVE_ET_COM_ERR_H
42 #include <et/com_err.h>
43 #endif /* HAVE_COM_ERR_H */
46 #endif /* HAVE_COM_ERR_H */
48 #if HAVE_GSSAPI_GSSAPI_H
49 #include <gssapi/gssapi.h>
52 #endif /* HAVE_GSSAPI_H */
53 #if HAVE_GSSAPI_GSSAPI_EXT_H
54 #include <gssapi/gssapi_ext.h>
55 #endif /* HAVE_GSSAPI_GSSAPI_EXT_H */
56 #if HAVE_GSSAPI_GSSAPI_KRB5_H
57 #include <gssapi/gssapi_krb5.h>
58 #endif /* HAVE_GSSAPI_GSSAPI_KRB5_H */
59 #if HAVE_GSSAPI_GSSAPI_GENERIC_H
60 #include <gssapi/gssapi_generic.h>
61 #endif /* HAVE_GSSAPI_GSSAPI_GENERIC_H */
63 #ifndef gss_nt_service_name
64 #define gss_nt_service_name GSS_C_NT_HOSTBASED_SERVICE
67 #if !HAVE_ERROR_MESSAGE && HAVE_KRB5_GET_ERR_TEXT
68 #define error_message(code) krb5_get_err_text(kparam.context,code)
69 #elif !HAVE_ERROR_MESSAGE && HAVE_KRB5_GET_ERROR_MESSAGE
70 #define error_message(code) krb5_get_error_message(kparam.context,code)
71 #elif !HAVE_ERROR_MESSAGE
72 static char err_code
[17];
73 const char *KRB5_CALLCONV
74 error_message(long code
)
76 snprintf(err_code
,16,"%ld",code
);
81 #ifndef gss_mech_spnego
82 static gss_OID_desc _gss_mech_spnego
=
83 { 6, (void *) "\x2b\x06\x01\x05\x05\x02" };
84 gss_OID gss_mech_spnego
= &_gss_mech_spnego
;
88 #include <ibm_svc/krb5_svc.h>
89 const char *KRB5_CALLCONV
error_message(long code
)
92 krb5_svc_get_msg(code
, &msg
);
98 * Kerberos context and cache structure
99 * Caches authentication details to reduce
100 * number of authentication requests to kdc
102 static struct kstruct
104 krb5_context context
;
110 * krb5_create_cache creates a Kerberos file credential cache or a memory
111 * credential cache if supported. The initial key for the principal
112 * principal_name is extracted from the keytab keytab_filename.
114 * If keytab_filename is NULL the default will be used.
115 * If principal_name is NULL the first working entry of the keytab will be used.
117 int krb5_create_cache(char *keytab_filename
, char *principal_name
);
120 * krb5_cleanup clears used Keberos memory
122 void krb5_cleanup(void);
125 * check_gss_err checks for gssapi error codes, extracts the error message
128 int check_gss_err(OM_uint32 major_status
, OM_uint32 minor_status
,
129 const char *function
);
131 int check_gss_err(OM_uint32 major_status
, OM_uint32 minor_status
,
132 const char *function
)
134 if (GSS_ERROR(major_status
)) {
135 OM_uint32 maj_stat
, min_stat
;
136 OM_uint32 msg_ctx
= 0;
137 gss_buffer_desc status_string
;
144 /* convert major status code (GSS-API error) to text */
145 maj_stat
= gss_display_status(&min_stat
, major_status
,
146 GSS_C_GSS_CODE
, GSS_C_NULL_OID
, &msg_ctx
, &status_string
);
147 if (maj_stat
== GSS_S_COMPLETE
) {
148 if (sizeof(buf
) > len
+ status_string
.length
+ 1) {
149 memcpy(buf
+ len
, status_string
.value
,
150 status_string
.length
);
151 len
+= status_string
.length
;
153 gss_release_buffer(&min_stat
, &status_string
);
156 gss_release_buffer(&min_stat
, &status_string
);
158 if (sizeof(buf
) > len
+ 2) {
159 strcpy(buf
+ len
, ". ");
164 /* convert minor status code (underlying routine error) to text */
165 maj_stat
= gss_display_status(&min_stat
, minor_status
,
166 GSS_C_MECH_CODE
, GSS_C_NULL_OID
, &msg_ctx
, &status_string
);
167 if (maj_stat
== GSS_S_COMPLETE
) {
168 if (sizeof(buf
) > len
+ status_string
.length
) {
169 memcpy(buf
+ len
, status_string
.value
,
170 status_string
.length
);
171 len
+= status_string
.length
;
173 gss_release_buffer(&min_stat
, &status_string
);
176 gss_release_buffer(&min_stat
, &status_string
);
178 debugs(11, 5, HERE
<< function
<< "failed: " << buf
);
186 debugs(11, 5, HERE
<< "Cleanup kerberos context");
187 if (kparam
.context
) {
189 krb5_cc_destroy(kparam
.context
, kparam
.cc
);
191 krb5_free_context(kparam
.context
);
192 kparam
.context
= NULL
;
196 int krb5_create_cache(char *kf
, char *pn
)
199 #define KT_PATH_MAX 256
200 #define MAX_RENEW_TIME "365d"
201 #define DEFAULT_SKEW (krb5_deltat) 600
203 static char *keytab_filename
= NULL
, *principal_name
= NULL
;
204 static krb5_keytab keytab
= 0;
205 static krb5_keytab_entry entry
;
206 static krb5_kt_cursor cursor
;
207 static krb5_creds
*creds
= NULL
;
208 #if HAVE_HEIMDAL_KERBEROS
209 static krb5_creds creds2
;
211 static krb5_principal principal
= NULL
;
212 static krb5_deltat skew
;
214 krb5_get_init_creds_opt options
;
215 krb5_error_code code
= 0;
217 #if HAVE_PROFILE_H && HAVE_KRB5_GET_PROFILE && HAVE_PROFILE_GET_INTEGER && HAVE_PROFILE_RELEASE
220 #if HAVE_HEIMDAL_KERBEROS
221 krb5_kdc_flags flags
;
222 krb5_realm
*client_realm
;
228 * Check if credentials need to be renewed
231 (creds
->times
.endtime
- time(0) > skew
) &&
232 (creds
->times
.renew_till
- time(0) > 2 * skew
)) {
233 if (creds
->times
.endtime
- time(0) < 2 * skew
) {
234 #if !HAVE_HEIMDAL_KERBEROS
237 krb5_get_renewed_creds(kparam
.context
, creds
, principal
,
242 flags
.b
.renewable
= flags
.b
.renew
= 1;
245 krb5_cc_get_principal(kparam
.context
, kparam
.cc
,
250 "Error while getting principal from credential cache : "
251 << error_message(code
));
254 client_realm
= krb5_princ_realm(kparam
.context
, creds2
.client
);
256 krb5_make_principal(kparam
.context
, &creds2
.server
,
257 *client_realm
, KRB5_TGS_NAME
, *client_realm
, NULL
);
260 HERE
<< "Error while getting krbtgt principal : " <<
261 error_message(code
));
265 krb5_get_kdc_cred(kparam
.context
, kparam
.cc
, flags
, NULL
,
266 NULL
, &creds2
, &creds
);
267 krb5_free_creds(kparam
.context
, &creds2
);
270 if (code
== KRB5KRB_AP_ERR_TKT_EXPIRED
) {
271 krb5_free_creds(kparam
.context
, creds
);
273 /* this can happen because of clock skew */
277 HERE
<< "Error while get credentials : " <<
278 error_message(code
));
284 if (!kparam
.context
) {
285 code
= krb5_init_context(&kparam
.context
);
288 HERE
<< "Error while initialising Kerberos library : "
289 << error_message(code
));
293 #if HAVE_PROFILE_H && HAVE_KRB5_GET_PROFILE && HAVE_PROFILE_GET_INTEGER && HAVE_PROFILE_RELEASE
294 code
= krb5_get_profile(kparam
.context
, &profile
);
297 profile_release(profile
);
299 HERE
<< "Error while getting profile : " <<
300 error_message(code
));
304 profile_get_integer(profile
, "libdefaults", "clockskew", 0,
307 profile_release(profile
);
310 HERE
<< "Error while getting clockskew : " <<
311 error_message(code
));
314 #elif HAVE_KRB5_GET_MAX_TIME_SKEW && HAVE_HEIMDAL_KERBEROS
315 skew
= krb5_get_max_time_skew(kparam
.context
);
316 #elif HAVE_MAX_SKEW_IN_KRB5_CONTEXT && HAVE_HEIMDAL_KERBEROS
317 skew
= kparam
.context
->max_skew
;
323 char buf
[KT_PATH_MAX
], *p
;
325 krb5_kt_default_name(kparam
.context
, buf
, KT_PATH_MAX
);
326 p
= strchr(buf
, ':');
330 xfree(keytab_filename
);
331 keytab_filename
= xstrdup(p
? p
: buf
);
333 keytab_filename
= xstrdup(kf
);
336 code
= krb5_kt_resolve(kparam
.context
, keytab_filename
, &keytab
);
339 HERE
<< "Error while resolving keytab filename " <<
340 keytab_filename
<< " : " << error_message(code
));
345 code
= krb5_kt_start_seq_get(kparam
.context
, keytab
, &cursor
);
348 HERE
<< "Error while starting keytab scan : " <<
349 error_message(code
));
353 krb5_kt_next_entry(kparam
.context
, keytab
, &entry
, &cursor
);
354 krb5_copy_principal(kparam
.context
, entry
.principal
,
356 if (code
&& code
!= KRB5_KT_END
) {
358 HERE
<< "Error while scanning keytab : " <<
359 error_message(code
));
363 code
= krb5_kt_end_seq_get(kparam
.context
, keytab
, &cursor
);
366 HERE
<< "Error while ending keytab scan : " <<
367 error_message(code
));
370 #if HAVE_HEIMDAL_KERBEROS || ( HAVE_KRB5_KT_FREE_ENTRY && HAVE_DECL_KRB5_KT_FREE_ENTRY)
371 code
= krb5_kt_free_entry(kparam
.context
, &entry
);
373 code
= krb5_free_keytab_entry_contents(kparam
.context
, &entry
);
377 HERE
<< "Error while freeing keytab entry : " <<
378 error_message(code
));
383 principal_name
= xstrdup(pn
);
388 krb5_parse_name(kparam
.context
, principal_name
, &principal
);
391 HERE
<< "Error while parsing principal name " <<
392 principal_name
<< " : " << error_message(code
));
397 creds
= (krb5_creds
*) xmalloc(sizeof(*creds
));
398 memset(creds
, 0, sizeof(*creds
));
399 krb5_get_init_creds_opt_init(&options
);
400 code
= krb5_string_to_deltat((char *) MAX_RENEW_TIME
, &rlife
);
401 if (code
!= 0 || rlife
== 0) {
403 HERE
<< "Error bad lifetime value " << MAX_RENEW_TIME
<<
404 " : " << error_message(code
));
407 krb5_get_init_creds_opt_set_renew_life(&options
, rlife
);
410 krb5_get_init_creds_keytab(kparam
.context
, creds
, principal
,
411 keytab
, 0, NULL
, &options
);
415 "Error while initializing credentials from keytab : " <<
416 error_message(code
));
419 #if !HAVE_KRB5_MEMORY_CACHE
421 (char *) xmalloc(strlen("FILE:/tmp/peer_proxy_negotiate_auth_")
424 strlen("FILE:/tmp/peer_proxy_negotiate_auth_") + 16,
425 "FILE:/tmp/peer_proxy_negotiate_auth_%d", (int) getpid());
428 (char *) xmalloc(strlen("MEMORY:peer_proxy_negotiate_auth_") +
431 strlen("MEMORY:peer_proxy_negotiate_auth_") + 16,
432 "MEMORY:peer_proxy_negotiate_auth_%d", (int) getpid());
435 setenv("KRB5CCNAME", mem_cache
, 1);
436 code
= krb5_cc_resolve(kparam
.context
, mem_cache
, &kparam
.cc
);
441 HERE
<< "Error while resolving memory credential cache : "
442 << error_message(code
));
445 code
= krb5_cc_initialize(kparam
.context
, kparam
.cc
, principal
);
449 "Error while initializing memory credential cache : " <<
450 error_message(code
));
453 code
= krb5_cc_store_cred(kparam
.context
, kparam
.cc
, creds
);
456 HERE
<< "Error while storing credentials : " <<
457 error_message(code
));
461 if (!creds
->times
.starttime
)
462 creds
->times
.starttime
= creds
->times
.authtime
;
468 * peer_proxy_negotiate_auth gets a GSSAPI token for principal_name
469 * and base64 encodes it.
471 char *peer_proxy_negotiate_auth(char *principal_name
, char *proxy
)
474 OM_uint32 major_status
, minor_status
;
475 gss_ctx_id_t gss_context
= GSS_C_NO_CONTEXT
;
476 gss_name_t server_name
= GSS_C_NO_NAME
;
477 gss_buffer_desc service
= GSS_C_EMPTY_BUFFER
;
478 gss_buffer_desc input_token
= GSS_C_EMPTY_BUFFER
;
479 gss_buffer_desc output_token
= GSS_C_EMPTY_BUFFER
;
482 setbuf(stdout
, NULL
);
486 debugs(11, 5, HERE
<< "Error : No proxy server name");
492 HERE
<< "Creating credential cache for " << principal_name
);
494 debugs(11, 5, HERE
<< "Creating credential cache");
495 rc
= krb5_create_cache(NULL
, principal_name
);
497 debugs(11, 5, HERE
<< "Error : Failed to create Kerberos cache");
502 service
.value
= (void *) xmalloc(strlen("HTTP") + strlen(proxy
) + 2);
503 snprintf((char *) service
.value
, strlen("HTTP") + strlen(proxy
) + 2,
504 "%s@%s", "HTTP", proxy
);
505 service
.length
= strlen((char *) service
.value
);
507 debugs(11, 5, HERE
<< "Import gss name");
508 major_status
= gss_import_name(&minor_status
, &service
,
509 gss_nt_service_name
, &server_name
);
511 if (check_gss_err(major_status
, minor_status
, "gss_import_name()"))
514 debugs(11, 5, HERE
<< "Initialize gss security context");
515 major_status
= gss_init_sec_context(&minor_status
,
522 GSS_C_NO_CHANNEL_BINDINGS
,
523 &input_token
, NULL
, &output_token
, NULL
, NULL
);
525 if (check_gss_err(major_status
, minor_status
, "gss_init_sec_context()"))
528 debugs(11, 5, HERE
<< "Got token with length " << output_token
.length
);
529 if (output_token
.length
) {
532 (char *) base64_encode_bin((const char *) output_token
.value
,
533 output_token
.length
);
538 gss_delete_sec_context(&minor_status
, &gss_context
, NULL
);
539 gss_release_buffer(&minor_status
, &service
);
540 gss_release_buffer(&minor_status
, &input_token
);
541 gss_release_buffer(&minor_status
, &output_token
);
542 gss_release_name(&minor_status
, &server_name
);
550 #endif /* HAVE_KRB5 && HAVE_GSSAPI */