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-2018 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>] any libldap ldap.conf options, plus\n"),
378 N_(" ldif_wrap=<width> (in columns, or \"no\" for no wrapping)\n"),
379 N_(" nettimeout=<timeout> (in seconds, or \"none\" or \"max\")\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 ) {
841 for ( next=control; *next; next++ ) {
842 if ( *next == '-' ) {
847 if ( strcasecmp( control, "nettimeout" ) == 0 ) {
848 if( nettimeout.tv_sec != -1 ) {
849 fprintf( stderr, "nettimeout option previously specified\n");
850 exit( EXIT_FAILURE );
852 if( cvalue == NULL || cvalue[0] == '\0' ) {
853 fprintf( stderr, "nettimeout: option value expected\n" );
856 if ( strcasecmp( cvalue, "none" ) == 0 ) {
857 nettimeout.tv_sec = 0;
858 } else if ( strcasecmp( cvalue, "max" ) == 0 ) {
859 nettimeout.tv_sec = LDAP_MAXINT;
861 ival = strtol( cvalue, &next, 10 );
862 if ( next == NULL || next[0] != '\0' ) {
864 _("Unable to parse network timeout \"%s\"\n"), cvalue );
865 exit( EXIT_FAILURE );
867 nettimeout.tv_sec = ival;
869 if( nettimeout.tv_sec < 0 || nettimeout.tv_sec > LDAP_MAXINT ) {
870 fprintf( stderr, _("%s: invalid network timeout (%ld) specified\n"),
871 prog, (long)nettimeout.tv_sec );
872 exit( EXIT_FAILURE );
875 } else if ( strcasecmp( control, "ldif_wrap" ) == 0 ) {
877 ldif_wrap = LDIF_LINE_WIDTH;
879 } else if ( strcasecmp( cvalue, "no" ) == 0 ) {
880 ldif_wrap = LDIF_LINE_WIDTH_MAX;
884 if ( lutil_atou( &u, cvalue ) ) {
886 _("Unable to parse ldif_wrap=\"%s\"\n"), cvalue );
887 exit( EXIT_FAILURE );
889 ldif_wrap = (ber_len_t)u;
892 } else if ( ldap_pvt_conf_option( control, cvalue, 1 ) ) {
893 fprintf( stderr, "Invalid general option name: %s\n",
897 ber_memfree(control);
900 #ifdef HAVE_CYRUS_SASL
901 if( sasl_secprops != NULL ) {
902 fprintf( stderr, "%s: -O previously specified\n", prog );
903 exit( EXIT_FAILURE );
905 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
906 fprintf( stderr, "%s: incompatible previous "
907 "authentication choice\n", prog );
908 exit( EXIT_FAILURE );
910 authmethod = LDAP_AUTH_SASL;
911 sasl_secprops = ber_strdup( optarg );
913 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
914 exit( EXIT_FAILURE );
919 fprintf( stderr, "%s: -p previously specified\n", prog );
920 exit( EXIT_FAILURE );
922 ival = strtol( optarg, &next, 10 );
923 if ( next == NULL || next[0] != '\0' ) {
924 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
925 exit( EXIT_FAILURE );
930 ival = strtol( optarg, &next, 10 );
931 if ( next == NULL || next[0] != '\0' ) {
932 fprintf( stderr, "%s: unable to parse protocol version \"%s\"\n", prog, optarg );
933 exit( EXIT_FAILURE );
937 if( protocol == LDAP_VERSION3 ) {
938 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
940 exit( EXIT_FAILURE );
942 protocol = LDAP_VERSION2;
945 if( protocol == LDAP_VERSION2 ) {
946 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
948 exit( EXIT_FAILURE );
950 protocol = LDAP_VERSION3;
953 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
959 #ifdef HAVE_CYRUS_SASL
960 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
961 fprintf( stderr, "%s: incompatible previous "
962 "authentication choice\n",
964 exit( EXIT_FAILURE );
966 authmethod = LDAP_AUTH_SASL;
967 sasl_flags = LDAP_SASL_QUIET;
970 fprintf( stderr, "%s: not compiled with SASL support\n",
972 exit( EXIT_FAILURE );
975 #ifdef HAVE_CYRUS_SASL
976 if( sasl_realm != NULL ) {
977 fprintf( stderr, "%s: -R previously specified\n", prog );
978 exit( EXIT_FAILURE );
980 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
981 fprintf( stderr, "%s: incompatible previous "
982 "authentication choice\n",
984 exit( EXIT_FAILURE );
986 authmethod = LDAP_AUTH_SASL;
987 sasl_realm = ber_strdup( optarg );
989 fprintf( stderr, "%s: not compiled with SASL support\n",
991 exit( EXIT_FAILURE );
995 #ifdef HAVE_CYRUS_SASL
996 if( sasl_authc_id != NULL ) {
997 fprintf( stderr, "%s: -U previously specified\n", prog );
998 exit( EXIT_FAILURE );
1000 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
1001 fprintf( stderr, "%s: incompatible previous "
1002 "authentication choice\n",
1004 exit( EXIT_FAILURE );
1006 authmethod = LDAP_AUTH_SASL;
1007 sasl_authc_id = ber_strdup( optarg );
1009 fprintf( stderr, "%s: not compiled with SASL support\n",
1011 exit( EXIT_FAILURE );
1014 case 'v': /* verbose mode */
1017 case 'V': /* version */
1020 case 'w': /* password */
1021 passwd.bv_val = ber_strdup( optarg );
1025 for( p = optarg; *p != '\0'; p++ ) {
1029 passwd.bv_len = strlen( passwd.bv_val );
1038 #ifdef HAVE_CYRUS_SASL
1039 if( sasl_mech != NULL ) {
1040 fprintf( stderr, "%s: -Y previously specified\n", prog );
1041 exit( EXIT_FAILURE );
1043 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
1045 "%s: incompatible with authentication choice\n", prog );
1046 exit( EXIT_FAILURE );
1048 authmethod = LDAP_AUTH_SASL;
1049 sasl_mech = ber_strdup( optarg );
1051 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1052 exit( EXIT_FAILURE );
1056 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
1057 fprintf( stderr, "%s: incompatible with previous "
1058 "authentication choice\n", prog );
1059 exit( EXIT_FAILURE );
1061 authmethod = LDAP_AUTH_SIMPLE;
1064 #ifdef HAVE_CYRUS_SASL
1065 if( sasl_authz_id != NULL ) {
1066 fprintf( stderr, "%s: -X previously specified\n", prog );
1067 exit( EXIT_FAILURE );
1069 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
1070 fprintf( stderr, "%s: -X incompatible with "
1071 "authentication choice\n", prog );
1072 exit( EXIT_FAILURE );
1074 authmethod = LDAP_AUTH_SASL;
1075 sasl_authz_id = ber_strdup( optarg );
1077 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1078 exit( EXIT_FAILURE );
1085 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
1086 exit( EXIT_FAILURE );
1090 if( handle_private_option( i ) ) break;
1091 fprintf( stderr, "%s: unrecognized option -%c\n",
1098 /* prevent bad linking */
1100 api.ldapai_info_version = LDAP_API_INFO_VERSION;
1102 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
1103 != LDAP_OPT_SUCCESS )
1105 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
1106 exit( EXIT_FAILURE );
1109 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
1110 fprintf( stderr, "LDAP APIInfo version mismatch: "
1111 "library %d, header %d\n",
1112 api.ldapai_info_version, LDAP_API_INFO_VERSION );
1113 exit( EXIT_FAILURE );
1116 if( api.ldapai_api_version != LDAP_API_VERSION ) {
1117 fprintf( stderr, "LDAP API version mismatch: "
1118 "library %d, header %d\n",
1119 api.ldapai_api_version, LDAP_API_VERSION );
1120 exit( EXIT_FAILURE );
1123 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
1124 fprintf( stderr, "LDAP vendor name mismatch: "
1125 "library %s, header %s\n",
1126 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
1127 exit( EXIT_FAILURE );
1130 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
1131 fprintf( stderr, "LDAP vendor version mismatch: "
1132 "library %d, header %d\n",
1133 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
1134 exit( EXIT_FAILURE );
1138 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
1140 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
1141 if (version > 1) exit( EXIT_SUCCESS );
1144 ldap_memfree( api.ldapai_vendor_name );
1145 ber_memvfree( (void **)api.ldapai_extensions );
1149 protocol = LDAP_VERSION3;
1151 if (authmethod == -1 && protocol > LDAP_VERSION2) {
1152 #ifdef HAVE_CYRUS_SASL
1153 if ( binddn != NULL ) {
1154 authmethod = LDAP_AUTH_SIMPLE;
1156 authmethod = LDAP_AUTH_SASL;
1159 authmethod = LDAP_AUTH_SIMPLE;
1163 if( ldapuri == NULL ) {
1164 if( ldapport && ( ldaphost == NULL )) {
1165 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
1166 exit( EXIT_FAILURE );
1169 if( ldaphost != NULL ) {
1170 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
1171 exit( EXIT_FAILURE );
1174 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
1175 exit( EXIT_FAILURE );
1179 if( protocol == LDAP_VERSION2 ) {
1180 if( assertctl || authzid || manageDIT || manageDSAit ||
1181 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1183 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1184 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1187 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1189 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1190 noop || ppolicy || preread || postread )
1192 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
1193 exit( EXIT_FAILURE );
1197 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
1198 exit( EXIT_FAILURE );
1201 #ifdef HAVE_CYRUS_SASL
1202 if( authmethod == LDAP_AUTH_SASL ) {
1203 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
1205 exit( EXIT_FAILURE );
1210 if ( ( pw_file || want_bindpw ) && !BER_BVISNULL( &passwd ) ) {
1211 fprintf( stderr, "%s: -%c incompatible with -w\n",
1212 prog, ( pw_file ? 'y' : 'W' ) );
1213 exit( EXIT_FAILURE );
1219 tool_conn_setup( int dont, void (*private_setup)( LDAP * ) )
1224 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
1225 != LBER_OPT_SUCCESS )
1228 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
1230 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
1231 != LDAP_OPT_SUCCESS )
1234 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
1239 (void) SIGNAL( SIGPIPE, SIG_IGN );
1243 SIGNAL( SIGINT, do_sig );
1249 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
1252 memset( &url, 0, sizeof(url));
1254 url.lud_scheme = "ldap";
1255 url.lud_host = ldaphost;
1256 url.lud_port = ldapport;
1257 url.lud_scope = LDAP_SCOPE_DEFAULT;
1259 ldapuri = ldap_url_desc2str( &url );
1261 } else if ( ldapuri != NULL ) {
1262 LDAPURLDesc *ludlist, **ludp;
1266 rc = ldap_url_parselist( &ludlist, ldapuri );
1267 if ( rc != LDAP_URL_SUCCESS ) {
1269 "Could not parse LDAP URI(s)=%s (%d)\n",
1271 exit( EXIT_FAILURE );
1274 for ( ludp = &ludlist; *ludp != NULL; ) {
1275 LDAPURLDesc *lud = *ludp;
1278 if ( lud->lud_dn != NULL && lud->lud_dn[ 0 ] != '\0' &&
1279 ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) )
1281 /* if no host but a DN is provided,
1282 * use DNS SRV to gather the host list
1283 * and turn it into a list of URIs
1284 * using the scheme provided */
1285 char *domain = NULL,
1289 len_proto = strlen( lud->lud_scheme );
1291 if ( ldap_dn2domain( lud->lud_dn, &domain )
1295 "DNS SRV: Could not turn "
1296 "DN=\"%s\" into a domain\n",
1301 rc = ldap_domain2hostlist( domain, &hostlist );
1304 "DNS SRV: Could not turn "
1305 "domain=%s into a hostlist\n",
1310 hosts = ldap_str2charray( hostlist, " " );
1311 if ( hosts == NULL ) {
1313 "DNS SRV: Could not parse "
1314 "hostlist=\"%s\"\n",
1319 for ( i = 0; hosts[ i ] != NULL; i++ )
1322 tmp = (char **)ber_memrealloc( urls, sizeof( char * ) * ( nurls + i + 1 ) );
1323 if ( tmp == NULL ) {
1325 "DNS SRV: out of memory?\n" );
1329 urls[ nurls ] = NULL;
1331 for ( i = 0; hosts[ i ] != NULL; i++ ) {
1332 size_t len = len_proto
1334 + strlen( hosts[ i ] )
1337 urls[ nurls + i + 1 ] = NULL;
1338 urls[ nurls + i ] = (char *)malloc( sizeof( char ) * len );
1339 if ( urls[ nurls + i ] == NULL ) {
1341 "DNS SRV: out of memory?\n" );
1345 snprintf( urls[ nurls + i ], len, "%s://%s",
1346 lud->lud_scheme, hosts[ i ] );
1351 ber_memvfree( (void **)hosts );
1352 ber_memfree( hostlist );
1353 ber_memfree( domain );
1356 tmp = (char **)ber_memrealloc( urls, sizeof( char * ) * ( nurls + 2 ) );
1357 if ( tmp == NULL ) {
1359 "DNS SRV: out of memory?\n" );
1363 urls[ nurls + 1 ] = NULL;
1365 urls[ nurls ] = ldap_url_desc2str( lud );
1366 if ( urls[ nurls ] == NULL ) {
1368 "DNS SRV: out of memory?\n" );
1374 *ludp = lud->lud_next;
1376 lud->lud_next = NULL;
1377 ldap_free_urldesc( lud );
1380 if ( ludlist != NULL ) {
1381 ldap_free_urllist( ludlist );
1382 exit( EXIT_FAILURE );
1384 } else if ( urls == NULL ) {
1385 exit( EXIT_FAILURE );
1388 ldap_memfree( ldapuri );
1389 ldapuri = ldap_charray2str( urls, " " );
1390 ber_memvfree( (void **)urls );
1394 fprintf( stderr, "ldap_initialize( %s )\n",
1395 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
1397 rc = ldap_initialize( &ld, ldapuri );
1398 if( rc != LDAP_SUCCESS ) {
1400 "Could not create LDAP session handle for URI=%s (%d): %s\n",
1401 ldapuri, rc, ldap_err2string(rc) );
1402 exit( EXIT_FAILURE );
1405 if( private_setup ) private_setup( ld );
1407 /* referrals: obsolete */
1408 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
1409 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
1411 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
1412 referrals ? "on" : "off" );
1413 tool_exit( ld, EXIT_FAILURE );
1416 #ifdef HAVE_CYRUS_SASL
1419 if( ldap_set_option( ld, LDAP_OPT_X_SASL_NOCANON,
1420 LDAP_OPT_ON ) != LDAP_OPT_SUCCESS )
1422 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_NOCANON on\n" );
1423 tool_exit( ld, EXIT_FAILURE );
1427 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
1428 != LDAP_OPT_SUCCESS )
1430 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
1432 tool_exit( ld, EXIT_FAILURE );
1436 rc = ldap_start_tls_s( ld, NULL, NULL );
1437 if ( rc != LDAP_SUCCESS ) {
1439 ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&msg);
1440 tool_perror( "ldap_start_tls", rc, NULL, NULL, msg, NULL );
1442 if ( use_tls > 1 ) {
1443 tool_exit( ld, EXIT_FAILURE );
1448 if ( nettimeout.tv_sec > 0 ) {
1449 if ( ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &nettimeout )
1450 != LDAP_OPT_SUCCESS )
1452 fprintf( stderr, "Could not set LDAP_OPT_NETWORK_TIMEOUT %ld\n",
1453 (long)nettimeout.tv_sec );
1454 tool_exit( ld, EXIT_FAILURE );
1464 tool_bind( LDAP *ld )
1466 LDAPControl **sctrlsp = NULL;
1467 LDAPControl *sctrls[4];
1468 LDAPControl sctrl[3];
1472 LDAPMessage *result = NULL;
1475 char *matched = NULL;
1478 LDAPControl **ctrls = NULL;
1483 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1486 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1487 c.ldctl_value.bv_val = NULL;
1488 c.ldctl_value.bv_len = 0;
1489 c.ldctl_iscritical = 0;
1491 sctrls[nsctrls] = &sctrl[nsctrls];
1492 sctrls[++nsctrls] = NULL;
1499 c.ldctl_oid = LDAP_CONTROL_AUTHZID_REQUEST;
1500 c.ldctl_iscritical = bauthzid > 1;
1501 BER_BVZERO( &c.ldctl_value );
1504 sctrls[nsctrls] = &sctrl[nsctrls];
1505 sctrls[++nsctrls] = NULL;
1508 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1509 if ( sessionTracking ) {
1512 if ( BER_BVISNULL( &stValue) && st_value( ld, &stValue ) ) {
1513 tool_exit( ld, EXIT_FAILURE );
1516 c.ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1517 c.ldctl_iscritical = 0;
1518 c.ldctl_value = stValue;
1521 sctrls[nsctrls] = &sctrl[nsctrls];
1522 sctrls[++nsctrls] = NULL;
1524 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1530 assert( nsctrls < (int) (sizeof(sctrls)/sizeof(sctrls[0])) );
1532 if ( pw_file || want_bindpw ) {
1533 assert( passwd.bv_val == NULL && passwd.bv_len == 0 );
1536 if ( lutil_get_filed_password( pw_file, &passwd ) ) {
1537 tool_exit( ld, EXIT_FAILURE );
1541 char *pw = getpassphrase( _("Enter LDAP Password: ") );
1543 passwd.bv_val = ber_strdup( pw );
1544 passwd.bv_len = strlen( passwd.bv_val );
1549 if ( authmethod == LDAP_AUTH_SASL ) {
1550 #ifdef HAVE_CYRUS_SASL
1552 const char *rmech = NULL;
1554 if( sasl_secprops != NULL ) {
1555 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
1556 (void *) sasl_secprops );
1558 if( rc != LDAP_OPT_SUCCESS ) {
1560 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
1562 tool_exit( ld, LDAP_LOCAL_ERROR );
1566 defaults = lutil_sasl_defaults( ld,
1574 rc = ldap_sasl_interactive_bind( ld, binddn, sasl_mech,
1575 sctrlsp, NULL, sasl_flags, lutil_sasl_interact, defaults,
1576 result, &rmech, &msgid );
1578 if ( rc != LDAP_SASL_BIND_IN_PROGRESS )
1581 ldap_msgfree( result );
1583 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 || !result ) {
1584 ldap_get_option( ld, LDAP_OPT_RESULT_CODE, (void*)&err );
1585 ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&info );
1586 tool_perror( "ldap_sasl_interactive_bind",
1587 err, NULL, NULL, info, NULL );
1588 ldap_memfree( info );
1589 tool_exit( ld, err );
1591 } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
1593 lutil_sasl_freedefs( defaults );
1595 if ( rc != LDAP_SUCCESS ) {
1596 ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&info );
1597 tool_perror( "ldap_sasl_interactive_bind",
1598 rc, NULL, NULL, info, NULL );
1599 ldap_memfree( info );
1600 tool_exit( ld, rc );
1603 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1604 tool_exit( ld, LDAP_NOT_SUPPORTED );
1608 rc = ldap_sasl_bind( ld, binddn, LDAP_SASL_SIMPLE, &passwd,
1609 sctrlsp, NULL, &msgid );
1610 if ( msgid == -1 ) {
1611 tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
1612 NULL, NULL, NULL, NULL );
1613 tool_exit( ld, rc );
1616 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result );
1618 tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
1619 tool_exit( ld, LDAP_LOCAL_ERROR );
1623 tool_perror( "ldap_result", LDAP_TIMEOUT, NULL, NULL, NULL, NULL );
1624 tool_exit( ld, LDAP_LOCAL_ERROR );
1629 rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
1631 if ( rc != LDAP_SUCCESS ) {
1632 tool_perror( "ldap_bind parse result", rc, NULL, matched, info, refs );
1633 tool_exit( ld, LDAP_LOCAL_ERROR );
1637 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1638 if ( ctrls && ppolicy ) {
1640 int expire, grace, len = 0;
1641 LDAPPasswordPolicyError pErr = -1;
1643 ctrl = ldap_control_find( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
1646 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
1647 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1649 if ( pErr != PP_noError ){
1652 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1653 len = strlen( msgbuf );
1655 if ( expire >= 0 ) {
1656 sprintf( msgbuf+len,
1657 " (Password expires in %d seconds)",
1659 } else if ( grace >= 0 ) {
1660 sprintf( msgbuf+len,
1661 " (Password expired, %d grace logins remain)",
1668 if ( ctrls && bauthzid ) {
1671 ctrl = ldap_control_find( LDAP_CONTROL_AUTHZID_RESPONSE,
1674 LDAPControl *ctmp[2];
1677 tool_print_ctrls( ld, ctmp );
1682 ldap_controls_free( ctrls );
1685 if ( err != LDAP_SUCCESS
1687 || ( matched && matched[ 0 ] )
1688 || ( info && info[ 0 ] )
1691 tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1693 if( matched ) ber_memfree( matched );
1694 if( info ) ber_memfree( info );
1695 if( refs ) ber_memvfree( (void **)refs );
1697 if ( err != LDAP_SUCCESS ) tool_exit( ld, err );
1702 tool_unbind( LDAP *ld )
1704 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1706 if ( err != LDAP_OPT_SUCCESS ) {
1707 fprintf( stderr, "Could not unset controls\n");
1710 (void) ldap_unbind_ext( ld, NULL, NULL );
1714 tool_exit( LDAP *ld, int status )
1724 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1726 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1728 int i = 0, j, crit = 0, err;
1729 LDAPControl c[16], **ctrls;
1733 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1735 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1739 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1744 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1746 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1747 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1749 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1751 || unknown_ctrls_num ) )
1756 ctrls = (LDAPControl**) malloc(sizeof(c) + (count + unknown_ctrls_num + 1)*sizeof(LDAPControl*));
1757 if ( ctrls == NULL ) {
1758 fprintf( stderr, "No memory\n" );
1759 tool_exit( ld, EXIT_FAILURE );
1763 if ( BER_BVISNULL( &assertionvalue ) ) {
1764 err = ldap_create_assertion_control_value( ld,
1765 assertion, &assertionvalue );
1768 "Unable to create assertion value "
1769 "\"%s\" (%d)\n", assertion, err );
1773 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1774 c[i].ldctl_value = assertionvalue;
1775 c[i].ldctl_iscritical = assertctl > 1;
1781 c[i].ldctl_value.bv_val = authzid;
1782 c[i].ldctl_value.bv_len = strlen( authzid );
1783 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1784 c[i].ldctl_iscritical = authzcrit;
1789 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1790 /* NOTE: doesn't need an extra count because it's incompatible
1793 BerElementBuffer berbuf;
1794 BerElement *ber = (BerElement *)&berbuf;
1796 ber_init2( ber, NULL, LBER_USE_DER );
1798 if ( ber_printf( ber, "s", proxydn ) == -1 ) {
1799 tool_exit( ld, EXIT_FAILURE );
1802 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1803 tool_exit( ld, EXIT_FAILURE );
1806 c[i].ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1807 c[i].ldctl_iscritical = authzcrit;
1811 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1814 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1815 BER_BVZERO( &c[i].ldctl_value );
1816 c[i].ldctl_iscritical = manageDIT > 1;
1821 if ( manageDSAit ) {
1822 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1823 BER_BVZERO( &c[i].ldctl_value );
1824 c[i].ldctl_iscritical = manageDSAit > 1;
1830 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1831 BER_BVZERO( &c[i].ldctl_value );
1832 c[i].ldctl_iscritical = noop > 1;
1837 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1839 c[i].ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1840 BER_BVZERO( &c[i].ldctl_value );
1841 c[i].ldctl_iscritical = 0;
1848 BerElementBuffer berbuf;
1849 BerElement *ber = (BerElement *)&berbuf;
1850 char **attrs = NULL;
1852 if( preread_attrs ) {
1853 attrs = ldap_str2charray( preread_attrs, "," );
1856 ber_init2( ber, NULL, LBER_USE_DER );
1858 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1859 fprintf( stderr, "preread attrs encode failed.\n" );
1860 tool_exit( ld, EXIT_FAILURE );
1863 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1865 fprintf( stderr, "preread flatten failed (%d)\n", err );
1866 tool_exit( ld, EXIT_FAILURE );
1869 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1870 c[i].ldctl_iscritical = preread > 1;
1874 if( attrs ) ldap_charray_free( attrs );
1878 BerElementBuffer berbuf;
1879 BerElement *ber = (BerElement *)&berbuf;
1880 char **attrs = NULL;
1882 if( postread_attrs ) {
1883 attrs = ldap_str2charray( postread_attrs, "," );
1886 ber_init2( ber, NULL, LBER_USE_DER );
1888 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1889 fprintf( stderr, "postread attrs encode failed.\n" );
1890 tool_exit( ld, EXIT_FAILURE );
1893 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1895 fprintf( stderr, "postread flatten failed (%d)\n", err );
1896 tool_exit( ld, EXIT_FAILURE );
1899 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1900 c[i].ldctl_iscritical = postread > 1;
1904 if( attrs ) ldap_charray_free( attrs );
1907 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1909 if ( chainingResolve > -1 ) {
1910 BerElementBuffer berbuf;
1911 BerElement *ber = (BerElement *)&berbuf;
1913 ber_init2( ber, NULL, LBER_USE_DER );
1915 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1918 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1919 tool_exit( ld, EXIT_FAILURE );
1922 if ( chainingContinuation > -1 ) {
1923 err = ber_printf( ber, "e", chainingContinuation );
1926 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1927 tool_exit( ld, EXIT_FAILURE );
1931 err = ber_printf( ber, /* { */ "N}" );
1934 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1935 tool_exit( ld, EXIT_FAILURE );
1938 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1939 tool_exit( ld, EXIT_FAILURE );
1943 BER_BVZERO( &c[i].ldctl_value );
1946 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1947 c[i].ldctl_iscritical = chaining > 1;
1951 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1953 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1954 if ( sessionTracking ) {
1955 if ( BER_BVISNULL( &stValue ) && st_value( ld, &stValue ) ) {
1956 tool_exit( ld, EXIT_FAILURE );
1959 c[i].ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1960 c[i].ldctl_iscritical = 0;
1961 c[i].ldctl_value = stValue;
1966 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1969 ctrls[i++] = extra_c++;
1971 for ( count = 0; count < unknown_ctrls_num; count++ ) {
1972 ctrls[i++] = &unknown_ctrls[count];
1976 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1978 if ( err != LDAP_OPT_SUCCESS ) {
1979 for ( j = 0; j < i; j++ ) {
1980 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1982 fprintf( stderr, "Could not set %scontrols\n",
1983 crit ? "critical " : "" );
1988 tool_exit( ld, EXIT_FAILURE );
1993 tool_check_abandon( LDAP *ld, int msgid )
1997 switch ( gotintr ) {
1999 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
2000 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
2001 rc, ldap_err2string( rc ) );
2005 rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
2006 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
2007 rc, ldap_err2string( rc ) );
2011 /* just unbind, ignoring the request */
2019 print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
2024 tool_write_ldif( LDIF_PUT_COMMENT, "==> ",
2025 what->bv_val, what->bv_len );
2026 ber = ber_init( &ctrl->ldctl_value );
2027 if ( ber == NULL ) {
2031 } else if ( ber_scanf( ber, "{m{" /*}}*/, &bv ) == LBER_ERROR ) {
2036 tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
2038 while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
2040 BerVarray vals = NULL;
2043 if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
2053 str = malloc( bv.bv_len + STRLENOF(": ") + 1 );
2056 ptr = lutil_strncopy( ptr, bv.bv_val, bv.bv_len );
2057 ptr = lutil_strcopy( ptr, ": " );
2060 for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
2062 ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2063 ldif ? str : bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
2066 ber_bvarray_free( vals );
2067 if ( str ) free( str );
2071 if ( ber != NULL ) {
2075 tool_write_ldif( LDIF_PUT_COMMENT, "<== ",
2076 what->bv_val, what->bv_len );
2082 print_preread( LDAP *ld, LDAPControl *ctrl )
2084 static struct berval what = BER_BVC( "preread" );
2086 return print_prepostread( ld, ctrl, &what );
2090 print_postread( LDAP *ld, LDAPControl *ctrl )
2092 static struct berval what = BER_BVC( "postread" );
2094 return print_prepostread( ld, ctrl, &what );
2098 print_paged_results( LDAP *ld, LDAPControl *ctrl )
2102 /* note: pr_cookie is being malloced; it's freed
2103 * the next time the control is sent, but the last
2104 * time it's not; we don't care too much, because
2105 * the last time an empty value is returned... */
2106 if ( ldap_parse_pageresponse_control( ld, ctrl, &estimate, &pr_cookie )
2113 /* FIXME: check buffer overflow */
2114 char buf[ BUFSIZ ], *ptr = buf;
2116 if ( estimate > 0 ) {
2117 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2118 "estimate=%d", estimate );
2121 if ( pr_cookie.bv_len > 0 ) {
2124 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
2125 pr_cookie.bv_len ) + 1;
2126 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
2128 bv.bv_len = lutil_b64_ntop(
2129 (unsigned char *) pr_cookie.bv_val,
2131 bv.bv_val, bv.bv_len );
2133 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2134 "%scookie=%s", ptr == buf ? "" : " ",
2137 ber_memfree( bv.bv_val );
2139 pr_morePagedResults = 1;
2142 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2143 "%scookie=", ptr == buf ? "" : " " );
2146 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2147 ldif ? "pagedresults: " : "pagedresults",
2155 print_sss( LDAP *ld, LDAPControl *ctrl )
2161 rc = ldap_parse_sortresponse_control( ld, ctrl, &err, &attr );
2162 if ( rc == LDAP_SUCCESS ) {
2164 rc = snprintf( buf, sizeof(buf), "(%d) %s%s%s",
2165 err, ldap_err2string(err), attr ? " " : "", attr ? attr : "" );
2167 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2168 ldif ? "sortResult: " : "sortResult", buf, rc );
2175 print_vlv( LDAP *ld, LDAPControl *ctrl )
2181 rc = ldap_parse_vlvresponse_control( ld, ctrl, &vlvPos, &vlvCount,
2182 &vlvContext, &err );
2183 if ( rc == LDAP_SUCCESS ) {
2186 if ( vlvContext && vlvContext->bv_len > 0 ) {
2187 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
2188 vlvContext->bv_len ) + 1;
2189 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
2191 bv.bv_len = lutil_b64_ntop(
2192 (unsigned char *) vlvContext->bv_val,
2194 bv.bv_val, bv.bv_len );
2200 rc = snprintf( buf, sizeof(buf), "pos=%d count=%d context=%s (%d) %s",
2201 vlvPos, vlvCount, bv.bv_val,
2202 err, ldap_err2string(err));
2205 ber_memfree( bv.bv_val );
2207 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2208 ldif ? "vlvResult" : "vlvResult", buf, rc );
2214 #ifdef LDAP_CONTROL_X_DEREF
2216 print_deref( LDAP *ld, LDAPControl *ctrl )
2218 LDAPDerefRes *drhead = NULL, *dr;
2221 rc = ldap_parse_derefresponse_control( ld, ctrl, &drhead );
2222 if ( rc != LDAP_SUCCESS ) {
2226 for ( dr = drhead; dr != NULL; dr = dr->next ) {
2231 len = strlen( dr->derefAttr ) + STRLENOF(": ");
2233 for ( dv = dr->attrVals; dv != NULL; dv = dv->next ) {
2234 if ( dv->vals != NULL ) {
2236 ber_len_t tlen = strlen(dv->type);
2238 for ( j = 0; dv->vals[ j ].bv_val != NULL; j++ ) {
2239 len += STRLENOF("<:=>;") + tlen + 4*((dv->vals[ j ].bv_len - 1)/3 + 1);
2243 len += dr->derefVal.bv_len + STRLENOF("\n");
2244 buf = ldap_memalloc( len + 1 );
2245 if ( buf == NULL ) {
2246 rc = LDAP_NO_MEMORY;
2251 ptr = lutil_strcopy( ptr, dr->derefAttr );
2254 for ( dv = dr->attrVals; dv != NULL; dv = dv->next ) {
2255 if ( dv->vals != NULL ) {
2257 for ( j = 0; dv->vals[ j ].bv_val != NULL; j++ ) {
2258 int k = ldif_is_not_printable( dv->vals[ j ].bv_val, dv->vals[ j ].bv_len );
2261 ptr = lutil_strcopy( ptr, dv->type );
2268 (unsigned char *) dv->vals[ j ].bv_val,
2269 dv->vals[ j ].bv_len,
2270 ptr, buf + len - ptr );
2275 ptr = lutil_memcopy( ptr, dv->vals[ j ].bv_val, dv->vals[ j ].bv_len );
2282 ptr = lutil_strncopy( ptr, dr->derefVal.bv_val, dr->derefVal.bv_len );
2285 assert( ptr <= buf + len );
2287 tool_write_ldif( LDIF_PUT_COMMENT, NULL, buf, ptr - buf);
2289 ldap_memfree( buf );
2295 ldap_derefresponse_free( drhead );
2301 #ifdef LDAP_CONTROL_X_WHATFAILED
2303 print_whatfailed( LDAP *ld, LDAPControl *ctrl )
2308 BerVarray bva = NULL;
2310 /* Create a BerElement from the berval returned in the control. */
2311 ber = ber_init( &ctrl->ldctl_value );
2313 if ( ber == NULL ) {
2314 return LDAP_NO_MEMORY;
2317 siz = sizeof(struct berval);
2318 tag = ber_scanf( ber, "[M]", &bva, &siz, 0 );
2319 if ( tag != LBER_ERROR ) {
2322 tool_write_ldif( LDIF_PUT_COMMENT, " what failed:", NULL, 0 );
2324 for ( i = 0; bva[i].bv_val != NULL; i++ ) {
2325 tool_write_ldif( LDIF_PUT_COMMENT, NULL, bva[i].bv_val, bva[i].bv_len );
2328 ldap_memfree( bva );
2338 #ifdef LDAP_CONTROL_AUTHZID_RESPONSE
2340 print_authzid( LDAP *ld, LDAPControl *ctrl )
2342 if ( ctrl->ldctl_value.bv_len ) {
2343 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2344 ldif ? "authzid: " : "authzid",
2345 ctrl->ldctl_value.bv_val, ctrl->ldctl_value.bv_len );
2347 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2348 ldif ? "authzid: " : "authzid",
2349 "anonymous", STRLENOF("anonymous") );
2356 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
2358 print_ppolicy( LDAP *ld, LDAPControl *ctrl )
2360 int expire = 0, grace = 0, rc;
2361 LDAPPasswordPolicyError pperr;
2363 rc = ldap_parse_passwordpolicy_control( ld, ctrl,
2364 &expire, &grace, &pperr );
2365 if ( rc == LDAP_SUCCESS ) {
2366 char buf[ BUFSIZ ], *ptr = buf;
2368 if ( expire != -1 ) {
2369 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2370 "expire=%d", expire );
2373 if ( grace != -1 ) {
2374 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2375 "%sgrace=%d", ptr == buf ? "" : " ", grace );
2378 if ( pperr != PP_noError ) {
2379 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2380 "%serror=%d (%s)", ptr == buf ? "" : " ",
2382 ldap_passwordpolicy_err2txt( pperr ) );
2385 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2386 ldif ? "ppolicy: " : "ppolicy", buf, ptr - buf );
2393 void tool_print_ctrls(
2395 LDAPControl **ctrls )
2400 for ( i = 0; ctrls[i] != NULL; i++ ) {
2401 /* control: OID criticality base64value */
2402 struct berval b64 = BER_BVNULL;
2407 /* FIXME: there might be cases where a control has NULL OID;
2408 * this makes little sense, especially when returned by the
2409 * server, but libldap happily allows it */
2410 if ( ctrls[i]->ldctl_oid == NULL ) {
2415 len += strlen( ctrls[i]->ldctl_oid );
2417 /* add enough for space after OID and the critical value itself */
2418 len += ctrls[i]->ldctl_iscritical
2419 ? sizeof("true") : sizeof("false");
2421 /* convert to base64 */
2422 if ( !BER_BVISNULL( &ctrls[i]->ldctl_value ) ) {
2423 b64.bv_len = LUTIL_BASE64_ENCODE_LEN(
2424 ctrls[i]->ldctl_value.bv_len ) + 1;
2425 b64.bv_val = ber_memalloc( b64.bv_len + 1 );
2427 b64.bv_len = lutil_b64_ntop(
2428 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
2429 ctrls[i]->ldctl_value.bv_len,
2430 b64.bv_val, b64.bv_len );
2434 len += 1 + b64.bv_len;
2437 ptr = str = malloc( len + 1 );
2439 ptr = lutil_strcopy( ptr, ": " );
2441 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_oid );
2442 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_iscritical
2443 ? " true" : " false" );
2446 ptr = lutil_strcopy( ptr, " " );
2447 ptr = lutil_strcopy( ptr, b64.bv_val );
2451 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2452 "control", str, len );
2457 ber_memfree( b64.bv_val );
2460 /* known controls */
2461 for ( j = 0; tool_ctrl_response[j].oid != NULL; j++ ) {
2462 if ( strcmp( tool_ctrl_response[j].oid, ctrls[i]->ldctl_oid ) == 0 ) {
2463 if ( !tool_ctrl_response[j].mask & tool_type ) {
2464 /* this control should not appear
2465 * with this tool; warning? */
2471 if ( tool_ctrl_response[j].oid != NULL && tool_ctrl_response[j].func ) {
2472 (void)tool_ctrl_response[j].func( ld, ctrls[i] );
2478 tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
2482 if (( ldif = ldif_put_wrap( type, name, value, vallen, ldif_wrap )) == NULL ) {
2486 fputs( ldif, stdout );
2487 ber_memfree( ldif );
2493 tool_is_oid( const char *s )
2497 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2501 for ( ; s[ 0 ]; s++ ) {
2502 if ( s[ 0 ] == '.' ) {
2503 if ( s[ 1 ] == '\0' ) {
2510 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2514 if ( first == 1 && s[ 0 ] == '0' && s[ 1 ] != '.' ) {