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-rc6"
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;
123 int krb_delegate_basic;
126 int krb_method_gssapi;
127 int krb_method_k5pass;
131 int krb_method_k4pass;
136 set_kerb_auth_headers(request_rec *r, const kerb_auth_config *conf,
137 int use_krb4, int use_krb5pwd, char *negotiate_ret_value);
140 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg);
142 #ifdef STANDARD20_MODULE_STUFF
143 #define command(name, func, var, type, usage) \
144 AP_INIT_ ## type (name, func, \
145 (void*)APR_XtOffsetOf(kerb_auth_config, var), \
148 #define command(name, func, var, type, usage) \
150 (void*)XtOffsetOf(kerb_auth_config, var), \
151 OR_AUTHCFG, type, usage }
154 static const command_rec kerb_auth_cmds[] = {
155 command("KrbAuthRealms", krb5_save_realms, krb_auth_realms,
156 RAW_ARGS, "Realms to attempt authentication against (can be multiple)."),
158 command("KrbAuthRealm", krb5_save_realms, krb_auth_realms,
159 RAW_ARGS, "Alias for KrbAuthRealms."),
161 command("KrbSaveCredentials", ap_set_flag_slot, krb_save_credentials,
162 FLAG, "Save and store credentials/tickets retrieved during auth."),
164 command("KrbVerifyKDC", ap_set_flag_slot, krb_verify_kdc,
165 FLAG, "Verify tickets against keytab to prevent KDC spoofing attacks."),
167 command("KrbServiceName", ap_set_string_slot, krb_service_name,
168 TAKE1, "Service name to be used by Apache for authentication."),
170 command("KrbAuthoritative", ap_set_flag_slot, krb_authoritative,
171 FLAG, "Set to 'off' to allow access control to be passed along to lower modules iff the UserID is not known to this module."),
173 command("KrbDelegateBasic", ap_set_flag_slot, krb_delegate_basic,
174 FLAG, "Always offer Basic authentication regardless of KrbMethodK5Pass and pass on authentication to lower modules if Basic headers arrive."),
177 command("Krb5Keytab", ap_set_file_slot, krb_5_keytab,
178 TAKE1, "Location of Kerberos V5 keytab file."),
180 command("KrbMethodNegotiate", ap_set_flag_slot, krb_method_gssapi,
181 FLAG, "Enable Negotiate authentication method."),
183 command("KrbMethodK5Passwd", ap_set_flag_slot, krb_method_k5pass,
184 FLAG, "Enable Kerberos V5 password authentication."),
188 command("Krb4Srvtab", ap_set_file_slot, krb_4_srvtab,
189 TAKE1, "Location of Kerberos V4 srvtab file."),
191 command("KrbMethodK4Passwd", ap_set_flag_slot, krb_method_k4pass,
192 FLAG, "Enable Kerberos V4 password authentication."),
198 #if defined(KRB5) && !defined(HEIMDAL)
199 /* Needed to work around problems with replay caches */
200 #include "mit-internals.h"
202 /* This is our replacement krb5_rc_store function */
203 static krb5_error_code
204 mod_auth_kerb_rc_store(krb5_context context, krb5_rcache rcache,
205 krb5_donot_replay_internal *donot_replay)
210 /* And this is the operations vector for our replay cache */
211 const krb5_rc_ops_internal mod_auth_kerb_rc_ops = {
218 mod_auth_kerb_rc_store,
220 krb5_rc_dfl_get_span,
221 krb5_rc_dfl_get_name,
227 /***************************************************************************
228 Auth Configuration Initialization
229 ***************************************************************************/
230 static void *kerb_dir_create_config(MK_POOL *p, char *d)
232 kerb_auth_config *rec;
234 rec = (kerb_auth_config *) ap_pcalloc(p, sizeof(kerb_auth_config));
235 ((kerb_auth_config *)rec)->krb_verify_kdc = 1;
236 ((kerb_auth_config *)rec)->krb_service_name = "HTTP";
237 ((kerb_auth_config *)rec)->krb_authoritative = 1;
238 ((kerb_auth_config *)rec)->krb_delegate_basic = 0;
240 ((kerb_auth_config *)rec)->krb_method_k5pass = 1;
241 ((kerb_auth_config *)rec)->krb_method_gssapi = 1;
244 ((kerb_auth_config *)rec)->krb_method_k4pass = 1;
250 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg)
252 sec->krb_auth_realms= ap_pstrdup(cmd->pool, arg);
256 void log_rerror(const char *file, int line, int level, int status,
257 const request_rec *r, const char *fmt, ...)
263 vsnprintf(errstr, sizeof(errstr), fmt, ap);
267 #ifdef STANDARD20_MODULE_STUFF
268 ap_log_rerror(file, line, level | APLOG_NOERRNO, status, r, "%s", errstr);
270 ap_log_rerror(file, line, level | APLOG_NOERRNO, r, "%s", errstr);
275 /***************************************************************************
276 Username/Password Validation for Krb4
277 ***************************************************************************/
279 verify_krb4_user(request_rec *r, char *name, char *instance, char *realm,
280 char *password, char *linstance, char *srvtab, int krb_verify_kdc)
286 const char *hostname;
289 char lrealm[REALM_SZ];
291 ret = krb_get_pw_in_tkt(name, instance, realm, "krbtgt", realm,
292 DEFAULT_TKT_LIFE, password);
294 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
295 "Cannot get krb4 ticket: krb_get_pw_in_tkt() failed: %s",
296 krb_get_err_text(ret));
303 hostname = ap_get_server_name(r);
305 hp = gethostbyname(hostname);
308 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
309 "Cannot verify krb4 ticket: gethostbyname() failed: %s",
313 memcpy(&addr, hp->h_addr, sizeof(addr));
315 phost = krb_get_phost((char *)hostname);
317 krb_get_lrealm(lrealm, 1);
319 ret = krb_mk_req(&ticket, linstance, phost, lrealm, 0);
321 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
322 "Cannot verify krb4 ticket: krb_mk_req() failed: %s",
323 krb_get_err_text(ret));
328 ret = krb_rd_req(&ticket, linstance, phost, addr, &authdata, srvtab);
330 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
331 "Cannot verify krb4 ticket: krb_rd_req() failed: %s",
332 krb_get_err_text(ret));
340 krb4_cache_cleanup(void *data)
342 char *tkt_file = (char *) data;
344 krb_set_tkt_string(tkt_file);
350 authenticate_user_krb4pwd(request_rec *r,
351 kerb_auth_config *conf,
352 const char *auth_line)
356 const char *sent_name;
359 char *tkt_file_p = NULL;
364 char lrealm[REALM_SZ];
365 int all_principals_unkown;
367 sent_pw = ap_pbase64decode(r->pool, auth_line);
368 sent_name = ap_getword (r->pool, &sent_pw, ':');
370 /* do not allow user to override realm setting of server */
371 if (strchr(sent_name, '@')) {
372 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
373 "specifying realm in user name is prohibited");
374 return HTTP_UNAUTHORIZED;
377 sent_instance = strchr(sent_name, '.');
379 *sent_instance++ = '\0';
381 snprintf(tkt_file, sizeof(tkt_file), "/tmp/apache_tkt_XXXXXX");
382 fd = mkstemp(tkt_file);
384 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
385 "Cannot create krb4 ccache: mkstemp() failed: %s",
387 return HTTP_INTERNAL_SERVER_ERROR;
390 tkt_file_p = ap_pstrdup(r->pool, tkt_file);
391 ap_register_cleanup(r->pool, tkt_file_p,
392 krb4_cache_cleanup, ap_null_cleanup);
394 krb_set_tkt_string(tkt_file);
396 all_principals_unkown = 1;
397 realms = conf->krb_auth_realms;
399 memset(lrealm, 0, sizeof(lrealm));
402 realm = ap_getword_white(r->pool, &realms);
405 ret = krb_get_lrealm(lrealm, 1);
411 ret = verify_krb4_user(r, (char *)sent_name,
412 (sent_instance) ? sent_instance : "",
413 (char *)realm, (char *)sent_pw,
414 conf->krb_service_name,
415 conf->krb_4_srvtab, conf->krb_verify_kdc);
416 if (!conf->krb_authoritative && ret) {
417 /* if we're not authoritative, we allow authentication to pass on
418 * to another modules if (and only if) the user is not known to us */
419 if (all_principals_unkown && ret != KDC_PR_UNKNOWN)
420 all_principals_unkown = 0;
425 } while (realms && *realms);
428 /* XXX log only in the verify_krb4_user() call */
429 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Verifying krb4 password failed");
430 ret = (!conf->krb_authoritative && all_principals_unkown == 1 && ret == KDC_PR_UNKNOWN) ?
431 DECLINED : HTTP_UNAUTHORIZED;
435 user = ap_pstrdup(r->pool, sent_name);
437 user = ap_pstrcat(r->pool, user, ".", sent_instance, NULL);
438 user = ap_pstrcat(r->pool, user, "@", realm, NULL);
441 MK_AUTH_TYPE = "Basic";
442 ap_table_setn(r->subprocess_env, "KRBTKFILE", tkt_file_p);
444 if (!conf->krb_save_credentials)
445 krb4_cache_cleanup(tkt_file);
449 krb4_cache_cleanup(tkt_file);
458 /***************************************************************************
459 Username/Password Validation for Krb5
460 ***************************************************************************/
462 /* MIT kerberos uses replay cache checks even during credential verification
463 * (i.e. in krb5_verify_init_creds()), which is obviosuly useless. In order to
464 * avoid problems with multiple apache processes accessing the same rcache file
465 * we had to use this call instead, which is only a bit modified version of
466 * krb5_verify_init_creds() */
467 static krb5_error_code
468 verify_krb5_init_creds(krb5_context context, krb5_creds *creds,
469 krb5_principal ap_req_server, krb5_keytab ap_req_keytab)
473 krb5_ccache local_ccache = NULL;
474 krb5_creds *new_creds = NULL;
475 krb5_auth_context auth_context = NULL;
476 krb5_keytab keytab = NULL;
478 memset(&req, 0, sizeof(req));
480 if (ap_req_keytab == NULL) {
481 ret = krb5_kt_default (context, &keytab);
485 keytab = ap_req_keytab;
487 ret = krb5_cc_resolve(context, "MEMORY:", &local_ccache);
491 ret = krb5_cc_initialize(context, local_ccache, creds->client);
495 ret = krb5_cc_store_cred (context, local_ccache, creds);
499 if (!krb5_principal_compare (context, ap_req_server, creds->server)) {
500 krb5_creds match_cred;
502 memset (&match_cred, 0, sizeof(match_cred));
504 match_cred.client = creds->client;
505 match_cred.server = ap_req_server;
507 ret = krb5_get_credentials (context, 0, local_ccache,
508 &match_cred, &new_creds);
514 ret = krb5_mk_req_extended (context, &auth_context, 0, NULL, creds, &req);
518 krb5_auth_con_free (context, auth_context);
520 ret = krb5_auth_con_init(context, &auth_context);
523 /* use KRB5_AUTH_CONTEXT_DO_SEQUENCE to skip replay cache checks */
524 krb5_auth_con_setflags(context, auth_context, KRB5_AUTH_CONTEXT_DO_SEQUENCE);
526 ret = krb5_rd_req (context, &auth_context, &req, ap_req_server,
531 /* XXX Do I ever want to support Heimdal 0.4 ??? */
532 krb5_data_free(&req);
534 krb5_free_data_contents(context, &req);
537 krb5_auth_con_free (context, auth_context);
539 krb5_free_creds (context, new_creds);
540 if (ap_req_keytab == NULL && keytab)
541 krb5_kt_close (context, keytab);
543 krb5_cc_destroy (context, local_ccache);
548 /* Inspired by krb5_verify_user from Heimdal */
549 static krb5_error_code
550 verify_krb5_user(request_rec *r, krb5_context context, krb5_principal principal,
551 const char *password, const char *service, krb5_keytab keytab,
552 int krb_verify_kdc, krb5_ccache *ccache)
555 krb5_principal server = NULL;
557 krb5_ccache ret_ccache = NULL;
559 /* XXX error messages shouldn't be logged here (and in the while() loop in
560 * authenticate_user_krb5pwd() as weell), in order to avoid confusing log
561 * entries when using multiple realms */
563 memset(&creds, 0, sizeof(creds));
565 ret = krb5_get_init_creds_password(context, &creds, principal,
566 (char *)password, NULL,
567 NULL, 0, NULL, NULL);
569 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
570 "krb5_get_init_creds_password() failed: %s",
571 krb5_get_err_text(context, ret));
575 ret = krb5_sname_to_principal(context, ap_get_server_name(r), service,
576 KRB5_NT_SRV_HST, &server);
578 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
579 "krb5_sname_to_principal() failed: %s",
580 krb5_get_err_text(context, ret));
583 /* XXX log_debug: lookig for <server_princ> in keytab */
589 krb5_get_default_realm(context, &realm);
590 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
591 "trying to verify password using key for %s/%s@%s",
592 service, ap_get_server_name(r), realm);
596 if (krb_verify_kdc &&
597 (ret = verify_krb5_init_creds(context, &creds, server, keytab))) {
598 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
599 "failed to verify krb5 credentials: %s",
600 krb5_get_err_text(context, ret));
604 ret = krb5_cc_resolve(context, "MEMORY:", &ret_ccache);
606 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
607 "generating new memory ccache failed: %s",
608 krb5_get_err_text(context, ret));
612 ret = krb5_cc_initialize(context, ret_ccache, principal);
614 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
615 "krb5_cc_initialize() failed: %s",
616 krb5_get_err_text(context, ret));
620 ret = krb5_cc_store_cred(context, ret_ccache, &creds);
622 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
623 "krb5_cc_store_cred() failed: %s",
624 krb5_get_err_text(context, ret));
627 *ccache = ret_ccache;
631 krb5_free_cred_contents(context, &creds);
633 krb5_free_principal(context, server);
635 krb5_cc_destroy(context, ret_ccache);
641 krb5_cache_cleanup(void *data)
643 krb5_context context;
645 krb5_error_code problem;
646 char *cache_name = (char *) data;
648 problem = krb5_init_context(&context);
650 /* ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "krb5_init_context() failed"); */
651 return HTTP_INTERNAL_SERVER_ERROR;
654 problem = krb5_cc_resolve(context, cache_name, &cache);
656 /* log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
657 "krb5_cc_resolve() failed (%s: %s)",
658 cache_name, krb5_get_err_text(context, problem)); */
659 return HTTP_INTERNAL_SERVER_ERROR;
662 krb5_cc_destroy(context, cache);
663 krb5_free_context(context);
668 create_krb5_ccache(krb5_context kcontext,
670 kerb_auth_config *conf,
671 krb5_principal princ,
676 krb5_error_code problem;
678 krb5_ccache tmp_ccache = NULL;
680 ccname = ap_psprintf(r->pool, "FILE:%s/krb5cc_apache_XXXXXX", P_tmpdir);
681 fd = mkstemp(ccname + strlen("FILE:"));
683 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
684 "mkstemp() failed: %s", strerror(errno));
685 ret = HTTP_INTERNAL_SERVER_ERROR;
690 problem = krb5_cc_resolve(kcontext, ccname, &tmp_ccache);
692 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
693 "krb5_cc_resolve() failed: %s",
694 krb5_get_err_text(kcontext, problem));
695 ret = HTTP_INTERNAL_SERVER_ERROR;
700 problem = krb5_cc_initialize(kcontext, tmp_ccache, princ);
702 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
703 "Cannot initialize krb5 ccache %s: krb5_cc_initialize() failed: %s",
704 ccname, krb5_get_err_text(kcontext, problem));
705 ret = HTTP_INTERNAL_SERVER_ERROR;
709 ap_table_setn(r->subprocess_env, "KRB5CCNAME", ccname);
710 ap_register_cleanup(r->pool, ccname,
711 krb5_cache_cleanup, ap_null_cleanup);
713 *ccache = tmp_ccache;
720 krb5_cc_destroy(kcontext, tmp_ccache);
726 store_krb5_creds(krb5_context kcontext,
728 kerb_auth_config *conf,
729 krb5_ccache delegated_cred)
732 krb5_error_code problem;
733 krb5_principal princ;
737 problem = krb5_cc_get_principal(kcontext, delegated_cred, &princ);
739 snprintf(errstr, sizeof(errstr), "krb5_cc_get_principal() failed: %s",
740 krb5_get_err_text(kcontext, problem));
741 return HTTP_INTERNAL_SERVER_ERROR;
744 ret = create_krb5_ccache(kcontext, r, conf, princ, &ccache);
746 krb5_free_principal(kcontext, princ);
751 problem = krb5_cc_copy_cache(kcontext, delegated_cred, ccache);
753 problem = krb5_cc_copy_creds(kcontext, delegated_cred, ccache);
755 krb5_free_principal(kcontext, princ);
757 snprintf(errstr, sizeof(errstr), "Failed to store credentials: %s",
758 krb5_get_err_text(kcontext, problem));
759 krb5_cc_destroy(kcontext, ccache);
760 return HTTP_INTERNAL_SERVER_ERROR;
763 krb5_cc_close(kcontext, ccache);
768 int authenticate_user_krb5pwd(request_rec *r,
769 kerb_auth_config *conf,
770 const char *auth_line)
772 const char *sent_pw = NULL;
773 const char *sent_name = NULL;
774 const char *realms = NULL;
775 krb5_context kcontext = NULL;
776 krb5_error_code code;
777 krb5_principal client = NULL;
778 krb5_ccache ccache = NULL;
779 krb5_keytab keytab = NULL;
782 int all_principals_unkown;
784 code = krb5_init_context(&kcontext);
786 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
787 "Cannot initialize Kerberos5 context (%d)", code);
788 return HTTP_INTERNAL_SERVER_ERROR;
791 sent_pw = ap_pbase64decode(r->pool, auth_line);
792 sent_name = ap_getword (r->pool, &sent_pw, ':');
793 /* do not allow user to override realm setting of server */
794 if (strchr(sent_name, '@')) {
795 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
796 "specifying realm in user name is prohibited");
797 ret = HTTP_UNAUTHORIZED;
801 if (sent_pw == NULL || *sent_pw == '\0') {
802 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
803 "empty passwords are not accepted");
804 ret = HTTP_UNAUTHORIZED;
808 if (conf->krb_5_keytab)
809 krb5_kt_resolve(kcontext, conf->krb_5_keytab, &keytab);
811 all_principals_unkown = 1;
812 realms = conf->krb_auth_realms;
814 if (realms && (code = krb5_set_default_realm(kcontext,
815 ap_getword_white(r->pool, &realms)))){
816 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
817 "krb5_set_default_realm() failed: %s",
818 krb5_get_err_text(kcontext, code));
823 krb5_free_principal(kcontext, client);
826 code = krb5_parse_name(kcontext, sent_name, &client);
828 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
829 "krb5_parse_name() failed: %s",
830 krb5_get_err_text(kcontext, code));
834 code = verify_krb5_user(r, kcontext, client, sent_pw,
835 conf->krb_service_name,
836 keytab, conf->krb_verify_kdc, &ccache);
837 if (!conf->krb_authoritative && code) {
838 /* if we're not authoritative, we allow authentication to pass on
839 * to another modules if (and only if) the user is not known to us */
840 if (all_principals_unkown && code != KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
841 all_principals_unkown = 0;
847 /* ap_getword_white() used above shifts the parameter, so it's not
848 needed to touch the realms variable */
849 } while (realms && *realms);
851 memset((char *)sent_pw, 0, strlen(sent_pw));
854 if (!conf->krb_authoritative && all_principals_unkown == 1 && code == KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
857 ret = HTTP_UNAUTHORIZED;
862 code = krb5_unparse_name(kcontext, client, &name);
864 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "krb5_unparse_name() failed: %s",
865 krb5_get_err_text(kcontext, code));
866 ret = HTTP_UNAUTHORIZED;
869 MK_USER = ap_pstrdup (r->pool, name);
870 MK_AUTH_TYPE = "Basic";
873 if (conf->krb_save_credentials)
874 store_krb5_creds(kcontext, r, conf, ccache);
879 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
880 "kerb_authenticate_user_krb5pwd ret=%d user=%s authtype=%s",
881 ret, (MK_USER)?MK_USER:"(NULL)", MK_AUTH_TYPE);
883 krb5_free_principal(kcontext, client);
885 krb5_cc_destroy(kcontext, ccache);
887 krb5_kt_close(kcontext, keytab);
888 krb5_free_context(kcontext);
893 /*********************************************************************
894 * GSSAPI Authentication
895 ********************************************************************/
898 get_gss_error(MK_POOL *p, OM_uint32 err_maj, OM_uint32 err_min, char *prefix)
900 OM_uint32 maj_stat, min_stat;
901 OM_uint32 msg_ctx = 0;
902 gss_buffer_desc status_string;
905 err_msg = ap_pstrdup(p, prefix);
907 maj_stat = gss_display_status (&min_stat,
913 if (GSS_ERROR(maj_stat))
915 err_msg = ap_pstrcat(p, err_msg, ": ", (char*) status_string.value, NULL);
916 gss_release_buffer(&min_stat, &status_string);
918 maj_stat = gss_display_status (&min_stat,
924 if (!GSS_ERROR(maj_stat)) {
925 err_msg = ap_pstrcat(p, err_msg,
926 " (", (char*) status_string.value, ")", NULL);
927 gss_release_buffer(&min_stat, &status_string);
929 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
935 store_gss_creds(request_rec *r, kerb_auth_config *conf, char *princ_name,
936 gss_cred_id_t delegated_cred)
938 OM_uint32 maj_stat, min_stat;
939 krb5_principal princ = NULL;
940 krb5_ccache ccache = NULL;
941 krb5_error_code problem;
942 krb5_context context;
943 int ret = HTTP_INTERNAL_SERVER_ERROR;
945 problem = krb5_init_context(&context);
947 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Cannot initialize krb5 context");
948 return HTTP_INTERNAL_SERVER_ERROR;
951 problem = krb5_parse_name(context, princ_name, &princ);
953 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
954 "Cannot parse delegated username (%s)", krb5_get_err_text(context, problem));
958 problem = create_krb5_ccache(context, r, conf, princ, &ccache);
960 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
961 "Cannot create krb5 ccache (%s)", krb5_get_err_text(context, problem));
965 maj_stat = gss_krb5_copy_ccache(&min_stat, delegated_cred, ccache);
966 if (GSS_ERROR(maj_stat)) {
967 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
968 "Cannot store delegated credential (%s)",
969 get_gss_error(r->pool, maj_stat, min_stat, "gss_krb5_copy_ccache"));
973 krb5_cc_close(context, ccache);
979 krb5_free_principal(context, princ);
981 krb5_cc_destroy(context, ccache);
982 krb5_free_context(context);
987 get_gss_creds(request_rec *r,
988 kerb_auth_config *conf,
989 gss_cred_id_t *server_creds)
991 gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
992 OM_uint32 major_status, minor_status, minor_status2;
993 gss_name_t server_name = GSS_C_NO_NAME;
996 snprintf(buf, sizeof(buf), "%s@%s", conf->krb_service_name,
997 ap_get_server_name(r));
1000 token.length = strlen(buf) + 1;
1002 major_status = gss_import_name(&minor_status, &token,
1003 GSS_C_NT_HOSTBASED_SERVICE,
1005 memset(&token, 0, sizeof(token));
1006 if (GSS_ERROR(major_status)) {
1007 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1008 "%s", get_gss_error(r->pool, major_status, minor_status,
1009 "gss_import_name() failed"));
1010 return HTTP_INTERNAL_SERVER_ERROR;
1013 major_status = gss_display_name(&minor_status, server_name, &token, NULL);
1014 if (GSS_ERROR(major_status)) {
1015 /* Perhaps we could just ignore this error but it's safer to give up now,
1017 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1018 "%s", get_gss_error(r->pool, major_status, minor_status,
1019 "gss_display_name() failed"));
1020 return HTTP_INTERNAL_SERVER_ERROR;
1023 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "Acquiring creds for %s",
1025 gss_release_buffer(&minor_status, &token);
1027 major_status = gss_acquire_cred(&minor_status, server_name, GSS_C_INDEFINITE,
1028 GSS_C_NO_OID_SET, GSS_C_ACCEPT,
1029 server_creds, NULL, NULL);
1030 gss_release_name(&minor_status2, &server_name);
1031 if (GSS_ERROR(major_status)) {
1032 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1033 "%s", get_gss_error(r->pool, major_status, minor_status,
1034 "gss_acquire_cred() failed"));
1035 return HTTP_INTERNAL_SERVER_ERROR;
1040 * With MIT Kerberos 5 1.3.x the gss_cred_id_t is the same as
1041 * krb5_gss_cred_id_t and krb5_gss_cred_id_rec contains a pointer to
1043 * This allows us to override the replay cache function vector with
1045 * Note that this is a dirty hack to get things working and there may
1046 * well be unknown side-effects.
1049 krb5_gss_cred_id_t gss_creds = (krb5_gss_cred_id_t) *server_creds;
1051 if (gss_creds && gss_creds->rcache && gss_creds->rcache->ops &&
1052 gss_creds->rcache->ops->type &&
1053 memcmp(gss_creds->rcache->ops->type, "dfl", 3) == 0)
1054 /* Override the rcache operations */
1055 gss_creds->rcache->ops = &mod_auth_kerb_rc_ops;
1063 cmp_gss_type(gss_buffer_t token, gss_OID oid)
1068 if (token->length == 0)
1069 return GSS_S_DEFECTIVE_TOKEN;
1073 return GSS_S_DEFECTIVE_TOKEN;
1076 if ((len & 0x7f) > 4)
1077 return GSS_S_DEFECTIVE_TOKEN;
1081 return GSS_S_DEFECTIVE_TOKEN;
1083 if (((OM_uint32) *p++) != oid->length)
1084 return GSS_S_DEFECTIVE_TOKEN;
1086 return memcmp(p, oid->elements, oid->length);
1090 authenticate_user_gss(request_rec *r, kerb_auth_config *conf,
1091 const char *auth_line, char **negotiate_ret_value)
1093 OM_uint32 major_status, minor_status, minor_status2;
1094 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
1095 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
1096 const char *auth_param = NULL;
1098 gss_name_t client_name = GSS_C_NO_NAME;
1099 gss_cred_id_t delegated_cred = GSS_C_NO_CREDENTIAL;
1100 OM_uint32 (*accept_sec_token)();
1101 gss_OID_desc spnego_oid;
1102 gss_ctx_id_t context = GSS_C_NO_CONTEXT;
1103 gss_cred_id_t server_creds = GSS_C_NO_CREDENTIAL;
1105 *negotiate_ret_value = "\0";
1107 spnego_oid.length = 6;
1108 spnego_oid.elements = (void *)"\x2b\x06\x01\x05\x05\x02";
1110 if (conf->krb_5_keytab) {
1112 /* we don't use the ap_* calls here, since the string passed to putenv()
1113 * will become part of the enviroment and shouldn't be free()ed by apache
1115 ktname = malloc(strlen("KRB5_KTNAME=") + strlen(conf->krb_5_keytab) + 1);
1116 if (ktname == NULL) {
1117 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "malloc() failed: not enough memory");
1118 ret = HTTP_INTERNAL_SERVER_ERROR;
1121 sprintf(ktname, "KRB5_KTNAME=%s", conf->krb_5_keytab);
1125 ret = get_gss_creds(r, conf, &server_creds);
1129 /* ap_getword() shifts parameter */
1130 auth_param = ap_getword_white(r->pool, &auth_line);
1131 if (auth_param == NULL) {
1132 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1133 "No Authorization parameter in request from client");
1134 ret = HTTP_UNAUTHORIZED;
1138 input_token.length = ap_base64decode_len(auth_param) + 1;
1139 input_token.value = ap_pcalloc(r->connection->pool, input_token.length);
1140 if (input_token.value == NULL) {
1141 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1142 "ap_pcalloc() failed (not enough memory)");
1143 ret = HTTP_INTERNAL_SERVER_ERROR;
1146 input_token.length = ap_base64decode(input_token.value, auth_param);
1148 accept_sec_token = (cmp_gss_type(&input_token, &spnego_oid) == 0) ?
1149 gss_accept_sec_context_spnego : gss_accept_sec_context;
1151 /* pridat: Read client Negotiate data of length XXX, prefix YYY */
1152 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "Verifying client data using %s",
1153 (accept_sec_token == gss_accept_sec_context)
1155 : "SPNEGO GSS-API");
1157 major_status = accept_sec_token(&minor_status,
1161 GSS_C_NO_CHANNEL_BINDINGS,
1168 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1169 "Verification returned code %d", major_status);
1170 if (output_token.length) {
1174 len = ap_base64encode_len(output_token.length) + 1;
1175 token = ap_pcalloc(r->connection->pool, len + 1);
1176 if (token == NULL) {
1177 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1178 "ap_pcalloc() failed (not enough memory)");
1179 ret = HTTP_INTERNAL_SERVER_ERROR;
1180 gss_release_buffer(&minor_status2, &output_token);
1183 ap_base64encode(token, output_token.value, output_token.length);
1185 *negotiate_ret_value = token;
1186 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1187 "GSS-API token of length %d bytes will be sent back",
1188 major_status, output_token.length);
1189 gss_release_buffer(&minor_status2, &output_token);
1192 if (GSS_ERROR(major_status)) {
1193 if (input_token.length > 7 && memcmp(input_token.value, "NTLMSSP", 7) == 0)
1194 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1195 "Warning: received token seems to be NTLM, which isn't supported by the Kerberos module. Check your IE configuration.");
1197 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1198 "%s", get_gss_error(r->pool, major_status, minor_status,
1199 "gss_accept_sec_context() failed"));
1200 /* Don't offer the Negotiate method again if call to GSS layer failed */
1201 *negotiate_ret_value = NULL;
1202 ret = HTTP_UNAUTHORIZED;
1207 /* This is a _Kerberos_ module so multiple authentication rounds aren't
1208 * supported. If we wanted a generic GSS authentication we would have to do
1209 * some magic with exporting context etc. */
1210 if (major_status & GSS_S_CONTINUE_NEEDED) {
1211 ret = HTTP_UNAUTHORIZED;
1216 major_status = gss_display_name(&minor_status, client_name, &output_token, NULL);
1217 gss_release_name(&minor_status, &client_name);
1218 if (GSS_ERROR(major_status)) {
1219 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1220 "%s", get_gss_error(r->pool, major_status, minor_status,
1221 "gss_export_name() failed"));
1222 ret = HTTP_INTERNAL_SERVER_ERROR;
1226 MK_AUTH_TYPE = "Negotiate";
1227 MK_USER = ap_pstrdup(r->pool, output_token.value);
1229 if (conf->krb_save_credentials && delegated_cred != GSS_C_NO_CREDENTIAL)
1230 store_gss_creds(r, conf, (char *)output_token.value, delegated_cred);
1232 if (*negotiate_ret_value)
1233 set_kerb_auth_headers(r, conf, 0, 0, *negotiate_ret_value);
1235 gss_release_buffer(&minor_status, &output_token);
1241 gss_release_cred(&minor_status, &delegated_cred);
1243 if (output_token.length)
1244 gss_release_buffer(&minor_status, &output_token);
1246 if (client_name != GSS_C_NO_NAME)
1247 gss_release_name(&minor_status, &client_name);
1249 if (server_creds != GSS_C_NO_CREDENTIAL)
1250 gss_release_cred(&minor_status, &server_creds);
1252 if (context != GSS_C_NO_CONTEXT)
1253 gss_delete_sec_context(&minor_status, &context, GSS_C_NO_BUFFER);
1260 already_succeeded(request_rec *r)
1262 if (ap_is_initial_req(r) || MK_AUTH_TYPE == NULL)
1264 if (strcmp(MK_AUTH_TYPE, "Negotiate") ||
1265 (strcmp(MK_AUTH_TYPE, "Basic") && strchr(MK_USER, '@')))
1271 set_kerb_auth_headers(request_rec *r, const kerb_auth_config *conf,
1272 int use_krb4, int use_krb5pwd, char *negotiate_ret_value)
1274 const char *auth_name = NULL;
1276 char *negoauth_param;
1277 const char *header_name =
1278 (r->proxyreq == PROXYREQ_PROXY) ? "Proxy-Authenticate" : "WWW-Authenticate";
1280 /* get the user realm specified in .htaccess */
1281 auth_name = ap_auth_name(r);
1283 /* XXX should the WWW-Authenticate header be cleared first?
1284 * apache in the proxy mode should retain client's authN headers? */
1286 if (negotiate_ret_value != NULL && conf->krb_method_gssapi) {
1287 negoauth_param = (*negotiate_ret_value == '\0') ? "Negotiate" :
1288 ap_pstrcat(r->pool, "Negotiate ", negotiate_ret_value, NULL);
1289 ap_table_add(r->err_headers_out, header_name, negoauth_param);
1291 if ((use_krb5pwd && conf->krb_method_k5pass) || conf->krb_delegate_basic) {
1292 ap_table_add(r->err_headers_out, header_name,
1293 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1300 ((use_krb4 && conf->krb_method_k4pass) || conf->krb_delegate_basic))
1301 ap_table_add(r->err_headers_out, header_name,
1302 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1306 int kerb_authenticate_user(request_rec *r)
1308 kerb_auth_config *conf =
1309 (kerb_auth_config *) ap_get_module_config(r->per_dir_config,
1311 const char *auth_type = NULL;
1312 const char *auth_line = NULL;
1313 const char *type = NULL;
1314 int use_krb5 = 0, use_krb4 = 0;
1316 static int last_return = HTTP_UNAUTHORIZED;
1317 char *negotiate_ret_value = NULL;
1319 /* get the type specified in .htaccess */
1320 type = ap_auth_type(r);
1322 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1323 "kerb_authenticate_user entered with user %s and auth_type %s",
1324 (MK_USER)?MK_USER:"(NULL)",type?type:"(NULL)");
1326 if (type && strcasecmp(type, "Kerberos") == 0)
1327 use_krb5 = use_krb4 = 1;
1328 else if(type && strcasecmp(type, "KerberosV5") == 0)
1330 else if(type && strcasecmp(type, "KerberosV4") == 0)
1335 /* get what the user sent us in the HTTP header */
1336 auth_line = MK_TABLE_GET(r->headers_in, (r->proxyreq == PROXYREQ_PROXY)
1337 ? "Proxy-Authorization"
1340 set_kerb_auth_headers(r, conf, use_krb4, use_krb5,
1341 (use_krb5) ? "\0" : NULL);
1342 return HTTP_UNAUTHORIZED;
1344 auth_type = ap_getword_white(r->pool, &auth_line);
1346 /* If we are delegating Basic to other modules, DECLINE the request */
1347 if (conf->krb_delegate_basic &&
1349 !conf->krb_method_k5pass &&
1352 !conf->krb_method_k4pass &&
1354 (strcasecmp(auth_type, "Basic") == 0))
1357 if (already_succeeded(r))
1360 ret = HTTP_UNAUTHORIZED;
1363 if (use_krb5 && conf->krb_method_gssapi &&
1364 strcasecmp(auth_type, "Negotiate") == 0) {
1365 ret = authenticate_user_gss(r, conf, auth_line, &negotiate_ret_value);
1366 } else if (use_krb5 && conf->krb_method_k5pass &&
1367 strcasecmp(auth_type, "Basic") == 0) {
1368 ret = authenticate_user_krb5pwd(r, conf, auth_line);
1373 if (ret == HTTP_UNAUTHORIZED && use_krb4 && conf->krb_method_k4pass &&
1374 strcasecmp(auth_type, "Basic") == 0)
1375 ret = authenticate_user_krb4pwd(r, conf, auth_line);
1378 if (ret == HTTP_UNAUTHORIZED)
1379 set_kerb_auth_headers(r, conf, use_krb4, use_krb5, negotiate_ret_value);
1381 /* XXX log_debug: if ret==OK, log(user XY authenticated) */
1388 /***************************************************************************
1389 Module Setup/Configuration
1390 ***************************************************************************/
1391 #ifndef STANDARD20_MODULE_STUFF
1392 module MODULE_VAR_EXPORT auth_kerb_module = {
1393 STANDARD_MODULE_STUFF,
1394 NULL, /* module initializer */
1395 kerb_dir_create_config, /* per-directory config creator */
1396 NULL, /* per-directory config merger */
1397 NULL, /* per-server config creator */
1398 NULL, /* per-server config merger */
1399 kerb_auth_cmds, /* command table */
1400 NULL, /* [ 9] content handlers */
1401 NULL, /* [ 2] URI-to-filename translation */
1402 kerb_authenticate_user, /* [ 5] check/validate user_id */
1403 NULL, /* [ 6] check user_id is valid *here* */
1404 NULL, /* [ 4] check access by host address */
1405 NULL, /* [ 7] MIME type checker/setter */
1406 NULL, /* [ 8] fixups */
1407 NULL, /* [10] logger */
1408 NULL, /* [ 3] header parser */
1409 NULL, /* process initialization */
1410 NULL, /* process exit/cleanup */
1411 NULL /* [ 1] post read_request handling */
1413 ,NULL, /* EAPI: add_module */
1414 NULL, /* EAPI: remove_module */
1415 NULL, /* EAPI: rewrite_command */
1416 NULL /* EAPI: new_connection */
1421 kerb_init_handler(apr_pool_t *p, apr_pool_t *plog,
1422 apr_pool_t *ptemp, server_rec *s)
1424 ap_add_version_component(p, "mod_auth_kerb/" MODAUTHKERB_VERSION);
1428 void kerb_register_hooks(apr_pool_t *p)
1430 ap_hook_post_config(kerb_init_handler, NULL, NULL, APR_HOOK_MIDDLE);
1431 ap_hook_check_user_id(kerb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
1434 module AP_MODULE_DECLARE_DATA auth_kerb_module =
1436 STANDARD20_MODULE_STUFF,
1437 kerb_dir_create_config, /* create per-dir conf structures */
1438 NULL, /* merge per-dir conf structures */
1439 NULL, /* create per-server conf structures */
1440 NULL, /* merge per-server conf structures */
1441 kerb_auth_cmds, /* table of configuration directives */
1442 kerb_register_hooks /* register hooks */