2 * Daniel Kouril <kouril@users.sourceforge.org>
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>
12 /* ====================================================================
13 * The Apache Software License, Version 1.1
15 * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in
27 * the documentation and/or other materials provided with the
30 * 3. The end-user documentation included with the redistribution,
31 * if any, must include the following acknowledgment:
32 * "This product includes software developed by the
33 * Apache Software Foundation (http://www.apache.org/)."
34 * Alternately, this acknowledgment may appear in the software itself,
35 * if and wherever such third-party acknowledgments normally appear.
37 * 4. The names "Apache" and "Apache Software Foundation" must
38 * not be used to endorse or promote products derived from this
39 * software without prior written permission. For written
40 * permission, please contact apache@apache.org.
42 * 5. Products derived from this software may not be called "Apache",
43 * nor may "Apache" appear in their name, without prior written
44 * permission of the Apache Software Foundation.
46 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
47 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
49 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
50 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
51 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
52 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
53 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
54 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
55 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
56 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * ====================================================================
60 * This software consists of voluntary contributions made by many
61 * individuals on behalf of the Apache Software Foundation. For more
62 * information on the Apache Software Foundation, please see
63 * <http://www.apache.org/>.
65 * Portions of this software are based upon public domain software
66 * originally written at the National Center for Supercomputing Applications,
67 * University of Illinois, Urbana-Champaign.
73 #include "ap_compat.h"
74 #include "apr_strings.h"
77 #include "http_config.h"
78 #include "http_core.h"
80 #include "http_protocol.h"
81 #include "http_request.h"
88 # include <gssapi/gssapi.h>
89 # include <gssapi/gssapi_generic.h>
90 # define GSS_C_NT_USER_NAME gss_nt_user_name
91 # define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
92 # define krb5_get_err_text(context,code) error_message(code)
97 /*Prevent warning about closesocket redefinition (Apache's ap_config.h and
98 * MIT Kerberos' port-sockets.h both define it as close) */
106 module auth_kerb_module;
108 module AP_MODULE_DECLARE_DATA auth_kerb_module;
111 /***************************************************************************
112 Macros To Ease Compatibility
113 ***************************************************************************/
116 #define MK_TABLE_GET ap_table_get
117 #define MK_TABLE_SET ap_table_set
118 #define MK_TABLE_TYPE table
119 #define MK_PSTRDUP ap_pstrdup
120 #define MK_USER r->connection->user
121 #define MK_AUTH_TYPE r->connection->ap_auth_type
122 #define MK_ARRAY_HEADER array_header
124 #define MK_POOL apr_pool_t
125 #define MK_TABLE_GET apr_table_get
126 #define MK_TABLE_SET apr_table_set
127 #define MK_TABLE_TYPE apr_table_t
128 #define MK_PSTRDUP apr_pstrdup
129 #define MK_USER r->user
130 #define MK_AUTH_TYPE r->ap_auth_type
131 #define MK_ARRAY_HEADER apr_array_header_t
135 /***************************************************************************
136 Auth Configuration Structure
137 ***************************************************************************/
139 char *krb_auth_realms;
140 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."),
178 command("Krb5Keytab", ap_set_file_slot, krb_5_keytab,
179 TAKE1, "Location of Kerberos V5 keytab file."),
181 command("KrbMethodNegotiate", ap_set_flag_slot, krb_method_gssapi,
182 FLAG, "Enable Negotiate authentication method."),
184 command("KrbMethodK5Pass", ap_set_flag_slot, krb_method_k5pass,
185 FLAG, "Enable Kerberos V5 password authentication."),
189 command("Krb4Srvtab", ap_set_file_slot, krb_4_srvtab,
190 TAKE1, "Location of Kerberos V4 srvtab file."),
192 command("KrbMethodK4Pass", ap_set_flag_slot, krb_method_k4pass,
193 FLAG, "Enable Kerberos V4 password authentication."),
201 gss_ctx_id_t context;
202 gss_cred_id_t server_creds;
205 static gss_connection_t *gss_connection = NULL;
209 /***************************************************************************
210 Auth Configuration Initialization
211 ***************************************************************************/
212 static void *kerb_dir_create_config(MK_POOL *p, char *d)
214 kerb_auth_config *rec;
216 rec = (kerb_auth_config *) ap_pcalloc(p, sizeof(kerb_auth_config));
218 ((kerb_auth_config *)rec)->krb_method_k5pass = 1;
219 ((kerb_auth_config *)rec)->krb_method_gssapi = 1;
222 ((kerb_auth_config *)rec)->krb_method_k4pass = 1;
228 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg)
230 sec->krb_auth_realms= ap_pstrdup(cmd->pool, arg);
234 void log_rerror(const char *file, int line, int level, int status,
235 const request_rec *r, const char *fmt, ...)
241 vsnprintf(errstr, sizeof(errstr), fmt, ap);
245 ap_log_rerror(file, line, level, r, "%s", errstr);
247 ap_log_rerror(file, line, level, status, r, "%s", errstr);
252 /***************************************************************************
253 Username/Password Validation for Krb4
254 ***************************************************************************/
256 verify_krb4_user(request_rec *r, char *name, char *instance, char *realm,
257 char *password, char *linstance, char *srvtab)
263 const char *hostname;
266 char lrealm[REALM_SZ];
268 ret = krb_get_pw_in_tkt(name, instance, realm, "krbtgt", realm,
269 DEFAULT_TKT_LIFE, password);
271 /* log(krb_err_txt[ret]) */
274 hostname = ap_get_server_name(r);
276 hp = gethostbyname(hostname);
281 memcpy(&addr, hp->h_addr, sizeof(addr));
283 phost = krb_get_phost((char *)hostname);
285 krb_get_lrealm(lrealm, 1);
287 ret = krb_mk_req(&ticket, linstance, phost, lrealm, 0);
293 ret = krb_rd_req(&ticket, linstance, phost, addr, &authdata, srvtab);
301 krb4_cache_cleanup(void *data)
303 char *tkt_file = (char *) data;
305 krb_set_tkt_string(tkt_file);
311 authenticate_user_krb4pwd(request_rec *r,
312 kerb_auth_config *conf,
313 const char *auth_line)
317 const char *sent_name;
320 char *tkt_file_p = NULL;
325 char lrealm[REALM_SZ];
327 sent_pw = ap_pbase64decode(r->pool, auth_line);
328 sent_name = ap_getword (r->pool, &sent_pw, ':');
330 /* do not allow user to override realm setting of server */
331 if (strchr(sent_name, '@')) {
332 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
333 "specifying realm in user name is prohibited");
334 return HTTP_UNAUTHORIZED;
337 sent_instance = strchr(sent_name, '.');
339 *sent_instance++ = '\0';
341 snprintf(tkt_file, sizeof(tkt_file), "/tmp/apache_tkt_XXXXXX");
342 fd = mkstemp(tkt_file);
344 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
345 "Cannot create krb4 ccache: mkstemp() failed: %s",
347 return HTTP_INTERNAL_SERVER_ERROR;
350 tkt_file_p = ap_pstrdup(r->pool, tkt_file);
351 ap_register_cleanup(r->pool, tkt_file_p,
352 krb4_cache_cleanup, ap_null_cleanup);
354 krb_set_tkt_string(tkt_file);
356 realms = conf->krb_auth_realms;
358 memset(lrealm, 0, sizeof(lrealm));
361 realm = ap_getword_white(r->pool, &realms);
364 ret = krb_get_lrealm(lrealm, 1);
367 if (realm == NULL || *realm == '\0')
370 ret = verify_krb4_user(r, (char *)sent_name,
371 (sent_instance) ? sent_instance : "",
372 (char *)realm, (char *)sent_pw, "khttp",
376 } while (realms && *realms);
379 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
380 "Verifying krb4 password failed (%d)", ret);
381 ret = HTTP_UNAUTHORIZED;
385 user = ap_pstrdup(r->pool, sent_name);
387 user = ap_pstrcat(r->pool, user, ".", sent_instance, NULL);
388 user = ap_pstrcat(r->pool, user, "@", realm, NULL);
391 MK_AUTH_TYPE = "Basic";
392 ap_table_setn(r->subprocess_env, "KRBTKFILE", tkt_file_p);
394 if (!conf->krb_save_credentials)
395 krb4_cache_cleanup(tkt_file);
399 krb4_cache_cleanup(tkt_file);
408 /***************************************************************************
409 Username/Password Validation for Krb5
410 ***************************************************************************/
411 /* Inspired by krb5_verify_user from Heimdal */
412 static krb5_error_code
413 verify_krb5_user(request_rec *r, krb5_context context, krb5_principal principal,
414 krb5_ccache ccache, const char *password, const char *service,
418 krb5_principal server = NULL;
420 krb5_verify_init_creds_opt opt;
422 memset(&creds, 0, sizeof(creds));
424 ret = krb5_get_init_creds_password(context, &creds, principal,
425 (char *)password, krb5_prompter_posix,
426 NULL, 0, NULL, NULL);
430 ret = krb5_sname_to_principal(context, ap_get_server_name(r), service,
431 KRB5_NT_SRV_HST, &server);
435 krb5_verify_init_creds_opt_init(&opt);
436 krb5_verify_init_creds_opt_set_ap_req_nofail(&opt, 1);
438 ret = krb5_verify_init_creds(context, &creds, server, keytab, NULL, &opt);
443 ret = krb5_cc_initialize(context, ccache, principal);
445 ret = krb5_cc_store_cred(context, ccache, &creds);
449 krb5_free_cred_contents(context, &creds);
451 krb5_free_principal(context, server);
456 krb5_cache_cleanup(void *data)
458 krb5_context context;
460 krb5_error_code problem;
461 char *cache_name = (char *) data;
463 problem = krb5_init_context(&context);
465 /* ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "krb5_init_context() failed"); */
466 return HTTP_INTERNAL_SERVER_ERROR;
469 problem = krb5_cc_resolve(context, cache_name, &cache);
471 /* log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
472 "krb5_cc_resolve() failed (%s: %s)",
473 cache_name, krb5_get_err_text(context, problem)); */
474 return HTTP_INTERNAL_SERVER_ERROR;
477 krb5_cc_destroy(context, cache);
478 krb5_free_context(context);
483 create_krb5_ccache(krb5_context kcontext,
485 kerb_auth_config *conf,
486 krb5_principal princ,
490 krb5_error_code problem;
492 krb5_ccache tmp_ccache = NULL;
495 problem = krb5_cc_gen_new(kcontext, &krb5_fcc_ops, &tmp_ccache);
497 problem = krb5_fcc_generate_new(kcontext, &tmp_ccache);
498 /* krb5_fcc_generate_new() doesn't set KRB5_TC_OPENCLOSE, which makes
499 krb5_cc_initialize() fail */
500 krb5_fcc_set_flags(kcontext, tmp_ccache, KRB5_TC_OPENCLOSE);
503 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
504 "Cannot create file for new krb5 ccache: %s",
505 krb5_get_err_text(kcontext, problem));
506 ret = HTTP_INTERNAL_SERVER_ERROR;
510 ccname = ap_pstrdup(r->pool, krb5_cc_get_name(kcontext, tmp_ccache));
512 problem = krb5_cc_initialize(kcontext, tmp_ccache, princ);
514 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
515 "Cannot initialize krb5 ccache %s: krb5_cc_initialize() failed: %s",
516 ccname, krb5_get_err_text(kcontext, problem));
517 ret = HTTP_INTERNAL_SERVER_ERROR;
521 ap_table_setn(r->subprocess_env, "KRB5CCNAME", ccname);
522 ap_register_cleanup(r->pool, ccname,
523 krb5_cache_cleanup, ap_null_cleanup);
525 *ccache = tmp_ccache;
532 krb5_cc_destroy(kcontext, tmp_ccache);
538 store_krb5_creds(krb5_context kcontext,
540 kerb_auth_config *conf,
541 krb5_ccache delegated_cred)
544 krb5_error_code problem;
545 krb5_principal princ;
549 problem = krb5_cc_get_principal(kcontext, delegated_cred, &princ);
551 snprintf(errstr, sizeof(errstr), "krb5_cc_get_principal() failed: %s",
552 krb5_get_err_text(kcontext, problem));
553 return HTTP_INTERNAL_SERVER_ERROR;
556 ret = create_krb5_ccache(kcontext, r, conf, princ, &ccache);
558 krb5_free_principal(kcontext, princ);
563 problem = krb5_cc_copy_cache(kcontext, delegated_cred, ccache);
565 problem = krb5_cc_copy_creds(kcontext, delegated_cred, ccache);
567 krb5_free_principal(kcontext, princ);
569 snprintf(errstr, sizeof(errstr), "Failed to store credentials: %s",
570 krb5_get_err_text(kcontext, problem));
571 krb5_cc_destroy(kcontext, ccache);
572 return HTTP_INTERNAL_SERVER_ERROR;
575 krb5_cc_close(kcontext, ccache);
580 int authenticate_user_krb5pwd(request_rec *r,
581 kerb_auth_config *conf,
582 const char *auth_line)
584 const char *sent_pw = NULL;
585 const char *sent_name = NULL;
586 const char *realms = NULL;
587 krb5_context kcontext = NULL;
588 krb5_error_code code;
589 krb5_principal client = NULL;
590 krb5_ccache ccache = NULL;
591 krb5_keytab keytab = NULL;
595 code = krb5_init_context(&kcontext);
597 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
598 "Cannot initialize Kerberos5 context (%d)", code);
599 return HTTP_INTERNAL_SERVER_ERROR;
602 sent_pw = ap_pbase64decode(r->pool, auth_line);
603 sent_name = ap_getword (r->pool, &sent_pw, ':');
604 /* do not allow user to override realm setting of server */
605 if (strchr(sent_name, '@')) {
606 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
607 "specifying realm in user name is prohibited");
608 ret = HTTP_UNAUTHORIZED;
613 code = krb5_cc_gen_new(kcontext, &krb5_mcc_ops, &ccache);
615 code = krb5_mcc_generate_new(kcontext, &ccache);
618 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
619 "Cannot generate new ccache: %s",
620 krb5_get_err_text(kcontext, code));
621 ret = HTTP_INTERNAL_SERVER_ERROR;
625 if (conf->krb_5_keytab)
626 krb5_kt_resolve(kcontext, conf->krb_5_keytab, &keytab);
627 /* setenv("KRB5_KTNAME", conf->krb_5_keytab, 1); */
629 realms = conf->krb_auth_realms;
631 if (realms && (code = krb5_set_default_realm(kcontext,
632 ap_getword_white(r->pool, &realms))))
636 krb5_free_principal(kcontext, client);
639 code = krb5_parse_name(kcontext, sent_name, &client);
643 code = verify_krb5_user(r, kcontext, client, ccache, sent_pw, "khttp",
648 /* ap_getword_white() used above shifts the parameter, so it's not
649 needed to touch the realms variable */
650 } while (realms && *realms);
652 memset((char *)sent_pw, 0, strlen(sent_pw));
655 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
656 "Verifying krb5 password failed: %s",
657 krb5_get_err_text(kcontext, code));
658 ret = HTTP_UNAUTHORIZED;
662 code = krb5_unparse_name(kcontext, client, &name);
664 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "krb5_unparse_name() failed: %s",
665 krb5_get_err_text(kcontext, code));
666 ret = HTTP_UNAUTHORIZED;
669 MK_USER = ap_pstrdup (r->pool, name);
670 MK_AUTH_TYPE = "Basic";
673 if (conf->krb_save_credentials)
674 store_krb5_creds(kcontext, r, conf, ccache);
680 krb5_free_principal(kcontext, client);
682 krb5_cc_destroy(kcontext, ccache);
684 krb5_kt_close(kcontext, keytab);
685 krb5_free_context(kcontext);
690 /*********************************************************************
691 * GSSAPI Authentication
692 ********************************************************************/
695 get_gss_error(MK_POOL *p, OM_uint32 error_status, char *prefix)
697 OM_uint32 maj_stat, min_stat;
698 OM_uint32 msg_ctx = 0;
699 gss_buffer_desc status_string;
703 snprintf(buf, sizeof(buf), "%s", prefix);
706 maj_stat = gss_display_status (&min_stat,
712 if (sizeof(buf) > len + status_string.length + 1) {
713 sprintf(buf+len, ": %s", (char*) status_string.value);
714 len += status_string.length;
716 gss_release_buffer(&min_stat, &status_string);
717 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
719 return (ap_pstrdup(p, buf));
723 cleanup_gss_connection(void *data)
725 OM_uint32 minor_status;
726 gss_connection_t *gss_conn = (gss_connection_t *)data;
730 if (gss_conn->context != GSS_C_NO_CONTEXT)
731 gss_delete_sec_context(&minor_status, &gss_conn->context,
733 if (gss_conn->server_creds != GSS_C_NO_CREDENTIAL)
734 gss_release_cred(&minor_status, &gss_conn->server_creds);
736 gss_connection = NULL;
742 store_gss_creds(request_rec *r, kerb_auth_config *conf, char *princ_name,
743 gss_cred_id_t delegated_cred)
745 OM_uint32 maj_stat, min_stat;
746 krb5_principal princ = NULL;
747 krb5_ccache ccache = NULL;
748 krb5_error_code problem;
749 krb5_context context;
750 int ret = HTTP_INTERNAL_SERVER_ERROR;
752 problem = krb5_init_context(&context);
754 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Cannot initialize krb5 context");
755 return HTTP_INTERNAL_SERVER_ERROR;
758 problem = krb5_parse_name(context, princ_name, &princ);
760 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
761 "Cannot parse delegated username (%s)", krb5_get_err_text(context, problem));
765 problem = create_krb5_ccache(context, r, conf, princ, &ccache);
767 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
768 "Cannot create krb5 ccache (%s)", krb5_get_err_text(context, problem));
772 maj_stat = gss_krb5_copy_ccache(&min_stat, delegated_cred, ccache);
773 if (GSS_ERROR(maj_stat)) {
774 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
775 "Cannot store delegated credential (%s)",
776 get_gss_error(r->pool, min_stat, "gss_krb5_copy_ccache"));
780 krb5_cc_close(context, ccache);
786 krb5_free_principal(context, princ);
788 krb5_cc_destroy(context, ccache);
789 krb5_free_context(context);
794 get_gss_creds(request_rec *r,
795 kerb_auth_config *conf,
796 gss_cred_id_t *server_creds)
798 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
799 OM_uint32 major_status, minor_status, minor_status2;
800 gss_name_t server_name = GSS_C_NO_NAME;
803 snprintf(buf, sizeof(buf), "%s/%s", "khttp", ap_get_server_name(r));
805 input_token.value = buf;
806 input_token.length = strlen(buf) + 1;
808 major_status = gss_import_name(&minor_status, &input_token,
811 if (GSS_ERROR(major_status)) {
812 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
813 "%s", get_gss_error(r->pool, minor_status,
814 "gss_import_name() failed"));
815 return HTTP_INTERNAL_SERVER_ERROR;
818 major_status = gss_acquire_cred(&minor_status, server_name, GSS_C_INDEFINITE,
819 GSS_C_NO_OID_SET, GSS_C_ACCEPT,
820 server_creds, NULL, NULL);
821 gss_release_name(&minor_status2, &server_name);
822 if (GSS_ERROR(major_status)) {
823 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
824 "%s", get_gss_error(r->pool, minor_status,
825 "gss_acquire_cred() failed"));
826 return HTTP_INTERNAL_SERVER_ERROR;
833 authenticate_user_gss(request_rec *r,
834 kerb_auth_config *conf,
835 const char *auth_line)
837 OM_uint32 major_status, minor_status, minor_status2;
838 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
839 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
840 const char *auth_param = NULL;
842 gss_name_t client_name = GSS_C_NO_NAME;
843 gss_cred_id_t delegated_cred = GSS_C_NO_CREDENTIAL;
844 static int initial_return = HTTP_UNAUTHORIZED;
846 /* needed to work around replay caches */
847 if (!ap_is_initial_req(r))
848 return initial_return;
849 initial_return = HTTP_UNAUTHORIZED;
851 if (gss_connection == NULL) {
852 gss_connection = ap_pcalloc(r->connection->pool, sizeof(*gss_connection));
853 if (gss_connection == NULL) {
854 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
855 "ap_pcalloc() failed (not enough memory)");
856 ret = HTTP_INTERNAL_SERVER_ERROR;
859 memset(gss_connection, 0, sizeof(*gss_connection));
860 ap_register_cleanup(r->connection->pool, gss_connection, cleanup_gss_connection, ap_null_cleanup);
863 if (conf->krb_5_keytab)
864 setenv("KRB5_KTNAME", conf->krb_5_keytab, 1);
866 if (gss_connection->server_creds == GSS_C_NO_CREDENTIAL) {
867 ret = get_gss_creds(r, conf, &gss_connection->server_creds);
872 /* ap_getword() shifts parameter */
873 auth_param = ap_getword_white(r->pool, &auth_line);
874 if (auth_param == NULL) {
875 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
876 "No Authorization parameter in request from client");
877 ret = HTTP_UNAUTHORIZED;
881 input_token.length = ap_base64decode_len(auth_param) + 1;
882 input_token.value = ap_pcalloc(r->connection->pool, input_token.length);
883 if (input_token.value == NULL) {
884 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
885 "ap_pcalloc() failed (not enough memory)");
886 ret = HTTP_INTERNAL_SERVER_ERROR;
889 input_token.length = ap_base64decode(input_token.value, auth_param);
891 major_status = gss_accept_sec_context(&minor_status,
892 &gss_connection->context,
893 gss_connection->server_creds,
895 GSS_C_NO_CHANNEL_BINDINGS,
902 if (output_token.length) {
906 len = ap_base64encode_len(output_token.length) + 1;
907 token = ap_pcalloc(r->connection->pool, len + 1);
909 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
910 "ap_pcalloc() failed (not enough memory)");
911 ret = HTTP_INTERNAL_SERVER_ERROR;
912 gss_release_buffer(&minor_status2, &output_token);
915 ap_base64encode(token, output_token.value, output_token.length);
917 ap_table_set(r->err_headers_out, "WWW-Authenticate",
918 ap_pstrcat(r->pool, "GSS-Negotiate ", token, NULL));
919 gss_release_buffer(&minor_status2, &output_token);
922 if (GSS_ERROR(major_status)) {
923 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
924 "%s", get_gss_error(r->pool, minor_status,
925 "gss_accept_sec_context() failed"));
926 ret = HTTP_UNAUTHORIZED;
930 if (major_status & GSS_S_CONTINUE_NEEDED) {
931 /* Some GSSAPI mechanism (eg GSI from Globus) may require multiple
932 * iterations to establish authentication */
933 ret = HTTP_UNAUTHORIZED;
937 major_status = gss_display_name(&minor_status, client_name, &output_token, NULL);
938 gss_release_name(&minor_status, &client_name);
939 if (GSS_ERROR(major_status)) {
940 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
941 "%s", get_gss_error(r->pool, minor_status,
942 "gss_export_name() failed"));
943 ret = HTTP_INTERNAL_SERVER_ERROR;
947 MK_AUTH_TYPE = "Negotiate";
948 MK_USER = ap_pstrdup(r->pool, output_token.value);
950 if (conf->krb_save_credentials && delegated_cred != GSS_C_NO_CREDENTIAL)
951 store_gss_creds(r, conf, (char *)output_token.value, delegated_cred);
953 gss_release_buffer(&minor_status, &output_token);
959 gss_release_cred(&minor_status, &delegated_cred);
961 if (output_token.length)
962 gss_release_buffer(&minor_status, &output_token);
964 if (client_name != GSS_C_NO_NAME)
965 gss_release_name(&minor_status, &client_name);
967 cleanup_gss_connection(gss_connection);
969 initial_return = ret;
976 note_kerb_auth_failure(request_rec *r, const kerb_auth_config *conf,
977 int use_krb4, int use_krb5)
979 const char *auth_name = NULL;
982 /* get the user realm specified in .htaccess */
983 auth_name = ap_auth_name(r);
985 /* XXX should the WWW-Authenticate header be cleared first? */
987 if (use_krb5 && conf->krb_method_gssapi)
988 ap_table_add(r->err_headers_out, "WWW-Authenticate", "GSS-Negotiate ");
989 if (use_krb5 && conf->krb_method_k5pass) {
990 ap_table_add(r->err_headers_out, "WWW-Authenticate",
991 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
997 if (use_krb4 && conf->krb_method_k4pass && !set_basic)
998 ap_table_add(r->err_headers_out, "WWW-Authenticate",
999 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1003 int kerb_authenticate_user(request_rec *r)
1005 kerb_auth_config *conf =
1006 (kerb_auth_config *) ap_get_module_config(r->per_dir_config,
1008 const char *auth_type = NULL;
1009 const char *auth_line = NULL;
1010 const char *type = NULL;
1011 int use_krb5 = 0, use_krb4 = 0;
1014 /* get the type specified in .htaccess */
1015 type = ap_auth_type(r);
1017 if (type && strcasecmp(type, "Kerberos") == 0)
1018 use_krb5 = use_krb4 = 1;
1019 else if(type && strcasecmp(type, "KerberosV5") == 0)
1021 else if(type && strcasecmp(type, "KerberosV4") == 0)
1026 /* get what the user sent us in the HTTP header */
1027 auth_line = MK_TABLE_GET(r->headers_in, "Authorization");
1029 note_kerb_auth_failure(r, conf, use_krb4, use_krb5);
1030 return HTTP_UNAUTHORIZED;
1032 auth_type = ap_getword_white(r->pool, &auth_line);
1034 ret = HTTP_UNAUTHORIZED;
1037 if (use_krb5 && conf->krb_method_gssapi &&
1038 strcasecmp(auth_type, "GSS-Negotiate") == 0) {
1039 ret = authenticate_user_gss(r, conf, auth_line);
1040 } else if (use_krb5 && conf->krb_method_k5pass &&
1041 strcasecmp(auth_type, "Basic") == 0) {
1042 ret = authenticate_user_krb5pwd(r, conf, auth_line);
1047 if (ret == HTTP_UNAUTHORIZED && use_krb4 && conf->krb_method_k4pass &&
1048 strcasecmp(auth_type, "Basic") == 0)
1049 ret = authenticate_user_krb4pwd(r, conf, auth_line);
1052 if (ret == HTTP_UNAUTHORIZED)
1053 note_kerb_auth_failure(r, conf, use_krb4, use_krb5);
1059 /***************************************************************************
1060 Module Setup/Configuration
1061 ***************************************************************************/
1063 module MODULE_VAR_EXPORT auth_kerb_module = {
1064 STANDARD_MODULE_STUFF,
1065 NULL, /* module initializer */
1066 kerb_dir_create_config, /* per-directory config creator */
1067 NULL, /* per-directory config merger */
1068 NULL, /* per-server config creator */
1069 NULL, /* per-server config merger */
1070 kerb_auth_cmds, /* command table */
1071 NULL, /* [ 9] content handlers */
1072 NULL, /* [ 2] URI-to-filename translation */
1073 kerb_authenticate_user, /* [ 5] check/validate user_id */
1074 NULL, /* [ 6] check user_id is valid *here* */
1075 NULL, /* [ 4] check access by host address */
1076 NULL, /* [ 7] MIME type checker/setter */
1077 NULL, /* [ 8] fixups */
1078 NULL, /* [10] logger */
1079 NULL, /* [ 3] header parser */
1080 NULL, /* process initialization */
1081 NULL, /* process exit/cleanup */
1082 NULL /* [ 1] post read_request handling */
1085 void kerb_register_hooks(apr_pool_t *p)
1087 ap_hook_check_user_id(kerb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
1090 module AP_MODULE_DECLARE_DATA auth_kerb_module =
1092 STANDARD20_MODULE_STUFF,
1093 kerb_dir_create_config, /* create per-dir conf structures */
1094 NULL, /* merge per-dir conf structures */
1095 NULL, /* create per-server conf structures */
1096 NULL, /* merge per-server conf structures */
1097 kerb_auth_cmds, /* table of configuration directives */
1098 kerb_register_hooks /* register hooks */