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>
61 #include <apr_base64.h>
69 # include <gssapi/gssapi.h>
70 # include <gssapi/gssapi_generic.h>
71 # include <gssapi/gssapi_krb5.h>
72 # define GSS_C_NT_USER_NAME gss_nt_user_name
73 # define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
74 # define krb5_get_err_text(context,code) error_message(code)
76 #include "spnegokrb5.h"
80 /* Prevent warning about closesocket redefinition (Apache's ap_config.h and
81 * MIT Kerberos' port-sockets.h both define it as close) */
86 #include <netdb.h> /* gethostbyname() */
89 /* XXX remove dependency on unistd.h ??? */
92 #ifdef STANDARD20_MODULE_STUFF
93 module AP_MODULE_DECLARE_DATA auth_kerb_module;
95 module auth_kerb_module;
98 /***************************************************************************
99 Macros To Ease Compatibility
100 ***************************************************************************/
101 #ifdef STANDARD20_MODULE_STUFF
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
108 #define MK_TABLE_GET ap_table_get
109 #define MK_USER r->connection->user
110 #define MK_AUTH_TYPE r->connection->ap_auth_type
111 #define PROXYREQ_PROXY STD_PROXY
114 /***************************************************************************
115 Auth Configuration Structure
116 ***************************************************************************/
118 char *krb_auth_realms;
119 int krb_save_credentials;
121 char *krb_service_name;
122 int krb_authoritative;
125 int krb_method_gssapi;
126 int krb_method_k5pass;
130 int krb_method_k4pass;
135 set_kerb_auth_headers(request_rec *r, const kerb_auth_config *conf,
136 int use_krb4, int use_krb5pwd, char *negotiate_ret_value);
139 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg);
141 #ifdef STANDARD20_MODULE_STUFF
142 #define command(name, func, var, type, usage) \
143 AP_INIT_ ## type (name, func, \
144 (void*)APR_XtOffsetOf(kerb_auth_config, var), \
147 #define command(name, func, var, type, usage) \
149 (void*)XtOffsetOf(kerb_auth_config, var), \
150 OR_AUTHCFG, type, usage }
153 static const command_rec kerb_auth_cmds[] = {
154 command("KrbAuthRealms", krb5_save_realms, krb_auth_realms,
155 RAW_ARGS, "Realms to attempt authentication against (can be multiple)."),
157 command("KrbAuthRealm", krb5_save_realms, krb_auth_realms,
158 RAW_ARGS, "Alias for KrbAuthRealms."),
160 command("KrbSaveCredentials", ap_set_flag_slot, krb_save_credentials,
161 FLAG, "Save and store credentials/tickets retrieved during auth."),
163 command("KrbVerifyKDC", ap_set_flag_slot, krb_verify_kdc,
164 FLAG, "Verify tickets against keytab to prevent KDC spoofing attacks."),
166 command("KrbServiceName", ap_set_string_slot, krb_service_name,
167 TAKE1, "Service name to be used by Apache for authentication."),
169 command("KrbAuthoritative", ap_set_flag_slot, krb_authoritative,
170 FLAG, "Set to 'off' to allow access control to be passed along to lower modules if the UserID is not known to this module."),
173 command("Krb5Keytab", ap_set_file_slot, krb_5_keytab,
174 TAKE1, "Location of Kerberos V5 keytab file."),
176 command("KrbMethodNegotiate", ap_set_flag_slot, krb_method_gssapi,
177 FLAG, "Enable Negotiate authentication method."),
179 command("KrbMethodK5Passwd", ap_set_flag_slot, krb_method_k5pass,
180 FLAG, "Enable Kerberos V5 password authentication."),
184 command("Krb4Srvtab", ap_set_file_slot, krb_4_srvtab,
185 TAKE1, "Location of Kerberos V4 srvtab file."),
187 command("KrbMethodK4Passwd", ap_set_flag_slot, krb_method_k4pass,
188 FLAG, "Enable Kerberos V4 password authentication."),
194 #if defined(KRB5) && !defined(HEIMDAL)
195 /* Needed to work around problems with replay caches */
196 #include "mit-internals.h"
198 /* This is our replacement krb5_rc_store function */
199 static krb5_error_code
200 mod_auth_kerb_rc_store(krb5_context context, krb5_rcache rcache,
201 krb5_donot_replay *donot_replay)
206 /* And this is the operations vector for our replay cache */
207 const krb5_rc_ops mod_auth_kerb_rc_ops = {
214 mod_auth_kerb_rc_store,
216 krb5_rc_dfl_get_span,
217 krb5_rc_dfl_get_name,
223 /***************************************************************************
224 Auth Configuration Initialization
225 ***************************************************************************/
226 static void *kerb_dir_create_config(MK_POOL *p, char *d)
228 kerb_auth_config *rec;
230 rec = (kerb_auth_config *) ap_pcalloc(p, sizeof(kerb_auth_config));
231 ((kerb_auth_config *)rec)->krb_verify_kdc = 1;
232 ((kerb_auth_config *)rec)->krb_service_name = "HTTP";
233 ((kerb_auth_config *)rec)->krb_authoritative = 1;
235 ((kerb_auth_config *)rec)->krb_method_k5pass = 1;
236 ((kerb_auth_config *)rec)->krb_method_gssapi = 1;
239 ((kerb_auth_config *)rec)->krb_method_k4pass = 1;
245 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg)
247 sec->krb_auth_realms= ap_pstrdup(cmd->pool, arg);
251 void log_rerror(const char *file, int line, int level, int status,
252 const request_rec *r, const char *fmt, ...)
258 vsnprintf(errstr, sizeof(errstr), fmt, ap);
262 #ifdef STANDARD20_MODULE_STUFF
263 ap_log_rerror(file, line, level | APLOG_NOERRNO, status, r, "%s", errstr);
265 ap_log_rerror(file, line, level | APLOG_NOERRNO, r, "%s", errstr);
270 /***************************************************************************
271 Username/Password Validation for Krb4
272 ***************************************************************************/
274 verify_krb4_user(request_rec *r, char *name, char *instance, char *realm,
275 char *password, char *linstance, char *srvtab, int krb_verify_kdc)
281 const char *hostname;
284 char lrealm[REALM_SZ];
286 ret = krb_get_pw_in_tkt(name, instance, realm, "krbtgt", realm,
287 DEFAULT_TKT_LIFE, password);
289 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
290 "Cannot get krb4 ticket: krb_get_pw_in_tkt() failed: %s",
291 krb_get_err_text(ret));
298 hostname = ap_get_server_name(r);
300 hp = gethostbyname(hostname);
303 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
304 "Cannot verify krb4 ticket: gethostbyname() failed: %s",
308 memcpy(&addr, hp->h_addr, sizeof(addr));
310 phost = krb_get_phost((char *)hostname);
312 krb_get_lrealm(lrealm, 1);
314 ret = krb_mk_req(&ticket, linstance, phost, lrealm, 0);
316 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
317 "Cannot verify krb4 ticket: krb_mk_req() failed: %s",
318 krb_get_err_text(ret));
323 ret = krb_rd_req(&ticket, linstance, phost, addr, &authdata, srvtab);
325 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
326 "Cannot verify krb4 ticket: krb_rd_req() failed: %s",
327 krb_get_err_text(ret));
335 krb4_cache_cleanup(void *data)
337 char *tkt_file = (char *) data;
339 krb_set_tkt_string(tkt_file);
345 authenticate_user_krb4pwd(request_rec *r,
346 kerb_auth_config *conf,
347 const char *auth_line)
351 const char *sent_name;
354 char *tkt_file_p = NULL;
359 char lrealm[REALM_SZ];
360 int all_principals_unkown;
362 sent_pw = ap_pbase64decode(r->pool, auth_line);
363 sent_name = ap_getword (r->pool, &sent_pw, ':');
365 /* do not allow user to override realm setting of server */
366 if (strchr(sent_name, '@')) {
367 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
368 "specifying realm in user name is prohibited");
369 return HTTP_UNAUTHORIZED;
372 sent_instance = strchr(sent_name, '.');
374 *sent_instance++ = '\0';
376 snprintf(tkt_file, sizeof(tkt_file), "/tmp/apache_tkt_XXXXXX");
377 fd = mkstemp(tkt_file);
379 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
380 "Cannot create krb4 ccache: mkstemp() failed: %s",
382 return HTTP_INTERNAL_SERVER_ERROR;
385 tkt_file_p = ap_pstrdup(r->pool, tkt_file);
386 ap_register_cleanup(r->pool, tkt_file_p,
387 krb4_cache_cleanup, ap_null_cleanup);
389 krb_set_tkt_string(tkt_file);
391 all_principals_unkown = 1;
392 realms = conf->krb_auth_realms;
394 memset(lrealm, 0, sizeof(lrealm));
397 realm = ap_getword_white(r->pool, &realms);
400 ret = krb_get_lrealm(lrealm, 1);
406 ret = verify_krb4_user(r, (char *)sent_name,
407 (sent_instance) ? sent_instance : "",
408 (char *)realm, (char *)sent_pw,
409 conf->krb_service_name,
410 conf->krb_4_srvtab, conf->krb_verify_kdc);
411 if (!conf->krb_authoritative && ret) {
412 /* if we're not authoritative, we allow authentication to pass on
413 * to another modules if (and only if) the user is not known to us */
414 if (all_principals_unkown && ret != KDC_PR_UNKNOWN)
415 all_principals_unkown = 0;
420 } while (realms && *realms);
423 /* XXX log only in the verify_krb4_user() call */
424 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Verifying krb4 password failed");
425 ret = (!conf->krb_authoritative && all_principals_unkown == 1 && ret == KDC_PR_UNKNOWN) ?
426 DECLINED : HTTP_UNAUTHORIZED;
430 user = ap_pstrdup(r->pool, sent_name);
432 user = ap_pstrcat(r->pool, user, ".", sent_instance, NULL);
433 user = ap_pstrcat(r->pool, user, "@", realm, NULL);
436 MK_AUTH_TYPE = "Basic";
437 ap_table_setn(r->subprocess_env, "KRBTKFILE", tkt_file_p);
439 if (!conf->krb_save_credentials)
440 krb4_cache_cleanup(tkt_file);
444 krb4_cache_cleanup(tkt_file);
453 /***************************************************************************
454 Username/Password Validation for Krb5
455 ***************************************************************************/
457 /* MIT kerberos uses replay cache checks even during credential verification
458 * (i.e. in krb5_verify_init_creds()), which is obviosuly useless. In order to
459 * avoid problems with multiple apache processes accessing the same rcache file
460 * we had to use this call instead, which is only a bit modified version of
461 * krb5_verify_init_creds() */
462 static krb5_error_code
463 verify_krb5_init_creds(krb5_context context, krb5_creds *creds,
464 krb5_principal ap_req_server, krb5_keytab ap_req_keytab)
468 krb5_ccache local_ccache = NULL;
469 krb5_creds *new_creds = NULL;
470 krb5_auth_context auth_context = NULL;
471 krb5_keytab keytab = NULL;
473 memset(&req, 0, sizeof(req));
475 if (ap_req_keytab == NULL) {
476 ret = krb5_kt_default (context, &keytab);
480 keytab = ap_req_keytab;
482 ret = krb5_cc_resolve(context, "MEMORY:", &local_ccache);
486 ret = krb5_cc_initialize(context, local_ccache, creds->client);
490 ret = krb5_cc_store_cred (context, local_ccache, creds);
494 if (!krb5_principal_compare (context, ap_req_server, creds->server)) {
495 krb5_creds match_cred;
497 memset (&match_cred, 0, sizeof(match_cred));
499 match_cred.client = creds->client;
500 match_cred.server = ap_req_server;
502 ret = krb5_get_credentials (context, 0, local_ccache,
503 &match_cred, &new_creds);
509 ret = krb5_mk_req_extended (context, &auth_context, 0, NULL, creds, &req);
513 krb5_auth_con_free (context, auth_context);
515 ret = krb5_auth_con_init(context, &auth_context);
518 /* use KRB5_AUTH_CONTEXT_DO_SEQUENCE to skip replay cache checks */
519 krb5_auth_con_setflags(context, auth_context, KRB5_AUTH_CONTEXT_DO_SEQUENCE);
521 ret = krb5_rd_req (context, &auth_context, &req, ap_req_server,
525 krb5_free_data_contents(context, &req);
527 krb5_auth_con_free (context, auth_context);
529 krb5_free_creds (context, new_creds);
530 if (ap_req_keytab == NULL && keytab)
531 krb5_kt_close (context, keytab);
533 krb5_cc_destroy (context, local_ccache);
538 /* Inspired by krb5_verify_user from Heimdal */
539 static krb5_error_code
540 verify_krb5_user(request_rec *r, krb5_context context, krb5_principal principal,
541 const char *password, const char *service, krb5_keytab keytab,
542 int krb_verify_kdc, krb5_ccache *ccache)
545 krb5_principal server = NULL;
547 krb5_ccache ret_ccache = NULL;
549 /* XXX error messages shouldn't be logged here (and in the while() loop in
550 * authenticate_user_krb5pwd() as weell), in order to avoid confusing log
551 * entries when using multiple realms */
553 memset(&creds, 0, sizeof(creds));
555 ret = krb5_get_init_creds_password(context, &creds, principal,
556 (char *)password, NULL,
557 NULL, 0, NULL, NULL);
559 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
560 "krb5_get_init_creds_password() failed: %s",
561 krb5_get_err_text(context, ret));
565 ret = krb5_sname_to_principal(context, ap_get_server_name(r), service,
566 KRB5_NT_UNKNOWN, &server);
568 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
569 "krb5_sname_to_principal() failed: %s",
570 krb5_get_err_text(context, ret));
573 /* XXX log_debug: lookig for <server_princ> in keytab */
579 krb5_get_default_realm(context, &realm);
580 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
581 "trying to verify password using key for %s/%s@%s",
582 service, ap_get_server_name(r), realm);
586 if (krb_verify_kdc &&
587 (ret = verify_krb5_init_creds(context, &creds, server, keytab))) {
588 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
589 "failed to verify krb5 credentials: %s",
590 krb5_get_err_text(context, ret));
594 ret = krb5_cc_resolve(context, "MEMORY:", &ret_ccache);
596 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
597 "generating new memory ccache failed: %s",
598 krb5_get_err_text(kcontext, ret));
602 ret = krb5_cc_initialize(context, ret_ccache, principal);
604 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
605 "krb5_cc_initialize() failed: %s",
606 krb5_get_err_text(context, ret));
610 ret = krb5_cc_store_cred(context, ret_ccache, &creds);
612 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
613 "krb5_cc_store_cred() failed: %s",
614 krb5_get_err_text(context, ret));
617 *ccache = ret_ccache;
621 krb5_free_cred_contents(context, &creds);
623 krb5_free_principal(context, server);
625 krb5_cc_destroy(context, ret_ccache);
631 krb5_cache_cleanup(void *data)
633 krb5_context context;
635 krb5_error_code problem;
636 char *cache_name = (char *) data;
638 problem = krb5_init_context(&context);
640 /* ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "krb5_init_context() failed"); */
641 return HTTP_INTERNAL_SERVER_ERROR;
644 problem = krb5_cc_resolve(context, cache_name, &cache);
646 /* log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
647 "krb5_cc_resolve() failed (%s: %s)",
648 cache_name, krb5_get_err_text(context, problem)); */
649 return HTTP_INTERNAL_SERVER_ERROR;
652 krb5_cc_destroy(context, cache);
653 krb5_free_context(context);
658 create_krb5_ccache(krb5_context kcontext,
660 kerb_auth_config *conf,
661 krb5_principal princ,
666 krb5_error_code problem;
668 krb5_ccache tmp_ccache = NULL;
670 ccname = ap_psprintf(r->pool, "FILE:%s/krb5cc_apache_XXXXXX", P_tmpdir);
671 fd = mkstemp(ccname + strlen("FILE:"));
673 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
674 "mkstemp() failed: %s", strerror(errno));
675 ret = HTTP_INTERNAL_SERVER_ERROR;
680 problem = krb5_cc_resolve(kcontext, ccname, &tmp_ccache);
682 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
683 "krb5_cc_resolve() failed: %s",
684 krb5_get_err_text(kcontext, problem));
685 ret = HTTP_INTERNAL_SERVER_ERROR;
690 problem = krb5_cc_initialize(kcontext, tmp_ccache, princ);
692 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
693 "Cannot initialize krb5 ccache %s: krb5_cc_initialize() failed: %s",
694 ccname, krb5_get_err_text(kcontext, problem));
695 ret = HTTP_INTERNAL_SERVER_ERROR;
699 ap_table_setn(r->subprocess_env, "KRB5CCNAME", ccname);
700 ap_register_cleanup(r->pool, ccname,
701 krb5_cache_cleanup, ap_null_cleanup);
703 *ccache = tmp_ccache;
710 krb5_cc_destroy(kcontext, tmp_ccache);
716 store_krb5_creds(krb5_context kcontext,
718 kerb_auth_config *conf,
719 krb5_ccache delegated_cred)
722 krb5_error_code problem;
723 krb5_principal princ;
727 problem = krb5_cc_get_principal(kcontext, delegated_cred, &princ);
729 snprintf(errstr, sizeof(errstr), "krb5_cc_get_principal() failed: %s",
730 krb5_get_err_text(kcontext, problem));
731 return HTTP_INTERNAL_SERVER_ERROR;
734 ret = create_krb5_ccache(kcontext, r, conf, princ, &ccache);
736 krb5_free_principal(kcontext, princ);
741 problem = krb5_cc_copy_cache(kcontext, delegated_cred, ccache);
743 problem = krb5_cc_copy_creds(kcontext, delegated_cred, ccache);
745 krb5_free_principal(kcontext, princ);
747 snprintf(errstr, sizeof(errstr), "Failed to store credentials: %s",
748 krb5_get_err_text(kcontext, problem));
749 krb5_cc_destroy(kcontext, ccache);
750 return HTTP_INTERNAL_SERVER_ERROR;
753 krb5_cc_close(kcontext, ccache);
758 int authenticate_user_krb5pwd(request_rec *r,
759 kerb_auth_config *conf,
760 const char *auth_line)
762 const char *sent_pw = NULL;
763 const char *sent_name = NULL;
764 const char *realms = NULL;
765 krb5_context kcontext = NULL;
766 krb5_error_code code;
767 krb5_principal client = NULL;
768 krb5_ccache ccache = NULL;
769 krb5_keytab keytab = NULL;
772 int all_principals_unkown;
774 code = krb5_init_context(&kcontext);
776 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
777 "Cannot initialize Kerberos5 context (%d)", code);
778 return HTTP_INTERNAL_SERVER_ERROR;
781 sent_pw = ap_pbase64decode(r->pool, auth_line);
782 sent_name = ap_getword (r->pool, &sent_pw, ':');
783 /* do not allow user to override realm setting of server */
784 if (strchr(sent_name, '@')) {
785 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
786 "specifying realm in user name is prohibited");
787 ret = HTTP_UNAUTHORIZED;
791 if (sent_pw == NULL || *sent_pw == '\0') {
792 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
793 "empty passwords are not accepted");
794 ret = HTTP_UNAUTHORIZED;
798 if (conf->krb_5_keytab)
799 krb5_kt_resolve(kcontext, conf->krb_5_keytab, &keytab);
801 all_principals_unkown = 1;
802 realms = conf->krb_auth_realms;
804 if (realms && (code = krb5_set_default_realm(kcontext,
805 ap_getword_white(r->pool, &realms)))){
806 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
807 "krb5_set_default_realm() failed: %s",
808 krb5_get_err_text(kcontext, code));
813 krb5_free_principal(kcontext, client);
816 code = krb5_parse_name(kcontext, sent_name, &client);
818 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
819 "krb5_parse_name() failed: %s",
820 krb5_get_err_text(kcontext, code));
824 code = verify_krb5_user(r, kcontext, client, sent_pw,
825 conf->krb_service_name,
826 keytab, conf->krb_verify_kdc, &ccache);
827 if (!conf->krb_authoritative && code) {
828 /* if we're not authoritative, we allow authentication to pass on
829 * to another modules if (and only if) the user is not known to us */
830 if (all_principals_unkown && code != KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
831 all_principals_unkown = 0;
837 /* ap_getword_white() used above shifts the parameter, so it's not
838 needed to touch the realms variable */
839 } while (realms && *realms);
841 memset((char *)sent_pw, 0, strlen(sent_pw));
844 if (!conf->krb_authoritative && all_principals_unkown == 1 && code == KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
847 ret = HTTP_UNAUTHORIZED;
852 code = krb5_unparse_name(kcontext, client, &name);
854 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "krb5_unparse_name() failed: %s",
855 krb5_get_err_text(kcontext, code));
856 ret = HTTP_UNAUTHORIZED;
859 MK_USER = ap_pstrdup (r->pool, name);
860 MK_AUTH_TYPE = "Basic";
863 if (conf->krb_save_credentials)
864 store_krb5_creds(kcontext, r, conf, ccache);
870 krb5_free_principal(kcontext, client);
872 krb5_cc_destroy(kcontext, ccache);
874 krb5_kt_close(kcontext, keytab);
875 krb5_free_context(kcontext);
880 /*********************************************************************
881 * GSSAPI Authentication
882 ********************************************************************/
885 get_gss_error(MK_POOL *p, OM_uint32 err_maj, OM_uint32 err_min, char *prefix)
887 OM_uint32 maj_stat, min_stat;
888 OM_uint32 msg_ctx = 0;
889 gss_buffer_desc status_string;
892 err_msg = ap_pstrdup(p, prefix);
894 maj_stat = gss_display_status (&min_stat,
900 if (GSS_ERROR(maj_stat))
902 err_msg = ap_pstrcat(p, err_msg, ": ", (char*) status_string.value, NULL);
903 gss_release_buffer(&min_stat, &status_string);
905 maj_stat = gss_display_status (&min_stat,
911 if (!GSS_ERROR(maj_stat)) {
912 err_msg = ap_pstrcat(p, err_msg,
913 " (", (char*) status_string.value, ")", NULL);
914 gss_release_buffer(&min_stat, &status_string);
916 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
922 store_gss_creds(request_rec *r, kerb_auth_config *conf, char *princ_name,
923 gss_cred_id_t delegated_cred)
925 OM_uint32 maj_stat, min_stat;
926 krb5_principal princ = NULL;
927 krb5_ccache ccache = NULL;
928 krb5_error_code problem;
929 krb5_context context;
930 int ret = HTTP_INTERNAL_SERVER_ERROR;
932 problem = krb5_init_context(&context);
934 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Cannot initialize krb5 context");
935 return HTTP_INTERNAL_SERVER_ERROR;
938 problem = krb5_parse_name(context, princ_name, &princ);
940 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
941 "Cannot parse delegated username (%s)", krb5_get_err_text(context, problem));
945 problem = create_krb5_ccache(context, r, conf, princ, &ccache);
947 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
948 "Cannot create krb5 ccache (%s)", krb5_get_err_text(context, problem));
952 maj_stat = gss_krb5_copy_ccache(&min_stat, delegated_cred, ccache);
953 if (GSS_ERROR(maj_stat)) {
954 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
955 "Cannot store delegated credential (%s)",
956 get_gss_error(r->pool, maj_stat, min_stat, "gss_krb5_copy_ccache"));
960 krb5_cc_close(context, ccache);
966 krb5_free_principal(context, princ);
968 krb5_cc_destroy(context, ccache);
969 krb5_free_context(context);
974 get_gss_creds(request_rec *r,
975 kerb_auth_config *conf,
976 gss_cred_id_t *server_creds)
978 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
979 OM_uint32 major_status, minor_status, minor_status2;
980 gss_name_t server_name = GSS_C_NO_NAME;
983 snprintf(buf, sizeof(buf), "%s@%s", conf->krb_service_name,
984 ap_get_server_name(r));
986 input_token.value = buf;
987 input_token.length = strlen(buf) + 1;
989 major_status = gss_import_name(&minor_status, &input_token,
990 GSS_C_NT_HOSTBASED_SERVICE,
992 if (GSS_ERROR(major_status)) {
993 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
994 "%s", get_gss_error(r->pool, major_status, minor_status,
995 "gss_import_name() failed"));
996 return HTTP_INTERNAL_SERVER_ERROR;
999 major_status = gss_acquire_cred(&minor_status, server_name, GSS_C_INDEFINITE,
1000 GSS_C_NO_OID_SET, GSS_C_ACCEPT,
1001 server_creds, NULL, NULL);
1002 gss_release_name(&minor_status2, &server_name);
1003 if (GSS_ERROR(major_status)) {
1004 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1005 "%s", get_gss_error(r->pool, major_status, minor_status,
1006 "gss_acquire_cred() failed"));
1007 return HTTP_INTERNAL_SERVER_ERROR;
1012 * With MIT Kerberos 5 1.3.x the gss_cred_id_t is the same as
1013 * krb5_gss_cred_id_t and krb5_gss_cred_id_rec contains a pointer to
1015 * This allows us to override the replay cache function vector with
1017 * Note that this is a dirty hack to get things working and there may
1018 * well be unknown side-effects.
1021 krb5_gss_cred_id_t gss_creds = (krb5_gss_cred_id_t) *server_creds;
1023 if (gss_creds && gss_creds->rcache && gss_creds->ops->type &&
1024 memcmp(gss_creds->rcache->ops->type, "dfl", 3) == 0)
1025 /* Override the rcache operations */
1026 gss_creds->rcache->ops = &mod_auth_kerb_rc_ops;
1034 cmp_gss_type(gss_buffer_t token, gss_OID oid)
1039 if (token->length == 0)
1040 return GSS_S_DEFECTIVE_TOKEN;
1042 /* XXX if (token->value == NTLMSSP) log_debug("NTLM mechanism used"); */
1046 return GSS_S_DEFECTIVE_TOKEN;
1049 if ((len & 0x7f) > 4)
1050 return GSS_S_DEFECTIVE_TOKEN;
1054 return GSS_S_DEFECTIVE_TOKEN;
1056 if (((OM_uint32) *p++) != oid->length)
1057 return GSS_S_DEFECTIVE_TOKEN;
1059 return memcmp(p, oid->elements, oid->length);
1063 authenticate_user_gss(request_rec *r, kerb_auth_config *conf,
1064 const char *auth_line, char **negotiate_ret_value)
1066 OM_uint32 major_status, minor_status, minor_status2;
1067 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
1068 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
1069 const char *auth_param = NULL;
1071 gss_name_t client_name = GSS_C_NO_NAME;
1072 gss_cred_id_t delegated_cred = GSS_C_NO_CREDENTIAL;
1073 OM_uint32 (*accept_sec_token)();
1074 gss_OID_desc spnego_oid;
1075 gss_ctx_id_t context = GSS_C_NO_CONTEXT;
1076 gss_cred_id_t server_creds = GSS_C_NO_CREDENTIAL;
1078 *negotiate_ret_value = "\0";
1080 spnego_oid.length = 6;
1081 spnego_oid.elements = (void *)"\x2b\x06\x01\x05\x05\x02";
1083 if (conf->krb_5_keytab) {
1085 /* we don't use the ap_* calls here, since the string passed to putenv()
1086 * will become part of the enviroment and shouldn't be free()ed by apache
1088 ktname = malloc(strlen("KRB5_KTNAME=") + strlen(conf->krb_5_keytab) + 1);
1089 if (ktname == NULL) {
1090 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "malloc() failed: not enough memory");
1091 ret = HTTP_INTERNAL_SERVER_ERROR;
1094 sprintf(ktname, "KRB5_KTNAME=%s", conf->krb_5_keytab);
1098 ret = get_gss_creds(r, conf, &server_creds);
1102 /* ap_getword() shifts parameter */
1103 auth_param = ap_getword_white(r->pool, &auth_line);
1104 if (auth_param == NULL) {
1105 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1106 "No Authorization parameter in request from client");
1107 ret = HTTP_UNAUTHORIZED;
1111 input_token.length = ap_base64decode_len(auth_param) + 1;
1112 input_token.value = ap_pcalloc(r->connection->pool, input_token.length);
1113 if (input_token.value == NULL) {
1114 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1115 "ap_pcalloc() failed (not enough memory)");
1116 ret = HTTP_INTERNAL_SERVER_ERROR;
1119 input_token.length = ap_base64decode(input_token.value, auth_param);
1121 accept_sec_token = (cmp_gss_type(&input_token, &spnego_oid) == 0) ?
1122 gss_accept_sec_context_spnego : gss_accept_sec_context;
1124 major_status = accept_sec_token(&minor_status,
1128 GSS_C_NO_CHANNEL_BINDINGS,
1135 if (output_token.length) {
1139 len = ap_base64encode_len(output_token.length) + 1;
1140 token = ap_pcalloc(r->connection->pool, len + 1);
1141 if (token == NULL) {
1142 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1143 "ap_pcalloc() failed (not enough memory)");
1144 ret = HTTP_INTERNAL_SERVER_ERROR;
1145 gss_release_buffer(&minor_status2, &output_token);
1148 ap_base64encode(token, output_token.value, output_token.length);
1150 *negotiate_ret_value = token;
1151 gss_release_buffer(&minor_status2, &output_token);
1154 if (GSS_ERROR(major_status)) {
1155 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1156 "%s", get_gss_error(r->pool, major_status, minor_status,
1157 "gss_accept_sec_context() failed"));
1158 /* Don't offer the Negotiate method again if call to GSS layer failed */
1159 *negotiate_ret_value = NULL;
1160 ret = HTTP_UNAUTHORIZED;
1165 /* This is a _Kerberos_ module so multiple authentication rounds aren't
1166 * supported. If we wanted a generic GSS authentication we would have to do
1167 * some magic with exporting context etc. */
1168 if (major_status & GSS_S_CONTINUE_NEEDED) {
1169 ret = HTTP_UNAUTHORIZED;
1174 major_status = gss_display_name(&minor_status, client_name, &output_token, NULL);
1175 gss_release_name(&minor_status, &client_name);
1176 if (GSS_ERROR(major_status)) {
1177 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1178 "%s", get_gss_error(r->pool, major_status, minor_status,
1179 "gss_export_name() failed"));
1180 ret = HTTP_INTERNAL_SERVER_ERROR;
1184 MK_AUTH_TYPE = "Negotiate";
1185 MK_USER = ap_pstrdup(r->pool, output_token.value);
1187 if (conf->krb_save_credentials && delegated_cred != GSS_C_NO_CREDENTIAL)
1188 store_gss_creds(r, conf, (char *)output_token.value, delegated_cred);
1190 if (*negotiate_ret_value)
1191 set_kerb_auth_headers(r, conf, 0, 0, *negotiate_ret_value);
1193 gss_release_buffer(&minor_status, &output_token);
1199 gss_release_cred(&minor_status, &delegated_cred);
1201 if (output_token.length)
1202 gss_release_buffer(&minor_status, &output_token);
1204 if (client_name != GSS_C_NO_NAME)
1205 gss_release_name(&minor_status, &client_name);
1207 if (server_creds != GSS_C_NO_CREDENTIAL)
1208 gss_release_cred(&minor_status, &server_creds);
1210 if (context != GSS_C_NO_CONTEXT)
1211 gss_delete_sec_context(&minor_status, &context, GSS_C_NO_BUFFER);
1218 already_succeeded(request_rec *r)
1220 if (ap_is_initial_req(r) || MK_AUTH_TYPE == NULL)
1222 if (strcmp(MK_AUTH_TYPE, "Negotiate") ||
1223 (strcmp(MK_AUTH_TYPE, "Basic") && strchr(MK_USER, '@')))
1229 set_kerb_auth_headers(request_rec *r, const kerb_auth_config *conf,
1230 int use_krb4, int use_krb5pwd, char *negotiate_ret_value)
1232 const char *auth_name = NULL;
1234 char *negoauth_param;
1235 const char *header_name =
1236 (r->proxyreq == PROXYREQ_PROXY) ? "Proxy-Authenticate" : "WWW-Authenticate";
1238 /* get the user realm specified in .htaccess */
1239 auth_name = ap_auth_name(r);
1241 /* XXX should the WWW-Authenticate header be cleared first? */
1243 if (negotiate_ret_value != NULL && conf->krb_method_gssapi) {
1244 negoauth_param = (*negotiate_ret_value == '\0') ? "Negotiate" :
1245 ap_pstrcat(r->pool, "Negotiate ", negotiate_ret_value, NULL);
1246 ap_table_add(r->err_headers_out, header_name, negoauth_param);
1248 if (use_krb5pwd && conf->krb_method_k5pass) {
1249 ap_table_add(r->err_headers_out, header_name,
1250 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1256 if (use_krb4 && conf->krb_method_k4pass && !set_basic)
1257 ap_table_add(r->err_headers_out, header_name,
1258 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1262 int kerb_authenticate_user(request_rec *r)
1264 kerb_auth_config *conf =
1265 (kerb_auth_config *) ap_get_module_config(r->per_dir_config,
1267 const char *auth_type = NULL;
1268 const char *auth_line = NULL;
1269 const char *type = NULL;
1270 int use_krb5 = 0, use_krb4 = 0;
1272 static int last_return = HTTP_UNAUTHORIZED;
1273 char *negotiate_ret_value = NULL;
1275 /* get the type specified in .htaccess */
1276 type = ap_auth_type(r);
1278 if (type && strcasecmp(type, "Kerberos") == 0)
1279 use_krb5 = use_krb4 = 1;
1280 else if(type && strcasecmp(type, "KerberosV5") == 0)
1282 else if(type && strcasecmp(type, "KerberosV4") == 0)
1287 /* get what the user sent us in the HTTP header */
1288 auth_line = MK_TABLE_GET(r->headers_in, "Authorization");
1290 auth_line = MK_TABLE_GET(r->headers_in, "Proxy-Authorization");
1292 set_kerb_auth_headers(r, conf, use_krb4, use_krb5,
1293 (use_krb5) ? "\0" : NULL);
1294 return HTTP_UNAUTHORIZED;
1297 auth_type = ap_getword_white(r->pool, &auth_line);
1299 if (already_succeeded(r))
1302 ret = HTTP_UNAUTHORIZED;
1305 if (use_krb5 && conf->krb_method_gssapi &&
1306 strcasecmp(auth_type, "Negotiate") == 0) {
1307 ret = authenticate_user_gss(r, conf, auth_line, &negotiate_ret_value);
1308 } else if (use_krb5 && conf->krb_method_k5pass &&
1309 strcasecmp(auth_type, "Basic") == 0) {
1310 ret = authenticate_user_krb5pwd(r, conf, auth_line);
1315 if (ret == HTTP_UNAUTHORIZED && use_krb4 && conf->krb_method_k4pass &&
1316 strcasecmp(auth_type, "Basic") == 0)
1317 ret = authenticate_user_krb4pwd(r, conf, auth_line);
1320 if (ret == HTTP_UNAUTHORIZED)
1321 set_kerb_auth_headers(r, conf, use_krb4, use_krb5, negotiate_ret_value);
1323 /* XXX log_debug: if ret==OK, log(user XY authenticated) */
1330 /***************************************************************************
1331 Module Setup/Configuration
1332 ***************************************************************************/
1333 #ifndef STANDARD20_MODULE_STUFF
1334 module MODULE_VAR_EXPORT auth_kerb_module = {
1335 STANDARD_MODULE_STUFF,
1336 NULL, /* module initializer */
1337 kerb_dir_create_config, /* per-directory config creator */
1338 NULL, /* per-directory config merger */
1339 NULL, /* per-server config creator */
1340 NULL, /* per-server config merger */
1341 kerb_auth_cmds, /* command table */
1342 NULL, /* [ 9] content handlers */
1343 NULL, /* [ 2] URI-to-filename translation */
1344 kerb_authenticate_user, /* [ 5] check/validate user_id */
1345 NULL, /* [ 6] check user_id is valid *here* */
1346 NULL, /* [ 4] check access by host address */
1347 NULL, /* [ 7] MIME type checker/setter */
1348 NULL, /* [ 8] fixups */
1349 NULL, /* [10] logger */
1350 NULL, /* [ 3] header parser */
1351 NULL, /* process initialization */
1352 NULL, /* process exit/cleanup */
1353 NULL /* [ 1] post read_request handling */
1357 kerb_init_handler(apr_pool_t *p, apr_pool_t *plog,
1358 apr_pool_t *ptemp, server_rec *s)
1360 ap_add_version_component(p, "mod_auth_kerb/" MODAUTHKERB_VERSION);
1364 void kerb_register_hooks(apr_pool_t *p)
1366 ap_hook_post_config(kerb_init_handler, NULL, NULL, APR_HOOK_MIDDLE);
1367 ap_hook_check_user_id(kerb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
1370 module AP_MODULE_DECLARE_DATA auth_kerb_module =
1372 STANDARD20_MODULE_STUFF,
1373 kerb_dir_create_config, /* create per-dir conf structures */
1374 NULL, /* merge per-dir conf structures */
1375 NULL, /* create per-server conf structures */
1376 NULL, /* merge per-server conf structures */
1377 kerb_auth_cmds, /* table of configuration directives */
1378 kerb_register_hooks /* register hooks */