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>
13 /* ====================================================================
14 * The Apache Software License, Version 1.1
16 * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
31 * 3. The end-user documentation included with the redistribution,
32 * if any, must include the following acknowledgment:
33 * "This product includes software developed by the
34 * Apache Software Foundation (http://www.apache.org/)."
35 * Alternately, this acknowledgment may appear in the software itself,
36 * if and wherever such third-party acknowledgments normally appear.
38 * 4. The names "Apache" and "Apache Software Foundation" must
39 * not be used to endorse or promote products derived from this
40 * software without prior written permission. For written
41 * permission, please contact apache@apache.org.
43 * 5. Products derived from this software may not be called "Apache",
44 * nor may "Apache" appear in their name, without prior written
45 * permission of the Apache Software Foundation.
47 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
48 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
51 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
54 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
57 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * ====================================================================
61 * This software consists of voluntary contributions made by many
62 * individuals on behalf of the Apache Software Foundation. For more
63 * information on the Apache Software Foundation, please see
64 * <http://www.apache.org/>.
66 * Portions of this software are based upon public domain software
67 * originally written at the National Center for Supercomputing Applications,
68 * University of Illinois, Urbana-Champaign.
75 #define MODAUTHKERB_VERSION "5.0-rc2"
78 #include "ap_compat.h"
79 #include "apr_strings.h"
82 #include "http_config.h"
83 #include "http_core.h"
85 #include "http_protocol.h"
86 #include "http_request.h"
93 # include <gssapi/gssapi.h>
94 # include <gssapi/gssapi_generic.h>
95 # define GSS_C_NT_USER_NAME gss_nt_user_name
96 # define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
97 # define krb5_get_err_text(context,code) error_message(code)
99 #include "spnegokrb5.h"
103 /*Prevent warning about closesocket redefinition (Apache's ap_config.h and
104 * MIT Kerberos' port-sockets.h both define it as close) */
109 #include <netdb.h> /* gethostbyname() */
113 module auth_kerb_module;
115 module AP_MODULE_DECLARE_DATA auth_kerb_module;
118 /***************************************************************************
119 Macros To Ease Compatibility
120 ***************************************************************************/
123 #define MK_TABLE_GET ap_table_get
124 #define MK_USER r->connection->user
125 #define MK_AUTH_TYPE r->connection->ap_auth_type
127 #define MK_POOL apr_pool_t
128 #define MK_TABLE_GET apr_table_get
129 #define MK_USER r->user
130 #define MK_AUTH_TYPE r->ap_auth_type
134 /***************************************************************************
135 Auth Configuration Structure
136 ***************************************************************************/
138 char *krb_auth_realms;
139 int krb_save_credentials;
143 int krb_method_gssapi;
144 int krb_method_k5pass;
148 int krb_method_k4pass;
153 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg);
156 #define command(name, func, var, type, usage) \
158 (void*)XtOffsetOf(kerb_auth_config, var), \
159 OR_AUTHCFG, type, usage }
161 #define command(name, func, var, type, usage) \
162 AP_INIT_ ## type (name, func, \
163 (void*)APR_XtOffsetOf(kerb_auth_config, var), \
167 static const command_rec kerb_auth_cmds[] = {
168 command("KrbAuthRealms", krb5_save_realms, krb_auth_realms,
169 RAW_ARGS, "Realms to attempt authentication against (can be multiple)."),
171 command("KrbAuthRealm", krb5_save_realms, krb_auth_realms,
172 RAW_ARGS, "Alias for KrbAuthRealms."),
174 command("KrbSaveCredentials", ap_set_flag_slot, krb_save_credentials,
175 FLAG, "Save and store credentials/tickets retrieved during auth."),
177 command("KrbVerifyKDC", ap_set_flag_slot, krb_verify_kdc,
178 FLAG, "Verify tickets against keytab to prevent KDC spoofing attacks."),
181 command("Krb5Keytab", ap_set_file_slot, krb_5_keytab,
182 TAKE1, "Location of Kerberos V5 keytab file."),
184 command("KrbMethodNegotiate", ap_set_flag_slot, krb_method_gssapi,
185 FLAG, "Enable Negotiate authentication method."),
187 command("KrbMethodK5Pass", ap_set_flag_slot, krb_method_k5pass,
188 FLAG, "Enable Kerberos V5 password authentication."),
192 command("Krb4Srvtab", ap_set_file_slot, krb_4_srvtab,
193 TAKE1, "Location of Kerberos V4 srvtab file."),
195 command("KrbMethodK4Pass", ap_set_flag_slot, krb_method_k4pass,
196 FLAG, "Enable Kerberos V4 password authentication."),
204 gss_ctx_id_t context;
205 gss_cred_id_t server_creds;
208 static gss_connection_t *gss_connection = NULL;
212 /***************************************************************************
213 Auth Configuration Initialization
214 ***************************************************************************/
215 static void *kerb_dir_create_config(MK_POOL *p, char *d)
217 kerb_auth_config *rec;
219 rec = (kerb_auth_config *) ap_pcalloc(p, sizeof(kerb_auth_config));
220 ((kerb_auth_config *)rec)->krb_verify_kdc = 1;
222 ((kerb_auth_config *)rec)->krb_method_k5pass = 1;
223 ((kerb_auth_config *)rec)->krb_method_gssapi = 1;
226 ((kerb_auth_config *)rec)->krb_method_k4pass = 1;
232 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg)
234 sec->krb_auth_realms= ap_pstrdup(cmd->pool, arg);
238 void log_rerror(const char *file, int line, int level, int status,
239 const request_rec *r, const char *fmt, ...)
246 vsnprintf(errstr, sizeof(errstr), fmt, ap);
251 snprintf(errnostr, sizeof(errnostr), "%s: (%s)", errstr, strerror(errno));
253 snprintf(errnostr, sizeof(errnostr), "%s", errstr);
256 ap_log_rerror(file, line, level | APLOG_NOERRNO, r, "%s", errnostr);
258 ap_log_rerror(file, line, level | APLOG_NOERRNO, status, r, "%s", errnostr);
263 /***************************************************************************
264 Username/Password Validation for Krb4
265 ***************************************************************************/
267 verify_krb4_user(request_rec *r, char *name, char *instance, char *realm,
268 char *password, char *linstance, char *srvtab, int krb_verify_kdc)
274 const char *hostname;
277 char lrealm[REALM_SZ];
279 ret = krb_get_pw_in_tkt(name, instance, realm, "krbtgt", realm,
280 DEFAULT_TKT_LIFE, password);
282 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
283 "Cannot get krb4 ticket: krb_get_pw_in_tkt() failed: %s",
284 krb_get_err_text(ret));
291 hostname = ap_get_server_name(r);
293 hp = gethostbyname(hostname);
296 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
297 "Cannot verify krb4 ticket: gethostbyname() failed: %s",
301 memcpy(&addr, hp->h_addr, sizeof(addr));
303 phost = krb_get_phost((char *)hostname);
305 krb_get_lrealm(lrealm, 1);
307 ret = krb_mk_req(&ticket, linstance, phost, lrealm, 0);
309 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
310 "Cannot verify krb4 ticket: krb_mk_req() failed: %s",
311 krb_get_err_text(ret));
316 ret = krb_rd_req(&ticket, linstance, phost, addr, &authdata, srvtab);
318 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
319 "Cannot verify krb4 ticket: krb_rd_req() failed: %s",
320 krb_get_err_text(ret));
328 krb4_cache_cleanup(void *data)
330 char *tkt_file = (char *) data;
332 krb_set_tkt_string(tkt_file);
338 authenticate_user_krb4pwd(request_rec *r,
339 kerb_auth_config *conf,
340 const char *auth_line)
344 const char *sent_name;
347 char *tkt_file_p = NULL;
352 char lrealm[REALM_SZ];
354 sent_pw = ap_pbase64decode(r->pool, auth_line);
355 sent_name = ap_getword (r->pool, &sent_pw, ':');
357 /* do not allow user to override realm setting of server */
358 if (strchr(sent_name, '@')) {
359 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
360 "specifying realm in user name is prohibited");
361 return HTTP_UNAUTHORIZED;
364 sent_instance = strchr(sent_name, '.');
366 *sent_instance++ = '\0';
368 snprintf(tkt_file, sizeof(tkt_file), "/tmp/apache_tkt_XXXXXX");
369 fd = mkstemp(tkt_file);
371 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
372 "Cannot create krb4 ccache: mkstemp() failed: %s",
374 return HTTP_INTERNAL_SERVER_ERROR;
377 tkt_file_p = ap_pstrdup(r->pool, tkt_file);
378 ap_register_cleanup(r->pool, tkt_file_p,
379 krb4_cache_cleanup, ap_null_cleanup);
381 krb_set_tkt_string(tkt_file);
383 realms = conf->krb_auth_realms;
385 memset(lrealm, 0, sizeof(lrealm));
388 realm = ap_getword_white(r->pool, &realms);
391 ret = krb_get_lrealm(lrealm, 1);
394 if (realm == NULL || *realm == '\0')
397 ret = verify_krb4_user(r, (char *)sent_name,
398 (sent_instance) ? sent_instance : "",
399 (char *)realm, (char *)sent_pw, "khttp",
400 conf->krb_4_srvtab, conf->krb_verify_kdc);
403 } while (realms && *realms);
406 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Verifying krb4 password failed");
407 ret = HTTP_UNAUTHORIZED;
411 user = ap_pstrdup(r->pool, sent_name);
413 user = ap_pstrcat(r->pool, user, ".", sent_instance, NULL);
414 user = ap_pstrcat(r->pool, user, "@", realm, NULL);
417 MK_AUTH_TYPE = "Basic";
418 ap_table_setn(r->subprocess_env, "KRBTKFILE", tkt_file_p);
420 if (!conf->krb_save_credentials)
421 krb4_cache_cleanup(tkt_file);
425 krb4_cache_cleanup(tkt_file);
434 /***************************************************************************
435 Username/Password Validation for Krb5
436 ***************************************************************************/
437 /* Inspired by krb5_verify_user from Heimdal */
438 static krb5_error_code
439 verify_krb5_user(request_rec *r, krb5_context context, krb5_principal principal,
440 krb5_ccache ccache, const char *password, const char *service,
441 krb5_keytab keytab, int krb_verify_kdc)
444 krb5_principal server = NULL;
446 krb5_verify_init_creds_opt opt;
448 memset(&creds, 0, sizeof(creds));
450 ret = krb5_get_init_creds_password(context, &creds, principal,
451 (char *)password, krb5_prompter_posix,
452 NULL, 0, NULL, NULL);
456 ret = krb5_sname_to_principal(context, ap_get_server_name(r), service,
457 KRB5_NT_SRV_HST, &server);
461 krb5_verify_init_creds_opt_init(&opt);
462 krb5_verify_init_creds_opt_set_ap_req_nofail(&opt, krb_verify_kdc);
464 ret = krb5_verify_init_creds(context, &creds, server, keytab, NULL, &opt);
469 ret = krb5_cc_initialize(context, ccache, principal);
471 ret = krb5_cc_store_cred(context, ccache, &creds);
475 krb5_free_cred_contents(context, &creds);
477 krb5_free_principal(context, server);
482 krb5_cache_cleanup(void *data)
484 krb5_context context;
486 krb5_error_code problem;
487 char *cache_name = (char *) data;
489 problem = krb5_init_context(&context);
491 /* ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "krb5_init_context() failed"); */
492 return HTTP_INTERNAL_SERVER_ERROR;
495 problem = krb5_cc_resolve(context, cache_name, &cache);
497 /* log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
498 "krb5_cc_resolve() failed (%s: %s)",
499 cache_name, krb5_get_err_text(context, problem)); */
500 return HTTP_INTERNAL_SERVER_ERROR;
503 krb5_cc_destroy(context, cache);
504 krb5_free_context(context);
509 create_krb5_ccache(krb5_context kcontext,
511 kerb_auth_config *conf,
512 krb5_principal princ,
516 krb5_error_code problem;
518 krb5_ccache tmp_ccache = NULL;
520 #ifdef HAVE_KRB5_CC_GEN_NEW
521 problem = krb5_cc_gen_new(kcontext, &krb5_fcc_ops, &tmp_ccache);
523 /* only older MIT seem to not have the krb5_cc_gen_new() call, so we use
524 * MIT specific call here */
525 problem = krb5_fcc_generate_new(kcontext, &tmp_ccache);
526 /* krb5_fcc_generate_new() doesn't set KRB5_TC_OPENCLOSE, which makes
527 krb5_cc_initialize() fail */
528 krb5_fcc_set_flags(kcontext, tmp_ccache, KRB5_TC_OPENCLOSE);
531 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
532 "Cannot create file for new krb5 ccache: %s",
533 krb5_get_err_text(kcontext, problem));
534 ret = HTTP_INTERNAL_SERVER_ERROR;
538 ccname = ap_pstrdup(r->pool, krb5_cc_get_name(kcontext, tmp_ccache));
540 problem = krb5_cc_initialize(kcontext, tmp_ccache, princ);
542 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
543 "Cannot initialize krb5 ccache %s: krb5_cc_initialize() failed: %s",
544 ccname, krb5_get_err_text(kcontext, problem));
545 ret = HTTP_INTERNAL_SERVER_ERROR;
549 ap_table_setn(r->subprocess_env, "KRB5CCNAME", ccname);
550 ap_register_cleanup(r->pool, ccname,
551 krb5_cache_cleanup, ap_null_cleanup);
553 *ccache = tmp_ccache;
560 krb5_cc_destroy(kcontext, tmp_ccache);
566 store_krb5_creds(krb5_context kcontext,
568 kerb_auth_config *conf,
569 krb5_ccache delegated_cred)
572 krb5_error_code problem;
573 krb5_principal princ;
577 problem = krb5_cc_get_principal(kcontext, delegated_cred, &princ);
579 snprintf(errstr, sizeof(errstr), "krb5_cc_get_principal() failed: %s",
580 krb5_get_err_text(kcontext, problem));
581 return HTTP_INTERNAL_SERVER_ERROR;
584 ret = create_krb5_ccache(kcontext, r, conf, princ, &ccache);
586 krb5_free_principal(kcontext, princ);
591 problem = krb5_cc_copy_cache(kcontext, delegated_cred, ccache);
593 problem = krb5_cc_copy_creds(kcontext, delegated_cred, ccache);
595 krb5_free_principal(kcontext, princ);
597 snprintf(errstr, sizeof(errstr), "Failed to store credentials: %s",
598 krb5_get_err_text(kcontext, problem));
599 krb5_cc_destroy(kcontext, ccache);
600 return HTTP_INTERNAL_SERVER_ERROR;
603 krb5_cc_close(kcontext, ccache);
608 int authenticate_user_krb5pwd(request_rec *r,
609 kerb_auth_config *conf,
610 const char *auth_line)
612 const char *sent_pw = NULL;
613 const char *sent_name = NULL;
614 const char *realms = NULL;
615 krb5_context kcontext = NULL;
616 krb5_error_code code;
617 krb5_principal client = NULL;
618 krb5_ccache ccache = NULL;
619 krb5_keytab keytab = NULL;
623 code = krb5_init_context(&kcontext);
625 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
626 "Cannot initialize Kerberos5 context (%d)", code);
627 return HTTP_INTERNAL_SERVER_ERROR;
630 sent_pw = ap_pbase64decode(r->pool, auth_line);
631 sent_name = ap_getword (r->pool, &sent_pw, ':');
632 /* do not allow user to override realm setting of server */
633 if (strchr(sent_name, '@')) {
634 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
635 "specifying realm in user name is prohibited");
636 ret = HTTP_UNAUTHORIZED;
640 #ifdef HAVE_KRB5_CC_GEN_NEW
641 code = krb5_cc_gen_new(kcontext, &krb5_mcc_ops, &ccache);
643 /* only older MIT seem to not have the krb5_cc_gen_new() call, so we use
644 * MIT specific call here */
645 code = krb5_mcc_generate_new(kcontext, &ccache);
648 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
649 "Cannot generate new ccache: %s",
650 krb5_get_err_text(kcontext, code));
651 ret = HTTP_INTERNAL_SERVER_ERROR;
655 if (conf->krb_5_keytab)
656 krb5_kt_resolve(kcontext, conf->krb_5_keytab, &keytab);
658 realms = conf->krb_auth_realms;
660 if (realms && (code = krb5_set_default_realm(kcontext,
661 ap_getword_white(r->pool, &realms))))
665 krb5_free_principal(kcontext, client);
668 code = krb5_parse_name(kcontext, sent_name, &client);
672 code = verify_krb5_user(r, kcontext, client, ccache, sent_pw, "khttp",
673 keytab, conf->krb_verify_kdc);
677 /* ap_getword_white() used above shifts the parameter, so it's not
678 needed to touch the realms variable */
679 } while (realms && *realms);
681 memset((char *)sent_pw, 0, strlen(sent_pw));
684 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
685 "Verifying krb5 password failed: %s",
686 krb5_get_err_text(kcontext, code));
687 ret = HTTP_UNAUTHORIZED;
691 code = krb5_unparse_name(kcontext, client, &name);
693 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "krb5_unparse_name() failed: %s",
694 krb5_get_err_text(kcontext, code));
695 ret = HTTP_UNAUTHORIZED;
698 MK_USER = ap_pstrdup (r->pool, name);
699 MK_AUTH_TYPE = "Basic";
702 if (conf->krb_save_credentials)
703 store_krb5_creds(kcontext, r, conf, ccache);
709 krb5_free_principal(kcontext, client);
711 krb5_cc_destroy(kcontext, ccache);
713 krb5_kt_close(kcontext, keytab);
714 krb5_free_context(kcontext);
719 /*********************************************************************
720 * GSSAPI Authentication
721 ********************************************************************/
724 get_gss_error(MK_POOL *p, OM_uint32 error_status, char *prefix)
726 OM_uint32 maj_stat, min_stat;
727 OM_uint32 msg_ctx = 0;
728 gss_buffer_desc status_string;
732 snprintf(buf, sizeof(buf), "%s", prefix);
735 maj_stat = gss_display_status (&min_stat,
741 if (sizeof(buf) > len + status_string.length + 1) {
742 sprintf(buf+len, ": %s", (char*) status_string.value);
743 len += status_string.length;
745 gss_release_buffer(&min_stat, &status_string);
746 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
748 return (ap_pstrdup(p, buf));
752 cleanup_gss_connection(void *data)
754 OM_uint32 minor_status;
755 gss_connection_t *gss_conn = (gss_connection_t *)data;
759 if (gss_conn->context != GSS_C_NO_CONTEXT)
760 gss_delete_sec_context(&minor_status, &gss_conn->context,
762 if (gss_conn->server_creds != GSS_C_NO_CREDENTIAL)
763 gss_release_cred(&minor_status, &gss_conn->server_creds);
765 gss_connection = NULL;
771 store_gss_creds(request_rec *r, kerb_auth_config *conf, char *princ_name,
772 gss_cred_id_t delegated_cred)
774 OM_uint32 maj_stat, min_stat;
775 krb5_principal princ = NULL;
776 krb5_ccache ccache = NULL;
777 krb5_error_code problem;
778 krb5_context context;
779 int ret = HTTP_INTERNAL_SERVER_ERROR;
781 problem = krb5_init_context(&context);
783 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Cannot initialize krb5 context");
784 return HTTP_INTERNAL_SERVER_ERROR;
787 problem = krb5_parse_name(context, princ_name, &princ);
789 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
790 "Cannot parse delegated username (%s)", krb5_get_err_text(context, problem));
794 problem = create_krb5_ccache(context, r, conf, princ, &ccache);
796 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
797 "Cannot create krb5 ccache (%s)", krb5_get_err_text(context, problem));
801 maj_stat = gss_krb5_copy_ccache(&min_stat, delegated_cred, ccache);
802 if (GSS_ERROR(maj_stat)) {
803 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
804 "Cannot store delegated credential (%s)",
805 get_gss_error(r->pool, min_stat, "gss_krb5_copy_ccache"));
809 krb5_cc_close(context, ccache);
815 krb5_free_principal(context, princ);
817 krb5_cc_destroy(context, ccache);
818 krb5_free_context(context);
823 get_gss_creds(request_rec *r,
824 kerb_auth_config *conf,
825 gss_cred_id_t *server_creds)
827 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
828 OM_uint32 major_status, minor_status, minor_status2;
829 gss_name_t server_name = GSS_C_NO_NAME;
832 snprintf(buf, sizeof(buf), "%s/%s", "khttp", ap_get_server_name(r));
834 input_token.value = buf;
835 input_token.length = strlen(buf) + 1;
837 major_status = gss_import_name(&minor_status, &input_token,
840 if (GSS_ERROR(major_status)) {
841 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
842 "%s", get_gss_error(r->pool, minor_status,
843 "gss_import_name() failed"));
844 return HTTP_INTERNAL_SERVER_ERROR;
847 major_status = gss_acquire_cred(&minor_status, server_name, GSS_C_INDEFINITE,
848 GSS_C_NO_OID_SET, GSS_C_ACCEPT,
849 server_creds, NULL, NULL);
850 gss_release_name(&minor_status2, &server_name);
851 if (GSS_ERROR(major_status)) {
852 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
853 "%s", get_gss_error(r->pool, minor_status,
854 "gss_acquire_cred() failed"));
855 return HTTP_INTERNAL_SERVER_ERROR;
862 cmp_gss_type(gss_buffer_t token, gss_OID oid)
867 if (token->length == 0)
868 return GSS_S_DEFECTIVE_TOKEN;
872 return GSS_S_DEFECTIVE_TOKEN;
875 if ((len & 0x7f) > 4)
876 return GSS_S_DEFECTIVE_TOKEN;
880 return GSS_S_DEFECTIVE_TOKEN;
882 if (((OM_uint32) *p++) != oid->length)
883 return GSS_S_DEFECTIVE_TOKEN;
885 return memcmp(p, oid->elements, oid->length);
889 authenticate_user_gss(request_rec *r,
890 kerb_auth_config *conf,
891 const char *auth_line)
893 OM_uint32 major_status, minor_status, minor_status2;
894 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
895 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
896 const char *auth_param = NULL;
898 gss_name_t client_name = GSS_C_NO_NAME;
899 gss_cred_id_t delegated_cred = GSS_C_NO_CREDENTIAL;
900 OM_uint32 (*accept_sec_token)();
901 gss_OID_desc spnego_oid;
903 spnego_oid.length = 6;
904 spnego_oid.elements = (void *)"\x2b\x06\x01\x05\x05\x02";
906 if (gss_connection == NULL) {
907 gss_connection = ap_pcalloc(r->connection->pool, sizeof(*gss_connection));
908 if (gss_connection == NULL) {
909 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
910 "ap_pcalloc() failed (not enough memory)");
911 ret = HTTP_INTERNAL_SERVER_ERROR;
914 memset(gss_connection, 0, sizeof(*gss_connection));
915 ap_register_cleanup(r->connection->pool, gss_connection, cleanup_gss_connection, ap_null_cleanup);
918 if (conf->krb_5_keytab) {
920 /* we don't use the ap_* calls here, since the string passed to putenv()
921 * will become part of the enviroment and shouldn't be free()ed by apache
923 ktname = malloc(strlen("KRB5_KTNAME=") + strlen(conf->krb_5_keytab) + 1);
924 if (ktname == NULL) {
925 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "malloc() failed: not enough memory");
926 ret = HTTP_INTERNAL_SERVER_ERROR;
929 sprintf(ktname, "KRB5_KTNAME=%s", conf->krb_5_keytab);
933 if (gss_connection->server_creds == GSS_C_NO_CREDENTIAL) {
934 ret = get_gss_creds(r, conf, &gss_connection->server_creds);
939 /* ap_getword() shifts parameter */
940 auth_param = ap_getword_white(r->pool, &auth_line);
941 if (auth_param == NULL) {
942 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
943 "No Authorization parameter in request from client");
944 ret = HTTP_UNAUTHORIZED;
948 input_token.length = ap_base64decode_len(auth_param) + 1;
949 input_token.value = ap_pcalloc(r->connection->pool, input_token.length);
950 if (input_token.value == NULL) {
951 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
952 "ap_pcalloc() failed (not enough memory)");
953 ret = HTTP_INTERNAL_SERVER_ERROR;
956 input_token.length = ap_base64decode(input_token.value, auth_param);
958 accept_sec_token = (cmp_gss_type(&input_token, &spnego_oid) == 0) ?
959 gss_accept_sec_context_spnego : gss_accept_sec_context;
961 major_status = accept_sec_token(&minor_status,
962 &gss_connection->context,
963 gss_connection->server_creds,
965 GSS_C_NO_CHANNEL_BINDINGS,
972 if (output_token.length) {
976 len = ap_base64encode_len(output_token.length) + 1;
977 token = ap_pcalloc(r->connection->pool, len + 1);
979 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
980 "ap_pcalloc() failed (not enough memory)");
981 ret = HTTP_INTERNAL_SERVER_ERROR;
982 gss_release_buffer(&minor_status2, &output_token);
985 ap_base64encode(token, output_token.value, output_token.length);
987 ap_table_set(r->err_headers_out, "WWW-Authenticate",
988 ap_pstrcat(r->pool, "Negotiate ", token, NULL));
989 gss_release_buffer(&minor_status2, &output_token);
992 if (GSS_ERROR(major_status)) {
993 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
994 "%s", get_gss_error(r->pool, minor_status,
995 "gss_accept_sec_context() failed"));
996 ret = HTTP_UNAUTHORIZED;
1000 if (major_status & GSS_S_CONTINUE_NEEDED) {
1001 /* Some GSSAPI mechanism (eg GSI from Globus) may require multiple
1002 * iterations to establish authentication */
1003 ret = HTTP_UNAUTHORIZED;
1007 major_status = gss_display_name(&minor_status, client_name, &output_token, NULL);
1008 gss_release_name(&minor_status, &client_name);
1009 if (GSS_ERROR(major_status)) {
1010 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1011 "%s", get_gss_error(r->pool, minor_status,
1012 "gss_export_name() failed"));
1013 ret = HTTP_INTERNAL_SERVER_ERROR;
1017 MK_AUTH_TYPE = "Negotiate";
1018 MK_USER = ap_pstrdup(r->pool, output_token.value);
1020 if (conf->krb_save_credentials && delegated_cred != GSS_C_NO_CREDENTIAL)
1021 store_gss_creds(r, conf, (char *)output_token.value, delegated_cred);
1023 gss_release_buffer(&minor_status, &output_token);
1029 gss_release_cred(&minor_status, &delegated_cred);
1031 if (output_token.length)
1032 gss_release_buffer(&minor_status, &output_token);
1034 if (client_name != GSS_C_NO_NAME)
1035 gss_release_name(&minor_status, &client_name);
1037 cleanup_gss_connection(gss_connection);
1044 already_succeeded(request_rec *r)
1046 if (ap_is_initial_req(r) || MK_AUTH_TYPE == NULL)
1048 if (strcmp(MK_AUTH_TYPE, "Negotiate") ||
1049 (strcmp(MK_AUTH_TYPE, "Basic") && strchr(MK_USER, '@')))
1055 note_kerb_auth_failure(request_rec *r, const kerb_auth_config *conf,
1056 int use_krb4, int use_krb5)
1058 const char *auth_name = NULL;
1061 /* get the user realm specified in .htaccess */
1062 auth_name = ap_auth_name(r);
1064 /* XXX should the WWW-Authenticate header be cleared first? */
1066 if (use_krb5 && conf->krb_method_gssapi)
1067 ap_table_add(r->err_headers_out, "WWW-Authenticate", "Negotiate");
1068 if (use_krb5 && conf->krb_method_k5pass) {
1069 ap_table_add(r->err_headers_out, "WWW-Authenticate",
1070 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1076 if (use_krb4 && conf->krb_method_k4pass && !set_basic)
1077 ap_table_add(r->err_headers_out, "WWW-Authenticate",
1078 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1082 int kerb_authenticate_user(request_rec *r)
1084 kerb_auth_config *conf =
1085 (kerb_auth_config *) ap_get_module_config(r->per_dir_config,
1087 const char *auth_type = NULL;
1088 const char *auth_line = NULL;
1089 const char *type = NULL;
1090 int use_krb5 = 0, use_krb4 = 0;
1092 static int last_return = HTTP_UNAUTHORIZED;
1094 /* get the type specified in .htaccess */
1095 type = ap_auth_type(r);
1097 if (type && strcasecmp(type, "Kerberos") == 0)
1098 use_krb5 = use_krb4 = 1;
1099 else if(type && strcasecmp(type, "KerberosV5") == 0)
1101 else if(type && strcasecmp(type, "KerberosV4") == 0)
1106 /* get what the user sent us in the HTTP header */
1107 auth_line = MK_TABLE_GET(r->headers_in, "Authorization");
1109 note_kerb_auth_failure(r, conf, use_krb4, use_krb5);
1110 return HTTP_UNAUTHORIZED;
1112 auth_type = ap_getword_white(r->pool, &auth_line);
1114 if (already_succeeded(r))
1117 ret = HTTP_UNAUTHORIZED;
1120 if (use_krb5 && conf->krb_method_gssapi &&
1121 strcasecmp(auth_type, "Negotiate") == 0) {
1122 ret = authenticate_user_gss(r, conf, auth_line);
1123 } else if (use_krb5 && conf->krb_method_k5pass &&
1124 strcasecmp(auth_type, "Basic") == 0) {
1125 ret = authenticate_user_krb5pwd(r, conf, auth_line);
1130 if (ret == HTTP_UNAUTHORIZED && use_krb4 && conf->krb_method_k4pass &&
1131 strcasecmp(auth_type, "Basic") == 0)
1132 ret = authenticate_user_krb4pwd(r, conf, auth_line);
1135 if (ret == HTTP_UNAUTHORIZED)
1136 note_kerb_auth_failure(r, conf, use_krb4, use_krb5);
1143 /***************************************************************************
1144 Module Setup/Configuration
1145 ***************************************************************************/
1147 module MODULE_VAR_EXPORT auth_kerb_module = {
1148 STANDARD_MODULE_STUFF,
1149 NULL, /* module initializer */
1150 kerb_dir_create_config, /* per-directory config creator */
1151 NULL, /* per-directory config merger */
1152 NULL, /* per-server config creator */
1153 NULL, /* per-server config merger */
1154 kerb_auth_cmds, /* command table */
1155 NULL, /* [ 9] content handlers */
1156 NULL, /* [ 2] URI-to-filename translation */
1157 kerb_authenticate_user, /* [ 5] check/validate user_id */
1158 NULL, /* [ 6] check user_id is valid *here* */
1159 NULL, /* [ 4] check access by host address */
1160 NULL, /* [ 7] MIME type checker/setter */
1161 NULL, /* [ 8] fixups */
1162 NULL, /* [10] logger */
1163 NULL, /* [ 3] header parser */
1164 NULL, /* process initialization */
1165 NULL, /* process exit/cleanup */
1166 NULL /* [ 1] post read_request handling */
1170 kerb_init_handler(apr_pool_t *p, apr_pool_t *plog,
1171 apr_pool_t *ptemp, server_rec *s)
1173 ap_add_version_component(p, "mod_auth_kerb/" MODAUTHKERB_VERSION);
1177 void kerb_register_hooks(apr_pool_t *p)
1179 ap_hook_post_config(kerb_init_handler, NULL, NULL, APR_HOOK_MIDDLE);
1180 ap_hook_check_user_id(kerb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
1183 module AP_MODULE_DECLARE_DATA auth_kerb_module =
1185 STANDARD20_MODULE_STUFF,
1186 kerb_dir_create_config, /* create per-dir conf structures */
1187 NULL, /* merge per-dir conf structures */
1188 NULL, /* create per-server conf structures */
1189 NULL, /* merge per-server conf structures */
1190 kerb_auth_cmds, /* table of configuration directives */
1191 kerb_register_hooks /* register hooks */