5 * - provides http_auth_backend_t "gssapi" for HTTP auth Basic realm="Kerberos"
6 * - provides http_auth_scheme_t "Negotiate"
7 * - (does not provide http_auth_backend_t for HTTP auth Digest)
9 * Note: Credentials cache (KRB5CCNAME) is exported into CGI and SSI environment
10 * as well as passed to FastCGI and SCGI (useful if on same machine
11 * and running under same user account with access to KRB5CCNAME file).
12 * Credentials are clean up at the end of each request.
15 * - no rate limiting of auth requests, so remote attacker can send many auth
16 * requests very quickly if attempting brute force password cracking attack
18 * FUTURE POTENTIAL PERFORMANCE ENHANCEMENTS:
19 * - Kerberos auth is synchronous and blocks waiting for response
20 * TODO: attempt async?
27 #include <gssapi/gssapi_krb5.h>
29 #include "http_auth.h"
41 buffer
*auth_gssapi_keytab
;
42 buffer
*auth_gssapi_principal
;
47 plugin_config
**config_storage
;
52 static handler_t
mod_authn_gssapi_check(server
*srv
, connection
*con
, void *p_d
, const struct http_auth_require_t
*require
, const struct http_auth_backend_t
*backend
);
53 static handler_t
mod_authn_gssapi_basic(server
*srv
, connection
*con
, void *p_d
, const http_auth_require_t
*require
, const buffer
*username
, const char *pw
);
55 static plugin_data
*plugin_data_singleton
;
57 INIT_FUNC(mod_authn_gssapi_init
) {
58 static http_auth_scheme_t http_auth_scheme_gssapi
=
59 { "gssapi", mod_authn_gssapi_check
, NULL
};
60 static http_auth_backend_t http_auth_backend_gssapi
=
61 { "gssapi", mod_authn_gssapi_basic
, NULL
, NULL
};
62 plugin_data
*p
= calloc(1, sizeof(*p
));
64 /* register http_auth_scheme_gssapi and http_auth_backend_gssapi */
65 http_auth_scheme_gssapi
.p_d
= p
;
66 http_auth_scheme_set(&http_auth_scheme_gssapi
);
67 http_auth_backend_gssapi
.p_d
= p
;
68 http_auth_backend_set(&http_auth_backend_gssapi
);
70 plugin_data_singleton
= p
;
74 FREE_FUNC(mod_authn_gssapi_free
) {
79 if (!p
) return HANDLER_GO_ON
;
81 if (p
->config_storage
) {
83 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
84 plugin_config
*s
= p
->config_storage
[i
];
86 if (NULL
== s
) continue;
88 buffer_free(s
->auth_gssapi_keytab
);
89 buffer_free(s
->auth_gssapi_principal
);
93 free(p
->config_storage
);
101 SETDEFAULTS_FUNC(mod_authn_gssapi_set_defaults
) {
102 plugin_data
*p
= p_d
;
104 config_values_t cv
[] = {
105 { "auth.backend.gssapi.keytab", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
},
106 { "auth.backend.gssapi.principal", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
},
107 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
110 p
->config_storage
= calloc(1, srv
->config_context
->used
* sizeof(plugin_config
*));
112 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
113 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
116 s
= calloc(1, sizeof(plugin_config
));
118 s
->auth_gssapi_keytab
= buffer_init();
119 s
->auth_gssapi_principal
= buffer_init();
121 cv
[0].destination
= s
->auth_gssapi_keytab
;
122 cv
[1].destination
= s
->auth_gssapi_principal
;
124 p
->config_storage
[i
] = s
;
126 if (0 != config_insert_values_global(srv
, config
->value
, cv
, i
== 0 ? T_CONFIG_SCOPE_SERVER
: T_CONFIG_SCOPE_CONNECTION
)) {
127 return HANDLER_ERROR
;
131 return HANDLER_GO_ON
;
136 static int mod_authn_gssapi_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
)
139 plugin_config
*s
= p
->config_storage
[0];
141 PATCH(auth_gssapi_keytab
);
142 PATCH(auth_gssapi_principal
);
144 /* skip the first, the global context */
145 for (i
= 1; i
< srv
->config_context
->used
; i
++) {
146 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
147 s
= p
->config_storage
[i
];
149 /* condition didn't match */
150 if (!config_check_cond(srv
, con
, dc
)) continue;
153 for (j
= 0; j
< dc
->value
->used
; j
++) {
154 data_unset
*du
= dc
->value
->data
[j
];
156 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("auth.backend.gssapi.keytab"))) {
157 PATCH(auth_gssapi_keytab
);
158 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("auth.backend.gssapi.principal"))) {
159 PATCH(auth_gssapi_principal
);
168 static handler_t
mod_authn_gssapi_send_400_bad_request (server
*srv
, connection
*con
)
171 con
->http_status
= 400;
173 return HANDLER_FINISHED
;
176 static void mod_authn_gssapi_log_gss_error(server
*srv
, const char *file
, unsigned int line
, const char *func
, const char *extra
, OM_uint32 err_maj
, OM_uint32 err_min
)
178 buffer
* const msg
= buffer_init_string(func
);
179 OM_uint32 maj_stat
, min_stat
;
180 OM_uint32 msg_ctx
= 0;
181 gss_buffer_desc status_string
;
183 buffer_append_string_len(msg
, CONST_STR_LEN("("));
184 if (extra
) buffer_append_string(msg
, extra
);
185 buffer_append_string_len(msg
, CONST_STR_LEN("):"));
188 maj_stat
= gss_display_status(&min_stat
, err_maj
, GSS_C_GSS_CODE
,
189 GSS_C_NO_OID
, &msg_ctx
, &status_string
);
190 if (GSS_ERROR(maj_stat
))
193 buffer_append_string(msg
, status_string
.value
);
194 gss_release_buffer(&min_stat
, &status_string
);
196 maj_stat
= gss_display_status(&min_stat
, err_min
, GSS_C_MECH_CODE
,
197 GSS_C_NULL_OID
, &msg_ctx
, &status_string
);
198 if (!GSS_ERROR(maj_stat
)) {
199 buffer_append_string(msg
, " (");
200 buffer_append_string(msg
, status_string
.value
);
201 buffer_append_string(msg
, ")");
202 gss_release_buffer(&min_stat
, &status_string
);
204 } while (!GSS_ERROR(maj_stat
) && msg_ctx
!= 0);
206 log_error_write(srv
, file
, line
, "b", msg
);
210 static void mod_authn_gssapi_log_krb5_error(server
*srv
, const char *file
, unsigned int line
, const char *func
, const char *extra
, krb5_context context
, int code
)
213 /*(extra might be NULL)*/
214 log_error_write(srv
, file
, line
, "sssss", func
, "(", extra
, "):",
215 error_message(code
));
218 static int mod_authn_gssapi_create_krb5_ccache(server
*srv
, connection
*con
, plugin_data
*p
, krb5_context kcontext
, krb5_principal princ
, krb5_ccache
*ccache
)
220 buffer
* const kccname
= buffer_init_string("FILE:/tmp/krb5cc_gssapi_XXXXXX");
221 char * const ccname
= kccname
->ptr
+ sizeof("FILE:")-1;
222 const size_t ccnamelen
= buffer_string_length(kccname
)-(sizeof("FILE:")-1);
223 /*(future: might consider using server.upload-dirs instead of /tmp)*/
224 int fd
= mkstemp(ccname
);
226 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "mkstemp():", strerror(errno
));
227 buffer_free(kccname
);
233 krb5_error_code problem
;
235 problem
= krb5_cc_resolve(kcontext
, kccname
->ptr
, ccache
);
237 mod_authn_gssapi_log_krb5_error(srv
, __FILE__
, __LINE__
, "krb5_cc_resolve", NULL
, kcontext
, problem
);
241 problem
= krb5_cc_initialize(kcontext
, *ccache
, princ
);
243 mod_authn_gssapi_log_krb5_error(srv
, __FILE__
, __LINE__
, "krb5_cc_initialize", kccname
->ptr
, kcontext
, problem
);
247 con
->plugin_ctx
[p
->id
] = kccname
;
249 array_set_key_value(con
->environment
, CONST_STR_LEN("KRB5CCNAME"), ccname
, ccnamelen
);
250 array_set_key_value(con
->request
.headers
, CONST_STR_LEN("X-Forwarded-Keytab"), ccname
, ccnamelen
);
257 krb5_cc_destroy(kcontext
, *ccache
);
261 buffer_free(kccname
);
267 * HTTP auth Negotiate
270 static handler_t
mod_authn_gssapi_send_401_unauthorized_negotiate (server
*srv
, connection
*con
)
272 con
->http_status
= 401;
274 response_header_insert(srv
, con
, CONST_STR_LEN("WWW-Authenticate"), CONST_STR_LEN("Negotiate"));
275 return HANDLER_FINISHED
;
278 static int mod_authn_gssapi_store_gss_creds(server
*srv
, connection
*con
, plugin_data
*p
, char *princ_name
, gss_cred_id_t delegated_cred
)
280 OM_uint32 maj_stat
, min_stat
;
281 krb5_principal princ
= NULL
;
282 krb5_ccache ccache
= NULL
;
283 krb5_error_code problem
;
284 krb5_context context
;
286 problem
= krb5_init_context(&context
);
288 mod_authn_gssapi_log_krb5_error(srv
, __FILE__
, __LINE__
, "krb5_init_context", NULL
, context
, problem
);
292 problem
= krb5_parse_name(context
, princ_name
, &princ
);
294 mod_authn_gssapi_log_krb5_error(srv
, __FILE__
, __LINE__
, "krb5_parse_name", NULL
, context
, problem
);
298 if (mod_authn_gssapi_create_krb5_ccache(srv
, con
, p
, context
, princ
, &ccache
))
301 maj_stat
= gss_krb5_copy_ccache(&min_stat
, delegated_cred
, ccache
);
302 if (GSS_ERROR(maj_stat
)) {
303 mod_authn_gssapi_log_gss_error(srv
, __FILE__
, __LINE__
, "gss_krb5_copy_ccache", princ_name
, maj_stat
, min_stat
);
307 krb5_cc_close(context
, ccache
);
308 krb5_free_principal(context
, princ
);
309 krb5_free_context(context
);
314 krb5_free_principal(context
, princ
);
316 krb5_cc_destroy(context
, ccache
);
317 krb5_free_context(context
);
322 static handler_t
mod_authn_gssapi_check_spnego(server
*srv
, connection
*con
, plugin_data
*p
, const http_auth_require_t
*require
, const char *realm_str
)
324 OM_uint32 st_major
, st_minor
, acc_flags
;
325 gss_buffer_desc token_s
= GSS_C_EMPTY_BUFFER
;
326 gss_buffer_desc token_in
= GSS_C_EMPTY_BUFFER
;
327 gss_buffer_desc token_out
= GSS_C_EMPTY_BUFFER
;
328 gss_cred_id_t server_cred
= GSS_C_NO_CREDENTIAL
;
329 gss_cred_id_t client_cred
= GSS_C_NO_CREDENTIAL
;
330 gss_ctx_id_t context
= GSS_C_NO_CONTEXT
;
331 gss_name_t server_name
= GSS_C_NO_NAME
;
332 gss_name_t client_name
= GSS_C_NO_NAME
;
334 /*(future: might modify http_auth_scheme_t to store (void *)p_d
335 * and pass to checkfn, similar to http_auth_backend_t) */
340 buffer
*t_in
= buffer_init();
341 if (!buffer_append_base64_decode(t_in
, realm_str
, strlen(realm_str
), BASE64_STANDARD
)) {
342 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "decoding GSSAPI authentication header failed", realm_str
);
344 return mod_authn_gssapi_send_400_bad_request(srv
, con
);
347 mod_authn_gssapi_patch_connection(srv
, con
, p
);
349 /* ??? Should code = krb5_kt_resolve(kcontext, p->conf.auth_gssapi_keytab->ptr, &keytab);
350 * be used, instead of putenv() of KRB5_KTNAME=...? See mod_authn_gssapi_basic() */
351 /* ??? Should KRB5_KTNAME go into con->environment instead ??? */
352 /* ??? Should KRB5_KTNAME be added to mod_authn_gssapi_basic(), too? */
353 ktname
= buffer_init_string("KRB5_KTNAME=");
354 buffer_append_string_buffer(ktname
, p
->conf
.auth_gssapi_keytab
);
356 /* ktname becomes part of the environment, do not free */
357 /* buffer_free(ktname); */
359 sprinc
= buffer_init_buffer(p
->conf
.auth_gssapi_principal
);
360 if (strchr(sprinc
->ptr
, '/') == NULL
) {
361 buffer_append_string(sprinc
, "/");
362 /*(copy HTTP Host, omitting port if port is present)*/
363 buffer_append_string_len(sprinc
, con
->request
.http_host
->ptr
, strcspn(con
->request
.http_host
->ptr
, ":"));
365 if (strchr(sprinc
->ptr
, '@') == NULL
) {
366 buffer_append_string(sprinc
, "@");
367 buffer_append_string_buffer(sprinc
, require
->realm
);
369 /*#define GSS_C_NT_USER_NAME gss_nt_user_name*/
370 /*#define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name*/
371 #define GSS_KRB5_NT_PRINCIPAL_NAME gss_nt_krb5_name
373 token_s
.value
= sprinc
->ptr
;
374 token_s
.length
= buffer_string_length(sprinc
);
375 st_major
= gss_import_name(&st_minor
, &token_s
, (gss_OID
) GSS_KRB5_NT_PRINCIPAL_NAME
, &server_name
);
376 if (GSS_ERROR(st_major
)) {
377 mod_authn_gssapi_log_gss_error(srv
, __FILE__
, __LINE__
, "gss_import_name", NULL
, st_major
, st_minor
);
381 memset(&token_s
, 0, sizeof(token_s
));
382 st_major
= gss_display_name(&st_minor
, server_name
, &token_s
, NULL
);
383 if (GSS_ERROR(st_major
)) {
384 mod_authn_gssapi_log_gss_error(srv
, __FILE__
, __LINE__
, "gss_display_name", NULL
, st_major
, st_minor
);
388 /* acquire server's own credentials */
389 st_major
= gss_acquire_cred(&st_minor
, server_name
, GSS_C_INDEFINITE
, GSS_C_NO_OID_SET
, GSS_C_ACCEPT
, &server_cred
, NULL
, NULL
);
390 if (GSS_ERROR(st_major
)) {
391 mod_authn_gssapi_log_gss_error(srv
, __FILE__
, __LINE__
, "gss_acquire_cred", sprinc
->ptr
, st_major
, st_minor
);
395 /* accept the user's context */
396 token_in
.length
= buffer_string_length(t_in
);
397 token_in
.value
= t_in
->ptr
;
398 st_major
= gss_accept_sec_context(&st_minor
, &context
, server_cred
, &token_in
, GSS_C_NO_CHANNEL_BINDINGS
,
399 &client_name
, NULL
, &token_out
, &acc_flags
, NULL
, &client_cred
);
400 if (GSS_ERROR(st_major
)) {
401 mod_authn_gssapi_log_gss_error(srv
, __FILE__
, __LINE__
, "gss_accept_sec_context", NULL
, st_major
, st_minor
);
405 /* fetch the username */
406 st_major
= gss_display_name(&st_minor
, client_name
, &token_out
, NULL
);
407 if (GSS_ERROR(st_major
)) {
408 mod_authn_gssapi_log_gss_error(srv
, __FILE__
, __LINE__
, "gss_display_name", NULL
, st_major
, st_minor
);
412 /* check the allow-rules */
413 if (!http_auth_match_rules(require
, token_out
.value
, NULL
, NULL
)) {
414 log_error_write(srv
, __FILE__
, __LINE__
, "s", "rules didn't match");
418 if (!(acc_flags
& GSS_C_CONF_FLAG
)) {
419 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "No confidentiality for user:", token_out
.value
);
423 if (!(acc_flags
& GSS_C_DELEG_FLAG
)) {
424 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "Unable to delegate credentials for user:", token_out
.value
);
428 ret
= mod_authn_gssapi_store_gss_creds(srv
, con
, p
, token_out
.value
, client_cred
);
430 http_auth_setenv(con
->environment
, token_out
.value
, token_out
.length
, CONST_STR_LEN("GSSAPI"));
437 if (context
!= GSS_C_NO_CONTEXT
)
438 gss_delete_sec_context(&st_minor
, &context
, GSS_C_NO_BUFFER
);
440 if (client_cred
!= GSS_C_NO_CREDENTIAL
)
441 gss_release_cred(&st_minor
, &client_cred
);
442 if (server_cred
!= GSS_C_NO_CREDENTIAL
)
443 gss_release_cred(&st_minor
, &server_cred
);
445 if (client_name
!= GSS_C_NO_NAME
)
446 gss_release_name(&st_minor
, &client_name
);
447 if (server_name
!= GSS_C_NO_NAME
)
448 gss_release_name(&st_minor
, &server_name
);
451 gss_release_buffer(&st_minor
, &token_s
);
452 /* if (token_in.length)
453 * gss_release_buffer(&st_minor, &token_in); */
454 if (token_out
.length
)
455 gss_release_buffer(&st_minor
, &token_out
);
457 return ret
? HANDLER_GO_ON
: mod_authn_gssapi_send_401_unauthorized_negotiate(srv
, con
);
460 static handler_t
mod_authn_gssapi_check (server
*srv
, connection
*con
, void *p_d
, const struct http_auth_require_t
*require
, const struct http_auth_backend_t
*backend
)
462 data_string
* const ds
=
463 (data_string
*)array_get_element(con
->request
.headers
, "Authorization");
466 if (NULL
== ds
|| buffer_is_empty(ds
->value
)) {
467 return mod_authn_gssapi_send_401_unauthorized_negotiate(srv
, con
);
470 if (0 != strncasecmp(ds
->value
->ptr
, "Negotiate ", sizeof("Negotiate ")-1)) {
471 return mod_authn_gssapi_send_400_bad_request(srv
, con
);
474 return mod_authn_gssapi_check_spnego(srv
, con
, (plugin_data
*)p_d
, require
, ds
->value
->ptr
+sizeof("Negotiate ")-1);
478 * HTTP auth Basic realm="kerberos"
481 static krb5_error_code
mod_authn_gssapi_verify_krb5_init_creds(server
*srv
, krb5_context context
, krb5_creds
*creds
, krb5_principal ap_req_server
, krb5_keytab ap_req_keytab
)
485 krb5_ccache local_ccache
= NULL
;
486 krb5_creds
*new_creds
= NULL
;
487 krb5_auth_context auth_context
= NULL
;
488 krb5_keytab keytab
= NULL
;
491 memset(&req
, 0, sizeof(req
));
493 if (ap_req_keytab
== NULL
) {
494 ret
= krb5_kt_default(context
, &keytab
);
498 keytab
= ap_req_keytab
;
500 ret
= krb5_cc_resolve(context
, "MEMORY:", &local_ccache
);
502 log_error_write(srv
, __FILE__
, __LINE__
, "s", "krb5_cc_resolve() failed when verifying KDC");
507 ret
= krb5_cc_initialize(context
, local_ccache
, creds
->client
);
509 log_error_write(srv
, __FILE__
, __LINE__
, "s", "krb5_cc_initialize() failed when verifying KDC");
513 ret
= krb5_cc_store_cred(context
, local_ccache
, creds
);
515 log_error_write(srv
, __FILE__
, __LINE__
, "s", "krb5_cc_store_cred() failed when verifying KDC");
519 ret
= krb5_unparse_name(context
, ap_req_server
, &server_name
);
521 log_error_write(srv
, __FILE__
, __LINE__
, "s", "krb5_unparse_name() failed when verifying KDC");
524 /* log_error_write(srv, __FILE__, __LINE__, "ss", "Trying to verify authenticity of KDC using principal", server_name); */
527 if (!krb5_principal_compare(context
, ap_req_server
, creds
->server
)) {
528 krb5_creds match_cred
;
530 memset(&match_cred
, 0, sizeof(match_cred
));
532 match_cred
.client
= creds
->client
;
533 match_cred
.server
= ap_req_server
;
535 ret
= krb5_get_credentials(context
, 0, local_ccache
, &match_cred
, &new_creds
);
537 log_error_write(srv
, __FILE__
, __LINE__
, "s", "krb5_get_credentials() failed when verifying KDC");
543 ret
= krb5_mk_req_extended(context
, &auth_context
, 0, NULL
, creds
, &req
);
545 log_error_write(srv
, __FILE__
, __LINE__
, "s", "krb5_mk_req_extended() failed when verifying KDC");
549 krb5_auth_con_free(context
, auth_context
);
551 ret
= krb5_auth_con_init(context
, &auth_context
);
553 log_error_write(srv
, __FILE__
, __LINE__
, "s", "krb5_auth_con_init() failed when verifying KDC");
557 /* use KRB5_AUTH_CONTEXT_DO_SEQUENCE to skip replay cache checks */
558 krb5_auth_con_setflags(context
, auth_context
, KRB5_AUTH_CONTEXT_DO_SEQUENCE
);
559 ret
= krb5_rd_req(context
, &auth_context
, &req
, ap_req_server
, keytab
, 0, NULL
);
561 log_error_write(srv
, __FILE__
, __LINE__
, "s", "krb5_rd_req() failed when verifying KDC");
566 krb5_free_data_contents(context
, &req
);
568 krb5_auth_con_free(context
, auth_context
);
570 krb5_free_creds(context
, new_creds
);
571 if (ap_req_keytab
== NULL
&& keytab
)
572 krb5_kt_close(context
, keytab
);
574 krb5_cc_destroy(context
, local_ccache
);
579 static int mod_authn_gssapi_store_krb5_creds(server
*srv
, connection
*con
, plugin_data
*p
,
580 krb5_context kcontext
, krb5_ccache delegated_cred
)
582 krb5_error_code problem
;
583 krb5_principal princ
= NULL
;
584 krb5_ccache ccache
= NULL
;
586 problem
= krb5_cc_get_principal(kcontext
, delegated_cred
, &princ
);
588 mod_authn_gssapi_log_krb5_error(srv
, __FILE__
, __LINE__
, "krb5_cc_get_principal", NULL
, kcontext
, problem
);
592 if (mod_authn_gssapi_create_krb5_ccache(srv
, con
, p
, kcontext
, princ
, &ccache
)) {
596 problem
= krb5_cc_copy_creds(kcontext
, delegated_cred
, ccache
);
598 mod_authn_gssapi_log_krb5_error(srv
, __FILE__
, __LINE__
, "krb5_cc_copy_creds", NULL
, kcontext
, problem
);
602 krb5_free_principal(kcontext
, princ
);
603 krb5_cc_close(kcontext
, ccache
);
608 krb5_free_principal(kcontext
, princ
);
610 krb5_cc_destroy(kcontext
, ccache
);
614 static handler_t
mod_authn_gssapi_send_401_unauthorized_basic (server
*srv
, connection
*con
)
616 con
->http_status
= 401;
618 response_header_insert(srv
, con
, CONST_STR_LEN("WWW-Authenticate"), CONST_STR_LEN("Basic realm=\"Kerberos\""));
619 return HANDLER_FINISHED
;
622 static handler_t
mod_authn_gssapi_basic(server
*srv
, connection
*con
, void *p_d
, const http_auth_require_t
*require
, const buffer
*username
, const char *pw
)
624 krb5_context kcontext
= NULL
;
625 krb5_keytab keytab
= NULL
;
626 krb5_principal s_princ
= NULL
;
627 krb5_principal c_princ
= NULL
;
629 krb5_ccache c_ccache
= NULL
;
630 krb5_ccache ret_ccache
= NULL
;
631 krb5_error_code code
;
634 buffer
*user_at_realm
= NULL
;
635 plugin_data
* const p
= (plugin_data
*)p_d
;
638 log_error_write(srv
, __FILE__
, __LINE__
, "s", "Empty passwords are not accepted");
639 mod_authn_gssapi_send_401_unauthorized_basic(srv
, con
);
642 mod_authn_gssapi_patch_connection(srv
, con
, p
);
644 code
= krb5_init_context(&kcontext
);
646 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "krb5_init_context():", code
);
647 mod_authn_gssapi_send_401_unauthorized_basic(srv
, con
); /*(well, should be 500)*/
650 code
= krb5_kt_resolve(kcontext
, p
->conf
.auth_gssapi_keytab
->ptr
, &keytab
);
652 log_error_write(srv
, __FILE__
, __LINE__
, "sdb", "krb5_kt_resolve():", code
, p
->conf
.auth_gssapi_keytab
);
653 mod_authn_gssapi_send_401_unauthorized_basic(srv
, con
); /*(well, should be 500)*/
656 sprinc
= buffer_init_buffer(p
->conf
.auth_gssapi_principal
);
657 if (strchr(sprinc
->ptr
, '/') == NULL
) {
658 buffer_append_string(sprinc
, "/");
659 /*(copy HTTP Host, omitting port if port is present)*/
660 buffer_append_string_len(sprinc
, con
->request
.http_host
->ptr
, strcspn(con
->request
.http_host
->ptr
, ":"));
663 /*(init c_creds before anything which might krb5_free_cred_contents())*/
664 memset(&c_creds
, 0, sizeof(c_creds
));
666 ret
= krb5_parse_name(kcontext
, sprinc
->ptr
, &s_princ
);
668 mod_authn_gssapi_log_krb5_error(srv
, __FILE__
, __LINE__
, "krb5_parse_name", sprinc
->ptr
, kcontext
, ret
);
673 if (strchr(username
->ptr
, '@') == NULL
) {
674 user_at_realm
= buffer_init_buffer(username
);
675 BUFFER_APPEND_STRING_CONST(user_at_realm
, "@");
676 buffer_append_string_buffer(user_at_realm
, require
->realm
);
679 ret
= krb5_parse_name(kcontext
, (user_at_realm
? user_at_realm
->ptr
: username
->ptr
), &c_princ
);
681 mod_authn_gssapi_log_krb5_error(srv
, __FILE__
, __LINE__
, "krb5_parse_name", (user_at_realm
? user_at_realm
->ptr
: username
->ptr
), kcontext
, ret
);
682 if (user_at_realm
) buffer_free(user_at_realm
);
686 if (user_at_realm
) buffer_free(user_at_realm
);
687 /* XXX: if the qualified username with @realm should be in REMOTE_USER,
688 * then http_auth_backend_t basic interface needs to change to pass
689 * modifiable buffer *username, but note that const char *pw follows
690 * in the truncated buffer *username, so pw would need to be copied
691 * before modifying buffer *username */
695 * ret = krb5_unparse_name(kcontext, c_princ, &name);
697 * log_error_write(srv, __FILE__, __LINE__, "sbss", "Trying to get TGT for user:", username, "password:", pw);
702 ret
= krb5_get_init_creds_password(kcontext
, &c_creds
, c_princ
, pw
, NULL
, NULL
, 0, NULL
, NULL
);
704 mod_authn_gssapi_log_krb5_error(srv
, __FILE__
, __LINE__
, "krb5_get_init_creds_password", NULL
, kcontext
, ret
);
708 ret
= mod_authn_gssapi_verify_krb5_init_creds(srv
, kcontext
, &c_creds
, s_princ
, keytab
);
710 mod_authn_gssapi_log_krb5_error(srv
, __FILE__
, __LINE__
, "mod_authn_gssapi_verify_krb5_init_creds", NULL
, kcontext
, ret
);
714 ret
= krb5_cc_resolve(kcontext
, "MEMORY:", &ret_ccache
);
716 mod_authn_gssapi_log_krb5_error(srv
, __FILE__
, __LINE__
, "krb5_cc_resolve", NULL
, kcontext
, ret
);
720 ret
= krb5_cc_initialize(kcontext
, ret_ccache
, c_princ
);
722 mod_authn_gssapi_log_krb5_error(srv
, __FILE__
, __LINE__
, "krb5_cc_initialize", NULL
, kcontext
, ret
);
726 ret
= krb5_cc_store_cred(kcontext
, ret_ccache
, &c_creds
);
728 mod_authn_gssapi_log_krb5_error(srv
, __FILE__
, __LINE__
, "krb5_cc_store_cred", NULL
, kcontext
, ret
);
732 c_ccache
= ret_ccache
;
737 krb5_free_cred_contents(kcontext
, &c_creds
);
739 krb5_cc_destroy(kcontext
, ret_ccache
);
741 if (!ret
&& c_ccache
&& (ret
= mod_authn_gssapi_store_krb5_creds(srv
, con
, p
, kcontext
, c_ccache
))) {
742 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "mod_authn_gssapi_store_krb5_creds failed for", username
);
746 krb5_free_principal(kcontext
, c_princ
);
748 krb5_free_principal(kcontext
, s_princ
);
752 krb5_cc_destroy(kcontext
, c_ccache
);
754 krb5_kt_close(kcontext
, keytab
);
756 krb5_free_context(kcontext
);
758 if (0 == ret
&& http_auth_match_rules(require
,username
->ptr
,NULL
,NULL
)){
759 return HANDLER_GO_ON
;
762 /* ret == KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN or no authz rules match */
763 log_error_write(srv
, __FILE__
, __LINE__
, "sbsBsB", "password doesn't match for", con
->uri
.path
, "username:", username
, ", IP:", con
->dst_addr_buf
);
764 return mod_authn_gssapi_send_401_unauthorized_basic(srv
, con
);
769 REQUESTDONE_FUNC(mod_authn_gssapi_request_done
) {
770 plugin_data
*p
= (plugin_data
*)p_d
;
771 buffer
*kccname
= (buffer
*)con
->plugin_ctx
[p
->id
];
772 if (NULL
!= kccname
) {
773 con
->plugin_ctx
[p
->id
] = NULL
;
774 unlink(kccname
->ptr
+sizeof("FILE:")-1);
775 buffer_free(kccname
);
779 return HANDLER_GO_ON
;
782 int mod_authn_gssapi_plugin_init(plugin
*p
);
783 int mod_authn_gssapi_plugin_init(plugin
*p
) {
784 p
->version
= LIGHTTPD_VERSION_ID
;
785 p
->name
= buffer_init_string("authn_gssapi");
786 p
->init
= mod_authn_gssapi_init
;
787 p
->set_defaults
= mod_authn_gssapi_set_defaults
;
788 p
->cleanup
= mod_authn_gssapi_free
;
789 p
->handle_request_done
= mod_authn_gssapi_request_done
;