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 );
1134 tool_conn_setup( int dont, void (*private_setup)( LDAP * ) )
1139 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
1140 != LBER_OPT_SUCCESS )
1143 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
1145 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
1146 != LDAP_OPT_SUCCESS )
1149 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
1154 (void) SIGNAL( SIGPIPE, SIG_IGN );
1158 SIGNAL( SIGINT, do_sig );
1164 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
1167 memset( &url, 0, sizeof(url));
1169 url.lud_scheme = "ldap";
1170 url.lud_host = ldaphost;
1171 url.lud_port = ldapport;
1172 url.lud_scope = LDAP_SCOPE_DEFAULT;
1174 ldapuri = ldap_url_desc2str( &url );
1176 } else if ( ldapuri != NULL ) {
1177 LDAPURLDesc *ludlist, **ludp;
1181 rc = ldap_url_parselist( &ludlist, ldapuri );
1182 if ( rc != LDAP_URL_SUCCESS ) {
1184 "Could not parse LDAP URI(s)=%s (%d)\n",
1186 exit( EXIT_FAILURE );
1189 for ( ludp = &ludlist; *ludp != NULL; ) {
1190 LDAPURLDesc *lud = *ludp;
1193 if ( lud->lud_dn != NULL && lud->lud_dn[ 0 ] != '\0' &&
1194 ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) )
1196 /* if no host but a DN is provided,
1197 * use DNS SRV to gather the host list
1198 * and turn it into a list of URIs
1199 * using the scheme provided */
1200 char *domain = NULL,
1204 len_proto = strlen( lud->lud_scheme );
1206 if ( ldap_dn2domain( lud->lud_dn, &domain )
1210 "DNS SRV: Could not turn "
1211 "DN=\"%s\" into a domain\n",
1216 rc = ldap_domain2hostlist( domain, &hostlist );
1219 "DNS SRV: Could not turn "
1220 "domain=%s into a hostlist\n",
1225 hosts = ldap_str2charray( hostlist, " " );
1226 if ( hosts == NULL ) {
1228 "DNS SRV: Could not parse "
1229 "hostlist=\"%s\"\n",
1234 for ( i = 0; hosts[ i ] != NULL; i++ )
1237 tmp = (char **)ber_memrealloc( urls, sizeof( char * ) * ( nurls + i + 1 ) );
1238 if ( tmp == NULL ) {
1240 "DNS SRV: out of memory?\n" );
1244 urls[ nurls ] = NULL;
1246 for ( i = 0; hosts[ i ] != NULL; i++ ) {
1247 size_t len = len_proto
1249 + strlen( hosts[ i ] )
1252 urls[ nurls + i + 1 ] = NULL;
1253 urls[ nurls + i ] = (char *)malloc( sizeof( char ) * len );
1254 if ( urls[ nurls + i ] == NULL ) {
1256 "DNS SRV: out of memory?\n" );
1260 snprintf( urls[ nurls + i ], len, "%s://%s",
1261 lud->lud_scheme, hosts[ i ] );
1266 ber_memvfree( (void **)hosts );
1267 ber_memfree( hostlist );
1268 ber_memfree( domain );
1271 tmp = (char **)ber_memrealloc( urls, sizeof( char * ) * ( nurls + 2 ) );
1272 if ( tmp == NULL ) {
1274 "DNS SRV: out of memory?\n" );
1278 urls[ nurls + 1 ] = NULL;
1280 urls[ nurls ] = ldap_url_desc2str( lud );
1281 if ( urls[ nurls ] == NULL ) {
1283 "DNS SRV: out of memory?\n" );
1289 *ludp = lud->lud_next;
1291 lud->lud_next = NULL;
1292 ldap_free_urldesc( lud );
1295 if ( ludlist != NULL ) {
1296 ldap_free_urllist( ludlist );
1297 exit( EXIT_FAILURE );
1299 } else if ( urls == NULL ) {
1300 exit( EXIT_FAILURE );
1303 ldap_memfree( ldapuri );
1304 ldapuri = ldap_charray2str( urls, " " );
1305 ber_memvfree( (void **)urls );
1309 fprintf( stderr, "ldap_initialize( %s )\n",
1310 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
1312 rc = ldap_initialize( &ld, ldapuri );
1313 if( rc != LDAP_SUCCESS ) {
1315 "Could not create LDAP session handle for URI=%s (%d): %s\n",
1316 ldapuri, rc, ldap_err2string(rc) );
1317 exit( EXIT_FAILURE );
1320 if( private_setup ) private_setup( ld );
1323 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
1324 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
1326 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
1327 referrals ? "on" : "off" );
1328 exit( EXIT_FAILURE );
1331 #ifdef HAVE_CYRUS_SASL
1333 if( ldap_set_option( ld, LDAP_OPT_X_SASL_NOCANON,
1334 nocanon ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
1336 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_NOCANON %s\n",
1337 nocanon ? "on" : "off" );
1338 exit( EXIT_FAILURE );
1341 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
1342 != LDAP_OPT_SUCCESS )
1344 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
1346 exit( EXIT_FAILURE );
1350 rc = ldap_start_tls_s( ld, NULL, NULL );
1351 if ( rc != LDAP_SUCCESS ) {
1353 ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&msg);
1354 tool_perror( "ldap_start_tls", rc, NULL, NULL, msg, NULL );
1356 if ( use_tls > 1 ) {
1357 exit( EXIT_FAILURE );
1362 if ( nettimeout.tv_sec > 0 ) {
1363 if ( ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &nettimeout )
1364 != LDAP_OPT_SUCCESS )
1366 fprintf( stderr, "Could not set LDAP_OPT_NETWORK_TIMEOUT %ld\n",
1367 (long)nettimeout.tv_sec );
1368 exit( EXIT_FAILURE );
1378 tool_bind( LDAP *ld )
1380 LDAPControl **sctrlsp = NULL;
1381 LDAPControl *sctrls[3];
1382 LDAPControl sctrl[3];
1385 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1388 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1389 c.ldctl_value.bv_val = NULL;
1390 c.ldctl_value.bv_len = 0;
1391 c.ldctl_iscritical = 0;
1393 sctrls[nsctrls] = &sctrl[nsctrls];
1394 sctrls[++nsctrls] = NULL;
1398 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1399 if ( sessionTracking ) {
1402 if (stValue.bv_val == NULL && st_value( ld, &stValue ) ) {
1403 exit( EXIT_FAILURE );
1406 c.ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1407 c.ldctl_iscritical = 0;
1408 ber_dupbv( &c.ldctl_value, &stValue );
1411 sctrls[nsctrls] = &sctrl[nsctrls];
1412 sctrls[++nsctrls] = NULL;
1414 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1420 assert( nsctrls < (int) (sizeof(sctrls)/sizeof(sctrls[0])) );
1422 if ( pw_file || want_bindpw ) {
1423 assert( passwd.bv_val == NULL && passwd.bv_len == 0 );
1426 if ( lutil_get_filed_password( pw_file, &passwd ) ) {
1427 exit( EXIT_FAILURE );
1431 char *pw = getpassphrase( _("Enter LDAP Password: ") );
1433 passwd.bv_val = ber_strdup( pw );
1434 passwd.bv_len = strlen( passwd.bv_val );
1439 if ( authmethod == LDAP_AUTH_SASL ) {
1440 #ifdef HAVE_CYRUS_SASL
1444 if( sasl_secprops != NULL ) {
1445 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
1446 (void *) sasl_secprops );
1448 if( rc != LDAP_OPT_SUCCESS ) {
1450 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
1452 exit( LDAP_LOCAL_ERROR );
1456 defaults = lutil_sasl_defaults( ld,
1463 rc = ldap_sasl_interactive_bind_s( ld, binddn, sasl_mech,
1465 NULL, sasl_flags, lutil_sasl_interact, defaults );
1467 lutil_sasl_freedefs( defaults );
1468 if( rc != LDAP_SUCCESS ) {
1470 ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&msg);
1471 tool_perror( "ldap_sasl_interactive_bind_s",
1472 rc, NULL, NULL, msg, NULL );
1477 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1478 exit( LDAP_NOT_SUPPORTED );
1482 LDAPMessage *result;
1483 LDAPControl **ctrls;
1485 char *matched = NULL;
1493 rc = ldap_sasl_bind( ld, binddn, LDAP_SASL_SIMPLE, &passwd,
1494 sctrlsp, NULL, &msgid );
1495 if ( msgid == -1 ) {
1496 tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
1497 NULL, NULL, NULL, NULL );
1502 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result );
1504 tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
1505 exit( LDAP_LOCAL_ERROR );
1509 tool_perror( "ldap_result", LDAP_TIMEOUT, NULL, NULL, NULL, NULL );
1510 exit( LDAP_LOCAL_ERROR );
1513 rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
1515 if ( rc != LDAP_SUCCESS ) {
1516 tool_perror( "ldap_bind parse result", rc, NULL, matched, info, refs );
1517 exit( LDAP_LOCAL_ERROR );
1520 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1521 if ( ctrls && ppolicy ) {
1523 int expire, grace, len = 0;
1524 LDAPPasswordPolicyError pErr = -1;
1526 ctrl = ldap_control_find( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
1529 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
1530 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1532 if ( pErr != PP_noError ){
1535 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1536 len = strlen( msgbuf );
1538 if ( expire >= 0 ) {
1539 sprintf( msgbuf+len,
1540 " (Password expires in %d seconds)",
1542 } else if ( grace >= 0 ) {
1543 sprintf( msgbuf+len,
1544 " (Password expired, %d grace logins remain)",
1552 ldap_controls_free( ctrls );
1555 if ( err != LDAP_SUCCESS
1557 || ( matched && matched[ 0 ] )
1558 || ( info && info[ 0 ] )
1561 tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1563 if( matched ) ber_memfree( matched );
1564 if( info ) ber_memfree( info );
1565 if( refs ) ber_memvfree( (void **)refs );
1567 if ( err != LDAP_SUCCESS ) exit( err );
1573 tool_unbind( LDAP *ld )
1575 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1577 if ( err != LDAP_OPT_SUCCESS ) {
1578 fprintf( stderr, "Could not unset controls\n");
1581 (void) ldap_unbind_ext( ld, NULL, NULL );
1585 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1587 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1589 int i = 0, j, crit = 0, err;
1590 LDAPControl c[16], **ctrls;
1594 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1596 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1600 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1605 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1607 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1608 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1610 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1612 || unknown_ctrls_num ) )
1617 ctrls = (LDAPControl**) malloc(sizeof(c) + (count + unknown_ctrls_num + 1)*sizeof(LDAPControl*));
1618 if ( ctrls == NULL ) {
1619 fprintf( stderr, "No memory\n" );
1620 exit( EXIT_FAILURE );
1624 if ( BER_BVISNULL( &assertionvalue ) ) {
1625 err = ldap_create_assertion_control_value( ld,
1626 assertion, &assertionvalue );
1629 "Unable to create assertion value "
1630 "\"%s\" (%d)\n", assertion, err );
1634 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1635 c[i].ldctl_value = assertionvalue;
1636 c[i].ldctl_iscritical = assertctl > 1;
1642 c[i].ldctl_value.bv_val = authzid;
1643 c[i].ldctl_value.bv_len = strlen( authzid );
1644 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1645 c[i].ldctl_iscritical = 1;
1650 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1651 /* NOTE: doesn't need an extra count because it's incompatible
1654 BerElementBuffer berbuf;
1655 BerElement *ber = (BerElement *)&berbuf;
1657 ber_init2( ber, NULL, LBER_USE_DER );
1659 if ( ber_printf( ber, "s", proxydn ) == -1 ) {
1660 exit( EXIT_FAILURE );
1663 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1664 exit( EXIT_FAILURE );
1667 c[i].ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1668 c[i].ldctl_iscritical = 1;
1672 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1675 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1676 BER_BVZERO( &c[i].ldctl_value );
1677 c[i].ldctl_iscritical = manageDIT > 1;
1682 if ( manageDSAit ) {
1683 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1684 BER_BVZERO( &c[i].ldctl_value );
1685 c[i].ldctl_iscritical = manageDSAit > 1;
1691 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1692 BER_BVZERO( &c[i].ldctl_value );
1693 c[i].ldctl_iscritical = noop > 1;
1698 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1700 c[i].ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1701 BER_BVZERO( &c[i].ldctl_value );
1702 c[i].ldctl_iscritical = 0;
1709 BerElementBuffer berbuf;
1710 BerElement *ber = (BerElement *)&berbuf;
1711 char **attrs = NULL;
1713 if( preread_attrs ) {
1714 attrs = ldap_str2charray( preread_attrs, "," );
1717 ber_init2( ber, NULL, LBER_USE_DER );
1719 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1720 fprintf( stderr, "preread attrs encode failed.\n" );
1721 exit( EXIT_FAILURE );
1724 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1726 fprintf( stderr, "preread flatten failed (%d)\n", err );
1727 exit( EXIT_FAILURE );
1730 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1731 c[i].ldctl_iscritical = preread > 1;
1735 if( attrs ) ldap_charray_free( attrs );
1739 BerElementBuffer berbuf;
1740 BerElement *ber = (BerElement *)&berbuf;
1741 char **attrs = NULL;
1743 if( postread_attrs ) {
1744 attrs = ldap_str2charray( postread_attrs, "," );
1747 ber_init2( ber, NULL, LBER_USE_DER );
1749 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1750 fprintf( stderr, "postread attrs encode failed.\n" );
1751 exit( EXIT_FAILURE );
1754 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1756 fprintf( stderr, "postread flatten failed (%d)\n", err );
1757 exit( EXIT_FAILURE );
1760 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1761 c[i].ldctl_iscritical = postread > 1;
1765 if( attrs ) ldap_charray_free( attrs );
1768 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1770 if ( chainingResolve > -1 ) {
1771 BerElementBuffer berbuf;
1772 BerElement *ber = (BerElement *)&berbuf;
1774 ber_init2( ber, NULL, LBER_USE_DER );
1776 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1779 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1780 exit( EXIT_FAILURE );
1783 if ( chainingContinuation > -1 ) {
1784 err = ber_printf( ber, "e", chainingContinuation );
1787 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1788 exit( EXIT_FAILURE );
1792 err = ber_printf( ber, /* { */ "N}" );
1795 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1796 exit( EXIT_FAILURE );
1799 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1800 exit( EXIT_FAILURE );
1804 BER_BVZERO( &c[i].ldctl_value );
1807 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1808 c[i].ldctl_iscritical = chaining > 1;
1812 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1814 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1815 if ( sessionTracking ) {
1816 if ( stValue.bv_val == NULL && st_value( ld, &stValue ) ) {
1817 exit( EXIT_FAILURE );
1820 c[i].ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1821 c[i].ldctl_iscritical = 0;
1822 ber_dupbv( &c[i].ldctl_value, &stValue );
1827 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1830 ctrls[i++] = extra_c++;
1832 for ( count = 0; count < unknown_ctrls_num; count++ ) {
1833 ctrls[i++] = &unknown_ctrls[count];
1837 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1839 if ( err != LDAP_OPT_SUCCESS ) {
1840 for ( j = 0; j < i; j++ ) {
1841 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1843 fprintf( stderr, "Could not set %scontrols\n",
1844 crit ? "critical " : "" );
1849 exit( EXIT_FAILURE );
1854 tool_check_abandon( LDAP *ld, int msgid )
1858 switch ( gotintr ) {
1860 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1861 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1862 rc, ldap_err2string( rc ) );
1866 rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
1867 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1868 rc, ldap_err2string( rc ) );
1872 /* just unbind, ignoring the request */
1880 print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
1885 tool_write_ldif( LDIF_PUT_COMMENT, "==> ",
1886 what->bv_val, what->bv_len );
1887 ber = ber_init( &ctrl->ldctl_value );
1888 if ( ber == NULL ) {
1892 } else if ( ber_scanf( ber, "{m{" /*}}*/, &bv ) == LBER_ERROR ) {
1897 tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1899 while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
1901 BerVarray vals = NULL;
1904 if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
1914 str = malloc( bv.bv_len + STRLENOF(": ") + 1 );
1917 ptr = lutil_strncopy( ptr, bv.bv_val, bv.bv_len );
1918 ptr = lutil_strcopy( ptr, ": " );
1921 for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
1923 ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1924 ldif ? str : bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
1927 ber_bvarray_free( vals );
1928 if ( str ) free( str );
1932 if ( ber != NULL ) {
1936 tool_write_ldif( LDIF_PUT_COMMENT, "<== ",
1937 what->bv_val, what->bv_len );
1943 print_preread( LDAP *ld, LDAPControl *ctrl )
1945 static struct berval what = BER_BVC( "preread" );
1947 return print_prepostread( ld, ctrl, &what );
1951 print_postread( LDAP *ld, LDAPControl *ctrl )
1953 static struct berval what = BER_BVC( "postread" );
1955 return print_prepostread( ld, ctrl, &what );
1959 print_paged_results( LDAP *ld, LDAPControl *ctrl )
1963 /* note: pr_cookie is being malloced; it's freed
1964 * the next time the control is sent, but the last
1965 * time it's not; we don't care too much, because
1966 * the last time an empty value is returned... */
1967 if ( ldap_parse_pageresponse_control( ld, ctrl, &estimate, &pr_cookie )
1974 /* FIXME: check buffer overflow */
1975 char buf[ BUFSIZ ], *ptr = buf;
1977 if ( estimate > 0 ) {
1978 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1979 "estimate=%d", estimate );
1982 if ( pr_cookie.bv_len > 0 ) {
1985 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
1986 pr_cookie.bv_len ) + 1;
1987 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
1989 bv.bv_len = lutil_b64_ntop(
1990 (unsigned char *) pr_cookie.bv_val,
1992 bv.bv_val, bv.bv_len );
1994 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1995 "%scookie=%s", ptr == buf ? "" : " ",
1998 ber_memfree( bv.bv_val );
2000 pr_morePagedResults = 1;
2003 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2004 "%scookie=", ptr == buf ? "" : " " );
2007 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2008 ldif ? "pagedresults: " : "pagedresults",
2016 print_sss( LDAP *ld, LDAPControl *ctrl )
2022 rc = ldap_parse_sortresponse_control( ld, ctrl, &err, &attr );
2023 if ( rc == LDAP_SUCCESS ) {
2025 rc = snprintf( buf, sizeof(buf), "(%d) %s%s%s",
2026 err, ldap_err2string(err), attr ? " " : "", attr ? attr : "" );
2028 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2029 ldif ? "sortResult: " : "sortResult", buf, rc );
2036 print_vlv( LDAP *ld, LDAPControl *ctrl )
2042 rc = ldap_parse_vlvresponse_control( ld, ctrl, &vlvPos, &vlvCount,
2043 &vlvContext, &err );
2044 if ( rc == LDAP_SUCCESS ) {
2047 if ( vlvContext && vlvContext->bv_len > 0 ) {
2048 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
2049 vlvContext->bv_len ) + 1;
2050 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
2052 bv.bv_len = lutil_b64_ntop(
2053 (unsigned char *) vlvContext->bv_val,
2055 bv.bv_val, bv.bv_len );
2061 rc = snprintf( buf, sizeof(buf), "pos=%d count=%d context=%s (%d) %s",
2062 vlvPos, vlvCount, bv.bv_val,
2063 err, ldap_err2string(err));
2066 ber_memfree( bv.bv_val );
2068 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2069 ldif ? "vlvResult" : "vlvResult", buf, rc );
2075 #ifdef LDAP_CONTROL_X_DEREF
2077 print_deref( LDAP *ld, LDAPControl *ctrl )
2079 LDAPDerefRes *drhead = NULL, *dr;
2082 rc = ldap_parse_derefresponse_control( ld, ctrl, &drhead );
2083 if ( rc != LDAP_SUCCESS ) {
2087 for ( dr = drhead; dr != NULL; dr = dr->next ) {
2092 len = strlen( dr->derefAttr ) + STRLENOF(": ");
2094 for ( dv = dr->attrVals; dv != NULL; dv = dv->next ) {
2095 if ( dv->vals != NULL ) {
2097 ber_len_t tlen = strlen(dv->type);
2099 for ( j = 0; dv->vals[ j ].bv_val != NULL; j++ ) {
2100 len += STRLENOF("<:=>;") + tlen + 4*((dv->vals[ j ].bv_len - 1)/3 + 1);
2104 len += dr->derefVal.bv_len + STRLENOF("\n");
2105 buf = ldap_memalloc( len + 1 );
2106 if ( buf == NULL ) {
2107 rc = LDAP_NO_MEMORY;
2112 ptr = lutil_strcopy( ptr, dr->derefAttr );
2115 for ( dv = dr->attrVals; dv != NULL; dv = dv->next ) {
2116 if ( dv->vals != NULL ) {
2118 for ( j = 0; dv->vals[ j ].bv_val != NULL; j++ ) {
2119 int k = ldif_is_not_printable( dv->vals[ j ].bv_val, dv->vals[ j ].bv_len );
2122 ptr = lutil_strcopy( ptr, dv->type );
2129 (unsigned char *) dv->vals[ j ].bv_val,
2130 dv->vals[ j ].bv_len,
2131 ptr, buf + len - ptr );
2136 ptr = lutil_memcopy( ptr, dv->vals[ j ].bv_val, dv->vals[ j ].bv_len );
2143 ptr = lutil_strncopy( ptr, dr->derefVal.bv_val, dr->derefVal.bv_len );
2146 assert( ptr <= buf + len );
2148 tool_write_ldif( LDIF_PUT_COMMENT, NULL, buf, ptr - buf);
2150 ldap_memfree( buf );
2156 ldap_derefresponse_free( drhead );
2162 #ifdef LDAP_CONTROL_X_WHATFAILED
2164 print_whatfailed( LDAP *ld, LDAPControl *ctrl )
2169 BerVarray bva = NULL;
2171 /* Create a BerElement from the berval returned in the control. */
2172 ber = ber_init( &ctrl->ldctl_value );
2174 if ( ber == NULL ) {
2175 return LDAP_NO_MEMORY;
2178 siz = sizeof(struct berval);
2179 tag = ber_scanf( ber, "[M]", &bva, &siz, 0 );
2180 if ( tag != LBER_ERROR ) {
2183 tool_write_ldif( LDIF_PUT_COMMENT, " what failed:", NULL, 0 );
2185 for ( i = 0; bva[i].bv_val != NULL; i++ ) {
2186 tool_write_ldif( LDIF_PUT_COMMENT, NULL, bva[i].bv_val, bva[i].bv_len );
2189 ldap_memfree( bva );
2199 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
2201 print_ppolicy( LDAP *ld, LDAPControl *ctrl )
2203 int expire = 0, grace = 0, rc;
2204 LDAPPasswordPolicyError pperr;
2206 rc = ldap_parse_passwordpolicy_control( ld, ctrl,
2207 &expire, &grace, &pperr );
2208 if ( rc == LDAP_SUCCESS ) {
2209 char buf[ BUFSIZ ], *ptr = buf;
2211 if ( expire != -1 ) {
2212 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2213 "expire=%d", expire );
2216 if ( grace != -1 ) {
2217 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2218 "%sgrace=%d", ptr == buf ? "" : " ", grace );
2221 if ( pperr != PP_noError ) {
2222 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2223 "%serror=%d (%s)", ptr == buf ? "" : " ",
2225 ldap_passwordpolicy_err2txt( pperr ) );
2228 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2229 ldif ? "ppolicy: " : "ppolicy", buf, ptr - buf );
2236 void tool_print_ctrls(
2238 LDAPControl **ctrls )
2243 for ( i = 0; ctrls[i] != NULL; i++ ) {
2244 /* control: OID criticality base64value */
2245 struct berval b64 = BER_BVNULL;
2250 /* FIXME: there might be cases where a control has NULL OID;
2251 * this makes little sense, especially when returned by the
2252 * server, but libldap happily allows it */
2253 if ( ctrls[i]->ldctl_oid == NULL ) {
2258 len += strlen( ctrls[i]->ldctl_oid );
2260 /* add enough for space after OID and the critical value itself */
2261 len += ctrls[i]->ldctl_iscritical
2262 ? sizeof("true") : sizeof("false");
2264 /* convert to base64 */
2265 if ( !BER_BVISNULL( &ctrls[i]->ldctl_value ) ) {
2266 b64.bv_len = LUTIL_BASE64_ENCODE_LEN(
2267 ctrls[i]->ldctl_value.bv_len ) + 1;
2268 b64.bv_val = ber_memalloc( b64.bv_len + 1 );
2270 b64.bv_len = lutil_b64_ntop(
2271 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
2272 ctrls[i]->ldctl_value.bv_len,
2273 b64.bv_val, b64.bv_len );
2277 len += 1 + b64.bv_len;
2280 ptr = str = malloc( len + 1 );
2282 ptr = lutil_strcopy( ptr, ": " );
2284 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_oid );
2285 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_iscritical
2286 ? " true" : " false" );
2289 ptr = lutil_strcopy( ptr, " " );
2290 ptr = lutil_strcopy( ptr, b64.bv_val );
2294 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2295 "control", str, len );
2300 ber_memfree( b64.bv_val );
2303 /* known controls */
2304 for ( j = 0; tool_ctrl_response[j].oid != NULL; j++ ) {
2305 if ( strcmp( tool_ctrl_response[j].oid, ctrls[i]->ldctl_oid ) == 0 ) {
2306 if ( !tool_ctrl_response[j].mask & tool_type ) {
2307 /* this control should not appear
2308 * with this tool; warning? */
2314 if ( tool_ctrl_response[j].oid != NULL && tool_ctrl_response[j].func ) {
2315 (void)tool_ctrl_response[j].func( ld, ctrls[i] );
2321 tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
2325 if (( ldif = ldif_put_wrap( type, name, value, vallen, ldif_wrap )) == NULL ) {
2329 fputs( ldif, stdout );
2330 ber_memfree( ldif );
2336 tool_is_oid( const char *s )
2340 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2344 for ( ; s[ 0 ]; s++ ) {
2345 if ( s[ 0 ] == '.' ) {
2346 if ( s[ 1 ] == '\0' ) {
2353 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2357 if ( first == 1 && s[ 0 ] == '0' && s[ 1 ] != '.' ) {