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)
99 #include "spnegokrb5.h"
103 /*Prevent warning about closesocket redefinition (Apache's ap_config.h and
104 * MIT Kerberos' port-sockets.h both define it as close) */
109 #include <netdb.h> /* gethostbyname() */
113 module auth_kerb_module;
115 module AP_MODULE_DECLARE_DATA auth_kerb_module;
118 /***************************************************************************
119 Macros To Ease Compatibility
120 ***************************************************************************/
123 #define MK_TABLE_GET ap_table_get
124 #define MK_USER r->connection->user
125 #define MK_AUTH_TYPE r->connection->ap_auth_type
127 #define MK_POOL apr_pool_t
128 #define MK_TABLE_GET apr_table_get
129 #define MK_USER r->user
130 #define MK_AUTH_TYPE r->ap_auth_type
134 /***************************************************************************
135 Auth Configuration Structure
136 ***************************************************************************/
138 char *krb_auth_realms;
139 int krb_save_credentials;
141 char *krb_service_name;
142 int krb_authoritative;
145 int krb_method_gssapi;
146 int krb_method_k5pass;
150 int krb_method_k4pass;
155 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg);
158 #define command(name, func, var, type, usage) \
160 (void*)XtOffsetOf(kerb_auth_config, var), \
161 OR_AUTHCFG, type, usage }
163 #define command(name, func, var, type, usage) \
164 AP_INIT_ ## type (name, func, \
165 (void*)APR_XtOffsetOf(kerb_auth_config, var), \
169 static const command_rec kerb_auth_cmds[] = {
170 command("KrbAuthRealms", krb5_save_realms, krb_auth_realms,
171 RAW_ARGS, "Realms to attempt authentication against (can be multiple)."),
173 command("KrbAuthRealm", krb5_save_realms, krb_auth_realms,
174 RAW_ARGS, "Alias for KrbAuthRealms."),
176 command("KrbSaveCredentials", ap_set_flag_slot, krb_save_credentials,
177 FLAG, "Save and store credentials/tickets retrieved during auth."),
179 command("KrbVerifyKDC", ap_set_flag_slot, krb_verify_kdc,
180 FLAG, "Verify tickets against keytab to prevent KDC spoofing attacks."),
182 command("KrbServiceName", ap_set_file_slot, krb_service_name,
183 TAKE1, "Service name to be used by Apache for authentication."),
185 command("KrbAuthoritative", ap_set_flag_slot, krb_authoritative,
186 FLAG, "Set to 'off' to allow access control to be passed along to lower modules if the UserID is not known to this module."),
189 command("Krb5Keytab", ap_set_file_slot, krb_5_keytab,
190 TAKE1, "Location of Kerberos V5 keytab file."),
192 command("KrbMethodNegotiate", ap_set_flag_slot, krb_method_gssapi,
193 FLAG, "Enable Negotiate authentication method."),
195 command("KrbMethodK5Pass", ap_set_flag_slot, krb_method_k5pass,
196 FLAG, "Enable Kerberos V5 password authentication."),
200 command("Krb4Srvtab", ap_set_file_slot, krb_4_srvtab,
201 TAKE1, "Location of Kerberos V4 srvtab file."),
203 command("KrbMethodK4Pass", ap_set_flag_slot, krb_method_k4pass,
204 FLAG, "Enable Kerberos V4 password authentication."),
212 gss_ctx_id_t context;
213 gss_cred_id_t server_creds;
216 static gss_connection_t *gss_connection = NULL;
220 /***************************************************************************
221 Auth Configuration Initialization
222 ***************************************************************************/
223 static void *kerb_dir_create_config(MK_POOL *p, char *d)
225 kerb_auth_config *rec;
227 rec = (kerb_auth_config *) ap_pcalloc(p, sizeof(kerb_auth_config));
228 ((kerb_auth_config *)rec)->krb_verify_kdc = 1;
229 ((kerb_auth_config *)rec)->krb_service_name = "khttp";
230 ((kerb_auth_config *)rec)->krb_authoritative = 1;
232 ((kerb_auth_config *)rec)->krb_method_k5pass = 1;
233 ((kerb_auth_config *)rec)->krb_method_gssapi = 1;
236 ((kerb_auth_config *)rec)->krb_method_k4pass = 1;
242 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg)
244 sec->krb_auth_realms= ap_pstrdup(cmd->pool, arg);
248 void log_rerror(const char *file, int line, int level, int status,
249 const request_rec *r, const char *fmt, ...)
256 vsnprintf(errstr, sizeof(errstr), fmt, ap);
261 snprintf(errnostr, sizeof(errnostr), "%s: (%s)", errstr, strerror(errno));
263 snprintf(errnostr, sizeof(errnostr), "%s", errstr);
266 ap_log_rerror(file, line, level | APLOG_NOERRNO, r, "%s", errnostr);
268 ap_log_rerror(file, line, level | APLOG_NOERRNO, status, r, "%s", errnostr);
273 /***************************************************************************
274 Username/Password Validation for Krb4
275 ***************************************************************************/
277 verify_krb4_user(request_rec *r, char *name, char *instance, char *realm,
278 char *password, char *linstance, char *srvtab, int krb_verify_kdc)
284 const char *hostname;
287 char lrealm[REALM_SZ];
289 ret = krb_get_pw_in_tkt(name, instance, realm, "krbtgt", realm,
290 DEFAULT_TKT_LIFE, password);
292 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
293 "Cannot get krb4 ticket: krb_get_pw_in_tkt() failed: %s",
294 krb_get_err_text(ret));
301 hostname = ap_get_server_name(r);
303 hp = gethostbyname(hostname);
306 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
307 "Cannot verify krb4 ticket: gethostbyname() failed: %s",
311 memcpy(&addr, hp->h_addr, sizeof(addr));
313 phost = krb_get_phost((char *)hostname);
315 krb_get_lrealm(lrealm, 1);
317 ret = krb_mk_req(&ticket, linstance, phost, lrealm, 0);
319 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
320 "Cannot verify krb4 ticket: krb_mk_req() failed: %s",
321 krb_get_err_text(ret));
326 ret = krb_rd_req(&ticket, linstance, phost, addr, &authdata, srvtab);
328 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
329 "Cannot verify krb4 ticket: krb_rd_req() failed: %s",
330 krb_get_err_text(ret));
338 krb4_cache_cleanup(void *data)
340 char *tkt_file = (char *) data;
342 krb_set_tkt_string(tkt_file);
348 authenticate_user_krb4pwd(request_rec *r,
349 kerb_auth_config *conf,
350 const char *auth_line)
354 const char *sent_name;
357 char *tkt_file_p = NULL;
362 char lrealm[REALM_SZ];
363 int all_principals_unkown;
365 sent_pw = ap_pbase64decode(r->pool, auth_line);
366 sent_name = ap_getword (r->pool, &sent_pw, ':');
368 /* do not allow user to override realm setting of server */
369 if (strchr(sent_name, '@')) {
370 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
371 "specifying realm in user name is prohibited");
372 return HTTP_UNAUTHORIZED;
375 sent_instance = strchr(sent_name, '.');
377 *sent_instance++ = '\0';
379 snprintf(tkt_file, sizeof(tkt_file), "/tmp/apache_tkt_XXXXXX");
380 fd = mkstemp(tkt_file);
382 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
383 "Cannot create krb4 ccache: mkstemp() failed: %s",
385 return HTTP_INTERNAL_SERVER_ERROR;
388 tkt_file_p = ap_pstrdup(r->pool, tkt_file);
389 ap_register_cleanup(r->pool, tkt_file_p,
390 krb4_cache_cleanup, ap_null_cleanup);
392 krb_set_tkt_string(tkt_file);
394 all_principals_unkown = 1;
395 realms = conf->krb_auth_realms;
397 memset(lrealm, 0, sizeof(lrealm));
400 realm = ap_getword_white(r->pool, &realms);
403 ret = krb_get_lrealm(lrealm, 1);
406 if (realm == NULL || *realm == '\0')
409 ret = verify_krb4_user(r, (char *)sent_name,
410 (sent_instance) ? sent_instance : "",
411 (char *)realm, (char *)sent_pw,
412 conf->krb_service_name,
413 conf->krb_4_srvtab, conf->krb_verify_kdc);
414 if (!conf->krb_authoritative && ret) {
415 /* if we're not authoritative, we allow authentication to pass on
416 * to another modules if (and only if) the user is not known to us */
417 if (all_principals_unkown && ret != KDC_PR_UNKNOWN)
418 all_principals_unkown = 0;
423 } while (realms && *realms);
426 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Verifying krb4 password failed");
427 ret = (all_principals_unkown = 1 && ret == KDC_PR_UNKNOWN) ?
428 DECLINED : HTTP_UNAUTHORIZED;
432 user = ap_pstrdup(r->pool, sent_name);
434 user = ap_pstrcat(r->pool, user, ".", sent_instance, NULL);
435 user = ap_pstrcat(r->pool, user, "@", realm, NULL);
438 MK_AUTH_TYPE = "Basic";
439 ap_table_setn(r->subprocess_env, "KRBTKFILE", tkt_file_p);
441 if (!conf->krb_save_credentials)
442 krb4_cache_cleanup(tkt_file);
446 krb4_cache_cleanup(tkt_file);
455 /***************************************************************************
456 Username/Password Validation for Krb5
457 ***************************************************************************/
458 /* Inspired by krb5_verify_user from Heimdal */
459 static krb5_error_code
460 verify_krb5_user(request_rec *r, krb5_context context, krb5_principal principal,
461 krb5_ccache ccache, const char *password, const char *service,
462 krb5_keytab keytab, int krb_verify_kdc)
465 krb5_principal server = NULL;
467 krb5_verify_init_creds_opt opt;
469 memset(&creds, 0, sizeof(creds));
471 ret = krb5_get_init_creds_password(context, &creds, principal,
472 (char *)password, krb5_prompter_posix,
473 NULL, 0, NULL, NULL);
477 ret = krb5_sname_to_principal(context, ap_get_server_name(r), service,
478 KRB5_NT_SRV_HST, &server);
482 krb5_verify_init_creds_opt_init(&opt);
483 krb5_verify_init_creds_opt_set_ap_req_nofail(&opt, krb_verify_kdc);
485 ret = krb5_verify_init_creds(context, &creds, server, keytab, NULL, &opt);
490 ret = krb5_cc_initialize(context, ccache, principal);
492 ret = krb5_cc_store_cred(context, ccache, &creds);
496 krb5_free_cred_contents(context, &creds);
498 krb5_free_principal(context, server);
503 krb5_cache_cleanup(void *data)
505 krb5_context context;
507 krb5_error_code problem;
508 char *cache_name = (char *) data;
510 problem = krb5_init_context(&context);
512 /* ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "krb5_init_context() failed"); */
513 return HTTP_INTERNAL_SERVER_ERROR;
516 problem = krb5_cc_resolve(context, cache_name, &cache);
518 /* log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
519 "krb5_cc_resolve() failed (%s: %s)",
520 cache_name, krb5_get_err_text(context, problem)); */
521 return HTTP_INTERNAL_SERVER_ERROR;
524 krb5_cc_destroy(context, cache);
525 krb5_free_context(context);
530 create_krb5_ccache(krb5_context kcontext,
532 kerb_auth_config *conf,
533 krb5_principal princ,
537 krb5_error_code problem;
539 krb5_ccache tmp_ccache = NULL;
541 #ifdef HAVE_KRB5_CC_GEN_NEW
542 problem = krb5_cc_gen_new(kcontext, &krb5_fcc_ops, &tmp_ccache);
544 /* only older MIT seem to not have the krb5_cc_gen_new() call, so we use
545 * MIT specific call here */
546 problem = krb5_fcc_generate_new(kcontext, &tmp_ccache);
547 /* krb5_fcc_generate_new() doesn't set KRB5_TC_OPENCLOSE, which makes
548 krb5_cc_initialize() fail */
549 krb5_fcc_set_flags(kcontext, tmp_ccache, KRB5_TC_OPENCLOSE);
552 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
553 "Cannot create file for new krb5 ccache: %s",
554 krb5_get_err_text(kcontext, problem));
555 ret = HTTP_INTERNAL_SERVER_ERROR;
559 ccname = ap_pstrdup(r->pool, krb5_cc_get_name(kcontext, tmp_ccache));
561 problem = krb5_cc_initialize(kcontext, tmp_ccache, princ);
563 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
564 "Cannot initialize krb5 ccache %s: krb5_cc_initialize() failed: %s",
565 ccname, krb5_get_err_text(kcontext, problem));
566 ret = HTTP_INTERNAL_SERVER_ERROR;
570 ap_table_setn(r->subprocess_env, "KRB5CCNAME", ccname);
571 ap_register_cleanup(r->pool, ccname,
572 krb5_cache_cleanup, ap_null_cleanup);
574 *ccache = tmp_ccache;
581 krb5_cc_destroy(kcontext, tmp_ccache);
587 store_krb5_creds(krb5_context kcontext,
589 kerb_auth_config *conf,
590 krb5_ccache delegated_cred)
593 krb5_error_code problem;
594 krb5_principal princ;
598 problem = krb5_cc_get_principal(kcontext, delegated_cred, &princ);
600 snprintf(errstr, sizeof(errstr), "krb5_cc_get_principal() failed: %s",
601 krb5_get_err_text(kcontext, problem));
602 return HTTP_INTERNAL_SERVER_ERROR;
605 ret = create_krb5_ccache(kcontext, r, conf, princ, &ccache);
607 krb5_free_principal(kcontext, princ);
612 problem = krb5_cc_copy_cache(kcontext, delegated_cred, ccache);
614 problem = krb5_cc_copy_creds(kcontext, delegated_cred, ccache);
616 krb5_free_principal(kcontext, princ);
618 snprintf(errstr, sizeof(errstr), "Failed to store credentials: %s",
619 krb5_get_err_text(kcontext, problem));
620 krb5_cc_destroy(kcontext, ccache);
621 return HTTP_INTERNAL_SERVER_ERROR;
624 krb5_cc_close(kcontext, ccache);
629 int authenticate_user_krb5pwd(request_rec *r,
630 kerb_auth_config *conf,
631 const char *auth_line)
633 const char *sent_pw = NULL;
634 const char *sent_name = NULL;
635 const char *realms = NULL;
636 krb5_context kcontext = NULL;
637 krb5_error_code code;
638 krb5_principal client = NULL;
639 krb5_ccache ccache = NULL;
640 krb5_keytab keytab = NULL;
643 int all_principals_unkown;
645 code = krb5_init_context(&kcontext);
647 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
648 "Cannot initialize Kerberos5 context (%d)", code);
649 return HTTP_INTERNAL_SERVER_ERROR;
652 sent_pw = ap_pbase64decode(r->pool, auth_line);
653 sent_name = ap_getword (r->pool, &sent_pw, ':');
654 /* do not allow user to override realm setting of server */
655 if (strchr(sent_name, '@')) {
656 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
657 "specifying realm in user name is prohibited");
658 ret = HTTP_UNAUTHORIZED;
662 #ifdef HAVE_KRB5_CC_GEN_NEW
663 code = krb5_cc_gen_new(kcontext, &krb5_mcc_ops, &ccache);
665 /* only older MIT seem to not have the krb5_cc_gen_new() call, so we use
666 * MIT specific call here */
667 code = krb5_mcc_generate_new(kcontext, &ccache);
670 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
671 "Cannot generate new ccache: %s",
672 krb5_get_err_text(kcontext, code));
673 ret = HTTP_INTERNAL_SERVER_ERROR;
677 if (conf->krb_5_keytab)
678 krb5_kt_resolve(kcontext, conf->krb_5_keytab, &keytab);
680 all_principals_unkown = 1;
681 realms = conf->krb_auth_realms;
683 if (realms && (code = krb5_set_default_realm(kcontext,
684 ap_getword_white(r->pool, &realms))))
688 krb5_free_principal(kcontext, client);
691 code = krb5_parse_name(kcontext, sent_name, &client);
695 code = verify_krb5_user(r, kcontext, client, ccache, sent_pw,
696 conf->krb_service_name,
697 keytab, conf->krb_verify_kdc);
698 if (!conf->krb_authoritative && code) {
699 /* if we're not authoritative, we allow authentication to pass on
700 * to another modules if (and only if) the user is not known to us */
701 if (all_principals_unkown && code != KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
702 all_principals_unkown = 0;
708 /* ap_getword_white() used above shifts the parameter, so it's not
709 needed to touch the realms variable */
710 } while (realms && *realms);
712 memset((char *)sent_pw, 0, strlen(sent_pw));
715 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
716 "Verifying krb5 password failed: %s",
717 krb5_get_err_text(kcontext, code));
718 if (all_principals_unkown = 1 && code == KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
721 ret = HTTP_UNAUTHORIZED;
726 code = krb5_unparse_name(kcontext, client, &name);
728 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "krb5_unparse_name() failed: %s",
729 krb5_get_err_text(kcontext, code));
730 ret = HTTP_UNAUTHORIZED;
733 MK_USER = ap_pstrdup (r->pool, name);
734 MK_AUTH_TYPE = "Basic";
737 if (conf->krb_save_credentials)
738 store_krb5_creds(kcontext, r, conf, ccache);
744 krb5_free_principal(kcontext, client);
746 krb5_cc_destroy(kcontext, ccache);
748 krb5_kt_close(kcontext, keytab);
749 krb5_free_context(kcontext);
754 /*********************************************************************
755 * GSSAPI Authentication
756 ********************************************************************/
759 get_gss_error(MK_POOL *p, OM_uint32 error_status, char *prefix)
761 OM_uint32 maj_stat, min_stat;
762 OM_uint32 msg_ctx = 0;
763 gss_buffer_desc status_string;
767 snprintf(buf, sizeof(buf), "%s", prefix);
770 maj_stat = gss_display_status (&min_stat,
776 if (sizeof(buf) > len + status_string.length + 1) {
777 sprintf(buf+len, ": %s", (char*) status_string.value);
778 len += status_string.length;
780 gss_release_buffer(&min_stat, &status_string);
781 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
783 return (ap_pstrdup(p, buf));
787 cleanup_gss_connection(void *data)
789 OM_uint32 minor_status;
790 gss_connection_t *gss_conn = (gss_connection_t *)data;
794 if (gss_conn->context != GSS_C_NO_CONTEXT)
795 gss_delete_sec_context(&minor_status, &gss_conn->context,
797 if (gss_conn->server_creds != GSS_C_NO_CREDENTIAL)
798 gss_release_cred(&minor_status, &gss_conn->server_creds);
800 gss_connection = NULL;
806 store_gss_creds(request_rec *r, kerb_auth_config *conf, char *princ_name,
807 gss_cred_id_t delegated_cred)
809 OM_uint32 maj_stat, min_stat;
810 krb5_principal princ = NULL;
811 krb5_ccache ccache = NULL;
812 krb5_error_code problem;
813 krb5_context context;
814 int ret = HTTP_INTERNAL_SERVER_ERROR;
816 problem = krb5_init_context(&context);
818 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Cannot initialize krb5 context");
819 return HTTP_INTERNAL_SERVER_ERROR;
822 problem = krb5_parse_name(context, princ_name, &princ);
824 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
825 "Cannot parse delegated username (%s)", krb5_get_err_text(context, problem));
829 problem = create_krb5_ccache(context, r, conf, princ, &ccache);
831 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
832 "Cannot create krb5 ccache (%s)", krb5_get_err_text(context, problem));
836 maj_stat = gss_krb5_copy_ccache(&min_stat, delegated_cred, ccache);
837 if (GSS_ERROR(maj_stat)) {
838 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
839 "Cannot store delegated credential (%s)",
840 get_gss_error(r->pool, min_stat, "gss_krb5_copy_ccache"));
844 krb5_cc_close(context, ccache);
850 krb5_free_principal(context, princ);
852 krb5_cc_destroy(context, ccache);
853 krb5_free_context(context);
858 get_gss_creds(request_rec *r,
859 kerb_auth_config *conf,
860 gss_cred_id_t *server_creds)
862 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
863 OM_uint32 major_status, minor_status, minor_status2;
864 gss_name_t server_name = GSS_C_NO_NAME;
867 snprintf(buf, sizeof(buf), "%s/%s", conf->krb_service_name, ap_get_server_name(r));
869 input_token.value = buf;
870 input_token.length = strlen(buf) + 1;
872 major_status = gss_import_name(&minor_status, &input_token,
875 if (GSS_ERROR(major_status)) {
876 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
877 "%s", get_gss_error(r->pool, minor_status,
878 "gss_import_name() failed"));
879 return HTTP_INTERNAL_SERVER_ERROR;
882 major_status = gss_acquire_cred(&minor_status, server_name, GSS_C_INDEFINITE,
883 GSS_C_NO_OID_SET, GSS_C_ACCEPT,
884 server_creds, NULL, NULL);
885 gss_release_name(&minor_status2, &server_name);
886 if (GSS_ERROR(major_status)) {
887 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
888 "%s", get_gss_error(r->pool, minor_status,
889 "gss_acquire_cred() failed"));
890 return HTTP_INTERNAL_SERVER_ERROR;
897 cmp_gss_type(gss_buffer_t token, gss_OID oid)
902 if (token->length == 0)
903 return GSS_S_DEFECTIVE_TOKEN;
907 return GSS_S_DEFECTIVE_TOKEN;
910 if ((len & 0x7f) > 4)
911 return GSS_S_DEFECTIVE_TOKEN;
915 return GSS_S_DEFECTIVE_TOKEN;
917 if (((OM_uint32) *p++) != oid->length)
918 return GSS_S_DEFECTIVE_TOKEN;
920 return memcmp(p, oid->elements, oid->length);
924 authenticate_user_gss(request_rec *r,
925 kerb_auth_config *conf,
926 const char *auth_line)
928 OM_uint32 major_status, minor_status, minor_status2;
929 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
930 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
931 const char *auth_param = NULL;
933 gss_name_t client_name = GSS_C_NO_NAME;
934 gss_cred_id_t delegated_cred = GSS_C_NO_CREDENTIAL;
935 OM_uint32 (*accept_sec_token)();
936 gss_OID_desc spnego_oid;
938 spnego_oid.length = 6;
939 spnego_oid.elements = (void *)"\x2b\x06\x01\x05\x05\x02";
941 if (gss_connection == NULL) {
942 gss_connection = ap_pcalloc(r->connection->pool, sizeof(*gss_connection));
943 if (gss_connection == NULL) {
944 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
945 "ap_pcalloc() failed (not enough memory)");
946 ret = HTTP_INTERNAL_SERVER_ERROR;
949 memset(gss_connection, 0, sizeof(*gss_connection));
950 ap_register_cleanup(r->connection->pool, gss_connection, cleanup_gss_connection, ap_null_cleanup);
953 if (conf->krb_5_keytab) {
955 /* we don't use the ap_* calls here, since the string passed to putenv()
956 * will become part of the enviroment and shouldn't be free()ed by apache
958 ktname = malloc(strlen("KRB5_KTNAME=") + strlen(conf->krb_5_keytab) + 1);
959 if (ktname == NULL) {
960 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "malloc() failed: not enough memory");
961 ret = HTTP_INTERNAL_SERVER_ERROR;
964 sprintf(ktname, "KRB5_KTNAME=%s", conf->krb_5_keytab);
968 if (gss_connection->server_creds == GSS_C_NO_CREDENTIAL) {
969 ret = get_gss_creds(r, conf, &gss_connection->server_creds);
974 /* ap_getword() shifts parameter */
975 auth_param = ap_getword_white(r->pool, &auth_line);
976 if (auth_param == NULL) {
977 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
978 "No Authorization parameter in request from client");
979 ret = HTTP_UNAUTHORIZED;
983 input_token.length = ap_base64decode_len(auth_param) + 1;
984 input_token.value = ap_pcalloc(r->connection->pool, input_token.length);
985 if (input_token.value == NULL) {
986 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
987 "ap_pcalloc() failed (not enough memory)");
988 ret = HTTP_INTERNAL_SERVER_ERROR;
991 input_token.length = ap_base64decode(input_token.value, auth_param);
993 accept_sec_token = (cmp_gss_type(&input_token, &spnego_oid) == 0) ?
994 gss_accept_sec_context_spnego : gss_accept_sec_context;
996 major_status = accept_sec_token(&minor_status,
997 &gss_connection->context,
998 gss_connection->server_creds,
1000 GSS_C_NO_CHANNEL_BINDINGS,
1007 if (output_token.length) {
1011 len = ap_base64encode_len(output_token.length) + 1;
1012 token = ap_pcalloc(r->connection->pool, len + 1);
1013 if (token == NULL) {
1014 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1015 "ap_pcalloc() failed (not enough memory)");
1016 ret = HTTP_INTERNAL_SERVER_ERROR;
1017 gss_release_buffer(&minor_status2, &output_token);
1020 ap_base64encode(token, output_token.value, output_token.length);
1022 ap_table_set(r->err_headers_out, "WWW-Authenticate",
1023 ap_pstrcat(r->pool, "Negotiate ", token, NULL));
1024 gss_release_buffer(&minor_status2, &output_token);
1027 if (GSS_ERROR(major_status)) {
1028 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1029 "%s", get_gss_error(r->pool, minor_status,
1030 "gss_accept_sec_context() failed"));
1031 ret = HTTP_UNAUTHORIZED;
1035 if (major_status & GSS_S_CONTINUE_NEEDED) {
1036 /* Some GSSAPI mechanism (eg GSI from Globus) may require multiple
1037 * iterations to establish authentication */
1038 ret = HTTP_UNAUTHORIZED;
1042 major_status = gss_display_name(&minor_status, client_name, &output_token, NULL);
1043 gss_release_name(&minor_status, &client_name);
1044 if (GSS_ERROR(major_status)) {
1045 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1046 "%s", get_gss_error(r->pool, minor_status,
1047 "gss_export_name() failed"));
1048 ret = HTTP_INTERNAL_SERVER_ERROR;
1052 MK_AUTH_TYPE = "Negotiate";
1053 MK_USER = ap_pstrdup(r->pool, output_token.value);
1055 if (conf->krb_save_credentials && delegated_cred != GSS_C_NO_CREDENTIAL)
1056 store_gss_creds(r, conf, (char *)output_token.value, delegated_cred);
1058 gss_release_buffer(&minor_status, &output_token);
1064 gss_release_cred(&minor_status, &delegated_cred);
1066 if (output_token.length)
1067 gss_release_buffer(&minor_status, &output_token);
1069 if (client_name != GSS_C_NO_NAME)
1070 gss_release_name(&minor_status, &client_name);
1072 cleanup_gss_connection(gss_connection);
1079 already_succeeded(request_rec *r)
1081 if (ap_is_initial_req(r) || MK_AUTH_TYPE == NULL)
1083 if (strcmp(MK_AUTH_TYPE, "Negotiate") ||
1084 (strcmp(MK_AUTH_TYPE, "Basic") && strchr(MK_USER, '@')))
1090 note_kerb_auth_failure(request_rec *r, const kerb_auth_config *conf,
1091 int use_krb4, int use_krb5)
1093 const char *auth_name = NULL;
1096 /* get the user realm specified in .htaccess */
1097 auth_name = ap_auth_name(r);
1099 /* XXX should the WWW-Authenticate header be cleared first? */
1101 if (use_krb5 && conf->krb_method_gssapi)
1102 ap_table_add(r->err_headers_out, "WWW-Authenticate", "Negotiate");
1103 if (use_krb5 && conf->krb_method_k5pass) {
1104 ap_table_add(r->err_headers_out, "WWW-Authenticate",
1105 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1111 if (use_krb4 && conf->krb_method_k4pass && !set_basic)
1112 ap_table_add(r->err_headers_out, "WWW-Authenticate",
1113 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1117 int kerb_authenticate_user(request_rec *r)
1119 kerb_auth_config *conf =
1120 (kerb_auth_config *) ap_get_module_config(r->per_dir_config,
1122 const char *auth_type = NULL;
1123 const char *auth_line = NULL;
1124 const char *type = NULL;
1125 int use_krb5 = 0, use_krb4 = 0;
1127 static int last_return = HTTP_UNAUTHORIZED;
1129 /* get the type specified in .htaccess */
1130 type = ap_auth_type(r);
1132 if (type && strcasecmp(type, "Kerberos") == 0)
1133 use_krb5 = use_krb4 = 1;
1134 else if(type && strcasecmp(type, "KerberosV5") == 0)
1136 else if(type && strcasecmp(type, "KerberosV4") == 0)
1141 /* get what the user sent us in the HTTP header */
1142 auth_line = MK_TABLE_GET(r->headers_in, "Authorization");
1144 note_kerb_auth_failure(r, conf, use_krb4, use_krb5);
1145 return HTTP_UNAUTHORIZED;
1147 auth_type = ap_getword_white(r->pool, &auth_line);
1149 if (already_succeeded(r))
1152 ret = HTTP_UNAUTHORIZED;
1155 if (use_krb5 && conf->krb_method_gssapi &&
1156 strcasecmp(auth_type, "Negotiate") == 0) {
1157 ret = authenticate_user_gss(r, conf, auth_line);
1158 } else if (use_krb5 && conf->krb_method_k5pass &&
1159 strcasecmp(auth_type, "Basic") == 0) {
1160 ret = authenticate_user_krb5pwd(r, conf, auth_line);
1165 if (ret == HTTP_UNAUTHORIZED && use_krb4 && conf->krb_method_k4pass &&
1166 strcasecmp(auth_type, "Basic") == 0)
1167 ret = authenticate_user_krb4pwd(r, conf, auth_line);
1170 if (ret == HTTP_UNAUTHORIZED)
1171 note_kerb_auth_failure(r, conf, use_krb4, use_krb5);
1178 /***************************************************************************
1179 Module Setup/Configuration
1180 ***************************************************************************/
1182 module MODULE_VAR_EXPORT auth_kerb_module = {
1183 STANDARD_MODULE_STUFF,
1184 NULL, /* module initializer */
1185 kerb_dir_create_config, /* per-directory config creator */
1186 NULL, /* per-directory config merger */
1187 NULL, /* per-server config creator */
1188 NULL, /* per-server config merger */
1189 kerb_auth_cmds, /* command table */
1190 NULL, /* [ 9] content handlers */
1191 NULL, /* [ 2] URI-to-filename translation */
1192 kerb_authenticate_user, /* [ 5] check/validate user_id */
1193 NULL, /* [ 6] check user_id is valid *here* */
1194 NULL, /* [ 4] check access by host address */
1195 NULL, /* [ 7] MIME type checker/setter */
1196 NULL, /* [ 8] fixups */
1197 NULL, /* [10] logger */
1198 NULL, /* [ 3] header parser */
1199 NULL, /* process initialization */
1200 NULL, /* process exit/cleanup */
1201 NULL /* [ 1] post read_request handling */
1205 kerb_init_handler(apr_pool_t *p, apr_pool_t *plog,
1206 apr_pool_t *ptemp, server_rec *s)
1208 ap_add_version_component(p, "mod_auth_kerb/" MODAUTHKERB_VERSION);
1212 void kerb_register_hooks(apr_pool_t *p)
1214 ap_hook_post_config(kerb_init_handler, NULL, NULL, APR_HOOK_MIDDLE);
1215 ap_hook_check_user_id(kerb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
1218 module AP_MODULE_DECLARE_DATA auth_kerb_module =
1220 STANDARD20_MODULE_STUFF,
1221 kerb_dir_create_config, /* create per-dir conf structures */
1222 NULL, /* merge per-dir conf structures */
1223 NULL, /* create per-server conf structures */
1224 NULL, /* merge per-server conf structures */
1225 kerb_auth_cmds, /* table of configuration directives */
1226 kerb_register_hooks /* register hooks */