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>
14 * Copyright (c) 2004 Masarykova universita
15 * (Masaryk University, Brno, Czech Republic)
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are met:
21 * 1. Redistributions of source code must retain the above copyright notice,
22 * this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
28 * 3. Neither the name of the University nor the names of its contributors may
29 * be used to endorse or promote products derived from this software
30 * without specific prior written permission.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
33 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
36 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGE.
49 #define MODAUTHKERB_VERSION "5.0-rc4"
52 #include "ap_compat.h"
53 #include "apr_strings.h"
56 #include "http_config.h"
57 #include "http_core.h"
59 #include "http_protocol.h"
60 #include "http_request.h"
67 # include <gssapi/gssapi.h>
68 # include <gssapi/gssapi_generic.h>
69 # define GSS_C_NT_USER_NAME gss_nt_user_name
70 # define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
71 # define krb5_get_err_text(context,code) error_message(code)
73 #include "spnegokrb5.h"
77 /*Prevent warning about closesocket redefinition (Apache's ap_config.h and
78 * MIT Kerberos' port-sockets.h both define it as close) */
83 #include <netdb.h> /* gethostbyname() */
87 module auth_kerb_module;
89 module AP_MODULE_DECLARE_DATA auth_kerb_module;
92 /***************************************************************************
93 Macros To Ease Compatibility
94 ***************************************************************************/
97 #define MK_TABLE_GET ap_table_get
98 #define MK_USER r->connection->user
99 #define MK_AUTH_TYPE r->connection->ap_auth_type
100 #define PROXYREQ_PROXY STD_PROXY
102 #define MK_POOL apr_pool_t
103 #define MK_TABLE_GET apr_table_get
104 #define MK_USER r->user
105 #define MK_AUTH_TYPE r->ap_auth_type
109 /***************************************************************************
110 Auth Configuration Structure
111 ***************************************************************************/
113 char *krb_auth_realms;
114 int krb_save_credentials;
116 char *krb_service_name;
117 int krb_authoritative;
120 int krb_method_gssapi;
121 int krb_method_k5pass;
125 int krb_method_k4pass;
130 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg);
133 #define command(name, func, var, type, usage) \
135 (void*)XtOffsetOf(kerb_auth_config, var), \
136 OR_AUTHCFG, type, usage }
138 #define command(name, func, var, type, usage) \
139 AP_INIT_ ## type (name, func, \
140 (void*)APR_XtOffsetOf(kerb_auth_config, var), \
144 static const command_rec kerb_auth_cmds[] = {
145 command("KrbAuthRealms", krb5_save_realms, krb_auth_realms,
146 RAW_ARGS, "Realms to attempt authentication against (can be multiple)."),
148 command("KrbAuthRealm", krb5_save_realms, krb_auth_realms,
149 RAW_ARGS, "Alias for KrbAuthRealms."),
151 command("KrbSaveCredentials", ap_set_flag_slot, krb_save_credentials,
152 FLAG, "Save and store credentials/tickets retrieved during auth."),
154 command("KrbVerifyKDC", ap_set_flag_slot, krb_verify_kdc,
155 FLAG, "Verify tickets against keytab to prevent KDC spoofing attacks."),
157 command("KrbServiceName", ap_set_string_slot, krb_service_name,
158 TAKE1, "Service name to be used by Apache for authentication."),
160 command("KrbAuthoritative", ap_set_flag_slot, krb_authoritative,
161 FLAG, "Set to 'off' to allow access control to be passed along to lower modules if the UserID is not known to this module."),
164 command("Krb5Keytab", ap_set_file_slot, krb_5_keytab,
165 TAKE1, "Location of Kerberos V5 keytab file."),
167 command("KrbMethodNegotiate", ap_set_flag_slot, krb_method_gssapi,
168 FLAG, "Enable Negotiate authentication method."),
170 command("KrbMethodK5Passwd", ap_set_flag_slot, krb_method_k5pass,
171 FLAG, "Enable Kerberos V5 password authentication."),
175 command("Krb4Srvtab", ap_set_file_slot, krb_4_srvtab,
176 TAKE1, "Location of Kerberos V4 srvtab file."),
178 command("KrbMethodK4Passwd", ap_set_flag_slot, krb_method_k4pass,
179 FLAG, "Enable Kerberos V4 password authentication."),
187 gss_ctx_id_t context;
188 gss_cred_id_t server_creds;
191 static gss_connection_t *gss_connection = NULL;
193 static const char *EMPTY_STRING = "\0";
197 /***************************************************************************
198 Auth Configuration Initialization
199 ***************************************************************************/
200 static void *kerb_dir_create_config(MK_POOL *p, char *d)
202 kerb_auth_config *rec;
204 rec = (kerb_auth_config *) ap_pcalloc(p, sizeof(kerb_auth_config));
205 ((kerb_auth_config *)rec)->krb_verify_kdc = 1;
206 ((kerb_auth_config *)rec)->krb_service_name = "HTTP";
207 ((kerb_auth_config *)rec)->krb_authoritative = 1;
209 ((kerb_auth_config *)rec)->krb_method_k5pass = 1;
210 ((kerb_auth_config *)rec)->krb_method_gssapi = 1;
213 ((kerb_auth_config *)rec)->krb_method_k4pass = 1;
219 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg)
221 sec->krb_auth_realms= ap_pstrdup(cmd->pool, arg);
225 void log_rerror(const char *file, int line, int level, int status,
226 const request_rec *r, const char *fmt, ...)
232 vsnprintf(errstr, sizeof(errstr), fmt, ap);
237 ap_log_rerror(file, line, level | APLOG_NOERRNO, r, "%s", errstr);
239 ap_log_rerror(file, line, level | APLOG_NOERRNO, status, r, "%s", errstr);
244 /***************************************************************************
245 Username/Password Validation for Krb4
246 ***************************************************************************/
248 verify_krb4_user(request_rec *r, char *name, char *instance, char *realm,
249 char *password, char *linstance, char *srvtab, int krb_verify_kdc)
255 const char *hostname;
258 char lrealm[REALM_SZ];
260 ret = krb_get_pw_in_tkt(name, instance, realm, "krbtgt", realm,
261 DEFAULT_TKT_LIFE, password);
263 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
264 "Cannot get krb4 ticket: krb_get_pw_in_tkt() failed: %s",
265 krb_get_err_text(ret));
272 hostname = ap_get_server_name(r);
274 hp = gethostbyname(hostname);
277 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
278 "Cannot verify krb4 ticket: gethostbyname() failed: %s",
282 memcpy(&addr, hp->h_addr, sizeof(addr));
284 phost = krb_get_phost((char *)hostname);
286 krb_get_lrealm(lrealm, 1);
288 ret = krb_mk_req(&ticket, linstance, phost, lrealm, 0);
290 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
291 "Cannot verify krb4 ticket: krb_mk_req() failed: %s",
292 krb_get_err_text(ret));
297 ret = krb_rd_req(&ticket, linstance, phost, addr, &authdata, srvtab);
299 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
300 "Cannot verify krb4 ticket: krb_rd_req() failed: %s",
301 krb_get_err_text(ret));
309 krb4_cache_cleanup(void *data)
311 char *tkt_file = (char *) data;
313 krb_set_tkt_string(tkt_file);
319 authenticate_user_krb4pwd(request_rec *r,
320 kerb_auth_config *conf,
321 const char *auth_line)
325 const char *sent_name;
328 char *tkt_file_p = NULL;
333 char lrealm[REALM_SZ];
334 int all_principals_unkown;
336 sent_pw = ap_pbase64decode(r->pool, auth_line);
337 sent_name = ap_getword (r->pool, &sent_pw, ':');
339 /* do not allow user to override realm setting of server */
340 if (strchr(sent_name, '@')) {
341 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
342 "specifying realm in user name is prohibited");
343 return HTTP_UNAUTHORIZED;
346 sent_instance = strchr(sent_name, '.');
348 *sent_instance++ = '\0';
350 snprintf(tkt_file, sizeof(tkt_file), "/tmp/apache_tkt_XXXXXX");
351 fd = mkstemp(tkt_file);
353 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
354 "Cannot create krb4 ccache: mkstemp() failed: %s",
356 return HTTP_INTERNAL_SERVER_ERROR;
359 tkt_file_p = ap_pstrdup(r->pool, tkt_file);
360 ap_register_cleanup(r->pool, tkt_file_p,
361 krb4_cache_cleanup, ap_null_cleanup);
363 krb_set_tkt_string(tkt_file);
365 all_principals_unkown = 1;
366 realms = conf->krb_auth_realms;
368 memset(lrealm, 0, sizeof(lrealm));
371 realm = ap_getword_white(r->pool, &realms);
374 ret = krb_get_lrealm(lrealm, 1);
380 ret = verify_krb4_user(r, (char *)sent_name,
381 (sent_instance) ? sent_instance : "",
382 (char *)realm, (char *)sent_pw,
383 conf->krb_service_name,
384 conf->krb_4_srvtab, conf->krb_verify_kdc);
385 if (!conf->krb_authoritative && ret) {
386 /* if we're not authoritative, we allow authentication to pass on
387 * to another modules if (and only if) the user is not known to us */
388 if (all_principals_unkown && ret != KDC_PR_UNKNOWN)
389 all_principals_unkown = 0;
394 } while (realms && *realms);
397 /* XXX log only in the verify_krb4_user() call */
398 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Verifying krb4 password failed");
399 ret = (!conf->krb_authoritative && all_principals_unkown == 1 && ret == KDC_PR_UNKNOWN) ?
400 DECLINED : HTTP_UNAUTHORIZED;
404 user = ap_pstrdup(r->pool, sent_name);
406 user = ap_pstrcat(r->pool, user, ".", sent_instance, NULL);
407 user = ap_pstrcat(r->pool, user, "@", realm, NULL);
410 MK_AUTH_TYPE = "Basic";
411 ap_table_setn(r->subprocess_env, "KRBTKFILE", tkt_file_p);
413 if (!conf->krb_save_credentials)
414 krb4_cache_cleanup(tkt_file);
418 krb4_cache_cleanup(tkt_file);
427 /***************************************************************************
428 Username/Password Validation for Krb5
429 ***************************************************************************/
430 /* Inspired by krb5_verify_user from Heimdal */
431 static krb5_error_code
432 verify_krb5_user(request_rec *r, krb5_context context, krb5_principal principal,
433 krb5_ccache ccache, const char *password, const char *service,
434 krb5_keytab keytab, int krb_verify_kdc)
437 krb5_principal server = NULL;
439 krb5_verify_init_creds_opt opt;
441 /* XXX error messages shouldn't be logged here (and in the while() loop in
442 * authenticate_user_krb5pwd() as weell), in order to avoid confusing log
443 * entries when using multiple realms */
445 memset(&creds, 0, sizeof(creds));
447 ret = krb5_get_init_creds_password(context, &creds, principal,
448 (char *)password, krb5_prompter_posix,
449 NULL, 0, NULL, NULL);
451 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
452 "krb5_get_init_creds_password() failed: %s",
453 krb5_get_err_text(context, ret));
457 ret = krb5_sname_to_principal(context, ap_get_server_name(r), service,
458 KRB5_NT_UNKNOWN, &server);
460 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
461 "krb5_sname_to_principal() failed: %s",
462 krb5_get_err_text(context, ret));
466 krb5_verify_init_creds_opt_init(&opt);
467 krb5_verify_init_creds_opt_set_ap_req_nofail(&opt, krb_verify_kdc);
469 ret = krb5_verify_init_creds(context, &creds, server, keytab, NULL, &opt);
471 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
472 "krb5_verify_init_creds() failed: %s",
473 krb5_get_err_text(context, ret));
478 ret = krb5_cc_initialize(context, ccache, principal);
480 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
481 "krb5_cc_initialize() failed: %s",
482 krb5_get_err_text(context, ret));
486 ret = krb5_cc_store_cred(context, ccache, &creds);
488 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
489 "krb5_cc_store_cred() failed: %s",
490 krb5_get_err_text(context, ret));
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,
538 krb5_error_code problem;
540 krb5_ccache tmp_ccache = NULL;
542 ccname = ap_psprintf(r->pool, "FILE:%s/krb5cc_apache_XXXXXX", P_tmpdir);
543 fd = mkstemp(ccname + strlen("FILE:"));
545 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
546 "mkstemp() failed: %s", strerror(errno));
547 ret = HTTP_INTERNAL_SERVER_ERROR;
552 problem = krb5_cc_resolve(kcontext, ccname, &tmp_ccache);
554 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
555 "krb5_cc_resolve() failed: %s",
556 krb5_get_err_text(kcontext, problem));
557 ret = HTTP_INTERNAL_SERVER_ERROR;
562 problem = krb5_cc_initialize(kcontext, tmp_ccache, princ);
564 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
565 "Cannot initialize krb5 ccache %s: krb5_cc_initialize() failed: %s",
566 ccname, krb5_get_err_text(kcontext, problem));
567 ret = HTTP_INTERNAL_SERVER_ERROR;
571 ap_table_setn(r->subprocess_env, "KRB5CCNAME", ccname);
572 ap_register_cleanup(r->pool, ccname,
573 krb5_cache_cleanup, ap_null_cleanup);
575 *ccache = tmp_ccache;
582 krb5_cc_destroy(kcontext, tmp_ccache);
588 store_krb5_creds(krb5_context kcontext,
590 kerb_auth_config *conf,
591 krb5_ccache delegated_cred)
594 krb5_error_code problem;
595 krb5_principal princ;
599 problem = krb5_cc_get_principal(kcontext, delegated_cred, &princ);
601 snprintf(errstr, sizeof(errstr), "krb5_cc_get_principal() failed: %s",
602 krb5_get_err_text(kcontext, problem));
603 return HTTP_INTERNAL_SERVER_ERROR;
606 ret = create_krb5_ccache(kcontext, r, conf, princ, &ccache);
608 krb5_free_principal(kcontext, princ);
613 problem = krb5_cc_copy_cache(kcontext, delegated_cred, ccache);
615 problem = krb5_cc_copy_creds(kcontext, delegated_cred, ccache);
617 krb5_free_principal(kcontext, princ);
619 snprintf(errstr, sizeof(errstr), "Failed to store credentials: %s",
620 krb5_get_err_text(kcontext, problem));
621 krb5_cc_destroy(kcontext, ccache);
622 return HTTP_INTERNAL_SERVER_ERROR;
625 krb5_cc_close(kcontext, ccache);
630 int authenticate_user_krb5pwd(request_rec *r,
631 kerb_auth_config *conf,
632 const char *auth_line)
634 const char *sent_pw = NULL;
635 const char *sent_name = NULL;
636 const char *realms = NULL;
637 krb5_context kcontext = NULL;
638 krb5_error_code code;
639 krb5_principal client = NULL;
640 krb5_ccache ccache = NULL;
641 krb5_keytab keytab = NULL;
644 int all_principals_unkown;
648 code = krb5_init_context(&kcontext);
650 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
651 "Cannot initialize Kerberos5 context (%d)", code);
652 return HTTP_INTERNAL_SERVER_ERROR;
655 sent_pw = ap_pbase64decode(r->pool, auth_line);
656 sent_name = ap_getword (r->pool, &sent_pw, ':');
657 /* do not allow user to override realm setting of server */
658 if (strchr(sent_name, '@')) {
659 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
660 "specifying realm in user name is prohibited");
661 ret = HTTP_UNAUTHORIZED;
665 if (sent_pw == NULL || *sent_pw == '\0') {
666 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
667 "empty passwords are not accepted");
668 ret = HTTP_UNAUTHORIZED;
673 code = krb5_cc_gen_new(kcontext, &krb5_mcc_ops, &ccache);
675 code = krb5_cc_resolve(kcontext, "MEMORY:", &ccache);
678 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
679 "generating new memory ccache failed: %s",
680 krb5_get_err_text(kcontext, code));
681 ret = HTTP_INTERNAL_SERVER_ERROR;
686 if (conf->krb_5_keytab)
687 krb5_kt_resolve(kcontext, conf->krb_5_keytab, &keytab);
689 all_principals_unkown = 1;
690 realms = conf->krb_auth_realms;
692 if (realms && (code = krb5_set_default_realm(kcontext,
693 ap_getword_white(r->pool, &realms)))){
694 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
695 "krb5_set_default_realm() failed: %s",
696 krb5_get_err_text(kcontext, code));
701 krb5_free_principal(kcontext, client);
704 code = krb5_parse_name(kcontext, sent_name, &client);
706 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
707 "krb5_parse_name() failed: %s",
708 krb5_get_err_text(kcontext, code));
712 code = verify_krb5_user(r, kcontext, client, ccache, sent_pw,
713 conf->krb_service_name,
714 keytab, conf->krb_verify_kdc);
715 if (!conf->krb_authoritative && code) {
716 /* if we're not authoritative, we allow authentication to pass on
717 * to another modules if (and only if) the user is not known to us */
718 if (all_principals_unkown && code != KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
719 all_principals_unkown = 0;
725 /* ap_getword_white() used above shifts the parameter, so it's not
726 needed to touch the realms variable */
727 } while (realms && *realms);
729 memset((char *)sent_pw, 0, strlen(sent_pw));
732 if (!conf->krb_authoritative && all_principals_unkown == 1 && code == KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
735 ret = HTTP_UNAUTHORIZED;
740 code = krb5_unparse_name(kcontext, client, &name);
742 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "krb5_unparse_name() failed: %s",
743 krb5_get_err_text(kcontext, code));
744 ret = HTTP_UNAUTHORIZED;
747 MK_USER = ap_pstrdup (r->pool, name);
748 MK_AUTH_TYPE = "Basic";
751 if (conf->krb_save_credentials)
752 store_krb5_creds(kcontext, r, conf, ccache);
758 krb5_free_principal(kcontext, client);
760 krb5_cc_destroy(kcontext, ccache);
762 krb5_kt_close(kcontext, keytab);
763 krb5_free_context(kcontext);
768 /*********************************************************************
769 * GSSAPI Authentication
770 ********************************************************************/
773 get_gss_error(MK_POOL *p, OM_uint32 err_maj, OM_uint32 err_min, char *prefix)
775 OM_uint32 maj_stat, min_stat;
776 OM_uint32 msg_ctx = 0;
777 gss_buffer_desc status_string;
781 err_msg = ap_pstrdup(p, prefix);
783 maj_stat = gss_display_status (&min_stat,
789 if (GSS_ERROR(maj_stat))
791 err_msg = ap_pstrcat(p, err_msg, ": ", (char*) status_string.value, NULL);
792 gss_release_buffer(&min_stat, &status_string);
794 maj_stat = gss_display_status (&min_stat,
800 if (!GSS_ERROR(maj_stat)) {
801 err_msg = ap_pstrcat(p, err_msg,
802 " (", (char*) status_string.value, ")", NULL);
803 gss_release_buffer(&min_stat, &status_string);
805 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
811 cleanup_gss_connection(void *data)
813 OM_uint32 minor_status;
814 gss_connection_t *gss_conn = (gss_connection_t *)data;
818 if (gss_conn->context != GSS_C_NO_CONTEXT)
819 gss_delete_sec_context(&minor_status, &gss_conn->context,
821 if (gss_conn->server_creds != GSS_C_NO_CREDENTIAL)
822 gss_release_cred(&minor_status, &gss_conn->server_creds);
824 gss_connection = NULL;
830 store_gss_creds(request_rec *r, kerb_auth_config *conf, char *princ_name,
831 gss_cred_id_t delegated_cred)
833 OM_uint32 maj_stat, min_stat;
834 krb5_principal princ = NULL;
835 krb5_ccache ccache = NULL;
836 krb5_error_code problem;
837 krb5_context context;
838 int ret = HTTP_INTERNAL_SERVER_ERROR;
840 problem = krb5_init_context(&context);
842 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Cannot initialize krb5 context");
843 return HTTP_INTERNAL_SERVER_ERROR;
846 problem = krb5_parse_name(context, princ_name, &princ);
848 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
849 "Cannot parse delegated username (%s)", krb5_get_err_text(context, problem));
853 problem = create_krb5_ccache(context, r, conf, princ, &ccache);
855 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
856 "Cannot create krb5 ccache (%s)", krb5_get_err_text(context, problem));
860 maj_stat = gss_krb5_copy_ccache(&min_stat, delegated_cred, ccache);
861 if (GSS_ERROR(maj_stat)) {
862 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
863 "Cannot store delegated credential (%s)",
864 get_gss_error(r->pool, maj_stat, min_stat, "gss_krb5_copy_ccache"));
868 krb5_cc_close(context, ccache);
874 krb5_free_principal(context, princ);
876 krb5_cc_destroy(context, ccache);
877 krb5_free_context(context);
882 get_gss_creds(request_rec *r,
883 kerb_auth_config *conf,
884 gss_cred_id_t *server_creds)
886 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
887 OM_uint32 major_status, minor_status, minor_status2;
888 gss_name_t server_name = GSS_C_NO_NAME;
892 /* Don't specify service name. This makes MIT 1.3 not to use replay caches,
893 * which causes large problems with the Microsoft krb5 implementation. MS
894 * obviously uses a format of the krb5 authenticator that is considered by
895 * the MIT as replay (Two valid MS authenticators may contain the same time
896 * and utime fields and only differ in the sequential numbers).
898 snprintf(buf, sizeof(buf), "%s@%s", conf->krb_service_name,
899 ap_get_server_name(r));
901 input_token.value = buf;
902 input_token.length = strlen(buf) + 1;
904 major_status = gss_import_name(&minor_status, &input_token,
905 GSS_C_NT_HOSTBASED_SERVICE,
907 if (GSS_ERROR(major_status)) {
908 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
909 "%s", get_gss_error(r->pool, major_status, minor_status,
910 "gss_import_name() failed"));
911 return HTTP_INTERNAL_SERVER_ERROR;
915 major_status = gss_acquire_cred(&minor_status, server_name, GSS_C_INDEFINITE,
916 GSS_C_NO_OID_SET, GSS_C_ACCEPT,
917 server_creds, NULL, NULL);
918 gss_release_name(&minor_status2, &server_name);
919 if (GSS_ERROR(major_status)) {
920 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
921 "%s", get_gss_error(r->pool, major_status, minor_status,
922 "gss_acquire_cred() failed"));
923 return HTTP_INTERNAL_SERVER_ERROR;
930 cmp_gss_type(gss_buffer_t token, gss_OID oid)
935 if (token->length == 0)
936 return GSS_S_DEFECTIVE_TOKEN;
940 return GSS_S_DEFECTIVE_TOKEN;
943 if ((len & 0x7f) > 4)
944 return GSS_S_DEFECTIVE_TOKEN;
948 return GSS_S_DEFECTIVE_TOKEN;
950 if (((OM_uint32) *p++) != oid->length)
951 return GSS_S_DEFECTIVE_TOKEN;
953 return memcmp(p, oid->elements, oid->length);
957 authenticate_user_gss(request_rec *r, kerb_auth_config *conf,
958 const char *auth_line, char **negotiate_ret_value)
960 OM_uint32 major_status, minor_status, minor_status2;
961 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
962 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
963 const char *auth_param = NULL;
965 gss_name_t client_name = GSS_C_NO_NAME;
966 gss_cred_id_t delegated_cred = GSS_C_NO_CREDENTIAL;
967 OM_uint32 (*accept_sec_token)();
968 gss_OID_desc spnego_oid;
970 *negotiate_ret_value = (char *)EMPTY_STRING;
972 spnego_oid.length = 6;
973 spnego_oid.elements = (void *)"\x2b\x06\x01\x05\x05\x02";
975 if (gss_connection == NULL) {
976 gss_connection = ap_pcalloc(r->connection->pool, sizeof(*gss_connection));
977 if (gss_connection == NULL) {
978 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
979 "ap_pcalloc() failed (not enough memory)");
980 ret = HTTP_INTERNAL_SERVER_ERROR;
983 memset(gss_connection, 0, sizeof(*gss_connection));
984 ap_register_cleanup(r->connection->pool, gss_connection, cleanup_gss_connection, ap_null_cleanup);
987 if (conf->krb_5_keytab) {
989 /* we don't use the ap_* calls here, since the string passed to putenv()
990 * will become part of the enviroment and shouldn't be free()ed by apache
992 ktname = malloc(strlen("KRB5_KTNAME=") + strlen(conf->krb_5_keytab) + 1);
993 if (ktname == NULL) {
994 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "malloc() failed: not enough memory");
995 ret = HTTP_INTERNAL_SERVER_ERROR;
998 sprintf(ktname, "KRB5_KTNAME=%s", conf->krb_5_keytab);
1002 if (gss_connection->server_creds == GSS_C_NO_CREDENTIAL) {
1003 ret = get_gss_creds(r, conf, &gss_connection->server_creds);
1008 /* ap_getword() shifts parameter */
1009 auth_param = ap_getword_white(r->pool, &auth_line);
1010 if (auth_param == NULL) {
1011 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1012 "No Authorization parameter in request from client");
1013 ret = HTTP_UNAUTHORIZED;
1017 input_token.length = ap_base64decode_len(auth_param) + 1;
1018 input_token.value = ap_pcalloc(r->connection->pool, input_token.length);
1019 if (input_token.value == NULL) {
1020 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1021 "ap_pcalloc() failed (not enough memory)");
1022 ret = HTTP_INTERNAL_SERVER_ERROR;
1025 input_token.length = ap_base64decode(input_token.value, auth_param);
1027 accept_sec_token = (cmp_gss_type(&input_token, &spnego_oid) == 0) ?
1028 gss_accept_sec_context_spnego : gss_accept_sec_context;
1030 major_status = accept_sec_token(&minor_status,
1031 &gss_connection->context,
1032 gss_connection->server_creds,
1034 GSS_C_NO_CHANNEL_BINDINGS,
1041 if (output_token.length) {
1045 len = ap_base64encode_len(output_token.length) + 1;
1046 token = ap_pcalloc(r->connection->pool, len + 1);
1047 if (token == NULL) {
1048 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1049 "ap_pcalloc() failed (not enough memory)");
1050 ret = HTTP_INTERNAL_SERVER_ERROR;
1051 gss_release_buffer(&minor_status2, &output_token);
1054 ap_base64encode(token, output_token.value, output_token.length);
1056 *negotiate_ret_value = token;
1057 gss_release_buffer(&minor_status2, &output_token);
1060 if (GSS_ERROR(major_status)) {
1061 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1062 "%s", get_gss_error(r->pool, major_status, minor_status,
1063 "gss_accept_sec_context() failed"));
1064 /* Don't offer the Negotiate method again if call to GSS layer failed */
1065 *negotiate_ret_value = NULL;
1066 ret = HTTP_UNAUTHORIZED;
1070 if (major_status & GSS_S_CONTINUE_NEEDED) {
1071 /* Some GSSAPI mechanism (eg GSI from Globus) may require multiple
1072 * iterations to establish authentication */
1073 ret = HTTP_UNAUTHORIZED;
1077 major_status = gss_display_name(&minor_status, client_name, &output_token, NULL);
1078 gss_release_name(&minor_status, &client_name);
1079 if (GSS_ERROR(major_status)) {
1080 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1081 "%s", get_gss_error(r->pool, major_status, minor_status,
1082 "gss_export_name() failed"));
1083 ret = HTTP_INTERNAL_SERVER_ERROR;
1087 MK_AUTH_TYPE = "Negotiate";
1088 MK_USER = ap_pstrdup(r->pool, output_token.value);
1090 if (conf->krb_save_credentials && delegated_cred != GSS_C_NO_CREDENTIAL)
1091 store_gss_creds(r, conf, (char *)output_token.value, delegated_cred);
1093 gss_release_buffer(&minor_status, &output_token);
1099 gss_release_cred(&minor_status, &delegated_cred);
1101 if (output_token.length)
1102 gss_release_buffer(&minor_status, &output_token);
1104 if (client_name != GSS_C_NO_NAME)
1105 gss_release_name(&minor_status, &client_name);
1107 if (! major_status & GSS_S_CONTINUE_NEEDED)
1108 cleanup_gss_connection(gss_connection);
1115 already_succeeded(request_rec *r)
1117 if (ap_is_initial_req(r) || MK_AUTH_TYPE == NULL)
1119 if (strcmp(MK_AUTH_TYPE, "Negotiate") ||
1120 (strcmp(MK_AUTH_TYPE, "Basic") && strchr(MK_USER, '@')))
1126 note_kerb_auth_failure(request_rec *r, const kerb_auth_config *conf,
1127 int use_krb4, int use_krb5, char *negotiate_ret_value)
1129 const char *auth_name = NULL;
1131 char *negoauth_param;
1132 const char *header_name =
1133 (r->proxyreq == PROXYREQ_PROXY) ? "Proxy-Authenticate" : "WWW-Authenticate";
1135 /* get the user realm specified in .htaccess */
1136 auth_name = ap_auth_name(r);
1138 /* XXX should the WWW-Authenticate header be cleared first? */
1140 if (use_krb5 && conf->krb_method_gssapi && negotiate_ret_value != NULL) {
1141 negoauth_param = (*negotiate_ret_value == '\0') ? "Negotiate" :
1142 ap_pstrcat(r->pool, "Negotiate ", negotiate_ret_value, NULL);
1143 ap_table_add(r->err_headers_out, header_name, negoauth_param);
1145 if (use_krb5 && conf->krb_method_k5pass) {
1146 ap_table_add(r->err_headers_out, header_name,
1147 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1153 if (use_krb4 && conf->krb_method_k4pass && !set_basic)
1154 ap_table_add(r->err_headers_out, header_name,
1155 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1159 int kerb_authenticate_user(request_rec *r)
1161 kerb_auth_config *conf =
1162 (kerb_auth_config *) ap_get_module_config(r->per_dir_config,
1164 const char *auth_type = NULL;
1165 const char *auth_line = NULL;
1166 const char *type = NULL;
1167 int use_krb5 = 0, use_krb4 = 0;
1169 static int last_return = HTTP_UNAUTHORIZED;
1170 char *negotiate_ret_value;
1172 /* get the type specified in .htaccess */
1173 type = ap_auth_type(r);
1175 if (type && strcasecmp(type, "Kerberos") == 0)
1176 use_krb5 = use_krb4 = 1;
1177 else if(type && strcasecmp(type, "KerberosV5") == 0)
1179 else if(type && strcasecmp(type, "KerberosV4") == 0)
1184 /* get what the user sent us in the HTTP header */
1185 auth_line = MK_TABLE_GET(r->headers_in, "Authorization");
1187 auth_line = MK_TABLE_GET(r->headers_in, "Proxy-Authorization");
1189 note_kerb_auth_failure(r, conf, use_krb4, use_krb5, "\0");
1190 return HTTP_UNAUTHORIZED;
1193 auth_type = ap_getword_white(r->pool, &auth_line);
1195 if (already_succeeded(r))
1198 ret = HTTP_UNAUTHORIZED;
1201 if (use_krb5 && conf->krb_method_gssapi &&
1202 strcasecmp(auth_type, "Negotiate") == 0) {
1203 ret = authenticate_user_gss(r, conf, auth_line, &negotiate_ret_value);
1204 } else if (use_krb5 && conf->krb_method_k5pass &&
1205 strcasecmp(auth_type, "Basic") == 0) {
1206 ret = authenticate_user_krb5pwd(r, conf, auth_line);
1211 if (ret == HTTP_UNAUTHORIZED && use_krb4 && conf->krb_method_k4pass &&
1212 strcasecmp(auth_type, "Basic") == 0)
1213 ret = authenticate_user_krb4pwd(r, conf, auth_line);
1216 if (ret == HTTP_UNAUTHORIZED)
1217 note_kerb_auth_failure(r, conf, use_krb4, use_krb5, negotiate_ret_value);
1224 /***************************************************************************
1225 Module Setup/Configuration
1226 ***************************************************************************/
1228 module MODULE_VAR_EXPORT auth_kerb_module = {
1229 STANDARD_MODULE_STUFF,
1230 NULL, /* module initializer */
1231 kerb_dir_create_config, /* per-directory config creator */
1232 NULL, /* per-directory config merger */
1233 NULL, /* per-server config creator */
1234 NULL, /* per-server config merger */
1235 kerb_auth_cmds, /* command table */
1236 NULL, /* [ 9] content handlers */
1237 NULL, /* [ 2] URI-to-filename translation */
1238 kerb_authenticate_user, /* [ 5] check/validate user_id */
1239 NULL, /* [ 6] check user_id is valid *here* */
1240 NULL, /* [ 4] check access by host address */
1241 NULL, /* [ 7] MIME type checker/setter */
1242 NULL, /* [ 8] fixups */
1243 NULL, /* [10] logger */
1244 NULL, /* [ 3] header parser */
1245 NULL, /* process initialization */
1246 NULL, /* process exit/cleanup */
1247 NULL /* [ 1] post read_request handling */
1251 kerb_init_handler(apr_pool_t *p, apr_pool_t *plog,
1252 apr_pool_t *ptemp, server_rec *s)
1254 ap_add_version_component(p, "mod_auth_kerb/" MODAUTHKERB_VERSION);
1258 void kerb_register_hooks(apr_pool_t *p)
1260 ap_hook_post_config(kerb_init_handler, NULL, NULL, APR_HOOK_MIDDLE);
1261 ap_hook_check_user_id(kerb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
1264 module AP_MODULE_DECLARE_DATA auth_kerb_module =
1266 STANDARD20_MODULE_STUFF,
1267 kerb_dir_create_config, /* create per-dir conf structures */
1268 NULL, /* merge per-dir conf structures */
1269 NULL, /* create per-server conf structures */
1270 NULL, /* merge per-server conf structures */
1271 kerb_auth_cmds, /* table of configuration directives */
1272 kerb_register_hooks /* register hooks */