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 set_kerb_auth_headers(request_rec *r, const kerb_auth_config *conf,
131 int use_krb4, int use_krb5pwd, char *negotiate_ret_value);
134 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg);
137 #define command(name, func, var, type, usage) \
139 (void*)XtOffsetOf(kerb_auth_config, var), \
140 OR_AUTHCFG, type, usage }
142 #define command(name, func, var, type, usage) \
143 AP_INIT_ ## type (name, func, \
144 (void*)APR_XtOffsetOf(kerb_auth_config, var), \
148 static const command_rec kerb_auth_cmds[] = {
149 command("KrbAuthRealms", krb5_save_realms, krb_auth_realms,
150 RAW_ARGS, "Realms to attempt authentication against (can be multiple)."),
152 command("KrbAuthRealm", krb5_save_realms, krb_auth_realms,
153 RAW_ARGS, "Alias for KrbAuthRealms."),
155 command("KrbSaveCredentials", ap_set_flag_slot, krb_save_credentials,
156 FLAG, "Save and store credentials/tickets retrieved during auth."),
158 command("KrbVerifyKDC", ap_set_flag_slot, krb_verify_kdc,
159 FLAG, "Verify tickets against keytab to prevent KDC spoofing attacks."),
161 command("KrbServiceName", ap_set_string_slot, krb_service_name,
162 TAKE1, "Service name to be used by Apache for authentication."),
164 command("KrbAuthoritative", ap_set_flag_slot, krb_authoritative,
165 FLAG, "Set to 'off' to allow access control to be passed along to lower modules if the UserID is not known to this module."),
168 command("Krb5Keytab", ap_set_file_slot, krb_5_keytab,
169 TAKE1, "Location of Kerberos V5 keytab file."),
171 command("KrbMethodNegotiate", ap_set_flag_slot, krb_method_gssapi,
172 FLAG, "Enable Negotiate authentication method."),
174 command("KrbMethodK5Passwd", ap_set_flag_slot, krb_method_k5pass,
175 FLAG, "Enable Kerberos V5 password authentication."),
179 command("Krb4Srvtab", ap_set_file_slot, krb_4_srvtab,
180 TAKE1, "Location of Kerberos V4 srvtab file."),
182 command("KrbMethodK4Passwd", ap_set_flag_slot, krb_method_k4pass,
183 FLAG, "Enable Kerberos V4 password authentication."),
191 gss_ctx_id_t context;
192 gss_cred_id_t server_creds;
195 static gss_connection_t *gss_connection = NULL;
199 /***************************************************************************
200 Auth Configuration Initialization
201 ***************************************************************************/
202 static void *kerb_dir_create_config(MK_POOL *p, char *d)
204 kerb_auth_config *rec;
206 rec = (kerb_auth_config *) ap_pcalloc(p, sizeof(kerb_auth_config));
207 ((kerb_auth_config *)rec)->krb_verify_kdc = 1;
208 ((kerb_auth_config *)rec)->krb_service_name = "HTTP";
209 ((kerb_auth_config *)rec)->krb_authoritative = 1;
211 ((kerb_auth_config *)rec)->krb_method_k5pass = 1;
212 ((kerb_auth_config *)rec)->krb_method_gssapi = 1;
215 ((kerb_auth_config *)rec)->krb_method_k4pass = 1;
221 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg)
223 sec->krb_auth_realms= ap_pstrdup(cmd->pool, arg);
227 void log_rerror(const char *file, int line, int level, int status,
228 const request_rec *r, const char *fmt, ...)
234 vsnprintf(errstr, sizeof(errstr), fmt, ap);
239 ap_log_rerror(file, line, level | APLOG_NOERRNO, r, "%s", errstr);
241 ap_log_rerror(file, line, level | APLOG_NOERRNO, status, r, "%s", errstr);
246 /***************************************************************************
247 Username/Password Validation for Krb4
248 ***************************************************************************/
250 verify_krb4_user(request_rec *r, char *name, char *instance, char *realm,
251 char *password, char *linstance, char *srvtab, int krb_verify_kdc)
257 const char *hostname;
260 char lrealm[REALM_SZ];
262 ret = krb_get_pw_in_tkt(name, instance, realm, "krbtgt", realm,
263 DEFAULT_TKT_LIFE, password);
265 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
266 "Cannot get krb4 ticket: krb_get_pw_in_tkt() failed: %s",
267 krb_get_err_text(ret));
274 hostname = ap_get_server_name(r);
276 hp = gethostbyname(hostname);
279 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
280 "Cannot verify krb4 ticket: gethostbyname() failed: %s",
284 memcpy(&addr, hp->h_addr, sizeof(addr));
286 phost = krb_get_phost((char *)hostname);
288 krb_get_lrealm(lrealm, 1);
290 ret = krb_mk_req(&ticket, linstance, phost, lrealm, 0);
292 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
293 "Cannot verify krb4 ticket: krb_mk_req() failed: %s",
294 krb_get_err_text(ret));
299 ret = krb_rd_req(&ticket, linstance, phost, addr, &authdata, srvtab);
301 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
302 "Cannot verify krb4 ticket: krb_rd_req() failed: %s",
303 krb_get_err_text(ret));
311 krb4_cache_cleanup(void *data)
313 char *tkt_file = (char *) data;
315 krb_set_tkt_string(tkt_file);
321 authenticate_user_krb4pwd(request_rec *r,
322 kerb_auth_config *conf,
323 const char *auth_line)
327 const char *sent_name;
330 char *tkt_file_p = NULL;
335 char lrealm[REALM_SZ];
336 int all_principals_unkown;
338 sent_pw = ap_pbase64decode(r->pool, auth_line);
339 sent_name = ap_getword (r->pool, &sent_pw, ':');
341 /* do not allow user to override realm setting of server */
342 if (strchr(sent_name, '@')) {
343 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
344 "specifying realm in user name is prohibited");
345 return HTTP_UNAUTHORIZED;
348 sent_instance = strchr(sent_name, '.');
350 *sent_instance++ = '\0';
352 snprintf(tkt_file, sizeof(tkt_file), "/tmp/apache_tkt_XXXXXX");
353 fd = mkstemp(tkt_file);
355 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
356 "Cannot create krb4 ccache: mkstemp() failed: %s",
358 return HTTP_INTERNAL_SERVER_ERROR;
361 tkt_file_p = ap_pstrdup(r->pool, tkt_file);
362 ap_register_cleanup(r->pool, tkt_file_p,
363 krb4_cache_cleanup, ap_null_cleanup);
365 krb_set_tkt_string(tkt_file);
367 all_principals_unkown = 1;
368 realms = conf->krb_auth_realms;
370 memset(lrealm, 0, sizeof(lrealm));
373 realm = ap_getword_white(r->pool, &realms);
376 ret = krb_get_lrealm(lrealm, 1);
382 ret = verify_krb4_user(r, (char *)sent_name,
383 (sent_instance) ? sent_instance : "",
384 (char *)realm, (char *)sent_pw,
385 conf->krb_service_name,
386 conf->krb_4_srvtab, conf->krb_verify_kdc);
387 if (!conf->krb_authoritative && ret) {
388 /* if we're not authoritative, we allow authentication to pass on
389 * to another modules if (and only if) the user is not known to us */
390 if (all_principals_unkown && ret != KDC_PR_UNKNOWN)
391 all_principals_unkown = 0;
396 } while (realms && *realms);
399 /* XXX log only in the verify_krb4_user() call */
400 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Verifying krb4 password failed");
401 ret = (!conf->krb_authoritative && all_principals_unkown == 1 && ret == KDC_PR_UNKNOWN) ?
402 DECLINED : HTTP_UNAUTHORIZED;
406 user = ap_pstrdup(r->pool, sent_name);
408 user = ap_pstrcat(r->pool, user, ".", sent_instance, NULL);
409 user = ap_pstrcat(r->pool, user, "@", realm, NULL);
412 MK_AUTH_TYPE = "Basic";
413 ap_table_setn(r->subprocess_env, "KRBTKFILE", tkt_file_p);
415 if (!conf->krb_save_credentials)
416 krb4_cache_cleanup(tkt_file);
420 krb4_cache_cleanup(tkt_file);
429 /***************************************************************************
430 Username/Password Validation for Krb5
431 ***************************************************************************/
432 /* Inspired by krb5_verify_user from Heimdal */
433 static krb5_error_code
434 verify_krb5_user(request_rec *r, krb5_context context, krb5_principal principal,
435 krb5_ccache ccache, const char *password, const char *service,
436 krb5_keytab keytab, int krb_verify_kdc)
439 krb5_principal server = NULL;
441 krb5_verify_init_creds_opt opt;
443 /* XXX error messages shouldn't be logged here (and in the while() loop in
444 * authenticate_user_krb5pwd() as weell), in order to avoid confusing log
445 * entries when using multiple realms */
447 memset(&creds, 0, sizeof(creds));
449 ret = krb5_get_init_creds_password(context, &creds, principal,
450 (char *)password, NULL,
451 NULL, 0, NULL, NULL);
453 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
454 "krb5_get_init_creds_password() failed: %s",
455 krb5_get_err_text(context, ret));
459 ret = krb5_sname_to_principal(context, ap_get_server_name(r), service,
460 KRB5_NT_UNKNOWN, &server);
462 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
463 "krb5_sname_to_principal() failed: %s",
464 krb5_get_err_text(context, ret));
468 krb5_verify_init_creds_opt_init(&opt);
469 krb5_verify_init_creds_opt_set_ap_req_nofail(&opt, krb_verify_kdc);
471 ret = krb5_verify_init_creds(context, &creds, server, keytab, NULL, &opt);
473 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
474 "krb5_verify_init_creds() failed: %s",
475 krb5_get_err_text(context, ret));
480 ret = krb5_cc_initialize(context, ccache, principal);
482 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
483 "krb5_cc_initialize() failed: %s",
484 krb5_get_err_text(context, ret));
488 ret = krb5_cc_store_cred(context, ccache, &creds);
490 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
491 "krb5_cc_store_cred() failed: %s",
492 krb5_get_err_text(context, ret));
498 krb5_free_cred_contents(context, &creds);
500 krb5_free_principal(context, server);
505 krb5_cache_cleanup(void *data)
507 krb5_context context;
509 krb5_error_code problem;
510 char *cache_name = (char *) data;
512 problem = krb5_init_context(&context);
514 /* ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "krb5_init_context() failed"); */
515 return HTTP_INTERNAL_SERVER_ERROR;
518 problem = krb5_cc_resolve(context, cache_name, &cache);
520 /* log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
521 "krb5_cc_resolve() failed (%s: %s)",
522 cache_name, krb5_get_err_text(context, problem)); */
523 return HTTP_INTERNAL_SERVER_ERROR;
526 krb5_cc_destroy(context, cache);
527 krb5_free_context(context);
532 create_krb5_ccache(krb5_context kcontext,
534 kerb_auth_config *conf,
535 krb5_principal princ,
540 krb5_error_code problem;
542 krb5_ccache tmp_ccache = NULL;
544 ccname = ap_psprintf(r->pool, "FILE:%s/krb5cc_apache_XXXXXX", P_tmpdir);
545 fd = mkstemp(ccname + strlen("FILE:"));
547 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
548 "mkstemp() failed: %s", strerror(errno));
549 ret = HTTP_INTERNAL_SERVER_ERROR;
554 problem = krb5_cc_resolve(kcontext, ccname, &tmp_ccache);
556 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
557 "krb5_cc_resolve() failed: %s",
558 krb5_get_err_text(kcontext, problem));
559 ret = HTTP_INTERNAL_SERVER_ERROR;
564 problem = krb5_cc_initialize(kcontext, tmp_ccache, princ);
566 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
567 "Cannot initialize krb5 ccache %s: krb5_cc_initialize() failed: %s",
568 ccname, krb5_get_err_text(kcontext, problem));
569 ret = HTTP_INTERNAL_SERVER_ERROR;
573 ap_table_setn(r->subprocess_env, "KRB5CCNAME", ccname);
574 ap_register_cleanup(r->pool, ccname,
575 krb5_cache_cleanup, ap_null_cleanup);
577 *ccache = tmp_ccache;
584 krb5_cc_destroy(kcontext, tmp_ccache);
590 store_krb5_creds(krb5_context kcontext,
592 kerb_auth_config *conf,
593 krb5_ccache delegated_cred)
596 krb5_error_code problem;
597 krb5_principal princ;
601 problem = krb5_cc_get_principal(kcontext, delegated_cred, &princ);
603 snprintf(errstr, sizeof(errstr), "krb5_cc_get_principal() failed: %s",
604 krb5_get_err_text(kcontext, problem));
605 return HTTP_INTERNAL_SERVER_ERROR;
608 ret = create_krb5_ccache(kcontext, r, conf, princ, &ccache);
610 krb5_free_principal(kcontext, princ);
615 problem = krb5_cc_copy_cache(kcontext, delegated_cred, ccache);
617 problem = krb5_cc_copy_creds(kcontext, delegated_cred, ccache);
619 krb5_free_principal(kcontext, princ);
621 snprintf(errstr, sizeof(errstr), "Failed to store credentials: %s",
622 krb5_get_err_text(kcontext, problem));
623 krb5_cc_destroy(kcontext, ccache);
624 return HTTP_INTERNAL_SERVER_ERROR;
627 krb5_cc_close(kcontext, ccache);
632 int authenticate_user_krb5pwd(request_rec *r,
633 kerb_auth_config *conf,
634 const char *auth_line)
636 const char *sent_pw = NULL;
637 const char *sent_name = NULL;
638 const char *realms = NULL;
639 krb5_context kcontext = NULL;
640 krb5_error_code code;
641 krb5_principal client = NULL;
642 krb5_ccache ccache = NULL;
643 krb5_keytab keytab = NULL;
646 int all_principals_unkown;
650 code = krb5_init_context(&kcontext);
652 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
653 "Cannot initialize Kerberos5 context (%d)", code);
654 return HTTP_INTERNAL_SERVER_ERROR;
657 sent_pw = ap_pbase64decode(r->pool, auth_line);
658 sent_name = ap_getword (r->pool, &sent_pw, ':');
659 /* do not allow user to override realm setting of server */
660 if (strchr(sent_name, '@')) {
661 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
662 "specifying realm in user name is prohibited");
663 ret = HTTP_UNAUTHORIZED;
667 if (sent_pw == NULL || *sent_pw == '\0') {
668 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
669 "empty passwords are not accepted");
670 ret = HTTP_UNAUTHORIZED;
674 code = krb5_cc_resolve(kcontext, "MEMORY:", &ccache);
676 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
677 "generating new memory ccache failed: %s",
678 krb5_get_err_text(kcontext, code));
679 ret = HTTP_INTERNAL_SERVER_ERROR;
684 if (conf->krb_5_keytab)
685 krb5_kt_resolve(kcontext, conf->krb_5_keytab, &keytab);
687 all_principals_unkown = 1;
688 realms = conf->krb_auth_realms;
690 if (realms && (code = krb5_set_default_realm(kcontext,
691 ap_getword_white(r->pool, &realms)))){
692 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
693 "krb5_set_default_realm() failed: %s",
694 krb5_get_err_text(kcontext, code));
699 krb5_free_principal(kcontext, client);
702 code = krb5_parse_name(kcontext, sent_name, &client);
704 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
705 "krb5_parse_name() failed: %s",
706 krb5_get_err_text(kcontext, code));
710 code = verify_krb5_user(r, kcontext, client, ccache, sent_pw,
711 conf->krb_service_name,
712 keytab, conf->krb_verify_kdc);
713 if (!conf->krb_authoritative && code) {
714 /* if we're not authoritative, we allow authentication to pass on
715 * to another modules if (and only if) the user is not known to us */
716 if (all_principals_unkown && code != KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
717 all_principals_unkown = 0;
723 /* ap_getword_white() used above shifts the parameter, so it's not
724 needed to touch the realms variable */
725 } while (realms && *realms);
727 memset((char *)sent_pw, 0, strlen(sent_pw));
730 if (!conf->krb_authoritative && all_principals_unkown == 1 && code == KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
733 ret = HTTP_UNAUTHORIZED;
738 code = krb5_unparse_name(kcontext, client, &name);
740 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "krb5_unparse_name() failed: %s",
741 krb5_get_err_text(kcontext, code));
742 ret = HTTP_UNAUTHORIZED;
745 MK_USER = ap_pstrdup (r->pool, name);
746 MK_AUTH_TYPE = "Basic";
749 if (conf->krb_save_credentials)
750 store_krb5_creds(kcontext, r, conf, ccache);
756 krb5_free_principal(kcontext, client);
758 krb5_cc_destroy(kcontext, ccache);
760 krb5_kt_close(kcontext, keytab);
761 krb5_free_context(kcontext);
766 /*********************************************************************
767 * GSSAPI Authentication
768 ********************************************************************/
771 get_gss_error(MK_POOL *p, OM_uint32 err_maj, OM_uint32 err_min, char *prefix)
773 OM_uint32 maj_stat, min_stat;
774 OM_uint32 msg_ctx = 0;
775 gss_buffer_desc status_string;
779 err_msg = ap_pstrdup(p, prefix);
781 maj_stat = gss_display_status (&min_stat,
787 if (GSS_ERROR(maj_stat))
789 err_msg = ap_pstrcat(p, err_msg, ": ", (char*) status_string.value, NULL);
790 gss_release_buffer(&min_stat, &status_string);
792 maj_stat = gss_display_status (&min_stat,
798 if (!GSS_ERROR(maj_stat)) {
799 err_msg = ap_pstrcat(p, err_msg,
800 " (", (char*) status_string.value, ")", NULL);
801 gss_release_buffer(&min_stat, &status_string);
803 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
809 cleanup_gss_connection(void *data)
811 OM_uint32 minor_status;
812 gss_connection_t *gss_conn = (gss_connection_t *)data;
816 if (gss_conn->context != GSS_C_NO_CONTEXT)
817 gss_delete_sec_context(&minor_status, &gss_conn->context,
819 if (gss_conn->server_creds != GSS_C_NO_CREDENTIAL)
820 gss_release_cred(&minor_status, &gss_conn->server_creds);
822 gss_connection = NULL;
828 store_gss_creds(request_rec *r, kerb_auth_config *conf, char *princ_name,
829 gss_cred_id_t delegated_cred)
831 OM_uint32 maj_stat, min_stat;
832 krb5_principal princ = NULL;
833 krb5_ccache ccache = NULL;
834 krb5_error_code problem;
835 krb5_context context;
836 int ret = HTTP_INTERNAL_SERVER_ERROR;
838 problem = krb5_init_context(&context);
840 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Cannot initialize krb5 context");
841 return HTTP_INTERNAL_SERVER_ERROR;
844 problem = krb5_parse_name(context, princ_name, &princ);
846 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
847 "Cannot parse delegated username (%s)", krb5_get_err_text(context, problem));
851 problem = create_krb5_ccache(context, r, conf, princ, &ccache);
853 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
854 "Cannot create krb5 ccache (%s)", krb5_get_err_text(context, problem));
858 maj_stat = gss_krb5_copy_ccache(&min_stat, delegated_cred, ccache);
859 if (GSS_ERROR(maj_stat)) {
860 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
861 "Cannot store delegated credential (%s)",
862 get_gss_error(r->pool, maj_stat, min_stat, "gss_krb5_copy_ccache"));
866 krb5_cc_close(context, ccache);
872 krb5_free_principal(context, princ);
874 krb5_cc_destroy(context, ccache);
875 krb5_free_context(context);
880 get_gss_creds(request_rec *r,
881 kerb_auth_config *conf,
882 gss_cred_id_t *server_creds)
884 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
885 OM_uint32 major_status, minor_status, minor_status2;
886 gss_name_t server_name = GSS_C_NO_NAME;
890 /* Don't specify service name. This makes MIT 1.3 not to use replay caches,
891 * which causes large problems with the Microsoft krb5 implementation. MS
892 * obviously uses a format of the krb5 authenticator that is considered by
893 * the MIT as replay (Two valid MS authenticators may contain the same time
894 * and utime fields and only differ in the sequential numbers).
896 snprintf(buf, sizeof(buf), "%s@%s", conf->krb_service_name,
897 ap_get_server_name(r));
899 input_token.value = buf;
900 input_token.length = strlen(buf) + 1;
902 major_status = gss_import_name(&minor_status, &input_token,
903 GSS_C_NT_HOSTBASED_SERVICE,
905 if (GSS_ERROR(major_status)) {
906 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
907 "%s", get_gss_error(r->pool, major_status, minor_status,
908 "gss_import_name() failed"));
909 return HTTP_INTERNAL_SERVER_ERROR;
913 major_status = gss_acquire_cred(&minor_status, server_name, GSS_C_INDEFINITE,
914 GSS_C_NO_OID_SET, GSS_C_ACCEPT,
915 server_creds, NULL, NULL);
916 gss_release_name(&minor_status2, &server_name);
917 if (GSS_ERROR(major_status)) {
918 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
919 "%s", get_gss_error(r->pool, major_status, minor_status,
920 "gss_acquire_cred() failed"));
921 return HTTP_INTERNAL_SERVER_ERROR;
928 cmp_gss_type(gss_buffer_t token, gss_OID oid)
933 if (token->length == 0)
934 return GSS_S_DEFECTIVE_TOKEN;
938 return GSS_S_DEFECTIVE_TOKEN;
941 if ((len & 0x7f) > 4)
942 return GSS_S_DEFECTIVE_TOKEN;
946 return GSS_S_DEFECTIVE_TOKEN;
948 if (((OM_uint32) *p++) != oid->length)
949 return GSS_S_DEFECTIVE_TOKEN;
951 return memcmp(p, oid->elements, oid->length);
955 authenticate_user_gss(request_rec *r, kerb_auth_config *conf,
956 const char *auth_line, char **negotiate_ret_value)
958 OM_uint32 major_status, minor_status, minor_status2;
959 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
960 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
961 const char *auth_param = NULL;
963 gss_name_t client_name = GSS_C_NO_NAME;
964 gss_cred_id_t delegated_cred = GSS_C_NO_CREDENTIAL;
965 OM_uint32 (*accept_sec_token)();
966 gss_OID_desc spnego_oid;
968 *negotiate_ret_value = "\0";
970 spnego_oid.length = 6;
971 spnego_oid.elements = (void *)"\x2b\x06\x01\x05\x05\x02";
973 if (gss_connection == NULL) {
974 gss_connection = ap_pcalloc(r->connection->pool, sizeof(*gss_connection));
975 if (gss_connection == NULL) {
976 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
977 "ap_pcalloc() failed (not enough memory)");
978 ret = HTTP_INTERNAL_SERVER_ERROR;
981 memset(gss_connection, 0, sizeof(*gss_connection));
982 ap_register_cleanup(r->connection->pool, gss_connection, cleanup_gss_connection, ap_null_cleanup);
985 if (conf->krb_5_keytab) {
987 /* we don't use the ap_* calls here, since the string passed to putenv()
988 * will become part of the enviroment and shouldn't be free()ed by apache
990 ktname = malloc(strlen("KRB5_KTNAME=") + strlen(conf->krb_5_keytab) + 1);
991 if (ktname == NULL) {
992 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "malloc() failed: not enough memory");
993 ret = HTTP_INTERNAL_SERVER_ERROR;
996 sprintf(ktname, "KRB5_KTNAME=%s", conf->krb_5_keytab);
1000 if (gss_connection->server_creds == GSS_C_NO_CREDENTIAL) {
1001 ret = get_gss_creds(r, conf, &gss_connection->server_creds);
1006 /* ap_getword() shifts parameter */
1007 auth_param = ap_getword_white(r->pool, &auth_line);
1008 if (auth_param == NULL) {
1009 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1010 "No Authorization parameter in request from client");
1011 ret = HTTP_UNAUTHORIZED;
1015 input_token.length = ap_base64decode_len(auth_param) + 1;
1016 input_token.value = ap_pcalloc(r->connection->pool, input_token.length);
1017 if (input_token.value == NULL) {
1018 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1019 "ap_pcalloc() failed (not enough memory)");
1020 ret = HTTP_INTERNAL_SERVER_ERROR;
1023 input_token.length = ap_base64decode(input_token.value, auth_param);
1025 accept_sec_token = (cmp_gss_type(&input_token, &spnego_oid) == 0) ?
1026 gss_accept_sec_context_spnego : gss_accept_sec_context;
1028 major_status = accept_sec_token(&minor_status,
1029 &gss_connection->context,
1030 gss_connection->server_creds,
1032 GSS_C_NO_CHANNEL_BINDINGS,
1039 if (output_token.length) {
1043 len = ap_base64encode_len(output_token.length) + 1;
1044 token = ap_pcalloc(r->connection->pool, len + 1);
1045 if (token == NULL) {
1046 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1047 "ap_pcalloc() failed (not enough memory)");
1048 ret = HTTP_INTERNAL_SERVER_ERROR;
1049 gss_release_buffer(&minor_status2, &output_token);
1052 ap_base64encode(token, output_token.value, output_token.length);
1054 *negotiate_ret_value = token;
1055 gss_release_buffer(&minor_status2, &output_token);
1058 if (GSS_ERROR(major_status)) {
1059 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1060 "%s", get_gss_error(r->pool, major_status, minor_status,
1061 "gss_accept_sec_context() failed"));
1062 /* Don't offer the Negotiate method again if call to GSS layer failed */
1063 *negotiate_ret_value = NULL;
1064 ret = HTTP_UNAUTHORIZED;
1068 if (major_status & GSS_S_CONTINUE_NEEDED) {
1069 /* Some GSSAPI mechanism (eg GSI from Globus) may require multiple
1070 * iterations to establish authentication */
1071 ret = HTTP_UNAUTHORIZED;
1075 major_status = gss_display_name(&minor_status, client_name, &output_token, NULL);
1076 gss_release_name(&minor_status, &client_name);
1077 if (GSS_ERROR(major_status)) {
1078 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1079 "%s", get_gss_error(r->pool, major_status, minor_status,
1080 "gss_export_name() failed"));
1081 ret = HTTP_INTERNAL_SERVER_ERROR;
1085 MK_AUTH_TYPE = "Negotiate";
1086 MK_USER = ap_pstrdup(r->pool, output_token.value);
1088 if (conf->krb_save_credentials && delegated_cred != GSS_C_NO_CREDENTIAL)
1089 store_gss_creds(r, conf, (char *)output_token.value, delegated_cred);
1091 if (*negotiate_ret_value)
1092 set_kerb_auth_headers(r, conf, 0, 0, *negotiate_ret_value);
1094 gss_release_buffer(&minor_status, &output_token);
1100 gss_release_cred(&minor_status, &delegated_cred);
1102 if (output_token.length)
1103 gss_release_buffer(&minor_status, &output_token);
1105 if (client_name != GSS_C_NO_NAME)
1106 gss_release_name(&minor_status, &client_name);
1108 if (! major_status & GSS_S_CONTINUE_NEEDED)
1109 cleanup_gss_connection(gss_connection);
1116 already_succeeded(request_rec *r)
1118 if (ap_is_initial_req(r) || MK_AUTH_TYPE == NULL)
1120 if (strcmp(MK_AUTH_TYPE, "Negotiate") ||
1121 (strcmp(MK_AUTH_TYPE, "Basic") && strchr(MK_USER, '@')))
1127 set_kerb_auth_headers(request_rec *r, const kerb_auth_config *conf,
1128 int use_krb4, int use_krb5pwd, char *negotiate_ret_value)
1130 const char *auth_name = NULL;
1132 char *negoauth_param;
1133 const char *header_name =
1134 (r->proxyreq == PROXYREQ_PROXY) ? "Proxy-Authenticate" : "WWW-Authenticate";
1136 /* get the user realm specified in .htaccess */
1137 auth_name = ap_auth_name(r);
1139 /* XXX should the WWW-Authenticate header be cleared first? */
1141 if (negotiate_ret_value != NULL && conf->krb_method_gssapi) {
1142 negoauth_param = (*negotiate_ret_value == '\0') ? "Negotiate" :
1143 ap_pstrcat(r->pool, "Negotiate ", negotiate_ret_value, NULL);
1144 ap_table_add(r->err_headers_out, header_name, negoauth_param);
1146 if (use_krb5pwd && conf->krb_method_k5pass) {
1147 ap_table_add(r->err_headers_out, header_name,
1148 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1154 if (use_krb4 && conf->krb_method_k4pass && !set_basic)
1155 ap_table_add(r->err_headers_out, header_name,
1156 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1160 int kerb_authenticate_user(request_rec *r)
1162 kerb_auth_config *conf =
1163 (kerb_auth_config *) ap_get_module_config(r->per_dir_config,
1165 const char *auth_type = NULL;
1166 const char *auth_line = NULL;
1167 const char *type = NULL;
1168 int use_krb5 = 0, use_krb4 = 0;
1170 static int last_return = HTTP_UNAUTHORIZED;
1171 char *negotiate_ret_value = NULL;
1173 /* get the type specified in .htaccess */
1174 type = ap_auth_type(r);
1176 if (type && strcasecmp(type, "Kerberos") == 0)
1177 use_krb5 = use_krb4 = 1;
1178 else if(type && strcasecmp(type, "KerberosV5") == 0)
1180 else if(type && strcasecmp(type, "KerberosV4") == 0)
1185 /* get what the user sent us in the HTTP header */
1186 auth_line = MK_TABLE_GET(r->headers_in, "Authorization");
1188 auth_line = MK_TABLE_GET(r->headers_in, "Proxy-Authorization");
1190 set_kerb_auth_headers(r, conf, use_krb4, use_krb5,
1191 (use_krb5) ? "\0" : NULL);
1192 return HTTP_UNAUTHORIZED;
1195 auth_type = ap_getword_white(r->pool, &auth_line);
1197 if (already_succeeded(r))
1200 ret = HTTP_UNAUTHORIZED;
1203 if (use_krb5 && conf->krb_method_gssapi &&
1204 strcasecmp(auth_type, "Negotiate") == 0) {
1205 ret = authenticate_user_gss(r, conf, auth_line, &negotiate_ret_value);
1206 } else if (use_krb5 && conf->krb_method_k5pass &&
1207 strcasecmp(auth_type, "Basic") == 0) {
1208 ret = authenticate_user_krb5pwd(r, conf, auth_line);
1213 if (ret == HTTP_UNAUTHORIZED && use_krb4 && conf->krb_method_k4pass &&
1214 strcasecmp(auth_type, "Basic") == 0)
1215 ret = authenticate_user_krb4pwd(r, conf, auth_line);
1218 if (ret == HTTP_UNAUTHORIZED)
1219 set_kerb_auth_headers(r, conf, use_krb4, use_krb5, negotiate_ret_value);
1226 /***************************************************************************
1227 Module Setup/Configuration
1228 ***************************************************************************/
1230 module MODULE_VAR_EXPORT auth_kerb_module = {
1231 STANDARD_MODULE_STUFF,
1232 NULL, /* module initializer */
1233 kerb_dir_create_config, /* per-directory config creator */
1234 NULL, /* per-directory config merger */
1235 NULL, /* per-server config creator */
1236 NULL, /* per-server config merger */
1237 kerb_auth_cmds, /* command table */
1238 NULL, /* [ 9] content handlers */
1239 NULL, /* [ 2] URI-to-filename translation */
1240 kerb_authenticate_user, /* [ 5] check/validate user_id */
1241 NULL, /* [ 6] check user_id is valid *here* */
1242 NULL, /* [ 4] check access by host address */
1243 NULL, /* [ 7] MIME type checker/setter */
1244 NULL, /* [ 8] fixups */
1245 NULL, /* [10] logger */
1246 NULL, /* [ 3] header parser */
1247 NULL, /* process initialization */
1248 NULL, /* process exit/cleanup */
1249 NULL /* [ 1] post read_request handling */
1253 kerb_init_handler(apr_pool_t *p, apr_pool_t *plog,
1254 apr_pool_t *ptemp, server_rec *s)
1256 ap_add_version_component(p, "mod_auth_kerb/" MODAUTHKERB_VERSION);
1260 void kerb_register_hooks(apr_pool_t *p)
1262 ap_hook_post_config(kerb_init_handler, NULL, NULL, APR_HOOK_MIDDLE);
1263 ap_hook_check_user_id(kerb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
1266 module AP_MODULE_DECLARE_DATA auth_kerb_module =
1268 STANDARD20_MODULE_STUFF,
1269 kerb_dir_create_config, /* create per-dir conf structures */
1270 NULL, /* merge per-dir conf structures */
1271 NULL, /* create per-server conf structures */
1272 NULL, /* merge per-server conf structures */
1273 kerb_auth_cmds, /* table of configuration directives */
1274 kerb_register_hooks /* register hooks */