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-2006 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.4"
55 #define MECH_NEGOTIATE "Negotiate"
56 #define SERVICE_NAME "HTTP"
59 #include <http_config.h>
60 #include <http_core.h>
62 #include <http_protocol.h>
63 #include <http_request.h>
65 #ifdef STANDARD20_MODULE_STUFF
66 #include <apr_strings.h>
67 #include <apr_base64.h>
69 #define apr_pstrdup ap_pstrdup
70 #define apr_psprintf ap_psprintf
71 #define apr_pstrcat ap_pstrcat
72 #define apr_pcalloc ap_pcalloc
73 #define apr_table_setn ap_table_setn
74 #define apr_table_add ap_table_add
75 #define apr_base64_decode_len ap_base64decode_len
76 #define apr_base64_decode ap_base64decode
77 #define apr_base64_encode_len ap_base64encode_len
78 #define apr_base64_encode ap_base64encode
79 #define apr_pool_cleanup_null ap_null_cleanup
80 #define apr_pool_cleanup_register ap_register_cleanup
81 #endif /* STANDARD20_MODULE_STUFF */
83 #if AP_SERVER_MAJORVERSION_NUMBER == 2 && AP_SERVER_MINORVERSION_NUMBER== 2
89 #define vsnprintf _vsnprintf
90 #define snprintf _snprintf
98 # include <gssapi/gssapi.h>
99 # include <gssapi/gssapi_generic.h>
100 # include <gssapi/gssapi_krb5.h>
101 # define GSS_C_NT_USER_NAME gss_nt_user_name
102 # define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
103 # define GSS_KRB5_NT_PRINCIPAL_NAME gss_nt_krb5_name
104 # define krb5_get_err_text(context,code) error_message(code)
106 #ifndef GSSAPI_SUPPORTS_SPNEGO
107 # include "spnegokrb5.h"
112 /* Prevent warning about closesocket redefinition (Apache's ap_config.h and
113 * MIT Kerberos' port-sockets.h both define it as close) */
118 #include <netdb.h> /* gethostbyname() */
125 #ifndef KRB5_LIB_FUNCTION
127 # define KRB5_LIB_FUNCTION _stdcall
129 # define KRB5_LIB_FUNCTION
133 #ifdef STANDARD20_MODULE_STUFF
134 module AP_MODULE_DECLARE_DATA auth_kerb_module;
136 module auth_kerb_module;
139 /***************************************************************************
140 Macros To Ease Compatibility
141 ***************************************************************************/
142 #ifdef STANDARD20_MODULE_STUFF
143 #define MK_POOL apr_pool_t
144 #define MK_TABLE_GET apr_table_get
145 #define MK_USER r->user
146 #define MK_AUTH_TYPE r->ap_auth_type
149 #define MK_TABLE_GET ap_table_get
150 #define MK_USER r->connection->user
151 #define MK_AUTH_TYPE r->connection->ap_auth_type
152 #define PROXYREQ_PROXY STD_PROXY
155 /***************************************************************************
156 Auth Configuration Structure
157 ***************************************************************************/
159 char *krb_auth_realms;
160 int krb_save_credentials;
162 const char *krb_service_name;
163 int krb_authoritative;
164 int krb_delegate_basic;
166 int krb_ssl_preauthentication;
170 int krb_method_gssapi;
171 int krb_method_k5pass;
172 int krb5_do_auth_to_local;
176 int krb_method_k4pass;
180 typedef struct krb5_conn_data {
188 set_kerb_auth_headers(request_rec *r, const kerb_auth_config *conf,
189 int use_krb4, int use_krb5pwd, char *negotiate_ret_value);
192 krb5_save_realms(cmd_parms *cmd, void *sec, const char *arg);
194 #ifdef STANDARD20_MODULE_STUFF
195 #define command(name, func, var, type, usage) \
196 AP_INIT_ ## type (name, (void*) func, \
197 (void*)APR_OFFSETOF(kerb_auth_config, var), \
198 OR_AUTHCFG | RSRC_CONF, usage)
200 #define command(name, func, var, type, usage) \
202 (void*)XtOffsetOf(kerb_auth_config, var), \
203 OR_AUTHCFG | RSRC_CONF, type, usage }
206 static const command_rec kerb_auth_cmds[] = {
207 command("KrbAuthRealms", krb5_save_realms, krb_auth_realms,
208 RAW_ARGS, "Realms to attempt authentication against (can be multiple)."),
210 command("KrbAuthRealm", krb5_save_realms, krb_auth_realms,
211 RAW_ARGS, "Alias for KrbAuthRealms."),
213 command("KrbSaveCredentials", ap_set_flag_slot, krb_save_credentials,
214 FLAG, "Save and store credentials/tickets retrieved during auth."),
216 command("KrbVerifyKDC", ap_set_flag_slot, krb_verify_kdc,
217 FLAG, "Verify tickets against keytab to prevent KDC spoofing attacks."),
219 command("KrbServiceName", ap_set_string_slot, krb_service_name,
220 TAKE1, "Full or partial service name to be used by Apache for authentication."),
222 command("KrbAuthoritative", ap_set_flag_slot, krb_authoritative,
223 FLAG, "Set to 'off' to allow access control to be passed along to lower modules iff the UserID is not known to this module."),
225 command("KrbDelegateBasic", ap_set_flag_slot, krb_delegate_basic,
226 FLAG, "Always offer Basic authentication regardless of KrbMethodK5Pass and pass on authentication to lower modules if Basic headers arrive."),
229 command("KrbEnableSSLPreauthentication", ap_set_flag_slot, krb_ssl_preauthentication,
230 FLAG, "Don't do Kerberos authentication if the user is already authenticated using SSL and her client certificate."),
234 command("Krb5Keytab", ap_set_file_slot, krb_5_keytab,
235 TAKE1, "Location of Kerberos V5 keytab file."),
237 command("KrbMethodNegotiate", ap_set_flag_slot, krb_method_gssapi,
238 FLAG, "Enable Negotiate authentication method."),
240 command("KrbMethodK5Passwd", ap_set_flag_slot, krb_method_k5pass,
241 FLAG, "Enable Kerberos V5 password authentication."),
243 command("KrbLocalUserMapping", ap_set_flag_slot, krb5_do_auth_to_local,
244 FLAG, "Set to 'on' to have Kerberos do auth_to_local mapping of principal names to system user names."),
248 command("Krb4Srvtab", ap_set_file_slot, krb_4_srvtab,
249 TAKE1, "Location of Kerberos V4 srvtab file."),
251 command("KrbMethodK4Passwd", ap_set_flag_slot, krb_method_k4pass,
252 FLAG, "Enable Kerberos V4 password authentication."),
260 mkstemp(char *template)
265 start = strlen(template) - 1;
266 while(template[start] == 'X') {
267 template[start] = '0' + val % 10;
274 fd = open(template, O_RDWR | O_CREAT | O_EXCL, 0600);
275 if(fd >= 0 || errno != EEXIST)
282 if(template[i] == '9' + 1)
284 if(template[i] <= 'z')
293 #if defined(KRB5) && !defined(HEIMDAL)
294 /* Needed to work around problems with replay caches */
295 #include "mit-internals.h"
297 /* This is our replacement krb5_rc_store function */
298 static krb5_error_code KRB5_LIB_FUNCTION
299 mod_auth_kerb_rc_store(krb5_context context, krb5_rcache rcache,
300 krb5_donot_replay_internal *donot_replay)
305 /* And this is the operations vector for our replay cache */
306 static const krb5_rc_ops_internal mod_auth_kerb_rc_ops = {
313 mod_auth_kerb_rc_store,
315 krb5_rc_dfl_get_span,
316 krb5_rc_dfl_get_name,
321 /***************************************************************************
322 Auth Configuration Initialization
323 ***************************************************************************/
324 static void *kerb_dir_create_config(MK_POOL *p, char *d)
326 kerb_auth_config *rec;
328 rec = (kerb_auth_config *) apr_pcalloc(p, sizeof(kerb_auth_config));
329 ((kerb_auth_config *)rec)->krb_verify_kdc = 1;
330 ((kerb_auth_config *)rec)->krb_service_name = NULL;
331 ((kerb_auth_config *)rec)->krb_authoritative = 1;
332 ((kerb_auth_config *)rec)->krb_delegate_basic = 0;
334 ((kerb_auth_config *)rec)->krb_ssl_preauthentication = 0;
337 ((kerb_auth_config *)rec)->krb5_do_auth_to_local = 0;
338 ((kerb_auth_config *)rec)->krb_method_k5pass = 1;
339 ((kerb_auth_config *)rec)->krb_method_gssapi = 1;
342 ((kerb_auth_config *)rec)->krb_method_k4pass = 1;
348 krb5_save_realms(cmd_parms *cmd, void *vsec, const char *arg)
350 kerb_auth_config *sec = (kerb_auth_config *) vsec;
351 sec->krb_auth_realms= apr_pstrdup(cmd->pool, arg);
356 log_rerror(const char *file, int line, int level, int status,
357 const request_rec *r, const char *fmt, ...)
363 vsnprintf(errstr, sizeof(errstr), fmt, ap);
367 #ifdef STANDARD20_MODULE_STUFF
368 ap_log_rerror(file, line, level | APLOG_NOERRNO, status, r, "%s", errstr);
370 ap_log_rerror(file, line, level | APLOG_NOERRNO, r, "%s", errstr);
375 /***************************************************************************
376 Username/Password Validation for Krb4
377 ***************************************************************************/
379 verify_krb4_user(request_rec *r, const char *name, const char *instance,
380 const char *realm, const char *password, const char *linstance, const char *srvtab, int krb_verify_kdc)
386 const char *hostname;
389 char lrealm[REALM_SZ];
391 ret = krb_get_pw_in_tkt(name, instance, realm, "krbtgt", realm,
392 DEFAULT_TKT_LIFE, password);
394 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
395 "Cannot get krb4 ticket: krb_get_pw_in_tkt() failed: %s",
396 krb_get_err_text(ret));
403 hostname = ap_get_server_name(r);
405 hp = gethostbyname(hostname);
408 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
409 "Cannot verify krb4 ticket: gethostbyname() failed: %s",
413 memcpy(&addr, hp->h_addr, sizeof(addr));
415 phost = krb_get_phost((char *)hostname);
417 krb_get_lrealm(lrealm, 1);
419 ret = krb_mk_req(&ticket, linstance, phost, lrealm, 0);
421 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
422 "Cannot verify krb4 ticket: krb_mk_req() failed: %s",
423 krb_get_err_text(ret));
428 ret = krb_rd_req(&ticket, (char *)linstance, phost, addr, &authdata, (char *)srvtab);
430 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
431 "Cannot verify krb4 ticket: krb_rd_req() failed: %s",
432 krb_get_err_text(ret));
440 krb4_cache_cleanup(void *data)
442 char *tkt_file = (char *) data;
444 krb_set_tkt_string(tkt_file);
450 authenticate_user_krb4pwd(request_rec *r,
451 kerb_auth_config *conf,
452 const char *auth_line)
456 const char *sent_name;
459 char *tkt_file_p = NULL;
464 char lrealm[REALM_SZ];
465 int all_principals_unkown;
467 sent_pw = ap_pbase64decode(r->pool, auth_line);
468 sent_name = ap_getword_nulls_nc (r->pool, (char **) &sent_pw, ':');
470 sent_instance = strchr(sent_name, '.');
472 *sent_instance++ = '\0';
474 snprintf(tkt_file, sizeof(tkt_file), "/tmp/apache_tkt_XXXXXX");
475 fd = mkstemp(tkt_file);
477 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
478 "Cannot create krb4 ccache: mkstemp() failed: %s",
480 return HTTP_INTERNAL_SERVER_ERROR;
483 tkt_file_p = apr_pstrdup(r->pool, tkt_file);
484 apr_pool_cleanup_register(r->pool, tkt_file_p, krb4_cache_cleanup,
485 apr_pool_cleanup_null);
487 krb_set_tkt_string(tkt_file);
489 all_principals_unkown = 1;
490 realms = conf->krb_auth_realms;
492 memset(lrealm, 0, sizeof(lrealm));
495 realm = ap_getword_white(r->pool, &realms);
498 ret = krb_get_lrealm(lrealm, 1);
504 /* XXX conf->krb_service_name */
505 ret = verify_krb4_user(r, (char *)sent_name,
506 (sent_instance) ? sent_instance : "",
507 (char *)realm, (char *)sent_pw,
508 conf->krb_service_name,
509 conf->krb_4_srvtab, conf->krb_verify_kdc);
510 if (!conf->krb_authoritative && ret) {
511 /* if we're not authoritative, we allow authentication to pass on
512 * to another modules if (and only if) the user is not known to us */
513 if (all_principals_unkown && ret != KDC_PR_UNKNOWN)
514 all_principals_unkown = 0;
519 } while (realms && *realms);
522 /* XXX log only in the verify_krb4_user() call */
523 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Verifying krb4 password failed");
524 ret = (!conf->krb_authoritative && all_principals_unkown == 1 && ret == KDC_PR_UNKNOWN) ?
525 DECLINED : HTTP_UNAUTHORIZED;
529 user = apr_pstrdup(r->pool, sent_name);
531 user = apr_pstrcat(r->pool, user, ".", sent_instance, NULL);
532 user = apr_pstrcat(r->pool, user, "@", realm, NULL);
535 MK_AUTH_TYPE = "Kerberos";
536 apr_table_setn(r->subprocess_env, "KRBTKFILE", tkt_file_p);
538 if (!conf->krb_save_credentials)
539 krb4_cache_cleanup(tkt_file);
543 krb4_cache_cleanup(tkt_file);
552 /***************************************************************************
553 Username/Password Validation for Krb5
554 ***************************************************************************/
556 /* MIT kerberos uses replay cache checks even during credential verification
557 * (i.e. in krb5_verify_init_creds()), which is obviosuly useless. In order to
558 * avoid problems with multiple apache processes accessing the same rcache file
559 * we had to use this call instead, which is only a bit modified version of
560 * krb5_verify_init_creds() */
561 static krb5_error_code
562 verify_krb5_init_creds(request_rec *r, krb5_context context, krb5_creds *creds,
563 krb5_principal ap_req_server, krb5_keytab ap_req_keytab)
567 krb5_ccache local_ccache = NULL;
568 krb5_creds *new_creds = NULL;
569 krb5_auth_context auth_context = NULL;
570 krb5_keytab keytab = NULL;
573 memset(&req, 0, sizeof(req));
575 if (ap_req_keytab == NULL) {
576 ret = krb5_kt_default (context, &keytab);
580 keytab = ap_req_keytab;
582 #ifdef HAVE_KRB5_CC_NEW_UNIQUE
583 ret = krb5_cc_new_unique(context, "MEMORY", NULL, &local_ccache);
585 ret = krb5_cc_resolve(context, "MEMORY:", &local_ccache);
589 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
590 "krb5_cc_resolve() failed when verifying KDC");
594 ret = krb5_cc_initialize(context, local_ccache, creds->client);
596 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
597 "krb5_cc_initialize() failed when verifying KDC");
601 ret = krb5_cc_store_cred (context, local_ccache, creds);
603 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
604 "krb5_cc_initialize() failed when verifying KDC");
608 ret = krb5_unparse_name(context, ap_req_server, &server_name);
610 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
611 "krb5_unparse_name() failed when verifying KDC");
614 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
615 "Trying to verify authenticity of KDC using principal %s", server_name);
618 if (!krb5_principal_compare (context, ap_req_server, creds->server)) {
619 krb5_creds match_cred;
621 memset (&match_cred, 0, sizeof(match_cred));
623 match_cred.client = creds->client;
624 match_cred.server = ap_req_server;
626 ret = krb5_get_credentials (context, 0, local_ccache,
627 &match_cred, &new_creds);
629 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
630 "krb5_get_credentials() failed when verifying KDC");
636 ret = krb5_mk_req_extended (context, &auth_context, 0, NULL, creds, &req);
638 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
639 "krb5_mk_req_extended() failed when verifying KDC");
643 krb5_auth_con_free (context, auth_context);
645 ret = krb5_auth_con_init(context, &auth_context);
647 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
648 "krb5_auth_con_init() failed when verifying KDC");
651 /* use KRB5_AUTH_CONTEXT_DO_SEQUENCE to skip replay cache checks */
652 krb5_auth_con_setflags(context, auth_context, KRB5_AUTH_CONTEXT_DO_SEQUENCE);
654 ret = krb5_rd_req (context, &auth_context, &req, ap_req_server,
657 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
658 "krb5_rd_req() failed when verifying KDC");
664 /* XXX Do I ever want to support Heimdal 0.4 ??? */
665 krb5_data_free(&req);
667 krb5_free_data_contents(context, &req);
670 krb5_auth_con_free (context, auth_context);
672 krb5_free_creds (context, new_creds);
673 if (ap_req_keytab == NULL && keytab)
674 krb5_kt_close (context, keytab);
676 krb5_cc_destroy (context, local_ccache);
681 /* Inspired by krb5_verify_user from Heimdal */
682 static krb5_error_code
683 verify_krb5_user(request_rec *r, krb5_context context, krb5_principal principal,
684 const char *password, krb5_principal server,
685 krb5_keytab keytab, int krb_verify_kdc,
686 const char *krb_service_name, krb5_ccache *ccache)
689 krb5_get_init_creds_opt options;
691 krb5_ccache ret_ccache = NULL;
693 krb5_keytab_entry entry;
694 krb5_kt_cursor cursor;
696 /* XXX error messages shouldn't be logged here (and in the while() loop in
697 * authenticate_user_krb5pwd() as weell), in order to avoid confusing log
698 * entries when using multiple realms */
700 memset(&creds, 0, sizeof(creds));
702 ret = krb5_unparse_name(context, principal, &name);
704 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
705 "Trying to get TGT for user %s", name);
709 krb5_get_init_creds_opt_init(&options);
710 ret = krb5_get_init_creds_password(context, &creds, principal,
711 (char *)password, NULL,
712 NULL, 0, NULL, &options);
714 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
715 "krb5_get_init_creds_password() failed: %s",
716 krb5_get_err_text(context, ret));
724 krb5_get_default_realm(context, &realm);
725 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
726 "trying to verify password using key for %s/%s@%s",
727 service, ap_get_server_name(r), realm);
731 /*if (krb_verify_kdc &&
732 (ret = verify_krb5_init_creds(r, context, &creds, server, keytab))) {
733 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
734 "failed to verify krb5 credentials: %s",
735 krb5_get_err_text(context, ret));
739 if (krb_verify_kdc) {
740 if (krb_service_name && strcmp(krb_service_name,"Any") == 0) {
741 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
743 while((krb5_kt_next_entry(context, keytab, &entry, &cursor)) == 0){
744 if ((ret = verify_krb5_init_creds(r, context, &creds, entry.principal, keytab)) == 0)
749 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
750 "failed to verify krb5 credentials: %s",
751 krb5_get_err_text(context, ret));
752 krb5_kt_end_seq_get(context, keytab, &cursor);
753 krb5_kt_close(context, keytab);
756 krb5_kt_end_seq_get(context, keytab, &cursor);
757 krb5_kt_close(context, keytab);
760 if ((ret = verify_krb5_init_creds(r, context, &creds, server, keytab))) {
761 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
762 "failed to verify krb5 credentials: %s",
763 krb5_get_err_text(context, ret));
769 #ifdef HAVE_KRB5_CC_NEW_UNIQUE
770 ret = krb5_cc_new_unique(context, "MEMORY", NULL, &ret_ccache);
772 ret = krb5_cc_resolve(context, "MEMORY:", &ret_ccache);
776 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
777 "generating new memory ccache failed: %s",
778 krb5_get_err_text(context, ret));
782 ret = krb5_cc_initialize(context, ret_ccache, principal);
784 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
785 "krb5_cc_initialize() failed: %s",
786 krb5_get_err_text(context, ret));
790 ret = krb5_cc_store_cred(context, ret_ccache, &creds);
792 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
793 "krb5_cc_store_cred() failed: %s",
794 krb5_get_err_text(context, ret));
797 *ccache = ret_ccache;
801 krb5_free_cred_contents(context, &creds);
803 krb5_cc_destroy(context, ret_ccache);
809 krb5_cache_cleanup(void *data)
811 krb5_context context;
813 krb5_error_code problem;
814 char *cache_name = (char *) data;
816 problem = krb5_init_context(&context);
818 /* ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "krb5_init_context() failed"); */
819 return HTTP_INTERNAL_SERVER_ERROR;
822 problem = krb5_cc_resolve(context, cache_name, &cache);
824 /* log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
825 "krb5_cc_resolve() failed (%s: %s)",
826 cache_name, krb5_get_err_text(context, problem)); */
827 return HTTP_INTERNAL_SERVER_ERROR;
830 krb5_cc_destroy(context, cache);
831 krb5_free_context(context);
836 create_krb5_ccache(krb5_context kcontext,
838 kerb_auth_config *conf,
839 krb5_principal princ,
844 krb5_error_code problem;
846 krb5_ccache tmp_ccache = NULL;
848 ccname = apr_psprintf(r->pool, "FILE:%s/krb5cc_apache_XXXXXX", P_tmpdir);
849 fd = mkstemp(ccname + strlen("FILE:"));
851 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
852 "mkstemp() failed: %s", strerror(errno));
853 ret = HTTP_INTERNAL_SERVER_ERROR;
858 problem = krb5_cc_resolve(kcontext, ccname, &tmp_ccache);
860 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
861 "krb5_cc_resolve() failed: %s",
862 krb5_get_err_text(kcontext, problem));
863 ret = HTTP_INTERNAL_SERVER_ERROR;
868 problem = krb5_cc_initialize(kcontext, tmp_ccache, princ);
870 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
871 "Cannot initialize krb5 ccache %s: krb5_cc_initialize() failed: %s",
872 ccname, krb5_get_err_text(kcontext, problem));
873 ret = HTTP_INTERNAL_SERVER_ERROR;
877 apr_table_setn(r->subprocess_env, "KRB5CCNAME", ccname);
878 apr_pool_cleanup_register(r->pool, ccname, krb5_cache_cleanup,
879 apr_pool_cleanup_null);
881 *ccache = tmp_ccache;
888 krb5_cc_destroy(kcontext, tmp_ccache);
894 store_krb5_creds(krb5_context kcontext,
896 kerb_auth_config *conf,
897 krb5_ccache delegated_cred)
900 krb5_error_code problem;
901 krb5_principal princ;
905 problem = krb5_cc_get_principal(kcontext, delegated_cred, &princ);
907 snprintf(errstr, sizeof(errstr), "krb5_cc_get_principal() failed: %s",
908 krb5_get_err_text(kcontext, problem));
909 return HTTP_INTERNAL_SERVER_ERROR;
912 ret = create_krb5_ccache(kcontext, r, conf, princ, &ccache);
914 krb5_free_principal(kcontext, princ);
919 problem = krb5_cc_copy_cache(kcontext, delegated_cred, ccache);
921 problem = krb5_cc_copy_creds(kcontext, delegated_cred, ccache);
923 krb5_free_principal(kcontext, princ);
925 snprintf(errstr, sizeof(errstr), "Failed to store credentials: %s",
926 krb5_get_err_text(kcontext, problem));
927 krb5_cc_destroy(kcontext, ccache);
928 return HTTP_INTERNAL_SERVER_ERROR;
931 krb5_cc_close(kcontext, ccache);
936 authenticate_user_krb5pwd(request_rec *r,
937 kerb_auth_config *conf,
938 const char *auth_line)
940 const char *sent_pw = NULL;
941 const char *sent_name = NULL;
942 const char *realms = NULL;
943 const char *realm = NULL;
944 krb5_context kcontext = NULL;
945 krb5_error_code code;
946 krb5_principal client = NULL;
947 krb5_principal server = NULL;
948 krb5_ccache ccache = NULL;
949 krb5_keytab keytab = NULL;
952 int all_principals_unkown;
955 code = krb5_init_context(&kcontext);
957 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
958 "Cannot initialize Kerberos5 context (%d)", code);
959 return HTTP_INTERNAL_SERVER_ERROR;
962 sent_pw = ap_pbase64decode(r->pool, auth_line);
963 sent_name = ap_getword_nulls_nc (r->pool, (char **) &sent_pw, ':');
965 if (sent_pw == NULL || *sent_pw == '\0') {
966 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
967 "empty passwords are not accepted");
968 ret = HTTP_UNAUTHORIZED;
972 if (conf->krb_5_keytab)
973 krb5_kt_resolve(kcontext, conf->krb_5_keytab, &keytab);
975 if (conf->krb_service_name && strchr(conf->krb_service_name, '/') != NULL)
976 ret = krb5_parse_name (kcontext, conf->krb_service_name, &server);
978 ret = krb5_sname_to_principal(kcontext, ap_get_server_name(r),
979 (conf->krb_service_name) ? conf->krb_service_name : SERVICE_NAME,
980 KRB5_NT_SRV_HST, &server);
983 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
984 "Error parsing server name (%s): %s",
985 (conf->krb_service_name) ? conf->krb_service_name : SERVICE_NAME,
986 krb5_get_err_text(kcontext, ret));
987 ret = HTTP_UNAUTHORIZED;
991 code = krb5_unparse_name(kcontext, server, &name);
993 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
994 "krb5_unparse_name() failed: %s",
995 krb5_get_err_text(kcontext, code));
996 ret = HTTP_UNAUTHORIZED;
999 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "Using %s as server principal for password verification", name);
1003 p = strchr(sent_name, '@');
1006 if (conf->krb_auth_realms && !ap_find_token(r->pool, conf->krb_auth_realms, p)) {
1007 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1008 "Specified realm `%s' not allowed by configuration", p);
1009 ret = HTTP_UNAUTHORIZED;
1014 realms = (p) ? p : conf->krb_auth_realms;
1015 all_principals_unkown = 1;
1017 name = (char *) sent_name;
1018 if (realms && (realm = ap_getword_white(r->pool, &realms)))
1019 name = apr_psprintf(r->pool, "%s@%s", sent_name, realm);
1022 krb5_free_principal(kcontext, client);
1026 code = krb5_parse_name(kcontext, name, &client);
1028 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1029 "krb5_parse_name() failed: %s",
1030 krb5_get_err_text(kcontext, code));
1034 code = verify_krb5_user(r, kcontext, client, sent_pw,
1035 server, keytab, conf->krb_verify_kdc, conf->krb_service_name, &ccache);
1036 if (!conf->krb_authoritative && code) {
1037 /* if we're not authoritative, we allow authentication to pass on
1038 * to another modules if (and only if) the user is not known to us */
1039 if (all_principals_unkown && code != KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
1040 all_principals_unkown = 0;
1046 /* ap_getword_white() used above shifts the parameter, so it's not
1047 needed to touch the realms variable */
1048 } while (realms && *realms);
1050 memset((char *)sent_pw, 0, strlen(sent_pw));
1053 if (!conf->krb_authoritative && all_principals_unkown == 1 && code == KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
1056 ret = HTTP_UNAUTHORIZED;
1061 code = krb5_unparse_name(kcontext, client, &name);
1063 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "krb5_unparse_name() failed: %s",
1064 krb5_get_err_text(kcontext, code));
1065 ret = HTTP_UNAUTHORIZED;
1068 MK_USER = apr_pstrdup (r->pool, name);
1069 MK_AUTH_TYPE = "Kerberos";
1072 if (conf->krb_save_credentials)
1073 store_krb5_creds(kcontext, r, conf, ccache);
1078 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1079 "kerb_authenticate_user_krb5pwd ret=%d user=%s authtype=%s",
1080 ret, (MK_USER)?MK_USER:"(NULL)", (MK_AUTH_TYPE)?MK_AUTH_TYPE:"(NULL)");
1082 krb5_free_principal(kcontext, client);
1084 krb5_free_principal(kcontext, server);
1086 krb5_cc_destroy(kcontext, ccache);
1088 krb5_kt_close(kcontext, keytab);
1089 krb5_free_context(kcontext);
1094 /*********************************************************************
1095 * GSSAPI Authentication
1096 ********************************************************************/
1099 get_gss_error(request_rec *r, OM_uint32 err_maj, OM_uint32 err_min, char *prefix)
1101 OM_uint32 maj_stat, min_stat;
1102 OM_uint32 msg_ctx = 0;
1103 gss_buffer_desc status_string;
1106 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1107 "GSS-API major_status:%8.8x, minor_status:%8.8x",
1110 err_msg = apr_pstrdup(r->pool, prefix);
1112 maj_stat = gss_display_status (&min_stat,
1118 if (!GSS_ERROR(maj_stat)) {
1119 err_msg = apr_pstrcat(r->pool, err_msg, ": ",
1120 (char*) status_string.value, NULL);
1121 gss_release_buffer(&min_stat, &status_string);
1123 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
1126 err_msg = apr_pstrcat(r->pool, err_msg, " (", NULL);
1128 maj_stat = gss_display_status (&min_stat,
1134 if (!GSS_ERROR(maj_stat)) {
1135 err_msg = apr_pstrcat(r->pool, err_msg, ", ",
1136 (char *) status_string.value, NULL);
1137 gss_release_buffer(&min_stat, &status_string);
1139 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
1140 err_msg = apr_pstrcat(r->pool, err_msg, ")", NULL);
1146 store_gss_creds(request_rec *r, kerb_auth_config *conf, char *princ_name,
1147 gss_cred_id_t delegated_cred)
1149 OM_uint32 maj_stat, min_stat;
1150 krb5_principal princ = NULL;
1151 krb5_ccache ccache = NULL;
1152 krb5_error_code problem;
1153 krb5_context context;
1154 int ret = HTTP_INTERNAL_SERVER_ERROR;
1156 problem = krb5_init_context(&context);
1158 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Cannot initialize krb5 context");
1159 return HTTP_INTERNAL_SERVER_ERROR;
1162 problem = krb5_parse_name(context, princ_name, &princ);
1164 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1165 "Cannot parse delegated username (%s)", krb5_get_err_text(context, problem));
1169 problem = create_krb5_ccache(context, r, conf, princ, &ccache);
1171 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1172 "Cannot create krb5 ccache (%s)", krb5_get_err_text(context, problem));
1176 maj_stat = gss_krb5_copy_ccache(&min_stat, delegated_cred, ccache);
1177 if (GSS_ERROR(maj_stat)) {
1178 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1179 "Cannot store delegated credential (%s)",
1180 get_gss_error(r, maj_stat, min_stat, "gss_krb5_copy_ccache"));
1184 krb5_cc_close(context, ccache);
1190 krb5_free_principal(context, princ);
1192 krb5_cc_destroy(context, ccache);
1193 krb5_free_context(context);
1198 get_gss_creds(request_rec *r,
1199 kerb_auth_config *conf,
1200 gss_cred_id_t *server_creds)
1202 gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
1203 OM_uint32 major_status, minor_status, minor_status2;
1204 gss_name_t server_name = GSS_C_NO_NAME;
1206 int have_server_princ;
1209 have_server_princ = conf->krb_service_name && strchr(conf->krb_service_name, '/') != NULL;
1210 if (have_server_princ)
1211 strncpy(buf, conf->krb_service_name, sizeof(buf));
1212 else if (conf->krb_service_name && strcmp(conf->krb_service_name,"Any") == 0) {
1213 *server_creds = GSS_C_NO_CREDENTIAL;
1217 snprintf(buf, sizeof(buf), "%s@%s",
1218 (conf->krb_service_name) ? conf->krb_service_name : SERVICE_NAME,
1219 ap_get_server_name(r));
1222 token.length = strlen(buf) + 1;
1224 major_status = gss_import_name(&minor_status, &token,
1225 (have_server_princ) ? (gss_OID) GSS_KRB5_NT_PRINCIPAL_NAME : (gss_OID) GSS_C_NT_HOSTBASED_SERVICE,
1227 memset(&token, 0, sizeof(token));
1228 if (GSS_ERROR(major_status)) {
1229 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1230 "%s", get_gss_error(r, major_status, minor_status,
1231 "gss_import_name() failed"));
1232 return HTTP_INTERNAL_SERVER_ERROR;
1235 major_status = gss_display_name(&minor_status, server_name, &token, NULL);
1236 if (GSS_ERROR(major_status)) {
1237 /* Perhaps we could just ignore this error but it's safer to give up now,
1239 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1240 "%s", get_gss_error(r, major_status, minor_status,
1241 "gss_display_name() failed"));
1242 return HTTP_INTERNAL_SERVER_ERROR;
1245 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "Acquiring creds for %s",
1247 gss_release_buffer(&minor_status, &token);
1249 major_status = gss_acquire_cred(&minor_status, server_name, GSS_C_INDEFINITE,
1250 GSS_C_NO_OID_SET, GSS_C_ACCEPT,
1251 server_creds, NULL, NULL);
1252 gss_release_name(&minor_status2, &server_name);
1253 if (GSS_ERROR(major_status)) {
1254 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1255 "%s", get_gss_error(r, major_status, minor_status,
1256 "gss_acquire_cred() failed"));
1257 return HTTP_INTERNAL_SERVER_ERROR;
1262 * With MIT Kerberos 5 1.3.x the gss_cred_id_t is the same as
1263 * krb5_gss_cred_id_t and krb5_gss_cred_id_rec contains a pointer to
1265 * This allows us to override the replay cache function vector with
1267 * Note that this is a dirty hack to get things working and there may
1268 * well be unknown side-effects.
1271 krb5_gss_cred_id_t gss_creds = (krb5_gss_cred_id_t) *server_creds;
1273 /* First we try to verify we are linked with 1.3.x to prevent from
1274 crashing when linked with 1.4.x */
1275 if (gss_creds && (gss_creds->usage == GSS_C_ACCEPT)) {
1276 if (gss_creds->rcache && gss_creds->rcache->ops &&
1277 gss_creds->rcache->ops->type &&
1278 memcmp(gss_creds->rcache->ops->type, "dfl", 3) == 0)
1279 /* Override the rcache operations */
1280 gss_creds->rcache->ops = &mod_auth_kerb_rc_ops;
1288 #ifndef GSSAPI_SUPPORTS_SPNEGO
1290 cmp_gss_type(gss_buffer_t token, gss_OID oid)
1295 if (token->length == 0)
1296 return GSS_S_DEFECTIVE_TOKEN;
1300 return GSS_S_DEFECTIVE_TOKEN;
1303 if ((len & 0x7f) > 4)
1304 return GSS_S_DEFECTIVE_TOKEN;
1308 return GSS_S_DEFECTIVE_TOKEN;
1310 if (((OM_uint32) *p++) != oid->length)
1311 return GSS_S_DEFECTIVE_TOKEN;
1313 return memcmp(p, oid->elements, oid->length);
1318 authenticate_user_gss(request_rec *r, kerb_auth_config *conf,
1319 const char *auth_line, char **negotiate_ret_value)
1321 OM_uint32 major_status, minor_status, minor_status2;
1322 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
1323 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
1324 const char *auth_param = NULL;
1326 gss_name_t client_name = GSS_C_NO_NAME;
1327 gss_cred_id_t delegated_cred = GSS_C_NO_CREDENTIAL;
1328 OM_uint32 (KRB5_LIB_FUNCTION *accept_sec_token)
1329 (OM_uint32 *, gss_ctx_id_t *, const gss_cred_id_t,
1330 const gss_buffer_t, const gss_channel_bindings_t,
1331 gss_name_t *, gss_OID *, gss_buffer_t, OM_uint32 *,
1332 OM_uint32 *, gss_cred_id_t *);
1333 gss_OID_desc spnego_oid;
1334 gss_ctx_id_t context = GSS_C_NO_CONTEXT;
1335 gss_cred_id_t server_creds = GSS_C_NO_CREDENTIAL;
1336 OM_uint32 ret_flags = 0;
1338 *negotiate_ret_value = "\0";
1340 spnego_oid.length = 6;
1341 spnego_oid.elements = (void *)"\x2b\x06\x01\x05\x05\x02";
1343 if (conf->krb_5_keytab) {
1345 /* we don't use the ap_* calls here, since the string passed to putenv()
1346 * will become part of the enviroment and shouldn't be free()ed by apache
1348 ktname = malloc(strlen("KRB5_KTNAME=") + strlen(conf->krb_5_keytab) + 1);
1349 if (ktname == NULL) {
1350 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "malloc() failed: not enough memory");
1351 ret = HTTP_INTERNAL_SERVER_ERROR;
1354 sprintf(ktname, "KRB5_KTNAME=%s", conf->krb_5_keytab);
1357 /* Seems to be also supported by latest MIT */
1358 gsskrb5_register_acceptor_identity(conf->krb_5_keytab);
1362 ret = get_gss_creds(r, conf, &server_creds);
1366 /* ap_getword() shifts parameter */
1367 auth_param = ap_getword_white(r->pool, &auth_line);
1368 if (auth_param == NULL) {
1369 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1370 "No Authorization parameter in request from client");
1371 ret = HTTP_UNAUTHORIZED;
1375 input_token.length = apr_base64_decode_len(auth_param) + 1;
1376 input_token.value = apr_pcalloc(r->connection->pool, input_token.length);
1377 if (input_token.value == NULL) {
1378 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1379 "ap_pcalloc() failed (not enough memory)");
1380 ret = HTTP_INTERNAL_SERVER_ERROR;
1383 input_token.length = apr_base64_decode(input_token.value, auth_param);
1385 #ifdef GSSAPI_SUPPORTS_SPNEGO
1386 accept_sec_token = gss_accept_sec_context;
1388 accept_sec_token = (cmp_gss_type(&input_token, &spnego_oid) == 0) ?
1389 gss_accept_sec_context_spnego : gss_accept_sec_context;
1392 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "Verifying client data using KRB5 GSS-API %s",
1393 (accept_sec_token == gss_accept_sec_context)
1395 : "with our SPNEGO lib");
1397 major_status = accept_sec_token(&minor_status,
1401 GSS_C_NO_CHANNEL_BINDINGS,
1408 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1409 "Client %s us their credential",
1410 (ret_flags & GSS_C_DELEG_FLAG) ? "delegated" : "didn't delegate");
1411 if (output_token.length) {
1415 len = apr_base64_encode_len(output_token.length) + 1;
1416 token = apr_pcalloc(r->connection->pool, len + 1);
1417 if (token == NULL) {
1418 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1419 "ap_pcalloc() failed (not enough memory)");
1420 ret = HTTP_INTERNAL_SERVER_ERROR;
1421 gss_release_buffer(&minor_status2, &output_token);
1424 apr_base64_encode(token, output_token.value, output_token.length);
1426 *negotiate_ret_value = token;
1427 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1428 "GSS-API token of length %d bytes will be sent back",
1429 output_token.length);
1430 gss_release_buffer(&minor_status2, &output_token);
1431 set_kerb_auth_headers(r, conf, 0, 0, *negotiate_ret_value);
1434 if (GSS_ERROR(major_status)) {
1435 if (input_token.length > 7 && memcmp(input_token.value, "NTLMSSP", 7) == 0)
1436 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1437 "Warning: received token seems to be NTLM, which isn't supported by the Kerberos module. Check your IE configuration.");
1439 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1440 "%s", get_gss_error(r, major_status, minor_status,
1441 "gss_accept_sec_context() failed"));
1442 /* Don't offer the Negotiate method again if call to GSS layer failed */
1443 *negotiate_ret_value = NULL;
1444 ret = HTTP_UNAUTHORIZED;
1448 /* Multiple authentication rounds aren't supported. If we wanted a generic
1449 * GSS authentication we would have to do some magic with exporting context
1451 if (major_status & GSS_S_CONTINUE_NEEDED) {
1452 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1453 "Multi-iteration authentication isn't supported");
1454 ret = HTTP_UNAUTHORIZED;
1458 major_status = gss_display_name(&minor_status, client_name, &output_token, NULL);
1459 gss_release_name(&minor_status, &client_name);
1460 if (GSS_ERROR(major_status)) {
1461 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1462 "%s", get_gss_error(r, major_status, minor_status,
1463 "gss_display_name() failed"));
1464 ret = HTTP_INTERNAL_SERVER_ERROR;
1468 MK_AUTH_TYPE = MECH_NEGOTIATE;
1469 MK_USER = apr_pstrdup(r->pool, output_token.value);
1471 if (conf->krb_save_credentials && delegated_cred != GSS_C_NO_CREDENTIAL)
1472 store_gss_creds(r, conf, (char *)output_token.value, delegated_cred);
1474 gss_release_buffer(&minor_status, &output_token);
1480 gss_release_cred(&minor_status, &delegated_cred);
1482 if (output_token.length)
1483 gss_release_buffer(&minor_status, &output_token);
1485 if (client_name != GSS_C_NO_NAME)
1486 gss_release_name(&minor_status, &client_name);
1488 if (server_creds != GSS_C_NO_CREDENTIAL)
1489 gss_release_cred(&minor_status, &server_creds);
1491 if (context != GSS_C_NO_CONTEXT)
1492 gss_delete_sec_context(&minor_status, &context, GSS_C_NO_BUFFER);
1498 do_krb5_an_to_ln(request_rec *r) {
1499 krb5_error_code code;
1500 int ret = HTTP_INTERNAL_SERVER_ERROR;
1501 char *MK_USER_LNAME = NULL;
1502 krb5_context kcontext = NULL;
1503 krb5_principal client = NULL;
1505 code = krb5_init_context(&kcontext);
1507 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1508 "Cannot initialize Kerberos5 context (%d)", code);
1512 code = krb5_parse_name(kcontext, MK_USER, &client);
1514 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1515 "krb5_parse_name() failed: %s",
1516 krb5_get_err_text(kcontext, code));
1519 MK_USER_LNAME = apr_pcalloc(r->pool, strlen(MK_USER)+1);
1520 if (MK_USER_LNAME == NULL) {
1521 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1522 "ap_pcalloc() failed (not enough memory)");
1525 code = krb5_aname_to_localname(kcontext, client, strlen(MK_USER), MK_USER_LNAME);
1527 if (code != KRB5_LNAME_NOTRANS) {
1528 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1529 "krb5_aname_to_localname() failed: %s",
1530 krb5_get_err_text(kcontext, code));
1534 log_rerror(APLOG_MARK, APLOG_NOTICE, 0, r,
1535 "krb5_aname_to_localname() found no "
1536 "mapping for principal %s",
1541 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1542 "kerb_authenticate_a_name_to_local_name %s -> %s",
1543 (MK_USER)?MK_USER:"(NULL)", (MK_USER_LNAME)?MK_USER_LNAME:"(NULL)");
1544 MK_USER = apr_pstrdup(r->pool, MK_USER_LNAME);
1549 krb5_free_principal(kcontext, client);
1551 krb5_free_context(kcontext);
1558 static krb5_conn_data *
1559 already_authorized(request_rec *r, char *auth_line)
1561 krb5_conn_data *conn_data;
1564 snprintf(keyname, sizeof(keyname) - 1,
1565 "mod_auth_kerb::connection::%s::%ld", r->connection->remote_ip,
1568 if (apr_pool_userdata_get((void**)&conn_data, keyname, r->connection->pool) != 0)
1572 if(strcmp(conn_data->authline, auth_line) == 0) {
1573 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "matched previous auth request");
1581 save_authorized(request_rec *r, char *auth_line, const char *auth_type, int ret) {
1582 krb5_conn_data *prevauth;
1583 prevauth = (krb5_conn_data *)apr_palloc(r->connection->pool, sizeof(krb5_conn_data));
1587 prevauth->user = apr_pstrdup(r->connection->pool, MK_USER);
1588 prevauth->authline = apr_pstrdup(r->connection->pool, auth_line);
1589 prevauth->mech = apr_pstrdup(r->connection->pool, auth_type);
1590 prevauth->last_return = ret;
1592 snprintf(keyname, sizeof(keyname) - 1,
1593 "mod_auth_kerb::connection::%s::%ld",
1594 r->connection->remote_ip, r->connection->id);
1595 apr_pool_userdata_set(prevauth, keyname, NULL, r->connection->pool);
1599 set_kerb_auth_headers(request_rec *r, const kerb_auth_config *conf,
1600 int use_krb4, int use_krb5pwd, char *negotiate_ret_value)
1602 const char *auth_name = NULL;
1604 char *negoauth_param;
1605 const char *header_name =
1606 (r->proxyreq == PROXYREQ_PROXY) ? "Proxy-Authenticate" : "WWW-Authenticate";
1608 /* get the user realm specified in .htaccess */
1609 auth_name = ap_auth_name(r);
1611 /* XXX should the WWW-Authenticate header be cleared first?
1612 * apache in the proxy mode should retain client's authN headers? */
1614 if (negotiate_ret_value != NULL && conf->krb_method_gssapi) {
1615 negoauth_param = (*negotiate_ret_value == '\0') ? MECH_NEGOTIATE :
1616 apr_pstrcat(r->pool, MECH_NEGOTIATE " ", negotiate_ret_value, NULL);
1617 apr_table_add(r->err_headers_out, header_name, negoauth_param);
1619 if ((use_krb5pwd && conf->krb_method_k5pass) || conf->krb_delegate_basic) {
1620 apr_table_add(r->err_headers_out, header_name,
1621 apr_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1628 ((use_krb4 && conf->krb_method_k4pass) || conf->krb_delegate_basic))
1629 apr_table_add(r->err_headers_out, header_name,
1630 apr_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1635 authenticate_user(request_rec *r, char *auth_line, const char *type, int use_krb4, int use_krb5)
1638 krb5_conn_data *prevauth = NULL;
1639 kerb_auth_config *conf =
1640 (kerb_auth_config *) ap_get_module_config(r->per_dir_config,
1642 char *negotiate_ret_value = NULL;
1643 const char *auth_type = NULL;
1646 set_kerb_auth_headers(r, conf, use_krb4, use_krb5,
1647 (use_krb5) ? "\0" : NULL);
1648 return HTTP_UNAUTHORIZED;
1650 auth_type = ap_getword_white(r->pool, (const char **)&auth_line);
1652 /* If we are delegating Basic to other modules, DECLINE the request */
1653 if (conf->krb_delegate_basic &&
1655 !conf->krb_method_k5pass &&
1658 !conf->krb_method_k4pass &&
1660 (strcasecmp(auth_type, "Basic") == 0))
1662 if ((prevauth = already_authorized(r, auth_line)) == NULL) {
1663 ret = HTTP_UNAUTHORIZED;
1666 if (use_krb5 && conf->krb_method_gssapi &&
1667 strcasecmp(auth_type, MECH_NEGOTIATE) == 0) {
1668 ret = authenticate_user_gss(r, conf, auth_line, &negotiate_ret_value);
1669 } else if (use_krb5 && (conf->krb_method_k5pass || strcasecmp(type, "Basic"))){
1670 ret = authenticate_user_krb5pwd(r, conf, auth_line);
1675 if (ret == HTTP_UNAUTHORIZED && use_krb4 && (conf->krb_method_k4pass || strcasecmp(type, "Basic")))
1676 ret = authenticate_user_krb4pwd(r, conf, auth_line);
1679 if (ret == HTTP_UNAUTHORIZED)
1680 set_kerb_auth_headers(r, conf, use_krb4, use_krb5, negotiate_ret_value);
1683 ret = prevauth->last_return;
1684 MK_USER = prevauth->user;
1685 MK_AUTH_TYPE = prevauth->mech;
1689 * save who was auth'd, if it's not already stashed.
1692 save_authorized(r, auth_line, auth_type, ret);
1695 if (ret == OK && conf->krb5_do_auth_to_local) {
1696 ret = do_krb5_an_to_ln(r);
1701 static authn_status authn_krb_password(request_rec *r, const char *user,
1702 const char *password)
1704 char *auth_line = NULL;
1706 const char *type = NULL;
1708 type = ap_auth_type(r);
1709 auth_line = ap_pbase64encode (r->pool, apr_psprintf(r->pool, "%s:%s", user, password));
1710 auth_line = apr_psprintf(r->pool, "Basic %s", auth_line);
1712 ret = authenticate_user(r, auth_line, type, 1, 1);
1714 if (ret == OK) return AUTH_GRANTED;
1715 else return AUTH_USER_NOT_FOUND;
1719 kerb_authenticate_user(request_rec *r)
1721 kerb_auth_config *conf =
1722 (kerb_auth_config *) ap_get_module_config(r->per_dir_config,
1724 char *auth_line = NULL;
1725 int ret, use_krb4 = 0, use_krb5 = 0;
1726 const char *type = NULL;
1728 /* get the type specified in .htaccess */
1729 type = ap_auth_type(r);
1731 log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1732 "kerb_authenticate_user entered with user %s and auth_type %s",
1733 (MK_USER)?MK_USER:"(NULL)",type?type:"(NULL)");
1735 if (type && strcasecmp(type, "Kerberos") == 0)
1736 use_krb5 = use_krb4 = 1;
1737 else if(type && strcasecmp(type, "KerberosV5") == 0)
1739 else if(type && strcasecmp(type, "KerberosV4") == 0)
1745 if (conf->krb_ssl_preauthentication) {
1746 const char *ssl_client_verify = ssl_var_lookup(r->pool, r->server,
1747 r->connection, r, "SSL_CLIENT_VERIFY");
1749 if (ssl_client_verify && strcmp(ssl_client_verify, "SUCCESS") == 0)
1754 /* get what the user sent us in the HTTP header */
1755 auth_line = (char *)MK_TABLE_GET(r->headers_in, (r->proxyreq == PROXYREQ_PROXY)
1756 ? "Proxy-Authorization"
1758 ret = authenticate_user(r, auth_line, type, use_krb4, use_krb5);
1764 have_rcache_type(const char *type)
1766 krb5_error_code ret;
1767 krb5_context context;
1768 krb5_rcache id = NULL;
1771 ret = krb5_init_context(&context);
1775 ret = krb5_rc_resolve_full(context, &id, "none:");
1779 krb5_rc_destroy(context, id);
1780 krb5_free_context(context);
1785 /***************************************************************************
1786 Module Setup/Configuration
1787 ***************************************************************************/
1788 #ifndef STANDARD20_MODULE_STUFF
1790 kerb_module_init(server_rec *dummy, pool *p)
1793 /* Suppress the MIT replay cache. Requires MIT Kerberos 1.4.0 or later.
1794 1.3.x are covered by the hack overiding the replay calls */
1795 if (getenv("KRB5RCACHETYPE") == NULL && have_rcache_type("none"))
1796 putenv(strdup("KRB5RCACHETYPE=none"));
1800 module MODULE_VAR_EXPORT auth_kerb_module = {
1801 STANDARD_MODULE_STUFF,
1802 kerb_module_init, /* module initializer */
1803 kerb_dir_create_config, /* per-directory config creator */
1804 NULL, /* per-directory config merger */
1805 NULL, /* per-server config creator */
1806 NULL, /* per-server config merger */
1807 kerb_auth_cmds, /* command table */
1808 NULL, /* [ 9] content handlers */
1809 NULL, /* [ 2] URI-to-filename translation */
1810 kerb_authenticate_user, /* [ 5] check/validate user_id */
1811 NULL, /* [ 6] check user_id is valid *here* */
1812 NULL, /* [ 4] check access by host address */
1813 NULL, /* [ 7] MIME type checker/setter */
1814 NULL, /* [ 8] fixups */
1815 NULL, /* [10] logger */
1816 NULL, /* [ 3] header parser */
1817 NULL, /* process initialization */
1818 NULL, /* process exit/cleanup */
1819 NULL /* [ 1] post read_request handling */
1821 ,NULL, /* EAPI: add_module */
1822 NULL, /* EAPI: remove_module */
1823 NULL, /* EAPI: rewrite_command */
1824 NULL /* EAPI: new_connection */
1829 kerb_init_handler(apr_pool_t *p, apr_pool_t *plog,
1830 apr_pool_t *ptemp, server_rec *s)
1832 ap_add_version_component(p, "mod_auth_kerb/" MODAUTHKERB_VERSION);
1834 /* Suppress the MIT replay cache. Requires MIT Kerberos 1.4.0 or later.
1835 1.3.x are covered by the hack overiding the replay calls */
1836 if (getenv("KRB5RCACHETYPE") == NULL && have_rcache_type("none"))
1837 putenv(strdup("KRB5RCACHETYPE=none"));
1844 kerb_register_hooks(apr_pool_t *p)
1847 static const authn_provider authn_krb_provider = {
1848 &authn_krb_password,
1852 ap_register_provider(p, AUTHN_PROVIDER_GROUP, "kerberos", "0", &authn_krb_provider);
1854 ap_hook_post_config(kerb_init_handler, NULL, NULL, APR_HOOK_MIDDLE);
1855 ap_hook_check_user_id(kerb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
1858 module AP_MODULE_DECLARE_DATA auth_kerb_module =
1860 STANDARD20_MODULE_STUFF,
1861 kerb_dir_create_config, /* create per-dir conf structures */
1862 NULL, /* merge per-dir conf structures */
1863 NULL, /* create per-server conf structures */
1864 NULL, /* merge per-server conf structures */
1865 kerb_auth_cmds, /* table of configuration directives */
1866 kerb_register_hooks /* register hooks */