Update include files to match code refactoring for two modules.
[mod_auth_kerb.cvs/.git] / mod_auth_gssapi.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_gssapi.h"
33
34 module AP_MODULE_DECLARE_DATA auth_gssapi_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 gss_config_cmds[] = {
42     command("GSSServiceName", ap_set_string_slot, service_name,
43             TAKE1, "Service name used for Apache authentication."),
44
45     command("GSSKrb5Keytab", ap_set_string_slot, krb5_keytab,
46             TAKE1, "Location of Kerberos V5 keytab file."),
47
48     { NULL }
49 };
50
51 static void
52 set_http_headers(request_rec *r, const gss_auth_config *conf,
53                  char *negotiate_ret_value)
54 {
55     char *negoauth_param;
56     const char *header_name = (r->proxyreq == PROXYREQ_PROXY) ?
57         "Proxy-Authenticate" : "WWW-Authenticate";
58
59     if (negotiate_ret_value == NULL)
60         return;
61
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);
65 }
66
67 static int
68 gss_authenticate_user(request_rec *r)
69 {
70     gss_auth_config *conf = 
71         (gss_auth_config *) ap_get_module_config(r->per_dir_config,
72                                                 &auth_gssapi_module);
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;
78     int ret;
79
80     gss_log(APLOG_MARK, APLOG_DEBUG, 0, r, "Entering GSSAPI authentication");
81    
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)");
88
89         return DECLINED;
90     }
91
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"
95                                             : "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;
101     }
102
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;
110     }
111
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;
117     }
118
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";
123         return OK;
124     }
125
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";
130         return OK;
131     }
132
133     ret = gss_authenticate(r, conf, conn_ctx,
134                            auth_line, &negotiate_ret_value);
135     if (ret == HTTP_UNAUTHORIZED || ret == OK) {
136         /* LOG?? */
137         set_http_headers(r, conf, negotiate_ret_value);
138     }
139
140     if (ret == OK) {
141         r->user = apr_pstrdup(r->pool, conn_ctx->user);
142         r->ap_auth_type = "Negotiate";
143     }
144
145     /* debug LOG ??? */
146
147     return ret;
148 }
149
150 static void
151 gss_register_hooks(apr_pool_t *p)
152 {
153     ap_hook_check_user_id(gss_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
154 }
155
156 module AP_MODULE_DECLARE_DATA auth_gssapi_module = {
157     STANDARD20_MODULE_STUFF,
158     gss_config_dir_create,
159     NULL,
160     NULL,
161     NULL,
162     gss_config_cmds,
163     gss_register_hooks
164 };