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>
37 #include "lutil_ldap.h"
38 #include "ldap_defaults.h"
51 char *ldaphost = NULL;
53 #ifdef HAVE_CYRUS_SASL
54 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
55 char *sasl_realm = NULL;
56 char *sasl_authc_id = NULL;
57 char *sasl_authz_id = NULL;
58 char *sasl_mech = NULL;
59 char *sasl_secprops = NULL;
64 char *assertion = NULL;
71 char *preread_attrs = NULL;
73 char *postread_attrs = NULL;
77 struct berval passwd = { 0, NULL };
84 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
86 static int chainingResolve = -1;
87 static int chainingContinuation = -1;
88 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
105 ldap_pvt_setlocale(LC_MESSAGES, "");
106 ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
107 ldap_pvt_textdomain(OPENLDAP_PACKAGE);
113 #ifdef HAVE_CYRUS_SASL
117 ldap_pvt_tls_destroy();
122 tool_common_usage( void )
124 static const char *const descriptions[] = {
125 N_(" -c continuous operation mode (do not stop on errors)\n"),
126 N_(" -C chase referrals (anonymously)\n"),
127 N_(" -d level set LDAP debugging level to `level'\n"),
128 N_(" -D binddn bind DN\n"),
129 N_(" -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
130 N_(" [!]assert=<filter> (an RFC 2254 Filter)\n")
131 N_(" [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n")
132 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
133 N_(" [!]chaining[=<resolveBehavior>[/<continuationBehavior>]]\n")
134 N_(" one of \"chainingPreferred\", \"chainingRequired\",\n")
135 N_(" \"referralsPreferred\", \"referralsRequired\"\n")
136 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
138 N_(" [!]manageDIT\n")
140 N_(" [!]manageDSAit\n")
142 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
145 N_(" [!]postread[=<attrs>] (a comma-separated attribute list)\n")
146 N_(" [!]preread[=<attrs>] (a comma-separated attribute list)\n"),
147 N_(" abandon, cancel (SIGINT sends abandon/cancel; not really controls)\n")
148 N_(" -f file read operations from `file'\n"),
149 N_(" -h host LDAP server\n"),
150 N_(" -H URI LDAP Uniform Resource Indentifier(s)\n"),
151 N_(" -I use SASL Interactive mode\n"),
152 N_(" -k use Kerberos authentication\n"),
153 N_(" -K like -k, but do only step 1 of the Kerberos bind\n"),
154 N_(" -M enable Manage DSA IT control (-MM to make critical)\n"),
155 N_(" -n show what would be done but don't actually do it\n"),
156 N_(" -O props SASL security properties\n"),
157 N_(" -p port port on LDAP server\n"),
158 N_(" -P version procotol version (default: 3)\n"),
159 N_(" -Q use SASL Quiet mode\n"),
160 N_(" -R realm SASL realm\n"),
161 N_(" -U authcid SASL authentication identity\n"),
162 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
163 N_(" -V print version info (-VV only)\n"),
164 N_(" -w passwd bind password (for simple authentication)\n"),
165 N_(" -W prompt for bind password\n"),
166 N_(" -x Simple authentication\n"),
167 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
168 N_(" -y file Read password from file\n"),
169 N_(" -Y mech SASL mechanism\n"),
170 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
173 const char *const *cpp;
175 fputs( _("Common options:\n"), stderr );
176 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
177 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
178 fputs( _(*cpp), stderr );
185 tool_args( int argc, char **argv )
189 while (( i = getopt( argc, argv, options )) != EOF ) {
191 char *control, *cvalue, *next;
193 case 'c': /* continuous operation mode */
200 ival = strtol( optarg, &next, 10 );
201 if (next == NULL || next[0] != '\0') {
202 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
207 case 'D': /* bind DN */
208 if( binddn != NULL ) {
209 fprintf( stderr, "%s: -D previously specified\n", prog );
210 exit( EXIT_FAILURE );
212 binddn = ber_strdup( optarg );
214 case 'e': /* general extensions (controls and such) */
215 /* should be extended to support comma separated list of
216 * [!]key[=value] parameters, e.g. -e !foo,bar=567
221 if( optarg[0] == '!' ) {
226 control = ber_strdup( optarg );
227 if ( (cvalue = strchr( control, '=' )) != NULL ) {
231 if ( strcasecmp( control, "assert" ) == 0 ) {
233 fprintf( stderr, "assert control previously specified\n");
234 exit( EXIT_FAILURE );
236 if( cvalue == NULL ) {
237 fprintf( stderr, "assert: control value expected\n" );
241 assertctl = 1 + crit;
243 assert( assertion == NULL );
246 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
247 if( authzid != NULL ) {
248 fprintf( stderr, "authzid control previously specified\n");
249 exit( EXIT_FAILURE );
251 if( cvalue == NULL ) {
252 fprintf( stderr, "authzid: control value expected\n" );
256 fprintf( stderr, "authzid: must be marked critical\n" );
260 assert( authzid == NULL );
263 } else if ( strcasecmp( control, "manageDIT" ) == 0 ) {
266 "manageDIT control previously specified\n");
267 exit( EXIT_FAILURE );
269 if( cvalue != NULL ) {
271 "manageDIT: no control value expected\n" );
275 manageDIT = 1 + crit;
277 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
280 "manageDSAit control previously specified\n");
281 exit( EXIT_FAILURE );
283 if( cvalue != NULL ) {
285 "manageDSAit: no control value expected\n" );
289 manageDSAit = 1 + crit;
291 } else if ( strcasecmp( control, "noop" ) == 0 ) {
293 fprintf( stderr, "noop control previously specified\n");
294 exit( EXIT_FAILURE );
296 if( cvalue != NULL ) {
297 fprintf( stderr, "noop: no control value expected\n" );
303 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
304 } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
306 fprintf( stderr, "ppolicy control previously specified\n");
307 exit( EXIT_FAILURE );
309 if( cvalue != NULL ) {
310 fprintf( stderr, "ppolicy: no control value expected\n" );
314 fprintf( stderr, "ppolicy: critical flag not allowed\n" );
321 } else if ( strcasecmp( control, "preread" ) == 0 ) {
323 fprintf( stderr, "preread control previously specified\n");
324 exit( EXIT_FAILURE );
328 preread_attrs = cvalue;
330 } else if ( strcasecmp( control, "postread" ) == 0 ) {
332 fprintf( stderr, "postread control previously specified\n");
333 exit( EXIT_FAILURE );
337 postread_attrs = cvalue;
339 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
340 } else if ( strcasecmp( control, "chaining" ) == 0 ) {
343 if ( cvalue != NULL ) {
346 continuation = strchr( cvalue, '/' );
347 if ( continuation ) {
348 /* FIXME: this makes sense only in searches */
349 *continuation++ = '\0';
350 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
351 chainingContinuation = LDAP_CHAINING_PREFERRED;
352 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
353 chainingContinuation = LDAP_CHAINING_REQUIRED;
354 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
355 chainingContinuation = LDAP_REFERRALS_PREFERRED;
356 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
357 chainingContinuation = LDAP_REFERRALS_REQUIRED;
360 "chaining behavior control "
361 "continuation value \"%s\" invalid\n",
363 exit( EXIT_FAILURE );
367 if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
368 chainingResolve = LDAP_CHAINING_PREFERRED;
369 } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
370 chainingResolve = LDAP_CHAINING_REQUIRED;
371 } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
372 chainingResolve = LDAP_REFERRALS_PREFERRED;
373 } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
374 chainingResolve = LDAP_REFERRALS_REQUIRED;
377 "chaining behavior control "
378 "resolve value \"%s\" invalid\n",
380 exit( EXIT_FAILURE );
383 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
385 /* this shouldn't go here, really; but it's a feature... */
386 } else if ( strcasecmp( control, "abandon" ) == 0 ) {
387 abcan = LDAP_REQ_ABANDON;
389 } else if ( strcasecmp( control, "cancel" ) == 0 ) {
390 abcan = LDAP_REQ_EXTENDED;
393 fprintf( stderr, "Invalid general control name: %s\n",
398 case 'f': /* read from file */
399 if( infile != NULL ) {
400 fprintf( stderr, "%s: -f previously specified\n", prog );
401 exit( EXIT_FAILURE );
403 infile = ber_strdup( optarg );
405 case 'h': /* ldap host */
406 if( ldaphost != NULL ) {
407 fprintf( stderr, "%s: -h previously specified\n", prog );
408 exit( EXIT_FAILURE );
410 ldaphost = ber_strdup( optarg );
412 case 'H': /* ldap URI */
413 if( ldapuri != NULL ) {
414 fprintf( stderr, "%s: -H previously specified\n", prog );
415 exit( EXIT_FAILURE );
417 ldapuri = ber_strdup( optarg );
420 #ifdef HAVE_CYRUS_SASL
421 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
422 fprintf( stderr, "%s: incompatible previous "
423 "authentication choice\n",
425 exit( EXIT_FAILURE );
427 authmethod = LDAP_AUTH_SASL;
428 sasl_flags = LDAP_SASL_INTERACTIVE;
431 fprintf( stderr, "%s: was not compiled with SASL support\n",
433 exit( EXIT_FAILURE );
435 case 'k': /* kerberos bind */
436 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
437 if( authmethod != -1 ) {
438 fprintf( stderr, "%s: -k incompatible with previous "
439 "authentication choice\n", prog );
440 exit( EXIT_FAILURE );
442 authmethod = LDAP_AUTH_KRBV4;
444 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
445 exit( EXIT_FAILURE );
448 case 'K': /* kerberos bind, part one only */
449 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
450 if( authmethod != -1 ) {
451 fprintf( stderr, "%s: incompatible with previous "
452 "authentication choice\n", prog );
453 exit( EXIT_FAILURE );
455 authmethod = LDAP_AUTH_KRBV41;
457 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
458 exit( EXIT_FAILURE );
462 /* enable Manage DSA IT */
465 case 'n': /* print operations, don't actually do them */
469 #ifdef HAVE_CYRUS_SASL
470 if( sasl_secprops != NULL ) {
471 fprintf( stderr, "%s: -O previously specified\n", prog );
472 exit( EXIT_FAILURE );
474 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
475 fprintf( stderr, "%s: incompatible previous "
476 "authentication choice\n", prog );
477 exit( EXIT_FAILURE );
479 authmethod = LDAP_AUTH_SASL;
480 sasl_secprops = ber_strdup( optarg );
482 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
483 exit( EXIT_FAILURE );
488 fprintf( stderr, "%s: -p previously specified\n", prog );
489 exit( EXIT_FAILURE );
491 ival = strtol( optarg, &next, 10 );
492 if ( next == NULL || next[0] != '\0' ) {
493 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
494 exit( EXIT_FAILURE );
499 ival = strtol( optarg, &next, 10 );
500 if ( next == NULL || next[0] != '\0' ) {
501 fprintf( stderr, "%s: unabel to parse protocol version \"%s\"\n", prog, optarg );
502 exit( EXIT_FAILURE );
506 if( protocol == LDAP_VERSION3 ) {
507 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
509 exit( EXIT_FAILURE );
511 protocol = LDAP_VERSION2;
514 if( protocol == LDAP_VERSION2 ) {
515 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
517 exit( EXIT_FAILURE );
519 protocol = LDAP_VERSION3;
522 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
528 #ifdef HAVE_CYRUS_SASL
529 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
530 fprintf( stderr, "%s: incompatible previous "
531 "authentication choice\n",
533 exit( EXIT_FAILURE );
535 authmethod = LDAP_AUTH_SASL;
536 sasl_flags = LDAP_SASL_QUIET;
539 fprintf( stderr, "%s: not compiled with SASL support\n",
541 exit( EXIT_FAILURE );
544 #ifdef HAVE_CYRUS_SASL
545 if( sasl_realm != NULL ) {
546 fprintf( stderr, "%s: -R previously specified\n", prog );
547 exit( EXIT_FAILURE );
549 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
550 fprintf( stderr, "%s: incompatible previous "
551 "authentication choice\n",
553 exit( EXIT_FAILURE );
555 authmethod = LDAP_AUTH_SASL;
556 sasl_realm = ber_strdup( optarg );
558 fprintf( stderr, "%s: not compiled with SASL support\n",
560 exit( EXIT_FAILURE );
564 #ifdef HAVE_CYRUS_SASL
565 if( sasl_authc_id != NULL ) {
566 fprintf( stderr, "%s: -U previously specified\n", prog );
567 exit( EXIT_FAILURE );
569 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
570 fprintf( stderr, "%s: incompatible previous "
571 "authentication choice\n",
573 exit( EXIT_FAILURE );
575 authmethod = LDAP_AUTH_SASL;
576 sasl_authc_id = ber_strdup( optarg );
578 fprintf( stderr, "%s: not compiled with SASL support\n",
580 exit( EXIT_FAILURE );
583 case 'v': /* verbose mode */
586 case 'V': /* version */
589 case 'w': /* password */
590 passwd.bv_val = ber_strdup( optarg );
594 for( p = optarg; *p != '\0'; p++ ) {
598 passwd.bv_len = strlen( passwd.bv_val );
607 #ifdef HAVE_CYRUS_SASL
608 if( sasl_mech != NULL ) {
609 fprintf( stderr, "%s: -Y previously specified\n", prog );
610 exit( EXIT_FAILURE );
612 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
614 "%s: incompatible with authentication choice\n", prog );
615 exit( EXIT_FAILURE );
617 authmethod = LDAP_AUTH_SASL;
618 sasl_mech = ber_strdup( optarg );
620 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
621 exit( EXIT_FAILURE );
625 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
626 fprintf( stderr, "%s: incompatible with previous "
627 "authentication choice\n", prog );
628 exit( EXIT_FAILURE );
630 authmethod = LDAP_AUTH_SIMPLE;
633 #ifdef HAVE_CYRUS_SASL
634 if( sasl_authz_id != NULL ) {
635 fprintf( stderr, "%s: -X previously specified\n", prog );
636 exit( EXIT_FAILURE );
638 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
639 fprintf( stderr, "%s: -X incompatible with "
640 "authentication choice\n", prog );
641 exit( EXIT_FAILURE );
643 authmethod = LDAP_AUTH_SASL;
644 sasl_authz_id = ber_strdup( optarg );
646 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
647 exit( EXIT_FAILURE );
654 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
655 exit( EXIT_FAILURE );
659 if( handle_private_option( i ) ) break;
660 fprintf( stderr, "%s: unrecognized option -%c\n",
667 /* prevent bad linking */
669 api.ldapai_info_version = LDAP_API_INFO_VERSION;
671 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
672 != LDAP_OPT_SUCCESS )
674 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
675 exit( EXIT_FAILURE );
678 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
679 fprintf( stderr, "LDAP APIInfo version mismatch: "
680 "library %d, header %d\n",
681 api.ldapai_info_version, LDAP_API_INFO_VERSION );
682 exit( EXIT_FAILURE );
685 if( api.ldapai_api_version != LDAP_API_VERSION ) {
686 fprintf( stderr, "LDAP API version mismatch: "
687 "library %d, header %d\n",
688 api.ldapai_api_version, LDAP_API_VERSION );
689 exit( EXIT_FAILURE );
692 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
693 fprintf( stderr, "LDAP vendor name mismatch: "
694 "library %s, header %s\n",
695 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
696 exit( EXIT_FAILURE );
699 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
700 fprintf( stderr, "LDAP vendor version mismatch: "
701 "library %d, header %d\n",
702 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
703 exit( EXIT_FAILURE );
707 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
709 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
710 if (version > 1) exit( EXIT_SUCCESS );
715 protocol = LDAP_VERSION3;
717 if (authmethod == -1 && protocol > LDAP_VERSION2) {
718 #ifdef HAVE_CYRUS_SASL
719 authmethod = LDAP_AUTH_SASL;
721 authmethod = LDAP_AUTH_SIMPLE;
725 if( ldapuri == NULL ) {
726 if( ldapport && ( ldaphost == NULL )) {
727 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
728 exit( EXIT_FAILURE );
731 if( ldaphost != NULL ) {
732 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
733 exit( EXIT_FAILURE );
736 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
737 exit( EXIT_FAILURE );
740 if( protocol == LDAP_VERSION2 ) {
741 if( assertctl || authzid || manageDIT || manageDSAit ||
742 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
745 noop || ppolicy || preread || postread )
747 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
748 exit( EXIT_FAILURE );
752 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
753 exit( EXIT_FAILURE );
756 #ifdef HAVE_CYRUS_SASL
757 if( authmethod == LDAP_AUTH_SASL ) {
758 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
760 exit( EXIT_FAILURE );
764 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
765 if ( authmethod == LDAP_AUTH_KRBV4 || authmethod == LDAP_AUTH_KRBV41 ) {
766 fprintf( stderr, "%s: -k/-K incompatible with LDAPv%d\n",
768 exit( EXIT_FAILURE );
776 tool_conn_setup( int not, void (*private_setup)( LDAP * ) )
781 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
782 != LBER_OPT_SUCCESS )
785 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
787 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
788 != LDAP_OPT_SUCCESS )
791 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
796 (void) SIGNAL( SIGPIPE, SIG_IGN );
800 SIGNAL( SIGINT, do_sig );
806 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
809 memset( &url, 0, sizeof(url));
811 url.lud_scheme = "ldap";
812 url.lud_host = ldaphost;
813 url.lud_port = ldapport;
814 url.lud_scope = LDAP_SCOPE_DEFAULT;
816 ldapuri = ldap_url_desc2str( &url );
820 fprintf( stderr, "ldap_initialize( %s )\n",
821 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
823 rc = ldap_initialize( &ld, ldapuri );
824 if( rc != LDAP_SUCCESS ) {
826 "Could not create LDAP session handle for URI=%s (%d): %s\n",
827 ldapuri, rc, ldap_err2string(rc) );
828 exit( EXIT_FAILURE );
831 if( private_setup ) private_setup( ld );
834 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
835 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
837 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
838 referrals ? "on" : "off" );
839 exit( EXIT_FAILURE );
842 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
843 != LDAP_OPT_SUCCESS )
845 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
847 exit( EXIT_FAILURE );
851 ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ))
853 ldap_perror( ld, "ldap_start_tls" );
855 exit( EXIT_FAILURE );
865 tool_bind( LDAP *ld )
867 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
869 LDAPControl *ctrls[2], c;
870 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
871 c.ldctl_value.bv_val = NULL;
872 c.ldctl_value.bv_len = 0;
873 c.ldctl_iscritical = 0;
876 ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
880 if ( authmethod == LDAP_AUTH_SASL ) {
881 #ifdef HAVE_CYRUS_SASL
885 if( sasl_secprops != NULL ) {
886 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
887 (void *) sasl_secprops );
889 if( rc != LDAP_OPT_SUCCESS ) {
891 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
893 exit( EXIT_FAILURE );
897 defaults = lutil_sasl_defaults( ld,
904 rc = ldap_sasl_interactive_bind_s( ld, binddn,
905 sasl_mech, NULL, NULL,
906 sasl_flags, lutil_sasl_interact, defaults );
908 lutil_sasl_freedefs( defaults );
909 if( rc != LDAP_SUCCESS ) {
910 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
911 exit( EXIT_FAILURE );
914 fprintf( stderr, "%s: not compiled with SASL support\n",
916 exit( EXIT_FAILURE );
926 if (( msgid = ldap_bind( ld, binddn, passwd.bv_val, authmethod )) == -1 )
928 ldap_perror( ld, "ldap_bind" );
929 exit( EXIT_FAILURE );
932 if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) {
933 ldap_perror( ld, "ldap_result" );
934 exit( EXIT_FAILURE );
937 if ( ldap_parse_result( ld, result, &err, NULL, NULL, NULL,
938 &ctrls, 1 ) != LDAP_SUCCESS ) {
939 ldap_perror( ld, "ldap_bind parse result" );
940 exit( EXIT_FAILURE );
943 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
944 if ( ctrls && ppolicy ) {
946 int expire, grace, len = 0;
947 LDAPPasswordPolicyError pErr = -1;
949 ctrl = ldap_find_control( LDAP_CONTROL_PASSWORDPOLICYRESPONSE, ctrls );
950 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
951 &expire, &grace, &pErr ) == LDAP_SUCCESS ) {
952 if ( pErr != PP_noError ){
955 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
956 len = strlen( msgbuf );
959 sprintf( msgbuf+len, " (Password expires in %d seconds)", expire );
960 } else if ( grace >= 0 ) {
961 sprintf( msgbuf+len, " (Password expired, %d grace logins remain)", grace );
966 if ( err != LDAP_SUCCESS || msgbuf[0] ) {
967 fprintf( stderr, "ldap_bind: %s%s\n", ldap_err2string( err ),
969 if ( err != LDAP_SUCCESS ) {
970 exit( EXIT_FAILURE );
977 tool_unbind( LDAP *ld )
979 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
981 if ( err != LDAP_OPT_SUCCESS ) {
982 fprintf( stderr, "Could not unset controls\n");
985 (void) ldap_unbind_ext( ld, NULL, NULL );
989 /* Set server controls. Add controls extra_c[0..count-1], if set. */
991 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
993 int i = 0, j, crit = 0, err;
994 LDAPControl c[10], **ctrls;
996 ctrls = (LDAPControl**) malloc(sizeof(c) + (count+1)*sizeof(LDAPControl*));
997 if ( ctrls == NULL ) {
998 fprintf( stderr, "No memory\n" );
999 exit( EXIT_FAILURE );
1003 BerElementBuffer berbuf;
1004 BerElement *ber = (BerElement *)&berbuf;
1006 if( assertion == NULL || *assertion == '\0' ) {
1007 fprintf( stderr, "Assertion=<empty>\n" );
1008 exit( EXIT_FAILURE );
1011 ber_init2( ber, NULL, LBER_USE_DER );
1013 err = ldap_pvt_put_filter( ber, assertion );
1015 fprintf( stderr, "assertion encode failed (%d)\n", err );
1016 exit( EXIT_FAILURE );
1019 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1021 fprintf( stderr, "assertion flatten failed (%d)\n", err );
1022 exit( EXIT_FAILURE );
1025 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1026 c[i].ldctl_iscritical = assertctl > 1;
1032 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1033 c[i].ldctl_value.bv_val = authzid;
1034 c[i].ldctl_value.bv_len = strlen( authzid );
1035 c[i].ldctl_iscritical = 1;
1041 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1042 BER_BVZERO( &c[i].ldctl_value );
1043 c[i].ldctl_iscritical = manageDIT > 1;
1048 if ( manageDSAit ) {
1049 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1050 BER_BVZERO( &c[i].ldctl_value );
1051 c[i].ldctl_iscritical = manageDSAit > 1;
1057 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1058 BER_BVZERO( &c[i].ldctl_value );
1059 c[i].ldctl_iscritical = noop > 1;
1065 char berbuf[LBER_ELEMENT_SIZEOF];
1066 BerElement *ber = (BerElement *)berbuf;
1067 char **attrs = NULL;
1069 if( preread_attrs ) {
1070 attrs = ldap_str2charray( preread_attrs, "," );
1073 ber_init2( ber, NULL, LBER_USE_DER );
1075 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1076 fprintf( stderr, "preread attrs encode failed.\n" );
1077 exit( EXIT_FAILURE );
1080 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1082 fprintf( stderr, "preread flatten failed (%d)\n", err );
1083 exit( EXIT_FAILURE );
1086 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1087 c[i].ldctl_iscritical = preread > 1;
1091 if( attrs ) ldap_charray_free( attrs );
1095 char berbuf[LBER_ELEMENT_SIZEOF];
1096 BerElement *ber = (BerElement *)berbuf;
1097 char **attrs = NULL;
1099 if( postread_attrs ) {
1100 attrs = ldap_str2charray( postread_attrs, "," );
1103 ber_init2( ber, NULL, LBER_USE_DER );
1105 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1106 fprintf( stderr, "postread attrs encode failed.\n" );
1107 exit( EXIT_FAILURE );
1110 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1112 fprintf( stderr, "postread flatten failed (%d)\n", err );
1113 exit( EXIT_FAILURE );
1116 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1117 c[i].ldctl_iscritical = postread > 1;
1121 if( attrs ) ldap_charray_free( attrs );
1124 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1126 if ( chainingResolve > -1 ) {
1127 BerElementBuffer berbuf;
1128 BerElement *ber = (BerElement *)&berbuf;
1130 ber_init2( ber, NULL, LBER_USE_DER );
1132 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1135 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1136 exit( EXIT_FAILURE );
1139 if ( chainingContinuation > -1 ) {
1140 err = ber_printf( ber, "e", chainingContinuation );
1143 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1144 exit( EXIT_FAILURE );
1148 err = ber_printf( ber, /* { */ "N}" );
1151 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1152 exit( EXIT_FAILURE );
1155 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1156 exit( EXIT_FAILURE );
1160 BER_BVZERO( &c[i].ldctl_value );
1163 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1164 c[i].ldctl_iscritical = chaining > 1;
1168 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1171 ctrls[i++] = extra_c++;
1175 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1177 if ( err != LDAP_OPT_SUCCESS ) {
1178 for ( j = 0; j < i; j++ ) {
1179 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1181 fprintf( stderr, "Could not set %scontrols\n",
1182 crit ? "critical " : "" );
1187 exit( EXIT_FAILURE );
1192 tool_check_abandon( LDAP *ld, int msgid )
1196 switch ( gotintr ) {
1197 case LDAP_REQ_EXTENDED:
1198 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1199 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1200 rc, ldap_err2string( rc ) );
1203 case LDAP_REQ_ABANDON:
1204 rc = ldap_abandon( ld, msgid );
1205 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1206 rc, ldap_err2string( rc ) );