2 * Daniel Kouril <kouril@users.sourceforge.net>
4 * Source and Documentation can be found at:
5 * http://modauthkerb.sourceforge.net/
8 * James E. Robinson, III <james@ncstate.net>
9 * Daniel Henninger <daniel@ncsu.edu>
10 * Ludek Sulak <xsulak@fi.muni.cz>
14 * Copyright (c) 2004-2006 Masarykova universita
15 * (Masaryk University, Brno, Czech Republic)
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are met:
21 * 1. Redistributions of source code must retain the above copyright notice,
22 * this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
28 * 3. Neither the name of the University nor the names of its contributors may
29 * be used to endorse or promote products derived from this software
30 * without specific prior written permission.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
33 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
36 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGE.
53 #define MODAUTHKERB_VERSION "5.3"
55 #define MECH_NEGOTIATE "Negotiate"
56 #define SERVICE_NAME "HTTP"
59 #include <http_config.h>
60 #include <http_core.h>
62 #include <http_protocol.h>
63 #include <http_request.h>
65 #ifdef STANDARD20_MODULE_STUFF
66 #include <apr_strings.h>
67 #include <apr_base64.h>
69 #define apr_pstrdup ap_pstrdup
70 #define apr_psprintf ap_psprintf
71 #define apr_pstrcat ap_pstrcat
72 #define apr_pcalloc ap_pcalloc
73 #define apr_table_setn ap_table_setn
74 #define apr_table_add ap_table_add
75 #define apr_base64_decode_len ap_base64decode_len
76 #define apr_base64_decode ap_base64decode
77 #define apr_base64_encode_len ap_base64encode_len
78 #define apr_base64_encode ap_base64encode
79 #define apr_pool_cleanup_null ap_null_cleanup
80 #define apr_pool_cleanup_register ap_register_cleanup
81 #endif /* STANDARD20_MODULE_STUFF */
84 #define vsnprintf _vsnprintf
85 #define snprintf _snprintf
93 # include <gssapi/gssapi.h>
94 # include <gssapi/gssapi_generic.h>
95 # include <gssapi/gssapi_krb5.h>
96 # define GSS_C_NT_USER_NAME gss_nt_user_name
97 # define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
98 # define GSS_KRB5_NT_PRINCIPAL_NAME gss_nt_krb5_name
99 # define krb5_get_err_text(context,code) error_message(code)
101 #ifndef GSSAPI_SUPPORTS_SPNEGO
102 # include "spnegokrb5.h"
107 /* Prevent warning about closesocket redefinition (Apache's ap_config.h and
108 * MIT Kerberos' port-sockets.h both define it as close) */
113 #include <netdb.h> /* gethostbyname() */
117 /* should be HAVE_UNISTD_H instead */
121 #ifndef KRB5_LIB_FUNCTION
123 # define KRB5_LIB_FUNCTION _stdcall
125 # define KRB5_LIB_FUNCTION
129 #ifdef STANDARD20_MODULE_STUFF
130 module AP_MODULE_DECLARE_DATA auth_kerb_module;
132 module auth_kerb_module;
135 /***************************************************************************
136 Macros To Ease Compatibility
137 ***************************************************************************/
138 #ifdef STANDARD20_MODULE_STUFF
139 #define MK_POOL apr_pool_t
140 #define MK_TABLE_GET apr_table_get
141 #define MK_USER r->user
142 #define MK_AUTH_TYPE r->ap_auth_type
145 #define MK_TABLE_GET ap_table_get
146 #define MK_USER r->connection->user
147 #define MK_AUTH_TYPE r->connection->ap_auth_type
148 #define PROXYREQ_PROXY STD_PROXY
151 /***************************************************************************
152 Auth Configuration Structure
153 ***************************************************************************/
155 char *krb_auth_realms;
156 int krb_save_credentials;
158 const char *krb_service_name;
159 int krb_authoritative;
160 int krb_delegate_basic;
162 int krb_ssl_preauthentication;
166 int krb_method_gssapi;
167 int krb_method_k5pass;
168 int krb5_do_auth_to_local;
172 int krb_method_k4pass;
177 set_kerb_auth_headers(request_rec *r, const kerb_auth_config *conf,
178 int use_krb4, int use_krb5pwd, char *negotiate_ret_value);
181 krb5_save_realms(cmd_parms *cmd, void *sec, const char *arg);
183 #ifdef STANDARD20_MODULE_STUFF
184 #define command(name, func, var, type, usage) \
185 AP_INIT_ ## type (name, (void*) func, \
186 (void*)APR_OFFSETOF(kerb_auth_config, var), \
187 OR_AUTHCFG | RSRC_CONF, usage)
189 #define command(name, func, var, type, usage) \
191 (void*)XtOffsetOf(kerb_auth_config, var), \
192 OR_AUTHCFG | RSRC_CONF, type, usage }
195 static const command_rec kerb_auth_cmds[] = {
196 command("KrbAuthRealms", krb5_save_realms, krb_auth_realms,
197 RAW_ARGS, "Realms to attempt authentication against (can be multiple)."),
199 command("KrbAuthRealm", krb5_save_realms, krb_auth_realms,
200 RAW_ARGS, "Alias for KrbAuthRealms."),
202 command("KrbSaveCredentials", ap_set_flag_slot, krb_save_credentials,
203 FLAG, "Save and store credentials/tickets retrieved during auth."),
205 command("KrbVerifyKDC", ap_set_flag_slot, krb_verify_kdc,
206 FLAG, "Verify tickets against keytab to prevent KDC spoofing attacks."),
208 command("KrbServiceName", ap_set_string_slot, krb_service_name,
209 TAKE1, "Full or partial service name to be used by Apache for authentication."),
211 command("KrbAuthoritative", ap_set_flag_slot, krb_authoritative,
212 FLAG, "Set to 'off' to allow access control to be passed along to lower modules iff the UserID is not known to this module."),
214 command("KrbDelegateBasic", ap_set_flag_slot, krb_delegate_basic,
215 FLAG, "Always offer Basic authentication regardless of KrbMethodK5Pass and pass on authentication to lower modules if Basic headers arrive."),
218 command("KrbEnableSSLPreauthentication", ap_set_flag_slot, krb_ssl_preauthentication,
219 FLAG, "Don't do Kerberos authentication if the user is already authenticated using SSL and her client certificate."),
223 command("Krb5Keytab", ap_set_file_slot, krb_5_keytab,
224 TAKE1, "Location of Kerberos V5 keytab file."),
226 command("KrbMethodNegotiate", ap_set_flag_slot, krb_method_gssapi,
227 FLAG, "Enable Negotiate authentication method."),
229 command("KrbMethodK5Passwd", ap_set_flag_slot, krb_method_k5pass,
230 FLAG, "Enable Kerberos V5 password authentication."),
232 command("Krb5TrimRealm", ap_set_flag_slot, krb5_do_auth_to_local,
233 FLAG, "Set to 'on' to have Kerberos do auth_to_local mapping of principal names to system user names."),
237 command("Krb4Srvtab", ap_set_file_slot, krb_4_srvtab,
238 TAKE1, "Location of Kerberos V4 srvtab file."),
240 command("KrbMethodK4Passwd", ap_set_flag_slot, krb_method_k4pass,
241 FLAG, "Enable Kerberos V4 password authentication."),
249 mkstemp(char *template)
254 start = strlen(template) - 1;
255 while(template[start] == 'X') {
256 template[start] = '0' + val % 10;
263 fd = open(template, O_RDWR | O_CREAT | O_EXCL, 0600);
264 if(fd >= 0 || errno != EEXIST)
271 if(template[i] == '9' + 1)
273 if(template[i] <= 'z')
282 #if defined(KRB5) && !defined(HEIMDAL)
283 /* Needed to work around problems with replay caches */
284 #include "mit-internals.h"
286 /* This is our replacement krb5_rc_store function */
287 static krb5_error_code KRB5_LIB_FUNCTION
288 mod_auth_kerb_rc_store(krb5_context context, krb5_rcache rcache,
289 krb5_donot_replay_internal *donot_replay)
294 /* And this is the operations vector for our replay cache */
295 const krb5_rc_ops_internal mod_auth_kerb_rc_ops = {
302 mod_auth_kerb_rc_store,
304 krb5_rc_dfl_get_span,
305 krb5_rc_dfl_get_name,
310 /***************************************************************************
311 Macro To Control krb5_aname_to_localname buffer size
312 ***************************************************************************/
313 #define AN_TO_LN_BUFFSIZE_MAX 1024
315 /***************************************************************************
316 Auth Configuration Initialization
317 ***************************************************************************/
318 static void *kerb_dir_create_config(MK_POOL *p, char *d)
320 kerb_auth_config *rec;
322 rec = (kerb_auth_config *) apr_pcalloc(p, sizeof(kerb_auth_config));
323 ((kerb_auth_config *)rec)->krb_verify_kdc = 1;
324 ((kerb_auth_config *)rec)->krb_service_name = NULL;
325 ((kerb_auth_config *)rec)->krb_authoritative = 1;
326 ((kerb_auth_config *)rec)->krb_delegate_basic = 0;
328 ((kerb_auth_config *)rec)->krb_ssl_preauthentication = 0;
331 ((kerb_auth_config *)rec)->krb5_do_auth_to_local = 0;
332 ((kerb_auth_config *)rec)->krb_method_k5pass = 1;
333 ((kerb_auth_config *)rec)->krb_method_gssapi = 1;
336 ((kerb_auth_config *)rec)->krb_method_k4pass = 1;
342 krb5_save_realms(cmd_parms *cmd, void *vsec, const char *arg)
344 kerb_auth_config *sec = (kerb_auth_config *) vsec;
345 sec->krb_auth_realms= apr_pstrdup(cmd->pool, arg);
350 log_rerror(const char *file, int line, int level, int status,
351 const request_rec *r, const char *fmt, ...)
357 vsnprintf(errstr, sizeof(errstr), fmt, ap);
361 #ifdef STANDARD20_MODULE_STUFF
362 ap_log_rerror(file, line, level | APLOG_NOERRNO, status, r, "%s", errstr);
364 ap_log_rerror(file, line, level | APLOG_NOERRNO, r, "%s", errstr);
369 /***************************************************************************
370 Username/Password Validation for Krb4
371 ***************************************************************************/
373 verify_krb4_user(request_rec *r, const char *name, const char *instance,
374 const char *realm, const char *password, const char *linstance, const char *srvtab, int krb_verify_kdc)
380 const char *hostname;
383 char lrealm[REALM_SZ];
385 ret = krb_get_pw_in_tkt(name, instance, realm, "krbtgt", realm,
386 DEFAULT_TKT_LIFE, password);
388 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
389 "Cannot get krb4 ticket: krb_get_pw_in_tkt() failed: %s",
390 krb_get_err_text(ret));
397 hostname = ap_get_server_name(r);
399 hp = gethostbyname(hostname);
402 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
403 "Cannot verify krb4 ticket: gethostbyname() failed: %s",
407 memcpy(&addr, hp->h_addr, sizeof(addr));
409 phost = krb_get_phost((char *)hostname);
411 krb_get_lrealm(lrealm, 1);
413 ret = krb_mk_req(&ticket, linstance, phost, lrealm, 0);
415 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
416 "Cannot verify krb4 ticket: krb_mk_req() failed: %s",
417 krb_get_err_text(ret));
422 ret = krb_rd_req(&ticket, (char *)linstance, phost, addr, &authdata, (char *)srvtab);
424 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
425 "Cannot verify krb4 ticket: krb_rd_req() failed: %s",
426 krb_get_err_text(ret));
434 krb4_cache_cleanup(void *data)
436 char *tkt_file = (char *) data;
438 krb_set_tkt_string(tkt_file);
444 authenticate_user_krb4pwd(request_rec *r,
445 kerb_auth_config *conf,
446 const char *auth_line)
450 const char *sent_name;
453 char *tkt_file_p = NULL;
458 char lrealm[REALM_SZ];
459 int all_principals_unkown;
461 sent_pw = ap_pbase64decode(r->pool, auth_line);
462 sent_name = ap_getword_nulls_nc (r->pool, (char **) &sent_pw, ':');
464 sent_instance = strchr(sent_name, '.');
466 *sent_instance++ = '\0';
468 snprintf(tkt_file, sizeof(tkt_file), "/tmp/apache_tkt_XXXXXX");
469 fd = mkstemp(tkt_file);
471 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
472 "Cannot create krb4 ccache: mkstemp() failed: %s",
474 return HTTP_INTERNAL_SERVER_ERROR;
477 tkt_file_p = apr_pstrdup(r->pool, tkt_file);
478 apr_pool_cleanup_register(r->pool, tkt_file_p, krb4_cache_cleanup,
479 apr_pool_cleanup_null);
481 krb_set_tkt_string(tkt_file);
483 all_principals_unkown = 1;
484 realms = conf->krb_auth_realms;
486 memset(lrealm, 0, sizeof(lrealm));
489 realm = ap_getword_white(r->pool, &realms);
492 ret = krb_get_lrealm(lrealm, 1);
498 /* XXX conf->krb_service_name */
499 ret = verify_krb4_user(r, (char *)sent_name,
500 (sent_instance) ? sent_instance : "",
501 (char *)realm, (char *)sent_pw,
502 conf->krb_service_name,
503 conf->krb_4_srvtab, conf->krb_verify_kdc);
504 if (!conf->krb_authoritative && ret) {
505 /* if we're not authoritative, we allow authentication to pass on
506 * to another modules if (and only if) the user is not known to us */
507 if (all_principals_unkown && ret != KDC_PR_UNKNOWN)
508 all_principals_unkown = 0;
513 } while (realms && *realms);
516 /* XXX log only in the verify_krb4_user() call */
517 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Verifying krb4 password failed");
518 ret = (!conf->krb_authoritative && all_principals_unkown == 1 && ret == KDC_PR_UNKNOWN) ?
519 DECLINED : HTTP_UNAUTHORIZED;
523 user = apr_pstrdup(r->pool, sent_name);
525 user = apr_pstrcat(r->pool, user, ".", sent_instance, NULL);
526 user = apr_pstrcat(r->pool, user, "@", realm, NULL);
529 MK_AUTH_TYPE = "Basic";
530 apr_table_setn(r->subprocess_env, "KRBTKFILE", tkt_file_p);
532 if (!conf->krb_save_credentials)
533 krb4_cache_cleanup(tkt_file);
537 krb4_cache_cleanup(tkt_file);
546 /***************************************************************************
547 Username/Password Validation for Krb5
548 ***************************************************************************/
550 /* MIT kerberos uses replay cache checks even during credential verification
551 * (i.e. in krb5_verify_init_creds()), which is obviosuly useless. In order to
552 * avoid problems with multiple apache processes accessing the same rcache file
553 * we had to use this call instead, which is only a bit modified version of
554 * krb5_verify_init_creds() */
555 static krb5_error_code
556 verify_krb5_init_creds(request_rec *r, krb5_context context, krb5_creds *creds,
557 krb5_principal ap_req_server, krb5_keytab ap_req_keytab)
561 krb5_ccache local_ccache = NULL;
562 krb5_creds *new_creds = NULL;
563 krb5_auth_context auth_context = NULL;
564 krb5_keytab keytab = NULL;
567 memset(&req, 0, sizeof(req));
569 if (ap_req_keytab == NULL) {
570 ret = krb5_kt_default (context, &keytab);
574 keytab = ap_req_keytab;
576 ret = krb5_cc_resolve(context, "MEMORY:", &local_ccache);
578 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
579 "krb5_cc_resolve() failed when verifying KDC");
583 ret = krb5_cc_initialize(context, local_ccache, creds->client);
585 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
586 "krb5_cc_initialize() failed when verifying KDC");
590 ret = krb5_cc_store_cred (context, local_ccache, creds);
592 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
593 "krb5_cc_initialize() failed when verifying KDC");
597 ret = krb5_unparse_name(context, ap_req_server, &server_name);
599 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
600 "krb5_unparse_name() failed when verifying KDC");
603 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
604 "Trying to verify authenticity of KDC using principal %s", server_name);
607 if (!krb5_principal_compare (context, ap_req_server, creds->server)) {
608 krb5_creds match_cred;
610 memset (&match_cred, 0, sizeof(match_cred));
612 match_cred.client = creds->client;
613 match_cred.server = ap_req_server;
615 ret = krb5_get_credentials (context, 0, local_ccache,
616 &match_cred, &new_creds);
618 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
619 "krb5_get_credentials() failed when verifying KDC");
625 ret = krb5_mk_req_extended (context, &auth_context, 0, NULL, creds, &req);
627 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
628 "krb5_mk_req_extended() failed when verifying KDC");
632 krb5_auth_con_free (context, auth_context);
634 ret = krb5_auth_con_init(context, &auth_context);
636 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
637 "krb5_auth_con_init() failed when verifying KDC");
640 /* use KRB5_AUTH_CONTEXT_DO_SEQUENCE to skip replay cache checks */
641 krb5_auth_con_setflags(context, auth_context, KRB5_AUTH_CONTEXT_DO_SEQUENCE);
643 ret = krb5_rd_req (context, &auth_context, &req, ap_req_server,
646 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
647 "krb5_rd_req() failed when verifying KDC");
653 /* XXX Do I ever want to support Heimdal 0.4 ??? */
654 krb5_data_free(&req);
656 krb5_free_data_contents(context, &req);
659 krb5_auth_con_free (context, auth_context);
661 krb5_free_creds (context, new_creds);
662 if (ap_req_keytab == NULL && keytab)
663 krb5_kt_close (context, keytab);
665 krb5_cc_destroy (context, local_ccache);
670 /* Inspired by krb5_verify_user from Heimdal */
671 static krb5_error_code
672 verify_krb5_user(request_rec *r, krb5_context context, krb5_principal principal,
673 const char *password, krb5_principal server,
674 krb5_keytab keytab, int krb_verify_kdc, krb5_ccache *ccache)
678 krb5_ccache ret_ccache = NULL;
681 /* XXX error messages shouldn't be logged here (and in the while() loop in
682 * authenticate_user_krb5pwd() as weell), in order to avoid confusing log
683 * entries when using multiple realms */
685 memset(&creds, 0, sizeof(creds));
687 ret = krb5_unparse_name(context, principal, &name);
689 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
690 "Trying to get TGT for user %s", name);
694 ret = krb5_get_init_creds_password(context, &creds, principal,
695 (char *)password, NULL,
696 NULL, 0, NULL, NULL);
698 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
699 "krb5_get_init_creds_password() failed: %s",
700 krb5_get_err_text(context, ret));
708 krb5_get_default_realm(context, &realm);
709 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
710 "trying to verify password using key for %s/%s@%s",
711 service, ap_get_server_name(r), realm);
715 if (krb_verify_kdc &&
716 (ret = verify_krb5_init_creds(r, context, &creds, server, keytab))) {
717 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
718 "failed to verify krb5 credentials: %s",
719 krb5_get_err_text(context, ret));
723 ret = krb5_cc_resolve(context, "MEMORY:", &ret_ccache);
725 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
726 "generating new memory ccache failed: %s",
727 krb5_get_err_text(context, ret));
731 ret = krb5_cc_initialize(context, ret_ccache, principal);
733 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
734 "krb5_cc_initialize() failed: %s",
735 krb5_get_err_text(context, ret));
739 ret = krb5_cc_store_cred(context, ret_ccache, &creds);
741 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
742 "krb5_cc_store_cred() failed: %s",
743 krb5_get_err_text(context, ret));
746 *ccache = ret_ccache;
750 krb5_free_cred_contents(context, &creds);
752 krb5_cc_destroy(context, ret_ccache);
758 krb5_cache_cleanup(void *data)
760 krb5_context context;
762 krb5_error_code problem;
763 char *cache_name = (char *) data;
765 problem = krb5_init_context(&context);
767 /* ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "krb5_init_context() failed"); */
768 return HTTP_INTERNAL_SERVER_ERROR;
771 problem = krb5_cc_resolve(context, cache_name, &cache);
773 /* log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
774 "krb5_cc_resolve() failed (%s: %s)",
775 cache_name, krb5_get_err_text(context, problem)); */
776 return HTTP_INTERNAL_SERVER_ERROR;
779 krb5_cc_destroy(context, cache);
780 krb5_free_context(context);
785 create_krb5_ccache(krb5_context kcontext,
787 kerb_auth_config *conf,
788 krb5_principal princ,
793 krb5_error_code problem;
795 krb5_ccache tmp_ccache = NULL;
797 ccname = apr_psprintf(r->pool, "FILE:%s/krb5cc_apache_XXXXXX", P_tmpdir);
798 fd = mkstemp(ccname + strlen("FILE:"));
800 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
801 "mkstemp() failed: %s", strerror(errno));
802 ret = HTTP_INTERNAL_SERVER_ERROR;
807 problem = krb5_cc_resolve(kcontext, ccname, &tmp_ccache);
809 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
810 "krb5_cc_resolve() failed: %s",
811 krb5_get_err_text(kcontext, problem));
812 ret = HTTP_INTERNAL_SERVER_ERROR;
817 problem = krb5_cc_initialize(kcontext, tmp_ccache, princ);
819 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
820 "Cannot initialize krb5 ccache %s: krb5_cc_initialize() failed: %s",
821 ccname, krb5_get_err_text(kcontext, problem));
822 ret = HTTP_INTERNAL_SERVER_ERROR;
826 apr_table_setn(r->subprocess_env, "KRB5CCNAME", ccname);
827 apr_pool_cleanup_register(r->pool, ccname, krb5_cache_cleanup,
828 apr_pool_cleanup_null);
830 *ccache = tmp_ccache;
837 krb5_cc_destroy(kcontext, tmp_ccache);
843 store_krb5_creds(krb5_context kcontext,
845 kerb_auth_config *conf,
846 krb5_ccache delegated_cred)
849 krb5_error_code problem;
850 krb5_principal princ;
854 problem = krb5_cc_get_principal(kcontext, delegated_cred, &princ);
856 snprintf(errstr, sizeof(errstr), "krb5_cc_get_principal() failed: %s",
857 krb5_get_err_text(kcontext, problem));
858 return HTTP_INTERNAL_SERVER_ERROR;
861 ret = create_krb5_ccache(kcontext, r, conf, princ, &ccache);
863 krb5_free_principal(kcontext, princ);
868 problem = krb5_cc_copy_cache(kcontext, delegated_cred, ccache);
870 problem = krb5_cc_copy_creds(kcontext, delegated_cred, ccache);
872 krb5_free_principal(kcontext, princ);
874 snprintf(errstr, sizeof(errstr), "Failed to store credentials: %s",
875 krb5_get_err_text(kcontext, problem));
876 krb5_cc_destroy(kcontext, ccache);
877 return HTTP_INTERNAL_SERVER_ERROR;
880 krb5_cc_close(kcontext, ccache);
886 authenticate_user_krb5pwd(request_rec *r,
887 kerb_auth_config *conf,
888 const char *auth_line)
890 const char *sent_pw = NULL;
891 const char *sent_name = NULL;
892 const char *realms = NULL;
893 const char *realm = NULL;
894 krb5_context kcontext = NULL;
895 krb5_error_code code;
896 krb5_principal client = NULL;
897 krb5_principal server = NULL;
898 krb5_ccache ccache = NULL;
899 krb5_keytab keytab = NULL;
902 int all_principals_unkown;
905 code = krb5_init_context(&kcontext);
907 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
908 "Cannot initialize Kerberos5 context (%d)", code);
909 return HTTP_INTERNAL_SERVER_ERROR;
912 sent_pw = ap_pbase64decode(r->pool, auth_line);
913 sent_name = ap_getword_nulls_nc (r->pool, (char **) &sent_pw, ':');
915 if (sent_pw == NULL || *sent_pw == '\0') {
916 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
917 "empty passwords are not accepted");
918 ret = HTTP_UNAUTHORIZED;
922 if (conf->krb_5_keytab)
923 krb5_kt_resolve(kcontext, conf->krb_5_keytab, &keytab);
925 if (conf->krb_service_name && strchr(conf->krb_service_name, '/') != NULL)
926 ret = krb5_parse_name (kcontext, conf->krb_service_name, &server);
928 ret = krb5_sname_to_principal(kcontext, ap_get_server_name(r),
929 (conf->krb_service_name) ? conf->krb_service_name : SERVICE_NAME,
930 KRB5_NT_SRV_HST, &server);
933 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
934 "Error parsing server name (%s): %s",
935 (conf->krb_service_name) ? conf->krb_service_name : SERVICE_NAME,
936 krb5_get_err_text(kcontext, ret));
937 ret = HTTP_UNAUTHORIZED;
941 code = krb5_unparse_name(kcontext, server, &name);
943 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
944 "krb5_unparse_name() failed: %s",
945 krb5_get_err_text(kcontext, code));
946 ret = HTTP_UNAUTHORIZED;
949 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "Using %s as server principal for password verification", name);
953 p = strchr(sent_name, '@');
956 if (conf->krb_auth_realms && !ap_find_token(r->pool, conf->krb_auth_realms, p)) {
957 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
958 "Specified realm `%s' not allowed by configuration", p);
959 ret = HTTP_UNAUTHORIZED;
964 realms = (p) ? p : conf->krb_auth_realms;
965 all_principals_unkown = 1;
967 name = (char *) sent_name;
968 if (realms && (realm = ap_getword_white(r->pool, &realms)))
969 name = apr_psprintf(r->pool, "%s@%s", sent_name, realm);
972 krb5_free_principal(kcontext, client);
976 code = krb5_parse_name(kcontext, name, &client);
978 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
979 "krb5_parse_name() failed: %s",
980 krb5_get_err_text(kcontext, code));
984 code = verify_krb5_user(r, kcontext, client, sent_pw,
985 server, keytab, conf->krb_verify_kdc, &ccache);
986 if (!conf->krb_authoritative && code) {
987 /* if we're not authoritative, we allow authentication to pass on
988 * to another modules if (and only if) the user is not known to us */
989 if (all_principals_unkown && code != KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
990 all_principals_unkown = 0;
996 /* ap_getword_white() used above shifts the parameter, so it's not
997 needed to touch the realms variable */
998 } while (realms && *realms);
1000 memset((char *)sent_pw, 0, strlen(sent_pw));
1003 if (!conf->krb_authoritative && all_principals_unkown == 1 && code == KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
1006 ret = HTTP_UNAUTHORIZED;
1011 code = krb5_unparse_name(kcontext, client, &name);
1013 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "krb5_unparse_name() failed: %s",
1014 krb5_get_err_text(kcontext, code));
1015 ret = HTTP_UNAUTHORIZED;
1018 MK_USER = apr_pstrdup (r->pool, name);
1019 MK_AUTH_TYPE = "Basic";
1022 if (conf->krb_save_credentials)
1023 store_krb5_creds(kcontext, r, conf, ccache);
1025 if (conf->krb5_do_auth_to_local) {
1026 krb5_aname_to_localname(kcontext, client, AN_TO_LN_BUFFSIZE_MAX, MK_USER);
1031 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1032 "kerb_authenticate_user_krb5pwd ret=%d user=%s authtype=%s",
1033 ret, (MK_USER)?MK_USER:"(NULL)", (MK_AUTH_TYPE)?MK_AUTH_TYPE:"(NULL)");
1035 krb5_free_principal(kcontext, client);
1037 krb5_free_principal(kcontext, server);
1039 krb5_cc_destroy(kcontext, ccache);
1041 krb5_kt_close(kcontext, keytab);
1042 krb5_free_context(kcontext);
1047 /*********************************************************************
1048 * GSSAPI Authentication
1049 ********************************************************************/
1052 get_gss_error(request_rec *r, OM_uint32 err_maj, OM_uint32 err_min, char *prefix)
1054 OM_uint32 maj_stat, min_stat;
1055 OM_uint32 msg_ctx = 0;
1056 gss_buffer_desc status_string;
1059 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1060 "GSS-API major_status:%8.8x, minor_status:%8.8x",
1063 err_msg = apr_pstrdup(r->pool, prefix);
1065 maj_stat = gss_display_status (&min_stat,
1071 if (!GSS_ERROR(maj_stat)) {
1072 err_msg = apr_pstrcat(r->pool, err_msg, ": ",
1073 (char*) status_string.value, NULL);
1074 gss_release_buffer(&min_stat, &status_string);
1076 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
1079 err_msg = apr_pstrcat(r->pool, err_msg, " (", NULL);
1081 maj_stat = gss_display_status (&min_stat,
1087 if (!GSS_ERROR(maj_stat)) {
1088 err_msg = apr_pstrcat(r->pool, err_msg, ", ",
1089 (char *) status_string.value, NULL);
1090 gss_release_buffer(&min_stat, &status_string);
1092 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
1093 err_msg = apr_pstrcat(r->pool, err_msg, ")", NULL);
1099 store_gss_creds(request_rec *r, kerb_auth_config *conf, char *princ_name,
1100 gss_cred_id_t delegated_cred)
1102 OM_uint32 maj_stat, min_stat;
1103 krb5_principal princ = NULL;
1104 krb5_ccache ccache = NULL;
1105 krb5_error_code problem;
1106 krb5_context context;
1107 int ret = HTTP_INTERNAL_SERVER_ERROR;
1109 problem = krb5_init_context(&context);
1111 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Cannot initialize krb5 context");
1112 return HTTP_INTERNAL_SERVER_ERROR;
1115 problem = krb5_parse_name(context, princ_name, &princ);
1117 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1118 "Cannot parse delegated username (%s)", krb5_get_err_text(context, problem));
1122 problem = create_krb5_ccache(context, r, conf, princ, &ccache);
1124 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1125 "Cannot create krb5 ccache (%s)", krb5_get_err_text(context, problem));
1129 maj_stat = gss_krb5_copy_ccache(&min_stat, delegated_cred, ccache);
1130 if (GSS_ERROR(maj_stat)) {
1131 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1132 "Cannot store delegated credential (%s)",
1133 get_gss_error(r, maj_stat, min_stat, "gss_krb5_copy_ccache"));
1137 krb5_cc_close(context, ccache);
1143 krb5_free_principal(context, princ);
1145 krb5_cc_destroy(context, ccache);
1146 krb5_free_context(context);
1151 get_gss_creds(request_rec *r,
1152 kerb_auth_config *conf,
1153 gss_cred_id_t *server_creds)
1155 gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
1156 OM_uint32 major_status, minor_status, minor_status2;
1157 gss_name_t server_name = GSS_C_NO_NAME;
1159 int have_server_princ;
1162 have_server_princ = conf->krb_service_name && strchr(conf->krb_service_name, '/') != NULL;
1163 if (have_server_princ)
1164 strncpy(buf, conf->krb_service_name, sizeof(buf));
1166 snprintf(buf, sizeof(buf), "%s@%s",
1167 (conf->krb_service_name) ? conf->krb_service_name : SERVICE_NAME,
1168 ap_get_server_name(r));
1171 token.length = strlen(buf) + 1;
1173 major_status = gss_import_name(&minor_status, &token,
1174 (have_server_princ) ? GSS_KRB5_NT_PRINCIPAL_NAME : GSS_C_NT_HOSTBASED_SERVICE,
1176 memset(&token, 0, sizeof(token));
1177 if (GSS_ERROR(major_status)) {
1178 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1179 "%s", get_gss_error(r, major_status, minor_status,
1180 "gss_import_name() failed"));
1181 return HTTP_INTERNAL_SERVER_ERROR;
1184 major_status = gss_display_name(&minor_status, server_name, &token, NULL);
1185 if (GSS_ERROR(major_status)) {
1186 /* Perhaps we could just ignore this error but it's safer to give up now,
1188 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1189 "%s", get_gss_error(r, major_status, minor_status,
1190 "gss_display_name() failed"));
1191 return HTTP_INTERNAL_SERVER_ERROR;
1194 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "Acquiring creds for %s",
1196 gss_release_buffer(&minor_status, &token);
1198 major_status = gss_acquire_cred(&minor_status, server_name, GSS_C_INDEFINITE,
1199 GSS_C_NO_OID_SET, GSS_C_ACCEPT,
1200 server_creds, NULL, NULL);
1201 gss_release_name(&minor_status2, &server_name);
1202 if (GSS_ERROR(major_status)) {
1203 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1204 "%s", get_gss_error(r, major_status, minor_status,
1205 "gss_acquire_cred() failed"));
1206 return HTTP_INTERNAL_SERVER_ERROR;
1211 * With MIT Kerberos 5 1.3.x the gss_cred_id_t is the same as
1212 * krb5_gss_cred_id_t and krb5_gss_cred_id_rec contains a pointer to
1214 * This allows us to override the replay cache function vector with
1216 * Note that this is a dirty hack to get things working and there may
1217 * well be unknown side-effects.
1220 krb5_gss_cred_id_t gss_creds = (krb5_gss_cred_id_t) *server_creds;
1222 /* First we try to verify we are linked with 1.3.x to prevent from
1223 crashing when linked with 1.4.x */
1224 if (gss_creds && (gss_creds->usage == GSS_C_ACCEPT)) {
1225 if (gss_creds->rcache && gss_creds->rcache->ops &&
1226 gss_creds->rcache->ops->type &&
1227 memcmp(gss_creds->rcache->ops->type, "dfl", 3) == 0)
1228 /* Override the rcache operations */
1229 gss_creds->rcache->ops = &mod_auth_kerb_rc_ops;
1238 cmp_gss_type(gss_buffer_t token, gss_OID oid)
1243 if (token->length == 0)
1244 return GSS_S_DEFECTIVE_TOKEN;
1248 return GSS_S_DEFECTIVE_TOKEN;
1251 if ((len & 0x7f) > 4)
1252 return GSS_S_DEFECTIVE_TOKEN;
1256 return GSS_S_DEFECTIVE_TOKEN;
1258 if (((OM_uint32) *p++) != oid->length)
1259 return GSS_S_DEFECTIVE_TOKEN;
1261 return memcmp(p, oid->elements, oid->length);
1265 authenticate_user_gss(request_rec *r, kerb_auth_config *conf,
1266 const char *auth_line, char **negotiate_ret_value)
1268 OM_uint32 major_status, minor_status, minor_status2;
1269 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
1270 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
1271 const char *auth_param = NULL;
1273 gss_name_t client_name = GSS_C_NO_NAME;
1274 gss_cred_id_t delegated_cred = GSS_C_NO_CREDENTIAL;
1275 OM_uint32 (KRB5_LIB_FUNCTION *accept_sec_token)
1276 (OM_uint32 *, gss_ctx_id_t *, const gss_cred_id_t,
1277 const gss_buffer_t, const gss_channel_bindings_t,
1278 gss_name_t *, gss_OID *, gss_buffer_t, OM_uint32 *,
1279 OM_uint32 *, gss_cred_id_t *);
1280 gss_OID_desc spnego_oid;
1281 gss_ctx_id_t context = GSS_C_NO_CONTEXT;
1282 gss_cred_id_t server_creds = GSS_C_NO_CREDENTIAL;
1283 OM_uint32 ret_flags = 0;
1285 *negotiate_ret_value = "\0";
1287 spnego_oid.length = 6;
1288 spnego_oid.elements = (void *)"\x2b\x06\x01\x05\x05\x02";
1290 if (conf->krb_5_keytab) {
1292 /* we don't use the ap_* calls here, since the string passed to putenv()
1293 * will become part of the enviroment and shouldn't be free()ed by apache
1295 ktname = malloc(strlen("KRB5_KTNAME=") + strlen(conf->krb_5_keytab) + 1);
1296 if (ktname == NULL) {
1297 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "malloc() failed: not enough memory");
1298 ret = HTTP_INTERNAL_SERVER_ERROR;
1301 sprintf(ktname, "KRB5_KTNAME=%s", conf->krb_5_keytab);
1304 /* Seems to be also supported by latest MIT */
1305 gsskrb5_register_acceptor_identity(conf->krb_5_keytab);
1309 ret = get_gss_creds(r, conf, &server_creds);
1313 /* ap_getword() shifts parameter */
1314 auth_param = ap_getword_white(r->pool, &auth_line);
1315 if (auth_param == NULL) {
1316 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1317 "No Authorization parameter in request from client");
1318 ret = HTTP_UNAUTHORIZED;
1322 input_token.length = apr_base64_decode_len(auth_param) + 1;
1323 input_token.value = apr_pcalloc(r->connection->pool, input_token.length);
1324 if (input_token.value == NULL) {
1325 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1326 "ap_pcalloc() failed (not enough memory)");
1327 ret = HTTP_INTERNAL_SERVER_ERROR;
1330 input_token.length = apr_base64_decode(input_token.value, auth_param);
1332 #ifdef GSSAPI_SUPPORTS_SPNEGO
1333 accept_sec_token = gss_accept_sec_context;
1335 accept_sec_token = (cmp_gss_type(&input_token, &spnego_oid) == 0) ?
1336 gss_accept_sec_context_spnego : gss_accept_sec_context;
1339 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "Verifying client data using %s",
1340 (accept_sec_token == gss_accept_sec_context)
1342 : "SPNEGO GSS-API");
1344 major_status = accept_sec_token(&minor_status,
1348 GSS_C_NO_CHANNEL_BINDINGS,
1355 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1356 "Client %s us their credential",
1357 (ret_flags & GSS_C_DELEG_FLAG) ? "sent" : "didn't send");
1358 if (output_token.length) {
1362 len = apr_base64_encode_len(output_token.length) + 1;
1363 token = apr_pcalloc(r->connection->pool, len + 1);
1364 if (token == NULL) {
1365 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1366 "ap_pcalloc() failed (not enough memory)");
1367 ret = HTTP_INTERNAL_SERVER_ERROR;
1368 gss_release_buffer(&minor_status2, &output_token);
1371 apr_base64_encode(token, output_token.value, output_token.length);
1373 *negotiate_ret_value = token;
1374 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1375 "GSS-API token of length %d bytes will be sent back",
1376 output_token.length);
1377 gss_release_buffer(&minor_status2, &output_token);
1378 set_kerb_auth_headers(r, conf, 0, 0, *negotiate_ret_value);
1381 if (GSS_ERROR(major_status)) {
1382 if (input_token.length > 7 && memcmp(input_token.value, "NTLMSSP", 7) == 0)
1383 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1384 "Warning: received token seems to be NTLM, which isn't supported by the Kerberos module. Check your IE configuration.");
1386 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1387 "%s", get_gss_error(r, major_status, minor_status,
1388 "gss_accept_sec_context() failed"));
1389 /* Don't offer the Negotiate method again if call to GSS layer failed */
1390 *negotiate_ret_value = NULL;
1391 ret = HTTP_UNAUTHORIZED;
1396 /* This is a _Kerberos_ module so multiple authentication rounds aren't
1397 * supported. If we wanted a generic GSS authentication we would have to do
1398 * some magic with exporting context etc. */
1399 if (major_status & GSS_S_CONTINUE_NEEDED) {
1400 ret = HTTP_UNAUTHORIZED;
1405 major_status = gss_display_name(&minor_status, client_name, &output_token, NULL);
1406 gss_release_name(&minor_status, &client_name);
1407 if (GSS_ERROR(major_status)) {
1408 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1409 "%s", get_gss_error(r, major_status, minor_status,
1410 "gss_display_name() failed"));
1411 ret = HTTP_INTERNAL_SERVER_ERROR;
1415 MK_AUTH_TYPE = MECH_NEGOTIATE;
1416 MK_USER = apr_pstrdup(r->pool, output_token.value);
1418 if (conf->krb_save_credentials && delegated_cred != GSS_C_NO_CREDENTIAL)
1419 store_gss_creds(r, conf, (char *)output_token.value, delegated_cred);
1421 gss_release_buffer(&minor_status, &output_token);
1427 gss_release_cred(&minor_status, &delegated_cred);
1429 if (output_token.length)
1430 gss_release_buffer(&minor_status, &output_token);
1432 if (client_name != GSS_C_NO_NAME)
1433 gss_release_name(&minor_status, &client_name);
1435 if (server_creds != GSS_C_NO_CREDENTIAL)
1436 gss_release_cred(&minor_status, &server_creds);
1438 if (context != GSS_C_NO_CONTEXT)
1439 gss_delete_sec_context(&minor_status, &context, GSS_C_NO_BUFFER);
1446 already_succeeded(request_rec *r)
1448 if (ap_is_initial_req(r) || MK_AUTH_TYPE == NULL)
1450 if (strcmp(MK_AUTH_TYPE, MECH_NEGOTIATE) ||
1451 (strcmp(MK_AUTH_TYPE, "Basic") && strchr(MK_USER, '@')))
1457 set_kerb_auth_headers(request_rec *r, const kerb_auth_config *conf,
1458 int use_krb4, int use_krb5pwd, char *negotiate_ret_value)
1460 const char *auth_name = NULL;
1462 char *negoauth_param;
1463 const char *header_name =
1464 (r->proxyreq == PROXYREQ_PROXY) ? "Proxy-Authenticate" : "WWW-Authenticate";
1466 /* get the user realm specified in .htaccess */
1467 auth_name = ap_auth_name(r);
1469 /* XXX should the WWW-Authenticate header be cleared first?
1470 * apache in the proxy mode should retain client's authN headers? */
1472 if (negotiate_ret_value != NULL && conf->krb_method_gssapi) {
1473 negoauth_param = (*negotiate_ret_value == '\0') ? MECH_NEGOTIATE :
1474 apr_pstrcat(r->pool, MECH_NEGOTIATE " ", negotiate_ret_value, NULL);
1475 apr_table_add(r->err_headers_out, header_name, negoauth_param);
1477 if ((use_krb5pwd && conf->krb_method_k5pass) || conf->krb_delegate_basic) {
1478 apr_table_add(r->err_headers_out, header_name,
1479 apr_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1486 ((use_krb4 && conf->krb_method_k4pass) || conf->krb_delegate_basic))
1487 apr_table_add(r->err_headers_out, header_name,
1488 apr_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1493 kerb_authenticate_user(request_rec *r)
1495 kerb_auth_config *conf =
1496 (kerb_auth_config *) ap_get_module_config(r->per_dir_config,
1498 const char *auth_type = NULL;
1499 const char *auth_line = NULL;
1500 const char *type = NULL;
1501 int use_krb5 = 0, use_krb4 = 0;
1503 static int last_return = HTTP_UNAUTHORIZED;
1504 char *negotiate_ret_value = NULL;
1506 /* get the type specified in .htaccess */
1507 type = ap_auth_type(r);
1509 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1510 "kerb_authenticate_user entered with user %s and auth_type %s",
1511 (MK_USER)?MK_USER:"(NULL)",type?type:"(NULL)");
1513 if (type && strcasecmp(type, "Kerberos") == 0)
1514 use_krb5 = use_krb4 = 1;
1515 else if(type && strcasecmp(type, "KerberosV5") == 0)
1517 else if(type && strcasecmp(type, "KerberosV4") == 0)
1523 if (conf->krb_ssl_preauthentication) {
1524 const char *ssl_client_verify = ssl_var_lookup(r->pool, r->server,
1525 r->connection, r, "SSL_CLIENT_VERIFY");
1527 if (ssl_client_verify && strcmp(ssl_client_verify, "SUCCESS") == 0)
1532 /* get what the user sent us in the HTTP header */
1533 auth_line = MK_TABLE_GET(r->headers_in, (r->proxyreq == PROXYREQ_PROXY)
1534 ? "Proxy-Authorization"
1537 set_kerb_auth_headers(r, conf, use_krb4, use_krb5,
1538 (use_krb5) ? "\0" : NULL);
1539 return HTTP_UNAUTHORIZED;
1541 auth_type = ap_getword_white(r->pool, &auth_line);
1543 /* If we are delegating Basic to other modules, DECLINE the request */
1544 if (conf->krb_delegate_basic &&
1546 !conf->krb_method_k5pass &&
1549 !conf->krb_method_k4pass &&
1551 (strcasecmp(auth_type, "Basic") == 0))
1554 if (already_succeeded(r))
1557 ret = HTTP_UNAUTHORIZED;
1560 if (use_krb5 && conf->krb_method_gssapi &&
1561 strcasecmp(auth_type, MECH_NEGOTIATE) == 0) {
1562 ret = authenticate_user_gss(r, conf, auth_line, &negotiate_ret_value);
1563 } else if (use_krb5 && conf->krb_method_k5pass &&
1564 strcasecmp(auth_type, "Basic") == 0) {
1565 ret = authenticate_user_krb5pwd(r, conf, auth_line);
1570 if (ret == HTTP_UNAUTHORIZED && use_krb4 && conf->krb_method_k4pass &&
1571 strcasecmp(auth_type, "Basic") == 0)
1572 ret = authenticate_user_krb4pwd(r, conf, auth_line);
1575 if (ret == HTTP_UNAUTHORIZED)
1576 set_kerb_auth_headers(r, conf, use_krb4, use_krb5, negotiate_ret_value);
1578 /* XXX log_debug: if ret==OK, log(user XY authenticated) */
1585 have_rcache_type(const char *type)
1587 krb5_error_code ret;
1588 krb5_context context;
1589 krb5_rcache id = NULL;
1592 ret = krb5_init_context(&context);
1596 ret = krb5_rc_resolve_full(context, &id, "none:");
1600 krb5_rc_destroy(context, id);
1601 krb5_free_context(context);
1606 /***************************************************************************
1607 Module Setup/Configuration
1608 ***************************************************************************/
1609 #ifndef STANDARD20_MODULE_STUFF
1611 kerb_module_init(server_rec *dummy, pool *p)
1614 /* Suppress the MIT replay cache. Requires MIT Kerberos 1.4.0 or later.
1615 1.3.x are covered by the hack overiding the replay calls */
1616 if (getenv("KRB5RCACHETYPE") == NULL && have_rcache_type("none"))
1617 putenv(strdup("KRB5RCACHETYPE=none"));
1621 module MODULE_VAR_EXPORT auth_kerb_module = {
1622 STANDARD_MODULE_STUFF,
1623 kerb_module_init, /* module initializer */
1624 kerb_dir_create_config, /* per-directory config creator */
1625 NULL, /* per-directory config merger */
1626 NULL, /* per-server config creator */
1627 NULL, /* per-server config merger */
1628 kerb_auth_cmds, /* command table */
1629 NULL, /* [ 9] content handlers */
1630 NULL, /* [ 2] URI-to-filename translation */
1631 kerb_authenticate_user, /* [ 5] check/validate user_id */
1632 NULL, /* [ 6] check user_id is valid *here* */
1633 NULL, /* [ 4] check access by host address */
1634 NULL, /* [ 7] MIME type checker/setter */
1635 NULL, /* [ 8] fixups */
1636 NULL, /* [10] logger */
1637 NULL, /* [ 3] header parser */
1638 NULL, /* process initialization */
1639 NULL, /* process exit/cleanup */
1640 NULL /* [ 1] post read_request handling */
1642 ,NULL, /* EAPI: add_module */
1643 NULL, /* EAPI: remove_module */
1644 NULL, /* EAPI: rewrite_command */
1645 NULL /* EAPI: new_connection */
1650 kerb_init_handler(apr_pool_t *p, apr_pool_t *plog,
1651 apr_pool_t *ptemp, server_rec *s)
1653 ap_add_version_component(p, "mod_auth_kerb/" MODAUTHKERB_VERSION);
1655 /* Suppress the MIT replay cache. Requires MIT Kerberos 1.4.0 or later.
1656 1.3.x are covered by the hack overiding the replay calls */
1657 if (getenv("KRB5RCACHETYPE") == NULL && have_rcache_type("none"))
1658 putenv(strdup("KRB5RCACHETYPE=none"));
1665 kerb_register_hooks(apr_pool_t *p)
1667 ap_hook_post_config(kerb_init_handler, NULL, NULL, APR_HOOK_MIDDLE);
1668 ap_hook_check_user_id(kerb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
1671 module AP_MODULE_DECLARE_DATA auth_kerb_module =
1673 STANDARD20_MODULE_STUFF,
1674 kerb_dir_create_config, /* create per-dir conf structures */
1675 NULL, /* merge per-dir conf structures */
1676 NULL, /* create per-server conf structures */
1677 NULL, /* merge per-server conf structures */
1678 kerb_auth_cmds, /* table of configuration directives */
1679 kerb_register_hooks /* register hooks */