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 ***************************************************************************/
456 static krb5_error_code
457 verify_krb5_init_creds(krb5_context context, krb5_creds *creds,
458 krb5_principal ap_req_server, krb5_keytab ap_req_keytab)
462 krb5_ccache local_ccache = NULL;
463 krb5_creds *new_creds = NULL;
464 krb5_auth_context auth_context = NULL;
465 krb5_keytab keytab = NULL;
467 memset(&req, 0, sizeof(req));
469 if (ap_req_keytab == NULL) {
470 ret = krb5_kt_default (context, &keytab);
474 keytab = ap_req_keytab;
476 ret = krb5_cc_resolve(context, "MEMORY:", &local_ccache);
480 ret = krb5_cc_initialize(context, local_ccache, creds->client);
484 ret = krb5_cc_store_cred (context, local_ccache, creds);
488 if (!krb5_principal_compare (context, ap_req_server, creds->server)) {
489 krb5_creds match_cred;
491 memset (&match_cred, 0, sizeof(match_cred));
493 match_cred.client = creds->client;
494 match_cred.server = ap_req_server;
496 ret = krb5_get_credentials (context, 0, local_ccache,
497 &match_cred, &new_creds);
503 ret = krb5_mk_req_extended (context, &auth_context, 0, NULL, creds, &req);
507 krb5_auth_con_free (context, auth_context);
509 ret = krb5_auth_con_init(context, &auth_context);
512 krb5_auth_con_setflags(context, auth_context, KRB5_AUTH_CONTEXT_DO_SEQUENCE);
514 ret = krb5_rd_req (context, &auth_context, &req, ap_req_server,
518 krb5_free_data_contents(context, &req);
520 krb5_auth_con_free (context, auth_context);
522 krb5_free_creds (context, new_creds);
523 if (ap_req_keytab == NULL && keytab)
524 krb5_kt_close (context, keytab);
526 krb5_cc_destroy (context, local_ccache);
531 /* Inspired by krb5_verify_user from Heimdal */
532 static krb5_error_code
533 verify_krb5_user(request_rec *r, krb5_context context, krb5_principal principal,
534 krb5_ccache ccache, const char *password, const char *service,
535 krb5_keytab keytab, int krb_verify_kdc)
538 krb5_principal server = NULL;
541 /* XXX error messages shouldn't be logged here (and in the while() loop in
542 * authenticate_user_krb5pwd() as weell), in order to avoid confusing log
543 * entries when using multiple realms */
545 memset(&creds, 0, sizeof(creds));
547 ret = krb5_get_init_creds_password(context, &creds, principal,
548 (char *)password, NULL,
549 NULL, 0, NULL, NULL);
551 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
552 "krb5_get_init_creds_password() failed: %s",
553 krb5_get_err_text(context, ret));
557 ret = krb5_sname_to_principal(context, ap_get_server_name(r), service,
558 KRB5_NT_UNKNOWN, &server);
560 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
561 "krb5_sname_to_principal() failed: %s",
562 krb5_get_err_text(context, ret));
565 /* XXX log_debug: lookig for <server_princ> in keytab */
571 krb5_get_default_realm(context, &realm);
572 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
573 "trying to verify password using key for %s/%s@%s",
574 service, ap_get_server_name(r), realm);
578 if (krb_verify_kdc &&
579 (ret = verify_krb5_init_creds(context, &creds, server, keytab))) {
580 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
581 "failed to verify krb5 credentials: %s",
582 krb5_get_err_text(context, ret));
587 ret = krb5_cc_initialize(context, ccache, principal);
589 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
590 "krb5_cc_initialize() failed: %s",
591 krb5_get_err_text(context, ret));
595 ret = krb5_cc_store_cred(context, ccache, &creds);
597 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
598 "krb5_cc_store_cred() failed: %s",
599 krb5_get_err_text(context, ret));
605 krb5_free_cred_contents(context, &creds);
607 krb5_free_principal(context, server);
613 krb5_cache_cleanup(void *data)
615 krb5_context context;
617 krb5_error_code problem;
618 char *cache_name = (char *) data;
620 problem = krb5_init_context(&context);
622 /* ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "krb5_init_context() failed"); */
623 return HTTP_INTERNAL_SERVER_ERROR;
626 problem = krb5_cc_resolve(context, cache_name, &cache);
628 /* log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
629 "krb5_cc_resolve() failed (%s: %s)",
630 cache_name, krb5_get_err_text(context, problem)); */
631 return HTTP_INTERNAL_SERVER_ERROR;
634 krb5_cc_destroy(context, cache);
635 krb5_free_context(context);
640 create_krb5_ccache(krb5_context kcontext,
642 kerb_auth_config *conf,
643 krb5_principal princ,
648 krb5_error_code problem;
650 krb5_ccache tmp_ccache = NULL;
652 ccname = ap_psprintf(r->pool, "FILE:%s/krb5cc_apache_XXXXXX", P_tmpdir);
653 fd = mkstemp(ccname + strlen("FILE:"));
655 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
656 "mkstemp() failed: %s", strerror(errno));
657 ret = HTTP_INTERNAL_SERVER_ERROR;
662 problem = krb5_cc_resolve(kcontext, ccname, &tmp_ccache);
664 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
665 "krb5_cc_resolve() failed: %s",
666 krb5_get_err_text(kcontext, problem));
667 ret = HTTP_INTERNAL_SERVER_ERROR;
672 problem = krb5_cc_initialize(kcontext, tmp_ccache, princ);
674 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
675 "Cannot initialize krb5 ccache %s: krb5_cc_initialize() failed: %s",
676 ccname, krb5_get_err_text(kcontext, problem));
677 ret = HTTP_INTERNAL_SERVER_ERROR;
681 ap_table_setn(r->subprocess_env, "KRB5CCNAME", ccname);
682 ap_register_cleanup(r->pool, ccname,
683 krb5_cache_cleanup, ap_null_cleanup);
685 *ccache = tmp_ccache;
692 krb5_cc_destroy(kcontext, tmp_ccache);
698 store_krb5_creds(krb5_context kcontext,
700 kerb_auth_config *conf,
701 krb5_ccache delegated_cred)
704 krb5_error_code problem;
705 krb5_principal princ;
709 problem = krb5_cc_get_principal(kcontext, delegated_cred, &princ);
711 snprintf(errstr, sizeof(errstr), "krb5_cc_get_principal() failed: %s",
712 krb5_get_err_text(kcontext, problem));
713 return HTTP_INTERNAL_SERVER_ERROR;
716 ret = create_krb5_ccache(kcontext, r, conf, princ, &ccache);
718 krb5_free_principal(kcontext, princ);
723 problem = krb5_cc_copy_cache(kcontext, delegated_cred, ccache);
725 problem = krb5_cc_copy_creds(kcontext, delegated_cred, ccache);
727 krb5_free_principal(kcontext, princ);
729 snprintf(errstr, sizeof(errstr), "Failed to store credentials: %s",
730 krb5_get_err_text(kcontext, problem));
731 krb5_cc_destroy(kcontext, ccache);
732 return HTTP_INTERNAL_SERVER_ERROR;
735 krb5_cc_close(kcontext, ccache);
740 int authenticate_user_krb5pwd(request_rec *r,
741 kerb_auth_config *conf,
742 const char *auth_line)
744 const char *sent_pw = NULL;
745 const char *sent_name = NULL;
746 const char *realms = NULL;
747 krb5_context kcontext = NULL;
748 krb5_error_code code;
749 krb5_principal client = NULL;
750 krb5_ccache ccache = NULL;
751 krb5_keytab keytab = NULL;
754 int all_principals_unkown;
758 code = krb5_init_context(&kcontext);
760 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
761 "Cannot initialize Kerberos5 context (%d)", code);
762 return HTTP_INTERNAL_SERVER_ERROR;
765 sent_pw = ap_pbase64decode(r->pool, auth_line);
766 sent_name = ap_getword (r->pool, &sent_pw, ':');
767 /* do not allow user to override realm setting of server */
768 if (strchr(sent_name, '@')) {
769 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
770 "specifying realm in user name is prohibited");
771 ret = HTTP_UNAUTHORIZED;
775 if (sent_pw == NULL || *sent_pw == '\0') {
776 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
777 "empty passwords are not accepted");
778 ret = HTTP_UNAUTHORIZED;
782 code = krb5_cc_resolve(kcontext, "MEMORY:", &ccache);
784 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
785 "generating new memory ccache failed: %s",
786 krb5_get_err_text(kcontext, code));
787 ret = HTTP_INTERNAL_SERVER_ERROR;
792 if (conf->krb_5_keytab)
793 krb5_kt_resolve(kcontext, conf->krb_5_keytab, &keytab);
795 all_principals_unkown = 1;
796 realms = conf->krb_auth_realms;
798 if (realms && (code = krb5_set_default_realm(kcontext,
799 ap_getword_white(r->pool, &realms)))){
800 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
801 "krb5_set_default_realm() failed: %s",
802 krb5_get_err_text(kcontext, code));
807 krb5_free_principal(kcontext, client);
810 code = krb5_parse_name(kcontext, sent_name, &client);
812 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
813 "krb5_parse_name() failed: %s",
814 krb5_get_err_text(kcontext, code));
818 code = verify_krb5_user(r, kcontext, client, ccache, sent_pw,
819 conf->krb_service_name,
820 keytab, conf->krb_verify_kdc);
821 if (!conf->krb_authoritative && code) {
822 /* if we're not authoritative, we allow authentication to pass on
823 * to another modules if (and only if) the user is not known to us */
824 if (all_principals_unkown && code != KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
825 all_principals_unkown = 0;
831 /* ap_getword_white() used above shifts the parameter, so it's not
832 needed to touch the realms variable */
833 } while (realms && *realms);
835 memset((char *)sent_pw, 0, strlen(sent_pw));
838 if (!conf->krb_authoritative && all_principals_unkown == 1 && code == KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
841 ret = HTTP_UNAUTHORIZED;
846 code = krb5_unparse_name(kcontext, client, &name);
848 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "krb5_unparse_name() failed: %s",
849 krb5_get_err_text(kcontext, code));
850 ret = HTTP_UNAUTHORIZED;
853 MK_USER = ap_pstrdup (r->pool, name);
854 MK_AUTH_TYPE = "Basic";
857 if (conf->krb_save_credentials)
858 store_krb5_creds(kcontext, r, conf, ccache);
864 krb5_free_principal(kcontext, client);
866 krb5_cc_destroy(kcontext, ccache);
868 krb5_kt_close(kcontext, keytab);
869 krb5_free_context(kcontext);
874 /*********************************************************************
875 * GSSAPI Authentication
876 ********************************************************************/
879 get_gss_error(MK_POOL *p, OM_uint32 err_maj, OM_uint32 err_min, char *prefix)
881 OM_uint32 maj_stat, min_stat;
882 OM_uint32 msg_ctx = 0;
883 gss_buffer_desc status_string;
887 err_msg = ap_pstrdup(p, prefix);
889 maj_stat = gss_display_status (&min_stat,
895 if (GSS_ERROR(maj_stat))
897 err_msg = ap_pstrcat(p, err_msg, ": ", (char*) status_string.value, NULL);
898 gss_release_buffer(&min_stat, &status_string);
900 maj_stat = gss_display_status (&min_stat,
906 if (!GSS_ERROR(maj_stat)) {
907 err_msg = ap_pstrcat(p, err_msg,
908 " (", (char*) status_string.value, ")", NULL);
909 gss_release_buffer(&min_stat, &status_string);
911 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
917 store_gss_creds(request_rec *r, kerb_auth_config *conf, char *princ_name,
918 gss_cred_id_t delegated_cred)
920 OM_uint32 maj_stat, min_stat;
921 krb5_principal princ = NULL;
922 krb5_ccache ccache = NULL;
923 krb5_error_code problem;
924 krb5_context context;
925 int ret = HTTP_INTERNAL_SERVER_ERROR;
927 problem = krb5_init_context(&context);
929 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Cannot initialize krb5 context");
930 return HTTP_INTERNAL_SERVER_ERROR;
933 problem = krb5_parse_name(context, princ_name, &princ);
935 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
936 "Cannot parse delegated username (%s)", krb5_get_err_text(context, problem));
940 problem = create_krb5_ccache(context, r, conf, princ, &ccache);
942 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
943 "Cannot create krb5 ccache (%s)", krb5_get_err_text(context, problem));
947 maj_stat = gss_krb5_copy_ccache(&min_stat, delegated_cred, ccache);
948 if (GSS_ERROR(maj_stat)) {
949 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
950 "Cannot store delegated credential (%s)",
951 get_gss_error(r->pool, maj_stat, min_stat, "gss_krb5_copy_ccache"));
955 krb5_cc_close(context, ccache);
961 krb5_free_principal(context, princ);
963 krb5_cc_destroy(context, ccache);
964 krb5_free_context(context);
969 get_gss_creds(request_rec *r,
970 kerb_auth_config *conf,
971 gss_cred_id_t *server_creds)
973 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
974 OM_uint32 major_status, minor_status, minor_status2;
975 gss_name_t server_name = GSS_C_NO_NAME;
978 snprintf(buf, sizeof(buf), "%s@%s", conf->krb_service_name,
979 ap_get_server_name(r));
981 input_token.value = buf;
982 input_token.length = strlen(buf) + 1;
984 major_status = gss_import_name(&minor_status, &input_token,
985 GSS_C_NT_HOSTBASED_SERVICE,
987 if (GSS_ERROR(major_status)) {
988 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
989 "%s", get_gss_error(r->pool, major_status, minor_status,
990 "gss_import_name() failed"));
991 return HTTP_INTERNAL_SERVER_ERROR;
994 major_status = gss_acquire_cred(&minor_status, server_name, GSS_C_INDEFINITE,
995 GSS_C_NO_OID_SET, GSS_C_ACCEPT,
996 server_creds, NULL, NULL);
997 gss_release_name(&minor_status2, &server_name);
998 if (GSS_ERROR(major_status)) {
999 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1000 "%s", get_gss_error(r->pool, major_status, minor_status,
1001 "gss_acquire_cred() failed"));
1002 return HTTP_INTERNAL_SERVER_ERROR;
1007 * With MIT Kerberos 5 1.3.x the gss_cred_id_t is the same as
1008 * krb5_gss_cred_id_t and krb5_gss_cred_id_rec contains a pointer to
1010 * This allows us to override the replay cache function vector with
1012 * Note that this is a dirty hack to get things working and there may
1013 * well be unknown side-effects.
1015 if (memcmp(((krb5_gss_cred_id_t) *server_creds)->rcache->ops->type, "dfl", 3) == 0)
1016 /* Override the rcache operations */
1017 ((krb5_gss_cred_id_t) *server_creds)->rcache->ops = &mod_auth_kerb_rc_ops;
1020 /* rcache did not point to default rcache structure, return error */
1021 return HTTP_INTERNAL_SERVER_ERROR;
1029 cmp_gss_type(gss_buffer_t token, gss_OID oid)
1034 if (token->length == 0)
1035 return GSS_S_DEFECTIVE_TOKEN;
1037 /* XXX if (token->value == NTLMSSP) log_debug("NTLM mechanism used"); */
1041 return GSS_S_DEFECTIVE_TOKEN;
1044 if ((len & 0x7f) > 4)
1045 return GSS_S_DEFECTIVE_TOKEN;
1049 return GSS_S_DEFECTIVE_TOKEN;
1051 if (((OM_uint32) *p++) != oid->length)
1052 return GSS_S_DEFECTIVE_TOKEN;
1054 return memcmp(p, oid->elements, oid->length);
1058 authenticate_user_gss(request_rec *r, kerb_auth_config *conf,
1059 const char *auth_line, char **negotiate_ret_value)
1061 OM_uint32 major_status, minor_status, minor_status2;
1062 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
1063 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
1064 const char *auth_param = NULL;
1066 gss_name_t client_name = GSS_C_NO_NAME;
1067 gss_cred_id_t delegated_cred = GSS_C_NO_CREDENTIAL;
1068 OM_uint32 (*accept_sec_token)();
1069 gss_OID_desc spnego_oid;
1070 gss_ctx_id_t context = GSS_C_NO_CONTEXT;
1071 gss_cred_id_t server_creds = GSS_C_NO_CREDENTIAL;
1073 *negotiate_ret_value = "\0";
1075 spnego_oid.length = 6;
1076 spnego_oid.elements = (void *)"\x2b\x06\x01\x05\x05\x02";
1078 if (conf->krb_5_keytab) {
1080 /* we don't use the ap_* calls here, since the string passed to putenv()
1081 * will become part of the enviroment and shouldn't be free()ed by apache
1083 ktname = malloc(strlen("KRB5_KTNAME=") + strlen(conf->krb_5_keytab) + 1);
1084 if (ktname == NULL) {
1085 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "malloc() failed: not enough memory");
1086 ret = HTTP_INTERNAL_SERVER_ERROR;
1089 sprintf(ktname, "KRB5_KTNAME=%s", conf->krb_5_keytab);
1093 ret = get_gss_creds(r, conf, &server_creds);
1097 /* ap_getword() shifts parameter */
1098 auth_param = ap_getword_white(r->pool, &auth_line);
1099 if (auth_param == NULL) {
1100 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1101 "No Authorization parameter in request from client");
1102 ret = HTTP_UNAUTHORIZED;
1106 input_token.length = ap_base64decode_len(auth_param) + 1;
1107 input_token.value = ap_pcalloc(r->connection->pool, input_token.length);
1108 if (input_token.value == NULL) {
1109 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1110 "ap_pcalloc() failed (not enough memory)");
1111 ret = HTTP_INTERNAL_SERVER_ERROR;
1114 input_token.length = ap_base64decode(input_token.value, auth_param);
1116 accept_sec_token = (cmp_gss_type(&input_token, &spnego_oid) == 0) ?
1117 gss_accept_sec_context_spnego : gss_accept_sec_context;
1119 major_status = accept_sec_token(&minor_status,
1123 GSS_C_NO_CHANNEL_BINDINGS,
1130 if (output_token.length) {
1134 len = ap_base64encode_len(output_token.length) + 1;
1135 token = ap_pcalloc(r->connection->pool, len + 1);
1136 if (token == NULL) {
1137 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1138 "ap_pcalloc() failed (not enough memory)");
1139 ret = HTTP_INTERNAL_SERVER_ERROR;
1140 gss_release_buffer(&minor_status2, &output_token);
1143 ap_base64encode(token, output_token.value, output_token.length);
1145 *negotiate_ret_value = token;
1146 gss_release_buffer(&minor_status2, &output_token);
1149 if (GSS_ERROR(major_status)) {
1150 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1151 "%s", get_gss_error(r->pool, major_status, minor_status,
1152 "gss_accept_sec_context() failed"));
1153 /* Don't offer the Negotiate method again if call to GSS layer failed */
1154 *negotiate_ret_value = NULL;
1155 ret = HTTP_UNAUTHORIZED;
1160 /* This is a _Kerberos_ module so multiple authentication rounds aren't
1161 * supported. If we wanted a generic GSS authentication we would have to do
1162 * some magic with exporting context etc. */
1163 if (major_status & GSS_S_CONTINUE_NEEDED) {
1164 ret = HTTP_UNAUTHORIZED;
1169 major_status = gss_display_name(&minor_status, client_name, &output_token, NULL);
1170 gss_release_name(&minor_status, &client_name);
1171 if (GSS_ERROR(major_status)) {
1172 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1173 "%s", get_gss_error(r->pool, major_status, minor_status,
1174 "gss_export_name() failed"));
1175 ret = HTTP_INTERNAL_SERVER_ERROR;
1179 MK_AUTH_TYPE = "Negotiate";
1180 MK_USER = ap_pstrdup(r->pool, output_token.value);
1182 if (conf->krb_save_credentials && delegated_cred != GSS_C_NO_CREDENTIAL)
1183 store_gss_creds(r, conf, (char *)output_token.value, delegated_cred);
1185 if (*negotiate_ret_value)
1186 set_kerb_auth_headers(r, conf, 0, 0, *negotiate_ret_value);
1188 gss_release_buffer(&minor_status, &output_token);
1194 gss_release_cred(&minor_status, &delegated_cred);
1196 if (output_token.length)
1197 gss_release_buffer(&minor_status, &output_token);
1199 if (client_name != GSS_C_NO_NAME)
1200 gss_release_name(&minor_status, &client_name);
1202 if (server_creds != GSS_C_NO_CREDENTIAL)
1203 gss_release_cred(&minor_status, &server_creds);
1205 if (context != GSS_C_NO_CONTEXT)
1206 gss_delete_sec_context(&minor_status, &context, GSS_C_NO_BUFFER);
1213 already_succeeded(request_rec *r)
1215 if (ap_is_initial_req(r) || MK_AUTH_TYPE == NULL)
1217 if (strcmp(MK_AUTH_TYPE, "Negotiate") ||
1218 (strcmp(MK_AUTH_TYPE, "Basic") && strchr(MK_USER, '@')))
1224 set_kerb_auth_headers(request_rec *r, const kerb_auth_config *conf,
1225 int use_krb4, int use_krb5pwd, char *negotiate_ret_value)
1227 const char *auth_name = NULL;
1229 char *negoauth_param;
1230 const char *header_name =
1231 (r->proxyreq == PROXYREQ_PROXY) ? "Proxy-Authenticate" : "WWW-Authenticate";
1233 /* get the user realm specified in .htaccess */
1234 auth_name = ap_auth_name(r);
1236 /* XXX should the WWW-Authenticate header be cleared first? */
1238 if (negotiate_ret_value != NULL && conf->krb_method_gssapi) {
1239 negoauth_param = (*negotiate_ret_value == '\0') ? "Negotiate" :
1240 ap_pstrcat(r->pool, "Negotiate ", negotiate_ret_value, NULL);
1241 ap_table_add(r->err_headers_out, header_name, negoauth_param);
1243 if (use_krb5pwd && conf->krb_method_k5pass) {
1244 ap_table_add(r->err_headers_out, header_name,
1245 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1251 if (use_krb4 && conf->krb_method_k4pass && !set_basic)
1252 ap_table_add(r->err_headers_out, header_name,
1253 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1257 int kerb_authenticate_user(request_rec *r)
1259 kerb_auth_config *conf =
1260 (kerb_auth_config *) ap_get_module_config(r->per_dir_config,
1262 const char *auth_type = NULL;
1263 const char *auth_line = NULL;
1264 const char *type = NULL;
1265 int use_krb5 = 0, use_krb4 = 0;
1267 static int last_return = HTTP_UNAUTHORIZED;
1268 char *negotiate_ret_value = NULL;
1270 /* get the type specified in .htaccess */
1271 type = ap_auth_type(r);
1273 if (type && strcasecmp(type, "Kerberos") == 0)
1274 use_krb5 = use_krb4 = 1;
1275 else if(type && strcasecmp(type, "KerberosV5") == 0)
1277 else if(type && strcasecmp(type, "KerberosV4") == 0)
1282 /* get what the user sent us in the HTTP header */
1283 auth_line = MK_TABLE_GET(r->headers_in, "Authorization");
1285 auth_line = MK_TABLE_GET(r->headers_in, "Proxy-Authorization");
1287 set_kerb_auth_headers(r, conf, use_krb4, use_krb5,
1288 (use_krb5) ? "\0" : NULL);
1289 return HTTP_UNAUTHORIZED;
1292 auth_type = ap_getword_white(r->pool, &auth_line);
1294 if (already_succeeded(r))
1297 ret = HTTP_UNAUTHORIZED;
1300 if (use_krb5 && conf->krb_method_gssapi &&
1301 strcasecmp(auth_type, "Negotiate") == 0) {
1302 ret = authenticate_user_gss(r, conf, auth_line, &negotiate_ret_value);
1303 } else if (use_krb5 && conf->krb_method_k5pass &&
1304 strcasecmp(auth_type, "Basic") == 0) {
1305 ret = authenticate_user_krb5pwd(r, conf, auth_line);
1310 if (ret == HTTP_UNAUTHORIZED && use_krb4 && conf->krb_method_k4pass &&
1311 strcasecmp(auth_type, "Basic") == 0)
1312 ret = authenticate_user_krb4pwd(r, conf, auth_line);
1315 if (ret == HTTP_UNAUTHORIZED)
1316 set_kerb_auth_headers(r, conf, use_krb4, use_krb5, negotiate_ret_value);
1318 /* XXX log_debug: if ret==OK, log(user XY authenticated) */
1325 /***************************************************************************
1326 Module Setup/Configuration
1327 ***************************************************************************/
1328 #ifndef STANDARD20_MODULE_STUFF
1329 module MODULE_VAR_EXPORT auth_kerb_module = {
1330 STANDARD_MODULE_STUFF,
1331 NULL, /* module initializer */
1332 kerb_dir_create_config, /* per-directory config creator */
1333 NULL, /* per-directory config merger */
1334 NULL, /* per-server config creator */
1335 NULL, /* per-server config merger */
1336 kerb_auth_cmds, /* command table */
1337 NULL, /* [ 9] content handlers */
1338 NULL, /* [ 2] URI-to-filename translation */
1339 kerb_authenticate_user, /* [ 5] check/validate user_id */
1340 NULL, /* [ 6] check user_id is valid *here* */
1341 NULL, /* [ 4] check access by host address */
1342 NULL, /* [ 7] MIME type checker/setter */
1343 NULL, /* [ 8] fixups */
1344 NULL, /* [10] logger */
1345 NULL, /* [ 3] header parser */
1346 NULL, /* process initialization */
1347 NULL, /* process exit/cleanup */
1348 NULL /* [ 1] post read_request handling */
1352 kerb_init_handler(apr_pool_t *p, apr_pool_t *plog,
1353 apr_pool_t *ptemp, server_rec *s)
1355 ap_add_version_component(p, "mod_auth_kerb/" MODAUTHKERB_VERSION);
1359 void kerb_register_hooks(apr_pool_t *p)
1361 ap_hook_post_config(kerb_init_handler, NULL, NULL, APR_HOOK_MIDDLE);
1362 ap_hook_check_user_id(kerb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
1365 module AP_MODULE_DECLARE_DATA auth_kerb_module =
1367 STANDARD20_MODULE_STUFF,
1368 kerb_dir_create_config, /* create per-dir conf structures */
1369 NULL, /* merge per-dir conf structures */
1370 NULL, /* create per-server conf structures */
1371 NULL, /* merge per-server conf structures */
1372 kerb_auth_cmds, /* table of configuration directives */
1373 kerb_register_hooks /* register hooks */