Break out of processing upon GSS error being reported.
[mod_auth_kerb.git] / gss.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 void
35 gss_log(const char *file, 
36         int line,
37 #if AP_SERVER_MAJORVERSION_NUMBER == 2 && AP_SERVER_MINORVERSION_NUMBER == 4
38         int module_index,
39 #endif
40         int level,
41         int status,
42         const request_rec *r,
43         const char *fmt, ...)
44 {
45     char errstr[1024];
46     va_list ap;
47
48     va_start(ap, fmt);
49     vsnprintf(errstr, sizeof(errstr), fmt, ap);
50     va_end(ap);
51    
52     ap_log_rerror(file,
53                   line, 
54 #if AP_SERVER_MAJORVERSION_NUMBER == 2 && AP_SERVER_MINORVERSION_NUMBER == 4
55                   module_index,
56 #endif
57                   level | APLOG_NOERRNO, 
58                   status, 
59                   r, 
60                   "%s", 
61                   errstr);
62 }
63
64 apr_status_t
65 gss_cleanup_conn_ctx(void *data)
66 {
67     gss_conn_ctx ctx = (gss_conn_ctx) data;
68     OM_uint32 minor_status;
69
70     if (ctx && ctx->context != GSS_C_NO_CONTEXT)
71         gss_delete_sec_context(&minor_status, &ctx->context, GSS_C_NO_BUFFER);
72   
73     if (ctx && ctx->server_creds != GSS_C_NO_CREDENTIAL)
74       gss_release_cred(&minor_status, &ctx->server_creds);
75
76     return APR_SUCCESS;
77 }
78
79 gss_conn_ctx
80 gss_create_conn_ctx(request_rec *r, gss_auth_config *conf)
81 {
82   char key[1024];
83   gss_conn_ctx ctx = NULL;
84  
85   snprintf(key, sizeof(key), "mod_auth_gssweb:conn_ctx");
86   
87   if (NULL == (ctx = (gss_conn_ctx) apr_palloc(r->connection->pool, sizeof(*ctx)))) {
88     gss_log(APLOG_MARK, APLOG_ERR, 0, r, "gss_create_conn_ctx: Can't allocate GSS context");
89     return NULL;
90   }
91   ctx->context = GSS_C_NO_CONTEXT;
92   ctx->state = GSS_CTX_EMPTY;
93   ctx->filter_stat = GSS_FILT_NEW;
94   ctx->user = NULL;
95
96   /* Acquire and store server credentials */
97   if (0 == get_gss_creds(r, conf, &(ctx->server_creds))) {
98     gss_log(APLOG_MARK, APLOG_DEBUG, 0, r, "gss_create_conn_ctx: Server credentials acquired");
99   } else {
100     gss_log(APLOG_MARK, APLOG_ERR, 0, r, "gss_create_conn_ctx: Error: Server credentials NOT acquired");
101     return NULL;
102   }
103
104   apr_pool_userdata_set(ctx, key, gss_cleanup_conn_ctx, r->connection->pool);
105
106   return ctx;
107 }
108
109 gss_conn_ctx
110 gss_retrieve_conn_ctx(request_rec *r)
111 {
112   char key[1024];
113   gss_conn_ctx ctx = NULL;
114  
115   snprintf(key, sizeof(key), "mod_auth_gssweb:conn_ctx");
116   apr_pool_userdata_get((void **)&ctx, key, r->connection->pool);
117
118   if (NULL == ctx)
119     gss_log(APLOG_MARK, APLOG_DEBUG, 0, r, "gss_retrieve_conn_ctx: No GSS context found");
120
121   return ctx;
122 }
123
124 void *
125 gss_config_dir_create(apr_pool_t *p, char *d)
126 {
127     gss_auth_config *conf;
128
129     conf = (gss_auth_config *) apr_pcalloc(p, sizeof(*conf));
130     return conf;
131 }
132
133
134 const char *
135 get_gss_error(request_rec *r, OM_uint32 err_maj, OM_uint32 err_min, char *prefix)
136 {
137    OM_uint32 maj_stat, min_stat; 
138    OM_uint32 msg_ctx = 0;
139    gss_buffer_desc status_string;
140    char *err_msg;
141    int first_pass;
142
143    gss_log(APLOG_MARK, APLOG_DEBUG, 0, r,
144            "GSS-API major_status:%8.8x, minor_status:%8.8x",
145            err_maj, err_min);
146
147    err_msg = apr_pstrdup(r->pool, prefix);
148    do {
149       maj_stat = gss_display_status (&min_stat,
150                                      err_maj,
151                                      GSS_C_GSS_CODE,
152                                      GSS_C_NO_OID,
153                                      &msg_ctx,
154                                      &status_string);
155       if (!GSS_ERROR(maj_stat)) {
156          err_msg = apr_pstrcat(r->pool, err_msg,
157                                ": ", (char*) status_string.value, NULL);
158          gss_release_buffer(&min_stat, &status_string);
159          first_pass = 0;
160       }
161    } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
162
163    msg_ctx = 0;
164    err_msg = apr_pstrcat(r->pool, err_msg, " (", NULL);
165    first_pass = 1;
166    do {
167       maj_stat = gss_display_status (&min_stat,
168                                      err_min,
169                                      GSS_C_MECH_CODE,
170                                      GSS_C_NULL_OID,
171                                      &msg_ctx,
172                                      &status_string);
173       if (!GSS_ERROR(maj_stat)) {
174          err_msg = apr_pstrcat(r->pool, err_msg,
175                                (first_pass) ? "" : ", ",
176                                (char *) status_string.value,
177                                NULL);
178          gss_release_buffer(&min_stat, &status_string);
179          first_pass = 0;
180       }
181    } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
182    err_msg = apr_pstrcat(r->pool, err_msg, ")", NULL);
183
184    return err_msg;
185 }
186
187 int
188 get_gss_creds(request_rec *r,
189               gss_auth_config *conf,
190               gss_cred_id_t *server_creds)
191 {
192    gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
193    OM_uint32 major_status, minor_status, minor_status2;
194    gss_name_t server_name = GSS_C_NO_NAME;
195    char buf[1024];
196    int have_server_princ;
197
198    if (conf->service_name && strcmp(conf->service_name, "Any") == 0) {
199        *server_creds = GSS_C_NO_CREDENTIAL;
200        return 0;
201    }
202
203    have_server_princ = conf->service_name && strchr(conf->service_name, '/') != NULL;
204    if (have_server_princ)
205        strncpy(buf, conf->service_name, sizeof(buf));
206    else
207        snprintf(buf, sizeof(buf), "%s@%s",
208                (conf->service_name) ? conf->service_name : SERVICE_NAME,
209                ap_get_server_name(r));
210
211    token.value = buf;
212    token.length = strlen(buf) + 1;
213
214    major_status = gss_import_name(&minor_status, &token,
215                                   (have_server_princ) ? (gss_OID) GSS_KRB5_NT_PRINCIPAL_NAME : (gss_OID) GSS_C_NT_HOSTBASED_SERVICE,
216                                   &server_name);
217    memset(&token, 0, sizeof(token));
218    if (GSS_ERROR(major_status)) {
219       gss_log(APLOG_MARK, APLOG_ERR, 0, r,
220               "%s", get_gss_error(r, major_status, minor_status,
221               "gss_import_name() failed"));
222       return HTTP_INTERNAL_SERVER_ERROR;
223    }
224
225    major_status = gss_display_name(&minor_status, server_name, &token, NULL);
226    if (GSS_ERROR(major_status)) {
227       /* Perhaps we could just ignore this error but it's safer to give up now,
228          I think */
229       gss_log(APLOG_MARK, APLOG_ERR, 0, r,
230               "%s", get_gss_error(r, major_status, minor_status,
231                                   "gss_display_name() failed"));
232       return HTTP_INTERNAL_SERVER_ERROR;
233    }
234
235    gss_log(APLOG_MARK, APLOG_DEBUG, 0, r, "Acquiring creds for %s", token.value);
236    gss_release_buffer(&minor_status, &token);
237    
238    major_status = gss_acquire_cred(&minor_status, server_name, GSS_C_INDEFINITE,
239                                    GSS_C_NO_OID_SET, GSS_C_ACCEPT,
240                                    server_creds, NULL, NULL);
241    gss_release_name(&minor_status2, &server_name);
242    if (GSS_ERROR(major_status)) {
243       gss_log(APLOG_MARK, APLOG_ERR, 0, r,
244               "%s", get_gss_error(r, major_status, minor_status,
245                                   "Failed to load GSS-API credentials"));
246       return HTTP_INTERNAL_SERVER_ERROR;
247    }
248
249    return 0;
250 }
251
252 int
253 cmp_gss_type(gss_buffer_t token, gss_OID oid)
254 {
255    unsigned char *p;
256    size_t len;
257
258    if (token->length == 0)
259       return GSS_S_DEFECTIVE_TOKEN;
260
261    p = token->value;
262    if (*p++ != 0x60)
263       return GSS_S_DEFECTIVE_TOKEN;
264    len = *p++;
265    if (len & 0x80) {
266       if ((len & 0x7f) > 4)
267          return GSS_S_DEFECTIVE_TOKEN;
268       p += len & 0x7f;
269    }
270    if (*p++ != 0x06)
271       return GSS_S_DEFECTIVE_TOKEN;
272
273    if (((OM_uint32) *p++) != oid->length)
274       return GSS_S_DEFECTIVE_TOKEN;
275
276    return memcmp(p, oid->elements, oid->length);
277 }
278