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;
101 /* support deprecated early version of proxyAuthz */
102 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ "2.16.840.1.113730.3.4.12"
103 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
104 char *proxydn = NULL;
105 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
111 static char *preread_attrs = NULL;
113 static char *postread_attrs = NULL;
114 ber_int_t pr_morePagedResults = 1;
115 struct berval pr_cookie = { 0, NULL };
116 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
118 static int chainingResolve = -1;
119 static int chainingContinuation = -1;
120 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
121 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
122 static int sessionTracking = 0;
123 static char *sessionTrackingName;
124 struct berval stValue;
125 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
128 struct berval *vlvContext;
131 LDAPControl *unknown_ctrls = NULL;
132 int unknown_ctrls_num = 0;
135 struct timeval nettimeout = { -1 , 0 };
137 typedef int (*print_ctrl_fn)( LDAP *ld, LDAPControl *ctrl );
139 static int print_preread( LDAP *ld, LDAPControl *ctrl );
140 static int print_postread( LDAP *ld, LDAPControl *ctrl );
141 static int print_paged_results( LDAP *ld, LDAPControl *ctrl );
142 #ifdef LDAP_CONTROL_AUTHZID_RESPONSE
143 static int print_authzid( LDAP *ld, LDAPControl *ctrl );
145 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
146 static int print_ppolicy( LDAP *ld, LDAPControl *ctrl );
148 static int print_sss( LDAP *ld, LDAPControl *ctrl );
149 static int print_vlv( LDAP *ld, LDAPControl *ctrl );
150 #ifdef LDAP_CONTROL_X_DEREF
151 static int print_deref( LDAP *ld, LDAPControl *ctrl );
153 #ifdef LDAP_CONTROL_X_WHATFAILED
154 static int print_whatfailed( LDAP *ld, LDAPControl *ctrl );
157 static struct tool_ctrls_t {
161 } tool_ctrl_response[] = {
162 { LDAP_CONTROL_PRE_READ, TOOL_ALL, print_preread },
163 { LDAP_CONTROL_POST_READ, TOOL_ALL, print_postread },
164 { LDAP_CONTROL_PAGEDRESULTS, TOOL_SEARCH, print_paged_results },
165 #ifdef LDAP_CONTROL_AUTHZID_RESPONSE
166 /* this is generally deprecated in favor of LDAP WhoAmI? operation, hence only supported as a VC inner control */
167 { LDAP_CONTROL_AUTHZID_RESPONSE, TOOL_VC, print_authzid },
169 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
170 { LDAP_CONTROL_PASSWORDPOLICYRESPONSE, TOOL_ALL, print_ppolicy },
172 { LDAP_CONTROL_SORTRESPONSE, TOOL_SEARCH, print_sss },
173 { LDAP_CONTROL_VLVRESPONSE, TOOL_SEARCH, print_vlv },
174 #ifdef LDAP_CONTROL_X_DEREF
175 { LDAP_CONTROL_X_DEREF, TOOL_SEARCH, print_deref },
177 #ifdef LDAP_CONTROL_X_WHATFAILED
178 { LDAP_CONTROL_X_WHATFAILED, TOOL_ALL, print_whatfailed },
184 enum { Intr_None = 0, Intr_Abandon, Intr_Cancel, Intr_Ignore };
185 static volatile sig_atomic_t gotintr, abcan;
188 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
190 st_value( LDAP *ld, struct berval *value )
192 char *ip = NULL, *name = NULL;
193 struct berval id = { 0 };
194 char namebuf[ MAXHOSTNAMELEN ];
196 if ( gethostname( namebuf, sizeof( namebuf ) ) == 0 ) {
202 h = gethostbyname( name );
204 AC_MEMCPY( &addr, h->h_addr, sizeof( addr ) );
205 ip = inet_ntoa( addr );
209 if ( sessionTrackingName != NULL ) {
210 ber_str2bv( sessionTrackingName , 0, 0, &id );
212 #ifdef HAVE_CYRUS_SASL
213 if ( sasl_authz_id != NULL ) {
214 ber_str2bv( sasl_authz_id, 0, 0, &id );
216 } else if ( sasl_authc_id != NULL ) {
217 ber_str2bv( sasl_authc_id, 0, 0, &id );
220 #endif /* HAVE_CYRUS_SASL */
221 if ( binddn != NULL ) {
222 ber_str2bv( binddn, 0, 0, &id );
225 if ( ldap_create_session_tracking_value( ld,
226 ip, name, LDAP_CONTROL_X_SESSION_TRACKING_USERNAME,
229 fprintf( stderr, _("Session tracking control encoding error!\n") );
235 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
244 tool_init( tool_type_t type )
247 ldap_pvt_setlocale(LC_MESSAGES, "");
248 ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
249 ldap_pvt_textdomain(OPENLDAP_PACKAGE);
255 #ifdef HAVE_CYRUS_SASL
259 ldap_pvt_tls_destroy();
262 if ( ldapuri != NULL ) {
263 ber_memfree( ldapuri );
267 if ( pr_cookie.bv_val != NULL ) {
268 ber_memfree( pr_cookie.bv_val );
269 BER_BVZERO( &pr_cookie );
272 if ( binddn != NULL ) {
273 ber_memfree( binddn );
277 if ( passwd.bv_val != NULL ) {
278 ber_memfree( passwd.bv_val );
279 BER_BVZERO( &passwd );
282 #ifdef HAVE_CYRUS_SASL
283 if ( sasl_mech != NULL ) {
284 ber_memfree( sasl_mech );
287 #endif /* HAVE_CYRUS_SASL */
289 if ( infile != NULL ) {
290 ber_memfree( infile );
295 ber_memfree( assertion );
300 ber_memfree( authzid );
305 ber_memfree( proxydn );
309 if ( preread_attrs ) {
310 ber_memfree( preread_attrs );
311 preread_attrs = NULL;
314 if ( postread_attrs ) {
315 ber_memfree( postread_attrs );
316 postread_attrs = NULL;
319 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
320 if ( !BER_BVISNULL( &stValue ) ) {
321 ber_memfree( stValue.bv_val );
322 BER_BVZERO( &stValue );
325 if ( sessionTrackingName ) {
326 ber_memfree( sessionTrackingName );
327 sessionTrackingName = NULL;
329 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
333 tool_common_usage( void )
335 static const char *const descriptions[] = {
336 N_(" -d level set LDAP debugging level to `level'\n"),
337 N_(" -D binddn bind DN\n"),
338 N_(" -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
339 N_(" [!]assert=<filter> (RFC 4528; a RFC 4515 Filter string)\n")
340 N_(" [!]authzid=<authzid> (RFC 4370; \"dn:<dn>\" or \"u:<user>\")\n")
341 N_(" [!]bauthzid (RFC 3829)\n")
342 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
344 /* non-advertized support for proxyDN */
345 N_(" [!]proxydn=<dn> (a RFC 4514 DN string)\n")
348 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
349 N_(" [!]chaining[=<resolveBehavior>[/<continuationBehavior>]]\n")
350 N_(" one of \"chainingPreferred\", \"chainingRequired\",\n")
351 N_(" \"referralsPreferred\", \"referralsRequired\"\n")
352 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
353 N_(" [!]manageDSAit (RFC 3296)\n")
355 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
358 N_(" [!]postread[=<attrs>] (RFC 4527; comma-separated attr list)\n")
359 N_(" [!]preread[=<attrs>] (RFC 4527; comma-separated attr list)\n")
361 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
362 N_(" [!]sessiontracking[=<username>]\n")
363 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
364 N_(" abandon, cancel, ignore (SIGINT sends abandon/cancel,\n"
365 " or ignores response; if critical, doesn't wait for SIGINT.\n"
366 " not really controls)\n")
367 N_(" -h host LDAP server\n"),
368 N_(" -H URI LDAP Uniform Resource Identifier(s)\n"),
369 N_(" -I use SASL Interactive mode\n"),
370 N_(" -n show what would be done but don't actually do it\n"),
371 N_(" -N do not use reverse DNS to canonicalize SASL host name\n"),
372 N_(" -O props SASL security properties\n"),
373 N_(" -o <opt>[=<optparam] general options\n"),
374 N_(" nettimeout=<timeout> (in seconds, or \"none\" or \"max\")\n"),
375 N_(" ldif-wrap=<width> (in columns, or \"no\" for no wrapping)\n"),
376 N_(" -p port port on LDAP server\n"),
377 N_(" -Q use SASL Quiet mode\n"),
378 N_(" -R realm SASL realm\n"),
379 N_(" -U authcid SASL authentication identity\n"),
380 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
381 N_(" -V print version info (-VV only)\n"),
382 N_(" -w passwd bind password (for simple authentication)\n"),
383 N_(" -W prompt for bind password\n"),
384 N_(" -x Simple authentication\n"),
385 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
386 N_(" -y file Read password from file\n"),
387 N_(" -Y mech SASL mechanism\n"),
388 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
391 const char *const *cpp;
393 fputs( _("Common options:\n"), stderr );
394 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
395 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
396 fputs( _(*cpp), stderr );
411 fprintf( stderr, "%s: %s (%d)%s\n",
412 func, ldap_err2string( err ), err, extra ? extra : "" );
414 if ( matched && *matched ) {
415 fprintf( stderr, _("\tmatched DN: %s\n"), matched );
418 if ( info && *info ) {
419 fprintf( stderr, _("\tadditional info: %s\n"), info );
422 if ( refs && *refs ) {
424 fprintf( stderr, _("\treferrals:\n") );
425 for( i=0; refs[i]; i++ ) {
426 fprintf( stderr, "\t\t%s\n", refs[i] );
433 tool_args( int argc, char **argv )
437 while (( i = getopt( argc, argv, options )) != EOF ) {
439 char *control, *cvalue, *next;
441 case 'c': /* continuous operation mode */
448 ival = strtol( optarg, &next, 10 );
449 if (next == NULL || next[0] != '\0') {
450 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
455 case 'D': /* bind DN */
456 if( binddn != NULL ) {
457 fprintf( stderr, "%s: -D previously specified\n", prog );
458 exit( EXIT_FAILURE );
460 binddn = ber_strdup( optarg );
462 case 'e': /* general extensions (controls and such) */
463 /* should be extended to support comma separated list of
464 * [!]key[=value] parameters, e.g. -e !foo,bar=567
469 while ( optarg[0] == '!' ) {
474 control = ber_strdup( optarg );
475 if ( (cvalue = strchr( control, '=' )) != NULL ) {
479 if ( strcasecmp( control, "assert" ) == 0 ) {
481 fprintf( stderr, "assert control previously specified\n");
482 exit( EXIT_FAILURE );
484 if( cvalue == NULL ) {
485 fprintf( stderr, "assert: control value expected\n" );
489 assertctl = 1 + crit;
491 assert( assertion == NULL );
492 assertion = ber_strdup( cvalue );
494 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
495 if( authzid != NULL ) {
496 fprintf( stderr, "authzid control previously specified\n");
497 exit( EXIT_FAILURE );
499 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
500 if( proxydn != NULL ) {
501 fprintf( stderr, "authzid control incompatible with proxydn\n");
502 exit( EXIT_FAILURE );
504 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
505 if( cvalue == NULL ) {
506 fprintf( stderr, "authzid: control value expected\n" );
510 fprintf( stderr, "authzid: must be marked critical\n" );
512 } else if ( crit > 1 ) {
513 /* purposely flag proxied authorization
514 * as non-critical, to test DSA */
518 assert( authzid == NULL );
519 authzid = ber_strdup( cvalue );
521 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
522 } else if ( strcasecmp( control, "proxydn" ) == 0 ) {
523 if( proxydn != NULL ) {
524 fprintf( stderr, "proxydn control previously specified\n");
525 exit( EXIT_FAILURE );
527 if( authzid != NULL ) {
528 fprintf( stderr, "proxydn control incompatible with authzid\n");
529 exit( EXIT_FAILURE );
531 if( cvalue == NULL ) {
532 fprintf( stderr, "proxydn: control value expected\n" );
536 fprintf( stderr, "proxydn: must be marked critical\n" );
538 } else if ( crit > 1 ) {
539 /* purposely flag proxied authorization
540 * as non-critical, to test DSA */
544 assert( proxydn == NULL );
545 proxydn = ber_strdup( cvalue );
546 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
548 } else if ( strcasecmp( control, "bauthzid" ) == 0 ) {
550 fprintf( stderr, "bauthzid control previously specified\n");
551 exit( EXIT_FAILURE );
553 if( cvalue != NULL ) {
554 fprintf( stderr, "bauthzid: no control value expected\n" );
559 } else if ( ( strcasecmp( control, "relax" ) == 0 ) ||
560 ( strcasecmp( control, "manageDIT" ) == 0 ) )
564 "relax control previously specified\n");
565 exit( EXIT_FAILURE );
567 if( cvalue != NULL ) {
569 "relax: no control value expected\n" );
573 manageDIT = 1 + crit;
575 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
578 "manageDSAit control previously specified\n");
579 exit( EXIT_FAILURE );
581 if( cvalue != NULL ) {
583 "manageDSAit: no control value expected\n" );
587 manageDSAit = 1 + crit;
589 } else if ( strcasecmp( control, "noop" ) == 0 ) {
591 fprintf( stderr, "noop control previously specified\n");
592 exit( EXIT_FAILURE );
594 if( cvalue != NULL ) {
595 fprintf( stderr, "noop: no control value expected\n" );
601 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
602 } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
604 fprintf( stderr, "ppolicy control previously specified\n");
605 exit( EXIT_FAILURE );
607 if( cvalue != NULL ) {
608 fprintf( stderr, "ppolicy: no control value expected\n" );
612 fprintf( stderr, "ppolicy: critical flag not allowed\n" );
619 } else if ( strcasecmp( control, "preread" ) == 0 ) {
621 fprintf( stderr, "preread control previously specified\n");
622 exit( EXIT_FAILURE );
626 preread_attrs = ber_strdup( cvalue );
628 } else if ( strcasecmp( control, "postread" ) == 0 ) {
630 fprintf( stderr, "postread control previously specified\n");
631 exit( EXIT_FAILURE );
635 postread_attrs = ber_strdup( cvalue );
637 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
638 } else if ( strcasecmp( control, "chaining" ) == 0 ) {
640 fprintf( stderr, "chaining control previously specified\n");
641 exit( EXIT_FAILURE );
646 if ( cvalue != NULL ) {
649 continuation = strchr( cvalue, '/' );
650 if ( continuation ) {
651 /* FIXME: this makes sense only in searches */
652 *continuation++ = '\0';
653 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
654 chainingContinuation = LDAP_CHAINING_PREFERRED;
655 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
656 chainingContinuation = LDAP_CHAINING_REQUIRED;
657 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
658 chainingContinuation = LDAP_REFERRALS_PREFERRED;
659 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
660 chainingContinuation = LDAP_REFERRALS_REQUIRED;
663 "chaining behavior control "
664 "continuation value \"%s\" invalid\n",
666 exit( EXIT_FAILURE );
670 if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
671 chainingResolve = LDAP_CHAINING_PREFERRED;
672 } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
673 chainingResolve = LDAP_CHAINING_REQUIRED;
674 } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
675 chainingResolve = LDAP_REFERRALS_PREFERRED;
676 } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
677 chainingResolve = LDAP_REFERRALS_REQUIRED;
680 "chaining behavior control "
681 "resolve value \"%s\" invalid\n",
683 exit( EXIT_FAILURE );
686 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
688 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
689 } else if ( strcasecmp( control, "sessiontracking" ) == 0 ) {
690 if ( sessionTracking ) {
691 fprintf( stderr, "%s: session tracking can be only specified once\n", prog );
692 exit( EXIT_FAILURE );
696 fprintf( stderr, "sessiontracking: critical flag not allowed\n" );
700 sessionTrackingName = ber_strdup( cvalue );
702 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
704 /* this shouldn't go here, really; but it's a feature... */
705 } else if ( strcasecmp( control, "abandon" ) == 0 ) {
706 abcan = Intr_Abandon;
711 } else if ( strcasecmp( control, "cancel" ) == 0 ) {
717 } else if ( strcasecmp( control, "ignore" ) == 0 ) {
723 } else if ( tool_is_oid( control ) ) {
724 LDAPControl *tmpctrls, ctrl;
726 if ( unknown_ctrls != NULL ) {
728 for ( i = 0; unknown_ctrls[ i ].ldctl_oid != NULL; i++ ) {
729 if ( strcmp( control, unknown_ctrls[ i ].ldctl_oid ) == 0 ) {
730 fprintf( stderr, "%s control previously specified\n", control );
731 exit( EXIT_FAILURE );
736 tmpctrls = (LDAPControl *)ber_memrealloc( unknown_ctrls,
737 (unknown_ctrls_num + 1)*sizeof( LDAPControl ) );
738 if ( tmpctrls == NULL ) {
739 fprintf( stderr, "%s: no memory?\n", prog );
740 exit( EXIT_FAILURE );
742 unknown_ctrls = tmpctrls;
743 ctrl.ldctl_oid = control;
746 ctrl.ldctl_value.bv_val = NULL;
747 ctrl.ldctl_value.bv_len = 0;
748 ctrl.ldctl_iscritical = crit;
750 if ( cvalue != NULL ) {
752 size_t len = strlen( cvalue );
755 bv.bv_len = LUTIL_BASE64_DECODE_LEN( len );
756 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
758 retcode = lutil_b64_pton( cvalue,
759 (unsigned char *)bv.bv_val,
762 if ( retcode == -1 || (unsigned) retcode > bv.bv_len ) {
763 fprintf( stderr, "Unable to parse value of general control %s\n",
769 ctrl.ldctl_value = bv;
772 unknown_ctrls[ unknown_ctrls_num ] = ctrl;
776 fprintf( stderr, "Invalid general control name: %s\n",
781 ber_memfree( control );
785 case 'f': /* read from file */
786 if( infile != NULL ) {
787 fprintf( stderr, "%s: -f previously specified\n", prog );
788 exit( EXIT_FAILURE );
790 infile = ber_strdup( optarg );
792 case 'h': /* ldap host */
793 if( ldaphost != NULL ) {
794 fprintf( stderr, "%s: -h previously specified\n", prog );
795 exit( EXIT_FAILURE );
797 ldaphost = ber_strdup( optarg );
799 case 'H': /* ldap URI */
800 if( ldapuri != NULL ) {
801 fprintf( stderr, "%s: -H previously specified\n", prog );
802 exit( EXIT_FAILURE );
804 ldapuri = ber_strdup( optarg );
807 #ifdef HAVE_CYRUS_SASL
808 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
809 fprintf( stderr, "%s: incompatible previous "
810 "authentication choice\n",
812 exit( EXIT_FAILURE );
814 authmethod = LDAP_AUTH_SASL;
815 sasl_flags = LDAP_SASL_INTERACTIVE;
818 fprintf( stderr, "%s: was not compiled with SASL support\n",
820 exit( EXIT_FAILURE );
823 /* enable Manage DSA IT */
826 case 'n': /* print operations, don't actually do them */
833 control = ber_strdup( optarg );
834 if ( (cvalue = strchr( control, '=' )) != NULL ) {
838 if ( strcasecmp( control, "nettimeout" ) == 0 ) {
839 if( nettimeout.tv_sec != -1 ) {
840 fprintf( stderr, "nettimeout option previously specified\n");
841 exit( EXIT_FAILURE );
843 if( cvalue == NULL || cvalue[0] == '\0' ) {
844 fprintf( stderr, "nettimeout: option value expected\n" );
847 if ( strcasecmp( cvalue, "none" ) == 0 ) {
848 nettimeout.tv_sec = 0;
849 } else if ( strcasecmp( cvalue, "max" ) == 0 ) {
850 nettimeout.tv_sec = LDAP_MAXINT;
852 ival = strtol( cvalue, &next, 10 );
853 if ( next == NULL || next[0] != '\0' ) {
855 _("Unable to parse network timeout \"%s\"\n"), cvalue );
856 exit( EXIT_FAILURE );
858 nettimeout.tv_sec = ival;
860 if( nettimeout.tv_sec < 0 || nettimeout.tv_sec > LDAP_MAXINT ) {
861 fprintf( stderr, _("%s: invalid network timeout (%ld) specified\n"),
862 prog, (long)nettimeout.tv_sec );
863 exit( EXIT_FAILURE );
866 } else if ( strcasecmp( control, "ldif-wrap" ) == 0 ) {
868 ldif_wrap = LDIF_LINE_WIDTH;
870 } else if ( strcasecmp( cvalue, "no" ) == 0 ) {
871 ldif_wrap = LDIF_LINE_WIDTH_MAX;
875 if ( lutil_atou( &u, cvalue ) ) {
877 _("Unable to parse ldif-wrap=\"%s\"\n"), cvalue );
878 exit( EXIT_FAILURE );
880 ldif_wrap = (ber_len_t)u;
884 fprintf( stderr, "Invalid general option name: %s\n",
888 ber_memfree(control);
891 #ifdef HAVE_CYRUS_SASL
892 if( sasl_secprops != NULL ) {
893 fprintf( stderr, "%s: -O previously specified\n", prog );
894 exit( EXIT_FAILURE );
896 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
897 fprintf( stderr, "%s: incompatible previous "
898 "authentication choice\n", prog );
899 exit( EXIT_FAILURE );
901 authmethod = LDAP_AUTH_SASL;
902 sasl_secprops = ber_strdup( optarg );
904 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
905 exit( EXIT_FAILURE );
910 fprintf( stderr, "%s: -p previously specified\n", prog );
911 exit( EXIT_FAILURE );
913 ival = strtol( optarg, &next, 10 );
914 if ( next == NULL || next[0] != '\0' ) {
915 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
916 exit( EXIT_FAILURE );
921 ival = strtol( optarg, &next, 10 );
922 if ( next == NULL || next[0] != '\0' ) {
923 fprintf( stderr, "%s: unable to parse protocol version \"%s\"\n", prog, optarg );
924 exit( EXIT_FAILURE );
928 if( protocol == LDAP_VERSION3 ) {
929 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
931 exit( EXIT_FAILURE );
933 protocol = LDAP_VERSION2;
936 if( protocol == LDAP_VERSION2 ) {
937 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
939 exit( EXIT_FAILURE );
941 protocol = LDAP_VERSION3;
944 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
950 #ifdef HAVE_CYRUS_SASL
951 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
952 fprintf( stderr, "%s: incompatible previous "
953 "authentication choice\n",
955 exit( EXIT_FAILURE );
957 authmethod = LDAP_AUTH_SASL;
958 sasl_flags = LDAP_SASL_QUIET;
961 fprintf( stderr, "%s: not compiled with SASL support\n",
963 exit( EXIT_FAILURE );
966 #ifdef HAVE_CYRUS_SASL
967 if( sasl_realm != NULL ) {
968 fprintf( stderr, "%s: -R previously specified\n", prog );
969 exit( EXIT_FAILURE );
971 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
972 fprintf( stderr, "%s: incompatible previous "
973 "authentication choice\n",
975 exit( EXIT_FAILURE );
977 authmethod = LDAP_AUTH_SASL;
978 sasl_realm = ber_strdup( optarg );
980 fprintf( stderr, "%s: not compiled with SASL support\n",
982 exit( EXIT_FAILURE );
986 #ifdef HAVE_CYRUS_SASL
987 if( sasl_authc_id != NULL ) {
988 fprintf( stderr, "%s: -U previously specified\n", prog );
989 exit( EXIT_FAILURE );
991 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
992 fprintf( stderr, "%s: incompatible previous "
993 "authentication choice\n",
995 exit( EXIT_FAILURE );
997 authmethod = LDAP_AUTH_SASL;
998 sasl_authc_id = ber_strdup( optarg );
1000 fprintf( stderr, "%s: not compiled with SASL support\n",
1002 exit( EXIT_FAILURE );
1005 case 'v': /* verbose mode */
1008 case 'V': /* version */
1011 case 'w': /* password */
1012 passwd.bv_val = ber_strdup( optarg );
1016 for( p = optarg; *p != '\0'; p++ ) {
1020 passwd.bv_len = strlen( passwd.bv_val );
1029 #ifdef HAVE_CYRUS_SASL
1030 if( sasl_mech != NULL ) {
1031 fprintf( stderr, "%s: -Y previously specified\n", prog );
1032 exit( EXIT_FAILURE );
1034 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
1036 "%s: incompatible with authentication choice\n", prog );
1037 exit( EXIT_FAILURE );
1039 authmethod = LDAP_AUTH_SASL;
1040 sasl_mech = ber_strdup( optarg );
1042 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1043 exit( EXIT_FAILURE );
1047 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
1048 fprintf( stderr, "%s: incompatible with previous "
1049 "authentication choice\n", prog );
1050 exit( EXIT_FAILURE );
1052 authmethod = LDAP_AUTH_SIMPLE;
1055 #ifdef HAVE_CYRUS_SASL
1056 if( sasl_authz_id != NULL ) {
1057 fprintf( stderr, "%s: -X previously specified\n", prog );
1058 exit( EXIT_FAILURE );
1060 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
1061 fprintf( stderr, "%s: -X incompatible with "
1062 "authentication choice\n", prog );
1063 exit( EXIT_FAILURE );
1065 authmethod = LDAP_AUTH_SASL;
1066 sasl_authz_id = ber_strdup( optarg );
1068 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1069 exit( EXIT_FAILURE );
1076 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
1077 exit( EXIT_FAILURE );
1081 if( handle_private_option( i ) ) break;
1082 fprintf( stderr, "%s: unrecognized option -%c\n",
1089 /* prevent bad linking */
1091 api.ldapai_info_version = LDAP_API_INFO_VERSION;
1093 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
1094 != LDAP_OPT_SUCCESS )
1096 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
1097 exit( EXIT_FAILURE );
1100 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
1101 fprintf( stderr, "LDAP APIInfo version mismatch: "
1102 "library %d, header %d\n",
1103 api.ldapai_info_version, LDAP_API_INFO_VERSION );
1104 exit( EXIT_FAILURE );
1107 if( api.ldapai_api_version != LDAP_API_VERSION ) {
1108 fprintf( stderr, "LDAP API version mismatch: "
1109 "library %d, header %d\n",
1110 api.ldapai_api_version, LDAP_API_VERSION );
1111 exit( EXIT_FAILURE );
1114 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
1115 fprintf( stderr, "LDAP vendor name mismatch: "
1116 "library %s, header %s\n",
1117 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
1118 exit( EXIT_FAILURE );
1121 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
1122 fprintf( stderr, "LDAP vendor version mismatch: "
1123 "library %d, header %d\n",
1124 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
1125 exit( EXIT_FAILURE );
1129 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
1131 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
1132 if (version > 1) exit( EXIT_SUCCESS );
1135 ldap_memfree( api.ldapai_vendor_name );
1136 ber_memvfree( (void **)api.ldapai_extensions );
1140 protocol = LDAP_VERSION3;
1142 if (authmethod == -1 && protocol > LDAP_VERSION2) {
1143 #ifdef HAVE_CYRUS_SASL
1144 if ( binddn != NULL ) {
1145 authmethod = LDAP_AUTH_SIMPLE;
1147 authmethod = LDAP_AUTH_SASL;
1150 authmethod = LDAP_AUTH_SIMPLE;
1154 if( ldapuri == NULL ) {
1155 if( ldapport && ( ldaphost == NULL )) {
1156 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
1157 exit( EXIT_FAILURE );
1160 if( ldaphost != NULL ) {
1161 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
1162 exit( EXIT_FAILURE );
1165 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
1166 exit( EXIT_FAILURE );
1170 if( protocol == LDAP_VERSION2 ) {
1171 if( assertctl || authzid || manageDIT || manageDSAit ||
1172 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1174 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1175 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1178 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1180 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1181 noop || ppolicy || preread || postread )
1183 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
1184 exit( EXIT_FAILURE );
1188 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
1189 exit( EXIT_FAILURE );
1192 #ifdef HAVE_CYRUS_SASL
1193 if( authmethod == LDAP_AUTH_SASL ) {
1194 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
1196 exit( EXIT_FAILURE );
1201 if ( ( pw_file || want_bindpw ) && !BER_BVISNULL( &passwd ) ) {
1202 fprintf( stderr, "%s: -%c incompatible with -w\n",
1203 prog, ( pw_file ? 'y' : 'W' ) );
1204 exit( EXIT_FAILURE );
1210 tool_conn_setup( int dont, void (*private_setup)( LDAP * ) )
1215 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
1216 != LBER_OPT_SUCCESS )
1219 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
1221 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
1222 != LDAP_OPT_SUCCESS )
1225 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
1230 (void) SIGNAL( SIGPIPE, SIG_IGN );
1234 SIGNAL( SIGINT, do_sig );
1240 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
1243 memset( &url, 0, sizeof(url));
1245 url.lud_scheme = "ldap";
1246 url.lud_host = ldaphost;
1247 url.lud_port = ldapport;
1248 url.lud_scope = LDAP_SCOPE_DEFAULT;
1250 ldapuri = ldap_url_desc2str( &url );
1252 } else if ( ldapuri != NULL ) {
1253 LDAPURLDesc *ludlist, **ludp;
1257 rc = ldap_url_parselist( &ludlist, ldapuri );
1258 if ( rc != LDAP_URL_SUCCESS ) {
1260 "Could not parse LDAP URI(s)=%s (%d)\n",
1262 exit( EXIT_FAILURE );
1265 for ( ludp = &ludlist; *ludp != NULL; ) {
1266 LDAPURLDesc *lud = *ludp;
1269 if ( lud->lud_dn != NULL && lud->lud_dn[ 0 ] != '\0' &&
1270 ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) )
1272 /* if no host but a DN is provided,
1273 * use DNS SRV to gather the host list
1274 * and turn it into a list of URIs
1275 * using the scheme provided */
1276 char *domain = NULL,
1280 len_proto = strlen( lud->lud_scheme );
1282 if ( ldap_dn2domain( lud->lud_dn, &domain )
1286 "DNS SRV: Could not turn "
1287 "DN=\"%s\" into a domain\n",
1292 rc = ldap_domain2hostlist( domain, &hostlist );
1295 "DNS SRV: Could not turn "
1296 "domain=%s into a hostlist\n",
1301 hosts = ldap_str2charray( hostlist, " " );
1302 if ( hosts == NULL ) {
1304 "DNS SRV: Could not parse "
1305 "hostlist=\"%s\"\n",
1310 for ( i = 0; hosts[ i ] != NULL; i++ )
1313 tmp = (char **)ber_memrealloc( urls, sizeof( char * ) * ( nurls + i + 1 ) );
1314 if ( tmp == NULL ) {
1316 "DNS SRV: out of memory?\n" );
1320 urls[ nurls ] = NULL;
1322 for ( i = 0; hosts[ i ] != NULL; i++ ) {
1323 size_t len = len_proto
1325 + strlen( hosts[ i ] )
1328 urls[ nurls + i + 1 ] = NULL;
1329 urls[ nurls + i ] = (char *)malloc( sizeof( char ) * len );
1330 if ( urls[ nurls + i ] == NULL ) {
1332 "DNS SRV: out of memory?\n" );
1336 snprintf( urls[ nurls + i ], len, "%s://%s",
1337 lud->lud_scheme, hosts[ i ] );
1342 ber_memvfree( (void **)hosts );
1343 ber_memfree( hostlist );
1344 ber_memfree( domain );
1347 tmp = (char **)ber_memrealloc( urls, sizeof( char * ) * ( nurls + 2 ) );
1348 if ( tmp == NULL ) {
1350 "DNS SRV: out of memory?\n" );
1354 urls[ nurls + 1 ] = NULL;
1356 urls[ nurls ] = ldap_url_desc2str( lud );
1357 if ( urls[ nurls ] == NULL ) {
1359 "DNS SRV: out of memory?\n" );
1365 *ludp = lud->lud_next;
1367 lud->lud_next = NULL;
1368 ldap_free_urldesc( lud );
1371 if ( ludlist != NULL ) {
1372 ldap_free_urllist( ludlist );
1373 exit( EXIT_FAILURE );
1375 } else if ( urls == NULL ) {
1376 exit( EXIT_FAILURE );
1379 ldap_memfree( ldapuri );
1380 ldapuri = ldap_charray2str( urls, " " );
1381 ber_memvfree( (void **)urls );
1385 fprintf( stderr, "ldap_initialize( %s )\n",
1386 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
1388 rc = ldap_initialize( &ld, ldapuri );
1389 if( rc != LDAP_SUCCESS ) {
1391 "Could not create LDAP session handle for URI=%s (%d): %s\n",
1392 ldapuri, rc, ldap_err2string(rc) );
1393 exit( EXIT_FAILURE );
1396 if( private_setup ) private_setup( ld );
1399 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
1400 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
1402 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
1403 referrals ? "on" : "off" );
1404 exit( EXIT_FAILURE );
1407 #ifdef HAVE_CYRUS_SASL
1409 if( ldap_set_option( ld, LDAP_OPT_X_SASL_NOCANON,
1410 nocanon ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
1412 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_NOCANON %s\n",
1413 nocanon ? "on" : "off" );
1414 exit( EXIT_FAILURE );
1417 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
1418 != LDAP_OPT_SUCCESS )
1420 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
1422 exit( EXIT_FAILURE );
1426 rc = ldap_start_tls_s( ld, NULL, NULL );
1427 if ( rc != LDAP_SUCCESS ) {
1429 ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&msg);
1430 tool_perror( "ldap_start_tls", rc, NULL, NULL, msg, NULL );
1432 if ( use_tls > 1 ) {
1433 exit( EXIT_FAILURE );
1438 if ( nettimeout.tv_sec > 0 ) {
1439 if ( ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &nettimeout )
1440 != LDAP_OPT_SUCCESS )
1442 fprintf( stderr, "Could not set LDAP_OPT_NETWORK_TIMEOUT %ld\n",
1443 (long)nettimeout.tv_sec );
1444 exit( EXIT_FAILURE );
1454 tool_bind( LDAP *ld )
1456 LDAPControl **sctrlsp = NULL;
1457 LDAPControl *sctrls[4];
1458 LDAPControl sctrl[3];
1462 LDAPMessage *result = NULL;
1465 char *matched = NULL;
1468 LDAPControl **ctrls = NULL;
1473 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1476 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1477 c.ldctl_value.bv_val = NULL;
1478 c.ldctl_value.bv_len = 0;
1479 c.ldctl_iscritical = 0;
1481 sctrls[nsctrls] = &sctrl[nsctrls];
1482 sctrls[++nsctrls] = NULL;
1489 c.ldctl_oid = LDAP_CONTROL_AUTHZID_REQUEST;
1490 c.ldctl_iscritical = bauthzid > 1;
1491 BER_BVZERO( &c.ldctl_value );
1494 sctrls[nsctrls] = &sctrl[nsctrls];
1495 sctrls[++nsctrls] = NULL;
1498 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1499 if ( sessionTracking ) {
1502 if ( BER_BVISNULL( &stValue) && st_value( ld, &stValue ) ) {
1503 exit( EXIT_FAILURE );
1506 c.ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1507 c.ldctl_iscritical = 0;
1508 c.ldctl_value = stValue;
1511 sctrls[nsctrls] = &sctrl[nsctrls];
1512 sctrls[++nsctrls] = NULL;
1514 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1520 assert( nsctrls < (int) (sizeof(sctrls)/sizeof(sctrls[0])) );
1522 if ( pw_file || want_bindpw ) {
1523 assert( passwd.bv_val == NULL && passwd.bv_len == 0 );
1526 if ( lutil_get_filed_password( pw_file, &passwd ) ) {
1527 exit( EXIT_FAILURE );
1531 char *pw = getpassphrase( _("Enter LDAP Password: ") );
1533 passwd.bv_val = ber_strdup( pw );
1534 passwd.bv_len = strlen( passwd.bv_val );
1539 if ( authmethod == LDAP_AUTH_SASL ) {
1540 #ifdef HAVE_CYRUS_SASL
1542 const char *rmech = NULL;
1544 if( sasl_secprops != NULL ) {
1545 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
1546 (void *) sasl_secprops );
1548 if( rc != LDAP_OPT_SUCCESS ) {
1550 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
1552 exit( LDAP_LOCAL_ERROR );
1556 defaults = lutil_sasl_defaults( ld,
1564 rc = ldap_sasl_interactive_bind( ld, binddn, sasl_mech,
1565 sctrlsp, NULL, sasl_flags, lutil_sasl_interact, defaults,
1566 result, &rmech, &msgid );
1568 if ( rc != LDAP_SASL_BIND_IN_PROGRESS )
1571 ldap_msgfree( result );
1573 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 || !result ) {
1574 ldap_get_option( ld, LDAP_OPT_RESULT_CODE, (void*)&err );
1575 ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&info );
1576 tool_perror( "ldap_sasl_interactive_bind",
1577 err, NULL, NULL, info, NULL );
1578 ldap_memfree( info );
1581 } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
1583 lutil_sasl_freedefs( defaults );
1585 if ( rc != LDAP_SUCCESS ) {
1586 ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&info );
1587 tool_perror( "ldap_sasl_interactive_bind",
1588 rc, NULL, NULL, info, NULL );
1589 ldap_memfree( info );
1593 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1594 exit( LDAP_NOT_SUPPORTED );
1598 rc = ldap_sasl_bind( ld, binddn, LDAP_SASL_SIMPLE, &passwd,
1599 sctrlsp, NULL, &msgid );
1600 if ( msgid == -1 ) {
1601 tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
1602 NULL, NULL, NULL, NULL );
1606 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result );
1608 tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
1609 exit( LDAP_LOCAL_ERROR );
1613 tool_perror( "ldap_result", LDAP_TIMEOUT, NULL, NULL, NULL, NULL );
1614 exit( LDAP_LOCAL_ERROR );
1618 rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
1620 if ( rc != LDAP_SUCCESS ) {
1621 tool_perror( "ldap_bind parse result", rc, NULL, matched, info, refs );
1622 exit( LDAP_LOCAL_ERROR );
1625 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1626 if ( ctrls && ppolicy ) {
1628 int expire, grace, len = 0;
1629 LDAPPasswordPolicyError pErr = -1;
1631 ctrl = ldap_control_find( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
1634 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
1635 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1637 if ( pErr != PP_noError ){
1640 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1641 len = strlen( msgbuf );
1643 if ( expire >= 0 ) {
1644 sprintf( msgbuf+len,
1645 " (Password expires in %d seconds)",
1647 } else if ( grace >= 0 ) {
1648 sprintf( msgbuf+len,
1649 " (Password expired, %d grace logins remain)",
1656 if ( ctrls && bauthzid ) {
1659 ctrl = ldap_control_find( LDAP_CONTROL_AUTHZID_RESPONSE,
1662 LDAPControl *ctmp[2];
1665 tool_print_ctrls( ld, ctmp );
1670 ldap_controls_free( ctrls );
1673 if ( err != LDAP_SUCCESS
1675 || ( matched && matched[ 0 ] )
1676 || ( info && info[ 0 ] )
1679 tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1681 if( matched ) ber_memfree( matched );
1682 if( info ) ber_memfree( info );
1683 if( refs ) ber_memvfree( (void **)refs );
1685 if ( err != LDAP_SUCCESS ) exit( err );
1690 tool_unbind( LDAP *ld )
1692 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1694 if ( err != LDAP_OPT_SUCCESS ) {
1695 fprintf( stderr, "Could not unset controls\n");
1698 (void) ldap_unbind_ext( ld, NULL, NULL );
1702 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1704 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1706 int i = 0, j, crit = 0, err;
1707 LDAPControl c[16], **ctrls;
1711 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1713 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1717 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1722 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1724 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1725 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1727 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1729 || unknown_ctrls_num ) )
1734 ctrls = (LDAPControl**) malloc(sizeof(c) + (count + unknown_ctrls_num + 1)*sizeof(LDAPControl*));
1735 if ( ctrls == NULL ) {
1736 fprintf( stderr, "No memory\n" );
1737 exit( EXIT_FAILURE );
1741 if ( BER_BVISNULL( &assertionvalue ) ) {
1742 err = ldap_create_assertion_control_value( ld,
1743 assertion, &assertionvalue );
1746 "Unable to create assertion value "
1747 "\"%s\" (%d)\n", assertion, err );
1751 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1752 c[i].ldctl_value = assertionvalue;
1753 c[i].ldctl_iscritical = assertctl > 1;
1759 c[i].ldctl_value.bv_val = authzid;
1760 c[i].ldctl_value.bv_len = strlen( authzid );
1761 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1762 c[i].ldctl_iscritical = authzcrit;
1767 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1768 /* NOTE: doesn't need an extra count because it's incompatible
1771 BerElementBuffer berbuf;
1772 BerElement *ber = (BerElement *)&berbuf;
1774 ber_init2( ber, NULL, LBER_USE_DER );
1776 if ( ber_printf( ber, "s", proxydn ) == -1 ) {
1777 exit( EXIT_FAILURE );
1780 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1781 exit( EXIT_FAILURE );
1784 c[i].ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1785 c[i].ldctl_iscritical = authzcrit;
1789 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1792 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1793 BER_BVZERO( &c[i].ldctl_value );
1794 c[i].ldctl_iscritical = manageDIT > 1;
1799 if ( manageDSAit ) {
1800 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1801 BER_BVZERO( &c[i].ldctl_value );
1802 c[i].ldctl_iscritical = manageDSAit > 1;
1808 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1809 BER_BVZERO( &c[i].ldctl_value );
1810 c[i].ldctl_iscritical = noop > 1;
1815 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1817 c[i].ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1818 BER_BVZERO( &c[i].ldctl_value );
1819 c[i].ldctl_iscritical = 0;
1826 BerElementBuffer berbuf;
1827 BerElement *ber = (BerElement *)&berbuf;
1828 char **attrs = NULL;
1830 if( preread_attrs ) {
1831 attrs = ldap_str2charray( preread_attrs, "," );
1834 ber_init2( ber, NULL, LBER_USE_DER );
1836 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1837 fprintf( stderr, "preread attrs encode failed.\n" );
1838 exit( EXIT_FAILURE );
1841 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1843 fprintf( stderr, "preread flatten failed (%d)\n", err );
1844 exit( EXIT_FAILURE );
1847 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1848 c[i].ldctl_iscritical = preread > 1;
1852 if( attrs ) ldap_charray_free( attrs );
1856 BerElementBuffer berbuf;
1857 BerElement *ber = (BerElement *)&berbuf;
1858 char **attrs = NULL;
1860 if( postread_attrs ) {
1861 attrs = ldap_str2charray( postread_attrs, "," );
1864 ber_init2( ber, NULL, LBER_USE_DER );
1866 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1867 fprintf( stderr, "postread attrs encode failed.\n" );
1868 exit( EXIT_FAILURE );
1871 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1873 fprintf( stderr, "postread flatten failed (%d)\n", err );
1874 exit( EXIT_FAILURE );
1877 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1878 c[i].ldctl_iscritical = postread > 1;
1882 if( attrs ) ldap_charray_free( attrs );
1885 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1887 if ( chainingResolve > -1 ) {
1888 BerElementBuffer berbuf;
1889 BerElement *ber = (BerElement *)&berbuf;
1891 ber_init2( ber, NULL, LBER_USE_DER );
1893 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1896 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1897 exit( EXIT_FAILURE );
1900 if ( chainingContinuation > -1 ) {
1901 err = ber_printf( ber, "e", chainingContinuation );
1904 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1905 exit( EXIT_FAILURE );
1909 err = ber_printf( ber, /* { */ "N}" );
1912 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1913 exit( EXIT_FAILURE );
1916 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1917 exit( EXIT_FAILURE );
1921 BER_BVZERO( &c[i].ldctl_value );
1924 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1925 c[i].ldctl_iscritical = chaining > 1;
1929 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1931 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1932 if ( sessionTracking ) {
1933 if ( BER_BVISNULL( &stValue ) && st_value( ld, &stValue ) ) {
1934 exit( EXIT_FAILURE );
1937 c[i].ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1938 c[i].ldctl_iscritical = 0;
1939 c[i].ldctl_value = stValue;
1944 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1947 ctrls[i++] = extra_c++;
1949 for ( count = 0; count < unknown_ctrls_num; count++ ) {
1950 ctrls[i++] = &unknown_ctrls[count];
1954 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1956 if ( err != LDAP_OPT_SUCCESS ) {
1957 for ( j = 0; j < i; j++ ) {
1958 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1960 fprintf( stderr, "Could not set %scontrols\n",
1961 crit ? "critical " : "" );
1966 exit( EXIT_FAILURE );
1971 tool_check_abandon( LDAP *ld, int msgid )
1975 switch ( gotintr ) {
1977 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1978 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1979 rc, ldap_err2string( rc ) );
1983 rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
1984 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1985 rc, ldap_err2string( rc ) );
1989 /* just unbind, ignoring the request */
1997 print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
2002 tool_write_ldif( LDIF_PUT_COMMENT, "==> ",
2003 what->bv_val, what->bv_len );
2004 ber = ber_init( &ctrl->ldctl_value );
2005 if ( ber == NULL ) {
2009 } else if ( ber_scanf( ber, "{m{" /*}}*/, &bv ) == LBER_ERROR ) {
2014 tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
2016 while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
2018 BerVarray vals = NULL;
2021 if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
2031 str = malloc( bv.bv_len + STRLENOF(": ") + 1 );
2034 ptr = lutil_strncopy( ptr, bv.bv_val, bv.bv_len );
2035 ptr = lutil_strcopy( ptr, ": " );
2038 for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
2040 ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2041 ldif ? str : bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
2044 ber_bvarray_free( vals );
2045 if ( str ) free( str );
2049 if ( ber != NULL ) {
2053 tool_write_ldif( LDIF_PUT_COMMENT, "<== ",
2054 what->bv_val, what->bv_len );
2060 print_preread( LDAP *ld, LDAPControl *ctrl )
2062 static struct berval what = BER_BVC( "preread" );
2064 return print_prepostread( ld, ctrl, &what );
2068 print_postread( LDAP *ld, LDAPControl *ctrl )
2070 static struct berval what = BER_BVC( "postread" );
2072 return print_prepostread( ld, ctrl, &what );
2076 print_paged_results( LDAP *ld, LDAPControl *ctrl )
2080 /* note: pr_cookie is being malloced; it's freed
2081 * the next time the control is sent, but the last
2082 * time it's not; we don't care too much, because
2083 * the last time an empty value is returned... */
2084 if ( ldap_parse_pageresponse_control( ld, ctrl, &estimate, &pr_cookie )
2091 /* FIXME: check buffer overflow */
2092 char buf[ BUFSIZ ], *ptr = buf;
2094 if ( estimate > 0 ) {
2095 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2096 "estimate=%d", estimate );
2099 if ( pr_cookie.bv_len > 0 ) {
2102 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
2103 pr_cookie.bv_len ) + 1;
2104 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
2106 bv.bv_len = lutil_b64_ntop(
2107 (unsigned char *) pr_cookie.bv_val,
2109 bv.bv_val, bv.bv_len );
2111 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2112 "%scookie=%s", ptr == buf ? "" : " ",
2115 ber_memfree( bv.bv_val );
2117 pr_morePagedResults = 1;
2120 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2121 "%scookie=", ptr == buf ? "" : " " );
2124 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2125 ldif ? "pagedresults: " : "pagedresults",
2133 print_sss( LDAP *ld, LDAPControl *ctrl )
2139 rc = ldap_parse_sortresponse_control( ld, ctrl, &err, &attr );
2140 if ( rc == LDAP_SUCCESS ) {
2142 rc = snprintf( buf, sizeof(buf), "(%d) %s%s%s",
2143 err, ldap_err2string(err), attr ? " " : "", attr ? attr : "" );
2145 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2146 ldif ? "sortResult: " : "sortResult", buf, rc );
2153 print_vlv( LDAP *ld, LDAPControl *ctrl )
2159 rc = ldap_parse_vlvresponse_control( ld, ctrl, &vlvPos, &vlvCount,
2160 &vlvContext, &err );
2161 if ( rc == LDAP_SUCCESS ) {
2164 if ( vlvContext && vlvContext->bv_len > 0 ) {
2165 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
2166 vlvContext->bv_len ) + 1;
2167 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
2169 bv.bv_len = lutil_b64_ntop(
2170 (unsigned char *) vlvContext->bv_val,
2172 bv.bv_val, bv.bv_len );
2178 rc = snprintf( buf, sizeof(buf), "pos=%d count=%d context=%s (%d) %s",
2179 vlvPos, vlvCount, bv.bv_val,
2180 err, ldap_err2string(err));
2183 ber_memfree( bv.bv_val );
2185 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2186 ldif ? "vlvResult" : "vlvResult", buf, rc );
2192 #ifdef LDAP_CONTROL_X_DEREF
2194 print_deref( LDAP *ld, LDAPControl *ctrl )
2196 LDAPDerefRes *drhead = NULL, *dr;
2199 rc = ldap_parse_derefresponse_control( ld, ctrl, &drhead );
2200 if ( rc != LDAP_SUCCESS ) {
2204 for ( dr = drhead; dr != NULL; dr = dr->next ) {
2209 len = strlen( dr->derefAttr ) + STRLENOF(": ");
2211 for ( dv = dr->attrVals; dv != NULL; dv = dv->next ) {
2212 if ( dv->vals != NULL ) {
2214 ber_len_t tlen = strlen(dv->type);
2216 for ( j = 0; dv->vals[ j ].bv_val != NULL; j++ ) {
2217 len += STRLENOF("<:=>;") + tlen + 4*((dv->vals[ j ].bv_len - 1)/3 + 1);
2221 len += dr->derefVal.bv_len + STRLENOF("\n");
2222 buf = ldap_memalloc( len + 1 );
2223 if ( buf == NULL ) {
2224 rc = LDAP_NO_MEMORY;
2229 ptr = lutil_strcopy( ptr, dr->derefAttr );
2232 for ( dv = dr->attrVals; dv != NULL; dv = dv->next ) {
2233 if ( dv->vals != NULL ) {
2235 for ( j = 0; dv->vals[ j ].bv_val != NULL; j++ ) {
2236 int k = ldif_is_not_printable( dv->vals[ j ].bv_val, dv->vals[ j ].bv_len );
2239 ptr = lutil_strcopy( ptr, dv->type );
2246 (unsigned char *) dv->vals[ j ].bv_val,
2247 dv->vals[ j ].bv_len,
2248 ptr, buf + len - ptr );
2253 ptr = lutil_memcopy( ptr, dv->vals[ j ].bv_val, dv->vals[ j ].bv_len );
2260 ptr = lutil_strncopy( ptr, dr->derefVal.bv_val, dr->derefVal.bv_len );
2263 assert( ptr <= buf + len );
2265 tool_write_ldif( LDIF_PUT_COMMENT, NULL, buf, ptr - buf);
2267 ldap_memfree( buf );
2273 ldap_derefresponse_free( drhead );
2279 #ifdef LDAP_CONTROL_X_WHATFAILED
2281 print_whatfailed( LDAP *ld, LDAPControl *ctrl )
2286 BerVarray bva = NULL;
2288 /* Create a BerElement from the berval returned in the control. */
2289 ber = ber_init( &ctrl->ldctl_value );
2291 if ( ber == NULL ) {
2292 return LDAP_NO_MEMORY;
2295 siz = sizeof(struct berval);
2296 tag = ber_scanf( ber, "[M]", &bva, &siz, 0 );
2297 if ( tag != LBER_ERROR ) {
2300 tool_write_ldif( LDIF_PUT_COMMENT, " what failed:", NULL, 0 );
2302 for ( i = 0; bva[i].bv_val != NULL; i++ ) {
2303 tool_write_ldif( LDIF_PUT_COMMENT, NULL, bva[i].bv_val, bva[i].bv_len );
2306 ldap_memfree( bva );
2316 #ifdef LDAP_CONTROL_AUTHZID_RESPONSE
2318 print_authzid( LDAP *ld, LDAPControl *ctrl )
2320 if ( ctrl->ldctl_value.bv_len ) {
2321 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2322 ldif ? "authzid: " : "authzid",
2323 ctrl->ldctl_value.bv_val, ctrl->ldctl_value.bv_len );
2325 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2326 ldif ? "authzid: " : "authzid",
2327 "anonymous", STRLENOF("anonymous") );
2334 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
2336 print_ppolicy( LDAP *ld, LDAPControl *ctrl )
2338 int expire = 0, grace = 0, rc;
2339 LDAPPasswordPolicyError pperr;
2341 rc = ldap_parse_passwordpolicy_control( ld, ctrl,
2342 &expire, &grace, &pperr );
2343 if ( rc == LDAP_SUCCESS ) {
2344 char buf[ BUFSIZ ], *ptr = buf;
2346 if ( expire != -1 ) {
2347 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2348 "expire=%d", expire );
2351 if ( grace != -1 ) {
2352 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2353 "%sgrace=%d", ptr == buf ? "" : " ", grace );
2356 if ( pperr != PP_noError ) {
2357 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2358 "%serror=%d (%s)", ptr == buf ? "" : " ",
2360 ldap_passwordpolicy_err2txt( pperr ) );
2363 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2364 ldif ? "ppolicy: " : "ppolicy", buf, ptr - buf );
2371 void tool_print_ctrls(
2373 LDAPControl **ctrls )
2378 for ( i = 0; ctrls[i] != NULL; i++ ) {
2379 /* control: OID criticality base64value */
2380 struct berval b64 = BER_BVNULL;
2385 /* FIXME: there might be cases where a control has NULL OID;
2386 * this makes little sense, especially when returned by the
2387 * server, but libldap happily allows it */
2388 if ( ctrls[i]->ldctl_oid == NULL ) {
2393 len += strlen( ctrls[i]->ldctl_oid );
2395 /* add enough for space after OID and the critical value itself */
2396 len += ctrls[i]->ldctl_iscritical
2397 ? sizeof("true") : sizeof("false");
2399 /* convert to base64 */
2400 if ( !BER_BVISNULL( &ctrls[i]->ldctl_value ) ) {
2401 b64.bv_len = LUTIL_BASE64_ENCODE_LEN(
2402 ctrls[i]->ldctl_value.bv_len ) + 1;
2403 b64.bv_val = ber_memalloc( b64.bv_len + 1 );
2405 b64.bv_len = lutil_b64_ntop(
2406 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
2407 ctrls[i]->ldctl_value.bv_len,
2408 b64.bv_val, b64.bv_len );
2412 len += 1 + b64.bv_len;
2415 ptr = str = malloc( len + 1 );
2417 ptr = lutil_strcopy( ptr, ": " );
2419 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_oid );
2420 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_iscritical
2421 ? " true" : " false" );
2424 ptr = lutil_strcopy( ptr, " " );
2425 ptr = lutil_strcopy( ptr, b64.bv_val );
2429 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2430 "control", str, len );
2435 ber_memfree( b64.bv_val );
2438 /* known controls */
2439 for ( j = 0; tool_ctrl_response[j].oid != NULL; j++ ) {
2440 if ( strcmp( tool_ctrl_response[j].oid, ctrls[i]->ldctl_oid ) == 0 ) {
2441 if ( !tool_ctrl_response[j].mask & tool_type ) {
2442 /* this control should not appear
2443 * with this tool; warning? */
2449 if ( tool_ctrl_response[j].oid != NULL && tool_ctrl_response[j].func ) {
2450 (void)tool_ctrl_response[j].func( ld, ctrls[i] );
2456 tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
2460 if (( ldif = ldif_put_wrap( type, name, value, vallen, ldif_wrap )) == NULL ) {
2464 fputs( ldif, stdout );
2465 ber_memfree( ldif );
2471 tool_is_oid( const char *s )
2475 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2479 for ( ; s[ 0 ]; s++ ) {
2480 if ( s[ 0 ] == '.' ) {
2481 if ( s[ 1 ] == '\0' ) {
2488 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2492 if ( first == 1 && s[ 0 ] == '0' && s[ 1 ] != '.' ) {