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>
69 # include <gssapi/gssapi.h>
70 # include <gssapi/gssapi_generic.h>
71 # define GSS_C_NT_USER_NAME gss_nt_user_name
72 # define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
73 # define krb5_get_err_text(context,code) error_message(code)
75 #include "spnegokrb5.h"
79 /*Prevent warning about closesocket redefinition (Apache's ap_config.h and
80 * MIT Kerberos' port-sockets.h both define it as close) */
85 #include <netdb.h> /* gethostbyname() */
88 #ifdef STANDARD20_MODULE_STUFF
89 module AP_MODULE_DECLARE_DATA auth_kerb_module;
91 module auth_kerb_module;
94 /***************************************************************************
95 Macros To Ease Compatibility
96 ***************************************************************************/
97 #ifdef STANDARD20_MODULE_STUFF
98 #define MK_POOL apr_pool_t
99 #define MK_TABLE_GET apr_table_get
100 #define MK_USER r->user
101 #define MK_AUTH_TYPE r->ap_auth_type
104 #define MK_TABLE_GET ap_table_get
105 #define MK_USER r->connection->user
106 #define MK_AUTH_TYPE r->connection->ap_auth_type
107 #define PROXYREQ_PROXY STD_PROXY
110 /***************************************************************************
111 Auth Configuration Structure
112 ***************************************************************************/
114 char *krb_auth_realms;
115 int krb_save_credentials;
117 char *krb_service_name;
118 int krb_authoritative;
121 int krb_method_gssapi;
122 int krb_method_k5pass;
126 int krb_method_k4pass;
131 set_kerb_auth_headers(request_rec *r, const kerb_auth_config *conf,
132 int use_krb4, int use_krb5pwd, char *negotiate_ret_value);
135 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg);
137 #ifdef STANDARD20_MODULE_STUFF
138 #define command(name, func, var, type, usage) \
139 AP_INIT_ ## type (name, func, \
140 (void*)APR_XtOffsetOf(kerb_auth_config, var), \
143 #define command(name, func, var, type, usage) \
145 (void*)XtOffsetOf(kerb_auth_config, var), \
146 OR_AUTHCFG, type, usage }
149 static const command_rec kerb_auth_cmds[] = {
150 command("KrbAuthRealms", krb5_save_realms, krb_auth_realms,
151 RAW_ARGS, "Realms to attempt authentication against (can be multiple)."),
153 command("KrbAuthRealm", krb5_save_realms, krb_auth_realms,
154 RAW_ARGS, "Alias for KrbAuthRealms."),
156 command("KrbSaveCredentials", ap_set_flag_slot, krb_save_credentials,
157 FLAG, "Save and store credentials/tickets retrieved during auth."),
159 command("KrbVerifyKDC", ap_set_flag_slot, krb_verify_kdc,
160 FLAG, "Verify tickets against keytab to prevent KDC spoofing attacks."),
162 command("KrbServiceName", ap_set_string_slot, krb_service_name,
163 TAKE1, "Service name to be used by Apache for authentication."),
165 command("KrbAuthoritative", ap_set_flag_slot, krb_authoritative,
166 FLAG, "Set to 'off' to allow access control to be passed along to lower modules if the UserID is not known to this module."),
169 command("Krb5Keytab", ap_set_file_slot, krb_5_keytab,
170 TAKE1, "Location of Kerberos V5 keytab file."),
172 command("KrbMethodNegotiate", ap_set_flag_slot, krb_method_gssapi,
173 FLAG, "Enable Negotiate authentication method."),
175 command("KrbMethodK5Passwd", ap_set_flag_slot, krb_method_k5pass,
176 FLAG, "Enable Kerberos V5 password authentication."),
180 command("Krb4Srvtab", ap_set_file_slot, krb_4_srvtab,
181 TAKE1, "Location of Kerberos V4 srvtab file."),
183 command("KrbMethodK4Passwd", ap_set_flag_slot, krb_method_k4pass,
184 FLAG, "Enable Kerberos V4 password authentication."),
192 gss_ctx_id_t context;
193 gss_cred_id_t server_creds;
196 static gss_connection_t *gss_connection = NULL;
200 /***************************************************************************
201 Auth Configuration Initialization
202 ***************************************************************************/
203 static void *kerb_dir_create_config(MK_POOL *p, char *d)
205 kerb_auth_config *rec;
207 rec = (kerb_auth_config *) ap_pcalloc(p, sizeof(kerb_auth_config));
208 ((kerb_auth_config *)rec)->krb_verify_kdc = 1;
209 ((kerb_auth_config *)rec)->krb_service_name = "HTTP";
210 ((kerb_auth_config *)rec)->krb_authoritative = 1;
212 ((kerb_auth_config *)rec)->krb_method_k5pass = 1;
213 ((kerb_auth_config *)rec)->krb_method_gssapi = 1;
216 ((kerb_auth_config *)rec)->krb_method_k4pass = 1;
222 krb5_save_realms(cmd_parms *cmd, kerb_auth_config *sec, char *arg)
224 sec->krb_auth_realms= ap_pstrdup(cmd->pool, arg);
228 void log_rerror(const char *file, int line, int level, int status,
229 const request_rec *r, const char *fmt, ...)
235 vsnprintf(errstr, sizeof(errstr), fmt, ap);
239 #ifdef STANDARD20_MODULE_STUFF
240 ap_log_rerror(file, line, level | APLOG_NOERRNO, status, r, "%s", errstr);
242 ap_log_rerror(file, line, level | APLOG_NOERRNO, r, "%s", errstr);
247 /***************************************************************************
248 Username/Password Validation for Krb4
249 ***************************************************************************/
251 verify_krb4_user(request_rec *r, char *name, char *instance, char *realm,
252 char *password, char *linstance, char *srvtab, int krb_verify_kdc)
258 const char *hostname;
261 char lrealm[REALM_SZ];
263 ret = krb_get_pw_in_tkt(name, instance, realm, "krbtgt", realm,
264 DEFAULT_TKT_LIFE, password);
266 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
267 "Cannot get krb4 ticket: krb_get_pw_in_tkt() failed: %s",
268 krb_get_err_text(ret));
275 hostname = ap_get_server_name(r);
277 hp = gethostbyname(hostname);
280 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
281 "Cannot verify krb4 ticket: gethostbyname() failed: %s",
285 memcpy(&addr, hp->h_addr, sizeof(addr));
287 phost = krb_get_phost((char *)hostname);
289 krb_get_lrealm(lrealm, 1);
291 ret = krb_mk_req(&ticket, linstance, phost, lrealm, 0);
293 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
294 "Cannot verify krb4 ticket: krb_mk_req() failed: %s",
295 krb_get_err_text(ret));
300 ret = krb_rd_req(&ticket, linstance, phost, addr, &authdata, srvtab);
302 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
303 "Cannot verify krb4 ticket: krb_rd_req() failed: %s",
304 krb_get_err_text(ret));
312 krb4_cache_cleanup(void *data)
314 char *tkt_file = (char *) data;
316 krb_set_tkt_string(tkt_file);
322 authenticate_user_krb4pwd(request_rec *r,
323 kerb_auth_config *conf,
324 const char *auth_line)
328 const char *sent_name;
331 char *tkt_file_p = NULL;
336 char lrealm[REALM_SZ];
337 int all_principals_unkown;
339 sent_pw = ap_pbase64decode(r->pool, auth_line);
340 sent_name = ap_getword (r->pool, &sent_pw, ':');
342 /* do not allow user to override realm setting of server */
343 if (strchr(sent_name, '@')) {
344 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
345 "specifying realm in user name is prohibited");
346 return HTTP_UNAUTHORIZED;
349 sent_instance = strchr(sent_name, '.');
351 *sent_instance++ = '\0';
353 snprintf(tkt_file, sizeof(tkt_file), "/tmp/apache_tkt_XXXXXX");
354 fd = mkstemp(tkt_file);
356 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
357 "Cannot create krb4 ccache: mkstemp() failed: %s",
359 return HTTP_INTERNAL_SERVER_ERROR;
362 tkt_file_p = ap_pstrdup(r->pool, tkt_file);
363 ap_register_cleanup(r->pool, tkt_file_p,
364 krb4_cache_cleanup, ap_null_cleanup);
366 krb_set_tkt_string(tkt_file);
368 all_principals_unkown = 1;
369 realms = conf->krb_auth_realms;
371 memset(lrealm, 0, sizeof(lrealm));
374 realm = ap_getword_white(r->pool, &realms);
377 ret = krb_get_lrealm(lrealm, 1);
383 ret = verify_krb4_user(r, (char *)sent_name,
384 (sent_instance) ? sent_instance : "",
385 (char *)realm, (char *)sent_pw,
386 conf->krb_service_name,
387 conf->krb_4_srvtab, conf->krb_verify_kdc);
388 if (!conf->krb_authoritative && ret) {
389 /* if we're not authoritative, we allow authentication to pass on
390 * to another modules if (and only if) the user is not known to us */
391 if (all_principals_unkown && ret != KDC_PR_UNKNOWN)
392 all_principals_unkown = 0;
397 } while (realms && *realms);
400 /* XXX log only in the verify_krb4_user() call */
401 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Verifying krb4 password failed");
402 ret = (!conf->krb_authoritative && all_principals_unkown == 1 && ret == KDC_PR_UNKNOWN) ?
403 DECLINED : HTTP_UNAUTHORIZED;
407 user = ap_pstrdup(r->pool, sent_name);
409 user = ap_pstrcat(r->pool, user, ".", sent_instance, NULL);
410 user = ap_pstrcat(r->pool, user, "@", realm, NULL);
413 MK_AUTH_TYPE = "Basic";
414 ap_table_setn(r->subprocess_env, "KRBTKFILE", tkt_file_p);
416 if (!conf->krb_save_credentials)
417 krb4_cache_cleanup(tkt_file);
421 krb4_cache_cleanup(tkt_file);
430 /***************************************************************************
431 Username/Password Validation for Krb5
432 ***************************************************************************/
433 /* Inspired by krb5_verify_user from Heimdal */
434 static krb5_error_code
435 verify_krb5_user(request_rec *r, krb5_context context, krb5_principal principal,
436 krb5_ccache ccache, const char *password, const char *service,
437 krb5_keytab keytab, int krb_verify_kdc)
440 krb5_principal server = NULL;
442 krb5_verify_init_creds_opt opt;
444 /* XXX error messages shouldn't be logged here (and in the while() loop in
445 * authenticate_user_krb5pwd() as weell), in order to avoid confusing log
446 * entries when using multiple realms */
448 memset(&creds, 0, sizeof(creds));
450 ret = krb5_get_init_creds_password(context, &creds, principal,
451 (char *)password, NULL,
452 NULL, 0, NULL, NULL);
454 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
455 "krb5_get_init_creds_password() failed: %s",
456 krb5_get_err_text(context, ret));
460 ret = krb5_sname_to_principal(context, ap_get_server_name(r), service,
461 KRB5_NT_UNKNOWN, &server);
463 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
464 "krb5_sname_to_principal() failed: %s",
465 krb5_get_err_text(context, ret));
469 krb5_verify_init_creds_opt_init(&opt);
470 krb5_verify_init_creds_opt_set_ap_req_nofail(&opt, krb_verify_kdc);
472 ret = krb5_verify_init_creds(context, &creds, server, keytab, NULL, &opt);
474 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
475 "krb5_verify_init_creds() failed: %s",
476 krb5_get_err_text(context, ret));
481 ret = krb5_cc_initialize(context, ccache, principal);
483 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
484 "krb5_cc_initialize() failed: %s",
485 krb5_get_err_text(context, ret));
489 ret = krb5_cc_store_cred(context, ccache, &creds);
491 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
492 "krb5_cc_store_cred() failed: %s",
493 krb5_get_err_text(context, ret));
499 krb5_free_cred_contents(context, &creds);
501 krb5_free_principal(context, server);
506 krb5_cache_cleanup(void *data)
508 krb5_context context;
510 krb5_error_code problem;
511 char *cache_name = (char *) data;
513 problem = krb5_init_context(&context);
515 /* ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "krb5_init_context() failed"); */
516 return HTTP_INTERNAL_SERVER_ERROR;
519 problem = krb5_cc_resolve(context, cache_name, &cache);
521 /* log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
522 "krb5_cc_resolve() failed (%s: %s)",
523 cache_name, krb5_get_err_text(context, problem)); */
524 return HTTP_INTERNAL_SERVER_ERROR;
527 krb5_cc_destroy(context, cache);
528 krb5_free_context(context);
533 create_krb5_ccache(krb5_context kcontext,
535 kerb_auth_config *conf,
536 krb5_principal princ,
541 krb5_error_code problem;
543 krb5_ccache tmp_ccache = NULL;
545 ccname = ap_psprintf(r->pool, "FILE:%s/krb5cc_apache_XXXXXX", P_tmpdir);
546 fd = mkstemp(ccname + strlen("FILE:"));
548 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
549 "mkstemp() failed: %s", strerror(errno));
550 ret = HTTP_INTERNAL_SERVER_ERROR;
555 problem = krb5_cc_resolve(kcontext, ccname, &tmp_ccache);
557 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
558 "krb5_cc_resolve() failed: %s",
559 krb5_get_err_text(kcontext, problem));
560 ret = HTTP_INTERNAL_SERVER_ERROR;
565 problem = krb5_cc_initialize(kcontext, tmp_ccache, princ);
567 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
568 "Cannot initialize krb5 ccache %s: krb5_cc_initialize() failed: %s",
569 ccname, krb5_get_err_text(kcontext, problem));
570 ret = HTTP_INTERNAL_SERVER_ERROR;
574 ap_table_setn(r->subprocess_env, "KRB5CCNAME", ccname);
575 ap_register_cleanup(r->pool, ccname,
576 krb5_cache_cleanup, ap_null_cleanup);
578 *ccache = tmp_ccache;
585 krb5_cc_destroy(kcontext, tmp_ccache);
591 store_krb5_creds(krb5_context kcontext,
593 kerb_auth_config *conf,
594 krb5_ccache delegated_cred)
597 krb5_error_code problem;
598 krb5_principal princ;
602 problem = krb5_cc_get_principal(kcontext, delegated_cred, &princ);
604 snprintf(errstr, sizeof(errstr), "krb5_cc_get_principal() failed: %s",
605 krb5_get_err_text(kcontext, problem));
606 return HTTP_INTERNAL_SERVER_ERROR;
609 ret = create_krb5_ccache(kcontext, r, conf, princ, &ccache);
611 krb5_free_principal(kcontext, princ);
616 problem = krb5_cc_copy_cache(kcontext, delegated_cred, ccache);
618 problem = krb5_cc_copy_creds(kcontext, delegated_cred, ccache);
620 krb5_free_principal(kcontext, princ);
622 snprintf(errstr, sizeof(errstr), "Failed to store credentials: %s",
623 krb5_get_err_text(kcontext, problem));
624 krb5_cc_destroy(kcontext, ccache);
625 return HTTP_INTERNAL_SERVER_ERROR;
628 krb5_cc_close(kcontext, ccache);
633 int authenticate_user_krb5pwd(request_rec *r,
634 kerb_auth_config *conf,
635 const char *auth_line)
637 const char *sent_pw = NULL;
638 const char *sent_name = NULL;
639 const char *realms = NULL;
640 krb5_context kcontext = NULL;
641 krb5_error_code code;
642 krb5_principal client = NULL;
643 krb5_ccache ccache = NULL;
644 krb5_keytab keytab = NULL;
647 int all_principals_unkown;
651 code = krb5_init_context(&kcontext);
653 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
654 "Cannot initialize Kerberos5 context (%d)", code);
655 return HTTP_INTERNAL_SERVER_ERROR;
658 sent_pw = ap_pbase64decode(r->pool, auth_line);
659 sent_name = ap_getword (r->pool, &sent_pw, ':');
660 /* do not allow user to override realm setting of server */
661 if (strchr(sent_name, '@')) {
662 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
663 "specifying realm in user name is prohibited");
664 ret = HTTP_UNAUTHORIZED;
668 if (sent_pw == NULL || *sent_pw == '\0') {
669 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
670 "empty passwords are not accepted");
671 ret = HTTP_UNAUTHORIZED;
675 code = krb5_cc_resolve(kcontext, "MEMORY:", &ccache);
677 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
678 "generating new memory ccache failed: %s",
679 krb5_get_err_text(kcontext, code));
680 ret = HTTP_INTERNAL_SERVER_ERROR;
685 if (conf->krb_5_keytab)
686 krb5_kt_resolve(kcontext, conf->krb_5_keytab, &keytab);
688 all_principals_unkown = 1;
689 realms = conf->krb_auth_realms;
691 if (realms && (code = krb5_set_default_realm(kcontext,
692 ap_getword_white(r->pool, &realms)))){
693 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
694 "krb5_set_default_realm() failed: %s",
695 krb5_get_err_text(kcontext, code));
700 krb5_free_principal(kcontext, client);
703 code = krb5_parse_name(kcontext, sent_name, &client);
705 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
706 "krb5_parse_name() failed: %s",
707 krb5_get_err_text(kcontext, code));
711 code = verify_krb5_user(r, kcontext, client, ccache, sent_pw,
712 conf->krb_service_name,
713 keytab, conf->krb_verify_kdc);
714 if (!conf->krb_authoritative && code) {
715 /* if we're not authoritative, we allow authentication to pass on
716 * to another modules if (and only if) the user is not known to us */
717 if (all_principals_unkown && code != KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
718 all_principals_unkown = 0;
724 /* ap_getword_white() used above shifts the parameter, so it's not
725 needed to touch the realms variable */
726 } while (realms && *realms);
728 memset((char *)sent_pw, 0, strlen(sent_pw));
731 if (!conf->krb_authoritative && all_principals_unkown == 1 && code == KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
734 ret = HTTP_UNAUTHORIZED;
739 code = krb5_unparse_name(kcontext, client, &name);
741 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "krb5_unparse_name() failed: %s",
742 krb5_get_err_text(kcontext, code));
743 ret = HTTP_UNAUTHORIZED;
746 MK_USER = ap_pstrdup (r->pool, name);
747 MK_AUTH_TYPE = "Basic";
750 if (conf->krb_save_credentials)
751 store_krb5_creds(kcontext, r, conf, ccache);
757 krb5_free_principal(kcontext, client);
759 krb5_cc_destroy(kcontext, ccache);
761 krb5_kt_close(kcontext, keytab);
762 krb5_free_context(kcontext);
767 /*********************************************************************
768 * GSSAPI Authentication
769 ********************************************************************/
772 get_gss_error(MK_POOL *p, OM_uint32 err_maj, OM_uint32 err_min, char *prefix)
774 OM_uint32 maj_stat, min_stat;
775 OM_uint32 msg_ctx = 0;
776 gss_buffer_desc status_string;
780 err_msg = ap_pstrdup(p, prefix);
782 maj_stat = gss_display_status (&min_stat,
788 if (GSS_ERROR(maj_stat))
790 err_msg = ap_pstrcat(p, err_msg, ": ", (char*) status_string.value, NULL);
791 gss_release_buffer(&min_stat, &status_string);
793 maj_stat = gss_display_status (&min_stat,
799 if (!GSS_ERROR(maj_stat)) {
800 err_msg = ap_pstrcat(p, err_msg,
801 " (", (char*) status_string.value, ")", NULL);
802 gss_release_buffer(&min_stat, &status_string);
804 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
810 cleanup_gss_connection(void *data)
812 OM_uint32 minor_status;
813 gss_connection_t *gss_conn = (gss_connection_t *)data;
817 if (gss_conn->context != GSS_C_NO_CONTEXT)
818 gss_delete_sec_context(&minor_status, &gss_conn->context,
820 if (gss_conn->server_creds != GSS_C_NO_CREDENTIAL)
821 gss_release_cred(&minor_status, &gss_conn->server_creds);
823 gss_connection = NULL;
829 store_gss_creds(request_rec *r, kerb_auth_config *conf, char *princ_name,
830 gss_cred_id_t delegated_cred)
832 OM_uint32 maj_stat, min_stat;
833 krb5_principal princ = NULL;
834 krb5_ccache ccache = NULL;
835 krb5_error_code problem;
836 krb5_context context;
837 int ret = HTTP_INTERNAL_SERVER_ERROR;
839 problem = krb5_init_context(&context);
841 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Cannot initialize krb5 context");
842 return HTTP_INTERNAL_SERVER_ERROR;
845 problem = krb5_parse_name(context, princ_name, &princ);
847 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
848 "Cannot parse delegated username (%s)", krb5_get_err_text(context, problem));
852 problem = create_krb5_ccache(context, r, conf, princ, &ccache);
854 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
855 "Cannot create krb5 ccache (%s)", krb5_get_err_text(context, problem));
859 maj_stat = gss_krb5_copy_ccache(&min_stat, delegated_cred, ccache);
860 if (GSS_ERROR(maj_stat)) {
861 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
862 "Cannot store delegated credential (%s)",
863 get_gss_error(r->pool, maj_stat, min_stat, "gss_krb5_copy_ccache"));
867 krb5_cc_close(context, ccache);
873 krb5_free_principal(context, princ);
875 krb5_cc_destroy(context, ccache);
876 krb5_free_context(context);
881 get_gss_creds(request_rec *r,
882 kerb_auth_config *conf,
883 gss_cred_id_t *server_creds)
885 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
886 OM_uint32 major_status, minor_status, minor_status2;
887 gss_name_t server_name = GSS_C_NO_NAME;
891 /* Don't specify service name. This makes MIT 1.3 not to use replay caches,
892 * which causes large problems with the Microsoft krb5 implementation. MS
893 * obviously uses a format of the krb5 authenticator that is considered by
894 * the MIT as replay (Two valid MS authenticators may contain the same time
895 * and utime fields and only differ in the sequential numbers).
897 snprintf(buf, sizeof(buf), "%s@%s", conf->krb_service_name,
898 ap_get_server_name(r));
900 input_token.value = buf;
901 input_token.length = strlen(buf) + 1;
903 major_status = gss_import_name(&minor_status, &input_token,
904 GSS_C_NT_HOSTBASED_SERVICE,
906 if (GSS_ERROR(major_status)) {
907 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
908 "%s", get_gss_error(r->pool, major_status, minor_status,
909 "gss_import_name() failed"));
910 return HTTP_INTERNAL_SERVER_ERROR;
914 major_status = gss_acquire_cred(&minor_status, server_name, GSS_C_INDEFINITE,
915 GSS_C_NO_OID_SET, GSS_C_ACCEPT,
916 server_creds, NULL, NULL);
917 gss_release_name(&minor_status2, &server_name);
918 if (GSS_ERROR(major_status)) {
919 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
920 "%s", get_gss_error(r->pool, major_status, minor_status,
921 "gss_acquire_cred() failed"));
922 return HTTP_INTERNAL_SERVER_ERROR;
929 cmp_gss_type(gss_buffer_t token, gss_OID oid)
934 if (token->length == 0)
935 return GSS_S_DEFECTIVE_TOKEN;
939 return GSS_S_DEFECTIVE_TOKEN;
942 if ((len & 0x7f) > 4)
943 return GSS_S_DEFECTIVE_TOKEN;
947 return GSS_S_DEFECTIVE_TOKEN;
949 if (((OM_uint32) *p++) != oid->length)
950 return GSS_S_DEFECTIVE_TOKEN;
952 return memcmp(p, oid->elements, oid->length);
956 authenticate_user_gss(request_rec *r, kerb_auth_config *conf,
957 const char *auth_line, char **negotiate_ret_value)
959 OM_uint32 major_status, minor_status, minor_status2;
960 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
961 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
962 const char *auth_param = NULL;
964 gss_name_t client_name = GSS_C_NO_NAME;
965 gss_cred_id_t delegated_cred = GSS_C_NO_CREDENTIAL;
966 OM_uint32 (*accept_sec_token)();
967 gss_OID_desc spnego_oid;
969 *negotiate_ret_value = "\0";
971 spnego_oid.length = 6;
972 spnego_oid.elements = (void *)"\x2b\x06\x01\x05\x05\x02";
974 if (gss_connection == NULL) {
975 gss_connection = ap_pcalloc(r->connection->pool, sizeof(*gss_connection));
976 if (gss_connection == NULL) {
977 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
978 "ap_pcalloc() failed (not enough memory)");
979 ret = HTTP_INTERNAL_SERVER_ERROR;
982 memset(gss_connection, 0, sizeof(*gss_connection));
983 ap_register_cleanup(r->connection->pool, gss_connection, cleanup_gss_connection, ap_null_cleanup);
986 if (conf->krb_5_keytab) {
988 /* we don't use the ap_* calls here, since the string passed to putenv()
989 * will become part of the enviroment and shouldn't be free()ed by apache
991 ktname = malloc(strlen("KRB5_KTNAME=") + strlen(conf->krb_5_keytab) + 1);
992 if (ktname == NULL) {
993 log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "malloc() failed: not enough memory");
994 ret = HTTP_INTERNAL_SERVER_ERROR;
997 sprintf(ktname, "KRB5_KTNAME=%s", conf->krb_5_keytab);
1001 if (gss_connection->server_creds == GSS_C_NO_CREDENTIAL) {
1002 ret = get_gss_creds(r, conf, &gss_connection->server_creds);
1007 /* ap_getword() shifts parameter */
1008 auth_param = ap_getword_white(r->pool, &auth_line);
1009 if (auth_param == NULL) {
1010 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1011 "No Authorization parameter in request from client");
1012 ret = HTTP_UNAUTHORIZED;
1016 input_token.length = ap_base64decode_len(auth_param) + 1;
1017 input_token.value = ap_pcalloc(r->connection->pool, input_token.length);
1018 if (input_token.value == NULL) {
1019 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1020 "ap_pcalloc() failed (not enough memory)");
1021 ret = HTTP_INTERNAL_SERVER_ERROR;
1024 input_token.length = ap_base64decode(input_token.value, auth_param);
1026 accept_sec_token = (cmp_gss_type(&input_token, &spnego_oid) == 0) ?
1027 gss_accept_sec_context_spnego : gss_accept_sec_context;
1029 major_status = accept_sec_token(&minor_status,
1030 &gss_connection->context,
1031 gss_connection->server_creds,
1033 GSS_C_NO_CHANNEL_BINDINGS,
1040 if (output_token.length) {
1044 len = ap_base64encode_len(output_token.length) + 1;
1045 token = ap_pcalloc(r->connection->pool, len + 1);
1046 if (token == NULL) {
1047 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1048 "ap_pcalloc() failed (not enough memory)");
1049 ret = HTTP_INTERNAL_SERVER_ERROR;
1050 gss_release_buffer(&minor_status2, &output_token);
1053 ap_base64encode(token, output_token.value, output_token.length);
1055 *negotiate_ret_value = token;
1056 gss_release_buffer(&minor_status2, &output_token);
1059 if (GSS_ERROR(major_status)) {
1060 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1061 "%s", get_gss_error(r->pool, major_status, minor_status,
1062 "gss_accept_sec_context() failed"));
1063 /* Don't offer the Negotiate method again if call to GSS layer failed */
1064 *negotiate_ret_value = NULL;
1065 ret = HTTP_UNAUTHORIZED;
1069 if (major_status & GSS_S_CONTINUE_NEEDED) {
1070 /* Some GSSAPI mechanism (eg GSI from Globus) may require multiple
1071 * iterations to establish authentication */
1072 ret = HTTP_UNAUTHORIZED;
1076 major_status = gss_display_name(&minor_status, client_name, &output_token, NULL);
1077 gss_release_name(&minor_status, &client_name);
1078 if (GSS_ERROR(major_status)) {
1079 log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1080 "%s", get_gss_error(r->pool, major_status, minor_status,
1081 "gss_export_name() failed"));
1082 ret = HTTP_INTERNAL_SERVER_ERROR;
1086 MK_AUTH_TYPE = "Negotiate";
1087 MK_USER = ap_pstrdup(r->pool, output_token.value);
1089 if (conf->krb_save_credentials && delegated_cred != GSS_C_NO_CREDENTIAL)
1090 store_gss_creds(r, conf, (char *)output_token.value, delegated_cred);
1092 if (*negotiate_ret_value)
1093 set_kerb_auth_headers(r, conf, 0, 0, *negotiate_ret_value);
1095 gss_release_buffer(&minor_status, &output_token);
1101 gss_release_cred(&minor_status, &delegated_cred);
1103 if (output_token.length)
1104 gss_release_buffer(&minor_status, &output_token);
1106 if (client_name != GSS_C_NO_NAME)
1107 gss_release_name(&minor_status, &client_name);
1109 if (! major_status & GSS_S_CONTINUE_NEEDED)
1110 cleanup_gss_connection(gss_connection);
1117 already_succeeded(request_rec *r)
1119 if (ap_is_initial_req(r) || MK_AUTH_TYPE == NULL)
1121 if (strcmp(MK_AUTH_TYPE, "Negotiate") ||
1122 (strcmp(MK_AUTH_TYPE, "Basic") && strchr(MK_USER, '@')))
1128 set_kerb_auth_headers(request_rec *r, const kerb_auth_config *conf,
1129 int use_krb4, int use_krb5pwd, char *negotiate_ret_value)
1131 const char *auth_name = NULL;
1133 char *negoauth_param;
1134 const char *header_name =
1135 (r->proxyreq == PROXYREQ_PROXY) ? "Proxy-Authenticate" : "WWW-Authenticate";
1137 /* get the user realm specified in .htaccess */
1138 auth_name = ap_auth_name(r);
1140 /* XXX should the WWW-Authenticate header be cleared first? */
1142 if (negotiate_ret_value != NULL && conf->krb_method_gssapi) {
1143 negoauth_param = (*negotiate_ret_value == '\0') ? "Negotiate" :
1144 ap_pstrcat(r->pool, "Negotiate ", negotiate_ret_value, NULL);
1145 ap_table_add(r->err_headers_out, header_name, negoauth_param);
1147 if (use_krb5pwd && conf->krb_method_k5pass) {
1148 ap_table_add(r->err_headers_out, header_name,
1149 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1155 if (use_krb4 && conf->krb_method_k4pass && !set_basic)
1156 ap_table_add(r->err_headers_out, header_name,
1157 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
1161 int kerb_authenticate_user(request_rec *r)
1163 kerb_auth_config *conf =
1164 (kerb_auth_config *) ap_get_module_config(r->per_dir_config,
1166 const char *auth_type = NULL;
1167 const char *auth_line = NULL;
1168 const char *type = NULL;
1169 int use_krb5 = 0, use_krb4 = 0;
1171 static int last_return = HTTP_UNAUTHORIZED;
1172 char *negotiate_ret_value = NULL;
1174 /* get the type specified in .htaccess */
1175 type = ap_auth_type(r);
1177 if (type && strcasecmp(type, "Kerberos") == 0)
1178 use_krb5 = use_krb4 = 1;
1179 else if(type && strcasecmp(type, "KerberosV5") == 0)
1181 else if(type && strcasecmp(type, "KerberosV4") == 0)
1186 /* get what the user sent us in the HTTP header */
1187 auth_line = MK_TABLE_GET(r->headers_in, "Authorization");
1189 auth_line = MK_TABLE_GET(r->headers_in, "Proxy-Authorization");
1191 set_kerb_auth_headers(r, conf, use_krb4, use_krb5,
1192 (use_krb5) ? "\0" : NULL);
1193 return HTTP_UNAUTHORIZED;
1196 auth_type = ap_getword_white(r->pool, &auth_line);
1198 if (already_succeeded(r))
1201 ret = HTTP_UNAUTHORIZED;
1204 if (use_krb5 && conf->krb_method_gssapi &&
1205 strcasecmp(auth_type, "Negotiate") == 0) {
1206 ret = authenticate_user_gss(r, conf, auth_line, &negotiate_ret_value);
1207 } else if (use_krb5 && conf->krb_method_k5pass &&
1208 strcasecmp(auth_type, "Basic") == 0) {
1209 ret = authenticate_user_krb5pwd(r, conf, auth_line);
1214 if (ret == HTTP_UNAUTHORIZED && use_krb4 && conf->krb_method_k4pass &&
1215 strcasecmp(auth_type, "Basic") == 0)
1216 ret = authenticate_user_krb4pwd(r, conf, auth_line);
1219 if (ret == HTTP_UNAUTHORIZED)
1220 set_kerb_auth_headers(r, conf, use_krb4, use_krb5, negotiate_ret_value);
1227 /***************************************************************************
1228 Module Setup/Configuration
1229 ***************************************************************************/
1230 #ifndef STANDARD20_MODULE_STUFF
1231 module MODULE_VAR_EXPORT auth_kerb_module = {
1232 STANDARD_MODULE_STUFF,
1233 NULL, /* module initializer */
1234 kerb_dir_create_config, /* per-directory config creator */
1235 NULL, /* per-directory config merger */
1236 NULL, /* per-server config creator */
1237 NULL, /* per-server config merger */
1238 kerb_auth_cmds, /* command table */
1239 NULL, /* [ 9] content handlers */
1240 NULL, /* [ 2] URI-to-filename translation */
1241 kerb_authenticate_user, /* [ 5] check/validate user_id */
1242 NULL, /* [ 6] check user_id is valid *here* */
1243 NULL, /* [ 4] check access by host address */
1244 NULL, /* [ 7] MIME type checker/setter */
1245 NULL, /* [ 8] fixups */
1246 NULL, /* [10] logger */
1247 NULL, /* [ 3] header parser */
1248 NULL, /* process initialization */
1249 NULL, /* process exit/cleanup */
1250 NULL /* [ 1] post read_request handling */
1254 kerb_init_handler(apr_pool_t *p, apr_pool_t *plog,
1255 apr_pool_t *ptemp, server_rec *s)
1257 ap_add_version_component(p, "mod_auth_kerb/" MODAUTHKERB_VERSION);
1261 void kerb_register_hooks(apr_pool_t *p)
1263 ap_hook_post_config(kerb_init_handler, NULL, NULL, APR_HOOK_MIDDLE);
1264 ap_hook_check_user_id(kerb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
1267 module AP_MODULE_DECLARE_DATA auth_kerb_module =
1269 STANDARD20_MODULE_STUFF,
1270 kerb_dir_create_config, /* create per-dir conf structures */
1271 NULL, /* merge per-dir conf structures */
1272 NULL, /* create per-server conf structures */
1273 NULL, /* merge per-server conf structures */
1274 kerb_auth_cmds, /* table of configuration directives */
1275 kerb_register_hooks /* register hooks */