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.
75 #define MODAUTHKERB_VERSION "5.0-rc2"
78 #include "ap_compat.h"
79 #include "apr_strings.h"
82 #include "http_config.h"
83 #include "http_core.h"
85 #include "http_protocol.h"
86 #include "http_request.h"
93 # include <gssapi/gssapi.h>
94 # include <gssapi/gssapi_generic.h>
95 # define GSS_C_NT_USER_NAME gss_nt_user_name
96 # define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
97 # define krb5_get_err_text(context,code) error_message(code)
102 /*Prevent warning about closesocket redefinition (Apache's ap_config.h and
103 * MIT Kerberos' port-sockets.h both define it as close) */
108 #include <netdb.h> /* gethostbyname() */
112 module auth_kerb_module;
114 module AP_MODULE_DECLARE_DATA auth_kerb_module;
117 /***************************************************************************
118 Macros To Ease Compatibility
119 ***************************************************************************/
122 #define MK_TABLE_GET ap_table_get
123 #define MK_USER r->connection->user
124 #define MK_AUTH_TYPE r->connection->ap_auth_type
126 #define MK_POOL apr_pool_t
127 #define MK_TABLE_GET apr_table_get
128 #define MK_USER r->user
129 #define MK_AUTH_TYPE r->ap_auth_type
133 /***************************************************************************
134 Auth Configuration Structure
135 ***************************************************************************/
137 char *krb_auth_realms;
138 int krb_save_credentials;
141 int krb_method_gssapi;
142 int krb_method_k5pass;
146 int krb_method_k4pass;
151 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg);
154 #define command(name, func, var, type, usage) \
156 (void*)XtOffsetOf(kerb_auth_config, var), \
157 OR_AUTHCFG, type, usage }
159 #define command(name, func, var, type, usage) \
160 AP_INIT_ ## type (name, func, \
161 (void*)APR_XtOffsetOf(kerb_auth_config, var), \
165 static const command_rec kerb_auth_cmds[] = {
166 command("KrbAuthRealms", krb5_save_realms, krb_auth_realms,
167 RAW_ARGS, "Realms to attempt authentication against (can be multiple)."),
169 command("KrbAuthRealm", krb5_save_realms, krb_auth_realms,
170 RAW_ARGS, "Alias for KrbAuthRealms."),
172 command("KrbSaveCredentials", ap_set_flag_slot, krb_save_credentials,
173 FLAG, "Save and store credentials/tickets retrieved during auth."),
176 command("Krb5Keytab", ap_set_file_slot, krb_5_keytab,
177 TAKE1, "Location of Kerberos V5 keytab file."),
179 command("KrbMethodNegotiate", ap_set_flag_slot, krb_method_gssapi,
180 FLAG, "Enable Negotiate authentication method."),
182 command("KrbMethodK5Pass", ap_set_flag_slot, krb_method_k5pass,
183 FLAG, "Enable Kerberos V5 password authentication."),
187 command("Krb4Srvtab", ap_set_file_slot, krb_4_srvtab,
188 TAKE1, "Location of Kerberos V4 srvtab file."),
190 command("KrbMethodK4Pass", ap_set_flag_slot, krb_method_k4pass,
191 FLAG, "Enable Kerberos V4 password authentication."),
199 gss_ctx_id_t context;
200 gss_cred_id_t server_creds;
203 static gss_connection_t *gss_connection = NULL;
207 /***************************************************************************
208 Auth Configuration Initialization
209 ***************************************************************************/
210 static void *kerb_dir_create_config(MK_POOL *p, char *d)
212 kerb_auth_config *rec;
214 rec = (kerb_auth_config *) ap_pcalloc(p, sizeof(kerb_auth_config));
216 ((kerb_auth_config *)rec)->krb_method_k5pass = 1;
217 ((kerb_auth_config *)rec)->krb_method_gssapi = 1;
220 ((kerb_auth_config *)rec)->krb_method_k4pass = 1;
226 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg)
228 sec->krb_auth_realms= ap_pstrdup(cmd->pool, arg);
232 void log_rerror(const char *file, int line, int level, int status,
233 const request_rec *r, const char *fmt, ...)
240 vsnprintf(errstr, sizeof(errstr), fmt, ap);
245 snprintf(errnostr, sizeof(errnostr), "%s: (%s)", errstr, strerror(errno));
247 snprintf(errnostr, sizeof(errnostr), "%s", errstr);
250 ap_log_rerror(file, line, level | APLOG_NOERRNO, r, "%s", errnostr);
252 ap_log_rerror(file, line, level | APLOG_NOERRNO, status, r, "%s", errnostr);
257 /***************************************************************************
258 Username/Password Validation for Krb4
259 ***************************************************************************/
261 verify_krb4_user(request_rec *r, char *name, char *instance, char *realm,
262 char *password, char *linstance, char *srvtab)
268 const char *hostname;
271 char lrealm[REALM_SZ];
273 ret = krb_get_pw_in_tkt(name, instance, realm, "krbtgt", realm,
274 DEFAULT_TKT_LIFE, password);
276 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
277 "Cannot get krb4 ticket: krb_get_pw_in_tkt() failed: %s",
278 krb_get_err_text(ret));
282 hostname = ap_get_server_name(r);
284 hp = gethostbyname(hostname);
287 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
288 "Cannot verify krb4 ticket: gethostbyname() failed: %s",
292 memcpy(&addr, hp->h_addr, sizeof(addr));
294 phost = krb_get_phost((char *)hostname);
296 krb_get_lrealm(lrealm, 1);
298 ret = krb_mk_req(&ticket, linstance, phost, lrealm, 0);
300 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
301 "Cannot verify krb4 ticket: krb_mk_req() failed: %s",
302 krb_get_err_text(ret));
307 ret = krb_rd_req(&ticket, linstance, phost, addr, &authdata, srvtab);
309 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
310 "Cannot verify krb4 ticket: krb_rd_req() failed: %s",
311 krb_get_err_text(ret));
319 krb4_cache_cleanup(void *data)
321 char *tkt_file = (char *) data;
323 krb_set_tkt_string(tkt_file);
329 authenticate_user_krb4pwd(request_rec *r,
330 kerb_auth_config *conf,
331 const char *auth_line)
335 const char *sent_name;
338 char *tkt_file_p = NULL;
343 char lrealm[REALM_SZ];
345 sent_pw = ap_pbase64decode(r->pool, auth_line);
346 sent_name = ap_getword (r->pool, &sent_pw, ':');
348 /* do not allow user to override realm setting of server */
349 if (strchr(sent_name, '@')) {
350 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
351 "specifying realm in user name is prohibited");
352 return HTTP_UNAUTHORIZED;
355 sent_instance = strchr(sent_name, '.');
357 *sent_instance++ = '\0';
359 snprintf(tkt_file, sizeof(tkt_file), "/tmp/apache_tkt_XXXXXX");
360 fd = mkstemp(tkt_file);
362 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
363 "Cannot create krb4 ccache: mkstemp() failed: %s",
365 return HTTP_INTERNAL_SERVER_ERROR;
368 tkt_file_p = ap_pstrdup(r->pool, tkt_file);
369 ap_register_cleanup(r->pool, tkt_file_p,
370 krb4_cache_cleanup, ap_null_cleanup);
372 krb_set_tkt_string(tkt_file);
374 realms = conf->krb_auth_realms;
376 memset(lrealm, 0, sizeof(lrealm));
379 realm = ap_getword_white(r->pool, &realms);
382 ret = krb_get_lrealm(lrealm, 1);
385 if (realm == NULL || *realm == '\0')
388 ret = verify_krb4_user(r, (char *)sent_name,
389 (sent_instance) ? sent_instance : "",
390 (char *)realm, (char *)sent_pw, "khttp",
394 } while (realms && *realms);
397 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Verifying krb4 password failed");
398 ret = HTTP_UNAUTHORIZED;
402 user = ap_pstrdup(r->pool, sent_name);
404 user = ap_pstrcat(r->pool, user, ".", sent_instance, NULL);
405 user = ap_pstrcat(r->pool, user, "@", realm, NULL);
408 MK_AUTH_TYPE = "Basic";
409 ap_table_setn(r->subprocess_env, "KRBTKFILE", tkt_file_p);
411 if (!conf->krb_save_credentials)
412 krb4_cache_cleanup(tkt_file);
416 krb4_cache_cleanup(tkt_file);
425 /***************************************************************************
426 Username/Password Validation for Krb5
427 ***************************************************************************/
428 /* Inspired by krb5_verify_user from Heimdal */
429 static krb5_error_code
430 verify_krb5_user(request_rec *r, krb5_context context, krb5_principal principal,
431 krb5_ccache ccache, const char *password, const char *service,
435 krb5_principal server = NULL;
437 krb5_verify_init_creds_opt opt;
439 memset(&creds, 0, sizeof(creds));
441 ret = krb5_get_init_creds_password(context, &creds, principal,
442 (char *)password, krb5_prompter_posix,
443 NULL, 0, NULL, NULL);
447 ret = krb5_sname_to_principal(context, ap_get_server_name(r), service,
448 KRB5_NT_SRV_HST, &server);
452 krb5_verify_init_creds_opt_init(&opt);
453 krb5_verify_init_creds_opt_set_ap_req_nofail(&opt, 1);
455 ret = krb5_verify_init_creds(context, &creds, server, keytab, NULL, &opt);
460 ret = krb5_cc_initialize(context, ccache, principal);
462 ret = krb5_cc_store_cred(context, ccache, &creds);
466 krb5_free_cred_contents(context, &creds);
468 krb5_free_principal(context, server);
473 krb5_cache_cleanup(void *data)
475 krb5_context context;
477 krb5_error_code problem;
478 char *cache_name = (char *) data;
480 problem = krb5_init_context(&context);
482 /* ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "krb5_init_context() failed"); */
483 return HTTP_INTERNAL_SERVER_ERROR;
486 problem = krb5_cc_resolve(context, cache_name, &cache);
488 /* log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
489 "krb5_cc_resolve() failed (%s: %s)",
490 cache_name, krb5_get_err_text(context, problem)); */
491 return HTTP_INTERNAL_SERVER_ERROR;
494 krb5_cc_destroy(context, cache);
495 krb5_free_context(context);
500 create_krb5_ccache(krb5_context kcontext,
502 kerb_auth_config *conf,
503 krb5_principal princ,
507 krb5_error_code problem;
509 krb5_ccache tmp_ccache = NULL;
511 #ifdef HAVE_KRB5_CC_GEN_NEW
512 problem = krb5_cc_gen_new(kcontext, &krb5_fcc_ops, &tmp_ccache);
514 /* only older MIT seem to not have the krb5_cc_gen_new() call, so we use
515 * MIT specific call here */
516 problem = krb5_fcc_generate_new(kcontext, &tmp_ccache);
517 /* krb5_fcc_generate_new() doesn't set KRB5_TC_OPENCLOSE, which makes
518 krb5_cc_initialize() fail */
519 krb5_fcc_set_flags(kcontext, tmp_ccache, KRB5_TC_OPENCLOSE);
522 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
523 "Cannot create file for new krb5 ccache: %s",
524 krb5_get_err_text(kcontext, problem));
525 ret = HTTP_INTERNAL_SERVER_ERROR;
529 ccname = ap_pstrdup(r->pool, krb5_cc_get_name(kcontext, tmp_ccache));
531 problem = krb5_cc_initialize(kcontext, tmp_ccache, princ);
533 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
534 "Cannot initialize krb5 ccache %s: krb5_cc_initialize() failed: %s",
535 ccname, krb5_get_err_text(kcontext, problem));
536 ret = HTTP_INTERNAL_SERVER_ERROR;
540 ap_table_setn(r->subprocess_env, "KRB5CCNAME", ccname);
541 ap_register_cleanup(r->pool, ccname,
542 krb5_cache_cleanup, ap_null_cleanup);
544 *ccache = tmp_ccache;
551 krb5_cc_destroy(kcontext, tmp_ccache);
557 store_krb5_creds(krb5_context kcontext,
559 kerb_auth_config *conf,
560 krb5_ccache delegated_cred)
563 krb5_error_code problem;
564 krb5_principal princ;
568 problem = krb5_cc_get_principal(kcontext, delegated_cred, &princ);
570 snprintf(errstr, sizeof(errstr), "krb5_cc_get_principal() failed: %s",
571 krb5_get_err_text(kcontext, problem));
572 return HTTP_INTERNAL_SERVER_ERROR;
575 ret = create_krb5_ccache(kcontext, r, conf, princ, &ccache);
577 krb5_free_principal(kcontext, princ);
582 problem = krb5_cc_copy_cache(kcontext, delegated_cred, ccache);
584 problem = krb5_cc_copy_creds(kcontext, delegated_cred, ccache);
586 krb5_free_principal(kcontext, princ);
588 snprintf(errstr, sizeof(errstr), "Failed to store credentials: %s",
589 krb5_get_err_text(kcontext, problem));
590 krb5_cc_destroy(kcontext, ccache);
591 return HTTP_INTERNAL_SERVER_ERROR;
594 krb5_cc_close(kcontext, ccache);
599 int authenticate_user_krb5pwd(request_rec *r,
600 kerb_auth_config *conf,
601 const char *auth_line)
603 const char *sent_pw = NULL;
604 const char *sent_name = NULL;
605 const char *realms = NULL;
606 krb5_context kcontext = NULL;
607 krb5_error_code code;
608 krb5_principal client = NULL;
609 krb5_ccache ccache = NULL;
610 krb5_keytab keytab = NULL;
614 code = krb5_init_context(&kcontext);
616 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
617 "Cannot initialize Kerberos5 context (%d)", code);
618 return HTTP_INTERNAL_SERVER_ERROR;
621 sent_pw = ap_pbase64decode(r->pool, auth_line);
622 sent_name = ap_getword (r->pool, &sent_pw, ':');
623 /* do not allow user to override realm setting of server */
624 if (strchr(sent_name, '@')) {
625 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
626 "specifying realm in user name is prohibited");
627 ret = HTTP_UNAUTHORIZED;
631 #ifdef HAVE_KRB5_CC_GEN_NEW
632 code = krb5_cc_gen_new(kcontext, &krb5_mcc_ops, &ccache);
634 /* only older MIT seem to not have the krb5_cc_gen_new() call, so we use
635 * MIT specific call here */
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;
865 if (gss_connection == NULL) {
866 gss_connection = ap_pcalloc(r->connection->pool, sizeof(*gss_connection));
867 if (gss_connection == NULL) {
868 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
869 "ap_pcalloc() failed (not enough memory)");
870 ret = HTTP_INTERNAL_SERVER_ERROR;
873 memset(gss_connection, 0, sizeof(*gss_connection));
874 ap_register_cleanup(r->connection->pool, gss_connection, cleanup_gss_connection, ap_null_cleanup);
877 if (conf->krb_5_keytab) {
879 /* we don't use the ap_* calls here, since the string passed to putenv()
880 * will become part of the enviroment and shouldn't be free()ed by apache
882 ktname = malloc(strlen("KRB5_KTNAME=") + strlen(conf->krb_5_keytab) + 1);
883 if (ktname == NULL) {
884 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "malloc() failed: not enough memory");
885 ret = HTTP_INTERNAL_SERVER_ERROR;
888 sprintf(ktname, "KRB5_KTNAME=%s", conf->krb_5_keytab);
892 if (gss_connection->server_creds == GSS_C_NO_CREDENTIAL) {
893 ret = get_gss_creds(r, conf, &gss_connection->server_creds);
898 /* ap_getword() shifts parameter */
899 auth_param = ap_getword_white(r->pool, &auth_line);
900 if (auth_param == NULL) {
901 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
902 "No Authorization parameter in request from client");
903 ret = HTTP_UNAUTHORIZED;
907 input_token.length = ap_base64decode_len(auth_param) + 1;
908 input_token.value = ap_pcalloc(r->connection->pool, input_token.length);
909 if (input_token.value == NULL) {
910 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
911 "ap_pcalloc() failed (not enough memory)");
912 ret = HTTP_INTERNAL_SERVER_ERROR;
915 input_token.length = ap_base64decode(input_token.value, auth_param);
918 major_status = gss_accept_sec_context(
920 major_status = gss_accept_sec_context_spnego(
923 &gss_connection->context,
924 gss_connection->server_creds,
926 GSS_C_NO_CHANNEL_BINDINGS,
933 if (output_token.length) {
937 len = ap_base64encode_len(output_token.length) + 1;
938 token = ap_pcalloc(r->connection->pool, len + 1);
940 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
941 "ap_pcalloc() failed (not enough memory)");
942 ret = HTTP_INTERNAL_SERVER_ERROR;
943 gss_release_buffer(&minor_status2, &output_token);
946 ap_base64encode(token, output_token.value, output_token.length);
948 ap_table_set(r->err_headers_out, "WWW-Authenticate",
949 ap_pstrcat(r->pool, "Negotiate ", token, NULL));
950 gss_release_buffer(&minor_status2, &output_token);
953 if (GSS_ERROR(major_status)) {
954 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
955 "%s", get_gss_error(r->pool, minor_status,
956 "gss_accept_sec_context() failed"));
957 ret = HTTP_UNAUTHORIZED;
961 if (major_status & GSS_S_CONTINUE_NEEDED) {
962 /* Some GSSAPI mechanism (eg GSI from Globus) may require multiple
963 * iterations to establish authentication */
964 ret = HTTP_UNAUTHORIZED;
968 major_status = gss_display_name(&minor_status, client_name, &output_token, NULL);
969 gss_release_name(&minor_status, &client_name);
970 if (GSS_ERROR(major_status)) {
971 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
972 "%s", get_gss_error(r->pool, minor_status,
973 "gss_export_name() failed"));
974 ret = HTTP_INTERNAL_SERVER_ERROR;
978 MK_AUTH_TYPE = "Negotiate";
979 MK_USER = ap_pstrdup(r->pool, output_token.value);
981 if (conf->krb_save_credentials && delegated_cred != GSS_C_NO_CREDENTIAL)
982 store_gss_creds(r, conf, (char *)output_token.value, delegated_cred);
984 gss_release_buffer(&minor_status, &output_token);
990 gss_release_cred(&minor_status, &delegated_cred);
992 if (output_token.length)
993 gss_release_buffer(&minor_status, &output_token);
995 if (client_name != GSS_C_NO_NAME)
996 gss_release_name(&minor_status, &client_name);
998 cleanup_gss_connection(gss_connection);
1005 already_succeeded(request_rec *r)
1007 if (ap_is_initial_req(r) || MK_AUTH_TYPE == NULL)
1009 if (strcmp(MK_AUTH_TYPE, "Negotiate") ||
1010 (strcmp(MK_AUTH_TYPE, "Basic") && strchr(MK_USER, '@')))
1016 note_kerb_auth_failure(request_rec *r, const kerb_auth_config *conf,
1017 int use_krb4, int use_krb5)
1019 const char *auth_name = NULL;
1022 /* get the user realm specified in .htaccess */
1023 auth_name = ap_auth_name(r);
1025 /* XXX should the WWW-Authenticate header be cleared first? */
1027 if (use_krb5 && conf->krb_method_gssapi)
1028 ap_table_add(r->err_headers_out, "WWW-Authenticate", "Negotiate");
1029 if (use_krb5 && conf->krb_method_k5pass) {
1030 ap_table_add(r->err_headers_out, "WWW-Authenticate",
1031 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1037 if (use_krb4 && conf->krb_method_k4pass && !set_basic)
1038 ap_table_add(r->err_headers_out, "WWW-Authenticate",
1039 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1043 int kerb_authenticate_user(request_rec *r)
1045 kerb_auth_config *conf =
1046 (kerb_auth_config *) ap_get_module_config(r->per_dir_config,
1048 const char *auth_type = NULL;
1049 const char *auth_line = NULL;
1050 const char *type = NULL;
1051 int use_krb5 = 0, use_krb4 = 0;
1053 static int last_return = HTTP_UNAUTHORIZED;
1055 /* get the type specified in .htaccess */
1056 type = ap_auth_type(r);
1058 if (type && strcasecmp(type, "Kerberos") == 0)
1059 use_krb5 = use_krb4 = 1;
1060 else if(type && strcasecmp(type, "KerberosV5") == 0)
1062 else if(type && strcasecmp(type, "KerberosV4") == 0)
1067 /* get what the user sent us in the HTTP header */
1068 auth_line = MK_TABLE_GET(r->headers_in, "Authorization");
1070 note_kerb_auth_failure(r, conf, use_krb4, use_krb5);
1071 return HTTP_UNAUTHORIZED;
1073 auth_type = ap_getword_white(r->pool, &auth_line);
1075 if (already_succeeded(r))
1078 ret = HTTP_UNAUTHORIZED;
1081 if (use_krb5 && conf->krb_method_gssapi &&
1082 strcasecmp(auth_type, "Negotiate") == 0) {
1083 ret = authenticate_user_gss(r, conf, auth_line);
1084 } else if (use_krb5 && conf->krb_method_k5pass &&
1085 strcasecmp(auth_type, "Basic") == 0) {
1086 ret = authenticate_user_krb5pwd(r, conf, auth_line);
1091 if (ret == HTTP_UNAUTHORIZED && use_krb4 && conf->krb_method_k4pass &&
1092 strcasecmp(auth_type, "Basic") == 0)
1093 ret = authenticate_user_krb4pwd(r, conf, auth_line);
1096 if (ret == HTTP_UNAUTHORIZED)
1097 note_kerb_auth_failure(r, conf, use_krb4, use_krb5);
1104 /***************************************************************************
1105 Module Setup/Configuration
1106 ***************************************************************************/
1108 module MODULE_VAR_EXPORT auth_kerb_module = {
1109 STANDARD_MODULE_STUFF,
1110 NULL, /* module initializer */
1111 kerb_dir_create_config, /* per-directory config creator */
1112 NULL, /* per-directory config merger */
1113 NULL, /* per-server config creator */
1114 NULL, /* per-server config merger */
1115 kerb_auth_cmds, /* command table */
1116 NULL, /* [ 9] content handlers */
1117 NULL, /* [ 2] URI-to-filename translation */
1118 kerb_authenticate_user, /* [ 5] check/validate user_id */
1119 NULL, /* [ 6] check user_id is valid *here* */
1120 NULL, /* [ 4] check access by host address */
1121 NULL, /* [ 7] MIME type checker/setter */
1122 NULL, /* [ 8] fixups */
1123 NULL, /* [10] logger */
1124 NULL, /* [ 3] header parser */
1125 NULL, /* process initialization */
1126 NULL, /* process exit/cleanup */
1127 NULL /* [ 1] post read_request handling */
1131 kerb_init_handler(apr_pool_t *p, apr_pool_t *plog,
1132 apr_pool_t *ptemp, server_rec *s)
1134 ap_add_version_component(p, "mod_auth_kerb/" MODAUTHKERB_VERSION);
1138 void kerb_register_hooks(apr_pool_t *p)
1140 ap_hook_post_config(kerb_init_handler, NULL, NULL, APR_HOOK_MIDDLE);
1141 ap_hook_check_user_id(kerb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
1144 module AP_MODULE_DECLARE_DATA auth_kerb_module =
1146 STANDARD20_MODULE_STUFF,
1147 kerb_dir_create_config, /* create per-dir conf structures */
1148 NULL, /* merge per-dir conf structures */
1149 NULL, /* create per-server conf structures */
1150 NULL, /* merge per-server conf structures */
1151 kerb_auth_cmds, /* table of configuration directives */
1152 kerb_register_hooks /* register hooks */