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-2011 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>
36 #include <ac/socket.h>
38 #ifdef HAVE_CYRUS_SASL
39 #ifdef HAVE_SASL_SASL_H
40 #include <sasl/sasl.h>
50 #include "lutil_ldap.h"
51 #include "ldap_defaults.h"
57 /* input-related vars */
59 /* misc. parameters */
60 tool_type_t tool_type;
69 ber_len_t ldif_wrap = LDIF_LINE_WIDTH;
74 char *ldaphost = NULL;
84 struct berval passwd = { 0, NULL };
86 #ifdef HAVE_CYRUS_SASL
87 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
88 char *sasl_realm = NULL;
89 char *sasl_authc_id = NULL;
90 char *sasl_authz_id = NULL;
91 char *sasl_mech = NULL;
92 char *sasl_secprops = NULL;
97 char *assertion = NULL;
98 struct berval assertionvalue = BER_BVNULL;
100 /* support deprecated early version of proxyAuthz */
101 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ "2.16.840.1.113730.3.4.12"
102 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
103 char *proxydn = NULL;
104 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
110 static char *preread_attrs = NULL;
112 static char *postread_attrs = NULL;
113 ber_int_t pr_morePagedResults = 1;
114 struct berval pr_cookie = { 0, NULL };
115 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
117 static int chainingResolve = -1;
118 static int chainingContinuation = -1;
119 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
120 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
121 static int sessionTracking = 0;
122 struct berval stValue;
123 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
126 struct berval *vlvContext;
128 LDAPControl *unknown_ctrls = NULL;
129 int unknown_ctrls_num = 0;
132 struct timeval nettimeout = { -1 , 0 };
134 typedef int (*print_ctrl_fn)( LDAP *ld, LDAPControl *ctrl );
136 static int print_preread( LDAP *ld, LDAPControl *ctrl );
137 static int print_postread( LDAP *ld, LDAPControl *ctrl );
138 static int print_paged_results( LDAP *ld, LDAPControl *ctrl );
139 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
140 static int print_ppolicy( LDAP *ld, LDAPControl *ctrl );
142 static int print_sss( LDAP *ld, LDAPControl *ctrl );
143 static int print_vlv( LDAP *ld, LDAPControl *ctrl );
144 #ifdef LDAP_CONTROL_X_DEREF
145 static int print_deref( LDAP *ld, LDAPControl *ctrl );
147 #ifdef LDAP_CONTROL_X_WHATFAILED
148 static int print_whatfailed( LDAP *ld, LDAPControl *ctrl );
151 static struct tool_ctrls_t {
155 } tool_ctrl_response[] = {
156 { LDAP_CONTROL_PRE_READ, TOOL_ALL, print_preread },
157 { LDAP_CONTROL_POST_READ, TOOL_ALL, print_postread },
158 { LDAP_CONTROL_PAGEDRESULTS, TOOL_SEARCH, print_paged_results },
159 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
160 { LDAP_CONTROL_PASSWORDPOLICYRESPONSE, TOOL_ALL, print_ppolicy },
162 { LDAP_CONTROL_SORTRESPONSE, TOOL_SEARCH, print_sss },
163 { LDAP_CONTROL_VLVRESPONSE, TOOL_SEARCH, print_vlv },
164 #ifdef LDAP_CONTROL_X_DEREF
165 { LDAP_CONTROL_X_DEREF, TOOL_SEARCH, print_deref },
167 #ifdef LDAP_CONTROL_X_WHATFAILED
168 { LDAP_CONTROL_X_WHATFAILED, TOOL_ALL, print_whatfailed },
174 enum { Intr_None = 0, Intr_Abandon, Intr_Cancel, Intr_Ignore };
175 static volatile sig_atomic_t gotintr, abcan;
178 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
180 st_value( LDAP *ld, struct berval *value )
182 char *ip = NULL, *name = NULL;
183 struct berval id = { 0 };
184 char namebuf[ MAXHOSTNAMELEN ];
186 if ( gethostname( namebuf, sizeof( namebuf ) ) == 0 ) {
192 h = gethostbyname( name );
194 AC_MEMCPY( &addr, h->h_addr, sizeof( addr ) );
195 ip = inet_ntoa( addr );
199 #ifdef HAVE_CYRUS_SASL
200 if ( sasl_authz_id != NULL ) {
201 ber_str2bv( sasl_authz_id, 0, 0, &id );
203 } else if ( sasl_authc_id != NULL ) {
204 ber_str2bv( sasl_authc_id, 0, 0, &id );
207 #endif /* HAVE_CYRUS_SASL */
208 if ( binddn != NULL ) {
209 ber_str2bv( binddn, 0, 0, &id );
212 if ( ldap_create_session_tracking_value( ld,
213 ip, name, LDAP_CONTROL_X_SESSION_TRACKING_USERNAME,
216 fprintf( stderr, _("Session tracking control encoding error!\n") );
222 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
231 tool_init( tool_type_t type )
234 ldap_pvt_setlocale(LC_MESSAGES, "");
235 ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
236 ldap_pvt_textdomain(OPENLDAP_PACKAGE);
242 #ifdef HAVE_CYRUS_SASL
246 ldap_pvt_tls_destroy();
249 if ( ldapuri != NULL ) {
250 ber_memfree( ldapuri );
254 if ( pr_cookie.bv_val != NULL ) {
255 ber_memfree( pr_cookie.bv_val );
256 BER_BVZERO( &pr_cookie );
259 if ( binddn != NULL ) {
260 ber_memfree( binddn );
264 if ( passwd.bv_val != NULL ) {
265 ber_memfree( passwd.bv_val );
266 BER_BVZERO( &passwd );
269 if ( infile != NULL ) {
270 ber_memfree( infile );
275 ber_memfree( assertion );
280 ber_memfree( authzid );
285 ber_memfree( proxydn );
289 if ( preread_attrs ) {
290 ber_memfree( preread_attrs );
291 preread_attrs = NULL;
294 if ( postread_attrs ) {
295 ber_memfree( postread_attrs );
296 postread_attrs = NULL;
301 tool_common_usage( void )
303 static const char *const descriptions[] = {
304 N_(" -d level set LDAP debugging level to `level'\n"),
305 N_(" -D binddn bind DN\n"),
306 N_(" -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
307 N_(" [!]assert=<filter> (RFC 4528; a RFC 4515 Filter string)\n")
308 N_(" [!]authzid=<authzid> (RFC 4370; \"dn:<dn>\" or \"u:<user>\")\n")
309 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
311 /* non-advertized support for proxyDN */
312 N_(" [!]proxydn=<dn> (a RFC 4514 DN string)\n")
315 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
316 N_(" [!]chaining[=<resolveBehavior>[/<continuationBehavior>]]\n")
317 N_(" one of \"chainingPreferred\", \"chainingRequired\",\n")
318 N_(" \"referralsPreferred\", \"referralsRequired\"\n")
319 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
320 N_(" [!]manageDSAit (RFC 3296)\n")
322 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
325 N_(" [!]postread[=<attrs>] (RFC 4527; comma-separated attr list)\n")
326 N_(" [!]preread[=<attrs>] (RFC 4527; comma-separated attr list)\n")
328 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
329 N_(" [!]sessiontracking\n")
330 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
331 N_(" abandon, cancel, ignore (SIGINT sends abandon/cancel,\n"
332 " or ignores response; if critical, doesn't wait for SIGINT.\n"
333 " not really controls)\n")
334 N_(" -h host LDAP server\n"),
335 N_(" -H URI LDAP Uniform Resource Identifier(s)\n"),
336 N_(" -I use SASL Interactive mode\n"),
337 N_(" -n show what would be done but don't actually do it\n"),
338 N_(" -N do not use reverse DNS to canonicalize SASL host name\n"),
339 N_(" -O props SASL security properties\n"),
340 N_(" -o <opt>[=<optparam] general options\n"),
341 N_(" nettimeout=<timeout> (in seconds, or \"none\" or \"max\")\n"),
342 N_(" ldif-wrap=<width> (in columns, or \"no\" for no wrapping)\n"),
343 N_(" -p port port on LDAP server\n"),
344 N_(" -Q use SASL Quiet mode\n"),
345 N_(" -R realm SASL realm\n"),
346 N_(" -U authcid SASL authentication identity\n"),
347 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
348 N_(" -V print version info (-VV only)\n"),
349 N_(" -w passwd bind password (for simple authentication)\n"),
350 N_(" -W prompt for bind password\n"),
351 N_(" -x Simple authentication\n"),
352 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
353 N_(" -y file Read password from file\n"),
354 N_(" -Y mech SASL mechanism\n"),
355 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
358 const char *const *cpp;
360 fputs( _("Common options:\n"), stderr );
361 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
362 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
363 fputs( _(*cpp), stderr );
378 fprintf( stderr, "%s: %s (%d)%s\n",
379 func, ldap_err2string( err ), err, extra ? extra : "" );
381 if ( matched && *matched ) {
382 fprintf( stderr, _("\tmatched DN: %s\n"), matched );
385 if ( info && *info ) {
386 fprintf( stderr, _("\tadditional info: %s\n"), info );
389 if ( refs && *refs ) {
391 fprintf( stderr, _("\treferrals:\n") );
392 for( i=0; refs[i]; i++ ) {
393 fprintf( stderr, "\t\t%s\n", refs[i] );
400 tool_args( int argc, char **argv )
404 while (( i = getopt( argc, argv, options )) != EOF ) {
406 char *control, *cvalue, *next;
408 case 'c': /* continuous operation mode */
415 ival = strtol( optarg, &next, 10 );
416 if (next == NULL || next[0] != '\0') {
417 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
422 case 'D': /* bind DN */
423 if( binddn != NULL ) {
424 fprintf( stderr, "%s: -D previously specified\n", prog );
425 exit( EXIT_FAILURE );
427 binddn = ber_strdup( optarg );
429 case 'e': /* general extensions (controls and such) */
430 /* should be extended to support comma separated list of
431 * [!]key[=value] parameters, e.g. -e !foo,bar=567
436 if( optarg[0] == '!' ) {
441 control = ber_strdup( optarg );
442 if ( (cvalue = strchr( control, '=' )) != NULL ) {
446 if ( strcasecmp( control, "assert" ) == 0 ) {
448 fprintf( stderr, "assert control previously specified\n");
449 exit( EXIT_FAILURE );
451 if( cvalue == NULL ) {
452 fprintf( stderr, "assert: control value expected\n" );
456 assertctl = 1 + crit;
458 assert( assertion == NULL );
459 assertion = ber_strdup( cvalue );
461 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
462 if( authzid != NULL ) {
463 fprintf( stderr, "authzid control previously specified\n");
464 exit( EXIT_FAILURE );
466 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
467 if( proxydn != NULL ) {
468 fprintf( stderr, "authzid control incompatible with proxydn\n");
469 exit( EXIT_FAILURE );
471 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
472 if( cvalue == NULL ) {
473 fprintf( stderr, "authzid: control value expected\n" );
477 fprintf( stderr, "authzid: must be marked critical\n" );
481 assert( authzid == NULL );
482 authzid = ber_strdup( cvalue );
484 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
485 } else if ( strcasecmp( control, "proxydn" ) == 0 ) {
486 if( proxydn != NULL ) {
487 fprintf( stderr, "proxydn control previously specified\n");
488 exit( EXIT_FAILURE );
490 if( authzid != NULL ) {
491 fprintf( stderr, "proxydn control incompatible with authzid\n");
492 exit( EXIT_FAILURE );
494 if( cvalue == NULL ) {
495 fprintf( stderr, "proxydn: control value expected\n" );
499 fprintf( stderr, "proxydn: must be marked critical\n" );
503 assert( proxydn == NULL );
504 proxydn = ber_strdup( cvalue );
505 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
507 } else if ( ( strcasecmp( control, "relax" ) == 0 ) ||
508 ( strcasecmp( control, "manageDIT" ) == 0 ) )
512 "relax control previously specified\n");
513 exit( EXIT_FAILURE );
515 if( cvalue != NULL ) {
517 "relax: no control value expected\n" );
521 manageDIT = 1 + crit;
523 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
526 "manageDSAit control previously specified\n");
527 exit( EXIT_FAILURE );
529 if( cvalue != NULL ) {
531 "manageDSAit: no control value expected\n" );
535 manageDSAit = 1 + crit;
537 } else if ( strcasecmp( control, "noop" ) == 0 ) {
539 fprintf( stderr, "noop control previously specified\n");
540 exit( EXIT_FAILURE );
542 if( cvalue != NULL ) {
543 fprintf( stderr, "noop: no control value expected\n" );
549 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
550 } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
552 fprintf( stderr, "ppolicy control previously specified\n");
553 exit( EXIT_FAILURE );
555 if( cvalue != NULL ) {
556 fprintf( stderr, "ppolicy: no control value expected\n" );
560 fprintf( stderr, "ppolicy: critical flag not allowed\n" );
567 } else if ( strcasecmp( control, "preread" ) == 0 ) {
569 fprintf( stderr, "preread control previously specified\n");
570 exit( EXIT_FAILURE );
574 preread_attrs = ber_strdup( cvalue );
576 } else if ( strcasecmp( control, "postread" ) == 0 ) {
578 fprintf( stderr, "postread control previously specified\n");
579 exit( EXIT_FAILURE );
583 postread_attrs = ber_strdup( cvalue );
585 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
586 } else if ( strcasecmp( control, "chaining" ) == 0 ) {
589 if ( cvalue != NULL ) {
592 continuation = strchr( cvalue, '/' );
593 if ( continuation ) {
594 /* FIXME: this makes sense only in searches */
595 *continuation++ = '\0';
596 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
597 chainingContinuation = LDAP_CHAINING_PREFERRED;
598 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
599 chainingContinuation = LDAP_CHAINING_REQUIRED;
600 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
601 chainingContinuation = LDAP_REFERRALS_PREFERRED;
602 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
603 chainingContinuation = LDAP_REFERRALS_REQUIRED;
606 "chaining behavior control "
607 "continuation value \"%s\" invalid\n",
609 exit( EXIT_FAILURE );
613 if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
614 chainingResolve = LDAP_CHAINING_PREFERRED;
615 } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
616 chainingResolve = LDAP_CHAINING_REQUIRED;
617 } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
618 chainingResolve = LDAP_REFERRALS_PREFERRED;
619 } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
620 chainingResolve = LDAP_REFERRALS_REQUIRED;
623 "chaining behavior control "
624 "resolve value \"%s\" invalid\n",
626 exit( EXIT_FAILURE );
629 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
631 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
632 } else if ( strcasecmp( control, "sessiontracking" ) == 0 ) {
633 if ( sessionTracking ) {
634 fprintf( stderr, "%s: session tracking can be only specified once\n", prog );
635 exit( EXIT_FAILURE );
639 fprintf( stderr, "sessiontracking: critical flag not allowed\n" );
642 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
644 /* this shouldn't go here, really; but it's a feature... */
645 } else if ( strcasecmp( control, "abandon" ) == 0 ) {
646 abcan = Intr_Abandon;
651 } else if ( strcasecmp( control, "cancel" ) == 0 ) {
657 } else if ( strcasecmp( control, "ignore" ) == 0 ) {
663 } else if ( tool_is_oid( control ) ) {
664 LDAPControl *tmpctrls, ctrl;
666 tmpctrls = (LDAPControl *)ber_memrealloc( unknown_ctrls,
667 (unknown_ctrls_num + 1)*sizeof( LDAPControl ) );
668 if ( tmpctrls == NULL ) {
669 fprintf( stderr, "%s: no memory?\n", prog );
670 exit( EXIT_FAILURE );
672 unknown_ctrls = tmpctrls;
673 ctrl.ldctl_oid = control;
676 ctrl.ldctl_value.bv_val = NULL;
677 ctrl.ldctl_value.bv_len = 0;
678 ctrl.ldctl_iscritical = crit;
680 if ( cvalue != NULL ) {
682 size_t len = strlen( cvalue );
685 bv.bv_len = LUTIL_BASE64_DECODE_LEN( len );
686 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
688 retcode = lutil_b64_pton( cvalue,
689 (unsigned char *)bv.bv_val,
692 if ( retcode == -1 || (unsigned) retcode > bv.bv_len ) {
693 fprintf( stderr, "Unable to parse value of general control %s\n",
699 ctrl.ldctl_value = bv;
702 unknown_ctrls[ unknown_ctrls_num ] = ctrl;
706 fprintf( stderr, "Invalid general control name: %s\n",
711 ber_memfree( control );
715 case 'f': /* read from file */
716 if( infile != NULL ) {
717 fprintf( stderr, "%s: -f previously specified\n", prog );
718 exit( EXIT_FAILURE );
720 infile = ber_strdup( optarg );
722 case 'h': /* ldap host */
723 if( ldaphost != NULL ) {
724 fprintf( stderr, "%s: -h previously specified\n", prog );
725 exit( EXIT_FAILURE );
727 ldaphost = ber_strdup( optarg );
729 case 'H': /* ldap URI */
730 if( ldapuri != NULL ) {
731 fprintf( stderr, "%s: -H previously specified\n", prog );
732 exit( EXIT_FAILURE );
734 ldapuri = ber_strdup( optarg );
737 #ifdef HAVE_CYRUS_SASL
738 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
739 fprintf( stderr, "%s: incompatible previous "
740 "authentication choice\n",
742 exit( EXIT_FAILURE );
744 authmethod = LDAP_AUTH_SASL;
745 sasl_flags = LDAP_SASL_INTERACTIVE;
748 fprintf( stderr, "%s: was not compiled with SASL support\n",
750 exit( EXIT_FAILURE );
753 /* enable Manage DSA IT */
756 case 'n': /* print operations, don't actually do them */
763 control = ber_strdup( optarg );
764 if ( (cvalue = strchr( control, '=' )) != NULL ) {
768 if ( strcasecmp( control, "nettimeout" ) == 0 ) {
769 if( nettimeout.tv_sec != -1 ) {
770 fprintf( stderr, "nettimeout option previously specified\n");
771 exit( EXIT_FAILURE );
773 if( cvalue == NULL || cvalue[0] == '\0' ) {
774 fprintf( stderr, "nettimeout: option value expected\n" );
777 if ( strcasecmp( cvalue, "none" ) == 0 ) {
778 nettimeout.tv_sec = 0;
779 } else if ( strcasecmp( cvalue, "max" ) == 0 ) {
780 nettimeout.tv_sec = LDAP_MAXINT;
782 ival = strtol( cvalue, &next, 10 );
783 if ( next == NULL || next[0] != '\0' ) {
785 _("Unable to parse network timeout \"%s\"\n"), cvalue );
786 exit( EXIT_FAILURE );
788 nettimeout.tv_sec = ival;
790 if( nettimeout.tv_sec < 0 || nettimeout.tv_sec > LDAP_MAXINT ) {
791 fprintf( stderr, _("%s: invalid network timeout (%ld) specified\n"),
792 prog, (long)nettimeout.tv_sec );
793 exit( EXIT_FAILURE );
796 } else if ( strcasecmp( control, "ldif-wrap" ) == 0 ) {
798 ldif_wrap = LDIF_LINE_WIDTH;
800 } else if ( strcasecmp( cvalue, "no" ) == 0 ) {
801 ldif_wrap = LDIF_LINE_WIDTH_MAX;
805 if ( lutil_atou( &u, cvalue ) ) {
807 _("Unable to parse ldif-wrap=\"%s\"\n"), cvalue );
808 exit( EXIT_FAILURE );
810 ldif_wrap = (ber_len_t)u;
814 fprintf( stderr, "Invalid general option name: %s\n",
818 ber_memfree(control);
821 #ifdef HAVE_CYRUS_SASL
822 if( sasl_secprops != NULL ) {
823 fprintf( stderr, "%s: -O previously specified\n", prog );
824 exit( EXIT_FAILURE );
826 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
827 fprintf( stderr, "%s: incompatible previous "
828 "authentication choice\n", prog );
829 exit( EXIT_FAILURE );
831 authmethod = LDAP_AUTH_SASL;
832 sasl_secprops = ber_strdup( optarg );
834 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
835 exit( EXIT_FAILURE );
840 fprintf( stderr, "%s: -p previously specified\n", prog );
841 exit( EXIT_FAILURE );
843 ival = strtol( optarg, &next, 10 );
844 if ( next == NULL || next[0] != '\0' ) {
845 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
846 exit( EXIT_FAILURE );
851 ival = strtol( optarg, &next, 10 );
852 if ( next == NULL || next[0] != '\0' ) {
853 fprintf( stderr, "%s: unable to parse protocol version \"%s\"\n", prog, optarg );
854 exit( EXIT_FAILURE );
858 if( protocol == LDAP_VERSION3 ) {
859 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
861 exit( EXIT_FAILURE );
863 protocol = LDAP_VERSION2;
866 if( protocol == LDAP_VERSION2 ) {
867 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
869 exit( EXIT_FAILURE );
871 protocol = LDAP_VERSION3;
874 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
880 #ifdef HAVE_CYRUS_SASL
881 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
882 fprintf( stderr, "%s: incompatible previous "
883 "authentication choice\n",
885 exit( EXIT_FAILURE );
887 authmethod = LDAP_AUTH_SASL;
888 sasl_flags = LDAP_SASL_QUIET;
891 fprintf( stderr, "%s: not compiled with SASL support\n",
893 exit( EXIT_FAILURE );
896 #ifdef HAVE_CYRUS_SASL
897 if( sasl_realm != NULL ) {
898 fprintf( stderr, "%s: -R previously specified\n", prog );
899 exit( EXIT_FAILURE );
901 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
902 fprintf( stderr, "%s: incompatible previous "
903 "authentication choice\n",
905 exit( EXIT_FAILURE );
907 authmethod = LDAP_AUTH_SASL;
908 sasl_realm = ber_strdup( optarg );
910 fprintf( stderr, "%s: not compiled with SASL support\n",
912 exit( EXIT_FAILURE );
916 #ifdef HAVE_CYRUS_SASL
917 if( sasl_authc_id != NULL ) {
918 fprintf( stderr, "%s: -U previously specified\n", prog );
919 exit( EXIT_FAILURE );
921 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
922 fprintf( stderr, "%s: incompatible previous "
923 "authentication choice\n",
925 exit( EXIT_FAILURE );
927 authmethod = LDAP_AUTH_SASL;
928 sasl_authc_id = ber_strdup( optarg );
930 fprintf( stderr, "%s: not compiled with SASL support\n",
932 exit( EXIT_FAILURE );
935 case 'v': /* verbose mode */
938 case 'V': /* version */
941 case 'w': /* password */
942 passwd.bv_val = ber_strdup( optarg );
946 for( p = optarg; *p != '\0'; p++ ) {
950 passwd.bv_len = strlen( passwd.bv_val );
959 #ifdef HAVE_CYRUS_SASL
960 if( sasl_mech != NULL ) {
961 fprintf( stderr, "%s: -Y previously specified\n", prog );
962 exit( EXIT_FAILURE );
964 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
966 "%s: incompatible with authentication choice\n", prog );
967 exit( EXIT_FAILURE );
969 authmethod = LDAP_AUTH_SASL;
970 sasl_mech = ber_strdup( optarg );
972 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
973 exit( EXIT_FAILURE );
977 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
978 fprintf( stderr, "%s: incompatible with previous "
979 "authentication choice\n", prog );
980 exit( EXIT_FAILURE );
982 authmethod = LDAP_AUTH_SIMPLE;
985 #ifdef HAVE_CYRUS_SASL
986 if( sasl_authz_id != NULL ) {
987 fprintf( stderr, "%s: -X previously specified\n", prog );
988 exit( EXIT_FAILURE );
990 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
991 fprintf( stderr, "%s: -X incompatible with "
992 "authentication choice\n", prog );
993 exit( EXIT_FAILURE );
995 authmethod = LDAP_AUTH_SASL;
996 sasl_authz_id = ber_strdup( optarg );
998 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
999 exit( EXIT_FAILURE );
1006 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
1007 exit( EXIT_FAILURE );
1011 if( handle_private_option( i ) ) break;
1012 fprintf( stderr, "%s: unrecognized option -%c\n",
1019 /* prevent bad linking */
1021 api.ldapai_info_version = LDAP_API_INFO_VERSION;
1023 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
1024 != LDAP_OPT_SUCCESS )
1026 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
1027 exit( EXIT_FAILURE );
1030 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
1031 fprintf( stderr, "LDAP APIInfo version mismatch: "
1032 "library %d, header %d\n",
1033 api.ldapai_info_version, LDAP_API_INFO_VERSION );
1034 exit( EXIT_FAILURE );
1037 if( api.ldapai_api_version != LDAP_API_VERSION ) {
1038 fprintf( stderr, "LDAP API version mismatch: "
1039 "library %d, header %d\n",
1040 api.ldapai_api_version, LDAP_API_VERSION );
1041 exit( EXIT_FAILURE );
1044 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
1045 fprintf( stderr, "LDAP vendor name mismatch: "
1046 "library %s, header %s\n",
1047 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
1048 exit( EXIT_FAILURE );
1051 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
1052 fprintf( stderr, "LDAP vendor version mismatch: "
1053 "library %d, header %d\n",
1054 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
1055 exit( EXIT_FAILURE );
1059 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
1061 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
1062 if (version > 1) exit( EXIT_SUCCESS );
1065 ldap_memfree( api.ldapai_vendor_name );
1066 ber_memvfree( (void **)api.ldapai_extensions );
1070 protocol = LDAP_VERSION3;
1072 if (authmethod == -1 && protocol > LDAP_VERSION2) {
1073 #ifdef HAVE_CYRUS_SASL
1074 if ( binddn != NULL ) {
1075 authmethod = LDAP_AUTH_SIMPLE;
1077 authmethod = LDAP_AUTH_SASL;
1080 authmethod = LDAP_AUTH_SIMPLE;
1084 if( ldapuri == NULL ) {
1085 if( ldapport && ( ldaphost == NULL )) {
1086 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
1087 exit( EXIT_FAILURE );
1090 if( ldaphost != NULL ) {
1091 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
1092 exit( EXIT_FAILURE );
1095 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
1096 exit( EXIT_FAILURE );
1100 if( protocol == LDAP_VERSION2 ) {
1101 if( assertctl || authzid || manageDIT || manageDSAit ||
1102 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1104 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1105 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1108 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1110 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1111 noop || ppolicy || preread || postread )
1113 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
1114 exit( EXIT_FAILURE );
1118 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
1119 exit( EXIT_FAILURE );
1122 #ifdef HAVE_CYRUS_SASL
1123 if( authmethod == LDAP_AUTH_SASL ) {
1124 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
1126 exit( EXIT_FAILURE );
1131 if ( ( pw_file || want_bindpw ) && !BER_BVISNULL( &passwd ) ) {
1132 fprintf( stderr, "%s: -%c incompatible with -w\n",
1133 prog, ( pw_file ? 'y' : 'W' ) );
1134 exit( EXIT_FAILURE );
1140 tool_conn_setup( int dont, void (*private_setup)( LDAP * ) )
1145 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
1146 != LBER_OPT_SUCCESS )
1149 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
1151 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
1152 != LDAP_OPT_SUCCESS )
1155 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
1160 (void) SIGNAL( SIGPIPE, SIG_IGN );
1164 SIGNAL( SIGINT, do_sig );
1170 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
1173 memset( &url, 0, sizeof(url));
1175 url.lud_scheme = "ldap";
1176 url.lud_host = ldaphost;
1177 url.lud_port = ldapport;
1178 url.lud_scope = LDAP_SCOPE_DEFAULT;
1180 ldapuri = ldap_url_desc2str( &url );
1182 } else if ( ldapuri != NULL ) {
1183 LDAPURLDesc *ludlist, **ludp;
1187 rc = ldap_url_parselist( &ludlist, ldapuri );
1188 if ( rc != LDAP_URL_SUCCESS ) {
1190 "Could not parse LDAP URI(s)=%s (%d)\n",
1192 exit( EXIT_FAILURE );
1195 for ( ludp = &ludlist; *ludp != NULL; ) {
1196 LDAPURLDesc *lud = *ludp;
1199 if ( lud->lud_dn != NULL && lud->lud_dn[ 0 ] != '\0' &&
1200 ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) )
1202 /* if no host but a DN is provided,
1203 * use DNS SRV to gather the host list
1204 * and turn it into a list of URIs
1205 * using the scheme provided */
1206 char *domain = NULL,
1210 len_proto = strlen( lud->lud_scheme );
1212 if ( ldap_dn2domain( lud->lud_dn, &domain )
1216 "DNS SRV: Could not turn "
1217 "DN=\"%s\" into a domain\n",
1222 rc = ldap_domain2hostlist( domain, &hostlist );
1225 "DNS SRV: Could not turn "
1226 "domain=%s into a hostlist\n",
1231 hosts = ldap_str2charray( hostlist, " " );
1232 if ( hosts == NULL ) {
1234 "DNS SRV: Could not parse "
1235 "hostlist=\"%s\"\n",
1240 for ( i = 0; hosts[ i ] != NULL; i++ )
1243 tmp = (char **)ber_memrealloc( urls, sizeof( char * ) * ( nurls + i + 1 ) );
1244 if ( tmp == NULL ) {
1246 "DNS SRV: out of memory?\n" );
1250 urls[ nurls ] = NULL;
1252 for ( i = 0; hosts[ i ] != NULL; i++ ) {
1253 size_t len = len_proto
1255 + strlen( hosts[ i ] )
1258 urls[ nurls + i + 1 ] = NULL;
1259 urls[ nurls + i ] = (char *)malloc( sizeof( char ) * len );
1260 if ( urls[ nurls + i ] == NULL ) {
1262 "DNS SRV: out of memory?\n" );
1266 snprintf( urls[ nurls + i ], len, "%s://%s",
1267 lud->lud_scheme, hosts[ i ] );
1272 ber_memvfree( (void **)hosts );
1273 ber_memfree( hostlist );
1274 ber_memfree( domain );
1277 tmp = (char **)ber_memrealloc( urls, sizeof( char * ) * ( nurls + 2 ) );
1278 if ( tmp == NULL ) {
1280 "DNS SRV: out of memory?\n" );
1284 urls[ nurls + 1 ] = NULL;
1286 urls[ nurls ] = ldap_url_desc2str( lud );
1287 if ( urls[ nurls ] == NULL ) {
1289 "DNS SRV: out of memory?\n" );
1295 *ludp = lud->lud_next;
1297 lud->lud_next = NULL;
1298 ldap_free_urldesc( lud );
1301 if ( ludlist != NULL ) {
1302 ldap_free_urllist( ludlist );
1303 exit( EXIT_FAILURE );
1305 } else if ( urls == NULL ) {
1306 exit( EXIT_FAILURE );
1309 ldap_memfree( ldapuri );
1310 ldapuri = ldap_charray2str( urls, " " );
1311 ber_memvfree( (void **)urls );
1315 fprintf( stderr, "ldap_initialize( %s )\n",
1316 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
1318 rc = ldap_initialize( &ld, ldapuri );
1319 if( rc != LDAP_SUCCESS ) {
1321 "Could not create LDAP session handle for URI=%s (%d): %s\n",
1322 ldapuri, rc, ldap_err2string(rc) );
1323 exit( EXIT_FAILURE );
1326 if( private_setup ) private_setup( ld );
1329 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
1330 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
1332 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
1333 referrals ? "on" : "off" );
1334 exit( EXIT_FAILURE );
1337 #ifdef HAVE_CYRUS_SASL
1339 if( ldap_set_option( ld, LDAP_OPT_X_SASL_NOCANON,
1340 nocanon ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
1342 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_NOCANON %s\n",
1343 nocanon ? "on" : "off" );
1344 exit( EXIT_FAILURE );
1347 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
1348 != LDAP_OPT_SUCCESS )
1350 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
1352 exit( EXIT_FAILURE );
1356 rc = ldap_start_tls_s( ld, NULL, NULL );
1357 if ( rc != LDAP_SUCCESS ) {
1359 ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&msg);
1360 tool_perror( "ldap_start_tls", rc, NULL, NULL, msg, NULL );
1362 if ( use_tls > 1 ) {
1363 exit( EXIT_FAILURE );
1368 if ( nettimeout.tv_sec > 0 ) {
1369 if ( ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &nettimeout )
1370 != LDAP_OPT_SUCCESS )
1372 fprintf( stderr, "Could not set LDAP_OPT_NETWORK_TIMEOUT %ld\n",
1373 (long)nettimeout.tv_sec );
1374 exit( EXIT_FAILURE );
1384 tool_bind( LDAP *ld )
1386 LDAPControl **sctrlsp = NULL;
1387 LDAPControl *sctrls[3];
1388 LDAPControl sctrl[3];
1391 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1394 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1395 c.ldctl_value.bv_val = NULL;
1396 c.ldctl_value.bv_len = 0;
1397 c.ldctl_iscritical = 0;
1399 sctrls[nsctrls] = &sctrl[nsctrls];
1400 sctrls[++nsctrls] = NULL;
1404 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1405 if ( sessionTracking ) {
1408 if (stValue.bv_val == NULL && st_value( ld, &stValue ) ) {
1409 exit( EXIT_FAILURE );
1412 c.ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1413 c.ldctl_iscritical = 0;
1414 ber_dupbv( &c.ldctl_value, &stValue );
1417 sctrls[nsctrls] = &sctrl[nsctrls];
1418 sctrls[++nsctrls] = NULL;
1420 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1426 assert( nsctrls < (int) (sizeof(sctrls)/sizeof(sctrls[0])) );
1428 if ( pw_file || want_bindpw ) {
1429 assert( passwd.bv_val == NULL && passwd.bv_len == 0 );
1432 if ( lutil_get_filed_password( pw_file, &passwd ) ) {
1433 exit( EXIT_FAILURE );
1437 char *pw = getpassphrase( _("Enter LDAP Password: ") );
1439 passwd.bv_val = ber_strdup( pw );
1440 passwd.bv_len = strlen( passwd.bv_val );
1445 if ( authmethod == LDAP_AUTH_SASL ) {
1446 #ifdef HAVE_CYRUS_SASL
1450 if( sasl_secprops != NULL ) {
1451 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
1452 (void *) sasl_secprops );
1454 if( rc != LDAP_OPT_SUCCESS ) {
1456 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
1458 exit( LDAP_LOCAL_ERROR );
1462 defaults = lutil_sasl_defaults( ld,
1469 rc = ldap_sasl_interactive_bind_s( ld, binddn, sasl_mech,
1471 NULL, sasl_flags, lutil_sasl_interact, defaults );
1473 lutil_sasl_freedefs( defaults );
1474 if( rc != LDAP_SUCCESS ) {
1476 ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&msg);
1477 tool_perror( "ldap_sasl_interactive_bind_s",
1478 rc, NULL, NULL, msg, NULL );
1483 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1484 exit( LDAP_NOT_SUPPORTED );
1488 LDAPMessage *result;
1489 LDAPControl **ctrls;
1491 char *matched = NULL;
1499 rc = ldap_sasl_bind( ld, binddn, LDAP_SASL_SIMPLE, &passwd,
1500 sctrlsp, NULL, &msgid );
1501 if ( msgid == -1 ) {
1502 tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
1503 NULL, NULL, NULL, NULL );
1508 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result );
1510 tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
1511 exit( LDAP_LOCAL_ERROR );
1515 tool_perror( "ldap_result", LDAP_TIMEOUT, NULL, NULL, NULL, NULL );
1516 exit( LDAP_LOCAL_ERROR );
1519 rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
1521 if ( rc != LDAP_SUCCESS ) {
1522 tool_perror( "ldap_bind parse result", rc, NULL, matched, info, refs );
1523 exit( LDAP_LOCAL_ERROR );
1526 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1527 if ( ctrls && ppolicy ) {
1529 int expire, grace, len = 0;
1530 LDAPPasswordPolicyError pErr = -1;
1532 ctrl = ldap_control_find( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
1535 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
1536 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1538 if ( pErr != PP_noError ){
1541 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1542 len = strlen( msgbuf );
1544 if ( expire >= 0 ) {
1545 sprintf( msgbuf+len,
1546 " (Password expires in %d seconds)",
1548 } else if ( grace >= 0 ) {
1549 sprintf( msgbuf+len,
1550 " (Password expired, %d grace logins remain)",
1558 ldap_controls_free( ctrls );
1561 if ( err != LDAP_SUCCESS
1563 || ( matched && matched[ 0 ] )
1564 || ( info && info[ 0 ] )
1567 tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1569 if( matched ) ber_memfree( matched );
1570 if( info ) ber_memfree( info );
1571 if( refs ) ber_memvfree( (void **)refs );
1573 if ( err != LDAP_SUCCESS ) exit( err );
1579 tool_unbind( LDAP *ld )
1581 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1583 if ( err != LDAP_OPT_SUCCESS ) {
1584 fprintf( stderr, "Could not unset controls\n");
1587 (void) ldap_unbind_ext( ld, NULL, NULL );
1591 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1593 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1595 int i = 0, j, crit = 0, err;
1596 LDAPControl c[16], **ctrls;
1600 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1602 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1606 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1611 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1613 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1614 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1616 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1618 || unknown_ctrls_num ) )
1623 ctrls = (LDAPControl**) malloc(sizeof(c) + (count + unknown_ctrls_num + 1)*sizeof(LDAPControl*));
1624 if ( ctrls == NULL ) {
1625 fprintf( stderr, "No memory\n" );
1626 exit( EXIT_FAILURE );
1630 if ( BER_BVISNULL( &assertionvalue ) ) {
1631 err = ldap_create_assertion_control_value( ld,
1632 assertion, &assertionvalue );
1635 "Unable to create assertion value "
1636 "\"%s\" (%d)\n", assertion, err );
1640 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1641 c[i].ldctl_value = assertionvalue;
1642 c[i].ldctl_iscritical = assertctl > 1;
1648 c[i].ldctl_value.bv_val = authzid;
1649 c[i].ldctl_value.bv_len = strlen( authzid );
1650 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1651 c[i].ldctl_iscritical = 1;
1656 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1657 /* NOTE: doesn't need an extra count because it's incompatible
1660 BerElementBuffer berbuf;
1661 BerElement *ber = (BerElement *)&berbuf;
1663 ber_init2( ber, NULL, LBER_USE_DER );
1665 if ( ber_printf( ber, "s", proxydn ) == -1 ) {
1666 exit( EXIT_FAILURE );
1669 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1670 exit( EXIT_FAILURE );
1673 c[i].ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1674 c[i].ldctl_iscritical = 1;
1678 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1681 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1682 BER_BVZERO( &c[i].ldctl_value );
1683 c[i].ldctl_iscritical = manageDIT > 1;
1688 if ( manageDSAit ) {
1689 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1690 BER_BVZERO( &c[i].ldctl_value );
1691 c[i].ldctl_iscritical = manageDSAit > 1;
1697 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1698 BER_BVZERO( &c[i].ldctl_value );
1699 c[i].ldctl_iscritical = noop > 1;
1704 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1706 c[i].ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1707 BER_BVZERO( &c[i].ldctl_value );
1708 c[i].ldctl_iscritical = 0;
1715 BerElementBuffer berbuf;
1716 BerElement *ber = (BerElement *)&berbuf;
1717 char **attrs = NULL;
1719 if( preread_attrs ) {
1720 attrs = ldap_str2charray( preread_attrs, "," );
1723 ber_init2( ber, NULL, LBER_USE_DER );
1725 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1726 fprintf( stderr, "preread attrs encode failed.\n" );
1727 exit( EXIT_FAILURE );
1730 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1732 fprintf( stderr, "preread flatten failed (%d)\n", err );
1733 exit( EXIT_FAILURE );
1736 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1737 c[i].ldctl_iscritical = preread > 1;
1741 if( attrs ) ldap_charray_free( attrs );
1745 BerElementBuffer berbuf;
1746 BerElement *ber = (BerElement *)&berbuf;
1747 char **attrs = NULL;
1749 if( postread_attrs ) {
1750 attrs = ldap_str2charray( postread_attrs, "," );
1753 ber_init2( ber, NULL, LBER_USE_DER );
1755 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1756 fprintf( stderr, "postread attrs encode failed.\n" );
1757 exit( EXIT_FAILURE );
1760 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1762 fprintf( stderr, "postread flatten failed (%d)\n", err );
1763 exit( EXIT_FAILURE );
1766 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1767 c[i].ldctl_iscritical = postread > 1;
1771 if( attrs ) ldap_charray_free( attrs );
1774 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1776 if ( chainingResolve > -1 ) {
1777 BerElementBuffer berbuf;
1778 BerElement *ber = (BerElement *)&berbuf;
1780 ber_init2( ber, NULL, LBER_USE_DER );
1782 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1785 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1786 exit( EXIT_FAILURE );
1789 if ( chainingContinuation > -1 ) {
1790 err = ber_printf( ber, "e", chainingContinuation );
1793 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1794 exit( EXIT_FAILURE );
1798 err = ber_printf( ber, /* { */ "N}" );
1801 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1802 exit( EXIT_FAILURE );
1805 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1806 exit( EXIT_FAILURE );
1810 BER_BVZERO( &c[i].ldctl_value );
1813 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1814 c[i].ldctl_iscritical = chaining > 1;
1818 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1820 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1821 if ( sessionTracking ) {
1822 if ( stValue.bv_val == NULL && st_value( ld, &stValue ) ) {
1823 exit( EXIT_FAILURE );
1826 c[i].ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1827 c[i].ldctl_iscritical = 0;
1828 ber_dupbv( &c[i].ldctl_value, &stValue );
1833 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1836 ctrls[i++] = extra_c++;
1838 for ( count = 0; count < unknown_ctrls_num; count++ ) {
1839 ctrls[i++] = &unknown_ctrls[count];
1843 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1845 if ( err != LDAP_OPT_SUCCESS ) {
1846 for ( j = 0; j < i; j++ ) {
1847 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1849 fprintf( stderr, "Could not set %scontrols\n",
1850 crit ? "critical " : "" );
1855 exit( EXIT_FAILURE );
1860 tool_check_abandon( LDAP *ld, int msgid )
1864 switch ( gotintr ) {
1866 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1867 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1868 rc, ldap_err2string( rc ) );
1872 rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
1873 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1874 rc, ldap_err2string( rc ) );
1878 /* just unbind, ignoring the request */
1886 print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
1891 tool_write_ldif( LDIF_PUT_COMMENT, "==> ",
1892 what->bv_val, what->bv_len );
1893 ber = ber_init( &ctrl->ldctl_value );
1894 if ( ber == NULL ) {
1898 } else if ( ber_scanf( ber, "{m{" /*}}*/, &bv ) == LBER_ERROR ) {
1903 tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1905 while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
1907 BerVarray vals = NULL;
1910 if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
1920 str = malloc( bv.bv_len + STRLENOF(": ") + 1 );
1923 ptr = lutil_strncopy( ptr, bv.bv_val, bv.bv_len );
1924 ptr = lutil_strcopy( ptr, ": " );
1927 for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
1929 ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1930 ldif ? str : bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
1933 ber_bvarray_free( vals );
1934 if ( str ) free( str );
1938 if ( ber != NULL ) {
1942 tool_write_ldif( LDIF_PUT_COMMENT, "<== ",
1943 what->bv_val, what->bv_len );
1949 print_preread( LDAP *ld, LDAPControl *ctrl )
1951 static struct berval what = BER_BVC( "preread" );
1953 return print_prepostread( ld, ctrl, &what );
1957 print_postread( LDAP *ld, LDAPControl *ctrl )
1959 static struct berval what = BER_BVC( "postread" );
1961 return print_prepostread( ld, ctrl, &what );
1965 print_paged_results( LDAP *ld, LDAPControl *ctrl )
1969 /* note: pr_cookie is being malloced; it's freed
1970 * the next time the control is sent, but the last
1971 * time it's not; we don't care too much, because
1972 * the last time an empty value is returned... */
1973 if ( ldap_parse_pageresponse_control( ld, ctrl, &estimate, &pr_cookie )
1980 /* FIXME: check buffer overflow */
1981 char buf[ BUFSIZ ], *ptr = buf;
1983 if ( estimate > 0 ) {
1984 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1985 "estimate=%d", estimate );
1988 if ( pr_cookie.bv_len > 0 ) {
1991 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
1992 pr_cookie.bv_len ) + 1;
1993 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
1995 bv.bv_len = lutil_b64_ntop(
1996 (unsigned char *) pr_cookie.bv_val,
1998 bv.bv_val, bv.bv_len );
2000 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2001 "%scookie=%s", ptr == buf ? "" : " ",
2004 ber_memfree( bv.bv_val );
2006 pr_morePagedResults = 1;
2009 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2010 "%scookie=", ptr == buf ? "" : " " );
2013 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2014 ldif ? "pagedresults: " : "pagedresults",
2022 print_sss( LDAP *ld, LDAPControl *ctrl )
2028 rc = ldap_parse_sortresponse_control( ld, ctrl, &err, &attr );
2029 if ( rc == LDAP_SUCCESS ) {
2031 rc = snprintf( buf, sizeof(buf), "(%d) %s%s%s",
2032 err, ldap_err2string(err), attr ? " " : "", attr ? attr : "" );
2034 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2035 ldif ? "sortResult: " : "sortResult", buf, rc );
2042 print_vlv( LDAP *ld, LDAPControl *ctrl )
2048 rc = ldap_parse_vlvresponse_control( ld, ctrl, &vlvPos, &vlvCount,
2049 &vlvContext, &err );
2050 if ( rc == LDAP_SUCCESS ) {
2053 if ( vlvContext && vlvContext->bv_len > 0 ) {
2054 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
2055 vlvContext->bv_len ) + 1;
2056 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
2058 bv.bv_len = lutil_b64_ntop(
2059 (unsigned char *) vlvContext->bv_val,
2061 bv.bv_val, bv.bv_len );
2067 rc = snprintf( buf, sizeof(buf), "pos=%d count=%d context=%s (%d) %s",
2068 vlvPos, vlvCount, bv.bv_val,
2069 err, ldap_err2string(err));
2072 ber_memfree( bv.bv_val );
2074 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2075 ldif ? "vlvResult" : "vlvResult", buf, rc );
2081 #ifdef LDAP_CONTROL_X_DEREF
2083 print_deref( LDAP *ld, LDAPControl *ctrl )
2085 LDAPDerefRes *drhead = NULL, *dr;
2088 rc = ldap_parse_derefresponse_control( ld, ctrl, &drhead );
2089 if ( rc != LDAP_SUCCESS ) {
2093 for ( dr = drhead; dr != NULL; dr = dr->next ) {
2098 len = strlen( dr->derefAttr ) + STRLENOF(": ");
2100 for ( dv = dr->attrVals; dv != NULL; dv = dv->next ) {
2101 if ( dv->vals != NULL ) {
2103 ber_len_t tlen = strlen(dv->type);
2105 for ( j = 0; dv->vals[ j ].bv_val != NULL; j++ ) {
2106 len += STRLENOF("<:=>;") + tlen + 4*((dv->vals[ j ].bv_len - 1)/3 + 1);
2110 len += dr->derefVal.bv_len + STRLENOF("\n");
2111 buf = ldap_memalloc( len + 1 );
2112 if ( buf == NULL ) {
2113 rc = LDAP_NO_MEMORY;
2118 ptr = lutil_strcopy( ptr, dr->derefAttr );
2121 for ( dv = dr->attrVals; dv != NULL; dv = dv->next ) {
2122 if ( dv->vals != NULL ) {
2124 for ( j = 0; dv->vals[ j ].bv_val != NULL; j++ ) {
2125 int k = ldif_is_not_printable( dv->vals[ j ].bv_val, dv->vals[ j ].bv_len );
2128 ptr = lutil_strcopy( ptr, dv->type );
2135 (unsigned char *) dv->vals[ j ].bv_val,
2136 dv->vals[ j ].bv_len,
2137 ptr, buf + len - ptr );
2142 ptr = lutil_memcopy( ptr, dv->vals[ j ].bv_val, dv->vals[ j ].bv_len );
2149 ptr = lutil_strncopy( ptr, dr->derefVal.bv_val, dr->derefVal.bv_len );
2152 assert( ptr <= buf + len );
2154 tool_write_ldif( LDIF_PUT_COMMENT, NULL, buf, ptr - buf);
2156 ldap_memfree( buf );
2162 ldap_derefresponse_free( drhead );
2168 #ifdef LDAP_CONTROL_X_WHATFAILED
2170 print_whatfailed( LDAP *ld, LDAPControl *ctrl )
2175 BerVarray bva = NULL;
2177 /* Create a BerElement from the berval returned in the control. */
2178 ber = ber_init( &ctrl->ldctl_value );
2180 if ( ber == NULL ) {
2181 return LDAP_NO_MEMORY;
2184 siz = sizeof(struct berval);
2185 tag = ber_scanf( ber, "[M]", &bva, &siz, 0 );
2186 if ( tag != LBER_ERROR ) {
2189 tool_write_ldif( LDIF_PUT_COMMENT, " what failed:", NULL, 0 );
2191 for ( i = 0; bva[i].bv_val != NULL; i++ ) {
2192 tool_write_ldif( LDIF_PUT_COMMENT, NULL, bva[i].bv_val, bva[i].bv_len );
2195 ldap_memfree( bva );
2205 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
2207 print_ppolicy( LDAP *ld, LDAPControl *ctrl )
2209 int expire = 0, grace = 0, rc;
2210 LDAPPasswordPolicyError pperr;
2212 rc = ldap_parse_passwordpolicy_control( ld, ctrl,
2213 &expire, &grace, &pperr );
2214 if ( rc == LDAP_SUCCESS ) {
2215 char buf[ BUFSIZ ], *ptr = buf;
2217 if ( expire != -1 ) {
2218 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2219 "expire=%d", expire );
2222 if ( grace != -1 ) {
2223 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2224 "%sgrace=%d", ptr == buf ? "" : " ", grace );
2227 if ( pperr != PP_noError ) {
2228 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2229 "%serror=%d (%s)", ptr == buf ? "" : " ",
2231 ldap_passwordpolicy_err2txt( pperr ) );
2234 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2235 ldif ? "ppolicy: " : "ppolicy", buf, ptr - buf );
2242 void tool_print_ctrls(
2244 LDAPControl **ctrls )
2249 for ( i = 0; ctrls[i] != NULL; i++ ) {
2250 /* control: OID criticality base64value */
2251 struct berval b64 = BER_BVNULL;
2256 /* FIXME: there might be cases where a control has NULL OID;
2257 * this makes little sense, especially when returned by the
2258 * server, but libldap happily allows it */
2259 if ( ctrls[i]->ldctl_oid == NULL ) {
2264 len += strlen( ctrls[i]->ldctl_oid );
2266 /* add enough for space after OID and the critical value itself */
2267 len += ctrls[i]->ldctl_iscritical
2268 ? sizeof("true") : sizeof("false");
2270 /* convert to base64 */
2271 if ( !BER_BVISNULL( &ctrls[i]->ldctl_value ) ) {
2272 b64.bv_len = LUTIL_BASE64_ENCODE_LEN(
2273 ctrls[i]->ldctl_value.bv_len ) + 1;
2274 b64.bv_val = ber_memalloc( b64.bv_len + 1 );
2276 b64.bv_len = lutil_b64_ntop(
2277 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
2278 ctrls[i]->ldctl_value.bv_len,
2279 b64.bv_val, b64.bv_len );
2283 len += 1 + b64.bv_len;
2286 ptr = str = malloc( len + 1 );
2288 ptr = lutil_strcopy( ptr, ": " );
2290 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_oid );
2291 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_iscritical
2292 ? " true" : " false" );
2295 ptr = lutil_strcopy( ptr, " " );
2296 ptr = lutil_strcopy( ptr, b64.bv_val );
2300 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2301 "control", str, len );
2306 ber_memfree( b64.bv_val );
2309 /* known controls */
2310 for ( j = 0; tool_ctrl_response[j].oid != NULL; j++ ) {
2311 if ( strcmp( tool_ctrl_response[j].oid, ctrls[i]->ldctl_oid ) == 0 ) {
2312 if ( !tool_ctrl_response[j].mask & tool_type ) {
2313 /* this control should not appear
2314 * with this tool; warning? */
2320 if ( tool_ctrl_response[j].oid != NULL && tool_ctrl_response[j].func ) {
2321 (void)tool_ctrl_response[j].func( ld, ctrls[i] );
2327 tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
2331 if (( ldif = ldif_put_wrap( type, name, value, vallen, ldif_wrap )) == NULL ) {
2335 fputs( ldif, stdout );
2336 ber_memfree( ldif );
2342 tool_is_oid( const char *s )
2346 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2350 for ( ; s[ 0 ]; s++ ) {
2351 if ( s[ 0 ] == '.' ) {
2352 if ( s[ 1 ] == '\0' ) {
2359 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2363 if ( first == 1 && s[ 0 ] == '0' && s[ 1 ] != '.' ) {