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"
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;
53 static krb5_error_code smb_krb5_context_destroy(struct smb_krb5_context *ctx)
55 /* Otherwise krb5_free_context will try and close what we have already free()ed */
56 krb5_set_warn_dest(ctx->krb5_context, NULL);
57 krb5_closelog(ctx->krb5_context, ctx->logf);
58 krb5_free_context(ctx->krb5_context);
62 /* We never close down the DEBUG system, and no need to unreference the use */
63 static void smb_krb5_debug_close(void *private_data) {
67 static void smb_krb5_debug_wrapper(const char *timestr, const char *msg, void *private_data)
69 DEBUG(3, ("Kerberos: %s\n", msg));
73 handle recv events on a smb_krb5 socket
75 static void smb_krb5_socket_recv(struct smb_krb5_socket *smb_krb5)
77 TALLOC_CTX *tmp_ctx = talloc_new(smb_krb5);
81 smb_krb5->status = socket_pending(smb_krb5->sock, &dsize);
82 if (!NT_STATUS_IS_OK(smb_krb5->status)) {
87 blob = data_blob_talloc(tmp_ctx, NULL, dsize);
88 if (blob.data == NULL && dsize != 0) {
89 smb_krb5->status = NT_STATUS_NO_MEMORY;
94 smb_krb5->status = socket_recv(smb_krb5->sock, blob.data, blob.length, &nread);
95 if (!NT_STATUS_IS_OK(smb_krb5->status)) {
102 smb_krb5->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
103 talloc_free(tmp_ctx);
107 DEBUG(4,("Received smb_krb5 packet of length %d\n",
110 talloc_steal(smb_krb5, blob.data);
111 smb_krb5->reply = blob;
112 talloc_free(tmp_ctx);
115 static NTSTATUS smb_krb5_full_packet(void *private_data, DATA_BLOB data)
117 struct smb_krb5_socket *smb_krb5 = talloc_get_type(private_data, struct smb_krb5_socket);
118 talloc_steal(smb_krb5, data.data);
119 smb_krb5->reply = data;
120 smb_krb5->reply.length -= 4;
121 smb_krb5->reply.data += 4;
126 handle request timeouts
128 static void smb_krb5_request_timeout(struct tevent_context *event_ctx,
129 struct tevent_timer *te, struct timeval t,
132 struct smb_krb5_socket *smb_krb5 = talloc_get_type(private_data, struct smb_krb5_socket);
133 DEBUG(5,("Timed out smb_krb5 packet\n"));
134 smb_krb5->status = NT_STATUS_IO_TIMEOUT;
137 static void smb_krb5_error_handler(void *private_data, NTSTATUS status)
139 struct smb_krb5_socket *smb_krb5 = talloc_get_type(private_data, struct smb_krb5_socket);
140 smb_krb5->status = status;
144 handle send events on a smb_krb5 socket
146 static void smb_krb5_socket_send(struct smb_krb5_socket *smb_krb5)
152 len = smb_krb5->request.length;
153 status = socket_send(smb_krb5->sock, &smb_krb5->request, &len);
155 if (!NT_STATUS_IS_OK(status)) return;
157 TEVENT_FD_READABLE(smb_krb5->fde);
159 TEVENT_FD_NOT_WRITEABLE(smb_krb5->fde);
165 handle fd events on a smb_krb5_socket
167 static void smb_krb5_socket_handler(struct tevent_context *ev, struct tevent_fd *fde,
168 uint16_t flags, void *private_data)
170 struct smb_krb5_socket *smb_krb5 = talloc_get_type(private_data, struct smb_krb5_socket);
171 switch (smb_krb5->hi->proto) {
172 case KRB5_KRBHST_UDP:
173 if (flags & TEVENT_FD_READ) {
174 smb_krb5_socket_recv(smb_krb5);
177 if (flags & TEVENT_FD_WRITE) {
178 smb_krb5_socket_send(smb_krb5);
183 case KRB5_KRBHST_TCP:
184 if (flags & TEVENT_FD_READ) {
185 packet_recv(smb_krb5->packet);
188 if (flags & TEVENT_FD_WRITE) {
189 packet_queue_run(smb_krb5->packet);
194 case KRB5_KRBHST_HTTP:
201 krb5_error_code smb_krb5_send_and_recv_func(krb5_context context,
203 krb5_krbhst_info *hi,
205 const krb5_data *send_buf,
211 struct addrinfo *ai, *a;
212 struct smb_krb5_socket *smb_krb5;
216 struct tevent_context *ev;
217 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
223 /* If no event context was available, then create one for this loop */
224 ev = tevent_context_init(tmp_ctx);
226 talloc_free(tmp_ctx);
230 ev = talloc_get_type_abort(data, struct tevent_context);
233 send_blob = data_blob_const(send_buf->data, send_buf->length);
235 ret = krb5_krbhst_get_addrinfo(context, hi, &ai);
237 talloc_free(tmp_ctx);
241 for (a = ai; a; a = a->ai_next) {
242 struct socket_address *remote_addr;
243 smb_krb5 = talloc(tmp_ctx, struct smb_krb5_socket);
245 talloc_free(tmp_ctx);
250 switch (a->ai_family) {
260 talloc_free(tmp_ctx);
264 status = NT_STATUS_INVALID_PARAMETER;
266 case KRB5_KRBHST_UDP:
267 status = socket_create(name, SOCKET_TYPE_DGRAM, &smb_krb5->sock, 0);
269 case KRB5_KRBHST_TCP:
270 status = socket_create(name, SOCKET_TYPE_STREAM, &smb_krb5->sock, 0);
272 case KRB5_KRBHST_HTTP:
273 talloc_free(tmp_ctx);
276 if (!NT_STATUS_IS_OK(status)) {
277 talloc_free(smb_krb5);
281 talloc_steal(smb_krb5, smb_krb5->sock);
283 remote_addr = socket_address_from_sockaddr(smb_krb5, a->ai_addr, a->ai_addrlen);
285 talloc_free(smb_krb5);
289 status = socket_connect_ev(smb_krb5->sock, NULL, remote_addr, 0, ev);
290 if (!NT_STATUS_IS_OK(status)) {
291 talloc_free(smb_krb5);
295 /* Setup the FDE, start listening for read events
296 * from the start (otherwise we may miss a socket
297 * drop) and mark as AUTOCLOSE along with the fde */
299 /* Ths is equivilant to EVENT_FD_READABLE(smb_krb5->fde) */
300 smb_krb5->fde = tevent_add_fd(ev, smb_krb5->sock,
301 socket_get_fd(smb_krb5->sock),
303 smb_krb5_socket_handler, smb_krb5);
304 /* its now the job of the event layer to close the socket */
305 tevent_fd_set_close_fn(smb_krb5->fde, socket_tevent_fd_close_fn);
306 socket_set_flags(smb_krb5->sock, SOCKET_FLAG_NOCLOSE);
308 tevent_add_timer(ev, smb_krb5,
309 timeval_current_ofs(timeout, 0),
310 smb_krb5_request_timeout, smb_krb5);
312 smb_krb5->status = NT_STATUS_OK;
313 smb_krb5->reply = data_blob(NULL, 0);
316 case KRB5_KRBHST_UDP:
317 TEVENT_FD_WRITEABLE(smb_krb5->fde);
318 smb_krb5->request = send_blob;
320 case KRB5_KRBHST_TCP:
322 smb_krb5->packet = packet_init(smb_krb5);
323 if (smb_krb5->packet == NULL) {
324 talloc_free(smb_krb5);
327 packet_set_private(smb_krb5->packet, smb_krb5);
328 packet_set_socket(smb_krb5->packet, smb_krb5->sock);
329 packet_set_callback(smb_krb5->packet, smb_krb5_full_packet);
330 packet_set_full_request(smb_krb5->packet, packet_full_request_u32);
331 packet_set_error_handler(smb_krb5->packet, smb_krb5_error_handler);
332 packet_set_event_context(smb_krb5->packet, ev);
333 packet_set_fde(smb_krb5->packet, smb_krb5->fde);
335 smb_krb5->request = data_blob_talloc(smb_krb5, NULL, send_blob.length + 4);
336 RSIVAL(smb_krb5->request.data, 0, send_blob.length);
337 memcpy(smb_krb5->request.data+4, send_blob.data, send_blob.length);
338 packet_send(smb_krb5->packet, smb_krb5->request);
340 case KRB5_KRBHST_HTTP:
341 talloc_free(tmp_ctx);
344 while ((NT_STATUS_IS_OK(smb_krb5->status)) && !smb_krb5->reply.length) {
345 if (tevent_loop_once(ev) != 0) {
346 talloc_free(tmp_ctx);
350 /* After each and every event loop, reset the
351 * send_to_kdc pointers to what they were when
352 * we entered this loop. That way, if a
353 * nested event has invalidated them, we put
354 * it back before we return to the heimdal
356 ret = krb5_set_send_to_kdc_func(context,
357 smb_krb5_send_and_recv_func,
360 talloc_free(tmp_ctx);
364 if (NT_STATUS_EQUAL(smb_krb5->status, NT_STATUS_IO_TIMEOUT)) {
365 talloc_free(smb_krb5);
369 if (!NT_STATUS_IS_OK(smb_krb5->status)) {
370 struct tsocket_address *addr = socket_address_to_tsocket_address(smb_krb5, remote_addr);
371 const char *addr_string = NULL;
373 addr_string = tsocket_address_inet_addr_string(addr, smb_krb5);
377 DEBUG(2,("Error reading smb_krb5 reply packet: %s from %s\n", nt_errstr(smb_krb5->status),
379 talloc_free(smb_krb5);
383 ret = krb5_data_copy(recv_buf, smb_krb5->reply.data, smb_krb5->reply.length);
385 talloc_free(tmp_ctx);
388 talloc_free(smb_krb5);
392 talloc_free(tmp_ctx);
396 return KRB5_KDC_UNREACH;
400 smb_krb5_init_context_basic(TALLOC_CTX *tmp_ctx,
401 struct loadparm_context *lp_ctx,
402 krb5_context *_krb5_context)
406 const char *config_file, *realm;
407 krb5_context krb5_ctx;
409 initialize_krb5_error_table();
411 ret = krb5_init_context(&krb5_ctx);
413 DEBUG(1,("krb5_init_context failed (%s)\n",
414 error_message(ret)));
418 config_file = lpcfg_config_path(tmp_ctx, lp_ctx, "krb5.conf");
420 krb5_free_context(krb5_ctx);
424 /* Use our local krb5.conf file by default */
425 ret = krb5_prepend_config_files_default(config_file == NULL?"":config_file, &config_files);
427 DEBUG(1,("krb5_prepend_config_files_default failed (%s)\n",
428 smb_get_krb5_error_message(krb5_ctx, ret, tmp_ctx)));
429 krb5_free_context(krb5_ctx);
433 ret = krb5_set_config_files(krb5_ctx, config_files);
434 krb5_free_config_files(config_files);
436 DEBUG(1,("krb5_set_config_files failed (%s)\n",
437 smb_get_krb5_error_message(krb5_ctx, ret, tmp_ctx)));
438 krb5_free_context(krb5_ctx);
442 realm = lpcfg_realm(lp_ctx);
444 ret = krb5_set_default_realm(krb5_ctx, realm);
446 DEBUG(1,("krb5_set_default_realm failed (%s)\n",
447 smb_get_krb5_error_message(krb5_ctx, ret, tmp_ctx)));
448 krb5_free_context(krb5_ctx);
453 *_krb5_context = krb5_ctx;
457 krb5_error_code smb_krb5_init_context(void *parent_ctx,
458 struct tevent_context *ev,
459 struct loadparm_context *lp_ctx,
460 struct smb_krb5_context **smb_krb5_context)
465 initialize_krb5_error_table();
467 tmp_ctx = talloc_new(parent_ctx);
468 *smb_krb5_context = talloc_zero(tmp_ctx, struct smb_krb5_context);
470 if (!*smb_krb5_context || !tmp_ctx) {
471 talloc_free(tmp_ctx);
475 ret = smb_krb5_init_context_basic(tmp_ctx, lp_ctx,
476 &(*smb_krb5_context)->krb5_context);
478 DEBUG(1,("smb_krb5_context_init_basic failed (%s)\n",
479 error_message(ret)));
480 talloc_free(tmp_ctx);
484 /* TODO: Should we have a different name here? */
485 ret = krb5_initlog((*smb_krb5_context)->krb5_context, "Samba", &(*smb_krb5_context)->logf);
488 DEBUG(1,("krb5_initlog failed (%s)\n",
489 smb_get_krb5_error_message((*smb_krb5_context)->krb5_context, ret, tmp_ctx)));
490 krb5_free_context((*smb_krb5_context)->krb5_context);
491 talloc_free(tmp_ctx);
495 talloc_set_destructor(*smb_krb5_context, smb_krb5_context_destroy);
497 ret = krb5_addlog_func((*smb_krb5_context)->krb5_context, (*smb_krb5_context)->logf, 0 /* min */, -1 /* max */,
498 smb_krb5_debug_wrapper, smb_krb5_debug_close, NULL);
500 DEBUG(1,("krb5_addlog_func failed (%s)\n",
501 smb_get_krb5_error_message((*smb_krb5_context)->krb5_context, ret, tmp_ctx)));
502 talloc_free(tmp_ctx);
505 krb5_set_warn_dest((*smb_krb5_context)->krb5_context, (*smb_krb5_context)->logf);
507 /* Set use of our socket lib */
509 struct tevent_context *previous_ev;
510 ret = smb_krb5_context_set_event_ctx(*smb_krb5_context,
513 talloc_free(tmp_ctx);
518 talloc_steal(parent_ctx, *smb_krb5_context);
519 talloc_free(tmp_ctx);
521 /* Set options in kerberos */
523 krb5_set_dns_canonicalize_hostname((*smb_krb5_context)->krb5_context,
524 lpcfg_parm_bool(lp_ctx, NULL, "krb5", "set_dns_canonicalize", false));
529 krb5_error_code smb_krb5_context_set_event_ctx(struct smb_krb5_context *smb_krb5_context,
530 struct tevent_context *ev,
531 struct tevent_context **previous_ev)
538 *previous_ev = smb_krb5_context->current_ev;
540 smb_krb5_context->current_ev = talloc_reference(smb_krb5_context, ev);
541 if (!smb_krb5_context->current_ev) {
545 /* Set use of our socket lib */
546 ret = krb5_set_send_to_kdc_func(smb_krb5_context->krb5_context,
547 smb_krb5_send_and_recv_func,
550 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
551 DEBUG(1,("krb5_set_send_recv_func failed (%s)\n",
552 smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, tmp_ctx)));
553 talloc_free(tmp_ctx);
554 talloc_unlink(smb_krb5_context, smb_krb5_context->current_ev);
555 smb_krb5_context->current_ev = NULL;
561 krb5_error_code smb_krb5_context_remove_event_ctx(struct smb_krb5_context *smb_krb5_context,
562 struct tevent_context *previous_ev,
563 struct tevent_context *ev)
566 talloc_unlink(smb_krb5_context, ev);
567 /* If there was a mismatch with things happening on a stack, then don't wipe things */
568 smb_krb5_context->current_ev = previous_ev;
569 /* Set use of our socket lib */
570 ret = krb5_set_send_to_kdc_func(smb_krb5_context->krb5_context,
571 smb_krb5_send_and_recv_func,
574 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
575 DEBUG(1,("krb5_set_send_recv_func failed (%s)\n",
576 smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, tmp_ctx)));
577 talloc_free(tmp_ctx);