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-2010 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;
73 char *ldaphost = NULL;
83 struct berval passwd = { 0, NULL };
85 #ifdef HAVE_CYRUS_SASL
86 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
87 char *sasl_realm = NULL;
88 char *sasl_authc_id = NULL;
89 char *sasl_authz_id = NULL;
90 char *sasl_mech = NULL;
91 char *sasl_secprops = NULL;
96 char *assertion = NULL;
97 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 pr_cookie.bv_val = NULL;
257 pr_cookie.bv_len = 0;
260 if ( binddn != NULL ) {
261 ber_memfree( binddn );
264 if ( passwd.bv_val != NULL ) {
265 ber_memfree( passwd.bv_val );
268 if ( infile != NULL ) {
269 ber_memfree( infile );
274 tool_common_usage( void )
276 static const char *const descriptions[] = {
277 N_(" -d level set LDAP debugging level to `level'\n"),
278 N_(" -D binddn bind DN\n"),
279 N_(" -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
280 N_(" [!]assert=<filter> (RFC 4528; a RFC 4515 Filter string)\n")
281 N_(" [!]authzid=<authzid> (RFC 4370; \"dn:<dn>\" or \"u:<user>\")\n")
282 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
284 /* non-advertized support for proxyDN */
285 N_(" [!]proxydn=<dn> (a RFC 4514 DN string)\n")
288 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
289 N_(" [!]chaining[=<resolveBehavior>[/<continuationBehavior>]]\n")
290 N_(" one of \"chainingPreferred\", \"chainingRequired\",\n")
291 N_(" \"referralsPreferred\", \"referralsRequired\"\n")
292 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
293 N_(" [!]manageDSAit (RFC 3296)\n")
295 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
298 N_(" [!]postread[=<attrs>] (RFC 4527; comma-separated attr list)\n")
299 N_(" [!]preread[=<attrs>] (RFC 4527; comma-separated attr list)\n")
301 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
302 N_(" [!]sessiontracking\n")
303 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
304 N_(" abandon, cancel, ignore (SIGINT sends abandon/cancel,\n"
305 " or ignores response; if critical, doesn't wait for SIGINT.\n"
306 " not really controls)\n")
307 N_(" -h host LDAP server\n"),
308 N_(" -H URI LDAP Uniform Resource Identifier(s)\n"),
309 N_(" -I use SASL Interactive mode\n"),
310 N_(" -n show what would be done but don't actually do it\n"),
311 N_(" -N do not use reverse DNS to canonicalize SASL host name\n"),
312 N_(" -O props SASL security properties\n"),
313 N_(" -o <opt>[=<optparam] general options\n"),
314 N_(" nettimeout=<timeout> (in seconds, or \"none\" or \"max\")\n"),
315 N_(" -p port port on LDAP server\n"),
316 N_(" -Q use SASL Quiet mode\n"),
317 N_(" -R realm SASL realm\n"),
318 N_(" -U authcid SASL authentication identity\n"),
319 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
320 N_(" -V print version info (-VV only)\n"),
321 N_(" -w passwd bind password (for simple authentication)\n"),
322 N_(" -W prompt for bind password\n"),
323 N_(" -x Simple authentication\n"),
324 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
325 N_(" -y file Read password from file\n"),
326 N_(" -Y mech SASL mechanism\n"),
327 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
330 const char *const *cpp;
332 fputs( _("Common options:\n"), stderr );
333 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
334 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
335 fputs( _(*cpp), stderr );
348 fprintf( stderr, "%s: %s (%d)%s\n",
349 func, ldap_err2string( err ), err, extra ? extra : "" );
351 if ( matched && *matched ) {
352 fprintf( stderr, _("\tmatched DN: %s\n"), matched );
355 if ( info && *info ) {
356 fprintf( stderr, _("\tadditional info: %s\n"), info );
359 if ( refs && *refs ) {
361 fprintf( stderr, _("\treferrals:\n") );
362 for( i=0; refs[i]; i++ ) {
363 fprintf( stderr, "\t\t%s\n", refs[i] );
370 tool_args( int argc, char **argv )
374 while (( i = getopt( argc, argv, options )) != EOF ) {
376 char *control, *cvalue, *next;
378 case 'c': /* continuous operation mode */
385 ival = strtol( optarg, &next, 10 );
386 if (next == NULL || next[0] != '\0') {
387 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
392 case 'D': /* bind DN */
393 if( binddn != NULL ) {
394 fprintf( stderr, "%s: -D previously specified\n", prog );
395 exit( EXIT_FAILURE );
397 binddn = ber_strdup( optarg );
399 case 'e': /* general extensions (controls and such) */
400 /* should be extended to support comma separated list of
401 * [!]key[=value] parameters, e.g. -e !foo,bar=567
406 while ( optarg[0] == '!' ) {
411 control = ber_strdup( optarg );
412 if ( (cvalue = strchr( control, '=' )) != NULL ) {
416 if ( strcasecmp( control, "assert" ) == 0 ) {
418 fprintf( stderr, "assert control previously specified\n");
419 exit( EXIT_FAILURE );
421 if( cvalue == NULL ) {
422 fprintf( stderr, "assert: control value expected\n" );
426 assertctl = 1 + crit;
428 assert( assertion == NULL );
431 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
432 if( authzid != NULL ) {
433 fprintf( stderr, "authzid control previously specified\n");
434 exit( EXIT_FAILURE );
436 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
437 if( proxydn != NULL ) {
438 fprintf( stderr, "authzid control incompatible with proxydn\n");
439 exit( EXIT_FAILURE );
441 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
442 if( cvalue == NULL ) {
443 fprintf( stderr, "authzid: control value expected\n" );
447 fprintf( stderr, "authzid: must be marked critical\n" );
449 } else if ( crit > 1 ) {
450 /* purposely flag proxied authorization
451 * as non-critical, to test DSA */
455 assert( authzid == NULL );
458 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
459 } else if ( strcasecmp( control, "proxydn" ) == 0 ) {
460 if( proxydn != NULL ) {
461 fprintf( stderr, "proxydn control previously specified\n");
462 exit( EXIT_FAILURE );
464 if( authzid != NULL ) {
465 fprintf( stderr, "proxydn control incompatible with authzid\n");
466 exit( EXIT_FAILURE );
468 if( cvalue == NULL ) {
469 fprintf( stderr, "proxydn: control value expected\n" );
473 fprintf( stderr, "proxydn: must be marked critical\n" );
475 } else if ( crit > 1 ) {
476 /* purposely flag proxied authorization
477 * as non-critical, to test DSA */
481 assert( proxydn == NULL );
483 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
485 } else if ( ( strcasecmp( control, "relax" ) == 0 ) ||
486 ( strcasecmp( control, "manageDIT" ) == 0 ) )
490 "relax control previously specified\n");
491 exit( EXIT_FAILURE );
493 if( cvalue != NULL ) {
495 "relax: no control value expected\n" );
499 manageDIT = 1 + crit;
501 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
504 "manageDSAit control previously specified\n");
505 exit( EXIT_FAILURE );
507 if( cvalue != NULL ) {
509 "manageDSAit: no control value expected\n" );
513 manageDSAit = 1 + crit;
515 } else if ( strcasecmp( control, "noop" ) == 0 ) {
517 fprintf( stderr, "noop control previously specified\n");
518 exit( EXIT_FAILURE );
520 if( cvalue != NULL ) {
521 fprintf( stderr, "noop: no control value expected\n" );
527 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
528 } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
530 fprintf( stderr, "ppolicy control previously specified\n");
531 exit( EXIT_FAILURE );
533 if( cvalue != NULL ) {
534 fprintf( stderr, "ppolicy: no control value expected\n" );
538 fprintf( stderr, "ppolicy: critical flag not allowed\n" );
545 } else if ( strcasecmp( control, "preread" ) == 0 ) {
547 fprintf( stderr, "preread control previously specified\n");
548 exit( EXIT_FAILURE );
552 preread_attrs = cvalue;
554 } else if ( strcasecmp( control, "postread" ) == 0 ) {
556 fprintf( stderr, "postread control previously specified\n");
557 exit( EXIT_FAILURE );
561 postread_attrs = cvalue;
563 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
564 } else if ( strcasecmp( control, "chaining" ) == 0 ) {
566 fprintf( stderr, "chaining control previously specified\n");
567 exit( EXIT_FAILURE );
572 if ( cvalue != NULL ) {
575 continuation = strchr( cvalue, '/' );
576 if ( continuation ) {
577 /* FIXME: this makes sense only in searches */
578 *continuation++ = '\0';
579 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
580 chainingContinuation = LDAP_CHAINING_PREFERRED;
581 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
582 chainingContinuation = LDAP_CHAINING_REQUIRED;
583 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
584 chainingContinuation = LDAP_REFERRALS_PREFERRED;
585 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
586 chainingContinuation = LDAP_REFERRALS_REQUIRED;
589 "chaining behavior control "
590 "continuation value \"%s\" invalid\n",
592 exit( EXIT_FAILURE );
596 if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
597 chainingResolve = LDAP_CHAINING_PREFERRED;
598 } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
599 chainingResolve = LDAP_CHAINING_REQUIRED;
600 } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
601 chainingResolve = LDAP_REFERRALS_PREFERRED;
602 } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
603 chainingResolve = LDAP_REFERRALS_REQUIRED;
606 "chaining behavior control "
607 "resolve value \"%s\" invalid\n",
609 exit( EXIT_FAILURE );
612 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
614 /* this shouldn't go here, really; but it's a feature... */
615 } else if ( strcasecmp( control, "abandon" ) == 0 ) {
616 abcan = Intr_Abandon;
621 } else if ( strcasecmp( control, "cancel" ) == 0 ) {
627 } else if ( strcasecmp( control, "ignore" ) == 0 ) {
633 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
634 } else if ( strcasecmp( control, "sessiontracking" ) == 0 ) {
635 if ( sessionTracking ) {
636 fprintf( stderr, "%s: session tracking can be only specified once\n", prog );
637 exit( EXIT_FAILURE );
640 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
642 } else if ( tool_is_oid( control ) ) {
643 LDAPControl *tmpctrls, ctrl;
645 if ( unknown_ctrls != NULL ) {
647 for ( i = 0; unknown_ctrls[ i ].ldctl_oid != NULL; i++ ) {
648 if ( strcmp( control, unknown_ctrls[ i ].ldctl_oid ) == 0 ) {
649 fprintf( stderr, "%s control previously specified\n", control );
650 exit( EXIT_FAILURE );
655 tmpctrls = (LDAPControl *)ber_memrealloc( unknown_ctrls,
656 (unknown_ctrls_num + 1)*sizeof( LDAPControl ) );
657 if ( tmpctrls == NULL ) {
658 fprintf( stderr, "%s: no memory?\n", prog );
659 exit( EXIT_FAILURE );
661 unknown_ctrls = tmpctrls;
662 ctrl.ldctl_oid = control;
663 ctrl.ldctl_value.bv_val = NULL;
664 ctrl.ldctl_value.bv_len = 0;
665 ctrl.ldctl_iscritical = crit;
667 if ( cvalue != NULL ) {
669 size_t len = strlen( cvalue );
672 bv.bv_len = LUTIL_BASE64_DECODE_LEN( len );
673 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
675 retcode = lutil_b64_pton( cvalue,
676 (unsigned char *)bv.bv_val,
679 if ( retcode == -1 || (unsigned) retcode > bv.bv_len ) {
680 fprintf( stderr, "Unable to parse value of general control %s\n",
686 ctrl.ldctl_value = bv;
689 unknown_ctrls[ unknown_ctrls_num ] = ctrl;
693 fprintf( stderr, "Invalid general control name: %s\n",
698 case 'f': /* read from file */
699 if( infile != NULL ) {
700 fprintf( stderr, "%s: -f previously specified\n", prog );
701 exit( EXIT_FAILURE );
703 infile = ber_strdup( optarg );
705 case 'h': /* ldap host */
706 if( ldaphost != NULL ) {
707 fprintf( stderr, "%s: -h previously specified\n", prog );
708 exit( EXIT_FAILURE );
710 ldaphost = ber_strdup( optarg );
712 case 'H': /* ldap URI */
713 if( ldapuri != NULL ) {
714 fprintf( stderr, "%s: -H previously specified\n", prog );
715 exit( EXIT_FAILURE );
717 ldapuri = ber_strdup( optarg );
720 #ifdef HAVE_CYRUS_SASL
721 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
722 fprintf( stderr, "%s: incompatible previous "
723 "authentication choice\n",
725 exit( EXIT_FAILURE );
727 authmethod = LDAP_AUTH_SASL;
728 sasl_flags = LDAP_SASL_INTERACTIVE;
731 fprintf( stderr, "%s: was not compiled with SASL support\n",
733 exit( EXIT_FAILURE );
736 /* enable Manage DSA IT */
739 case 'n': /* print operations, don't actually do them */
746 control = ber_strdup( optarg );
747 if ( (cvalue = strchr( control, '=' )) != NULL ) {
751 if ( strcasecmp( control, "nettimeout" ) == 0 ) {
752 if( nettimeout.tv_sec != -1 ) {
753 fprintf( stderr, "nettimeout option previously specified\n");
754 exit( EXIT_FAILURE );
756 if( cvalue == NULL || cvalue[0] == '\0' ) {
757 fprintf( stderr, "nettimeout: option value expected\n" );
760 if ( strcasecmp( cvalue, "none" ) == 0 ) {
761 nettimeout.tv_sec = 0;
762 } else if ( strcasecmp( cvalue, "max" ) == 0 ) {
763 nettimeout.tv_sec = LDAP_MAXINT;
765 ival = strtol( cvalue, &next, 10 );
766 if ( next == NULL || next[0] != '\0' ) {
768 _("Unable to parse network timeout \"%s\"\n"), cvalue );
769 exit( EXIT_FAILURE );
771 nettimeout.tv_sec = ival;
773 if( nettimeout.tv_sec < 0 || nettimeout.tv_sec > LDAP_MAXINT ) {
774 fprintf( stderr, _("%s: invalid network timeout (%ld) specified\n"),
775 prog, (long)nettimeout.tv_sec );
776 exit( EXIT_FAILURE );
779 fprintf( stderr, "Invalid general option name: %s\n",
785 #ifdef HAVE_CYRUS_SASL
786 if( sasl_secprops != NULL ) {
787 fprintf( stderr, "%s: -O previously specified\n", prog );
788 exit( EXIT_FAILURE );
790 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
791 fprintf( stderr, "%s: incompatible previous "
792 "authentication choice\n", prog );
793 exit( EXIT_FAILURE );
795 authmethod = LDAP_AUTH_SASL;
796 sasl_secprops = ber_strdup( optarg );
798 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
799 exit( EXIT_FAILURE );
804 fprintf( stderr, "%s: -p previously specified\n", prog );
805 exit( EXIT_FAILURE );
807 ival = strtol( optarg, &next, 10 );
808 if ( next == NULL || next[0] != '\0' ) {
809 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
810 exit( EXIT_FAILURE );
815 ival = strtol( optarg, &next, 10 );
816 if ( next == NULL || next[0] != '\0' ) {
817 fprintf( stderr, "%s: unable to parse protocol version \"%s\"\n", prog, optarg );
818 exit( EXIT_FAILURE );
822 if( protocol == LDAP_VERSION3 ) {
823 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
825 exit( EXIT_FAILURE );
827 protocol = LDAP_VERSION2;
830 if( protocol == LDAP_VERSION2 ) {
831 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
833 exit( EXIT_FAILURE );
835 protocol = LDAP_VERSION3;
838 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
844 #ifdef HAVE_CYRUS_SASL
845 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
846 fprintf( stderr, "%s: incompatible previous "
847 "authentication choice\n",
849 exit( EXIT_FAILURE );
851 authmethod = LDAP_AUTH_SASL;
852 sasl_flags = LDAP_SASL_QUIET;
855 fprintf( stderr, "%s: not compiled with SASL support\n",
857 exit( EXIT_FAILURE );
860 #ifdef HAVE_CYRUS_SASL
861 if( sasl_realm != NULL ) {
862 fprintf( stderr, "%s: -R previously specified\n", prog );
863 exit( EXIT_FAILURE );
865 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
866 fprintf( stderr, "%s: incompatible previous "
867 "authentication choice\n",
869 exit( EXIT_FAILURE );
871 authmethod = LDAP_AUTH_SASL;
872 sasl_realm = ber_strdup( optarg );
874 fprintf( stderr, "%s: not compiled with SASL support\n",
876 exit( EXIT_FAILURE );
880 #ifdef HAVE_CYRUS_SASL
881 if( sasl_authc_id != NULL ) {
882 fprintf( stderr, "%s: -U previously specified\n", prog );
883 exit( EXIT_FAILURE );
885 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
886 fprintf( stderr, "%s: incompatible previous "
887 "authentication choice\n",
889 exit( EXIT_FAILURE );
891 authmethod = LDAP_AUTH_SASL;
892 sasl_authc_id = ber_strdup( optarg );
894 fprintf( stderr, "%s: not compiled with SASL support\n",
896 exit( EXIT_FAILURE );
899 case 'v': /* verbose mode */
902 case 'V': /* version */
905 case 'w': /* password */
906 passwd.bv_val = ber_strdup( optarg );
910 for( p = optarg; *p != '\0'; p++ ) {
914 passwd.bv_len = strlen( passwd.bv_val );
923 #ifdef HAVE_CYRUS_SASL
924 if( sasl_mech != NULL ) {
925 fprintf( stderr, "%s: -Y previously specified\n", prog );
926 exit( EXIT_FAILURE );
928 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
930 "%s: incompatible with authentication choice\n", prog );
931 exit( EXIT_FAILURE );
933 authmethod = LDAP_AUTH_SASL;
934 sasl_mech = ber_strdup( optarg );
936 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
937 exit( EXIT_FAILURE );
941 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
942 fprintf( stderr, "%s: incompatible with previous "
943 "authentication choice\n", prog );
944 exit( EXIT_FAILURE );
946 authmethod = LDAP_AUTH_SIMPLE;
949 #ifdef HAVE_CYRUS_SASL
950 if( sasl_authz_id != NULL ) {
951 fprintf( stderr, "%s: -X previously specified\n", prog );
952 exit( EXIT_FAILURE );
954 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
955 fprintf( stderr, "%s: -X incompatible with "
956 "authentication choice\n", prog );
957 exit( EXIT_FAILURE );
959 authmethod = LDAP_AUTH_SASL;
960 sasl_authz_id = ber_strdup( optarg );
962 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
963 exit( EXIT_FAILURE );
970 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
971 exit( EXIT_FAILURE );
975 if( handle_private_option( i ) ) break;
976 fprintf( stderr, "%s: unrecognized option -%c\n",
983 /* prevent bad linking */
985 api.ldapai_info_version = LDAP_API_INFO_VERSION;
987 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
988 != LDAP_OPT_SUCCESS )
990 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
991 exit( EXIT_FAILURE );
994 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
995 fprintf( stderr, "LDAP APIInfo version mismatch: "
996 "library %d, header %d\n",
997 api.ldapai_info_version, LDAP_API_INFO_VERSION );
998 exit( EXIT_FAILURE );
1001 if( api.ldapai_api_version != LDAP_API_VERSION ) {
1002 fprintf( stderr, "LDAP API version mismatch: "
1003 "library %d, header %d\n",
1004 api.ldapai_api_version, LDAP_API_VERSION );
1005 exit( EXIT_FAILURE );
1008 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
1009 fprintf( stderr, "LDAP vendor name mismatch: "
1010 "library %s, header %s\n",
1011 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
1012 exit( EXIT_FAILURE );
1015 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
1016 fprintf( stderr, "LDAP vendor version mismatch: "
1017 "library %d, header %d\n",
1018 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
1019 exit( EXIT_FAILURE );
1023 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
1025 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
1026 if (version > 1) exit( EXIT_SUCCESS );
1029 ldap_memfree( api.ldapai_vendor_name );
1030 ber_memvfree( (void **)api.ldapai_extensions );
1034 protocol = LDAP_VERSION3;
1036 if (authmethod == -1 && protocol > LDAP_VERSION2) {
1037 #ifdef HAVE_CYRUS_SASL
1038 if ( binddn != NULL ) {
1039 authmethod = LDAP_AUTH_SIMPLE;
1041 authmethod = LDAP_AUTH_SASL;
1044 authmethod = LDAP_AUTH_SIMPLE;
1048 if( ldapuri == NULL ) {
1049 if( ldapport && ( ldaphost == NULL )) {
1050 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
1051 exit( EXIT_FAILURE );
1054 if( ldaphost != NULL ) {
1055 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
1056 exit( EXIT_FAILURE );
1059 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
1060 exit( EXIT_FAILURE );
1064 if( protocol == LDAP_VERSION2 ) {
1065 if( assertctl || authzid || manageDIT || manageDSAit ||
1066 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1068 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1069 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1072 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1074 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1075 noop || ppolicy || preread || postread )
1077 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
1078 exit( EXIT_FAILURE );
1082 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
1083 exit( EXIT_FAILURE );
1086 #ifdef HAVE_CYRUS_SASL
1087 if( authmethod == LDAP_AUTH_SASL ) {
1088 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
1090 exit( EXIT_FAILURE );
1098 tool_conn_setup( int dont, void (*private_setup)( LDAP * ) )
1103 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
1104 != LBER_OPT_SUCCESS )
1107 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
1109 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
1110 != LDAP_OPT_SUCCESS )
1113 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
1118 (void) SIGNAL( SIGPIPE, SIG_IGN );
1122 SIGNAL( SIGINT, do_sig );
1128 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
1131 memset( &url, 0, sizeof(url));
1133 url.lud_scheme = "ldap";
1134 url.lud_host = ldaphost;
1135 url.lud_port = ldapport;
1136 url.lud_scope = LDAP_SCOPE_DEFAULT;
1138 ldapuri = ldap_url_desc2str( &url );
1140 } else if ( ldapuri != NULL ) {
1141 LDAPURLDesc *ludlist, **ludp;
1145 rc = ldap_url_parselist( &ludlist, ldapuri );
1146 if ( rc != LDAP_URL_SUCCESS ) {
1148 "Could not parse LDAP URI(s)=%s (%d)\n",
1150 exit( EXIT_FAILURE );
1153 for ( ludp = &ludlist; *ludp != NULL; ) {
1154 LDAPURLDesc *lud = *ludp;
1157 if ( lud->lud_dn != NULL && lud->lud_dn[ 0 ] != '\0' &&
1158 ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) )
1160 /* if no host but a DN is provided,
1161 * use DNS SRV to gather the host list
1162 * and turn it into a list of URIs
1163 * using the scheme provided */
1164 char *domain = NULL,
1168 len_proto = strlen( lud->lud_scheme );
1170 if ( ldap_dn2domain( lud->lud_dn, &domain )
1174 "DNS SRV: Could not turn "
1175 "DN=\"%s\" into a domain\n",
1180 rc = ldap_domain2hostlist( domain, &hostlist );
1183 "DNS SRV: Could not turn "
1184 "domain=%s into a hostlist\n",
1189 hosts = ldap_str2charray( hostlist, " " );
1190 if ( hosts == NULL ) {
1192 "DNS SRV: Could not parse "
1193 "hostlist=\"%s\"\n",
1198 for ( i = 0; hosts[ i ] != NULL; i++ )
1201 tmp = (char **)ber_memrealloc( urls, sizeof( char * ) * ( nurls + i + 1 ) );
1202 if ( tmp == NULL ) {
1204 "DNS SRV: out of memory?\n" );
1208 urls[ nurls ] = NULL;
1210 for ( i = 0; hosts[ i ] != NULL; i++ ) {
1211 size_t len = len_proto
1213 + strlen( hosts[ i ] )
1216 urls[ nurls + i + 1 ] = NULL;
1217 urls[ nurls + i ] = (char *)malloc( sizeof( char ) * len );
1218 if ( urls[ nurls + i ] == NULL ) {
1220 "DNS SRV: out of memory?\n" );
1224 snprintf( urls[ nurls + i ], len, "%s://%s",
1225 lud->lud_scheme, hosts[ i ] );
1230 ber_memvfree( (void **)hosts );
1231 ber_memfree( hostlist );
1232 ber_memfree( domain );
1235 tmp = (char **)ber_memrealloc( urls, sizeof( char * ) * ( nurls + 2 ) );
1236 if ( tmp == NULL ) {
1238 "DNS SRV: out of memory?\n" );
1242 urls[ nurls + 1 ] = NULL;
1244 urls[ nurls ] = ldap_url_desc2str( lud );
1245 if ( urls[ nurls ] == NULL ) {
1247 "DNS SRV: out of memory?\n" );
1253 *ludp = lud->lud_next;
1255 lud->lud_next = NULL;
1256 ldap_free_urldesc( lud );
1259 if ( ludlist != NULL ) {
1260 ldap_free_urllist( ludlist );
1261 exit( EXIT_FAILURE );
1263 } else if ( urls == NULL ) {
1264 exit( EXIT_FAILURE );
1267 ldap_memfree( ldapuri );
1268 ldapuri = ldap_charray2str( urls, " " );
1269 ber_memvfree( (void **)urls );
1273 fprintf( stderr, "ldap_initialize( %s )\n",
1274 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
1276 rc = ldap_initialize( &ld, ldapuri );
1277 if( rc != LDAP_SUCCESS ) {
1279 "Could not create LDAP session handle for URI=%s (%d): %s\n",
1280 ldapuri, rc, ldap_err2string(rc) );
1281 exit( EXIT_FAILURE );
1284 if( private_setup ) private_setup( ld );
1287 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
1288 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
1290 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
1291 referrals ? "on" : "off" );
1292 exit( EXIT_FAILURE );
1295 #ifdef HAVE_CYRUS_SASL
1297 if( ldap_set_option( ld, LDAP_OPT_X_SASL_NOCANON,
1298 nocanon ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
1300 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_NOCANON %s\n",
1301 nocanon ? "on" : "off" );
1302 exit( EXIT_FAILURE );
1305 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
1306 != LDAP_OPT_SUCCESS )
1308 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
1310 exit( EXIT_FAILURE );
1314 rc = ldap_start_tls_s( ld, NULL, NULL );
1315 if ( rc != LDAP_SUCCESS ) {
1317 ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&msg);
1318 tool_perror( "ldap_start_tls", rc, NULL, NULL, msg, NULL );
1320 if ( use_tls > 1 ) {
1321 exit( EXIT_FAILURE );
1326 if ( nettimeout.tv_sec > 0 ) {
1327 if ( ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &nettimeout )
1328 != LDAP_OPT_SUCCESS )
1330 fprintf( stderr, "Could not set LDAP_OPT_NETWORK_TIMEOUT %ld\n",
1331 (long)nettimeout.tv_sec );
1332 exit( EXIT_FAILURE );
1342 tool_bind( LDAP *ld )
1344 LDAPControl **sctrlsp = NULL;
1345 LDAPControl *sctrls[3];
1346 LDAPControl sctrl[3];
1349 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1352 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1353 c.ldctl_value.bv_val = NULL;
1354 c.ldctl_value.bv_len = 0;
1355 c.ldctl_iscritical = 0;
1357 sctrls[nsctrls] = &sctrl[nsctrls];
1358 sctrls[++nsctrls] = NULL;
1362 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1363 if ( sessionTracking ) {
1366 if (stValue.bv_val == NULL && st_value( ld, &stValue ) ) {
1367 exit( EXIT_FAILURE );
1370 c.ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1371 c.ldctl_iscritical = 0;
1372 ber_dupbv( &c.ldctl_value, &stValue );
1375 sctrls[nsctrls] = &sctrl[nsctrls];
1376 sctrls[++nsctrls] = NULL;
1378 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1384 assert( nsctrls < (int) (sizeof(sctrls)/sizeof(sctrls[0])) );
1386 if ( pw_file || want_bindpw ) {
1387 assert( passwd.bv_val == NULL && passwd.bv_len == 0 );
1390 if ( lutil_get_filed_password( pw_file, &passwd ) ) {
1391 exit( EXIT_FAILURE );
1395 char *pw = getpassphrase( _("Enter LDAP Password: ") );
1397 passwd.bv_val = ber_strdup( pw );
1398 passwd.bv_len = strlen( passwd.bv_val );
1403 if ( authmethod == LDAP_AUTH_SASL ) {
1404 #ifdef HAVE_CYRUS_SASL
1408 if( sasl_secprops != NULL ) {
1409 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
1410 (void *) sasl_secprops );
1412 if( rc != LDAP_OPT_SUCCESS ) {
1414 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
1416 exit( LDAP_LOCAL_ERROR );
1420 defaults = lutil_sasl_defaults( ld,
1427 rc = ldap_sasl_interactive_bind_s( ld, binddn, sasl_mech,
1429 NULL, sasl_flags, lutil_sasl_interact, defaults );
1431 lutil_sasl_freedefs( defaults );
1432 if( rc != LDAP_SUCCESS ) {
1434 ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&msg);
1435 tool_perror( "ldap_sasl_interactive_bind_s",
1436 rc, NULL, NULL, msg, NULL );
1441 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1442 exit( LDAP_NOT_SUPPORTED );
1446 LDAPMessage *result;
1447 LDAPControl **ctrls;
1449 char *matched = NULL;
1457 rc = ldap_sasl_bind( ld, binddn, LDAP_SASL_SIMPLE, &passwd,
1458 sctrlsp, NULL, &msgid );
1459 if ( msgid == -1 ) {
1460 tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
1461 NULL, NULL, NULL, NULL );
1466 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result );
1468 tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
1469 exit( LDAP_LOCAL_ERROR );
1473 tool_perror( "ldap_result", LDAP_TIMEOUT, NULL, NULL, NULL, NULL );
1474 exit( LDAP_LOCAL_ERROR );
1477 rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
1479 if ( rc != LDAP_SUCCESS ) {
1480 tool_perror( "ldap_bind parse result", rc, NULL, matched, info, refs );
1481 exit( LDAP_LOCAL_ERROR );
1484 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1485 if ( ctrls && ppolicy ) {
1487 int expire, grace, len = 0;
1488 LDAPPasswordPolicyError pErr = -1;
1490 ctrl = ldap_control_find( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
1493 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
1494 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1496 if ( pErr != PP_noError ){
1499 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1500 len = strlen( msgbuf );
1502 if ( expire >= 0 ) {
1503 sprintf( msgbuf+len,
1504 " (Password expires in %d seconds)",
1506 } else if ( grace >= 0 ) {
1507 sprintf( msgbuf+len,
1508 " (Password expired, %d grace logins remain)",
1516 ldap_controls_free( ctrls );
1519 if ( err != LDAP_SUCCESS
1521 || ( matched && matched[ 0 ] )
1522 || ( info && info[ 0 ] )
1525 tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1527 if( matched ) ber_memfree( matched );
1528 if( info ) ber_memfree( info );
1529 if( refs ) ber_memvfree( (void **)refs );
1531 if ( err != LDAP_SUCCESS ) exit( err );
1537 tool_unbind( LDAP *ld )
1539 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1541 if ( err != LDAP_OPT_SUCCESS ) {
1542 fprintf( stderr, "Could not unset controls\n");
1545 (void) ldap_unbind_ext( ld, NULL, NULL );
1549 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1551 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1553 int i = 0, j, crit = 0, err;
1554 LDAPControl c[16], **ctrls;
1558 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1560 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1564 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1569 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1571 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1572 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1574 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1576 || unknown_ctrls_num ) )
1581 ctrls = (LDAPControl**) malloc(sizeof(c) + (count + unknown_ctrls_num + 1)*sizeof(LDAPControl*));
1582 if ( ctrls == NULL ) {
1583 fprintf( stderr, "No memory\n" );
1584 exit( EXIT_FAILURE );
1588 if ( BER_BVISNULL( &assertionvalue ) ) {
1589 err = ldap_create_assertion_control_value( ld,
1590 assertion, &assertionvalue );
1593 "Unable to create assertion value "
1594 "\"%s\" (%d)\n", assertion, err );
1598 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1599 c[i].ldctl_value = assertionvalue;
1600 c[i].ldctl_iscritical = assertctl > 1;
1606 c[i].ldctl_value.bv_val = authzid;
1607 c[i].ldctl_value.bv_len = strlen( authzid );
1608 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1609 c[i].ldctl_iscritical = authzcrit;
1614 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1615 /* NOTE: doesn't need an extra count because it's incompatible
1618 BerElementBuffer berbuf;
1619 BerElement *ber = (BerElement *)&berbuf;
1621 ber_init2( ber, NULL, LBER_USE_DER );
1623 if ( ber_printf( ber, "s", proxydn ) == -1 ) {
1624 exit( EXIT_FAILURE );
1627 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1628 exit( EXIT_FAILURE );
1631 c[i].ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1632 c[i].ldctl_iscritical = authzcrit;
1636 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1639 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1640 BER_BVZERO( &c[i].ldctl_value );
1641 c[i].ldctl_iscritical = manageDIT > 1;
1646 if ( manageDSAit ) {
1647 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1648 BER_BVZERO( &c[i].ldctl_value );
1649 c[i].ldctl_iscritical = manageDSAit > 1;
1655 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1656 BER_BVZERO( &c[i].ldctl_value );
1657 c[i].ldctl_iscritical = noop > 1;
1662 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1664 c[i].ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1665 BER_BVZERO( &c[i].ldctl_value );
1666 c[i].ldctl_iscritical = 0;
1673 BerElementBuffer berbuf;
1674 BerElement *ber = (BerElement *)&berbuf;
1675 char **attrs = NULL;
1677 if( preread_attrs ) {
1678 attrs = ldap_str2charray( preread_attrs, "," );
1681 ber_init2( ber, NULL, LBER_USE_DER );
1683 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1684 fprintf( stderr, "preread attrs encode failed.\n" );
1685 exit( EXIT_FAILURE );
1688 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1690 fprintf( stderr, "preread flatten failed (%d)\n", err );
1691 exit( EXIT_FAILURE );
1694 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1695 c[i].ldctl_iscritical = preread > 1;
1699 if( attrs ) ldap_charray_free( attrs );
1703 BerElementBuffer berbuf;
1704 BerElement *ber = (BerElement *)&berbuf;
1705 char **attrs = NULL;
1707 if( postread_attrs ) {
1708 attrs = ldap_str2charray( postread_attrs, "," );
1711 ber_init2( ber, NULL, LBER_USE_DER );
1713 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1714 fprintf( stderr, "postread attrs encode failed.\n" );
1715 exit( EXIT_FAILURE );
1718 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1720 fprintf( stderr, "postread flatten failed (%d)\n", err );
1721 exit( EXIT_FAILURE );
1724 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1725 c[i].ldctl_iscritical = postread > 1;
1729 if( attrs ) ldap_charray_free( attrs );
1732 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1734 if ( chainingResolve > -1 ) {
1735 BerElementBuffer berbuf;
1736 BerElement *ber = (BerElement *)&berbuf;
1738 ber_init2( ber, NULL, LBER_USE_DER );
1740 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1743 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1744 exit( EXIT_FAILURE );
1747 if ( chainingContinuation > -1 ) {
1748 err = ber_printf( ber, "e", chainingContinuation );
1751 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1752 exit( EXIT_FAILURE );
1756 err = ber_printf( ber, /* { */ "N}" );
1759 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1760 exit( EXIT_FAILURE );
1763 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1764 exit( EXIT_FAILURE );
1768 BER_BVZERO( &c[i].ldctl_value );
1771 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1772 c[i].ldctl_iscritical = chaining > 1;
1776 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1778 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1779 if ( sessionTracking ) {
1780 if ( stValue.bv_val == NULL && st_value( ld, &stValue ) ) {
1781 exit( EXIT_FAILURE );
1784 c[i].ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1785 c[i].ldctl_iscritical = 0;
1786 ber_dupbv( &c[i].ldctl_value, &stValue );
1791 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1794 ctrls[i++] = extra_c++;
1796 for ( count = 0; count < unknown_ctrls_num; count++ ) {
1797 ctrls[i++] = &unknown_ctrls[count];
1801 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1803 if ( err != LDAP_OPT_SUCCESS ) {
1804 for ( j = 0; j < i; j++ ) {
1805 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1807 fprintf( stderr, "Could not set %scontrols\n",
1808 crit ? "critical " : "" );
1813 exit( EXIT_FAILURE );
1818 tool_check_abandon( LDAP *ld, int msgid )
1822 switch ( gotintr ) {
1824 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1825 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1826 rc, ldap_err2string( rc ) );
1830 rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
1831 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1832 rc, ldap_err2string( rc ) );
1836 /* just unbind, ignoring the request */
1844 print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
1849 tool_write_ldif( LDIF_PUT_COMMENT, "==> ",
1850 what->bv_val, what->bv_len );
1851 ber = ber_init( &ctrl->ldctl_value );
1852 if ( ber == NULL ) {
1856 } else if ( ber_scanf( ber, "{m{" /*}}*/, &bv ) == LBER_ERROR ) {
1861 tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1863 while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
1865 BerVarray vals = NULL;
1867 if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
1874 for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
1876 ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1877 bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
1880 ber_bvarray_free( vals );
1884 if ( ber != NULL ) {
1888 tool_write_ldif( LDIF_PUT_COMMENT, "<== ",
1889 what->bv_val, what->bv_len );
1895 print_preread( LDAP *ld, LDAPControl *ctrl )
1897 static struct berval what = BER_BVC( "preread" );
1899 return print_prepostread( ld, ctrl, &what );
1903 print_postread( LDAP *ld, LDAPControl *ctrl )
1905 static struct berval what = BER_BVC( "postread" );
1907 return print_prepostread( ld, ctrl, &what );
1911 print_paged_results( LDAP *ld, LDAPControl *ctrl )
1915 /* note: pr_cookie is being malloced; it's freed
1916 * the next time the control is sent, but the last
1917 * time it's not; we don't care too much, because
1918 * the last time an empty value is returned... */
1919 if ( ldap_parse_pageresponse_control( ld, ctrl, &estimate, &pr_cookie )
1926 /* FIXME: check buffer overflow */
1927 char buf[ BUFSIZ ], *ptr = buf;
1929 if ( estimate > 0 ) {
1930 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1931 "estimate=%d", estimate );
1934 if ( pr_cookie.bv_len > 0 ) {
1937 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
1938 pr_cookie.bv_len ) + 1;
1939 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
1941 bv.bv_len = lutil_b64_ntop(
1942 (unsigned char *) pr_cookie.bv_val,
1944 bv.bv_val, bv.bv_len );
1946 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1947 "%scookie=%s", ptr == buf ? "" : " ",
1950 ber_memfree( bv.bv_val );
1952 pr_morePagedResults = 1;
1955 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1956 "%scookie=", ptr == buf ? "" : " " );
1959 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1960 "pagedresults", buf, ptr - buf );
1967 print_sss( LDAP *ld, LDAPControl *ctrl )
1973 rc = ldap_parse_sortresponse_control( ld, ctrl, &err, &attr );
1974 if ( rc == LDAP_SUCCESS ) {
1976 rc = snprintf( buf, sizeof(buf), "(%d) %s%s%s",
1977 err, ldap_err2string(err), attr ? " " : "", attr ? attr : "" );
1979 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1980 "sortResult", buf, rc );
1987 print_vlv( LDAP *ld, LDAPControl *ctrl )
1993 rc = ldap_parse_vlvresponse_control( ld, ctrl, &vlvPos, &vlvCount,
1994 &vlvContext, &err );
1995 if ( rc == LDAP_SUCCESS ) {
1998 if ( vlvContext && vlvContext->bv_len > 0 ) {
1999 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
2000 vlvContext->bv_len ) + 1;
2001 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
2003 bv.bv_len = lutil_b64_ntop(
2004 (unsigned char *) vlvContext->bv_val,
2006 bv.bv_val, bv.bv_len );
2012 rc = snprintf( buf, sizeof(buf), "pos=%d count=%d context=%s (%d) %s",
2013 vlvPos, vlvCount, bv.bv_val,
2014 err, ldap_err2string(err));
2017 ber_memfree( bv.bv_val );
2019 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2020 "vlvResult", buf, rc );
2026 #ifdef LDAP_CONTROL_X_DEREF
2028 print_deref( LDAP *ld, LDAPControl *ctrl )
2030 LDAPDerefRes *drhead = NULL, *dr;
2033 rc = ldap_parse_derefresponse_control( ld, ctrl, &drhead );
2034 if ( rc != LDAP_SUCCESS ) {
2038 for ( dr = drhead; dr != NULL; dr = dr->next ) {
2043 len = strlen( dr->derefAttr ) + STRLENOF(": ");
2045 for ( dv = dr->attrVals; dv != NULL; dv = dv->next ) {
2046 if ( dv->vals != NULL ) {
2048 ber_len_t tlen = strlen(dv->type);
2050 for ( j = 0; dv->vals[ j ].bv_val != NULL; j++ ) {
2051 len += STRLENOF("<:=>;") + tlen + 4*((dv->vals[ j ].bv_len - 1)/3 + 1);
2055 len += dr->derefVal.bv_len + STRLENOF("\n");
2056 buf = ldap_memalloc( len + 1 );
2057 if ( buf == NULL ) {
2058 rc = LDAP_NO_MEMORY;
2063 ptr = lutil_strcopy( ptr, dr->derefAttr );
2066 for ( dv = dr->attrVals; dv != NULL; dv = dv->next ) {
2067 if ( dv->vals != NULL ) {
2069 for ( j = 0; dv->vals[ j ].bv_val != NULL; j++ ) {
2070 int k = ldif_is_not_printable( dv->vals[ j ].bv_val, dv->vals[ j ].bv_len );
2073 ptr = lutil_strcopy( ptr, dv->type );
2080 (unsigned char *) dv->vals[ j ].bv_val,
2081 dv->vals[ j ].bv_len,
2082 ptr, buf + len - ptr );
2087 ptr = lutil_memcopy( ptr, dv->vals[ j ].bv_val, dv->vals[ j ].bv_len );
2094 ptr = lutil_strncopy( ptr, dr->derefVal.bv_val, dr->derefVal.bv_len );
2097 assert( ptr <= buf + len );
2099 tool_write_ldif( LDIF_PUT_COMMENT, NULL, buf, ptr - buf);
2101 ldap_memfree( buf );
2107 ldap_derefresponse_free( drhead );
2113 #ifdef LDAP_CONTROL_X_WHATFAILED
2115 print_whatfailed( LDAP *ld, LDAPControl *ctrl )
2120 BerVarray bva = NULL;
2122 /* Create a BerElement from the berval returned in the control. */
2123 ber = ber_init( &ctrl->ldctl_value );
2125 if ( ber == NULL ) {
2126 return LDAP_NO_MEMORY;
2129 siz = sizeof(struct berval);
2130 tag = ber_scanf( ber, "[M]", &bva, &siz, 0 );
2131 if ( tag != LBER_ERROR ) {
2134 tool_write_ldif( LDIF_PUT_COMMENT, " what failed:", NULL, 0 );
2136 for ( i = 0; bva[i].bv_val != NULL; i++ ) {
2137 tool_write_ldif( LDIF_PUT_COMMENT, NULL, bva[i].bv_val, bva[i].bv_len );
2140 ldap_memfree( bva );
2150 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
2152 print_ppolicy( LDAP *ld, LDAPControl *ctrl )
2154 int expire = 0, grace = 0, rc;
2155 LDAPPasswordPolicyError pperr;
2157 rc = ldap_parse_passwordpolicy_control( ld, ctrl,
2158 &expire, &grace, &pperr );
2159 if ( rc == LDAP_SUCCESS ) {
2160 char buf[ BUFSIZ ], *ptr = buf;
2162 if ( expire != -1 ) {
2163 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2164 "expire=%d", expire );
2167 if ( grace != -1 ) {
2168 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2169 "%sgrace=%d", ptr == buf ? "" : " ", grace );
2172 if ( pperr != PP_noError ) {
2173 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2174 "%serror=%d (%s)", ptr == buf ? "" : " ",
2176 ldap_passwordpolicy_err2txt( pperr ) );
2179 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2180 "ppolicy", buf, ptr - buf );
2187 void tool_print_ctrls(
2189 LDAPControl **ctrls )
2194 for ( i = 0; ctrls[i] != NULL; i++ ) {
2195 /* control: OID criticality base64value */
2196 struct berval b64 = BER_BVNULL;
2201 /* FIXME: there might be cases where a control has NULL OID;
2202 * this makes little sense, especially when returned by the
2203 * server, but libldap happily allows it */
2204 if ( ctrls[i]->ldctl_oid == NULL ) {
2209 len += strlen( ctrls[i]->ldctl_oid );
2211 /* add enough for space after OID and the critical value itself */
2212 len += ctrls[i]->ldctl_iscritical
2213 ? sizeof("true") : sizeof("false");
2215 /* convert to base64 */
2216 if ( !BER_BVISNULL( &ctrls[i]->ldctl_value ) ) {
2217 b64.bv_len = LUTIL_BASE64_ENCODE_LEN(
2218 ctrls[i]->ldctl_value.bv_len ) + 1;
2219 b64.bv_val = ber_memalloc( b64.bv_len + 1 );
2221 b64.bv_len = lutil_b64_ntop(
2222 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
2223 ctrls[i]->ldctl_value.bv_len,
2224 b64.bv_val, b64.bv_len );
2228 len += 1 + b64.bv_len;
2231 ptr = str = malloc( len + 1 );
2233 ptr = lutil_strcopy( ptr, ": " );
2235 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_oid );
2236 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_iscritical
2237 ? " true" : " false" );
2240 ptr = lutil_strcopy( ptr, " " );
2241 ptr = lutil_strcopy( ptr, b64.bv_val );
2245 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2246 "control", str, len );
2251 ber_memfree( b64.bv_val );
2254 /* known controls */
2255 for ( j = 0; tool_ctrl_response[j].oid != NULL; j++ ) {
2256 if ( strcmp( tool_ctrl_response[j].oid, ctrls[i]->ldctl_oid ) == 0 ) {
2257 if ( !tool_ctrl_response[j].mask & tool_type ) {
2258 /* this control should not appear
2259 * with this tool; warning? */
2265 if ( tool_ctrl_response[j].oid != NULL && tool_ctrl_response[j].func ) {
2266 (void)tool_ctrl_response[j].func( ld, ctrls[i] );
2272 tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
2276 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
2280 fputs( ldif, stdout );
2281 ber_memfree( ldif );
2287 tool_is_oid( const char *s )
2291 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2295 for ( ; s[ 0 ]; s++ ) {
2296 if ( s[ 0 ] == '.' ) {
2297 if ( s[ 1 ] == '\0' ) {
2304 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2308 if ( first == 1 && s[ 0 ] == '0' && s[ 1 ] != '.' ) {