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"
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 );
193 tool_args( int argc, char **argv )
197 while (( i = getopt( argc, argv, options )) != EOF ) {
199 char *control, *cvalue, *next;
201 case 'c': /* continuous operation mode */
208 ival = strtol( optarg, &next, 10 );
209 if (next == NULL || next[0] != '\0') {
210 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
215 case 'D': /* bind DN */
216 if( binddn != NULL ) {
217 fprintf( stderr, "%s: -D previously specified\n", prog );
218 exit( EXIT_FAILURE );
220 binddn = ber_strdup( optarg );
222 case 'e': /* general extensions (controls and such) */
223 /* should be extended to support comma separated list of
224 * [!]key[=value] parameters, e.g. -e !foo,bar=567
229 if( optarg[0] == '!' ) {
234 control = ber_strdup( optarg );
235 if ( (cvalue = strchr( control, '=' )) != NULL ) {
239 if ( strcasecmp( control, "assert" ) == 0 ) {
241 fprintf( stderr, "assert control previously specified\n");
242 exit( EXIT_FAILURE );
244 if( cvalue == NULL ) {
245 fprintf( stderr, "assert: control value expected\n" );
249 assertctl = 1 + crit;
251 assert( assertion == NULL );
254 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
255 if( authzid != NULL ) {
256 fprintf( stderr, "authzid control previously specified\n");
257 exit( EXIT_FAILURE );
259 if( cvalue == NULL ) {
260 fprintf( stderr, "authzid: control value expected\n" );
264 fprintf( stderr, "authzid: must be marked critical\n" );
268 assert( authzid == NULL );
271 } else if ( strcasecmp( control, "manageDIT" ) == 0 ) {
274 "manageDIT control previously specified\n");
275 exit( EXIT_FAILURE );
277 if( cvalue != NULL ) {
279 "manageDIT: no control value expected\n" );
283 manageDIT = 1 + crit;
285 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
288 "manageDSAit control previously specified\n");
289 exit( EXIT_FAILURE );
291 if( cvalue != NULL ) {
293 "manageDSAit: no control value expected\n" );
297 manageDSAit = 1 + crit;
299 } else if ( strcasecmp( control, "noop" ) == 0 ) {
301 fprintf( stderr, "noop control previously specified\n");
302 exit( EXIT_FAILURE );
304 if( cvalue != NULL ) {
305 fprintf( stderr, "noop: no control value expected\n" );
311 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
312 } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
314 fprintf( stderr, "ppolicy control previously specified\n");
315 exit( EXIT_FAILURE );
317 if( cvalue != NULL ) {
318 fprintf( stderr, "ppolicy: no control value expected\n" );
322 fprintf( stderr, "ppolicy: critical flag not allowed\n" );
329 } else if ( strcasecmp( control, "preread" ) == 0 ) {
331 fprintf( stderr, "preread control previously specified\n");
332 exit( EXIT_FAILURE );
336 preread_attrs = cvalue;
338 } else if ( strcasecmp( control, "postread" ) == 0 ) {
340 fprintf( stderr, "postread control previously specified\n");
341 exit( EXIT_FAILURE );
345 postread_attrs = cvalue;
347 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
348 } else if ( strcasecmp( control, "chaining" ) == 0 ) {
351 if ( cvalue != NULL ) {
354 continuation = strchr( cvalue, '/' );
355 if ( continuation ) {
356 /* FIXME: this makes sense only in searches */
357 *continuation++ = '\0';
358 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
359 chainingContinuation = LDAP_CHAINING_PREFERRED;
360 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
361 chainingContinuation = LDAP_CHAINING_REQUIRED;
362 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
363 chainingContinuation = LDAP_REFERRALS_PREFERRED;
364 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
365 chainingContinuation = LDAP_REFERRALS_REQUIRED;
368 "chaining behavior control "
369 "continuation value \"%s\" invalid\n",
371 exit( EXIT_FAILURE );
375 if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
376 chainingResolve = LDAP_CHAINING_PREFERRED;
377 } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
378 chainingResolve = LDAP_CHAINING_REQUIRED;
379 } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
380 chainingResolve = LDAP_REFERRALS_PREFERRED;
381 } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
382 chainingResolve = LDAP_REFERRALS_REQUIRED;
385 "chaining behavior control "
386 "resolve value \"%s\" invalid\n",
388 exit( EXIT_FAILURE );
391 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
393 /* this shouldn't go here, really; but it's a feature... */
394 } else if ( strcasecmp( control, "abandon" ) == 0 ) {
395 abcan = LDAP_REQ_ABANDON;
397 } else if ( strcasecmp( control, "cancel" ) == 0 ) {
398 abcan = LDAP_REQ_EXTENDED;
401 fprintf( stderr, "Invalid general control name: %s\n",
406 case 'f': /* read from file */
407 if( infile != NULL ) {
408 fprintf( stderr, "%s: -f previously specified\n", prog );
409 exit( EXIT_FAILURE );
411 infile = ber_strdup( optarg );
413 case 'h': /* ldap host */
414 if( ldaphost != NULL ) {
415 fprintf( stderr, "%s: -h previously specified\n", prog );
416 exit( EXIT_FAILURE );
418 ldaphost = ber_strdup( optarg );
420 case 'H': /* ldap URI */
421 if( ldapuri != NULL ) {
422 fprintf( stderr, "%s: -H previously specified\n", prog );
423 exit( EXIT_FAILURE );
425 ldapuri = ber_strdup( optarg );
428 #ifdef HAVE_CYRUS_SASL
429 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
430 fprintf( stderr, "%s: incompatible previous "
431 "authentication choice\n",
433 exit( EXIT_FAILURE );
435 authmethod = LDAP_AUTH_SASL;
436 sasl_flags = LDAP_SASL_INTERACTIVE;
439 fprintf( stderr, "%s: was not compiled with SASL support\n",
441 exit( EXIT_FAILURE );
443 case 'k': /* kerberos bind */
444 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
445 if( authmethod != -1 ) {
446 fprintf( stderr, "%s: -k incompatible with previous "
447 "authentication choice\n", prog );
448 exit( EXIT_FAILURE );
450 authmethod = LDAP_AUTH_KRBV4;
452 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
453 exit( EXIT_FAILURE );
456 case 'K': /* kerberos bind, part one only */
457 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
458 if( authmethod != -1 ) {
459 fprintf( stderr, "%s: incompatible with previous "
460 "authentication choice\n", prog );
461 exit( EXIT_FAILURE );
463 authmethod = LDAP_AUTH_KRBV41;
465 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
466 exit( EXIT_FAILURE );
470 /* enable Manage DSA IT */
473 case 'n': /* print operations, don't actually do them */
477 #ifdef HAVE_CYRUS_SASL
478 if( sasl_secprops != NULL ) {
479 fprintf( stderr, "%s: -O previously specified\n", prog );
480 exit( EXIT_FAILURE );
482 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
483 fprintf( stderr, "%s: incompatible previous "
484 "authentication choice\n", prog );
485 exit( EXIT_FAILURE );
487 authmethod = LDAP_AUTH_SASL;
488 sasl_secprops = ber_strdup( optarg );
490 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
491 exit( EXIT_FAILURE );
496 fprintf( stderr, "%s: -p previously specified\n", prog );
497 exit( EXIT_FAILURE );
499 ival = strtol( optarg, &next, 10 );
500 if ( next == NULL || next[0] != '\0' ) {
501 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
502 exit( EXIT_FAILURE );
507 ival = strtol( optarg, &next, 10 );
508 if ( next == NULL || next[0] != '\0' ) {
509 fprintf( stderr, "%s: unabel to parse protocol version \"%s\"\n", prog, optarg );
510 exit( EXIT_FAILURE );
514 if( protocol == LDAP_VERSION3 ) {
515 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
517 exit( EXIT_FAILURE );
519 protocol = LDAP_VERSION2;
522 if( protocol == LDAP_VERSION2 ) {
523 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
525 exit( EXIT_FAILURE );
527 protocol = LDAP_VERSION3;
530 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
536 #ifdef HAVE_CYRUS_SASL
537 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
538 fprintf( stderr, "%s: incompatible previous "
539 "authentication choice\n",
541 exit( EXIT_FAILURE );
543 authmethod = LDAP_AUTH_SASL;
544 sasl_flags = LDAP_SASL_QUIET;
547 fprintf( stderr, "%s: not compiled with SASL support\n",
549 exit( EXIT_FAILURE );
552 #ifdef HAVE_CYRUS_SASL
553 if( sasl_realm != NULL ) {
554 fprintf( stderr, "%s: -R previously specified\n", prog );
555 exit( EXIT_FAILURE );
557 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
558 fprintf( stderr, "%s: incompatible previous "
559 "authentication choice\n",
561 exit( EXIT_FAILURE );
563 authmethod = LDAP_AUTH_SASL;
564 sasl_realm = ber_strdup( optarg );
566 fprintf( stderr, "%s: not compiled with SASL support\n",
568 exit( EXIT_FAILURE );
572 #ifdef HAVE_CYRUS_SASL
573 if( sasl_authc_id != NULL ) {
574 fprintf( stderr, "%s: -U previously specified\n", prog );
575 exit( EXIT_FAILURE );
577 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
578 fprintf( stderr, "%s: incompatible previous "
579 "authentication choice\n",
581 exit( EXIT_FAILURE );
583 authmethod = LDAP_AUTH_SASL;
584 sasl_authc_id = ber_strdup( optarg );
586 fprintf( stderr, "%s: not compiled with SASL support\n",
588 exit( EXIT_FAILURE );
591 case 'v': /* verbose mode */
594 case 'V': /* version */
597 case 'w': /* password */
598 passwd.bv_val = ber_strdup( optarg );
602 for( p = optarg; *p != '\0'; p++ ) {
606 passwd.bv_len = strlen( passwd.bv_val );
615 #ifdef HAVE_CYRUS_SASL
616 if( sasl_mech != NULL ) {
617 fprintf( stderr, "%s: -Y previously specified\n", prog );
618 exit( EXIT_FAILURE );
620 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
622 "%s: incompatible with authentication choice\n", prog );
623 exit( EXIT_FAILURE );
625 authmethod = LDAP_AUTH_SASL;
626 sasl_mech = ber_strdup( optarg );
628 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
629 exit( EXIT_FAILURE );
633 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
634 fprintf( stderr, "%s: incompatible with previous "
635 "authentication choice\n", prog );
636 exit( EXIT_FAILURE );
638 authmethod = LDAP_AUTH_SIMPLE;
641 #ifdef HAVE_CYRUS_SASL
642 if( sasl_authz_id != NULL ) {
643 fprintf( stderr, "%s: -X previously specified\n", prog );
644 exit( EXIT_FAILURE );
646 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
647 fprintf( stderr, "%s: -X incompatible with "
648 "authentication choice\n", prog );
649 exit( EXIT_FAILURE );
651 authmethod = LDAP_AUTH_SASL;
652 sasl_authz_id = ber_strdup( optarg );
654 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
655 exit( EXIT_FAILURE );
662 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
663 exit( EXIT_FAILURE );
667 if( handle_private_option( i ) ) break;
668 fprintf( stderr, "%s: unrecognized option -%c\n",
675 /* prevent bad linking */
677 api.ldapai_info_version = LDAP_API_INFO_VERSION;
679 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
680 != LDAP_OPT_SUCCESS )
682 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
683 exit( EXIT_FAILURE );
686 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
687 fprintf( stderr, "LDAP APIInfo version mismatch: "
688 "library %d, header %d\n",
689 api.ldapai_info_version, LDAP_API_INFO_VERSION );
690 exit( EXIT_FAILURE );
693 if( api.ldapai_api_version != LDAP_API_VERSION ) {
694 fprintf( stderr, "LDAP API version mismatch: "
695 "library %d, header %d\n",
696 api.ldapai_api_version, LDAP_API_VERSION );
697 exit( EXIT_FAILURE );
700 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
701 fprintf( stderr, "LDAP vendor name mismatch: "
702 "library %s, header %s\n",
703 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
704 exit( EXIT_FAILURE );
707 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
708 fprintf( stderr, "LDAP vendor version mismatch: "
709 "library %d, header %d\n",
710 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
711 exit( EXIT_FAILURE );
715 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
717 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
718 if (version > 1) exit( EXIT_SUCCESS );
721 ldap_memfree( api.ldapai_vendor_name );
722 ldap_value_free( api.ldapai_extensions );
726 protocol = LDAP_VERSION3;
728 if (authmethod == -1 && protocol > LDAP_VERSION2) {
729 #ifdef HAVE_CYRUS_SASL
730 authmethod = LDAP_AUTH_SASL;
732 authmethod = LDAP_AUTH_SIMPLE;
736 if( ldapuri == NULL ) {
737 if( ldapport && ( ldaphost == NULL )) {
738 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
739 exit( EXIT_FAILURE );
742 if( ldaphost != NULL ) {
743 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
744 exit( EXIT_FAILURE );
747 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
748 exit( EXIT_FAILURE );
751 if( protocol == LDAP_VERSION2 ) {
752 if( assertctl || authzid || manageDIT || manageDSAit ||
753 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
756 noop || ppolicy || preread || postread )
758 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
759 exit( EXIT_FAILURE );
763 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
764 exit( EXIT_FAILURE );
767 #ifdef HAVE_CYRUS_SASL
768 if( authmethod == LDAP_AUTH_SASL ) {
769 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
771 exit( EXIT_FAILURE );
775 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
776 if ( authmethod == LDAP_AUTH_KRBV4 || authmethod == LDAP_AUTH_KRBV41 ) {
777 fprintf( stderr, "%s: -k/-K incompatible with LDAPv%d\n",
779 exit( EXIT_FAILURE );
787 tool_conn_setup( int not, void (*private_setup)( LDAP * ) )
792 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
793 != LBER_OPT_SUCCESS )
796 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
798 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
799 != LDAP_OPT_SUCCESS )
802 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
807 (void) SIGNAL( SIGPIPE, SIG_IGN );
811 SIGNAL( SIGINT, do_sig );
817 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
820 memset( &url, 0, sizeof(url));
822 url.lud_scheme = "ldap";
823 url.lud_host = ldaphost;
824 url.lud_port = ldapport;
825 url.lud_scope = LDAP_SCOPE_DEFAULT;
827 ldapuri = ldap_url_desc2str( &url );
831 fprintf( stderr, "ldap_initialize( %s )\n",
832 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
834 rc = ldap_initialize( &ld, ldapuri );
835 if( rc != LDAP_SUCCESS ) {
837 "Could not create LDAP session handle for URI=%s (%d): %s\n",
838 ldapuri, rc, ldap_err2string(rc) );
839 exit( EXIT_FAILURE );
842 if( private_setup ) private_setup( ld );
845 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
846 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
848 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
849 referrals ? "on" : "off" );
850 exit( EXIT_FAILURE );
853 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
854 != LDAP_OPT_SUCCESS )
856 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
858 exit( EXIT_FAILURE );
862 ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ))
864 ldap_perror( ld, "ldap_start_tls" );
866 exit( EXIT_FAILURE );
876 tool_bind( LDAP *ld )
878 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
880 LDAPControl *ctrls[2], c;
881 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
882 c.ldctl_value.bv_val = NULL;
883 c.ldctl_value.bv_len = 0;
884 c.ldctl_iscritical = 0;
887 ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
891 if ( authmethod == LDAP_AUTH_SASL ) {
892 #ifdef HAVE_CYRUS_SASL
896 if( sasl_secprops != NULL ) {
897 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
898 (void *) sasl_secprops );
900 if( rc != LDAP_OPT_SUCCESS ) {
902 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
904 exit( EXIT_FAILURE );
908 defaults = lutil_sasl_defaults( ld,
915 rc = ldap_sasl_interactive_bind_s( ld, binddn,
916 sasl_mech, NULL, NULL,
917 sasl_flags, lutil_sasl_interact, defaults );
919 lutil_sasl_freedefs( defaults );
920 if( rc != LDAP_SUCCESS ) {
921 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
922 exit( EXIT_FAILURE );
925 fprintf( stderr, "%s: not compiled with SASL support\n",
927 exit( EXIT_FAILURE );
937 if (( msgid = ldap_bind( ld, binddn, passwd.bv_val, authmethod )) == -1 )
939 ldap_perror( ld, "ldap_bind" );
940 exit( EXIT_FAILURE );
943 if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) {
944 ldap_perror( ld, "ldap_result" );
945 exit( EXIT_FAILURE );
948 if ( ldap_parse_result( ld, result, &err, NULL, NULL, NULL,
949 &ctrls, 1 ) != LDAP_SUCCESS ) {
950 ldap_perror( ld, "ldap_bind parse result" );
951 exit( EXIT_FAILURE );
954 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
955 if ( ctrls && ppolicy ) {
957 int expire, grace, len = 0;
958 LDAPPasswordPolicyError pErr = -1;
960 ctrl = ldap_find_control( LDAP_CONTROL_PASSWORDPOLICYRESPONSE, ctrls );
961 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
962 &expire, &grace, &pErr ) == LDAP_SUCCESS ) {
963 if ( pErr != PP_noError ){
966 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
967 len = strlen( msgbuf );
970 sprintf( msgbuf+len, " (Password expires in %d seconds)", expire );
971 } else if ( grace >= 0 ) {
972 sprintf( msgbuf+len, " (Password expired, %d grace logins remain)", grace );
978 ldap_controls_free( ctrls );
980 if ( err != LDAP_SUCCESS || msgbuf[0] ) {
981 fprintf( stderr, "ldap_bind: %s%s\n", ldap_err2string( err ),
983 if ( err != LDAP_SUCCESS ) {
984 exit( EXIT_FAILURE );
991 tool_unbind( LDAP *ld )
993 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
995 if ( err != LDAP_OPT_SUCCESS ) {
996 fprintf( stderr, "Could not unset controls\n");
999 (void) ldap_unbind_ext( ld, NULL, NULL );
1003 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1005 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1007 int i = 0, j, crit = 0, err;
1008 LDAPControl c[10], **ctrls;
1010 ctrls = (LDAPControl**) malloc(sizeof(c) + (count+1)*sizeof(LDAPControl*));
1011 if ( ctrls == NULL ) {
1012 fprintf( stderr, "No memory\n" );
1013 exit( EXIT_FAILURE );
1017 BerElementBuffer berbuf;
1018 BerElement *ber = (BerElement *)&berbuf;
1020 if( assertion == NULL || *assertion == '\0' ) {
1021 fprintf( stderr, "Assertion=<empty>\n" );
1022 exit( EXIT_FAILURE );
1025 ber_init2( ber, NULL, LBER_USE_DER );
1027 err = ldap_pvt_put_filter( ber, assertion );
1029 fprintf( stderr, "assertion encode failed (%d)\n", err );
1030 exit( EXIT_FAILURE );
1033 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1035 fprintf( stderr, "assertion flatten failed (%d)\n", err );
1036 exit( EXIT_FAILURE );
1039 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1040 c[i].ldctl_iscritical = assertctl > 1;
1046 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1047 c[i].ldctl_value.bv_val = authzid;
1048 c[i].ldctl_value.bv_len = strlen( authzid );
1049 c[i].ldctl_iscritical = 1;
1055 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1056 BER_BVZERO( &c[i].ldctl_value );
1057 c[i].ldctl_iscritical = manageDIT > 1;
1062 if ( manageDSAit ) {
1063 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1064 BER_BVZERO( &c[i].ldctl_value );
1065 c[i].ldctl_iscritical = manageDSAit > 1;
1071 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1072 BER_BVZERO( &c[i].ldctl_value );
1073 c[i].ldctl_iscritical = noop > 1;
1079 char berbuf[LBER_ELEMENT_SIZEOF];
1080 BerElement *ber = (BerElement *)berbuf;
1081 char **attrs = NULL;
1083 if( preread_attrs ) {
1084 attrs = ldap_str2charray( preread_attrs, "," );
1087 ber_init2( ber, NULL, LBER_USE_DER );
1089 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1090 fprintf( stderr, "preread attrs encode failed.\n" );
1091 exit( EXIT_FAILURE );
1094 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1096 fprintf( stderr, "preread flatten failed (%d)\n", err );
1097 exit( EXIT_FAILURE );
1100 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1101 c[i].ldctl_iscritical = preread > 1;
1105 if( attrs ) ldap_charray_free( attrs );
1109 char berbuf[LBER_ELEMENT_SIZEOF];
1110 BerElement *ber = (BerElement *)berbuf;
1111 char **attrs = NULL;
1113 if( postread_attrs ) {
1114 attrs = ldap_str2charray( postread_attrs, "," );
1117 ber_init2( ber, NULL, LBER_USE_DER );
1119 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1120 fprintf( stderr, "postread attrs encode failed.\n" );
1121 exit( EXIT_FAILURE );
1124 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1126 fprintf( stderr, "postread flatten failed (%d)\n", err );
1127 exit( EXIT_FAILURE );
1130 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1131 c[i].ldctl_iscritical = postread > 1;
1135 if( attrs ) ldap_charray_free( attrs );
1138 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1140 if ( chainingResolve > -1 ) {
1141 BerElementBuffer berbuf;
1142 BerElement *ber = (BerElement *)&berbuf;
1144 ber_init2( ber, NULL, LBER_USE_DER );
1146 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1149 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1150 exit( EXIT_FAILURE );
1153 if ( chainingContinuation > -1 ) {
1154 err = ber_printf( ber, "e", chainingContinuation );
1157 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1158 exit( EXIT_FAILURE );
1162 err = ber_printf( ber, /* { */ "N}" );
1165 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1166 exit( EXIT_FAILURE );
1169 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1170 exit( EXIT_FAILURE );
1174 BER_BVZERO( &c[i].ldctl_value );
1177 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1178 c[i].ldctl_iscritical = chaining > 1;
1182 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1185 ctrls[i++] = extra_c++;
1189 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1191 if ( err != LDAP_OPT_SUCCESS ) {
1192 for ( j = 0; j < i; j++ ) {
1193 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1195 fprintf( stderr, "Could not set %scontrols\n",
1196 crit ? "critical " : "" );
1201 exit( EXIT_FAILURE );
1206 tool_check_abandon( LDAP *ld, int msgid )
1210 switch ( gotintr ) {
1211 case LDAP_REQ_EXTENDED:
1212 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1213 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1214 rc, ldap_err2string( rc ) );
1217 case LDAP_REQ_ABANDON:
1218 rc = ldap_abandon( ld, msgid );
1219 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1220 rc, ldap_err2string( rc ) );