1 /* common.c - common routines for the ldap client tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 The OpenLDAP Foundation.
6 * Portions Copyright 2003 Kurt D. Zeilenga.
7 * Portions Copyright 2003 IBM Corporation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
19 * This file was initially created by Hallvard B. Furuseth based (in
20 * part) upon argument parsing code for individual tools located in
21 * this directory. Additional contributors include:
22 * Kurt D. Zeilenga (additional common argument and control support)
29 #include <ac/stdlib.h>
30 #include <ac/signal.h>
31 #include <ac/string.h>
32 #include <ac/unistd.h>
35 #ifdef HAVE_CYRUS_SASL
36 #ifdef HAVE_SASL_SASL_H
37 #include <sasl/sasl.h>
45 #include "lutil_ldap.h"
46 #include "ldap_defaults.h"
52 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
54 /* Necessary for old LDAPv2 Kerberos Bind methods */
56 ldap_bind LDAP_P(( /* deprecated */
59 LDAP_CONST char *passwd,
70 char *ldaphost = NULL;
72 #ifdef HAVE_CYRUS_SASL
73 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
74 char *sasl_realm = NULL;
75 char *sasl_authc_id = NULL;
76 char *sasl_authz_id = NULL;
77 char *sasl_mech = NULL;
78 char *sasl_secprops = NULL;
83 char *assertion = NULL;
90 char *preread_attrs = NULL;
92 char *postread_attrs = NULL;
96 struct berval passwd = { 0, NULL };
103 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
105 static int chainingResolve = -1;
106 static int chainingContinuation = -1;
107 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
124 ldap_pvt_setlocale(LC_MESSAGES, "");
125 ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
126 ldap_pvt_textdomain(OPENLDAP_PACKAGE);
132 #ifdef HAVE_CYRUS_SASL
136 ldap_pvt_tls_destroy();
141 tool_common_usage( void )
143 static const char *const descriptions[] = {
144 N_(" -c continuous operation mode (do not stop on errors)\n"),
145 N_(" -C chase referrals (anonymously)\n"),
146 N_(" -d level set LDAP debugging level to `level'\n"),
147 N_(" -D binddn bind DN\n"),
148 N_(" -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
149 N_(" [!]assert=<filter> (an RFC 2254 Filter)\n")
150 N_(" [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n")
151 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
152 N_(" [!]chaining[=<resolveBehavior>[/<continuationBehavior>]]\n")
153 N_(" one of \"chainingPreferred\", \"chainingRequired\",\n")
154 N_(" \"referralsPreferred\", \"referralsRequired\"\n")
155 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
157 N_(" [!]manageDIT\n")
159 N_(" [!]manageDSAit\n")
161 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
164 N_(" [!]postread[=<attrs>] (a comma-separated attribute list)\n")
165 N_(" [!]preread[=<attrs>] (a comma-separated attribute list)\n"),
166 N_(" abandon, cancel (SIGINT sends abandon/cancel; not really controls)\n")
167 N_(" -f file read operations from `file'\n"),
168 N_(" -h host LDAP server\n"),
169 N_(" -H URI LDAP Uniform Resource Indentifier(s)\n"),
170 N_(" -I use SASL Interactive mode\n"),
171 N_(" -k use Kerberos authentication\n"),
172 N_(" -K like -k, but do only step 1 of the Kerberos bind\n"),
173 N_(" -M enable Manage DSA IT control (-MM to make critical)\n"),
174 N_(" -n show what would be done but don't actually do it\n"),
175 N_(" -O props SASL security properties\n"),
176 N_(" -p port port on LDAP server\n"),
177 N_(" -P version procotol version (default: 3)\n"),
178 N_(" -Q use SASL Quiet mode\n"),
179 N_(" -R realm SASL realm\n"),
180 N_(" -U authcid SASL authentication identity\n"),
181 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
182 N_(" -V print version info (-VV only)\n"),
183 N_(" -w passwd bind password (for simple authentication)\n"),
184 N_(" -W prompt for bind password\n"),
185 N_(" -x Simple authentication\n"),
186 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
187 N_(" -y file Read password from file\n"),
188 N_(" -Y mech SASL mechanism\n"),
189 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
192 const char *const *cpp;
194 fputs( _("Common options:\n"), stderr );
195 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
196 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
197 fputs( _(*cpp), stderr );
210 fprintf( stderr, "%s: %s (%d)%s\n",
211 func, ldap_err2string( err ), err, extra ? extra : "" );
213 if ( matched && *matched ) {
214 fprintf( stderr, _("\tmatched DN: %s\n"), matched );
217 if ( info && *info ) {
218 fprintf( stderr, _("\tadditional info: %s\n"), info );
221 if ( refs && *refs ) {
223 fprintf( stderr, _("\treferrals:\n") );
224 for( i=0; refs[i]; i++ ) {
225 fprintf( stderr, "\t\t%s\n", refs[i] );
232 tool_args( int argc, char **argv )
236 while (( i = getopt( argc, argv, options )) != EOF ) {
238 char *control, *cvalue, *next;
240 case 'c': /* continuous operation mode */
247 ival = strtol( optarg, &next, 10 );
248 if (next == NULL || next[0] != '\0') {
249 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
254 case 'D': /* bind DN */
255 if( binddn != NULL ) {
256 fprintf( stderr, "%s: -D previously specified\n", prog );
257 exit( EXIT_FAILURE );
259 binddn = ber_strdup( optarg );
261 case 'e': /* general extensions (controls and such) */
262 /* should be extended to support comma separated list of
263 * [!]key[=value] parameters, e.g. -e !foo,bar=567
268 if( optarg[0] == '!' ) {
273 control = ber_strdup( optarg );
274 if ( (cvalue = strchr( control, '=' )) != NULL ) {
278 if ( strcasecmp( control, "assert" ) == 0 ) {
280 fprintf( stderr, "assert control previously specified\n");
281 exit( EXIT_FAILURE );
283 if( cvalue == NULL ) {
284 fprintf( stderr, "assert: control value expected\n" );
288 assertctl = 1 + crit;
290 assert( assertion == NULL );
293 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
294 if( authzid != NULL ) {
295 fprintf( stderr, "authzid control previously specified\n");
296 exit( EXIT_FAILURE );
298 if( cvalue == NULL ) {
299 fprintf( stderr, "authzid: control value expected\n" );
303 fprintf( stderr, "authzid: must be marked critical\n" );
307 assert( authzid == NULL );
310 } else if ( strcasecmp( control, "manageDIT" ) == 0 ) {
313 "manageDIT control previously specified\n");
314 exit( EXIT_FAILURE );
316 if( cvalue != NULL ) {
318 "manageDIT: no control value expected\n" );
322 manageDIT = 1 + crit;
324 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
327 "manageDSAit control previously specified\n");
328 exit( EXIT_FAILURE );
330 if( cvalue != NULL ) {
332 "manageDSAit: no control value expected\n" );
336 manageDSAit = 1 + crit;
338 } else if ( strcasecmp( control, "noop" ) == 0 ) {
340 fprintf( stderr, "noop control previously specified\n");
341 exit( EXIT_FAILURE );
343 if( cvalue != NULL ) {
344 fprintf( stderr, "noop: no control value expected\n" );
350 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
351 } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
353 fprintf( stderr, "ppolicy control previously specified\n");
354 exit( EXIT_FAILURE );
356 if( cvalue != NULL ) {
357 fprintf( stderr, "ppolicy: no control value expected\n" );
361 fprintf( stderr, "ppolicy: critical flag not allowed\n" );
368 } else if ( strcasecmp( control, "preread" ) == 0 ) {
370 fprintf( stderr, "preread control previously specified\n");
371 exit( EXIT_FAILURE );
375 preread_attrs = cvalue;
377 } else if ( strcasecmp( control, "postread" ) == 0 ) {
379 fprintf( stderr, "postread control previously specified\n");
380 exit( EXIT_FAILURE );
384 postread_attrs = cvalue;
386 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
387 } else if ( strcasecmp( control, "chaining" ) == 0 ) {
390 if ( cvalue != NULL ) {
393 continuation = strchr( cvalue, '/' );
394 if ( continuation ) {
395 /* FIXME: this makes sense only in searches */
396 *continuation++ = '\0';
397 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
398 chainingContinuation = LDAP_CHAINING_PREFERRED;
399 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
400 chainingContinuation = LDAP_CHAINING_REQUIRED;
401 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
402 chainingContinuation = LDAP_REFERRALS_PREFERRED;
403 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
404 chainingContinuation = LDAP_REFERRALS_REQUIRED;
407 "chaining behavior control "
408 "continuation value \"%s\" invalid\n",
410 exit( EXIT_FAILURE );
414 if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
415 chainingResolve = LDAP_CHAINING_PREFERRED;
416 } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
417 chainingResolve = LDAP_CHAINING_REQUIRED;
418 } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
419 chainingResolve = LDAP_REFERRALS_PREFERRED;
420 } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
421 chainingResolve = LDAP_REFERRALS_REQUIRED;
424 "chaining behavior control "
425 "resolve value \"%s\" invalid\n",
427 exit( EXIT_FAILURE );
430 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
432 /* this shouldn't go here, really; but it's a feature... */
433 } else if ( strcasecmp( control, "abandon" ) == 0 ) {
434 abcan = LDAP_REQ_ABANDON;
436 } else if ( strcasecmp( control, "cancel" ) == 0 ) {
437 abcan = LDAP_REQ_EXTENDED;
440 fprintf( stderr, "Invalid general control name: %s\n",
445 case 'f': /* read from file */
446 if( infile != NULL ) {
447 fprintf( stderr, "%s: -f previously specified\n", prog );
448 exit( EXIT_FAILURE );
450 infile = ber_strdup( optarg );
452 case 'h': /* ldap host */
453 if( ldaphost != NULL ) {
454 fprintf( stderr, "%s: -h previously specified\n", prog );
455 exit( EXIT_FAILURE );
457 ldaphost = ber_strdup( optarg );
459 case 'H': /* ldap URI */
460 if( ldapuri != NULL ) {
461 fprintf( stderr, "%s: -H previously specified\n", prog );
462 exit( EXIT_FAILURE );
464 ldapuri = ber_strdup( optarg );
467 #ifdef HAVE_CYRUS_SASL
468 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
469 fprintf( stderr, "%s: incompatible previous "
470 "authentication choice\n",
472 exit( EXIT_FAILURE );
474 authmethod = LDAP_AUTH_SASL;
475 sasl_flags = LDAP_SASL_INTERACTIVE;
478 fprintf( stderr, "%s: was not compiled with SASL support\n",
480 exit( EXIT_FAILURE );
482 case 'k': /* kerberos bind */
483 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
484 if( authmethod != -1 ) {
485 fprintf( stderr, "%s: -k incompatible with previous "
486 "authentication choice\n", prog );
487 exit( EXIT_FAILURE );
489 authmethod = LDAP_AUTH_KRBV4;
491 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
492 exit( EXIT_FAILURE );
495 case 'K': /* kerberos bind, part one only */
496 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
497 if( authmethod != -1 ) {
498 fprintf( stderr, "%s: incompatible with previous "
499 "authentication choice\n", prog );
500 exit( EXIT_FAILURE );
502 authmethod = LDAP_AUTH_KRBV41;
504 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
505 exit( EXIT_FAILURE );
509 /* enable Manage DSA IT */
512 case 'n': /* print operations, don't actually do them */
516 #ifdef HAVE_CYRUS_SASL
517 if( sasl_secprops != NULL ) {
518 fprintf( stderr, "%s: -O previously specified\n", prog );
519 exit( EXIT_FAILURE );
521 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
522 fprintf( stderr, "%s: incompatible previous "
523 "authentication choice\n", prog );
524 exit( EXIT_FAILURE );
526 authmethod = LDAP_AUTH_SASL;
527 sasl_secprops = ber_strdup( optarg );
529 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
530 exit( EXIT_FAILURE );
535 fprintf( stderr, "%s: -p previously specified\n", prog );
536 exit( EXIT_FAILURE );
538 ival = strtol( optarg, &next, 10 );
539 if ( next == NULL || next[0] != '\0' ) {
540 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
541 exit( EXIT_FAILURE );
546 ival = strtol( optarg, &next, 10 );
547 if ( next == NULL || next[0] != '\0' ) {
548 fprintf( stderr, "%s: unabel to parse protocol version \"%s\"\n", prog, optarg );
549 exit( EXIT_FAILURE );
553 if( protocol == LDAP_VERSION3 ) {
554 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
556 exit( EXIT_FAILURE );
558 protocol = LDAP_VERSION2;
561 if( protocol == LDAP_VERSION2 ) {
562 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
564 exit( EXIT_FAILURE );
566 protocol = LDAP_VERSION3;
569 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
575 #ifdef HAVE_CYRUS_SASL
576 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
577 fprintf( stderr, "%s: incompatible previous "
578 "authentication choice\n",
580 exit( EXIT_FAILURE );
582 authmethod = LDAP_AUTH_SASL;
583 sasl_flags = LDAP_SASL_QUIET;
586 fprintf( stderr, "%s: not compiled with SASL support\n",
588 exit( EXIT_FAILURE );
591 #ifdef HAVE_CYRUS_SASL
592 if( sasl_realm != NULL ) {
593 fprintf( stderr, "%s: -R previously specified\n", prog );
594 exit( EXIT_FAILURE );
596 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
597 fprintf( stderr, "%s: incompatible previous "
598 "authentication choice\n",
600 exit( EXIT_FAILURE );
602 authmethod = LDAP_AUTH_SASL;
603 sasl_realm = ber_strdup( optarg );
605 fprintf( stderr, "%s: not compiled with SASL support\n",
607 exit( EXIT_FAILURE );
611 #ifdef HAVE_CYRUS_SASL
612 if( sasl_authc_id != NULL ) {
613 fprintf( stderr, "%s: -U previously specified\n", prog );
614 exit( EXIT_FAILURE );
616 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
617 fprintf( stderr, "%s: incompatible previous "
618 "authentication choice\n",
620 exit( EXIT_FAILURE );
622 authmethod = LDAP_AUTH_SASL;
623 sasl_authc_id = ber_strdup( optarg );
625 fprintf( stderr, "%s: not compiled with SASL support\n",
627 exit( EXIT_FAILURE );
630 case 'v': /* verbose mode */
633 case 'V': /* version */
636 case 'w': /* password */
637 passwd.bv_val = ber_strdup( optarg );
641 for( p = optarg; *p != '\0'; p++ ) {
645 passwd.bv_len = strlen( passwd.bv_val );
654 #ifdef HAVE_CYRUS_SASL
655 if( sasl_mech != NULL ) {
656 fprintf( stderr, "%s: -Y previously specified\n", prog );
657 exit( EXIT_FAILURE );
659 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
661 "%s: incompatible with authentication choice\n", prog );
662 exit( EXIT_FAILURE );
664 authmethod = LDAP_AUTH_SASL;
665 sasl_mech = ber_strdup( optarg );
667 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
668 exit( EXIT_FAILURE );
672 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
673 fprintf( stderr, "%s: incompatible with previous "
674 "authentication choice\n", prog );
675 exit( EXIT_FAILURE );
677 authmethod = LDAP_AUTH_SIMPLE;
680 #ifdef HAVE_CYRUS_SASL
681 if( sasl_authz_id != NULL ) {
682 fprintf( stderr, "%s: -X previously specified\n", prog );
683 exit( EXIT_FAILURE );
685 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
686 fprintf( stderr, "%s: -X incompatible with "
687 "authentication choice\n", prog );
688 exit( EXIT_FAILURE );
690 authmethod = LDAP_AUTH_SASL;
691 sasl_authz_id = ber_strdup( optarg );
693 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
694 exit( EXIT_FAILURE );
701 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
702 exit( EXIT_FAILURE );
706 if( handle_private_option( i ) ) break;
707 fprintf( stderr, "%s: unrecognized option -%c\n",
714 /* prevent bad linking */
716 api.ldapai_info_version = LDAP_API_INFO_VERSION;
718 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
719 != LDAP_OPT_SUCCESS )
721 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
722 exit( EXIT_FAILURE );
725 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
726 fprintf( stderr, "LDAP APIInfo version mismatch: "
727 "library %d, header %d\n",
728 api.ldapai_info_version, LDAP_API_INFO_VERSION );
729 exit( EXIT_FAILURE );
732 if( api.ldapai_api_version != LDAP_API_VERSION ) {
733 fprintf( stderr, "LDAP API version mismatch: "
734 "library %d, header %d\n",
735 api.ldapai_api_version, LDAP_API_VERSION );
736 exit( EXIT_FAILURE );
739 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
740 fprintf( stderr, "LDAP vendor name mismatch: "
741 "library %s, header %s\n",
742 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
743 exit( EXIT_FAILURE );
746 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
747 fprintf( stderr, "LDAP vendor version mismatch: "
748 "library %d, header %d\n",
749 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
750 exit( EXIT_FAILURE );
754 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
756 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
757 if (version > 1) exit( EXIT_SUCCESS );
760 ldap_memfree( api.ldapai_vendor_name );
761 ber_memvfree( (void **)api.ldapai_extensions );
765 protocol = LDAP_VERSION3;
767 if (authmethod == -1 && protocol > LDAP_VERSION2) {
768 #ifdef HAVE_CYRUS_SASL
769 authmethod = LDAP_AUTH_SASL;
771 authmethod = LDAP_AUTH_SIMPLE;
775 if( ldapuri == NULL ) {
776 if( ldapport && ( ldaphost == NULL )) {
777 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
778 exit( EXIT_FAILURE );
781 if( ldaphost != NULL ) {
782 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
783 exit( EXIT_FAILURE );
786 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
787 exit( EXIT_FAILURE );
791 if( protocol == LDAP_VERSION2 ) {
792 if( assertctl || authzid || manageDIT || manageDSAit ||
793 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
796 noop || ppolicy || preread || postread )
798 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
799 exit( EXIT_FAILURE );
803 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
804 exit( EXIT_FAILURE );
807 #ifdef HAVE_CYRUS_SASL
808 if( authmethod == LDAP_AUTH_SASL ) {
809 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
811 exit( EXIT_FAILURE );
815 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
817 if ( authmethod == LDAP_AUTH_KRBV4 || authmethod == LDAP_AUTH_KRBV41 ) {
818 fprintf( stderr, "%s: -k/-K incompatible with LDAPv%d\n",
820 exit( EXIT_FAILURE );
828 tool_conn_setup( int not, void (*private_setup)( LDAP * ) )
833 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
834 != LBER_OPT_SUCCESS )
837 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
839 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
840 != LDAP_OPT_SUCCESS )
843 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
848 (void) SIGNAL( SIGPIPE, SIG_IGN );
852 SIGNAL( SIGINT, do_sig );
858 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
861 memset( &url, 0, sizeof(url));
863 url.lud_scheme = "ldap";
864 url.lud_host = ldaphost;
865 url.lud_port = ldapport;
866 url.lud_scope = LDAP_SCOPE_DEFAULT;
868 ldapuri = ldap_url_desc2str( &url );
872 fprintf( stderr, "ldap_initialize( %s )\n",
873 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
875 rc = ldap_initialize( &ld, ldapuri );
876 if( rc != LDAP_SUCCESS ) {
878 "Could not create LDAP session handle for URI=%s (%d): %s\n",
879 ldapuri, rc, ldap_err2string(rc) );
880 exit( EXIT_FAILURE );
883 if( private_setup ) private_setup( ld );
886 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
887 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
889 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
890 referrals ? "on" : "off" );
891 exit( EXIT_FAILURE );
894 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
895 != LDAP_OPT_SUCCESS )
897 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
899 exit( EXIT_FAILURE );
903 rc = ldap_start_tls_s( ld, NULL, NULL );
904 if ( rc != LDAP_SUCCESS ) {
905 tool_perror( "ldap_start_tls", rc, NULL, NULL, NULL, NULL );
907 exit( EXIT_FAILURE );
918 tool_bind( LDAP *ld )
920 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
922 LDAPControl *ctrls[2], c;
923 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
924 c.ldctl_value.bv_val = NULL;
925 c.ldctl_value.bv_len = 0;
926 c.ldctl_iscritical = 0;
929 ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
933 if ( authmethod == LDAP_AUTH_SASL ) {
934 #ifdef HAVE_CYRUS_SASL
938 if( sasl_secprops != NULL ) {
939 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
940 (void *) sasl_secprops );
942 if( rc != LDAP_OPT_SUCCESS ) {
944 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
946 exit( EXIT_FAILURE );
950 defaults = lutil_sasl_defaults( ld,
957 rc = ldap_sasl_interactive_bind_s( ld, binddn,
958 sasl_mech, NULL, NULL,
959 sasl_flags, lutil_sasl_interact, defaults );
961 lutil_sasl_freedefs( defaults );
962 if( rc != LDAP_SUCCESS ) {
963 tool_perror( "ldap_sasl_interactive_bind_s",
964 rc, NULL, NULL, NULL, NULL );
965 exit( EXIT_FAILURE );
968 fprintf( stderr, "%s: not compiled with SASL support\n",
970 exit( EXIT_FAILURE );
977 char *matched = NULL;
983 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
984 if ( authmethod == LDAP_AUTH_KRBV4 || authmethod == LDAP_AUTH_KRBV41 ) {
985 msgid = ldap_bind( ld, binddn, passwd.bv_val, authmethod );
987 tool_perror( "ldap_bind", -1, NULL, NULL, NULL, NULL );
988 exit( EXIT_FAILURE );
994 rc = ldap_sasl_bind( ld, binddn, LDAP_SASL_SIMPLE,
995 &passwd, NULL, NULL, &msgid );
997 tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
998 NULL, NULL, NULL, NULL );
999 exit( EXIT_FAILURE );
1003 if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) {
1004 tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
1005 exit( EXIT_FAILURE );
1008 rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
1010 if ( rc != LDAP_SUCCESS ) {
1011 tool_perror( "ldap_bind parse result", rc, NULL, NULL, NULL, NULL );
1012 exit( EXIT_FAILURE );
1015 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1016 if ( ctrls && ppolicy ) {
1018 int expire, grace, len = 0;
1019 LDAPPasswordPolicyError pErr = -1;
1021 ctrl = ldap_find_control( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
1024 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
1025 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1027 if ( pErr != PP_noError ){
1030 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1031 len = strlen( msgbuf );
1033 if ( expire >= 0 ) {
1034 sprintf( msgbuf+len,
1035 " (Password expires in %d seconds)",
1037 } else if ( grace >= 0 ) {
1038 sprintf( msgbuf+len,
1039 " (Password expired, %d grace logins remain)",
1047 ldap_controls_free( ctrls );
1050 if ( err != LDAP_SUCCESS
1052 || ( matched && matched[ 0 ] )
1053 || ( info && info[ 0 ] )
1056 tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1058 if( matched ) ber_memfree( matched );
1059 if( info ) ber_memfree( info );
1060 if( refs ) ber_memvfree( (void **)refs );
1062 if ( err != LDAP_SUCCESS ) exit( EXIT_FAILURE );
1068 tool_unbind( LDAP *ld )
1070 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1072 if ( err != LDAP_OPT_SUCCESS ) {
1073 fprintf( stderr, "Could not unset controls\n");
1076 (void) ldap_unbind_ext( ld, NULL, NULL );
1080 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1082 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1084 int i = 0, j, crit = 0, err;
1085 LDAPControl c[10], **ctrls;
1087 ctrls = (LDAPControl**) malloc(sizeof(c) + (count+1)*sizeof(LDAPControl*));
1088 if ( ctrls == NULL ) {
1089 fprintf( stderr, "No memory\n" );
1090 exit( EXIT_FAILURE );
1094 BerElementBuffer berbuf;
1095 BerElement *ber = (BerElement *)&berbuf;
1097 if( assertion == NULL || *assertion == '\0' ) {
1098 fprintf( stderr, "Assertion=<empty>\n" );
1099 exit( EXIT_FAILURE );
1102 ber_init2( ber, NULL, LBER_USE_DER );
1104 err = ldap_pvt_put_filter( ber, assertion );
1106 fprintf( stderr, "assertion encode failed (%d)\n", err );
1107 exit( EXIT_FAILURE );
1110 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1112 fprintf( stderr, "assertion flatten failed (%d)\n", err );
1113 exit( EXIT_FAILURE );
1116 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1117 c[i].ldctl_iscritical = assertctl > 1;
1123 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1124 c[i].ldctl_value.bv_val = authzid;
1125 c[i].ldctl_value.bv_len = strlen( authzid );
1126 c[i].ldctl_iscritical = 1;
1132 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1133 BER_BVZERO( &c[i].ldctl_value );
1134 c[i].ldctl_iscritical = manageDIT > 1;
1139 if ( manageDSAit ) {
1140 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1141 BER_BVZERO( &c[i].ldctl_value );
1142 c[i].ldctl_iscritical = manageDSAit > 1;
1148 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1149 BER_BVZERO( &c[i].ldctl_value );
1150 c[i].ldctl_iscritical = noop > 1;
1156 char berbuf[LBER_ELEMENT_SIZEOF];
1157 BerElement *ber = (BerElement *)berbuf;
1158 char **attrs = NULL;
1160 if( preread_attrs ) {
1161 attrs = ldap_str2charray( preread_attrs, "," );
1164 ber_init2( ber, NULL, LBER_USE_DER );
1166 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1167 fprintf( stderr, "preread attrs encode failed.\n" );
1168 exit( EXIT_FAILURE );
1171 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1173 fprintf( stderr, "preread flatten failed (%d)\n", err );
1174 exit( EXIT_FAILURE );
1177 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1178 c[i].ldctl_iscritical = preread > 1;
1182 if( attrs ) ldap_charray_free( attrs );
1186 char berbuf[LBER_ELEMENT_SIZEOF];
1187 BerElement *ber = (BerElement *)berbuf;
1188 char **attrs = NULL;
1190 if( postread_attrs ) {
1191 attrs = ldap_str2charray( postread_attrs, "," );
1194 ber_init2( ber, NULL, LBER_USE_DER );
1196 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1197 fprintf( stderr, "postread attrs encode failed.\n" );
1198 exit( EXIT_FAILURE );
1201 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1203 fprintf( stderr, "postread flatten failed (%d)\n", err );
1204 exit( EXIT_FAILURE );
1207 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1208 c[i].ldctl_iscritical = postread > 1;
1212 if( attrs ) ldap_charray_free( attrs );
1215 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1217 if ( chainingResolve > -1 ) {
1218 BerElementBuffer berbuf;
1219 BerElement *ber = (BerElement *)&berbuf;
1221 ber_init2( ber, NULL, LBER_USE_DER );
1223 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1226 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1227 exit( EXIT_FAILURE );
1230 if ( chainingContinuation > -1 ) {
1231 err = ber_printf( ber, "e", chainingContinuation );
1234 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1235 exit( EXIT_FAILURE );
1239 err = ber_printf( ber, /* { */ "N}" );
1242 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1243 exit( EXIT_FAILURE );
1246 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1247 exit( EXIT_FAILURE );
1251 BER_BVZERO( &c[i].ldctl_value );
1254 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1255 c[i].ldctl_iscritical = chaining > 1;
1259 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1262 ctrls[i++] = extra_c++;
1266 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1268 if ( err != LDAP_OPT_SUCCESS ) {
1269 for ( j = 0; j < i; j++ ) {
1270 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1272 fprintf( stderr, "Could not set %scontrols\n",
1273 crit ? "critical " : "" );
1278 exit( EXIT_FAILURE );
1283 tool_check_abandon( LDAP *ld, int msgid )
1287 switch ( gotintr ) {
1288 case LDAP_REQ_EXTENDED:
1289 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1290 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1291 rc, ldap_err2string( rc ) );
1294 case LDAP_REQ_ABANDON:
1295 rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
1296 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1297 rc, ldap_err2string( rc ) );