# Makefile for libvirt python library ## Copyright (C) 2005-2012 Red Hat, Inc. ## See COPYING.LIB for the License of this software SUBDIRS= . tests INCLUDES = \ $(PYTHON_INCLUDES) \ -I$(top_builddir)/gnulib/lib \ -I$(top_srcdir)/gnulib/lib \ -I$(top_builddir)/src \ -I$(top_srcdir)/src \ -I$(top_srcdir)/src/util \ -I$(top_builddir)/include \ -I$(top_srcdir)/include \ $(GETTEXT_CPPFLAGS) AM_CFLAGS = $(WARN_CFLAGS) DOCS_DIR = $(datadir)/doc/libvirt-python-$(VERSION) DOCS = ${srcdir}/TODO CLASSES_EXTRA = \ libvirt-override-virConnect.py \ libvirt-override-virDomain.py \ libvirt-override-virDomainSnapshot.py \ libvirt-override-virStream.py EXTRA_DIST = \ generator.py \ typewrappers.c \ typewrappers.h \ libvirt-override.c \ libvirt-override.py \ libvirt-override-api.xml \ libvirt-lxc-override.c \ libvirt-lxc-override-api.xml \ libvirt-qemu-override.c \ libvirt-qemu-override-api.xml \ $(CLASSES_EXTRA) \ $(DOCS) if WITH_PYTHON mylibs = \ $(top_builddir)/src/libvirt.la \ $(top_builddir)/gnulib/lib/libgnu.la myqemulibs = \ $(top_builddir)/src/libvirt-qemu.la \ $(top_builddir)/gnulib/lib/libgnu.la mylxclibs = \ $(top_builddir)/src/libvirt-lxc.la \ $(top_builddir)/gnulib/lib/libgnu.la all-local: libvirt.py libvirt_qemu.py libvirt_lxc.py pyexec_LTLIBRARIES = libvirtmod.la libvirtmod_qemu.la libvirtmod_lxc.la libvirtmod_la_SOURCES = libvirt-override.c typewrappers.c nodist_libvirtmod_la_SOURCES = libvirt.c libvirt.h # Python <= 2.4 header files contain a redundant decl, hence we # need extra flags here libvirtmod_la_CFLAGS = $(WARN_CFLAGS) $(WARN_PYTHON_CFLAGS) libvirtmod_la_LDFLAGS = -module -avoid-version -shared -L$(top_builddir)/src/.libs \ $(CYGWIN_EXTRA_LDFLAGS) libvirtmod_la_LIBADD = $(mylibs) \ $(CYGWIN_EXTRA_LIBADD) $(CYGWIN_EXTRA_PYTHON_LIBADD) libvirtmod_qemu_la_SOURCES = libvirt-qemu-override.c typewrappers.c nodist_libvirtmod_qemu_la_SOURCES = libvirt-qemu.c libvirt-qemu.h # Python <= 2.4 header files contain a redundant decl, hence we # need extra flags here libvirtmod_qemu_la_CFLAGS = $(WARN_PYTHON_CFLAGS) libvirtmod_qemu_la_LDFLAGS = -module -avoid-version -shared -L$(top_builddir)/src/.libs \ $(CYGWIN_EXTRA_LDFLAGS) libvirtmod_qemu_la_LIBADD = $(myqemulibs) \ $(CYGWIN_EXTRA_LIBADD) $(CYGWIN_EXTRA_PYTHON_LIBADD) libvirtmod_lxc_la_SOURCES = libvirt-lxc-override.c typewrappers.c nodist_libvirtmod_lxc_la_SOURCES = libvirt-lxc.c libvirt-lxc.h # Python <= 2.4 header files contain a redundant decl, hence we # need extra flags here libvirtmod_lxc_la_CFLAGS = $(WARN_PYTHON_CFLAGS) libvirtmod_lxc_la_LDFLAGS = -module -avoid-version -shared -L$(top_builddir)/src/.libs \ $(CYGWIN_EXTRA_LDFLAGS) libvirtmod_lxc_la_LIBADD = $(mylxclibs) \ $(CYGWIN_EXTRA_LIBADD) $(CYGWIN_EXTRA_PYTHON_LIBADD) GENERATE = generator.py API_DESC = $(top_srcdir)/docs/libvirt-api.xml $(srcdir)/libvirt-override-api.xml GENERATED= libvirt-export.c \ libvirt.c \ libvirt.h \ libvirt.py QEMU_API_DESC = $(top_srcdir)/docs/libvirt-qemu-api.xml $(srcdir)/libvirt-qemu-override-api.xml QEMU_GENERATED= libvirt-qemu-export.c \ libvirt-qemu.c \ libvirt-qemu.h \ libvirt_qemu.py LXC_API_DESC = $(top_srcdir)/docs/libvirt-lxc-api.xml $(srcdir)/libvirt-lxc-override-api.xml LXC_GENERATED= libvirt-lxc-export.c \ libvirt-lxc.c \ libvirt-lxc.h \ libvirt_lxc.py $(GENERATE).stamp: $(srcdir)/$(GENERATE) $(API_DESC) $(QEMU_API_DESC) $(LXC_API_DESC) $(AM_V_GEN)$(PYTHON) $(srcdir)/$(GENERATE) $(PYTHON) && \ touch $@ $(GENERATED) $(QEMU_GENERATED) $(LXC_GENERATED): $(GENERATE).stamp $(libvirtmod_la_OBJECTS): $(GENERATED) $(libvirtmod_qemu_la_OBJECTS): $(QEMU_GENERATED) $(libvirtmod_lxc_la_OBJECTS): $(LXC_GENERATED) EXTRA_DIST += sanitytest.py check-local: $(AM_V_GEN)../run $(PYTHON) $(srcdir)/sanitytest.py install-data-local: $(mkinstalldirs) $(DESTDIR)$(pyexecdir) $(INSTALL) -m 0644 libvirt.py $(DESTDIR)$(pyexecdir) $(INSTALL) -m 0644 libvirt_lxc.py $(DESTDIR)$(pyexecdir) $(INSTALL) -m 0644 libvirt_qemu.py $(DESTDIR)$(pyexecdir) $(mkinstalldirs) $(DESTDIR)$(DOCS_DIR) @(for doc in $(DOCS) ; \ do $(INSTALL) -m 0644 $$doc $(DESTDIR)$(DOCS_DIR) ; done) uninstall-local: rm -f $(DESTDIR)$(pyexecdir)/libvirt.py rm -f $(DESTDIR)$(pyexecdir)/libvirt_lxc.py rm -f $(DESTDIR)$(pyexecdir)/libvirt_qemu.py CLEANFILES= $(GENERATED) $(QEMU_GENERATED) $(LXC_GENERATED) $(GENERATE).stamp else all: endif dummy: tests test: all dummy -@(cd tests && $(MAKE) MAKEFLAGS+=--silent tests) b'>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
/*
SSSD
LDAP ID backend operation retry logic and connection cache
Authors:
Eugene Indenbom <eindenbom@gmail.com>
Copyright (C) 2008-2010 Red Hat
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "providers/ldap/sdap_id_op.h"
#include "providers/ldap/sdap_async.h"
#include "util/util.h"
/* LDAP async connection cache */
struct sdap_id_conn_cache {
struct be_ctx *be;
struct sdap_options *opts;
struct sdap_service *service;
/* list of all open connections */
struct sdap_id_conn_data *connections;
/* cached (current) connection */
struct sdap_id_conn_data *cached_connection;
};
/* LDAP async operation tracker:
* - keeps track of connection usage
* - keeps track of operation retries */
struct sdap_id_op {
/* ID backend context */
struct sdap_id_conn_cache *conn_cache;
/* double linked list pointers */
struct sdap_id_op *prev, *next;
/* current connection */
struct sdap_id_conn_data *conn_data;
/* number of reconnects for this operation */
int reconnect_retry_count;
/* connection request
* It is required as we need to know which requests to notify
* when shared connection request to sdap_handle completes.
* This member is cleared when sdap_id_op_connect_state
* associated with request is destroyed */
struct tevent_req *connect_req;
};
/* LDAP connection cache connection attempt/established connection data */
struct sdap_id_conn_data {
/* LDAP connection cache */
struct sdap_id_conn_cache *conn_cache;
/* double linked list pointers */
struct sdap_id_conn_data *prev, *next;
/* sdap handle */
struct sdap_handle *sh;
/* what rootDSE returns */
struct sysdb_attrs *rootDSE;
/* connection request */
struct tevent_req *connect_req;
/* timer for connection expiration */
struct tevent_timer *expire_timer;
/* number of running connection notifies */
int notify_lock;
/* list of operations using connect */
struct sdap_id_op *ops;
};
/* state of connect request */
struct sdap_id_op_connect_state {
struct sdap_id_op *op;
int dp_error;
int result;
};
static void sdap_id_conn_cache_be_offline_cb(void *pvt);
static void sdap_id_release_conn_data(struct sdap_id_conn_data *conn_data);
static int sdap_id_conn_data_destroy(struct sdap_id_conn_data *conn_data);
static bool sdap_is_connection_expired(struct sdap_id_conn_data *conn_data, int timeout);
static bool sdap_can_reuse_connection(struct sdap_id_conn_data *conn_data);
static void sdap_id_conn_data_expire_handler(struct tevent_context *ev,
struct tevent_timer *te,
struct timeval current_time,
void *pvt);
static int sdap_id_conn_data_set_expire_timer(struct sdap_id_conn_data *conn_data);
static void sdap_id_op_hook_conn_data(struct sdap_id_op *op, struct sdap_id_conn_data *conn_data);
static int sdap_id_op_destroy(void *pvt);
static bool sdap_id_op_can_reconnect(struct sdap_id_op *op);
static void sdap_id_op_connect_req_complete(struct sdap_id_op *op, int dp_error, int ret);
static int sdap_id_op_connect_state_destroy(void *pvt);
static int sdap_id_op_connect_step(struct tevent_req *req);
static void sdap_id_op_connect_done(struct tevent_req *subreq);
/* Create a connection cache */
int sdap_id_conn_cache_create(TALLOC_CTX *memctx,
struct be_ctx *be,
struct sdap_options *opts,
struct sdap_service *service,
struct sdap_id_conn_cache** conn_cache_out)
{
int ret;
struct sdap_id_conn_cache *conn_cache = talloc_zero(memctx, struct sdap_id_conn_cache);
if (!conn_cache) {
DEBUG(1, ("talloc_zero(struct sdap_id_conn_cache) failed.\n"));
ret = ENOMEM;
goto fail;
}
conn_cache->be = be;
conn_cache->opts = opts;
conn_cache->service = service;
ret = be_add_offline_cb(conn_cache, be,
sdap_id_conn_cache_be_offline_cb, conn_cache,
NULL);
if (ret != EOK) {
DEBUG(1, ("be_add_offline_cb failed.\n"));
goto fail;
}
*conn_cache_out = conn_cache;
return EOK;
fail:
talloc_zfree(conn_cache);
return ret;
}
/* Callback on BE going offline */
static void sdap_id_conn_cache_be_offline_cb(void *pvt)
{
struct sdap_id_conn_cache *conn_cache = talloc_get_type(pvt, struct sdap_id_conn_cache);
struct sdap_id_conn_data *cached_connection = conn_cache->cached_connection;
/* Release any cached connection on going offline */
if (cached_connection != NULL) {
conn_cache->cached_connection = NULL;
sdap_id_release_conn_data(cached_connection);
}
}
/* Release sdap_id_conn_data and destroy it if no longer needed */
static void sdap_id_release_conn_data(struct sdap_id_conn_data *conn_data)
{
struct sdap_id_conn_cache *conn_cache;
if (!conn_data || conn_data->ops || conn_data->notify_lock) {
/* connection is in use */
return;
}
conn_cache = conn_data->conn_cache;
if (conn_data == conn_cache->cached_connection) {
return;
}
DEBUG(9, ("releasing unused connection\n"));
DLIST_REMOVE(conn_cache->connections, conn_data);
talloc_zfree(conn_data);
}
/* Destructor for struct sdap_id_conn_data */
static int sdap_id_conn_data_destroy(struct sdap_id_conn_data *conn_data)
{
struct sdap_id_op *op;
/* we clean out list of ops to make sure that order of destruction does not matter */
while ((op = conn_data->ops) != NULL) {
op->conn_data = NULL;
DLIST_REMOVE(conn_data->ops, op);
}
return 0;
}
/* Check whether connection will expire after timeout seconds */
static bool sdap_is_connection_expired(struct sdap_id_conn_data *conn_data, int timeout)
{
time_t expire_time;
if (!conn_data || !conn_data->sh || !conn_data->sh->connected) {
return true;
}
expire_time = conn_data->sh->expire_time;
if ((expire_time != 0) && (expire_time < time( NULL ) + timeout) ) {
return true;
}
return false;
}
/* Check whether connection can be reused for next LDAP ID operation */
static bool sdap_can_reuse_connection(struct sdap_id_conn_data *conn_data)
{
int timeout;
if (!conn_data || !conn_data->sh || !conn_data->sh->connected) {
return false;
}
timeout = dp_opt_get_int(conn_data->conn_cache->opts->basic, SDAP_OPT_TIMEOUT);
return !sdap_is_connection_expired(conn_data, timeout);
}
/* Set expiration timer for connection if needed */
static int sdap_id_conn_data_set_expire_timer(struct sdap_id_conn_data *conn_data)
{
int timeout;
struct timeval tv;
memset(&tv, 0, sizeof(tv));
tv.tv_sec = conn_data->sh->expire_time;
if (tv.tv_sec <= 0) {
return EOK;
}
timeout = dp_opt_get_int(conn_data->conn_cache->opts->basic, SDAP_OPT_TIMEOUT);
if (timeout > 0) {
tv.tv_sec -= timeout;
}
if (tv.tv_sec <= time(NULL)) {
return EOK;
}
talloc_zfree(conn_data->expire_timer);
conn_data->expire_timer = tevent_add_timer(conn_data->conn_cache->be->ev,
conn_data,
tv,
sdap_id_conn_data_expire_handler,
conn_data);
if (!conn_data->expire_timer) {
return ENOMEM;
}
return EOK;
}
/* Handler for connection expiration timer */
static void sdap_id_conn_data_expire_handler(struct tevent_context *ev,
struct tevent_timer *te,
struct timeval current_time,
void *pvt)
{
struct sdap_id_conn_data *conn_data = talloc_get_type(pvt,
struct sdap_id_conn_data);
struct sdap_id_conn_cache *conn_cache = conn_data->conn_cache;
DEBUG(3, ("connection is about to expire, releasing it\n"));
if (conn_cache->cached_connection == conn_data) {
conn_cache->cached_connection = NULL;
sdap_id_release_conn_data(conn_data);
}
}
/* Create an operation object */
struct sdap_id_op *sdap_id_op_create(TALLOC_CTX *memctx, struct sdap_id_conn_cache *conn_cache)
{
struct sdap_id_op *op = talloc_zero(memctx, struct sdap_id_op);
if (!op) {
return NULL;
}
op->conn_cache = conn_cache;
talloc_set_destructor((void*)op, sdap_id_op_destroy);
return op;
}
/* Attach/detach connection to sdap_id_op */
static void sdap_id_op_hook_conn_data(struct sdap_id_op *op, struct sdap_id_conn_data *conn_data)
{
if (!op) {
DEBUG(0, ("NULL op passed!!!\n"));
return;
}
struct sdap_id_conn_data *current = op->conn_data;
if (conn_data == current) {
return;
}
if (current) {
DLIST_REMOVE(current->ops, op);
}
op->conn_data = conn_data;
if (conn_data) {
DLIST_ADD_END(conn_data->ops, op, struct sdap_id_op*);
}
if (current) {
sdap_id_release_conn_data(current);
}
}
/* Destructor for sdap_id_op */
static int sdap_id_op_destroy(void *pvt)
{
struct sdap_id_op *op = talloc_get_type(pvt, struct sdap_id_op);
if (op->conn_data) {
DEBUG(9, ("releasing operation connection\n"));
sdap_id_op_hook_conn_data(op, NULL);
}
return 0;
}
/* Check whether retry with reconnect can be performed for the operation */
static bool sdap_id_op_can_reconnect(struct sdap_id_op *op)
{
/* we allow 2 retries for failover server configured:
* - one for connection broken during request execution
* - one for the following (probably failed) reconnect attempt */
int max_retries = 2 * be_fo_get_server_count(op->conn_cache->be, op->conn_cache->service->name) - 1;
if (max_retries < 1) {
max_retries = 1;
}
return op->reconnect_retry_count < max_retries;
}
/* Destructor for operation connection request */
static int sdap_id_op_connect_state_destroy(void *pvt)
{
struct sdap_id_op_connect_state *state = talloc_get_type(pvt,
struct sdap_id_op_connect_state);
if (state->op != NULL) {
/* clear destroyed connection request */
state->op->connect_req = NULL;
}
return 0;
}
/* Begin to connect to LDAP server */
struct tevent_req *sdap_id_op_connect_send(struct sdap_id_op *op,
TALLOC_CTX *memctx,
int *ret_out)
{
struct tevent_req *req = NULL;
struct sdap_id_op_connect_state *state;
int ret = EOK;
if (!memctx) {
DEBUG(1, ("Bug: no memory context passed.\n"));
ret = EINVAL;
goto done;
}
if (op->connect_req) {
/* Connection already in progress, invalid operation */
DEBUG(1, ("Bug: connection request is already running or completed and leaked.\n"));
ret = EINVAL;
goto done;
}
req = tevent_req_create(memctx, &state, struct sdap_id_op_connect_state);
if (!req) {
ret = ENOMEM;
goto done;
}
talloc_set_destructor((void*)state, sdap_id_op_connect_state_destroy);
state->op = op;
op->connect_req = req;
if (op->conn_data) {
/* If the operation is already connected,
* reuse existing connection regardless of its status */
DEBUG(9, ("reusing operation connection\n"));
ret = EOK;
goto done;
}
ret = sdap_id_op_connect_step(req);
if (ret != EOK) {
goto done;
}
done:
if (ret != EOK) {
talloc_zfree(req);
} else if (op->conn_data && !op->conn_data->connect_req) {
/* Connection is already established */
tevent_req_done(req);
tevent_req_post(req, op->conn_cache->be->ev);
}
if (ret_out) {
*ret_out = ret;
}
return req;
}
/* Begin a connection retry to LDAP server */
static int sdap_id_op_connect_step(struct tevent_req *req)
{
struct sdap_id_op_connect_state *state = tevent_req_data(req, struct sdap_id_op_connect_state);
struct sdap_id_op *op = state->op;
struct sdap_id_conn_cache *conn_cache = op->conn_cache;
int ret = EOK;
struct sdap_id_conn_data *conn_data;
struct tevent_req *subreq = NULL;
/* Try to reuse context cached connection */
conn_data = conn_cache->cached_connection;
if (conn_data) {
if (conn_data->connect_req) {
DEBUG(9, ("waiting for connection to complete\n"));
sdap_id_op_hook_conn_data(op, conn_data);
goto done;
}
if (sdap_can_reuse_connection(conn_data)) {
DEBUG(9, ("reusing cached connection\n"));
sdap_id_op_hook_conn_data(op, conn_data);
goto done;
}
DEBUG(9, ("releasing expired cached connection\n"));
conn_cache->cached_connection = NULL;
sdap_id_release_conn_data(conn_data);
}
DEBUG(9, ("beginning to connect\n"));
conn_data = talloc_zero(conn_cache, struct sdap_id_conn_data);
if (!conn_data) {
ret = ENOMEM;
goto done;
}
talloc_set_destructor(conn_data, sdap_id_conn_data_destroy);
conn_data->conn_cache = conn_cache;
subreq = sdap_cli_connect_send(conn_data, conn_cache->be->ev,
conn_cache->opts, conn_cache->be,
conn_cache->service, false);
if (!subreq) {
ret = ENOMEM;
goto done;
}
tevent_req_set_callback(subreq, sdap_id_op_connect_done, conn_data);
conn_data->connect_req = subreq;
DLIST_ADD(conn_cache->connections, conn_data);
conn_cache->cached_connection = conn_data;
sdap_id_op_hook_conn_data(op, conn_data);
done:
if (ret != EOK && conn_data) {
sdap_id_release_conn_data(conn_data);
}
if (ret != EOK) {
talloc_zfree(subreq);
}
return ret;
}
/* Subrequest callback for connection completion */
static void sdap_id_op_connect_done(struct tevent_req *subreq)
{
struct sdap_id_conn_data *conn_data = tevent_req_callback_data(subreq,
struct sdap_id_conn_data);
struct sdap_id_conn_cache *conn_cache = conn_data->conn_cache;