Updates/fixes to gssweb filter code.
[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 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     return APR_SUCCESS;
58 }
59
60 gss_conn_ctx
61 gss_get_conn_ctx(request_rec *r)
62 {
63     char key[1024];
64     gss_conn_ctx ctx = NULL;
65
66     snprintf(key, sizeof(key), "mod_auth_gssweb:conn_ctx");
67     apr_pool_userdata_get((void **)&ctx, key, r->connection->pool);
68     /* XXX LOG */
69     if (ctx == NULL) {
70         ctx = (gss_conn_ctx) apr_palloc(r->connection->pool, sizeof(*ctx));
71         if (ctx == NULL)
72             return NULL;
73         ctx->context = GSS_C_NO_CONTEXT;
74         ctx->state = GSS_CTX_EMPTY;
75         ctx->filter_stat = GSS_FILT_NEW;
76         ctx->user = NULL;
77         apr_pool_userdata_set(ctx, key, cleanup_conn_ctx, r->connection->pool);
78     }
79     return ctx;
80 }
81
82 void *
83 gss_config_dir_create(apr_pool_t *p, char *d)
84 {
85     gss_auth_config *conf;
86
87     conf = (gss_auth_config *) apr_pcalloc(p, sizeof(*conf));
88     return conf;
89 }
90
91
92 const char *
93 get_gss_error(request_rec *r, OM_uint32 err_maj, OM_uint32 err_min, char *prefix)
94 {
95    OM_uint32 maj_stat, min_stat; 
96    OM_uint32 msg_ctx = 0;
97    gss_buffer_desc status_string;
98    char *err_msg;
99    int first_pass;
100
101    gss_log(APLOG_MARK, APLOG_DEBUG, 0, r,
102            "GSS-API major_status:%8.8x, minor_status:%8.8x",
103            err_maj, err_min);
104
105    err_msg = apr_pstrdup(r->pool, prefix);
106    do {
107       maj_stat = gss_display_status (&min_stat,
108                                      err_maj,
109                                      GSS_C_GSS_CODE,
110                                      GSS_C_NO_OID,
111                                      &msg_ctx,
112                                      &status_string);
113       if (!GSS_ERROR(maj_stat)) {
114          err_msg = apr_pstrcat(r->pool, err_msg,
115                                ": ", (char*) status_string.value, NULL);
116          gss_release_buffer(&min_stat, &status_string);
117          first_pass = 0;
118       }
119    } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
120
121    msg_ctx = 0;
122    err_msg = apr_pstrcat(r->pool, err_msg, " (", NULL);
123    first_pass = 1;
124    do {
125       maj_stat = gss_display_status (&min_stat,
126                                      err_min,
127                                      GSS_C_MECH_CODE,
128                                      GSS_C_NULL_OID,
129                                      &msg_ctx,
130                                      &status_string);
131       if (!GSS_ERROR(maj_stat)) {
132          err_msg = apr_pstrcat(r->pool, err_msg,
133                                (first_pass) ? "" : ", ",
134                                (char *) status_string.value,
135                                NULL);
136          gss_release_buffer(&min_stat, &status_string);
137          first_pass = 0;
138       }
139    } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
140    err_msg = apr_pstrcat(r->pool, err_msg, ")", NULL);
141
142    return err_msg;
143 }
144
145 int
146 get_gss_creds(request_rec *r,
147               gss_auth_config *conf,
148               gss_cred_id_t *server_creds)
149 {
150    gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
151    OM_uint32 major_status, minor_status, minor_status2;
152    gss_name_t server_name = GSS_C_NO_NAME;
153    char buf[1024];
154    int have_server_princ;
155
156    if (conf->service_name && strcmp(conf->service_name, "Any") == 0) {
157        *server_creds = GSS_C_NO_CREDENTIAL;
158        return 0;
159    }
160
161    have_server_princ = conf->service_name && strchr(conf->service_name, '/') != NULL;
162    if (have_server_princ)
163        strncpy(buf, conf->service_name, sizeof(buf));
164    else
165        snprintf(buf, sizeof(buf), "%s@%s",
166                (conf->service_name) ? conf->service_name : SERVICE_NAME,
167                ap_get_server_name(r));
168
169    token.value = buf;
170    token.length = strlen(buf) + 1;
171
172    major_status = gss_import_name(&minor_status, &token,
173                                   (have_server_princ) ? (gss_OID) GSS_KRB5_NT_PRINCIPAL_NAME : (gss_OID) GSS_C_NT_HOSTBASED_SERVICE,
174                                   &server_name);
175    memset(&token, 0, sizeof(token));
176    if (GSS_ERROR(major_status)) {
177       gss_log(APLOG_MARK, APLOG_ERR, 0, r,
178               "%s", get_gss_error(r, major_status, minor_status,
179               "gss_import_name() failed"));
180       return HTTP_INTERNAL_SERVER_ERROR;
181    }
182
183    major_status = gss_display_name(&minor_status, server_name, &token, NULL);
184    if (GSS_ERROR(major_status)) {
185       /* Perhaps we could just ignore this error but it's safer to give up now,
186          I think */
187       gss_log(APLOG_MARK, APLOG_ERR, 0, r,
188               "%s", get_gss_error(r, major_status, minor_status,
189                                   "gss_display_name() failed"));
190       return HTTP_INTERNAL_SERVER_ERROR;
191    }
192
193    gss_log(APLOG_MARK, APLOG_DEBUG, 0, r, "Acquiring creds for %s", token.value);
194    gss_release_buffer(&minor_status, &token);
195    
196    major_status = gss_acquire_cred(&minor_status, server_name, GSS_C_INDEFINITE,
197                                    GSS_C_NO_OID_SET, GSS_C_ACCEPT,
198                                    server_creds, NULL, NULL);
199    gss_release_name(&minor_status2, &server_name);
200    if (GSS_ERROR(major_status)) {
201       gss_log(APLOG_MARK, APLOG_ERR, 0, r,
202               "%s", get_gss_error(r, major_status, minor_status,
203                                   "Failed to load GSS-API credentials"));
204       return HTTP_INTERNAL_SERVER_ERROR;
205    }
206
207    return 0;
208 }
209
210 int
211 cmp_gss_type(gss_buffer_t token, gss_OID oid)
212 {
213    unsigned char *p;
214    size_t len;
215
216    if (token->length == 0)
217       return GSS_S_DEFECTIVE_TOKEN;
218
219    p = token->value;
220    if (*p++ != 0x60)
221       return GSS_S_DEFECTIVE_TOKEN;
222    len = *p++;
223    if (len & 0x80) {
224       if ((len & 0x7f) > 4)
225          return GSS_S_DEFECTIVE_TOKEN;
226       p += len & 0x7f;
227    }
228    if (*p++ != 0x06)
229       return GSS_S_DEFECTIVE_TOKEN;
230
231    if (((OM_uint32) *p++) != oid->length)
232       return GSS_S_DEFECTIVE_TOKEN;
233
234    return memcmp(p, oid->elements, oid->length);
235 }
236