Code for gssweb module check_user hook.
[mod_auth_kerb.git] / mod_auth_gssweb.c
1 /*
2  * Copyright (c) 2010 CESNET
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  *
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.
14  *
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.
18  *
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.
30  */
31
32 #include "mod_auth_gssweb.h"
33
34 module AP_MODULE_DECLARE_DATA auth_gssweb_module;
35
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)
40
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."),
44
45     { NULL }
46 };
47
48 static int
49 gssweb_authenticate_user(request_rec *r)
50 {
51     gss_auth_config *conf = 
52         (gss_auth_config *) ap_get_module_config(r->per_dir_config,
53                                                 &auth_gssweb_module);
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;
59     int ret;
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;
64     int ret;
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;
69     unsigned int nonce;
70
71     gss_log(APLOG_MARK, APLOG_DEBUG, 0, r, "Entering GSSWeb authentication");
72    
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)");
79
80         return DECLINED;
81     }
82
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;
89     }
90
91     /* Set-up the output filter, if we haven't already */
92     if (GSS_CTX_EMPTY == conn_ctx->state) {
93
94       // TBD -- Set-up the output filter 
95     }
96
97     /* Read the token and nonce from the POST */
98     //TBD -- gss_log the values
99
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);
104         }
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
110         }
111         conn_ctx->output_token = NULL;
112       }
113       conn_ctx->nonce = nonce;
114       
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.");
120       goto end;
121     }
122
123     /* Acquire server credentials */
124     ret = get_gss_creds(r, conf, &server_creds);
125     if (ret)
126       goto end;
127
128     /* Decode input token */
129     input_token.length = apr_base64_decode(input_token.value, posted_token);
130
131     /* Call gss_accept_sec_context */
132     major_status = gss_accept_sec_context(&minor_status,
133                                           &ctx->context,
134                                           server_creds,
135                                           &input_token,
136                                           GSS_C_NO_CHANNEL_BINDINGS,
137                                           NULL,
138                                           NULL,
139                                           &output_token,
140                                           &ret_flags,
141                                           NULL,
142                                           &delegated_cred);
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");
146
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;
155       goto end;
156     }
157
158     /* Store the token & nonce in the stored context */
159     conn_ctx.output_token = &output_token;
160     conn_ctx.nonce = nonce;
161
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;
166       goto end;
167     }
168
169     ctx->state = GSS_CTX_ESTABLISHED;
170     ret = OK;
171
172  end:
173   if (delegated_cred)
174      gss_release_cred(&minor_status, &delegated_cred);
175
176   if (output_token.length) 
177      gss_release_buffer(&minor_status, &output_token);
178
179   if (client_name != GSS_C_NO_NAME)
180      gss_release_name(&minor_status, &client_name);
181
182   if (server_creds != GSS_C_NO_CREDENTIAL)
183      gss_release_cred(&minor_status, &server_creds);
184
185   return ret;
186
187 }
188
189 static void
190 gssweb_register_hooks(apr_pool_t *p)
191 {
192     ap_hook_check_user_id(gssweb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
193 }
194
195 module AP_MODULE_DECLARE_DATA auth_gssweb_module = {
196     STANDARD20_MODULE_STUFF,
197     gss_config_dir_create,
198     NULL,
199     NULL,
200     NULL,
201     gssweb_config_cmds,
202     gssweb_register_hooks
203 };