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_rerror(APLOG_MARK, APLOG_ERR, 0, r,
276 "Cannot get krb4 ticket: krb_get_pw_in_tkt() failed: %s",
277 krb_get_err_text(ret));
281 hostname = ap_get_server_name(r);
283 hp = gethostbyname(hostname);
286 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
287 "Cannot verify krb4 ticket: gethostbyname() failed: %s",
291 memcpy(&addr, hp->h_addr, sizeof(addr));
293 phost = krb_get_phost((char *)hostname);
295 krb_get_lrealm(lrealm, 1);
297 ret = krb_mk_req(&ticket, linstance, phost, lrealm, 0);
299 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
300 "Cannot verify krb4 ticket: krb_mk_req() failed: %s",
301 krb_get_err_text(ret));
306 ret = krb_rd_req(&ticket, linstance, phost, addr, &authdata, srvtab);
308 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
309 "Cannot verify krb4 ticket: krb_rd_req() failed: %s",
310 krb_get_err_text(ret));
318 krb4_cache_cleanup(void *data)
320 char *tkt_file = (char *) data;
322 krb_set_tkt_string(tkt_file);
328 authenticate_user_krb4pwd(request_rec *r,
329 kerb_auth_config *conf,
330 const char *auth_line)
334 const char *sent_name;
337 char *tkt_file_p = NULL;
342 char lrealm[REALM_SZ];
344 sent_pw = ap_pbase64decode(r->pool, auth_line);
345 sent_name = ap_getword (r->pool, &sent_pw, ':');
347 /* do not allow user to override realm setting of server */
348 if (strchr(sent_name, '@')) {
349 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
350 "specifying realm in user name is prohibited");
351 return HTTP_UNAUTHORIZED;
354 sent_instance = strchr(sent_name, '.');
356 *sent_instance++ = '\0';
358 snprintf(tkt_file, sizeof(tkt_file), "/tmp/apache_tkt_XXXXXX");
359 fd = mkstemp(tkt_file);
361 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
362 "Cannot create krb4 ccache: mkstemp() failed: %s",
364 return HTTP_INTERNAL_SERVER_ERROR;
367 tkt_file_p = ap_pstrdup(r->pool, tkt_file);
368 ap_register_cleanup(r->pool, tkt_file_p,
369 krb4_cache_cleanup, ap_null_cleanup);
371 krb_set_tkt_string(tkt_file);
373 realms = conf->krb_auth_realms;
375 memset(lrealm, 0, sizeof(lrealm));
378 realm = ap_getword_white(r->pool, &realms);
381 ret = krb_get_lrealm(lrealm, 1);
384 if (realm == NULL || *realm == '\0')
387 ret = verify_krb4_user(r, (char *)sent_name,
388 (sent_instance) ? sent_instance : "",
389 (char *)realm, (char *)sent_pw, "khttp",
393 } while (realms && *realms);
396 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Verifying krb4 password failed");
397 ret = HTTP_UNAUTHORIZED;
401 user = ap_pstrdup(r->pool, sent_name);
403 user = ap_pstrcat(r->pool, user, ".", sent_instance, NULL);
404 user = ap_pstrcat(r->pool, user, "@", realm, NULL);
407 MK_AUTH_TYPE = "Basic";
408 ap_table_setn(r->subprocess_env, "KRBTKFILE", tkt_file_p);
410 if (!conf->krb_save_credentials)
411 krb4_cache_cleanup(tkt_file);
415 krb4_cache_cleanup(tkt_file);
424 /***************************************************************************
425 Username/Password Validation for Krb5
426 ***************************************************************************/
427 /* Inspired by krb5_verify_user from Heimdal */
428 static krb5_error_code
429 verify_krb5_user(request_rec *r, krb5_context context, krb5_principal principal,
430 krb5_ccache ccache, const char *password, const char *service,
434 krb5_principal server = NULL;
436 krb5_verify_init_creds_opt opt;
438 memset(&creds, 0, sizeof(creds));
440 ret = krb5_get_init_creds_password(context, &creds, principal,
441 (char *)password, krb5_prompter_posix,
442 NULL, 0, NULL, NULL);
446 ret = krb5_sname_to_principal(context, ap_get_server_name(r), service,
447 KRB5_NT_SRV_HST, &server);
451 krb5_verify_init_creds_opt_init(&opt);
452 krb5_verify_init_creds_opt_set_ap_req_nofail(&opt, 1);
454 ret = krb5_verify_init_creds(context, &creds, server, keytab, NULL, &opt);
459 ret = krb5_cc_initialize(context, ccache, principal);
461 ret = krb5_cc_store_cred(context, ccache, &creds);
465 krb5_free_cred_contents(context, &creds);
467 krb5_free_principal(context, server);
472 krb5_cache_cleanup(void *data)
474 krb5_context context;
476 krb5_error_code problem;
477 char *cache_name = (char *) data;
479 problem = krb5_init_context(&context);
481 /* ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "krb5_init_context() failed"); */
482 return HTTP_INTERNAL_SERVER_ERROR;
485 problem = krb5_cc_resolve(context, cache_name, &cache);
487 /* log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
488 "krb5_cc_resolve() failed (%s: %s)",
489 cache_name, krb5_get_err_text(context, problem)); */
490 return HTTP_INTERNAL_SERVER_ERROR;
493 krb5_cc_destroy(context, cache);
494 krb5_free_context(context);
499 create_krb5_ccache(krb5_context kcontext,
501 kerb_auth_config *conf,
502 krb5_principal princ,
506 krb5_error_code problem;
508 krb5_ccache tmp_ccache = NULL;
511 problem = krb5_cc_gen_new(kcontext, &krb5_fcc_ops, &tmp_ccache);
513 problem = krb5_fcc_generate_new(kcontext, &tmp_ccache);
514 /* krb5_fcc_generate_new() doesn't set KRB5_TC_OPENCLOSE, which makes
515 krb5_cc_initialize() fail */
516 krb5_fcc_set_flags(kcontext, tmp_ccache, KRB5_TC_OPENCLOSE);
519 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
520 "Cannot create file for new krb5 ccache: %s",
521 krb5_get_err_text(kcontext, problem));
522 ret = HTTP_INTERNAL_SERVER_ERROR;
526 ccname = ap_pstrdup(r->pool, krb5_cc_get_name(kcontext, tmp_ccache));
528 problem = krb5_cc_initialize(kcontext, tmp_ccache, princ);
530 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
531 "Cannot initialize krb5 ccache %s: krb5_cc_initialize() failed: %s",
532 ccname, krb5_get_err_text(kcontext, problem));
533 ret = HTTP_INTERNAL_SERVER_ERROR;
537 ap_table_setn(r->subprocess_env, "KRB5CCNAME", ccname);
538 ap_register_cleanup(r->pool, ccname,
539 krb5_cache_cleanup, ap_null_cleanup);
541 *ccache = tmp_ccache;
548 krb5_cc_destroy(kcontext, tmp_ccache);
554 store_krb5_creds(krb5_context kcontext,
556 kerb_auth_config *conf,
557 krb5_ccache delegated_cred)
560 krb5_error_code problem;
561 krb5_principal princ;
565 problem = krb5_cc_get_principal(kcontext, delegated_cred, &princ);
567 snprintf(errstr, sizeof(errstr), "krb5_cc_get_principal() failed: %s",
568 krb5_get_err_text(kcontext, problem));
569 return HTTP_INTERNAL_SERVER_ERROR;
572 ret = create_krb5_ccache(kcontext, r, conf, princ, &ccache);
574 krb5_free_principal(kcontext, princ);
579 problem = krb5_cc_copy_cache(kcontext, delegated_cred, ccache);
581 problem = krb5_cc_copy_creds(kcontext, delegated_cred, ccache);
583 krb5_free_principal(kcontext, princ);
585 snprintf(errstr, sizeof(errstr), "Failed to store credentials: %s",
586 krb5_get_err_text(kcontext, problem));
587 krb5_cc_destroy(kcontext, ccache);
588 return HTTP_INTERNAL_SERVER_ERROR;
591 krb5_cc_close(kcontext, ccache);
596 int authenticate_user_krb5pwd(request_rec *r,
597 kerb_auth_config *conf,
598 const char *auth_line)
600 const char *sent_pw = NULL;
601 const char *sent_name = NULL;
602 const char *realms = NULL;
603 krb5_context kcontext = NULL;
604 krb5_error_code code;
605 krb5_principal client = NULL;
606 krb5_ccache ccache = NULL;
607 krb5_keytab keytab = NULL;
611 code = krb5_init_context(&kcontext);
613 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
614 "Cannot initialize Kerberos5 context (%d)", code);
615 return HTTP_INTERNAL_SERVER_ERROR;
618 sent_pw = ap_pbase64decode(r->pool, auth_line);
619 sent_name = ap_getword (r->pool, &sent_pw, ':');
620 /* do not allow user to override realm setting of server */
621 if (strchr(sent_name, '@')) {
622 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
623 "specifying realm in user name is prohibited");
624 ret = HTTP_UNAUTHORIZED;
629 code = krb5_cc_gen_new(kcontext, &krb5_mcc_ops, &ccache);
631 code = krb5_mcc_generate_new(kcontext, &ccache);
634 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
635 "Cannot generate new ccache: %s",
636 krb5_get_err_text(kcontext, code));
637 ret = HTTP_INTERNAL_SERVER_ERROR;
641 if (conf->krb_5_keytab)
642 krb5_kt_resolve(kcontext, conf->krb_5_keytab, &keytab);
644 realms = conf->krb_auth_realms;
646 if (realms && (code = krb5_set_default_realm(kcontext,
647 ap_getword_white(r->pool, &realms))))
651 krb5_free_principal(kcontext, client);
654 code = krb5_parse_name(kcontext, sent_name, &client);
658 code = verify_krb5_user(r, kcontext, client, ccache, sent_pw, "khttp",
663 /* ap_getword_white() used above shifts the parameter, so it's not
664 needed to touch the realms variable */
665 } while (realms && *realms);
667 memset((char *)sent_pw, 0, strlen(sent_pw));
670 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
671 "Verifying krb5 password failed: %s",
672 krb5_get_err_text(kcontext, code));
673 ret = HTTP_UNAUTHORIZED;
677 code = krb5_unparse_name(kcontext, client, &name);
679 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "krb5_unparse_name() failed: %s",
680 krb5_get_err_text(kcontext, code));
681 ret = HTTP_UNAUTHORIZED;
684 MK_USER = ap_pstrdup (r->pool, name);
685 MK_AUTH_TYPE = "Basic";
688 if (conf->krb_save_credentials)
689 store_krb5_creds(kcontext, r, conf, ccache);
695 krb5_free_principal(kcontext, client);
697 krb5_cc_destroy(kcontext, ccache);
699 krb5_kt_close(kcontext, keytab);
700 krb5_free_context(kcontext);
705 /*********************************************************************
706 * GSSAPI Authentication
707 ********************************************************************/
710 get_gss_error(MK_POOL *p, OM_uint32 error_status, char *prefix)
712 OM_uint32 maj_stat, min_stat;
713 OM_uint32 msg_ctx = 0;
714 gss_buffer_desc status_string;
718 snprintf(buf, sizeof(buf), "%s", prefix);
721 maj_stat = gss_display_status (&min_stat,
727 if (sizeof(buf) > len + status_string.length + 1) {
728 sprintf(buf+len, ": %s", (char*) status_string.value);
729 len += status_string.length;
731 gss_release_buffer(&min_stat, &status_string);
732 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
734 return (ap_pstrdup(p, buf));
738 cleanup_gss_connection(void *data)
740 OM_uint32 minor_status;
741 gss_connection_t *gss_conn = (gss_connection_t *)data;
745 if (gss_conn->context != GSS_C_NO_CONTEXT)
746 gss_delete_sec_context(&minor_status, &gss_conn->context,
748 if (gss_conn->server_creds != GSS_C_NO_CREDENTIAL)
749 gss_release_cred(&minor_status, &gss_conn->server_creds);
751 gss_connection = NULL;
757 store_gss_creds(request_rec *r, kerb_auth_config *conf, char *princ_name,
758 gss_cred_id_t delegated_cred)
760 OM_uint32 maj_stat, min_stat;
761 krb5_principal princ = NULL;
762 krb5_ccache ccache = NULL;
763 krb5_error_code problem;
764 krb5_context context;
765 int ret = HTTP_INTERNAL_SERVER_ERROR;
767 problem = krb5_init_context(&context);
769 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Cannot initialize krb5 context");
770 return HTTP_INTERNAL_SERVER_ERROR;
773 problem = krb5_parse_name(context, princ_name, &princ);
775 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
776 "Cannot parse delegated username (%s)", krb5_get_err_text(context, problem));
780 problem = create_krb5_ccache(context, r, conf, princ, &ccache);
782 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
783 "Cannot create krb5 ccache (%s)", krb5_get_err_text(context, problem));
787 maj_stat = gss_krb5_copy_ccache(&min_stat, delegated_cred, ccache);
788 if (GSS_ERROR(maj_stat)) {
789 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
790 "Cannot store delegated credential (%s)",
791 get_gss_error(r->pool, min_stat, "gss_krb5_copy_ccache"));
795 krb5_cc_close(context, ccache);
801 krb5_free_principal(context, princ);
803 krb5_cc_destroy(context, ccache);
804 krb5_free_context(context);
809 get_gss_creds(request_rec *r,
810 kerb_auth_config *conf,
811 gss_cred_id_t *server_creds)
813 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
814 OM_uint32 major_status, minor_status, minor_status2;
815 gss_name_t server_name = GSS_C_NO_NAME;
818 snprintf(buf, sizeof(buf), "%s/%s", "khttp", ap_get_server_name(r));
820 input_token.value = buf;
821 input_token.length = strlen(buf) + 1;
823 major_status = gss_import_name(&minor_status, &input_token,
826 if (GSS_ERROR(major_status)) {
827 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
828 "%s", get_gss_error(r->pool, minor_status,
829 "gss_import_name() failed"));
830 return HTTP_INTERNAL_SERVER_ERROR;
833 major_status = gss_acquire_cred(&minor_status, server_name, GSS_C_INDEFINITE,
834 GSS_C_NO_OID_SET, GSS_C_ACCEPT,
835 server_creds, NULL, NULL);
836 gss_release_name(&minor_status2, &server_name);
837 if (GSS_ERROR(major_status)) {
838 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
839 "%s", get_gss_error(r->pool, minor_status,
840 "gss_acquire_cred() failed"));
841 return HTTP_INTERNAL_SERVER_ERROR;
848 authenticate_user_gss(request_rec *r,
849 kerb_auth_config *conf,
850 const char *auth_line)
852 OM_uint32 major_status, minor_status, minor_status2;
853 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
854 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
855 const char *auth_param = NULL;
857 gss_name_t client_name = GSS_C_NO_NAME;
858 gss_cred_id_t delegated_cred = GSS_C_NO_CREDENTIAL;
859 static int initial_return = HTTP_UNAUTHORIZED;
861 /* needed to work around replay caches */
862 if (!ap_is_initial_req(r))
863 return initial_return;
864 initial_return = HTTP_UNAUTHORIZED;
866 if (gss_connection == NULL) {
867 gss_connection = ap_pcalloc(r->connection->pool, sizeof(*gss_connection));
868 if (gss_connection == NULL) {
869 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
870 "ap_pcalloc() failed (not enough memory)");
871 ret = HTTP_INTERNAL_SERVER_ERROR;
874 memset(gss_connection, 0, sizeof(*gss_connection));
875 ap_register_cleanup(r->connection->pool, gss_connection, cleanup_gss_connection, ap_null_cleanup);
878 if (conf->krb_5_keytab)
879 /* use really strcat(), since the string passed to putenv() will become
880 * part of the enviroment and shouldn't be free()ed by apache */
881 /* XXX space isn't allocated !!! */
882 putenv(strcat("KRB5_KTNAME=", conf->krb_5_keytab));
884 if (gss_connection->server_creds == GSS_C_NO_CREDENTIAL) {
885 ret = get_gss_creds(r, conf, &gss_connection->server_creds);
890 /* ap_getword() shifts parameter */
891 auth_param = ap_getword_white(r->pool, &auth_line);
892 if (auth_param == NULL) {
893 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
894 "No Authorization parameter in request from client");
895 ret = HTTP_UNAUTHORIZED;
899 input_token.length = ap_base64decode_len(auth_param) + 1;
900 input_token.value = ap_pcalloc(r->connection->pool, input_token.length);
901 if (input_token.value == NULL) {
902 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
903 "ap_pcalloc() failed (not enough memory)");
904 ret = HTTP_INTERNAL_SERVER_ERROR;
907 input_token.length = ap_base64decode(input_token.value, auth_param);
910 major_status = gss_accept_sec_context(
912 major_status = gss_accept_sec_context_spnego(
915 &gss_connection->context,
916 gss_connection->server_creds,
918 GSS_C_NO_CHANNEL_BINDINGS,
925 if (output_token.length) {
929 len = ap_base64encode_len(output_token.length) + 1;
930 token = ap_pcalloc(r->connection->pool, len + 1);
932 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
933 "ap_pcalloc() failed (not enough memory)");
934 ret = HTTP_INTERNAL_SERVER_ERROR;
935 gss_release_buffer(&minor_status2, &output_token);
938 ap_base64encode(token, output_token.value, output_token.length);
940 ap_table_set(r->err_headers_out, "WWW-Authenticate",
941 ap_pstrcat(r->pool, "Negotiate ", token, NULL));
942 gss_release_buffer(&minor_status2, &output_token);
945 if (GSS_ERROR(major_status)) {
946 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
947 "%s", get_gss_error(r->pool, minor_status,
948 "gss_accept_sec_context() failed"));
949 ret = HTTP_UNAUTHORIZED;
953 if (major_status & GSS_S_CONTINUE_NEEDED) {
954 /* Some GSSAPI mechanism (eg GSI from Globus) may require multiple
955 * iterations to establish authentication */
956 ret = HTTP_UNAUTHORIZED;
960 major_status = gss_display_name(&minor_status, client_name, &output_token, NULL);
961 gss_release_name(&minor_status, &client_name);
962 if (GSS_ERROR(major_status)) {
963 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
964 "%s", get_gss_error(r->pool, minor_status,
965 "gss_export_name() failed"));
966 ret = HTTP_INTERNAL_SERVER_ERROR;
970 MK_AUTH_TYPE = "Negotiate";
971 MK_USER = ap_pstrdup(r->pool, output_token.value);
973 if (conf->krb_save_credentials && delegated_cred != GSS_C_NO_CREDENTIAL)
974 store_gss_creds(r, conf, (char *)output_token.value, delegated_cred);
976 gss_release_buffer(&minor_status, &output_token);
982 gss_release_cred(&minor_status, &delegated_cred);
984 if (output_token.length)
985 gss_release_buffer(&minor_status, &output_token);
987 if (client_name != GSS_C_NO_NAME)
988 gss_release_name(&minor_status, &client_name);
990 cleanup_gss_connection(gss_connection);
992 initial_return = ret;
999 note_kerb_auth_failure(request_rec *r, const kerb_auth_config *conf,
1000 int use_krb4, int use_krb5)
1002 const char *auth_name = NULL;
1005 /* get the user realm specified in .htaccess */
1006 auth_name = ap_auth_name(r);
1008 /* XXX should the WWW-Authenticate header be cleared first? */
1010 if (use_krb5 && conf->krb_method_gssapi)
1011 ap_table_add(r->err_headers_out, "WWW-Authenticate", "Negotiate ");
1012 if (use_krb5 && conf->krb_method_k5pass) {
1013 ap_table_add(r->err_headers_out, "WWW-Authenticate",
1014 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1020 if (use_krb4 && conf->krb_method_k4pass && !set_basic)
1021 ap_table_add(r->err_headers_out, "WWW-Authenticate",
1022 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1026 int kerb_authenticate_user(request_rec *r)
1028 kerb_auth_config *conf =
1029 (kerb_auth_config *) ap_get_module_config(r->per_dir_config,
1031 const char *auth_type = NULL;
1032 const char *auth_line = NULL;
1033 const char *type = NULL;
1034 int use_krb5 = 0, use_krb4 = 0;
1037 /* get the type specified in .htaccess */
1038 type = ap_auth_type(r);
1040 if (type && strcasecmp(type, "Kerberos") == 0)
1041 use_krb5 = use_krb4 = 1;
1042 else if(type && strcasecmp(type, "KerberosV5") == 0)
1044 else if(type && strcasecmp(type, "KerberosV4") == 0)
1049 /* get what the user sent us in the HTTP header */
1050 auth_line = MK_TABLE_GET(r->headers_in, "Authorization");
1052 note_kerb_auth_failure(r, conf, use_krb4, use_krb5);
1053 return HTTP_UNAUTHORIZED;
1055 auth_type = ap_getword_white(r->pool, &auth_line);
1057 ret = HTTP_UNAUTHORIZED;
1060 if (use_krb5 && conf->krb_method_gssapi &&
1061 strcasecmp(auth_type, "Negotiate") == 0) {
1062 ret = authenticate_user_gss(r, conf, auth_line);
1063 } else if (use_krb5 && conf->krb_method_k5pass &&
1064 strcasecmp(auth_type, "Basic") == 0) {
1065 ret = authenticate_user_krb5pwd(r, conf, auth_line);
1070 if (ret == HTTP_UNAUTHORIZED && use_krb4 && conf->krb_method_k4pass &&
1071 strcasecmp(auth_type, "Basic") == 0)
1072 ret = authenticate_user_krb4pwd(r, conf, auth_line);
1075 if (ret == HTTP_UNAUTHORIZED)
1076 note_kerb_auth_failure(r, conf, use_krb4, use_krb5);
1082 /***************************************************************************
1083 Module Setup/Configuration
1084 ***************************************************************************/
1086 module MODULE_VAR_EXPORT auth_kerb_module = {
1087 STANDARD_MODULE_STUFF,
1088 NULL, /* module initializer */
1089 kerb_dir_create_config, /* per-directory config creator */
1090 NULL, /* per-directory config merger */
1091 NULL, /* per-server config creator */
1092 NULL, /* per-server config merger */
1093 kerb_auth_cmds, /* command table */
1094 NULL, /* [ 9] content handlers */
1095 NULL, /* [ 2] URI-to-filename translation */
1096 kerb_authenticate_user, /* [ 5] check/validate user_id */
1097 NULL, /* [ 6] check user_id is valid *here* */
1098 NULL, /* [ 4] check access by host address */
1099 NULL, /* [ 7] MIME type checker/setter */
1100 NULL, /* [ 8] fixups */
1101 NULL, /* [10] logger */
1102 NULL, /* [ 3] header parser */
1103 NULL, /* process initialization */
1104 NULL, /* process exit/cleanup */
1105 NULL /* [ 1] post read_request handling */
1109 kerb_init_handler(apr_pool_t *p, apr_pool_t *plog,
1110 apr_pool_t *ptemp, server_rec *s)
1112 ap_add_version_component(p, "mod_auth_kerb/" MODAUTHKERB_VERSION);
1116 void kerb_register_hooks(apr_pool_t *p)
1118 ap_hook_post_config(kerb_init_handler, NULL, NULL, APR_HOOK_MIDDLE);
1119 ap_hook_check_user_id(kerb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
1122 module AP_MODULE_DECLARE_DATA auth_kerb_module =
1124 STANDARD20_MODULE_STUFF,
1125 kerb_dir_create_config, /* create per-dir conf structures */
1126 NULL, /* merge per-dir conf structures */
1127 NULL, /* create per-server conf structures */
1128 NULL, /* merge per-server conf structures */
1129 kerb_auth_cmds, /* table of configuration directives */
1130 kerb_register_hooks /* register hooks */