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;
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."),
176 command("KrbVerifyKDC", ap_set_flag_slot, krb_verify_kdc,
177 FLAG, "Verify tickets against keytab to prevent KDC spoofing attacks."),
180 command("Krb5Keytab", ap_set_file_slot, krb_5_keytab,
181 TAKE1, "Location of Kerberos V5 keytab file."),
183 command("KrbMethodNegotiate", ap_set_flag_slot, krb_method_gssapi,
184 FLAG, "Enable Negotiate authentication method."),
186 command("KrbMethodK5Pass", ap_set_flag_slot, krb_method_k5pass,
187 FLAG, "Enable Kerberos V5 password authentication."),
191 command("Krb4Srvtab", ap_set_file_slot, krb_4_srvtab,
192 TAKE1, "Location of Kerberos V4 srvtab file."),
194 command("KrbMethodK4Pass", ap_set_flag_slot, krb_method_k4pass,
195 FLAG, "Enable Kerberos V4 password authentication."),
203 gss_ctx_id_t context;
204 gss_cred_id_t server_creds;
207 static gss_connection_t *gss_connection = NULL;
211 /***************************************************************************
212 Auth Configuration Initialization
213 ***************************************************************************/
214 static void *kerb_dir_create_config(MK_POOL *p, char *d)
216 kerb_auth_config *rec;
218 rec = (kerb_auth_config *) ap_pcalloc(p, sizeof(kerb_auth_config));
219 ((kerb_auth_config *)rec)->krb_verify_kdc = 1;
221 ((kerb_auth_config *)rec)->krb_method_k5pass = 1;
222 ((kerb_auth_config *)rec)->krb_method_gssapi = 1;
225 ((kerb_auth_config *)rec)->krb_method_k4pass = 1;
231 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg)
233 sec->krb_auth_realms= ap_pstrdup(cmd->pool, arg);
237 void log_rerror(const char *file, int line, int level, int status,
238 const request_rec *r, const char *fmt, ...)
245 vsnprintf(errstr, sizeof(errstr), fmt, ap);
250 snprintf(errnostr, sizeof(errnostr), "%s: (%s)", errstr, strerror(errno));
252 snprintf(errnostr, sizeof(errnostr), "%s", errstr);
255 ap_log_rerror(file, line, level | APLOG_NOERRNO, r, "%s", errnostr);
257 ap_log_rerror(file, line, level | APLOG_NOERRNO, status, r, "%s", errnostr);
262 /***************************************************************************
263 Username/Password Validation for Krb4
264 ***************************************************************************/
266 verify_krb4_user(request_rec *r, char *name, char *instance, char *realm,
267 char *password, char *linstance, char *srvtab, int krb_verify_kdc)
273 const char *hostname;
276 char lrealm[REALM_SZ];
278 ret = krb_get_pw_in_tkt(name, instance, realm, "krbtgt", realm,
279 DEFAULT_TKT_LIFE, password);
281 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
282 "Cannot get krb4 ticket: krb_get_pw_in_tkt() failed: %s",
283 krb_get_err_text(ret));
290 hostname = ap_get_server_name(r);
292 hp = gethostbyname(hostname);
295 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
296 "Cannot verify krb4 ticket: gethostbyname() failed: %s",
300 memcpy(&addr, hp->h_addr, sizeof(addr));
302 phost = krb_get_phost((char *)hostname);
304 krb_get_lrealm(lrealm, 1);
306 ret = krb_mk_req(&ticket, linstance, phost, lrealm, 0);
308 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
309 "Cannot verify krb4 ticket: krb_mk_req() failed: %s",
310 krb_get_err_text(ret));
315 ret = krb_rd_req(&ticket, linstance, phost, addr, &authdata, srvtab);
317 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
318 "Cannot verify krb4 ticket: krb_rd_req() failed: %s",
319 krb_get_err_text(ret));
327 krb4_cache_cleanup(void *data)
329 char *tkt_file = (char *) data;
331 krb_set_tkt_string(tkt_file);
337 authenticate_user_krb4pwd(request_rec *r,
338 kerb_auth_config *conf,
339 const char *auth_line)
343 const char *sent_name;
346 char *tkt_file_p = NULL;
351 char lrealm[REALM_SZ];
353 sent_pw = ap_pbase64decode(r->pool, auth_line);
354 sent_name = ap_getword (r->pool, &sent_pw, ':');
356 /* do not allow user to override realm setting of server */
357 if (strchr(sent_name, '@')) {
358 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
359 "specifying realm in user name is prohibited");
360 return HTTP_UNAUTHORIZED;
363 sent_instance = strchr(sent_name, '.');
365 *sent_instance++ = '\0';
367 snprintf(tkt_file, sizeof(tkt_file), "/tmp/apache_tkt_XXXXXX");
368 fd = mkstemp(tkt_file);
370 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
371 "Cannot create krb4 ccache: mkstemp() failed: %s",
373 return HTTP_INTERNAL_SERVER_ERROR;
376 tkt_file_p = ap_pstrdup(r->pool, tkt_file);
377 ap_register_cleanup(r->pool, tkt_file_p,
378 krb4_cache_cleanup, ap_null_cleanup);
380 krb_set_tkt_string(tkt_file);
382 realms = conf->krb_auth_realms;
384 memset(lrealm, 0, sizeof(lrealm));
387 realm = ap_getword_white(r->pool, &realms);
390 ret = krb_get_lrealm(lrealm, 1);
393 if (realm == NULL || *realm == '\0')
396 ret = verify_krb4_user(r, (char *)sent_name,
397 (sent_instance) ? sent_instance : "",
398 (char *)realm, (char *)sent_pw, "khttp",
399 conf->krb_4_srvtab, conf->krb_verify_kdc);
402 } while (realms && *realms);
405 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Verifying krb4 password failed");
406 ret = HTTP_UNAUTHORIZED;
410 user = ap_pstrdup(r->pool, sent_name);
412 user = ap_pstrcat(r->pool, user, ".", sent_instance, NULL);
413 user = ap_pstrcat(r->pool, user, "@", realm, NULL);
416 MK_AUTH_TYPE = "Basic";
417 ap_table_setn(r->subprocess_env, "KRBTKFILE", tkt_file_p);
419 if (!conf->krb_save_credentials)
420 krb4_cache_cleanup(tkt_file);
424 krb4_cache_cleanup(tkt_file);
433 /***************************************************************************
434 Username/Password Validation for Krb5
435 ***************************************************************************/
436 /* Inspired by krb5_verify_user from Heimdal */
437 static krb5_error_code
438 verify_krb5_user(request_rec *r, krb5_context context, krb5_principal principal,
439 krb5_ccache ccache, const char *password, const char *service,
440 krb5_keytab keytab, int krb_verify_kdc)
443 krb5_principal server = NULL;
445 krb5_verify_init_creds_opt opt;
447 memset(&creds, 0, sizeof(creds));
449 ret = krb5_get_init_creds_password(context, &creds, principal,
450 (char *)password, krb5_prompter_posix,
451 NULL, 0, NULL, NULL);
455 ret = krb5_sname_to_principal(context, ap_get_server_name(r), service,
456 KRB5_NT_SRV_HST, &server);
460 krb5_verify_init_creds_opt_init(&opt);
461 krb5_verify_init_creds_opt_set_ap_req_nofail(&opt, krb_verify_kdc);
463 ret = krb5_verify_init_creds(context, &creds, server, keytab, NULL, &opt);
468 ret = krb5_cc_initialize(context, ccache, principal);
470 ret = krb5_cc_store_cred(context, ccache, &creds);
474 krb5_free_cred_contents(context, &creds);
476 krb5_free_principal(context, server);
481 krb5_cache_cleanup(void *data)
483 krb5_context context;
485 krb5_error_code problem;
486 char *cache_name = (char *) data;
488 problem = krb5_init_context(&context);
490 /* ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "krb5_init_context() failed"); */
491 return HTTP_INTERNAL_SERVER_ERROR;
494 problem = krb5_cc_resolve(context, cache_name, &cache);
496 /* log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
497 "krb5_cc_resolve() failed (%s: %s)",
498 cache_name, krb5_get_err_text(context, problem)); */
499 return HTTP_INTERNAL_SERVER_ERROR;
502 krb5_cc_destroy(context, cache);
503 krb5_free_context(context);
508 create_krb5_ccache(krb5_context kcontext,
510 kerb_auth_config *conf,
511 krb5_principal princ,
515 krb5_error_code problem;
517 krb5_ccache tmp_ccache = NULL;
519 #ifdef HAVE_KRB5_CC_GEN_NEW
520 problem = krb5_cc_gen_new(kcontext, &krb5_fcc_ops, &tmp_ccache);
522 /* only older MIT seem to not have the krb5_cc_gen_new() call, so we use
523 * MIT specific call here */
524 problem = krb5_fcc_generate_new(kcontext, &tmp_ccache);
525 /* krb5_fcc_generate_new() doesn't set KRB5_TC_OPENCLOSE, which makes
526 krb5_cc_initialize() fail */
527 krb5_fcc_set_flags(kcontext, tmp_ccache, KRB5_TC_OPENCLOSE);
530 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
531 "Cannot create file for new krb5 ccache: %s",
532 krb5_get_err_text(kcontext, problem));
533 ret = HTTP_INTERNAL_SERVER_ERROR;
537 ccname = ap_pstrdup(r->pool, krb5_cc_get_name(kcontext, tmp_ccache));
539 problem = krb5_cc_initialize(kcontext, tmp_ccache, princ);
541 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
542 "Cannot initialize krb5 ccache %s: krb5_cc_initialize() failed: %s",
543 ccname, krb5_get_err_text(kcontext, problem));
544 ret = HTTP_INTERNAL_SERVER_ERROR;
548 ap_table_setn(r->subprocess_env, "KRB5CCNAME", ccname);
549 ap_register_cleanup(r->pool, ccname,
550 krb5_cache_cleanup, ap_null_cleanup);
552 *ccache = tmp_ccache;
559 krb5_cc_destroy(kcontext, tmp_ccache);
565 store_krb5_creds(krb5_context kcontext,
567 kerb_auth_config *conf,
568 krb5_ccache delegated_cred)
571 krb5_error_code problem;
572 krb5_principal princ;
576 problem = krb5_cc_get_principal(kcontext, delegated_cred, &princ);
578 snprintf(errstr, sizeof(errstr), "krb5_cc_get_principal() failed: %s",
579 krb5_get_err_text(kcontext, problem));
580 return HTTP_INTERNAL_SERVER_ERROR;
583 ret = create_krb5_ccache(kcontext, r, conf, princ, &ccache);
585 krb5_free_principal(kcontext, princ);
590 problem = krb5_cc_copy_cache(kcontext, delegated_cred, ccache);
592 problem = krb5_cc_copy_creds(kcontext, delegated_cred, ccache);
594 krb5_free_principal(kcontext, princ);
596 snprintf(errstr, sizeof(errstr), "Failed to store credentials: %s",
597 krb5_get_err_text(kcontext, problem));
598 krb5_cc_destroy(kcontext, ccache);
599 return HTTP_INTERNAL_SERVER_ERROR;
602 krb5_cc_close(kcontext, ccache);
607 int authenticate_user_krb5pwd(request_rec *r,
608 kerb_auth_config *conf,
609 const char *auth_line)
611 const char *sent_pw = NULL;
612 const char *sent_name = NULL;
613 const char *realms = NULL;
614 krb5_context kcontext = NULL;
615 krb5_error_code code;
616 krb5_principal client = NULL;
617 krb5_ccache ccache = NULL;
618 krb5_keytab keytab = NULL;
622 code = krb5_init_context(&kcontext);
624 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
625 "Cannot initialize Kerberos5 context (%d)", code);
626 return HTTP_INTERNAL_SERVER_ERROR;
629 sent_pw = ap_pbase64decode(r->pool, auth_line);
630 sent_name = ap_getword (r->pool, &sent_pw, ':');
631 /* do not allow user to override realm setting of server */
632 if (strchr(sent_name, '@')) {
633 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
634 "specifying realm in user name is prohibited");
635 ret = HTTP_UNAUTHORIZED;
639 #ifdef HAVE_KRB5_CC_GEN_NEW
640 code = krb5_cc_gen_new(kcontext, &krb5_mcc_ops, &ccache);
642 /* only older MIT seem to not have the krb5_cc_gen_new() call, so we use
643 * MIT specific call here */
644 code = krb5_mcc_generate_new(kcontext, &ccache);
647 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
648 "Cannot generate new ccache: %s",
649 krb5_get_err_text(kcontext, code));
650 ret = HTTP_INTERNAL_SERVER_ERROR;
654 if (conf->krb_5_keytab)
655 krb5_kt_resolve(kcontext, conf->krb_5_keytab, &keytab);
657 realms = conf->krb_auth_realms;
659 if (realms && (code = krb5_set_default_realm(kcontext,
660 ap_getword_white(r->pool, &realms))))
664 krb5_free_principal(kcontext, client);
667 code = krb5_parse_name(kcontext, sent_name, &client);
671 code = verify_krb5_user(r, kcontext, client, ccache, sent_pw, "khttp",
672 keytab, conf->krb_verify_kdc);
676 /* ap_getword_white() used above shifts the parameter, so it's not
677 needed to touch the realms variable */
678 } while (realms && *realms);
680 memset((char *)sent_pw, 0, strlen(sent_pw));
683 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
684 "Verifying krb5 password failed: %s",
685 krb5_get_err_text(kcontext, code));
686 ret = HTTP_UNAUTHORIZED;
690 code = krb5_unparse_name(kcontext, client, &name);
692 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "krb5_unparse_name() failed: %s",
693 krb5_get_err_text(kcontext, code));
694 ret = HTTP_UNAUTHORIZED;
697 MK_USER = ap_pstrdup (r->pool, name);
698 MK_AUTH_TYPE = "Basic";
701 if (conf->krb_save_credentials)
702 store_krb5_creds(kcontext, r, conf, ccache);
708 krb5_free_principal(kcontext, client);
710 krb5_cc_destroy(kcontext, ccache);
712 krb5_kt_close(kcontext, keytab);
713 krb5_free_context(kcontext);
718 /*********************************************************************
719 * GSSAPI Authentication
720 ********************************************************************/
723 get_gss_error(MK_POOL *p, OM_uint32 error_status, char *prefix)
725 OM_uint32 maj_stat, min_stat;
726 OM_uint32 msg_ctx = 0;
727 gss_buffer_desc status_string;
731 snprintf(buf, sizeof(buf), "%s", prefix);
734 maj_stat = gss_display_status (&min_stat,
740 if (sizeof(buf) > len + status_string.length + 1) {
741 sprintf(buf+len, ": %s", (char*) status_string.value);
742 len += status_string.length;
744 gss_release_buffer(&min_stat, &status_string);
745 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
747 return (ap_pstrdup(p, buf));
751 cleanup_gss_connection(void *data)
753 OM_uint32 minor_status;
754 gss_connection_t *gss_conn = (gss_connection_t *)data;
758 if (gss_conn->context != GSS_C_NO_CONTEXT)
759 gss_delete_sec_context(&minor_status, &gss_conn->context,
761 if (gss_conn->server_creds != GSS_C_NO_CREDENTIAL)
762 gss_release_cred(&minor_status, &gss_conn->server_creds);
764 gss_connection = NULL;
770 store_gss_creds(request_rec *r, kerb_auth_config *conf, char *princ_name,
771 gss_cred_id_t delegated_cred)
773 OM_uint32 maj_stat, min_stat;
774 krb5_principal princ = NULL;
775 krb5_ccache ccache = NULL;
776 krb5_error_code problem;
777 krb5_context context;
778 int ret = HTTP_INTERNAL_SERVER_ERROR;
780 problem = krb5_init_context(&context);
782 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Cannot initialize krb5 context");
783 return HTTP_INTERNAL_SERVER_ERROR;
786 problem = krb5_parse_name(context, princ_name, &princ);
788 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
789 "Cannot parse delegated username (%s)", krb5_get_err_text(context, problem));
793 problem = create_krb5_ccache(context, r, conf, princ, &ccache);
795 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
796 "Cannot create krb5 ccache (%s)", krb5_get_err_text(context, problem));
800 maj_stat = gss_krb5_copy_ccache(&min_stat, delegated_cred, ccache);
801 if (GSS_ERROR(maj_stat)) {
802 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
803 "Cannot store delegated credential (%s)",
804 get_gss_error(r->pool, min_stat, "gss_krb5_copy_ccache"));
808 krb5_cc_close(context, ccache);
814 krb5_free_principal(context, princ);
816 krb5_cc_destroy(context, ccache);
817 krb5_free_context(context);
822 get_gss_creds(request_rec *r,
823 kerb_auth_config *conf,
824 gss_cred_id_t *server_creds)
826 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
827 OM_uint32 major_status, minor_status, minor_status2;
828 gss_name_t server_name = GSS_C_NO_NAME;
831 snprintf(buf, sizeof(buf), "%s/%s", "khttp", ap_get_server_name(r));
833 input_token.value = buf;
834 input_token.length = strlen(buf) + 1;
836 major_status = gss_import_name(&minor_status, &input_token,
839 if (GSS_ERROR(major_status)) {
840 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
841 "%s", get_gss_error(r->pool, minor_status,
842 "gss_import_name() failed"));
843 return HTTP_INTERNAL_SERVER_ERROR;
846 major_status = gss_acquire_cred(&minor_status, server_name, GSS_C_INDEFINITE,
847 GSS_C_NO_OID_SET, GSS_C_ACCEPT,
848 server_creds, NULL, NULL);
849 gss_release_name(&minor_status2, &server_name);
850 if (GSS_ERROR(major_status)) {
851 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
852 "%s", get_gss_error(r->pool, minor_status,
853 "gss_acquire_cred() failed"));
854 return HTTP_INTERNAL_SERVER_ERROR;
861 authenticate_user_gss(request_rec *r,
862 kerb_auth_config *conf,
863 const char *auth_line)
865 OM_uint32 major_status, minor_status, minor_status2;
866 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
867 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
868 const char *auth_param = NULL;
870 gss_name_t client_name = GSS_C_NO_NAME;
871 gss_cred_id_t delegated_cred = GSS_C_NO_CREDENTIAL;
873 if (gss_connection == NULL) {
874 gss_connection = ap_pcalloc(r->connection->pool, sizeof(*gss_connection));
875 if (gss_connection == NULL) {
876 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
877 "ap_pcalloc() failed (not enough memory)");
878 ret = HTTP_INTERNAL_SERVER_ERROR;
881 memset(gss_connection, 0, sizeof(*gss_connection));
882 ap_register_cleanup(r->connection->pool, gss_connection, cleanup_gss_connection, ap_null_cleanup);
885 if (conf->krb_5_keytab) {
887 /* we don't use the ap_* calls here, since the string passed to putenv()
888 * will become part of the enviroment and shouldn't be free()ed by apache
890 ktname = malloc(strlen("KRB5_KTNAME=") + strlen(conf->krb_5_keytab) + 1);
891 if (ktname == NULL) {
892 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "malloc() failed: not enough memory");
893 ret = HTTP_INTERNAL_SERVER_ERROR;
896 sprintf(ktname, "KRB5_KTNAME=%s", conf->krb_5_keytab);
900 if (gss_connection->server_creds == GSS_C_NO_CREDENTIAL) {
901 ret = get_gss_creds(r, conf, &gss_connection->server_creds);
906 /* ap_getword() shifts parameter */
907 auth_param = ap_getword_white(r->pool, &auth_line);
908 if (auth_param == NULL) {
909 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
910 "No Authorization parameter in request from client");
911 ret = HTTP_UNAUTHORIZED;
915 input_token.length = ap_base64decode_len(auth_param) + 1;
916 input_token.value = ap_pcalloc(r->connection->pool, input_token.length);
917 if (input_token.value == NULL) {
918 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
919 "ap_pcalloc() failed (not enough memory)");
920 ret = HTTP_INTERNAL_SERVER_ERROR;
923 input_token.length = ap_base64decode(input_token.value, auth_param);
926 major_status = gss_accept_sec_context(
928 major_status = gss_accept_sec_context_spnego(
931 &gss_connection->context,
932 gss_connection->server_creds,
934 GSS_C_NO_CHANNEL_BINDINGS,
941 if (output_token.length) {
945 len = ap_base64encode_len(output_token.length) + 1;
946 token = ap_pcalloc(r->connection->pool, len + 1);
948 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
949 "ap_pcalloc() failed (not enough memory)");
950 ret = HTTP_INTERNAL_SERVER_ERROR;
951 gss_release_buffer(&minor_status2, &output_token);
954 ap_base64encode(token, output_token.value, output_token.length);
956 ap_table_set(r->err_headers_out, "WWW-Authenticate",
957 ap_pstrcat(r->pool, "Negotiate ", token, NULL));
958 gss_release_buffer(&minor_status2, &output_token);
961 if (GSS_ERROR(major_status)) {
962 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
963 "%s", get_gss_error(r->pool, minor_status,
964 "gss_accept_sec_context() failed"));
965 ret = HTTP_UNAUTHORIZED;
969 if (major_status & GSS_S_CONTINUE_NEEDED) {
970 /* Some GSSAPI mechanism (eg GSI from Globus) may require multiple
971 * iterations to establish authentication */
972 ret = HTTP_UNAUTHORIZED;
976 major_status = gss_display_name(&minor_status, client_name, &output_token, NULL);
977 gss_release_name(&minor_status, &client_name);
978 if (GSS_ERROR(major_status)) {
979 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
980 "%s", get_gss_error(r->pool, minor_status,
981 "gss_export_name() failed"));
982 ret = HTTP_INTERNAL_SERVER_ERROR;
986 MK_AUTH_TYPE = "Negotiate";
987 MK_USER = ap_pstrdup(r->pool, output_token.value);
989 if (conf->krb_save_credentials && delegated_cred != GSS_C_NO_CREDENTIAL)
990 store_gss_creds(r, conf, (char *)output_token.value, delegated_cred);
992 gss_release_buffer(&minor_status, &output_token);
998 gss_release_cred(&minor_status, &delegated_cred);
1000 if (output_token.length)
1001 gss_release_buffer(&minor_status, &output_token);
1003 if (client_name != GSS_C_NO_NAME)
1004 gss_release_name(&minor_status, &client_name);
1006 cleanup_gss_connection(gss_connection);
1013 already_succeeded(request_rec *r)
1015 if (ap_is_initial_req(r) || MK_AUTH_TYPE == NULL)
1017 if (strcmp(MK_AUTH_TYPE, "Negotiate") ||
1018 (strcmp(MK_AUTH_TYPE, "Basic") && strchr(MK_USER, '@')))
1024 note_kerb_auth_failure(request_rec *r, const kerb_auth_config *conf,
1025 int use_krb4, int use_krb5)
1027 const char *auth_name = NULL;
1030 /* get the user realm specified in .htaccess */
1031 auth_name = ap_auth_name(r);
1033 /* XXX should the WWW-Authenticate header be cleared first? */
1035 if (use_krb5 && conf->krb_method_gssapi)
1036 ap_table_add(r->err_headers_out, "WWW-Authenticate", "Negotiate");
1037 if (use_krb5 && conf->krb_method_k5pass) {
1038 ap_table_add(r->err_headers_out, "WWW-Authenticate",
1039 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1045 if (use_krb4 && conf->krb_method_k4pass && !set_basic)
1046 ap_table_add(r->err_headers_out, "WWW-Authenticate",
1047 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1051 int kerb_authenticate_user(request_rec *r)
1053 kerb_auth_config *conf =
1054 (kerb_auth_config *) ap_get_module_config(r->per_dir_config,
1056 const char *auth_type = NULL;
1057 const char *auth_line = NULL;
1058 const char *type = NULL;
1059 int use_krb5 = 0, use_krb4 = 0;
1061 static int last_return = HTTP_UNAUTHORIZED;
1063 /* get the type specified in .htaccess */
1064 type = ap_auth_type(r);
1066 if (type && strcasecmp(type, "Kerberos") == 0)
1067 use_krb5 = use_krb4 = 1;
1068 else if(type && strcasecmp(type, "KerberosV5") == 0)
1070 else if(type && strcasecmp(type, "KerberosV4") == 0)
1075 /* get what the user sent us in the HTTP header */
1076 auth_line = MK_TABLE_GET(r->headers_in, "Authorization");
1078 note_kerb_auth_failure(r, conf, use_krb4, use_krb5);
1079 return HTTP_UNAUTHORIZED;
1081 auth_type = ap_getword_white(r->pool, &auth_line);
1083 if (already_succeeded(r))
1086 ret = HTTP_UNAUTHORIZED;
1089 if (use_krb5 && conf->krb_method_gssapi &&
1090 strcasecmp(auth_type, "Negotiate") == 0) {
1091 ret = authenticate_user_gss(r, conf, auth_line);
1092 } else if (use_krb5 && conf->krb_method_k5pass &&
1093 strcasecmp(auth_type, "Basic") == 0) {
1094 ret = authenticate_user_krb5pwd(r, conf, auth_line);
1099 if (ret == HTTP_UNAUTHORIZED && use_krb4 && conf->krb_method_k4pass &&
1100 strcasecmp(auth_type, "Basic") == 0)
1101 ret = authenticate_user_krb4pwd(r, conf, auth_line);
1104 if (ret == HTTP_UNAUTHORIZED)
1105 note_kerb_auth_failure(r, conf, use_krb4, use_krb5);
1112 /***************************************************************************
1113 Module Setup/Configuration
1114 ***************************************************************************/
1116 module MODULE_VAR_EXPORT auth_kerb_module = {
1117 STANDARD_MODULE_STUFF,
1118 NULL, /* module initializer */
1119 kerb_dir_create_config, /* per-directory config creator */
1120 NULL, /* per-directory config merger */
1121 NULL, /* per-server config creator */
1122 NULL, /* per-server config merger */
1123 kerb_auth_cmds, /* command table */
1124 NULL, /* [ 9] content handlers */
1125 NULL, /* [ 2] URI-to-filename translation */
1126 kerb_authenticate_user, /* [ 5] check/validate user_id */
1127 NULL, /* [ 6] check user_id is valid *here* */
1128 NULL, /* [ 4] check access by host address */
1129 NULL, /* [ 7] MIME type checker/setter */
1130 NULL, /* [ 8] fixups */
1131 NULL, /* [10] logger */
1132 NULL, /* [ 3] header parser */
1133 NULL, /* process initialization */
1134 NULL, /* process exit/cleanup */
1135 NULL /* [ 1] post read_request handling */
1139 kerb_init_handler(apr_pool_t *p, apr_pool_t *plog,
1140 apr_pool_t *ptemp, server_rec *s)
1142 ap_add_version_component(p, "mod_auth_kerb/" MODAUTHKERB_VERSION);
1146 void kerb_register_hooks(apr_pool_t *p)
1148 ap_hook_post_config(kerb_init_handler, NULL, NULL, APR_HOOK_MIDDLE);
1149 ap_hook_check_user_id(kerb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
1152 module AP_MODULE_DECLARE_DATA auth_kerb_module =
1154 STANDARD20_MODULE_STUFF,
1155 kerb_dir_create_config, /* create per-dir conf structures */
1156 NULL, /* merge per-dir conf structures */
1157 NULL, /* create per-server conf structures */
1158 NULL, /* merge per-server conf structures */
1159 kerb_auth_cmds, /* table of configuration directives */
1160 kerb_register_hooks /* register hooks */