1 #include "mod_auth_gssapi.h"
4 get_gss_error(request_rec *r, OM_uint32 err_maj, OM_uint32 err_min, char *prefix)
6 OM_uint32 maj_stat, min_stat;
8 gss_buffer_desc status_string;
12 gss_log(APLOG_MARK, APLOG_DEBUG, 0, r,
13 "GSS-API major_status:%8.8x, minor_status:%8.8x",
16 err_msg = apr_pstrdup(r->pool, prefix);
18 maj_stat = gss_display_status (&min_stat,
24 if (!GSS_ERROR(maj_stat)) {
25 err_msg = apr_pstrcat(r->pool, err_msg,
26 ": ", (char*) status_string.value, NULL);
27 gss_release_buffer(&min_stat, &status_string);
30 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
33 err_msg = apr_pstrcat(r->pool, err_msg, " (", NULL);
36 maj_stat = gss_display_status (&min_stat,
42 if (!GSS_ERROR(maj_stat)) {
43 err_msg = apr_pstrcat(r->pool, err_msg,
44 (first_pass) ? "" : ", ",
45 (char *) status_string.value,
47 gss_release_buffer(&min_stat, &status_string);
50 } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
51 err_msg = apr_pstrcat(r->pool, err_msg, ")", NULL);
57 get_gss_creds(request_rec *r,
58 gss_auth_config *conf,
59 gss_cred_id_t *server_creds)
61 gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
62 OM_uint32 major_status, minor_status, minor_status2;
63 gss_name_t server_name = GSS_C_NO_NAME;
65 int have_server_princ;
67 if (conf->service_name && strcmp(conf->service_name, "Any") == 0) {
68 *server_creds = GSS_C_NO_CREDENTIAL;
72 have_server_princ = conf->service_name && strchr(conf->service_name, '/') != NULL;
73 if (have_server_princ)
74 strncpy(buf, conf->service_name, sizeof(buf));
76 snprintf(buf, sizeof(buf), "%s@%s",
77 (conf->service_name) ? conf->service_name : SERVICE_NAME,
78 ap_get_server_name(r));
81 token.length = strlen(buf) + 1;
83 major_status = gss_import_name(&minor_status, &token,
84 (have_server_princ) ? (gss_OID) GSS_KRB5_NT_PRINCIPAL_NAME : (gss_OID) GSS_C_NT_HOSTBASED_SERVICE,
86 memset(&token, 0, sizeof(token));
87 if (GSS_ERROR(major_status)) {
88 gss_log(APLOG_MARK, APLOG_ERR, 0, r,
89 "%s", get_gss_error(r, major_status, minor_status,
90 "gss_import_name() failed"));
91 return HTTP_INTERNAL_SERVER_ERROR;
94 major_status = gss_display_name(&minor_status, server_name, &token, NULL);
95 if (GSS_ERROR(major_status)) {
96 /* Perhaps we could just ignore this error but it's safer to give up now,
98 gss_log(APLOG_MARK, APLOG_ERR, 0, r,
99 "%s", get_gss_error(r, major_status, minor_status,
100 "gss_display_name() failed"));
101 return HTTP_INTERNAL_SERVER_ERROR;
104 gss_log(APLOG_MARK, APLOG_DEBUG, 0, r, "Acquiring creds for %s", token.value);
105 gss_release_buffer(&minor_status, &token);
107 major_status = gss_acquire_cred(&minor_status, server_name, GSS_C_INDEFINITE,
108 GSS_C_NO_OID_SET, GSS_C_ACCEPT,
109 server_creds, NULL, NULL);
110 gss_release_name(&minor_status2, &server_name);
111 if (GSS_ERROR(major_status)) {
112 gss_log(APLOG_MARK, APLOG_ERR, 0, r,
113 "%s", get_gss_error(r, major_status, minor_status,
114 "Failed to load GSS-API credentials"));
115 return HTTP_INTERNAL_SERVER_ERROR;
122 cmp_gss_type(gss_buffer_t token, gss_OID oid)
127 if (token->length == 0)
128 return GSS_S_DEFECTIVE_TOKEN;
132 return GSS_S_DEFECTIVE_TOKEN;
135 if ((len & 0x7f) > 4)
136 return GSS_S_DEFECTIVE_TOKEN;
140 return GSS_S_DEFECTIVE_TOKEN;
142 if (((OM_uint32) *p++) != oid->length)
143 return GSS_S_DEFECTIVE_TOKEN;
145 return memcmp(p, oid->elements, oid->length);
149 gss_authenticate(request_rec *r, gss_auth_config *conf, gss_conn_ctx ctx,
150 const char *auth_line, char **negotiate_ret_value)
152 OM_uint32 major_status, minor_status, minor_status2;
153 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
154 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
155 const char *auth_param = NULL;
157 gss_name_t client_name = GSS_C_NO_NAME;
158 gss_cred_id_t delegated_cred = GSS_C_NO_CREDENTIAL;
159 gss_cred_id_t server_creds = GSS_C_NO_CREDENTIAL;
160 OM_uint32 ret_flags = 0;
161 gss_OID_desc spnego_oid;
162 OM_uint32 (*accept_sec_context)
163 (OM_uint32 *, gss_ctx_id_t *, const gss_cred_id_t,
164 const gss_buffer_t, const gss_channel_bindings_t,
165 gss_name_t *, gss_OID *, gss_buffer_t, OM_uint32 *,
166 OM_uint32 *, gss_cred_id_t *);
168 *negotiate_ret_value = "\0";
170 spnego_oid.length = 6;
171 spnego_oid.elements = (void *)"\x2b\x06\x01\x05\x05\x02";
173 if (conf->krb5_keytab) {
175 /* we don't use the ap_* calls here, since the string passed to putenv()
176 * will become part of the enviroment and shouldn't be free()ed by apache
178 ktname = malloc(strlen("KRB5_KTNAME=") + strlen(conf->krb5_keytab) + 1);
179 if (ktname == NULL) {
180 gss_log(APLOG_MARK, APLOG_ERR, 0, r, "malloc() failed: not enough memory");
181 ret = HTTP_INTERNAL_SERVER_ERROR;
184 sprintf(ktname, "KRB5_KTNAME=%s", conf->krb5_keytab);
187 /* Seems to be also supported by latest MIT */
188 gsskrb5_register_acceptor_identity(conf->krb_5_keytab);
192 ret = get_gss_creds(r, conf, &server_creds);
196 /* ap_getword() shifts parameter */
197 auth_param = ap_getword_white(r->pool, &auth_line);
198 if (auth_param == NULL) {
199 gss_log(APLOG_MARK, APLOG_ERR, 0, r,
200 "No Authorization parameter in request from client");
201 ret = HTTP_UNAUTHORIZED;
205 if (ctx->state == GSS_CTX_ESTABLISHED) {
206 gss_delete_sec_context(&minor_status, &ctx->context, GSS_C_NO_BUFFER);
207 ctx->context = GSS_C_NO_CONTEXT;
208 ctx->state = GSS_CTX_EMPTY;
211 input_token.length = apr_base64_decode_len(auth_param) + 1;
212 input_token.value = apr_pcalloc(r->connection->pool, input_token.length);
213 if (input_token.value == NULL) {
214 gss_log(APLOG_MARK, APLOG_ERR, 0, r,
215 "ap_pcalloc() failed (not enough memory)");
216 ret = HTTP_INTERNAL_SERVER_ERROR;
219 input_token.length = apr_base64_decode(input_token.value, auth_param);
221 /* LOG length, type */
223 #ifdef GSSAPI_SUPPORTS_SPNEGO
224 accept_sec_context = gss_accept_sec_context;
226 accept_sec_context = (cmp_gss_type(&input_token, &spnego_oid) == 0) ?
227 gss_accept_sec_context_spnego : gss_accept_sec_context;
230 major_status = accept_sec_context(&minor_status,
234 GSS_C_NO_CHANNEL_BINDINGS,
241 gss_log(APLOG_MARK, APLOG_DEBUG, 0, r,
242 "Client %s us their credential",
243 (ret_flags & GSS_C_DELEG_FLAG) ? "delegated" : "didn't delegate");
244 if (output_token.length) {
248 len = apr_base64_encode_len(output_token.length) + 1;
249 token = apr_pcalloc(r->connection->pool, len + 1);
251 gss_log(APLOG_MARK, APLOG_ERR, 0, r,
252 "ap_pcalloc() failed (not enough memory)");
253 ret = HTTP_INTERNAL_SERVER_ERROR;
254 gss_release_buffer(&minor_status2, &output_token);
257 apr_base64_encode(token, output_token.value, output_token.length);
259 *negotiate_ret_value = token;
260 gss_log(APLOG_MARK, APLOG_DEBUG, 0, r,
261 "GSS-API token of length %d bytes will be sent back",
262 output_token.length);
263 gss_release_buffer(&minor_status2, &output_token);
266 if (GSS_ERROR(major_status)) {
267 gss_log(APLOG_MARK, APLOG_ERR, 0, r,
268 "%s", get_gss_error(r, major_status, minor_status,
269 "Failed to establish authentication"));
271 /* Don't offer the Negotiate method again if call to GSS layer failed */
272 /* XXX ... which means we don't return the "error" output */
273 *negotiate_ret_value = NULL;
275 gss_delete_sec_context(&minor_status, &ctx->context, GSS_C_NO_BUFFER);
276 ctx->context = GSS_C_NO_CONTEXT;
277 ctx->state = GSS_CTX_EMPTY;
278 ret = HTTP_UNAUTHORIZED;
282 if (major_status & GSS_S_CONTINUE_NEEDED) {
283 ctx->state = GSS_CTX_IN_PROGRESS;
284 ret = HTTP_UNAUTHORIZED;
288 major_status = gss_inquire_context(&minor_status, ctx->context, &client_name,
289 NULL, NULL, NULL, NULL, NULL, NULL);
290 if (GSS_ERROR(major_status)) {
291 gss_log(APLOG_MARK, APLOG_ERR, 0, r,
292 "%s", get_gss_error(r, major_status, minor_status, "gss_inquire_context() failed"));
293 ret = HTTP_INTERNAL_SERVER_ERROR;
297 major_status = gss_display_name(&minor_status, client_name, &output_token, NULL);
298 gss_release_name(&minor_status, &client_name);
299 if (GSS_ERROR(major_status)) {
300 gss_log(APLOG_MARK, APLOG_ERR, 0, r,
301 "%s", get_gss_error(r, major_status, minor_status,
302 "gss_display_name() failed"));
303 ret = HTTP_INTERNAL_SERVER_ERROR;
307 ctx->state = GSS_CTX_ESTABLISHED;
308 ctx->user = apr_pstrdup(r->pool, output_token.value);
309 gss_release_buffer(&minor_status, &output_token);
315 gss_release_cred(&minor_status, &delegated_cred);
317 if (output_token.length)
318 gss_release_buffer(&minor_status, &output_token);
320 if (client_name != GSS_C_NO_NAME)
321 gss_release_name(&minor_status, &client_name);
323 if (server_creds != GSS_C_NO_CREDENTIAL)
324 gss_release_cred(&minor_status, &server_creds);