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-2012 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 static int destroyed;
259 #ifdef HAVE_CYRUS_SASL
263 ldap_pvt_tls_destroy();
266 if ( ldapuri != NULL ) {
267 ber_memfree( ldapuri );
271 if ( pr_cookie.bv_val != NULL ) {
272 ber_memfree( pr_cookie.bv_val );
273 BER_BVZERO( &pr_cookie );
276 if ( binddn != NULL ) {
277 ber_memfree( binddn );
281 if ( passwd.bv_val != NULL ) {
282 ber_memfree( passwd.bv_val );
283 BER_BVZERO( &passwd );
286 #ifdef HAVE_CYRUS_SASL
287 if ( sasl_mech != NULL ) {
288 ber_memfree( sasl_mech );
291 #endif /* HAVE_CYRUS_SASL */
293 if ( infile != NULL ) {
294 ber_memfree( infile );
299 ber_memfree( assertion );
304 ber_memfree( authzid );
309 ber_memfree( proxydn );
313 if ( preread_attrs ) {
314 ber_memfree( preread_attrs );
315 preread_attrs = NULL;
318 if ( postread_attrs ) {
319 ber_memfree( postread_attrs );
320 postread_attrs = NULL;
323 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
324 if ( !BER_BVISNULL( &stValue ) ) {
325 ber_memfree( stValue.bv_val );
326 BER_BVZERO( &stValue );
329 if ( sessionTrackingName ) {
330 ber_memfree( sessionTrackingName );
331 sessionTrackingName = NULL;
333 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
337 tool_common_usage( void )
339 static const char *const descriptions[] = {
340 N_(" -d level set LDAP debugging level to `level'\n"),
341 N_(" -D binddn bind DN\n"),
342 N_(" -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
343 N_(" [!]assert=<filter> (RFC 4528; a RFC 4515 Filter string)\n")
344 N_(" [!]authzid=<authzid> (RFC 4370; \"dn:<dn>\" or \"u:<user>\")\n")
345 N_(" [!]bauthzid (RFC 3829)\n")
346 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
348 /* non-advertized support for proxyDN */
349 N_(" [!]proxydn=<dn> (a RFC 4514 DN string)\n")
352 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
353 N_(" [!]chaining[=<resolveBehavior>[/<continuationBehavior>]]\n")
354 N_(" one of \"chainingPreferred\", \"chainingRequired\",\n")
355 N_(" \"referralsPreferred\", \"referralsRequired\"\n")
356 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
357 N_(" [!]manageDSAit (RFC 3296)\n")
359 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
362 N_(" [!]postread[=<attrs>] (RFC 4527; comma-separated attr list)\n")
363 N_(" [!]preread[=<attrs>] (RFC 4527; comma-separated attr list)\n")
365 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
366 N_(" [!]sessiontracking[=<username>]\n")
367 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
368 N_(" abandon, cancel, ignore (SIGINT sends abandon/cancel,\n"
369 " or ignores response; if critical, doesn't wait for SIGINT.\n"
370 " not really controls)\n")
371 N_(" -h host LDAP server\n"),
372 N_(" -H URI LDAP Uniform Resource Identifier(s)\n"),
373 N_(" -I use SASL Interactive mode\n"),
374 N_(" -n show what would be done but don't actually do it\n"),
375 N_(" -N do not use reverse DNS to canonicalize SASL host name\n"),
376 N_(" -O props SASL security properties\n"),
377 N_(" -o <opt>[=<optparam>] general options\n"),
378 N_(" nettimeout=<timeout> (in seconds, or \"none\" or \"max\")\n"),
379 N_(" ldif-wrap=<width> (in columns, or \"no\" for no wrapping)\n"),
380 N_(" -p port port on LDAP server\n"),
381 N_(" -Q use SASL Quiet mode\n"),
382 N_(" -R realm SASL realm\n"),
383 N_(" -U authcid SASL authentication identity\n"),
384 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
385 N_(" -V print version info (-VV only)\n"),
386 N_(" -w passwd bind password (for simple authentication)\n"),
387 N_(" -W prompt for bind password\n"),
388 N_(" -x Simple authentication\n"),
389 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
390 N_(" -y file Read password from file\n"),
391 N_(" -Y mech SASL mechanism\n"),
392 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
395 const char *const *cpp;
397 fputs( _("Common options:\n"), stderr );
398 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
399 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
400 fputs( _(*cpp), stderr );
415 fprintf( stderr, "%s: %s (%d)%s\n",
416 func, ldap_err2string( err ), err, extra ? extra : "" );
418 if ( matched && *matched ) {
419 fprintf( stderr, _("\tmatched DN: %s\n"), matched );
422 if ( info && *info ) {
423 fprintf( stderr, _("\tadditional info: %s\n"), info );
426 if ( refs && *refs ) {
428 fprintf( stderr, _("\treferrals:\n") );
429 for( i=0; refs[i]; i++ ) {
430 fprintf( stderr, "\t\t%s\n", refs[i] );
437 tool_args( int argc, char **argv )
441 while (( i = getopt( argc, argv, options )) != EOF ) {
443 char *control, *cvalue, *next;
445 case 'c': /* continuous operation mode */
448 case 'C': /* referrals: obsolete */
452 ival = strtol( optarg, &next, 10 );
453 if (next == NULL || next[0] != '\0') {
454 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
459 case 'D': /* bind DN */
460 if( binddn != NULL ) {
461 fprintf( stderr, "%s: -D previously specified\n", prog );
462 exit( EXIT_FAILURE );
464 binddn = ber_strdup( optarg );
466 case 'e': /* general extensions (controls and such) */
467 /* should be extended to support comma separated list of
468 * [!]key[=value] parameters, e.g. -e !foo,bar=567
473 while ( optarg[0] == '!' ) {
478 control = ber_strdup( optarg );
479 if ( (cvalue = strchr( control, '=' )) != NULL ) {
483 if ( strcasecmp( control, "assert" ) == 0 ) {
485 fprintf( stderr, "assert control previously specified\n");
486 exit( EXIT_FAILURE );
488 if( cvalue == NULL ) {
489 fprintf( stderr, "assert: control value expected\n" );
493 assertctl = 1 + crit;
495 assert( assertion == NULL );
496 assertion = ber_strdup( cvalue );
498 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
499 if( authzid != NULL ) {
500 fprintf( stderr, "authzid control previously specified\n");
501 exit( EXIT_FAILURE );
503 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
504 if( proxydn != NULL ) {
505 fprintf( stderr, "authzid control incompatible with proxydn\n");
506 exit( EXIT_FAILURE );
508 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
509 if( cvalue == NULL ) {
510 fprintf( stderr, "authzid: control value expected\n" );
514 fprintf( stderr, "authzid: must be marked critical\n" );
516 } else if ( crit > 1 ) {
517 /* purposely flag proxied authorization
518 * as non-critical, to test DSA */
522 assert( authzid == NULL );
523 authzid = ber_strdup( cvalue );
525 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
526 } else if ( strcasecmp( control, "proxydn" ) == 0 ) {
527 if( proxydn != NULL ) {
528 fprintf( stderr, "proxydn control previously specified\n");
529 exit( EXIT_FAILURE );
531 if( authzid != NULL ) {
532 fprintf( stderr, "proxydn control incompatible with authzid\n");
533 exit( EXIT_FAILURE );
535 if( cvalue == NULL ) {
536 fprintf( stderr, "proxydn: control value expected\n" );
540 fprintf( stderr, "proxydn: must be marked critical\n" );
542 } else if ( crit > 1 ) {
543 /* purposely flag proxied authorization
544 * as non-critical, to test DSA */
548 assert( proxydn == NULL );
549 proxydn = ber_strdup( cvalue );
550 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
552 } else if ( strcasecmp( control, "bauthzid" ) == 0 ) {
554 fprintf( stderr, "bauthzid control previously specified\n");
555 exit( EXIT_FAILURE );
557 if( cvalue != NULL ) {
558 fprintf( stderr, "bauthzid: no control value expected\n" );
563 } else if ( ( strcasecmp( control, "relax" ) == 0 ) ||
564 ( strcasecmp( control, "manageDIT" ) == 0 ) )
568 "relax control previously specified\n");
569 exit( EXIT_FAILURE );
571 if( cvalue != NULL ) {
573 "relax: no control value expected\n" );
577 manageDIT = 1 + crit;
579 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
582 "manageDSAit control previously specified\n");
583 exit( EXIT_FAILURE );
585 if( cvalue != NULL ) {
587 "manageDSAit: no control value expected\n" );
591 manageDSAit = 1 + crit;
593 } else if ( strcasecmp( control, "noop" ) == 0 ) {
595 fprintf( stderr, "noop control previously specified\n");
596 exit( EXIT_FAILURE );
598 if( cvalue != NULL ) {
599 fprintf( stderr, "noop: no control value expected\n" );
605 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
606 } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
608 fprintf( stderr, "ppolicy control previously specified\n");
609 exit( EXIT_FAILURE );
611 if( cvalue != NULL ) {
612 fprintf( stderr, "ppolicy: no control value expected\n" );
616 fprintf( stderr, "ppolicy: critical flag not allowed\n" );
623 } else if ( strcasecmp( control, "preread" ) == 0 ) {
625 fprintf( stderr, "preread control previously specified\n");
626 exit( EXIT_FAILURE );
630 preread_attrs = ber_strdup( cvalue );
632 } else if ( strcasecmp( control, "postread" ) == 0 ) {
634 fprintf( stderr, "postread control previously specified\n");
635 exit( EXIT_FAILURE );
639 postread_attrs = ber_strdup( cvalue );
641 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
642 } else if ( strcasecmp( control, "chaining" ) == 0 ) {
644 fprintf( stderr, "chaining control previously specified\n");
645 exit( EXIT_FAILURE );
650 if ( cvalue != NULL ) {
653 continuation = strchr( cvalue, '/' );
654 if ( continuation ) {
655 /* FIXME: this makes sense only in searches */
656 *continuation++ = '\0';
657 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
658 chainingContinuation = LDAP_CHAINING_PREFERRED;
659 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
660 chainingContinuation = LDAP_CHAINING_REQUIRED;
661 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
662 chainingContinuation = LDAP_REFERRALS_PREFERRED;
663 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
664 chainingContinuation = LDAP_REFERRALS_REQUIRED;
667 "chaining behavior control "
668 "continuation value \"%s\" invalid\n",
670 exit( EXIT_FAILURE );
674 if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
675 chainingResolve = LDAP_CHAINING_PREFERRED;
676 } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
677 chainingResolve = LDAP_CHAINING_REQUIRED;
678 } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
679 chainingResolve = LDAP_REFERRALS_PREFERRED;
680 } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
681 chainingResolve = LDAP_REFERRALS_REQUIRED;
684 "chaining behavior control "
685 "resolve value \"%s\" invalid\n",
687 exit( EXIT_FAILURE );
690 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
692 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
693 } else if ( strcasecmp( control, "sessiontracking" ) == 0 ) {
694 if ( sessionTracking ) {
695 fprintf( stderr, "%s: session tracking can be only specified once\n", prog );
696 exit( EXIT_FAILURE );
700 fprintf( stderr, "sessiontracking: critical flag not allowed\n" );
704 sessionTrackingName = ber_strdup( cvalue );
706 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
708 /* this shouldn't go here, really; but it's a feature... */
709 } else if ( strcasecmp( control, "abandon" ) == 0 ) {
710 abcan = Intr_Abandon;
715 } else if ( strcasecmp( control, "cancel" ) == 0 ) {
721 } else if ( strcasecmp( control, "ignore" ) == 0 ) {
727 } else if ( tool_is_oid( control ) ) {
728 LDAPControl *tmpctrls, ctrl;
730 if ( unknown_ctrls != NULL ) {
732 for ( i = 0; unknown_ctrls[ i ].ldctl_oid != NULL; i++ ) {
733 if ( strcmp( control, unknown_ctrls[ i ].ldctl_oid ) == 0 ) {
734 fprintf( stderr, "%s control previously specified\n", control );
735 exit( EXIT_FAILURE );
740 tmpctrls = (LDAPControl *)ber_memrealloc( unknown_ctrls,
741 (unknown_ctrls_num + 1)*sizeof( LDAPControl ) );
742 if ( tmpctrls == NULL ) {
743 fprintf( stderr, "%s: no memory?\n", prog );
744 exit( EXIT_FAILURE );
746 unknown_ctrls = tmpctrls;
747 ctrl.ldctl_oid = control;
750 ctrl.ldctl_value.bv_val = NULL;
751 ctrl.ldctl_value.bv_len = 0;
752 ctrl.ldctl_iscritical = crit;
754 if ( cvalue != NULL ) {
756 size_t len = strlen( cvalue );
759 bv.bv_len = LUTIL_BASE64_DECODE_LEN( len );
760 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
762 retcode = lutil_b64_pton( cvalue,
763 (unsigned char *)bv.bv_val,
766 if ( retcode == -1 || (unsigned) retcode > bv.bv_len ) {
767 fprintf( stderr, "Unable to parse value of general control %s\n",
773 ctrl.ldctl_value = bv;
776 unknown_ctrls[ unknown_ctrls_num ] = ctrl;
780 fprintf( stderr, "Invalid general control name: %s\n",
785 ber_memfree( control );
789 case 'f': /* read from file */
790 if( infile != NULL ) {
791 fprintf( stderr, "%s: -f previously specified\n", prog );
792 exit( EXIT_FAILURE );
794 infile = ber_strdup( optarg );
796 case 'h': /* ldap host */
797 if( ldaphost != NULL ) {
798 fprintf( stderr, "%s: -h previously specified\n", prog );
799 exit( EXIT_FAILURE );
801 ldaphost = ber_strdup( optarg );
803 case 'H': /* ldap URI */
804 if( ldapuri != NULL ) {
805 fprintf( stderr, "%s: -H previously specified\n", prog );
806 exit( EXIT_FAILURE );
808 ldapuri = ber_strdup( optarg );
811 #ifdef HAVE_CYRUS_SASL
812 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
813 fprintf( stderr, "%s: incompatible previous "
814 "authentication choice\n",
816 exit( EXIT_FAILURE );
818 authmethod = LDAP_AUTH_SASL;
819 sasl_flags = LDAP_SASL_INTERACTIVE;
822 fprintf( stderr, "%s: was not compiled with SASL support\n",
824 exit( EXIT_FAILURE );
827 /* enable Manage DSA IT */
830 case 'n': /* print operations, don't actually do them */
837 control = ber_strdup( optarg );
838 if ( (cvalue = strchr( control, '=' )) != NULL ) {
842 if ( strcasecmp( control, "nettimeout" ) == 0 ) {
843 if( nettimeout.tv_sec != -1 ) {
844 fprintf( stderr, "nettimeout option previously specified\n");
845 exit( EXIT_FAILURE );
847 if( cvalue == NULL || cvalue[0] == '\0' ) {
848 fprintf( stderr, "nettimeout: option value expected\n" );
851 if ( strcasecmp( cvalue, "none" ) == 0 ) {
852 nettimeout.tv_sec = 0;
853 } else if ( strcasecmp( cvalue, "max" ) == 0 ) {
854 nettimeout.tv_sec = LDAP_MAXINT;
856 ival = strtol( cvalue, &next, 10 );
857 if ( next == NULL || next[0] != '\0' ) {
859 _("Unable to parse network timeout \"%s\"\n"), cvalue );
860 exit( EXIT_FAILURE );
862 nettimeout.tv_sec = ival;
864 if( nettimeout.tv_sec < 0 || nettimeout.tv_sec > LDAP_MAXINT ) {
865 fprintf( stderr, _("%s: invalid network timeout (%ld) specified\n"),
866 prog, (long)nettimeout.tv_sec );
867 exit( EXIT_FAILURE );
870 } else if ( strcasecmp( control, "ldif-wrap" ) == 0 ) {
872 ldif_wrap = LDIF_LINE_WIDTH;
874 } else if ( strcasecmp( cvalue, "no" ) == 0 ) {
875 ldif_wrap = LDIF_LINE_WIDTH_MAX;
879 if ( lutil_atou( &u, cvalue ) ) {
881 _("Unable to parse ldif-wrap=\"%s\"\n"), cvalue );
882 exit( EXIT_FAILURE );
884 ldif_wrap = (ber_len_t)u;
888 fprintf( stderr, "Invalid general option name: %s\n",
892 ber_memfree(control);
895 #ifdef HAVE_CYRUS_SASL
896 if( sasl_secprops != NULL ) {
897 fprintf( stderr, "%s: -O previously specified\n", prog );
898 exit( EXIT_FAILURE );
900 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
901 fprintf( stderr, "%s: incompatible previous "
902 "authentication choice\n", prog );
903 exit( EXIT_FAILURE );
905 authmethod = LDAP_AUTH_SASL;
906 sasl_secprops = ber_strdup( optarg );
908 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
909 exit( EXIT_FAILURE );
914 fprintf( stderr, "%s: -p previously specified\n", prog );
915 exit( EXIT_FAILURE );
917 ival = strtol( optarg, &next, 10 );
918 if ( next == NULL || next[0] != '\0' ) {
919 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
920 exit( EXIT_FAILURE );
925 ival = strtol( optarg, &next, 10 );
926 if ( next == NULL || next[0] != '\0' ) {
927 fprintf( stderr, "%s: unable to parse protocol version \"%s\"\n", prog, optarg );
928 exit( EXIT_FAILURE );
932 if( protocol == LDAP_VERSION3 ) {
933 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
935 exit( EXIT_FAILURE );
937 protocol = LDAP_VERSION2;
940 if( protocol == LDAP_VERSION2 ) {
941 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
943 exit( EXIT_FAILURE );
945 protocol = LDAP_VERSION3;
948 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
954 #ifdef HAVE_CYRUS_SASL
955 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
956 fprintf( stderr, "%s: incompatible previous "
957 "authentication choice\n",
959 exit( EXIT_FAILURE );
961 authmethod = LDAP_AUTH_SASL;
962 sasl_flags = LDAP_SASL_QUIET;
965 fprintf( stderr, "%s: not compiled with SASL support\n",
967 exit( EXIT_FAILURE );
970 #ifdef HAVE_CYRUS_SASL
971 if( sasl_realm != NULL ) {
972 fprintf( stderr, "%s: -R previously specified\n", prog );
973 exit( EXIT_FAILURE );
975 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
976 fprintf( stderr, "%s: incompatible previous "
977 "authentication choice\n",
979 exit( EXIT_FAILURE );
981 authmethod = LDAP_AUTH_SASL;
982 sasl_realm = ber_strdup( optarg );
984 fprintf( stderr, "%s: not compiled with SASL support\n",
986 exit( EXIT_FAILURE );
990 #ifdef HAVE_CYRUS_SASL
991 if( sasl_authc_id != NULL ) {
992 fprintf( stderr, "%s: -U previously specified\n", prog );
993 exit( EXIT_FAILURE );
995 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
996 fprintf( stderr, "%s: incompatible previous "
997 "authentication choice\n",
999 exit( EXIT_FAILURE );
1001 authmethod = LDAP_AUTH_SASL;
1002 sasl_authc_id = ber_strdup( optarg );
1004 fprintf( stderr, "%s: not compiled with SASL support\n",
1006 exit( EXIT_FAILURE );
1009 case 'v': /* verbose mode */
1012 case 'V': /* version */
1015 case 'w': /* password */
1016 passwd.bv_val = ber_strdup( optarg );
1020 for( p = optarg; *p != '\0'; p++ ) {
1024 passwd.bv_len = strlen( passwd.bv_val );
1033 #ifdef HAVE_CYRUS_SASL
1034 if( sasl_mech != NULL ) {
1035 fprintf( stderr, "%s: -Y previously specified\n", prog );
1036 exit( EXIT_FAILURE );
1038 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
1040 "%s: incompatible with authentication choice\n", prog );
1041 exit( EXIT_FAILURE );
1043 authmethod = LDAP_AUTH_SASL;
1044 sasl_mech = ber_strdup( optarg );
1046 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1047 exit( EXIT_FAILURE );
1051 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
1052 fprintf( stderr, "%s: incompatible with previous "
1053 "authentication choice\n", prog );
1054 exit( EXIT_FAILURE );
1056 authmethod = LDAP_AUTH_SIMPLE;
1059 #ifdef HAVE_CYRUS_SASL
1060 if( sasl_authz_id != NULL ) {
1061 fprintf( stderr, "%s: -X previously specified\n", prog );
1062 exit( EXIT_FAILURE );
1064 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
1065 fprintf( stderr, "%s: -X incompatible with "
1066 "authentication choice\n", prog );
1067 exit( EXIT_FAILURE );
1069 authmethod = LDAP_AUTH_SASL;
1070 sasl_authz_id = ber_strdup( optarg );
1072 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1073 exit( EXIT_FAILURE );
1080 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
1081 exit( EXIT_FAILURE );
1085 if( handle_private_option( i ) ) break;
1086 fprintf( stderr, "%s: unrecognized option -%c\n",
1093 /* prevent bad linking */
1095 api.ldapai_info_version = LDAP_API_INFO_VERSION;
1097 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
1098 != LDAP_OPT_SUCCESS )
1100 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
1101 exit( EXIT_FAILURE );
1104 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
1105 fprintf( stderr, "LDAP APIInfo version mismatch: "
1106 "library %d, header %d\n",
1107 api.ldapai_info_version, LDAP_API_INFO_VERSION );
1108 exit( EXIT_FAILURE );
1111 if( api.ldapai_api_version != LDAP_API_VERSION ) {
1112 fprintf( stderr, "LDAP API version mismatch: "
1113 "library %d, header %d\n",
1114 api.ldapai_api_version, LDAP_API_VERSION );
1115 exit( EXIT_FAILURE );
1118 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
1119 fprintf( stderr, "LDAP vendor name mismatch: "
1120 "library %s, header %s\n",
1121 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
1122 exit( EXIT_FAILURE );
1125 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
1126 fprintf( stderr, "LDAP vendor version mismatch: "
1127 "library %d, header %d\n",
1128 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
1129 exit( EXIT_FAILURE );
1133 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
1135 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
1136 if (version > 1) exit( EXIT_SUCCESS );
1139 ldap_memfree( api.ldapai_vendor_name );
1140 ber_memvfree( (void **)api.ldapai_extensions );
1144 protocol = LDAP_VERSION3;
1146 if (authmethod == -1 && protocol > LDAP_VERSION2) {
1147 #ifdef HAVE_CYRUS_SASL
1148 if ( binddn != NULL ) {
1149 authmethod = LDAP_AUTH_SIMPLE;
1151 authmethod = LDAP_AUTH_SASL;
1154 authmethod = LDAP_AUTH_SIMPLE;
1158 if( ldapuri == NULL ) {
1159 if( ldapport && ( ldaphost == NULL )) {
1160 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
1161 exit( EXIT_FAILURE );
1164 if( ldaphost != NULL ) {
1165 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
1166 exit( EXIT_FAILURE );
1169 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
1170 exit( EXIT_FAILURE );
1174 if( protocol == LDAP_VERSION2 ) {
1175 if( assertctl || authzid || manageDIT || manageDSAit ||
1176 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1178 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1179 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1182 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1184 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1185 noop || ppolicy || preread || postread )
1187 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
1188 exit( EXIT_FAILURE );
1192 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
1193 exit( EXIT_FAILURE );
1196 #ifdef HAVE_CYRUS_SASL
1197 if( authmethod == LDAP_AUTH_SASL ) {
1198 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
1200 exit( EXIT_FAILURE );
1205 if ( ( pw_file || want_bindpw ) && !BER_BVISNULL( &passwd ) ) {
1206 fprintf( stderr, "%s: -%c incompatible with -w\n",
1207 prog, ( pw_file ? 'y' : 'W' ) );
1208 exit( EXIT_FAILURE );
1214 tool_conn_setup( int dont, void (*private_setup)( LDAP * ) )
1219 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
1220 != LBER_OPT_SUCCESS )
1223 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
1225 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
1226 != LDAP_OPT_SUCCESS )
1229 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
1234 (void) SIGNAL( SIGPIPE, SIG_IGN );
1238 SIGNAL( SIGINT, do_sig );
1244 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
1247 memset( &url, 0, sizeof(url));
1249 url.lud_scheme = "ldap";
1250 url.lud_host = ldaphost;
1251 url.lud_port = ldapport;
1252 url.lud_scope = LDAP_SCOPE_DEFAULT;
1254 ldapuri = ldap_url_desc2str( &url );
1256 } else if ( ldapuri != NULL ) {
1257 LDAPURLDesc *ludlist, **ludp;
1261 rc = ldap_url_parselist( &ludlist, ldapuri );
1262 if ( rc != LDAP_URL_SUCCESS ) {
1264 "Could not parse LDAP URI(s)=%s (%d)\n",
1266 exit( EXIT_FAILURE );
1269 for ( ludp = &ludlist; *ludp != NULL; ) {
1270 LDAPURLDesc *lud = *ludp;
1273 if ( lud->lud_dn != NULL && lud->lud_dn[ 0 ] != '\0' &&
1274 ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) )
1276 /* if no host but a DN is provided,
1277 * use DNS SRV to gather the host list
1278 * and turn it into a list of URIs
1279 * using the scheme provided */
1280 char *domain = NULL,
1284 len_proto = strlen( lud->lud_scheme );
1286 if ( ldap_dn2domain( lud->lud_dn, &domain )
1290 "DNS SRV: Could not turn "
1291 "DN=\"%s\" into a domain\n",
1296 rc = ldap_domain2hostlist( domain, &hostlist );
1299 "DNS SRV: Could not turn "
1300 "domain=%s into a hostlist\n",
1305 hosts = ldap_str2charray( hostlist, " " );
1306 if ( hosts == NULL ) {
1308 "DNS SRV: Could not parse "
1309 "hostlist=\"%s\"\n",
1314 for ( i = 0; hosts[ i ] != NULL; i++ )
1317 tmp = (char **)ber_memrealloc( urls, sizeof( char * ) * ( nurls + i + 1 ) );
1318 if ( tmp == NULL ) {
1320 "DNS SRV: out of memory?\n" );
1324 urls[ nurls ] = NULL;
1326 for ( i = 0; hosts[ i ] != NULL; i++ ) {
1327 size_t len = len_proto
1329 + strlen( hosts[ i ] )
1332 urls[ nurls + i + 1 ] = NULL;
1333 urls[ nurls + i ] = (char *)malloc( sizeof( char ) * len );
1334 if ( urls[ nurls + i ] == NULL ) {
1336 "DNS SRV: out of memory?\n" );
1340 snprintf( urls[ nurls + i ], len, "%s://%s",
1341 lud->lud_scheme, hosts[ i ] );
1346 ber_memvfree( (void **)hosts );
1347 ber_memfree( hostlist );
1348 ber_memfree( domain );
1351 tmp = (char **)ber_memrealloc( urls, sizeof( char * ) * ( nurls + 2 ) );
1352 if ( tmp == NULL ) {
1354 "DNS SRV: out of memory?\n" );
1358 urls[ nurls + 1 ] = NULL;
1360 urls[ nurls ] = ldap_url_desc2str( lud );
1361 if ( urls[ nurls ] == NULL ) {
1363 "DNS SRV: out of memory?\n" );
1369 *ludp = lud->lud_next;
1371 lud->lud_next = NULL;
1372 ldap_free_urldesc( lud );
1375 if ( ludlist != NULL ) {
1376 ldap_free_urllist( ludlist );
1377 exit( EXIT_FAILURE );
1379 } else if ( urls == NULL ) {
1380 exit( EXIT_FAILURE );
1383 ldap_memfree( ldapuri );
1384 ldapuri = ldap_charray2str( urls, " " );
1385 ber_memvfree( (void **)urls );
1389 fprintf( stderr, "ldap_initialize( %s )\n",
1390 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
1392 rc = ldap_initialize( &ld, ldapuri );
1393 if( rc != LDAP_SUCCESS ) {
1395 "Could not create LDAP session handle for URI=%s (%d): %s\n",
1396 ldapuri, rc, ldap_err2string(rc) );
1397 exit( EXIT_FAILURE );
1400 if( private_setup ) private_setup( ld );
1402 /* referrals: obsolete */
1403 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
1404 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
1406 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
1407 referrals ? "on" : "off" );
1408 tool_exit( ld, EXIT_FAILURE );
1411 #ifdef HAVE_CYRUS_SASL
1413 if( ldap_set_option( ld, LDAP_OPT_X_SASL_NOCANON,
1414 nocanon ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
1416 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_NOCANON %s\n",
1417 nocanon ? "on" : "off" );
1418 tool_exit( ld, EXIT_FAILURE );
1421 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
1422 != LDAP_OPT_SUCCESS )
1424 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
1426 tool_exit( ld, EXIT_FAILURE );
1430 rc = ldap_start_tls_s( ld, NULL, NULL );
1431 if ( rc != LDAP_SUCCESS ) {
1433 ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&msg);
1434 tool_perror( "ldap_start_tls", rc, NULL, NULL, msg, NULL );
1436 if ( use_tls > 1 ) {
1437 tool_exit( ld, EXIT_FAILURE );
1442 if ( nettimeout.tv_sec > 0 ) {
1443 if ( ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &nettimeout )
1444 != LDAP_OPT_SUCCESS )
1446 fprintf( stderr, "Could not set LDAP_OPT_NETWORK_TIMEOUT %ld\n",
1447 (long)nettimeout.tv_sec );
1448 tool_exit( ld, EXIT_FAILURE );
1458 tool_bind( LDAP *ld )
1460 LDAPControl **sctrlsp = NULL;
1461 LDAPControl *sctrls[4];
1462 LDAPControl sctrl[3];
1466 LDAPMessage *result = NULL;
1469 char *matched = NULL;
1472 LDAPControl **ctrls = NULL;
1477 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1480 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1481 c.ldctl_value.bv_val = NULL;
1482 c.ldctl_value.bv_len = 0;
1483 c.ldctl_iscritical = 0;
1485 sctrls[nsctrls] = &sctrl[nsctrls];
1486 sctrls[++nsctrls] = NULL;
1493 c.ldctl_oid = LDAP_CONTROL_AUTHZID_REQUEST;
1494 c.ldctl_iscritical = bauthzid > 1;
1495 BER_BVZERO( &c.ldctl_value );
1498 sctrls[nsctrls] = &sctrl[nsctrls];
1499 sctrls[++nsctrls] = NULL;
1502 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1503 if ( sessionTracking ) {
1506 if ( BER_BVISNULL( &stValue) && st_value( ld, &stValue ) ) {
1507 tool_exit( ld, EXIT_FAILURE );
1510 c.ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1511 c.ldctl_iscritical = 0;
1512 c.ldctl_value = stValue;
1515 sctrls[nsctrls] = &sctrl[nsctrls];
1516 sctrls[++nsctrls] = NULL;
1518 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1524 assert( nsctrls < (int) (sizeof(sctrls)/sizeof(sctrls[0])) );
1526 if ( pw_file || want_bindpw ) {
1527 assert( passwd.bv_val == NULL && passwd.bv_len == 0 );
1530 if ( lutil_get_filed_password( pw_file, &passwd ) ) {
1531 tool_exit( ld, EXIT_FAILURE );
1535 char *pw = getpassphrase( _("Enter LDAP Password: ") );
1537 passwd.bv_val = ber_strdup( pw );
1538 passwd.bv_len = strlen( passwd.bv_val );
1543 if ( authmethod == LDAP_AUTH_SASL ) {
1544 #ifdef HAVE_CYRUS_SASL
1546 const char *rmech = NULL;
1548 if( sasl_secprops != NULL ) {
1549 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
1550 (void *) sasl_secprops );
1552 if( rc != LDAP_OPT_SUCCESS ) {
1554 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
1556 tool_exit( ld, LDAP_LOCAL_ERROR );
1560 defaults = lutil_sasl_defaults( ld,
1568 rc = ldap_sasl_interactive_bind( ld, binddn, sasl_mech,
1569 sctrlsp, NULL, sasl_flags, lutil_sasl_interact, defaults,
1570 result, &rmech, &msgid );
1572 if ( rc != LDAP_SASL_BIND_IN_PROGRESS )
1575 ldap_msgfree( result );
1577 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 || !result ) {
1578 ldap_get_option( ld, LDAP_OPT_RESULT_CODE, (void*)&err );
1579 ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&info );
1580 tool_perror( "ldap_sasl_interactive_bind",
1581 err, NULL, NULL, info, NULL );
1582 ldap_memfree( info );
1583 tool_exit( ld, err );
1585 } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
1587 lutil_sasl_freedefs( defaults );
1589 if ( rc != LDAP_SUCCESS ) {
1590 ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&info );
1591 tool_perror( "ldap_sasl_interactive_bind",
1592 rc, NULL, NULL, info, NULL );
1593 ldap_memfree( info );
1594 tool_exit( ld, rc );
1597 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1598 tool_exit( ld, LDAP_NOT_SUPPORTED );
1602 rc = ldap_sasl_bind( ld, binddn, LDAP_SASL_SIMPLE, &passwd,
1603 sctrlsp, NULL, &msgid );
1604 if ( msgid == -1 ) {
1605 tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
1606 NULL, NULL, NULL, NULL );
1607 tool_exit( ld, rc );
1610 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result );
1612 tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
1613 tool_exit( ld, LDAP_LOCAL_ERROR );
1617 tool_perror( "ldap_result", LDAP_TIMEOUT, NULL, NULL, NULL, NULL );
1618 tool_exit( ld, LDAP_LOCAL_ERROR );
1622 rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
1624 if ( rc != LDAP_SUCCESS ) {
1625 tool_perror( "ldap_bind parse result", rc, NULL, matched, info, refs );
1626 tool_exit( ld, LDAP_LOCAL_ERROR );
1629 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1630 if ( ctrls && ppolicy ) {
1632 int expire, grace, len = 0;
1633 LDAPPasswordPolicyError pErr = -1;
1635 ctrl = ldap_control_find( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
1638 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
1639 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1641 if ( pErr != PP_noError ){
1644 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1645 len = strlen( msgbuf );
1647 if ( expire >= 0 ) {
1648 sprintf( msgbuf+len,
1649 " (Password expires in %d seconds)",
1651 } else if ( grace >= 0 ) {
1652 sprintf( msgbuf+len,
1653 " (Password expired, %d grace logins remain)",
1660 if ( ctrls && bauthzid ) {
1663 ctrl = ldap_control_find( LDAP_CONTROL_AUTHZID_RESPONSE,
1666 LDAPControl *ctmp[2];
1669 tool_print_ctrls( ld, ctmp );
1674 ldap_controls_free( ctrls );
1677 if ( err != LDAP_SUCCESS
1679 || ( matched && matched[ 0 ] )
1680 || ( info && info[ 0 ] )
1683 tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1685 if( matched ) ber_memfree( matched );
1686 if( info ) ber_memfree( info );
1687 if( refs ) ber_memvfree( (void **)refs );
1689 if ( err != LDAP_SUCCESS ) tool_exit( ld, err );
1694 tool_unbind( LDAP *ld )
1696 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1698 if ( err != LDAP_OPT_SUCCESS ) {
1699 fprintf( stderr, "Could not unset controls\n");
1702 (void) ldap_unbind_ext( ld, NULL, NULL );
1706 tool_exit( LDAP *ld, int status )
1716 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1718 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1720 int i = 0, j, crit = 0, err;
1721 LDAPControl c[16], **ctrls;
1725 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1727 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1731 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1736 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1738 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1739 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1741 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1743 || unknown_ctrls_num ) )
1748 ctrls = (LDAPControl**) malloc(sizeof(c) + (count + unknown_ctrls_num + 1)*sizeof(LDAPControl*));
1749 if ( ctrls == NULL ) {
1750 fprintf( stderr, "No memory\n" );
1751 tool_exit( ld, EXIT_FAILURE );
1755 if ( BER_BVISNULL( &assertionvalue ) ) {
1756 err = ldap_create_assertion_control_value( ld,
1757 assertion, &assertionvalue );
1760 "Unable to create assertion value "
1761 "\"%s\" (%d)\n", assertion, err );
1765 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1766 c[i].ldctl_value = assertionvalue;
1767 c[i].ldctl_iscritical = assertctl > 1;
1773 c[i].ldctl_value.bv_val = authzid;
1774 c[i].ldctl_value.bv_len = strlen( authzid );
1775 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1776 c[i].ldctl_iscritical = authzcrit;
1781 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1782 /* NOTE: doesn't need an extra count because it's incompatible
1785 BerElementBuffer berbuf;
1786 BerElement *ber = (BerElement *)&berbuf;
1788 ber_init2( ber, NULL, LBER_USE_DER );
1790 if ( ber_printf( ber, "s", proxydn ) == -1 ) {
1791 tool_exit( ld, EXIT_FAILURE );
1794 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1795 tool_exit( ld, EXIT_FAILURE );
1798 c[i].ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1799 c[i].ldctl_iscritical = authzcrit;
1803 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1806 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1807 BER_BVZERO( &c[i].ldctl_value );
1808 c[i].ldctl_iscritical = manageDIT > 1;
1813 if ( manageDSAit ) {
1814 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1815 BER_BVZERO( &c[i].ldctl_value );
1816 c[i].ldctl_iscritical = manageDSAit > 1;
1822 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1823 BER_BVZERO( &c[i].ldctl_value );
1824 c[i].ldctl_iscritical = noop > 1;
1829 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1831 c[i].ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1832 BER_BVZERO( &c[i].ldctl_value );
1833 c[i].ldctl_iscritical = 0;
1840 BerElementBuffer berbuf;
1841 BerElement *ber = (BerElement *)&berbuf;
1842 char **attrs = NULL;
1844 if( preread_attrs ) {
1845 attrs = ldap_str2charray( preread_attrs, "," );
1848 ber_init2( ber, NULL, LBER_USE_DER );
1850 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1851 fprintf( stderr, "preread attrs encode failed.\n" );
1852 tool_exit( ld, EXIT_FAILURE );
1855 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1857 fprintf( stderr, "preread flatten failed (%d)\n", err );
1858 tool_exit( ld, EXIT_FAILURE );
1861 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1862 c[i].ldctl_iscritical = preread > 1;
1866 if( attrs ) ldap_charray_free( attrs );
1870 BerElementBuffer berbuf;
1871 BerElement *ber = (BerElement *)&berbuf;
1872 char **attrs = NULL;
1874 if( postread_attrs ) {
1875 attrs = ldap_str2charray( postread_attrs, "," );
1878 ber_init2( ber, NULL, LBER_USE_DER );
1880 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1881 fprintf( stderr, "postread attrs encode failed.\n" );
1882 tool_exit( ld, EXIT_FAILURE );
1885 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1887 fprintf( stderr, "postread flatten failed (%d)\n", err );
1888 tool_exit( ld, EXIT_FAILURE );
1891 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1892 c[i].ldctl_iscritical = postread > 1;
1896 if( attrs ) ldap_charray_free( attrs );
1899 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1901 if ( chainingResolve > -1 ) {
1902 BerElementBuffer berbuf;
1903 BerElement *ber = (BerElement *)&berbuf;
1905 ber_init2( ber, NULL, LBER_USE_DER );
1907 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1910 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1911 tool_exit( ld, EXIT_FAILURE );
1914 if ( chainingContinuation > -1 ) {
1915 err = ber_printf( ber, "e", chainingContinuation );
1918 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1919 tool_exit( ld, EXIT_FAILURE );
1923 err = ber_printf( ber, /* { */ "N}" );
1926 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1927 tool_exit( ld, EXIT_FAILURE );
1930 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1931 tool_exit( ld, EXIT_FAILURE );
1935 BER_BVZERO( &c[i].ldctl_value );
1938 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1939 c[i].ldctl_iscritical = chaining > 1;
1943 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1945 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1946 if ( sessionTracking ) {
1947 if ( BER_BVISNULL( &stValue ) && st_value( ld, &stValue ) ) {
1948 tool_exit( ld, EXIT_FAILURE );
1951 c[i].ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1952 c[i].ldctl_iscritical = 0;
1953 c[i].ldctl_value = stValue;
1958 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1961 ctrls[i++] = extra_c++;
1963 for ( count = 0; count < unknown_ctrls_num; count++ ) {
1964 ctrls[i++] = &unknown_ctrls[count];
1968 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1970 if ( err != LDAP_OPT_SUCCESS ) {
1971 for ( j = 0; j < i; j++ ) {
1972 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1974 fprintf( stderr, "Could not set %scontrols\n",
1975 crit ? "critical " : "" );
1980 tool_exit( ld, EXIT_FAILURE );
1985 tool_check_abandon( LDAP *ld, int msgid )
1989 switch ( gotintr ) {
1991 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1992 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1993 rc, ldap_err2string( rc ) );
1997 rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
1998 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1999 rc, ldap_err2string( rc ) );
2003 /* just unbind, ignoring the request */
2011 print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
2016 tool_write_ldif( LDIF_PUT_COMMENT, "==> ",
2017 what->bv_val, what->bv_len );
2018 ber = ber_init( &ctrl->ldctl_value );
2019 if ( ber == NULL ) {
2023 } else if ( ber_scanf( ber, "{m{" /*}}*/, &bv ) == LBER_ERROR ) {
2028 tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
2030 while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
2032 BerVarray vals = NULL;
2035 if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
2045 str = malloc( bv.bv_len + STRLENOF(": ") + 1 );
2048 ptr = lutil_strncopy( ptr, bv.bv_val, bv.bv_len );
2049 ptr = lutil_strcopy( ptr, ": " );
2052 for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
2054 ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2055 ldif ? str : bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
2058 ber_bvarray_free( vals );
2059 if ( str ) free( str );
2063 if ( ber != NULL ) {
2067 tool_write_ldif( LDIF_PUT_COMMENT, "<== ",
2068 what->bv_val, what->bv_len );
2074 print_preread( LDAP *ld, LDAPControl *ctrl )
2076 static struct berval what = BER_BVC( "preread" );
2078 return print_prepostread( ld, ctrl, &what );
2082 print_postread( LDAP *ld, LDAPControl *ctrl )
2084 static struct berval what = BER_BVC( "postread" );
2086 return print_prepostread( ld, ctrl, &what );
2090 print_paged_results( LDAP *ld, LDAPControl *ctrl )
2094 /* note: pr_cookie is being malloced; it's freed
2095 * the next time the control is sent, but the last
2096 * time it's not; we don't care too much, because
2097 * the last time an empty value is returned... */
2098 if ( ldap_parse_pageresponse_control( ld, ctrl, &estimate, &pr_cookie )
2105 /* FIXME: check buffer overflow */
2106 char buf[ BUFSIZ ], *ptr = buf;
2108 if ( estimate > 0 ) {
2109 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2110 "estimate=%d", estimate );
2113 if ( pr_cookie.bv_len > 0 ) {
2116 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
2117 pr_cookie.bv_len ) + 1;
2118 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
2120 bv.bv_len = lutil_b64_ntop(
2121 (unsigned char *) pr_cookie.bv_val,
2123 bv.bv_val, bv.bv_len );
2125 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2126 "%scookie=%s", ptr == buf ? "" : " ",
2129 ber_memfree( bv.bv_val );
2131 pr_morePagedResults = 1;
2134 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2135 "%scookie=", ptr == buf ? "" : " " );
2138 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2139 ldif ? "pagedresults: " : "pagedresults",
2147 print_sss( LDAP *ld, LDAPControl *ctrl )
2153 rc = ldap_parse_sortresponse_control( ld, ctrl, &err, &attr );
2154 if ( rc == LDAP_SUCCESS ) {
2156 rc = snprintf( buf, sizeof(buf), "(%d) %s%s%s",
2157 err, ldap_err2string(err), attr ? " " : "", attr ? attr : "" );
2159 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2160 ldif ? "sortResult: " : "sortResult", buf, rc );
2167 print_vlv( LDAP *ld, LDAPControl *ctrl )
2173 rc = ldap_parse_vlvresponse_control( ld, ctrl, &vlvPos, &vlvCount,
2174 &vlvContext, &err );
2175 if ( rc == LDAP_SUCCESS ) {
2178 if ( vlvContext && vlvContext->bv_len > 0 ) {
2179 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
2180 vlvContext->bv_len ) + 1;
2181 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
2183 bv.bv_len = lutil_b64_ntop(
2184 (unsigned char *) vlvContext->bv_val,
2186 bv.bv_val, bv.bv_len );
2192 rc = snprintf( buf, sizeof(buf), "pos=%d count=%d context=%s (%d) %s",
2193 vlvPos, vlvCount, bv.bv_val,
2194 err, ldap_err2string(err));
2197 ber_memfree( bv.bv_val );
2199 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2200 ldif ? "vlvResult" : "vlvResult", buf, rc );
2206 #ifdef LDAP_CONTROL_X_DEREF
2208 print_deref( LDAP *ld, LDAPControl *ctrl )
2210 LDAPDerefRes *drhead = NULL, *dr;
2213 rc = ldap_parse_derefresponse_control( ld, ctrl, &drhead );
2214 if ( rc != LDAP_SUCCESS ) {
2218 for ( dr = drhead; dr != NULL; dr = dr->next ) {
2223 len = strlen( dr->derefAttr ) + STRLENOF(": ");
2225 for ( dv = dr->attrVals; dv != NULL; dv = dv->next ) {
2226 if ( dv->vals != NULL ) {
2228 ber_len_t tlen = strlen(dv->type);
2230 for ( j = 0; dv->vals[ j ].bv_val != NULL; j++ ) {
2231 len += STRLENOF("<:=>;") + tlen + 4*((dv->vals[ j ].bv_len - 1)/3 + 1);
2235 len += dr->derefVal.bv_len + STRLENOF("\n");
2236 buf = ldap_memalloc( len + 1 );
2237 if ( buf == NULL ) {
2238 rc = LDAP_NO_MEMORY;
2243 ptr = lutil_strcopy( ptr, dr->derefAttr );
2246 for ( dv = dr->attrVals; dv != NULL; dv = dv->next ) {
2247 if ( dv->vals != NULL ) {
2249 for ( j = 0; dv->vals[ j ].bv_val != NULL; j++ ) {
2250 int k = ldif_is_not_printable( dv->vals[ j ].bv_val, dv->vals[ j ].bv_len );
2253 ptr = lutil_strcopy( ptr, dv->type );
2260 (unsigned char *) dv->vals[ j ].bv_val,
2261 dv->vals[ j ].bv_len,
2262 ptr, buf + len - ptr );
2267 ptr = lutil_memcopy( ptr, dv->vals[ j ].bv_val, dv->vals[ j ].bv_len );
2274 ptr = lutil_strncopy( ptr, dr->derefVal.bv_val, dr->derefVal.bv_len );
2277 assert( ptr <= buf + len );
2279 tool_write_ldif( LDIF_PUT_COMMENT, NULL, buf, ptr - buf);
2281 ldap_memfree( buf );
2287 ldap_derefresponse_free( drhead );
2293 #ifdef LDAP_CONTROL_X_WHATFAILED
2295 print_whatfailed( LDAP *ld, LDAPControl *ctrl )
2300 BerVarray bva = NULL;
2302 /* Create a BerElement from the berval returned in the control. */
2303 ber = ber_init( &ctrl->ldctl_value );
2305 if ( ber == NULL ) {
2306 return LDAP_NO_MEMORY;
2309 siz = sizeof(struct berval);
2310 tag = ber_scanf( ber, "[M]", &bva, &siz, 0 );
2311 if ( tag != LBER_ERROR ) {
2314 tool_write_ldif( LDIF_PUT_COMMENT, " what failed:", NULL, 0 );
2316 for ( i = 0; bva[i].bv_val != NULL; i++ ) {
2317 tool_write_ldif( LDIF_PUT_COMMENT, NULL, bva[i].bv_val, bva[i].bv_len );
2320 ldap_memfree( bva );
2330 #ifdef LDAP_CONTROL_AUTHZID_RESPONSE
2332 print_authzid( LDAP *ld, LDAPControl *ctrl )
2334 if ( ctrl->ldctl_value.bv_len ) {
2335 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2336 ldif ? "authzid: " : "authzid",
2337 ctrl->ldctl_value.bv_val, ctrl->ldctl_value.bv_len );
2339 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2340 ldif ? "authzid: " : "authzid",
2341 "anonymous", STRLENOF("anonymous") );
2348 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
2350 print_ppolicy( LDAP *ld, LDAPControl *ctrl )
2352 int expire = 0, grace = 0, rc;
2353 LDAPPasswordPolicyError pperr;
2355 rc = ldap_parse_passwordpolicy_control( ld, ctrl,
2356 &expire, &grace, &pperr );
2357 if ( rc == LDAP_SUCCESS ) {
2358 char buf[ BUFSIZ ], *ptr = buf;
2360 if ( expire != -1 ) {
2361 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2362 "expire=%d", expire );
2365 if ( grace != -1 ) {
2366 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2367 "%sgrace=%d", ptr == buf ? "" : " ", grace );
2370 if ( pperr != PP_noError ) {
2371 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2372 "%serror=%d (%s)", ptr == buf ? "" : " ",
2374 ldap_passwordpolicy_err2txt( pperr ) );
2377 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2378 ldif ? "ppolicy: " : "ppolicy", buf, ptr - buf );
2385 void tool_print_ctrls(
2387 LDAPControl **ctrls )
2392 for ( i = 0; ctrls[i] != NULL; i++ ) {
2393 /* control: OID criticality base64value */
2394 struct berval b64 = BER_BVNULL;
2399 /* FIXME: there might be cases where a control has NULL OID;
2400 * this makes little sense, especially when returned by the
2401 * server, but libldap happily allows it */
2402 if ( ctrls[i]->ldctl_oid == NULL ) {
2407 len += strlen( ctrls[i]->ldctl_oid );
2409 /* add enough for space after OID and the critical value itself */
2410 len += ctrls[i]->ldctl_iscritical
2411 ? sizeof("true") : sizeof("false");
2413 /* convert to base64 */
2414 if ( !BER_BVISNULL( &ctrls[i]->ldctl_value ) ) {
2415 b64.bv_len = LUTIL_BASE64_ENCODE_LEN(
2416 ctrls[i]->ldctl_value.bv_len ) + 1;
2417 b64.bv_val = ber_memalloc( b64.bv_len + 1 );
2419 b64.bv_len = lutil_b64_ntop(
2420 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
2421 ctrls[i]->ldctl_value.bv_len,
2422 b64.bv_val, b64.bv_len );
2426 len += 1 + b64.bv_len;
2429 ptr = str = malloc( len + 1 );
2431 ptr = lutil_strcopy( ptr, ": " );
2433 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_oid );
2434 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_iscritical
2435 ? " true" : " false" );
2438 ptr = lutil_strcopy( ptr, " " );
2439 ptr = lutil_strcopy( ptr, b64.bv_val );
2443 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2444 "control", str, len );
2449 ber_memfree( b64.bv_val );
2452 /* known controls */
2453 for ( j = 0; tool_ctrl_response[j].oid != NULL; j++ ) {
2454 if ( strcmp( tool_ctrl_response[j].oid, ctrls[i]->ldctl_oid ) == 0 ) {
2455 if ( !tool_ctrl_response[j].mask & tool_type ) {
2456 /* this control should not appear
2457 * with this tool; warning? */
2463 if ( tool_ctrl_response[j].oid != NULL && tool_ctrl_response[j].func ) {
2464 (void)tool_ctrl_response[j].func( ld, ctrls[i] );
2470 tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
2474 if (( ldif = ldif_put_wrap( type, name, value, vallen, ldif_wrap )) == NULL ) {
2478 fputs( ldif, stdout );
2479 ber_memfree( ldif );
2485 tool_is_oid( const char *s )
2489 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2493 for ( ; s[ 0 ]; s++ ) {
2494 if ( s[ 0 ] == '.' ) {
2495 if ( s[ 1 ] == '\0' ) {
2502 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2506 if ( first == 1 && s[ 0 ] == '0' && s[ 1 ] != '.' ) {