Added protocol description for GSS Web authentication.
[mod_auth_kerb.cvs/.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
61     gss_log(APLOG_MARK, APLOG_DEBUG, 0, r, "Entering GSSWeb authentication");
62    
63     /* get the type specified in Apache configuration */
64     type = ap_auth_type(r);
65     if (type == NULL || strcasecmp(type, "GSSWeb") != 0) {
66         gss_log(APLOG_MARK, APLOG_DEBUG, 0, r,
67                 "AuthType '%s' is not for us, bailing out",
68                 (type) ? type : "(NULL)");
69
70         return DECLINED;
71     }
72
73     /* get what the user sent us in the HTTP header */
74     auth_line = apr_table_get(r->headers_in, (r->proxyreq == PROXYREQ_PROXY)
75                                             ? "Proxy-Authorization"
76                                             : "Authorization");
77     if (auth_line == NULL) {
78         gss_log(APLOG_MARK, APLOG_DEBUG, 0, r,
79                 "Client hasn't sent an authorization header, giving up");
80         set_http_headers(r, conf, "\0");
81         return HTTP_UNAUTHORIZED;
82     }
83
84     auth_type = ap_getword_white(r->pool, &auth_line);
85     if (strcasecmp(auth_type, "gssweb") != 0) {
86         gss_log(APLOG_MARK, APLOG_DEBUG, 0, r,
87                 "Unsupported authentication type (%s) requested by client",
88                 (auth_type) ? auth_type : "(NULL)");
89         set_http_headers(r, conf, "\0");
90         return HTTP_UNAUTHORIZED;
91     }
92
93     conn_ctx = gss_get_conn_ctx(r);
94     if (conn_ctx == NULL) {
95         gss_log(APLOG_MARK, APLOG_ERR, 0, r,
96                 "Failed to create internal context: probably not enough memory");
97         return HTTP_INTERNAL_SERVER_ERROR;
98     }
99
100     /* optimizing hack */
101     if (conn_ctx->state == GSS_CTX_ESTABLISHED && auth_line == NULL) {
102         r->user = apr_pstrdup(r->pool, conn_ctx->user);
103         r->ap_auth_type = "Negotiate";
104         return OK;
105     }
106
107     /* XXXX subrequests ignored, only successful accesses taken into account! */
108     if (!ap_is_initial_req(r) && conn_ctx->state == GSS_CTX_ESTABLISHED) {
109         r->user = apr_pstrdup(r->pool, conn_ctx->user);
110         r->ap_auth_type = "Negotiate";
111         return OK;
112     }
113
114     ret = gss_authenticate(r, conf, conn_ctx,
115                            auth_line, &negotiate_ret_value);
116     if (ret == HTTP_UNAUTHORIZED || ret == OK) {
117         /* LOG?? */
118         set_http_headers(r, conf, negotiate_ret_value);
119     }
120
121     if (ret == OK) {
122         r->user = apr_pstrdup(r->pool, conn_ctx->user);
123         r->ap_auth_type = "Negotiate";
124     }
125
126     /* debug LOG ??? */
127
128     return ret;
129 }
130
131 static void
132 gssweb_register_hooks(apr_pool_t *p)
133 {
134     ap_hook_check_user_id(gssweb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
135 }
136
137 module AP_MODULE_DECLARE_DATA auth_gssweb_module = {
138     STANDARD20_MODULE_STUFF,
139     gss_config_dir_create,
140     NULL,
141     NULL,
142     NULL,
143     gssweb_config_cmds,
144     gssweb_register_hooks
145 };