1 /* common.c - common routines for the ldap client tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 The OpenLDAP Foundation.
6 * Portions Copyright 2003 Kurt D. Zeilenga.
7 * Portions Copyright 2003 IBM Corporation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
19 * This file was initially created by Hallvard B. Furuseth based (in
20 * part) upon argument parsing code for individual tools located in
21 * this directory. Additional contributors include:
22 * Kurt D. Zeilenga (additional common argument and control support)
29 #include <ac/stdlib.h>
30 #include <ac/signal.h>
31 #include <ac/string.h>
32 #include <ac/unistd.h>
35 #ifdef HAVE_CYRUS_SASL
36 #ifdef HAVE_SASL_SASL_H
37 #include <sasl/sasl.h>
45 #include "lutil_ldap.h"
46 #include "ldap_defaults.h"
54 * NOTE: we declare it here only because we want to keep supporting
55 * (how long?) ancient, deprecated LDAP_AUTH_KRB* auth methods
58 ldap_bind LDAP_P(( /* deprecated */
61 LDAP_CONST char *passwd,
71 char *ldaphost = NULL;
73 #ifdef HAVE_CYRUS_SASL
74 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
75 char *sasl_realm = NULL;
76 char *sasl_authc_id = NULL;
77 char *sasl_authz_id = NULL;
78 char *sasl_mech = NULL;
79 char *sasl_secprops = NULL;
84 char *assertion = NULL;
91 char *preread_attrs = NULL;
93 char *postread_attrs = NULL;
97 struct berval passwd = { 0, NULL };
104 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
106 static int chainingResolve = -1;
107 static int chainingContinuation = -1;
108 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
125 ldap_pvt_setlocale(LC_MESSAGES, "");
126 ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
127 ldap_pvt_textdomain(OPENLDAP_PACKAGE);
133 #ifdef HAVE_CYRUS_SASL
137 ldap_pvt_tls_destroy();
142 tool_common_usage( void )
144 static const char *const descriptions[] = {
145 N_(" -c continuous operation mode (do not stop on errors)\n"),
146 N_(" -C chase referrals (anonymously)\n"),
147 N_(" -d level set LDAP debugging level to `level'\n"),
148 N_(" -D binddn bind DN\n"),
149 N_(" -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
150 N_(" [!]assert=<filter> (an RFC 2254 Filter)\n")
151 N_(" [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n")
152 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
153 N_(" [!]chaining[=<resolveBehavior>[/<continuationBehavior>]]\n")
154 N_(" one of \"chainingPreferred\", \"chainingRequired\",\n")
155 N_(" \"referralsPreferred\", \"referralsRequired\"\n")
156 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
158 N_(" [!]manageDIT\n")
160 N_(" [!]manageDSAit\n")
162 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
165 N_(" [!]postread[=<attrs>] (a comma-separated attribute list)\n")
166 N_(" [!]preread[=<attrs>] (a comma-separated attribute list)\n"),
167 N_(" abandon, cancel (SIGINT sends abandon/cancel; not really controls)\n")
168 N_(" -f file read operations from `file'\n"),
169 N_(" -h host LDAP server\n"),
170 N_(" -H URI LDAP Uniform Resource Indentifier(s)\n"),
171 N_(" -I use SASL Interactive mode\n"),
172 N_(" -k use Kerberos authentication\n"),
173 N_(" -K like -k, but do only step 1 of the Kerberos bind\n"),
174 N_(" -M enable Manage DSA IT control (-MM to make critical)\n"),
175 N_(" -n show what would be done but don't actually do it\n"),
176 N_(" -O props SASL security properties\n"),
177 N_(" -p port port on LDAP server\n"),
178 N_(" -P version procotol version (default: 3)\n"),
179 N_(" -Q use SASL Quiet mode\n"),
180 N_(" -R realm SASL realm\n"),
181 N_(" -U authcid SASL authentication identity\n"),
182 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
183 N_(" -V print version info (-VV only)\n"),
184 N_(" -w passwd bind password (for simple authentication)\n"),
185 N_(" -W prompt for bind password\n"),
186 N_(" -x Simple authentication\n"),
187 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
188 N_(" -y file Read password from file\n"),
189 N_(" -Y mech SASL mechanism\n"),
190 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
193 const char *const *cpp;
195 fputs( _("Common options:\n"), stderr );
196 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
197 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
198 fputs( _(*cpp), stderr );
211 fprintf( stderr, "%s: %s (%d)%s\n",
212 func, ldap_err2string( err ), err, extra ? extra : "" );
214 if ( matched && *matched ) {
215 fprintf( stderr, _("\tmatched DN: %s\n"), matched );
218 if ( info && *info ) {
219 fprintf( stderr, _("\tadditional info: %s\n"), info );
222 if ( refs && *refs ) {
224 fprintf( stderr, _("\treferrals:\n") );
225 for( i=0; refs[i]; i++ ) {
226 fprintf( stderr, "\t\t%s\n", refs[i] );
233 tool_args( int argc, char **argv )
237 while (( i = getopt( argc, argv, options )) != EOF ) {
239 char *control, *cvalue, *next;
241 case 'c': /* continuous operation mode */
248 ival = strtol( optarg, &next, 10 );
249 if (next == NULL || next[0] != '\0') {
250 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
255 case 'D': /* bind DN */
256 if( binddn != NULL ) {
257 fprintf( stderr, "%s: -D previously specified\n", prog );
258 exit( EXIT_FAILURE );
260 binddn = ber_strdup( optarg );
262 case 'e': /* general extensions (controls and such) */
263 /* should be extended to support comma separated list of
264 * [!]key[=value] parameters, e.g. -e !foo,bar=567
269 if( optarg[0] == '!' ) {
274 control = ber_strdup( optarg );
275 if ( (cvalue = strchr( control, '=' )) != NULL ) {
279 if ( strcasecmp( control, "assert" ) == 0 ) {
281 fprintf( stderr, "assert control previously specified\n");
282 exit( EXIT_FAILURE );
284 if( cvalue == NULL ) {
285 fprintf( stderr, "assert: control value expected\n" );
289 assertctl = 1 + crit;
291 assert( assertion == NULL );
294 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
295 if( authzid != NULL ) {
296 fprintf( stderr, "authzid control previously specified\n");
297 exit( EXIT_FAILURE );
299 if( cvalue == NULL ) {
300 fprintf( stderr, "authzid: control value expected\n" );
304 fprintf( stderr, "authzid: must be marked critical\n" );
308 assert( authzid == NULL );
311 } else if ( strcasecmp( control, "manageDIT" ) == 0 ) {
314 "manageDIT control previously specified\n");
315 exit( EXIT_FAILURE );
317 if( cvalue != NULL ) {
319 "manageDIT: no control value expected\n" );
323 manageDIT = 1 + crit;
325 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
328 "manageDSAit control previously specified\n");
329 exit( EXIT_FAILURE );
331 if( cvalue != NULL ) {
333 "manageDSAit: no control value expected\n" );
337 manageDSAit = 1 + crit;
339 } else if ( strcasecmp( control, "noop" ) == 0 ) {
341 fprintf( stderr, "noop control previously specified\n");
342 exit( EXIT_FAILURE );
344 if( cvalue != NULL ) {
345 fprintf( stderr, "noop: no control value expected\n" );
351 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
352 } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
354 fprintf( stderr, "ppolicy control previously specified\n");
355 exit( EXIT_FAILURE );
357 if( cvalue != NULL ) {
358 fprintf( stderr, "ppolicy: no control value expected\n" );
362 fprintf( stderr, "ppolicy: critical flag not allowed\n" );
369 } else if ( strcasecmp( control, "preread" ) == 0 ) {
371 fprintf( stderr, "preread control previously specified\n");
372 exit( EXIT_FAILURE );
376 preread_attrs = cvalue;
378 } else if ( strcasecmp( control, "postread" ) == 0 ) {
380 fprintf( stderr, "postread control previously specified\n");
381 exit( EXIT_FAILURE );
385 postread_attrs = cvalue;
387 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
388 } else if ( strcasecmp( control, "chaining" ) == 0 ) {
391 if ( cvalue != NULL ) {
394 continuation = strchr( cvalue, '/' );
395 if ( continuation ) {
396 /* FIXME: this makes sense only in searches */
397 *continuation++ = '\0';
398 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
399 chainingContinuation = LDAP_CHAINING_PREFERRED;
400 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
401 chainingContinuation = LDAP_CHAINING_REQUIRED;
402 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
403 chainingContinuation = LDAP_REFERRALS_PREFERRED;
404 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
405 chainingContinuation = LDAP_REFERRALS_REQUIRED;
408 "chaining behavior control "
409 "continuation value \"%s\" invalid\n",
411 exit( EXIT_FAILURE );
415 if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
416 chainingResolve = LDAP_CHAINING_PREFERRED;
417 } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
418 chainingResolve = LDAP_CHAINING_REQUIRED;
419 } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
420 chainingResolve = LDAP_REFERRALS_PREFERRED;
421 } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
422 chainingResolve = LDAP_REFERRALS_REQUIRED;
425 "chaining behavior control "
426 "resolve value \"%s\" invalid\n",
428 exit( EXIT_FAILURE );
431 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
433 /* this shouldn't go here, really; but it's a feature... */
434 } else if ( strcasecmp( control, "abandon" ) == 0 ) {
435 abcan = LDAP_REQ_ABANDON;
437 } else if ( strcasecmp( control, "cancel" ) == 0 ) {
438 abcan = LDAP_REQ_EXTENDED;
441 fprintf( stderr, "Invalid general control name: %s\n",
446 case 'f': /* read from file */
447 if( infile != NULL ) {
448 fprintf( stderr, "%s: -f previously specified\n", prog );
449 exit( EXIT_FAILURE );
451 infile = ber_strdup( optarg );
453 case 'h': /* ldap host */
454 if( ldaphost != NULL ) {
455 fprintf( stderr, "%s: -h previously specified\n", prog );
456 exit( EXIT_FAILURE );
458 ldaphost = ber_strdup( optarg );
460 case 'H': /* ldap URI */
461 if( ldapuri != NULL ) {
462 fprintf( stderr, "%s: -H previously specified\n", prog );
463 exit( EXIT_FAILURE );
465 ldapuri = ber_strdup( optarg );
468 #ifdef HAVE_CYRUS_SASL
469 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
470 fprintf( stderr, "%s: incompatible previous "
471 "authentication choice\n",
473 exit( EXIT_FAILURE );
475 authmethod = LDAP_AUTH_SASL;
476 sasl_flags = LDAP_SASL_INTERACTIVE;
479 fprintf( stderr, "%s: was not compiled with SASL support\n",
481 exit( EXIT_FAILURE );
483 case 'k': /* kerberos bind */
484 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
485 if( authmethod != -1 ) {
486 fprintf( stderr, "%s: -k incompatible with previous "
487 "authentication choice\n", prog );
488 exit( EXIT_FAILURE );
490 authmethod = LDAP_AUTH_KRBV4;
492 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
493 exit( EXIT_FAILURE );
496 case 'K': /* kerberos bind, part one only */
497 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
498 if( authmethod != -1 ) {
499 fprintf( stderr, "%s: incompatible with previous "
500 "authentication choice\n", prog );
501 exit( EXIT_FAILURE );
503 authmethod = LDAP_AUTH_KRBV41;
505 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
506 exit( EXIT_FAILURE );
510 /* enable Manage DSA IT */
513 case 'n': /* print operations, don't actually do them */
517 #ifdef HAVE_CYRUS_SASL
518 if( sasl_secprops != NULL ) {
519 fprintf( stderr, "%s: -O previously specified\n", prog );
520 exit( EXIT_FAILURE );
522 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
523 fprintf( stderr, "%s: incompatible previous "
524 "authentication choice\n", prog );
525 exit( EXIT_FAILURE );
527 authmethod = LDAP_AUTH_SASL;
528 sasl_secprops = ber_strdup( optarg );
530 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
531 exit( EXIT_FAILURE );
536 fprintf( stderr, "%s: -p previously specified\n", prog );
537 exit( EXIT_FAILURE );
539 ival = strtol( optarg, &next, 10 );
540 if ( next == NULL || next[0] != '\0' ) {
541 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
542 exit( EXIT_FAILURE );
547 ival = strtol( optarg, &next, 10 );
548 if ( next == NULL || next[0] != '\0' ) {
549 fprintf( stderr, "%s: unabel to parse protocol version \"%s\"\n", prog, optarg );
550 exit( EXIT_FAILURE );
554 if( protocol == LDAP_VERSION3 ) {
555 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
557 exit( EXIT_FAILURE );
559 protocol = LDAP_VERSION2;
562 if( protocol == LDAP_VERSION2 ) {
563 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
565 exit( EXIT_FAILURE );
567 protocol = LDAP_VERSION3;
570 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
576 #ifdef HAVE_CYRUS_SASL
577 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
578 fprintf( stderr, "%s: incompatible previous "
579 "authentication choice\n",
581 exit( EXIT_FAILURE );
583 authmethod = LDAP_AUTH_SASL;
584 sasl_flags = LDAP_SASL_QUIET;
587 fprintf( stderr, "%s: not compiled with SASL support\n",
589 exit( EXIT_FAILURE );
592 #ifdef HAVE_CYRUS_SASL
593 if( sasl_realm != NULL ) {
594 fprintf( stderr, "%s: -R 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",
601 exit( EXIT_FAILURE );
603 authmethod = LDAP_AUTH_SASL;
604 sasl_realm = ber_strdup( optarg );
606 fprintf( stderr, "%s: not compiled with SASL support\n",
608 exit( EXIT_FAILURE );
612 #ifdef HAVE_CYRUS_SASL
613 if( sasl_authc_id != NULL ) {
614 fprintf( stderr, "%s: -U previously specified\n", prog );
615 exit( EXIT_FAILURE );
617 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
618 fprintf( stderr, "%s: incompatible previous "
619 "authentication choice\n",
621 exit( EXIT_FAILURE );
623 authmethod = LDAP_AUTH_SASL;
624 sasl_authc_id = ber_strdup( optarg );
626 fprintf( stderr, "%s: not compiled with SASL support\n",
628 exit( EXIT_FAILURE );
631 case 'v': /* verbose mode */
634 case 'V': /* version */
637 case 'w': /* password */
638 passwd.bv_val = ber_strdup( optarg );
642 for( p = optarg; *p != '\0'; p++ ) {
646 passwd.bv_len = strlen( passwd.bv_val );
655 #ifdef HAVE_CYRUS_SASL
656 if( sasl_mech != NULL ) {
657 fprintf( stderr, "%s: -Y previously specified\n", prog );
658 exit( EXIT_FAILURE );
660 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
662 "%s: incompatible with authentication choice\n", prog );
663 exit( EXIT_FAILURE );
665 authmethod = LDAP_AUTH_SASL;
666 sasl_mech = ber_strdup( optarg );
668 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
669 exit( EXIT_FAILURE );
673 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
674 fprintf( stderr, "%s: incompatible with previous "
675 "authentication choice\n", prog );
676 exit( EXIT_FAILURE );
678 authmethod = LDAP_AUTH_SIMPLE;
681 #ifdef HAVE_CYRUS_SASL
682 if( sasl_authz_id != NULL ) {
683 fprintf( stderr, "%s: -X previously specified\n", prog );
684 exit( EXIT_FAILURE );
686 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
687 fprintf( stderr, "%s: -X incompatible with "
688 "authentication choice\n", prog );
689 exit( EXIT_FAILURE );
691 authmethod = LDAP_AUTH_SASL;
692 sasl_authz_id = ber_strdup( optarg );
694 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
695 exit( EXIT_FAILURE );
702 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
703 exit( EXIT_FAILURE );
707 if( handle_private_option( i ) ) break;
708 fprintf( stderr, "%s: unrecognized option -%c\n",
715 /* prevent bad linking */
717 api.ldapai_info_version = LDAP_API_INFO_VERSION;
719 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
720 != LDAP_OPT_SUCCESS )
722 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
723 exit( EXIT_FAILURE );
726 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
727 fprintf( stderr, "LDAP APIInfo version mismatch: "
728 "library %d, header %d\n",
729 api.ldapai_info_version, LDAP_API_INFO_VERSION );
730 exit( EXIT_FAILURE );
733 if( api.ldapai_api_version != LDAP_API_VERSION ) {
734 fprintf( stderr, "LDAP API version mismatch: "
735 "library %d, header %d\n",
736 api.ldapai_api_version, LDAP_API_VERSION );
737 exit( EXIT_FAILURE );
740 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
741 fprintf( stderr, "LDAP vendor name mismatch: "
742 "library %s, header %s\n",
743 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
744 exit( EXIT_FAILURE );
747 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
748 fprintf( stderr, "LDAP vendor version mismatch: "
749 "library %d, header %d\n",
750 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
751 exit( EXIT_FAILURE );
755 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
757 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
758 if (version > 1) exit( EXIT_SUCCESS );
761 ldap_memfree( api.ldapai_vendor_name );
762 ber_memvfree( (void **)api.ldapai_extensions );
766 protocol = LDAP_VERSION3;
768 if (authmethod == -1 && protocol > LDAP_VERSION2) {
769 #ifdef HAVE_CYRUS_SASL
770 authmethod = LDAP_AUTH_SASL;
772 authmethod = LDAP_AUTH_SIMPLE;
776 if( ldapuri == NULL ) {
777 if( ldapport && ( ldaphost == NULL )) {
778 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
779 exit( EXIT_FAILURE );
782 if( ldaphost != NULL ) {
783 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
784 exit( EXIT_FAILURE );
787 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
788 exit( EXIT_FAILURE );
791 if( protocol == LDAP_VERSION2 ) {
792 if( assertctl || authzid || manageDIT || manageDSAit ||
793 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
796 noop || ppolicy || preread || postread )
798 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
799 exit( EXIT_FAILURE );
803 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
804 exit( EXIT_FAILURE );
807 #ifdef HAVE_CYRUS_SASL
808 if( authmethod == LDAP_AUTH_SASL ) {
809 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
811 exit( EXIT_FAILURE );
815 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
816 if ( authmethod == LDAP_AUTH_KRBV4 || authmethod == LDAP_AUTH_KRBV41 ) {
817 fprintf( stderr, "%s: -k/-K incompatible with LDAPv%d\n",
819 exit( EXIT_FAILURE );
827 tool_conn_setup( int not, void (*private_setup)( LDAP * ) )
832 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
833 != LBER_OPT_SUCCESS )
836 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
838 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
839 != LDAP_OPT_SUCCESS )
842 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
847 (void) SIGNAL( SIGPIPE, SIG_IGN );
851 SIGNAL( SIGINT, do_sig );
857 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
860 memset( &url, 0, sizeof(url));
862 url.lud_scheme = "ldap";
863 url.lud_host = ldaphost;
864 url.lud_port = ldapport;
865 url.lud_scope = LDAP_SCOPE_DEFAULT;
867 ldapuri = ldap_url_desc2str( &url );
871 fprintf( stderr, "ldap_initialize( %s )\n",
872 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
874 rc = ldap_initialize( &ld, ldapuri );
875 if( rc != LDAP_SUCCESS ) {
877 "Could not create LDAP session handle for URI=%s (%d): %s\n",
878 ldapuri, rc, ldap_err2string(rc) );
879 exit( EXIT_FAILURE );
882 if( private_setup ) private_setup( ld );
885 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
886 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
888 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
889 referrals ? "on" : "off" );
890 exit( EXIT_FAILURE );
893 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
894 != LDAP_OPT_SUCCESS )
896 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
898 exit( EXIT_FAILURE );
902 rc = ldap_start_tls_s( ld, NULL, NULL );
903 if ( rc != LDAP_SUCCESS ) {
904 tool_perror( "ldap_start_tls", rc, NULL, NULL, NULL, NULL );
906 exit( EXIT_FAILURE );
917 tool_bind( LDAP *ld )
919 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
921 LDAPControl *ctrls[2], c;
922 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
923 c.ldctl_value.bv_val = NULL;
924 c.ldctl_value.bv_len = 0;
925 c.ldctl_iscritical = 0;
928 ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
932 if ( authmethod == LDAP_AUTH_SASL ) {
933 #ifdef HAVE_CYRUS_SASL
937 if( sasl_secprops != NULL ) {
938 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
939 (void *) sasl_secprops );
941 if( rc != LDAP_OPT_SUCCESS ) {
943 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
945 exit( EXIT_FAILURE );
949 defaults = lutil_sasl_defaults( ld,
956 rc = ldap_sasl_interactive_bind_s( ld, binddn,
957 sasl_mech, NULL, NULL,
958 sasl_flags, lutil_sasl_interact, defaults );
960 lutil_sasl_freedefs( defaults );
961 if( rc != LDAP_SUCCESS ) {
962 tool_perror( "ldap_sasl_interactive_bind_s",
963 rc, NULL, NULL, NULL, NULL );
964 exit( EXIT_FAILURE );
967 fprintf( stderr, "%s: not compiled with SASL support\n",
969 exit( EXIT_FAILURE );
976 char *matched = NULL;
982 msgid = ldap_bind( ld, binddn, passwd.bv_val, authmethod );
984 tool_perror( "ldap_bind", -1, NULL, NULL, NULL, NULL );
985 exit( EXIT_FAILURE );
988 if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) {
989 tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
990 exit( EXIT_FAILURE );
993 rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
995 if ( rc != LDAP_SUCCESS ) {
996 tool_perror( "ldap_bind parse result", rc, NULL, NULL, NULL, NULL );
997 exit( EXIT_FAILURE );
1000 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1001 if ( ctrls && ppolicy ) {
1003 int expire, grace, len = 0;
1004 LDAPPasswordPolicyError pErr = -1;
1006 ctrl = ldap_find_control( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
1009 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
1010 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1012 if ( pErr != PP_noError ){
1015 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1016 len = strlen( msgbuf );
1018 if ( expire >= 0 ) {
1019 sprintf( msgbuf+len,
1020 " (Password expires in %d seconds)",
1022 } else if ( grace >= 0 ) {
1023 sprintf( msgbuf+len,
1024 " (Password expired, %d grace logins remain)",
1032 ldap_controls_free( ctrls );
1035 if ( err != LDAP_SUCCESS
1037 || ( matched && matched[ 0 ] )
1038 || ( info && info[ 0 ] )
1041 tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1043 if( matched ) ber_memfree( matched );
1044 if( info ) ber_memfree( info );
1045 if( refs ) ber_memvfree( (void **)refs );
1047 if ( err != LDAP_SUCCESS ) exit( EXIT_FAILURE );
1053 tool_unbind( LDAP *ld )
1055 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1057 if ( err != LDAP_OPT_SUCCESS ) {
1058 fprintf( stderr, "Could not unset controls\n");
1061 (void) ldap_unbind_ext( ld, NULL, NULL );
1065 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1067 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1069 int i = 0, j, crit = 0, err;
1070 LDAPControl c[10], **ctrls;
1072 ctrls = (LDAPControl**) malloc(sizeof(c) + (count+1)*sizeof(LDAPControl*));
1073 if ( ctrls == NULL ) {
1074 fprintf( stderr, "No memory\n" );
1075 exit( EXIT_FAILURE );
1079 BerElementBuffer berbuf;
1080 BerElement *ber = (BerElement *)&berbuf;
1082 if( assertion == NULL || *assertion == '\0' ) {
1083 fprintf( stderr, "Assertion=<empty>\n" );
1084 exit( EXIT_FAILURE );
1087 ber_init2( ber, NULL, LBER_USE_DER );
1089 err = ldap_pvt_put_filter( ber, assertion );
1091 fprintf( stderr, "assertion encode failed (%d)\n", err );
1092 exit( EXIT_FAILURE );
1095 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1097 fprintf( stderr, "assertion flatten failed (%d)\n", err );
1098 exit( EXIT_FAILURE );
1101 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1102 c[i].ldctl_iscritical = assertctl > 1;
1108 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1109 c[i].ldctl_value.bv_val = authzid;
1110 c[i].ldctl_value.bv_len = strlen( authzid );
1111 c[i].ldctl_iscritical = 1;
1117 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1118 BER_BVZERO( &c[i].ldctl_value );
1119 c[i].ldctl_iscritical = manageDIT > 1;
1124 if ( manageDSAit ) {
1125 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1126 BER_BVZERO( &c[i].ldctl_value );
1127 c[i].ldctl_iscritical = manageDSAit > 1;
1133 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1134 BER_BVZERO( &c[i].ldctl_value );
1135 c[i].ldctl_iscritical = noop > 1;
1141 char berbuf[LBER_ELEMENT_SIZEOF];
1142 BerElement *ber = (BerElement *)berbuf;
1143 char **attrs = NULL;
1145 if( preread_attrs ) {
1146 attrs = ldap_str2charray( preread_attrs, "," );
1149 ber_init2( ber, NULL, LBER_USE_DER );
1151 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1152 fprintf( stderr, "preread attrs encode failed.\n" );
1153 exit( EXIT_FAILURE );
1156 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1158 fprintf( stderr, "preread flatten failed (%d)\n", err );
1159 exit( EXIT_FAILURE );
1162 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1163 c[i].ldctl_iscritical = preread > 1;
1167 if( attrs ) ldap_charray_free( attrs );
1171 char berbuf[LBER_ELEMENT_SIZEOF];
1172 BerElement *ber = (BerElement *)berbuf;
1173 char **attrs = NULL;
1175 if( postread_attrs ) {
1176 attrs = ldap_str2charray( postread_attrs, "," );
1179 ber_init2( ber, NULL, LBER_USE_DER );
1181 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1182 fprintf( stderr, "postread attrs encode failed.\n" );
1183 exit( EXIT_FAILURE );
1186 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1188 fprintf( stderr, "postread flatten failed (%d)\n", err );
1189 exit( EXIT_FAILURE );
1192 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1193 c[i].ldctl_iscritical = postread > 1;
1197 if( attrs ) ldap_charray_free( attrs );
1200 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1202 if ( chainingResolve > -1 ) {
1203 BerElementBuffer berbuf;
1204 BerElement *ber = (BerElement *)&berbuf;
1206 ber_init2( ber, NULL, LBER_USE_DER );
1208 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1211 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1212 exit( EXIT_FAILURE );
1215 if ( chainingContinuation > -1 ) {
1216 err = ber_printf( ber, "e", chainingContinuation );
1219 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1220 exit( EXIT_FAILURE );
1224 err = ber_printf( ber, /* { */ "N}" );
1227 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1228 exit( EXIT_FAILURE );
1231 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1232 exit( EXIT_FAILURE );
1236 BER_BVZERO( &c[i].ldctl_value );
1239 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1240 c[i].ldctl_iscritical = chaining > 1;
1244 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1247 ctrls[i++] = extra_c++;
1251 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1253 if ( err != LDAP_OPT_SUCCESS ) {
1254 for ( j = 0; j < i; j++ ) {
1255 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1257 fprintf( stderr, "Could not set %scontrols\n",
1258 crit ? "critical " : "" );
1263 exit( EXIT_FAILURE );
1268 tool_check_abandon( LDAP *ld, int msgid )
1272 switch ( gotintr ) {
1273 case LDAP_REQ_EXTENDED:
1274 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1275 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1276 rc, ldap_err2string( rc ) );
1279 case LDAP_REQ_ABANDON:
1280 rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
1281 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1282 rc, ldap_err2string( rc ) );