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 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
|
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998-1999
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* Copyright (c) 1995 Regents of the University of Michigan.
* All rights reserved.
*/
/*
* open.c
*/
#if 0
#ifndef lint
static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
#endif
#endif
#include "ldap-int.h"
#ifdef LDAP_SASLIO_HOOKS
/* Valid for any ANSI C compiler */
#include <limits.h>
extern sasl_callback_t client_callbacks[];
#endif
#define VI_PRODUCTVERSION 3
#ifndef INADDR_LOOPBACK
#define INADDR_LOOPBACK ((unsigned long) 0x7f000001)
#endif
#ifdef LDAP_DEBUG
int ldap_debug = 0;
#endif
#ifdef _WINDOWS
#define USE_WINDOWS_TLS /* thread local storage */
#endif
/*
* global defaults for callbacks are stored here. callers of the API set
* these by passing a NULL "ld" to ldap_set_option(). Everything in
* nsldapi_ld_defaults can be overridden on a per-ld basis as well (the
* memory allocation functions are global to all ld's).
*/
struct ldap nsldapi_ld_defaults;
struct ldap_memalloc_fns nsldapi_memalloc_fns = { 0, 0, 0, 0 };
int nsldapi_initialized = 0;
#ifdef USE_PTHREADS
#include <pthread.h>
#ifdef VMS
/*
** pthread_self() is not a routine on OpenVMS; it's inline assembler code.
** Since we need a real address which we can stuff away into a table, we need
** to make sure that pthread_self maps to the real pthread_self routine (yes,
** we do have one fortunately).
*/
#undef pthread_self
#define pthread_self PTHREAD_SELF
extern pthread_t pthread_self (void);
#endif
static pthread_key_t nsldapi_key;
static pthread_mutex_t nsldapi_init_mutex = PTHREAD_MUTEX_INITIALIZER;
struct nsldapi_ldap_error {
int le_errno;
char *le_matched;
char *le_errmsg;
};
#elif defined (USE_WINDOWS_TLS)
static DWORD dwTlsIndex;
struct nsldapi_ldap_error {
int le_errno;
char *le_matched;
char *le_errmsg;
};
#elif defined (_WINDOWS) /* use static tls */
__declspec ( thread ) int nsldapi_gldaperrno;
__declspec ( thread ) char *nsldapi_gmatched = NULL;
__declspec ( thread ) char *nsldapi_gldaperror = NULL;
#endif /* USE_WINDOWS_TLS */
#ifdef _WINDOWS
#define LDAP_MUTEX_T HANDLE
static LDAP_MUTEX_T nsldapi_init_mutex;
int
pthread_mutex_init( LDAP_MUTEX_T *mp, void *attr)
{
if ( (*mp = CreateMutex(NULL, FALSE, NULL)) == NULL )
return( 1 );
else
return( 0 );
}
static void *
pthread_mutex_alloc( void )
{
LDAP_MUTEX_T *mutexp;
if ( (mutexp = malloc( sizeof(LDAP_MUTEX_T) )) != NULL ) {
pthread_mutex_init( mutexp, NULL );
}
return( mutexp );
}
int
pthread_mutex_destroy( LDAP_MUTEX_T *mp )
{
if ( !(CloseHandle(*mp)) )
return( 1 );
else
return( 0 );
}
static void
pthread_mutex_free( void *mutexp )
{
pthread_mutex_destroy( (LDAP_MUTEX_T *) mutexp );
free( mutexp );
}
int
pthread_mutex_lock( LDAP_MUTEX_T *mp )
{
if ( (WaitForSingleObject(*mp, INFINITE) != WAIT_OBJECT_0) )
return( 1 );
else
return( 0 );
}
int
pthread_mutex_unlock( LDAP_MUTEX_T *mp )
{
if ( !(ReleaseMutex(*mp)) )
return( 1 );
else
return( 0 );
}
static int
get_errno( void )
{
return errno;
}
static void
set_errno( int Errno )
{
errno = Errno;
}
#ifdef USE_WINDOWS_TLS
static void
set_ld_error( int err, char *matched, char *errmsg, void *dummy )
{
struct nsldapi_ldap_error *le;
void *tsd;
le = TlsGetValue( dwTlsIndex );
if (le == NULL) {
tsd = (void *)calloc(1, sizeof(struct nsldapi_ldap_error));
TlsSetValue( dwTlsIndex, tsd );
}
le = TlsGetValue ( dwTlsIndex );
if (le == NULL)
return;
le->le_errno = err;
if ( le->le_matched != NULL ) {
ldap_memfree( le->le_matched );
}
le->le_matched = matched;
if ( le->le_errmsg != NULL ) {
ldap_memfree( le->le_errmsg );
}
le->le_errmsg = errmsg;
}
static int
get_ld_error ( char **matched, char **errmsg, void *dummy )
{
struct nsldapi_ldap_error *le;
le = TlsGetValue( dwTlsIndex );
if ( matched != NULL ) {
*matched = le->le_matched;
}
if ( errmsg != NULL ) {
*errmsg = le->le_errmsg;
}
return( le->le_errno );
}
#else
static int
get_ld_error( char **LDMatched, char **LDError, void * Args )
{
if ( LDMatched != NULL )
{
*LDMatched = nsldapi_gmatched;
}
if ( LDError != NULL )
{
*LDError = nsldapi_gldaperror;
}
return nsldapi_gldaperrno;
}
static void
set_ld_error( int LDErrno, char * LDMatched, char * LDError,
void * Args )
{
/* Clean up any previous string storage. */
if ( nsldapi_gmatched != NULL )
{
ldap_memfree( nsldapi_gmatched );
}
if ( nsldapi_gldaperror != NULL )
{
ldap_memfree( nsldapi_gldaperror );
}
nsldapi_gldaperrno = LDErrno;
nsldapi_gmatched = LDMatched;
nsldapi_gldaperror = LDError;
}
#endif /* USE_WINDOWS_TLS */
#endif /* ! _WINDOWS */
#ifdef USE_PTHREADS
static void *
pthread_mutex_alloc( void )
{
pthread_mutex_t *mutexp;
if ( (mutexp = malloc( sizeof(pthread_mutex_t) )) != NULL ) {
pthread_mutex_init( mutexp, NULL );
}
return( mutexp );
}
static void
pthread_mutex_free( void *mutexp )
{
pthread_mutex_destroy( (pthread_mutex_t *) mutexp );
free( mutexp );
}
static void
set_ld_error( int err, char *matched, char *errmsg, void *dummy )
{
struct nsldapi_ldap_error *le;
void *tsd;
le = pthread_getspecific( nsldapi_key );
if (le == NULL) {
tsd = (void *)calloc(1, sizeof(struct nsldapi_ldap_error));
pthread_setspecific( nsldapi_key, tsd );
}
le = pthread_getspecific( nsldapi_key );
if (le == NULL)
return;
le->le_errno = err;
if ( le->le_matched != NULL ) {
ldap_memfree( le->le_matched );
}
le->le_matched = matched;
if ( le->le_errmsg != NULL ) {
ldap_memfree( le->le_errmsg );
}
le->le_errmsg = errmsg;
}
static int
get_ld_error( char **matched, char **errmsg, void *dummy )
{
struct nsldapi_ldap_error *le;
le = pthread_getspecific( nsldapi_key );
if (le == NULL)
return( LDAP_SUCCESS );
if ( matched != NULL ) {
*matched = le->le_matched;
}
if ( errmsg != NULL ) {
*errmsg = le->le_errmsg;
}
return( le->le_errno );
}
static void
set_errno( int err )
{
errno = err;
}
static int
get_errno( void )
{
return( errno );
}
#endif /* use_pthreads */
#if defined(USE_PTHREADS) || defined(_WINDOWS)
static struct ldap_thread_fns
nsldapi_default_thread_fns = {
(void *(*)(void))pthread_mutex_alloc,
(void (*)(void *))pthread_mutex_free,
(int (*)(void *))pthread_mutex_lock,
(int (*)(void *))pthread_mutex_unlock,
(int (*)(void))get_errno,
(void (*)(int))set_errno,
(int (*)(char **, char **, void *))get_ld_error,
(void (*)(int, char *, char *, void *))set_ld_error,
0 };
static struct ldap_extra_thread_fns
nsldapi_default_extra_thread_fns = {
0, 0, 0, 0, 0,
#ifdef _WINDOWS
0
#else
(void *(*)(void))pthread_self
#endif /* _WINDOWS */
};
#endif /* use_pthreads || _windows */
void
nsldapi_initialize_defaults( void )
{
#ifdef _WINDOWS
pthread_mutex_init( &nsldapi_init_mutex, NULL );
#endif /* _WINDOWS */
#if defined(USE_PTHREADS) || defined(_WINDOWS)
pthread_mutex_lock( &nsldapi_init_mutex );
if ( nsldapi_initialized ) {
pthread_mutex_unlock( &nsldapi_init_mutex );
return;
}
#else
if ( nsldapi_initialized ) {
return;
}
#endif /* use_pthreads || _windows */
#ifdef USE_PTHREADS
if ( pthread_key_create(&nsldapi_key, free ) != 0) {
perror("pthread_key_create");
}
#elif defined(USE_WINDOWS_TLS)
dwTlsIndex = TlsAlloc();
#endif /* USE_WINDOWS_TLS */
memset( &nsldapi_memalloc_fns, 0, sizeof( nsldapi_memalloc_fns ));
memset( &nsldapi_ld_defaults, 0, sizeof( nsldapi_ld_defaults ));
nsldapi_ld_defaults.ld_options = LDAP_BITOPT_REFERRALS;
nsldapi_ld_defaults.ld_version = LDAP_VERSION3;
nsldapi_ld_defaults.ld_lberoptions = LBER_OPT_USE_DER;
nsldapi_ld_defaults.ld_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
#ifdef LDAP_SASLIO_HOOKS
/* SASL default option settings */
nsldapi_ld_defaults.ld_def_sasl_mech = NULL;
nsldapi_ld_defaults.ld_def_sasl_realm = NULL;
nsldapi_ld_defaults.ld_def_sasl_authcid = NULL;
nsldapi_ld_defaults.ld_def_sasl_authzid = NULL;
/* SASL Security properties */
nsldapi_ld_defaults.ld_sasl_secprops.max_ssf = UINT_MAX;
nsldapi_ld_defaults.ld_sasl_secprops.maxbufsize = SASL_MAX_BUFF_SIZE;
nsldapi_ld_defaults.ld_sasl_secprops.security_flags =
SASL_SEC_NOPLAINTEXT | SASL_SEC_NOANONYMOUS;
/* SASL mutex function callbacks */
sasl_set_mutex(
(sasl_mutex_alloc_t *)nsldapi_default_thread_fns.ltf_mutex_alloc,
(sasl_mutex_lock_t *)nsldapi_default_thread_fns.ltf_mutex_lock,
(sasl_mutex_unlock_t *)nsldapi_default_thread_fns.ltf_mutex_unlock,
(sasl_mutex_free_t *)nsldapi_default_thread_fns.ltf_mutex_free );
/* SASL memory allocation function callbacks */
sasl_set_alloc(
(sasl_malloc_t *)ldap_x_malloc,
(sasl_calloc_t *)ldap_x_calloc,
(sasl_realloc_t *)ldap_x_realloc,
(sasl_free_t *)ldap_x_free );
/* SASL library initialization */
if ( sasl_client_init( client_callbacks ) != SASL_OK ) {
nsldapi_initialized = 0;
pthread_mutex_unlock( &nsldapi_init_mutex );
return;
}
#endif
#if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
nsldapi_ld_defaults.ld_lberoptions |= LBER_OPT_TRANSLATE_STRINGS;
#if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
ldap_set_string_translators( &nsldapi_ld_defaults, ldap_8859_to_t61,
ldap_t61_to_8859 );
#endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
#endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
/* set default connect timeout (in milliseconds) */
/* this was picked as it is the standard tcp timeout as well */
nsldapi_ld_defaults.ld_connect_timeout = LDAP_X_IO_TIMEOUT_NO_TIMEOUT;
#if defined(USE_PTHREADS) || defined(_WINDOWS)
/* load up default platform specific locking routines */
if (ldap_set_option( &nsldapi_ld_defaults, LDAP_OPT_THREAD_FN_PTRS,
(void *)&nsldapi_default_thread_fns) != LDAP_SUCCESS) {
nsldapi_initialized = 0;
pthread_mutex_unlock( &nsldapi_init_mutex );
return;
}
#ifndef _WINDOWS
/* load up default threadid function */
if (ldap_set_option( &nsldapi_ld_defaults, LDAP_OPT_EXTRA_THREAD_FN_PTRS,
(void *)&nsldapi_default_extra_thread_fns) != LDAP_SUCCESS) {
nsldapi_initialized = 0;
pthread_mutex_unlock( &nsldapi_init_mutex );
return;
}
#endif /* _WINDOWS */
nsldapi_initialized = 1;
pthread_mutex_unlock( &nsldapi_init_mutex );
#else
nsldapi_initialized = 1;
#endif /* use_pthreads || _windows */
}
/*
* ldap_version - report version levels for important properties
* This function is deprecated. Use ldap_get_option( ..., LDAP_OPT_API_INFO,
* ... ) instead.
*
* Example:
* LDAPVersion ver;
* ldap_version( &ver );
* if ( (ver.sdk_version < 100) || (ver.SSL_version < 300) )
* fprintf( stderr, "LDAP SDK level insufficient\n" );
*
* or:
* if ( ldap_version(NULL) < 100 )
* fprintf( stderr, "LDAP SDK level insufficient\n" );
*
*/
int
LDAP_CALL
ldap_version( LDAPVersion *ver )
{
if ( NULL != ver )
{
memset( ver, 0, sizeof(*ver) );
ver->sdk_version = (int)(VI_PRODUCTVERSION * 100);
ver->protocol_version = LDAP_VERSION_MAX * 100;
ver->SSL_version = SSL_VERSION * 100;
/*
* set security to none by default
*/
ver->security_level = LDAP_SECURITY_NONE;
#if defined(LINK_SSL)
#if defined(NS_DOMESTIC)
ver->security_level = 128;
#elif defined(NSS_EXPORT)
ver->security_level = 40;
#endif
#endif
}
return (int)(VI_PRODUCTVERSION * 100);
}
/*
* ldap_open - initialize and connect to an ldap server. A magic cookie to
* be used for future communication is returned on success, NULL on failure.
* "host" may be a space-separated list of hosts or IP addresses
*
* Example:
* LDAP *ld;
* ld = ldap_open( hostname, port );
*/
LDAP *
LDAP_CALL
ldap_open( const char *host, int port )
{
LDAP *ld;
LDAPDebug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
if (( ld = ldap_init( host, port )) == NULL ) {
return( NULL );
}
LDAP_MUTEX_LOCK( ld, LDAP_CONN_LOCK );
if ( nsldapi_open_ldap_defconn( ld ) < 0 ) {
LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
ldap_ld_free( ld, NULL, NULL, 0 );
return( NULL );
}
LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
LDAPDebug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
return( ld );
}
/*
* ldap_init - initialize the LDAP library. A magic cookie to be used for
* future communication is returned on success, NULL on failure.
* "defhost" may be a space-separated list of hosts or IP addresses
*
* NOTE: If you want to use IPv6, you must use prldap creating a LDAP handle
* with prldap_init instead of ldap_init. Or install the NSPR functions
* by calling prldap_install_routines. (See the nspr samples in examples)
*
* Example:
* LDAP *ld;
* ld = ldap_init( default_hostname, default_port );
*/
LDAP *
LDAP_CALL
ldap_init( const char *defhost, int defport )
{
LDAP *ld;
if ( !nsldapi_initialized ) {
nsldapi_initialize_defaults();
}
if ( defport < 0 || defport > LDAP_PORT_MAX ) {
LDAPDebug( LDAP_DEBUG_ANY,
"ldap_init: port %d is invalid (port numbers must range from 1 to %d)\n",
defport, LDAP_PORT_MAX, 0 );
#if !defined( macintosh ) && !defined( DOS ) && !defined( BEOS )
errno = EINVAL;
#endif
return( NULL );
}
LDAPDebug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
if ( (ld = (LDAP*)NSLDAPI_MALLOC( sizeof(struct ldap) )) == NULL ) {
return( NULL );
}
/* copy defaults */
SAFEMEMCPY( ld, &nsldapi_ld_defaults, sizeof( struct ldap ));
if ( nsldapi_ld_defaults.ld_io_fns_ptr != NULL ) {
if (( ld->ld_io_fns_ptr = (struct ldap_io_fns *)NSLDAPI_MALLOC(
sizeof( struct ldap_io_fns ))) == NULL ) {
NSLDAPI_FREE( (char *)ld );
return( NULL );
}
/* struct copy */
*(ld->ld_io_fns_ptr) = *(nsldapi_ld_defaults.ld_io_fns_ptr);
}
/* call the new handle I/O callback if one is defined */
if ( ld->ld_extnewhandle_fn != NULL ) {
/*
* We always pass the session extended I/O argument to
* the new handle callback.
*/
if ( ld->ld_extnewhandle_fn( ld, ld->ld_ext_session_arg )
!= LDAP_SUCCESS ) {
NSLDAPI_FREE( (char*)ld );
return( NULL );
}
}
/* allocate session-specific resources */
if (( ld->ld_sbp = ber_sockbuf_alloc()) == NULL ||
( defhost != NULL &&
( ld->ld_defhost = nsldapi_strdup( defhost )) == NULL ) ||
((ld->ld_mutex = (void **) NSLDAPI_CALLOC( LDAP_MAX_LOCK, sizeof(void *))) == NULL )) {
if ( ld->ld_sbp != NULL ) {
ber_sockbuf_free( ld->ld_sbp );
}
if( ld->ld_mutex != NULL ) {
NSLDAPI_FREE( ld->ld_mutex );
}
NSLDAPI_FREE( (char*)ld );
return( NULL );
}
/* install Sockbuf I/O functions if set in LDAP * */
if ( ld->ld_extread_fn != NULL || ld->ld_extwrite_fn != NULL ) {
struct lber_x_ext_io_fns lberiofns;
memset( &lberiofns, 0, sizeof( lberiofns ));
lberiofns.lbextiofn_size = LBER_X_EXTIO_FNS_SIZE;
lberiofns.lbextiofn_read = ld->ld_extread_fn;
lberiofns.lbextiofn_write = ld->ld_extwrite_fn;
lberiofns.lbextiofn_writev = ld->ld_extwritev_fn;
lberiofns.lbextiofn_socket_arg = NULL;
ber_sockbuf_set_option( ld->ld_sbp, LBER_SOCKBUF_OPT_EXT_IO_FNS,
(void *)&lberiofns );
}
/* allocate mutexes */
nsldapi_mutex_alloc_all( ld );
/* set default port */
ld->ld_defport = ( defport == 0 ) ? LDAP_PORT : defport;
return( ld );
}
void
nsldapi_mutex_alloc_all( LDAP *ld )
{
int i;
if ( ld != &nsldapi_ld_defaults && ld->ld_mutex != NULL ) {
for ( i = 0; i<LDAP_MAX_LOCK; i++ ) {
ld->ld_mutex[i] = LDAP_MUTEX_ALLOC( ld );
ld->ld_mutex_threadid[i] = (void *) -1;
ld->ld_mutex_refcnt[i] = 0;
}
}
}
void
nsldapi_mutex_free_all( LDAP *ld )
{
int i;
if ( ld != &nsldapi_ld_defaults && ld->ld_mutex != NULL ) {
for ( i = 0; i<LDAP_MAX_LOCK; i++ ) {
LDAP_MUTEX_FREE( ld, ld->ld_mutex[i] );
}
}
}
/* returns 0 if connection opened and -1 if an error occurs */
int
nsldapi_open_ldap_defconn( LDAP *ld )
{
LDAPServer *srv;
if (( srv = (LDAPServer *)NSLDAPI_CALLOC( 1, sizeof( LDAPServer ))) ==
NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
nsldapi_strdup( ld->ld_defhost )) == NULL )) {
LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL );
return( -1 );
}
srv->lsrv_port = ld->ld_defport;
if (( ld->ld_options & LDAP_BITOPT_SSL ) != 0 ) {
srv->lsrv_options |= LDAP_SRV_OPT_SECURE;
}
if (( ld->ld_defconn = nsldapi_new_connection( ld, &srv, 1, 1, 0 ))
== NULL ) {
if ( ld->ld_defhost != NULL ) {
NSLDAPI_FREE( srv->lsrv_host );
}
NSLDAPI_FREE( (char *)srv );
return( -1 );
}
++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
return( 0 );
}
struct ldap_x_hostlist_status {
char *lhs_hostlist;
char *lhs_nexthost;
int lhs_defport;
};
/*
* Return the first host and port in hostlist (setting *hostp and *portp).
* Return value is an LDAP API error code (LDAP_SUCCESS if all goes well).
* Note that a NULL or zero-length hostlist causes the host "127.0.0.1" to
* be returned.
*/
int LDAP_CALL
ldap_x_hostlist_first( const char *hostlist, int defport, char **hostp,
int *portp, struct ldap_x_hostlist_status **statusp )
{
if ( NULL == hostp || NULL == portp || NULL == statusp ) {
return( LDAP_PARAM_ERROR );
}
if ( NULL == hostlist || *hostlist == '\0' ) {
*hostp = nsldapi_strdup( "127.0.0.1" );
if ( NULL == *hostp ) {
return( LDAP_NO_MEMORY );
}
*portp = defport;
*statusp = NULL;
return( LDAP_SUCCESS );
}
*statusp = NSLDAPI_CALLOC( 1, sizeof( struct ldap_x_hostlist_status ));
if ( NULL == *statusp ) {
return( LDAP_NO_MEMORY );
}
(*statusp)->lhs_hostlist = nsldapi_strdup( hostlist );
if ( NULL == (*statusp)->lhs_hostlist ) {
return( LDAP_NO_MEMORY );
}
(*statusp)->lhs_nexthost = (*statusp)->lhs_hostlist;
(*statusp)->lhs_defport = defport;
return( ldap_x_hostlist_next( hostp, portp, *statusp ));
}
/*
* Return the next host and port in hostlist (setting *hostp and *portp).
* Return value is an LDAP API error code (LDAP_SUCCESS if all goes well).
* If no more hosts are available, LDAP_SUCCESS is returned but *hostp is set
* to NULL.
*/
int LDAP_CALL
ldap_x_hostlist_next( char **hostp, int *portp,
struct ldap_x_hostlist_status *status )
{
char *q;
int squarebrackets = 0;
if ( NULL == hostp || NULL == portp ) {
return( LDAP_PARAM_ERROR );
}
if ( NULL == status || NULL == status->lhs_nexthost ) {
*hostp = NULL;
return( LDAP_SUCCESS );
}
/*
* skip past leading '[' if present (IPv6 addresses may be surrounded
* with square brackets, e.g., [fe80::a00:20ff:fee5:c0b4]:389
*/
if ( status->lhs_nexthost[0] == '[' ) {
++status->lhs_nexthost;
squarebrackets = 1;
}
/* copy host into *hostp */
if ( NULL != ( q = strchr( status->lhs_nexthost, ' ' ))) {
size_t len = q - status->lhs_nexthost;
*hostp = NSLDAPI_MALLOC( len + 1 );
if ( NULL == *hostp ) {
return( LDAP_NO_MEMORY );
}
strncpy( *hostp, status->lhs_nexthost, len );
(*hostp)[len] = '\0';
status->lhs_nexthost += ( len + 1 );
} else { /* last host */
*hostp = nsldapi_strdup( status->lhs_nexthost );
if ( NULL == *hostp ) {
return( LDAP_NO_MEMORY );
}
status->lhs_nexthost = NULL;
}
/*
* Look for closing ']' and skip past it before looking for port.
*/
if ( squarebrackets && NULL != ( q = strchr( *hostp, ']' ))) {
*q++ = '\0';
} else {
q = *hostp;
}
/* determine and set port */
if ( NULL != ( q = strchr( q, ':' ))) {
*q++ = '\0';
*portp = atoi( q );
} else {
*portp = status->lhs_defport;
}
return( LDAP_SUCCESS );
}
void LDAP_CALL
ldap_x_hostlist_statusfree( struct ldap_x_hostlist_status *status )
{
if ( NULL != status ) {
if ( NULL != status->lhs_hostlist ) {
NSLDAPI_FREE( status->lhs_hostlist );
}
NSLDAPI_FREE( status );
}
}
/*
* memory allocation functions. we include these in open.c since every
* LDAP application is likely to pull the rest of the code in this file
* in anyways.
*/
void *
ldap_x_malloc( size_t size )
{
return( nsldapi_memalloc_fns.ldapmem_malloc == NULL ?
malloc( size ) :
nsldapi_memalloc_fns.ldapmem_malloc( size ));
}
void *
ldap_x_calloc( size_t nelem, size_t elsize )
{
return( nsldapi_memalloc_fns.ldapmem_calloc == NULL ?
calloc( nelem, elsize ) :
nsldapi_memalloc_fns.ldapmem_calloc( nelem, elsize ));
}
void *
ldap_x_realloc( void *ptr, size_t size )
{
return( nsldapi_memalloc_fns.ldapmem_realloc == NULL ?
realloc( ptr, size ) :
nsldapi_memalloc_fns.ldapmem_realloc( ptr, size ));
}
void
ldap_x_free( void *ptr )
{
if ( nsldapi_memalloc_fns.ldapmem_free == NULL ) {
free( ptr );
} else {
nsldapi_memalloc_fns.ldapmem_free( ptr );
}
}
/* if s is NULL, returns NULL */
char *
nsldapi_strdup( const char *s )
{
char *p;
if ( s == NULL ||
(p = (char *)NSLDAPI_MALLOC( strlen( s ) + 1 )) == NULL )
return( NULL );
strcpy( p, s );
return( p );
}
|