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_gssweb.h"
34 module AP_MODULE_DECLARE_DATA auth_gssweb_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 gssweb_config_cmds[] = {
42 command("GSSServiceName", ap_set_string_slot, service_name,
43 TAKE1, "Service name used for Apache authentication."),
49 gssweb_authenticate_user(request_rec *r)
51 gss_auth_config *conf =
52 (gss_auth_config *) ap_get_module_config(r->per_dir_config,
54 const char *auth_line = NULL;
55 const char *type = NULL;
56 char *auth_type = NULL;
57 char *negotiate_ret_value = NULL;
58 gss_conn_ctx conn_ctx = NULL;
60 OM_uint32 major_status, minor_status, minor_status2;
61 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
62 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
63 const char *posted_token = NULL;
65 gss_name_t client_name = GSS_C_NO_NAME;
66 gss_cred_id_t delegated_cred = GSS_C_NO_CREDENTIAL;
67 gss_cred_id_t server_creds = GSS_C_NO_CREDENTIAL;
68 OM_uint32 ret_flags = 0;
71 gss_log(APLOG_MARK, APLOG_DEBUG, 0, r, "Entering GSSWeb authentication");
73 /* Check if this is for our auth type */
74 type = ap_auth_type(r);
75 if (type == NULL || strcasecmp(type, "GSSWeb") != 0) {
76 gss_log(APLOG_MARK, APLOG_DEBUG, 0, r,
77 "AuthType '%s' is not GSSWeb, bailing out",
78 (type) ? type : "(NULL)");
83 /* Set up a GSS context for this request, if there isn't one already */
84 conn_ctx = gss_get_conn_ctx(r);
85 if (conn_ctx == NULL) {
86 gss_log(APLOG_MARK, APLOG_ERR, 0, r,
87 "Failed to create internal context: probably not enough memory");
88 return HTTP_INTERNAL_SERVER_ERROR;
91 /* Set-up the output filter, if we haven't already */
92 if (GSS_CTX_EMPTY == conn_ctx->state) {
94 // TBD -- Set-up the output filter
97 /* Read the token and nonce from the POST */
98 //TBD -- gss_log the values
100 /* If the nonce is set and doesn't match, start over */
101 if ((0 != conn_ctx_nonce) && (conn_ctx->nonce != nonce) {
102 if (GSS_C_NO_CONTEXT != conn_ctx->context) {
103 gss_delete_sec_context(&minor_status, &conn_ctx->context, GSS_C_NO_BUFFER);
105 conn_ctx->context = GSS_C_NO_CONTEXT;
106 conn_ctx->state = GSS_CTX_EMPTY;
107 conn_ctx->user = NULL;
108 if (NULL != conn_ctx->output_token) {
109 // TBD -- release the output token
111 conn_ctx->output_token = NULL;
113 conn_ctx->nonce = nonce;
115 /* If the output filter reported an internal server error, return it */
116 if (GSS_CTX_ERROR == conn_ctx->state) {
117 ret = HTTP_INTERNAL_SERVER_ERROR;
118 gss_log(APLOG_MARK, APLOG_ERR, 0, r,
119 "Output filter returned internal server error, reporting.");
123 /* Acquire server credentials */
124 ret = get_gss_creds(r, conf, &server_creds);
128 /* Decode input token */
129 input_token.length = apr_base64_decode(input_token.value, posted_token);
131 /* Call gss_accept_sec_context */
132 major_status = gss_accept_sec_context(&minor_status,
136 GSS_C_NO_CHANNEL_BINDINGS,
143 gss_log(APLOG_MARK, APLOG_DEBUG, 0, r,
144 "Client %s us their credential",
145 (ret_flags & GSS_C_DELEG_FLAG) ? "delegated" : "didn't delegate");
147 if (GSS_ERROR(major_status)) {
148 gss_log(APLOG_MARK, APLOG_ERR, 0, r,
149 "%s", get_gss_error(r, major_status, minor_status,
150 "Failed to establish authentication"));
151 gss_delete_sec_context(&minor_status, &ctx->context, GSS_C_NO_BUFFER);
152 ctx->context = GSS_C_NO_CONTEXT;
153 ctx->state = GSS_CTX_EMPTY;
154 ret = HTTP_UNAUTHORIZED;
158 /* Store the token & nonce in the stored context */
159 conn_ctx.output_token = &output_token;
160 conn_ctx.nonce = nonce;
162 /* If we aren't done yet, go around again */
163 if (major_status & GSS_S_CONTINUE_NEEDED) {
164 ctx->state = GSS_CTX_IN_PROGRESS;
165 ret = HTTP_UNAUTHORIZED;
169 ctx->state = GSS_CTX_ESTABLISHED;
174 gss_release_cred(&minor_status, &delegated_cred);
176 if (output_token.length)
177 gss_release_buffer(&minor_status, &output_token);
179 if (client_name != GSS_C_NO_NAME)
180 gss_release_name(&minor_status, &client_name);
182 if (server_creds != GSS_C_NO_CREDENTIAL)
183 gss_release_cred(&minor_status, &server_creds);
190 gssweb_register_hooks(apr_pool_t *p)
192 ap_hook_check_user_id(gssweb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
195 module AP_MODULE_DECLARE_DATA auth_gssweb_module = {
196 STANDARD20_MODULE_STUFF,
197 gss_config_dir_create,
202 gssweb_register_hooks