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 <http_config.h>
53 #include <http_core.h>
55 #include <http_protocol.h>
56 #include <http_request.h>
58 #ifdef STANDARD20_MODULE_STUFF
59 #include <ap_compat.h>
60 #include <apr_strings.h>
69 # include <gssapi/gssapi.h>
70 # include <gssapi/gssapi_generic.h>
71 # define GSS_C_NT_USER_NAME gss_nt_user_name
72 # define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
73 # define krb5_get_err_text(context,code) error_message(code)
75 #include "spnegokrb5.h"
79 /* Prevent warning about closesocket redefinition (Apache's ap_config.h and
80 * MIT Kerberos' port-sockets.h both define it as close) */
85 #include <netdb.h> /* gethostbyname() */
88 #ifdef STANDARD20_MODULE_STUFF
89 module AP_MODULE_DECLARE_DATA auth_kerb_module;
91 module auth_kerb_module;
94 /***************************************************************************
95 Macros To Ease Compatibility
96 ***************************************************************************/
97 #ifdef STANDARD20_MODULE_STUFF
98 #define MK_POOL apr_pool_t
99 #define MK_TABLE_GET apr_table_get
100 #define MK_USER r->user
101 #define MK_AUTH_TYPE r->ap_auth_type
104 #define MK_TABLE_GET ap_table_get
105 #define MK_USER r->connection->user
106 #define MK_AUTH_TYPE r->connection->ap_auth_type
107 #define PROXYREQ_PROXY STD_PROXY
110 /***************************************************************************
111 Auth Configuration Structure
112 ***************************************************************************/
114 char *krb_auth_realms;
115 int krb_save_credentials;
117 char *krb_service_name;
118 int krb_authoritative;
121 int krb_method_gssapi;
122 int krb_method_k5pass;
126 int krb_method_k4pass;
131 set_kerb_auth_headers(request_rec *r, const kerb_auth_config *conf,
132 int use_krb4, int use_krb5pwd, char *negotiate_ret_value);
135 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg);
137 #ifdef STANDARD20_MODULE_STUFF
138 #define command(name, func, var, type, usage) \
139 AP_INIT_ ## type (name, func, \
140 (void*)APR_XtOffsetOf(kerb_auth_config, var), \
143 #define command(name, func, var, type, usage) \
145 (void*)XtOffsetOf(kerb_auth_config, var), \
146 OR_AUTHCFG, type, usage }
149 static const command_rec kerb_auth_cmds[] = {
150 command("KrbAuthRealms", krb5_save_realms, krb_auth_realms,
151 RAW_ARGS, "Realms to attempt authentication against (can be multiple)."),
153 command("KrbAuthRealm", krb5_save_realms, krb_auth_realms,
154 RAW_ARGS, "Alias for KrbAuthRealms."),
156 command("KrbSaveCredentials", ap_set_flag_slot, krb_save_credentials,
157 FLAG, "Save and store credentials/tickets retrieved during auth."),
159 command("KrbVerifyKDC", ap_set_flag_slot, krb_verify_kdc,
160 FLAG, "Verify tickets against keytab to prevent KDC spoofing attacks."),
162 command("KrbServiceName", ap_set_string_slot, krb_service_name,
163 TAKE1, "Service name to be used by Apache for authentication."),
165 command("KrbAuthoritative", ap_set_flag_slot, krb_authoritative,
166 FLAG, "Set to 'off' to allow access control to be passed along to lower modules if the UserID is not known to this module."),
169 command("Krb5Keytab", ap_set_file_slot, krb_5_keytab,
170 TAKE1, "Location of Kerberos V5 keytab file."),
172 command("KrbMethodNegotiate", ap_set_flag_slot, krb_method_gssapi,
173 FLAG, "Enable Negotiate authentication method."),
175 command("KrbMethodK5Passwd", ap_set_flag_slot, krb_method_k5pass,
176 FLAG, "Enable Kerberos V5 password authentication."),
180 command("Krb4Srvtab", ap_set_file_slot, krb_4_srvtab,
181 TAKE1, "Location of Kerberos V4 srvtab file."),
183 command("KrbMethodK4Passwd", ap_set_flag_slot, krb_method_k4pass,
184 FLAG, "Enable Kerberos V4 password authentication."),
190 #if defined(KRB5) && !defined(HEIMDAL)
191 /* Needed to work around problems with replay caches */
192 #include "mit-internals.h"
194 /* This is our replacement krb5_rc_store function */
195 static krb5_error_code
196 mod_auth_kerb_rc_store(krb5_context context, krb5_rcache rcache,
197 krb5_donot_replay *donot_replay)
202 /* And this is the operations vector for our replay cache */
203 const krb5_rc_ops mod_auth_kerb_rc_ops = {
210 mod_auth_kerb_rc_store,
212 krb5_rc_dfl_get_span,
213 krb5_rc_dfl_get_name,
219 /***************************************************************************
220 Auth Configuration Initialization
221 ***************************************************************************/
222 static void *kerb_dir_create_config(MK_POOL *p, char *d)
224 kerb_auth_config *rec;
226 rec = (kerb_auth_config *) ap_pcalloc(p, sizeof(kerb_auth_config));
227 ((kerb_auth_config *)rec)->krb_verify_kdc = 1;
228 ((kerb_auth_config *)rec)->krb_service_name = "HTTP";
229 ((kerb_auth_config *)rec)->krb_authoritative = 1;
231 ((kerb_auth_config *)rec)->krb_method_k5pass = 1;
232 ((kerb_auth_config *)rec)->krb_method_gssapi = 1;
235 ((kerb_auth_config *)rec)->krb_method_k4pass = 1;
241 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg)
243 sec->krb_auth_realms= ap_pstrdup(cmd->pool, arg);
247 void log_rerror(const char *file, int line, int level, int status,
248 const request_rec *r, const char *fmt, ...)
254 vsnprintf(errstr, sizeof(errstr), fmt, ap);
258 #ifdef STANDARD20_MODULE_STUFF
259 ap_log_rerror(file, line, level | APLOG_NOERRNO, status, r, "%s", errstr);
261 ap_log_rerror(file, line, level | APLOG_NOERRNO, r, "%s", errstr);
266 /***************************************************************************
267 Username/Password Validation for Krb4
268 ***************************************************************************/
270 verify_krb4_user(request_rec *r, char *name, char *instance, char *realm,
271 char *password, char *linstance, char *srvtab, int krb_verify_kdc)
277 const char *hostname;
280 char lrealm[REALM_SZ];
282 ret = krb_get_pw_in_tkt(name, instance, realm, "krbtgt", realm,
283 DEFAULT_TKT_LIFE, password);
285 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
286 "Cannot get krb4 ticket: krb_get_pw_in_tkt() failed: %s",
287 krb_get_err_text(ret));
294 hostname = ap_get_server_name(r);
296 hp = gethostbyname(hostname);
299 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
300 "Cannot verify krb4 ticket: gethostbyname() failed: %s",
304 memcpy(&addr, hp->h_addr, sizeof(addr));
306 phost = krb_get_phost((char *)hostname);
308 krb_get_lrealm(lrealm, 1);
310 ret = krb_mk_req(&ticket, linstance, phost, lrealm, 0);
312 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
313 "Cannot verify krb4 ticket: krb_mk_req() failed: %s",
314 krb_get_err_text(ret));
319 ret = krb_rd_req(&ticket, linstance, phost, addr, &authdata, srvtab);
321 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
322 "Cannot verify krb4 ticket: krb_rd_req() failed: %s",
323 krb_get_err_text(ret));
331 krb4_cache_cleanup(void *data)
333 char *tkt_file = (char *) data;
335 krb_set_tkt_string(tkt_file);
341 authenticate_user_krb4pwd(request_rec *r,
342 kerb_auth_config *conf,
343 const char *auth_line)
347 const char *sent_name;
350 char *tkt_file_p = NULL;
355 char lrealm[REALM_SZ];
356 int all_principals_unkown;
358 sent_pw = ap_pbase64decode(r->pool, auth_line);
359 sent_name = ap_getword (r->pool, &sent_pw, ':');
361 /* do not allow user to override realm setting of server */
362 if (strchr(sent_name, '@')) {
363 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
364 "specifying realm in user name is prohibited");
365 return HTTP_UNAUTHORIZED;
368 sent_instance = strchr(sent_name, '.');
370 *sent_instance++ = '\0';
372 snprintf(tkt_file, sizeof(tkt_file), "/tmp/apache_tkt_XXXXXX");
373 fd = mkstemp(tkt_file);
375 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
376 "Cannot create krb4 ccache: mkstemp() failed: %s",
378 return HTTP_INTERNAL_SERVER_ERROR;
381 tkt_file_p = ap_pstrdup(r->pool, tkt_file);
382 ap_register_cleanup(r->pool, tkt_file_p,
383 krb4_cache_cleanup, ap_null_cleanup);
385 krb_set_tkt_string(tkt_file);
387 all_principals_unkown = 1;
388 realms = conf->krb_auth_realms;
390 memset(lrealm, 0, sizeof(lrealm));
393 realm = ap_getword_white(r->pool, &realms);
396 ret = krb_get_lrealm(lrealm, 1);
402 ret = verify_krb4_user(r, (char *)sent_name,
403 (sent_instance) ? sent_instance : "",
404 (char *)realm, (char *)sent_pw,
405 conf->krb_service_name,
406 conf->krb_4_srvtab, conf->krb_verify_kdc);
407 if (!conf->krb_authoritative && ret) {
408 /* if we're not authoritative, we allow authentication to pass on
409 * to another modules if (and only if) the user is not known to us */
410 if (all_principals_unkown && ret != KDC_PR_UNKNOWN)
411 all_principals_unkown = 0;
416 } while (realms && *realms);
419 /* XXX log only in the verify_krb4_user() call */
420 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Verifying krb4 password failed");
421 ret = (!conf->krb_authoritative && all_principals_unkown == 1 && ret == KDC_PR_UNKNOWN) ?
422 DECLINED : HTTP_UNAUTHORIZED;
426 user = ap_pstrdup(r->pool, sent_name);
428 user = ap_pstrcat(r->pool, user, ".", sent_instance, NULL);
429 user = ap_pstrcat(r->pool, user, "@", realm, NULL);
432 MK_AUTH_TYPE = "Basic";
433 ap_table_setn(r->subprocess_env, "KRBTKFILE", tkt_file_p);
435 if (!conf->krb_save_credentials)
436 krb4_cache_cleanup(tkt_file);
440 krb4_cache_cleanup(tkt_file);
449 /***************************************************************************
450 Username/Password Validation for Krb5
451 ***************************************************************************/
452 static krb5_error_code
453 verify_krb5_init_creds(krb5_context context, krb5_creds *creds,
454 krb5_principal ap_req_server, krb5_keytab ap_req_keytab)
458 krb5_ccache local_ccache = NULL;
459 krb5_creds *new_creds = NULL;
460 krb5_auth_context auth_context = NULL;
461 krb5_keytab keytab = NULL;
463 krb5_data_zero (&req);
465 if (ap_req_keytab == NULL) {
466 ret = krb5_kt_default (context, &keytab);
470 keytab = ap_req_keytab;
472 ret = krb5_cc_resolve(context, "MEMORY:", &local_ccache);
476 ret = krb5_cc_initialize(context, local_ccache, creds->client);
480 ret = krb5_cc_store_cred (context, local_ccache, creds);
484 if (!krb5_principal_compare (context, ap_req_server, creds->server)) {
485 krb5_creds match_cred;
487 memset (&match_cred, 0, sizeof(match_cred));
489 match_cred.client = creds->client;
490 match_cred.server = ap_req_server;
492 ret = krb5_get_credentials (context, 0, local_ccache,
493 &match_cred, &new_creds);
499 ret = krb5_mk_req_extended (context, &auth_context, 0, NULL, creds, &req);
503 krb5_auth_con_free (context, auth_context);
504 ret = krb5_auth_con_init(context, &auth_context);
507 krb5_auth_con_setflags(context, auth_context, KRB5_AUTH_CONTEXT_DO_SEQUENCE);
511 ret = krb5_rd_req (context, &auth_context, &req, ap_req_server,
515 krb5_free_data_contents(context, &req);
517 krb5_auth_con_free (context, auth_context);
519 krb5_free_creds (context, new_creds);
520 if (ap_req_keytab == NULL && keytab)
521 krb5_kt_close (context, keytab);
523 krb5_cc_destroy (context, local_ccache);
528 /* Inspired by krb5_verify_user from Heimdal */
529 static krb5_error_code
530 verify_krb5_user(request_rec *r, krb5_context context, krb5_principal principal,
531 krb5_ccache ccache, const char *password, const char *service,
532 krb5_keytab keytab, int krb_verify_kdc)
535 krb5_principal server = NULL;
538 /* XXX error messages shouldn't be logged here (and in the while() loop in
539 * authenticate_user_krb5pwd() as weell), in order to avoid confusing log
540 * entries when using multiple realms */
542 memset(&creds, 0, sizeof(creds));
544 ret = krb5_get_init_creds_password(context, &creds, principal,
545 (char *)password, NULL,
546 NULL, 0, NULL, NULL);
548 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
549 "krb5_get_init_creds_password() failed: %s",
550 krb5_get_err_text(context, ret));
554 ret = krb5_sname_to_principal(context, ap_get_server_name(r), service,
555 KRB5_NT_UNKNOWN, &server);
557 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
558 "krb5_sname_to_principal() failed: %s",
559 krb5_get_err_text(context, ret));
562 /* XXX log_debug: lookig for <server_princ> in keytab */
568 krb5_get_default_realm(context, &realm);
569 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
570 "trying to verify password using key for %s/%s@%s",
571 service, ap_get_server_name(r), realm);
575 if (krb_verify_kdc &&
576 (ret = verify_krb5_init_creds(context, &creds, server, keytab))) {
577 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
578 "failed to verify krb5 credentials: %s",
579 krb5_get_err_text(context, ret));
584 ret = krb5_cc_initialize(context, ccache, principal);
586 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
587 "krb5_cc_initialize() failed: %s",
588 krb5_get_err_text(context, ret));
592 ret = krb5_cc_store_cred(context, ccache, &creds);
594 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
595 "krb5_cc_store_cred() failed: %s",
596 krb5_get_err_text(context, ret));
602 krb5_free_cred_contents(context, &creds);
604 krb5_free_principal(context, server);
610 krb5_cache_cleanup(void *data)
612 krb5_context context;
614 krb5_error_code problem;
615 char *cache_name = (char *) data;
617 problem = krb5_init_context(&context);
619 /* ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "krb5_init_context() failed"); */
620 return HTTP_INTERNAL_SERVER_ERROR;
623 problem = krb5_cc_resolve(context, cache_name, &cache);
625 /* log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
626 "krb5_cc_resolve() failed (%s: %s)",
627 cache_name, krb5_get_err_text(context, problem)); */
628 return HTTP_INTERNAL_SERVER_ERROR;
631 krb5_cc_destroy(context, cache);
632 krb5_free_context(context);
637 create_krb5_ccache(krb5_context kcontext,
639 kerb_auth_config *conf,
640 krb5_principal princ,
645 krb5_error_code problem;
647 krb5_ccache tmp_ccache = NULL;
649 ccname = ap_psprintf(r->pool, "FILE:%s/krb5cc_apache_XXXXXX", P_tmpdir);
650 fd = mkstemp(ccname + strlen("FILE:"));
652 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
653 "mkstemp() failed: %s", strerror(errno));
654 ret = HTTP_INTERNAL_SERVER_ERROR;
659 problem = krb5_cc_resolve(kcontext, ccname, &tmp_ccache);
661 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
662 "krb5_cc_resolve() failed: %s",
663 krb5_get_err_text(kcontext, problem));
664 ret = HTTP_INTERNAL_SERVER_ERROR;
669 problem = krb5_cc_initialize(kcontext, tmp_ccache, princ);
671 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
672 "Cannot initialize krb5 ccache %s: krb5_cc_initialize() failed: %s",
673 ccname, krb5_get_err_text(kcontext, problem));
674 ret = HTTP_INTERNAL_SERVER_ERROR;
678 ap_table_setn(r->subprocess_env, "KRB5CCNAME", ccname);
679 ap_register_cleanup(r->pool, ccname,
680 krb5_cache_cleanup, ap_null_cleanup);
682 *ccache = tmp_ccache;
689 krb5_cc_destroy(kcontext, tmp_ccache);
695 store_krb5_creds(krb5_context kcontext,
697 kerb_auth_config *conf,
698 krb5_ccache delegated_cred)
701 krb5_error_code problem;
702 krb5_principal princ;
706 problem = krb5_cc_get_principal(kcontext, delegated_cred, &princ);
708 snprintf(errstr, sizeof(errstr), "krb5_cc_get_principal() failed: %s",
709 krb5_get_err_text(kcontext, problem));
710 return HTTP_INTERNAL_SERVER_ERROR;
713 ret = create_krb5_ccache(kcontext, r, conf, princ, &ccache);
715 krb5_free_principal(kcontext, princ);
720 problem = krb5_cc_copy_cache(kcontext, delegated_cred, ccache);
722 problem = krb5_cc_copy_creds(kcontext, delegated_cred, ccache);
724 krb5_free_principal(kcontext, princ);
726 snprintf(errstr, sizeof(errstr), "Failed to store credentials: %s",
727 krb5_get_err_text(kcontext, problem));
728 krb5_cc_destroy(kcontext, ccache);
729 return HTTP_INTERNAL_SERVER_ERROR;
732 krb5_cc_close(kcontext, ccache);
737 int authenticate_user_krb5pwd(request_rec *r,
738 kerb_auth_config *conf,
739 const char *auth_line)
741 const char *sent_pw = NULL;
742 const char *sent_name = NULL;
743 const char *realms = NULL;
744 krb5_context kcontext = NULL;
745 krb5_error_code code;
746 krb5_principal client = NULL;
747 krb5_ccache ccache = NULL;
748 krb5_keytab keytab = NULL;
751 int all_principals_unkown;
755 code = krb5_init_context(&kcontext);
757 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
758 "Cannot initialize Kerberos5 context (%d)", code);
759 return HTTP_INTERNAL_SERVER_ERROR;
762 sent_pw = ap_pbase64decode(r->pool, auth_line);
763 sent_name = ap_getword (r->pool, &sent_pw, ':');
764 /* do not allow user to override realm setting of server */
765 if (strchr(sent_name, '@')) {
766 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
767 "specifying realm in user name is prohibited");
768 ret = HTTP_UNAUTHORIZED;
772 if (sent_pw == NULL || *sent_pw == '\0') {
773 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
774 "empty passwords are not accepted");
775 ret = HTTP_UNAUTHORIZED;
779 code = krb5_cc_resolve(kcontext, "MEMORY:", &ccache);
781 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
782 "generating new memory ccache failed: %s",
783 krb5_get_err_text(kcontext, code));
784 ret = HTTP_INTERNAL_SERVER_ERROR;
789 if (conf->krb_5_keytab)
790 krb5_kt_resolve(kcontext, conf->krb_5_keytab, &keytab);
792 all_principals_unkown = 1;
793 realms = conf->krb_auth_realms;
795 if (realms && (code = krb5_set_default_realm(kcontext,
796 ap_getword_white(r->pool, &realms)))){
797 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
798 "krb5_set_default_realm() failed: %s",
799 krb5_get_err_text(kcontext, code));
804 krb5_free_principal(kcontext, client);
807 code = krb5_parse_name(kcontext, sent_name, &client);
809 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
810 "krb5_parse_name() failed: %s",
811 krb5_get_err_text(kcontext, code));
815 code = verify_krb5_user(r, kcontext, client, ccache, sent_pw,
816 conf->krb_service_name,
817 keytab, conf->krb_verify_kdc);
818 if (!conf->krb_authoritative && code) {
819 /* if we're not authoritative, we allow authentication to pass on
820 * to another modules if (and only if) the user is not known to us */
821 if (all_principals_unkown && code != KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
822 all_principals_unkown = 0;
828 /* ap_getword_white() used above shifts the parameter, so it's not
829 needed to touch the realms variable */
830 } while (realms && *realms);
832 memset((char *)sent_pw, 0, strlen(sent_pw));
835 if (!conf->krb_authoritative && all_principals_unkown == 1 && code == KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
838 ret = HTTP_UNAUTHORIZED;
843 code = krb5_unparse_name(kcontext, client, &name);
845 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "krb5_unparse_name() failed: %s",
846 krb5_get_err_text(kcontext, code));
847 ret = HTTP_UNAUTHORIZED;
850 MK_USER = ap_pstrdup (r->pool, name);
851 MK_AUTH_TYPE = "Basic";
854 if (conf->krb_save_credentials)
855 store_krb5_creds(kcontext, r, conf, ccache);
861 krb5_free_principal(kcontext, client);
863 krb5_cc_destroy(kcontext, ccache);
865 krb5_kt_close(kcontext, keytab);
866 krb5_free_context(kcontext);
871 /*********************************************************************
872 * GSSAPI Authentication
873 ********************************************************************/
876 get_gss_error(MK_POOL *p, OM_uint32 err_maj, OM_uint32 err_min, char *prefix)
878 OM_uint32 maj_stat, min_stat;
879 OM_uint32 msg_ctx = 0;
880 gss_buffer_desc status_string;
884 err_msg = ap_pstrdup(p, prefix);
886 maj_stat = gss_display_status (&min_stat,
892 if (GSS_ERROR(maj_stat))
894 err_msg = ap_pstrcat(p, err_msg, ": ", (char*) status_string.value, NULL);
895 gss_release_buffer(&min_stat, &status_string);
897 maj_stat = gss_display_status (&min_stat,
903 if (!GSS_ERROR(maj_stat)) {
904 err_msg = ap_pstrcat(p, err_msg,
905 " (", (char*) status_string.value, ")", NULL);
906 gss_release_buffer(&min_stat, &status_string);
908 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
914 store_gss_creds(request_rec *r, kerb_auth_config *conf, char *princ_name,
915 gss_cred_id_t delegated_cred)
917 OM_uint32 maj_stat, min_stat;
918 krb5_principal princ = NULL;
919 krb5_ccache ccache = NULL;
920 krb5_error_code problem;
921 krb5_context context;
922 int ret = HTTP_INTERNAL_SERVER_ERROR;
924 problem = krb5_init_context(&context);
926 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Cannot initialize krb5 context");
927 return HTTP_INTERNAL_SERVER_ERROR;
930 problem = krb5_parse_name(context, princ_name, &princ);
932 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
933 "Cannot parse delegated username (%s)", krb5_get_err_text(context, problem));
937 problem = create_krb5_ccache(context, r, conf, princ, &ccache);
939 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
940 "Cannot create krb5 ccache (%s)", krb5_get_err_text(context, problem));
944 maj_stat = gss_krb5_copy_ccache(&min_stat, delegated_cred, ccache);
945 if (GSS_ERROR(maj_stat)) {
946 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
947 "Cannot store delegated credential (%s)",
948 get_gss_error(r->pool, maj_stat, min_stat, "gss_krb5_copy_ccache"));
952 krb5_cc_close(context, ccache);
958 krb5_free_principal(context, princ);
960 krb5_cc_destroy(context, ccache);
961 krb5_free_context(context);
966 get_gss_creds(request_rec *r,
967 kerb_auth_config *conf,
968 gss_cred_id_t *server_creds)
970 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
971 OM_uint32 major_status, minor_status, minor_status2;
972 gss_name_t server_name = GSS_C_NO_NAME;
975 snprintf(buf, sizeof(buf), "%s@%s", conf->krb_service_name,
976 ap_get_server_name(r));
978 input_token.value = buf;
979 input_token.length = strlen(buf) + 1;
981 major_status = gss_import_name(&minor_status, &input_token,
982 GSS_C_NT_HOSTBASED_SERVICE,
984 if (GSS_ERROR(major_status)) {
985 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
986 "%s", get_gss_error(r->pool, major_status, minor_status,
987 "gss_import_name() failed"));
988 return HTTP_INTERNAL_SERVER_ERROR;
991 major_status = gss_acquire_cred(&minor_status, server_name, GSS_C_INDEFINITE,
992 GSS_C_NO_OID_SET, GSS_C_ACCEPT,
993 server_creds, NULL, NULL);
994 gss_release_name(&minor_status2, &server_name);
995 if (GSS_ERROR(major_status)) {
996 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
997 "%s", get_gss_error(r->pool, major_status, minor_status,
998 "gss_acquire_cred() failed"));
999 return HTTP_INTERNAL_SERVER_ERROR;
1004 * With MIT Kerberos 5 1.3.x the gss_cred_id_t is the same as
1005 * krb5_gss_cred_id_t and krb5_gss_cred_id_rec contains a pointer to
1007 * This allows us to override the replay cache function vector with
1009 * Note that this is a dirty hack to get things working and there may
1010 * well be unknown side-effects.
1012 if (memcmp(((krb5_gss_cred_id_t) *server_creds)->rcache->ops->type, "dfl", 3) == 0)
1013 /* Override the rcache operations */
1014 ((krb5_gss_cred_id_t) *server_creds)->rcache->ops = &mod_auth_kerb_rc_ops;
1017 /* rcache did not point to default rcache structure, return error */
1018 return HTTP_INTERNAL_SERVER_ERROR;
1026 cmp_gss_type(gss_buffer_t token, gss_OID oid)
1031 if (token->length == 0)
1032 return GSS_S_DEFECTIVE_TOKEN;
1034 /* XXX if (token->value == NTLMSSP) log_debug("NTLM mechanism used"); */
1038 return GSS_S_DEFECTIVE_TOKEN;
1041 if ((len & 0x7f) > 4)
1042 return GSS_S_DEFECTIVE_TOKEN;
1046 return GSS_S_DEFECTIVE_TOKEN;
1048 if (((OM_uint32) *p++) != oid->length)
1049 return GSS_S_DEFECTIVE_TOKEN;
1051 return memcmp(p, oid->elements, oid->length);
1055 authenticate_user_gss(request_rec *r, kerb_auth_config *conf,
1056 const char *auth_line, char **negotiate_ret_value)
1058 OM_uint32 major_status, minor_status, minor_status2;
1059 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
1060 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
1061 const char *auth_param = NULL;
1063 gss_name_t client_name = GSS_C_NO_NAME;
1064 gss_cred_id_t delegated_cred = GSS_C_NO_CREDENTIAL;
1065 OM_uint32 (*accept_sec_token)();
1066 gss_OID_desc spnego_oid;
1067 gss_ctx_id_t context = GSS_C_NO_CONTEXT;
1068 gss_cred_id_t server_creds = GSS_C_NO_CREDENTIAL;
1070 *negotiate_ret_value = "\0";
1072 spnego_oid.length = 6;
1073 spnego_oid.elements = (void *)"\x2b\x06\x01\x05\x05\x02";
1075 if (conf->krb_5_keytab) {
1077 /* we don't use the ap_* calls here, since the string passed to putenv()
1078 * will become part of the enviroment and shouldn't be free()ed by apache
1080 ktname = malloc(strlen("KRB5_KTNAME=") + strlen(conf->krb_5_keytab) + 1);
1081 if (ktname == NULL) {
1082 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "malloc() failed: not enough memory");
1083 ret = HTTP_INTERNAL_SERVER_ERROR;
1086 sprintf(ktname, "KRB5_KTNAME=%s", conf->krb_5_keytab);
1090 ret = get_gss_creds(r, conf, &server_creds);
1094 /* ap_getword() shifts parameter */
1095 auth_param = ap_getword_white(r->pool, &auth_line);
1096 if (auth_param == NULL) {
1097 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1098 "No Authorization parameter in request from client");
1099 ret = HTTP_UNAUTHORIZED;
1103 input_token.length = ap_base64decode_len(auth_param) + 1;
1104 input_token.value = ap_pcalloc(r->connection->pool, input_token.length);
1105 if (input_token.value == NULL) {
1106 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1107 "ap_pcalloc() failed (not enough memory)");
1108 ret = HTTP_INTERNAL_SERVER_ERROR;
1111 input_token.length = ap_base64decode(input_token.value, auth_param);
1113 accept_sec_token = (cmp_gss_type(&input_token, &spnego_oid) == 0) ?
1114 gss_accept_sec_context_spnego : gss_accept_sec_context;
1116 major_status = accept_sec_token(&minor_status,
1120 GSS_C_NO_CHANNEL_BINDINGS,
1127 if (output_token.length) {
1131 len = ap_base64encode_len(output_token.length) + 1;
1132 token = ap_pcalloc(r->connection->pool, len + 1);
1133 if (token == NULL) {
1134 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1135 "ap_pcalloc() failed (not enough memory)");
1136 ret = HTTP_INTERNAL_SERVER_ERROR;
1137 gss_release_buffer(&minor_status2, &output_token);
1140 ap_base64encode(token, output_token.value, output_token.length);
1142 *negotiate_ret_value = token;
1143 gss_release_buffer(&minor_status2, &output_token);
1146 if (GSS_ERROR(major_status)) {
1147 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1148 "%s", get_gss_error(r->pool, major_status, minor_status,
1149 "gss_accept_sec_context() failed"));
1150 /* Don't offer the Negotiate method again if call to GSS layer failed */
1151 *negotiate_ret_value = NULL;
1152 ret = HTTP_UNAUTHORIZED;
1157 /* This is a _Kerberos_ module so multiple authentication rounds aren't
1158 * supported. If we wanted a generic GSS authentication we would have to do
1159 * some magic with exporting context etc. */
1160 if (major_status & GSS_S_CONTINUE_NEEDED) {
1161 ret = HTTP_UNAUTHORIZED;
1166 major_status = gss_display_name(&minor_status, client_name, &output_token, NULL);
1167 gss_release_name(&minor_status, &client_name);
1168 if (GSS_ERROR(major_status)) {
1169 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1170 "%s", get_gss_error(r->pool, major_status, minor_status,
1171 "gss_export_name() failed"));
1172 ret = HTTP_INTERNAL_SERVER_ERROR;
1176 MK_AUTH_TYPE = "Negotiate";
1177 MK_USER = ap_pstrdup(r->pool, output_token.value);
1179 if (conf->krb_save_credentials && delegated_cred != GSS_C_NO_CREDENTIAL)
1180 store_gss_creds(r, conf, (char *)output_token.value, delegated_cred);
1182 if (*negotiate_ret_value)
1183 set_kerb_auth_headers(r, conf, 0, 0, *negotiate_ret_value);
1185 gss_release_buffer(&minor_status, &output_token);
1191 gss_release_cred(&minor_status, &delegated_cred);
1193 if (output_token.length)
1194 gss_release_buffer(&minor_status, &output_token);
1196 if (client_name != GSS_C_NO_NAME)
1197 gss_release_name(&minor_status, &client_name);
1199 if (server_creds != GSS_C_NO_CREDENTIAL)
1200 gss_release_cred(&minor_status, &server_creds);
1202 if (context != GSS_C_NO_CONTEXT)
1203 gss_delete_sec_context(&minor_status, &context, GSS_C_NO_BUFFER);
1210 already_succeeded(request_rec *r)
1212 if (ap_is_initial_req(r) || MK_AUTH_TYPE == NULL)
1214 if (strcmp(MK_AUTH_TYPE, "Negotiate") ||
1215 (strcmp(MK_AUTH_TYPE, "Basic") && strchr(MK_USER, '@')))
1221 set_kerb_auth_headers(request_rec *r, const kerb_auth_config *conf,
1222 int use_krb4, int use_krb5pwd, char *negotiate_ret_value)
1224 const char *auth_name = NULL;
1226 char *negoauth_param;
1227 const char *header_name =
1228 (r->proxyreq == PROXYREQ_PROXY) ? "Proxy-Authenticate" : "WWW-Authenticate";
1230 /* get the user realm specified in .htaccess */
1231 auth_name = ap_auth_name(r);
1233 /* XXX should the WWW-Authenticate header be cleared first? */
1235 if (negotiate_ret_value != NULL && conf->krb_method_gssapi) {
1236 negoauth_param = (*negotiate_ret_value == '\0') ? "Negotiate" :
1237 ap_pstrcat(r->pool, "Negotiate ", negotiate_ret_value, NULL);
1238 ap_table_add(r->err_headers_out, header_name, negoauth_param);
1240 if (use_krb5pwd && conf->krb_method_k5pass) {
1241 ap_table_add(r->err_headers_out, header_name,
1242 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1248 if (use_krb4 && conf->krb_method_k4pass && !set_basic)
1249 ap_table_add(r->err_headers_out, header_name,
1250 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1254 int kerb_authenticate_user(request_rec *r)
1256 kerb_auth_config *conf =
1257 (kerb_auth_config *) ap_get_module_config(r->per_dir_config,
1259 const char *auth_type = NULL;
1260 const char *auth_line = NULL;
1261 const char *type = NULL;
1262 int use_krb5 = 0, use_krb4 = 0;
1264 static int last_return = HTTP_UNAUTHORIZED;
1265 char *negotiate_ret_value = NULL;
1267 /* get the type specified in .htaccess */
1268 type = ap_auth_type(r);
1270 if (type && strcasecmp(type, "Kerberos") == 0)
1271 use_krb5 = use_krb4 = 1;
1272 else if(type && strcasecmp(type, "KerberosV5") == 0)
1274 else if(type && strcasecmp(type, "KerberosV4") == 0)
1279 /* get what the user sent us in the HTTP header */
1280 auth_line = MK_TABLE_GET(r->headers_in, "Authorization");
1282 auth_line = MK_TABLE_GET(r->headers_in, "Proxy-Authorization");
1284 set_kerb_auth_headers(r, conf, use_krb4, use_krb5,
1285 (use_krb5) ? "\0" : NULL);
1286 return HTTP_UNAUTHORIZED;
1289 auth_type = ap_getword_white(r->pool, &auth_line);
1291 if (already_succeeded(r))
1294 ret = HTTP_UNAUTHORIZED;
1297 if (use_krb5 && conf->krb_method_gssapi &&
1298 strcasecmp(auth_type, "Negotiate") == 0) {
1299 ret = authenticate_user_gss(r, conf, auth_line, &negotiate_ret_value);
1300 } else if (use_krb5 && conf->krb_method_k5pass &&
1301 strcasecmp(auth_type, "Basic") == 0) {
1302 ret = authenticate_user_krb5pwd(r, conf, auth_line);
1307 if (ret == HTTP_UNAUTHORIZED && use_krb4 && conf->krb_method_k4pass &&
1308 strcasecmp(auth_type, "Basic") == 0)
1309 ret = authenticate_user_krb4pwd(r, conf, auth_line);
1312 if (ret == HTTP_UNAUTHORIZED)
1313 set_kerb_auth_headers(r, conf, use_krb4, use_krb5, negotiate_ret_value);
1315 /* XXX log_debug: if ret==OK, log(user XY authenticated) */
1322 /***************************************************************************
1323 Module Setup/Configuration
1324 ***************************************************************************/
1325 #ifndef STANDARD20_MODULE_STUFF
1326 module MODULE_VAR_EXPORT auth_kerb_module = {
1327 STANDARD_MODULE_STUFF,
1328 NULL, /* module initializer */
1329 kerb_dir_create_config, /* per-directory config creator */
1330 NULL, /* per-directory config merger */
1331 NULL, /* per-server config creator */
1332 NULL, /* per-server config merger */
1333 kerb_auth_cmds, /* command table */
1334 NULL, /* [ 9] content handlers */
1335 NULL, /* [ 2] URI-to-filename translation */
1336 kerb_authenticate_user, /* [ 5] check/validate user_id */
1337 NULL, /* [ 6] check user_id is valid *here* */
1338 NULL, /* [ 4] check access by host address */
1339 NULL, /* [ 7] MIME type checker/setter */
1340 NULL, /* [ 8] fixups */
1341 NULL, /* [10] logger */
1342 NULL, /* [ 3] header parser */
1343 NULL, /* process initialization */
1344 NULL, /* process exit/cleanup */
1345 NULL /* [ 1] post read_request handling */
1349 kerb_init_handler(apr_pool_t *p, apr_pool_t *plog,
1350 apr_pool_t *ptemp, server_rec *s)
1352 ap_add_version_component(p, "mod_auth_kerb/" MODAUTHKERB_VERSION);
1356 void kerb_register_hooks(apr_pool_t *p)
1358 ap_hook_post_config(kerb_init_handler, NULL, NULL, APR_HOOK_MIDDLE);
1359 ap_hook_check_user_id(kerb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
1362 module AP_MODULE_DECLARE_DATA auth_kerb_module =
1364 STANDARD20_MODULE_STUFF,
1365 kerb_dir_create_config, /* create per-dir conf structures */
1366 NULL, /* merge per-dir conf structures */
1367 NULL, /* create per-server conf structures */
1368 NULL, /* merge per-server conf structures */
1369 kerb_auth_cmds, /* table of configuration directives */
1370 kerb_register_hooks /* register hooks */