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);
248 /* these functions also print out current errno (if not zero), resulting in
249 * lines of the format:
250 * (errno)strerror(errno): errstr
251 * This behaviour can be avoided by using APLOG_NOERRNO */
253 ap_log_rerror(file, line, level, r, "%s", errstr);
255 ap_log_rerror(file, line, level, status, r, "%s", errstr);
260 /***************************************************************************
261 Username/Password Validation for Krb4
262 ***************************************************************************/
264 verify_krb4_user(request_rec *r, char *name, char *instance, char *realm,
265 char *password, char *linstance, char *srvtab)
271 const char *hostname;
274 char lrealm[REALM_SZ];
276 ret = krb_get_pw_in_tkt(name, instance, realm, "krbtgt", realm,
277 DEFAULT_TKT_LIFE, password);
279 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
280 "Cannot get krb4 ticket: krb_get_pw_in_tkt() failed: %s",
281 krb_get_err_text(ret));
285 hostname = ap_get_server_name(r);
287 hp = gethostbyname(hostname);
290 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
291 "Cannot verify krb4 ticket: gethostbyname() failed: %s",
295 memcpy(&addr, hp->h_addr, sizeof(addr));
297 phost = krb_get_phost((char *)hostname);
299 krb_get_lrealm(lrealm, 1);
301 ret = krb_mk_req(&ticket, linstance, phost, lrealm, 0);
303 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
304 "Cannot verify krb4 ticket: krb_mk_req() failed: %s",
305 krb_get_err_text(ret));
310 ret = krb_rd_req(&ticket, linstance, phost, addr, &authdata, srvtab);
312 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
313 "Cannot verify krb4 ticket: krb_rd_req() failed: %s",
314 krb_get_err_text(ret));
322 krb4_cache_cleanup(void *data)
324 char *tkt_file = (char *) data;
326 krb_set_tkt_string(tkt_file);
332 authenticate_user_krb4pwd(request_rec *r,
333 kerb_auth_config *conf,
334 const char *auth_line)
338 const char *sent_name;
341 char *tkt_file_p = NULL;
346 char lrealm[REALM_SZ];
348 sent_pw = ap_pbase64decode(r->pool, auth_line);
349 sent_name = ap_getword (r->pool, &sent_pw, ':');
351 /* do not allow user to override realm setting of server */
352 if (strchr(sent_name, '@')) {
353 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
354 "specifying realm in user name is prohibited");
355 return HTTP_UNAUTHORIZED;
358 sent_instance = strchr(sent_name, '.');
360 *sent_instance++ = '\0';
362 snprintf(tkt_file, sizeof(tkt_file), "/tmp/apache_tkt_XXXXXX");
363 fd = mkstemp(tkt_file);
365 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
366 "Cannot create krb4 ccache: mkstemp() failed: %s",
368 return HTTP_INTERNAL_SERVER_ERROR;
371 tkt_file_p = ap_pstrdup(r->pool, tkt_file);
372 ap_register_cleanup(r->pool, tkt_file_p,
373 krb4_cache_cleanup, ap_null_cleanup);
375 krb_set_tkt_string(tkt_file);
377 realms = conf->krb_auth_realms;
379 memset(lrealm, 0, sizeof(lrealm));
382 realm = ap_getword_white(r->pool, &realms);
385 ret = krb_get_lrealm(lrealm, 1);
388 if (realm == NULL || *realm == '\0')
391 ret = verify_krb4_user(r, (char *)sent_name,
392 (sent_instance) ? sent_instance : "",
393 (char *)realm, (char *)sent_pw, "khttp",
397 } while (realms && *realms);
400 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Verifying krb4 password failed");
401 ret = HTTP_UNAUTHORIZED;
405 user = ap_pstrdup(r->pool, sent_name);
407 user = ap_pstrcat(r->pool, user, ".", sent_instance, NULL);
408 user = ap_pstrcat(r->pool, user, "@", realm, NULL);
411 MK_AUTH_TYPE = "Basic";
412 ap_table_setn(r->subprocess_env, "KRBTKFILE", tkt_file_p);
414 if (!conf->krb_save_credentials)
415 krb4_cache_cleanup(tkt_file);
419 krb4_cache_cleanup(tkt_file);
428 /***************************************************************************
429 Username/Password Validation for Krb5
430 ***************************************************************************/
431 /* Inspired by krb5_verify_user from Heimdal */
432 static krb5_error_code
433 verify_krb5_user(request_rec *r, krb5_context context, krb5_principal principal,
434 krb5_ccache ccache, const char *password, const char *service,
438 krb5_principal server = NULL;
440 krb5_verify_init_creds_opt opt;
442 memset(&creds, 0, sizeof(creds));
444 ret = krb5_get_init_creds_password(context, &creds, principal,
445 (char *)password, krb5_prompter_posix,
446 NULL, 0, NULL, NULL);
450 ret = krb5_sname_to_principal(context, ap_get_server_name(r), service,
451 KRB5_NT_SRV_HST, &server);
455 krb5_verify_init_creds_opt_init(&opt);
456 krb5_verify_init_creds_opt_set_ap_req_nofail(&opt, 1);
458 ret = krb5_verify_init_creds(context, &creds, server, keytab, NULL, &opt);
463 ret = krb5_cc_initialize(context, ccache, principal);
465 ret = krb5_cc_store_cred(context, ccache, &creds);
469 krb5_free_cred_contents(context, &creds);
471 krb5_free_principal(context, server);
476 krb5_cache_cleanup(void *data)
478 krb5_context context;
480 krb5_error_code problem;
481 char *cache_name = (char *) data;
483 problem = krb5_init_context(&context);
485 /* ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "krb5_init_context() failed"); */
486 return HTTP_INTERNAL_SERVER_ERROR;
489 problem = krb5_cc_resolve(context, cache_name, &cache);
491 /* log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
492 "krb5_cc_resolve() failed (%s: %s)",
493 cache_name, krb5_get_err_text(context, problem)); */
494 return HTTP_INTERNAL_SERVER_ERROR;
497 krb5_cc_destroy(context, cache);
498 krb5_free_context(context);
503 create_krb5_ccache(krb5_context kcontext,
505 kerb_auth_config *conf,
506 krb5_principal princ,
510 krb5_error_code problem;
512 krb5_ccache tmp_ccache = NULL;
515 /* new MIT krb5-1.3.x also supports this call */
516 problem = krb5_cc_gen_new(kcontext, &krb5_fcc_ops, &tmp_ccache);
518 problem = krb5_fcc_generate_new(kcontext, &tmp_ccache);
519 /* krb5_fcc_generate_new() doesn't set KRB5_TC_OPENCLOSE, which makes
520 krb5_cc_initialize() fail */
521 krb5_fcc_set_flags(kcontext, tmp_ccache, KRB5_TC_OPENCLOSE);
524 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
525 "Cannot create file for new krb5 ccache: %s",
526 krb5_get_err_text(kcontext, problem));
527 ret = HTTP_INTERNAL_SERVER_ERROR;
531 ccname = ap_pstrdup(r->pool, krb5_cc_get_name(kcontext, tmp_ccache));
533 problem = krb5_cc_initialize(kcontext, tmp_ccache, princ);
535 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
536 "Cannot initialize krb5 ccache %s: krb5_cc_initialize() failed: %s",
537 ccname, krb5_get_err_text(kcontext, problem));
538 ret = HTTP_INTERNAL_SERVER_ERROR;
542 ap_table_setn(r->subprocess_env, "KRB5CCNAME", ccname);
543 ap_register_cleanup(r->pool, ccname,
544 krb5_cache_cleanup, ap_null_cleanup);
546 *ccache = tmp_ccache;
553 krb5_cc_destroy(kcontext, tmp_ccache);
559 store_krb5_creds(krb5_context kcontext,
561 kerb_auth_config *conf,
562 krb5_ccache delegated_cred)
565 krb5_error_code problem;
566 krb5_principal princ;
570 problem = krb5_cc_get_principal(kcontext, delegated_cred, &princ);
572 snprintf(errstr, sizeof(errstr), "krb5_cc_get_principal() failed: %s",
573 krb5_get_err_text(kcontext, problem));
574 return HTTP_INTERNAL_SERVER_ERROR;
577 ret = create_krb5_ccache(kcontext, r, conf, princ, &ccache);
579 krb5_free_principal(kcontext, princ);
584 problem = krb5_cc_copy_cache(kcontext, delegated_cred, ccache);
586 problem = krb5_cc_copy_creds(kcontext, delegated_cred, ccache);
588 krb5_free_principal(kcontext, princ);
590 snprintf(errstr, sizeof(errstr), "Failed to store credentials: %s",
591 krb5_get_err_text(kcontext, problem));
592 krb5_cc_destroy(kcontext, ccache);
593 return HTTP_INTERNAL_SERVER_ERROR;
596 krb5_cc_close(kcontext, ccache);
601 int authenticate_user_krb5pwd(request_rec *r,
602 kerb_auth_config *conf,
603 const char *auth_line)
605 const char *sent_pw = NULL;
606 const char *sent_name = NULL;
607 const char *realms = NULL;
608 krb5_context kcontext = NULL;
609 krb5_error_code code;
610 krb5_principal client = NULL;
611 krb5_ccache ccache = NULL;
612 krb5_keytab keytab = NULL;
616 code = krb5_init_context(&kcontext);
618 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
619 "Cannot initialize Kerberos5 context (%d)", code);
620 return HTTP_INTERNAL_SERVER_ERROR;
623 sent_pw = ap_pbase64decode(r->pool, auth_line);
624 sent_name = ap_getword (r->pool, &sent_pw, ':');
625 /* do not allow user to override realm setting of server */
626 if (strchr(sent_name, '@')) {
627 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
628 "specifying realm in user name is prohibited");
629 ret = HTTP_UNAUTHORIZED;
634 code = krb5_cc_gen_new(kcontext, &krb5_mcc_ops, &ccache);
636 code = krb5_mcc_generate_new(kcontext, &ccache);
639 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
640 "Cannot generate new ccache: %s",
641 krb5_get_err_text(kcontext, code));
642 ret = HTTP_INTERNAL_SERVER_ERROR;
646 if (conf->krb_5_keytab)
647 krb5_kt_resolve(kcontext, conf->krb_5_keytab, &keytab);
649 realms = conf->krb_auth_realms;
651 if (realms && (code = krb5_set_default_realm(kcontext,
652 ap_getword_white(r->pool, &realms))))
656 krb5_free_principal(kcontext, client);
659 code = krb5_parse_name(kcontext, sent_name, &client);
663 code = verify_krb5_user(r, kcontext, client, ccache, sent_pw, "khttp",
668 /* ap_getword_white() used above shifts the parameter, so it's not
669 needed to touch the realms variable */
670 } while (realms && *realms);
672 memset((char *)sent_pw, 0, strlen(sent_pw));
675 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
676 "Verifying krb5 password failed: %s",
677 krb5_get_err_text(kcontext, code));
678 ret = HTTP_UNAUTHORIZED;
682 code = krb5_unparse_name(kcontext, client, &name);
684 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "krb5_unparse_name() failed: %s",
685 krb5_get_err_text(kcontext, code));
686 ret = HTTP_UNAUTHORIZED;
689 MK_USER = ap_pstrdup (r->pool, name);
690 MK_AUTH_TYPE = "Basic";
693 if (conf->krb_save_credentials)
694 store_krb5_creds(kcontext, r, conf, ccache);
700 krb5_free_principal(kcontext, client);
702 krb5_cc_destroy(kcontext, ccache);
704 krb5_kt_close(kcontext, keytab);
705 krb5_free_context(kcontext);
710 /*********************************************************************
711 * GSSAPI Authentication
712 ********************************************************************/
715 get_gss_error(MK_POOL *p, OM_uint32 error_status, char *prefix)
717 OM_uint32 maj_stat, min_stat;
718 OM_uint32 msg_ctx = 0;
719 gss_buffer_desc status_string;
723 snprintf(buf, sizeof(buf), "%s", prefix);
726 maj_stat = gss_display_status (&min_stat,
732 if (sizeof(buf) > len + status_string.length + 1) {
733 sprintf(buf+len, ": %s", (char*) status_string.value);
734 len += status_string.length;
736 gss_release_buffer(&min_stat, &status_string);
737 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
739 return (ap_pstrdup(p, buf));
743 cleanup_gss_connection(void *data)
745 OM_uint32 minor_status;
746 gss_connection_t *gss_conn = (gss_connection_t *)data;
750 if (gss_conn->context != GSS_C_NO_CONTEXT)
751 gss_delete_sec_context(&minor_status, &gss_conn->context,
753 if (gss_conn->server_creds != GSS_C_NO_CREDENTIAL)
754 gss_release_cred(&minor_status, &gss_conn->server_creds);
756 gss_connection = NULL;
762 store_gss_creds(request_rec *r, kerb_auth_config *conf, char *princ_name,
763 gss_cred_id_t delegated_cred)
765 OM_uint32 maj_stat, min_stat;
766 krb5_principal princ = NULL;
767 krb5_ccache ccache = NULL;
768 krb5_error_code problem;
769 krb5_context context;
770 int ret = HTTP_INTERNAL_SERVER_ERROR;
772 problem = krb5_init_context(&context);
774 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Cannot initialize krb5 context");
775 return HTTP_INTERNAL_SERVER_ERROR;
778 problem = krb5_parse_name(context, princ_name, &princ);
780 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
781 "Cannot parse delegated username (%s)", krb5_get_err_text(context, problem));
785 problem = create_krb5_ccache(context, r, conf, princ, &ccache);
787 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
788 "Cannot create krb5 ccache (%s)", krb5_get_err_text(context, problem));
792 maj_stat = gss_krb5_copy_ccache(&min_stat, delegated_cred, ccache);
793 if (GSS_ERROR(maj_stat)) {
794 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
795 "Cannot store delegated credential (%s)",
796 get_gss_error(r->pool, min_stat, "gss_krb5_copy_ccache"));
800 krb5_cc_close(context, ccache);
806 krb5_free_principal(context, princ);
808 krb5_cc_destroy(context, ccache);
809 krb5_free_context(context);
814 get_gss_creds(request_rec *r,
815 kerb_auth_config *conf,
816 gss_cred_id_t *server_creds)
818 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
819 OM_uint32 major_status, minor_status, minor_status2;
820 gss_name_t server_name = GSS_C_NO_NAME;
823 snprintf(buf, sizeof(buf), "%s/%s", "khttp", ap_get_server_name(r));
825 input_token.value = buf;
826 input_token.length = strlen(buf) + 1;
828 major_status = gss_import_name(&minor_status, &input_token,
831 if (GSS_ERROR(major_status)) {
832 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
833 "%s", get_gss_error(r->pool, minor_status,
834 "gss_import_name() failed"));
835 return HTTP_INTERNAL_SERVER_ERROR;
838 major_status = gss_acquire_cred(&minor_status, server_name, GSS_C_INDEFINITE,
839 GSS_C_NO_OID_SET, GSS_C_ACCEPT,
840 server_creds, NULL, NULL);
841 gss_release_name(&minor_status2, &server_name);
842 if (GSS_ERROR(major_status)) {
843 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
844 "%s", get_gss_error(r->pool, minor_status,
845 "gss_acquire_cred() failed"));
846 return HTTP_INTERNAL_SERVER_ERROR;
853 authenticate_user_gss(request_rec *r,
854 kerb_auth_config *conf,
855 const char *auth_line)
857 OM_uint32 major_status, minor_status, minor_status2;
858 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
859 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
860 const char *auth_param = NULL;
862 gss_name_t client_name = GSS_C_NO_NAME;
863 gss_cred_id_t delegated_cred = GSS_C_NO_CREDENTIAL;
864 static int initial_return = HTTP_UNAUTHORIZED;
866 /* needed to work around replay caches */
867 if (!ap_is_initial_req(r))
868 return initial_return;
869 initial_return = HTTP_UNAUTHORIZED;
871 if (gss_connection == NULL) {
872 gss_connection = ap_pcalloc(r->connection->pool, sizeof(*gss_connection));
873 if (gss_connection == NULL) {
874 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
875 "ap_pcalloc() failed (not enough memory)");
876 ret = HTTP_INTERNAL_SERVER_ERROR;
879 memset(gss_connection, 0, sizeof(*gss_connection));
880 ap_register_cleanup(r->connection->pool, gss_connection, cleanup_gss_connection, ap_null_cleanup);
883 if (conf->krb_5_keytab) {
885 /* we don't use the ap_* calls here, since the string passed to putenv()
886 * will become part of the enviroment and shouldn't be free()ed by apache
888 ktname = malloc(strlen("KRB5_KTNAME=") + strlen(conf->krb_5_keytab) + 1);
889 if (ktname == NULL) {
890 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "malloc() failed: not enough memory");
891 ret = HTTP_INTERNAL_SERVER_ERROR;
894 sprintf(ktname, "KRB5_KTNAME=%s", conf->krb_5_keytab);
898 if (gss_connection->server_creds == GSS_C_NO_CREDENTIAL) {
899 ret = get_gss_creds(r, conf, &gss_connection->server_creds);
904 /* ap_getword() shifts parameter */
905 auth_param = ap_getword_white(r->pool, &auth_line);
906 if (auth_param == NULL) {
907 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
908 "No Authorization parameter in request from client");
909 ret = HTTP_UNAUTHORIZED;
913 input_token.length = ap_base64decode_len(auth_param) + 1;
914 input_token.value = ap_pcalloc(r->connection->pool, input_token.length);
915 if (input_token.value == NULL) {
916 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
917 "ap_pcalloc() failed (not enough memory)");
918 ret = HTTP_INTERNAL_SERVER_ERROR;
921 input_token.length = ap_base64decode(input_token.value, auth_param);
924 major_status = gss_accept_sec_context(
926 major_status = gss_accept_sec_context_spnego(
929 &gss_connection->context,
930 gss_connection->server_creds,
932 GSS_C_NO_CHANNEL_BINDINGS,
939 if (output_token.length) {
943 len = ap_base64encode_len(output_token.length) + 1;
944 token = ap_pcalloc(r->connection->pool, len + 1);
946 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
947 "ap_pcalloc() failed (not enough memory)");
948 ret = HTTP_INTERNAL_SERVER_ERROR;
949 gss_release_buffer(&minor_status2, &output_token);
952 ap_base64encode(token, output_token.value, output_token.length);
954 ap_table_set(r->err_headers_out, "WWW-Authenticate",
955 ap_pstrcat(r->pool, "Negotiate ", token, NULL));
956 gss_release_buffer(&minor_status2, &output_token);
959 if (GSS_ERROR(major_status)) {
960 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
961 "%s", get_gss_error(r->pool, minor_status,
962 "gss_accept_sec_context() failed"));
963 ret = HTTP_UNAUTHORIZED;
967 if (major_status & GSS_S_CONTINUE_NEEDED) {
968 /* Some GSSAPI mechanism (eg GSI from Globus) may require multiple
969 * iterations to establish authentication */
970 ret = HTTP_UNAUTHORIZED;
974 major_status = gss_display_name(&minor_status, client_name, &output_token, NULL);
975 gss_release_name(&minor_status, &client_name);
976 if (GSS_ERROR(major_status)) {
977 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
978 "%s", get_gss_error(r->pool, minor_status,
979 "gss_export_name() failed"));
980 ret = HTTP_INTERNAL_SERVER_ERROR;
984 MK_AUTH_TYPE = "Negotiate";
985 MK_USER = ap_pstrdup(r->pool, output_token.value);
987 if (conf->krb_save_credentials && delegated_cred != GSS_C_NO_CREDENTIAL)
988 store_gss_creds(r, conf, (char *)output_token.value, delegated_cred);
990 gss_release_buffer(&minor_status, &output_token);
996 gss_release_cred(&minor_status, &delegated_cred);
998 if (output_token.length)
999 gss_release_buffer(&minor_status, &output_token);
1001 if (client_name != GSS_C_NO_NAME)
1002 gss_release_name(&minor_status, &client_name);
1004 cleanup_gss_connection(gss_connection);
1006 initial_return = ret;
1013 note_kerb_auth_failure(request_rec *r, const kerb_auth_config *conf,
1014 int use_krb4, int use_krb5)
1016 const char *auth_name = NULL;
1019 /* get the user realm specified in .htaccess */
1020 auth_name = ap_auth_name(r);
1022 /* XXX should the WWW-Authenticate header be cleared first? */
1024 if (use_krb5 && conf->krb_method_gssapi)
1025 ap_table_add(r->err_headers_out, "WWW-Authenticate", "Negotiate");
1026 if (use_krb5 && conf->krb_method_k5pass) {
1027 ap_table_add(r->err_headers_out, "WWW-Authenticate",
1028 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1034 if (use_krb4 && conf->krb_method_k4pass && !set_basic)
1035 ap_table_add(r->err_headers_out, "WWW-Authenticate",
1036 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1040 int kerb_authenticate_user(request_rec *r)
1042 kerb_auth_config *conf =
1043 (kerb_auth_config *) ap_get_module_config(r->per_dir_config,
1045 const char *auth_type = NULL;
1046 const char *auth_line = NULL;
1047 const char *type = NULL;
1048 int use_krb5 = 0, use_krb4 = 0;
1051 /* get the type specified in .htaccess */
1052 type = ap_auth_type(r);
1054 if (type && strcasecmp(type, "Kerberos") == 0)
1055 use_krb5 = use_krb4 = 1;
1056 else if(type && strcasecmp(type, "KerberosV5") == 0)
1058 else if(type && strcasecmp(type, "KerberosV4") == 0)
1063 /* get what the user sent us in the HTTP header */
1064 auth_line = MK_TABLE_GET(r->headers_in, "Authorization");
1066 note_kerb_auth_failure(r, conf, use_krb4, use_krb5);
1067 return HTTP_UNAUTHORIZED;
1069 auth_type = ap_getword_white(r->pool, &auth_line);
1071 ret = HTTP_UNAUTHORIZED;
1074 if (use_krb5 && conf->krb_method_gssapi &&
1075 strcasecmp(auth_type, "Negotiate") == 0) {
1076 ret = authenticate_user_gss(r, conf, auth_line);
1077 } else if (use_krb5 && conf->krb_method_k5pass &&
1078 strcasecmp(auth_type, "Basic") == 0) {
1079 ret = authenticate_user_krb5pwd(r, conf, auth_line);
1084 if (ret == HTTP_UNAUTHORIZED && use_krb4 && conf->krb_method_k4pass &&
1085 strcasecmp(auth_type, "Basic") == 0)
1086 ret = authenticate_user_krb4pwd(r, conf, auth_line);
1089 if (ret == HTTP_UNAUTHORIZED)
1090 note_kerb_auth_failure(r, conf, use_krb4, use_krb5);
1096 /***************************************************************************
1097 Module Setup/Configuration
1098 ***************************************************************************/
1100 module MODULE_VAR_EXPORT auth_kerb_module = {
1101 STANDARD_MODULE_STUFF,
1102 NULL, /* module initializer */
1103 kerb_dir_create_config, /* per-directory config creator */
1104 NULL, /* per-directory config merger */
1105 NULL, /* per-server config creator */
1106 NULL, /* per-server config merger */
1107 kerb_auth_cmds, /* command table */
1108 NULL, /* [ 9] content handlers */
1109 NULL, /* [ 2] URI-to-filename translation */
1110 kerb_authenticate_user, /* [ 5] check/validate user_id */
1111 NULL, /* [ 6] check user_id is valid *here* */
1112 NULL, /* [ 4] check access by host address */
1113 NULL, /* [ 7] MIME type checker/setter */
1114 NULL, /* [ 8] fixups */
1115 NULL, /* [10] logger */
1116 NULL, /* [ 3] header parser */
1117 NULL, /* process initialization */
1118 NULL, /* process exit/cleanup */
1119 NULL /* [ 1] post read_request handling */
1123 kerb_init_handler(apr_pool_t *p, apr_pool_t *plog,
1124 apr_pool_t *ptemp, server_rec *s)
1126 ap_add_version_component(p, "mod_auth_kerb/" MODAUTHKERB_VERSION);
1130 void kerb_register_hooks(apr_pool_t *p)
1132 ap_hook_post_config(kerb_init_handler, NULL, NULL, APR_HOOK_MIDDLE);
1133 ap_hook_check_user_id(kerb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
1136 module AP_MODULE_DECLARE_DATA auth_kerb_module =
1138 STANDARD20_MODULE_STUFF,
1139 kerb_dir_create_config, /* create per-dir conf structures */
1140 NULL, /* merge per-dir conf structures */
1141 NULL, /* create per-server conf structures */
1142 NULL, /* merge per-server conf structures */
1143 kerb_auth_cmds, /* table of configuration directives */
1144 kerb_register_hooks /* register hooks */