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.
53 #define MODAUTHKERB_VERSION "5.0-rc6"
54 #define MECH_NEGOTIATE "Negotiate"
57 #include <http_config.h>
58 #include <http_core.h>
60 #include <http_protocol.h>
61 #include <http_request.h>
63 #ifdef STANDARD20_MODULE_STUFF
64 #include <ap_compat.h>
65 #include <apr_strings.h>
66 #include <apr_base64.h>
74 # include <gssapi/gssapi.h>
75 # include <gssapi/gssapi_generic.h>
76 # include <gssapi/gssapi_krb5.h>
77 # define GSS_C_NT_USER_NAME gss_nt_user_name
78 # define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
79 # define krb5_get_err_text(context,code) error_message(code)
81 #include "spnegokrb5.h"
85 /* Prevent warning about closesocket redefinition (Apache's ap_config.h and
86 * MIT Kerberos' port-sockets.h both define it as close) */
91 #include <netdb.h> /* gethostbyname() */
95 #define vsnprintf _vsnprintf
96 #define snprintf _snprintf
98 /* XXX remove dependency on unistd.h ??? */
102 #ifdef STANDARD20_MODULE_STUFF
103 module AP_MODULE_DECLARE_DATA auth_kerb_module;
105 module auth_kerb_module;
108 /***************************************************************************
109 Macros To Ease Compatibility
110 ***************************************************************************/
111 #ifdef STANDARD20_MODULE_STUFF
112 #define MK_POOL apr_pool_t
113 #define MK_TABLE_GET apr_table_get
114 #define MK_USER r->user
115 #define MK_AUTH_TYPE r->ap_auth_type
118 #define MK_TABLE_GET ap_table_get
119 #define MK_USER r->connection->user
120 #define MK_AUTH_TYPE r->connection->ap_auth_type
121 #define PROXYREQ_PROXY STD_PROXY
124 /***************************************************************************
125 Auth Configuration Structure
126 ***************************************************************************/
128 char *krb_auth_realms;
129 int krb_save_credentials;
131 char *krb_service_name;
132 int krb_authoritative;
133 int krb_delegate_basic;
136 int krb_method_gssapi;
137 int krb_method_k5pass;
141 int krb_method_k4pass;
146 set_kerb_auth_headers(request_rec *r, const kerb_auth_config *conf,
147 int use_krb4, int use_krb5pwd, char *negotiate_ret_value);
150 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg);
152 #ifdef STANDARD20_MODULE_STUFF
153 #define command(name, func, var, type, usage) \
154 AP_INIT_ ## type (name, func, \
155 (void*)APR_XtOffsetOf(kerb_auth_config, var), \
156 OR_AUTHCFG | RSRC_CONF, usage)
158 #define command(name, func, var, type, usage) \
160 (void*)XtOffsetOf(kerb_auth_config, var), \
161 OR_AUTHCFG | RSRC_CONF, type, usage }
164 static const command_rec kerb_auth_cmds[] = {
165 command("KrbAuthRealms", krb5_save_realms, krb_auth_realms,
166 RAW_ARGS, "Realms to attempt authentication against (can be multiple)."),
168 command("KrbAuthRealm", krb5_save_realms, krb_auth_realms,
169 RAW_ARGS, "Alias for KrbAuthRealms."),
171 command("KrbSaveCredentials", ap_set_flag_slot, krb_save_credentials,
172 FLAG, "Save and store credentials/tickets retrieved during auth."),
174 command("KrbVerifyKDC", ap_set_flag_slot, krb_verify_kdc,
175 FLAG, "Verify tickets against keytab to prevent KDC spoofing attacks."),
177 command("KrbServiceName", ap_set_string_slot, krb_service_name,
178 TAKE1, "Service name to be used by Apache for authentication."),
180 command("KrbAuthoritative", ap_set_flag_slot, krb_authoritative,
181 FLAG, "Set to 'off' to allow access control to be passed along to lower modules iff the UserID is not known to this module."),
183 command("KrbDelegateBasic", ap_set_flag_slot, krb_delegate_basic,
184 FLAG, "Always offer Basic authentication regardless of KrbMethodK5Pass and pass on authentication to lower modules if Basic headers arrive."),
187 command("Krb5Keytab", ap_set_file_slot, krb_5_keytab,
188 TAKE1, "Location of Kerberos V5 keytab file."),
190 command("KrbMethodNegotiate", ap_set_flag_slot, krb_method_gssapi,
191 FLAG, "Enable Negotiate authentication method."),
193 command("KrbMethodK5Passwd", ap_set_flag_slot, krb_method_k5pass,
194 FLAG, "Enable Kerberos V5 password authentication."),
198 command("Krb4Srvtab", ap_set_file_slot, krb_4_srvtab,
199 TAKE1, "Location of Kerberos V4 srvtab file."),
201 command("KrbMethodK4Passwd", ap_set_flag_slot, krb_method_k4pass,
202 FLAG, "Enable Kerberos V4 password authentication."),
210 mkstemp(char *template)
215 start = strlen(template) - 1;
216 while(template[start] == 'X') {
217 template[start] = '0' + val % 10;
224 fd = open(template, O_RDWR | O_CREAT | O_EXCL, 0600);
225 if(fd >= 0 || errno != EEXIST)
232 if(template[i] == '9' + 1)
234 if(template[i] <= 'z')
243 #if defined(KRB5) && !defined(HEIMDAL)
244 /* Needed to work around problems with replay caches */
245 #include "mit-internals.h"
247 /* This is our replacement krb5_rc_store function */
248 static krb5_error_code
249 mod_auth_kerb_rc_store(krb5_context context, krb5_rcache rcache,
250 krb5_donot_replay_internal *donot_replay)
255 /* And this is the operations vector for our replay cache */
256 const krb5_rc_ops_internal mod_auth_kerb_rc_ops = {
263 mod_auth_kerb_rc_store,
265 krb5_rc_dfl_get_span,
266 krb5_rc_dfl_get_name,
272 /***************************************************************************
273 Auth Configuration Initialization
274 ***************************************************************************/
275 static void *kerb_dir_create_config(MK_POOL *p, char *d)
277 kerb_auth_config *rec;
279 rec = (kerb_auth_config *) ap_pcalloc(p, sizeof(kerb_auth_config));
280 ((kerb_auth_config *)rec)->krb_verify_kdc = 1;
281 ((kerb_auth_config *)rec)->krb_service_name = "HTTP";
282 ((kerb_auth_config *)rec)->krb_authoritative = 1;
283 ((kerb_auth_config *)rec)->krb_delegate_basic = 0;
285 ((kerb_auth_config *)rec)->krb_method_k5pass = 1;
286 ((kerb_auth_config *)rec)->krb_method_gssapi = 1;
289 ((kerb_auth_config *)rec)->krb_method_k4pass = 1;
295 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg)
297 sec->krb_auth_realms= ap_pstrdup(cmd->pool, arg);
301 void log_rerror(const char *file, int line, int level, int status,
302 const request_rec *r, const char *fmt, ...)
308 vsnprintf(errstr, sizeof(errstr), fmt, ap);
312 #ifdef STANDARD20_MODULE_STUFF
313 ap_log_rerror(file, line, level | APLOG_NOERRNO, status, r, "%s", errstr);
315 ap_log_rerror(file, line, level | APLOG_NOERRNO, r, "%s", errstr);
320 /***************************************************************************
321 Username/Password Validation for Krb4
322 ***************************************************************************/
324 verify_krb4_user(request_rec *r, char *name, char *instance, char *realm,
325 char *password, char *linstance, char *srvtab, int krb_verify_kdc)
331 const char *hostname;
334 char lrealm[REALM_SZ];
336 ret = krb_get_pw_in_tkt(name, instance, realm, "krbtgt", realm,
337 DEFAULT_TKT_LIFE, password);
339 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
340 "Cannot get krb4 ticket: krb_get_pw_in_tkt() failed: %s",
341 krb_get_err_text(ret));
348 hostname = ap_get_server_name(r);
350 hp = gethostbyname(hostname);
353 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
354 "Cannot verify krb4 ticket: gethostbyname() failed: %s",
358 memcpy(&addr, hp->h_addr, sizeof(addr));
360 phost = krb_get_phost((char *)hostname);
362 krb_get_lrealm(lrealm, 1);
364 ret = krb_mk_req(&ticket, linstance, phost, lrealm, 0);
366 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
367 "Cannot verify krb4 ticket: krb_mk_req() failed: %s",
368 krb_get_err_text(ret));
373 ret = krb_rd_req(&ticket, linstance, phost, addr, &authdata, srvtab);
375 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
376 "Cannot verify krb4 ticket: krb_rd_req() failed: %s",
377 krb_get_err_text(ret));
385 krb4_cache_cleanup(void *data)
387 char *tkt_file = (char *) data;
389 krb_set_tkt_string(tkt_file);
395 authenticate_user_krb4pwd(request_rec *r,
396 kerb_auth_config *conf,
397 const char *auth_line)
401 const char *sent_name;
404 char *tkt_file_p = NULL;
409 char lrealm[REALM_SZ];
410 int all_principals_unkown;
412 sent_pw = ap_pbase64decode(r->pool, auth_line);
413 sent_name = ap_getword (r->pool, &sent_pw, ':');
415 /* do not allow user to override realm setting of server */
416 if (strchr(sent_name, '@')) {
417 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
418 "specifying realm in user name is prohibited");
419 return HTTP_UNAUTHORIZED;
422 sent_instance = strchr(sent_name, '.');
424 *sent_instance++ = '\0';
426 snprintf(tkt_file, sizeof(tkt_file), "/tmp/apache_tkt_XXXXXX");
427 fd = mkstemp(tkt_file);
429 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
430 "Cannot create krb4 ccache: mkstemp() failed: %s",
432 return HTTP_INTERNAL_SERVER_ERROR;
435 tkt_file_p = ap_pstrdup(r->pool, tkt_file);
436 ap_register_cleanup(r->pool, tkt_file_p,
437 krb4_cache_cleanup, ap_null_cleanup);
439 krb_set_tkt_string(tkt_file);
441 all_principals_unkown = 1;
442 realms = conf->krb_auth_realms;
444 memset(lrealm, 0, sizeof(lrealm));
447 realm = ap_getword_white(r->pool, &realms);
450 ret = krb_get_lrealm(lrealm, 1);
456 ret = verify_krb4_user(r, (char *)sent_name,
457 (sent_instance) ? sent_instance : "",
458 (char *)realm, (char *)sent_pw,
459 conf->krb_service_name,
460 conf->krb_4_srvtab, conf->krb_verify_kdc);
461 if (!conf->krb_authoritative && ret) {
462 /* if we're not authoritative, we allow authentication to pass on
463 * to another modules if (and only if) the user is not known to us */
464 if (all_principals_unkown && ret != KDC_PR_UNKNOWN)
465 all_principals_unkown = 0;
470 } while (realms && *realms);
473 /* XXX log only in the verify_krb4_user() call */
474 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Verifying krb4 password failed");
475 ret = (!conf->krb_authoritative && all_principals_unkown == 1 && ret == KDC_PR_UNKNOWN) ?
476 DECLINED : HTTP_UNAUTHORIZED;
480 user = ap_pstrdup(r->pool, sent_name);
482 user = ap_pstrcat(r->pool, user, ".", sent_instance, NULL);
483 user = ap_pstrcat(r->pool, user, "@", realm, NULL);
486 MK_AUTH_TYPE = "Basic";
487 ap_table_setn(r->subprocess_env, "KRBTKFILE", tkt_file_p);
489 if (!conf->krb_save_credentials)
490 krb4_cache_cleanup(tkt_file);
494 krb4_cache_cleanup(tkt_file);
503 /***************************************************************************
504 Username/Password Validation for Krb5
505 ***************************************************************************/
507 /* MIT kerberos uses replay cache checks even during credential verification
508 * (i.e. in krb5_verify_init_creds()), which is obviosuly useless. In order to
509 * avoid problems with multiple apache processes accessing the same rcache file
510 * we had to use this call instead, which is only a bit modified version of
511 * krb5_verify_init_creds() */
512 static krb5_error_code
513 verify_krb5_init_creds(krb5_context context, krb5_creds *creds,
514 krb5_principal ap_req_server, krb5_keytab ap_req_keytab)
518 krb5_ccache local_ccache = NULL;
519 krb5_creds *new_creds = NULL;
520 krb5_auth_context auth_context = NULL;
521 krb5_keytab keytab = NULL;
523 memset(&req, 0, sizeof(req));
525 if (ap_req_keytab == NULL) {
526 ret = krb5_kt_default (context, &keytab);
530 keytab = ap_req_keytab;
532 ret = krb5_cc_resolve(context, "MEMORY:", &local_ccache);
536 ret = krb5_cc_initialize(context, local_ccache, creds->client);
540 ret = krb5_cc_store_cred (context, local_ccache, creds);
544 if (!krb5_principal_compare (context, ap_req_server, creds->server)) {
545 krb5_creds match_cred;
547 memset (&match_cred, 0, sizeof(match_cred));
549 match_cred.client = creds->client;
550 match_cred.server = ap_req_server;
552 ret = krb5_get_credentials (context, 0, local_ccache,
553 &match_cred, &new_creds);
559 ret = krb5_mk_req_extended (context, &auth_context, 0, NULL, creds, &req);
563 krb5_auth_con_free (context, auth_context);
565 ret = krb5_auth_con_init(context, &auth_context);
568 /* use KRB5_AUTH_CONTEXT_DO_SEQUENCE to skip replay cache checks */
569 krb5_auth_con_setflags(context, auth_context, KRB5_AUTH_CONTEXT_DO_SEQUENCE);
571 ret = krb5_rd_req (context, &auth_context, &req, ap_req_server,
576 /* XXX Do I ever want to support Heimdal 0.4 ??? */
577 krb5_data_free(&req);
579 krb5_free_data_contents(context, &req);
582 krb5_auth_con_free (context, auth_context);
584 krb5_free_creds (context, new_creds);
585 if (ap_req_keytab == NULL && keytab)
586 krb5_kt_close (context, keytab);
588 krb5_cc_destroy (context, local_ccache);
593 /* Inspired by krb5_verify_user from Heimdal */
594 static krb5_error_code
595 verify_krb5_user(request_rec *r, krb5_context context, krb5_principal principal,
596 const char *password, const char *service, krb5_keytab keytab,
597 int krb_verify_kdc, krb5_ccache *ccache)
600 krb5_principal server = NULL;
602 krb5_ccache ret_ccache = NULL;
604 /* XXX error messages shouldn't be logged here (and in the while() loop in
605 * authenticate_user_krb5pwd() as weell), in order to avoid confusing log
606 * entries when using multiple realms */
608 memset(&creds, 0, sizeof(creds));
610 ret = krb5_get_init_creds_password(context, &creds, principal,
611 (char *)password, NULL,
612 NULL, 0, NULL, NULL);
614 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
615 "krb5_get_init_creds_password() failed: %s",
616 krb5_get_err_text(context, ret));
620 ret = krb5_sname_to_principal(context, ap_get_server_name(r), service,
621 KRB5_NT_SRV_HST, &server);
623 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
624 "krb5_sname_to_principal() failed: %s",
625 krb5_get_err_text(context, ret));
628 /* XXX log_debug: lookig for <server_princ> in keytab */
634 krb5_get_default_realm(context, &realm);
635 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
636 "trying to verify password using key for %s/%s@%s",
637 service, ap_get_server_name(r), realm);
641 if (krb_verify_kdc &&
642 (ret = verify_krb5_init_creds(context, &creds, server, keytab))) {
643 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
644 "failed to verify krb5 credentials: %s",
645 krb5_get_err_text(context, ret));
649 ret = krb5_cc_resolve(context, "MEMORY:", &ret_ccache);
651 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
652 "generating new memory ccache failed: %s",
653 krb5_get_err_text(context, ret));
657 ret = krb5_cc_initialize(context, ret_ccache, principal);
659 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
660 "krb5_cc_initialize() failed: %s",
661 krb5_get_err_text(context, ret));
665 ret = krb5_cc_store_cred(context, ret_ccache, &creds);
667 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
668 "krb5_cc_store_cred() failed: %s",
669 krb5_get_err_text(context, ret));
672 *ccache = ret_ccache;
676 krb5_free_cred_contents(context, &creds);
678 krb5_free_principal(context, server);
680 krb5_cc_destroy(context, ret_ccache);
686 krb5_cache_cleanup(void *data)
688 krb5_context context;
690 krb5_error_code problem;
691 char *cache_name = (char *) data;
693 problem = krb5_init_context(&context);
695 /* ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "krb5_init_context() failed"); */
696 return HTTP_INTERNAL_SERVER_ERROR;
699 problem = krb5_cc_resolve(context, cache_name, &cache);
701 /* log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
702 "krb5_cc_resolve() failed (%s: %s)",
703 cache_name, krb5_get_err_text(context, problem)); */
704 return HTTP_INTERNAL_SERVER_ERROR;
707 krb5_cc_destroy(context, cache);
708 krb5_free_context(context);
713 create_krb5_ccache(krb5_context kcontext,
715 kerb_auth_config *conf,
716 krb5_principal princ,
721 krb5_error_code problem;
723 krb5_ccache tmp_ccache = NULL;
725 ccname = ap_psprintf(r->pool, "FILE:%s/krb5cc_apache_XXXXXX", P_tmpdir);
726 fd = mkstemp(ccname + strlen("FILE:"));
728 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
729 "mkstemp() failed: %s", strerror(errno));
730 ret = HTTP_INTERNAL_SERVER_ERROR;
735 problem = krb5_cc_resolve(kcontext, ccname, &tmp_ccache);
737 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
738 "krb5_cc_resolve() failed: %s",
739 krb5_get_err_text(kcontext, problem));
740 ret = HTTP_INTERNAL_SERVER_ERROR;
745 problem = krb5_cc_initialize(kcontext, tmp_ccache, princ);
747 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
748 "Cannot initialize krb5 ccache %s: krb5_cc_initialize() failed: %s",
749 ccname, krb5_get_err_text(kcontext, problem));
750 ret = HTTP_INTERNAL_SERVER_ERROR;
754 ap_table_setn(r->subprocess_env, "KRB5CCNAME", ccname);
755 ap_register_cleanup(r->pool, ccname,
756 krb5_cache_cleanup, ap_null_cleanup);
758 *ccache = tmp_ccache;
765 krb5_cc_destroy(kcontext, tmp_ccache);
771 store_krb5_creds(krb5_context kcontext,
773 kerb_auth_config *conf,
774 krb5_ccache delegated_cred)
777 krb5_error_code problem;
778 krb5_principal princ;
782 problem = krb5_cc_get_principal(kcontext, delegated_cred, &princ);
784 snprintf(errstr, sizeof(errstr), "krb5_cc_get_principal() failed: %s",
785 krb5_get_err_text(kcontext, problem));
786 return HTTP_INTERNAL_SERVER_ERROR;
789 ret = create_krb5_ccache(kcontext, r, conf, princ, &ccache);
791 krb5_free_principal(kcontext, princ);
796 problem = krb5_cc_copy_cache(kcontext, delegated_cred, ccache);
798 problem = krb5_cc_copy_creds(kcontext, delegated_cred, ccache);
800 krb5_free_principal(kcontext, princ);
802 snprintf(errstr, sizeof(errstr), "Failed to store credentials: %s",
803 krb5_get_err_text(kcontext, problem));
804 krb5_cc_destroy(kcontext, ccache);
805 return HTTP_INTERNAL_SERVER_ERROR;
808 krb5_cc_close(kcontext, ccache);
813 int authenticate_user_krb5pwd(request_rec *r,
814 kerb_auth_config *conf,
815 const char *auth_line)
817 const char *sent_pw = NULL;
818 const char *sent_name = NULL;
819 const char *realms = NULL;
820 const char *realm = NULL;
821 krb5_context kcontext = NULL;
822 krb5_error_code code;
823 krb5_principal client = NULL;
824 krb5_ccache ccache = NULL;
825 krb5_keytab keytab = NULL;
828 int all_principals_unkown;
830 code = krb5_init_context(&kcontext);
832 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
833 "Cannot initialize Kerberos5 context (%d)", code);
834 return HTTP_INTERNAL_SERVER_ERROR;
837 sent_pw = ap_pbase64decode(r->pool, auth_line);
838 sent_name = ap_getword (r->pool, &sent_pw, ':');
839 /* do not allow user to override realm setting of server */
840 if (strchr(sent_name, '@')) {
841 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
842 "specifying realm in user name is prohibited");
843 ret = HTTP_UNAUTHORIZED;
847 if (sent_pw == NULL || *sent_pw == '\0') {
848 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
849 "empty passwords are not accepted");
850 ret = HTTP_UNAUTHORIZED;
854 if (conf->krb_5_keytab)
855 krb5_kt_resolve(kcontext, conf->krb_5_keytab, &keytab);
857 all_principals_unkown = 1;
858 realms = conf->krb_auth_realms;
861 if (realms && (realm = ap_getword_white(r->pool, &realms)))
862 name = ap_psprintf(r->pool, "%s@%s", sent_name, realm);
865 krb5_free_principal(kcontext, client);
869 code = krb5_parse_name(kcontext, name, &client);
871 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
872 "krb5_parse_name() failed: %s",
873 krb5_get_err_text(kcontext, code));
877 code = verify_krb5_user(r, kcontext, client, sent_pw,
878 conf->krb_service_name,
879 keytab, conf->krb_verify_kdc, &ccache);
880 if (!conf->krb_authoritative && code) {
881 /* if we're not authoritative, we allow authentication to pass on
882 * to another modules if (and only if) the user is not known to us */
883 if (all_principals_unkown && code != KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
884 all_principals_unkown = 0;
890 /* ap_getword_white() used above shifts the parameter, so it's not
891 needed to touch the realms variable */
892 } while (realms && *realms);
894 memset((char *)sent_pw, 0, strlen(sent_pw));
897 if (!conf->krb_authoritative && all_principals_unkown == 1 && code == KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
900 ret = HTTP_UNAUTHORIZED;
905 code = krb5_unparse_name(kcontext, client, &name);
907 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "krb5_unparse_name() failed: %s",
908 krb5_get_err_text(kcontext, code));
909 ret = HTTP_UNAUTHORIZED;
912 MK_USER = ap_pstrdup (r->pool, name);
913 MK_AUTH_TYPE = "Basic";
916 if (conf->krb_save_credentials)
917 store_krb5_creds(kcontext, r, conf, ccache);
922 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
923 "kerb_authenticate_user_krb5pwd ret=%d user=%s authtype=%s",
924 ret, (MK_USER)?MK_USER:"(NULL)", (MK_AUTH_TYPE)?MK_AUTH_TYPE:"(NULL)");
926 krb5_free_principal(kcontext, client);
928 krb5_cc_destroy(kcontext, ccache);
930 krb5_kt_close(kcontext, keytab);
931 krb5_free_context(kcontext);
936 /*********************************************************************
937 * GSSAPI Authentication
938 ********************************************************************/
941 get_gss_error(MK_POOL *p, OM_uint32 err_maj, OM_uint32 err_min, char *prefix)
943 OM_uint32 maj_stat, min_stat;
944 OM_uint32 msg_ctx = 0;
945 gss_buffer_desc status_string;
948 err_msg = ap_pstrdup(p, prefix);
950 maj_stat = gss_display_status (&min_stat,
956 if (GSS_ERROR(maj_stat))
958 err_msg = ap_pstrcat(p, err_msg, ": ", (char*) status_string.value, NULL);
959 gss_release_buffer(&min_stat, &status_string);
961 maj_stat = gss_display_status (&min_stat,
967 if (!GSS_ERROR(maj_stat)) {
968 err_msg = ap_pstrcat(p, err_msg,
969 " (", (char*) status_string.value, ")", NULL);
970 gss_release_buffer(&min_stat, &status_string);
972 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
978 store_gss_creds(request_rec *r, kerb_auth_config *conf, char *princ_name,
979 gss_cred_id_t delegated_cred)
981 OM_uint32 maj_stat, min_stat;
982 krb5_principal princ = NULL;
983 krb5_ccache ccache = NULL;
984 krb5_error_code problem;
985 krb5_context context;
986 int ret = HTTP_INTERNAL_SERVER_ERROR;
988 problem = krb5_init_context(&context);
990 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Cannot initialize krb5 context");
991 return HTTP_INTERNAL_SERVER_ERROR;
994 problem = krb5_parse_name(context, princ_name, &princ);
996 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
997 "Cannot parse delegated username (%s)", krb5_get_err_text(context, problem));
1001 problem = create_krb5_ccache(context, r, conf, princ, &ccache);
1003 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1004 "Cannot create krb5 ccache (%s)", krb5_get_err_text(context, problem));
1008 maj_stat = gss_krb5_copy_ccache(&min_stat, delegated_cred, ccache);
1009 if (GSS_ERROR(maj_stat)) {
1010 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1011 "Cannot store delegated credential (%s)",
1012 get_gss_error(r->pool, maj_stat, min_stat, "gss_krb5_copy_ccache"));
1016 krb5_cc_close(context, ccache);
1022 krb5_free_principal(context, princ);
1024 krb5_cc_destroy(context, ccache);
1025 krb5_free_context(context);
1030 get_gss_creds(request_rec *r,
1031 kerb_auth_config *conf,
1032 gss_cred_id_t *server_creds)
1034 gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
1035 OM_uint32 major_status, minor_status, minor_status2;
1036 gss_name_t server_name = GSS_C_NO_NAME;
1039 snprintf(buf, sizeof(buf), "%s@%s", conf->krb_service_name,
1040 ap_get_server_name(r));
1043 token.length = strlen(buf) + 1;
1045 major_status = gss_import_name(&minor_status, &token,
1046 GSS_C_NT_HOSTBASED_SERVICE,
1048 memset(&token, 0, sizeof(token));
1049 if (GSS_ERROR(major_status)) {
1050 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1051 "%s", get_gss_error(r->pool, major_status, minor_status,
1052 "gss_import_name() failed"));
1053 return HTTP_INTERNAL_SERVER_ERROR;
1056 major_status = gss_display_name(&minor_status, server_name, &token, NULL);
1057 if (GSS_ERROR(major_status)) {
1058 /* Perhaps we could just ignore this error but it's safer to give up now,
1060 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1061 "%s", get_gss_error(r->pool, major_status, minor_status,
1062 "gss_display_name() failed"));
1063 return HTTP_INTERNAL_SERVER_ERROR;
1066 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "Acquiring creds for %s",
1068 gss_release_buffer(&minor_status, &token);
1070 major_status = gss_acquire_cred(&minor_status, server_name, GSS_C_INDEFINITE,
1071 GSS_C_NO_OID_SET, GSS_C_ACCEPT,
1072 server_creds, NULL, NULL);
1073 gss_release_name(&minor_status2, &server_name);
1074 if (GSS_ERROR(major_status)) {
1075 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1076 "%s", get_gss_error(r->pool, major_status, minor_status,
1077 "gss_acquire_cred() failed"));
1078 return HTTP_INTERNAL_SERVER_ERROR;
1083 * With MIT Kerberos 5 1.3.x the gss_cred_id_t is the same as
1084 * krb5_gss_cred_id_t and krb5_gss_cred_id_rec contains a pointer to
1086 * This allows us to override the replay cache function vector with
1088 * Note that this is a dirty hack to get things working and there may
1089 * well be unknown side-effects.
1092 krb5_gss_cred_id_t gss_creds = (krb5_gss_cred_id_t) *server_creds;
1094 if (gss_creds && gss_creds->rcache && gss_creds->rcache->ops &&
1095 gss_creds->rcache->ops->type &&
1096 memcmp(gss_creds->rcache->ops->type, "dfl", 3) == 0)
1097 /* Override the rcache operations */
1098 gss_creds->rcache->ops = &mod_auth_kerb_rc_ops;
1106 cmp_gss_type(gss_buffer_t token, gss_OID oid)
1111 if (token->length == 0)
1112 return GSS_S_DEFECTIVE_TOKEN;
1116 return GSS_S_DEFECTIVE_TOKEN;
1119 if ((len & 0x7f) > 4)
1120 return GSS_S_DEFECTIVE_TOKEN;
1124 return GSS_S_DEFECTIVE_TOKEN;
1126 if (((OM_uint32) *p++) != oid->length)
1127 return GSS_S_DEFECTIVE_TOKEN;
1129 return memcmp(p, oid->elements, oid->length);
1133 authenticate_user_gss(request_rec *r, kerb_auth_config *conf,
1134 const char *auth_line, char **negotiate_ret_value)
1136 OM_uint32 major_status, minor_status, minor_status2;
1137 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
1138 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
1139 const char *auth_param = NULL;
1141 gss_name_t client_name = GSS_C_NO_NAME;
1142 gss_cred_id_t delegated_cred = GSS_C_NO_CREDENTIAL;
1144 (*accept_sec_token)(OM_uint32 *, gss_ctx_id_t *, const gss_cred_id_t,
1145 const gss_buffer_t, const gss_channel_bindings_t,
1146 gss_name_t *, gss_OID *, gss_buffer_t, OM_uint32 *,
1147 OM_uint32 *, gss_cred_id_t *);
1148 gss_OID_desc spnego_oid;
1149 gss_ctx_id_t context = GSS_C_NO_CONTEXT;
1150 gss_cred_id_t server_creds = GSS_C_NO_CREDENTIAL;
1152 *negotiate_ret_value = "\0";
1154 spnego_oid.length = 6;
1155 spnego_oid.elements = (void *)"\x2b\x06\x01\x05\x05\x02";
1157 if (conf->krb_5_keytab) {
1159 /* we don't use the ap_* calls here, since the string passed to putenv()
1160 * will become part of the enviroment and shouldn't be free()ed by apache
1162 ktname = malloc(strlen("KRB5_KTNAME=") + strlen(conf->krb_5_keytab) + 1);
1163 if (ktname == NULL) {
1164 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "malloc() failed: not enough memory");
1165 ret = HTTP_INTERNAL_SERVER_ERROR;
1168 sprintf(ktname, "KRB5_KTNAME=%s", conf->krb_5_keytab);
1172 ret = get_gss_creds(r, conf, &server_creds);
1176 /* ap_getword() shifts parameter */
1177 auth_param = ap_getword_white(r->pool, &auth_line);
1178 if (auth_param == NULL) {
1179 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1180 "No Authorization parameter in request from client");
1181 ret = HTTP_UNAUTHORIZED;
1185 input_token.length = ap_base64decode_len(auth_param) + 1;
1186 input_token.value = ap_pcalloc(r->connection->pool, input_token.length);
1187 if (input_token.value == NULL) {
1188 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1189 "ap_pcalloc() failed (not enough memory)");
1190 ret = HTTP_INTERNAL_SERVER_ERROR;
1193 input_token.length = ap_base64decode(input_token.value, auth_param);
1195 accept_sec_token = (cmp_gss_type(&input_token, &spnego_oid) == 0) ?
1196 gss_accept_sec_context_spnego : gss_accept_sec_context;
1198 /* pridat: Read client Negotiate data of length XXX, prefix YYY */
1199 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "Verifying client data using %s",
1200 (accept_sec_token == gss_accept_sec_context)
1202 : "SPNEGO GSS-API");
1204 major_status = accept_sec_token(&minor_status,
1208 GSS_C_NO_CHANNEL_BINDINGS,
1215 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1216 "Verification returned code %d", major_status);
1217 if (output_token.length) {
1221 len = ap_base64encode_len(output_token.length) + 1;
1222 token = ap_pcalloc(r->connection->pool, len + 1);
1223 if (token == NULL) {
1224 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1225 "ap_pcalloc() failed (not enough memory)");
1226 ret = HTTP_INTERNAL_SERVER_ERROR;
1227 gss_release_buffer(&minor_status2, &output_token);
1230 ap_base64encode(token, output_token.value, output_token.length);
1232 *negotiate_ret_value = token;
1233 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1234 "GSS-API token of length %d bytes will be sent back",
1235 output_token.length);
1236 gss_release_buffer(&minor_status2, &output_token);
1239 if (GSS_ERROR(major_status)) {
1240 if (input_token.length > 7 && memcmp(input_token.value, "NTLMSSP", 7) == 0)
1241 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1242 "Warning: received token seems to be NTLM, which isn't supported by the Kerberos module. Check your IE configuration.");
1244 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1245 "%s", get_gss_error(r->pool, major_status, minor_status,
1246 "gss_accept_sec_context() failed"));
1247 /* Don't offer the Negotiate method again if call to GSS layer failed */
1248 *negotiate_ret_value = NULL;
1249 ret = HTTP_UNAUTHORIZED;
1254 /* This is a _Kerberos_ module so multiple authentication rounds aren't
1255 * supported. If we wanted a generic GSS authentication we would have to do
1256 * some magic with exporting context etc. */
1257 if (major_status & GSS_S_CONTINUE_NEEDED) {
1258 ret = HTTP_UNAUTHORIZED;
1263 major_status = gss_display_name(&minor_status, client_name, &output_token, NULL);
1264 gss_release_name(&minor_status, &client_name);
1265 if (GSS_ERROR(major_status)) {
1266 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1267 "%s", get_gss_error(r->pool, major_status, minor_status,
1268 "gss_export_name() failed"));
1269 ret = HTTP_INTERNAL_SERVER_ERROR;
1273 MK_AUTH_TYPE = MECH_NEGOTIATE;
1274 MK_USER = ap_pstrdup(r->pool, output_token.value);
1276 if (conf->krb_save_credentials && delegated_cred != GSS_C_NO_CREDENTIAL)
1277 store_gss_creds(r, conf, (char *)output_token.value, delegated_cred);
1279 if (*negotiate_ret_value)
1280 set_kerb_auth_headers(r, conf, 0, 0, *negotiate_ret_value);
1282 gss_release_buffer(&minor_status, &output_token);
1288 gss_release_cred(&minor_status, &delegated_cred);
1290 if (output_token.length)
1291 gss_release_buffer(&minor_status, &output_token);
1293 if (client_name != GSS_C_NO_NAME)
1294 gss_release_name(&minor_status, &client_name);
1296 if (server_creds != GSS_C_NO_CREDENTIAL)
1297 gss_release_cred(&minor_status, &server_creds);
1299 if (context != GSS_C_NO_CONTEXT)
1300 gss_delete_sec_context(&minor_status, &context, GSS_C_NO_BUFFER);
1307 already_succeeded(request_rec *r)
1309 if (ap_is_initial_req(r) || MK_AUTH_TYPE == NULL)
1311 if (strcmp(MK_AUTH_TYPE, MECH_NEGOTIATE) ||
1312 (strcmp(MK_AUTH_TYPE, "Basic") && strchr(MK_USER, '@')))
1318 set_kerb_auth_headers(request_rec *r, const kerb_auth_config *conf,
1319 int use_krb4, int use_krb5pwd, char *negotiate_ret_value)
1321 const char *auth_name = NULL;
1323 char *negoauth_param;
1324 const char *header_name =
1325 (r->proxyreq == PROXYREQ_PROXY) ? "Proxy-Authenticate" : "WWW-Authenticate";
1327 /* get the user realm specified in .htaccess */
1328 auth_name = ap_auth_name(r);
1330 /* XXX should the WWW-Authenticate header be cleared first?
1331 * apache in the proxy mode should retain client's authN headers? */
1333 if (negotiate_ret_value != NULL && conf->krb_method_gssapi) {
1334 negoauth_param = (*negotiate_ret_value == '\0') ? MECH_NEGOTIATE :
1335 ap_pstrcat(r->pool, MECH_NEGOTIATE " ", negotiate_ret_value, NULL);
1336 ap_table_add(r->err_headers_out, header_name, negoauth_param);
1338 if ((use_krb5pwd && conf->krb_method_k5pass) || conf->krb_delegate_basic) {
1339 ap_table_add(r->err_headers_out, header_name,
1340 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1347 ((use_krb4 && conf->krb_method_k4pass) || conf->krb_delegate_basic))
1348 ap_table_add(r->err_headers_out, header_name,
1349 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1353 int kerb_authenticate_user(request_rec *r)
1355 kerb_auth_config *conf =
1356 (kerb_auth_config *) ap_get_module_config(r->per_dir_config,
1358 const char *auth_type = NULL;
1359 const char *auth_line = NULL;
1360 const char *type = NULL;
1361 int use_krb5 = 0, use_krb4 = 0;
1363 static int last_return = HTTP_UNAUTHORIZED;
1364 char *negotiate_ret_value = NULL;
1366 /* get the type specified in .htaccess */
1367 type = ap_auth_type(r);
1369 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1370 "kerb_authenticate_user entered with user %s and auth_type %s",
1371 (MK_USER)?MK_USER:"(NULL)",type?type:"(NULL)");
1373 if (type && strcasecmp(type, "Kerberos") == 0)
1374 use_krb5 = use_krb4 = 1;
1375 else if(type && strcasecmp(type, "KerberosV5") == 0)
1377 else if(type && strcasecmp(type, "KerberosV4") == 0)
1382 /* get what the user sent us in the HTTP header */
1383 auth_line = MK_TABLE_GET(r->headers_in, (r->proxyreq == PROXYREQ_PROXY)
1384 ? "Proxy-Authorization"
1387 set_kerb_auth_headers(r, conf, use_krb4, use_krb5,
1388 (use_krb5) ? "\0" : NULL);
1389 return HTTP_UNAUTHORIZED;
1391 auth_type = ap_getword_white(r->pool, &auth_line);
1393 /* If we are delegating Basic to other modules, DECLINE the request */
1394 if (conf->krb_delegate_basic &&
1396 !conf->krb_method_k5pass &&
1399 !conf->krb_method_k4pass &&
1401 (strcasecmp(auth_type, "Basic") == 0))
1404 if (already_succeeded(r))
1407 ret = HTTP_UNAUTHORIZED;
1410 if (use_krb5 && conf->krb_method_gssapi &&
1411 strcasecmp(auth_type, MECH_NEGOTIATE) == 0) {
1412 ret = authenticate_user_gss(r, conf, auth_line, &negotiate_ret_value);
1413 } else if (use_krb5 && conf->krb_method_k5pass &&
1414 strcasecmp(auth_type, "Basic") == 0) {
1415 ret = authenticate_user_krb5pwd(r, conf, auth_line);
1420 if (ret == HTTP_UNAUTHORIZED && use_krb4 && conf->krb_method_k4pass &&
1421 strcasecmp(auth_type, "Basic") == 0)
1422 ret = authenticate_user_krb4pwd(r, conf, auth_line);
1425 if (ret == HTTP_UNAUTHORIZED)
1426 set_kerb_auth_headers(r, conf, use_krb4, use_krb5, negotiate_ret_value);
1428 /* XXX log_debug: if ret==OK, log(user XY authenticated) */
1435 /***************************************************************************
1436 Module Setup/Configuration
1437 ***************************************************************************/
1438 #ifndef STANDARD20_MODULE_STUFF
1439 module MODULE_VAR_EXPORT auth_kerb_module = {
1440 STANDARD_MODULE_STUFF,
1441 NULL, /* module initializer */
1442 kerb_dir_create_config, /* per-directory config creator */
1443 NULL, /* per-directory config merger */
1444 NULL, /* per-server config creator */
1445 NULL, /* per-server config merger */
1446 kerb_auth_cmds, /* command table */
1447 NULL, /* [ 9] content handlers */
1448 NULL, /* [ 2] URI-to-filename translation */
1449 kerb_authenticate_user, /* [ 5] check/validate user_id */
1450 NULL, /* [ 6] check user_id is valid *here* */
1451 NULL, /* [ 4] check access by host address */
1452 NULL, /* [ 7] MIME type checker/setter */
1453 NULL, /* [ 8] fixups */
1454 NULL, /* [10] logger */
1455 NULL, /* [ 3] header parser */
1456 NULL, /* process initialization */
1457 NULL, /* process exit/cleanup */
1458 NULL /* [ 1] post read_request handling */
1460 ,NULL, /* EAPI: add_module */
1461 NULL, /* EAPI: remove_module */
1462 NULL, /* EAPI: rewrite_command */
1463 NULL /* EAPI: new_connection */
1468 kerb_init_handler(apr_pool_t *p, apr_pool_t *plog,
1469 apr_pool_t *ptemp, server_rec *s)
1471 ap_add_version_component(p, "mod_auth_kerb/" MODAUTHKERB_VERSION);
1475 void kerb_register_hooks(apr_pool_t *p)
1477 ap_hook_post_config(kerb_init_handler, NULL, NULL, APR_HOOK_MIDDLE);
1478 ap_hook_check_user_id(kerb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
1481 module AP_MODULE_DECLARE_DATA auth_kerb_module =
1483 STANDARD20_MODULE_STUFF,
1484 kerb_dir_create_config, /* create per-dir conf structures */
1485 NULL, /* merge per-dir conf structures */
1486 NULL, /* create per-server conf structures */
1487 NULL, /* merge per-server conf structures */
1488 kerb_auth_cmds, /* table of configuration directives */
1489 kerb_register_hooks /* register hooks */