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 */
118 struct timeval nettimeout = { -1 , 0 };
120 typedef int (*print_ctrl_fn)( LDAP *ld, LDAPControl *ctrl );
122 static int print_preread( LDAP *ld, LDAPControl *ctrl );
123 static int print_postread( LDAP *ld, LDAPControl *ctrl );
124 static int print_paged_results( LDAP *ld, LDAPControl *ctrl );
125 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
126 static int print_ppolicy( LDAP *ld, LDAPControl *ctrl );
129 static struct tool_ctrls_t {
133 } tool_ctrl_response[] = {
134 { LDAP_CONTROL_PRE_READ, TOOL_ALL, print_preread },
135 { LDAP_CONTROL_POST_READ, TOOL_ALL, print_postread },
136 { LDAP_CONTROL_PAGEDRESULTS, TOOL_SEARCH, print_paged_results },
137 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
138 { LDAP_CONTROL_PASSWORDPOLICYRESPONSE, TOOL_ALL, print_ppolicy },
154 tool_init( tool_type_t type )
157 ldap_pvt_setlocale(LC_MESSAGES, "");
158 ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
159 ldap_pvt_textdomain(OPENLDAP_PACKAGE);
165 #ifdef HAVE_CYRUS_SASL
169 ldap_pvt_tls_destroy();
174 tool_common_usage( void )
176 static const char *const descriptions[] = {
177 N_(" -c continuous operation mode (do not stop on errors)\n"),
178 N_(" -C chase referrals (anonymously)\n"),
179 N_(" -d level set LDAP debugging level to `level'\n"),
180 N_(" -D binddn bind DN\n"),
181 N_(" -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
182 N_(" [!]assert=<filter> (a RFC 4515 Filter string)\n")
183 N_(" [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n")
184 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
186 /* non-advertized support for proxyDN */
187 N_(" [!]proxydn=<dn> (a RFC 4514 DN string)\n")
190 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
191 N_(" [!]chaining[=<resolveBehavior>[/<continuationBehavior>]]\n")
192 N_(" one of \"chainingPreferred\", \"chainingRequired\",\n")
193 N_(" \"referralsPreferred\", \"referralsRequired\"\n")
194 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
195 N_(" [!]manageDSAit\n")
197 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
200 N_(" [!]postread[=<attrs>] (a comma-separated attribute list)\n")
201 N_(" [!]preread[=<attrs>] (a comma-separated attribute list)\n")
205 N_(" abandon, cancel, ignore (SIGINT sends abandon/cancel,\n"
206 " or ignores response; if critical, doesn't wait for SIGINT.\n"
207 " not really controls)\n")
208 N_(" -f file read operations from `file'\n"),
209 N_(" -h host LDAP server\n"),
210 N_(" -H URI LDAP Uniform Resource Identifier(s)\n"),
211 N_(" -I use SASL Interactive mode\n"),
212 N_(" -M enable Manage DSA IT control (-MM to make critical)\n"),
213 N_(" -n show what would be done but don't actually do it\n"),
214 N_(" -O props SASL security properties\n"),
215 N_(" -o <opt>[=<optparam] general options\n"),
216 N_(" nettimeout=<timeout> (in seconds, or \"none\" or \"max\")\n"),
217 N_(" -p port port on LDAP server\n"),
218 N_(" -P version protocol version (default: 3)\n"),
219 N_(" -Q use SASL Quiet mode\n"),
220 N_(" -R realm SASL realm\n"),
221 N_(" -U authcid SASL authentication identity\n"),
222 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
223 N_(" -V print version info (-VV only)\n"),
224 N_(" -w passwd bind password (for simple authentication)\n"),
225 N_(" -W prompt for bind password\n"),
226 N_(" -x Simple authentication\n"),
227 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
228 N_(" -y file Read password from file\n"),
229 N_(" -Y mech SASL mechanism\n"),
230 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
233 const char *const *cpp;
235 fputs( _("Common options:\n"), stderr );
236 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
237 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
238 fputs( _(*cpp), stderr );
251 fprintf( stderr, "%s: %s (%d)%s\n",
252 func, ldap_err2string( err ), err, extra ? extra : "" );
254 if ( matched && *matched ) {
255 fprintf( stderr, _("\tmatched DN: %s\n"), matched );
258 if ( info && *info ) {
259 fprintf( stderr, _("\tadditional info: %s\n"), info );
262 if ( refs && *refs ) {
264 fprintf( stderr, _("\treferrals:\n") );
265 for( i=0; refs[i]; i++ ) {
266 fprintf( stderr, "\t\t%s\n", refs[i] );
273 tool_args( int argc, char **argv )
277 while (( i = getopt( argc, argv, options )) != EOF ) {
279 char *control, *cvalue, *next;
281 case 'c': /* continuous operation mode */
288 ival = strtol( optarg, &next, 10 );
289 if (next == NULL || next[0] != '\0') {
290 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
295 case 'D': /* bind DN */
296 if( binddn != NULL ) {
297 fprintf( stderr, "%s: -D previously specified\n", prog );
298 exit( EXIT_FAILURE );
300 binddn = ber_strdup( optarg );
302 case 'e': /* general extensions (controls and such) */
303 /* should be extended to support comma separated list of
304 * [!]key[=value] parameters, e.g. -e !foo,bar=567
309 if( optarg[0] == '!' ) {
314 control = ber_strdup( optarg );
315 if ( (cvalue = strchr( control, '=' )) != NULL ) {
319 if ( strcasecmp( control, "assert" ) == 0 ) {
321 fprintf( stderr, "assert control previously specified\n");
322 exit( EXIT_FAILURE );
324 if( cvalue == NULL ) {
325 fprintf( stderr, "assert: control value expected\n" );
329 assertctl = 1 + crit;
331 assert( assertion == NULL );
334 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
335 if( authzid != NULL ) {
336 fprintf( stderr, "authzid control previously specified\n");
337 exit( EXIT_FAILURE );
339 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
340 if( proxydn != NULL ) {
341 fprintf( stderr, "authzid control incompatible with proxydn\n");
342 exit( EXIT_FAILURE );
344 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
345 if( cvalue == NULL ) {
346 fprintf( stderr, "authzid: control value expected\n" );
350 fprintf( stderr, "authzid: must be marked critical\n" );
354 assert( authzid == NULL );
357 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
358 } else if ( strcasecmp( control, "proxydn" ) == 0 ) {
359 if( proxydn != NULL ) {
360 fprintf( stderr, "proxydn control previously specified\n");
361 exit( EXIT_FAILURE );
363 if( authzid != NULL ) {
364 fprintf( stderr, "proxydn control incompatible with authzid\n");
365 exit( EXIT_FAILURE );
367 if( cvalue == NULL ) {
368 fprintf( stderr, "proxydn: control value expected\n" );
372 fprintf( stderr, "proxydn: must be marked critical\n" );
376 assert( proxydn == NULL );
378 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
380 } else if ( ( strcasecmp( control, "relax" ) == 0 ) ||
381 ( strcasecmp( control, "manageDIT" ) == 0 ) )
385 "relax control previously specified\n");
386 exit( EXIT_FAILURE );
388 if( cvalue != NULL ) {
390 "relax: no control value expected\n" );
394 manageDIT = 1 + crit;
396 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
399 "manageDSAit control previously specified\n");
400 exit( EXIT_FAILURE );
402 if( cvalue != NULL ) {
404 "manageDSAit: no control value expected\n" );
408 manageDSAit = 1 + crit;
410 } else if ( strcasecmp( control, "noop" ) == 0 ) {
412 fprintf( stderr, "noop control previously specified\n");
413 exit( EXIT_FAILURE );
415 if( cvalue != NULL ) {
416 fprintf( stderr, "noop: no control value expected\n" );
422 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
423 } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
425 fprintf( stderr, "ppolicy control previously specified\n");
426 exit( EXIT_FAILURE );
428 if( cvalue != NULL ) {
429 fprintf( stderr, "ppolicy: no control value expected\n" );
433 fprintf( stderr, "ppolicy: critical flag not allowed\n" );
440 } else if ( strcasecmp( control, "preread" ) == 0 ) {
442 fprintf( stderr, "preread control previously specified\n");
443 exit( EXIT_FAILURE );
447 preread_attrs = cvalue;
449 } else if ( strcasecmp( control, "postread" ) == 0 ) {
451 fprintf( stderr, "postread control previously specified\n");
452 exit( EXIT_FAILURE );
456 postread_attrs = cvalue;
458 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
459 } else if ( strcasecmp( control, "chaining" ) == 0 ) {
462 if ( cvalue != NULL ) {
465 continuation = strchr( cvalue, '/' );
466 if ( continuation ) {
467 /* FIXME: this makes sense only in searches */
468 *continuation++ = '\0';
469 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
470 chainingContinuation = LDAP_CHAINING_PREFERRED;
471 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
472 chainingContinuation = LDAP_CHAINING_REQUIRED;
473 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
474 chainingContinuation = LDAP_REFERRALS_PREFERRED;
475 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
476 chainingContinuation = LDAP_REFERRALS_REQUIRED;
479 "chaining behavior control "
480 "continuation value \"%s\" invalid\n",
482 exit( EXIT_FAILURE );
486 if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
487 chainingResolve = LDAP_CHAINING_PREFERRED;
488 } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
489 chainingResolve = LDAP_CHAINING_REQUIRED;
490 } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
491 chainingResolve = LDAP_REFERRALS_PREFERRED;
492 } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
493 chainingResolve = LDAP_REFERRALS_REQUIRED;
496 "chaining behavior control "
497 "resolve value \"%s\" invalid\n",
499 exit( EXIT_FAILURE );
502 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
504 /* this shouldn't go here, really; but it's a feature... */
505 } else if ( strcasecmp( control, "abandon" ) == 0 ) {
506 abcan = LDAP_REQ_ABANDON;
511 } else if ( strcasecmp( control, "cancel" ) == 0 ) {
512 abcan = LDAP_REQ_EXTENDED;
517 } else if ( strcasecmp( control, "ignore" ) == 0 ) {
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 );
567 /* enable Manage DSA IT */
570 case 'n': /* print operations, don't actually do them */
574 control = ber_strdup( optarg );
575 if ( (cvalue = strchr( control, '=' )) != NULL ) {
579 if ( strcasecmp( control, "nettimeout" ) == 0 ) {
580 if( nettimeout.tv_sec != -1 ) {
581 fprintf( stderr, "nettimeout option previously specified\n");
582 exit( EXIT_FAILURE );
584 if( cvalue == NULL || cvalue[0] == '\0' ) {
585 fprintf( stderr, "nettimeout: option value expected\n" );
588 if ( strcasecmp( cvalue, "none" ) == 0 ) {
589 nettimeout.tv_sec = 0;
590 } else if ( strcasecmp( cvalue, "max" ) == 0 ) {
591 nettimeout.tv_sec = LDAP_MAXINT;
593 ival = strtol( cvalue, &next, 10 );
594 if ( next == NULL || next[0] != '\0' ) {
596 _("Unable to parse network timeout \"%s\"\n"), cvalue );
597 exit( EXIT_FAILURE );
599 nettimeout.tv_sec = ival;
601 if( nettimeout.tv_sec < 0 || nettimeout.tv_sec > LDAP_MAXINT ) {
602 fprintf( stderr, _("%s: invalid network timeout (%ld) specified\n"),
603 prog, (long)nettimeout.tv_sec );
604 exit( EXIT_FAILURE );
607 fprintf( stderr, "Invalid general option name: %s\n",
613 #ifdef HAVE_CYRUS_SASL
614 if( sasl_secprops != NULL ) {
615 fprintf( stderr, "%s: -O previously specified\n", prog );
616 exit( EXIT_FAILURE );
618 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
619 fprintf( stderr, "%s: incompatible previous "
620 "authentication choice\n", prog );
621 exit( EXIT_FAILURE );
623 authmethod = LDAP_AUTH_SASL;
624 sasl_secprops = ber_strdup( optarg );
626 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
627 exit( EXIT_FAILURE );
632 fprintf( stderr, "%s: -p previously specified\n", prog );
633 exit( EXIT_FAILURE );
635 ival = strtol( optarg, &next, 10 );
636 if ( next == NULL || next[0] != '\0' ) {
637 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
638 exit( EXIT_FAILURE );
643 ival = strtol( optarg, &next, 10 );
644 if ( next == NULL || next[0] != '\0' ) {
645 fprintf( stderr, "%s: unabel to parse protocol version \"%s\"\n", prog, optarg );
646 exit( EXIT_FAILURE );
650 if( protocol == LDAP_VERSION3 ) {
651 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
653 exit( EXIT_FAILURE );
655 protocol = LDAP_VERSION2;
658 if( protocol == LDAP_VERSION2 ) {
659 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
661 exit( EXIT_FAILURE );
663 protocol = LDAP_VERSION3;
666 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
672 #ifdef HAVE_CYRUS_SASL
673 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
674 fprintf( stderr, "%s: incompatible previous "
675 "authentication choice\n",
677 exit( EXIT_FAILURE );
679 authmethod = LDAP_AUTH_SASL;
680 sasl_flags = LDAP_SASL_QUIET;
683 fprintf( stderr, "%s: not compiled with SASL support\n",
685 exit( EXIT_FAILURE );
688 #ifdef HAVE_CYRUS_SASL
689 if( sasl_realm != NULL ) {
690 fprintf( stderr, "%s: -R previously specified\n", prog );
691 exit( EXIT_FAILURE );
693 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
694 fprintf( stderr, "%s: incompatible previous "
695 "authentication choice\n",
697 exit( EXIT_FAILURE );
699 authmethod = LDAP_AUTH_SASL;
700 sasl_realm = ber_strdup( optarg );
702 fprintf( stderr, "%s: not compiled with SASL support\n",
704 exit( EXIT_FAILURE );
708 #ifdef HAVE_CYRUS_SASL
709 if( sasl_authc_id != NULL ) {
710 fprintf( stderr, "%s: -U previously specified\n", prog );
711 exit( EXIT_FAILURE );
713 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
714 fprintf( stderr, "%s: incompatible previous "
715 "authentication choice\n",
717 exit( EXIT_FAILURE );
719 authmethod = LDAP_AUTH_SASL;
720 sasl_authc_id = ber_strdup( optarg );
722 fprintf( stderr, "%s: not compiled with SASL support\n",
724 exit( EXIT_FAILURE );
727 case 'v': /* verbose mode */
730 case 'V': /* version */
733 case 'w': /* password */
734 passwd.bv_val = ber_strdup( optarg );
738 for( p = optarg; *p != '\0'; p++ ) {
742 passwd.bv_len = strlen( passwd.bv_val );
751 #ifdef HAVE_CYRUS_SASL
752 if( sasl_mech != NULL ) {
753 fprintf( stderr, "%s: -Y previously specified\n", prog );
754 exit( EXIT_FAILURE );
756 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
758 "%s: incompatible with authentication choice\n", prog );
759 exit( EXIT_FAILURE );
761 authmethod = LDAP_AUTH_SASL;
762 sasl_mech = ber_strdup( optarg );
764 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
765 exit( EXIT_FAILURE );
769 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
770 fprintf( stderr, "%s: incompatible with previous "
771 "authentication choice\n", prog );
772 exit( EXIT_FAILURE );
774 authmethod = LDAP_AUTH_SIMPLE;
777 #ifdef HAVE_CYRUS_SASL
778 if( sasl_authz_id != NULL ) {
779 fprintf( stderr, "%s: -X previously specified\n", prog );
780 exit( EXIT_FAILURE );
782 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
783 fprintf( stderr, "%s: -X incompatible with "
784 "authentication choice\n", prog );
785 exit( EXIT_FAILURE );
787 authmethod = LDAP_AUTH_SASL;
788 sasl_authz_id = ber_strdup( optarg );
790 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
791 exit( EXIT_FAILURE );
798 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
799 exit( EXIT_FAILURE );
803 if( handle_private_option( i ) ) break;
804 fprintf( stderr, "%s: unrecognized option -%c\n",
811 /* prevent bad linking */
813 api.ldapai_info_version = LDAP_API_INFO_VERSION;
815 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
816 != LDAP_OPT_SUCCESS )
818 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
819 exit( EXIT_FAILURE );
822 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
823 fprintf( stderr, "LDAP APIInfo version mismatch: "
824 "library %d, header %d\n",
825 api.ldapai_info_version, LDAP_API_INFO_VERSION );
826 exit( EXIT_FAILURE );
829 if( api.ldapai_api_version != LDAP_API_VERSION ) {
830 fprintf( stderr, "LDAP API version mismatch: "
831 "library %d, header %d\n",
832 api.ldapai_api_version, LDAP_API_VERSION );
833 exit( EXIT_FAILURE );
836 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
837 fprintf( stderr, "LDAP vendor name mismatch: "
838 "library %s, header %s\n",
839 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
840 exit( EXIT_FAILURE );
843 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
844 fprintf( stderr, "LDAP vendor version mismatch: "
845 "library %d, header %d\n",
846 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
847 exit( EXIT_FAILURE );
851 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
853 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
854 if (version > 1) exit( EXIT_SUCCESS );
857 ldap_memfree( api.ldapai_vendor_name );
858 ber_memvfree( (void **)api.ldapai_extensions );
862 protocol = LDAP_VERSION3;
864 if (authmethod == -1 && protocol > LDAP_VERSION2) {
865 #ifdef HAVE_CYRUS_SASL
866 authmethod = LDAP_AUTH_SASL;
868 authmethod = LDAP_AUTH_SIMPLE;
872 if( ldapuri == NULL ) {
873 if( ldapport && ( ldaphost == NULL )) {
874 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
875 exit( EXIT_FAILURE );
878 if( ldaphost != NULL ) {
879 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
880 exit( EXIT_FAILURE );
883 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
884 exit( EXIT_FAILURE );
888 if( protocol == LDAP_VERSION2 ) {
889 if( assertctl || authzid || manageDIT || manageDSAit ||
890 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
892 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
893 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
896 noop || ppolicy || preread || postread )
898 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
899 exit( EXIT_FAILURE );
903 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
904 exit( EXIT_FAILURE );
907 #ifdef HAVE_CYRUS_SASL
908 if( authmethod == LDAP_AUTH_SASL ) {
909 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
911 exit( EXIT_FAILURE );
919 tool_conn_setup( int dont, void (*private_setup)( LDAP * ) )
924 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
925 != LBER_OPT_SUCCESS )
928 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
930 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
931 != LDAP_OPT_SUCCESS )
934 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
939 (void) SIGNAL( SIGPIPE, SIG_IGN );
943 SIGNAL( SIGINT, do_sig );
949 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
952 memset( &url, 0, sizeof(url));
954 url.lud_scheme = "ldap";
955 url.lud_host = ldaphost;
956 url.lud_port = ldapport;
957 url.lud_scope = LDAP_SCOPE_DEFAULT;
959 ldapuri = ldap_url_desc2str( &url );
961 } else if ( ldapuri != NULL ) {
962 LDAPURLDesc *ludlist, **ludp;
966 rc = ldap_url_parselist( &ludlist, ldapuri );
967 if ( rc != LDAP_URL_SUCCESS ) {
969 "Could not parse LDAP URI(s)=%s (%d)\n",
971 exit( EXIT_FAILURE );
974 for ( ludp = &ludlist; *ludp != NULL; ) {
975 LDAPURLDesc *lud = *ludp;
978 if ( lud->lud_dn != NULL && lud->lud_dn[ 0 ] != '\0' &&
979 ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) )
981 /* if no host but a DN is provided,
982 * use DNS SRV to gather the host list
983 * and turn it into a list of URIs
984 * using the scheme provided */
989 len_proto = strlen( lud->lud_scheme );
991 if ( ldap_dn2domain( lud->lud_dn, &domain )
995 "DNS SRV: Could not turn "
996 "DN=\"%s\" into a domain\n",
1001 rc = ldap_domain2hostlist( domain, &hostlist );
1004 "DNS SRV: Could not turn "
1005 "domain=%s into a hostlist\n",
1010 hosts = ldap_str2charray( hostlist, " " );
1011 if ( hosts == NULL ) {
1013 "DNS SRV: Could not parse "
1014 "hostlist=\"%s\"\n",
1019 for ( i = 0; hosts[ i ] != NULL; i++ )
1022 tmp = (char **)realloc( urls, sizeof( char * ) * ( nurls + i + 1 ) );
1023 if ( tmp == NULL ) {
1025 "DNS SRV: out of memory?\n" );
1029 urls[ nurls ] = NULL;
1031 for ( i = 0; hosts[ i ] != NULL; i++ ) {
1032 size_t len = len_proto
1034 + strlen( hosts[ i ] )
1037 urls[ nurls + i + 1 ] = NULL;
1038 urls[ nurls + i ] = (char *)malloc( sizeof( char ) * len );
1039 if ( urls[ nurls + i ] == NULL ) {
1041 "DNS SRV: out of memory?\n" );
1045 snprintf( urls[ nurls + i ], len, "%s://%s",
1046 lud->lud_scheme, hosts[ i ] );
1051 ber_memvfree( (void **)hosts );
1052 ber_memfree( hostlist );
1053 ber_memfree( domain );
1056 tmp = (char **)realloc( urls, sizeof( char * ) * ( nurls + 2 ) );
1057 if ( tmp == NULL ) {
1059 "DNS SRV: out of memory?\n" );
1063 urls[ nurls + 1 ] = NULL;
1065 urls[ nurls ] = ldap_url_desc2str( lud );
1066 if ( urls[ nurls ] == NULL ) {
1068 "DNS SRV: out of memory?\n" );
1074 *ludp = lud->lud_next;
1076 lud->lud_next = NULL;
1077 ldap_free_urldesc( lud );
1080 if ( ludlist != NULL ) {
1081 ldap_free_urllist( ludlist );
1082 exit( EXIT_FAILURE );
1084 } else if ( urls == NULL ) {
1085 exit( EXIT_FAILURE );
1088 ldap_memfree( ldapuri );
1089 ldapuri = ldap_charray2str( urls, " " );
1090 ber_memvfree( (void **)urls );
1094 fprintf( stderr, "ldap_initialize( %s )\n",
1095 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
1097 rc = ldap_initialize( &ld, ldapuri );
1098 if( rc != LDAP_SUCCESS ) {
1100 "Could not create LDAP session handle for URI=%s (%d): %s\n",
1101 ldapuri, rc, ldap_err2string(rc) );
1102 exit( EXIT_FAILURE );
1105 if( private_setup ) private_setup( ld );
1108 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
1109 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
1111 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
1112 referrals ? "on" : "off" );
1113 exit( EXIT_FAILURE );
1116 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
1117 != LDAP_OPT_SUCCESS )
1119 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
1121 exit( EXIT_FAILURE );
1125 rc = ldap_start_tls_s( ld, NULL, NULL );
1126 if ( rc != LDAP_SUCCESS ) {
1127 tool_perror( "ldap_start_tls", rc, NULL, NULL, NULL, NULL );
1128 if ( use_tls > 1 ) {
1129 exit( EXIT_FAILURE );
1134 if ( nettimeout.tv_sec > 0 ) {
1135 if ( ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &nettimeout )
1136 != LDAP_OPT_SUCCESS )
1138 fprintf( stderr, "Could not set LDAP_OPT_NETWORK_TIMEOUT %ld\n",
1139 (long)nettimeout.tv_sec );
1140 exit( EXIT_FAILURE );
1150 tool_bind( LDAP *ld )
1152 LDAPControl **sctrlsp = NULL;
1153 LDAPControl *sctrls[2];
1156 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1159 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1160 c.ldctl_value.bv_val = NULL;
1161 c.ldctl_value.bv_len = 0;
1162 c.ldctl_iscritical = 0;
1163 sctrls[nsctrls] = &c;
1164 sctrls[++nsctrls] = NULL;
1172 assert( nsctrls < sizeof(sctrls)/sizeof(sctrls[0]) );
1174 if ( authmethod == LDAP_AUTH_SASL ) {
1175 #ifdef HAVE_CYRUS_SASL
1179 if( sasl_secprops != NULL ) {
1180 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
1181 (void *) sasl_secprops );
1183 if( rc != LDAP_OPT_SUCCESS ) {
1185 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
1187 exit( LDAP_LOCAL_ERROR );
1191 defaults = lutil_sasl_defaults( ld,
1198 rc = ldap_sasl_interactive_bind_s( ld, binddn, sasl_mech,
1200 NULL, sasl_flags, lutil_sasl_interact, defaults );
1202 lutil_sasl_freedefs( defaults );
1203 if( rc != LDAP_SUCCESS ) {
1204 tool_perror( "ldap_sasl_interactive_bind_s",
1205 rc, NULL, NULL, NULL, NULL );
1209 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1210 exit( LDAP_NOT_SUPPORTED );
1214 LDAPMessage *result;
1215 LDAPControl **ctrls;
1217 char *matched = NULL;
1225 rc = ldap_sasl_bind( ld, binddn, LDAP_SASL_SIMPLE, &passwd,
1226 sctrlsp, NULL, &msgid );
1227 if ( msgid == -1 ) {
1228 tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
1229 NULL, NULL, NULL, NULL );
1234 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 ) {
1235 tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
1236 exit( LDAP_LOCAL_ERROR );
1239 rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
1241 if ( rc != LDAP_SUCCESS ) {
1242 tool_perror( "ldap_bind parse result", rc, NULL, NULL, NULL, NULL );
1243 exit( LDAP_LOCAL_ERROR );
1246 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1247 if ( ctrls && ppolicy ) {
1249 int expire, grace, len = 0;
1250 LDAPPasswordPolicyError pErr = -1;
1252 ctrl = ldap_find_control( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
1255 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
1256 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1258 if ( pErr != PP_noError ){
1261 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1262 len = strlen( msgbuf );
1264 if ( expire >= 0 ) {
1265 sprintf( msgbuf+len,
1266 " (Password expires in %d seconds)",
1268 } else if ( grace >= 0 ) {
1269 sprintf( msgbuf+len,
1270 " (Password expired, %d grace logins remain)",
1278 ldap_controls_free( ctrls );
1281 if ( err != LDAP_SUCCESS
1283 || ( matched && matched[ 0 ] )
1284 || ( info && info[ 0 ] )
1287 tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1289 if( matched ) ber_memfree( matched );
1290 if( info ) ber_memfree( info );
1291 if( refs ) ber_memvfree( (void **)refs );
1293 if ( err != LDAP_SUCCESS ) exit( err );
1299 tool_unbind( LDAP *ld )
1301 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1303 if ( err != LDAP_OPT_SUCCESS ) {
1304 fprintf( stderr, "Could not unset controls\n");
1307 (void) ldap_unbind_ext( ld, NULL, NULL );
1311 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1313 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1315 int i = 0, j, crit = 0, err;
1316 LDAPControl c[12], **ctrls;
1320 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1322 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1326 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1331 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1333 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1339 ctrls = (LDAPControl**) malloc(sizeof(c) + (count+1)*sizeof(LDAPControl*));
1340 if ( ctrls == NULL ) {
1341 fprintf( stderr, "No memory\n" );
1342 exit( EXIT_FAILURE );
1346 BerElementBuffer berbuf;
1347 BerElement *ber = (BerElement *)&berbuf;
1349 if( assertion == NULL || *assertion == '\0' ) {
1350 fprintf( stderr, "Assertion=<empty>\n" );
1351 exit( EXIT_FAILURE );
1354 ber_init2( ber, NULL, LBER_USE_DER );
1356 err = ldap_pvt_put_filter( ber, assertion );
1358 fprintf( stderr, "assertion encode failed (%d)\n", err );
1359 exit( EXIT_FAILURE );
1362 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1364 fprintf( stderr, "assertion flatten failed (%d)\n", err );
1365 exit( EXIT_FAILURE );
1368 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1369 c[i].ldctl_iscritical = assertctl > 1;
1375 c[i].ldctl_value.bv_val = authzid;
1376 c[i].ldctl_value.bv_len = strlen( authzid );
1377 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1378 c[i].ldctl_iscritical = 1;
1383 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1384 /* NOTE: doesn't need an extra count because it's incompatible
1387 BerElementBuffer berbuf;
1388 BerElement *ber = (BerElement *)&berbuf;
1390 ber_init2( ber, NULL, LBER_USE_DER );
1392 if ( ber_printf( ber, "s", proxydn ) == LBER_ERROR ) {
1393 exit( EXIT_FAILURE );
1396 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1397 exit( EXIT_FAILURE );
1400 c[i].ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1401 c[i].ldctl_iscritical = 1;
1405 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1408 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1409 BER_BVZERO( &c[i].ldctl_value );
1410 c[i].ldctl_iscritical = manageDIT > 1;
1415 if ( manageDSAit ) {
1416 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1417 BER_BVZERO( &c[i].ldctl_value );
1418 c[i].ldctl_iscritical = manageDSAit > 1;
1424 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1425 BER_BVZERO( &c[i].ldctl_value );
1426 c[i].ldctl_iscritical = noop > 1;
1431 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1433 c[i].ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1434 BER_BVZERO( &c[i].ldctl_value );
1435 c[i].ldctl_iscritical = 0;
1442 char berbuf[LBER_ELEMENT_SIZEOF];
1443 BerElement *ber = (BerElement *)berbuf;
1444 char **attrs = NULL;
1446 if( preread_attrs ) {
1447 attrs = ldap_str2charray( preread_attrs, "," );
1450 ber_init2( ber, NULL, LBER_USE_DER );
1452 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1453 fprintf( stderr, "preread attrs encode failed.\n" );
1454 exit( EXIT_FAILURE );
1457 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1459 fprintf( stderr, "preread flatten failed (%d)\n", err );
1460 exit( EXIT_FAILURE );
1463 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1464 c[i].ldctl_iscritical = preread > 1;
1468 if( attrs ) ldap_charray_free( attrs );
1472 char berbuf[LBER_ELEMENT_SIZEOF];
1473 BerElement *ber = (BerElement *)berbuf;
1474 char **attrs = NULL;
1476 if( postread_attrs ) {
1477 attrs = ldap_str2charray( postread_attrs, "," );
1480 ber_init2( ber, NULL, LBER_USE_DER );
1482 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1483 fprintf( stderr, "postread attrs encode failed.\n" );
1484 exit( EXIT_FAILURE );
1487 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1489 fprintf( stderr, "postread flatten failed (%d)\n", err );
1490 exit( EXIT_FAILURE );
1493 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1494 c[i].ldctl_iscritical = postread > 1;
1498 if( attrs ) ldap_charray_free( attrs );
1501 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1503 if ( chainingResolve > -1 ) {
1504 BerElementBuffer berbuf;
1505 BerElement *ber = (BerElement *)&berbuf;
1507 ber_init2( ber, NULL, LBER_USE_DER );
1509 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1512 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1513 exit( EXIT_FAILURE );
1516 if ( chainingContinuation > -1 ) {
1517 err = ber_printf( ber, "e", chainingContinuation );
1520 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1521 exit( EXIT_FAILURE );
1525 err = ber_printf( ber, /* { */ "N}" );
1528 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1529 exit( EXIT_FAILURE );
1532 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1533 exit( EXIT_FAILURE );
1537 BER_BVZERO( &c[i].ldctl_value );
1540 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1541 c[i].ldctl_iscritical = chaining > 1;
1545 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1548 ctrls[i++] = extra_c++;
1552 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1554 if ( err != LDAP_OPT_SUCCESS ) {
1555 for ( j = 0; j < i; j++ ) {
1556 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1558 fprintf( stderr, "Could not set %scontrols\n",
1559 crit ? "critical " : "" );
1564 exit( EXIT_FAILURE );
1569 tool_check_abandon( LDAP *ld, int msgid )
1573 switch ( gotintr ) {
1574 case LDAP_REQ_EXTENDED:
1575 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1576 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1577 rc, ldap_err2string( rc ) );
1580 case LDAP_REQ_ABANDON:
1581 rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
1582 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1583 rc, ldap_err2string( rc ) );
1587 /* just unbind, ignoring the request */
1595 print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
1600 tool_write_ldif( LDIF_PUT_COMMENT, "==> ",
1601 what->bv_val, what->bv_len );
1602 ber = ber_init( &ctrl->ldctl_value );
1603 if ( ber == NULL ) {
1607 } else if ( ber_scanf( ber, "{m{" /*}}*/, &bv ) == LBER_ERROR ) {
1612 tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1614 while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
1616 BerVarray vals = NULL;
1618 if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
1625 for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
1627 ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1628 bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
1631 ber_bvarray_free( vals );
1635 if ( ber != NULL ) {
1639 tool_write_ldif( LDIF_PUT_COMMENT, "<== ",
1640 what->bv_val, what->bv_len );
1646 print_preread( LDAP *ld, LDAPControl *ctrl )
1648 static struct berval what = BER_BVC( "preread" );
1650 return print_prepostread( ld, ctrl, &what );
1654 print_postread( LDAP *ld, LDAPControl *ctrl )
1656 static struct berval what = BER_BVC( "postread" );
1658 return print_prepostread( ld, ctrl, &what );
1662 print_paged_results( LDAP *ld, LDAPControl *ctrl )
1666 /* note: pr_cookie is being malloced; it's freed
1667 * the next time the control is sent, but the last
1668 * time it's not; we don't care too much, because
1669 * the last time an empty value is returned... */
1670 if ( ldap_parse_pageresponse_control( ld, ctrl, &estimate, &pr_cookie )
1677 /* FIXME: check buffer overflow */
1678 char buf[ BUFSIZ ], *ptr = buf;
1680 if ( estimate > 0 ) {
1681 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1682 "estimate=%d", estimate );
1685 if ( pr_cookie.bv_len > 0 ) {
1688 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
1689 pr_cookie.bv_len ) + 1;
1690 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
1692 bv.bv_len = lutil_b64_ntop(
1693 (unsigned char *) pr_cookie.bv_val,
1695 bv.bv_val, bv.bv_len );
1697 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1698 "%scookie=%s", ptr == buf ? "" : " ",
1701 ber_memfree( bv.bv_val );
1703 pr_morePagedResults = 1;
1706 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1707 "%scookie=", ptr == buf ? "" : " " );
1710 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1711 "pagedresults", buf, ptr - buf );
1717 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1719 print_ppolicy( LDAP *ld, LDAPControl *ctrl )
1721 int expire = 0, grace = 0, rc;
1722 LDAPPasswordPolicyError pperr;
1724 rc = ldap_parse_passwordpolicy_control( ld, ctrl,
1725 &expire, &grace, &pperr );
1726 if ( rc == LDAP_SUCCESS ) {
1727 char buf[ BUFSIZ ], *ptr = buf;
1729 if ( expire != -1 ) {
1730 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1731 "expire=%d", expire );
1734 if ( grace != -1 ) {
1735 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1736 "%sgrace=%d", ptr == buf ? "" : " ", grace );
1739 if ( pperr != PP_noError ) {
1740 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1741 "%serror=%d (%s)", ptr == buf ? "" : " ",
1743 ldap_passwordpolicy_err2txt( pperr ) );
1746 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1747 "ppolicy", buf, ptr - buf );
1754 void tool_print_ctrls(
1756 LDAPControl **ctrls )
1761 for ( i = 0; ctrls[i] != NULL; i++ ) {
1762 /* control: OID criticality base64value */
1763 struct berval b64 = BER_BVNULL;
1769 len += strlen( ctrls[i]->ldctl_oid );
1771 /* add enough for space after OID and the critical value itself */
1772 len += ctrls[i]->ldctl_iscritical
1773 ? sizeof("true") : sizeof("false");
1775 /* convert to base64 */
1776 if ( ctrls[i]->ldctl_value.bv_len ) {
1777 b64.bv_len = LUTIL_BASE64_ENCODE_LEN(
1778 ctrls[i]->ldctl_value.bv_len ) + 1;
1779 b64.bv_val = ber_memalloc( b64.bv_len + 1 );
1781 b64.bv_len = lutil_b64_ntop(
1782 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
1783 ctrls[i]->ldctl_value.bv_len,
1784 b64.bv_val, b64.bv_len );
1788 len += 1 + b64.bv_len;
1791 ptr = str = malloc( len + 1 );
1793 ptr = lutil_strcopy( ptr, ": " );
1795 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_oid );
1796 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_iscritical
1797 ? " true" : " false" );
1800 ptr = lutil_strcopy( ptr, " " );
1801 ptr = lutil_strcopy( ptr, b64.bv_val );
1805 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1806 "control", str, len );
1811 ber_memfree( b64.bv_val );
1814 /* known controls */
1815 for ( j = 0; tool_ctrl_response[j].oid != NULL; j++ ) {
1816 if ( strcmp( tool_ctrl_response[j].oid, ctrls[i]->ldctl_oid ) == 0 ) {
1817 if ( !tool_ctrl_response[j].mask & tool_type ) {
1818 /* this control should not appear
1819 * with this tool; warning? */
1825 if ( tool_ctrl_response[j].oid != NULL && tool_ctrl_response[j].func ) {
1826 (void)tool_ctrl_response[j].func( ld, ctrls[i] );
1832 tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
1836 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1840 fputs( ldif, stdout );
1841 ber_memfree( ldif );
1847 tool_is_oid( const char *s )
1851 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
1855 for ( ; s[ 0 ]; s++ ) {
1856 if ( s[ 0 ] == '.' ) {
1857 if ( s[ 1 ] == '\0' ) {
1864 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
1868 if ( first == 1 && s[ 0 ] == '0' && s[ 1 ] != '.' ) {