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-2009 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;
73 char *ldaphost = NULL;
83 struct berval passwd = { 0, NULL };
85 #ifdef HAVE_CYRUS_SASL
86 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
87 char *sasl_realm = NULL;
88 char *sasl_authc_id = NULL;
89 char *sasl_authz_id = NULL;
90 char *sasl_mech = NULL;
91 char *sasl_secprops = NULL;
96 char *assertion = NULL;
97 struct berval assertionvalue = BER_BVNULL;
99 /* support deprecated early version of proxyAuthz */
100 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ "2.16.840.1.113730.3.4.12"
101 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
102 char *proxydn = NULL;
103 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
109 static char *preread_attrs = NULL;
111 static char *postread_attrs = NULL;
112 ber_int_t pr_morePagedResults = 1;
113 struct berval pr_cookie = { 0, NULL };
114 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
116 static int chainingResolve = -1;
117 static int chainingContinuation = -1;
118 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
119 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
120 static int sessionTracking = 0;
121 struct berval stValue;
122 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
124 LDAPControl *unknown_ctrls = NULL;
125 int unknown_ctrls_num = 0;
128 struct timeval nettimeout = { -1 , 0 };
130 typedef int (*print_ctrl_fn)( LDAP *ld, LDAPControl *ctrl );
132 static int print_preread( LDAP *ld, LDAPControl *ctrl );
133 static int print_postread( LDAP *ld, LDAPControl *ctrl );
134 static int print_paged_results( LDAP *ld, LDAPControl *ctrl );
135 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
136 static int print_ppolicy( LDAP *ld, LDAPControl *ctrl );
138 static int print_sss( LDAP *ld, LDAPControl *ctrl );
139 #ifdef LDAP_CONTROL_X_DEREF
140 static int print_deref( LDAP *ld, LDAPControl *ctrl );
142 #ifdef LDAP_CONTROL_X_WHATFAILED
143 static int print_whatfailed( LDAP *ld, LDAPControl *ctrl );
146 static struct tool_ctrls_t {
150 } tool_ctrl_response[] = {
151 { LDAP_CONTROL_PRE_READ, TOOL_ALL, print_preread },
152 { LDAP_CONTROL_POST_READ, TOOL_ALL, print_postread },
153 { LDAP_CONTROL_PAGEDRESULTS, TOOL_SEARCH, print_paged_results },
154 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
155 { LDAP_CONTROL_PASSWORDPOLICYRESPONSE, TOOL_ALL, print_ppolicy },
157 { LDAP_CONTROL_SORTRESPONSE, TOOL_SEARCH, print_sss },
158 #ifdef LDAP_CONTROL_X_DEREF
159 { LDAP_CONTROL_X_DEREF, TOOL_SEARCH, print_deref },
161 #ifdef LDAP_CONTROL_X_WHATFAILED
162 { LDAP_CONTROL_X_WHATFAILED, TOOL_ALL, print_whatfailed },
168 enum { Intr_None = 0, Intr_Abandon, Intr_Cancel, Intr_Ignore };
169 static volatile sig_atomic_t gotintr, abcan;
172 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
174 st_value( LDAP *ld, struct berval *value )
176 char *ip = NULL, *name = NULL;
177 struct berval id = { 0 };
178 char namebuf[ MAXHOSTNAMELEN ];
180 if ( gethostname( namebuf, sizeof( namebuf ) ) == 0 ) {
186 h = gethostbyname( name );
188 AC_MEMCPY( &addr, h->h_addr, sizeof( addr ) );
189 ip = inet_ntoa( addr );
193 #ifdef HAVE_CYRUS_SASL
194 if ( sasl_authz_id != NULL ) {
195 ber_str2bv( sasl_authz_id, 0, 0, &id );
197 } else if ( sasl_authc_id != NULL ) {
198 ber_str2bv( sasl_authc_id, 0, 0, &id );
201 #endif /* HAVE_CYRUS_SASL */
202 if ( binddn != NULL ) {
203 ber_str2bv( binddn, 0, 0, &id );
206 if ( ldap_create_session_tracking_value( ld,
207 ip, name, LDAP_CONTROL_X_SESSION_TRACKING_USERNAME,
210 fprintf( stderr, _("Session tracking control encoding error!\n") );
216 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
225 tool_init( tool_type_t type )
228 ldap_pvt_setlocale(LC_MESSAGES, "");
229 ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
230 ldap_pvt_textdomain(OPENLDAP_PACKAGE);
236 #ifdef HAVE_CYRUS_SASL
240 ldap_pvt_tls_destroy();
243 if ( ldapuri != NULL ) {
244 ber_memfree( ldapuri );
248 if ( pr_cookie.bv_val != NULL ) {
249 ber_memfree( pr_cookie.bv_val );
250 pr_cookie.bv_val = NULL;
251 pr_cookie.bv_len = 0;
254 if ( binddn != NULL ) {
255 ber_memfree( binddn );
258 if ( passwd.bv_val != NULL ) {
259 ber_memfree( passwd.bv_val );
264 tool_common_usage( void )
266 static const char *const descriptions[] = {
267 N_(" -c continuous operation mode (do not stop on errors)\n"),
268 N_(" -d level set LDAP debugging level to `level'\n"),
269 N_(" -D binddn bind DN\n"),
270 N_(" -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
271 N_(" [!]assert=<filter> (RFC 4528; a RFC 4515 Filter string)\n")
272 N_(" [!]authzid=<authzid> (RFC 4370; \"dn:<dn>\" or \"u:<user>\")\n")
273 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
275 /* non-advertized support for proxyDN */
276 N_(" [!]proxydn=<dn> (a RFC 4514 DN string)\n")
279 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
280 N_(" [!]chaining[=<resolveBehavior>[/<continuationBehavior>]]\n")
281 N_(" one of \"chainingPreferred\", \"chainingRequired\",\n")
282 N_(" \"referralsPreferred\", \"referralsRequired\"\n")
283 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
284 N_(" [!]manageDSAit (RFC 3296)\n")
286 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
289 N_(" [!]postread[=<attrs>] (RFC 4527; comma-separated attr list)\n")
290 N_(" [!]preread[=<attrs>] (RFC 4527; comma-separated attr list)\n")
292 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
293 N_(" [!]sessiontracking\n")
294 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
295 N_(" abandon, cancel, ignore (SIGINT sends abandon/cancel,\n"
296 " or ignores response; if critical, doesn't wait for SIGINT.\n"
297 " not really controls)\n")
298 N_(" -f file read operations from `file'\n"),
299 N_(" -h host LDAP server\n"),
300 N_(" -H URI LDAP Uniform Resource Identifier(s)\n"),
301 N_(" -I use SASL Interactive mode\n"),
302 N_(" -M enable Manage DSA IT control (-MM to make critical)\n"),
303 N_(" -n show what would be done but don't actually do it\n"),
304 N_(" -N do not use reverse DNS to canonicalize SASL host name\n"),
305 N_(" -O props SASL security properties\n"),
306 N_(" -o <opt>[=<optparam] general options\n"),
307 N_(" nettimeout=<timeout> (in seconds, or \"none\" or \"max\")\n"),
308 N_(" -p port port on LDAP server\n"),
309 N_(" -P version protocol version (default: 3)\n"),
310 N_(" -Q use SASL Quiet mode\n"),
311 N_(" -R realm SASL realm\n"),
312 N_(" -U authcid SASL authentication identity\n"),
313 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
314 N_(" -V print version info (-VV only)\n"),
315 N_(" -w passwd bind password (for simple authentication)\n"),
316 N_(" -W prompt for bind password\n"),
317 N_(" -x Simple authentication\n"),
318 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
319 N_(" -y file Read password from file\n"),
320 N_(" -Y mech SASL mechanism\n"),
321 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
324 const char *const *cpp;
326 fputs( _("Common options:\n"), stderr );
327 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
328 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
329 fputs( _(*cpp), stderr );
342 fprintf( stderr, "%s: %s (%d)%s\n",
343 func, ldap_err2string( err ), err, extra ? extra : "" );
345 if ( matched && *matched ) {
346 fprintf( stderr, _("\tmatched DN: %s\n"), matched );
349 if ( info && *info ) {
350 fprintf( stderr, _("\tadditional info: %s\n"), info );
353 if ( refs && *refs ) {
355 fprintf( stderr, _("\treferrals:\n") );
356 for( i=0; refs[i]; i++ ) {
357 fprintf( stderr, "\t\t%s\n", refs[i] );
364 tool_args( int argc, char **argv )
368 while (( i = getopt( argc, argv, options )) != EOF ) {
370 char *control, *cvalue, *next;
372 case 'c': /* continuous operation mode */
379 ival = strtol( optarg, &next, 10 );
380 if (next == NULL || next[0] != '\0') {
381 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
386 case 'D': /* bind DN */
387 if( binddn != NULL ) {
388 fprintf( stderr, "%s: -D previously specified\n", prog );
389 exit( EXIT_FAILURE );
391 binddn = ber_strdup( optarg );
393 case 'e': /* general extensions (controls and such) */
394 /* should be extended to support comma separated list of
395 * [!]key[=value] parameters, e.g. -e !foo,bar=567
400 if( optarg[0] == '!' ) {
405 control = ber_strdup( optarg );
406 if ( (cvalue = strchr( control, '=' )) != NULL ) {
410 if ( strcasecmp( control, "assert" ) == 0 ) {
412 fprintf( stderr, "assert control previously specified\n");
413 exit( EXIT_FAILURE );
415 if( cvalue == NULL ) {
416 fprintf( stderr, "assert: control value expected\n" );
420 assertctl = 1 + crit;
422 assert( assertion == NULL );
425 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
426 if( authzid != NULL ) {
427 fprintf( stderr, "authzid control previously specified\n");
428 exit( EXIT_FAILURE );
430 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
431 if( proxydn != NULL ) {
432 fprintf( stderr, "authzid control incompatible with proxydn\n");
433 exit( EXIT_FAILURE );
435 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
436 if( cvalue == NULL ) {
437 fprintf( stderr, "authzid: control value expected\n" );
441 fprintf( stderr, "authzid: must be marked critical\n" );
445 assert( authzid == NULL );
448 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
449 } else if ( strcasecmp( control, "proxydn" ) == 0 ) {
450 if( proxydn != NULL ) {
451 fprintf( stderr, "proxydn control previously specified\n");
452 exit( EXIT_FAILURE );
454 if( authzid != NULL ) {
455 fprintf( stderr, "proxydn control incompatible with authzid\n");
456 exit( EXIT_FAILURE );
458 if( cvalue == NULL ) {
459 fprintf( stderr, "proxydn: control value expected\n" );
463 fprintf( stderr, "proxydn: must be marked critical\n" );
467 assert( proxydn == NULL );
469 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
471 } else if ( ( strcasecmp( control, "relax" ) == 0 ) ||
472 ( strcasecmp( control, "manageDIT" ) == 0 ) )
476 "relax control previously specified\n");
477 exit( EXIT_FAILURE );
479 if( cvalue != NULL ) {
481 "relax: no control value expected\n" );
485 manageDIT = 1 + crit;
487 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
490 "manageDSAit control previously specified\n");
491 exit( EXIT_FAILURE );
493 if( cvalue != NULL ) {
495 "manageDSAit: no control value expected\n" );
499 manageDSAit = 1 + crit;
501 } else if ( strcasecmp( control, "noop" ) == 0 ) {
503 fprintf( stderr, "noop control previously specified\n");
504 exit( EXIT_FAILURE );
506 if( cvalue != NULL ) {
507 fprintf( stderr, "noop: no control value expected\n" );
513 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
514 } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
516 fprintf( stderr, "ppolicy control previously specified\n");
517 exit( EXIT_FAILURE );
519 if( cvalue != NULL ) {
520 fprintf( stderr, "ppolicy: no control value expected\n" );
524 fprintf( stderr, "ppolicy: critical flag not allowed\n" );
531 } else if ( strcasecmp( control, "preread" ) == 0 ) {
533 fprintf( stderr, "preread control previously specified\n");
534 exit( EXIT_FAILURE );
538 preread_attrs = cvalue;
540 } else if ( strcasecmp( control, "postread" ) == 0 ) {
542 fprintf( stderr, "postread control previously specified\n");
543 exit( EXIT_FAILURE );
547 postread_attrs = cvalue;
549 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
550 } else if ( strcasecmp( control, "chaining" ) == 0 ) {
553 if ( cvalue != NULL ) {
556 continuation = strchr( cvalue, '/' );
557 if ( continuation ) {
558 /* FIXME: this makes sense only in searches */
559 *continuation++ = '\0';
560 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
561 chainingContinuation = LDAP_CHAINING_PREFERRED;
562 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
563 chainingContinuation = LDAP_CHAINING_REQUIRED;
564 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
565 chainingContinuation = LDAP_REFERRALS_PREFERRED;
566 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
567 chainingContinuation = LDAP_REFERRALS_REQUIRED;
570 "chaining behavior control "
571 "continuation value \"%s\" invalid\n",
573 exit( EXIT_FAILURE );
577 if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
578 chainingResolve = LDAP_CHAINING_PREFERRED;
579 } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
580 chainingResolve = LDAP_CHAINING_REQUIRED;
581 } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
582 chainingResolve = LDAP_REFERRALS_PREFERRED;
583 } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
584 chainingResolve = LDAP_REFERRALS_REQUIRED;
587 "chaining behavior control "
588 "resolve value \"%s\" invalid\n",
590 exit( EXIT_FAILURE );
593 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
595 /* this shouldn't go here, really; but it's a feature... */
596 } else if ( strcasecmp( control, "abandon" ) == 0 ) {
597 abcan = Intr_Abandon;
602 } else if ( strcasecmp( control, "cancel" ) == 0 ) {
608 } else if ( strcasecmp( control, "ignore" ) == 0 ) {
614 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
615 } else if ( strcasecmp( control, "sessiontracking" ) == 0 ) {
616 if ( sessionTracking ) {
617 fprintf( stderr, "%s: session tracking can be only specified once\n", prog );
618 exit( EXIT_FAILURE );
621 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
623 } else if ( tool_is_oid( control ) ) {
624 LDAPControl *tmpctrls, ctrl;
626 tmpctrls = (LDAPControl *)ber_memrealloc( unknown_ctrls,
627 (unknown_ctrls_num + 1)*sizeof( LDAPControl ) );
628 if ( tmpctrls == NULL ) {
629 fprintf( stderr, "%s: no memory?\n", prog );
630 exit( EXIT_FAILURE );
632 unknown_ctrls = tmpctrls;
633 ctrl.ldctl_oid = control;
634 ctrl.ldctl_value.bv_val = NULL;
635 ctrl.ldctl_value.bv_len = 0;
636 ctrl.ldctl_iscritical = crit;
638 if ( cvalue != NULL ) {
640 size_t len = strlen( cvalue );
643 bv.bv_len = LUTIL_BASE64_DECODE_LEN( len );
644 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
646 retcode = lutil_b64_pton( cvalue,
647 (unsigned char *)bv.bv_val,
650 if ( retcode == -1 || (unsigned) retcode > bv.bv_len ) {
651 fprintf( stderr, "Unable to parse value of general control %s\n",
657 ctrl.ldctl_value = bv;
660 unknown_ctrls[ unknown_ctrls_num ] = ctrl;
664 fprintf( stderr, "Invalid general control name: %s\n",
669 case 'f': /* read from file */
670 if( infile != NULL ) {
671 fprintf( stderr, "%s: -f previously specified\n", prog );
672 exit( EXIT_FAILURE );
674 infile = ber_strdup( optarg );
676 case 'h': /* ldap host */
677 if( ldaphost != NULL ) {
678 fprintf( stderr, "%s: -h previously specified\n", prog );
679 exit( EXIT_FAILURE );
681 ldaphost = ber_strdup( optarg );
683 case 'H': /* ldap URI */
684 if( ldapuri != NULL ) {
685 fprintf( stderr, "%s: -H previously specified\n", prog );
686 exit( EXIT_FAILURE );
688 ldapuri = ber_strdup( optarg );
691 #ifdef HAVE_CYRUS_SASL
692 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
693 fprintf( stderr, "%s: incompatible previous "
694 "authentication choice\n",
696 exit( EXIT_FAILURE );
698 authmethod = LDAP_AUTH_SASL;
699 sasl_flags = LDAP_SASL_INTERACTIVE;
702 fprintf( stderr, "%s: was not compiled with SASL support\n",
704 exit( EXIT_FAILURE );
707 /* enable Manage DSA IT */
710 case 'n': /* print operations, don't actually do them */
717 control = ber_strdup( optarg );
718 if ( (cvalue = strchr( control, '=' )) != NULL ) {
722 if ( strcasecmp( control, "nettimeout" ) == 0 ) {
723 if( nettimeout.tv_sec != -1 ) {
724 fprintf( stderr, "nettimeout option previously specified\n");
725 exit( EXIT_FAILURE );
727 if( cvalue == NULL || cvalue[0] == '\0' ) {
728 fprintf( stderr, "nettimeout: option value expected\n" );
731 if ( strcasecmp( cvalue, "none" ) == 0 ) {
732 nettimeout.tv_sec = 0;
733 } else if ( strcasecmp( cvalue, "max" ) == 0 ) {
734 nettimeout.tv_sec = LDAP_MAXINT;
736 ival = strtol( cvalue, &next, 10 );
737 if ( next == NULL || next[0] != '\0' ) {
739 _("Unable to parse network timeout \"%s\"\n"), cvalue );
740 exit( EXIT_FAILURE );
742 nettimeout.tv_sec = ival;
744 if( nettimeout.tv_sec < 0 || nettimeout.tv_sec > LDAP_MAXINT ) {
745 fprintf( stderr, _("%s: invalid network timeout (%ld) specified\n"),
746 prog, (long)nettimeout.tv_sec );
747 exit( EXIT_FAILURE );
750 fprintf( stderr, "Invalid general option name: %s\n",
756 #ifdef HAVE_CYRUS_SASL
757 if( sasl_secprops != NULL ) {
758 fprintf( stderr, "%s: -O previously specified\n", prog );
759 exit( EXIT_FAILURE );
761 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
762 fprintf( stderr, "%s: incompatible previous "
763 "authentication choice\n", prog );
764 exit( EXIT_FAILURE );
766 authmethod = LDAP_AUTH_SASL;
767 sasl_secprops = ber_strdup( optarg );
769 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
770 exit( EXIT_FAILURE );
775 fprintf( stderr, "%s: -p previously specified\n", prog );
776 exit( EXIT_FAILURE );
778 ival = strtol( optarg, &next, 10 );
779 if ( next == NULL || next[0] != '\0' ) {
780 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
781 exit( EXIT_FAILURE );
786 ival = strtol( optarg, &next, 10 );
787 if ( next == NULL || next[0] != '\0' ) {
788 fprintf( stderr, "%s: unable to parse protocol version \"%s\"\n", prog, optarg );
789 exit( EXIT_FAILURE );
793 if( protocol == LDAP_VERSION3 ) {
794 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
796 exit( EXIT_FAILURE );
798 protocol = LDAP_VERSION2;
801 if( protocol == LDAP_VERSION2 ) {
802 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
804 exit( EXIT_FAILURE );
806 protocol = LDAP_VERSION3;
809 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
815 #ifdef HAVE_CYRUS_SASL
816 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
817 fprintf( stderr, "%s: incompatible previous "
818 "authentication choice\n",
820 exit( EXIT_FAILURE );
822 authmethod = LDAP_AUTH_SASL;
823 sasl_flags = LDAP_SASL_QUIET;
826 fprintf( stderr, "%s: not compiled with SASL support\n",
828 exit( EXIT_FAILURE );
831 #ifdef HAVE_CYRUS_SASL
832 if( sasl_realm != NULL ) {
833 fprintf( stderr, "%s: -R previously specified\n", prog );
834 exit( EXIT_FAILURE );
836 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
837 fprintf( stderr, "%s: incompatible previous "
838 "authentication choice\n",
840 exit( EXIT_FAILURE );
842 authmethod = LDAP_AUTH_SASL;
843 sasl_realm = ber_strdup( optarg );
845 fprintf( stderr, "%s: not compiled with SASL support\n",
847 exit( EXIT_FAILURE );
851 #ifdef HAVE_CYRUS_SASL
852 if( sasl_authc_id != NULL ) {
853 fprintf( stderr, "%s: -U previously specified\n", prog );
854 exit( EXIT_FAILURE );
856 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
857 fprintf( stderr, "%s: incompatible previous "
858 "authentication choice\n",
860 exit( EXIT_FAILURE );
862 authmethod = LDAP_AUTH_SASL;
863 sasl_authc_id = ber_strdup( optarg );
865 fprintf( stderr, "%s: not compiled with SASL support\n",
867 exit( EXIT_FAILURE );
870 case 'v': /* verbose mode */
873 case 'V': /* version */
876 case 'w': /* password */
877 passwd.bv_val = ber_strdup( optarg );
881 for( p = optarg; *p != '\0'; p++ ) {
885 passwd.bv_len = strlen( passwd.bv_val );
894 #ifdef HAVE_CYRUS_SASL
895 if( sasl_mech != NULL ) {
896 fprintf( stderr, "%s: -Y previously specified\n", prog );
897 exit( EXIT_FAILURE );
899 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
901 "%s: incompatible with authentication choice\n", prog );
902 exit( EXIT_FAILURE );
904 authmethod = LDAP_AUTH_SASL;
905 sasl_mech = ber_strdup( optarg );
907 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
908 exit( EXIT_FAILURE );
912 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
913 fprintf( stderr, "%s: incompatible with previous "
914 "authentication choice\n", prog );
915 exit( EXIT_FAILURE );
917 authmethod = LDAP_AUTH_SIMPLE;
920 #ifdef HAVE_CYRUS_SASL
921 if( sasl_authz_id != NULL ) {
922 fprintf( stderr, "%s: -X previously specified\n", prog );
923 exit( EXIT_FAILURE );
925 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
926 fprintf( stderr, "%s: -X incompatible with "
927 "authentication choice\n", prog );
928 exit( EXIT_FAILURE );
930 authmethod = LDAP_AUTH_SASL;
931 sasl_authz_id = ber_strdup( optarg );
933 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
934 exit( EXIT_FAILURE );
941 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
942 exit( EXIT_FAILURE );
946 if( handle_private_option( i ) ) break;
947 fprintf( stderr, "%s: unrecognized option -%c\n",
954 /* prevent bad linking */
956 api.ldapai_info_version = LDAP_API_INFO_VERSION;
958 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
959 != LDAP_OPT_SUCCESS )
961 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
962 exit( EXIT_FAILURE );
965 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
966 fprintf( stderr, "LDAP APIInfo version mismatch: "
967 "library %d, header %d\n",
968 api.ldapai_info_version, LDAP_API_INFO_VERSION );
969 exit( EXIT_FAILURE );
972 if( api.ldapai_api_version != LDAP_API_VERSION ) {
973 fprintf( stderr, "LDAP API version mismatch: "
974 "library %d, header %d\n",
975 api.ldapai_api_version, LDAP_API_VERSION );
976 exit( EXIT_FAILURE );
979 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
980 fprintf( stderr, "LDAP vendor name mismatch: "
981 "library %s, header %s\n",
982 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
983 exit( EXIT_FAILURE );
986 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
987 fprintf( stderr, "LDAP vendor version mismatch: "
988 "library %d, header %d\n",
989 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
990 exit( EXIT_FAILURE );
994 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
996 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
997 if (version > 1) exit( EXIT_SUCCESS );
1000 ldap_memfree( api.ldapai_vendor_name );
1001 ber_memvfree( (void **)api.ldapai_extensions );
1005 protocol = LDAP_VERSION3;
1007 if (authmethod == -1 && protocol > LDAP_VERSION2) {
1008 #ifdef HAVE_CYRUS_SASL
1009 if ( binddn != NULL ) {
1010 authmethod = LDAP_AUTH_SIMPLE;
1012 authmethod = LDAP_AUTH_SASL;
1015 authmethod = LDAP_AUTH_SIMPLE;
1019 if( ldapuri == NULL ) {
1020 if( ldapport && ( ldaphost == NULL )) {
1021 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
1022 exit( EXIT_FAILURE );
1025 if( ldaphost != NULL ) {
1026 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
1027 exit( EXIT_FAILURE );
1030 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
1031 exit( EXIT_FAILURE );
1035 if( protocol == LDAP_VERSION2 ) {
1036 if( assertctl || authzid || manageDIT || manageDSAit ||
1037 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1039 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1040 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1043 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1045 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1046 noop || ppolicy || preread || postread )
1048 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
1049 exit( EXIT_FAILURE );
1053 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
1054 exit( EXIT_FAILURE );
1057 #ifdef HAVE_CYRUS_SASL
1058 if( authmethod == LDAP_AUTH_SASL ) {
1059 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
1061 exit( EXIT_FAILURE );
1069 tool_conn_setup( int dont, void (*private_setup)( LDAP * ) )
1074 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
1075 != LBER_OPT_SUCCESS )
1078 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
1080 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
1081 != LDAP_OPT_SUCCESS )
1084 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
1089 (void) SIGNAL( SIGPIPE, SIG_IGN );
1093 SIGNAL( SIGINT, do_sig );
1099 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
1102 memset( &url, 0, sizeof(url));
1104 url.lud_scheme = "ldap";
1105 url.lud_host = ldaphost;
1106 url.lud_port = ldapport;
1107 url.lud_scope = LDAP_SCOPE_DEFAULT;
1109 ldapuri = ldap_url_desc2str( &url );
1111 } else if ( ldapuri != NULL ) {
1112 LDAPURLDesc *ludlist, **ludp;
1116 rc = ldap_url_parselist( &ludlist, ldapuri );
1117 if ( rc != LDAP_URL_SUCCESS ) {
1119 "Could not parse LDAP URI(s)=%s (%d)\n",
1121 exit( EXIT_FAILURE );
1124 for ( ludp = &ludlist; *ludp != NULL; ) {
1125 LDAPURLDesc *lud = *ludp;
1128 if ( lud->lud_dn != NULL && lud->lud_dn[ 0 ] != '\0' &&
1129 ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) )
1131 /* if no host but a DN is provided,
1132 * use DNS SRV to gather the host list
1133 * and turn it into a list of URIs
1134 * using the scheme provided */
1135 char *domain = NULL,
1139 len_proto = strlen( lud->lud_scheme );
1141 if ( ldap_dn2domain( lud->lud_dn, &domain )
1145 "DNS SRV: Could not turn "
1146 "DN=\"%s\" into a domain\n",
1151 rc = ldap_domain2hostlist( domain, &hostlist );
1154 "DNS SRV: Could not turn "
1155 "domain=%s into a hostlist\n",
1160 hosts = ldap_str2charray( hostlist, " " );
1161 if ( hosts == NULL ) {
1163 "DNS SRV: Could not parse "
1164 "hostlist=\"%s\"\n",
1169 for ( i = 0; hosts[ i ] != NULL; i++ )
1172 tmp = (char **)ber_memrealloc( urls, sizeof( char * ) * ( nurls + i + 1 ) );
1173 if ( tmp == NULL ) {
1175 "DNS SRV: out of memory?\n" );
1179 urls[ nurls ] = NULL;
1181 for ( i = 0; hosts[ i ] != NULL; i++ ) {
1182 size_t len = len_proto
1184 + strlen( hosts[ i ] )
1187 urls[ nurls + i + 1 ] = NULL;
1188 urls[ nurls + i ] = (char *)malloc( sizeof( char ) * len );
1189 if ( urls[ nurls + i ] == NULL ) {
1191 "DNS SRV: out of memory?\n" );
1195 snprintf( urls[ nurls + i ], len, "%s://%s",
1196 lud->lud_scheme, hosts[ i ] );
1201 ber_memvfree( (void **)hosts );
1202 ber_memfree( hostlist );
1203 ber_memfree( domain );
1206 tmp = (char **)ber_memrealloc( urls, sizeof( char * ) * ( nurls + 2 ) );
1207 if ( tmp == NULL ) {
1209 "DNS SRV: out of memory?\n" );
1213 urls[ nurls + 1 ] = NULL;
1215 urls[ nurls ] = ldap_url_desc2str( lud );
1216 if ( urls[ nurls ] == NULL ) {
1218 "DNS SRV: out of memory?\n" );
1224 *ludp = lud->lud_next;
1226 lud->lud_next = NULL;
1227 ldap_free_urldesc( lud );
1230 if ( ludlist != NULL ) {
1231 ldap_free_urllist( ludlist );
1232 exit( EXIT_FAILURE );
1234 } else if ( urls == NULL ) {
1235 exit( EXIT_FAILURE );
1238 ldap_memfree( ldapuri );
1239 ldapuri = ldap_charray2str( urls, " " );
1240 ber_memvfree( (void **)urls );
1244 fprintf( stderr, "ldap_initialize( %s )\n",
1245 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
1247 rc = ldap_initialize( &ld, ldapuri );
1248 if( rc != LDAP_SUCCESS ) {
1250 "Could not create LDAP session handle for URI=%s (%d): %s\n",
1251 ldapuri, rc, ldap_err2string(rc) );
1252 exit( EXIT_FAILURE );
1255 if( private_setup ) private_setup( ld );
1258 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
1259 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
1261 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
1262 referrals ? "on" : "off" );
1263 exit( EXIT_FAILURE );
1266 #ifdef HAVE_CYRUS_SASL
1268 if( ldap_set_option( ld, LDAP_OPT_X_SASL_NOCANON,
1269 nocanon ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
1271 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_NOCANON %s\n",
1272 nocanon ? "on" : "off" );
1273 exit( EXIT_FAILURE );
1276 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
1277 != LDAP_OPT_SUCCESS )
1279 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
1281 exit( EXIT_FAILURE );
1285 rc = ldap_start_tls_s( ld, NULL, NULL );
1286 if ( rc != LDAP_SUCCESS ) {
1287 tool_perror( "ldap_start_tls", rc, NULL, NULL, NULL, NULL );
1288 if ( use_tls > 1 ) {
1289 exit( EXIT_FAILURE );
1294 if ( nettimeout.tv_sec > 0 ) {
1295 if ( ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &nettimeout )
1296 != LDAP_OPT_SUCCESS )
1298 fprintf( stderr, "Could not set LDAP_OPT_NETWORK_TIMEOUT %ld\n",
1299 (long)nettimeout.tv_sec );
1300 exit( EXIT_FAILURE );
1310 tool_bind( LDAP *ld )
1312 LDAPControl **sctrlsp = NULL;
1313 LDAPControl *sctrls[3];
1314 LDAPControl sctrl[3];
1317 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1320 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1321 c.ldctl_value.bv_val = NULL;
1322 c.ldctl_value.bv_len = 0;
1323 c.ldctl_iscritical = 0;
1325 sctrls[nsctrls] = &sctrl[nsctrls];
1326 sctrls[++nsctrls] = NULL;
1330 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1331 if ( sessionTracking ) {
1334 if (stValue.bv_val == NULL && st_value( ld, &stValue ) ) {
1335 exit( EXIT_FAILURE );
1338 c.ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1339 c.ldctl_iscritical = 0;
1340 ber_dupbv( &c.ldctl_value, &stValue );
1343 sctrls[nsctrls] = &sctrl[nsctrls];
1344 sctrls[++nsctrls] = NULL;
1346 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1352 assert( nsctrls < (int) (sizeof(sctrls)/sizeof(sctrls[0])) );
1354 if ( authmethod == LDAP_AUTH_SASL ) {
1355 #ifdef HAVE_CYRUS_SASL
1359 if( sasl_secprops != NULL ) {
1360 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
1361 (void *) sasl_secprops );
1363 if( rc != LDAP_OPT_SUCCESS ) {
1365 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
1367 exit( LDAP_LOCAL_ERROR );
1371 defaults = lutil_sasl_defaults( ld,
1378 rc = ldap_sasl_interactive_bind_s( ld, binddn, sasl_mech,
1380 NULL, sasl_flags, lutil_sasl_interact, defaults );
1382 lutil_sasl_freedefs( defaults );
1383 if( rc != LDAP_SUCCESS ) {
1384 tool_perror( "ldap_sasl_interactive_bind_s",
1385 rc, NULL, NULL, NULL, NULL );
1389 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1390 exit( LDAP_NOT_SUPPORTED );
1394 LDAPMessage *result;
1395 LDAPControl **ctrls;
1397 char *matched = NULL;
1405 rc = ldap_sasl_bind( ld, binddn, LDAP_SASL_SIMPLE, &passwd,
1406 sctrlsp, NULL, &msgid );
1407 if ( msgid == -1 ) {
1408 tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
1409 NULL, NULL, NULL, NULL );
1414 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 ) {
1415 tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
1416 exit( LDAP_LOCAL_ERROR );
1419 rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
1421 if ( rc != LDAP_SUCCESS ) {
1422 tool_perror( "ldap_bind parse result", rc, NULL, matched, info, refs );
1423 exit( LDAP_LOCAL_ERROR );
1426 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1427 if ( ctrls && ppolicy ) {
1429 int expire, grace, len = 0;
1430 LDAPPasswordPolicyError pErr = -1;
1432 ctrl = ldap_control_find( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
1435 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
1436 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1438 if ( pErr != PP_noError ){
1441 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1442 len = strlen( msgbuf );
1444 if ( expire >= 0 ) {
1445 sprintf( msgbuf+len,
1446 " (Password expires in %d seconds)",
1448 } else if ( grace >= 0 ) {
1449 sprintf( msgbuf+len,
1450 " (Password expired, %d grace logins remain)",
1458 ldap_controls_free( ctrls );
1461 if ( err != LDAP_SUCCESS
1463 || ( matched && matched[ 0 ] )
1464 || ( info && info[ 0 ] )
1467 tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1469 if( matched ) ber_memfree( matched );
1470 if( info ) ber_memfree( info );
1471 if( refs ) ber_memvfree( (void **)refs );
1473 if ( err != LDAP_SUCCESS ) exit( err );
1479 tool_unbind( LDAP *ld )
1481 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1483 if ( err != LDAP_OPT_SUCCESS ) {
1484 fprintf( stderr, "Could not unset controls\n");
1487 (void) ldap_unbind_ext( ld, NULL, NULL );
1491 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1493 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1495 int i = 0, j, crit = 0, err;
1496 LDAPControl c[16], **ctrls;
1500 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1502 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1506 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1511 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1513 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1514 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1516 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1518 || unknown_ctrls_num ) )
1523 ctrls = (LDAPControl**) malloc(sizeof(c) + (count + unknown_ctrls_num + 1)*sizeof(LDAPControl*));
1524 if ( ctrls == NULL ) {
1525 fprintf( stderr, "No memory\n" );
1526 exit( EXIT_FAILURE );
1530 if ( BER_BVISNULL( &assertionvalue ) ) {
1531 err = ldap_create_assertion_control_value( ld,
1532 assertion, &assertionvalue );
1535 "Unable to create assertion value "
1536 "\"%s\" (%d)\n", assertion, err );
1540 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1541 c[i].ldctl_value = assertionvalue;
1542 c[i].ldctl_iscritical = assertctl > 1;
1548 c[i].ldctl_value.bv_val = authzid;
1549 c[i].ldctl_value.bv_len = strlen( authzid );
1550 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1551 c[i].ldctl_iscritical = 1;
1556 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1557 /* NOTE: doesn't need an extra count because it's incompatible
1560 BerElementBuffer berbuf;
1561 BerElement *ber = (BerElement *)&berbuf;
1563 ber_init2( ber, NULL, LBER_USE_DER );
1565 if ( ber_printf( ber, "s", proxydn ) == -1 ) {
1566 exit( EXIT_FAILURE );
1569 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1570 exit( EXIT_FAILURE );
1573 c[i].ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1574 c[i].ldctl_iscritical = 1;
1578 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1581 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1582 BER_BVZERO( &c[i].ldctl_value );
1583 c[i].ldctl_iscritical = manageDIT > 1;
1588 if ( manageDSAit ) {
1589 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1590 BER_BVZERO( &c[i].ldctl_value );
1591 c[i].ldctl_iscritical = manageDSAit > 1;
1597 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1598 BER_BVZERO( &c[i].ldctl_value );
1599 c[i].ldctl_iscritical = noop > 1;
1604 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1606 c[i].ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1607 BER_BVZERO( &c[i].ldctl_value );
1608 c[i].ldctl_iscritical = 0;
1615 BerElementBuffer berbuf;
1616 BerElement *ber = (BerElement *)&berbuf;
1617 char **attrs = NULL;
1619 if( preread_attrs ) {
1620 attrs = ldap_str2charray( preread_attrs, "," );
1623 ber_init2( ber, NULL, LBER_USE_DER );
1625 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1626 fprintf( stderr, "preread attrs encode failed.\n" );
1627 exit( EXIT_FAILURE );
1630 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1632 fprintf( stderr, "preread flatten failed (%d)\n", err );
1633 exit( EXIT_FAILURE );
1636 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1637 c[i].ldctl_iscritical = preread > 1;
1641 if( attrs ) ldap_charray_free( attrs );
1645 BerElementBuffer berbuf;
1646 BerElement *ber = (BerElement *)&berbuf;
1647 char **attrs = NULL;
1649 if( postread_attrs ) {
1650 attrs = ldap_str2charray( postread_attrs, "," );
1653 ber_init2( ber, NULL, LBER_USE_DER );
1655 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1656 fprintf( stderr, "postread attrs encode failed.\n" );
1657 exit( EXIT_FAILURE );
1660 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1662 fprintf( stderr, "postread flatten failed (%d)\n", err );
1663 exit( EXIT_FAILURE );
1666 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1667 c[i].ldctl_iscritical = postread > 1;
1671 if( attrs ) ldap_charray_free( attrs );
1674 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1676 if ( chainingResolve > -1 ) {
1677 BerElementBuffer berbuf;
1678 BerElement *ber = (BerElement *)&berbuf;
1680 ber_init2( ber, NULL, LBER_USE_DER );
1682 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1685 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1686 exit( EXIT_FAILURE );
1689 if ( chainingContinuation > -1 ) {
1690 err = ber_printf( ber, "e", chainingContinuation );
1693 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1694 exit( EXIT_FAILURE );
1698 err = ber_printf( ber, /* { */ "N}" );
1701 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1702 exit( EXIT_FAILURE );
1705 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1706 exit( EXIT_FAILURE );
1710 BER_BVZERO( &c[i].ldctl_value );
1713 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1714 c[i].ldctl_iscritical = chaining > 1;
1718 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1720 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1721 if ( sessionTracking ) {
1722 if ( stValue.bv_val == NULL && st_value( ld, &stValue ) ) {
1723 exit( EXIT_FAILURE );
1726 c[i].ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1727 c[i].ldctl_iscritical = 0;
1728 ber_dupbv( &c[i].ldctl_value, &stValue );
1733 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1736 ctrls[i++] = extra_c++;
1738 for ( count = 0; count < unknown_ctrls_num; count++ ) {
1739 ctrls[i++] = &unknown_ctrls[count];
1743 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1745 if ( err != LDAP_OPT_SUCCESS ) {
1746 for ( j = 0; j < i; j++ ) {
1747 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1749 fprintf( stderr, "Could not set %scontrols\n",
1750 crit ? "critical " : "" );
1755 exit( EXIT_FAILURE );
1760 tool_check_abandon( LDAP *ld, int msgid )
1764 switch ( gotintr ) {
1766 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1767 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1768 rc, ldap_err2string( rc ) );
1772 rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
1773 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1774 rc, ldap_err2string( rc ) );
1778 /* just unbind, ignoring the request */
1786 print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
1791 tool_write_ldif( LDIF_PUT_COMMENT, "==> ",
1792 what->bv_val, what->bv_len );
1793 ber = ber_init( &ctrl->ldctl_value );
1794 if ( ber == NULL ) {
1798 } else if ( ber_scanf( ber, "{m{" /*}}*/, &bv ) == LBER_ERROR ) {
1803 tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1805 while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
1807 BerVarray vals = NULL;
1809 if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
1816 for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
1818 ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1819 bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
1822 ber_bvarray_free( vals );
1826 if ( ber != NULL ) {
1830 tool_write_ldif( LDIF_PUT_COMMENT, "<== ",
1831 what->bv_val, what->bv_len );
1837 print_preread( LDAP *ld, LDAPControl *ctrl )
1839 static struct berval what = BER_BVC( "preread" );
1841 return print_prepostread( ld, ctrl, &what );
1845 print_postread( LDAP *ld, LDAPControl *ctrl )
1847 static struct berval what = BER_BVC( "postread" );
1849 return print_prepostread( ld, ctrl, &what );
1853 print_paged_results( LDAP *ld, LDAPControl *ctrl )
1857 /* note: pr_cookie is being malloced; it's freed
1858 * the next time the control is sent, but the last
1859 * time it's not; we don't care too much, because
1860 * the last time an empty value is returned... */
1861 if ( ldap_parse_pageresponse_control( ld, ctrl, &estimate, &pr_cookie )
1868 /* FIXME: check buffer overflow */
1869 char buf[ BUFSIZ ], *ptr = buf;
1871 if ( estimate > 0 ) {
1872 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1873 "estimate=%d", estimate );
1876 if ( pr_cookie.bv_len > 0 ) {
1879 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
1880 pr_cookie.bv_len ) + 1;
1881 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
1883 bv.bv_len = lutil_b64_ntop(
1884 (unsigned char *) pr_cookie.bv_val,
1886 bv.bv_val, bv.bv_len );
1888 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1889 "%scookie=%s", ptr == buf ? "" : " ",
1892 ber_memfree( bv.bv_val );
1894 pr_morePagedResults = 1;
1897 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1898 "%scookie=", ptr == buf ? "" : " " );
1901 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1902 "pagedresults", buf, ptr - buf );
1909 print_sss( LDAP *ld, LDAPControl *ctrl )
1915 rc = ldap_parse_sortresponse_control( ld, ctrl, &err, &attr );
1916 if ( rc == LDAP_SUCCESS ) {
1918 rc = snprintf( buf, sizeof(buf), "(%d) %s %s",
1919 err, ldap_err2string(err), attr ? attr : "" );
1921 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1922 "sortResult", buf, rc );
1928 #ifdef LDAP_CONTROL_X_DEREF
1930 print_deref( LDAP *ld, LDAPControl *ctrl )
1932 LDAPDerefRes *drhead = NULL, *dr;
1935 rc = ldap_parse_derefresponse_control( ld, ctrl, &drhead );
1936 if ( rc != LDAP_SUCCESS ) {
1940 for ( dr = drhead; dr != NULL; dr = dr->next ) {
1945 len = strlen( dr->derefAttr ) + STRLENOF(": ");
1947 for ( dv = dr->attrVals; dv != NULL; dv = dv->next ) {
1948 if ( dv->vals != NULL ) {
1950 ber_len_t tlen = strlen(dv->type);
1952 for ( j = 0; dv->vals[ j ].bv_val != NULL; j++ ) {
1953 len += STRLENOF("<:=>;") + tlen + 4*((dv->vals[ j ].bv_len - 1)/3 + 1);
1957 len += dr->derefVal.bv_len + STRLENOF("\n");
1958 buf = ldap_memalloc( len + 1 );
1959 if ( buf == NULL ) {
1960 rc = LDAP_NO_MEMORY;
1965 ptr = lutil_strcopy( ptr, dr->derefAttr );
1968 for ( dv = dr->attrVals; dv != NULL; dv = dv->next ) {
1969 if ( dv->vals != NULL ) {
1971 for ( j = 0; dv->vals[ j ].bv_val != NULL; j++ ) {
1974 for ( k = 0; k < dv->vals[ j ].bv_len; k++ ) {
1975 if ( !isprint( dv->vals[ j ].bv_val[k] ) ) {
1982 ptr = lutil_strcopy( ptr, dv->type );
1988 k = lutil_b64_ntop( dv->vals[ j ].bv_val, dv->vals[ j ].bv_len, ptr, buf + len - ptr );
1993 ptr = lutil_memcopy( ptr, dv->vals[ j ].bv_val, dv->vals[ j ].bv_len );
2000 ptr = lutil_strncopy( ptr, dr->derefVal.bv_val, dr->derefVal.bv_len );
2003 assert( ptr <= buf + len );
2005 tool_write_ldif( LDIF_PUT_COMMENT, NULL, buf, ptr - buf);
2007 ldap_memfree( buf );
2013 ldap_derefresponse_free( drhead );
2019 #ifdef LDAP_CONTROL_X_WHATFAILED
2021 print_whatfailed( LDAP *ld, LDAPControl *ctrl )
2026 BerVarray bva = NULL;
2028 /* Create a BerElement from the berval returned in the control. */
2029 ber = ber_init( &ctrl->ldctl_value );
2031 if ( ber == NULL ) {
2032 return LDAP_NO_MEMORY;
2035 siz = sizeof(struct berval);
2036 tag = ber_scanf( ber, "[M]", &bva, &siz, 0 );
2037 if ( tag != LBER_ERROR ) {
2040 tool_write_ldif( LDIF_PUT_COMMENT, " what failed:", NULL, 0 );
2042 for ( i = 0; bva[i].bv_val != NULL; i++ ) {
2043 tool_write_ldif( LDIF_PUT_COMMENT, NULL, bva[i].bv_val, bva[i].bv_len );
2046 ldap_memfree( bva );
2056 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
2058 print_ppolicy( LDAP *ld, LDAPControl *ctrl )
2060 int expire = 0, grace = 0, rc;
2061 LDAPPasswordPolicyError pperr;
2063 rc = ldap_parse_passwordpolicy_control( ld, ctrl,
2064 &expire, &grace, &pperr );
2065 if ( rc == LDAP_SUCCESS ) {
2066 char buf[ BUFSIZ ], *ptr = buf;
2068 if ( expire != -1 ) {
2069 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2070 "expire=%d", expire );
2073 if ( grace != -1 ) {
2074 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2075 "%sgrace=%d", ptr == buf ? "" : " ", grace );
2078 if ( pperr != PP_noError ) {
2079 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2080 "%serror=%d (%s)", ptr == buf ? "" : " ",
2082 ldap_passwordpolicy_err2txt( pperr ) );
2085 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2086 "ppolicy", buf, ptr - buf );
2093 void tool_print_ctrls(
2095 LDAPControl **ctrls )
2100 for ( i = 0; ctrls[i] != NULL; i++ ) {
2101 /* control: OID criticality base64value */
2102 struct berval b64 = BER_BVNULL;
2107 /* FIXME: there might be cases where a control has NULL OID;
2108 * this makes little sense, especially when returned by the
2109 * server, but libldap happily allows it */
2110 if ( ctrls[i]->ldctl_oid == NULL ) {
2115 len += strlen( ctrls[i]->ldctl_oid );
2117 /* add enough for space after OID and the critical value itself */
2118 len += ctrls[i]->ldctl_iscritical
2119 ? sizeof("true") : sizeof("false");
2121 /* convert to base64 */
2122 if ( !BER_BVISNULL( &ctrls[i]->ldctl_value ) ) {
2123 b64.bv_len = LUTIL_BASE64_ENCODE_LEN(
2124 ctrls[i]->ldctl_value.bv_len ) + 1;
2125 b64.bv_val = ber_memalloc( b64.bv_len + 1 );
2127 b64.bv_len = lutil_b64_ntop(
2128 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
2129 ctrls[i]->ldctl_value.bv_len,
2130 b64.bv_val, b64.bv_len );
2134 len += 1 + b64.bv_len;
2137 ptr = str = malloc( len + 1 );
2139 ptr = lutil_strcopy( ptr, ": " );
2141 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_oid );
2142 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_iscritical
2143 ? " true" : " false" );
2146 ptr = lutil_strcopy( ptr, " " );
2147 ptr = lutil_strcopy( ptr, b64.bv_val );
2151 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2152 "control", str, len );
2157 ber_memfree( b64.bv_val );
2160 /* known controls */
2161 for ( j = 0; tool_ctrl_response[j].oid != NULL; j++ ) {
2162 if ( strcmp( tool_ctrl_response[j].oid, ctrls[i]->ldctl_oid ) == 0 ) {
2163 if ( !tool_ctrl_response[j].mask & tool_type ) {
2164 /* this control should not appear
2165 * with this tool; warning? */
2171 if ( tool_ctrl_response[j].oid != NULL && tool_ctrl_response[j].func ) {
2172 (void)tool_ctrl_response[j].func( ld, ctrls[i] );
2178 tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
2182 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
2186 fputs( ldif, stdout );
2187 ber_memfree( ldif );
2193 tool_is_oid( const char *s )
2197 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2201 for ( ; s[ 0 ]; s++ ) {
2202 if ( s[ 0 ] == '.' ) {
2203 if ( s[ 1 ] == '\0' ) {
2210 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2214 if ( first == 1 && s[ 0 ] == '0' && s[ 1 ] != '.' ) {