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-2008 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;
72 char *ldaphost = NULL;
82 struct berval passwd = { 0, NULL };
84 #ifdef HAVE_CYRUS_SASL
85 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
86 char *sasl_realm = NULL;
87 char *sasl_authc_id = NULL;
88 char *sasl_authz_id = NULL;
89 char *sasl_mech = NULL;
90 char *sasl_secprops = NULL;
95 char *assertion = NULL;
96 struct berval assertionvalue = BER_BVNULL;
98 /* support deprecated early version of proxyAuthz */
99 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ "2.16.840.1.113730.3.4.12"
100 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
101 char *proxydn = NULL;
102 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
108 static char *preread_attrs = NULL;
110 static char *postread_attrs = NULL;
111 ber_int_t pr_morePagedResults = 1;
112 struct berval pr_cookie = { 0, NULL };
113 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
115 static int chainingResolve = -1;
116 static int chainingContinuation = -1;
117 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
118 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
119 static int sessionTracking = 0;
120 struct berval stValue;
121 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
123 LDAPControl *unknown_ctrls = NULL;
124 int unknown_ctrls_num = 0;
127 struct timeval nettimeout = { -1 , 0 };
129 typedef int (*print_ctrl_fn)( LDAP *ld, LDAPControl *ctrl );
131 static int print_preread( LDAP *ld, LDAPControl *ctrl );
132 static int print_postread( LDAP *ld, LDAPControl *ctrl );
133 static int print_paged_results( LDAP *ld, LDAPControl *ctrl );
134 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
135 static int print_ppolicy( LDAP *ld, LDAPControl *ctrl );
137 static int print_sss( LDAP *ld, LDAPControl *ctrl );
138 #ifdef LDAP_CONTROL_X_DEREF
139 static int print_deref( LDAP *ld, LDAPControl *ctrl );
141 #ifdef LDAP_CONTROL_X_WHATFAILED
142 static int print_whatfailed( LDAP *ld, LDAPControl *ctrl );
145 static struct tool_ctrls_t {
149 } tool_ctrl_response[] = {
150 { LDAP_CONTROL_PRE_READ, TOOL_ALL, print_preread },
151 { LDAP_CONTROL_POST_READ, TOOL_ALL, print_postread },
152 { LDAP_CONTROL_PAGEDRESULTS, TOOL_SEARCH, print_paged_results },
153 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
154 { LDAP_CONTROL_PASSWORDPOLICYRESPONSE, TOOL_ALL, print_ppolicy },
156 { LDAP_CONTROL_SORTRESPONSE, TOOL_SEARCH, print_sss },
157 #ifdef LDAP_CONTROL_X_DEREF
158 { LDAP_CONTROL_X_DEREF, TOOL_SEARCH, print_deref },
160 #ifdef LDAP_CONTROL_X_WHATFAILED
161 { LDAP_CONTROL_X_WHATFAILED, TOOL_ALL, print_whatfailed },
167 enum { Intr_None = 0, Intr_Abandon, Intr_Cancel, Intr_Ignore };
168 static volatile sig_atomic_t gotintr, abcan;
171 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
173 st_value( LDAP *ld, struct berval *value )
175 char *ip = NULL, *name = NULL;
176 struct berval id = { 0 };
177 char namebuf[ MAXHOSTNAMELEN ];
179 if ( gethostname( namebuf, sizeof( namebuf ) ) == 0 ) {
185 h = gethostbyname( name );
187 AC_MEMCPY( &addr, h->h_addr, sizeof( addr ) );
188 ip = inet_ntoa( addr );
192 #ifdef HAVE_CYRUS_SASL
193 if ( sasl_authz_id != NULL ) {
194 ber_str2bv( sasl_authz_id, 0, 0, &id );
196 } else if ( sasl_authc_id != NULL ) {
197 ber_str2bv( sasl_authc_id, 0, 0, &id );
200 #endif /* HAVE_CYRUS_SASL */
201 if ( binddn != NULL ) {
202 ber_str2bv( binddn, 0, 0, &id );
205 if ( ldap_create_session_tracking_value( ld,
206 ip, name, LDAP_CONTROL_X_SESSION_TRACKING_USERNAME,
209 fprintf( stderr, _("Session tracking control encoding error!\n") );
215 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
224 tool_init( tool_type_t type )
227 ldap_pvt_setlocale(LC_MESSAGES, "");
228 ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
229 ldap_pvt_textdomain(OPENLDAP_PACKAGE);
235 #ifdef HAVE_CYRUS_SASL
239 ldap_pvt_tls_destroy();
242 if ( ldapuri != NULL ) {
243 ber_memfree( ldapuri );
247 if ( pr_cookie.bv_val != NULL ) {
248 ber_memfree( pr_cookie.bv_val );
249 pr_cookie.bv_val = NULL;
250 pr_cookie.bv_len = 0;
255 tool_common_usage( void )
257 static const char *const descriptions[] = {
258 N_(" -c continuous operation mode (do not stop on errors)\n"),
259 N_(" -d level set LDAP debugging level to `level'\n"),
260 N_(" -D binddn bind DN\n"),
261 N_(" -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
262 N_(" [!]assert=<filter> (RFC 4528; a RFC 4515 Filter string)\n")
263 N_(" [!]authzid=<authzid> (RFC 4370; \"dn:<dn>\" or \"u:<user>\")\n")
264 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
266 /* non-advertized support for proxyDN */
267 N_(" [!]proxydn=<dn> (a RFC 4514 DN string)\n")
270 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
271 N_(" [!]chaining[=<resolveBehavior>[/<continuationBehavior>]]\n")
272 N_(" one of \"chainingPreferred\", \"chainingRequired\",\n")
273 N_(" \"referralsPreferred\", \"referralsRequired\"\n")
274 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
275 N_(" [!]manageDSAit (RFC 3296)\n")
277 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
280 N_(" [!]postread[=<attrs>] (RFC 4527; comma-separated attr list)\n")
281 N_(" [!]preread[=<attrs>] (RFC 4527; comma-separated attr list)\n")
283 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
284 N_(" [!]sessiontracking\n")
285 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
286 N_(" abandon, cancel, ignore (SIGINT sends abandon/cancel,\n"
287 " or ignores response; if critical, doesn't wait for SIGINT.\n"
288 " not really controls)\n")
289 N_(" -f file read operations from `file'\n"),
290 N_(" -h host LDAP server\n"),
291 N_(" -H URI LDAP Uniform Resource Identifier(s)\n"),
292 N_(" -I use SASL Interactive mode\n"),
293 N_(" -M enable Manage DSA IT control (-MM to make critical)\n"),
294 N_(" -n show what would be done but don't actually do it\n"),
295 N_(" -O props SASL security properties\n"),
296 N_(" -o <opt>[=<optparam] general options\n"),
297 N_(" nettimeout=<timeout> (in seconds, or \"none\" or \"max\")\n"),
298 N_(" -p port port on LDAP server\n"),
299 N_(" -P version protocol version (default: 3)\n"),
300 N_(" -Q use SASL Quiet mode\n"),
301 N_(" -R realm SASL realm\n"),
302 N_(" -U authcid SASL authentication identity\n"),
303 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
304 N_(" -V print version info (-VV only)\n"),
305 N_(" -w passwd bind password (for simple authentication)\n"),
306 N_(" -W prompt for bind password\n"),
307 N_(" -x Simple authentication\n"),
308 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
309 N_(" -y file Read password from file\n"),
310 N_(" -Y mech SASL mechanism\n"),
311 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
314 const char *const *cpp;
316 fputs( _("Common options:\n"), stderr );
317 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
318 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
319 fputs( _(*cpp), stderr );
332 fprintf( stderr, "%s: %s (%d)%s\n",
333 func, ldap_err2string( err ), err, extra ? extra : "" );
335 if ( matched && *matched ) {
336 fprintf( stderr, _("\tmatched DN: %s\n"), matched );
339 if ( info && *info ) {
340 fprintf( stderr, _("\tadditional info: %s\n"), info );
343 if ( refs && *refs ) {
345 fprintf( stderr, _("\treferrals:\n") );
346 for( i=0; refs[i]; i++ ) {
347 fprintf( stderr, "\t\t%s\n", refs[i] );
354 tool_args( int argc, char **argv )
358 while (( i = getopt( argc, argv, options )) != EOF ) {
360 char *control, *cvalue, *next;
362 case 'c': /* continuous operation mode */
369 ival = strtol( optarg, &next, 10 );
370 if (next == NULL || next[0] != '\0') {
371 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
376 case 'D': /* bind DN */
377 if( binddn != NULL ) {
378 fprintf( stderr, "%s: -D previously specified\n", prog );
379 exit( EXIT_FAILURE );
381 binddn = ber_strdup( optarg );
383 case 'e': /* general extensions (controls and such) */
384 /* should be extended to support comma separated list of
385 * [!]key[=value] parameters, e.g. -e !foo,bar=567
390 if( optarg[0] == '!' ) {
395 control = ber_strdup( optarg );
396 if ( (cvalue = strchr( control, '=' )) != NULL ) {
400 if ( strcasecmp( control, "assert" ) == 0 ) {
402 fprintf( stderr, "assert control previously specified\n");
403 exit( EXIT_FAILURE );
405 if( cvalue == NULL ) {
406 fprintf( stderr, "assert: control value expected\n" );
410 assertctl = 1 + crit;
412 assert( assertion == NULL );
415 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
416 if( authzid != NULL ) {
417 fprintf( stderr, "authzid control previously specified\n");
418 exit( EXIT_FAILURE );
420 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
421 if( proxydn != NULL ) {
422 fprintf( stderr, "authzid control incompatible with proxydn\n");
423 exit( EXIT_FAILURE );
425 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
426 if( cvalue == NULL ) {
427 fprintf( stderr, "authzid: control value expected\n" );
431 fprintf( stderr, "authzid: must be marked critical\n" );
435 assert( authzid == NULL );
438 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
439 } else if ( strcasecmp( control, "proxydn" ) == 0 ) {
440 if( proxydn != NULL ) {
441 fprintf( stderr, "proxydn control previously specified\n");
442 exit( EXIT_FAILURE );
444 if( authzid != NULL ) {
445 fprintf( stderr, "proxydn control incompatible with authzid\n");
446 exit( EXIT_FAILURE );
448 if( cvalue == NULL ) {
449 fprintf( stderr, "proxydn: control value expected\n" );
453 fprintf( stderr, "proxydn: must be marked critical\n" );
457 assert( proxydn == NULL );
459 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
461 } else if ( ( strcasecmp( control, "relax" ) == 0 ) ||
462 ( strcasecmp( control, "manageDIT" ) == 0 ) )
466 "relax control previously specified\n");
467 exit( EXIT_FAILURE );
469 if( cvalue != NULL ) {
471 "relax: no control value expected\n" );
475 manageDIT = 1 + crit;
477 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
480 "manageDSAit control previously specified\n");
481 exit( EXIT_FAILURE );
483 if( cvalue != NULL ) {
485 "manageDSAit: no control value expected\n" );
489 manageDSAit = 1 + crit;
491 } else if ( strcasecmp( control, "noop" ) == 0 ) {
493 fprintf( stderr, "noop control previously specified\n");
494 exit( EXIT_FAILURE );
496 if( cvalue != NULL ) {
497 fprintf( stderr, "noop: no control value expected\n" );
503 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
504 } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
506 fprintf( stderr, "ppolicy control previously specified\n");
507 exit( EXIT_FAILURE );
509 if( cvalue != NULL ) {
510 fprintf( stderr, "ppolicy: no control value expected\n" );
514 fprintf( stderr, "ppolicy: critical flag not allowed\n" );
521 } else if ( strcasecmp( control, "preread" ) == 0 ) {
523 fprintf( stderr, "preread control previously specified\n");
524 exit( EXIT_FAILURE );
528 preread_attrs = cvalue;
530 } else if ( strcasecmp( control, "postread" ) == 0 ) {
532 fprintf( stderr, "postread control previously specified\n");
533 exit( EXIT_FAILURE );
537 postread_attrs = cvalue;
539 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
540 } else if ( strcasecmp( control, "chaining" ) == 0 ) {
543 if ( cvalue != NULL ) {
546 continuation = strchr( cvalue, '/' );
547 if ( continuation ) {
548 /* FIXME: this makes sense only in searches */
549 *continuation++ = '\0';
550 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
551 chainingContinuation = LDAP_CHAINING_PREFERRED;
552 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
553 chainingContinuation = LDAP_CHAINING_REQUIRED;
554 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
555 chainingContinuation = LDAP_REFERRALS_PREFERRED;
556 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
557 chainingContinuation = LDAP_REFERRALS_REQUIRED;
560 "chaining behavior control "
561 "continuation value \"%s\" invalid\n",
563 exit( EXIT_FAILURE );
567 if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
568 chainingResolve = LDAP_CHAINING_PREFERRED;
569 } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
570 chainingResolve = LDAP_CHAINING_REQUIRED;
571 } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
572 chainingResolve = LDAP_REFERRALS_PREFERRED;
573 } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
574 chainingResolve = LDAP_REFERRALS_REQUIRED;
577 "chaining behavior control "
578 "resolve value \"%s\" invalid\n",
580 exit( EXIT_FAILURE );
583 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
585 /* this shouldn't go here, really; but it's a feature... */
586 } else if ( strcasecmp( control, "abandon" ) == 0 ) {
587 abcan = Intr_Abandon;
592 } else if ( strcasecmp( control, "cancel" ) == 0 ) {
598 } else if ( strcasecmp( control, "ignore" ) == 0 ) {
604 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
605 } else if ( strcasecmp( control, "sessiontracking" ) == 0 ) {
606 if ( sessionTracking ) {
607 fprintf( stderr, "%s: session tracking can be only specified once\n", prog );
608 exit( EXIT_FAILURE );
611 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
613 } else if ( tool_is_oid( control ) ) {
614 LDAPControl *tmpctrls, ctrl;
616 tmpctrls = (LDAPControl *)realloc( unknown_ctrls,
617 (unknown_ctrls_num + 1)*sizeof( LDAPControl ) );
618 if ( tmpctrls == NULL ) {
619 fprintf( stderr, "%s: no memory?\n", prog );
620 exit( EXIT_FAILURE );
622 unknown_ctrls = tmpctrls;
623 ctrl.ldctl_oid = control;
624 ctrl.ldctl_value.bv_val = NULL;
625 ctrl.ldctl_value.bv_len = 0;
626 ctrl.ldctl_iscritical = crit;
628 if ( cvalue != NULL ) {
630 size_t len = strlen( cvalue );
633 bv.bv_len = LUTIL_BASE64_DECODE_LEN( len );
634 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
636 retcode = lutil_b64_pton( cvalue,
637 (unsigned char *)bv.bv_val,
640 if ( retcode == -1 || (unsigned) retcode > bv.bv_len ) {
641 fprintf( stderr, "Unable to parse value of general control %s\n",
647 ctrl.ldctl_value = bv;
650 unknown_ctrls[ unknown_ctrls_num ] = ctrl;
654 fprintf( stderr, "Invalid general control name: %s\n",
659 case 'f': /* read from file */
660 if( infile != NULL ) {
661 fprintf( stderr, "%s: -f previously specified\n", prog );
662 exit( EXIT_FAILURE );
664 infile = ber_strdup( optarg );
666 case 'h': /* ldap host */
667 if( ldaphost != NULL ) {
668 fprintf( stderr, "%s: -h previously specified\n", prog );
669 exit( EXIT_FAILURE );
671 ldaphost = ber_strdup( optarg );
673 case 'H': /* ldap URI */
674 if( ldapuri != NULL ) {
675 fprintf( stderr, "%s: -H previously specified\n", prog );
676 exit( EXIT_FAILURE );
678 ldapuri = ber_strdup( optarg );
681 #ifdef HAVE_CYRUS_SASL
682 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
683 fprintf( stderr, "%s: incompatible previous "
684 "authentication choice\n",
686 exit( EXIT_FAILURE );
688 authmethod = LDAP_AUTH_SASL;
689 sasl_flags = LDAP_SASL_INTERACTIVE;
692 fprintf( stderr, "%s: was not compiled with SASL support\n",
694 exit( EXIT_FAILURE );
697 /* enable Manage DSA IT */
700 case 'n': /* print operations, don't actually do them */
704 control = ber_strdup( optarg );
705 if ( (cvalue = strchr( control, '=' )) != NULL ) {
709 if ( strcasecmp( control, "nettimeout" ) == 0 ) {
710 if( nettimeout.tv_sec != -1 ) {
711 fprintf( stderr, "nettimeout option previously specified\n");
712 exit( EXIT_FAILURE );
714 if( cvalue == NULL || cvalue[0] == '\0' ) {
715 fprintf( stderr, "nettimeout: option value expected\n" );
718 if ( strcasecmp( cvalue, "none" ) == 0 ) {
719 nettimeout.tv_sec = 0;
720 } else if ( strcasecmp( cvalue, "max" ) == 0 ) {
721 nettimeout.tv_sec = LDAP_MAXINT;
723 ival = strtol( cvalue, &next, 10 );
724 if ( next == NULL || next[0] != '\0' ) {
726 _("Unable to parse network timeout \"%s\"\n"), cvalue );
727 exit( EXIT_FAILURE );
729 nettimeout.tv_sec = ival;
731 if( nettimeout.tv_sec < 0 || nettimeout.tv_sec > LDAP_MAXINT ) {
732 fprintf( stderr, _("%s: invalid network timeout (%ld) specified\n"),
733 prog, (long)nettimeout.tv_sec );
734 exit( EXIT_FAILURE );
737 fprintf( stderr, "Invalid general option name: %s\n",
743 #ifdef HAVE_CYRUS_SASL
744 if( sasl_secprops != NULL ) {
745 fprintf( stderr, "%s: -O previously specified\n", prog );
746 exit( EXIT_FAILURE );
748 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
749 fprintf( stderr, "%s: incompatible previous "
750 "authentication choice\n", prog );
751 exit( EXIT_FAILURE );
753 authmethod = LDAP_AUTH_SASL;
754 sasl_secprops = ber_strdup( optarg );
756 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
757 exit( EXIT_FAILURE );
762 fprintf( stderr, "%s: -p previously specified\n", prog );
763 exit( EXIT_FAILURE );
765 ival = strtol( optarg, &next, 10 );
766 if ( next == NULL || next[0] != '\0' ) {
767 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
768 exit( EXIT_FAILURE );
773 ival = strtol( optarg, &next, 10 );
774 if ( next == NULL || next[0] != '\0' ) {
775 fprintf( stderr, "%s: unable to parse protocol version \"%s\"\n", prog, optarg );
776 exit( EXIT_FAILURE );
780 if( protocol == LDAP_VERSION3 ) {
781 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
783 exit( EXIT_FAILURE );
785 protocol = LDAP_VERSION2;
788 if( protocol == LDAP_VERSION2 ) {
789 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
791 exit( EXIT_FAILURE );
793 protocol = LDAP_VERSION3;
796 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
802 #ifdef HAVE_CYRUS_SASL
803 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
804 fprintf( stderr, "%s: incompatible previous "
805 "authentication choice\n",
807 exit( EXIT_FAILURE );
809 authmethod = LDAP_AUTH_SASL;
810 sasl_flags = LDAP_SASL_QUIET;
813 fprintf( stderr, "%s: not compiled with SASL support\n",
815 exit( EXIT_FAILURE );
818 #ifdef HAVE_CYRUS_SASL
819 if( sasl_realm != NULL ) {
820 fprintf( stderr, "%s: -R previously specified\n", prog );
821 exit( EXIT_FAILURE );
823 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
824 fprintf( stderr, "%s: incompatible previous "
825 "authentication choice\n",
827 exit( EXIT_FAILURE );
829 authmethod = LDAP_AUTH_SASL;
830 sasl_realm = ber_strdup( optarg );
832 fprintf( stderr, "%s: not compiled with SASL support\n",
834 exit( EXIT_FAILURE );
838 #ifdef HAVE_CYRUS_SASL
839 if( sasl_authc_id != NULL ) {
840 fprintf( stderr, "%s: -U previously specified\n", prog );
841 exit( EXIT_FAILURE );
843 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
844 fprintf( stderr, "%s: incompatible previous "
845 "authentication choice\n",
847 exit( EXIT_FAILURE );
849 authmethod = LDAP_AUTH_SASL;
850 sasl_authc_id = ber_strdup( optarg );
852 fprintf( stderr, "%s: not compiled with SASL support\n",
854 exit( EXIT_FAILURE );
857 case 'v': /* verbose mode */
860 case 'V': /* version */
863 case 'w': /* password */
864 passwd.bv_val = ber_strdup( optarg );
868 for( p = optarg; *p != '\0'; p++ ) {
872 passwd.bv_len = strlen( passwd.bv_val );
881 #ifdef HAVE_CYRUS_SASL
882 if( sasl_mech != NULL ) {
883 fprintf( stderr, "%s: -Y previously specified\n", prog );
884 exit( EXIT_FAILURE );
886 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
888 "%s: incompatible with authentication choice\n", prog );
889 exit( EXIT_FAILURE );
891 authmethod = LDAP_AUTH_SASL;
892 sasl_mech = ber_strdup( optarg );
894 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
895 exit( EXIT_FAILURE );
899 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
900 fprintf( stderr, "%s: incompatible with previous "
901 "authentication choice\n", prog );
902 exit( EXIT_FAILURE );
904 authmethod = LDAP_AUTH_SIMPLE;
907 #ifdef HAVE_CYRUS_SASL
908 if( sasl_authz_id != NULL ) {
909 fprintf( stderr, "%s: -X previously specified\n", prog );
910 exit( EXIT_FAILURE );
912 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
913 fprintf( stderr, "%s: -X incompatible with "
914 "authentication choice\n", prog );
915 exit( EXIT_FAILURE );
917 authmethod = LDAP_AUTH_SASL;
918 sasl_authz_id = ber_strdup( optarg );
920 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
921 exit( EXIT_FAILURE );
928 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
929 exit( EXIT_FAILURE );
933 if( handle_private_option( i ) ) break;
934 fprintf( stderr, "%s: unrecognized option -%c\n",
941 /* prevent bad linking */
943 api.ldapai_info_version = LDAP_API_INFO_VERSION;
945 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
946 != LDAP_OPT_SUCCESS )
948 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
949 exit( EXIT_FAILURE );
952 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
953 fprintf( stderr, "LDAP APIInfo version mismatch: "
954 "library %d, header %d\n",
955 api.ldapai_info_version, LDAP_API_INFO_VERSION );
956 exit( EXIT_FAILURE );
959 if( api.ldapai_api_version != LDAP_API_VERSION ) {
960 fprintf( stderr, "LDAP API version mismatch: "
961 "library %d, header %d\n",
962 api.ldapai_api_version, LDAP_API_VERSION );
963 exit( EXIT_FAILURE );
966 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
967 fprintf( stderr, "LDAP vendor name mismatch: "
968 "library %s, header %s\n",
969 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
970 exit( EXIT_FAILURE );
973 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
974 fprintf( stderr, "LDAP vendor version mismatch: "
975 "library %d, header %d\n",
976 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
977 exit( EXIT_FAILURE );
981 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
983 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
984 if (version > 1) exit( EXIT_SUCCESS );
987 ldap_memfree( api.ldapai_vendor_name );
988 ber_memvfree( (void **)api.ldapai_extensions );
992 protocol = LDAP_VERSION3;
994 if (authmethod == -1 && protocol > LDAP_VERSION2) {
995 #ifdef HAVE_CYRUS_SASL
996 if ( binddn != NULL ) {
997 authmethod = LDAP_AUTH_SIMPLE;
999 authmethod = LDAP_AUTH_SASL;
1002 authmethod = LDAP_AUTH_SIMPLE;
1006 if( ldapuri == NULL ) {
1007 if( ldapport && ( ldaphost == NULL )) {
1008 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
1009 exit( EXIT_FAILURE );
1012 if( ldaphost != NULL ) {
1013 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
1014 exit( EXIT_FAILURE );
1017 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
1018 exit( EXIT_FAILURE );
1022 if( protocol == LDAP_VERSION2 ) {
1023 if( assertctl || authzid || manageDIT || manageDSAit ||
1024 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1026 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1027 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1030 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1032 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1033 noop || ppolicy || preread || postread )
1035 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
1036 exit( EXIT_FAILURE );
1040 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
1041 exit( EXIT_FAILURE );
1044 #ifdef HAVE_CYRUS_SASL
1045 if( authmethod == LDAP_AUTH_SASL ) {
1046 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
1048 exit( EXIT_FAILURE );
1056 tool_conn_setup( int dont, void (*private_setup)( LDAP * ) )
1061 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
1062 != LBER_OPT_SUCCESS )
1065 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
1067 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
1068 != LDAP_OPT_SUCCESS )
1071 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
1076 (void) SIGNAL( SIGPIPE, SIG_IGN );
1080 SIGNAL( SIGINT, do_sig );
1086 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
1089 memset( &url, 0, sizeof(url));
1091 url.lud_scheme = "ldap";
1092 url.lud_host = ldaphost;
1093 url.lud_port = ldapport;
1094 url.lud_scope = LDAP_SCOPE_DEFAULT;
1096 ldapuri = ldap_url_desc2str( &url );
1098 } else if ( ldapuri != NULL ) {
1099 LDAPURLDesc *ludlist, **ludp;
1103 rc = ldap_url_parselist( &ludlist, ldapuri );
1104 if ( rc != LDAP_URL_SUCCESS ) {
1106 "Could not parse LDAP URI(s)=%s (%d)\n",
1108 exit( EXIT_FAILURE );
1111 for ( ludp = &ludlist; *ludp != NULL; ) {
1112 LDAPURLDesc *lud = *ludp;
1115 if ( lud->lud_dn != NULL && lud->lud_dn[ 0 ] != '\0' &&
1116 ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) )
1118 /* if no host but a DN is provided,
1119 * use DNS SRV to gather the host list
1120 * and turn it into a list of URIs
1121 * using the scheme provided */
1122 char *domain = NULL,
1126 len_proto = strlen( lud->lud_scheme );
1128 if ( ldap_dn2domain( lud->lud_dn, &domain )
1132 "DNS SRV: Could not turn "
1133 "DN=\"%s\" into a domain\n",
1138 rc = ldap_domain2hostlist( domain, &hostlist );
1141 "DNS SRV: Could not turn "
1142 "domain=%s into a hostlist\n",
1147 hosts = ldap_str2charray( hostlist, " " );
1148 if ( hosts == NULL ) {
1150 "DNS SRV: Could not parse "
1151 "hostlist=\"%s\"\n",
1156 for ( i = 0; hosts[ i ] != NULL; i++ )
1159 tmp = (char **)realloc( urls, sizeof( char * ) * ( nurls + i + 1 ) );
1160 if ( tmp == NULL ) {
1162 "DNS SRV: out of memory?\n" );
1166 urls[ nurls ] = NULL;
1168 for ( i = 0; hosts[ i ] != NULL; i++ ) {
1169 size_t len = len_proto
1171 + strlen( hosts[ i ] )
1174 urls[ nurls + i + 1 ] = NULL;
1175 urls[ nurls + i ] = (char *)malloc( sizeof( char ) * len );
1176 if ( urls[ nurls + i ] == NULL ) {
1178 "DNS SRV: out of memory?\n" );
1182 snprintf( urls[ nurls + i ], len, "%s://%s",
1183 lud->lud_scheme, hosts[ i ] );
1188 ber_memvfree( (void **)hosts );
1189 ber_memfree( hostlist );
1190 ber_memfree( domain );
1193 tmp = (char **)realloc( urls, sizeof( char * ) * ( nurls + 2 ) );
1194 if ( tmp == NULL ) {
1196 "DNS SRV: out of memory?\n" );
1200 urls[ nurls + 1 ] = NULL;
1202 urls[ nurls ] = ldap_url_desc2str( lud );
1203 if ( urls[ nurls ] == NULL ) {
1205 "DNS SRV: out of memory?\n" );
1211 *ludp = lud->lud_next;
1213 lud->lud_next = NULL;
1214 ldap_free_urldesc( lud );
1217 if ( ludlist != NULL ) {
1218 ldap_free_urllist( ludlist );
1219 exit( EXIT_FAILURE );
1221 } else if ( urls == NULL ) {
1222 exit( EXIT_FAILURE );
1225 ldap_memfree( ldapuri );
1226 ldapuri = ldap_charray2str( urls, " " );
1227 ber_memvfree( (void **)urls );
1231 fprintf( stderr, "ldap_initialize( %s )\n",
1232 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
1234 rc = ldap_initialize( &ld, ldapuri );
1235 if( rc != LDAP_SUCCESS ) {
1237 "Could not create LDAP session handle for URI=%s (%d): %s\n",
1238 ldapuri, rc, ldap_err2string(rc) );
1239 exit( EXIT_FAILURE );
1242 if( private_setup ) private_setup( ld );
1245 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
1246 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
1248 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
1249 referrals ? "on" : "off" );
1250 exit( EXIT_FAILURE );
1253 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
1254 != LDAP_OPT_SUCCESS )
1256 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
1258 exit( EXIT_FAILURE );
1262 rc = ldap_start_tls_s( ld, NULL, NULL );
1263 if ( rc != LDAP_SUCCESS ) {
1264 tool_perror( "ldap_start_tls", rc, NULL, NULL, NULL, NULL );
1265 if ( use_tls > 1 ) {
1266 exit( EXIT_FAILURE );
1271 if ( nettimeout.tv_sec > 0 ) {
1272 if ( ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &nettimeout )
1273 != LDAP_OPT_SUCCESS )
1275 fprintf( stderr, "Could not set LDAP_OPT_NETWORK_TIMEOUT %ld\n",
1276 (long)nettimeout.tv_sec );
1277 exit( EXIT_FAILURE );
1287 tool_bind( LDAP *ld )
1289 LDAPControl **sctrlsp = NULL;
1290 LDAPControl *sctrls[3];
1291 LDAPControl sctrl[3];
1294 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1297 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1298 c.ldctl_value.bv_val = NULL;
1299 c.ldctl_value.bv_len = 0;
1300 c.ldctl_iscritical = 0;
1302 sctrls[nsctrls] = &sctrl[nsctrls];
1303 sctrls[++nsctrls] = NULL;
1307 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1308 if ( sessionTracking ) {
1311 if (stValue.bv_val == NULL && st_value( ld, &stValue ) ) {
1312 exit( EXIT_FAILURE );
1315 c.ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1316 c.ldctl_iscritical = 0;
1317 ber_dupbv( &c.ldctl_value, &stValue );
1320 sctrls[nsctrls] = &sctrl[nsctrls];
1321 sctrls[++nsctrls] = NULL;
1323 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1329 assert( nsctrls < (int) (sizeof(sctrls)/sizeof(sctrls[0])) );
1331 if ( authmethod == LDAP_AUTH_SASL ) {
1332 #ifdef HAVE_CYRUS_SASL
1336 if( sasl_secprops != NULL ) {
1337 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
1338 (void *) sasl_secprops );
1340 if( rc != LDAP_OPT_SUCCESS ) {
1342 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
1344 exit( LDAP_LOCAL_ERROR );
1348 defaults = lutil_sasl_defaults( ld,
1355 rc = ldap_sasl_interactive_bind_s( ld, binddn, sasl_mech,
1357 NULL, sasl_flags, lutil_sasl_interact, defaults );
1359 lutil_sasl_freedefs( defaults );
1360 if( rc != LDAP_SUCCESS ) {
1361 tool_perror( "ldap_sasl_interactive_bind_s",
1362 rc, NULL, NULL, NULL, NULL );
1366 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1367 exit( LDAP_NOT_SUPPORTED );
1371 LDAPMessage *result;
1372 LDAPControl **ctrls;
1374 char *matched = NULL;
1382 rc = ldap_sasl_bind( ld, binddn, LDAP_SASL_SIMPLE, &passwd,
1383 sctrlsp, NULL, &msgid );
1384 if ( msgid == -1 ) {
1385 tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
1386 NULL, NULL, NULL, NULL );
1391 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 ) {
1392 tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
1393 exit( LDAP_LOCAL_ERROR );
1396 rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
1398 if ( rc != LDAP_SUCCESS ) {
1399 tool_perror( "ldap_bind parse result", rc, NULL, matched, info, refs );
1400 exit( LDAP_LOCAL_ERROR );
1403 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1404 if ( ctrls && ppolicy ) {
1406 int expire, grace, len = 0;
1407 LDAPPasswordPolicyError pErr = -1;
1409 ctrl = ldap_control_find( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
1412 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
1413 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1415 if ( pErr != PP_noError ){
1418 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1419 len = strlen( msgbuf );
1421 if ( expire >= 0 ) {
1422 sprintf( msgbuf+len,
1423 " (Password expires in %d seconds)",
1425 } else if ( grace >= 0 ) {
1426 sprintf( msgbuf+len,
1427 " (Password expired, %d grace logins remain)",
1435 ldap_controls_free( ctrls );
1438 if ( err != LDAP_SUCCESS
1440 || ( matched && matched[ 0 ] )
1441 || ( info && info[ 0 ] )
1444 tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1446 if( matched ) ber_memfree( matched );
1447 if( info ) ber_memfree( info );
1448 if( refs ) ber_memvfree( (void **)refs );
1450 if ( err != LDAP_SUCCESS ) exit( err );
1456 tool_unbind( LDAP *ld )
1458 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1460 if ( err != LDAP_OPT_SUCCESS ) {
1461 fprintf( stderr, "Could not unset controls\n");
1464 (void) ldap_unbind_ext( ld, NULL, NULL );
1468 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1470 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1472 int i = 0, j, crit = 0, err;
1473 LDAPControl c[16], **ctrls;
1477 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1479 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1483 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1488 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1490 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1491 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1493 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1495 || unknown_ctrls_num ) )
1500 ctrls = (LDAPControl**) malloc(sizeof(c) + (count + unknown_ctrls_num + 1)*sizeof(LDAPControl*));
1501 if ( ctrls == NULL ) {
1502 fprintf( stderr, "No memory\n" );
1503 exit( EXIT_FAILURE );
1507 if ( BER_BVISNULL( &assertionvalue ) ) {
1508 err = ldap_create_assertion_control_value( ld,
1509 assertion, &assertionvalue );
1512 "Unable to create assertion value "
1513 "\"%s\" (%d)\n", assertion, err );
1517 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1518 c[i].ldctl_value = assertionvalue;
1519 c[i].ldctl_iscritical = assertctl > 1;
1525 c[i].ldctl_value.bv_val = authzid;
1526 c[i].ldctl_value.bv_len = strlen( authzid );
1527 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1528 c[i].ldctl_iscritical = 1;
1533 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1534 /* NOTE: doesn't need an extra count because it's incompatible
1537 BerElementBuffer berbuf;
1538 BerElement *ber = (BerElement *)&berbuf;
1540 ber_init2( ber, NULL, LBER_USE_DER );
1542 if ( ber_printf( ber, "s", proxydn ) == -1 ) {
1543 exit( EXIT_FAILURE );
1546 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1547 exit( EXIT_FAILURE );
1550 c[i].ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1551 c[i].ldctl_iscritical = 1;
1555 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1558 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1559 BER_BVZERO( &c[i].ldctl_value );
1560 c[i].ldctl_iscritical = manageDIT > 1;
1565 if ( manageDSAit ) {
1566 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1567 BER_BVZERO( &c[i].ldctl_value );
1568 c[i].ldctl_iscritical = manageDSAit > 1;
1574 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1575 BER_BVZERO( &c[i].ldctl_value );
1576 c[i].ldctl_iscritical = noop > 1;
1581 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1583 c[i].ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1584 BER_BVZERO( &c[i].ldctl_value );
1585 c[i].ldctl_iscritical = 0;
1592 BerElementBuffer berbuf;
1593 BerElement *ber = (BerElement *)&berbuf;
1594 char **attrs = NULL;
1596 if( preread_attrs ) {
1597 attrs = ldap_str2charray( preread_attrs, "," );
1600 ber_init2( ber, NULL, LBER_USE_DER );
1602 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1603 fprintf( stderr, "preread attrs encode failed.\n" );
1604 exit( EXIT_FAILURE );
1607 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1609 fprintf( stderr, "preread flatten failed (%d)\n", err );
1610 exit( EXIT_FAILURE );
1613 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1614 c[i].ldctl_iscritical = preread > 1;
1618 if( attrs ) ldap_charray_free( attrs );
1622 BerElementBuffer berbuf;
1623 BerElement *ber = (BerElement *)&berbuf;
1624 char **attrs = NULL;
1626 if( postread_attrs ) {
1627 attrs = ldap_str2charray( postread_attrs, "," );
1630 ber_init2( ber, NULL, LBER_USE_DER );
1632 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1633 fprintf( stderr, "postread attrs encode failed.\n" );
1634 exit( EXIT_FAILURE );
1637 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1639 fprintf( stderr, "postread flatten failed (%d)\n", err );
1640 exit( EXIT_FAILURE );
1643 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1644 c[i].ldctl_iscritical = postread > 1;
1648 if( attrs ) ldap_charray_free( attrs );
1651 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1653 if ( chainingResolve > -1 ) {
1654 BerElementBuffer berbuf;
1655 BerElement *ber = (BerElement *)&berbuf;
1657 ber_init2( ber, NULL, LBER_USE_DER );
1659 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1662 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1663 exit( EXIT_FAILURE );
1666 if ( chainingContinuation > -1 ) {
1667 err = ber_printf( ber, "e", chainingContinuation );
1670 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1671 exit( EXIT_FAILURE );
1675 err = ber_printf( ber, /* { */ "N}" );
1678 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1679 exit( EXIT_FAILURE );
1682 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1683 exit( EXIT_FAILURE );
1687 BER_BVZERO( &c[i].ldctl_value );
1690 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1691 c[i].ldctl_iscritical = chaining > 1;
1695 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1697 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1698 if ( sessionTracking ) {
1699 if ( stValue.bv_val == NULL && st_value( ld, &stValue ) ) {
1700 exit( EXIT_FAILURE );
1703 c[i].ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1704 c[i].ldctl_iscritical = 0;
1705 ber_dupbv( &c[i].ldctl_value, &stValue );
1710 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1713 ctrls[i++] = extra_c++;
1715 for ( count = 0; count < unknown_ctrls_num; count++ ) {
1716 ctrls[i++] = &unknown_ctrls[count];
1720 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1722 if ( err != LDAP_OPT_SUCCESS ) {
1723 for ( j = 0; j < i; j++ ) {
1724 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1726 fprintf( stderr, "Could not set %scontrols\n",
1727 crit ? "critical " : "" );
1732 exit( EXIT_FAILURE );
1737 tool_check_abandon( LDAP *ld, int msgid )
1741 switch ( gotintr ) {
1743 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1744 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1745 rc, ldap_err2string( rc ) );
1749 rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
1750 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1751 rc, ldap_err2string( rc ) );
1755 /* just unbind, ignoring the request */
1763 print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
1768 tool_write_ldif( LDIF_PUT_COMMENT, "==> ",
1769 what->bv_val, what->bv_len );
1770 ber = ber_init( &ctrl->ldctl_value );
1771 if ( ber == NULL ) {
1775 } else if ( ber_scanf( ber, "{m{" /*}}*/, &bv ) == LBER_ERROR ) {
1780 tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1782 while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
1784 BerVarray vals = NULL;
1786 if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
1793 for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
1795 ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1796 bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
1799 ber_bvarray_free( vals );
1803 if ( ber != NULL ) {
1807 tool_write_ldif( LDIF_PUT_COMMENT, "<== ",
1808 what->bv_val, what->bv_len );
1814 print_preread( LDAP *ld, LDAPControl *ctrl )
1816 static struct berval what = BER_BVC( "preread" );
1818 return print_prepostread( ld, ctrl, &what );
1822 print_postread( LDAP *ld, LDAPControl *ctrl )
1824 static struct berval what = BER_BVC( "postread" );
1826 return print_prepostread( ld, ctrl, &what );
1830 print_paged_results( LDAP *ld, LDAPControl *ctrl )
1834 /* note: pr_cookie is being malloced; it's freed
1835 * the next time the control is sent, but the last
1836 * time it's not; we don't care too much, because
1837 * the last time an empty value is returned... */
1838 if ( ldap_parse_pageresponse_control( ld, ctrl, &estimate, &pr_cookie )
1845 /* FIXME: check buffer overflow */
1846 char buf[ BUFSIZ ], *ptr = buf;
1848 if ( estimate > 0 ) {
1849 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1850 "estimate=%d", estimate );
1853 if ( pr_cookie.bv_len > 0 ) {
1856 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
1857 pr_cookie.bv_len ) + 1;
1858 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
1860 bv.bv_len = lutil_b64_ntop(
1861 (unsigned char *) pr_cookie.bv_val,
1863 bv.bv_val, bv.bv_len );
1865 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1866 "%scookie=%s", ptr == buf ? "" : " ",
1869 ber_memfree( bv.bv_val );
1871 pr_morePagedResults = 1;
1874 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1875 "%scookie=", ptr == buf ? "" : " " );
1878 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1879 "pagedresults", buf, ptr - buf );
1886 print_sss( LDAP *ld, LDAPControl *ctrl )
1892 rc = ldap_parse_sortresponse_control( ld, ctrl, &err, &attr );
1893 if ( rc == LDAP_SUCCESS ) {
1895 rc = snprintf( buf, sizeof(buf), "(%d) %s %s",
1896 err, ldap_err2string(err), attr ? attr : "" );
1898 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1899 "sortResult", buf, rc );
1905 #ifdef LDAP_CONTROL_X_DEREF
1907 print_deref( LDAP *ld, LDAPControl *ctrl )
1909 LDAPDerefRes *drhead = NULL, *dr;
1912 rc = ldap_parse_derefresponse_control( ld, ctrl, &drhead );
1913 if ( rc != LDAP_SUCCESS ) {
1917 for ( dr = drhead; dr != NULL; dr = dr->next ) {
1922 len = strlen( dr->derefAttr ) + STRLENOF(": ");
1924 for ( dv = dr->attrVals; dv != NULL; dv = dv->next ) {
1925 if ( dv->vals != NULL ) {
1927 ber_len_t tlen = strlen(dv->type);
1929 for ( j = 0; dv->vals[ j ].bv_val != NULL; j++ ) {
1930 len += STRLENOF("<=>;") + tlen + dv->vals[ j ].bv_len;
1934 len += dr->derefVal.bv_len;
1935 buf = ldap_memalloc( len + 1 );
1936 if ( buf == NULL ) {
1937 rc = LDAP_NO_MEMORY;
1942 ptr = lutil_strcopy( ptr, dr->derefAttr );
1945 for ( dv = dr->attrVals; dv != NULL; dv = dv->next ) {
1946 if ( dv->vals != NULL ) {
1948 for ( j = 0; dv->vals[ j ].bv_val != NULL; j++ ) {
1950 ptr = lutil_strcopy( ptr, dv->type );
1952 ptr = lutil_strncopy( ptr, dv->vals[ j ].bv_val, dv->vals[ j ].bv_len );
1958 ptr = lutil_strncopy( ptr, dr->derefVal.bv_val, dr->derefVal.bv_len );
1961 tool_write_ldif( LDIF_PUT_COMMENT, NULL, buf, len );
1963 ldap_memfree( buf );
1969 ldap_derefresponse_free( drhead );
1975 #ifdef LDAP_CONTROL_X_WHATFAILED
1977 print_whatfailed( LDAP *ld, LDAPControl *ctrl )
1982 BerVarray bva = NULL;
1984 /* Create a BerElement from the berval returned in the control. */
1985 ber = ber_init( &ctrl->ldctl_value );
1987 if ( ber == NULL ) {
1988 return LDAP_NO_MEMORY;
1991 siz = sizeof(struct berval);
1992 tag = ber_scanf( ber, "[M]", &bva, &siz, 0 );
1993 if ( tag != LBER_ERROR ) {
1996 tool_write_ldif( LDIF_PUT_COMMENT, " what failed:", NULL, 0 );
1998 for ( i = 0; bva[i].bv_val != NULL; i++ ) {
1999 tool_write_ldif( LDIF_PUT_COMMENT, NULL, bva[i].bv_val, bva[i].bv_len );
2002 ldap_memfree( bva );
2012 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
2014 print_ppolicy( LDAP *ld, LDAPControl *ctrl )
2016 int expire = 0, grace = 0, rc;
2017 LDAPPasswordPolicyError pperr;
2019 rc = ldap_parse_passwordpolicy_control( ld, ctrl,
2020 &expire, &grace, &pperr );
2021 if ( rc == LDAP_SUCCESS ) {
2022 char buf[ BUFSIZ ], *ptr = buf;
2024 if ( expire != -1 ) {
2025 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2026 "expire=%d", expire );
2029 if ( grace != -1 ) {
2030 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2031 "%sgrace=%d", ptr == buf ? "" : " ", grace );
2034 if ( pperr != PP_noError ) {
2035 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2036 "%serror=%d (%s)", ptr == buf ? "" : " ",
2038 ldap_passwordpolicy_err2txt( pperr ) );
2041 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2042 "ppolicy", buf, ptr - buf );
2049 void tool_print_ctrls(
2051 LDAPControl **ctrls )
2056 for ( i = 0; ctrls[i] != NULL; i++ ) {
2057 /* control: OID criticality base64value */
2058 struct berval b64 = BER_BVNULL;
2063 /* FIXME: there might be cases where a control has NULL OID;
2064 * this makes little sense, especially when returned by the
2065 * server, but libldap happily allows it */
2066 if ( ctrls[i]->ldctl_oid == NULL ) {
2071 len += strlen( ctrls[i]->ldctl_oid );
2073 /* add enough for space after OID and the critical value itself */
2074 len += ctrls[i]->ldctl_iscritical
2075 ? sizeof("true") : sizeof("false");
2077 /* convert to base64 */
2078 if ( !BER_BVISNULL( &ctrls[i]->ldctl_value ) ) {
2079 b64.bv_len = LUTIL_BASE64_ENCODE_LEN(
2080 ctrls[i]->ldctl_value.bv_len ) + 1;
2081 b64.bv_val = ber_memalloc( b64.bv_len + 1 );
2083 b64.bv_len = lutil_b64_ntop(
2084 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
2085 ctrls[i]->ldctl_value.bv_len,
2086 b64.bv_val, b64.bv_len );
2090 len += 1 + b64.bv_len;
2093 ptr = str = malloc( len + 1 );
2095 ptr = lutil_strcopy( ptr, ": " );
2097 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_oid );
2098 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_iscritical
2099 ? " true" : " false" );
2102 ptr = lutil_strcopy( ptr, " " );
2103 ptr = lutil_strcopy( ptr, b64.bv_val );
2107 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2108 "control", str, len );
2113 ber_memfree( b64.bv_val );
2116 /* known controls */
2117 for ( j = 0; tool_ctrl_response[j].oid != NULL; j++ ) {
2118 if ( strcmp( tool_ctrl_response[j].oid, ctrls[i]->ldctl_oid ) == 0 ) {
2119 if ( !tool_ctrl_response[j].mask & tool_type ) {
2120 /* this control should not appear
2121 * with this tool; warning? */
2127 if ( tool_ctrl_response[j].oid != NULL && tool_ctrl_response[j].func ) {
2128 (void)tool_ctrl_response[j].func( ld, ctrls[i] );
2134 tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
2138 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
2142 fputs( ldif, stdout );
2143 ber_memfree( ldif );
2149 tool_is_oid( const char *s )
2153 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2157 for ( ; s[ 0 ]; s++ ) {
2158 if ( s[ 0 ] == '.' ) {
2159 if ( s[ 1 ] == '\0' ) {
2166 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2170 if ( first == 1 && s[ 0 ] == '0' && s[ 1 ] != '.' ) {