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