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 /* input-related vars */
58 /* misc. parameters */
59 tool_type_t tool_type;
71 char *ldaphost = NULL;
81 struct berval passwd = { 0, NULL };
83 #ifdef HAVE_CYRUS_SASL
84 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
85 char *sasl_realm = NULL;
86 char *sasl_authc_id = NULL;
87 char *sasl_authz_id = NULL;
88 char *sasl_mech = NULL;
89 char *sasl_secprops = NULL;
94 char *assertion = NULL;
96 /* support deprecated early version of proxyAuthz */
97 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ "2.16.840.1.113730.3.4.12"
98 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
100 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
106 static char *preread_attrs = NULL;
108 static char *postread_attrs = NULL;
109 ber_int_t pr_morePagedResults = 1;
110 struct berval pr_cookie = { 0, NULL };
111 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
113 static int chainingResolve = -1;
114 static int chainingContinuation = -1;
115 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
117 LDAPControl *unknown_ctrls = NULL;
118 int unknown_ctrls_num = 0;
121 struct timeval nettimeout = { -1 , 0 };
123 typedef int (*print_ctrl_fn)( LDAP *ld, LDAPControl *ctrl );
125 static int print_preread( LDAP *ld, LDAPControl *ctrl );
126 static int print_postread( LDAP *ld, LDAPControl *ctrl );
127 static int print_paged_results( LDAP *ld, LDAPControl *ctrl );
128 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
129 static int print_ppolicy( LDAP *ld, LDAPControl *ctrl );
132 static struct tool_ctrls_t {
136 } tool_ctrl_response[] = {
137 { LDAP_CONTROL_PRE_READ, TOOL_ALL, print_preread },
138 { LDAP_CONTROL_POST_READ, TOOL_ALL, print_postread },
139 { LDAP_CONTROL_PAGEDRESULTS, TOOL_SEARCH, print_paged_results },
140 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
141 { LDAP_CONTROL_PASSWORDPOLICYRESPONSE, TOOL_ALL, print_ppolicy },
157 tool_init( tool_type_t type )
160 ldap_pvt_setlocale(LC_MESSAGES, "");
161 ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
162 ldap_pvt_textdomain(OPENLDAP_PACKAGE);
168 #ifdef HAVE_CYRUS_SASL
172 ldap_pvt_tls_destroy();
177 tool_common_usage( void )
179 static const char *const descriptions[] = {
180 N_(" -c continuous operation mode (do not stop on errors)\n"),
181 N_(" -d level set LDAP debugging level to `level'\n"),
182 N_(" -D binddn bind DN\n"),
183 N_(" -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
184 N_(" [!]assert=<filter> (a RFC 4515 Filter string)\n")
185 N_(" [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n")
186 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
188 /* non-advertized support for proxyDN */
189 N_(" [!]proxydn=<dn> (a RFC 4514 DN string)\n")
192 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
193 N_(" [!]chaining[=<resolveBehavior>[/<continuationBehavior>]]\n")
194 N_(" one of \"chainingPreferred\", \"chainingRequired\",\n")
195 N_(" \"referralsPreferred\", \"referralsRequired\"\n")
196 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
197 N_(" [!]manageDSAit\n")
199 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
202 N_(" [!]postread[=<attrs>] (a comma-separated attribute list)\n")
203 N_(" [!]preread[=<attrs>] (a comma-separated attribute list)\n")
207 N_(" abandon, cancel, ignore (SIGINT sends abandon/cancel,\n"
208 " or ignores response; if critical, doesn't wait for SIGINT.\n"
209 " not really controls)\n")
210 N_(" -f file read operations from `file'\n"),
211 N_(" -h host LDAP server\n"),
212 N_(" -H URI LDAP Uniform Resource Identifier(s)\n"),
213 N_(" -I use SASL Interactive mode\n"),
214 N_(" -M enable Manage DSA IT control (-MM to make critical)\n"),
215 N_(" -n show what would be done but don't actually do it\n"),
216 N_(" -O props SASL security properties\n"),
217 N_(" -o <opt>[=<optparam] general options\n"),
218 N_(" nettimeout=<timeout> (in seconds, or \"none\" or \"max\")\n"),
219 N_(" -p port port on LDAP server\n"),
220 N_(" -P version protocol version (default: 3)\n"),
221 N_(" -Q use SASL Quiet mode\n"),
222 N_(" -R realm SASL realm\n"),
223 N_(" -U authcid SASL authentication identity\n"),
224 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
225 N_(" -V print version info (-VV only)\n"),
226 N_(" -w passwd bind password (for simple authentication)\n"),
227 N_(" -W prompt for bind password\n"),
228 N_(" -x Simple authentication\n"),
229 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
230 N_(" -y file Read password from file\n"),
231 N_(" -Y mech SASL mechanism\n"),
232 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
235 const char *const *cpp;
237 fputs( _("Common options:\n"), stderr );
238 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
239 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
240 fputs( _(*cpp), stderr );
253 fprintf( stderr, "%s: %s (%d)%s\n",
254 func, ldap_err2string( err ), err, extra ? extra : "" );
256 if ( matched && *matched ) {
257 fprintf( stderr, _("\tmatched DN: %s\n"), matched );
260 if ( info && *info ) {
261 fprintf( stderr, _("\tadditional info: %s\n"), info );
264 if ( refs && *refs ) {
266 fprintf( stderr, _("\treferrals:\n") );
267 for( i=0; refs[i]; i++ ) {
268 fprintf( stderr, "\t\t%s\n", refs[i] );
275 tool_args( int argc, char **argv )
279 while (( i = getopt( argc, argv, options )) != EOF ) {
281 char *control, *cvalue, *next;
283 case 'c': /* continuous operation mode */
290 ival = strtol( optarg, &next, 10 );
291 if (next == NULL || next[0] != '\0') {
292 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
297 case 'D': /* bind DN */
298 if( binddn != NULL ) {
299 fprintf( stderr, "%s: -D previously specified\n", prog );
300 exit( EXIT_FAILURE );
302 binddn = ber_strdup( optarg );
304 case 'e': /* general extensions (controls and such) */
305 /* should be extended to support comma separated list of
306 * [!]key[=value] parameters, e.g. -e !foo,bar=567
311 if( optarg[0] == '!' ) {
316 control = ber_strdup( optarg );
317 if ( (cvalue = strchr( control, '=' )) != NULL ) {
321 if ( strcasecmp( control, "assert" ) == 0 ) {
323 fprintf( stderr, "assert control previously specified\n");
324 exit( EXIT_FAILURE );
326 if( cvalue == NULL ) {
327 fprintf( stderr, "assert: control value expected\n" );
331 assertctl = 1 + crit;
333 assert( assertion == NULL );
336 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
337 if( authzid != NULL ) {
338 fprintf( stderr, "authzid control previously specified\n");
339 exit( EXIT_FAILURE );
341 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
342 if( proxydn != NULL ) {
343 fprintf( stderr, "authzid control incompatible with proxydn\n");
344 exit( EXIT_FAILURE );
346 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
347 if( cvalue == NULL ) {
348 fprintf( stderr, "authzid: control value expected\n" );
352 fprintf( stderr, "authzid: must be marked critical\n" );
356 assert( authzid == NULL );
359 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
360 } else if ( strcasecmp( control, "proxydn" ) == 0 ) {
361 if( proxydn != NULL ) {
362 fprintf( stderr, "proxydn control previously specified\n");
363 exit( EXIT_FAILURE );
365 if( authzid != NULL ) {
366 fprintf( stderr, "proxydn control incompatible with authzid\n");
367 exit( EXIT_FAILURE );
369 if( cvalue == NULL ) {
370 fprintf( stderr, "proxydn: control value expected\n" );
374 fprintf( stderr, "proxydn: must be marked critical\n" );
378 assert( proxydn == NULL );
380 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
382 } else if ( ( strcasecmp( control, "relax" ) == 0 ) ||
383 ( strcasecmp( control, "manageDIT" ) == 0 ) )
387 "relax control previously specified\n");
388 exit( EXIT_FAILURE );
390 if( cvalue != NULL ) {
392 "relax: no control value expected\n" );
396 manageDIT = 1 + crit;
398 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
401 "manageDSAit control previously specified\n");
402 exit( EXIT_FAILURE );
404 if( cvalue != NULL ) {
406 "manageDSAit: no control value expected\n" );
410 manageDSAit = 1 + crit;
412 } else if ( strcasecmp( control, "noop" ) == 0 ) {
414 fprintf( stderr, "noop control previously specified\n");
415 exit( EXIT_FAILURE );
417 if( cvalue != NULL ) {
418 fprintf( stderr, "noop: no control value expected\n" );
424 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
425 } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
427 fprintf( stderr, "ppolicy control previously specified\n");
428 exit( EXIT_FAILURE );
430 if( cvalue != NULL ) {
431 fprintf( stderr, "ppolicy: no control value expected\n" );
435 fprintf( stderr, "ppolicy: critical flag not allowed\n" );
442 } else if ( strcasecmp( control, "preread" ) == 0 ) {
444 fprintf( stderr, "preread control previously specified\n");
445 exit( EXIT_FAILURE );
449 preread_attrs = cvalue;
451 } else if ( strcasecmp( control, "postread" ) == 0 ) {
453 fprintf( stderr, "postread control previously specified\n");
454 exit( EXIT_FAILURE );
458 postread_attrs = cvalue;
460 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
461 } else if ( strcasecmp( control, "chaining" ) == 0 ) {
464 if ( cvalue != NULL ) {
467 continuation = strchr( cvalue, '/' );
468 if ( continuation ) {
469 /* FIXME: this makes sense only in searches */
470 *continuation++ = '\0';
471 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
472 chainingContinuation = LDAP_CHAINING_PREFERRED;
473 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
474 chainingContinuation = LDAP_CHAINING_REQUIRED;
475 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
476 chainingContinuation = LDAP_REFERRALS_PREFERRED;
477 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
478 chainingContinuation = LDAP_REFERRALS_REQUIRED;
481 "chaining behavior control "
482 "continuation value \"%s\" invalid\n",
484 exit( EXIT_FAILURE );
488 if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
489 chainingResolve = LDAP_CHAINING_PREFERRED;
490 } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
491 chainingResolve = LDAP_CHAINING_REQUIRED;
492 } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
493 chainingResolve = LDAP_REFERRALS_PREFERRED;
494 } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
495 chainingResolve = LDAP_REFERRALS_REQUIRED;
498 "chaining behavior control "
499 "resolve value \"%s\" invalid\n",
501 exit( EXIT_FAILURE );
504 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
506 /* this shouldn't go here, really; but it's a feature... */
507 } else if ( strcasecmp( control, "abandon" ) == 0 ) {
508 abcan = LDAP_REQ_ABANDON;
513 } else if ( strcasecmp( control, "cancel" ) == 0 ) {
514 abcan = LDAP_REQ_EXTENDED;
519 } else if ( strcasecmp( control, "ignore" ) == 0 ) {
525 } else if ( tool_is_oid( control ) ) {
526 LDAPControl *tmpctrls, ctrl;
528 tmpctrls = (LDAPControl *)realloc( unknown_ctrls,
529 (unknown_ctrls_num + 1)*sizeof( LDAPControl ) );
530 if ( tmpctrls == NULL ) {
531 fprintf( stderr, "%s: no memory?\n", prog );
532 exit( EXIT_FAILURE );
534 unknown_ctrls = tmpctrls;
535 ctrl.ldctl_oid = control;
536 ctrl.ldctl_value.bv_val = NULL;
537 ctrl.ldctl_value.bv_len = 0;
538 ctrl.ldctl_iscritical = crit;
540 if ( cvalue != NULL ) {
542 size_t len = strlen( cvalue );
545 bv.bv_len = LUTIL_BASE64_DECODE_LEN( len );
546 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
548 retcode = lutil_b64_pton( cvalue,
549 (unsigned char *)bv.bv_val,
552 if ( retcode == -1 || retcode > bv.bv_len ) {
553 fprintf( stderr, "Unable to parse value of general control %s\n",
559 ctrl.ldctl_value = bv;
562 unknown_ctrls[ unknown_ctrls_num ] = ctrl;
566 fprintf( stderr, "Invalid general control name: %s\n",
571 case 'f': /* read from file */
572 if( infile != NULL ) {
573 fprintf( stderr, "%s: -f previously specified\n", prog );
574 exit( EXIT_FAILURE );
576 infile = ber_strdup( optarg );
578 case 'h': /* ldap host */
579 if( ldaphost != NULL ) {
580 fprintf( stderr, "%s: -h previously specified\n", prog );
581 exit( EXIT_FAILURE );
583 ldaphost = ber_strdup( optarg );
585 case 'H': /* ldap URI */
586 if( ldapuri != NULL ) {
587 fprintf( stderr, "%s: -H previously specified\n", prog );
588 exit( EXIT_FAILURE );
590 ldapuri = ber_strdup( optarg );
593 #ifdef HAVE_CYRUS_SASL
594 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
595 fprintf( stderr, "%s: incompatible previous "
596 "authentication choice\n",
598 exit( EXIT_FAILURE );
600 authmethod = LDAP_AUTH_SASL;
601 sasl_flags = LDAP_SASL_INTERACTIVE;
604 fprintf( stderr, "%s: was not compiled with SASL support\n",
606 exit( EXIT_FAILURE );
609 /* enable Manage DSA IT */
612 case 'n': /* print operations, don't actually do them */
616 control = ber_strdup( optarg );
617 if ( (cvalue = strchr( control, '=' )) != NULL ) {
621 if ( strcasecmp( control, "nettimeout" ) == 0 ) {
622 if( nettimeout.tv_sec != -1 ) {
623 fprintf( stderr, "nettimeout option previously specified\n");
624 exit( EXIT_FAILURE );
626 if( cvalue == NULL || cvalue[0] == '\0' ) {
627 fprintf( stderr, "nettimeout: option value expected\n" );
630 if ( strcasecmp( cvalue, "none" ) == 0 ) {
631 nettimeout.tv_sec = 0;
632 } else if ( strcasecmp( cvalue, "max" ) == 0 ) {
633 nettimeout.tv_sec = LDAP_MAXINT;
635 ival = strtol( cvalue, &next, 10 );
636 if ( next == NULL || next[0] != '\0' ) {
638 _("Unable to parse network timeout \"%s\"\n"), cvalue );
639 exit( EXIT_FAILURE );
641 nettimeout.tv_sec = ival;
643 if( nettimeout.tv_sec < 0 || nettimeout.tv_sec > LDAP_MAXINT ) {
644 fprintf( stderr, _("%s: invalid network timeout (%ld) specified\n"),
645 prog, (long)nettimeout.tv_sec );
646 exit( EXIT_FAILURE );
649 fprintf( stderr, "Invalid general option name: %s\n",
655 #ifdef HAVE_CYRUS_SASL
656 if( sasl_secprops != NULL ) {
657 fprintf( stderr, "%s: -O previously specified\n", prog );
658 exit( EXIT_FAILURE );
660 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
661 fprintf( stderr, "%s: incompatible previous "
662 "authentication choice\n", prog );
663 exit( EXIT_FAILURE );
665 authmethod = LDAP_AUTH_SASL;
666 sasl_secprops = ber_strdup( optarg );
668 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
669 exit( EXIT_FAILURE );
674 fprintf( stderr, "%s: -p previously specified\n", prog );
675 exit( EXIT_FAILURE );
677 ival = strtol( optarg, &next, 10 );
678 if ( next == NULL || next[0] != '\0' ) {
679 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
680 exit( EXIT_FAILURE );
685 ival = strtol( optarg, &next, 10 );
686 if ( next == NULL || next[0] != '\0' ) {
687 fprintf( stderr, "%s: unabel to parse protocol version \"%s\"\n", prog, optarg );
688 exit( EXIT_FAILURE );
692 if( protocol == LDAP_VERSION3 ) {
693 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
695 exit( EXIT_FAILURE );
697 protocol = LDAP_VERSION2;
700 if( protocol == LDAP_VERSION2 ) {
701 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
703 exit( EXIT_FAILURE );
705 protocol = LDAP_VERSION3;
708 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
714 #ifdef HAVE_CYRUS_SASL
715 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
716 fprintf( stderr, "%s: incompatible previous "
717 "authentication choice\n",
719 exit( EXIT_FAILURE );
721 authmethod = LDAP_AUTH_SASL;
722 sasl_flags = LDAP_SASL_QUIET;
725 fprintf( stderr, "%s: not compiled with SASL support\n",
727 exit( EXIT_FAILURE );
730 #ifdef HAVE_CYRUS_SASL
731 if( sasl_realm != NULL ) {
732 fprintf( stderr, "%s: -R previously specified\n", prog );
733 exit( EXIT_FAILURE );
735 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
736 fprintf( stderr, "%s: incompatible previous "
737 "authentication choice\n",
739 exit( EXIT_FAILURE );
741 authmethod = LDAP_AUTH_SASL;
742 sasl_realm = ber_strdup( optarg );
744 fprintf( stderr, "%s: not compiled with SASL support\n",
746 exit( EXIT_FAILURE );
750 #ifdef HAVE_CYRUS_SASL
751 if( sasl_authc_id != NULL ) {
752 fprintf( stderr, "%s: -U previously specified\n", prog );
753 exit( EXIT_FAILURE );
755 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
756 fprintf( stderr, "%s: incompatible previous "
757 "authentication choice\n",
759 exit( EXIT_FAILURE );
761 authmethod = LDAP_AUTH_SASL;
762 sasl_authc_id = ber_strdup( optarg );
764 fprintf( stderr, "%s: not compiled with SASL support\n",
766 exit( EXIT_FAILURE );
769 case 'v': /* verbose mode */
772 case 'V': /* version */
775 case 'w': /* password */
776 passwd.bv_val = ber_strdup( optarg );
780 for( p = optarg; *p != '\0'; p++ ) {
784 passwd.bv_len = strlen( passwd.bv_val );
793 #ifdef HAVE_CYRUS_SASL
794 if( sasl_mech != NULL ) {
795 fprintf( stderr, "%s: -Y previously specified\n", prog );
796 exit( EXIT_FAILURE );
798 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
800 "%s: incompatible with authentication choice\n", prog );
801 exit( EXIT_FAILURE );
803 authmethod = LDAP_AUTH_SASL;
804 sasl_mech = ber_strdup( optarg );
806 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
807 exit( EXIT_FAILURE );
811 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
812 fprintf( stderr, "%s: incompatible with previous "
813 "authentication choice\n", prog );
814 exit( EXIT_FAILURE );
816 authmethod = LDAP_AUTH_SIMPLE;
819 #ifdef HAVE_CYRUS_SASL
820 if( sasl_authz_id != NULL ) {
821 fprintf( stderr, "%s: -X previously specified\n", prog );
822 exit( EXIT_FAILURE );
824 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
825 fprintf( stderr, "%s: -X incompatible with "
826 "authentication choice\n", prog );
827 exit( EXIT_FAILURE );
829 authmethod = LDAP_AUTH_SASL;
830 sasl_authz_id = ber_strdup( optarg );
832 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
833 exit( EXIT_FAILURE );
840 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
841 exit( EXIT_FAILURE );
845 if( handle_private_option( i ) ) break;
846 fprintf( stderr, "%s: unrecognized option -%c\n",
853 /* prevent bad linking */
855 api.ldapai_info_version = LDAP_API_INFO_VERSION;
857 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
858 != LDAP_OPT_SUCCESS )
860 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
861 exit( EXIT_FAILURE );
864 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
865 fprintf( stderr, "LDAP APIInfo version mismatch: "
866 "library %d, header %d\n",
867 api.ldapai_info_version, LDAP_API_INFO_VERSION );
868 exit( EXIT_FAILURE );
871 if( api.ldapai_api_version != LDAP_API_VERSION ) {
872 fprintf( stderr, "LDAP API version mismatch: "
873 "library %d, header %d\n",
874 api.ldapai_api_version, LDAP_API_VERSION );
875 exit( EXIT_FAILURE );
878 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
879 fprintf( stderr, "LDAP vendor name mismatch: "
880 "library %s, header %s\n",
881 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
882 exit( EXIT_FAILURE );
885 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
886 fprintf( stderr, "LDAP vendor version mismatch: "
887 "library %d, header %d\n",
888 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
889 exit( EXIT_FAILURE );
893 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
895 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
896 if (version > 1) exit( EXIT_SUCCESS );
899 ldap_memfree( api.ldapai_vendor_name );
900 ber_memvfree( (void **)api.ldapai_extensions );
904 protocol = LDAP_VERSION3;
906 if (authmethod == -1 && protocol > LDAP_VERSION2) {
907 #ifdef HAVE_CYRUS_SASL
908 authmethod = LDAP_AUTH_SASL;
910 authmethod = LDAP_AUTH_SIMPLE;
914 if( ldapuri == NULL ) {
915 if( ldapport && ( ldaphost == NULL )) {
916 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
917 exit( EXIT_FAILURE );
920 if( ldaphost != NULL ) {
921 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
922 exit( EXIT_FAILURE );
925 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
926 exit( EXIT_FAILURE );
930 if( protocol == LDAP_VERSION2 ) {
931 if( assertctl || authzid || manageDIT || manageDSAit ||
932 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
934 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
935 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
938 noop || ppolicy || preread || postread )
940 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
941 exit( EXIT_FAILURE );
945 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
946 exit( EXIT_FAILURE );
949 #ifdef HAVE_CYRUS_SASL
950 if( authmethod == LDAP_AUTH_SASL ) {
951 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
953 exit( EXIT_FAILURE );
961 tool_conn_setup( int dont, void (*private_setup)( LDAP * ) )
966 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
967 != LBER_OPT_SUCCESS )
970 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
972 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
973 != LDAP_OPT_SUCCESS )
976 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
981 (void) SIGNAL( SIGPIPE, SIG_IGN );
985 SIGNAL( SIGINT, do_sig );
991 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
994 memset( &url, 0, sizeof(url));
996 url.lud_scheme = "ldap";
997 url.lud_host = ldaphost;
998 url.lud_port = ldapport;
999 url.lud_scope = LDAP_SCOPE_DEFAULT;
1001 ldapuri = ldap_url_desc2str( &url );
1003 } else if ( ldapuri != NULL ) {
1004 LDAPURLDesc *ludlist, **ludp;
1008 rc = ldap_url_parselist( &ludlist, ldapuri );
1009 if ( rc != LDAP_URL_SUCCESS ) {
1011 "Could not parse LDAP URI(s)=%s (%d)\n",
1013 exit( EXIT_FAILURE );
1016 for ( ludp = &ludlist; *ludp != NULL; ) {
1017 LDAPURLDesc *lud = *ludp;
1020 if ( lud->lud_dn != NULL && lud->lud_dn[ 0 ] != '\0' &&
1021 ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) )
1023 /* if no host but a DN is provided,
1024 * use DNS SRV to gather the host list
1025 * and turn it into a list of URIs
1026 * using the scheme provided */
1027 char *domain = NULL,
1031 len_proto = strlen( lud->lud_scheme );
1033 if ( ldap_dn2domain( lud->lud_dn, &domain )
1037 "DNS SRV: Could not turn "
1038 "DN=\"%s\" into a domain\n",
1043 rc = ldap_domain2hostlist( domain, &hostlist );
1046 "DNS SRV: Could not turn "
1047 "domain=%s into a hostlist\n",
1052 hosts = ldap_str2charray( hostlist, " " );
1053 if ( hosts == NULL ) {
1055 "DNS SRV: Could not parse "
1056 "hostlist=\"%s\"\n",
1061 for ( i = 0; hosts[ i ] != NULL; i++ )
1064 tmp = (char **)realloc( urls, sizeof( char * ) * ( nurls + i + 1 ) );
1065 if ( tmp == NULL ) {
1067 "DNS SRV: out of memory?\n" );
1071 urls[ nurls ] = NULL;
1073 for ( i = 0; hosts[ i ] != NULL; i++ ) {
1074 size_t len = len_proto
1076 + strlen( hosts[ i ] )
1079 urls[ nurls + i + 1 ] = NULL;
1080 urls[ nurls + i ] = (char *)malloc( sizeof( char ) * len );
1081 if ( urls[ nurls + i ] == NULL ) {
1083 "DNS SRV: out of memory?\n" );
1087 snprintf( urls[ nurls + i ], len, "%s://%s",
1088 lud->lud_scheme, hosts[ i ] );
1093 ber_memvfree( (void **)hosts );
1094 ber_memfree( hostlist );
1095 ber_memfree( domain );
1098 tmp = (char **)realloc( urls, sizeof( char * ) * ( nurls + 2 ) );
1099 if ( tmp == NULL ) {
1101 "DNS SRV: out of memory?\n" );
1105 urls[ nurls + 1 ] = NULL;
1107 urls[ nurls ] = ldap_url_desc2str( lud );
1108 if ( urls[ nurls ] == NULL ) {
1110 "DNS SRV: out of memory?\n" );
1116 *ludp = lud->lud_next;
1118 lud->lud_next = NULL;
1119 ldap_free_urldesc( lud );
1122 if ( ludlist != NULL ) {
1123 ldap_free_urllist( ludlist );
1124 exit( EXIT_FAILURE );
1126 } else if ( urls == NULL ) {
1127 exit( EXIT_FAILURE );
1130 ldap_memfree( ldapuri );
1131 ldapuri = ldap_charray2str( urls, " " );
1132 ber_memvfree( (void **)urls );
1136 fprintf( stderr, "ldap_initialize( %s )\n",
1137 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
1139 rc = ldap_initialize( &ld, ldapuri );
1140 if( rc != LDAP_SUCCESS ) {
1142 "Could not create LDAP session handle for URI=%s (%d): %s\n",
1143 ldapuri, rc, ldap_err2string(rc) );
1144 exit( EXIT_FAILURE );
1147 if( private_setup ) private_setup( ld );
1150 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
1151 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
1153 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
1154 referrals ? "on" : "off" );
1155 exit( EXIT_FAILURE );
1158 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
1159 != LDAP_OPT_SUCCESS )
1161 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
1163 exit( EXIT_FAILURE );
1167 rc = ldap_start_tls_s( ld, NULL, NULL );
1168 if ( rc != LDAP_SUCCESS ) {
1169 tool_perror( "ldap_start_tls", rc, NULL, NULL, NULL, NULL );
1170 if ( use_tls > 1 ) {
1171 exit( EXIT_FAILURE );
1176 if ( nettimeout.tv_sec > 0 ) {
1177 if ( ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &nettimeout )
1178 != LDAP_OPT_SUCCESS )
1180 fprintf( stderr, "Could not set LDAP_OPT_NETWORK_TIMEOUT %ld\n",
1181 (long)nettimeout.tv_sec );
1182 exit( EXIT_FAILURE );
1192 tool_bind( LDAP *ld )
1194 LDAPControl **sctrlsp = NULL;
1195 LDAPControl *sctrls[2];
1198 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1201 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1202 c.ldctl_value.bv_val = NULL;
1203 c.ldctl_value.bv_len = 0;
1204 c.ldctl_iscritical = 0;
1205 sctrls[nsctrls] = &c;
1206 sctrls[++nsctrls] = NULL;
1214 assert( nsctrls < sizeof(sctrls)/sizeof(sctrls[0]) );
1216 if ( authmethod == LDAP_AUTH_SASL ) {
1217 #ifdef HAVE_CYRUS_SASL
1221 if( sasl_secprops != NULL ) {
1222 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
1223 (void *) sasl_secprops );
1225 if( rc != LDAP_OPT_SUCCESS ) {
1227 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
1229 exit( LDAP_LOCAL_ERROR );
1233 defaults = lutil_sasl_defaults( ld,
1240 rc = ldap_sasl_interactive_bind_s( ld, binddn, sasl_mech,
1242 NULL, sasl_flags, lutil_sasl_interact, defaults );
1244 lutil_sasl_freedefs( defaults );
1245 if( rc != LDAP_SUCCESS ) {
1246 tool_perror( "ldap_sasl_interactive_bind_s",
1247 rc, NULL, NULL, NULL, NULL );
1251 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1252 exit( LDAP_NOT_SUPPORTED );
1256 LDAPMessage *result;
1257 LDAPControl **ctrls;
1259 char *matched = NULL;
1267 rc = ldap_sasl_bind( ld, binddn, LDAP_SASL_SIMPLE, &passwd,
1268 sctrlsp, NULL, &msgid );
1269 if ( msgid == -1 ) {
1270 tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
1271 NULL, NULL, NULL, NULL );
1276 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 ) {
1277 tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
1278 exit( LDAP_LOCAL_ERROR );
1281 rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
1283 if ( rc != LDAP_SUCCESS ) {
1284 tool_perror( "ldap_bind parse result", rc, NULL, matched, info, refs );
1285 exit( LDAP_LOCAL_ERROR );
1288 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1289 if ( ctrls && ppolicy ) {
1291 int expire, grace, len = 0;
1292 LDAPPasswordPolicyError pErr = -1;
1294 ctrl = ldap_find_control( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
1297 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
1298 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1300 if ( pErr != PP_noError ){
1303 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1304 len = strlen( msgbuf );
1306 if ( expire >= 0 ) {
1307 sprintf( msgbuf+len,
1308 " (Password expires in %d seconds)",
1310 } else if ( grace >= 0 ) {
1311 sprintf( msgbuf+len,
1312 " (Password expired, %d grace logins remain)",
1320 ldap_controls_free( ctrls );
1323 if ( err != LDAP_SUCCESS
1325 || ( matched && matched[ 0 ] )
1326 || ( info && info[ 0 ] )
1329 tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1331 if( matched ) ber_memfree( matched );
1332 if( info ) ber_memfree( info );
1333 if( refs ) ber_memvfree( (void **)refs );
1335 if ( err != LDAP_SUCCESS ) exit( err );
1341 tool_unbind( LDAP *ld )
1343 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1345 if ( err != LDAP_OPT_SUCCESS ) {
1346 fprintf( stderr, "Could not unset controls\n");
1349 (void) ldap_unbind_ext( ld, NULL, NULL );
1353 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1355 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1357 int i = 0, j, crit = 0, err;
1358 LDAPControl c[12], **ctrls;
1362 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1364 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1368 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1373 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1375 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1377 || unknown_ctrls_num ) )
1382 ctrls = (LDAPControl**) malloc(sizeof(c) + (count + unknown_ctrls_num + 1)*sizeof(LDAPControl*));
1383 if ( ctrls == NULL ) {
1384 fprintf( stderr, "No memory\n" );
1385 exit( EXIT_FAILURE );
1389 BerElementBuffer berbuf;
1390 BerElement *ber = (BerElement *)&berbuf;
1392 if( assertion == NULL || *assertion == '\0' ) {
1393 fprintf( stderr, "Assertion=<empty>\n" );
1394 exit( EXIT_FAILURE );
1397 ber_init2( ber, NULL, LBER_USE_DER );
1399 err = ldap_pvt_put_filter( ber, assertion );
1401 fprintf( stderr, "assertion encode failed (%d)\n", err );
1402 exit( EXIT_FAILURE );
1405 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1407 fprintf( stderr, "assertion flatten failed (%d)\n", err );
1408 exit( EXIT_FAILURE );
1411 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1412 c[i].ldctl_iscritical = assertctl > 1;
1418 c[i].ldctl_value.bv_val = authzid;
1419 c[i].ldctl_value.bv_len = strlen( authzid );
1420 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1421 c[i].ldctl_iscritical = 1;
1426 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1427 /* NOTE: doesn't need an extra count because it's incompatible
1430 BerElementBuffer berbuf;
1431 BerElement *ber = (BerElement *)&berbuf;
1433 ber_init2( ber, NULL, LBER_USE_DER );
1435 if ( ber_printf( ber, "s", proxydn ) == LBER_ERROR ) {
1436 exit( EXIT_FAILURE );
1439 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1440 exit( EXIT_FAILURE );
1443 c[i].ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1444 c[i].ldctl_iscritical = 1;
1448 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1451 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1452 BER_BVZERO( &c[i].ldctl_value );
1453 c[i].ldctl_iscritical = manageDIT > 1;
1458 if ( manageDSAit ) {
1459 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1460 BER_BVZERO( &c[i].ldctl_value );
1461 c[i].ldctl_iscritical = manageDSAit > 1;
1467 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1468 BER_BVZERO( &c[i].ldctl_value );
1469 c[i].ldctl_iscritical = noop > 1;
1474 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1476 c[i].ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1477 BER_BVZERO( &c[i].ldctl_value );
1478 c[i].ldctl_iscritical = 0;
1485 char berbuf[LBER_ELEMENT_SIZEOF];
1486 BerElement *ber = (BerElement *)berbuf;
1487 char **attrs = NULL;
1489 if( preread_attrs ) {
1490 attrs = ldap_str2charray( preread_attrs, "," );
1493 ber_init2( ber, NULL, LBER_USE_DER );
1495 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1496 fprintf( stderr, "preread attrs encode failed.\n" );
1497 exit( EXIT_FAILURE );
1500 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1502 fprintf( stderr, "preread flatten failed (%d)\n", err );
1503 exit( EXIT_FAILURE );
1506 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1507 c[i].ldctl_iscritical = preread > 1;
1511 if( attrs ) ldap_charray_free( attrs );
1515 char berbuf[LBER_ELEMENT_SIZEOF];
1516 BerElement *ber = (BerElement *)berbuf;
1517 char **attrs = NULL;
1519 if( postread_attrs ) {
1520 attrs = ldap_str2charray( postread_attrs, "," );
1523 ber_init2( ber, NULL, LBER_USE_DER );
1525 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1526 fprintf( stderr, "postread attrs encode failed.\n" );
1527 exit( EXIT_FAILURE );
1530 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1532 fprintf( stderr, "postread flatten failed (%d)\n", err );
1533 exit( EXIT_FAILURE );
1536 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1537 c[i].ldctl_iscritical = postread > 1;
1541 if( attrs ) ldap_charray_free( attrs );
1544 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1546 if ( chainingResolve > -1 ) {
1547 BerElementBuffer berbuf;
1548 BerElement *ber = (BerElement *)&berbuf;
1550 ber_init2( ber, NULL, LBER_USE_DER );
1552 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1555 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1556 exit( EXIT_FAILURE );
1559 if ( chainingContinuation > -1 ) {
1560 err = ber_printf( ber, "e", chainingContinuation );
1563 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1564 exit( EXIT_FAILURE );
1568 err = ber_printf( ber, /* { */ "N}" );
1571 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1572 exit( EXIT_FAILURE );
1575 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1576 exit( EXIT_FAILURE );
1580 BER_BVZERO( &c[i].ldctl_value );
1583 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1584 c[i].ldctl_iscritical = chaining > 1;
1588 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1591 ctrls[i++] = extra_c++;
1593 for ( count = 0; count < unknown_ctrls_num; count++ ) {
1594 ctrls[i++] = &unknown_ctrls[count];
1598 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1600 if ( err != LDAP_OPT_SUCCESS ) {
1601 for ( j = 0; j < i; j++ ) {
1602 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1604 fprintf( stderr, "Could not set %scontrols\n",
1605 crit ? "critical " : "" );
1610 exit( EXIT_FAILURE );
1615 tool_check_abandon( LDAP *ld, int msgid )
1619 switch ( gotintr ) {
1620 case LDAP_REQ_EXTENDED:
1621 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1622 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1623 rc, ldap_err2string( rc ) );
1626 case LDAP_REQ_ABANDON:
1627 rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
1628 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1629 rc, ldap_err2string( rc ) );
1633 /* just unbind, ignoring the request */
1641 print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
1646 tool_write_ldif( LDIF_PUT_COMMENT, "==> ",
1647 what->bv_val, what->bv_len );
1648 ber = ber_init( &ctrl->ldctl_value );
1649 if ( ber == NULL ) {
1653 } else if ( ber_scanf( ber, "{m{" /*}}*/, &bv ) == LBER_ERROR ) {
1658 tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1660 while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
1662 BerVarray vals = NULL;
1664 if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
1671 for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
1673 ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1674 bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
1677 ber_bvarray_free( vals );
1681 if ( ber != NULL ) {
1685 tool_write_ldif( LDIF_PUT_COMMENT, "<== ",
1686 what->bv_val, what->bv_len );
1692 print_preread( LDAP *ld, LDAPControl *ctrl )
1694 static struct berval what = BER_BVC( "preread" );
1696 return print_prepostread( ld, ctrl, &what );
1700 print_postread( LDAP *ld, LDAPControl *ctrl )
1702 static struct berval what = BER_BVC( "postread" );
1704 return print_prepostread( ld, ctrl, &what );
1708 print_paged_results( LDAP *ld, LDAPControl *ctrl )
1712 /* note: pr_cookie is being malloced; it's freed
1713 * the next time the control is sent, but the last
1714 * time it's not; we don't care too much, because
1715 * the last time an empty value is returned... */
1716 if ( ldap_parse_pageresponse_control( ld, ctrl, &estimate, &pr_cookie )
1723 /* FIXME: check buffer overflow */
1724 char buf[ BUFSIZ ], *ptr = buf;
1726 if ( estimate > 0 ) {
1727 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1728 "estimate=%d", estimate );
1731 if ( pr_cookie.bv_len > 0 ) {
1734 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
1735 pr_cookie.bv_len ) + 1;
1736 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
1738 bv.bv_len = lutil_b64_ntop(
1739 (unsigned char *) pr_cookie.bv_val,
1741 bv.bv_val, bv.bv_len );
1743 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1744 "%scookie=%s", ptr == buf ? "" : " ",
1747 ber_memfree( bv.bv_val );
1749 pr_morePagedResults = 1;
1752 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1753 "%scookie=", ptr == buf ? "" : " " );
1756 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1757 "pagedresults", buf, ptr - buf );
1763 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1765 print_ppolicy( LDAP *ld, LDAPControl *ctrl )
1767 int expire = 0, grace = 0, rc;
1768 LDAPPasswordPolicyError pperr;
1770 rc = ldap_parse_passwordpolicy_control( ld, ctrl,
1771 &expire, &grace, &pperr );
1772 if ( rc == LDAP_SUCCESS ) {
1773 char buf[ BUFSIZ ], *ptr = buf;
1775 if ( expire != -1 ) {
1776 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1777 "expire=%d", expire );
1780 if ( grace != -1 ) {
1781 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1782 "%sgrace=%d", ptr == buf ? "" : " ", grace );
1785 if ( pperr != PP_noError ) {
1786 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1787 "%serror=%d (%s)", ptr == buf ? "" : " ",
1789 ldap_passwordpolicy_err2txt( pperr ) );
1792 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1793 "ppolicy", buf, ptr - buf );
1800 void tool_print_ctrls(
1802 LDAPControl **ctrls )
1807 for ( i = 0; ctrls[i] != NULL; i++ ) {
1808 /* control: OID criticality base64value */
1809 struct berval b64 = BER_BVNULL;
1814 /* FIXME: there might be cases where a control has NULL OID;
1815 * this makes little sense, especially when returned by the
1816 * server, but libldap happily allows it */
1817 if ( ctrls[i]->ldctl_oid == NULL ) {
1822 len += strlen( ctrls[i]->ldctl_oid );
1824 /* add enough for space after OID and the critical value itself */
1825 len += ctrls[i]->ldctl_iscritical
1826 ? sizeof("true") : sizeof("false");
1828 /* convert to base64 */
1829 if ( !BER_BVISNULL( &ctrls[i]->ldctl_value ) ) {
1830 b64.bv_len = LUTIL_BASE64_ENCODE_LEN(
1831 ctrls[i]->ldctl_value.bv_len ) + 1;
1832 b64.bv_val = ber_memalloc( b64.bv_len + 1 );
1834 b64.bv_len = lutil_b64_ntop(
1835 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
1836 ctrls[i]->ldctl_value.bv_len,
1837 b64.bv_val, b64.bv_len );
1841 len += 1 + b64.bv_len;
1844 ptr = str = malloc( len + 1 );
1846 ptr = lutil_strcopy( ptr, ": " );
1848 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_oid );
1849 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_iscritical
1850 ? " true" : " false" );
1853 ptr = lutil_strcopy( ptr, " " );
1854 ptr = lutil_strcopy( ptr, b64.bv_val );
1858 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1859 "control", str, len );
1864 ber_memfree( b64.bv_val );
1867 /* known controls */
1868 for ( j = 0; tool_ctrl_response[j].oid != NULL; j++ ) {
1869 if ( strcmp( tool_ctrl_response[j].oid, ctrls[i]->ldctl_oid ) == 0 ) {
1870 if ( !tool_ctrl_response[j].mask & tool_type ) {
1871 /* this control should not appear
1872 * with this tool; warning? */
1878 if ( tool_ctrl_response[j].oid != NULL && tool_ctrl_response[j].func ) {
1879 (void)tool_ctrl_response[j].func( ld, ctrls[i] );
1885 tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
1889 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1893 fputs( ldif, stdout );
1894 ber_memfree( ldif );
1900 tool_is_oid( const char *s )
1904 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
1908 for ( ; s[ 0 ]; s++ ) {
1909 if ( s[ 0 ] == '.' ) {
1910 if ( s[ 1 ] == '\0' ) {
1917 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
1921 if ( first == 1 && s[ 0 ] == '0' && s[ 1 ] != '.' ) {