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.
74 #include "ap_compat.h"
75 #include "apr_strings.h"
78 #include "http_config.h"
79 #include "http_core.h"
81 #include "http_protocol.h"
82 #include "http_request.h"
88 # include <gssapi_generic.h>
89 # define GSS_C_NT_USER_NAME gss_nt_user_name
90 # define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
91 # define krb5_get_err_text(context,code) error_message(code)
96 /*Prevent warning about closesocket redefinition (Apache's ap_config.h and
97 * MIT Kerberos' port-sockets.h both define it as close) */
105 module auth_kerb_module;
107 module AP_MODULE_DECLARE_DATA auth_kerb_module;
110 /***************************************************************************
111 Macros To Ease Compatibility
112 ***************************************************************************/
115 #define MK_TABLE_GET ap_table_get
116 #define MK_TABLE_SET ap_table_set
117 #define MK_TABLE_TYPE table
118 #define MK_PSTRDUP ap_pstrdup
119 #define MK_USER r->connection->user
120 #define MK_AUTH_TYPE r->connection->ap_auth_type
121 #define MK_ARRAY_HEADER array_header
123 #define MK_POOL apr_pool_t
124 #define MK_TABLE_GET apr_table_get
125 #define MK_TABLE_SET apr_table_set
126 #define MK_TABLE_TYPE apr_table_t
127 #define MK_PSTRDUP apr_pstrdup
128 #define MK_USER r->user
129 #define MK_AUTH_TYPE r->ap_auth_type
130 #define MK_ARRAY_HEADER apr_array_header_t
134 /***************************************************************************
135 Auth Configuration Structure
136 ***************************************************************************/
138 char *krb_auth_realms;
139 int krb_save_credentials;
142 int krb_method_gssapi;
143 int krb_method_k5pass;
147 int krb_method_k4pass;
152 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg);
155 #define command(name, func, var, type, usage) \
157 (void*)XtOffsetOf(kerb_auth_config, var), \
158 OR_AUTHCFG, type, usage }
160 #define command(name, func, var, type, usage) \
161 AP_INIT_ ## type (name, func, \
162 (void*)APR_XtOffsetOf(kerb_auth_config, var), \
166 static const command_rec kerb_auth_cmds[] = {
167 command("KrbAuthRealms", krb5_save_realms, krb_auth_realms,
168 RAW_ARGS, "Realms to attempt authentication against (can be multiple)."),
170 command("KrbAuthRealm", krb5_save_realms, krb_auth_realms,
171 RAW_ARGS, "Alias for KrbAuthRealms."),
173 command("KrbSaveCredentials", ap_set_flag_slot, krb_save_credentials,
174 FLAG, "Save and store credentials/tickets retrieved during auth."),
177 command("Krb5Keytab", ap_set_file_slot, krb_5_keytab,
178 TAKE1, "Location of Kerberos V5 keytab file."),
180 command("KrbMethodNegotiate", ap_set_flag_slot, krb_method_gssapi,
181 FLAG, "Enable Negotiate authentication method."),
183 command("KrbMethodK5Pass", ap_set_flag_slot, krb_method_k5pass,
184 FLAG, "Enable Kerberos V5 password authentication."),
188 command("Krb4Srvtab", ap_set_file_slot, krb_4_srvtab,
189 TAKE1, "Location of Kerberos V4 srvtab file."),
191 command("KrbMethodK4Pass", ap_set_flag_slot, krb_method_k4pass,
192 FLAG, "Enable Kerberos V4 password authentication."),
200 gss_ctx_id_t context;
201 gss_cred_id_t server_creds;
204 static gss_connection_t *gss_connection = NULL;
208 /***************************************************************************
209 Auth Configuration Initialization
210 ***************************************************************************/
211 static void *kerb_dir_create_config(MK_POOL *p, char *d)
213 kerb_auth_config *rec;
215 rec = (kerb_auth_config *) ap_pcalloc(p, sizeof(kerb_auth_config));
217 ((kerb_auth_config *)rec)->krb_method_k5pass = 1;
218 ((kerb_auth_config *)rec)->krb_method_gssapi = 1;
221 ((kerb_auth_config *)rec)->krb_method_k4pass = 1;
227 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg)
229 sec->krb_auth_realms= ap_pstrdup(cmd->pool, arg);
233 void log_rerror(const char *file, int line, int level, int status,
234 const request_rec *r, const char *fmt, ...)
240 vsnprintf(errstr, sizeof(errstr), fmt, ap);
244 ap_log_rerror(file, line, level, r, "%s", errstr);
246 ap_log_rerror(file, line, level, status, r, "%s", errstr);
251 /***************************************************************************
252 Username/Password Validation for Krb4
253 ***************************************************************************/
255 verify_krb4_user(request_rec *r, char *name, char *instance, char *realm,
256 char *password, char *linstance, char *srvtab)
262 const char *hostname;
265 char lrealm[REALM_SZ];
267 ret = krb_get_pw_in_tkt(name, instance, realm, "krbtgt", realm,
268 DEFAULT_TKT_LIFE, password);
270 /* log(krb_err_txt[ret]) */
273 hostname = ap_get_server_name(r);
275 hp = gethostbyname(hostname);
280 memcpy(&addr, hp->h_addr, sizeof(addr));
282 phost = krb_get_phost((char *)hostname);
284 krb_get_lrealm(lrealm, 1);
286 ret = krb_mk_req(&ticket, linstance, phost, lrealm, 0);
292 ret = krb_rd_req(&ticket, linstance, phost, addr, &authdata, srvtab);
300 krb4_cache_cleanup(void *data)
302 char *tkt_file = (char *) data;
304 krb_set_tkt_string(tkt_file);
310 authenticate_user_krb4pwd(request_rec *r,
311 kerb_auth_config *conf,
312 const char *auth_line)
316 const char *sent_name;
319 char *tkt_file_p = NULL;
324 char lrealm[REALM_SZ];
326 sent_pw = ap_pbase64decode(r->pool, auth_line);
327 sent_name = ap_getword (r->pool, &sent_pw, ':');
329 /* do not allow user to override realm setting of server */
330 if (strchr(sent_name, '@')) {
331 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
332 "specifying realm in user name is prohibited");
333 return HTTP_UNAUTHORIZED;
336 sent_instance = strchr(sent_name, '.');
338 *sent_instance++ = '\0';
340 snprintf(tkt_file, sizeof(tkt_file), "/tmp/apache_tkt_XXXXXX");
341 fd = mkstemp(tkt_file);
343 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
344 "Cannot create krb4 ccache: mkstemp() failed: %s",
346 return HTTP_INTERNAL_SERVER_ERROR;
349 tkt_file_p = ap_pstrdup(r->pool, tkt_file);
350 ap_register_cleanup(r->pool, tkt_file_p,
351 krb4_cache_cleanup, ap_null_cleanup);
353 krb_set_tkt_string(tkt_file);
355 realms = conf->krb_auth_realms;
357 memset(lrealm, 0, sizeof(lrealm));
360 realm = ap_getword_white(r->pool, &realms);
363 ret = krb_get_lrealm(lrealm, 1);
366 if (realm == NULL || *realm == '\0')
369 ret = verify_krb4_user(r, (char *)sent_name,
370 (sent_instance) ? sent_instance : "",
371 (char *)realm, (char *)sent_pw, "khttp",
375 } while (realms && *realms);
378 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
379 "Verifying krb4 password failed (%d)", ret);
380 ret = HTTP_UNAUTHORIZED;
384 user = ap_pstrdup(r->pool, sent_name);
386 user = ap_pstrcat(r->pool, user, ".", sent_instance, NULL);
387 user = ap_pstrcat(r->pool, user, "@", realm, NULL);
390 MK_AUTH_TYPE = "Basic";
391 ap_table_setn(r->subprocess_env, "KRBTKFILE", tkt_file_p);
393 if (!conf->krb_save_credentials)
394 krb4_cache_cleanup(tkt_file);
398 krb4_cache_cleanup(tkt_file);
407 /***************************************************************************
408 Username/Password Validation for Krb5
409 ***************************************************************************/
410 /* Inspired by krb5_verify_user from Heimdal */
411 static krb5_error_code
412 verify_krb5_user(request_rec *r, krb5_context context, krb5_principal principal,
413 krb5_ccache ccache, const char *password, const char *service,
417 krb5_principal server = NULL;
419 krb5_verify_init_creds_opt opt;
421 memset(&creds, 0, sizeof(creds));
423 ret = krb5_get_init_creds_password(context, &creds, principal,
424 (char *)password, krb5_prompter_posix,
425 NULL, 0, NULL, NULL);
429 ret = krb5_sname_to_principal(context, ap_get_server_name(r), service,
430 KRB5_NT_SRV_HST, &server);
434 krb5_verify_init_creds_opt_init(&opt);
435 krb5_verify_init_creds_opt_set_ap_req_nofail(&opt, 1);
437 ret = krb5_verify_init_creds(context, &creds, server, keytab, NULL, &opt);
442 ret = krb5_cc_initialize(context, ccache, principal);
444 ret = krb5_cc_store_cred(context, ccache, &creds);
448 krb5_free_cred_contents(context, &creds);
450 krb5_free_principal(context, server);
455 krb5_cache_cleanup(void *data)
457 krb5_context context;
459 krb5_error_code problem;
460 char *cache_name = (char *) data;
462 problem = krb5_init_context(&context);
464 /* ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "krb5_init_context() failed"); */
465 return HTTP_INTERNAL_SERVER_ERROR;
468 problem = krb5_cc_resolve(context, cache_name, &cache);
470 /* log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
471 "krb5_cc_resolve() failed (%s: %s)",
472 cache_name, krb5_get_err_text(context, problem)); */
473 return HTTP_INTERNAL_SERVER_ERROR;
476 krb5_cc_destroy(context, cache);
477 krb5_free_context(context);
482 create_krb5_ccache(krb5_context kcontext,
484 kerb_auth_config *conf,
485 krb5_principal princ,
489 krb5_error_code problem;
491 krb5_ccache tmp_ccache = NULL;
494 problem = krb5_cc_gen_new(kcontext, &krb5_fcc_ops, &tmp_ccache);
496 problem = krb5_fcc_generate_new(kcontext, &tmp_ccache);
497 /* krb5_fcc_generate_new() doesn't set KRB5_TC_OPENCLOSE, which makes
498 krb5_cc_initialize() fail */
499 krb5_fcc_set_flags(kcontext, tmp_ccache, KRB5_TC_OPENCLOSE);
502 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
503 "Cannot create file for new krb5 ccache: %s",
504 krb5_get_err_text(kcontext, problem));
505 ret = HTTP_INTERNAL_SERVER_ERROR;
509 ccname = ap_pstrdup(r->pool, krb5_cc_get_name(kcontext, tmp_ccache));
511 problem = krb5_cc_initialize(kcontext, tmp_ccache, princ);
513 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
514 "Cannot initialize krb5 ccache %s: krb5_cc_initialize() failed: %s",
515 ccname, krb5_get_err_text(kcontext, problem));
516 ret = HTTP_INTERNAL_SERVER_ERROR;
520 ap_table_setn(r->subprocess_env, "KRB5CCNAME", ccname);
521 ap_register_cleanup(r->pool, ccname,
522 krb5_cache_cleanup, ap_null_cleanup);
524 *ccache = tmp_ccache;
531 krb5_cc_destroy(kcontext, tmp_ccache);
537 store_krb5_creds(krb5_context kcontext,
539 kerb_auth_config *conf,
540 krb5_ccache delegated_cred)
543 krb5_error_code problem;
544 krb5_principal princ;
548 problem = krb5_cc_get_principal(kcontext, delegated_cred, &princ);
550 snprintf(errstr, sizeof(errstr), "krb5_cc_get_principal() failed: %s",
551 krb5_get_err_text(kcontext, problem));
552 return HTTP_INTERNAL_SERVER_ERROR;
555 ret = create_krb5_ccache(kcontext, r, conf, princ, &ccache);
557 krb5_free_principal(kcontext, princ);
562 problem = krb5_cc_copy_cache(kcontext, delegated_cred, ccache);
564 problem = krb5_cc_copy_creds(kcontext, delegated_cred, ccache);
566 krb5_free_principal(kcontext, princ);
568 snprintf(errstr, sizeof(errstr), "Failed to store credentials: %s",
569 krb5_get_err_text(kcontext, problem));
570 krb5_cc_destroy(kcontext, ccache);
571 return HTTP_INTERNAL_SERVER_ERROR;
574 krb5_cc_close(kcontext, ccache);
579 int authenticate_user_krb5pwd(request_rec *r,
580 kerb_auth_config *conf,
581 const char *auth_line)
583 const char *sent_pw = NULL;
584 const char *sent_name = NULL;
585 const char *realms = NULL;
586 krb5_context kcontext = NULL;
587 krb5_error_code code;
588 krb5_principal client = NULL;
589 krb5_ccache ccache = NULL;
590 krb5_keytab keytab = NULL;
594 code = krb5_init_context(&kcontext);
596 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
597 "Cannot initialize Kerberos5 context (%d)", code);
598 return HTTP_INTERNAL_SERVER_ERROR;
601 sent_pw = ap_pbase64decode(r->pool, auth_line);
602 sent_name = ap_getword (r->pool, &sent_pw, ':');
603 /* do not allow user to override realm setting of server */
604 if (strchr(sent_name, '@')) {
605 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
606 "specifying realm in user name is prohibited");
607 ret = HTTP_UNAUTHORIZED;
612 code = krb5_cc_gen_new(kcontext, &krb5_mcc_ops, &ccache);
614 code = krb5_mcc_generate_new(kcontext, &ccache);
617 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
618 "Cannot generate new ccache: %s",
619 krb5_get_err_text(kcontext, code));
620 ret = HTTP_INTERNAL_SERVER_ERROR;
624 if (conf->krb_5_keytab)
625 krb5_kt_resolve(kcontext, conf->krb_5_keytab, &keytab);
626 /* setenv("KRB5_KTNAME", conf->krb_5_keytab, 1); */
628 realms = conf->krb_auth_realms;
630 if (realms && (code = krb5_set_default_realm(kcontext,
631 ap_getword_white(r->pool, &realms))))
635 krb5_free_principal(kcontext, client);
638 code = krb5_parse_name(kcontext, sent_name, &client);
642 code = verify_krb5_user(r, kcontext, client, ccache, sent_pw, "khttp",
647 /* ap_getword_white() used above shifts the parameter, so it's not
648 needed to touch the realms variable */
649 } while (realms && *realms);
651 memset((char *)sent_pw, 0, strlen(sent_pw));
654 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
655 "Verifying krb5 password failed: %s",
656 krb5_get_err_text(kcontext, code));
657 ret = HTTP_UNAUTHORIZED;
661 code = krb5_unparse_name(kcontext, client, &name);
663 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "krb5_unparse_name() failed: %s",
664 krb5_get_err_text(kcontext, code));
665 ret = HTTP_UNAUTHORIZED;
668 MK_USER = ap_pstrdup (r->pool, name);
669 MK_AUTH_TYPE = "Basic";
672 if (conf->krb_save_credentials)
673 store_krb5_creds(kcontext, r, conf, ccache);
679 krb5_free_principal(kcontext, client);
681 krb5_cc_destroy(kcontext, ccache);
683 krb5_kt_close(kcontext, keytab);
684 krb5_free_context(kcontext);
689 /*********************************************************************
690 * GSSAPI Authentication
691 ********************************************************************/
694 get_gss_error(MK_POOL *p, OM_uint32 error_status, char *prefix)
696 OM_uint32 maj_stat, min_stat;
697 OM_uint32 msg_ctx = 0;
698 gss_buffer_desc status_string;
702 snprintf(buf, sizeof(buf), "%s", prefix);
705 maj_stat = gss_display_status (&min_stat,
711 if (sizeof(buf) > len + status_string.length + 1) {
712 sprintf(buf+len, ": %s", (char*) status_string.value);
713 len += status_string.length;
715 gss_release_buffer(&min_stat, &status_string);
716 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
718 return (ap_pstrdup(p, buf));
722 cleanup_gss_connection(void *data)
724 OM_uint32 minor_status;
725 gss_connection_t *gss_conn = (gss_connection_t *)data;
729 if (gss_conn->context != GSS_C_NO_CONTEXT)
730 gss_delete_sec_context(&minor_status, &gss_conn->context,
732 if (gss_conn->server_creds != GSS_C_NO_CREDENTIAL)
733 gss_release_cred(&minor_status, &gss_conn->server_creds);
735 gss_connection = NULL;
741 store_gss_creds(request_rec *r, kerb_auth_config *conf, char *princ_name,
742 gss_cred_id_t delegated_cred)
744 OM_uint32 maj_stat, min_stat;
745 krb5_principal princ = NULL;
746 krb5_ccache ccache = NULL;
747 krb5_error_code problem;
748 krb5_context context;
749 int ret = HTTP_INTERNAL_SERVER_ERROR;
751 problem = krb5_init_context(&context);
753 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Cannot initialize krb5 context");
754 return HTTP_INTERNAL_SERVER_ERROR;
757 problem = krb5_parse_name(context, princ_name, &princ);
759 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
760 "Cannot parse delegated username (%s)", krb5_get_err_text(context, problem));
764 problem = create_krb5_ccache(context, r, conf, princ, &ccache);
766 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
767 "Cannot create krb5 ccache (%s)", krb5_get_err_text(context, problem));
771 maj_stat = gss_krb5_copy_ccache(&min_stat, delegated_cred, ccache);
772 if (GSS_ERROR(maj_stat)) {
773 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
774 "Cannot store delegated credential (%s)",
775 get_gss_error(r->pool, min_stat, "gss_krb5_copy_ccache"));
779 krb5_cc_close(context, ccache);
785 krb5_free_principal(context, princ);
787 krb5_cc_destroy(context, ccache);
788 krb5_free_context(context);
793 get_gss_creds(request_rec *r,
794 kerb_auth_config *conf,
795 gss_cred_id_t *server_creds)
797 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
798 OM_uint32 major_status, minor_status, minor_status2;
799 gss_name_t server_name = GSS_C_NO_NAME;
802 snprintf(buf, sizeof(buf), "%s/%s", "khttp", ap_get_server_name(r));
804 input_token.value = buf;
805 input_token.length = strlen(buf) + 1;
807 major_status = gss_import_name(&minor_status, &input_token,
810 if (GSS_ERROR(major_status)) {
811 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
812 "%s", get_gss_error(r->pool, minor_status,
813 "gss_import_name() failed"));
814 return HTTP_INTERNAL_SERVER_ERROR;
817 major_status = gss_acquire_cred(&minor_status, server_name, GSS_C_INDEFINITE,
818 GSS_C_NO_OID_SET, GSS_C_ACCEPT,
819 server_creds, NULL, NULL);
820 gss_release_name(&minor_status2, &server_name);
821 if (GSS_ERROR(major_status)) {
822 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
823 "%s", get_gss_error(r->pool, minor_status,
824 "gss_acquire_cred() failed"));
825 return HTTP_INTERNAL_SERVER_ERROR;
832 authenticate_user_gss(request_rec *r,
833 kerb_auth_config *conf,
834 const char *auth_line)
836 OM_uint32 major_status, minor_status, minor_status2;
837 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
838 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
839 const char *auth_param = NULL;
841 gss_name_t client_name = GSS_C_NO_NAME;
842 gss_cred_id_t delegated_cred = GSS_C_NO_CREDENTIAL;
843 static int initial_return = HTTP_UNAUTHORIZED;
845 /* needed to work around replay caches */
846 if (!ap_is_initial_req(r))
847 return initial_return;
848 initial_return = HTTP_UNAUTHORIZED;
850 if (gss_connection == NULL) {
851 gss_connection = ap_pcalloc(r->connection->pool, sizeof(*gss_connection));
852 if (gss_connection == NULL) {
853 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
854 "ap_pcalloc() failed (not enough memory)");
855 ret = HTTP_INTERNAL_SERVER_ERROR;
858 memset(gss_connection, 0, sizeof(*gss_connection));
859 ap_register_cleanup(r->connection->pool, gss_connection, cleanup_gss_connection, ap_null_cleanup);
862 if (conf->krb_5_keytab)
863 setenv("KRB5_KTNAME", conf->krb_5_keytab, 1);
865 if (gss_connection->server_creds == GSS_C_NO_CREDENTIAL) {
866 ret = get_gss_creds(r, conf, &gss_connection->server_creds);
871 /* ap_getword() shifts parameter */
872 auth_param = ap_getword_white(r->pool, &auth_line);
873 if (auth_param == NULL) {
874 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
875 "No Authorization parameter in request from client");
876 ret = HTTP_UNAUTHORIZED;
880 input_token.length = ap_base64decode_len(auth_param) + 1;
881 input_token.value = ap_pcalloc(r->connection->pool, input_token.length);
882 if (input_token.value == NULL) {
883 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
884 "ap_pcalloc() failed (not enough memory)");
885 ret = HTTP_INTERNAL_SERVER_ERROR;
888 input_token.length = ap_base64decode(input_token.value, auth_param);
891 major_status = gss_accept_sec_context(
893 major_status = gss_accept_sec_context_spnego(
896 &gss_connection->context,
897 gss_connection->server_creds,
899 GSS_C_NO_CHANNEL_BINDINGS,
906 if (output_token.length) {
910 len = ap_base64encode_len(output_token.length) + 1;
911 token = ap_pcalloc(r->connection->pool, len + 1);
913 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
914 "ap_pcalloc() failed (not enough memory)");
915 ret = HTTP_INTERNAL_SERVER_ERROR;
916 gss_release_buffer(&minor_status2, &output_token);
919 ap_base64encode(token, output_token.value, output_token.length);
921 ap_table_set(r->err_headers_out, "WWW-Authenticate",
922 ap_pstrcat(r->pool, "GSS-Negotiate ", token, NULL));
923 gss_release_buffer(&minor_status2, &output_token);
926 if (GSS_ERROR(major_status)) {
927 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
928 "%s", get_gss_error(r->pool, minor_status,
929 "gss_accept_sec_context() failed"));
930 ret = HTTP_UNAUTHORIZED;
934 if (major_status & GSS_S_CONTINUE_NEEDED) {
935 /* Some GSSAPI mechanism (eg GSI from Globus) may require multiple
936 * iterations to establish authentication */
937 ret = HTTP_UNAUTHORIZED;
941 major_status = gss_display_name(&minor_status, client_name, &output_token, NULL);
942 gss_release_name(&minor_status, &client_name);
943 if (GSS_ERROR(major_status)) {
944 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
945 "%s", get_gss_error(r->pool, minor_status,
946 "gss_export_name() failed"));
947 ret = HTTP_INTERNAL_SERVER_ERROR;
951 MK_AUTH_TYPE = "Negotiate";
952 MK_USER = ap_pstrdup(r->pool, output_token.value);
954 if (conf->krb_save_credentials && delegated_cred != GSS_C_NO_CREDENTIAL)
955 store_gss_creds(r, conf, (char *)output_token.value, delegated_cred);
957 gss_release_buffer(&minor_status, &output_token);
963 gss_release_cred(&minor_status, &delegated_cred);
965 if (output_token.length)
966 gss_release_buffer(&minor_status, &output_token);
968 if (client_name != GSS_C_NO_NAME)
969 gss_release_name(&minor_status, &client_name);
971 cleanup_gss_connection(gss_connection);
973 initial_return = ret;
980 note_kerb_auth_failure(request_rec *r, const kerb_auth_config *conf,
981 int use_krb4, int use_krb5)
983 const char *auth_name = NULL;
986 /* get the user realm specified in .htaccess */
987 auth_name = ap_auth_name(r);
989 /* XXX should the WWW-Authenticate header be cleared first? */
991 if (use_krb5 && conf->krb_method_gssapi)
992 ap_table_add(r->err_headers_out, "WWW-Authenticate", "GSS-Negotiate ");
993 if (use_krb5 && conf->krb_method_k5pass) {
994 ap_table_add(r->err_headers_out, "WWW-Authenticate",
995 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1001 if (use_krb4 && conf->krb_method_k4pass && !set_basic)
1002 ap_table_add(r->err_headers_out, "WWW-Authenticate",
1003 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1007 int kerb_authenticate_user(request_rec *r)
1009 kerb_auth_config *conf =
1010 (kerb_auth_config *) ap_get_module_config(r->per_dir_config,
1012 const char *auth_type = NULL;
1013 const char *auth_line = NULL;
1014 const char *type = NULL;
1015 int use_krb5 = 0, use_krb4 = 0;
1018 /* get the type specified in .htaccess */
1019 type = ap_auth_type(r);
1021 if (type && strcasecmp(type, "Kerberos") == 0)
1022 use_krb5 = use_krb4 = 1;
1023 else if(type && strcasecmp(type, "KerberosV5") == 0)
1025 else if(type && strcasecmp(type, "KerberosV4") == 0)
1030 /* get what the user sent us in the HTTP header */
1031 auth_line = MK_TABLE_GET(r->headers_in, "Authorization");
1033 note_kerb_auth_failure(r, conf, use_krb4, use_krb5);
1034 return HTTP_UNAUTHORIZED;
1036 auth_type = ap_getword_white(r->pool, &auth_line);
1038 ret = HTTP_UNAUTHORIZED;
1041 if (use_krb5 && conf->krb_method_gssapi &&
1042 strcasecmp(auth_type, "GSS-Negotiate") == 0) {
1043 ret = authenticate_user_gss(r, conf, auth_line);
1044 } else if (use_krb5 && conf->krb_method_k5pass &&
1045 strcasecmp(auth_type, "Basic") == 0) {
1046 ret = authenticate_user_krb5pwd(r, conf, auth_line);
1051 if (ret == HTTP_UNAUTHORIZED && use_krb4 && conf->krb_method_k4pass &&
1052 strcasecmp(auth_type, "Basic") == 0)
1053 ret = authenticate_user_krb4pwd(r, conf, auth_line);
1056 if (ret == HTTP_UNAUTHORIZED)
1057 note_kerb_auth_failure(r, conf, use_krb4, use_krb5);
1063 /***************************************************************************
1064 Module Setup/Configuration
1065 ***************************************************************************/
1067 module MODULE_VAR_EXPORT auth_kerb_module = {
1068 STANDARD_MODULE_STUFF,
1069 NULL, /* module initializer */
1070 kerb_dir_create_config, /* per-directory config creator */
1071 NULL, /* per-directory config merger */
1072 NULL, /* per-server config creator */
1073 NULL, /* per-server config merger */
1074 kerb_auth_cmds, /* command table */
1075 NULL, /* [ 9] content handlers */
1076 NULL, /* [ 2] URI-to-filename translation */
1077 kerb_authenticate_user, /* [ 5] check/validate user_id */
1078 NULL, /* [ 6] check user_id is valid *here* */
1079 NULL, /* [ 4] check access by host address */
1080 NULL, /* [ 7] MIME type checker/setter */
1081 NULL, /* [ 8] fixups */
1082 NULL, /* [10] logger */
1083 NULL, /* [ 3] header parser */
1084 NULL, /* process initialization */
1085 NULL, /* process exit/cleanup */
1086 NULL /* [ 1] post read_request handling */
1089 void kerb_register_hooks(apr_pool_t *p)
1091 ap_hook_check_user_id(kerb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
1094 module AP_MODULE_DECLARE_DATA auth_kerb_module =
1096 STANDARD20_MODULE_STUFF,
1097 kerb_dir_create_config, /* create per-dir conf structures */
1098 NULL, /* merge per-dir conf structures */
1099 NULL, /* create per-server conf structures */
1100 NULL, /* merge per-server conf structures */
1101 kerb_auth_cmds, /* table of configuration directives */
1102 kerb_register_hooks /* register hooks */