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("KrbLocalUserMapping", 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 Auth Configuration Initialization
312 ***************************************************************************/
313 static void *kerb_dir_create_config(MK_POOL *p, char *d)
315 kerb_auth_config *rec;
317 rec = (kerb_auth_config *) apr_pcalloc(p, sizeof(kerb_auth_config));
318 ((kerb_auth_config *)rec)->krb_verify_kdc = 1;
319 ((kerb_auth_config *)rec)->krb_service_name = NULL;
320 ((kerb_auth_config *)rec)->krb_authoritative = 1;
321 ((kerb_auth_config *)rec)->krb_delegate_basic = 0;
323 ((kerb_auth_config *)rec)->krb_ssl_preauthentication = 0;
326 ((kerb_auth_config *)rec)->krb5_do_auth_to_local = 0;
327 ((kerb_auth_config *)rec)->krb_method_k5pass = 1;
328 ((kerb_auth_config *)rec)->krb_method_gssapi = 1;
331 ((kerb_auth_config *)rec)->krb_method_k4pass = 1;
337 krb5_save_realms(cmd_parms *cmd, void *vsec, const char *arg)
339 kerb_auth_config *sec = (kerb_auth_config *) vsec;
340 sec->krb_auth_realms= apr_pstrdup(cmd->pool, arg);
345 log_rerror(const char *file, int line, int level, int status,
346 const request_rec *r, const char *fmt, ...)
352 vsnprintf(errstr, sizeof(errstr), fmt, ap);
356 #ifdef STANDARD20_MODULE_STUFF
357 ap_log_rerror(file, line, level | APLOG_NOERRNO, status, r, "%s", errstr);
359 ap_log_rerror(file, line, level | APLOG_NOERRNO, r, "%s", errstr);
364 /***************************************************************************
365 Username/Password Validation for Krb4
366 ***************************************************************************/
368 verify_krb4_user(request_rec *r, const char *name, const char *instance,
369 const char *realm, const char *password, const char *linstance, const char *srvtab, int krb_verify_kdc)
375 const char *hostname;
378 char lrealm[REALM_SZ];
380 ret = krb_get_pw_in_tkt(name, instance, realm, "krbtgt", realm,
381 DEFAULT_TKT_LIFE, password);
383 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
384 "Cannot get krb4 ticket: krb_get_pw_in_tkt() failed: %s",
385 krb_get_err_text(ret));
392 hostname = ap_get_server_name(r);
394 hp = gethostbyname(hostname);
397 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
398 "Cannot verify krb4 ticket: gethostbyname() failed: %s",
402 memcpy(&addr, hp->h_addr, sizeof(addr));
404 phost = krb_get_phost((char *)hostname);
406 krb_get_lrealm(lrealm, 1);
408 ret = krb_mk_req(&ticket, linstance, phost, lrealm, 0);
410 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
411 "Cannot verify krb4 ticket: krb_mk_req() failed: %s",
412 krb_get_err_text(ret));
417 ret = krb_rd_req(&ticket, (char *)linstance, phost, addr, &authdata, (char *)srvtab);
419 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
420 "Cannot verify krb4 ticket: krb_rd_req() failed: %s",
421 krb_get_err_text(ret));
429 krb4_cache_cleanup(void *data)
431 char *tkt_file = (char *) data;
433 krb_set_tkt_string(tkt_file);
439 authenticate_user_krb4pwd(request_rec *r,
440 kerb_auth_config *conf,
441 const char *auth_line)
445 const char *sent_name;
448 char *tkt_file_p = NULL;
453 char lrealm[REALM_SZ];
454 int all_principals_unkown;
456 sent_pw = ap_pbase64decode(r->pool, auth_line);
457 sent_name = ap_getword_nulls_nc (r->pool, (char **) &sent_pw, ':');
459 sent_instance = strchr(sent_name, '.');
461 *sent_instance++ = '\0';
463 snprintf(tkt_file, sizeof(tkt_file), "/tmp/apache_tkt_XXXXXX");
464 fd = mkstemp(tkt_file);
466 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
467 "Cannot create krb4 ccache: mkstemp() failed: %s",
469 return HTTP_INTERNAL_SERVER_ERROR;
472 tkt_file_p = apr_pstrdup(r->pool, tkt_file);
473 apr_pool_cleanup_register(r->pool, tkt_file_p, krb4_cache_cleanup,
474 apr_pool_cleanup_null);
476 krb_set_tkt_string(tkt_file);
478 all_principals_unkown = 1;
479 realms = conf->krb_auth_realms;
481 memset(lrealm, 0, sizeof(lrealm));
484 realm = ap_getword_white(r->pool, &realms);
487 ret = krb_get_lrealm(lrealm, 1);
493 /* XXX conf->krb_service_name */
494 ret = verify_krb4_user(r, (char *)sent_name,
495 (sent_instance) ? sent_instance : "",
496 (char *)realm, (char *)sent_pw,
497 conf->krb_service_name,
498 conf->krb_4_srvtab, conf->krb_verify_kdc);
499 if (!conf->krb_authoritative && ret) {
500 /* if we're not authoritative, we allow authentication to pass on
501 * to another modules if (and only if) the user is not known to us */
502 if (all_principals_unkown && ret != KDC_PR_UNKNOWN)
503 all_principals_unkown = 0;
508 } while (realms && *realms);
511 /* XXX log only in the verify_krb4_user() call */
512 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Verifying krb4 password failed");
513 ret = (!conf->krb_authoritative && all_principals_unkown == 1 && ret == KDC_PR_UNKNOWN) ?
514 DECLINED : HTTP_UNAUTHORIZED;
518 user = apr_pstrdup(r->pool, sent_name);
520 user = apr_pstrcat(r->pool, user, ".", sent_instance, NULL);
521 user = apr_pstrcat(r->pool, user, "@", realm, NULL);
524 MK_AUTH_TYPE = "Basic";
525 apr_table_setn(r->subprocess_env, "KRBTKFILE", tkt_file_p);
527 if (!conf->krb_save_credentials)
528 krb4_cache_cleanup(tkt_file);
532 krb4_cache_cleanup(tkt_file);
541 /***************************************************************************
542 Username/Password Validation for Krb5
543 ***************************************************************************/
545 /* MIT kerberos uses replay cache checks even during credential verification
546 * (i.e. in krb5_verify_init_creds()), which is obviosuly useless. In order to
547 * avoid problems with multiple apache processes accessing the same rcache file
548 * we had to use this call instead, which is only a bit modified version of
549 * krb5_verify_init_creds() */
550 static krb5_error_code
551 verify_krb5_init_creds(request_rec *r, krb5_context context, krb5_creds *creds,
552 krb5_principal ap_req_server, krb5_keytab ap_req_keytab)
556 krb5_ccache local_ccache = NULL;
557 krb5_creds *new_creds = NULL;
558 krb5_auth_context auth_context = NULL;
559 krb5_keytab keytab = NULL;
562 memset(&req, 0, sizeof(req));
564 if (ap_req_keytab == NULL) {
565 ret = krb5_kt_default (context, &keytab);
569 keytab = ap_req_keytab;
571 #ifdef HAVE_KRB5_CC_NEW_UNIQUE
572 ret = krb5_cc_new_unique(context, "MEMORY", NULL, &local_ccache);
574 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 #ifdef HAVE_KRB5_CC_NEW_UNIQUE
724 ret = krb5_cc_new_unique(context, "MEMORY", NULL, &ret_ccache);
726 ret = krb5_cc_resolve(context, "MEMORY:", &ret_ccache);
730 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
731 "generating new memory ccache failed: %s",
732 krb5_get_err_text(context, ret));
736 ret = krb5_cc_initialize(context, ret_ccache, principal);
738 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
739 "krb5_cc_initialize() failed: %s",
740 krb5_get_err_text(context, ret));
744 ret = krb5_cc_store_cred(context, ret_ccache, &creds);
746 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
747 "krb5_cc_store_cred() failed: %s",
748 krb5_get_err_text(context, ret));
751 *ccache = ret_ccache;
755 krb5_free_cred_contents(context, &creds);
757 krb5_cc_destroy(context, ret_ccache);
763 krb5_cache_cleanup(void *data)
765 krb5_context context;
767 krb5_error_code problem;
768 char *cache_name = (char *) data;
770 problem = krb5_init_context(&context);
772 /* ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "krb5_init_context() failed"); */
773 return HTTP_INTERNAL_SERVER_ERROR;
776 problem = krb5_cc_resolve(context, cache_name, &cache);
778 /* log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
779 "krb5_cc_resolve() failed (%s: %s)",
780 cache_name, krb5_get_err_text(context, problem)); */
781 return HTTP_INTERNAL_SERVER_ERROR;
784 krb5_cc_destroy(context, cache);
785 krb5_free_context(context);
790 create_krb5_ccache(krb5_context kcontext,
792 kerb_auth_config *conf,
793 krb5_principal princ,
798 krb5_error_code problem;
800 krb5_ccache tmp_ccache = NULL;
802 ccname = apr_psprintf(r->pool, "FILE:%s/krb5cc_apache_XXXXXX", P_tmpdir);
803 fd = mkstemp(ccname + strlen("FILE:"));
805 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
806 "mkstemp() failed: %s", strerror(errno));
807 ret = HTTP_INTERNAL_SERVER_ERROR;
812 problem = krb5_cc_resolve(kcontext, ccname, &tmp_ccache);
814 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
815 "krb5_cc_resolve() failed: %s",
816 krb5_get_err_text(kcontext, problem));
817 ret = HTTP_INTERNAL_SERVER_ERROR;
822 problem = krb5_cc_initialize(kcontext, tmp_ccache, princ);
824 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
825 "Cannot initialize krb5 ccache %s: krb5_cc_initialize() failed: %s",
826 ccname, krb5_get_err_text(kcontext, problem));
827 ret = HTTP_INTERNAL_SERVER_ERROR;
831 apr_table_setn(r->subprocess_env, "KRB5CCNAME", ccname);
832 apr_pool_cleanup_register(r->pool, ccname, krb5_cache_cleanup,
833 apr_pool_cleanup_null);
835 *ccache = tmp_ccache;
842 krb5_cc_destroy(kcontext, tmp_ccache);
848 store_krb5_creds(krb5_context kcontext,
850 kerb_auth_config *conf,
851 krb5_ccache delegated_cred)
854 krb5_error_code problem;
855 krb5_principal princ;
859 problem = krb5_cc_get_principal(kcontext, delegated_cred, &princ);
861 snprintf(errstr, sizeof(errstr), "krb5_cc_get_principal() failed: %s",
862 krb5_get_err_text(kcontext, problem));
863 return HTTP_INTERNAL_SERVER_ERROR;
866 ret = create_krb5_ccache(kcontext, r, conf, princ, &ccache);
868 krb5_free_principal(kcontext, princ);
873 problem = krb5_cc_copy_cache(kcontext, delegated_cred, ccache);
875 problem = krb5_cc_copy_creds(kcontext, delegated_cred, ccache);
877 krb5_free_principal(kcontext, princ);
879 snprintf(errstr, sizeof(errstr), "Failed to store credentials: %s",
880 krb5_get_err_text(kcontext, problem));
881 krb5_cc_destroy(kcontext, ccache);
882 return HTTP_INTERNAL_SERVER_ERROR;
885 krb5_cc_close(kcontext, ccache);
891 authenticate_user_krb5pwd(request_rec *r,
892 kerb_auth_config *conf,
893 const char *auth_line)
895 const char *sent_pw = NULL;
896 const char *sent_name = NULL;
897 const char *realms = NULL;
898 const char *realm = NULL;
899 krb5_context kcontext = NULL;
900 krb5_error_code code;
901 krb5_principal client = NULL;
902 krb5_principal server = NULL;
903 krb5_ccache ccache = NULL;
904 krb5_keytab keytab = NULL;
907 int all_principals_unkown;
910 //temporary fix for KrbServiceName Any, use default SERVICE_NAME
911 if (conf->krb_service_name && strcmp(conf->krb_service_name,"Any") == 0)
912 snprintf(conf->krb_service_name, 5,"%s",SERVICE_NAME);
914 code = krb5_init_context(&kcontext);
916 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
917 "Cannot initialize Kerberos5 context (%d)", code);
918 return HTTP_INTERNAL_SERVER_ERROR;
921 sent_pw = ap_pbase64decode(r->pool, auth_line);
922 sent_name = ap_getword_nulls_nc (r->pool, (char **) &sent_pw, ':');
924 if (sent_pw == NULL || *sent_pw == '\0') {
925 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
926 "empty passwords are not accepted");
927 ret = HTTP_UNAUTHORIZED;
931 if (conf->krb_5_keytab)
932 krb5_kt_resolve(kcontext, conf->krb_5_keytab, &keytab);
934 if (conf->krb_service_name && strchr(conf->krb_service_name, '/') != NULL)
935 ret = krb5_parse_name (kcontext, conf->krb_service_name, &server);
937 ret = krb5_sname_to_principal(kcontext, ap_get_server_name(r),
938 (conf->krb_service_name) ? conf->krb_service_name : SERVICE_NAME,
939 KRB5_NT_SRV_HST, &server);
942 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
943 "Error parsing server name (%s): %s",
944 (conf->krb_service_name) ? conf->krb_service_name : SERVICE_NAME,
945 krb5_get_err_text(kcontext, ret));
946 ret = HTTP_UNAUTHORIZED;
950 code = krb5_unparse_name(kcontext, server, &name);
952 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
953 "krb5_unparse_name() failed: %s",
954 krb5_get_err_text(kcontext, code));
955 ret = HTTP_UNAUTHORIZED;
958 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "Using %s as server principal for password verification", name);
962 p = strchr(sent_name, '@');
965 if (conf->krb_auth_realms && !ap_find_token(r->pool, conf->krb_auth_realms, p)) {
966 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
967 "Specified realm `%s' not allowed by configuration", p);
968 ret = HTTP_UNAUTHORIZED;
973 realms = (p) ? p : conf->krb_auth_realms;
974 all_principals_unkown = 1;
976 name = (char *) sent_name;
977 if (realms && (realm = ap_getword_white(r->pool, &realms)))
978 name = apr_psprintf(r->pool, "%s@%s", sent_name, realm);
981 krb5_free_principal(kcontext, client);
985 code = krb5_parse_name(kcontext, name, &client);
987 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
988 "krb5_parse_name() failed: %s",
989 krb5_get_err_text(kcontext, code));
993 code = verify_krb5_user(r, kcontext, client, sent_pw,
994 server, keytab, conf->krb_verify_kdc, &ccache);
995 if (!conf->krb_authoritative && code) {
996 /* if we're not authoritative, we allow authentication to pass on
997 * to another modules if (and only if) the user is not known to us */
998 if (all_principals_unkown && code != KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
999 all_principals_unkown = 0;
1005 /* ap_getword_white() used above shifts the parameter, so it's not
1006 needed to touch the realms variable */
1007 } while (realms && *realms);
1009 memset((char *)sent_pw, 0, strlen(sent_pw));
1012 if (!conf->krb_authoritative && all_principals_unkown == 1 && code == KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
1015 ret = HTTP_UNAUTHORIZED;
1020 code = krb5_unparse_name(kcontext, client, &name);
1022 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "krb5_unparse_name() failed: %s",
1023 krb5_get_err_text(kcontext, code));
1024 ret = HTTP_UNAUTHORIZED;
1027 MK_USER = apr_pstrdup (r->pool, name);
1028 MK_AUTH_TYPE = "Basic";
1031 if (conf->krb_save_credentials)
1032 store_krb5_creds(kcontext, r, conf, ccache);
1037 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1038 "kerb_authenticate_user_krb5pwd ret=%d user=%s authtype=%s",
1039 ret, (MK_USER)?MK_USER:"(NULL)", (MK_AUTH_TYPE)?MK_AUTH_TYPE:"(NULL)");
1041 krb5_free_principal(kcontext, client);
1043 krb5_free_principal(kcontext, server);
1045 krb5_cc_destroy(kcontext, ccache);
1047 krb5_kt_close(kcontext, keytab);
1048 krb5_free_context(kcontext);
1053 /*********************************************************************
1054 * GSSAPI Authentication
1055 ********************************************************************/
1058 get_gss_error(request_rec *r, OM_uint32 err_maj, OM_uint32 err_min, char *prefix)
1060 OM_uint32 maj_stat, min_stat;
1061 OM_uint32 msg_ctx = 0;
1062 gss_buffer_desc status_string;
1065 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1066 "GSS-API major_status:%8.8x, minor_status:%8.8x",
1069 err_msg = apr_pstrdup(r->pool, prefix);
1071 maj_stat = gss_display_status (&min_stat,
1077 if (!GSS_ERROR(maj_stat)) {
1078 err_msg = apr_pstrcat(r->pool, err_msg, ": ",
1079 (char*) status_string.value, NULL);
1080 gss_release_buffer(&min_stat, &status_string);
1082 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
1085 err_msg = apr_pstrcat(r->pool, err_msg, " (", NULL);
1087 maj_stat = gss_display_status (&min_stat,
1093 if (!GSS_ERROR(maj_stat)) {
1094 err_msg = apr_pstrcat(r->pool, err_msg, ", ",
1095 (char *) status_string.value, NULL);
1096 gss_release_buffer(&min_stat, &status_string);
1098 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
1099 err_msg = apr_pstrcat(r->pool, err_msg, ")", NULL);
1105 store_gss_creds(request_rec *r, kerb_auth_config *conf, char *princ_name,
1106 gss_cred_id_t delegated_cred)
1108 OM_uint32 maj_stat, min_stat;
1109 krb5_principal princ = NULL;
1110 krb5_ccache ccache = NULL;
1111 krb5_error_code problem;
1112 krb5_context context;
1113 int ret = HTTP_INTERNAL_SERVER_ERROR;
1115 problem = krb5_init_context(&context);
1117 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Cannot initialize krb5 context");
1118 return HTTP_INTERNAL_SERVER_ERROR;
1121 problem = krb5_parse_name(context, princ_name, &princ);
1123 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1124 "Cannot parse delegated username (%s)", krb5_get_err_text(context, problem));
1128 problem = create_krb5_ccache(context, r, conf, princ, &ccache);
1130 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1131 "Cannot create krb5 ccache (%s)", krb5_get_err_text(context, problem));
1135 maj_stat = gss_krb5_copy_ccache(&min_stat, delegated_cred, ccache);
1136 if (GSS_ERROR(maj_stat)) {
1137 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1138 "Cannot store delegated credential (%s)",
1139 get_gss_error(r, maj_stat, min_stat, "gss_krb5_copy_ccache"));
1143 krb5_cc_close(context, ccache);
1149 krb5_free_principal(context, princ);
1151 krb5_cc_destroy(context, ccache);
1152 krb5_free_context(context);
1157 get_gss_creds(request_rec *r,
1158 kerb_auth_config *conf,
1159 gss_cred_id_t *server_creds)
1161 gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
1162 OM_uint32 major_status, minor_status, minor_status2;
1163 gss_name_t server_name = GSS_C_NO_NAME;
1165 int have_server_princ;
1168 have_server_princ = conf->krb_service_name && strchr(conf->krb_service_name, '/') != NULL;
1169 if (have_server_princ)
1170 strncpy(buf, conf->krb_service_name, sizeof(buf));
1171 else if (conf->krb_service_name && strcmp(conf->krb_service_name,"Any") == 0) {
1172 *server_creds = GSS_C_NO_CREDENTIAL;
1176 snprintf(buf, sizeof(buf), "%s@%s",
1177 (conf->krb_service_name) ? conf->krb_service_name : SERVICE_NAME,
1178 ap_get_server_name(r));
1181 token.length = strlen(buf) + 1;
1183 major_status = gss_import_name(&minor_status, &token,
1184 (have_server_princ) ? GSS_KRB5_NT_PRINCIPAL_NAME : GSS_C_NT_HOSTBASED_SERVICE,
1186 memset(&token, 0, sizeof(token));
1187 if (GSS_ERROR(major_status)) {
1188 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1189 "%s", get_gss_error(r, major_status, minor_status,
1190 "gss_import_name() failed"));
1191 return HTTP_INTERNAL_SERVER_ERROR;
1194 major_status = gss_display_name(&minor_status, server_name, &token, NULL);
1195 if (GSS_ERROR(major_status)) {
1196 /* Perhaps we could just ignore this error but it's safer to give up now,
1198 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1199 "%s", get_gss_error(r, major_status, minor_status,
1200 "gss_display_name() failed"));
1201 return HTTP_INTERNAL_SERVER_ERROR;
1204 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "Acquiring creds for %s",
1206 gss_release_buffer(&minor_status, &token);
1208 major_status = gss_acquire_cred(&minor_status, server_name, GSS_C_INDEFINITE,
1209 GSS_C_NO_OID_SET, GSS_C_ACCEPT,
1210 server_creds, NULL, NULL);
1211 gss_release_name(&minor_status2, &server_name);
1212 if (GSS_ERROR(major_status)) {
1213 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1214 "%s", get_gss_error(r, major_status, minor_status,
1215 "gss_acquire_cred() failed"));
1216 return HTTP_INTERNAL_SERVER_ERROR;
1221 * With MIT Kerberos 5 1.3.x the gss_cred_id_t is the same as
1222 * krb5_gss_cred_id_t and krb5_gss_cred_id_rec contains a pointer to
1224 * This allows us to override the replay cache function vector with
1226 * Note that this is a dirty hack to get things working and there may
1227 * well be unknown side-effects.
1230 krb5_gss_cred_id_t gss_creds = (krb5_gss_cred_id_t) *server_creds;
1232 /* First we try to verify we are linked with 1.3.x to prevent from
1233 crashing when linked with 1.4.x */
1234 if (gss_creds && (gss_creds->usage == GSS_C_ACCEPT)) {
1235 if (gss_creds->rcache && gss_creds->rcache->ops &&
1236 gss_creds->rcache->ops->type &&
1237 memcmp(gss_creds->rcache->ops->type, "dfl", 3) == 0)
1238 /* Override the rcache operations */
1239 gss_creds->rcache->ops = &mod_auth_kerb_rc_ops;
1248 cmp_gss_type(gss_buffer_t token, gss_OID oid)
1253 if (token->length == 0)
1254 return GSS_S_DEFECTIVE_TOKEN;
1258 return GSS_S_DEFECTIVE_TOKEN;
1261 if ((len & 0x7f) > 4)
1262 return GSS_S_DEFECTIVE_TOKEN;
1266 return GSS_S_DEFECTIVE_TOKEN;
1268 if (((OM_uint32) *p++) != oid->length)
1269 return GSS_S_DEFECTIVE_TOKEN;
1271 return memcmp(p, oid->elements, oid->length);
1275 authenticate_user_gss(request_rec *r, kerb_auth_config *conf,
1276 const char *auth_line, char **negotiate_ret_value)
1278 OM_uint32 major_status, minor_status, minor_status2;
1279 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
1280 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
1281 const char *auth_param = NULL;
1283 gss_name_t client_name = GSS_C_NO_NAME;
1284 gss_cred_id_t delegated_cred = GSS_C_NO_CREDENTIAL;
1285 OM_uint32 (KRB5_LIB_FUNCTION *accept_sec_token)
1286 (OM_uint32 *, gss_ctx_id_t *, const gss_cred_id_t,
1287 const gss_buffer_t, const gss_channel_bindings_t,
1288 gss_name_t *, gss_OID *, gss_buffer_t, OM_uint32 *,
1289 OM_uint32 *, gss_cred_id_t *);
1290 gss_OID_desc spnego_oid;
1291 gss_ctx_id_t context = GSS_C_NO_CONTEXT;
1292 gss_cred_id_t server_creds = GSS_C_NO_CREDENTIAL;
1293 OM_uint32 ret_flags = 0;
1295 *negotiate_ret_value = "\0";
1297 spnego_oid.length = 6;
1298 spnego_oid.elements = (void *)"\x2b\x06\x01\x05\x05\x02";
1300 if (conf->krb_5_keytab) {
1302 /* we don't use the ap_* calls here, since the string passed to putenv()
1303 * will become part of the enviroment and shouldn't be free()ed by apache
1305 ktname = malloc(strlen("KRB5_KTNAME=") + strlen(conf->krb_5_keytab) + 1);
1306 if (ktname == NULL) {
1307 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "malloc() failed: not enough memory");
1308 ret = HTTP_INTERNAL_SERVER_ERROR;
1311 sprintf(ktname, "KRB5_KTNAME=%s", conf->krb_5_keytab);
1314 /* Seems to be also supported by latest MIT */
1315 gsskrb5_register_acceptor_identity(conf->krb_5_keytab);
1319 ret = get_gss_creds(r, conf, &server_creds);
1323 /* ap_getword() shifts parameter */
1324 auth_param = ap_getword_white(r->pool, &auth_line);
1325 if (auth_param == NULL) {
1326 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1327 "No Authorization parameter in request from client");
1328 ret = HTTP_UNAUTHORIZED;
1332 input_token.length = apr_base64_decode_len(auth_param) + 1;
1333 input_token.value = apr_pcalloc(r->connection->pool, input_token.length);
1334 if (input_token.value == NULL) {
1335 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1336 "ap_pcalloc() failed (not enough memory)");
1337 ret = HTTP_INTERNAL_SERVER_ERROR;
1340 input_token.length = apr_base64_decode(input_token.value, auth_param);
1342 #ifdef GSSAPI_SUPPORTS_SPNEGO
1343 accept_sec_token = gss_accept_sec_context;
1345 accept_sec_token = (cmp_gss_type(&input_token, &spnego_oid) == 0) ?
1346 gss_accept_sec_context_spnego : gss_accept_sec_context;
1349 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "Verifying client data using %s",
1350 (accept_sec_token == gss_accept_sec_context)
1352 : "SPNEGO GSS-API");
1354 major_status = accept_sec_token(&minor_status,
1358 GSS_C_NO_CHANNEL_BINDINGS,
1365 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1366 "Client %s us their credential",
1367 (ret_flags & GSS_C_DELEG_FLAG) ? "sent" : "didn't send");
1368 if (output_token.length) {
1372 len = apr_base64_encode_len(output_token.length) + 1;
1373 token = apr_pcalloc(r->connection->pool, len + 1);
1374 if (token == NULL) {
1375 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1376 "ap_pcalloc() failed (not enough memory)");
1377 ret = HTTP_INTERNAL_SERVER_ERROR;
1378 gss_release_buffer(&minor_status2, &output_token);
1381 apr_base64_encode(token, output_token.value, output_token.length);
1383 *negotiate_ret_value = token;
1384 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1385 "GSS-API token of length %d bytes will be sent back",
1386 output_token.length);
1387 gss_release_buffer(&minor_status2, &output_token);
1388 set_kerb_auth_headers(r, conf, 0, 0, *negotiate_ret_value);
1391 if (GSS_ERROR(major_status)) {
1392 if (input_token.length > 7 && memcmp(input_token.value, "NTLMSSP", 7) == 0)
1393 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1394 "Warning: received token seems to be NTLM, which isn't supported by the Kerberos module. Check your IE configuration.");
1396 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1397 "%s", get_gss_error(r, major_status, minor_status,
1398 "gss_accept_sec_context() failed"));
1399 /* Don't offer the Negotiate method again if call to GSS layer failed */
1400 *negotiate_ret_value = NULL;
1401 ret = HTTP_UNAUTHORIZED;
1406 /* This is a _Kerberos_ module so multiple authentication rounds aren't
1407 * supported. If we wanted a generic GSS authentication we would have to do
1408 * some magic with exporting context etc. */
1409 if (major_status & GSS_S_CONTINUE_NEEDED) {
1410 ret = HTTP_UNAUTHORIZED;
1415 major_status = gss_display_name(&minor_status, client_name, &output_token, NULL);
1416 gss_release_name(&minor_status, &client_name);
1417 if (GSS_ERROR(major_status)) {
1418 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1419 "%s", get_gss_error(r, major_status, minor_status,
1420 "gss_display_name() failed"));
1421 ret = HTTP_INTERNAL_SERVER_ERROR;
1425 MK_AUTH_TYPE = MECH_NEGOTIATE;
1426 MK_USER = apr_pstrdup(r->pool, output_token.value);
1428 if (conf->krb_save_credentials && delegated_cred != GSS_C_NO_CREDENTIAL)
1429 store_gss_creds(r, conf, (char *)output_token.value, delegated_cred);
1431 gss_release_buffer(&minor_status, &output_token);
1437 gss_release_cred(&minor_status, &delegated_cred);
1439 if (output_token.length)
1440 gss_release_buffer(&minor_status, &output_token);
1442 if (client_name != GSS_C_NO_NAME)
1443 gss_release_name(&minor_status, &client_name);
1445 if (server_creds != GSS_C_NO_CREDENTIAL)
1446 gss_release_cred(&minor_status, &server_creds);
1448 if (context != GSS_C_NO_CONTEXT)
1449 gss_delete_sec_context(&minor_status, &context, GSS_C_NO_BUFFER);
1455 do_krb5_an_to_ln(request_rec *r) {
1456 krb5_error_code code;
1457 int ret = HTTP_INTERNAL_SERVER_ERROR;
1458 char *MK_USER_LNAME = NULL;
1459 krb5_context kcontext = NULL;
1460 krb5_principal client = NULL;
1462 code = krb5_init_context(&kcontext);
1464 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1465 "Cannot initialize Kerberos5 context (%d)", code);
1469 code = krb5_parse_name(kcontext, MK_USER, &client);
1471 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1472 "krb5_parse_name() failed: %s",
1473 krb5_get_err_text(kcontext, code));
1476 MK_USER_LNAME = apr_pcalloc(r->pool, strlen(MK_USER)+1);
1477 if (MK_USER_LNAME == NULL) {
1478 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1479 "ap_pcalloc() failed (not enough memory)");
1482 code = krb5_aname_to_localname(kcontext, client, strlen(MK_USER), MK_USER_LNAME);
1484 if (code != KRB5_LNAME_NOTRANS) {
1485 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1486 "krb5_aname_to_localname() failed: %s",
1487 krb5_get_err_text(kcontext, code));
1491 log_rerror(APLOG_MARK, APLOG_NOTICE, 0, r,
1492 "krb5_aname_to_localname() found no "
1493 "mapping for principal %s",
1498 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1499 "kerb_authenticate_a_name_to_local_name %s -> %s",
1500 (MK_USER)?MK_USER:"(NULL)", (MK_USER_LNAME)?MK_USER_LNAME:"(NULL)");
1501 MK_USER = apr_pstrdup(r->pool, MK_USER_LNAME);
1506 krb5_free_principal(kcontext, client);
1508 krb5_free_context(kcontext);
1516 already_succeeded(request_rec *r)
1518 if (ap_is_initial_req(r) || MK_AUTH_TYPE == NULL)
1520 if (strcmp(MK_AUTH_TYPE, MECH_NEGOTIATE) ||
1521 (strcmp(MK_AUTH_TYPE, "Basic") && strchr(MK_USER, '@')))
1527 set_kerb_auth_headers(request_rec *r, const kerb_auth_config *conf,
1528 int use_krb4, int use_krb5pwd, char *negotiate_ret_value)
1530 const char *auth_name = NULL;
1532 char *negoauth_param;
1533 const char *header_name =
1534 (r->proxyreq == PROXYREQ_PROXY) ? "Proxy-Authenticate" : "WWW-Authenticate";
1536 /* get the user realm specified in .htaccess */
1537 auth_name = ap_auth_name(r);
1539 /* XXX should the WWW-Authenticate header be cleared first?
1540 * apache in the proxy mode should retain client's authN headers? */
1542 if (negotiate_ret_value != NULL && conf->krb_method_gssapi) {
1543 negoauth_param = (*negotiate_ret_value == '\0') ? MECH_NEGOTIATE :
1544 apr_pstrcat(r->pool, MECH_NEGOTIATE " ", negotiate_ret_value, NULL);
1545 apr_table_add(r->err_headers_out, header_name, negoauth_param);
1547 if ((use_krb5pwd && conf->krb_method_k5pass) || conf->krb_delegate_basic) {
1548 apr_table_add(r->err_headers_out, header_name,
1549 apr_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1556 ((use_krb4 && conf->krb_method_k4pass) || conf->krb_delegate_basic))
1557 apr_table_add(r->err_headers_out, header_name,
1558 apr_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1563 kerb_authenticate_user(request_rec *r)
1565 kerb_auth_config *conf =
1566 (kerb_auth_config *) ap_get_module_config(r->per_dir_config,
1568 const char *auth_type = NULL;
1569 const char *auth_line = NULL;
1570 const char *type = NULL;
1571 int use_krb5 = 0, use_krb4 = 0;
1573 static int last_return = HTTP_UNAUTHORIZED;
1574 char *negotiate_ret_value = NULL;
1576 /* get the type specified in .htaccess */
1577 type = ap_auth_type(r);
1579 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1580 "kerb_authenticate_user entered with user %s and auth_type %s",
1581 (MK_USER)?MK_USER:"(NULL)",type?type:"(NULL)");
1583 if (type && strcasecmp(type, "Kerberos") == 0)
1584 use_krb5 = use_krb4 = 1;
1585 else if(type && strcasecmp(type, "KerberosV5") == 0)
1587 else if(type && strcasecmp(type, "KerberosV4") == 0)
1593 if (conf->krb_ssl_preauthentication) {
1594 const char *ssl_client_verify = ssl_var_lookup(r->pool, r->server,
1595 r->connection, r, "SSL_CLIENT_VERIFY");
1597 if (ssl_client_verify && strcmp(ssl_client_verify, "SUCCESS") == 0)
1602 /* get what the user sent us in the HTTP header */
1603 auth_line = MK_TABLE_GET(r->headers_in, (r->proxyreq == PROXYREQ_PROXY)
1604 ? "Proxy-Authorization"
1607 set_kerb_auth_headers(r, conf, use_krb4, use_krb5,
1608 (use_krb5) ? "\0" : NULL);
1609 return HTTP_UNAUTHORIZED;
1611 auth_type = ap_getword_white(r->pool, &auth_line);
1613 /* If we are delegating Basic to other modules, DECLINE the request */
1614 if (conf->krb_delegate_basic &&
1616 !conf->krb_method_k5pass &&
1619 !conf->krb_method_k4pass &&
1621 (strcasecmp(auth_type, "Basic") == 0))
1624 if (already_succeeded(r))
1627 ret = HTTP_UNAUTHORIZED;
1630 if (use_krb5 && conf->krb_method_gssapi &&
1631 strcasecmp(auth_type, MECH_NEGOTIATE) == 0) {
1632 ret = authenticate_user_gss(r, conf, auth_line, &negotiate_ret_value);
1633 } else if (use_krb5 && conf->krb_method_k5pass &&
1634 strcasecmp(auth_type, "Basic") == 0) {
1635 ret = authenticate_user_krb5pwd(r, conf, auth_line);
1637 if (ret == OK && conf->krb5_do_auth_to_local)
1638 ret = do_krb5_an_to_ln(r);
1642 if (ret == HTTP_UNAUTHORIZED && use_krb4 && conf->krb_method_k4pass &&
1643 strcasecmp(auth_type, "Basic") == 0)
1644 ret = authenticate_user_krb4pwd(r, conf, auth_line);
1647 if (ret == HTTP_UNAUTHORIZED)
1648 set_kerb_auth_headers(r, conf, use_krb4, use_krb5, negotiate_ret_value);
1650 /* XXX log_debug: if ret==OK, log(user XY authenticated) */
1657 have_rcache_type(const char *type)
1659 krb5_error_code ret;
1660 krb5_context context;
1661 krb5_rcache id = NULL;
1664 ret = krb5_init_context(&context);
1668 ret = krb5_rc_resolve_full(context, &id, "none:");
1672 krb5_rc_destroy(context, id);
1673 krb5_free_context(context);
1678 /***************************************************************************
1679 Module Setup/Configuration
1680 ***************************************************************************/
1681 #ifndef STANDARD20_MODULE_STUFF
1683 kerb_module_init(server_rec *dummy, pool *p)
1686 /* Suppress the MIT replay cache. Requires MIT Kerberos 1.4.0 or later.
1687 1.3.x are covered by the hack overiding the replay calls */
1688 if (getenv("KRB5RCACHETYPE") == NULL && have_rcache_type("none"))
1689 putenv(strdup("KRB5RCACHETYPE=none"));
1693 module MODULE_VAR_EXPORT auth_kerb_module = {
1694 STANDARD_MODULE_STUFF,
1695 kerb_module_init, /* module initializer */
1696 kerb_dir_create_config, /* per-directory config creator */
1697 NULL, /* per-directory config merger */
1698 NULL, /* per-server config creator */
1699 NULL, /* per-server config merger */
1700 kerb_auth_cmds, /* command table */
1701 NULL, /* [ 9] content handlers */
1702 NULL, /* [ 2] URI-to-filename translation */
1703 kerb_authenticate_user, /* [ 5] check/validate user_id */
1704 NULL, /* [ 6] check user_id is valid *here* */
1705 NULL, /* [ 4] check access by host address */
1706 NULL, /* [ 7] MIME type checker/setter */
1707 NULL, /* [ 8] fixups */
1708 NULL, /* [10] logger */
1709 NULL, /* [ 3] header parser */
1710 NULL, /* process initialization */
1711 NULL, /* process exit/cleanup */
1712 NULL /* [ 1] post read_request handling */
1714 ,NULL, /* EAPI: add_module */
1715 NULL, /* EAPI: remove_module */
1716 NULL, /* EAPI: rewrite_command */
1717 NULL /* EAPI: new_connection */
1722 kerb_init_handler(apr_pool_t *p, apr_pool_t *plog,
1723 apr_pool_t *ptemp, server_rec *s)
1725 ap_add_version_component(p, "mod_auth_kerb/" MODAUTHKERB_VERSION);
1727 /* Suppress the MIT replay cache. Requires MIT Kerberos 1.4.0 or later.
1728 1.3.x are covered by the hack overiding the replay calls */
1729 if (getenv("KRB5RCACHETYPE") == NULL && have_rcache_type("none"))
1730 putenv(strdup("KRB5RCACHETYPE=none"));
1737 kerb_register_hooks(apr_pool_t *p)
1739 ap_hook_post_config(kerb_init_handler, NULL, NULL, APR_HOOK_MIDDLE);
1740 ap_hook_check_user_id(kerb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
1743 module AP_MODULE_DECLARE_DATA auth_kerb_module =
1745 STANDARD20_MODULE_STUFF,
1746 kerb_dir_create_config, /* create per-dir conf structures */
1747 NULL, /* merge per-dir conf structures */
1748 NULL, /* create per-server conf structures */
1749 NULL, /* merge per-server conf structures */
1750 kerb_auth_cmds, /* table of configuration directives */
1751 kerb_register_hooks /* register hooks */