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
|
/* $OpenLDAP: pkg/ldap/servers/slurpd/ldap_op.c,v 1.13.2.12 2002/01/12 20:23:03 kurt Exp $ */
/*
* Copyright (c) 1996 Regents of the University of Michigan.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that this notice is preserved and that due credit is given
* to the University of Michigan at Ann Arbor. The name of the University
* may not be used to endorse or promote products derived from this
* software without specific prior written permission. This software
* is provided ``as is'' without express or implied warranty.
*/
/*
* ldap_op.c - routines to perform LDAP operations
*/
#include "portable.h"
#include <stdio.h>
#include <ac/stdlib.h>
#include <ac/errno.h>
#include <ac/string.h>
#include <ac/ctype.h>
#include <ac/time.h>
#include <ac/unistd.h>
#include <ldap.h>
#include "lutil_ldap.h"
#include "slurp.h"
/* Forward references */
static struct berval **make_singlevalued_berval LDAP_P(( char *, int ));
static int op_ldap_add LDAP_P(( Ri *, Re *, char ** ));
static int op_ldap_modify LDAP_P(( Ri *, Re *, char ** ));
static int op_ldap_delete LDAP_P(( Ri *, Re *, char ** ));
static int op_ldap_modrdn LDAP_P(( Ri *, Re *, char ** ));
static LDAPMod *alloc_ldapmod LDAP_P(( void ));
static void free_ldapmod LDAP_P(( LDAPMod * ));
static void free_ldmarr LDAP_P(( LDAPMod ** ));
static int getmodtype LDAP_P(( char * ));
static void dump_ldm_array LDAP_P(( LDAPMod ** ));
static int do_bind LDAP_P(( Ri *, int * ));
static int do_unbind LDAP_P(( Ri * ));
/*
* Determine the type of ldap operation being performed and call the
* appropriate routine.
* - If successful, returns DO_LDAP_OK
* - If a retryable error occurs, ERR_DO_LDAP_RETRYABLE is returned.
* The caller should wait a while and retry the operation.
* - If a fatal error occurs, ERR_DO_LDAP_FATAL is returned. The caller
* should reject the operation and continue with the next replication
* entry.
*/
int
do_ldap(
Ri *ri,
Re *re,
char **errmsg
)
{
int retry = 2;
*errmsg = NULL;
do {
int lderr;
if ( ri->ri_ldp == NULL ) {
lderr = do_bind( ri, &lderr );
if ( lderr != BIND_OK ) {
return DO_LDAP_ERR_RETRYABLE;
}
}
switch ( re->re_changetype ) {
case T_ADDCT:
lderr = op_ldap_add( ri, re, errmsg );
if ( lderr != LDAP_SUCCESS ) {
Debug( LDAP_DEBUG_ANY,
"Error: ldap_add_s failed adding \"%s\": %s\n",
*errmsg ? *errmsg : ldap_err2string( lderr ),
re->re_dn, 0 );
}
break;
case T_MODIFYCT:
lderr = op_ldap_modify( ri, re, errmsg );
if ( lderr != LDAP_SUCCESS ) {
Debug( LDAP_DEBUG_ANY,
"Error: ldap_modify_s failed modifying \"%s\": %s\n",
*errmsg ? *errmsg : ldap_err2string( lderr ),
re->re_dn, 0 );
}
break;
case T_DELETECT:
lderr = op_ldap_delete( ri, re, errmsg );
if ( lderr != LDAP_SUCCESS ) {
Debug( LDAP_DEBUG_ANY,
"Error: ldap_delete_s failed deleting \"%s\": %s\n",
*errmsg ? *errmsg : ldap_err2string( lderr ),
re->re_dn, 0 );
}
break;
case T_MODRDNCT:
lderr = op_ldap_modrdn( ri, re, errmsg );
if ( lderr != LDAP_SUCCESS ) {
Debug( LDAP_DEBUG_ANY,
"Error: ldap_modrdn_s failed modifying %s: %s\n",
*errmsg ? *errmsg : ldap_err2string( lderr ),
re->re_dn, 0 );
}
break;
default:
Debug( LDAP_DEBUG_ANY,
"Error: do_ldap: bad op \"%d\", dn = \"%s\"\n",
re->re_changetype, re->re_dn, 0 );
return DO_LDAP_ERR_FATAL;
}
/*
* Analyze return code. If ok, just return. If LDAP_SERVER_DOWN,
* we may have been idle long enough that the remote slapd timed
* us out. Rebind and try again.
*/
switch( lderr ) {
case LDAP_SUCCESS:
return DO_LDAP_OK;
default:
return DO_LDAP_ERR_FATAL;
case LDAP_SERVER_DOWN: /* server went down */
(void) do_unbind( ri );
retry--;
}
} while ( retry > 0 );
return DO_LDAP_ERR_RETRYABLE;
}
/*
* Perform an ldap add operation.
*/
static int
op_ldap_add(
Ri *ri,
Re *re,
char **errmsg
)
{
Mi *mi;
int nattrs, rc = 0, i;
LDAPMod *ldm, **ldmarr;
int lderr = 0;
nattrs = i = 0;
ldmarr = NULL;
/*
* Construct a null-terminated array of LDAPMod structs.
*/
mi = re->re_mods;
while ( mi[ i ].mi_type != NULL ) {
ldm = alloc_ldapmod();
ldmarr = ( LDAPMod ** ) ch_realloc( ldmarr,
( nattrs + 2 ) * sizeof( LDAPMod * ));
ldmarr[ nattrs ] = ldm;
ldm->mod_op = LDAP_MOD_BVALUES;
ldm->mod_type = mi[ i ].mi_type;
ldm->mod_bvalues =
make_singlevalued_berval( mi[ i ].mi_val, mi[ i ].mi_len );
i++;
nattrs++;
}
if ( ldmarr != NULL ) {
ldmarr[ nattrs ] = NULL;
/* Perform the operation */
Debug( LDAP_DEBUG_ARGS, "replica %s:%d - add dn \"%s\"\n",
ri->ri_hostname, ri->ri_port, re->re_dn );
rc = ldap_add_s( ri->ri_ldp, re->re_dn, ldmarr );
ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &lderr);
} else {
*errmsg = "No modifications to do";
Debug( LDAP_DEBUG_ANY,
"Error: op_ldap_add: no mods to do (%s)!\n", re->re_dn, 0, 0 );
}
free_ldmarr( ldmarr );
return( lderr );
}
/*
* Perform an ldap modify operation.
*/
#define AWAITING_OP -1
static int
op_ldap_modify(
Ri *ri,
Re *re,
char **errmsg
)
{
Mi *mi;
int state; /* This code is a simple-minded state machine */
int nvals; /* Number of values we're modifying */
int nops; /* Number of LDAPMod structs in ldmarr */
LDAPMod *ldm, **ldmarr;
int i, len;
char *type, *value;
int rc = 0;
state = AWAITING_OP;
nvals = 0;
nops = 0;
ldmarr = NULL;
if ( re->re_mods == NULL ) {
*errmsg = "No arguments given";
Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modify: no arguments\n",
0, 0, 0 );
return -1;
}
/*
* Construct a null-terminated array of LDAPMod structs.
*/
for ( mi = re->re_mods, i = 0; mi[ i ].mi_type != NULL; i++ ) {
type = mi[ i ].mi_type;
value = mi[ i ].mi_val;
len = mi[ i ].mi_len;
switch ( getmodtype( type )) {
case T_MODSEP:
state = T_MODSEP; /* Got a separator line "-\n" */
continue;
case T_MODOPADD:
state = T_MODOPADD;
ldmarr = ( LDAPMod ** )
ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
ldmarr[ nops ] = ldm = alloc_ldapmod();
ldm->mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
ldm->mod_type = value;
nvals = 0;
nops++;
break;
case T_MODOPREPLACE:
state = T_MODOPREPLACE;
ldmarr = ( LDAPMod ** )
ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
ldmarr[ nops ] = ldm = alloc_ldapmod();
ldm->mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
ldm->mod_type = value;
nvals = 0;
nops++;
break;
case T_MODOPDELETE:
state = T_MODOPDELETE;
ldmarr = ( LDAPMod ** )
ch_realloc(ldmarr, (( nops + 2 ) * ( sizeof( LDAPMod * ))));
ldmarr[ nops ] = ldm = alloc_ldapmod();
ldm->mod_op = LDAP_MOD_DELETE | LDAP_MOD_BVALUES;
ldm->mod_type = value;
nvals = 0;
nops++;
break;
default:
if ( state == AWAITING_OP ) {
Debug( LDAP_DEBUG_ANY,
"Error: op_ldap_modify: unknown mod type \"%s\"\n",
type, 0, 0 );
continue;
}
/*
* We should have an attribute: value pair here.
* Construct the mod_bvalues part of the ldapmod struct.
*/
if ( strcasecmp( type, ldm->mod_type )) {
Debug( LDAP_DEBUG_ANY,
"Error: malformed modify op, %s: %s (expecting %s:)\n",
type, value, ldm->mod_type );
continue;
}
ldm->mod_bvalues = ( struct berval ** )
ch_realloc( ldm->mod_bvalues,
( nvals + 2 ) * sizeof( struct berval * ));
ldm->mod_bvalues[ nvals + 1 ] = NULL;
ldm->mod_bvalues[ nvals ] = ( struct berval * )
ch_malloc( sizeof( struct berval ));
ldm->mod_bvalues[ nvals ]->bv_val = value;
ldm->mod_bvalues[ nvals ]->bv_len = len;
nvals++;
}
}
ldmarr[ nops ] = NULL;
if ( nops > 0 ) {
/* Actually perform the LDAP operation */
Debug( LDAP_DEBUG_ARGS, "replica %s:%d - modify dn \"%s\"\n",
ri->ri_hostname, ri->ri_port, re->re_dn );
rc = ldap_modify_s( ri->ri_ldp, re->re_dn, ldmarr );
}
free_ldmarr( ldmarr );
return( rc );
}
/*
* Perform an ldap delete operation.
*/
static int
op_ldap_delete(
Ri *ri,
Re *re,
char **errmsg
)
{
int rc;
Debug( LDAP_DEBUG_ARGS, "replica %s:%d - delete dn \"%s\"\n",
ri->ri_hostname, ri->ri_port, re->re_dn );
rc = ldap_delete_s( ri->ri_ldp, re->re_dn );
return( rc );
}
/*
* Perform an ldap modrdn operation.
*/
#define GOT_NEWRDN 0x1
#define GOT_DELOLDRDN 0x2
#define GOT_NEWSUP 0x4
#define GOT_MODDN_REQ (GOT_NEWRDN|GOT_DELOLDRDN)
#define GOT_ALL_MODDN(f) (((f) & GOT_MODDN_REQ) == GOT_MODDN_REQ)
static int
op_ldap_modrdn(
Ri *ri,
Re *re,
char **errmsg
)
{
int rc = 0;
Mi *mi;
int i;
int lderr = 0;
int state = 0;
int drdnflag = -1;
char *newrdn;
char *newsup = NULL;
if ( re->re_mods == NULL ) {
*errmsg = "No arguments given";
Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: no arguments\n",
0, 0, 0 );
return -1;
}
/*
* Get the arguments: should see newrdn: and deleteoldrdn: args.
*/
for ( mi = re->re_mods, i = 0; mi[ i ].mi_type != NULL; i++ ) {
if ( !strcmp( mi[ i ].mi_type, T_NEWRDNSTR )) {
if( state & GOT_NEWRDN ) {
Debug( LDAP_DEBUG_ANY,
"Error: op_ldap_modrdn: multiple newrdn arg \"%s\"\n",
mi[ i ].mi_val, 0, 0 );
*errmsg = "Multiple newrdn argument";
return -1;
}
newrdn = mi[ i ].mi_val;
state |= GOT_NEWRDN;
} else if ( !strcmp( mi[ i ].mi_type, T_DELOLDRDNSTR )) {
if( state & GOT_DELOLDRDN ) {
Debug( LDAP_DEBUG_ANY,
"Error: op_ldap_modrdn: multiple deleteoldrdn arg \"%s\"\n",
mi[ i ].mi_val, 0, 0 );
*errmsg = "Multiple newrdn argument";
return -1;
}
state |= GOT_DELOLDRDN;
if ( !strcmp( mi[ i ].mi_val, "0" )) {
drdnflag = 0;
} else if ( !strcmp( mi[ i ].mi_val, "1" )) {
drdnflag = 1;
} else {
Debug( LDAP_DEBUG_ANY,
"Error: op_ldap_modrdn: bad deleteoldrdn arg \"%s\"\n",
mi[ i ].mi_val, 0, 0 );
*errmsg = "Incorrect argument to deleteoldrdn";
return -1;
}
} else if ( !strcmp( mi[ i ].mi_type, T_NEWSUPSTR )) {
if( state & GOT_NEWSUP ) {
Debug( LDAP_DEBUG_ANY,
"Error: op_ldap_modrdn: multiple newsuperior arg \"%s\"\n",
mi[ i ].mi_val, 0, 0 );
*errmsg = "Multiple newsuperior argument";
return -1;
}
newsup = mi[ i ].mi_val;
state |= GOT_NEWSUP;
} else {
Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: bad type \"%s\"\n",
mi[ i ].mi_type, 0, 0 );
*errmsg = "Bad value in replication log entry";
return -1;
}
}
/*
* Punt if we don't have all the args.
*/
if ( !GOT_ALL_MODDN(state) ) {
Debug( LDAP_DEBUG_ANY, "Error: op_ldap_modrdn: missing arguments\n",
0, 0, 0 );
*errmsg = "Missing argument: requires \"newrdn\" and \"deleteoldrdn\"";
return -1;
}
#ifdef LDAP_DEBUG
if ( ldap_debug & LDAP_DEBUG_ARGS ) {
char buf[ 256 ];
char *buf2;
sprintf( buf, "%s:%d", ri->ri_hostname, ri->ri_port );
buf2 = (char *) ch_malloc( strlen( re->re_dn ) + strlen( mi->mi_val )
+ 10 );
sprintf( buf2, "(\"%s\" -> \"%s\")", re->re_dn, mi->mi_val );
Debug( LDAP_DEBUG_ARGS,
"replica %s - modify rdn %s (flag: %d)\n",
buf, buf2, drdnflag );
free( buf2 );
}
#endif /* LDAP_DEBUG */
/* Do the modrdn */
rc = ldap_rename2_s( ri->ri_ldp, re->re_dn, newrdn, newsup, drdnflag );
ldap_get_option( ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &lderr);
return( lderr );
}
/*
* Allocate and initialize an ldapmod struct.
*/
static LDAPMod *
alloc_ldapmod( void )
{
LDAPMod *ldm;
ldm = ( struct ldapmod * ) ch_malloc( sizeof ( struct ldapmod ));
ldm->mod_type = NULL;
ldm->mod_bvalues = ( struct berval ** ) NULL;
return( ldm );
}
/*
* Free an ldapmod struct associated mod_bvalues. NOTE - it is assumed
* that mod_bvalues and mod_type contain pointers to the same block of memory
* pointed to by the repl struct. Therefore, it's not freed here.
*/
static void
free_ldapmod(
LDAPMod *ldm )
{
int i;
if ( ldm == NULL ) {
return;
}
if ( ldm->mod_bvalues != NULL ) {
for ( i = 0; ldm->mod_bvalues[ i ] != NULL; i++ ) {
free( ldm->mod_bvalues[ i ] );
}
free( ldm->mod_bvalues );
}
free( ldm );
return;
}
/*
* Free an an array of LDAPMod pointers and the LDAPMod structs they point
* to.
*/
static void
free_ldmarr(
LDAPMod **ldmarr )
{
int i;
for ( i = 0; ldmarr[ i ] != NULL; i++ ) {
free_ldapmod( ldmarr[ i ] );
}
free( ldmarr );
}
/*
* Create a berval with a single value.
*/
static struct berval **
make_singlevalued_berval(
char *value,
int len )
{
struct berval **p;
p = ( struct berval ** ) ch_malloc( 2 * sizeof( struct berval * ));
p[ 0 ] = ( struct berval * ) ch_malloc( sizeof( struct berval ));
p[ 1 ] = NULL;
p[ 0 ]->bv_val = value;
p[ 0 ]->bv_len = len;
return( p );
}
/*
* Given a modification type (string), return an enumerated type.
* Avoids ugly copy in op_ldap_modify - lets us use a switch statement
* there.
*/
static int
getmodtype(
char *type )
{
if ( !strcmp( type, T_MODSEPSTR )) {
return( T_MODSEP );
}
if ( !strcmp( type, T_MODOPADDSTR )) {
return( T_MODOPADD );
}
if ( !strcmp( type, T_MODOPREPLACESTR )) {
return( T_MODOPREPLACE );
}
if ( !strcmp( type, T_MODOPDELETESTR )) {
return( T_MODOPDELETE );
}
return( T_ERR );
}
/*
* Perform an LDAP unbind operation. If replica is NULL, or the
* repl_ldp is NULL, just return LDAP_SUCCESS. Otherwise, unbind,
* set the ldp to NULL, and return the result of the unbind call.
*/
static int
do_unbind(
Ri *ri
)
{
int rc = LDAP_SUCCESS;
if (( ri != NULL ) && ( ri->ri_ldp != NULL )) {
rc = ldap_unbind( ri->ri_ldp );
if ( rc != LDAP_SUCCESS ) {
Debug( LDAP_DEBUG_ANY,
"Error: do_unbind: ldap_unbind failed for %s:%d: %s\n",
ri->ri_hostname, ri->ri_port, ldap_err2string( rc ) );
}
ri->ri_ldp = NULL;
}
return rc;
}
/*
* Perform an LDAP bind operation to the replication site given
* by replica. If replica->repl_ldp is non-NULL, then we unbind
* from the replica before rebinding. It should be safe to call
* this to re-connect if the replica's connection goes away
* for some reason.
*
* Returns 0 on success, -1 if an LDAP error occurred, and a return
* code > 0 if some other error occurred, e.g. invalid bind method.
* If an LDAP error occurs, the LDAP error is returned in lderr.
*/
static int
do_bind(
Ri *ri,
int *lderr
)
{
int ldrc;
*lderr = 0;
if ( ri == NULL ) {
Debug( LDAP_DEBUG_ANY, "Error: do_bind: null ri ptr\n", 0, 0, 0 );
return( BIND_ERR_BADRI );
}
if ( ri->ri_ldp != NULL ) {
ldrc = ldap_unbind( ri->ri_ldp );
if ( ldrc != LDAP_SUCCESS ) {
Debug( LDAP_DEBUG_ANY,
"Error: do_bind: ldap_unbind failed: %s\n",
ldap_err2string( ldrc ), 0, 0 );
}
ri->ri_ldp = NULL;
}
Debug( LDAP_DEBUG_ARGS, "Initializing session to %s:%d\n",
ri->ri_hostname, ri->ri_port, 0 );
ri->ri_ldp = ldap_init( ri->ri_hostname, ri->ri_port );
if ( ri->ri_ldp == NULL ) {
Debug( LDAP_DEBUG_ANY, "Error: ldap_init(%s, %d) failed: %s\n",
ri->ri_hostname, ri->ri_port, sys_errlist[ errno ] );
return( BIND_ERR_OPEN );
}
{ /* set version 3 */
int err, version = 3;
err = ldap_set_option(ri->ri_ldp,
LDAP_OPT_PROTOCOL_VERSION, &version);
if( err != LDAP_OPT_SUCCESS ) {
Debug( LDAP_DEBUG_ANY,
"Error: ldap_set_option(%s, LDAP_OPT_VERSION, 3) failed!\n",
ri->ri_hostname, NULL, NULL );
ldap_unbind( ri->ri_ldp );
ri->ri_ldp = NULL;
return BIND_ERR_VERSION;
}
}
/*
* Set ldap library options to (1) not follow referrals, and
* (2) restart the select() system call.
*/
{
int err;
err = ldap_set_option(ri->ri_ldp, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
if( err != LDAP_OPT_SUCCESS ) {
Debug( LDAP_DEBUG_ANY,
"Error: ldap_set_option(%s,REFERRALS, OFF) failed!\n",
ri->ri_hostname, NULL, NULL );
ldap_unbind( ri->ri_ldp );
ri->ri_ldp = NULL;
return BIND_ERR_REFERRALS;
}
}
ldap_set_option(ri->ri_ldp, LDAP_OPT_RESTART, LDAP_OPT_ON);
if( ri->ri_tls ) {
int err;
err = ldap_start_tls_s(ri->ri_ldp, NULL, NULL);
if( err != LDAP_SUCCESS ) {
Debug( LDAP_DEBUG_ANY,
"%s: ldap_start_tls failed: %s (%d)\n",
ri->ri_tls == TLS_CRITICAL ? "Error" : "Warning",
ldap_err2string( err ), err );
if( ri->ri_tls == TLS_CRITICAL ) {
ldap_unbind( ri->ri_ldp );
ri->ri_ldp = NULL;
return BIND_ERR_TLS_FAILED;
}
}
}
switch ( ri->ri_bind_method ) {
case AUTH_SIMPLE:
/*
* Bind with a plaintext password.
*/
Debug( LDAP_DEBUG_ARGS, "bind to %s:%d as %s (simple)\n",
ri->ri_hostname, ri->ri_port, ri->ri_bind_dn );
ldrc = ldap_simple_bind_s( ri->ri_ldp, ri->ri_bind_dn,
ri->ri_password );
if ( ldrc != LDAP_SUCCESS ) {
Debug( LDAP_DEBUG_ANY,
"Error: ldap_simple_bind_s for %s:%d failed: %s\n",
ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ));
*lderr = ldrc;
ldap_unbind( ri->ri_ldp );
ri->ri_ldp = NULL;
return( BIND_ERR_SIMPLE_FAILED );
}
break;
case AUTH_SASL:
Debug( LDAP_DEBUG_ARGS, "bind to %s as %s via %s (SASL)\n",
ri->ri_hostname, ri->ri_authcId, ri->ri_saslmech );
#ifdef HAVE_CYRUS_SASL
if( ri->ri_secprops != NULL ) {
int err;
err = ldap_set_option(ri->ri_ldp, LDAP_OPT_X_SASL_SECPROPS,
ri->ri_secprops);
if( err != LDAP_OPT_SUCCESS ) {
Debug( LDAP_DEBUG_ANY,
"Error: ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
ri->ri_hostname, ri->ri_secprops, NULL );
ldap_unbind( ri->ri_ldp );
ri->ri_ldp = NULL;
return BIND_ERR_SASL_FAILED;
}
}
{
char *passwd = ri->ri_password ? ber_strdup( ri->ri_password ) : NULL;
void *defaults = lutil_sasl_defaults( ri->ri_ldp, ri->ri_saslmech,
ri->ri_realm, ri->ri_authcId, passwd, ri->ri_authzId );
ldrc = ldap_sasl_interactive_bind_s( ri->ri_ldp, ri->ri_bind_dn,
ri->ri_saslmech, NULL, NULL,
LDAP_SASL_QUIET, lutil_sasl_interact, defaults );
if ( ldrc != LDAP_SUCCESS ) {
Debug( LDAP_DEBUG_ANY, "Error: LDAP SASL for %s:%d failed: %s\n",
ri->ri_hostname, ri->ri_port, ldap_err2string( ldrc ));
*lderr = ldrc;
ldap_unbind( ri->ri_ldp );
ri->ri_ldp = NULL;
return( BIND_ERR_SASL_FAILED );
}
ber_memfree( passwd );
ber_memfree( defaults );
}
break;
#else
Debug( LDAP_DEBUG_ANY,
"Error: do_bind: SASL not supported %s:%d\n",
ri->ri_hostname, ri->ri_port, NULL );
ldap_unbind( ri->ri_ldp );
ri->ri_ldp = NULL;
return( BIND_ERR_BAD_ATYPE );
#endif
default:
Debug( LDAP_DEBUG_ANY,
"Error: do_bind: unknown auth type \"%d\" for %s:%d\n",
ri->ri_bind_method, ri->ri_hostname, ri->ri_port );
ldap_unbind( ri->ri_ldp );
ri->ri_ldp = NULL;
return( BIND_ERR_BAD_ATYPE );
}
{
int err;
LDAPControl c;
LDAPControl *ctrls[2];
ctrls[0] = &c;
ctrls[1] = NULL;
c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
c.ldctl_value.bv_val = NULL;
c.ldctl_value.bv_len = 0;
c.ldctl_iscritical = 0;
err = ldap_set_option(ri->ri_ldp, LDAP_OPT_SERVER_CONTROLS, &ctrls);
if( err != LDAP_OPT_SUCCESS ) {
Debug( LDAP_DEBUG_ANY, "Error: "
"ldap_set_option(%s, SERVER_CONTROLS, ManageDSAit) failed!\n",
ri->ri_hostname, NULL, NULL );
ldap_unbind( ri->ri_ldp );
ri->ri_ldp = NULL;
return BIND_ERR_MANAGEDSAIT;
}
}
return( BIND_OK );
}
/*
* For debugging. Print the contents of an ldmarr array.
*/
static void
dump_ldm_array(
LDAPMod **ldmarr
)
{
int i, j;
LDAPMod *ldm;
struct berval *b;
char *msgbuf;
for ( i = 0; ldmarr[ i ] != NULL; i++ ) {
ldm = ldmarr[ i ];
Debug( LDAP_DEBUG_TRACE,
"Trace (%ld): *** ldmarr[ %d ] contents:\n",
(long) getpid(), i, 0 );
Debug( LDAP_DEBUG_TRACE,
"Trace (%ld): *** ldm->mod_op: %d\n",
(long) getpid(), ldm->mod_op, 0 );
Debug( LDAP_DEBUG_TRACE,
"Trace (%ld): *** ldm->mod_type: %s\n",
(long) getpid(), ldm->mod_type, 0 );
if ( ldm->mod_bvalues != NULL ) {
for ( j = 0; ( b = ldm->mod_bvalues[ j ] ) != NULL; j++ ) {
msgbuf = ch_malloc( b->bv_len + 512 );
sprintf( msgbuf, "***** bv[ %d ] len = %ld, val = <%s>",
j, b->bv_len, b->bv_val );
Debug( LDAP_DEBUG_TRACE,
"Trace (%ld):%s\n", (long) getpid(), msgbuf, 0 );
free( msgbuf );
}
}
}
}
|