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-2008 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"
61 char *ldaphost = NULL;
63 #ifdef HAVE_CYRUS_SASL
64 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
65 char *sasl_realm = NULL;
66 char *sasl_authc_id = NULL;
67 char *sasl_authz_id = NULL;
68 char *sasl_mech = NULL;
69 char *sasl_secprops = NULL;
74 char *assertion = NULL;
81 char *preread_attrs = NULL;
83 char *postread_attrs = NULL;
87 struct berval passwd = { 0, NULL };
95 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
97 static int chainingResolve = -1;
98 static int chainingContinuation = -1;
99 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
116 ldap_pvt_setlocale(LC_MESSAGES, "");
117 ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
118 ldap_pvt_textdomain(OPENLDAP_PACKAGE);
124 #ifdef HAVE_CYRUS_SASL
128 ldap_pvt_tls_destroy();
133 tool_common_usage( void )
135 static const char *const descriptions[] = {
136 N_(" -c continuous operation mode (do not stop on errors)\n"),
137 N_(" -C chase referrals (anonymously)\n"),
138 N_(" -d level set LDAP debugging level to `level'\n"),
139 N_(" -D binddn bind DN\n"),
140 N_(" -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
141 N_(" [!]assert=<filter> (an RFC 2254 Filter)\n")
142 N_(" [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n")
143 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
144 N_(" [!]chaining[=<resolveBehavior>[/<continuationBehavior>]]\n")
145 N_(" one of \"chainingPreferred\", \"chainingRequired\",\n")
146 N_(" \"referralsPreferred\", \"referralsRequired\"\n")
147 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
149 N_(" [!]manageDIT\n")
151 N_(" [!]manageDSAit\n")
153 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
156 N_(" [!]postread[=<attrs>] (a comma-separated attribute list)\n")
157 N_(" [!]preread[=<attrs>] (a comma-separated attribute list)\n"),
158 N_(" abandon, cancel (SIGINT sends abandon/cancel; not really controls)\n")
159 N_(" -f file read operations from `file'\n"),
160 N_(" -h host LDAP server\n"),
161 N_(" -H URI LDAP Uniform Resource Identifier(s)\n"),
162 N_(" -I use SASL Interactive mode\n"),
163 N_(" -k use Kerberos authentication\n"),
164 N_(" -K like -k, but do only step 1 of the Kerberos bind\n"),
165 N_(" -M enable Manage DSA IT control (-MM to make critical)\n"),
166 N_(" -n show what would be done but don't actually do it\n"),
167 N_(" -O props SASL security properties\n"),
168 N_(" -p port port on LDAP server\n"),
169 N_(" -P version protocol version (default: 3)\n"),
170 N_(" -Q use SASL Quiet mode\n"),
171 N_(" -R realm SASL realm\n"),
172 N_(" -U authcid SASL authentication identity\n"),
173 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
174 N_(" -V print version info (-VV only)\n"),
175 N_(" -w passwd bind password (for simple authentication)\n"),
176 N_(" -W prompt for bind password\n"),
177 N_(" -x Simple authentication\n"),
178 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
179 N_(" -y file Read password from file\n"),
180 N_(" -Y mech SASL mechanism\n"),
181 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
184 const char *const *cpp;
186 fputs( _("Common options:\n"), stderr );
187 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
188 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
189 fputs( _(*cpp), stderr );
202 fprintf( stderr, "%s: %s (%d)%s\n",
203 func, ldap_err2string( err ), err, extra ? extra : "" );
205 if ( matched && *matched ) {
206 fprintf( stderr, _("\tmatched DN: %s\n"), matched );
209 if ( info && *info ) {
210 fprintf( stderr, _("\tadditional info: %s\n"), info );
213 if ( refs && *refs ) {
215 fprintf( stderr, _("\treferrals:\n") );
216 for( i=0; refs[i]; i++ ) {
217 fprintf( stderr, "\t\t%s\n", refs[i] );
224 tool_args( int argc, char **argv )
228 while (( i = getopt( argc, argv, options )) != EOF ) {
230 char *control, *cvalue, *next;
232 case 'c': /* continuous operation mode */
239 ival = strtol( optarg, &next, 10 );
240 if (next == NULL || next[0] != '\0') {
241 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
246 case 'D': /* bind DN */
247 if( binddn != NULL ) {
248 fprintf( stderr, "%s: -D previously specified\n", prog );
249 exit( EXIT_FAILURE );
251 binddn = ber_strdup( optarg );
253 case 'e': /* general extensions (controls and such) */
254 /* should be extended to support comma separated list of
255 * [!]key[=value] parameters, e.g. -e !foo,bar=567
260 if( optarg[0] == '!' ) {
265 control = ber_strdup( optarg );
266 if ( (cvalue = strchr( control, '=' )) != NULL ) {
270 if ( strcasecmp( control, "assert" ) == 0 ) {
272 fprintf( stderr, "assert control previously specified\n");
273 exit( EXIT_FAILURE );
275 if( cvalue == NULL ) {
276 fprintf( stderr, "assert: control value expected\n" );
280 assertctl = 1 + crit;
282 assert( assertion == NULL );
285 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
286 if( authzid != NULL ) {
287 fprintf( stderr, "authzid control previously specified\n");
288 exit( EXIT_FAILURE );
290 if( cvalue == NULL ) {
291 fprintf( stderr, "authzid: control value expected\n" );
295 fprintf( stderr, "authzid: must be marked critical\n" );
299 assert( authzid == NULL );
302 } else if ( strcasecmp( control, "manageDIT" ) == 0 ) {
305 "manageDIT control previously specified\n");
306 exit( EXIT_FAILURE );
308 if( cvalue != NULL ) {
310 "manageDIT: no control value expected\n" );
314 manageDIT = 1 + crit;
316 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
319 "manageDSAit control previously specified\n");
320 exit( EXIT_FAILURE );
322 if( cvalue != NULL ) {
324 "manageDSAit: no control value expected\n" );
328 manageDSAit = 1 + crit;
330 } else if ( strcasecmp( control, "noop" ) == 0 ) {
332 fprintf( stderr, "noop control previously specified\n");
333 exit( EXIT_FAILURE );
335 if( cvalue != NULL ) {
336 fprintf( stderr, "noop: no control value expected\n" );
342 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
343 } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
345 fprintf( stderr, "ppolicy control previously specified\n");
346 exit( EXIT_FAILURE );
348 if( cvalue != NULL ) {
349 fprintf( stderr, "ppolicy: no control value expected\n" );
353 fprintf( stderr, "ppolicy: critical flag not allowed\n" );
360 } else if ( strcasecmp( control, "preread" ) == 0 ) {
362 fprintf( stderr, "preread control previously specified\n");
363 exit( EXIT_FAILURE );
367 preread_attrs = cvalue;
369 } else if ( strcasecmp( control, "postread" ) == 0 ) {
371 fprintf( stderr, "postread control previously specified\n");
372 exit( EXIT_FAILURE );
376 postread_attrs = cvalue;
378 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
379 } else if ( strcasecmp( control, "chaining" ) == 0 ) {
382 if ( cvalue != NULL ) {
385 continuation = strchr( cvalue, '/' );
386 if ( continuation ) {
387 /* FIXME: this makes sense only in searches */
388 *continuation++ = '\0';
389 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
390 chainingContinuation = LDAP_CHAINING_PREFERRED;
391 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
392 chainingContinuation = LDAP_CHAINING_REQUIRED;
393 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
394 chainingContinuation = LDAP_REFERRALS_PREFERRED;
395 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
396 chainingContinuation = LDAP_REFERRALS_REQUIRED;
399 "chaining behavior control "
400 "continuation value \"%s\" invalid\n",
402 exit( EXIT_FAILURE );
406 if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
407 chainingResolve = LDAP_CHAINING_PREFERRED;
408 } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
409 chainingResolve = LDAP_CHAINING_REQUIRED;
410 } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
411 chainingResolve = LDAP_REFERRALS_PREFERRED;
412 } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
413 chainingResolve = LDAP_REFERRALS_REQUIRED;
416 "chaining behavior control "
417 "resolve value \"%s\" invalid\n",
419 exit( EXIT_FAILURE );
422 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
424 /* this shouldn't go here, really; but it's a feature... */
425 } else if ( strcasecmp( control, "abandon" ) == 0 ) {
426 abcan = LDAP_REQ_ABANDON;
428 } else if ( strcasecmp( control, "cancel" ) == 0 ) {
429 abcan = LDAP_REQ_EXTENDED;
432 fprintf( stderr, "Invalid general control name: %s\n",
437 case 'f': /* read from file */
438 if( infile != NULL ) {
439 fprintf( stderr, "%s: -f previously specified\n", prog );
440 exit( EXIT_FAILURE );
442 infile = ber_strdup( optarg );
444 case 'h': /* ldap host */
445 if( ldaphost != NULL ) {
446 fprintf( stderr, "%s: -h previously specified\n", prog );
447 exit( EXIT_FAILURE );
449 ldaphost = ber_strdup( optarg );
451 case 'H': /* ldap URI */
452 if( ldapuri != NULL ) {
453 fprintf( stderr, "%s: -H previously specified\n", prog );
454 exit( EXIT_FAILURE );
456 ldapuri = ber_strdup( optarg );
459 #ifdef HAVE_CYRUS_SASL
460 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
461 fprintf( stderr, "%s: incompatible previous "
462 "authentication choice\n",
464 exit( EXIT_FAILURE );
466 authmethod = LDAP_AUTH_SASL;
467 sasl_flags = LDAP_SASL_INTERACTIVE;
470 fprintf( stderr, "%s: was not compiled with SASL support\n",
472 exit( EXIT_FAILURE );
474 case 'k': /* kerberos bind */
475 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
476 if( authmethod != -1 ) {
477 fprintf( stderr, "%s: -k incompatible with previous "
478 "authentication choice\n", prog );
479 exit( EXIT_FAILURE );
481 authmethod = LDAP_AUTH_KRBV4;
483 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
484 exit( EXIT_FAILURE );
487 case 'K': /* kerberos bind, part one only */
488 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
489 if( authmethod != -1 ) {
490 fprintf( stderr, "%s: incompatible with previous "
491 "authentication choice\n", prog );
492 exit( EXIT_FAILURE );
494 authmethod = LDAP_AUTH_KRBV41;
496 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
497 exit( EXIT_FAILURE );
501 /* enable Manage DSA IT */
504 case 'n': /* print operations, don't actually do them */
508 #ifdef HAVE_CYRUS_SASL
509 if( sasl_secprops != NULL ) {
510 fprintf( stderr, "%s: -O previously specified\n", prog );
511 exit( EXIT_FAILURE );
513 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
514 fprintf( stderr, "%s: incompatible previous "
515 "authentication choice\n", prog );
516 exit( EXIT_FAILURE );
518 authmethod = LDAP_AUTH_SASL;
519 sasl_secprops = ber_strdup( optarg );
521 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
522 exit( EXIT_FAILURE );
527 fprintf( stderr, "%s: -p previously specified\n", prog );
528 exit( EXIT_FAILURE );
530 ival = strtol( optarg, &next, 10 );
531 if ( next == NULL || next[0] != '\0' ) {
532 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
533 exit( EXIT_FAILURE );
538 ival = strtol( optarg, &next, 10 );
539 if ( next == NULL || next[0] != '\0' ) {
540 fprintf( stderr, "%s: unabel to parse protocol version \"%s\"\n", prog, optarg );
541 exit( EXIT_FAILURE );
545 if( protocol == LDAP_VERSION3 ) {
546 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
548 exit( EXIT_FAILURE );
550 protocol = LDAP_VERSION2;
553 if( protocol == LDAP_VERSION2 ) {
554 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
556 exit( EXIT_FAILURE );
558 protocol = LDAP_VERSION3;
561 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
567 #ifdef HAVE_CYRUS_SASL
568 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
569 fprintf( stderr, "%s: incompatible previous "
570 "authentication choice\n",
572 exit( EXIT_FAILURE );
574 authmethod = LDAP_AUTH_SASL;
575 sasl_flags = LDAP_SASL_QUIET;
578 fprintf( stderr, "%s: not compiled with SASL support\n",
580 exit( EXIT_FAILURE );
583 #ifdef HAVE_CYRUS_SASL
584 if( sasl_realm != NULL ) {
585 fprintf( stderr, "%s: -R previously specified\n", prog );
586 exit( EXIT_FAILURE );
588 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
589 fprintf( stderr, "%s: incompatible previous "
590 "authentication choice\n",
592 exit( EXIT_FAILURE );
594 authmethod = LDAP_AUTH_SASL;
595 sasl_realm = ber_strdup( optarg );
597 fprintf( stderr, "%s: not compiled with SASL support\n",
599 exit( EXIT_FAILURE );
603 #ifdef HAVE_CYRUS_SASL
604 if( sasl_authc_id != NULL ) {
605 fprintf( stderr, "%s: -U previously specified\n", prog );
606 exit( EXIT_FAILURE );
608 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
609 fprintf( stderr, "%s: incompatible previous "
610 "authentication choice\n",
612 exit( EXIT_FAILURE );
614 authmethod = LDAP_AUTH_SASL;
615 sasl_authc_id = ber_strdup( optarg );
617 fprintf( stderr, "%s: not compiled with SASL support\n",
619 exit( EXIT_FAILURE );
622 case 'v': /* verbose mode */
625 case 'V': /* version */
628 case 'w': /* password */
629 passwd.bv_val = ber_strdup( optarg );
633 for( p = optarg; *p != '\0'; p++ ) {
637 passwd.bv_len = strlen( passwd.bv_val );
646 #ifdef HAVE_CYRUS_SASL
647 if( sasl_mech != NULL ) {
648 fprintf( stderr, "%s: -Y previously specified\n", prog );
649 exit( EXIT_FAILURE );
651 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
653 "%s: incompatible with authentication choice\n", prog );
654 exit( EXIT_FAILURE );
656 authmethod = LDAP_AUTH_SASL;
657 sasl_mech = ber_strdup( optarg );
659 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
660 exit( EXIT_FAILURE );
664 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
665 fprintf( stderr, "%s: incompatible with previous "
666 "authentication choice\n", prog );
667 exit( EXIT_FAILURE );
669 authmethod = LDAP_AUTH_SIMPLE;
672 #ifdef HAVE_CYRUS_SASL
673 if( sasl_authz_id != NULL ) {
674 fprintf( stderr, "%s: -X previously specified\n", prog );
675 exit( EXIT_FAILURE );
677 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
678 fprintf( stderr, "%s: -X incompatible with "
679 "authentication choice\n", prog );
680 exit( EXIT_FAILURE );
682 authmethod = LDAP_AUTH_SASL;
683 sasl_authz_id = ber_strdup( optarg );
685 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
686 exit( EXIT_FAILURE );
693 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
694 exit( EXIT_FAILURE );
698 if( handle_private_option( i ) ) break;
699 fprintf( stderr, "%s: unrecognized option -%c\n",
706 /* prevent bad linking */
708 api.ldapai_info_version = LDAP_API_INFO_VERSION;
710 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
711 != LDAP_OPT_SUCCESS )
713 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
714 exit( EXIT_FAILURE );
717 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
718 fprintf( stderr, "LDAP APIInfo version mismatch: "
719 "library %d, header %d\n",
720 api.ldapai_info_version, LDAP_API_INFO_VERSION );
721 exit( EXIT_FAILURE );
724 if( api.ldapai_api_version != LDAP_API_VERSION ) {
725 fprintf( stderr, "LDAP API version mismatch: "
726 "library %d, header %d\n",
727 api.ldapai_api_version, LDAP_API_VERSION );
728 exit( EXIT_FAILURE );
731 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
732 fprintf( stderr, "LDAP vendor name mismatch: "
733 "library %s, header %s\n",
734 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
735 exit( EXIT_FAILURE );
738 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
739 fprintf( stderr, "LDAP vendor version mismatch: "
740 "library %d, header %d\n",
741 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
742 exit( EXIT_FAILURE );
746 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
748 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
749 if (version > 1) exit( EXIT_SUCCESS );
752 ldap_memfree( api.ldapai_vendor_name );
753 ber_memvfree( (void **)api.ldapai_extensions );
757 protocol = LDAP_VERSION3;
759 if (authmethod == -1 && protocol > LDAP_VERSION2) {
760 #ifdef HAVE_CYRUS_SASL
761 authmethod = LDAP_AUTH_SASL;
763 authmethod = LDAP_AUTH_SIMPLE;
767 if( ldapuri == NULL ) {
768 if( ldapport && ( ldaphost == NULL )) {
769 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
770 exit( EXIT_FAILURE );
773 if( ldaphost != NULL ) {
774 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
775 exit( EXIT_FAILURE );
778 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
779 exit( EXIT_FAILURE );
782 if( protocol == LDAP_VERSION2 ) {
783 if( assertctl || authzid || manageDIT || manageDSAit ||
784 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
787 noop || ppolicy || preread || postread )
789 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
790 exit( EXIT_FAILURE );
794 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
795 exit( EXIT_FAILURE );
798 #ifdef HAVE_CYRUS_SASL
799 if( authmethod == LDAP_AUTH_SASL ) {
800 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
802 exit( EXIT_FAILURE );
806 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
807 if ( authmethod == LDAP_AUTH_KRBV4 || authmethod == LDAP_AUTH_KRBV41 ) {
808 fprintf( stderr, "%s: -k/-K incompatible with LDAPv%d\n",
810 exit( EXIT_FAILURE );
818 tool_conn_setup( int not, void (*private_setup)( LDAP * ) )
823 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
824 != LBER_OPT_SUCCESS )
827 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
829 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
830 != LDAP_OPT_SUCCESS )
833 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
838 (void) SIGNAL( SIGPIPE, SIG_IGN );
842 SIGNAL( SIGINT, do_sig );
848 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
851 memset( &url, 0, sizeof(url));
853 url.lud_scheme = "ldap";
854 url.lud_host = ldaphost;
855 url.lud_port = ldapport;
856 url.lud_scope = LDAP_SCOPE_DEFAULT;
858 ldapuri = ldap_url_desc2str( &url );
862 fprintf( stderr, "ldap_initialize( %s )\n",
863 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
865 rc = ldap_initialize( &ld, ldapuri );
866 if( rc != LDAP_SUCCESS ) {
868 "Could not create LDAP session handle for URI=%s (%d): %s\n",
869 ldapuri, rc, ldap_err2string(rc) );
870 exit( EXIT_FAILURE );
873 if( private_setup ) private_setup( ld );
876 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
877 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
879 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
880 referrals ? "on" : "off" );
881 exit( EXIT_FAILURE );
884 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
885 != LDAP_OPT_SUCCESS )
887 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
889 exit( EXIT_FAILURE );
893 ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ))
895 ldap_perror( ld, "ldap_start_tls" );
897 exit( EXIT_FAILURE );
907 tool_bind( LDAP *ld )
909 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
911 LDAPControl *ctrls[2], c;
912 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
913 c.ldctl_value.bv_val = NULL;
914 c.ldctl_value.bv_len = 0;
915 c.ldctl_iscritical = 0;
918 ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
922 if ( authmethod == LDAP_AUTH_SASL ) {
923 #ifdef HAVE_CYRUS_SASL
927 if( sasl_secprops != NULL ) {
928 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
929 (void *) sasl_secprops );
931 if( rc != LDAP_OPT_SUCCESS ) {
933 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
935 exit( EXIT_FAILURE );
939 defaults = lutil_sasl_defaults( ld,
946 rc = ldap_sasl_interactive_bind_s( ld, binddn,
947 sasl_mech, NULL, NULL,
948 sasl_flags, lutil_sasl_interact, defaults );
950 lutil_sasl_freedefs( defaults );
951 if( rc != LDAP_SUCCESS ) {
952 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
953 exit( EXIT_FAILURE );
956 fprintf( stderr, "%s: not compiled with SASL support\n",
958 exit( EXIT_FAILURE );
965 char *matched = NULL;
971 msgid = ldap_bind( ld, binddn, passwd.bv_val, authmethod );
973 ldap_perror( ld, "ldap_bind" );
974 exit( EXIT_FAILURE );
977 if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) {
978 ldap_perror( ld, "ldap_result" );
979 exit( EXIT_FAILURE );
982 if ( ldap_parse_result( ld, result, &err, &matched, &info, &refs,
983 &ctrls, 1 ) != LDAP_SUCCESS )
985 ldap_perror( ld, "ldap_bind parse result" );
986 exit( EXIT_FAILURE );
989 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
990 if ( ctrls && ppolicy ) {
992 int expire, grace, len = 0;
993 LDAPPasswordPolicyError pErr = -1;
995 ctrl = ldap_find_control( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
998 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
999 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1001 if ( pErr != PP_noError ){
1004 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1005 len = strlen( msgbuf );
1007 if ( expire >= 0 ) {
1008 sprintf( msgbuf+len,
1009 " (Password expires in %d seconds)",
1011 } else if ( grace >= 0 ) {
1012 sprintf( msgbuf+len,
1013 " (Password expired, %d grace logins remain)",
1021 ldap_controls_free( ctrls );
1024 if ( err != LDAP_SUCCESS
1026 || ( matched && matched[ 0 ] )
1027 || ( info && info[ 0 ] )
1030 tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1032 if( matched ) ber_memfree( matched );
1033 if( info ) ber_memfree( info );
1034 if( refs ) ber_memvfree( (void **)refs );
1036 if ( err != LDAP_SUCCESS ) exit( EXIT_FAILURE );
1042 tool_unbind( LDAP *ld )
1044 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1046 if ( err != LDAP_OPT_SUCCESS ) {
1047 fprintf( stderr, "Could not unset controls\n");
1050 (void) ldap_unbind_ext( ld, NULL, NULL );
1054 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1056 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1058 int i = 0, j, crit = 0, err;
1059 LDAPControl c[10], **ctrls;
1061 ctrls = (LDAPControl**) malloc(sizeof(c) + (count+1)*sizeof(LDAPControl*));
1062 if ( ctrls == NULL ) {
1063 fprintf( stderr, "No memory\n" );
1064 exit( EXIT_FAILURE );
1068 BerElementBuffer berbuf;
1069 BerElement *ber = (BerElement *)&berbuf;
1071 if( assertion == NULL || *assertion == '\0' ) {
1072 fprintf( stderr, "Assertion=<empty>\n" );
1073 exit( EXIT_FAILURE );
1076 ber_init2( ber, NULL, LBER_USE_DER );
1078 err = ldap_pvt_put_filter( ber, assertion );
1080 fprintf( stderr, "assertion encode failed (%d)\n", err );
1081 exit( EXIT_FAILURE );
1084 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1086 fprintf( stderr, "assertion flatten failed (%d)\n", err );
1087 exit( EXIT_FAILURE );
1090 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1091 c[i].ldctl_iscritical = assertctl > 1;
1097 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1098 c[i].ldctl_value.bv_val = authzid;
1099 c[i].ldctl_value.bv_len = strlen( authzid );
1100 c[i].ldctl_iscritical = 1;
1106 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1107 BER_BVZERO( &c[i].ldctl_value );
1108 c[i].ldctl_iscritical = manageDIT > 1;
1113 if ( manageDSAit ) {
1114 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1115 BER_BVZERO( &c[i].ldctl_value );
1116 c[i].ldctl_iscritical = manageDSAit > 1;
1122 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1123 BER_BVZERO( &c[i].ldctl_value );
1124 c[i].ldctl_iscritical = noop > 1;
1130 char berbuf[LBER_ELEMENT_SIZEOF];
1131 BerElement *ber = (BerElement *)berbuf;
1132 char **attrs = NULL;
1134 if( preread_attrs ) {
1135 attrs = ldap_str2charray( preread_attrs, "," );
1138 ber_init2( ber, NULL, LBER_USE_DER );
1140 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1141 fprintf( stderr, "preread attrs encode failed.\n" );
1142 exit( EXIT_FAILURE );
1145 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1147 fprintf( stderr, "preread flatten failed (%d)\n", err );
1148 exit( EXIT_FAILURE );
1151 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1152 c[i].ldctl_iscritical = preread > 1;
1156 if( attrs ) ldap_charray_free( attrs );
1160 char berbuf[LBER_ELEMENT_SIZEOF];
1161 BerElement *ber = (BerElement *)berbuf;
1162 char **attrs = NULL;
1164 if( postread_attrs ) {
1165 attrs = ldap_str2charray( postread_attrs, "," );
1168 ber_init2( ber, NULL, LBER_USE_DER );
1170 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1171 fprintf( stderr, "postread attrs encode failed.\n" );
1172 exit( EXIT_FAILURE );
1175 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1177 fprintf( stderr, "postread flatten failed (%d)\n", err );
1178 exit( EXIT_FAILURE );
1181 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1182 c[i].ldctl_iscritical = postread > 1;
1186 if( attrs ) ldap_charray_free( attrs );
1189 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1191 if ( chainingResolve > -1 ) {
1192 BerElementBuffer berbuf;
1193 BerElement *ber = (BerElement *)&berbuf;
1195 ber_init2( ber, NULL, LBER_USE_DER );
1197 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1200 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1201 exit( EXIT_FAILURE );
1204 if ( chainingContinuation > -1 ) {
1205 err = ber_printf( ber, "e", chainingContinuation );
1208 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1209 exit( EXIT_FAILURE );
1213 err = ber_printf( ber, /* { */ "N}" );
1216 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1217 exit( EXIT_FAILURE );
1220 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1221 exit( EXIT_FAILURE );
1225 BER_BVZERO( &c[i].ldctl_value );
1228 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1229 c[i].ldctl_iscritical = chaining > 1;
1233 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1236 ctrls[i++] = extra_c++;
1240 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1242 if ( err != LDAP_OPT_SUCCESS ) {
1243 for ( j = 0; j < i; j++ ) {
1244 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1246 fprintf( stderr, "Could not set %scontrols\n",
1247 crit ? "critical " : "" );
1252 exit( EXIT_FAILURE );
1257 tool_check_abandon( LDAP *ld, int msgid )
1261 switch ( gotintr ) {
1262 case LDAP_REQ_EXTENDED:
1263 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1264 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1265 rc, ldap_err2string( rc ) );
1268 case LDAP_REQ_ABANDON:
1269 rc = ldap_abandon( ld, msgid );
1270 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1271 rc, ldap_err2string( rc ) );
1278 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1280 print_ppolicy( LDAP *ld, LDAPControl *ctrl )
1282 int expire = 0, grace = 0, rc;
1283 LDAPPasswordPolicyError pperr;
1285 rc = ldap_parse_passwordpolicy_control( ld, ctrl,
1286 &expire, &grace, &pperr );
1287 if ( rc == LDAP_SUCCESS ) {
1288 char buf[ BUFSIZ ], *ptr = buf;
1290 if ( expire != -1 ) {
1291 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1292 "expire=%d", expire );
1295 if ( grace != -1 ) {
1296 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1297 "%sgrace=%d", ptr == buf ? "" : " ", grace );
1300 if ( pperr != PP_noError ) {
1301 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1302 "%serror=%d (%s)", ptr == buf ? "" : " ",
1304 ldap_passwordpolicy_err2txt( pperr ) );
1307 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1308 "ppolicy", buf, ptr - buf );
1315 void tool_print_ctrls(
1317 LDAPControl **ctrls )
1322 for ( i = 0; ctrls[i] != NULL; i++ ) {
1323 /* control: OID criticality base64value */
1324 struct berval b64 = BER_BVNULL;
1330 len += strlen( ctrls[i]->ldctl_oid );
1332 /* add enough for space after OID and the critical value itself */
1333 len += ctrls[i]->ldctl_iscritical
1334 ? sizeof("true") : sizeof("false");
1336 /* convert to base64 */
1337 if ( ctrls[i]->ldctl_value.bv_len ) {
1338 b64.bv_len = LUTIL_BASE64_ENCODE_LEN(
1339 ctrls[i]->ldctl_value.bv_len ) + 1;
1340 b64.bv_val = ber_memalloc( b64.bv_len + 1 );
1342 b64.bv_len = lutil_b64_ntop(
1343 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
1344 ctrls[i]->ldctl_value.bv_len,
1345 b64.bv_val, b64.bv_len );
1349 len += 1 + b64.bv_len;
1352 ptr = str = malloc( len + 1 );
1354 ptr = lutil_strcopy( ptr, ": " );
1356 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_oid );
1357 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_iscritical
1358 ? " true" : " false" );
1361 ptr = lutil_strcopy( ptr, " " );
1362 ptr = lutil_strcopy( ptr, b64.bv_val );
1366 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1367 "control", str, len );
1372 ber_memfree( b64.bv_val );
1375 /* known controls */
1378 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1379 } else if ( strcmp( LDAP_CONTROL_PASSWORDPOLICYRESPONSE, ctrls[i]->ldctl_oid ) == 0 ) {
1380 (void)print_ppolicy( ld, ctrls[i] );
1387 tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
1391 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1395 fputs( ldif, stdout );
1396 ber_memfree( ldif );