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.
73 #define MODAUTHKERB_VERSION "5.0-rc2"
76 #include "ap_compat.h"
77 #include "apr_strings.h"
80 #include "http_config.h"
81 #include "http_core.h"
83 #include "http_protocol.h"
84 #include "http_request.h"
91 # include <gssapi/gssapi.h>
92 # include <gssapi/gssapi_generic.h>
93 # define GSS_C_NT_USER_NAME gss_nt_user_name
94 # define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
95 # define krb5_get_err_text(context,code) error_message(code)
100 /*Prevent warning about closesocket redefinition (Apache's ap_config.h and
101 * MIT Kerberos' port-sockets.h both define it as close) */
106 #include <netdb.h> /* gethostbyname() */
110 module auth_kerb_module;
112 module AP_MODULE_DECLARE_DATA auth_kerb_module;
115 /***************************************************************************
116 Macros To Ease Compatibility
117 ***************************************************************************/
120 #define MK_TABLE_GET ap_table_get
121 #define MK_TABLE_SET ap_table_set
122 #define MK_TABLE_TYPE table
123 #define MK_PSTRDUP ap_pstrdup
124 #define MK_USER r->connection->user
125 #define MK_AUTH_TYPE r->connection->ap_auth_type
126 #define MK_ARRAY_HEADER array_header
128 #define MK_POOL apr_pool_t
129 #define MK_TABLE_GET apr_table_get
130 #define MK_TABLE_SET apr_table_set
131 #define MK_TABLE_TYPE apr_table_t
132 #define MK_PSTRDUP apr_pstrdup
133 #define MK_USER r->user
134 #define MK_AUTH_TYPE r->ap_auth_type
135 #define MK_ARRAY_HEADER apr_array_header_t
139 /***************************************************************************
140 Auth Configuration Structure
141 ***************************************************************************/
143 char *krb_auth_realms;
144 int krb_save_credentials;
147 int krb_method_gssapi;
148 int krb_method_k5pass;
152 int krb_method_k4pass;
157 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg);
160 #define command(name, func, var, type, usage) \
162 (void*)XtOffsetOf(kerb_auth_config, var), \
163 OR_AUTHCFG, type, usage }
165 #define command(name, func, var, type, usage) \
166 AP_INIT_ ## type (name, func, \
167 (void*)APR_XtOffsetOf(kerb_auth_config, var), \
171 static const command_rec kerb_auth_cmds[] = {
172 command("KrbAuthRealms", krb5_save_realms, krb_auth_realms,
173 RAW_ARGS, "Realms to attempt authentication against (can be multiple)."),
175 command("KrbAuthRealm", krb5_save_realms, krb_auth_realms,
176 RAW_ARGS, "Alias for KrbAuthRealms."),
178 command("KrbSaveCredentials", ap_set_flag_slot, krb_save_credentials,
179 FLAG, "Save and store credentials/tickets retrieved during auth."),
182 command("Krb5Keytab", ap_set_file_slot, krb_5_keytab,
183 TAKE1, "Location of Kerberos V5 keytab file."),
185 command("KrbMethodNegotiate", ap_set_flag_slot, krb_method_gssapi,
186 FLAG, "Enable Negotiate authentication method."),
188 command("KrbMethodK5Pass", ap_set_flag_slot, krb_method_k5pass,
189 FLAG, "Enable Kerberos V5 password authentication."),
193 command("Krb4Srvtab", ap_set_file_slot, krb_4_srvtab,
194 TAKE1, "Location of Kerberos V4 srvtab file."),
196 command("KrbMethodK4Pass", ap_set_flag_slot, krb_method_k4pass,
197 FLAG, "Enable Kerberos V4 password authentication."),
205 gss_ctx_id_t context;
206 gss_cred_id_t server_creds;
209 static gss_connection_t *gss_connection = NULL;
213 /***************************************************************************
214 Auth Configuration Initialization
215 ***************************************************************************/
216 static void *kerb_dir_create_config(MK_POOL *p, char *d)
218 kerb_auth_config *rec;
220 rec = (kerb_auth_config *) ap_pcalloc(p, sizeof(kerb_auth_config));
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, ...)
245 vsnprintf(errstr, sizeof(errstr), fmt, ap);
249 ap_log_rerror(file, line, level, r, "%s", errstr);
251 ap_log_rerror(file, line, level, status, r, "%s", errstr);
256 /***************************************************************************
257 Username/Password Validation for Krb4
258 ***************************************************************************/
260 verify_krb4_user(request_rec *r, char *name, char *instance, char *realm,
261 char *password, char *linstance, char *srvtab)
267 const char *hostname;
270 char lrealm[REALM_SZ];
272 ret = krb_get_pw_in_tkt(name, instance, realm, "krbtgt", realm,
273 DEFAULT_TKT_LIFE, password);
275 /* log(krb_err_txt[ret]) */
278 hostname = ap_get_server_name(r);
280 hp = gethostbyname(hostname);
285 memcpy(&addr, hp->h_addr, sizeof(addr));
287 phost = krb_get_phost((char *)hostname);
289 krb_get_lrealm(lrealm, 1);
291 ret = krb_mk_req(&ticket, linstance, phost, lrealm, 0);
297 ret = krb_rd_req(&ticket, linstance, phost, addr, &authdata, srvtab);
305 krb4_cache_cleanup(void *data)
307 char *tkt_file = (char *) data;
309 krb_set_tkt_string(tkt_file);
315 authenticate_user_krb4pwd(request_rec *r,
316 kerb_auth_config *conf,
317 const char *auth_line)
321 const char *sent_name;
324 char *tkt_file_p = NULL;
329 char lrealm[REALM_SZ];
331 sent_pw = ap_pbase64decode(r->pool, auth_line);
332 sent_name = ap_getword (r->pool, &sent_pw, ':');
334 /* do not allow user to override realm setting of server */
335 if (strchr(sent_name, '@')) {
336 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
337 "specifying realm in user name is prohibited");
338 return HTTP_UNAUTHORIZED;
341 sent_instance = strchr(sent_name, '.');
343 *sent_instance++ = '\0';
345 snprintf(tkt_file, sizeof(tkt_file), "/tmp/apache_tkt_XXXXXX");
346 fd = mkstemp(tkt_file);
348 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
349 "Cannot create krb4 ccache: mkstemp() failed: %s",
351 return HTTP_INTERNAL_SERVER_ERROR;
354 tkt_file_p = ap_pstrdup(r->pool, tkt_file);
355 ap_register_cleanup(r->pool, tkt_file_p,
356 krb4_cache_cleanup, ap_null_cleanup);
358 krb_set_tkt_string(tkt_file);
360 realms = conf->krb_auth_realms;
362 memset(lrealm, 0, sizeof(lrealm));
365 realm = ap_getword_white(r->pool, &realms);
368 ret = krb_get_lrealm(lrealm, 1);
371 if (realm == NULL || *realm == '\0')
374 ret = verify_krb4_user(r, (char *)sent_name,
375 (sent_instance) ? sent_instance : "",
376 (char *)realm, (char *)sent_pw, "khttp",
380 } while (realms && *realms);
383 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
384 "Verifying krb4 password failed (%d)", ret);
385 ret = HTTP_UNAUTHORIZED;
389 user = ap_pstrdup(r->pool, sent_name);
391 user = ap_pstrcat(r->pool, user, ".", sent_instance, NULL);
392 user = ap_pstrcat(r->pool, user, "@", realm, NULL);
395 MK_AUTH_TYPE = "Basic";
396 ap_table_setn(r->subprocess_env, "KRBTKFILE", tkt_file_p);
398 if (!conf->krb_save_credentials)
399 krb4_cache_cleanup(tkt_file);
403 krb4_cache_cleanup(tkt_file);
412 /***************************************************************************
413 Username/Password Validation for Krb5
414 ***************************************************************************/
415 /* Inspired by krb5_verify_user from Heimdal */
416 static krb5_error_code
417 verify_krb5_user(request_rec *r, krb5_context context, krb5_principal principal,
418 krb5_ccache ccache, const char *password, const char *service,
422 krb5_principal server = NULL;
424 krb5_verify_init_creds_opt opt;
426 memset(&creds, 0, sizeof(creds));
428 ret = krb5_get_init_creds_password(context, &creds, principal,
429 (char *)password, krb5_prompter_posix,
430 NULL, 0, NULL, NULL);
434 ret = krb5_sname_to_principal(context, ap_get_server_name(r), service,
435 KRB5_NT_SRV_HST, &server);
439 krb5_verify_init_creds_opt_init(&opt);
440 krb5_verify_init_creds_opt_set_ap_req_nofail(&opt, 1);
442 ret = krb5_verify_init_creds(context, &creds, server, keytab, NULL, &opt);
447 ret = krb5_cc_initialize(context, ccache, principal);
449 ret = krb5_cc_store_cred(context, ccache, &creds);
453 krb5_free_cred_contents(context, &creds);
455 krb5_free_principal(context, server);
460 krb5_cache_cleanup(void *data)
462 krb5_context context;
464 krb5_error_code problem;
465 char *cache_name = (char *) data;
467 problem = krb5_init_context(&context);
469 /* ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "krb5_init_context() failed"); */
470 return HTTP_INTERNAL_SERVER_ERROR;
473 problem = krb5_cc_resolve(context, cache_name, &cache);
475 /* log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
476 "krb5_cc_resolve() failed (%s: %s)",
477 cache_name, krb5_get_err_text(context, problem)); */
478 return HTTP_INTERNAL_SERVER_ERROR;
481 krb5_cc_destroy(context, cache);
482 krb5_free_context(context);
487 create_krb5_ccache(krb5_context kcontext,
489 kerb_auth_config *conf,
490 krb5_principal princ,
494 krb5_error_code problem;
496 krb5_ccache tmp_ccache = NULL;
499 problem = krb5_cc_gen_new(kcontext, &krb5_fcc_ops, &tmp_ccache);
501 problem = krb5_fcc_generate_new(kcontext, &tmp_ccache);
502 /* krb5_fcc_generate_new() doesn't set KRB5_TC_OPENCLOSE, which makes
503 krb5_cc_initialize() fail */
504 krb5_fcc_set_flags(kcontext, tmp_ccache, KRB5_TC_OPENCLOSE);
507 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
508 "Cannot create file for new krb5 ccache: %s",
509 krb5_get_err_text(kcontext, problem));
510 ret = HTTP_INTERNAL_SERVER_ERROR;
514 ccname = ap_pstrdup(r->pool, krb5_cc_get_name(kcontext, tmp_ccache));
516 problem = krb5_cc_initialize(kcontext, tmp_ccache, princ);
518 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
519 "Cannot initialize krb5 ccache %s: krb5_cc_initialize() failed: %s",
520 ccname, krb5_get_err_text(kcontext, problem));
521 ret = HTTP_INTERNAL_SERVER_ERROR;
525 ap_table_setn(r->subprocess_env, "KRB5CCNAME", ccname);
526 ap_register_cleanup(r->pool, ccname,
527 krb5_cache_cleanup, ap_null_cleanup);
529 *ccache = tmp_ccache;
536 krb5_cc_destroy(kcontext, tmp_ccache);
542 store_krb5_creds(krb5_context kcontext,
544 kerb_auth_config *conf,
545 krb5_ccache delegated_cred)
548 krb5_error_code problem;
549 krb5_principal princ;
553 problem = krb5_cc_get_principal(kcontext, delegated_cred, &princ);
555 snprintf(errstr, sizeof(errstr), "krb5_cc_get_principal() failed: %s",
556 krb5_get_err_text(kcontext, problem));
557 return HTTP_INTERNAL_SERVER_ERROR;
560 ret = create_krb5_ccache(kcontext, r, conf, princ, &ccache);
562 krb5_free_principal(kcontext, princ);
567 problem = krb5_cc_copy_cache(kcontext, delegated_cred, ccache);
569 problem = krb5_cc_copy_creds(kcontext, delegated_cred, ccache);
571 krb5_free_principal(kcontext, princ);
573 snprintf(errstr, sizeof(errstr), "Failed to store credentials: %s",
574 krb5_get_err_text(kcontext, problem));
575 krb5_cc_destroy(kcontext, ccache);
576 return HTTP_INTERNAL_SERVER_ERROR;
579 krb5_cc_close(kcontext, ccache);
584 int authenticate_user_krb5pwd(request_rec *r,
585 kerb_auth_config *conf,
586 const char *auth_line)
588 const char *sent_pw = NULL;
589 const char *sent_name = NULL;
590 const char *realms = NULL;
591 krb5_context kcontext = NULL;
592 krb5_error_code code;
593 krb5_principal client = NULL;
594 krb5_ccache ccache = NULL;
595 krb5_keytab keytab = NULL;
599 code = krb5_init_context(&kcontext);
601 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
602 "Cannot initialize Kerberos5 context (%d)", code);
603 return HTTP_INTERNAL_SERVER_ERROR;
606 sent_pw = ap_pbase64decode(r->pool, auth_line);
607 sent_name = ap_getword (r->pool, &sent_pw, ':');
608 /* do not allow user to override realm setting of server */
609 if (strchr(sent_name, '@')) {
610 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
611 "specifying realm in user name is prohibited");
612 ret = HTTP_UNAUTHORIZED;
617 code = krb5_cc_gen_new(kcontext, &krb5_mcc_ops, &ccache);
619 code = krb5_mcc_generate_new(kcontext, &ccache);
622 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
623 "Cannot generate new ccache: %s",
624 krb5_get_err_text(kcontext, code));
625 ret = HTTP_INTERNAL_SERVER_ERROR;
629 if (conf->krb_5_keytab)
630 krb5_kt_resolve(kcontext, conf->krb_5_keytab, &keytab);
632 realms = conf->krb_auth_realms;
634 if (realms && (code = krb5_set_default_realm(kcontext,
635 ap_getword_white(r->pool, &realms))))
639 krb5_free_principal(kcontext, client);
642 code = krb5_parse_name(kcontext, sent_name, &client);
646 code = verify_krb5_user(r, kcontext, client, ccache, sent_pw, "khttp",
651 /* ap_getword_white() used above shifts the parameter, so it's not
652 needed to touch the realms variable */
653 } while (realms && *realms);
655 memset((char *)sent_pw, 0, strlen(sent_pw));
658 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
659 "Verifying krb5 password failed: %s",
660 krb5_get_err_text(kcontext, code));
661 ret = HTTP_UNAUTHORIZED;
665 code = krb5_unparse_name(kcontext, client, &name);
667 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "krb5_unparse_name() failed: %s",
668 krb5_get_err_text(kcontext, code));
669 ret = HTTP_UNAUTHORIZED;
672 MK_USER = ap_pstrdup (r->pool, name);
673 MK_AUTH_TYPE = "Basic";
676 if (conf->krb_save_credentials)
677 store_krb5_creds(kcontext, r, conf, ccache);
683 krb5_free_principal(kcontext, client);
685 krb5_cc_destroy(kcontext, ccache);
687 krb5_kt_close(kcontext, keytab);
688 krb5_free_context(kcontext);
693 /*********************************************************************
694 * GSSAPI Authentication
695 ********************************************************************/
698 get_gss_error(MK_POOL *p, OM_uint32 error_status, char *prefix)
700 OM_uint32 maj_stat, min_stat;
701 OM_uint32 msg_ctx = 0;
702 gss_buffer_desc status_string;
706 snprintf(buf, sizeof(buf), "%s", prefix);
709 maj_stat = gss_display_status (&min_stat,
715 if (sizeof(buf) > len + status_string.length + 1) {
716 sprintf(buf+len, ": %s", (char*) status_string.value);
717 len += status_string.length;
719 gss_release_buffer(&min_stat, &status_string);
720 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
722 return (ap_pstrdup(p, buf));
726 cleanup_gss_connection(void *data)
728 OM_uint32 minor_status;
729 gss_connection_t *gss_conn = (gss_connection_t *)data;
733 if (gss_conn->context != GSS_C_NO_CONTEXT)
734 gss_delete_sec_context(&minor_status, &gss_conn->context,
736 if (gss_conn->server_creds != GSS_C_NO_CREDENTIAL)
737 gss_release_cred(&minor_status, &gss_conn->server_creds);
739 gss_connection = NULL;
745 store_gss_creds(request_rec *r, kerb_auth_config *conf, char *princ_name,
746 gss_cred_id_t delegated_cred)
748 OM_uint32 maj_stat, min_stat;
749 krb5_principal princ = NULL;
750 krb5_ccache ccache = NULL;
751 krb5_error_code problem;
752 krb5_context context;
753 int ret = HTTP_INTERNAL_SERVER_ERROR;
755 problem = krb5_init_context(&context);
757 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Cannot initialize krb5 context");
758 return HTTP_INTERNAL_SERVER_ERROR;
761 problem = krb5_parse_name(context, princ_name, &princ);
763 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
764 "Cannot parse delegated username (%s)", krb5_get_err_text(context, problem));
768 problem = create_krb5_ccache(context, r, conf, princ, &ccache);
770 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
771 "Cannot create krb5 ccache (%s)", krb5_get_err_text(context, problem));
775 maj_stat = gss_krb5_copy_ccache(&min_stat, delegated_cred, ccache);
776 if (GSS_ERROR(maj_stat)) {
777 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
778 "Cannot store delegated credential (%s)",
779 get_gss_error(r->pool, min_stat, "gss_krb5_copy_ccache"));
783 krb5_cc_close(context, ccache);
789 krb5_free_principal(context, princ);
791 krb5_cc_destroy(context, ccache);
792 krb5_free_context(context);
797 get_gss_creds(request_rec *r,
798 kerb_auth_config *conf,
799 gss_cred_id_t *server_creds)
801 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
802 OM_uint32 major_status, minor_status, minor_status2;
803 gss_name_t server_name = GSS_C_NO_NAME;
806 snprintf(buf, sizeof(buf), "%s/%s", "khttp", ap_get_server_name(r));
808 input_token.value = buf;
809 input_token.length = strlen(buf) + 1;
811 major_status = gss_import_name(&minor_status, &input_token,
814 if (GSS_ERROR(major_status)) {
815 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
816 "%s", get_gss_error(r->pool, minor_status,
817 "gss_import_name() failed"));
818 return HTTP_INTERNAL_SERVER_ERROR;
821 major_status = gss_acquire_cred(&minor_status, server_name, GSS_C_INDEFINITE,
822 GSS_C_NO_OID_SET, GSS_C_ACCEPT,
823 server_creds, NULL, NULL);
824 gss_release_name(&minor_status2, &server_name);
825 if (GSS_ERROR(major_status)) {
826 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
827 "%s", get_gss_error(r->pool, minor_status,
828 "gss_acquire_cred() failed"));
829 return HTTP_INTERNAL_SERVER_ERROR;
836 authenticate_user_gss(request_rec *r,
837 kerb_auth_config *conf,
838 const char *auth_line)
840 OM_uint32 major_status, minor_status, minor_status2;
841 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
842 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
843 const char *auth_param = NULL;
845 gss_name_t client_name = GSS_C_NO_NAME;
846 gss_cred_id_t delegated_cred = GSS_C_NO_CREDENTIAL;
847 static int initial_return = HTTP_UNAUTHORIZED;
849 /* needed to work around replay caches */
850 if (!ap_is_initial_req(r))
851 return initial_return;
852 initial_return = HTTP_UNAUTHORIZED;
854 if (gss_connection == NULL) {
855 gss_connection = ap_pcalloc(r->connection->pool, sizeof(*gss_connection));
856 if (gss_connection == NULL) {
857 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
858 "ap_pcalloc() failed (not enough memory)");
859 ret = HTTP_INTERNAL_SERVER_ERROR;
862 memset(gss_connection, 0, sizeof(*gss_connection));
863 ap_register_cleanup(r->connection->pool, gss_connection, cleanup_gss_connection, ap_null_cleanup);
866 if (conf->krb_5_keytab)
867 /* use really strcat(), since the string passed to putenv() will become
868 * part of the enviroment and shouldn't be free()ed by apache */
869 putenv(strcat("KRB5_KTNAME=", conf->krb_5_keytab));
871 if (gss_connection->server_creds == GSS_C_NO_CREDENTIAL) {
872 ret = get_gss_creds(r, conf, &gss_connection->server_creds);
877 /* ap_getword() shifts parameter */
878 auth_param = ap_getword_white(r->pool, &auth_line);
879 if (auth_param == NULL) {
880 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
881 "No Authorization parameter in request from client");
882 ret = HTTP_UNAUTHORIZED;
886 input_token.length = ap_base64decode_len(auth_param) + 1;
887 input_token.value = ap_pcalloc(r->connection->pool, input_token.length);
888 if (input_token.value == NULL) {
889 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
890 "ap_pcalloc() failed (not enough memory)");
891 ret = HTTP_INTERNAL_SERVER_ERROR;
894 input_token.length = ap_base64decode(input_token.value, auth_param);
897 major_status = gss_accept_sec_context(
899 major_status = gss_accept_sec_context_spnego(
902 &gss_connection->context,
903 gss_connection->server_creds,
905 GSS_C_NO_CHANNEL_BINDINGS,
912 if (output_token.length) {
916 len = ap_base64encode_len(output_token.length) + 1;
917 token = ap_pcalloc(r->connection->pool, len + 1);
919 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
920 "ap_pcalloc() failed (not enough memory)");
921 ret = HTTP_INTERNAL_SERVER_ERROR;
922 gss_release_buffer(&minor_status2, &output_token);
925 ap_base64encode(token, output_token.value, output_token.length);
927 ap_table_set(r->err_headers_out, "WWW-Authenticate",
928 ap_pstrcat(r->pool, "Negotiate ", token, NULL));
929 gss_release_buffer(&minor_status2, &output_token);
932 if (GSS_ERROR(major_status)) {
933 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
934 "%s", get_gss_error(r->pool, minor_status,
935 "gss_accept_sec_context() failed"));
936 ret = HTTP_UNAUTHORIZED;
940 if (major_status & GSS_S_CONTINUE_NEEDED) {
941 /* Some GSSAPI mechanism (eg GSI from Globus) may require multiple
942 * iterations to establish authentication */
943 ret = HTTP_UNAUTHORIZED;
947 major_status = gss_display_name(&minor_status, client_name, &output_token, NULL);
948 gss_release_name(&minor_status, &client_name);
949 if (GSS_ERROR(major_status)) {
950 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
951 "%s", get_gss_error(r->pool, minor_status,
952 "gss_export_name() failed"));
953 ret = HTTP_INTERNAL_SERVER_ERROR;
957 MK_AUTH_TYPE = "Negotiate";
958 MK_USER = ap_pstrdup(r->pool, output_token.value);
960 if (conf->krb_save_credentials && delegated_cred != GSS_C_NO_CREDENTIAL)
961 store_gss_creds(r, conf, (char *)output_token.value, delegated_cred);
963 gss_release_buffer(&minor_status, &output_token);
969 gss_release_cred(&minor_status, &delegated_cred);
971 if (output_token.length)
972 gss_release_buffer(&minor_status, &output_token);
974 if (client_name != GSS_C_NO_NAME)
975 gss_release_name(&minor_status, &client_name);
977 cleanup_gss_connection(gss_connection);
979 initial_return = ret;
986 note_kerb_auth_failure(request_rec *r, const kerb_auth_config *conf,
987 int use_krb4, int use_krb5)
989 const char *auth_name = NULL;
992 /* get the user realm specified in .htaccess */
993 auth_name = ap_auth_name(r);
995 /* XXX should the WWW-Authenticate header be cleared first? */
997 if (use_krb5 && conf->krb_method_gssapi)
998 ap_table_add(r->err_headers_out, "WWW-Authenticate", "Negotiate ");
999 if (use_krb5 && conf->krb_method_k5pass) {
1000 ap_table_add(r->err_headers_out, "WWW-Authenticate",
1001 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1007 if (use_krb4 && conf->krb_method_k4pass && !set_basic)
1008 ap_table_add(r->err_headers_out, "WWW-Authenticate",
1009 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1013 int kerb_authenticate_user(request_rec *r)
1015 kerb_auth_config *conf =
1016 (kerb_auth_config *) ap_get_module_config(r->per_dir_config,
1018 const char *auth_type = NULL;
1019 const char *auth_line = NULL;
1020 const char *type = NULL;
1021 int use_krb5 = 0, use_krb4 = 0;
1024 /* get the type specified in .htaccess */
1025 type = ap_auth_type(r);
1027 if (type && strcasecmp(type, "Kerberos") == 0)
1028 use_krb5 = use_krb4 = 1;
1029 else if(type && strcasecmp(type, "KerberosV5") == 0)
1031 else if(type && strcasecmp(type, "KerberosV4") == 0)
1036 /* get what the user sent us in the HTTP header */
1037 auth_line = MK_TABLE_GET(r->headers_in, "Authorization");
1039 note_kerb_auth_failure(r, conf, use_krb4, use_krb5);
1040 return HTTP_UNAUTHORIZED;
1042 auth_type = ap_getword_white(r->pool, &auth_line);
1044 ret = HTTP_UNAUTHORIZED;
1047 if (use_krb5 && conf->krb_method_gssapi &&
1048 strcasecmp(auth_type, "Negotiate") == 0) {
1049 ret = authenticate_user_gss(r, conf, auth_line);
1050 } else if (use_krb5 && conf->krb_method_k5pass &&
1051 strcasecmp(auth_type, "Basic") == 0) {
1052 ret = authenticate_user_krb5pwd(r, conf, auth_line);
1057 if (ret == HTTP_UNAUTHORIZED && use_krb4 && conf->krb_method_k4pass &&
1058 strcasecmp(auth_type, "Basic") == 0)
1059 ret = authenticate_user_krb4pwd(r, conf, auth_line);
1062 if (ret == HTTP_UNAUTHORIZED)
1063 note_kerb_auth_failure(r, conf, use_krb4, use_krb5);
1069 /***************************************************************************
1070 Module Setup/Configuration
1071 ***************************************************************************/
1073 module MODULE_VAR_EXPORT auth_kerb_module = {
1074 STANDARD_MODULE_STUFF,
1075 NULL, /* module initializer */
1076 kerb_dir_create_config, /* per-directory config creator */
1077 NULL, /* per-directory config merger */
1078 NULL, /* per-server config creator */
1079 NULL, /* per-server config merger */
1080 kerb_auth_cmds, /* command table */
1081 NULL, /* [ 9] content handlers */
1082 NULL, /* [ 2] URI-to-filename translation */
1083 kerb_authenticate_user, /* [ 5] check/validate user_id */
1084 NULL, /* [ 6] check user_id is valid *here* */
1085 NULL, /* [ 4] check access by host address */
1086 NULL, /* [ 7] MIME type checker/setter */
1087 NULL, /* [ 8] fixups */
1088 NULL, /* [10] logger */
1089 NULL, /* [ 3] header parser */
1090 NULL, /* process initialization */
1091 NULL, /* process exit/cleanup */
1092 NULL /* [ 1] post read_request handling */
1096 kerb_init_handler(apr_pool_t *p, apr_pool_t *plog,
1097 apr_pool_t *ptemp, server_rec *s)
1099 ap_add_version_component(p, "mod_auth_kerb/" MODAUTHKERB_VERSION);
1103 void kerb_register_hooks(apr_pool_t *p)
1105 ap_hook_post_config(kerb_init_handler, NULL, NULL, APR_HOOK_MIDDLE);
1106 ap_hook_check_user_id(kerb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
1109 module AP_MODULE_DECLARE_DATA auth_kerb_module =
1111 STANDARD20_MODULE_STUFF,
1112 kerb_dir_create_config, /* create per-dir conf structures */
1113 NULL, /* merge per-dir conf structures */
1114 NULL, /* create per-server conf structures */
1115 NULL, /* merge per-server conf structures */
1116 kerb_auth_cmds, /* table of configuration directives */
1117 kerb_register_hooks /* register hooks */