Conf file is now in source tree
[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 gss_config_dir_create(apr_pool_t *p, char *d)
53 {
54     gss_auth_config *conf;
55
56     conf = (gss_auth_config *) apr_pcalloc(p, sizeof(*conf));
57     return conf;
58 }
59
60 void
61 gss_log(const char *file, int line, int level, int status,
62         const request_rec *r, const char *fmt, ...)
63 {
64     char errstr[1024];
65     va_list ap;
66
67     va_start(ap, fmt);
68     vsnprintf(errstr, sizeof(errstr), fmt, ap);
69     va_end(ap);
70    
71     ap_log_rerror(file, line, level | APLOG_NOERRNO, status, r, "%s", errstr);
72 }
73
74 static void
75 set_http_headers(request_rec *r, const gss_auth_config *conf,
76                  char *negotiate_ret_value)
77 {
78     char *negoauth_param;
79     const char *header_name = (r->proxyreq == PROXYREQ_PROXY) ?
80         "Proxy-Authenticate" : "WWW-Authenticate";
81
82     if (negotiate_ret_value == NULL)
83         return;
84
85     negoauth_param = (*negotiate_ret_value == '\0') ? "Negotiate" :
86         apr_pstrcat(r->pool, "Negotiate ", negotiate_ret_value, NULL);
87     apr_table_add(r->err_headers_out, header_name, negoauth_param);
88 }
89
90 static apr_status_t
91 cleanup_conn_ctx(void *data)
92 {
93     gss_conn_ctx ctx = (gss_conn_ctx) data;
94     OM_uint32 minor_status;
95
96     if (ctx && ctx->context != GSS_C_NO_CONTEXT)
97         gss_delete_sec_context(&minor_status, &ctx->context, GSS_C_NO_BUFFER);
98
99     return APR_SUCCESS;
100 }
101
102 static gss_conn_ctx
103 gss_get_conn_ctx(request_rec *r)
104 {
105     char key[1024];
106     gss_conn_ctx ctx = NULL;
107
108     snprintf(key, sizeof(key), "mod_auth_gssapi:conn_ctx");
109     apr_pool_userdata_get((void **)&ctx, key, r->connection->pool);
110     /* XXX LOG */
111     if (ctx == NULL) {
112         ctx = (gss_conn_ctx) apr_palloc(r->connection->pool, sizeof(*ctx));
113         if (ctx == NULL)
114             return NULL;
115         ctx->context = GSS_C_NO_CONTEXT;
116         ctx->state = GSS_CTX_EMPTY;
117         ctx->user = NULL;
118         apr_pool_userdata_set(ctx, key, cleanup_conn_ctx, r->connection->pool);
119     }
120     return ctx;
121 }
122
123 static int
124 gss_authenticate_user(request_rec *r)
125 {
126     gss_auth_config *conf = 
127         (gss_auth_config *) ap_get_module_config(r->per_dir_config,
128                                                 &auth_gssapi_module);
129     const char *auth_line = NULL;
130     const char *type = NULL;
131     char *auth_type = NULL;
132     char *negotiate_ret_value = NULL;
133     gss_conn_ctx conn_ctx = NULL;
134     int ret;
135
136     gss_log(APLOG_MARK, APLOG_DEBUG, 0, r, "Entering GSSAPI authentication");
137    
138     /* get the type specified in Apache configuration */
139     type = ap_auth_type(r);
140     if (type == NULL || strcmp(type, "Negotiate") != 0) {
141         gss_log(APLOG_MARK, APLOG_DEBUG, 0, r,
142                 "AuthType '%s' is not for us, bailing out",
143                 (type) ? type : "(NULL)");
144
145         return DECLINED;
146     }
147
148     /* get what the user sent us in the HTTP header */
149     auth_line = apr_table_get(r->headers_in, (r->proxyreq == PROXYREQ_PROXY)
150                                             ? "Proxy-Authorization"
151                                             : "Authorization");
152     if (auth_line == NULL) {
153         gss_log(APLOG_MARK, APLOG_DEBUG, 0, r,
154                 "Client hasn't sent any authentication data, giving up");
155         set_http_headers(r, conf, "\0");
156         return HTTP_UNAUTHORIZED;
157     }
158
159     auth_type = ap_getword_white(r->pool, &auth_line);
160     if (strcasecmp(auth_type, "Negotiate") != 0) {
161         gss_log(APLOG_MARK, APLOG_DEBUG, 0, r,
162                 "Unsupported authentication type (%s) requested by client",
163                 (auth_type) ? auth_type : "(NULL)");
164         set_http_headers(r, conf, "\0");
165         return HTTP_UNAUTHORIZED;
166     }
167
168     conn_ctx = gss_get_conn_ctx(r);
169     if (conn_ctx == NULL) {
170         gss_log(APLOG_MARK, APLOG_ERR, 0, r,
171                 "Failed to create internal context: probably not enough memory");
172         return HTTP_INTERNAL_SERVER_ERROR;
173     }
174
175     /* optimizing hack */
176     if (conn_ctx->state == GSS_CTX_ESTABLISHED && auth_line == NULL) {
177         r->user = apr_pstrdup(r->pool, conn_ctx->user);
178         r->ap_auth_type = "Negotiate";
179         return OK;
180     }
181
182     /* XXXX subrequests ignored, only successful accesses taken into account! */
183     if (!ap_is_initial_req(r) && conn_ctx->state == GSS_CTX_ESTABLISHED) {
184         r->user = apr_pstrdup(r->pool, conn_ctx->user);
185         r->ap_auth_type = "Negotiate";
186         return OK;
187     }
188
189     ret = gss_authenticate(r, conf, conn_ctx,
190                            auth_line, &negotiate_ret_value);
191     if (ret == HTTP_UNAUTHORIZED || ret == OK) {
192         /* LOG?? */
193         set_http_headers(r, conf, negotiate_ret_value);
194     }
195
196     if (ret == OK) {
197         r->user = apr_pstrdup(r->pool, conn_ctx->user);
198         r->ap_auth_type = "Negotiate";
199     }
200
201     /* debug LOG ??? */
202
203     return ret;
204 }
205
206 static void
207 gss_register_hooks(apr_pool_t *p)
208 {
209     ap_hook_check_user_id(gss_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
210 }
211
212 module AP_MODULE_DECLARE_DATA auth_gssapi_module = {
213     STANDARD20_MODULE_STUFF,
214     gss_config_dir_create,
215     NULL,
216     NULL,
217     NULL,
218     gss_config_cmds,
219     gss_register_hooks
220 };