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-2006 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"
59 char *ldaphost = NULL;
61 #ifdef HAVE_CYRUS_SASL
62 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
63 char *sasl_realm = NULL;
64 char *sasl_authc_id = NULL;
65 char *sasl_authz_id = NULL;
66 char *sasl_mech = NULL;
67 char *sasl_secprops = NULL;
72 char *assertion = NULL;
79 char *preread_attrs = NULL;
81 char *postread_attrs = NULL;
85 struct berval passwd = { 0, NULL };
92 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
94 static int chainingResolve = -1;
95 static int chainingContinuation = -1;
96 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
113 ldap_pvt_setlocale(LC_MESSAGES, "");
114 ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
115 ldap_pvt_textdomain(OPENLDAP_PACKAGE);
121 #ifdef HAVE_CYRUS_SASL
125 ldap_pvt_tls_destroy();
130 tool_common_usage( void )
132 static const char *const descriptions[] = {
133 N_(" -c continuous operation mode (do not stop on errors)\n"),
134 N_(" -C chase referrals (anonymously)\n"),
135 N_(" -d level set LDAP debugging level to `level'\n"),
136 N_(" -D binddn bind DN\n"),
137 N_(" -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
138 N_(" [!]assert=<filter> (an RFC 2254 Filter)\n")
139 N_(" [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n")
140 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
141 N_(" [!]chaining[=<resolveBehavior>[/<continuationBehavior>]]\n")
142 N_(" one of \"chainingPreferred\", \"chainingRequired\",\n")
143 N_(" \"referralsPreferred\", \"referralsRequired\"\n")
144 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
146 N_(" [!]manageDIT\n")
148 N_(" [!]manageDSAit\n")
150 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
153 N_(" [!]postread[=<attrs>] (a comma-separated attribute list)\n")
154 N_(" [!]preread[=<attrs>] (a comma-separated attribute list)\n"),
155 N_(" abandon, cancel (SIGINT sends abandon/cancel; not really controls)\n")
156 N_(" -f file read operations from `file'\n"),
157 N_(" -h host LDAP server\n"),
158 N_(" -H URI LDAP Uniform Resource Indentifier(s)\n"),
159 N_(" -I use SASL Interactive mode\n"),
160 N_(" -k use Kerberos authentication\n"),
161 N_(" -K like -k, but do only step 1 of the Kerberos bind\n"),
162 N_(" -M enable Manage DSA IT control (-MM to make critical)\n"),
163 N_(" -n show what would be done but don't actually do it\n"),
164 N_(" -O props SASL security properties\n"),
165 N_(" -p port port on LDAP server\n"),
166 N_(" -P version procotol version (default: 3)\n"),
167 N_(" -Q use SASL Quiet mode\n"),
168 N_(" -R realm SASL realm\n"),
169 N_(" -U authcid SASL authentication identity\n"),
170 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
171 N_(" -V print version info (-VV only)\n"),
172 N_(" -w passwd bind password (for simple authentication)\n"),
173 N_(" -W prompt for bind password\n"),
174 N_(" -x Simple authentication\n"),
175 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
176 N_(" -y file Read password from file\n"),
177 N_(" -Y mech SASL mechanism\n"),
178 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
181 const char *const *cpp;
183 fputs( _("Common options:\n"), stderr );
184 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
185 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
186 fputs( _(*cpp), stderr );
199 fprintf( stderr, "%s: %s (%d)%s\n",
200 func, ldap_err2string( err ), err, extra ? extra : "" );
202 if ( matched && *matched ) {
203 fprintf( stderr, _("\tmatched DN: %s\n"), matched );
206 if ( info && *info ) {
207 fprintf( stderr, _("\tadditional info: %s\n"), info );
210 if ( refs && *refs ) {
212 fprintf( stderr, _("\treferrals:\n") );
213 for( i=0; refs[i]; i++ ) {
214 fprintf( stderr, "\t\t%s\n", refs[i] );
221 tool_args( int argc, char **argv )
225 while (( i = getopt( argc, argv, options )) != EOF ) {
227 char *control, *cvalue, *next;
229 case 'c': /* continuous operation mode */
236 ival = strtol( optarg, &next, 10 );
237 if (next == NULL || next[0] != '\0') {
238 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
243 case 'D': /* bind DN */
244 if( binddn != NULL ) {
245 fprintf( stderr, "%s: -D previously specified\n", prog );
246 exit( EXIT_FAILURE );
248 binddn = ber_strdup( optarg );
250 case 'e': /* general extensions (controls and such) */
251 /* should be extended to support comma separated list of
252 * [!]key[=value] parameters, e.g. -e !foo,bar=567
257 if( optarg[0] == '!' ) {
262 control = ber_strdup( optarg );
263 if ( (cvalue = strchr( control, '=' )) != NULL ) {
267 if ( strcasecmp( control, "assert" ) == 0 ) {
269 fprintf( stderr, "assert control previously specified\n");
270 exit( EXIT_FAILURE );
272 if( cvalue == NULL ) {
273 fprintf( stderr, "assert: control value expected\n" );
277 assertctl = 1 + crit;
279 assert( assertion == NULL );
282 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
283 if( authzid != NULL ) {
284 fprintf( stderr, "authzid control previously specified\n");
285 exit( EXIT_FAILURE );
287 if( cvalue == NULL ) {
288 fprintf( stderr, "authzid: control value expected\n" );
292 fprintf( stderr, "authzid: must be marked critical\n" );
296 assert( authzid == NULL );
299 } else if ( strcasecmp( control, "manageDIT" ) == 0 ) {
302 "manageDIT control previously specified\n");
303 exit( EXIT_FAILURE );
305 if( cvalue != NULL ) {
307 "manageDIT: no control value expected\n" );
311 manageDIT = 1 + crit;
313 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
316 "manageDSAit control previously specified\n");
317 exit( EXIT_FAILURE );
319 if( cvalue != NULL ) {
321 "manageDSAit: no control value expected\n" );
325 manageDSAit = 1 + crit;
327 } else if ( strcasecmp( control, "noop" ) == 0 ) {
329 fprintf( stderr, "noop control previously specified\n");
330 exit( EXIT_FAILURE );
332 if( cvalue != NULL ) {
333 fprintf( stderr, "noop: no control value expected\n" );
339 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
340 } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
342 fprintf( stderr, "ppolicy control previously specified\n");
343 exit( EXIT_FAILURE );
345 if( cvalue != NULL ) {
346 fprintf( stderr, "ppolicy: no control value expected\n" );
350 fprintf( stderr, "ppolicy: critical flag not allowed\n" );
357 } else if ( strcasecmp( control, "preread" ) == 0 ) {
359 fprintf( stderr, "preread control previously specified\n");
360 exit( EXIT_FAILURE );
364 preread_attrs = cvalue;
366 } else if ( strcasecmp( control, "postread" ) == 0 ) {
368 fprintf( stderr, "postread control previously specified\n");
369 exit( EXIT_FAILURE );
373 postread_attrs = cvalue;
375 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
376 } else if ( strcasecmp( control, "chaining" ) == 0 ) {
379 if ( cvalue != NULL ) {
382 continuation = strchr( cvalue, '/' );
383 if ( continuation ) {
384 /* FIXME: this makes sense only in searches */
385 *continuation++ = '\0';
386 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
387 chainingContinuation = LDAP_CHAINING_PREFERRED;
388 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
389 chainingContinuation = LDAP_CHAINING_REQUIRED;
390 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
391 chainingContinuation = LDAP_REFERRALS_PREFERRED;
392 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
393 chainingContinuation = LDAP_REFERRALS_REQUIRED;
396 "chaining behavior control "
397 "continuation value \"%s\" invalid\n",
399 exit( EXIT_FAILURE );
403 if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
404 chainingResolve = LDAP_CHAINING_PREFERRED;
405 } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
406 chainingResolve = LDAP_CHAINING_REQUIRED;
407 } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
408 chainingResolve = LDAP_REFERRALS_PREFERRED;
409 } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
410 chainingResolve = LDAP_REFERRALS_REQUIRED;
413 "chaining behavior control "
414 "resolve value \"%s\" invalid\n",
416 exit( EXIT_FAILURE );
419 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
421 /* this shouldn't go here, really; but it's a feature... */
422 } else if ( strcasecmp( control, "abandon" ) == 0 ) {
423 abcan = LDAP_REQ_ABANDON;
425 } else if ( strcasecmp( control, "cancel" ) == 0 ) {
426 abcan = LDAP_REQ_EXTENDED;
429 fprintf( stderr, "Invalid general control name: %s\n",
434 case 'f': /* read from file */
435 if( infile != NULL ) {
436 fprintf( stderr, "%s: -f previously specified\n", prog );
437 exit( EXIT_FAILURE );
439 infile = ber_strdup( optarg );
441 case 'h': /* ldap host */
442 if( ldaphost != NULL ) {
443 fprintf( stderr, "%s: -h previously specified\n", prog );
444 exit( EXIT_FAILURE );
446 ldaphost = ber_strdup( optarg );
448 case 'H': /* ldap URI */
449 if( ldapuri != NULL ) {
450 fprintf( stderr, "%s: -H previously specified\n", prog );
451 exit( EXIT_FAILURE );
453 ldapuri = ber_strdup( optarg );
456 #ifdef HAVE_CYRUS_SASL
457 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
458 fprintf( stderr, "%s: incompatible previous "
459 "authentication choice\n",
461 exit( EXIT_FAILURE );
463 authmethod = LDAP_AUTH_SASL;
464 sasl_flags = LDAP_SASL_INTERACTIVE;
467 fprintf( stderr, "%s: was not compiled with SASL support\n",
469 exit( EXIT_FAILURE );
471 case 'k': /* kerberos bind */
472 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
473 if( authmethod != -1 ) {
474 fprintf( stderr, "%s: -k incompatible with previous "
475 "authentication choice\n", prog );
476 exit( EXIT_FAILURE );
478 authmethod = LDAP_AUTH_KRBV4;
480 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
481 exit( EXIT_FAILURE );
484 case 'K': /* kerberos bind, part one only */
485 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
486 if( authmethod != -1 ) {
487 fprintf( stderr, "%s: incompatible with previous "
488 "authentication choice\n", prog );
489 exit( EXIT_FAILURE );
491 authmethod = LDAP_AUTH_KRBV41;
493 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
494 exit( EXIT_FAILURE );
498 /* enable Manage DSA IT */
501 case 'n': /* print operations, don't actually do them */
505 #ifdef HAVE_CYRUS_SASL
506 if( sasl_secprops != NULL ) {
507 fprintf( stderr, "%s: -O previously specified\n", prog );
508 exit( EXIT_FAILURE );
510 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
511 fprintf( stderr, "%s: incompatible previous "
512 "authentication choice\n", prog );
513 exit( EXIT_FAILURE );
515 authmethod = LDAP_AUTH_SASL;
516 sasl_secprops = ber_strdup( optarg );
518 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
519 exit( EXIT_FAILURE );
524 fprintf( stderr, "%s: -p previously specified\n", prog );
525 exit( EXIT_FAILURE );
527 ival = strtol( optarg, &next, 10 );
528 if ( next == NULL || next[0] != '\0' ) {
529 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
530 exit( EXIT_FAILURE );
535 ival = strtol( optarg, &next, 10 );
536 if ( next == NULL || next[0] != '\0' ) {
537 fprintf( stderr, "%s: unabel to parse protocol version \"%s\"\n", prog, optarg );
538 exit( EXIT_FAILURE );
542 if( protocol == LDAP_VERSION3 ) {
543 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
545 exit( EXIT_FAILURE );
547 protocol = LDAP_VERSION2;
550 if( protocol == LDAP_VERSION2 ) {
551 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
553 exit( EXIT_FAILURE );
555 protocol = LDAP_VERSION3;
558 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
564 #ifdef HAVE_CYRUS_SASL
565 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
566 fprintf( stderr, "%s: incompatible previous "
567 "authentication choice\n",
569 exit( EXIT_FAILURE );
571 authmethod = LDAP_AUTH_SASL;
572 sasl_flags = LDAP_SASL_QUIET;
575 fprintf( stderr, "%s: not compiled with SASL support\n",
577 exit( EXIT_FAILURE );
580 #ifdef HAVE_CYRUS_SASL
581 if( sasl_realm != NULL ) {
582 fprintf( stderr, "%s: -R previously specified\n", prog );
583 exit( EXIT_FAILURE );
585 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
586 fprintf( stderr, "%s: incompatible previous "
587 "authentication choice\n",
589 exit( EXIT_FAILURE );
591 authmethod = LDAP_AUTH_SASL;
592 sasl_realm = ber_strdup( optarg );
594 fprintf( stderr, "%s: not compiled with SASL support\n",
596 exit( EXIT_FAILURE );
600 #ifdef HAVE_CYRUS_SASL
601 if( sasl_authc_id != NULL ) {
602 fprintf( stderr, "%s: -U previously specified\n", prog );
603 exit( EXIT_FAILURE );
605 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
606 fprintf( stderr, "%s: incompatible previous "
607 "authentication choice\n",
609 exit( EXIT_FAILURE );
611 authmethod = LDAP_AUTH_SASL;
612 sasl_authc_id = ber_strdup( optarg );
614 fprintf( stderr, "%s: not compiled with SASL support\n",
616 exit( EXIT_FAILURE );
619 case 'v': /* verbose mode */
622 case 'V': /* version */
625 case 'w': /* password */
626 passwd.bv_val = ber_strdup( optarg );
630 for( p = optarg; *p != '\0'; p++ ) {
634 passwd.bv_len = strlen( passwd.bv_val );
643 #ifdef HAVE_CYRUS_SASL
644 if( sasl_mech != NULL ) {
645 fprintf( stderr, "%s: -Y previously specified\n", prog );
646 exit( EXIT_FAILURE );
648 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
650 "%s: incompatible with authentication choice\n", prog );
651 exit( EXIT_FAILURE );
653 authmethod = LDAP_AUTH_SASL;
654 sasl_mech = ber_strdup( optarg );
656 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
657 exit( EXIT_FAILURE );
661 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
662 fprintf( stderr, "%s: incompatible with previous "
663 "authentication choice\n", prog );
664 exit( EXIT_FAILURE );
666 authmethod = LDAP_AUTH_SIMPLE;
669 #ifdef HAVE_CYRUS_SASL
670 if( sasl_authz_id != NULL ) {
671 fprintf( stderr, "%s: -X previously specified\n", prog );
672 exit( EXIT_FAILURE );
674 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
675 fprintf( stderr, "%s: -X incompatible with "
676 "authentication choice\n", prog );
677 exit( EXIT_FAILURE );
679 authmethod = LDAP_AUTH_SASL;
680 sasl_authz_id = ber_strdup( optarg );
682 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
683 exit( EXIT_FAILURE );
690 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
691 exit( EXIT_FAILURE );
695 if( handle_private_option( i ) ) break;
696 fprintf( stderr, "%s: unrecognized option -%c\n",
703 /* prevent bad linking */
705 api.ldapai_info_version = LDAP_API_INFO_VERSION;
707 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
708 != LDAP_OPT_SUCCESS )
710 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
711 exit( EXIT_FAILURE );
714 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
715 fprintf( stderr, "LDAP APIInfo version mismatch: "
716 "library %d, header %d\n",
717 api.ldapai_info_version, LDAP_API_INFO_VERSION );
718 exit( EXIT_FAILURE );
721 if( api.ldapai_api_version != LDAP_API_VERSION ) {
722 fprintf( stderr, "LDAP API version mismatch: "
723 "library %d, header %d\n",
724 api.ldapai_api_version, LDAP_API_VERSION );
725 exit( EXIT_FAILURE );
728 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
729 fprintf( stderr, "LDAP vendor name mismatch: "
730 "library %s, header %s\n",
731 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
732 exit( EXIT_FAILURE );
735 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
736 fprintf( stderr, "LDAP vendor version mismatch: "
737 "library %d, header %d\n",
738 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
739 exit( EXIT_FAILURE );
743 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
745 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
746 if (version > 1) exit( EXIT_SUCCESS );
749 ldap_memfree( api.ldapai_vendor_name );
750 ber_memvfree( (void **)api.ldapai_extensions );
754 protocol = LDAP_VERSION3;
756 if (authmethod == -1 && protocol > LDAP_VERSION2) {
757 #ifdef HAVE_CYRUS_SASL
758 authmethod = LDAP_AUTH_SASL;
760 authmethod = LDAP_AUTH_SIMPLE;
764 if( ldapuri == NULL ) {
765 if( ldapport && ( ldaphost == NULL )) {
766 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
767 exit( EXIT_FAILURE );
770 if( ldaphost != NULL ) {
771 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
772 exit( EXIT_FAILURE );
775 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
776 exit( EXIT_FAILURE );
779 if( protocol == LDAP_VERSION2 ) {
780 if( assertctl || authzid || manageDIT || manageDSAit ||
781 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
784 noop || ppolicy || preread || postread )
786 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
787 exit( EXIT_FAILURE );
791 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
792 exit( EXIT_FAILURE );
795 #ifdef HAVE_CYRUS_SASL
796 if( authmethod == LDAP_AUTH_SASL ) {
797 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
799 exit( EXIT_FAILURE );
803 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
804 if ( authmethod == LDAP_AUTH_KRBV4 || authmethod == LDAP_AUTH_KRBV41 ) {
805 fprintf( stderr, "%s: -k/-K incompatible with LDAPv%d\n",
807 exit( EXIT_FAILURE );
815 tool_conn_setup( int not, void (*private_setup)( LDAP * ) )
820 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
821 != LBER_OPT_SUCCESS )
824 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
826 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
827 != LDAP_OPT_SUCCESS )
830 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
835 (void) SIGNAL( SIGPIPE, SIG_IGN );
839 SIGNAL( SIGINT, do_sig );
845 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
848 memset( &url, 0, sizeof(url));
850 url.lud_scheme = "ldap";
851 url.lud_host = ldaphost;
852 url.lud_port = ldapport;
853 url.lud_scope = LDAP_SCOPE_DEFAULT;
855 ldapuri = ldap_url_desc2str( &url );
859 fprintf( stderr, "ldap_initialize( %s )\n",
860 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
862 rc = ldap_initialize( &ld, ldapuri );
863 if( rc != LDAP_SUCCESS ) {
865 "Could not create LDAP session handle for URI=%s (%d): %s\n",
866 ldapuri, rc, ldap_err2string(rc) );
867 exit( EXIT_FAILURE );
870 if( private_setup ) private_setup( ld );
873 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
874 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
876 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
877 referrals ? "on" : "off" );
878 exit( EXIT_FAILURE );
881 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
882 != LDAP_OPT_SUCCESS )
884 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
886 exit( EXIT_FAILURE );
890 ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ))
892 ldap_perror( ld, "ldap_start_tls" );
894 exit( EXIT_FAILURE );
904 tool_bind( LDAP *ld )
906 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
908 LDAPControl *ctrls[2], c;
909 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
910 c.ldctl_value.bv_val = NULL;
911 c.ldctl_value.bv_len = 0;
912 c.ldctl_iscritical = 0;
915 ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
919 if ( authmethod == LDAP_AUTH_SASL ) {
920 #ifdef HAVE_CYRUS_SASL
924 if( sasl_secprops != NULL ) {
925 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
926 (void *) sasl_secprops );
928 if( rc != LDAP_OPT_SUCCESS ) {
930 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
932 exit( EXIT_FAILURE );
936 defaults = lutil_sasl_defaults( ld,
943 rc = ldap_sasl_interactive_bind_s( ld, binddn,
944 sasl_mech, NULL, NULL,
945 sasl_flags, lutil_sasl_interact, defaults );
947 lutil_sasl_freedefs( defaults );
948 if( rc != LDAP_SUCCESS ) {
949 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
950 exit( EXIT_FAILURE );
953 fprintf( stderr, "%s: not compiled with SASL support\n",
955 exit( EXIT_FAILURE );
962 char *matched = NULL;
968 msgid = ldap_bind( ld, binddn, passwd.bv_val, authmethod );
970 ldap_perror( ld, "ldap_bind" );
971 exit( EXIT_FAILURE );
974 if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) {
975 ldap_perror( ld, "ldap_result" );
976 exit( EXIT_FAILURE );
979 if ( ldap_parse_result( ld, result, &err, &matched, &info, &refs,
980 &ctrls, 1 ) != LDAP_SUCCESS )
982 ldap_perror( ld, "ldap_bind parse result" );
983 exit( EXIT_FAILURE );
986 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
987 if ( ctrls && ppolicy ) {
989 int expire, grace, len = 0;
990 LDAPPasswordPolicyError pErr = -1;
992 ctrl = ldap_find_control( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
995 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
996 &expire, &grace, &pErr ) == LDAP_SUCCESS )
998 if ( pErr != PP_noError ){
1001 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1002 len = strlen( msgbuf );
1004 if ( expire >= 0 ) {
1005 sprintf( msgbuf+len,
1006 " (Password expires in %d seconds)",
1008 } else if ( grace >= 0 ) {
1009 sprintf( msgbuf+len,
1010 " (Password expired, %d grace logins remain)",
1018 ldap_controls_free( ctrls );
1021 if ( err != LDAP_SUCCESS
1023 || ( matched && matched[ 0 ] )
1024 || ( info && info[ 0 ] )
1027 tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1029 if( matched ) ber_memfree( matched );
1030 if( info ) ber_memfree( info );
1031 if( refs ) ber_memvfree( (void **)refs );
1033 if ( err != LDAP_SUCCESS ) exit( EXIT_FAILURE );
1039 tool_unbind( LDAP *ld )
1041 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1043 if ( err != LDAP_OPT_SUCCESS ) {
1044 fprintf( stderr, "Could not unset controls\n");
1047 (void) ldap_unbind_ext( ld, NULL, NULL );
1051 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1053 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1055 int i = 0, j, crit = 0, err;
1056 LDAPControl c[10], **ctrls;
1058 ctrls = (LDAPControl**) malloc(sizeof(c) + (count+1)*sizeof(LDAPControl*));
1059 if ( ctrls == NULL ) {
1060 fprintf( stderr, "No memory\n" );
1061 exit( EXIT_FAILURE );
1065 BerElementBuffer berbuf;
1066 BerElement *ber = (BerElement *)&berbuf;
1068 if( assertion == NULL || *assertion == '\0' ) {
1069 fprintf( stderr, "Assertion=<empty>\n" );
1070 exit( EXIT_FAILURE );
1073 ber_init2( ber, NULL, LBER_USE_DER );
1075 err = ldap_pvt_put_filter( ber, assertion );
1077 fprintf( stderr, "assertion encode failed (%d)\n", err );
1078 exit( EXIT_FAILURE );
1081 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1083 fprintf( stderr, "assertion flatten failed (%d)\n", err );
1084 exit( EXIT_FAILURE );
1087 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1088 c[i].ldctl_iscritical = assertctl > 1;
1094 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1095 c[i].ldctl_value.bv_val = authzid;
1096 c[i].ldctl_value.bv_len = strlen( authzid );
1097 c[i].ldctl_iscritical = 1;
1103 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1104 BER_BVZERO( &c[i].ldctl_value );
1105 c[i].ldctl_iscritical = manageDIT > 1;
1110 if ( manageDSAit ) {
1111 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1112 BER_BVZERO( &c[i].ldctl_value );
1113 c[i].ldctl_iscritical = manageDSAit > 1;
1119 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1120 BER_BVZERO( &c[i].ldctl_value );
1121 c[i].ldctl_iscritical = noop > 1;
1127 char berbuf[LBER_ELEMENT_SIZEOF];
1128 BerElement *ber = (BerElement *)berbuf;
1129 char **attrs = NULL;
1131 if( preread_attrs ) {
1132 attrs = ldap_str2charray( preread_attrs, "," );
1135 ber_init2( ber, NULL, LBER_USE_DER );
1137 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1138 fprintf( stderr, "preread attrs encode failed.\n" );
1139 exit( EXIT_FAILURE );
1142 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1144 fprintf( stderr, "preread flatten failed (%d)\n", err );
1145 exit( EXIT_FAILURE );
1148 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1149 c[i].ldctl_iscritical = preread > 1;
1153 if( attrs ) ldap_charray_free( attrs );
1157 char berbuf[LBER_ELEMENT_SIZEOF];
1158 BerElement *ber = (BerElement *)berbuf;
1159 char **attrs = NULL;
1161 if( postread_attrs ) {
1162 attrs = ldap_str2charray( postread_attrs, "," );
1165 ber_init2( ber, NULL, LBER_USE_DER );
1167 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1168 fprintf( stderr, "postread attrs encode failed.\n" );
1169 exit( EXIT_FAILURE );
1172 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1174 fprintf( stderr, "postread flatten failed (%d)\n", err );
1175 exit( EXIT_FAILURE );
1178 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1179 c[i].ldctl_iscritical = postread > 1;
1183 if( attrs ) ldap_charray_free( attrs );
1186 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1188 if ( chainingResolve > -1 ) {
1189 BerElementBuffer berbuf;
1190 BerElement *ber = (BerElement *)&berbuf;
1192 ber_init2( ber, NULL, LBER_USE_DER );
1194 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1197 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1198 exit( EXIT_FAILURE );
1201 if ( chainingContinuation > -1 ) {
1202 err = ber_printf( ber, "e", chainingContinuation );
1205 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1206 exit( EXIT_FAILURE );
1210 err = ber_printf( ber, /* { */ "N}" );
1213 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1214 exit( EXIT_FAILURE );
1217 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1218 exit( EXIT_FAILURE );
1222 BER_BVZERO( &c[i].ldctl_value );
1225 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1226 c[i].ldctl_iscritical = chaining > 1;
1230 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1233 ctrls[i++] = extra_c++;
1237 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1239 if ( err != LDAP_OPT_SUCCESS ) {
1240 for ( j = 0; j < i; j++ ) {
1241 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1243 fprintf( stderr, "Could not set %scontrols\n",
1244 crit ? "critical " : "" );
1249 exit( EXIT_FAILURE );
1254 tool_check_abandon( LDAP *ld, int msgid )
1258 switch ( gotintr ) {
1259 case LDAP_REQ_EXTENDED:
1260 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1261 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1262 rc, ldap_err2string( rc ) );
1265 case LDAP_REQ_ABANDON:
1266 rc = ldap_abandon( ld, msgid );
1267 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1268 rc, ldap_err2string( rc ) );