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>
33 #include <ac/unistd.h>
36 #ifdef HAVE_CYRUS_SASL
37 #ifdef HAVE_SASL_SASL_H
38 #include <sasl/sasl.h>
48 #include "lutil_ldap.h"
49 #include "ldap_defaults.h"
55 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
57 /* Necessary for old LDAPv2 Kerberos Bind methods */
59 ldap_bind LDAP_P(( /* deprecated */
62 LDAP_CONST char *passwd,
67 /* input-related vars */
69 /* misc. parameters */
70 tool_type_t tool_type;
82 char *ldaphost = NULL;
92 struct berval passwd = { 0, NULL };
94 #ifdef HAVE_CYRUS_SASL
95 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
96 char *sasl_realm = NULL;
97 char *sasl_authc_id = NULL;
98 char *sasl_authz_id = NULL;
99 char *sasl_mech = NULL;
100 char *sasl_secprops = NULL;
105 char *assertion = NULL;
106 char *authzid = NULL;
112 static char *preread_attrs = NULL;
114 static char *postread_attrs = NULL;
115 ber_int_t pr_morePagedResults = 1;
116 struct berval pr_cookie = { 0, NULL };
117 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
119 static int chainingResolve = -1;
120 static int chainingContinuation = -1;
121 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
123 typedef int (*print_ctrl_fn)( LDAP *ld, LDAPControl *ctrl );
125 static int print_preread( LDAP *ld, LDAPControl *ctrl );
126 static int print_postread( LDAP *ld, LDAPControl *ctrl );
127 static int print_paged_results( LDAP *ld, LDAPControl *ctrl );
128 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
129 static int print_ppolicy( LDAP *ld, LDAPControl *ctrl );
132 static struct tool_ctrls_t {
136 } tool_ctrl_response[] = {
137 { LDAP_CONTROL_PRE_READ, TOOL_ALL, print_preread },
138 { LDAP_CONTROL_POST_READ, TOOL_ALL, print_postread },
139 { LDAP_CONTROL_PAGEDRESULTS, TOOL_SEARCH, print_paged_results },
140 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
141 { LDAP_CONTROL_PASSWORDPOLICYRESPONSE, TOOL_ALL, print_ppolicy },
157 tool_init( tool_type_t type )
160 ldap_pvt_setlocale(LC_MESSAGES, "");
161 ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
162 ldap_pvt_textdomain(OPENLDAP_PACKAGE);
168 #ifdef HAVE_CYRUS_SASL
172 ldap_pvt_tls_destroy();
177 tool_common_usage( void )
179 static const char *const descriptions[] = {
180 N_(" -c continuous operation mode (do not stop on errors)\n"),
181 N_(" -C chase referrals (anonymously)\n"),
182 N_(" -d level set LDAP debugging level to `level'\n"),
183 N_(" -D binddn bind DN\n"),
184 N_(" -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
185 N_(" [!]assert=<filter> (an RFC 2254 Filter)\n")
186 N_(" [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n")
187 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
188 N_(" [!]chaining[=<resolveBehavior>[/<continuationBehavior>]]\n")
189 N_(" one of \"chainingPreferred\", \"chainingRequired\",\n")
190 N_(" \"referralsPreferred\", \"referralsRequired\"\n")
191 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
193 N_(" [!]manageDIT\n")
195 N_(" [!]manageDSAit\n")
197 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
200 N_(" [!]postread[=<attrs>] (a comma-separated attribute list)\n")
201 N_(" [!]preread[=<attrs>] (a comma-separated attribute list)\n"),
202 N_(" abandon, cancel (SIGINT sends abandon/cancel; not really controls)\n")
203 N_(" -f file read operations from `file'\n"),
204 N_(" -h host LDAP server\n"),
205 N_(" -H URI LDAP Uniform Resource Indentifier(s)\n"),
206 N_(" -I use SASL Interactive mode\n"),
207 N_(" -k use Kerberos authentication\n"),
208 N_(" -K like -k, but do only step 1 of the Kerberos bind\n"),
209 N_(" -M enable Manage DSA IT control (-MM to make critical)\n"),
210 N_(" -n show what would be done but don't actually do it\n"),
211 N_(" -O props SASL security properties\n"),
212 N_(" -p port port on LDAP server\n"),
213 N_(" -P version procotol version (default: 3)\n"),
214 N_(" -Q use SASL Quiet mode\n"),
215 N_(" -R realm SASL realm\n"),
216 N_(" -U authcid SASL authentication identity\n"),
217 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
218 N_(" -V print version info (-VV only)\n"),
219 N_(" -w passwd bind password (for simple authentication)\n"),
220 N_(" -W prompt for bind password\n"),
221 N_(" -x Simple authentication\n"),
222 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
223 N_(" -y file Read password from file\n"),
224 N_(" -Y mech SASL mechanism\n"),
225 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
228 const char *const *cpp;
230 fputs( _("Common options:\n"), stderr );
231 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
232 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
233 fputs( _(*cpp), stderr );
246 fprintf( stderr, "%s: %s (%d)%s\n",
247 func, ldap_err2string( err ), err, extra ? extra : "" );
249 if ( matched && *matched ) {
250 fprintf( stderr, _("\tmatched DN: %s\n"), matched );
253 if ( info && *info ) {
254 fprintf( stderr, _("\tadditional info: %s\n"), info );
257 if ( refs && *refs ) {
259 fprintf( stderr, _("\treferrals:\n") );
260 for( i=0; refs[i]; i++ ) {
261 fprintf( stderr, "\t\t%s\n", refs[i] );
268 tool_args( int argc, char **argv )
272 while (( i = getopt( argc, argv, options )) != EOF ) {
274 char *control, *cvalue, *next;
276 case 'c': /* continuous operation mode */
283 ival = strtol( optarg, &next, 10 );
284 if (next == NULL || next[0] != '\0') {
285 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
290 case 'D': /* bind DN */
291 if( binddn != NULL ) {
292 fprintf( stderr, "%s: -D previously specified\n", prog );
293 exit( EXIT_FAILURE );
295 binddn = ber_strdup( optarg );
297 case 'e': /* general extensions (controls and such) */
298 /* should be extended to support comma separated list of
299 * [!]key[=value] parameters, e.g. -e !foo,bar=567
304 if( optarg[0] == '!' ) {
309 control = ber_strdup( optarg );
310 if ( (cvalue = strchr( control, '=' )) != NULL ) {
314 if ( strcasecmp( control, "assert" ) == 0 ) {
316 fprintf( stderr, "assert control previously specified\n");
317 exit( EXIT_FAILURE );
319 if( cvalue == NULL ) {
320 fprintf( stderr, "assert: control value expected\n" );
324 assertctl = 1 + crit;
326 assert( assertion == NULL );
329 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
330 if( authzid != NULL ) {
331 fprintf( stderr, "authzid control previously specified\n");
332 exit( EXIT_FAILURE );
334 if( cvalue == NULL ) {
335 fprintf( stderr, "authzid: control value expected\n" );
339 fprintf( stderr, "authzid: must be marked critical\n" );
343 assert( authzid == NULL );
346 } else if ( strcasecmp( control, "manageDIT" ) == 0 ) {
349 "manageDIT control previously specified\n");
350 exit( EXIT_FAILURE );
352 if( cvalue != NULL ) {
354 "manageDIT: no control value expected\n" );
358 manageDIT = 1 + crit;
360 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
363 "manageDSAit control previously specified\n");
364 exit( EXIT_FAILURE );
366 if( cvalue != NULL ) {
368 "manageDSAit: no control value expected\n" );
372 manageDSAit = 1 + crit;
374 } else if ( strcasecmp( control, "noop" ) == 0 ) {
376 fprintf( stderr, "noop control previously specified\n");
377 exit( EXIT_FAILURE );
379 if( cvalue != NULL ) {
380 fprintf( stderr, "noop: no control value expected\n" );
386 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
387 } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
389 fprintf( stderr, "ppolicy control previously specified\n");
390 exit( EXIT_FAILURE );
392 if( cvalue != NULL ) {
393 fprintf( stderr, "ppolicy: no control value expected\n" );
397 fprintf( stderr, "ppolicy: critical flag not allowed\n" );
404 } else if ( strcasecmp( control, "preread" ) == 0 ) {
406 fprintf( stderr, "preread control previously specified\n");
407 exit( EXIT_FAILURE );
411 preread_attrs = cvalue;
413 } else if ( strcasecmp( control, "postread" ) == 0 ) {
415 fprintf( stderr, "postread control previously specified\n");
416 exit( EXIT_FAILURE );
420 postread_attrs = cvalue;
422 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
423 } else if ( strcasecmp( control, "chaining" ) == 0 ) {
426 if ( cvalue != NULL ) {
429 continuation = strchr( cvalue, '/' );
430 if ( continuation ) {
431 /* FIXME: this makes sense only in searches */
432 *continuation++ = '\0';
433 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
434 chainingContinuation = LDAP_CHAINING_PREFERRED;
435 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
436 chainingContinuation = LDAP_CHAINING_REQUIRED;
437 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
438 chainingContinuation = LDAP_REFERRALS_PREFERRED;
439 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
440 chainingContinuation = LDAP_REFERRALS_REQUIRED;
443 "chaining behavior control "
444 "continuation value \"%s\" invalid\n",
446 exit( EXIT_FAILURE );
450 if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
451 chainingResolve = LDAP_CHAINING_PREFERRED;
452 } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
453 chainingResolve = LDAP_CHAINING_REQUIRED;
454 } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
455 chainingResolve = LDAP_REFERRALS_PREFERRED;
456 } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
457 chainingResolve = LDAP_REFERRALS_REQUIRED;
460 "chaining behavior control "
461 "resolve value \"%s\" invalid\n",
463 exit( EXIT_FAILURE );
466 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
468 /* this shouldn't go here, really; but it's a feature... */
469 } else if ( strcasecmp( control, "abandon" ) == 0 ) {
470 abcan = LDAP_REQ_ABANDON;
472 } else if ( strcasecmp( control, "cancel" ) == 0 ) {
473 abcan = LDAP_REQ_EXTENDED;
476 fprintf( stderr, "Invalid general control name: %s\n",
481 case 'f': /* read from file */
482 if( infile != NULL ) {
483 fprintf( stderr, "%s: -f previously specified\n", prog );
484 exit( EXIT_FAILURE );
486 infile = ber_strdup( optarg );
488 case 'h': /* ldap host */
489 if( ldaphost != NULL ) {
490 fprintf( stderr, "%s: -h previously specified\n", prog );
491 exit( EXIT_FAILURE );
493 ldaphost = ber_strdup( optarg );
495 case 'H': /* ldap URI */
496 if( ldapuri != NULL ) {
497 fprintf( stderr, "%s: -H previously specified\n", prog );
498 exit( EXIT_FAILURE );
500 ldapuri = ber_strdup( optarg );
503 #ifdef HAVE_CYRUS_SASL
504 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
505 fprintf( stderr, "%s: incompatible previous "
506 "authentication choice\n",
508 exit( EXIT_FAILURE );
510 authmethod = LDAP_AUTH_SASL;
511 sasl_flags = LDAP_SASL_INTERACTIVE;
514 fprintf( stderr, "%s: was not compiled with SASL support\n",
516 exit( EXIT_FAILURE );
518 case 'k': /* kerberos bind */
519 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
520 if( authmethod != -1 ) {
521 fprintf( stderr, "%s: -k incompatible with previous "
522 "authentication choice\n", prog );
523 exit( EXIT_FAILURE );
525 authmethod = LDAP_AUTH_KRBV4;
527 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
528 exit( EXIT_FAILURE );
531 case 'K': /* kerberos bind, part one only */
532 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
533 if( authmethod != -1 ) {
534 fprintf( stderr, "%s: incompatible with previous "
535 "authentication choice\n", prog );
536 exit( EXIT_FAILURE );
538 authmethod = LDAP_AUTH_KRBV41;
540 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
541 exit( EXIT_FAILURE );
545 /* enable Manage DSA IT */
548 case 'n': /* print operations, don't actually do them */
552 #ifdef HAVE_CYRUS_SASL
553 if( sasl_secprops != NULL ) {
554 fprintf( stderr, "%s: -O 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", prog );
560 exit( EXIT_FAILURE );
562 authmethod = LDAP_AUTH_SASL;
563 sasl_secprops = ber_strdup( optarg );
565 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
566 exit( EXIT_FAILURE );
571 fprintf( stderr, "%s: -p previously specified\n", prog );
572 exit( EXIT_FAILURE );
574 ival = strtol( optarg, &next, 10 );
575 if ( next == NULL || next[0] != '\0' ) {
576 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
577 exit( EXIT_FAILURE );
582 ival = strtol( optarg, &next, 10 );
583 if ( next == NULL || next[0] != '\0' ) {
584 fprintf( stderr, "%s: unabel to parse protocol version \"%s\"\n", prog, optarg );
585 exit( EXIT_FAILURE );
589 if( protocol == LDAP_VERSION3 ) {
590 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
592 exit( EXIT_FAILURE );
594 protocol = LDAP_VERSION2;
597 if( protocol == LDAP_VERSION2 ) {
598 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
600 exit( EXIT_FAILURE );
602 protocol = LDAP_VERSION3;
605 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
611 #ifdef HAVE_CYRUS_SASL
612 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
613 fprintf( stderr, "%s: incompatible previous "
614 "authentication choice\n",
616 exit( EXIT_FAILURE );
618 authmethod = LDAP_AUTH_SASL;
619 sasl_flags = LDAP_SASL_QUIET;
622 fprintf( stderr, "%s: not compiled with SASL support\n",
624 exit( EXIT_FAILURE );
627 #ifdef HAVE_CYRUS_SASL
628 if( sasl_realm != NULL ) {
629 fprintf( stderr, "%s: -R previously specified\n", prog );
630 exit( EXIT_FAILURE );
632 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
633 fprintf( stderr, "%s: incompatible previous "
634 "authentication choice\n",
636 exit( EXIT_FAILURE );
638 authmethod = LDAP_AUTH_SASL;
639 sasl_realm = ber_strdup( optarg );
641 fprintf( stderr, "%s: not compiled with SASL support\n",
643 exit( EXIT_FAILURE );
647 #ifdef HAVE_CYRUS_SASL
648 if( sasl_authc_id != NULL ) {
649 fprintf( stderr, "%s: -U previously specified\n", prog );
650 exit( EXIT_FAILURE );
652 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
653 fprintf( stderr, "%s: incompatible previous "
654 "authentication choice\n",
656 exit( EXIT_FAILURE );
658 authmethod = LDAP_AUTH_SASL;
659 sasl_authc_id = ber_strdup( optarg );
661 fprintf( stderr, "%s: not compiled with SASL support\n",
663 exit( EXIT_FAILURE );
666 case 'v': /* verbose mode */
669 case 'V': /* version */
672 case 'w': /* password */
673 passwd.bv_val = ber_strdup( optarg );
677 for( p = optarg; *p != '\0'; p++ ) {
681 passwd.bv_len = strlen( passwd.bv_val );
690 #ifdef HAVE_CYRUS_SASL
691 if( sasl_mech != NULL ) {
692 fprintf( stderr, "%s: -Y previously specified\n", prog );
693 exit( EXIT_FAILURE );
695 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
697 "%s: incompatible with authentication choice\n", prog );
698 exit( EXIT_FAILURE );
700 authmethod = LDAP_AUTH_SASL;
701 sasl_mech = ber_strdup( optarg );
703 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
704 exit( EXIT_FAILURE );
708 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
709 fprintf( stderr, "%s: incompatible with previous "
710 "authentication choice\n", prog );
711 exit( EXIT_FAILURE );
713 authmethod = LDAP_AUTH_SIMPLE;
716 #ifdef HAVE_CYRUS_SASL
717 if( sasl_authz_id != NULL ) {
718 fprintf( stderr, "%s: -X previously specified\n", prog );
719 exit( EXIT_FAILURE );
721 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
722 fprintf( stderr, "%s: -X incompatible with "
723 "authentication choice\n", prog );
724 exit( EXIT_FAILURE );
726 authmethod = LDAP_AUTH_SASL;
727 sasl_authz_id = ber_strdup( optarg );
729 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
730 exit( EXIT_FAILURE );
737 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
738 exit( EXIT_FAILURE );
742 if( handle_private_option( i ) ) break;
743 fprintf( stderr, "%s: unrecognized option -%c\n",
750 /* prevent bad linking */
752 api.ldapai_info_version = LDAP_API_INFO_VERSION;
754 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
755 != LDAP_OPT_SUCCESS )
757 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
758 exit( EXIT_FAILURE );
761 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
762 fprintf( stderr, "LDAP APIInfo version mismatch: "
763 "library %d, header %d\n",
764 api.ldapai_info_version, LDAP_API_INFO_VERSION );
765 exit( EXIT_FAILURE );
768 if( api.ldapai_api_version != LDAP_API_VERSION ) {
769 fprintf( stderr, "LDAP API version mismatch: "
770 "library %d, header %d\n",
771 api.ldapai_api_version, LDAP_API_VERSION );
772 exit( EXIT_FAILURE );
775 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
776 fprintf( stderr, "LDAP vendor name mismatch: "
777 "library %s, header %s\n",
778 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
779 exit( EXIT_FAILURE );
782 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
783 fprintf( stderr, "LDAP vendor version mismatch: "
784 "library %d, header %d\n",
785 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
786 exit( EXIT_FAILURE );
790 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
792 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
793 if (version > 1) exit( EXIT_SUCCESS );
796 ldap_memfree( api.ldapai_vendor_name );
797 ber_memvfree( (void **)api.ldapai_extensions );
801 protocol = LDAP_VERSION3;
803 if (authmethod == -1 && protocol > LDAP_VERSION2) {
804 #ifdef HAVE_CYRUS_SASL
805 authmethod = LDAP_AUTH_SASL;
807 authmethod = LDAP_AUTH_SIMPLE;
811 if( ldapuri == NULL ) {
812 if( ldapport && ( ldaphost == NULL )) {
813 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
814 exit( EXIT_FAILURE );
817 if( ldaphost != NULL ) {
818 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
819 exit( EXIT_FAILURE );
822 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
823 exit( EXIT_FAILURE );
827 if( protocol == LDAP_VERSION2 ) {
828 if( assertctl || authzid || manageDIT || manageDSAit ||
829 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
832 noop || ppolicy || preread || postread )
834 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
835 exit( EXIT_FAILURE );
839 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
840 exit( EXIT_FAILURE );
843 #ifdef HAVE_CYRUS_SASL
844 if( authmethod == LDAP_AUTH_SASL ) {
845 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
847 exit( EXIT_FAILURE );
851 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
853 if ( authmethod == LDAP_AUTH_KRBV4 || authmethod == LDAP_AUTH_KRBV41 ) {
854 fprintf( stderr, "%s: -k/-K incompatible with LDAPv%d\n",
856 exit( EXIT_FAILURE );
864 tool_conn_setup( int dont, void (*private_setup)( LDAP * ) )
869 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
870 != LBER_OPT_SUCCESS )
873 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
875 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
876 != LDAP_OPT_SUCCESS )
879 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
884 (void) SIGNAL( SIGPIPE, SIG_IGN );
888 SIGNAL( SIGINT, do_sig );
894 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
897 memset( &url, 0, sizeof(url));
899 url.lud_scheme = "ldap";
900 url.lud_host = ldaphost;
901 url.lud_port = ldapport;
902 url.lud_scope = LDAP_SCOPE_DEFAULT;
904 ldapuri = ldap_url_desc2str( &url );
908 fprintf( stderr, "ldap_initialize( %s )\n",
909 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
911 rc = ldap_initialize( &ld, ldapuri );
912 if( rc != LDAP_SUCCESS ) {
914 "Could not create LDAP session handle for URI=%s (%d): %s\n",
915 ldapuri, rc, ldap_err2string(rc) );
916 exit( EXIT_FAILURE );
919 if( private_setup ) private_setup( ld );
922 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
923 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
925 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
926 referrals ? "on" : "off" );
927 exit( EXIT_FAILURE );
930 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
931 != LDAP_OPT_SUCCESS )
933 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
935 exit( EXIT_FAILURE );
939 rc = ldap_start_tls_s( ld, NULL, NULL );
940 if ( rc != LDAP_SUCCESS ) {
941 tool_perror( "ldap_start_tls", rc, NULL, NULL, NULL, NULL );
943 exit( EXIT_FAILURE );
954 tool_bind( LDAP *ld )
956 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
958 LDAPControl *ctrls[2], c;
959 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
960 c.ldctl_value.bv_val = NULL;
961 c.ldctl_value.bv_len = 0;
962 c.ldctl_iscritical = 0;
965 ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
969 if ( authmethod == LDAP_AUTH_SASL ) {
970 #ifdef HAVE_CYRUS_SASL
974 if( sasl_secprops != NULL ) {
975 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
976 (void *) sasl_secprops );
978 if( rc != LDAP_OPT_SUCCESS ) {
980 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
982 exit( LDAP_LOCAL_ERROR );
986 defaults = lutil_sasl_defaults( ld,
993 rc = ldap_sasl_interactive_bind_s( ld, binddn,
994 sasl_mech, NULL, NULL,
995 sasl_flags, lutil_sasl_interact, defaults );
997 lutil_sasl_freedefs( defaults );
998 if( rc != LDAP_SUCCESS ) {
999 tool_perror( "ldap_sasl_interactive_bind_s",
1000 rc, NULL, NULL, NULL, NULL );
1004 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1005 exit( LDAP_NOT_SUPPORTED );
1009 LDAPMessage *result;
1010 LDAPControl **ctrls;
1012 char *matched = NULL;
1018 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
1019 if ( authmethod == LDAP_AUTH_KRBV4 || authmethod == LDAP_AUTH_KRBV41 ) {
1020 msgid = ldap_bind( ld, binddn, passwd.bv_val, authmethod );
1021 if ( msgid == -1 ) {
1022 tool_perror( "ldap_bind", -1, NULL, NULL, NULL, NULL );
1023 exit( LDAP_LOCAL_ERROR );
1029 rc = ldap_sasl_bind( ld, binddn, LDAP_SASL_SIMPLE,
1030 &passwd, NULL, NULL, &msgid );
1031 if ( msgid == -1 ) {
1032 tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
1033 NULL, NULL, NULL, NULL );
1038 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 ) {
1039 tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
1040 exit( LDAP_LOCAL_ERROR );
1043 rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
1045 if ( rc != LDAP_SUCCESS ) {
1046 tool_perror( "ldap_bind parse result", rc, NULL, NULL, NULL, NULL );
1047 exit( LDAP_LOCAL_ERROR );
1050 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1051 if ( ctrls && ppolicy ) {
1053 int expire, grace, len = 0;
1054 LDAPPasswordPolicyError pErr = -1;
1056 ctrl = ldap_find_control( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
1059 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
1060 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1062 if ( pErr != PP_noError ){
1065 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1066 len = strlen( msgbuf );
1068 if ( expire >= 0 ) {
1069 sprintf( msgbuf+len,
1070 " (Password expires in %d seconds)",
1072 } else if ( grace >= 0 ) {
1073 sprintf( msgbuf+len,
1074 " (Password expired, %d grace logins remain)",
1082 ldap_controls_free( ctrls );
1085 if ( err != LDAP_SUCCESS
1087 || ( matched && matched[ 0 ] )
1088 || ( info && info[ 0 ] )
1091 tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1093 if( matched ) ber_memfree( matched );
1094 if( info ) ber_memfree( info );
1095 if( refs ) ber_memvfree( (void **)refs );
1097 if ( err != LDAP_SUCCESS ) exit( err );
1103 tool_unbind( LDAP *ld )
1105 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1107 if ( err != LDAP_OPT_SUCCESS ) {
1108 fprintf( stderr, "Could not unset controls\n");
1111 (void) ldap_unbind_ext( ld, NULL, NULL );
1115 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1117 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1119 int i = 0, j, crit = 0, err;
1120 LDAPControl c[10], **ctrls;
1122 ctrls = (LDAPControl**) malloc(sizeof(c) + (count+1)*sizeof(LDAPControl*));
1123 if ( ctrls == NULL ) {
1124 fprintf( stderr, "No memory\n" );
1125 exit( EXIT_FAILURE );
1129 BerElementBuffer berbuf;
1130 BerElement *ber = (BerElement *)&berbuf;
1132 if( assertion == NULL || *assertion == '\0' ) {
1133 fprintf( stderr, "Assertion=<empty>\n" );
1134 exit( EXIT_FAILURE );
1137 ber_init2( ber, NULL, LBER_USE_DER );
1139 err = ldap_pvt_put_filter( ber, assertion );
1141 fprintf( stderr, "assertion encode failed (%d)\n", err );
1142 exit( EXIT_FAILURE );
1145 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1147 fprintf( stderr, "assertion flatten failed (%d)\n", err );
1148 exit( EXIT_FAILURE );
1151 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1152 c[i].ldctl_iscritical = assertctl > 1;
1158 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1159 c[i].ldctl_value.bv_val = authzid;
1160 c[i].ldctl_value.bv_len = strlen( authzid );
1161 c[i].ldctl_iscritical = 1;
1167 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1168 BER_BVZERO( &c[i].ldctl_value );
1169 c[i].ldctl_iscritical = manageDIT > 1;
1174 if ( manageDSAit ) {
1175 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1176 BER_BVZERO( &c[i].ldctl_value );
1177 c[i].ldctl_iscritical = manageDSAit > 1;
1183 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1184 BER_BVZERO( &c[i].ldctl_value );
1185 c[i].ldctl_iscritical = noop > 1;
1191 char berbuf[LBER_ELEMENT_SIZEOF];
1192 BerElement *ber = (BerElement *)berbuf;
1193 char **attrs = NULL;
1195 if( preread_attrs ) {
1196 attrs = ldap_str2charray( preread_attrs, "," );
1199 ber_init2( ber, NULL, LBER_USE_DER );
1201 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1202 fprintf( stderr, "preread attrs encode failed.\n" );
1203 exit( EXIT_FAILURE );
1206 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1208 fprintf( stderr, "preread flatten failed (%d)\n", err );
1209 exit( EXIT_FAILURE );
1212 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1213 c[i].ldctl_iscritical = preread > 1;
1217 if( attrs ) ldap_charray_free( attrs );
1221 char berbuf[LBER_ELEMENT_SIZEOF];
1222 BerElement *ber = (BerElement *)berbuf;
1223 char **attrs = NULL;
1225 if( postread_attrs ) {
1226 attrs = ldap_str2charray( postread_attrs, "," );
1229 ber_init2( ber, NULL, LBER_USE_DER );
1231 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1232 fprintf( stderr, "postread attrs encode failed.\n" );
1233 exit( EXIT_FAILURE );
1236 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1238 fprintf( stderr, "postread flatten failed (%d)\n", err );
1239 exit( EXIT_FAILURE );
1242 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1243 c[i].ldctl_iscritical = postread > 1;
1247 if( attrs ) ldap_charray_free( attrs );
1250 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1252 if ( chainingResolve > -1 ) {
1253 BerElementBuffer berbuf;
1254 BerElement *ber = (BerElement *)&berbuf;
1256 ber_init2( ber, NULL, LBER_USE_DER );
1258 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1261 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1262 exit( EXIT_FAILURE );
1265 if ( chainingContinuation > -1 ) {
1266 err = ber_printf( ber, "e", chainingContinuation );
1269 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1270 exit( EXIT_FAILURE );
1274 err = ber_printf( ber, /* { */ "N}" );
1277 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1278 exit( EXIT_FAILURE );
1281 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1282 exit( EXIT_FAILURE );
1286 BER_BVZERO( &c[i].ldctl_value );
1289 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1290 c[i].ldctl_iscritical = chaining > 1;
1294 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1297 ctrls[i++] = extra_c++;
1301 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1303 if ( err != LDAP_OPT_SUCCESS ) {
1304 for ( j = 0; j < i; j++ ) {
1305 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1307 fprintf( stderr, "Could not set %scontrols\n",
1308 crit ? "critical " : "" );
1313 exit( EXIT_FAILURE );
1318 tool_check_abandon( LDAP *ld, int msgid )
1322 switch ( gotintr ) {
1323 case LDAP_REQ_EXTENDED:
1324 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1325 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1326 rc, ldap_err2string( rc ) );
1329 case LDAP_REQ_ABANDON:
1330 rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
1331 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1332 rc, ldap_err2string( rc ) );
1340 print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
1345 tool_write_ldif( LDIF_PUT_COMMENT, "==> ",
1346 what->bv_val, what->bv_len );
1347 ber = ber_init( &ctrl->ldctl_value );
1348 if ( ber == NULL ) {
1352 } else if ( ber_scanf( ber, "{m{" /*}}*/, &bv ) == LBER_ERROR ) {
1357 tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1359 while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
1361 BerVarray vals = NULL;
1363 if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
1370 for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
1372 ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1373 bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
1376 ber_bvarray_free( vals );
1380 if ( ber != NULL ) {
1384 tool_write_ldif( LDIF_PUT_COMMENT, "<== ",
1385 what->bv_val, what->bv_len );
1391 print_preread( LDAP *ld, LDAPControl *ctrl )
1393 static struct berval what = BER_BVC( "preread" );
1395 return print_prepostread( ld, ctrl, &what );
1399 print_postread( LDAP *ld, LDAPControl *ctrl )
1401 static struct berval what = BER_BVC( "postread" );
1403 return print_prepostread( ld, ctrl, &what );
1407 print_paged_results( LDAP *ld, LDAPControl *ctrl )
1412 ber = ber_init( &ctrl->ldctl_value );
1413 /* note: pr_cookie is being malloced; it's freed
1414 * the next time the control is sent, but the last
1415 * time it's not; we don't care too much, because
1416 * the last time an empty value is returned... */
1417 if ( ber_scanf( ber, "{io}", &estimate, &pr_cookie ) == LBER_ERROR ) {
1422 char buf[ BUFSIZ ], *ptr = buf;
1424 if ( estimate > 0 ) {
1425 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1426 "estimate=%d", estimate );
1429 if ( pr_cookie.bv_len > 0 ) {
1432 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
1433 pr_cookie.bv_len ) + 1;
1434 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
1436 bv.bv_len = lutil_b64_ntop(
1437 (unsigned char *) pr_cookie.bv_val,
1439 bv.bv_val, bv.bv_len );
1441 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1442 "%scookie=%s", ptr == buf ? "" : " ",
1445 ber_memfree( bv.bv_val );
1447 pr_morePagedResults = 1;
1450 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1451 "%scookie=", ptr == buf ? "" : " " );
1454 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1455 "pagedresults", buf, ptr - buf );
1458 if ( ber != NULL ) {
1465 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1467 print_ppolicy( LDAP *ld, LDAPControl *ctrl )
1469 int expire = 0, grace = 0, rc;
1470 LDAPPasswordPolicyError pperr;
1472 rc = ldap_parse_passwordpolicy_control( ld, ctrl,
1473 &expire, &grace, &pperr );
1474 if ( rc == LDAP_SUCCESS ) {
1475 char buf[ BUFSIZ ], *ptr = buf;
1477 if ( expire != -1 ) {
1478 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1479 "expire=%d", expire );
1482 if ( grace != -1 ) {
1483 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1484 "%sgrace=%d", ptr == buf ? "" : " ", grace );
1487 if ( pperr != PP_noError ) {
1488 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1489 "%serror=%s", ptr == buf ? "" : " ",
1490 ldap_passwordpolicy_err2txt( pperr ) );
1493 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1494 "ppolicy", buf, ptr - buf );
1501 void tool_print_ctrls(
1503 LDAPControl **ctrls )
1508 for ( i = 0; ctrls[i] != NULL; i++ ) {
1509 /* control: OID criticality base64value */
1510 struct berval b64 = BER_BVNULL;
1516 len += strlen( ctrls[i]->ldctl_oid );
1518 /* add enough for space after OID and the critical value itself */
1519 len += ctrls[i]->ldctl_iscritical
1520 ? sizeof("true") : sizeof("false");
1522 /* convert to base64 */
1523 if ( ctrls[i]->ldctl_value.bv_len ) {
1524 b64.bv_len = LUTIL_BASE64_ENCODE_LEN(
1525 ctrls[i]->ldctl_value.bv_len ) + 1;
1526 b64.bv_val = ber_memalloc( b64.bv_len + 1 );
1528 b64.bv_len = lutil_b64_ntop(
1529 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
1530 ctrls[i]->ldctl_value.bv_len,
1531 b64.bv_val, b64.bv_len );
1535 len += 1 + b64.bv_len;
1538 ptr = str = malloc( len + 1 );
1540 ptr = lutil_strcopy( ptr, ": " );
1542 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_oid );
1543 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_iscritical
1544 ? " true" : " false" );
1547 ptr = lutil_strcopy( ptr, " " );
1548 ptr = lutil_strcopy( ptr, b64.bv_val );
1552 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1553 "control", str, len );
1558 ber_memfree( b64.bv_val );
1561 /* known controls */
1562 for ( j = 0; tool_ctrl_response[j].oid != NULL; j++ ) {
1563 if ( strcmp( tool_ctrl_response[j].oid, ctrls[i]->ldctl_oid ) == 0 ) {
1564 if ( !tool_ctrl_response[j].mask & tool_type ) {
1565 /* this control should not appear
1566 * with this tool; warning? */
1572 if ( tool_ctrl_response[j].oid != NULL && tool_ctrl_response[j].func ) {
1573 (void)tool_ctrl_response[j].func( ld, ctrls[i] );
1579 tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
1583 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1587 fputs( ldif, stdout );
1588 ber_memfree( ldif );
1594 tool_is_oid( const char *s )
1598 if ( !isdigit( s[ 0 ] ) ) {
1602 for ( ; s[ 0 ]; s++ ) {
1603 if ( s[ 0 ] == '.' ) {
1604 if ( s[ 1 ] == '\0' ) {
1611 if ( !isdigit( s[ 0 ] ) ) {
1615 if ( first == 1 && s[ 0 ] == '0' && s[ 1 ] != '.' ) {