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-2007 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>
37 #ifdef HAVE_CYRUS_SASL
38 #ifdef HAVE_SASL_SASL_H
39 #include <sasl/sasl.h>
49 #include "lutil_ldap.h"
50 #include "ldap_defaults.h"
56 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
58 /* Necessary for old LDAPv2 Kerberos Bind methods */
60 ldap_bind LDAP_P(( /* deprecated */
63 LDAP_CONST char *passwd,
68 /* input-related vars */
70 /* misc. parameters */
71 tool_type_t tool_type;
83 char *ldaphost = NULL;
93 struct berval passwd = { 0, NULL };
95 #ifdef HAVE_CYRUS_SASL
96 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
97 char *sasl_realm = NULL;
98 char *sasl_authc_id = NULL;
99 char *sasl_authz_id = NULL;
100 char *sasl_mech = NULL;
101 char *sasl_secprops = NULL;
106 char *assertion = NULL;
107 char *authzid = NULL;
108 /* support deprecated early version of proxyAuthz */
109 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ "2.16.840.1.113730.3.4.12"
110 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
111 char *proxydn = NULL;
112 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
118 static char *preread_attrs = NULL;
120 static char *postread_attrs = NULL;
121 ber_int_t pr_morePagedResults = 1;
122 struct berval pr_cookie = { 0, NULL };
123 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
125 static int chainingResolve = -1;
126 static int chainingContinuation = -1;
127 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
130 struct timeval nettimeout = { -1 , 0 };
132 typedef int (*print_ctrl_fn)( LDAP *ld, LDAPControl *ctrl );
134 static int print_preread( LDAP *ld, LDAPControl *ctrl );
135 static int print_postread( LDAP *ld, LDAPControl *ctrl );
136 static int print_paged_results( LDAP *ld, LDAPControl *ctrl );
137 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
138 static int print_ppolicy( LDAP *ld, LDAPControl *ctrl );
141 static struct tool_ctrls_t {
145 } tool_ctrl_response[] = {
146 { LDAP_CONTROL_PRE_READ, TOOL_ALL, print_preread },
147 { LDAP_CONTROL_POST_READ, TOOL_ALL, print_postread },
148 { LDAP_CONTROL_PAGEDRESULTS, TOOL_SEARCH, print_paged_results },
149 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
150 { LDAP_CONTROL_PASSWORDPOLICYRESPONSE, TOOL_ALL, print_ppolicy },
166 tool_init( tool_type_t type )
169 ldap_pvt_setlocale(LC_MESSAGES, "");
170 ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
171 ldap_pvt_textdomain(OPENLDAP_PACKAGE);
177 #ifdef HAVE_CYRUS_SASL
181 ldap_pvt_tls_destroy();
186 tool_common_usage( void )
188 static const char *const descriptions[] = {
189 N_(" -c continuous operation mode (do not stop on errors)\n"),
190 N_(" -C chase referrals (anonymously)\n"),
191 N_(" -d level set LDAP debugging level to `level'\n"),
192 N_(" -D binddn bind DN\n"),
193 N_(" -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
194 N_(" [!]assert=<filter> (a RFC 4515 Filter string)\n")
195 N_(" [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n")
196 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
198 /* non-advertized support for proxyDN */
199 N_(" [!]proxydn=<dn> (a RFC 4514 DN string)\n")
202 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
203 N_(" [!]chaining[=<resolveBehavior>[/<continuationBehavior>]]\n")
204 N_(" one of \"chainingPreferred\", \"chainingRequired\",\n")
205 N_(" \"referralsPreferred\", \"referralsRequired\"\n")
206 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
207 N_(" [!]manageDSAit\n")
209 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
212 N_(" [!]postread[=<attrs>] (a comma-separated attribute list)\n")
213 N_(" [!]preread[=<attrs>] (a comma-separated attribute list)\n")
217 N_(" abandon, cancel (SIGINT sends abandon/cancel; not really controls)\n"),
218 N_(" -f file read operations from `file'\n"),
219 N_(" -h host LDAP server\n"),
220 N_(" -H URI LDAP Uniform Resource Indentifier(s)\n"),
221 N_(" -I use SASL Interactive mode\n"),
222 N_(" -k use Kerberos authentication\n"),
223 N_(" -K like -k, but do only step 1 of the Kerberos bind\n"),
224 N_(" -M enable Manage DSA IT control (-MM to make critical)\n"),
225 N_(" -n show what would be done but don't actually do it\n"),
226 N_(" -O props SASL security properties\n"),
227 N_(" -o <opt>[=<optparam] general options\n"),
228 N_(" nettimeout=<timeout> (in seconds, or \"none\" or \"max\")\n"),
229 N_(" -p port port on LDAP server\n"),
230 N_(" -P version procotol version (default: 3)\n"),
231 N_(" -Q use SASL Quiet mode\n"),
232 N_(" -R realm SASL realm\n"),
233 N_(" -U authcid SASL authentication identity\n"),
234 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
235 N_(" -V print version info (-VV only)\n"),
236 N_(" -w passwd bind password (for simple authentication)\n"),
237 N_(" -W prompt for bind password\n"),
238 N_(" -x Simple authentication\n"),
239 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
240 N_(" -y file Read password from file\n"),
241 N_(" -Y mech SASL mechanism\n"),
242 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
245 const char *const *cpp;
247 fputs( _("Common options:\n"), stderr );
248 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
249 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
250 fputs( _(*cpp), stderr );
263 fprintf( stderr, "%s: %s (%d)%s\n",
264 func, ldap_err2string( err ), err, extra ? extra : "" );
266 if ( matched && *matched ) {
267 fprintf( stderr, _("\tmatched DN: %s\n"), matched );
270 if ( info && *info ) {
271 fprintf( stderr, _("\tadditional info: %s\n"), info );
274 if ( refs && *refs ) {
276 fprintf( stderr, _("\treferrals:\n") );
277 for( i=0; refs[i]; i++ ) {
278 fprintf( stderr, "\t\t%s\n", refs[i] );
285 tool_args( int argc, char **argv )
289 while (( i = getopt( argc, argv, options )) != EOF ) {
291 char *control, *cvalue, *next;
293 case 'c': /* continuous operation mode */
300 ival = strtol( optarg, &next, 10 );
301 if (next == NULL || next[0] != '\0') {
302 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
307 case 'D': /* bind DN */
308 if( binddn != NULL ) {
309 fprintf( stderr, "%s: -D previously specified\n", prog );
310 exit( EXIT_FAILURE );
312 binddn = ber_strdup( optarg );
314 case 'e': /* general extensions (controls and such) */
315 /* should be extended to support comma separated list of
316 * [!]key[=value] parameters, e.g. -e !foo,bar=567
321 if( optarg[0] == '!' ) {
326 control = ber_strdup( optarg );
327 if ( (cvalue = strchr( control, '=' )) != NULL ) {
331 if ( strcasecmp( control, "assert" ) == 0 ) {
333 fprintf( stderr, "assert control previously specified\n");
334 exit( EXIT_FAILURE );
336 if( cvalue == NULL ) {
337 fprintf( stderr, "assert: control value expected\n" );
341 assertctl = 1 + crit;
343 assert( assertion == NULL );
346 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
347 if( authzid != NULL ) {
348 fprintf( stderr, "authzid control previously specified\n");
349 exit( EXIT_FAILURE );
351 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
352 if( proxydn != NULL ) {
353 fprintf( stderr, "authzid control incompatible with proxydn\n");
354 exit( EXIT_FAILURE );
356 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
357 if( cvalue == NULL ) {
358 fprintf( stderr, "authzid: control value expected\n" );
362 fprintf( stderr, "authzid: must be marked critical\n" );
366 assert( authzid == NULL );
369 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
370 } else if ( strcasecmp( control, "proxydn" ) == 0 ) {
371 if( proxydn != NULL ) {
372 fprintf( stderr, "proxydn control previously specified\n");
373 exit( EXIT_FAILURE );
375 if( authzid != NULL ) {
376 fprintf( stderr, "proxydn control incompatible with authzid\n");
377 exit( EXIT_FAILURE );
379 if( cvalue == NULL ) {
380 fprintf( stderr, "proxydn: control value expected\n" );
384 fprintf( stderr, "proxydn: must be marked critical\n" );
388 assert( proxydn == NULL );
390 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
392 } else if ( ( strcasecmp( control, "relax" ) == 0 ) ||
393 ( strcasecmp( control, "manageDIT" ) == 0 ) )
397 "relax control previously specified\n");
398 exit( EXIT_FAILURE );
400 if( cvalue != NULL ) {
402 "relax: no control value expected\n" );
406 manageDIT = 1 + crit;
408 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
411 "manageDSAit control previously specified\n");
412 exit( EXIT_FAILURE );
414 if( cvalue != NULL ) {
416 "manageDSAit: no control value expected\n" );
420 manageDSAit = 1 + crit;
422 } else if ( strcasecmp( control, "noop" ) == 0 ) {
424 fprintf( stderr, "noop control previously specified\n");
425 exit( EXIT_FAILURE );
427 if( cvalue != NULL ) {
428 fprintf( stderr, "noop: no control value expected\n" );
434 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
435 } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
437 fprintf( stderr, "ppolicy control previously specified\n");
438 exit( EXIT_FAILURE );
440 if( cvalue != NULL ) {
441 fprintf( stderr, "ppolicy: no control value expected\n" );
445 fprintf( stderr, "ppolicy: critical flag not allowed\n" );
452 } else if ( strcasecmp( control, "preread" ) == 0 ) {
454 fprintf( stderr, "preread control previously specified\n");
455 exit( EXIT_FAILURE );
459 preread_attrs = cvalue;
461 } else if ( strcasecmp( control, "postread" ) == 0 ) {
463 fprintf( stderr, "postread control previously specified\n");
464 exit( EXIT_FAILURE );
468 postread_attrs = cvalue;
470 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
471 } else if ( strcasecmp( control, "chaining" ) == 0 ) {
474 if ( cvalue != NULL ) {
477 continuation = strchr( cvalue, '/' );
478 if ( continuation ) {
479 /* FIXME: this makes sense only in searches */
480 *continuation++ = '\0';
481 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
482 chainingContinuation = LDAP_CHAINING_PREFERRED;
483 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
484 chainingContinuation = LDAP_CHAINING_REQUIRED;
485 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
486 chainingContinuation = LDAP_REFERRALS_PREFERRED;
487 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
488 chainingContinuation = LDAP_REFERRALS_REQUIRED;
491 "chaining behavior control "
492 "continuation value \"%s\" invalid\n",
494 exit( EXIT_FAILURE );
498 if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
499 chainingResolve = LDAP_CHAINING_PREFERRED;
500 } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
501 chainingResolve = LDAP_CHAINING_REQUIRED;
502 } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
503 chainingResolve = LDAP_REFERRALS_PREFERRED;
504 } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
505 chainingResolve = LDAP_REFERRALS_REQUIRED;
508 "chaining behavior control "
509 "resolve value \"%s\" invalid\n",
511 exit( EXIT_FAILURE );
514 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
516 /* this shouldn't go here, really; but it's a feature... */
517 } else if ( strcasecmp( control, "abandon" ) == 0 ) {
518 abcan = LDAP_REQ_ABANDON;
520 } else if ( strcasecmp( control, "cancel" ) == 0 ) {
521 abcan = LDAP_REQ_EXTENDED;
524 fprintf( stderr, "Invalid general control name: %s\n",
529 case 'f': /* read from file */
530 if( infile != NULL ) {
531 fprintf( stderr, "%s: -f previously specified\n", prog );
532 exit( EXIT_FAILURE );
534 infile = ber_strdup( optarg );
536 case 'h': /* ldap host */
537 if( ldaphost != NULL ) {
538 fprintf( stderr, "%s: -h previously specified\n", prog );
539 exit( EXIT_FAILURE );
541 ldaphost = ber_strdup( optarg );
543 case 'H': /* ldap URI */
544 if( ldapuri != NULL ) {
545 fprintf( stderr, "%s: -H previously specified\n", prog );
546 exit( EXIT_FAILURE );
548 ldapuri = ber_strdup( optarg );
551 #ifdef HAVE_CYRUS_SASL
552 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
553 fprintf( stderr, "%s: incompatible previous "
554 "authentication choice\n",
556 exit( EXIT_FAILURE );
558 authmethod = LDAP_AUTH_SASL;
559 sasl_flags = LDAP_SASL_INTERACTIVE;
562 fprintf( stderr, "%s: was not compiled with SASL support\n",
564 exit( EXIT_FAILURE );
566 case 'k': /* kerberos bind */
567 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
568 if( authmethod != -1 ) {
569 fprintf( stderr, "%s: -k incompatible with previous "
570 "authentication choice\n", prog );
571 exit( EXIT_FAILURE );
573 authmethod = LDAP_AUTH_KRBV4;
575 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
576 exit( EXIT_FAILURE );
579 case 'K': /* kerberos bind, part one only */
580 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
581 if( authmethod != -1 ) {
582 fprintf( stderr, "%s: incompatible with previous "
583 "authentication choice\n", prog );
584 exit( EXIT_FAILURE );
586 authmethod = LDAP_AUTH_KRBV41;
588 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
589 exit( EXIT_FAILURE );
593 /* enable Manage DSA IT */
596 case 'n': /* print operations, don't actually do them */
600 control = ber_strdup( optarg );
601 if ( (cvalue = strchr( control, '=' )) != NULL ) {
605 if ( strcasecmp( control, "nettimeout" ) == 0 ) {
606 if( nettimeout.tv_sec != -1 ) {
607 fprintf( stderr, "nettimeout option previously specified\n");
608 exit( EXIT_FAILURE );
610 if( cvalue == NULL || cvalue[0] == '\0' ) {
611 fprintf( stderr, "nettimeout: option value expected\n" );
614 if ( strcasecmp( cvalue, "none" ) == 0 ) {
615 nettimeout.tv_sec = 0;
616 } else if ( strcasecmp( cvalue, "max" ) == 0 ) {
617 nettimeout.tv_sec = LDAP_MAXINT;
619 ival = strtol( cvalue, &next, 10 );
620 if ( next == NULL || next[0] != '\0' ) {
622 _("Unable to parse network timeout \"%s\"\n"), cvalue );
623 exit( EXIT_FAILURE );
625 nettimeout.tv_sec = ival;
627 if( nettimeout.tv_sec < 0 || nettimeout.tv_sec > LDAP_MAXINT ) {
628 fprintf( stderr, _("%s: invalid network timeout (%ld) specified\n"),
629 prog, (long)nettimeout.tv_sec );
630 exit( EXIT_FAILURE );
633 fprintf( stderr, "Invalid general option name: %s\n",
639 #ifdef HAVE_CYRUS_SASL
640 if( sasl_secprops != NULL ) {
641 fprintf( stderr, "%s: -O previously specified\n", prog );
642 exit( EXIT_FAILURE );
644 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
645 fprintf( stderr, "%s: incompatible previous "
646 "authentication choice\n", prog );
647 exit( EXIT_FAILURE );
649 authmethod = LDAP_AUTH_SASL;
650 sasl_secprops = ber_strdup( optarg );
652 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
653 exit( EXIT_FAILURE );
658 fprintf( stderr, "%s: -p previously specified\n", prog );
659 exit( EXIT_FAILURE );
661 ival = strtol( optarg, &next, 10 );
662 if ( next == NULL || next[0] != '\0' ) {
663 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
664 exit( EXIT_FAILURE );
669 ival = strtol( optarg, &next, 10 );
670 if ( next == NULL || next[0] != '\0' ) {
671 fprintf( stderr, "%s: unabel to parse protocol version \"%s\"\n", prog, optarg );
672 exit( EXIT_FAILURE );
676 if( protocol == LDAP_VERSION3 ) {
677 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
679 exit( EXIT_FAILURE );
681 protocol = LDAP_VERSION2;
684 if( protocol == LDAP_VERSION2 ) {
685 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
687 exit( EXIT_FAILURE );
689 protocol = LDAP_VERSION3;
692 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
698 #ifdef HAVE_CYRUS_SASL
699 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
700 fprintf( stderr, "%s: incompatible previous "
701 "authentication choice\n",
703 exit( EXIT_FAILURE );
705 authmethod = LDAP_AUTH_SASL;
706 sasl_flags = LDAP_SASL_QUIET;
709 fprintf( stderr, "%s: not compiled with SASL support\n",
711 exit( EXIT_FAILURE );
714 #ifdef HAVE_CYRUS_SASL
715 if( sasl_realm != NULL ) {
716 fprintf( stderr, "%s: -R previously specified\n", prog );
717 exit( EXIT_FAILURE );
719 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
720 fprintf( stderr, "%s: incompatible previous "
721 "authentication choice\n",
723 exit( EXIT_FAILURE );
725 authmethod = LDAP_AUTH_SASL;
726 sasl_realm = ber_strdup( optarg );
728 fprintf( stderr, "%s: not compiled with SASL support\n",
730 exit( EXIT_FAILURE );
734 #ifdef HAVE_CYRUS_SASL
735 if( sasl_authc_id != NULL ) {
736 fprintf( stderr, "%s: -U previously specified\n", prog );
737 exit( EXIT_FAILURE );
739 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
740 fprintf( stderr, "%s: incompatible previous "
741 "authentication choice\n",
743 exit( EXIT_FAILURE );
745 authmethod = LDAP_AUTH_SASL;
746 sasl_authc_id = ber_strdup( optarg );
748 fprintf( stderr, "%s: not compiled with SASL support\n",
750 exit( EXIT_FAILURE );
753 case 'v': /* verbose mode */
756 case 'V': /* version */
759 case 'w': /* password */
760 passwd.bv_val = ber_strdup( optarg );
764 for( p = optarg; *p != '\0'; p++ ) {
768 passwd.bv_len = strlen( passwd.bv_val );
777 #ifdef HAVE_CYRUS_SASL
778 if( sasl_mech != NULL ) {
779 fprintf( stderr, "%s: -Y previously specified\n", prog );
780 exit( EXIT_FAILURE );
782 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
784 "%s: incompatible with authentication choice\n", prog );
785 exit( EXIT_FAILURE );
787 authmethod = LDAP_AUTH_SASL;
788 sasl_mech = ber_strdup( optarg );
790 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
791 exit( EXIT_FAILURE );
795 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
796 fprintf( stderr, "%s: incompatible with previous "
797 "authentication choice\n", prog );
798 exit( EXIT_FAILURE );
800 authmethod = LDAP_AUTH_SIMPLE;
803 #ifdef HAVE_CYRUS_SASL
804 if( sasl_authz_id != NULL ) {
805 fprintf( stderr, "%s: -X previously specified\n", prog );
806 exit( EXIT_FAILURE );
808 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
809 fprintf( stderr, "%s: -X incompatible with "
810 "authentication choice\n", prog );
811 exit( EXIT_FAILURE );
813 authmethod = LDAP_AUTH_SASL;
814 sasl_authz_id = ber_strdup( optarg );
816 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
817 exit( EXIT_FAILURE );
824 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
825 exit( EXIT_FAILURE );
829 if( handle_private_option( i ) ) break;
830 fprintf( stderr, "%s: unrecognized option -%c\n",
837 /* prevent bad linking */
839 api.ldapai_info_version = LDAP_API_INFO_VERSION;
841 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
842 != LDAP_OPT_SUCCESS )
844 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
845 exit( EXIT_FAILURE );
848 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
849 fprintf( stderr, "LDAP APIInfo version mismatch: "
850 "library %d, header %d\n",
851 api.ldapai_info_version, LDAP_API_INFO_VERSION );
852 exit( EXIT_FAILURE );
855 if( api.ldapai_api_version != LDAP_API_VERSION ) {
856 fprintf( stderr, "LDAP API version mismatch: "
857 "library %d, header %d\n",
858 api.ldapai_api_version, LDAP_API_VERSION );
859 exit( EXIT_FAILURE );
862 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
863 fprintf( stderr, "LDAP vendor name mismatch: "
864 "library %s, header %s\n",
865 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
866 exit( EXIT_FAILURE );
869 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
870 fprintf( stderr, "LDAP vendor version mismatch: "
871 "library %d, header %d\n",
872 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
873 exit( EXIT_FAILURE );
877 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
879 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
880 if (version > 1) exit( EXIT_SUCCESS );
883 ldap_memfree( api.ldapai_vendor_name );
884 ber_memvfree( (void **)api.ldapai_extensions );
888 protocol = LDAP_VERSION3;
890 if (authmethod == -1 && protocol > LDAP_VERSION2) {
891 #ifdef HAVE_CYRUS_SASL
892 authmethod = LDAP_AUTH_SASL;
894 authmethod = LDAP_AUTH_SIMPLE;
898 if( ldapuri == NULL ) {
899 if( ldapport && ( ldaphost == NULL )) {
900 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
901 exit( EXIT_FAILURE );
904 if( ldaphost != NULL ) {
905 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
906 exit( EXIT_FAILURE );
909 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
910 exit( EXIT_FAILURE );
914 if( protocol == LDAP_VERSION2 ) {
915 if( assertctl || authzid || manageDIT || manageDSAit ||
916 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
918 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
919 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
922 noop || ppolicy || preread || postread )
924 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
925 exit( EXIT_FAILURE );
929 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
930 exit( EXIT_FAILURE );
933 #ifdef HAVE_CYRUS_SASL
934 if( authmethod == LDAP_AUTH_SASL ) {
935 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
937 exit( EXIT_FAILURE );
941 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
943 if ( authmethod == LDAP_AUTH_KRBV4 || authmethod == LDAP_AUTH_KRBV41 ) {
944 fprintf( stderr, "%s: -k/-K incompatible with LDAPv%d\n",
946 exit( EXIT_FAILURE );
954 tool_conn_setup( int dont, void (*private_setup)( LDAP * ) )
959 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
960 != LBER_OPT_SUCCESS )
963 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
965 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
966 != LDAP_OPT_SUCCESS )
969 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
974 (void) SIGNAL( SIGPIPE, SIG_IGN );
978 SIGNAL( SIGINT, do_sig );
984 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
987 memset( &url, 0, sizeof(url));
989 url.lud_scheme = "ldap";
990 url.lud_host = ldaphost;
991 url.lud_port = ldapport;
992 url.lud_scope = LDAP_SCOPE_DEFAULT;
994 ldapuri = ldap_url_desc2str( &url );
998 fprintf( stderr, "ldap_initialize( %s )\n",
999 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
1001 rc = ldap_initialize( &ld, ldapuri );
1002 if( rc != LDAP_SUCCESS ) {
1004 "Could not create LDAP session handle for URI=%s (%d): %s\n",
1005 ldapuri, rc, ldap_err2string(rc) );
1006 exit( EXIT_FAILURE );
1009 if( private_setup ) private_setup( ld );
1012 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
1013 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
1015 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
1016 referrals ? "on" : "off" );
1017 exit( EXIT_FAILURE );
1020 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
1021 != LDAP_OPT_SUCCESS )
1023 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
1025 exit( EXIT_FAILURE );
1029 rc = ldap_start_tls_s( ld, NULL, NULL );
1030 if ( rc != LDAP_SUCCESS ) {
1031 tool_perror( "ldap_start_tls", rc, NULL, NULL, NULL, NULL );
1032 if ( use_tls > 1 ) {
1033 exit( EXIT_FAILURE );
1038 if ( nettimeout.tv_sec > 0 ) {
1039 if ( ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &nettimeout )
1040 != LDAP_OPT_SUCCESS )
1042 fprintf( stderr, "Could not set LDAP_OPT_NETWORK_TIMEOUT %ld\n",
1043 (long)nettimeout.tv_sec );
1044 exit( EXIT_FAILURE );
1054 tool_bind( LDAP *ld )
1056 LDAPControl **sctrlsp = NULL;
1057 LDAPControl *sctrls[2];
1060 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1063 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1064 c.ldctl_value.bv_val = NULL;
1065 c.ldctl_value.bv_len = 0;
1066 c.ldctl_iscritical = 0;
1067 sctrls[nsctrls] = &c;
1068 sctrls[++nsctrls] = NULL;
1076 assert( nsctrls < sizeof(sctrls)/sizeof(sctrls[0]) );
1078 if ( authmethod == LDAP_AUTH_SASL ) {
1079 #ifdef HAVE_CYRUS_SASL
1083 if( sasl_secprops != NULL ) {
1084 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
1085 (void *) sasl_secprops );
1087 if( rc != LDAP_OPT_SUCCESS ) {
1089 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
1091 exit( LDAP_LOCAL_ERROR );
1095 defaults = lutil_sasl_defaults( ld,
1102 rc = ldap_sasl_interactive_bind_s( ld, binddn, sasl_mech,
1104 NULL, sasl_flags, lutil_sasl_interact, defaults );
1106 lutil_sasl_freedefs( defaults );
1107 if( rc != LDAP_SUCCESS ) {
1108 tool_perror( "ldap_sasl_interactive_bind_s",
1109 rc, NULL, NULL, NULL, NULL );
1113 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1114 exit( LDAP_NOT_SUPPORTED );
1118 LDAPMessage *result;
1119 LDAPControl **ctrls;
1121 char *matched = NULL;
1127 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
1128 if ( authmethod == LDAP_AUTH_KRBV4 || authmethod == LDAP_AUTH_KRBV41 ) {
1129 msgid = ldap_bind( ld, binddn, passwd.bv_val, authmethod );
1130 if ( msgid == -1 ) {
1131 tool_perror( "ldap_bind", -1, NULL, NULL, NULL, NULL );
1132 exit( LDAP_LOCAL_ERROR );
1138 rc = ldap_sasl_bind( ld, binddn, LDAP_SASL_SIMPLE, &passwd,
1139 sctrlsp, NULL, &msgid );
1140 if ( msgid == -1 ) {
1141 tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
1142 NULL, NULL, NULL, NULL );
1147 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 ) {
1148 tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
1149 exit( LDAP_LOCAL_ERROR );
1152 rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
1154 if ( rc != LDAP_SUCCESS ) {
1155 tool_perror( "ldap_bind parse result", rc, NULL, NULL, NULL, NULL );
1156 exit( LDAP_LOCAL_ERROR );
1159 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1160 if ( ctrls && ppolicy ) {
1162 int expire, grace, len = 0;
1163 LDAPPasswordPolicyError pErr = -1;
1165 ctrl = ldap_find_control( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
1168 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
1169 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1171 if ( pErr != PP_noError ){
1174 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1175 len = strlen( msgbuf );
1177 if ( expire >= 0 ) {
1178 sprintf( msgbuf+len,
1179 " (Password expires in %d seconds)",
1181 } else if ( grace >= 0 ) {
1182 sprintf( msgbuf+len,
1183 " (Password expired, %d grace logins remain)",
1191 ldap_controls_free( ctrls );
1194 if ( err != LDAP_SUCCESS
1196 || ( matched && matched[ 0 ] )
1197 || ( info && info[ 0 ] )
1200 tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1202 if( matched ) ber_memfree( matched );
1203 if( info ) ber_memfree( info );
1204 if( refs ) ber_memvfree( (void **)refs );
1206 if ( err != LDAP_SUCCESS ) exit( err );
1212 tool_unbind( LDAP *ld )
1214 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1216 if ( err != LDAP_OPT_SUCCESS ) {
1217 fprintf( stderr, "Could not unset controls\n");
1220 (void) ldap_unbind_ext( ld, NULL, NULL );
1224 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1226 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1228 int i = 0, j, crit = 0, err;
1229 LDAPControl c[10], **ctrls;
1233 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1235 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1241 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1243 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1249 ctrls = (LDAPControl**) malloc(sizeof(c) + (count+1)*sizeof(LDAPControl*));
1250 if ( ctrls == NULL ) {
1251 fprintf( stderr, "No memory\n" );
1252 exit( EXIT_FAILURE );
1256 BerElementBuffer berbuf;
1257 BerElement *ber = (BerElement *)&berbuf;
1259 if( assertion == NULL || *assertion == '\0' ) {
1260 fprintf( stderr, "Assertion=<empty>\n" );
1261 exit( EXIT_FAILURE );
1264 ber_init2( ber, NULL, LBER_USE_DER );
1266 err = ldap_pvt_put_filter( ber, assertion );
1268 fprintf( stderr, "assertion encode failed (%d)\n", err );
1269 exit( EXIT_FAILURE );
1272 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1274 fprintf( stderr, "assertion flatten failed (%d)\n", err );
1275 exit( EXIT_FAILURE );
1278 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1279 c[i].ldctl_iscritical = assertctl > 1;
1285 c[i].ldctl_value.bv_val = authzid;
1286 c[i].ldctl_value.bv_len = strlen( authzid );
1287 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1288 c[i].ldctl_iscritical = 1;
1293 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1294 /* NOTE: doesn't need an extra count because it's incompatible
1297 BerElementBuffer berbuf;
1298 BerElement *ber = (BerElement *)&berbuf;
1300 ber_init2( ber, NULL, LBER_USE_DER );
1302 if ( ber_printf( ber, "s", proxydn ) == LBER_ERROR ) {
1303 exit( EXIT_FAILURE );
1306 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1307 exit( EXIT_FAILURE );
1310 c[i].ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1311 c[i].ldctl_iscritical = 1;
1315 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1318 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1319 BER_BVZERO( &c[i].ldctl_value );
1320 c[i].ldctl_iscritical = manageDIT > 1;
1325 if ( manageDSAit ) {
1326 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1327 BER_BVZERO( &c[i].ldctl_value );
1328 c[i].ldctl_iscritical = manageDSAit > 1;
1334 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1335 BER_BVZERO( &c[i].ldctl_value );
1336 c[i].ldctl_iscritical = noop > 1;
1342 char berbuf[LBER_ELEMENT_SIZEOF];
1343 BerElement *ber = (BerElement *)berbuf;
1344 char **attrs = NULL;
1346 if( preread_attrs ) {
1347 attrs = ldap_str2charray( preread_attrs, "," );
1350 ber_init2( ber, NULL, LBER_USE_DER );
1352 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1353 fprintf( stderr, "preread attrs encode failed.\n" );
1354 exit( EXIT_FAILURE );
1357 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1359 fprintf( stderr, "preread flatten failed (%d)\n", err );
1360 exit( EXIT_FAILURE );
1363 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1364 c[i].ldctl_iscritical = preread > 1;
1368 if( attrs ) ldap_charray_free( attrs );
1372 char berbuf[LBER_ELEMENT_SIZEOF];
1373 BerElement *ber = (BerElement *)berbuf;
1374 char **attrs = NULL;
1376 if( postread_attrs ) {
1377 attrs = ldap_str2charray( postread_attrs, "," );
1380 ber_init2( ber, NULL, LBER_USE_DER );
1382 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1383 fprintf( stderr, "postread attrs encode failed.\n" );
1384 exit( EXIT_FAILURE );
1387 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1389 fprintf( stderr, "postread flatten failed (%d)\n", err );
1390 exit( EXIT_FAILURE );
1393 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1394 c[i].ldctl_iscritical = postread > 1;
1398 if( attrs ) ldap_charray_free( attrs );
1401 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1403 if ( chainingResolve > -1 ) {
1404 BerElementBuffer berbuf;
1405 BerElement *ber = (BerElement *)&berbuf;
1407 ber_init2( ber, NULL, LBER_USE_DER );
1409 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1412 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1413 exit( EXIT_FAILURE );
1416 if ( chainingContinuation > -1 ) {
1417 err = ber_printf( ber, "e", chainingContinuation );
1420 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1421 exit( EXIT_FAILURE );
1425 err = ber_printf( ber, /* { */ "N}" );
1428 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1429 exit( EXIT_FAILURE );
1432 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1433 exit( EXIT_FAILURE );
1437 BER_BVZERO( &c[i].ldctl_value );
1440 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1441 c[i].ldctl_iscritical = chaining > 1;
1445 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1448 ctrls[i++] = extra_c++;
1452 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1454 if ( err != LDAP_OPT_SUCCESS ) {
1455 for ( j = 0; j < i; j++ ) {
1456 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1458 fprintf( stderr, "Could not set %scontrols\n",
1459 crit ? "critical " : "" );
1464 exit( EXIT_FAILURE );
1469 tool_check_abandon( LDAP *ld, int msgid )
1473 switch ( gotintr ) {
1474 case LDAP_REQ_EXTENDED:
1475 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1476 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1477 rc, ldap_err2string( rc ) );
1480 case LDAP_REQ_ABANDON:
1481 rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
1482 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1483 rc, ldap_err2string( rc ) );
1491 print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
1496 tool_write_ldif( LDIF_PUT_COMMENT, "==> ",
1497 what->bv_val, what->bv_len );
1498 ber = ber_init( &ctrl->ldctl_value );
1499 if ( ber == NULL ) {
1503 } else if ( ber_scanf( ber, "{m{" /*}}*/, &bv ) == LBER_ERROR ) {
1508 tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1510 while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
1512 BerVarray vals = NULL;
1514 if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
1521 for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
1523 ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1524 bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
1527 ber_bvarray_free( vals );
1531 if ( ber != NULL ) {
1535 tool_write_ldif( LDIF_PUT_COMMENT, "<== ",
1536 what->bv_val, what->bv_len );
1542 print_preread( LDAP *ld, LDAPControl *ctrl )
1544 static struct berval what = BER_BVC( "preread" );
1546 return print_prepostread( ld, ctrl, &what );
1550 print_postread( LDAP *ld, LDAPControl *ctrl )
1552 static struct berval what = BER_BVC( "postread" );
1554 return print_prepostread( ld, ctrl, &what );
1558 print_paged_results( LDAP *ld, LDAPControl *ctrl )
1562 /* note: pr_cookie is being malloced; it's freed
1563 * the next time the control is sent, but the last
1564 * time it's not; we don't care too much, because
1565 * the last time an empty value is returned... */
1566 if ( ldap_parse_pageresponse_control( ld, ctrl, &estimate, &pr_cookie )
1573 /* FIXME: check buffer overflow */
1574 char buf[ BUFSIZ ], *ptr = buf;
1576 if ( estimate > 0 ) {
1577 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1578 "estimate=%d", estimate );
1581 if ( pr_cookie.bv_len > 0 ) {
1584 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
1585 pr_cookie.bv_len ) + 1;
1586 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
1588 bv.bv_len = lutil_b64_ntop(
1589 (unsigned char *) pr_cookie.bv_val,
1591 bv.bv_val, bv.bv_len );
1593 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1594 "%scookie=%s", ptr == buf ? "" : " ",
1597 ber_memfree( bv.bv_val );
1599 pr_morePagedResults = 1;
1602 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1603 "%scookie=", ptr == buf ? "" : " " );
1606 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1607 "pagedresults", buf, ptr - buf );
1613 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1615 print_ppolicy( LDAP *ld, LDAPControl *ctrl )
1617 int expire = 0, grace = 0, rc;
1618 LDAPPasswordPolicyError pperr;
1620 rc = ldap_parse_passwordpolicy_control( ld, ctrl,
1621 &expire, &grace, &pperr );
1622 if ( rc == LDAP_SUCCESS ) {
1623 char buf[ BUFSIZ ], *ptr = buf;
1625 if ( expire != -1 ) {
1626 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1627 "expire=%d", expire );
1630 if ( grace != -1 ) {
1631 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1632 "%sgrace=%d", ptr == buf ? "" : " ", grace );
1635 if ( pperr != PP_noError ) {
1636 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1637 "%serror=%s", ptr == buf ? "" : " ",
1638 ldap_passwordpolicy_err2txt( pperr ) );
1641 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1642 "ppolicy", buf, ptr - buf );
1649 void tool_print_ctrls(
1651 LDAPControl **ctrls )
1656 for ( i = 0; ctrls[i] != NULL; i++ ) {
1657 /* control: OID criticality base64value */
1658 struct berval b64 = BER_BVNULL;
1664 len += strlen( ctrls[i]->ldctl_oid );
1666 /* add enough for space after OID and the critical value itself */
1667 len += ctrls[i]->ldctl_iscritical
1668 ? sizeof("true") : sizeof("false");
1670 /* convert to base64 */
1671 if ( ctrls[i]->ldctl_value.bv_len ) {
1672 b64.bv_len = LUTIL_BASE64_ENCODE_LEN(
1673 ctrls[i]->ldctl_value.bv_len ) + 1;
1674 b64.bv_val = ber_memalloc( b64.bv_len + 1 );
1676 b64.bv_len = lutil_b64_ntop(
1677 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
1678 ctrls[i]->ldctl_value.bv_len,
1679 b64.bv_val, b64.bv_len );
1683 len += 1 + b64.bv_len;
1686 ptr = str = malloc( len + 1 );
1688 ptr = lutil_strcopy( ptr, ": " );
1690 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_oid );
1691 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_iscritical
1692 ? " true" : " false" );
1695 ptr = lutil_strcopy( ptr, " " );
1696 ptr = lutil_strcopy( ptr, b64.bv_val );
1700 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1701 "control", str, len );
1706 ber_memfree( b64.bv_val );
1709 /* known controls */
1710 for ( j = 0; tool_ctrl_response[j].oid != NULL; j++ ) {
1711 if ( strcmp( tool_ctrl_response[j].oid, ctrls[i]->ldctl_oid ) == 0 ) {
1712 if ( !tool_ctrl_response[j].mask & tool_type ) {
1713 /* this control should not appear
1714 * with this tool; warning? */
1720 if ( tool_ctrl_response[j].oid != NULL && tool_ctrl_response[j].func ) {
1721 (void)tool_ctrl_response[j].func( ld, ctrls[i] );
1727 tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
1731 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1735 fputs( ldif, stdout );
1736 ber_memfree( ldif );
1742 tool_is_oid( const char *s )
1746 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
1750 for ( ; s[ 0 ]; s++ ) {
1751 if ( s[ 0 ] == '.' ) {
1752 if ( s[ 1 ] == '\0' ) {
1759 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
1763 if ( first == 1 && s[ 0 ] == '0' && s[ 1 ] != '.' ) {