2 Unix SMB/CIFS implementation.
3 Wrapper for krb5_init_context
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Stefan Metzmacher 2004
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "system/kerberos.h"
26 #include "auth/kerberos/kerberos.h"
27 #include "lib/socket/socket.h"
28 #include "lib/stream/packet.h"
29 #include "system/network.h"
30 #include "param/param.h"
31 #include "libcli/resolve/resolve.h"
32 #include "../lib/tsocket/tsocket.h"
33 #include "krb5_init_context.h"
35 context structure for operations on cldap packets
37 struct smb_krb5_socket {
38 struct socket_context *sock;
41 struct tevent_fd *fde;
44 DATA_BLOB request, reply;
46 struct packet_context *packet;
49 #ifdef SAMBA4_USES_HEIMDAL
54 static krb5_error_code smb_krb5_context_destroy(struct smb_krb5_context *ctx)
56 #ifdef SAMBA4_USES_HEIMDAL
57 if (ctx->pvt_log_data) {
58 /* Otherwise krb5_free_context will try and close what we
59 * have already free()ed */
60 krb5_set_warn_dest(ctx->krb5_context, NULL);
61 krb5_closelog(ctx->krb5_context,
62 (krb5_log_facility *)ctx->pvt_log_data);
65 krb5_free_context(ctx->krb5_context);
69 #ifdef SAMBA4_USES_HEIMDAL
70 /* We never close down the DEBUG system, and no need to unreference the use */
71 static void smb_krb5_debug_close(void *private_data) {
76 #ifdef SAMBA4_USES_HEIMDAL
77 static void smb_krb5_debug_wrapper(
78 #ifdef HAVE_KRB5_ADDLOG_FUNC_NEED_CONTEXT
80 #endif /* HAVE_KRB5_ADDLOG_FUNC_NEED_CONTEXT */
81 const char *timestr, const char *msg, void *private_data)
83 DEBUGC(DBGC_KERBEROS, 3, ("Kerberos: %s\n", msg));
87 #ifdef SAMBA4_USES_HEIMDAL
89 handle recv events on a smb_krb5 socket
91 static void smb_krb5_socket_recv(struct smb_krb5_socket *smb_krb5)
93 TALLOC_CTX *tmp_ctx = talloc_new(smb_krb5);
97 smb_krb5->status = socket_pending(smb_krb5->sock, &dsize);
98 if (!NT_STATUS_IS_OK(smb_krb5->status)) {
103 blob = data_blob_talloc(tmp_ctx, NULL, dsize);
104 if (blob.data == NULL && dsize != 0) {
105 smb_krb5->status = NT_STATUS_NO_MEMORY;
106 talloc_free(tmp_ctx);
110 smb_krb5->status = socket_recv(smb_krb5->sock, blob.data, blob.length, &nread);
111 if (!NT_STATUS_IS_OK(smb_krb5->status)) {
112 talloc_free(tmp_ctx);
118 smb_krb5->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
119 talloc_free(tmp_ctx);
123 DEBUG(4,("Received smb_krb5 packet of length %d\n",
126 talloc_steal(smb_krb5, blob.data);
127 smb_krb5->reply = blob;
128 talloc_free(tmp_ctx);
131 static NTSTATUS smb_krb5_full_packet(void *private_data, DATA_BLOB data)
133 struct smb_krb5_socket *smb_krb5 = talloc_get_type(private_data, struct smb_krb5_socket);
134 talloc_steal(smb_krb5, data.data);
135 smb_krb5->reply = data;
136 smb_krb5->reply.length -= 4;
137 smb_krb5->reply.data += 4;
142 handle request timeouts
144 static void smb_krb5_request_timeout(struct tevent_context *event_ctx,
145 struct tevent_timer *te, struct timeval t,
148 struct smb_krb5_socket *smb_krb5 = talloc_get_type(private_data, struct smb_krb5_socket);
149 DEBUG(5,("Timed out smb_krb5 packet\n"));
150 smb_krb5->status = NT_STATUS_IO_TIMEOUT;
153 static void smb_krb5_error_handler(void *private_data, NTSTATUS status)
155 struct smb_krb5_socket *smb_krb5 = talloc_get_type(private_data, struct smb_krb5_socket);
156 smb_krb5->status = status;
160 handle send events on a smb_krb5 socket
162 static void smb_krb5_socket_send(struct smb_krb5_socket *smb_krb5)
168 len = smb_krb5->request.length;
169 status = socket_send(smb_krb5->sock, &smb_krb5->request, &len);
171 if (!NT_STATUS_IS_OK(status)) return;
173 TEVENT_FD_READABLE(smb_krb5->fde);
175 TEVENT_FD_NOT_WRITEABLE(smb_krb5->fde);
181 handle fd events on a smb_krb5_socket
183 static void smb_krb5_socket_handler(struct tevent_context *ev, struct tevent_fd *fde,
184 uint16_t flags, void *private_data)
186 struct smb_krb5_socket *smb_krb5 = talloc_get_type(private_data, struct smb_krb5_socket);
187 switch (smb_krb5->hi->proto) {
188 case KRB5_KRBHST_UDP:
189 if (flags & TEVENT_FD_READ) {
190 smb_krb5_socket_recv(smb_krb5);
193 if (flags & TEVENT_FD_WRITE) {
194 smb_krb5_socket_send(smb_krb5);
199 case KRB5_KRBHST_TCP:
200 if (flags & TEVENT_FD_READ) {
201 packet_recv(smb_krb5->packet);
204 if (flags & TEVENT_FD_WRITE) {
205 packet_queue_run(smb_krb5->packet);
210 case KRB5_KRBHST_HTTP:
216 static krb5_error_code smb_krb5_send_and_recv_func_int(krb5_context context,
217 struct tevent_context *ev,
218 krb5_krbhst_info *hi,
220 krb5_send_to_kdc_func func,
223 const krb5_data *send_buf,
230 struct smb_krb5_socket *smb_krb5;
234 TALLOC_CTX *frame = talloc_stackframe();
239 send_blob = data_blob_const(send_buf->data, send_buf->length);
241 for (a = ai; a; a = a->ai_next) {
242 struct socket_address *remote_addr;
243 smb_krb5 = talloc(frame, struct smb_krb5_socket);
250 switch (a->ai_family) {
264 status = NT_STATUS_INVALID_PARAMETER;
266 case KRB5_KRBHST_UDP:
267 status = socket_create(smb_krb5, name,
271 case KRB5_KRBHST_TCP:
272 status = socket_create(smb_krb5, name,
276 case KRB5_KRBHST_HTTP:
280 if (!NT_STATUS_IS_OK(status)) {
281 talloc_free(smb_krb5);
285 remote_addr = socket_address_from_sockaddr(smb_krb5, a->ai_addr, a->ai_addrlen);
287 talloc_free(smb_krb5);
291 status = socket_connect_ev(smb_krb5->sock, NULL, remote_addr, 0, ev);
292 if (!NT_STATUS_IS_OK(status)) {
293 talloc_free(smb_krb5);
297 /* Setup the FDE, start listening for read events
298 * from the start (otherwise we may miss a socket
299 * drop) and mark as AUTOCLOSE along with the fde */
301 /* Ths is equivilant to EVENT_FD_READABLE(smb_krb5->fde) */
302 smb_krb5->fde = tevent_add_fd(ev, smb_krb5->sock,
303 socket_get_fd(smb_krb5->sock),
305 smb_krb5_socket_handler, smb_krb5);
306 /* its now the job of the event layer to close the socket */
307 tevent_fd_set_close_fn(smb_krb5->fde, socket_tevent_fd_close_fn);
308 socket_set_flags(smb_krb5->sock, SOCKET_FLAG_NOCLOSE);
310 tevent_add_timer(ev, smb_krb5,
311 timeval_current_ofs(timeout, 0),
312 smb_krb5_request_timeout, smb_krb5);
314 smb_krb5->status = NT_STATUS_OK;
315 smb_krb5->reply = data_blob(NULL, 0);
318 case KRB5_KRBHST_UDP:
319 TEVENT_FD_WRITEABLE(smb_krb5->fde);
320 smb_krb5->request = send_blob;
322 case KRB5_KRBHST_TCP:
324 smb_krb5->packet = packet_init(smb_krb5);
325 if (smb_krb5->packet == NULL) {
326 talloc_free(smb_krb5);
329 packet_set_private(smb_krb5->packet, smb_krb5);
330 packet_set_socket(smb_krb5->packet, smb_krb5->sock);
331 packet_set_callback(smb_krb5->packet, smb_krb5_full_packet);
332 packet_set_full_request(smb_krb5->packet, packet_full_request_u32);
333 packet_set_error_handler(smb_krb5->packet, smb_krb5_error_handler);
334 packet_set_event_context(smb_krb5->packet, ev);
335 packet_set_fde(smb_krb5->packet, smb_krb5->fde);
337 smb_krb5->request = data_blob_talloc(smb_krb5, NULL, send_blob.length + 4);
338 RSIVAL(smb_krb5->request.data, 0, send_blob.length);
339 memcpy(smb_krb5->request.data+4, send_blob.data, send_blob.length);
340 packet_send(smb_krb5->packet, smb_krb5->request);
342 case KRB5_KRBHST_HTTP:
346 while ((NT_STATUS_IS_OK(smb_krb5->status)) && !smb_krb5->reply.length) {
347 if (tevent_loop_once(ev) != 0) {
353 /* After each and every event loop, reset the
354 * send_to_kdc pointers to what they were when
355 * we entered this loop. That way, if a
356 * nested event has invalidated them, we put
357 * it back before we return to the heimdal
359 ret = krb5_set_send_to_kdc_func(context,
368 if (NT_STATUS_EQUAL(smb_krb5->status, NT_STATUS_IO_TIMEOUT)) {
369 talloc_free(smb_krb5);
373 if (!NT_STATUS_IS_OK(smb_krb5->status)) {
374 struct tsocket_address *addr = socket_address_to_tsocket_address(smb_krb5, remote_addr);
375 const char *addr_string = NULL;
377 addr_string = tsocket_address_inet_addr_string(addr, smb_krb5);
381 DEBUG(2,("Error reading smb_krb5 reply packet: %s from %s\n", nt_errstr(smb_krb5->status),
383 talloc_free(smb_krb5);
387 ret = krb5_data_copy(recv_buf, smb_krb5->reply.data, smb_krb5->reply.length);
392 talloc_free(smb_krb5);
400 return KRB5_KDC_UNREACH;
403 krb5_error_code smb_krb5_send_and_recv_func(krb5_context context,
405 krb5_krbhst_info *hi,
407 const krb5_data *send_buf,
413 struct tevent_context *ev;
414 TALLOC_CTX *frame = talloc_stackframe();
420 /* If no event context was available, then create one for this loop */
421 ev = samba_tevent_context_init(frame);
427 ev = talloc_get_type_abort(data, struct tevent_context);
430 ret = krb5_krbhst_get_addrinfo(context, hi, &ai);
436 ret = smb_krb5_send_and_recv_func_int(context, ev, hi, ai, smb_krb5_send_and_recv_func, data, timeout, send_buf, recv_buf);
441 krb5_error_code smb_krb5_send_and_recv_func_forced(krb5_context context,
442 void *data, /* struct addrinfo */
443 krb5_krbhst_info *hi,
445 const krb5_data *send_buf,
448 krb5_error_code k5ret;
449 struct addrinfo *ai = data;
451 struct tevent_context *ev;
452 TALLOC_CTX *frame = talloc_stackframe();
457 /* no event context is passed in, create one for this loop */
458 ev = samba_tevent_context_init(frame);
464 /* No need to pass in send_and_recv functions, we won't nest on this private event loop */
465 k5ret = smb_krb5_send_and_recv_func_int(context, ev, hi, ai, NULL, NULL,
466 timeout, send_buf, recv_buf);
473 smb_krb5_init_context_basic(TALLOC_CTX *tmp_ctx,
474 struct loadparm_context *lp_ctx,
475 krb5_context *_krb5_context)
478 #ifdef SAMBA4_USES_HEIMDAL
480 const char *config_file, *realm;
482 krb5_context krb5_ctx;
484 ret = smb_krb5_init_context_common(&krb5_ctx);
489 /* The MIT Kerberos build relies on using the system krb5.conf file.
490 * If you really want to use another file please set KRB5_CONFIG
492 #ifdef SAMBA4_USES_HEIMDAL
493 config_file = lpcfg_config_path(tmp_ctx, lp_ctx, "krb5.conf");
495 krb5_free_context(krb5_ctx);
499 /* Use our local krb5.conf file by default */
500 ret = krb5_prepend_config_files_default(config_file, &config_files);
502 DEBUG(1,("krb5_prepend_config_files_default failed (%s)\n",
503 smb_get_krb5_error_message(krb5_ctx, ret, tmp_ctx)));
504 krb5_free_context(krb5_ctx);
508 ret = krb5_set_config_files(krb5_ctx, config_files);
509 krb5_free_config_files(config_files);
511 DEBUG(1,("krb5_set_config_files failed (%s)\n",
512 smb_get_krb5_error_message(krb5_ctx, ret, tmp_ctx)));
513 krb5_free_context(krb5_ctx);
518 * This is already called in smb_krb5_init_context_common(),
519 * but krb5_set_config_files() may resets it.
521 krb5_set_dns_canonicalize_hostname(krb5_ctx, false);
523 realm = lpcfg_realm(lp_ctx);
525 ret = krb5_set_default_realm(krb5_ctx, realm);
527 DEBUG(1,("krb5_set_default_realm failed (%s)\n",
528 smb_get_krb5_error_message(krb5_ctx, ret, tmp_ctx)));
529 krb5_free_context(krb5_ctx);
534 *_krb5_context = krb5_ctx;
538 krb5_error_code smb_krb5_init_context(void *parent_ctx,
539 struct loadparm_context *lp_ctx,
540 struct smb_krb5_context **smb_krb5_context)
545 #ifdef SAMBA4_USES_HEIMDAL
546 krb5_log_facility *logf;
549 tmp_ctx = talloc_new(parent_ctx);
550 *smb_krb5_context = talloc_zero(tmp_ctx, struct smb_krb5_context);
552 if (!*smb_krb5_context || !tmp_ctx) {
553 talloc_free(tmp_ctx);
557 ret = smb_krb5_init_context_basic(tmp_ctx, lp_ctx, &kctx);
559 DEBUG(1,("smb_krb5_context_init_basic failed (%s)\n",
560 error_message(ret)));
561 talloc_free(tmp_ctx);
564 (*smb_krb5_context)->krb5_context = kctx;
566 talloc_set_destructor(*smb_krb5_context, smb_krb5_context_destroy);
568 #ifdef SAMBA4_USES_HEIMDAL
569 /* TODO: Should we have a different name here? */
570 ret = krb5_initlog(kctx, "Samba", &logf);
573 DEBUG(1,("krb5_initlog failed (%s)\n",
574 smb_get_krb5_error_message(kctx, ret, tmp_ctx)));
575 talloc_free(tmp_ctx);
578 (*smb_krb5_context)->pvt_log_data = logf;
580 ret = krb5_addlog_func(kctx, logf, 0 /* min */, -1 /* max */,
581 smb_krb5_debug_wrapper,
582 smb_krb5_debug_close, NULL);
584 DEBUG(1,("krb5_addlog_func failed (%s)\n",
585 smb_get_krb5_error_message(kctx, ret, tmp_ctx)));
586 talloc_free(tmp_ctx);
589 krb5_set_warn_dest(kctx, logf);
591 talloc_steal(parent_ctx, *smb_krb5_context);
592 talloc_free(tmp_ctx);
597 #ifdef SAMBA4_USES_HEIMDAL
598 krb5_error_code smb_krb5_context_set_event_ctx(struct smb_krb5_context *smb_krb5_context,
599 struct tevent_context *ev,
600 struct tevent_context **previous_ev)
607 *previous_ev = smb_krb5_context->current_ev;
609 smb_krb5_context->current_ev = talloc_reference(smb_krb5_context, ev);
610 if (!smb_krb5_context->current_ev) {
614 /* Set use of our socket lib */
615 ret = krb5_set_send_to_kdc_func(smb_krb5_context->krb5_context,
616 smb_krb5_send_and_recv_func,
619 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
620 DEBUG(1,("krb5_set_send_recv_func failed (%s)\n",
621 smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, tmp_ctx)));
622 talloc_free(tmp_ctx);
623 talloc_unlink(smb_krb5_context, smb_krb5_context->current_ev);
624 smb_krb5_context->current_ev = NULL;
630 krb5_error_code smb_krb5_context_remove_event_ctx(struct smb_krb5_context *smb_krb5_context,
631 struct tevent_context *previous_ev,
632 struct tevent_context *ev)
635 talloc_unlink(smb_krb5_context, ev);
636 /* If there was a mismatch with things happening on a stack, then don't wipe things */
637 smb_krb5_context->current_ev = previous_ev;
638 /* Set use of our socket lib */
639 ret = krb5_set_send_to_kdc_func(smb_krb5_context->krb5_context,
640 smb_krb5_send_and_recv_func,
643 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
644 DEBUG(1,("krb5_set_send_recv_func failed (%s)\n",
645 smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, tmp_ctx)));
646 talloc_free(tmp_ctx);