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-2014 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 = 0;
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
1414 if( ldap_set_option( ld, LDAP_OPT_X_SASL_NOCANON,
1415 LDAP_OPT_ON ) != LDAP_OPT_SUCCESS )
1417 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_NOCANON on\n" );
1418 tool_exit( ld, EXIT_FAILURE );
1422 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
1423 != LDAP_OPT_SUCCESS )
1425 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
1427 tool_exit( ld, EXIT_FAILURE );
1431 rc = ldap_start_tls_s( ld, NULL, NULL );
1432 if ( rc != LDAP_SUCCESS ) {
1434 ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&msg);
1435 tool_perror( "ldap_start_tls", rc, NULL, NULL, msg, NULL );
1437 if ( use_tls > 1 ) {
1438 tool_exit( ld, EXIT_FAILURE );
1443 if ( nettimeout.tv_sec > 0 ) {
1444 if ( ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &nettimeout )
1445 != LDAP_OPT_SUCCESS )
1447 fprintf( stderr, "Could not set LDAP_OPT_NETWORK_TIMEOUT %ld\n",
1448 (long)nettimeout.tv_sec );
1449 tool_exit( ld, EXIT_FAILURE );
1459 tool_bind( LDAP *ld )
1461 LDAPControl **sctrlsp = NULL;
1462 LDAPControl *sctrls[4];
1463 LDAPControl sctrl[3];
1467 LDAPMessage *result = NULL;
1470 char *matched = NULL;
1473 LDAPControl **ctrls = NULL;
1478 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1481 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1482 c.ldctl_value.bv_val = NULL;
1483 c.ldctl_value.bv_len = 0;
1484 c.ldctl_iscritical = 0;
1486 sctrls[nsctrls] = &sctrl[nsctrls];
1487 sctrls[++nsctrls] = NULL;
1494 c.ldctl_oid = LDAP_CONTROL_AUTHZID_REQUEST;
1495 c.ldctl_iscritical = bauthzid > 1;
1496 BER_BVZERO( &c.ldctl_value );
1499 sctrls[nsctrls] = &sctrl[nsctrls];
1500 sctrls[++nsctrls] = NULL;
1503 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1504 if ( sessionTracking ) {
1507 if ( BER_BVISNULL( &stValue) && st_value( ld, &stValue ) ) {
1508 tool_exit( ld, EXIT_FAILURE );
1511 c.ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1512 c.ldctl_iscritical = 0;
1513 c.ldctl_value = stValue;
1516 sctrls[nsctrls] = &sctrl[nsctrls];
1517 sctrls[++nsctrls] = NULL;
1519 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1525 assert( nsctrls < (int) (sizeof(sctrls)/sizeof(sctrls[0])) );
1527 if ( pw_file || want_bindpw ) {
1528 assert( passwd.bv_val == NULL && passwd.bv_len == 0 );
1531 if ( lutil_get_filed_password( pw_file, &passwd ) ) {
1532 tool_exit( ld, EXIT_FAILURE );
1536 char *pw = getpassphrase( _("Enter LDAP Password: ") );
1538 passwd.bv_val = ber_strdup( pw );
1539 passwd.bv_len = strlen( passwd.bv_val );
1544 if ( authmethod == LDAP_AUTH_SASL ) {
1545 #ifdef HAVE_CYRUS_SASL
1547 const char *rmech = NULL;
1549 if( sasl_secprops != NULL ) {
1550 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
1551 (void *) sasl_secprops );
1553 if( rc != LDAP_OPT_SUCCESS ) {
1555 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
1557 tool_exit( ld, LDAP_LOCAL_ERROR );
1561 defaults = lutil_sasl_defaults( ld,
1569 rc = ldap_sasl_interactive_bind( ld, binddn, sasl_mech,
1570 sctrlsp, NULL, sasl_flags, lutil_sasl_interact, defaults,
1571 result, &rmech, &msgid );
1573 if ( rc != LDAP_SASL_BIND_IN_PROGRESS )
1576 ldap_msgfree( result );
1578 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 || !result ) {
1579 ldap_get_option( ld, LDAP_OPT_RESULT_CODE, (void*)&err );
1580 ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&info );
1581 tool_perror( "ldap_sasl_interactive_bind",
1582 err, NULL, NULL, info, NULL );
1583 ldap_memfree( info );
1584 tool_exit( ld, err );
1586 } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
1588 lutil_sasl_freedefs( defaults );
1590 if ( rc != LDAP_SUCCESS ) {
1591 ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&info );
1592 tool_perror( "ldap_sasl_interactive_bind",
1593 rc, NULL, NULL, info, NULL );
1594 ldap_memfree( info );
1595 tool_exit( ld, rc );
1598 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1599 tool_exit( ld, LDAP_NOT_SUPPORTED );
1603 rc = ldap_sasl_bind( ld, binddn, LDAP_SASL_SIMPLE, &passwd,
1604 sctrlsp, NULL, &msgid );
1605 if ( msgid == -1 ) {
1606 tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
1607 NULL, NULL, NULL, NULL );
1608 tool_exit( ld, rc );
1611 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result );
1613 tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
1614 tool_exit( ld, LDAP_LOCAL_ERROR );
1618 tool_perror( "ldap_result", LDAP_TIMEOUT, NULL, NULL, NULL, NULL );
1619 tool_exit( ld, LDAP_LOCAL_ERROR );
1624 rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
1626 if ( rc != LDAP_SUCCESS ) {
1627 tool_perror( "ldap_bind parse result", rc, NULL, matched, info, refs );
1628 tool_exit( ld, LDAP_LOCAL_ERROR );
1632 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1633 if ( ctrls && ppolicy ) {
1635 int expire, grace, len = 0;
1636 LDAPPasswordPolicyError pErr = -1;
1638 ctrl = ldap_control_find( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
1641 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
1642 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1644 if ( pErr != PP_noError ){
1647 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1648 len = strlen( msgbuf );
1650 if ( expire >= 0 ) {
1651 sprintf( msgbuf+len,
1652 " (Password expires in %d seconds)",
1654 } else if ( grace >= 0 ) {
1655 sprintf( msgbuf+len,
1656 " (Password expired, %d grace logins remain)",
1663 if ( ctrls && bauthzid ) {
1666 ctrl = ldap_control_find( LDAP_CONTROL_AUTHZID_RESPONSE,
1669 LDAPControl *ctmp[2];
1672 tool_print_ctrls( ld, ctmp );
1677 ldap_controls_free( ctrls );
1680 if ( err != LDAP_SUCCESS
1682 || ( matched && matched[ 0 ] )
1683 || ( info && info[ 0 ] )
1686 tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1688 if( matched ) ber_memfree( matched );
1689 if( info ) ber_memfree( info );
1690 if( refs ) ber_memvfree( (void **)refs );
1692 if ( err != LDAP_SUCCESS ) tool_exit( ld, err );
1697 tool_unbind( LDAP *ld )
1699 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1701 if ( err != LDAP_OPT_SUCCESS ) {
1702 fprintf( stderr, "Could not unset controls\n");
1705 (void) ldap_unbind_ext( ld, NULL, NULL );
1709 tool_exit( LDAP *ld, int status )
1719 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1721 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1723 int i = 0, j, crit = 0, err;
1724 LDAPControl c[16], **ctrls;
1728 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1730 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1734 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1739 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1741 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1742 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1744 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1746 || unknown_ctrls_num ) )
1751 ctrls = (LDAPControl**) malloc(sizeof(c) + (count + unknown_ctrls_num + 1)*sizeof(LDAPControl*));
1752 if ( ctrls == NULL ) {
1753 fprintf( stderr, "No memory\n" );
1754 tool_exit( ld, EXIT_FAILURE );
1758 if ( BER_BVISNULL( &assertionvalue ) ) {
1759 err = ldap_create_assertion_control_value( ld,
1760 assertion, &assertionvalue );
1763 "Unable to create assertion value "
1764 "\"%s\" (%d)\n", assertion, err );
1768 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1769 c[i].ldctl_value = assertionvalue;
1770 c[i].ldctl_iscritical = assertctl > 1;
1776 c[i].ldctl_value.bv_val = authzid;
1777 c[i].ldctl_value.bv_len = strlen( authzid );
1778 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1779 c[i].ldctl_iscritical = authzcrit;
1784 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1785 /* NOTE: doesn't need an extra count because it's incompatible
1788 BerElementBuffer berbuf;
1789 BerElement *ber = (BerElement *)&berbuf;
1791 ber_init2( ber, NULL, LBER_USE_DER );
1793 if ( ber_printf( ber, "s", proxydn ) == -1 ) {
1794 tool_exit( ld, EXIT_FAILURE );
1797 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1798 tool_exit( ld, EXIT_FAILURE );
1801 c[i].ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1802 c[i].ldctl_iscritical = authzcrit;
1806 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1809 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1810 BER_BVZERO( &c[i].ldctl_value );
1811 c[i].ldctl_iscritical = manageDIT > 1;
1816 if ( manageDSAit ) {
1817 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1818 BER_BVZERO( &c[i].ldctl_value );
1819 c[i].ldctl_iscritical = manageDSAit > 1;
1825 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1826 BER_BVZERO( &c[i].ldctl_value );
1827 c[i].ldctl_iscritical = noop > 1;
1832 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1834 c[i].ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1835 BER_BVZERO( &c[i].ldctl_value );
1836 c[i].ldctl_iscritical = 0;
1843 BerElementBuffer berbuf;
1844 BerElement *ber = (BerElement *)&berbuf;
1845 char **attrs = NULL;
1847 if( preread_attrs ) {
1848 attrs = ldap_str2charray( preread_attrs, "," );
1851 ber_init2( ber, NULL, LBER_USE_DER );
1853 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1854 fprintf( stderr, "preread attrs encode failed.\n" );
1855 tool_exit( ld, EXIT_FAILURE );
1858 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1860 fprintf( stderr, "preread flatten failed (%d)\n", err );
1861 tool_exit( ld, EXIT_FAILURE );
1864 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1865 c[i].ldctl_iscritical = preread > 1;
1869 if( attrs ) ldap_charray_free( attrs );
1873 BerElementBuffer berbuf;
1874 BerElement *ber = (BerElement *)&berbuf;
1875 char **attrs = NULL;
1877 if( postread_attrs ) {
1878 attrs = ldap_str2charray( postread_attrs, "," );
1881 ber_init2( ber, NULL, LBER_USE_DER );
1883 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1884 fprintf( stderr, "postread attrs encode failed.\n" );
1885 tool_exit( ld, EXIT_FAILURE );
1888 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1890 fprintf( stderr, "postread flatten failed (%d)\n", err );
1891 tool_exit( ld, EXIT_FAILURE );
1894 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1895 c[i].ldctl_iscritical = postread > 1;
1899 if( attrs ) ldap_charray_free( attrs );
1902 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1904 if ( chainingResolve > -1 ) {
1905 BerElementBuffer berbuf;
1906 BerElement *ber = (BerElement *)&berbuf;
1908 ber_init2( ber, NULL, LBER_USE_DER );
1910 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1913 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1914 tool_exit( ld, EXIT_FAILURE );
1917 if ( chainingContinuation > -1 ) {
1918 err = ber_printf( ber, "e", chainingContinuation );
1921 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1922 tool_exit( ld, EXIT_FAILURE );
1926 err = ber_printf( ber, /* { */ "N}" );
1929 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1930 tool_exit( ld, EXIT_FAILURE );
1933 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1934 tool_exit( ld, EXIT_FAILURE );
1938 BER_BVZERO( &c[i].ldctl_value );
1941 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1942 c[i].ldctl_iscritical = chaining > 1;
1946 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1948 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1949 if ( sessionTracking ) {
1950 if ( BER_BVISNULL( &stValue ) && st_value( ld, &stValue ) ) {
1951 tool_exit( ld, EXIT_FAILURE );
1954 c[i].ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1955 c[i].ldctl_iscritical = 0;
1956 c[i].ldctl_value = stValue;
1961 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1964 ctrls[i++] = extra_c++;
1966 for ( count = 0; count < unknown_ctrls_num; count++ ) {
1967 ctrls[i++] = &unknown_ctrls[count];
1971 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1973 if ( err != LDAP_OPT_SUCCESS ) {
1974 for ( j = 0; j < i; j++ ) {
1975 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1977 fprintf( stderr, "Could not set %scontrols\n",
1978 crit ? "critical " : "" );
1983 tool_exit( ld, EXIT_FAILURE );
1988 tool_check_abandon( LDAP *ld, int msgid )
1992 switch ( gotintr ) {
1994 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1995 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1996 rc, ldap_err2string( rc ) );
2000 rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
2001 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
2002 rc, ldap_err2string( rc ) );
2006 /* just unbind, ignoring the request */
2014 print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
2019 tool_write_ldif( LDIF_PUT_COMMENT, "==> ",
2020 what->bv_val, what->bv_len );
2021 ber = ber_init( &ctrl->ldctl_value );
2022 if ( ber == NULL ) {
2026 } else if ( ber_scanf( ber, "{m{" /*}}*/, &bv ) == LBER_ERROR ) {
2031 tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
2033 while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
2035 BerVarray vals = NULL;
2038 if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
2048 str = malloc( bv.bv_len + STRLENOF(": ") + 1 );
2051 ptr = lutil_strncopy( ptr, bv.bv_val, bv.bv_len );
2052 ptr = lutil_strcopy( ptr, ": " );
2055 for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
2057 ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2058 ldif ? str : bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
2061 ber_bvarray_free( vals );
2062 if ( str ) free( str );
2066 if ( ber != NULL ) {
2070 tool_write_ldif( LDIF_PUT_COMMENT, "<== ",
2071 what->bv_val, what->bv_len );
2077 print_preread( LDAP *ld, LDAPControl *ctrl )
2079 static struct berval what = BER_BVC( "preread" );
2081 return print_prepostread( ld, ctrl, &what );
2085 print_postread( LDAP *ld, LDAPControl *ctrl )
2087 static struct berval what = BER_BVC( "postread" );
2089 return print_prepostread( ld, ctrl, &what );
2093 print_paged_results( LDAP *ld, LDAPControl *ctrl )
2097 /* note: pr_cookie is being malloced; it's freed
2098 * the next time the control is sent, but the last
2099 * time it's not; we don't care too much, because
2100 * the last time an empty value is returned... */
2101 if ( ldap_parse_pageresponse_control( ld, ctrl, &estimate, &pr_cookie )
2108 /* FIXME: check buffer overflow */
2109 char buf[ BUFSIZ ], *ptr = buf;
2111 if ( estimate > 0 ) {
2112 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2113 "estimate=%d", estimate );
2116 if ( pr_cookie.bv_len > 0 ) {
2119 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
2120 pr_cookie.bv_len ) + 1;
2121 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
2123 bv.bv_len = lutil_b64_ntop(
2124 (unsigned char *) pr_cookie.bv_val,
2126 bv.bv_val, bv.bv_len );
2128 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2129 "%scookie=%s", ptr == buf ? "" : " ",
2132 ber_memfree( bv.bv_val );
2134 pr_morePagedResults = 1;
2137 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2138 "%scookie=", ptr == buf ? "" : " " );
2141 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2142 ldif ? "pagedresults: " : "pagedresults",
2150 print_sss( LDAP *ld, LDAPControl *ctrl )
2156 rc = ldap_parse_sortresponse_control( ld, ctrl, &err, &attr );
2157 if ( rc == LDAP_SUCCESS ) {
2159 rc = snprintf( buf, sizeof(buf), "(%d) %s%s%s",
2160 err, ldap_err2string(err), attr ? " " : "", attr ? attr : "" );
2162 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2163 ldif ? "sortResult: " : "sortResult", buf, rc );
2170 print_vlv( LDAP *ld, LDAPControl *ctrl )
2176 rc = ldap_parse_vlvresponse_control( ld, ctrl, &vlvPos, &vlvCount,
2177 &vlvContext, &err );
2178 if ( rc == LDAP_SUCCESS ) {
2181 if ( vlvContext && vlvContext->bv_len > 0 ) {
2182 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
2183 vlvContext->bv_len ) + 1;
2184 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
2186 bv.bv_len = lutil_b64_ntop(
2187 (unsigned char *) vlvContext->bv_val,
2189 bv.bv_val, bv.bv_len );
2195 rc = snprintf( buf, sizeof(buf), "pos=%d count=%d context=%s (%d) %s",
2196 vlvPos, vlvCount, bv.bv_val,
2197 err, ldap_err2string(err));
2200 ber_memfree( bv.bv_val );
2202 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2203 ldif ? "vlvResult" : "vlvResult", buf, rc );
2209 #ifdef LDAP_CONTROL_X_DEREF
2211 print_deref( LDAP *ld, LDAPControl *ctrl )
2213 LDAPDerefRes *drhead = NULL, *dr;
2216 rc = ldap_parse_derefresponse_control( ld, ctrl, &drhead );
2217 if ( rc != LDAP_SUCCESS ) {
2221 for ( dr = drhead; dr != NULL; dr = dr->next ) {
2226 len = strlen( dr->derefAttr ) + STRLENOF(": ");
2228 for ( dv = dr->attrVals; dv != NULL; dv = dv->next ) {
2229 if ( dv->vals != NULL ) {
2231 ber_len_t tlen = strlen(dv->type);
2233 for ( j = 0; dv->vals[ j ].bv_val != NULL; j++ ) {
2234 len += STRLENOF("<:=>;") + tlen + 4*((dv->vals[ j ].bv_len - 1)/3 + 1);
2238 len += dr->derefVal.bv_len + STRLENOF("\n");
2239 buf = ldap_memalloc( len + 1 );
2240 if ( buf == NULL ) {
2241 rc = LDAP_NO_MEMORY;
2246 ptr = lutil_strcopy( ptr, dr->derefAttr );
2249 for ( dv = dr->attrVals; dv != NULL; dv = dv->next ) {
2250 if ( dv->vals != NULL ) {
2252 for ( j = 0; dv->vals[ j ].bv_val != NULL; j++ ) {
2253 int k = ldif_is_not_printable( dv->vals[ j ].bv_val, dv->vals[ j ].bv_len );
2256 ptr = lutil_strcopy( ptr, dv->type );
2263 (unsigned char *) dv->vals[ j ].bv_val,
2264 dv->vals[ j ].bv_len,
2265 ptr, buf + len - ptr );
2270 ptr = lutil_memcopy( ptr, dv->vals[ j ].bv_val, dv->vals[ j ].bv_len );
2277 ptr = lutil_strncopy( ptr, dr->derefVal.bv_val, dr->derefVal.bv_len );
2280 assert( ptr <= buf + len );
2282 tool_write_ldif( LDIF_PUT_COMMENT, NULL, buf, ptr - buf);
2284 ldap_memfree( buf );
2290 ldap_derefresponse_free( drhead );
2296 #ifdef LDAP_CONTROL_X_WHATFAILED
2298 print_whatfailed( LDAP *ld, LDAPControl *ctrl )
2303 BerVarray bva = NULL;
2305 /* Create a BerElement from the berval returned in the control. */
2306 ber = ber_init( &ctrl->ldctl_value );
2308 if ( ber == NULL ) {
2309 return LDAP_NO_MEMORY;
2312 siz = sizeof(struct berval);
2313 tag = ber_scanf( ber, "[M]", &bva, &siz, 0 );
2314 if ( tag != LBER_ERROR ) {
2317 tool_write_ldif( LDIF_PUT_COMMENT, " what failed:", NULL, 0 );
2319 for ( i = 0; bva[i].bv_val != NULL; i++ ) {
2320 tool_write_ldif( LDIF_PUT_COMMENT, NULL, bva[i].bv_val, bva[i].bv_len );
2323 ldap_memfree( bva );
2333 #ifdef LDAP_CONTROL_AUTHZID_RESPONSE
2335 print_authzid( LDAP *ld, LDAPControl *ctrl )
2337 if ( ctrl->ldctl_value.bv_len ) {
2338 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2339 ldif ? "authzid: " : "authzid",
2340 ctrl->ldctl_value.bv_val, ctrl->ldctl_value.bv_len );
2342 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2343 ldif ? "authzid: " : "authzid",
2344 "anonymous", STRLENOF("anonymous") );
2351 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
2353 print_ppolicy( LDAP *ld, LDAPControl *ctrl )
2355 int expire = 0, grace = 0, rc;
2356 LDAPPasswordPolicyError pperr;
2358 rc = ldap_parse_passwordpolicy_control( ld, ctrl,
2359 &expire, &grace, &pperr );
2360 if ( rc == LDAP_SUCCESS ) {
2361 char buf[ BUFSIZ ], *ptr = buf;
2363 if ( expire != -1 ) {
2364 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2365 "expire=%d", expire );
2368 if ( grace != -1 ) {
2369 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2370 "%sgrace=%d", ptr == buf ? "" : " ", grace );
2373 if ( pperr != PP_noError ) {
2374 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2375 "%serror=%d (%s)", ptr == buf ? "" : " ",
2377 ldap_passwordpolicy_err2txt( pperr ) );
2380 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2381 ldif ? "ppolicy: " : "ppolicy", buf, ptr - buf );
2388 void tool_print_ctrls(
2390 LDAPControl **ctrls )
2395 for ( i = 0; ctrls[i] != NULL; i++ ) {
2396 /* control: OID criticality base64value */
2397 struct berval b64 = BER_BVNULL;
2402 /* FIXME: there might be cases where a control has NULL OID;
2403 * this makes little sense, especially when returned by the
2404 * server, but libldap happily allows it */
2405 if ( ctrls[i]->ldctl_oid == NULL ) {
2410 len += strlen( ctrls[i]->ldctl_oid );
2412 /* add enough for space after OID and the critical value itself */
2413 len += ctrls[i]->ldctl_iscritical
2414 ? sizeof("true") : sizeof("false");
2416 /* convert to base64 */
2417 if ( !BER_BVISNULL( &ctrls[i]->ldctl_value ) ) {
2418 b64.bv_len = LUTIL_BASE64_ENCODE_LEN(
2419 ctrls[i]->ldctl_value.bv_len ) + 1;
2420 b64.bv_val = ber_memalloc( b64.bv_len + 1 );
2422 b64.bv_len = lutil_b64_ntop(
2423 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
2424 ctrls[i]->ldctl_value.bv_len,
2425 b64.bv_val, b64.bv_len );
2429 len += 1 + b64.bv_len;
2432 ptr = str = malloc( len + 1 );
2434 ptr = lutil_strcopy( ptr, ": " );
2436 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_oid );
2437 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_iscritical
2438 ? " true" : " false" );
2441 ptr = lutil_strcopy( ptr, " " );
2442 ptr = lutil_strcopy( ptr, b64.bv_val );
2446 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2447 "control", str, len );
2452 ber_memfree( b64.bv_val );
2455 /* known controls */
2456 for ( j = 0; tool_ctrl_response[j].oid != NULL; j++ ) {
2457 if ( strcmp( tool_ctrl_response[j].oid, ctrls[i]->ldctl_oid ) == 0 ) {
2458 if ( !tool_ctrl_response[j].mask & tool_type ) {
2459 /* this control should not appear
2460 * with this tool; warning? */
2466 if ( tool_ctrl_response[j].oid != NULL && tool_ctrl_response[j].func ) {
2467 (void)tool_ctrl_response[j].func( ld, ctrls[i] );
2473 tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
2477 if (( ldif = ldif_put_wrap( type, name, value, vallen, ldif_wrap )) == NULL ) {
2481 fputs( ldif, stdout );
2482 ber_memfree( ldif );
2488 tool_is_oid( const char *s )
2492 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2496 for ( ; s[ 0 ]; s++ ) {
2497 if ( s[ 0 ] == '.' ) {
2498 if ( s[ 1 ] == '\0' ) {
2505 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2509 if ( first == 1 && s[ 0 ] == '0' && s[ 1 ] != '.' ) {