2 * Copyright (c) 2010 CESNET
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of CESNET nor the names of its contributors may
16 * be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include "mod_auth_gssapi.h"
34 module AP_MODULE_DECLARE_DATA auth_gssapi_module;
36 #define command(name, func, var, type, usage) \
37 AP_INIT_ ## type (name, (void*) func, \
38 (void*)APR_OFFSETOF(gss_auth_config, var), \
39 OR_AUTHCFG | RSRC_CONF, usage)
41 static const command_rec gss_config_cmds[] = {
42 command("GSSServiceName", ap_set_string_slot, service_name,
43 TAKE1, "Service name used for Apache authentication."),
45 command("GSSKrb5Keytab", ap_set_string_slot, krb5_keytab,
46 TAKE1, "Location of Kerberos V5 keytab file."),
52 set_http_headers(request_rec *r, const gss_auth_config *conf,
53 char *negotiate_ret_value)
56 const char *header_name = (r->proxyreq == PROXYREQ_PROXY) ?
57 "Proxy-Authenticate" : "WWW-Authenticate";
59 if (negotiate_ret_value == NULL)
62 negoauth_param = (*negotiate_ret_value == '\0') ? "Negotiate" :
63 apr_pstrcat(r->pool, "Negotiate ", negotiate_ret_value, NULL);
64 apr_table_add(r->err_headers_out, header_name, negoauth_param);
68 gss_authenticate_user(request_rec *r)
70 gss_auth_config *conf =
71 (gss_auth_config *) ap_get_module_config(r->per_dir_config,
73 const char *auth_line = NULL;
74 const char *type = NULL;
75 char *auth_type = NULL;
76 char *negotiate_ret_value = NULL;
77 gss_conn_ctx conn_ctx = NULL;
80 gss_log(APLOG_MARK, APLOG_DEBUG, 0, r, "Entering GSSAPI authentication");
82 /* get the type specified in Apache configuration */
83 type = ap_auth_type(r);
84 if (type == NULL || strcmp(type, "Negotiate") != 0) {
85 gss_log(APLOG_MARK, APLOG_DEBUG, 0, r,
86 "AuthType '%s' is not for us, bailing out",
87 (type) ? type : "(NULL)");
92 /* get what the user sent us in the HTTP header */
93 auth_line = apr_table_get(r->headers_in, (r->proxyreq == PROXYREQ_PROXY)
94 ? "Proxy-Authorization"
96 if (auth_line == NULL) {
97 gss_log(APLOG_MARK, APLOG_DEBUG, 0, r,
98 "Client hasn't sent any authentication data, giving up");
99 set_http_headers(r, conf, "\0");
100 return HTTP_UNAUTHORIZED;
103 auth_type = ap_getword_white(r->pool, &auth_line);
104 if (strcasecmp(auth_type, "Negotiate") != 0) {
105 gss_log(APLOG_MARK, APLOG_DEBUG, 0, r,
106 "Unsupported authentication type (%s) requested by client",
107 (auth_type) ? auth_type : "(NULL)");
108 set_http_headers(r, conf, "\0");
109 return HTTP_UNAUTHORIZED;
112 conn_ctx = gss_get_conn_ctx(r);
113 if (conn_ctx == NULL) {
114 gss_log(APLOG_MARK, APLOG_ERR, 0, r,
115 "Failed to create internal context: probably not enough memory");
116 return HTTP_INTERNAL_SERVER_ERROR;
119 /* optimizing hack */
120 if (conn_ctx->state == GSS_CTX_ESTABLISHED && auth_line == NULL) {
121 r->user = apr_pstrdup(r->pool, conn_ctx->user);
122 r->ap_auth_type = "Negotiate";
126 /* XXXX subrequests ignored, only successful accesses taken into account! */
127 if (!ap_is_initial_req(r) && conn_ctx->state == GSS_CTX_ESTABLISHED) {
128 r->user = apr_pstrdup(r->pool, conn_ctx->user);
129 r->ap_auth_type = "Negotiate";
133 ret = gss_authenticate(r, conf, conn_ctx,
134 auth_line, &negotiate_ret_value);
135 if (ret == HTTP_UNAUTHORIZED || ret == OK) {
137 set_http_headers(r, conf, negotiate_ret_value);
141 r->user = apr_pstrdup(r->pool, conn_ctx->user);
142 r->ap_auth_type = "Negotiate";
151 gss_register_hooks(apr_pool_t *p)
153 ap_hook_check_user_id(gss_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
156 module AP_MODULE_DECLARE_DATA auth_gssapi_module = {
157 STANDARD20_MODULE_STUFF,
158 gss_config_dir_create,