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;
107 /* support deprecated early version of proxyAuthz */
108 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ "2.16.840.1.113730.3.4.12"
109 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
110 char *proxydn = NULL;
111 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
117 static char *preread_attrs = NULL;
119 static char *postread_attrs = NULL;
120 ber_int_t pr_morePagedResults = 1;
121 struct berval pr_cookie = { 0, NULL };
122 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
124 static int chainingResolve = -1;
125 static int chainingContinuation = -1;
126 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
128 typedef int (*print_ctrl_fn)( LDAP *ld, LDAPControl *ctrl );
130 static int print_preread( LDAP *ld, LDAPControl *ctrl );
131 static int print_postread( LDAP *ld, LDAPControl *ctrl );
132 static int print_paged_results( LDAP *ld, LDAPControl *ctrl );
133 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
134 static int print_ppolicy( LDAP *ld, LDAPControl *ctrl );
137 static struct tool_ctrls_t {
141 } tool_ctrl_response[] = {
142 { LDAP_CONTROL_PRE_READ, TOOL_ALL, print_preread },
143 { LDAP_CONTROL_POST_READ, TOOL_ALL, print_postread },
144 { LDAP_CONTROL_PAGEDRESULTS, TOOL_SEARCH, print_paged_results },
145 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
146 { LDAP_CONTROL_PASSWORDPOLICYRESPONSE, TOOL_ALL, print_ppolicy },
162 tool_init( tool_type_t type )
165 ldap_pvt_setlocale(LC_MESSAGES, "");
166 ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
167 ldap_pvt_textdomain(OPENLDAP_PACKAGE);
173 #ifdef HAVE_CYRUS_SASL
177 ldap_pvt_tls_destroy();
182 tool_common_usage( void )
184 static const char *const descriptions[] = {
185 N_(" -c continuous operation mode (do not stop on errors)\n"),
186 N_(" -C chase referrals (anonymously)\n"),
187 N_(" -d level set LDAP debugging level to `level'\n"),
188 N_(" -D binddn bind DN\n"),
189 N_(" -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
190 N_(" [!]assert=<filter> (an RFC 2254 Filter)\n")
191 N_(" [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n")
192 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
194 /* non-advertized support for proxyDN */
195 N_(" [!]proxydn=<dn> (an RFC 2253 DN)\n")
198 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
199 N_(" [!]chaining[=<resolveBehavior>[/<continuationBehavior>]]\n")
200 N_(" one of \"chainingPreferred\", \"chainingRequired\",\n")
201 N_(" \"referralsPreferred\", \"referralsRequired\"\n")
202 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
204 N_(" [!]manageDIT\n")
206 N_(" [!]manageDSAit\n")
208 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
211 N_(" [!]postread[=<attrs>] (a comma-separated attribute list)\n")
212 N_(" [!]preread[=<attrs>] (a comma-separated attribute list)\n"),
213 N_(" abandon, cancel (SIGINT sends abandon/cancel; not really controls)\n")
214 N_(" -f file read operations from `file'\n"),
215 N_(" -h host LDAP server\n"),
216 N_(" -H URI LDAP Uniform Resource Indentifier(s)\n"),
217 N_(" -I use SASL Interactive mode\n"),
218 N_(" -k use Kerberos authentication\n"),
219 N_(" -K like -k, but do only step 1 of the Kerberos bind\n"),
220 N_(" -M enable Manage DSA IT control (-MM to make critical)\n"),
221 N_(" -n show what would be done but don't actually do it\n"),
222 N_(" -O props SASL security properties\n"),
223 N_(" -p port port on LDAP server\n"),
224 N_(" -P version procotol version (default: 3)\n"),
225 N_(" -Q use SASL Quiet mode\n"),
226 N_(" -R realm SASL realm\n"),
227 N_(" -U authcid SASL authentication identity\n"),
228 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
229 N_(" -V print version info (-VV only)\n"),
230 N_(" -w passwd bind password (for simple authentication)\n"),
231 N_(" -W prompt for bind password\n"),
232 N_(" -x Simple authentication\n"),
233 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
234 N_(" -y file Read password from file\n"),
235 N_(" -Y mech SASL mechanism\n"),
236 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
239 const char *const *cpp;
241 fputs( _("Common options:\n"), stderr );
242 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
243 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
244 fputs( _(*cpp), stderr );
257 fprintf( stderr, "%s: %s (%d)%s\n",
258 func, ldap_err2string( err ), err, extra ? extra : "" );
260 if ( matched && *matched ) {
261 fprintf( stderr, _("\tmatched DN: %s\n"), matched );
264 if ( info && *info ) {
265 fprintf( stderr, _("\tadditional info: %s\n"), info );
268 if ( refs && *refs ) {
270 fprintf( stderr, _("\treferrals:\n") );
271 for( i=0; refs[i]; i++ ) {
272 fprintf( stderr, "\t\t%s\n", refs[i] );
279 tool_args( int argc, char **argv )
283 while (( i = getopt( argc, argv, options )) != EOF ) {
285 char *control, *cvalue, *next;
287 case 'c': /* continuous operation mode */
294 ival = strtol( optarg, &next, 10 );
295 if (next == NULL || next[0] != '\0') {
296 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
301 case 'D': /* bind DN */
302 if( binddn != NULL ) {
303 fprintf( stderr, "%s: -D previously specified\n", prog );
304 exit( EXIT_FAILURE );
306 binddn = ber_strdup( optarg );
308 case 'e': /* general extensions (controls and such) */
309 /* should be extended to support comma separated list of
310 * [!]key[=value] parameters, e.g. -e !foo,bar=567
315 if( optarg[0] == '!' ) {
320 control = ber_strdup( optarg );
321 if ( (cvalue = strchr( control, '=' )) != NULL ) {
325 if ( strcasecmp( control, "assert" ) == 0 ) {
327 fprintf( stderr, "assert control previously specified\n");
328 exit( EXIT_FAILURE );
330 if( cvalue == NULL ) {
331 fprintf( stderr, "assert: control value expected\n" );
335 assertctl = 1 + crit;
337 assert( assertion == NULL );
340 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
341 if( authzid != NULL ) {
342 fprintf( stderr, "authzid control previously specified\n");
343 exit( EXIT_FAILURE );
345 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
346 if( proxydn != NULL ) {
347 fprintf( stderr, "authzid control incompatible with proxydn\n");
348 exit( EXIT_FAILURE );
350 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
351 if( cvalue == NULL ) {
352 fprintf( stderr, "authzid: control value expected\n" );
356 fprintf( stderr, "authzid: must be marked critical\n" );
360 assert( authzid == NULL );
363 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
364 } else if ( strcasecmp( control, "proxydn" ) == 0 ) {
365 if( proxydn != NULL ) {
366 fprintf( stderr, "proxydn control previously specified\n");
367 exit( EXIT_FAILURE );
369 if( authzid != NULL ) {
370 fprintf( stderr, "proxydn control incompatible with authzid\n");
371 exit( EXIT_FAILURE );
373 if( cvalue == NULL ) {
374 fprintf( stderr, "proxydn: control value expected\n" );
378 fprintf( stderr, "proxydn: must be marked critical\n" );
382 assert( proxydn == NULL );
384 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
386 } else if ( strcasecmp( control, "manageDIT" ) == 0 ) {
389 "manageDIT control previously specified\n");
390 exit( EXIT_FAILURE );
392 if( cvalue != NULL ) {
394 "manageDIT: no control value expected\n" );
398 manageDIT = 1 + crit;
400 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
403 "manageDSAit control previously specified\n");
404 exit( EXIT_FAILURE );
406 if( cvalue != NULL ) {
408 "manageDSAit: no control value expected\n" );
412 manageDSAit = 1 + crit;
414 } else if ( strcasecmp( control, "noop" ) == 0 ) {
416 fprintf( stderr, "noop control previously specified\n");
417 exit( EXIT_FAILURE );
419 if( cvalue != NULL ) {
420 fprintf( stderr, "noop: no control value expected\n" );
426 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
427 } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
429 fprintf( stderr, "ppolicy control previously specified\n");
430 exit( EXIT_FAILURE );
432 if( cvalue != NULL ) {
433 fprintf( stderr, "ppolicy: no control value expected\n" );
437 fprintf( stderr, "ppolicy: critical flag not allowed\n" );
444 } else if ( strcasecmp( control, "preread" ) == 0 ) {
446 fprintf( stderr, "preread control previously specified\n");
447 exit( EXIT_FAILURE );
451 preread_attrs = cvalue;
453 } else if ( strcasecmp( control, "postread" ) == 0 ) {
455 fprintf( stderr, "postread control previously specified\n");
456 exit( EXIT_FAILURE );
460 postread_attrs = cvalue;
462 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
463 } else if ( strcasecmp( control, "chaining" ) == 0 ) {
466 if ( cvalue != NULL ) {
469 continuation = strchr( cvalue, '/' );
470 if ( continuation ) {
471 /* FIXME: this makes sense only in searches */
472 *continuation++ = '\0';
473 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
474 chainingContinuation = LDAP_CHAINING_PREFERRED;
475 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
476 chainingContinuation = LDAP_CHAINING_REQUIRED;
477 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
478 chainingContinuation = LDAP_REFERRALS_PREFERRED;
479 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
480 chainingContinuation = LDAP_REFERRALS_REQUIRED;
483 "chaining behavior control "
484 "continuation value \"%s\" invalid\n",
486 exit( EXIT_FAILURE );
490 if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
491 chainingResolve = LDAP_CHAINING_PREFERRED;
492 } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
493 chainingResolve = LDAP_CHAINING_REQUIRED;
494 } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
495 chainingResolve = LDAP_REFERRALS_PREFERRED;
496 } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
497 chainingResolve = LDAP_REFERRALS_REQUIRED;
500 "chaining behavior control "
501 "resolve value \"%s\" invalid\n",
503 exit( EXIT_FAILURE );
506 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
508 /* this shouldn't go here, really; but it's a feature... */
509 } else if ( strcasecmp( control, "abandon" ) == 0 ) {
510 abcan = LDAP_REQ_ABANDON;
512 } else if ( strcasecmp( control, "cancel" ) == 0 ) {
513 abcan = LDAP_REQ_EXTENDED;
516 fprintf( stderr, "Invalid general control name: %s\n",
521 case 'f': /* read from file */
522 if( infile != NULL ) {
523 fprintf( stderr, "%s: -f previously specified\n", prog );
524 exit( EXIT_FAILURE );
526 infile = ber_strdup( optarg );
528 case 'h': /* ldap host */
529 if( ldaphost != NULL ) {
530 fprintf( stderr, "%s: -h previously specified\n", prog );
531 exit( EXIT_FAILURE );
533 ldaphost = ber_strdup( optarg );
535 case 'H': /* ldap URI */
536 if( ldapuri != NULL ) {
537 fprintf( stderr, "%s: -H previously specified\n", prog );
538 exit( EXIT_FAILURE );
540 ldapuri = ber_strdup( optarg );
543 #ifdef HAVE_CYRUS_SASL
544 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
545 fprintf( stderr, "%s: incompatible previous "
546 "authentication choice\n",
548 exit( EXIT_FAILURE );
550 authmethod = LDAP_AUTH_SASL;
551 sasl_flags = LDAP_SASL_INTERACTIVE;
554 fprintf( stderr, "%s: was not compiled with SASL support\n",
556 exit( EXIT_FAILURE );
558 case 'k': /* kerberos bind */
559 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
560 if( authmethod != -1 ) {
561 fprintf( stderr, "%s: -k incompatible with previous "
562 "authentication choice\n", prog );
563 exit( EXIT_FAILURE );
565 authmethod = LDAP_AUTH_KRBV4;
567 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
568 exit( EXIT_FAILURE );
571 case 'K': /* kerberos bind, part one only */
572 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
573 if( authmethod != -1 ) {
574 fprintf( stderr, "%s: incompatible with previous "
575 "authentication choice\n", prog );
576 exit( EXIT_FAILURE );
578 authmethod = LDAP_AUTH_KRBV41;
580 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
581 exit( EXIT_FAILURE );
585 /* enable Manage DSA IT */
588 case 'n': /* print operations, don't actually do them */
592 #ifdef HAVE_CYRUS_SASL
593 if( sasl_secprops != NULL ) {
594 fprintf( stderr, "%s: -O previously specified\n", prog );
595 exit( EXIT_FAILURE );
597 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
598 fprintf( stderr, "%s: incompatible previous "
599 "authentication choice\n", prog );
600 exit( EXIT_FAILURE );
602 authmethod = LDAP_AUTH_SASL;
603 sasl_secprops = ber_strdup( optarg );
605 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
606 exit( EXIT_FAILURE );
611 fprintf( stderr, "%s: -p previously specified\n", prog );
612 exit( EXIT_FAILURE );
614 ival = strtol( optarg, &next, 10 );
615 if ( next == NULL || next[0] != '\0' ) {
616 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
617 exit( EXIT_FAILURE );
622 ival = strtol( optarg, &next, 10 );
623 if ( next == NULL || next[0] != '\0' ) {
624 fprintf( stderr, "%s: unabel to parse protocol version \"%s\"\n", prog, optarg );
625 exit( EXIT_FAILURE );
629 if( protocol == LDAP_VERSION3 ) {
630 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
632 exit( EXIT_FAILURE );
634 protocol = LDAP_VERSION2;
637 if( protocol == LDAP_VERSION2 ) {
638 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
640 exit( EXIT_FAILURE );
642 protocol = LDAP_VERSION3;
645 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
651 #ifdef HAVE_CYRUS_SASL
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_flags = LDAP_SASL_QUIET;
662 fprintf( stderr, "%s: not compiled with SASL support\n",
664 exit( EXIT_FAILURE );
667 #ifdef HAVE_CYRUS_SASL
668 if( sasl_realm != NULL ) {
669 fprintf( stderr, "%s: -R previously specified\n", prog );
670 exit( EXIT_FAILURE );
672 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
673 fprintf( stderr, "%s: incompatible previous "
674 "authentication choice\n",
676 exit( EXIT_FAILURE );
678 authmethod = LDAP_AUTH_SASL;
679 sasl_realm = ber_strdup( optarg );
681 fprintf( stderr, "%s: not compiled with SASL support\n",
683 exit( EXIT_FAILURE );
687 #ifdef HAVE_CYRUS_SASL
688 if( sasl_authc_id != NULL ) {
689 fprintf( stderr, "%s: -U previously specified\n", prog );
690 exit( EXIT_FAILURE );
692 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
693 fprintf( stderr, "%s: incompatible previous "
694 "authentication choice\n",
696 exit( EXIT_FAILURE );
698 authmethod = LDAP_AUTH_SASL;
699 sasl_authc_id = ber_strdup( optarg );
701 fprintf( stderr, "%s: not compiled with SASL support\n",
703 exit( EXIT_FAILURE );
706 case 'v': /* verbose mode */
709 case 'V': /* version */
712 case 'w': /* password */
713 passwd.bv_val = ber_strdup( optarg );
717 for( p = optarg; *p != '\0'; p++ ) {
721 passwd.bv_len = strlen( passwd.bv_val );
730 #ifdef HAVE_CYRUS_SASL
731 if( sasl_mech != NULL ) {
732 fprintf( stderr, "%s: -Y previously specified\n", prog );
733 exit( EXIT_FAILURE );
735 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
737 "%s: incompatible with authentication choice\n", prog );
738 exit( EXIT_FAILURE );
740 authmethod = LDAP_AUTH_SASL;
741 sasl_mech = ber_strdup( optarg );
743 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
744 exit( EXIT_FAILURE );
748 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
749 fprintf( stderr, "%s: incompatible with previous "
750 "authentication choice\n", prog );
751 exit( EXIT_FAILURE );
753 authmethod = LDAP_AUTH_SIMPLE;
756 #ifdef HAVE_CYRUS_SASL
757 if( sasl_authz_id != NULL ) {
758 fprintf( stderr, "%s: -X previously specified\n", prog );
759 exit( EXIT_FAILURE );
761 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
762 fprintf( stderr, "%s: -X incompatible with "
763 "authentication choice\n", prog );
764 exit( EXIT_FAILURE );
766 authmethod = LDAP_AUTH_SASL;
767 sasl_authz_id = ber_strdup( optarg );
769 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
770 exit( EXIT_FAILURE );
777 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
778 exit( EXIT_FAILURE );
782 if( handle_private_option( i ) ) break;
783 fprintf( stderr, "%s: unrecognized option -%c\n",
790 /* prevent bad linking */
792 api.ldapai_info_version = LDAP_API_INFO_VERSION;
794 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
795 != LDAP_OPT_SUCCESS )
797 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
798 exit( EXIT_FAILURE );
801 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
802 fprintf( stderr, "LDAP APIInfo version mismatch: "
803 "library %d, header %d\n",
804 api.ldapai_info_version, LDAP_API_INFO_VERSION );
805 exit( EXIT_FAILURE );
808 if( api.ldapai_api_version != LDAP_API_VERSION ) {
809 fprintf( stderr, "LDAP API version mismatch: "
810 "library %d, header %d\n",
811 api.ldapai_api_version, LDAP_API_VERSION );
812 exit( EXIT_FAILURE );
815 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
816 fprintf( stderr, "LDAP vendor name mismatch: "
817 "library %s, header %s\n",
818 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
819 exit( EXIT_FAILURE );
822 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
823 fprintf( stderr, "LDAP vendor version mismatch: "
824 "library %d, header %d\n",
825 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
826 exit( EXIT_FAILURE );
830 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
832 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
833 if (version > 1) exit( EXIT_SUCCESS );
836 ldap_memfree( api.ldapai_vendor_name );
837 ber_memvfree( (void **)api.ldapai_extensions );
841 protocol = LDAP_VERSION3;
843 if (authmethod == -1 && protocol > LDAP_VERSION2) {
844 #ifdef HAVE_CYRUS_SASL
845 authmethod = LDAP_AUTH_SASL;
847 authmethod = LDAP_AUTH_SIMPLE;
851 if( ldapuri == NULL ) {
852 if( ldapport && ( ldaphost == NULL )) {
853 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
854 exit( EXIT_FAILURE );
857 if( ldaphost != NULL ) {
858 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
859 exit( EXIT_FAILURE );
862 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
863 exit( EXIT_FAILURE );
867 if( protocol == LDAP_VERSION2 ) {
868 if( assertctl || authzid || manageDIT || manageDSAit ||
869 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
871 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
872 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
875 noop || ppolicy || preread || postread )
877 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
878 exit( EXIT_FAILURE );
882 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
883 exit( EXIT_FAILURE );
886 #ifdef HAVE_CYRUS_SASL
887 if( authmethod == LDAP_AUTH_SASL ) {
888 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
890 exit( EXIT_FAILURE );
894 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
896 if ( authmethod == LDAP_AUTH_KRBV4 || authmethod == LDAP_AUTH_KRBV41 ) {
897 fprintf( stderr, "%s: -k/-K incompatible with LDAPv%d\n",
899 exit( EXIT_FAILURE );
907 tool_conn_setup( int dont, void (*private_setup)( LDAP * ) )
912 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
913 != LBER_OPT_SUCCESS )
916 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
918 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
919 != LDAP_OPT_SUCCESS )
922 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
927 (void) SIGNAL( SIGPIPE, SIG_IGN );
931 SIGNAL( SIGINT, do_sig );
937 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
940 memset( &url, 0, sizeof(url));
942 url.lud_scheme = "ldap";
943 url.lud_host = ldaphost;
944 url.lud_port = ldapport;
945 url.lud_scope = LDAP_SCOPE_DEFAULT;
947 ldapuri = ldap_url_desc2str( &url );
951 fprintf( stderr, "ldap_initialize( %s )\n",
952 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
954 rc = ldap_initialize( &ld, ldapuri );
955 if( rc != LDAP_SUCCESS ) {
957 "Could not create LDAP session handle for URI=%s (%d): %s\n",
958 ldapuri, rc, ldap_err2string(rc) );
959 exit( EXIT_FAILURE );
962 if( private_setup ) private_setup( ld );
965 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
966 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
968 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
969 referrals ? "on" : "off" );
970 exit( EXIT_FAILURE );
973 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
974 != LDAP_OPT_SUCCESS )
976 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
978 exit( EXIT_FAILURE );
982 rc = ldap_start_tls_s( ld, NULL, NULL );
983 if ( rc != LDAP_SUCCESS ) {
984 tool_perror( "ldap_start_tls", rc, NULL, NULL, NULL, NULL );
986 exit( EXIT_FAILURE );
997 tool_bind( LDAP *ld )
999 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1001 LDAPControl *ctrls[2], c;
1002 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1003 c.ldctl_value.bv_val = NULL;
1004 c.ldctl_value.bv_len = 0;
1005 c.ldctl_iscritical = 0;
1008 ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1012 if ( authmethod == LDAP_AUTH_SASL ) {
1013 #ifdef HAVE_CYRUS_SASL
1017 if( sasl_secprops != NULL ) {
1018 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
1019 (void *) sasl_secprops );
1021 if( rc != LDAP_OPT_SUCCESS ) {
1023 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
1025 exit( LDAP_LOCAL_ERROR );
1029 defaults = lutil_sasl_defaults( ld,
1036 rc = ldap_sasl_interactive_bind_s( ld, binddn,
1037 sasl_mech, NULL, NULL,
1038 sasl_flags, lutil_sasl_interact, defaults );
1040 lutil_sasl_freedefs( defaults );
1041 if( rc != LDAP_SUCCESS ) {
1042 tool_perror( "ldap_sasl_interactive_bind_s",
1043 rc, NULL, NULL, NULL, NULL );
1047 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1048 exit( LDAP_NOT_SUPPORTED );
1052 LDAPMessage *result;
1053 LDAPControl **ctrls;
1055 char *matched = NULL;
1061 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
1062 if ( authmethod == LDAP_AUTH_KRBV4 || authmethod == LDAP_AUTH_KRBV41 ) {
1063 msgid = ldap_bind( ld, binddn, passwd.bv_val, authmethod );
1064 if ( msgid == -1 ) {
1065 tool_perror( "ldap_bind", -1, NULL, NULL, NULL, NULL );
1066 exit( LDAP_LOCAL_ERROR );
1072 rc = ldap_sasl_bind( ld, binddn, LDAP_SASL_SIMPLE,
1073 &passwd, NULL, NULL, &msgid );
1074 if ( msgid == -1 ) {
1075 tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
1076 NULL, NULL, NULL, NULL );
1081 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 ) {
1082 tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
1083 exit( LDAP_LOCAL_ERROR );
1086 rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
1088 if ( rc != LDAP_SUCCESS ) {
1089 tool_perror( "ldap_bind parse result", rc, NULL, NULL, NULL, NULL );
1090 exit( LDAP_LOCAL_ERROR );
1093 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1094 if ( ctrls && ppolicy ) {
1096 int expire, grace, len = 0;
1097 LDAPPasswordPolicyError pErr = -1;
1099 ctrl = ldap_find_control( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
1102 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
1103 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1105 if ( pErr != PP_noError ){
1108 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1109 len = strlen( msgbuf );
1111 if ( expire >= 0 ) {
1112 sprintf( msgbuf+len,
1113 " (Password expires in %d seconds)",
1115 } else if ( grace >= 0 ) {
1116 sprintf( msgbuf+len,
1117 " (Password expired, %d grace logins remain)",
1125 ldap_controls_free( ctrls );
1128 if ( err != LDAP_SUCCESS
1130 || ( matched && matched[ 0 ] )
1131 || ( info && info[ 0 ] )
1134 tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1136 if( matched ) ber_memfree( matched );
1137 if( info ) ber_memfree( info );
1138 if( refs ) ber_memvfree( (void **)refs );
1140 if ( err != LDAP_SUCCESS ) exit( err );
1146 tool_unbind( LDAP *ld )
1148 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1150 if ( err != LDAP_OPT_SUCCESS ) {
1151 fprintf( stderr, "Could not unset controls\n");
1154 (void) ldap_unbind_ext( ld, NULL, NULL );
1158 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1160 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1162 int i = 0, j, crit = 0, err;
1163 LDAPControl c[10], **ctrls;
1167 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1169 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1175 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1177 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1183 ctrls = (LDAPControl**) malloc(sizeof(c) + (count+1)*sizeof(LDAPControl*));
1184 if ( ctrls == NULL ) {
1185 fprintf( stderr, "No memory\n" );
1186 exit( EXIT_FAILURE );
1190 BerElementBuffer berbuf;
1191 BerElement *ber = (BerElement *)&berbuf;
1193 if( assertion == NULL || *assertion == '\0' ) {
1194 fprintf( stderr, "Assertion=<empty>\n" );
1195 exit( EXIT_FAILURE );
1198 ber_init2( ber, NULL, LBER_USE_DER );
1200 err = ldap_pvt_put_filter( ber, assertion );
1202 fprintf( stderr, "assertion encode failed (%d)\n", err );
1203 exit( EXIT_FAILURE );
1206 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1208 fprintf( stderr, "assertion flatten failed (%d)\n", err );
1209 exit( EXIT_FAILURE );
1212 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1213 c[i].ldctl_iscritical = assertctl > 1;
1219 c[i].ldctl_value.bv_val = authzid;
1220 c[i].ldctl_value.bv_len = strlen( authzid );
1221 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1222 c[i].ldctl_iscritical = 1;
1227 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1228 /* NOTE: doesn't need an extra count because it's incompatible
1231 BerElementBuffer berbuf;
1232 BerElement *ber = (BerElement *)&berbuf;
1234 ber_init2( ber, NULL, LBER_USE_DER );
1236 if ( ber_printf( ber, "s", proxydn ) == LBER_ERROR ) {
1237 exit( EXIT_FAILURE );
1240 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1241 exit( EXIT_FAILURE );
1244 c[i].ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1245 c[i].ldctl_iscritical = 1;
1249 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1252 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1253 BER_BVZERO( &c[i].ldctl_value );
1254 c[i].ldctl_iscritical = manageDIT > 1;
1259 if ( manageDSAit ) {
1260 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1261 BER_BVZERO( &c[i].ldctl_value );
1262 c[i].ldctl_iscritical = manageDSAit > 1;
1268 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1269 BER_BVZERO( &c[i].ldctl_value );
1270 c[i].ldctl_iscritical = noop > 1;
1276 char berbuf[LBER_ELEMENT_SIZEOF];
1277 BerElement *ber = (BerElement *)berbuf;
1278 char **attrs = NULL;
1280 if( preread_attrs ) {
1281 attrs = ldap_str2charray( preread_attrs, "," );
1284 ber_init2( ber, NULL, LBER_USE_DER );
1286 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1287 fprintf( stderr, "preread attrs encode failed.\n" );
1288 exit( EXIT_FAILURE );
1291 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1293 fprintf( stderr, "preread flatten failed (%d)\n", err );
1294 exit( EXIT_FAILURE );
1297 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1298 c[i].ldctl_iscritical = preread > 1;
1302 if( attrs ) ldap_charray_free( attrs );
1306 char berbuf[LBER_ELEMENT_SIZEOF];
1307 BerElement *ber = (BerElement *)berbuf;
1308 char **attrs = NULL;
1310 if( postread_attrs ) {
1311 attrs = ldap_str2charray( postread_attrs, "," );
1314 ber_init2( ber, NULL, LBER_USE_DER );
1316 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1317 fprintf( stderr, "postread attrs encode failed.\n" );
1318 exit( EXIT_FAILURE );
1321 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1323 fprintf( stderr, "postread flatten failed (%d)\n", err );
1324 exit( EXIT_FAILURE );
1327 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1328 c[i].ldctl_iscritical = postread > 1;
1332 if( attrs ) ldap_charray_free( attrs );
1335 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1337 if ( chainingResolve > -1 ) {
1338 BerElementBuffer berbuf;
1339 BerElement *ber = (BerElement *)&berbuf;
1341 ber_init2( ber, NULL, LBER_USE_DER );
1343 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1346 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1347 exit( EXIT_FAILURE );
1350 if ( chainingContinuation > -1 ) {
1351 err = ber_printf( ber, "e", chainingContinuation );
1354 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1355 exit( EXIT_FAILURE );
1359 err = ber_printf( ber, /* { */ "N}" );
1362 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1363 exit( EXIT_FAILURE );
1366 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1367 exit( EXIT_FAILURE );
1371 BER_BVZERO( &c[i].ldctl_value );
1374 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1375 c[i].ldctl_iscritical = chaining > 1;
1379 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1382 ctrls[i++] = extra_c++;
1386 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1388 if ( err != LDAP_OPT_SUCCESS ) {
1389 for ( j = 0; j < i; j++ ) {
1390 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1392 fprintf( stderr, "Could not set %scontrols\n",
1393 crit ? "critical " : "" );
1398 exit( EXIT_FAILURE );
1403 tool_check_abandon( LDAP *ld, int msgid )
1407 switch ( gotintr ) {
1408 case LDAP_REQ_EXTENDED:
1409 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1410 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1411 rc, ldap_err2string( rc ) );
1414 case LDAP_REQ_ABANDON:
1415 rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
1416 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1417 rc, ldap_err2string( rc ) );
1425 print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
1430 tool_write_ldif( LDIF_PUT_COMMENT, "==> ",
1431 what->bv_val, what->bv_len );
1432 ber = ber_init( &ctrl->ldctl_value );
1433 if ( ber == NULL ) {
1437 } else if ( ber_scanf( ber, "{m{" /*}}*/, &bv ) == LBER_ERROR ) {
1442 tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1444 while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
1446 BerVarray vals = NULL;
1448 if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
1455 for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
1457 ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1458 bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
1461 ber_bvarray_free( vals );
1465 if ( ber != NULL ) {
1469 tool_write_ldif( LDIF_PUT_COMMENT, "<== ",
1470 what->bv_val, what->bv_len );
1476 print_preread( LDAP *ld, LDAPControl *ctrl )
1478 static struct berval what = BER_BVC( "preread" );
1480 return print_prepostread( ld, ctrl, &what );
1484 print_postread( LDAP *ld, LDAPControl *ctrl )
1486 static struct berval what = BER_BVC( "postread" );
1488 return print_prepostread( ld, ctrl, &what );
1492 print_paged_results( LDAP *ld, LDAPControl *ctrl )
1496 /* note: pr_cookie is being malloced; it's freed
1497 * the next time the control is sent, but the last
1498 * time it's not; we don't care too much, because
1499 * the last time an empty value is returned... */
1500 if ( ldap_parse_pageresponse_control( ld, ctrl, &estimate, &pr_cookie )
1507 /* FIXME: check buffer overflow */
1508 char buf[ BUFSIZ ], *ptr = buf;
1510 if ( estimate > 0 ) {
1511 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1512 "estimate=%d", estimate );
1515 if ( pr_cookie.bv_len > 0 ) {
1518 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
1519 pr_cookie.bv_len ) + 1;
1520 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
1522 bv.bv_len = lutil_b64_ntop(
1523 (unsigned char *) pr_cookie.bv_val,
1525 bv.bv_val, bv.bv_len );
1527 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1528 "%scookie=%s", ptr == buf ? "" : " ",
1531 ber_memfree( bv.bv_val );
1533 pr_morePagedResults = 1;
1536 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1537 "%scookie=", ptr == buf ? "" : " " );
1540 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1541 "pagedresults", buf, ptr - buf );
1547 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1549 print_ppolicy( LDAP *ld, LDAPControl *ctrl )
1551 int expire = 0, grace = 0, rc;
1552 LDAPPasswordPolicyError pperr;
1554 rc = ldap_parse_passwordpolicy_control( ld, ctrl,
1555 &expire, &grace, &pperr );
1556 if ( rc == LDAP_SUCCESS ) {
1557 char buf[ BUFSIZ ], *ptr = buf;
1559 if ( expire != -1 ) {
1560 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1561 "expire=%d", expire );
1564 if ( grace != -1 ) {
1565 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1566 "%sgrace=%d", ptr == buf ? "" : " ", grace );
1569 if ( pperr != PP_noError ) {
1570 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1571 "%serror=%s", ptr == buf ? "" : " ",
1572 ldap_passwordpolicy_err2txt( pperr ) );
1575 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1576 "ppolicy", buf, ptr - buf );
1583 void tool_print_ctrls(
1585 LDAPControl **ctrls )
1590 for ( i = 0; ctrls[i] != NULL; i++ ) {
1591 /* control: OID criticality base64value */
1592 struct berval b64 = BER_BVNULL;
1598 len += strlen( ctrls[i]->ldctl_oid );
1600 /* add enough for space after OID and the critical value itself */
1601 len += ctrls[i]->ldctl_iscritical
1602 ? sizeof("true") : sizeof("false");
1604 /* convert to base64 */
1605 if ( ctrls[i]->ldctl_value.bv_len ) {
1606 b64.bv_len = LUTIL_BASE64_ENCODE_LEN(
1607 ctrls[i]->ldctl_value.bv_len ) + 1;
1608 b64.bv_val = ber_memalloc( b64.bv_len + 1 );
1610 b64.bv_len = lutil_b64_ntop(
1611 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
1612 ctrls[i]->ldctl_value.bv_len,
1613 b64.bv_val, b64.bv_len );
1617 len += 1 + b64.bv_len;
1620 ptr = str = malloc( len + 1 );
1622 ptr = lutil_strcopy( ptr, ": " );
1624 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_oid );
1625 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_iscritical
1626 ? " true" : " false" );
1629 ptr = lutil_strcopy( ptr, " " );
1630 ptr = lutil_strcopy( ptr, b64.bv_val );
1634 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1635 "control", str, len );
1640 ber_memfree( b64.bv_val );
1643 /* known controls */
1644 for ( j = 0; tool_ctrl_response[j].oid != NULL; j++ ) {
1645 if ( strcmp( tool_ctrl_response[j].oid, ctrls[i]->ldctl_oid ) == 0 ) {
1646 if ( !tool_ctrl_response[j].mask & tool_type ) {
1647 /* this control should not appear
1648 * with this tool; warning? */
1654 if ( tool_ctrl_response[j].oid != NULL && tool_ctrl_response[j].func ) {
1655 (void)tool_ctrl_response[j].func( ld, ctrls[i] );
1661 tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
1665 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1669 fputs( ldif, stdout );
1670 ber_memfree( ldif );
1676 tool_is_oid( const char *s )
1680 if ( !isdigit( s[ 0 ] ) ) {
1684 for ( ; s[ 0 ]; s++ ) {
1685 if ( s[ 0 ] == '.' ) {
1686 if ( s[ 1 ] == '\0' ) {
1693 if ( !isdigit( s[ 0 ] ) ) {
1697 if ( first == 1 && s[ 0 ] == '0' && s[ 1 ] != '.' ) {