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 );
142 static struct tool_ctrls_t {
146 } tool_ctrl_response[] = {
147 { LDAP_CONTROL_PRE_READ, TOOL_ALL, print_preread },
148 { LDAP_CONTROL_POST_READ, TOOL_ALL, print_postread },
149 { LDAP_CONTROL_PAGEDRESULTS, TOOL_SEARCH, print_paged_results },
150 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
151 { LDAP_CONTROL_PASSWORDPOLICYRESPONSE, TOOL_ALL, print_ppolicy },
153 { LDAP_CONTROL_SORTRESPONSE, TOOL_SEARCH, print_sss },
154 #ifdef LDAP_CONTROL_X_DEREF
155 { LDAP_CONTROL_X_DEREF, TOOL_SEARCH, print_deref },
161 enum { Intr_None = 0, Intr_Abandon, Intr_Cancel, Intr_Ignore };
162 static volatile sig_atomic_t gotintr, abcan;
165 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
167 st_value( LDAP *ld, struct berval *value )
169 char *ip = NULL, *name = NULL;
170 struct berval id = { 0 };
171 char namebuf[ MAXHOSTNAMELEN ];
173 if ( gethostname( namebuf, sizeof( namebuf ) ) == 0 ) {
179 h = gethostbyname( name );
181 AC_MEMCPY( &addr, h->h_addr, sizeof( addr ) );
182 ip = inet_ntoa( addr );
186 #ifdef HAVE_CYRUS_SASL
187 if ( sasl_authz_id != NULL ) {
188 ber_str2bv( sasl_authz_id, 0, 0, &id );
190 } else if ( sasl_authc_id != NULL ) {
191 ber_str2bv( sasl_authc_id, 0, 0, &id );
194 #endif /* HAVE_CYRUS_SASL */
195 if ( binddn != NULL ) {
196 ber_str2bv( binddn, 0, 0, &id );
199 if ( ldap_create_session_tracking_value( ld,
200 ip, name, LDAP_CONTROL_X_SESSION_TRACKING_USERNAME,
203 fprintf( stderr, _("Session tracking control encoding error!\n") );
209 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
218 tool_init( tool_type_t type )
221 ldap_pvt_setlocale(LC_MESSAGES, "");
222 ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
223 ldap_pvt_textdomain(OPENLDAP_PACKAGE);
229 #ifdef HAVE_CYRUS_SASL
233 ldap_pvt_tls_destroy();
236 if ( ldapuri != NULL ) {
237 ber_memfree( ldapuri );
241 if ( pr_cookie.bv_val != NULL ) {
242 ber_memfree( pr_cookie.bv_val );
243 pr_cookie.bv_val = NULL;
244 pr_cookie.bv_len = 0;
249 tool_common_usage( void )
251 static const char *const descriptions[] = {
252 N_(" -c continuous operation mode (do not stop on errors)\n"),
253 N_(" -d level set LDAP debugging level to `level'\n"),
254 N_(" -D binddn bind DN\n"),
255 N_(" -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
256 N_(" [!]assert=<filter> (RFC 4528; a RFC 4515 Filter string)\n")
257 N_(" [!]authzid=<authzid> (RFC 4370; \"dn:<dn>\" or \"u:<user>\")\n")
258 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
260 /* non-advertized support for proxyDN */
261 N_(" [!]proxydn=<dn> (a RFC 4514 DN string)\n")
264 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
265 N_(" [!]chaining[=<resolveBehavior>[/<continuationBehavior>]]\n")
266 N_(" one of \"chainingPreferred\", \"chainingRequired\",\n")
267 N_(" \"referralsPreferred\", \"referralsRequired\"\n")
268 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
269 N_(" [!]manageDSAit (RFC 3296)\n")
271 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
274 N_(" [!]postread[=<attrs>] (RFC 4527; comma-separated attr list)\n")
275 N_(" [!]preread[=<attrs>] (RFC 4527; comma-separated attr list)\n")
277 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
278 N_(" [!]sessiontracking\n")
279 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
280 N_(" abandon, cancel, ignore (SIGINT sends abandon/cancel,\n"
281 " or ignores response; if critical, doesn't wait for SIGINT.\n"
282 " not really controls)\n")
283 N_(" -f file read operations from `file'\n"),
284 N_(" -h host LDAP server\n"),
285 N_(" -H URI LDAP Uniform Resource Identifier(s)\n"),
286 N_(" -I use SASL Interactive mode\n"),
287 N_(" -M enable Manage DSA IT control (-MM to make critical)\n"),
288 N_(" -n show what would be done but don't actually do it\n"),
289 N_(" -O props SASL security properties\n"),
290 N_(" -o <opt>[=<optparam] general options\n"),
291 N_(" nettimeout=<timeout> (in seconds, or \"none\" or \"max\")\n"),
292 N_(" -p port port on LDAP server\n"),
293 N_(" -P version protocol version (default: 3)\n"),
294 N_(" -Q use SASL Quiet mode\n"),
295 N_(" -R realm SASL realm\n"),
296 N_(" -U authcid SASL authentication identity\n"),
297 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
298 N_(" -V print version info (-VV only)\n"),
299 N_(" -w passwd bind password (for simple authentication)\n"),
300 N_(" -W prompt for bind password\n"),
301 N_(" -x Simple authentication\n"),
302 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
303 N_(" -y file Read password from file\n"),
304 N_(" -Y mech SASL mechanism\n"),
305 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
308 const char *const *cpp;
310 fputs( _("Common options:\n"), stderr );
311 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
312 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
313 fputs( _(*cpp), stderr );
326 fprintf( stderr, "%s: %s (%d)%s\n",
327 func, ldap_err2string( err ), err, extra ? extra : "" );
329 if ( matched && *matched ) {
330 fprintf( stderr, _("\tmatched DN: %s\n"), matched );
333 if ( info && *info ) {
334 fprintf( stderr, _("\tadditional info: %s\n"), info );
337 if ( refs && *refs ) {
339 fprintf( stderr, _("\treferrals:\n") );
340 for( i=0; refs[i]; i++ ) {
341 fprintf( stderr, "\t\t%s\n", refs[i] );
348 tool_args( int argc, char **argv )
352 while (( i = getopt( argc, argv, options )) != EOF ) {
354 char *control, *cvalue, *next;
356 case 'c': /* continuous operation mode */
363 ival = strtol( optarg, &next, 10 );
364 if (next == NULL || next[0] != '\0') {
365 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
370 case 'D': /* bind DN */
371 if( binddn != NULL ) {
372 fprintf( stderr, "%s: -D previously specified\n", prog );
373 exit( EXIT_FAILURE );
375 binddn = ber_strdup( optarg );
377 case 'e': /* general extensions (controls and such) */
378 /* should be extended to support comma separated list of
379 * [!]key[=value] parameters, e.g. -e !foo,bar=567
384 if( optarg[0] == '!' ) {
389 control = ber_strdup( optarg );
390 if ( (cvalue = strchr( control, '=' )) != NULL ) {
394 if ( strcasecmp( control, "assert" ) == 0 ) {
396 fprintf( stderr, "assert control previously specified\n");
397 exit( EXIT_FAILURE );
399 if( cvalue == NULL ) {
400 fprintf( stderr, "assert: control value expected\n" );
404 assertctl = 1 + crit;
406 assert( assertion == NULL );
409 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
410 if( authzid != NULL ) {
411 fprintf( stderr, "authzid control previously specified\n");
412 exit( EXIT_FAILURE );
414 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
415 if( proxydn != NULL ) {
416 fprintf( stderr, "authzid control incompatible with proxydn\n");
417 exit( EXIT_FAILURE );
419 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
420 if( cvalue == NULL ) {
421 fprintf( stderr, "authzid: control value expected\n" );
425 fprintf( stderr, "authzid: must be marked critical\n" );
429 assert( authzid == NULL );
432 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
433 } else if ( strcasecmp( control, "proxydn" ) == 0 ) {
434 if( proxydn != NULL ) {
435 fprintf( stderr, "proxydn control previously specified\n");
436 exit( EXIT_FAILURE );
438 if( authzid != NULL ) {
439 fprintf( stderr, "proxydn control incompatible with authzid\n");
440 exit( EXIT_FAILURE );
442 if( cvalue == NULL ) {
443 fprintf( stderr, "proxydn: control value expected\n" );
447 fprintf( stderr, "proxydn: must be marked critical\n" );
451 assert( proxydn == NULL );
453 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
455 } else if ( ( strcasecmp( control, "relax" ) == 0 ) ||
456 ( strcasecmp( control, "manageDIT" ) == 0 ) )
460 "relax control previously specified\n");
461 exit( EXIT_FAILURE );
463 if( cvalue != NULL ) {
465 "relax: no control value expected\n" );
469 manageDIT = 1 + crit;
471 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
474 "manageDSAit control previously specified\n");
475 exit( EXIT_FAILURE );
477 if( cvalue != NULL ) {
479 "manageDSAit: no control value expected\n" );
483 manageDSAit = 1 + crit;
485 } else if ( strcasecmp( control, "noop" ) == 0 ) {
487 fprintf( stderr, "noop control previously specified\n");
488 exit( EXIT_FAILURE );
490 if( cvalue != NULL ) {
491 fprintf( stderr, "noop: no control value expected\n" );
497 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
498 } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
500 fprintf( stderr, "ppolicy control previously specified\n");
501 exit( EXIT_FAILURE );
503 if( cvalue != NULL ) {
504 fprintf( stderr, "ppolicy: no control value expected\n" );
508 fprintf( stderr, "ppolicy: critical flag not allowed\n" );
515 } else if ( strcasecmp( control, "preread" ) == 0 ) {
517 fprintf( stderr, "preread control previously specified\n");
518 exit( EXIT_FAILURE );
522 preread_attrs = cvalue;
524 } else if ( strcasecmp( control, "postread" ) == 0 ) {
526 fprintf( stderr, "postread control previously specified\n");
527 exit( EXIT_FAILURE );
531 postread_attrs = cvalue;
533 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
534 } else if ( strcasecmp( control, "chaining" ) == 0 ) {
537 if ( cvalue != NULL ) {
540 continuation = strchr( cvalue, '/' );
541 if ( continuation ) {
542 /* FIXME: this makes sense only in searches */
543 *continuation++ = '\0';
544 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
545 chainingContinuation = LDAP_CHAINING_PREFERRED;
546 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
547 chainingContinuation = LDAP_CHAINING_REQUIRED;
548 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
549 chainingContinuation = LDAP_REFERRALS_PREFERRED;
550 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
551 chainingContinuation = LDAP_REFERRALS_REQUIRED;
554 "chaining behavior control "
555 "continuation value \"%s\" invalid\n",
557 exit( EXIT_FAILURE );
561 if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
562 chainingResolve = LDAP_CHAINING_PREFERRED;
563 } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
564 chainingResolve = LDAP_CHAINING_REQUIRED;
565 } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
566 chainingResolve = LDAP_REFERRALS_PREFERRED;
567 } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
568 chainingResolve = LDAP_REFERRALS_REQUIRED;
571 "chaining behavior control "
572 "resolve value \"%s\" invalid\n",
574 exit( EXIT_FAILURE );
577 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
579 /* this shouldn't go here, really; but it's a feature... */
580 } else if ( strcasecmp( control, "abandon" ) == 0 ) {
581 abcan = Intr_Abandon;
586 } else if ( strcasecmp( control, "cancel" ) == 0 ) {
592 } else if ( strcasecmp( control, "ignore" ) == 0 ) {
598 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
599 } else if ( strcasecmp( control, "sessiontracking" ) == 0 ) {
600 if ( sessionTracking ) {
601 fprintf( stderr, "%s: session tracking can be only specified once\n", prog );
602 exit( EXIT_FAILURE );
605 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
607 } else if ( tool_is_oid( control ) ) {
608 LDAPControl *tmpctrls, ctrl;
610 tmpctrls = (LDAPControl *)realloc( unknown_ctrls,
611 (unknown_ctrls_num + 1)*sizeof( LDAPControl ) );
612 if ( tmpctrls == NULL ) {
613 fprintf( stderr, "%s: no memory?\n", prog );
614 exit( EXIT_FAILURE );
616 unknown_ctrls = tmpctrls;
617 ctrl.ldctl_oid = control;
618 ctrl.ldctl_value.bv_val = NULL;
619 ctrl.ldctl_value.bv_len = 0;
620 ctrl.ldctl_iscritical = crit;
622 if ( cvalue != NULL ) {
624 size_t len = strlen( cvalue );
627 bv.bv_len = LUTIL_BASE64_DECODE_LEN( len );
628 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
630 retcode = lutil_b64_pton( cvalue,
631 (unsigned char *)bv.bv_val,
634 if ( retcode == -1 || (unsigned) retcode > bv.bv_len ) {
635 fprintf( stderr, "Unable to parse value of general control %s\n",
641 ctrl.ldctl_value = bv;
644 unknown_ctrls[ unknown_ctrls_num ] = ctrl;
648 fprintf( stderr, "Invalid general control name: %s\n",
653 case 'f': /* read from file */
654 if( infile != NULL ) {
655 fprintf( stderr, "%s: -f previously specified\n", prog );
656 exit( EXIT_FAILURE );
658 infile = ber_strdup( optarg );
660 case 'h': /* ldap host */
661 if( ldaphost != NULL ) {
662 fprintf( stderr, "%s: -h previously specified\n", prog );
663 exit( EXIT_FAILURE );
665 ldaphost = ber_strdup( optarg );
667 case 'H': /* ldap URI */
668 if( ldapuri != NULL ) {
669 fprintf( stderr, "%s: -H previously specified\n", prog );
670 exit( EXIT_FAILURE );
672 ldapuri = ber_strdup( optarg );
675 #ifdef HAVE_CYRUS_SASL
676 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
677 fprintf( stderr, "%s: incompatible previous "
678 "authentication choice\n",
680 exit( EXIT_FAILURE );
682 authmethod = LDAP_AUTH_SASL;
683 sasl_flags = LDAP_SASL_INTERACTIVE;
686 fprintf( stderr, "%s: was not compiled with SASL support\n",
688 exit( EXIT_FAILURE );
691 /* enable Manage DSA IT */
694 case 'n': /* print operations, don't actually do them */
698 control = ber_strdup( optarg );
699 if ( (cvalue = strchr( control, '=' )) != NULL ) {
703 if ( strcasecmp( control, "nettimeout" ) == 0 ) {
704 if( nettimeout.tv_sec != -1 ) {
705 fprintf( stderr, "nettimeout option previously specified\n");
706 exit( EXIT_FAILURE );
708 if( cvalue == NULL || cvalue[0] == '\0' ) {
709 fprintf( stderr, "nettimeout: option value expected\n" );
712 if ( strcasecmp( cvalue, "none" ) == 0 ) {
713 nettimeout.tv_sec = 0;
714 } else if ( strcasecmp( cvalue, "max" ) == 0 ) {
715 nettimeout.tv_sec = LDAP_MAXINT;
717 ival = strtol( cvalue, &next, 10 );
718 if ( next == NULL || next[0] != '\0' ) {
720 _("Unable to parse network timeout \"%s\"\n"), cvalue );
721 exit( EXIT_FAILURE );
723 nettimeout.tv_sec = ival;
725 if( nettimeout.tv_sec < 0 || nettimeout.tv_sec > LDAP_MAXINT ) {
726 fprintf( stderr, _("%s: invalid network timeout (%ld) specified\n"),
727 prog, (long)nettimeout.tv_sec );
728 exit( EXIT_FAILURE );
731 fprintf( stderr, "Invalid general option name: %s\n",
737 #ifdef HAVE_CYRUS_SASL
738 if( sasl_secprops != NULL ) {
739 fprintf( stderr, "%s: -O previously specified\n", prog );
740 exit( EXIT_FAILURE );
742 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
743 fprintf( stderr, "%s: incompatible previous "
744 "authentication choice\n", prog );
745 exit( EXIT_FAILURE );
747 authmethod = LDAP_AUTH_SASL;
748 sasl_secprops = ber_strdup( optarg );
750 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
751 exit( EXIT_FAILURE );
756 fprintf( stderr, "%s: -p previously specified\n", prog );
757 exit( EXIT_FAILURE );
759 ival = strtol( optarg, &next, 10 );
760 if ( next == NULL || next[0] != '\0' ) {
761 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
762 exit( EXIT_FAILURE );
767 ival = strtol( optarg, &next, 10 );
768 if ( next == NULL || next[0] != '\0' ) {
769 fprintf( stderr, "%s: unable to parse protocol version \"%s\"\n", prog, optarg );
770 exit( EXIT_FAILURE );
774 if( protocol == LDAP_VERSION3 ) {
775 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
777 exit( EXIT_FAILURE );
779 protocol = LDAP_VERSION2;
782 if( protocol == LDAP_VERSION2 ) {
783 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
785 exit( EXIT_FAILURE );
787 protocol = LDAP_VERSION3;
790 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
796 #ifdef HAVE_CYRUS_SASL
797 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
798 fprintf( stderr, "%s: incompatible previous "
799 "authentication choice\n",
801 exit( EXIT_FAILURE );
803 authmethod = LDAP_AUTH_SASL;
804 sasl_flags = LDAP_SASL_QUIET;
807 fprintf( stderr, "%s: not compiled with SASL support\n",
809 exit( EXIT_FAILURE );
812 #ifdef HAVE_CYRUS_SASL
813 if( sasl_realm != NULL ) {
814 fprintf( stderr, "%s: -R previously specified\n", prog );
815 exit( EXIT_FAILURE );
817 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
818 fprintf( stderr, "%s: incompatible previous "
819 "authentication choice\n",
821 exit( EXIT_FAILURE );
823 authmethod = LDAP_AUTH_SASL;
824 sasl_realm = ber_strdup( optarg );
826 fprintf( stderr, "%s: not compiled with SASL support\n",
828 exit( EXIT_FAILURE );
832 #ifdef HAVE_CYRUS_SASL
833 if( sasl_authc_id != NULL ) {
834 fprintf( stderr, "%s: -U previously specified\n", prog );
835 exit( EXIT_FAILURE );
837 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
838 fprintf( stderr, "%s: incompatible previous "
839 "authentication choice\n",
841 exit( EXIT_FAILURE );
843 authmethod = LDAP_AUTH_SASL;
844 sasl_authc_id = ber_strdup( optarg );
846 fprintf( stderr, "%s: not compiled with SASL support\n",
848 exit( EXIT_FAILURE );
851 case 'v': /* verbose mode */
854 case 'V': /* version */
857 case 'w': /* password */
858 passwd.bv_val = ber_strdup( optarg );
862 for( p = optarg; *p != '\0'; p++ ) {
866 passwd.bv_len = strlen( passwd.bv_val );
875 #ifdef HAVE_CYRUS_SASL
876 if( sasl_mech != NULL ) {
877 fprintf( stderr, "%s: -Y previously specified\n", prog );
878 exit( EXIT_FAILURE );
880 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
882 "%s: incompatible with authentication choice\n", prog );
883 exit( EXIT_FAILURE );
885 authmethod = LDAP_AUTH_SASL;
886 sasl_mech = ber_strdup( optarg );
888 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
889 exit( EXIT_FAILURE );
893 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
894 fprintf( stderr, "%s: incompatible with previous "
895 "authentication choice\n", prog );
896 exit( EXIT_FAILURE );
898 authmethod = LDAP_AUTH_SIMPLE;
901 #ifdef HAVE_CYRUS_SASL
902 if( sasl_authz_id != NULL ) {
903 fprintf( stderr, "%s: -X previously specified\n", prog );
904 exit( EXIT_FAILURE );
906 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
907 fprintf( stderr, "%s: -X incompatible with "
908 "authentication choice\n", prog );
909 exit( EXIT_FAILURE );
911 authmethod = LDAP_AUTH_SASL;
912 sasl_authz_id = ber_strdup( optarg );
914 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
915 exit( EXIT_FAILURE );
922 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
923 exit( EXIT_FAILURE );
927 if( handle_private_option( i ) ) break;
928 fprintf( stderr, "%s: unrecognized option -%c\n",
935 /* prevent bad linking */
937 api.ldapai_info_version = LDAP_API_INFO_VERSION;
939 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
940 != LDAP_OPT_SUCCESS )
942 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
943 exit( EXIT_FAILURE );
946 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
947 fprintf( stderr, "LDAP APIInfo version mismatch: "
948 "library %d, header %d\n",
949 api.ldapai_info_version, LDAP_API_INFO_VERSION );
950 exit( EXIT_FAILURE );
953 if( api.ldapai_api_version != LDAP_API_VERSION ) {
954 fprintf( stderr, "LDAP API version mismatch: "
955 "library %d, header %d\n",
956 api.ldapai_api_version, LDAP_API_VERSION );
957 exit( EXIT_FAILURE );
960 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
961 fprintf( stderr, "LDAP vendor name mismatch: "
962 "library %s, header %s\n",
963 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
964 exit( EXIT_FAILURE );
967 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
968 fprintf( stderr, "LDAP vendor version mismatch: "
969 "library %d, header %d\n",
970 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
971 exit( EXIT_FAILURE );
975 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
977 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
978 if (version > 1) exit( EXIT_SUCCESS );
981 ldap_memfree( api.ldapai_vendor_name );
982 ber_memvfree( (void **)api.ldapai_extensions );
986 protocol = LDAP_VERSION3;
988 if (authmethod == -1 && protocol > LDAP_VERSION2) {
989 #ifdef HAVE_CYRUS_SASL
990 if ( binddn != NULL ) {
991 authmethod = LDAP_AUTH_SIMPLE;
993 authmethod = LDAP_AUTH_SASL;
996 authmethod = LDAP_AUTH_SIMPLE;
1000 if( ldapuri == NULL ) {
1001 if( ldapport && ( ldaphost == NULL )) {
1002 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
1003 exit( EXIT_FAILURE );
1006 if( ldaphost != NULL ) {
1007 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
1008 exit( EXIT_FAILURE );
1011 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
1012 exit( EXIT_FAILURE );
1016 if( protocol == LDAP_VERSION2 ) {
1017 if( assertctl || authzid || manageDIT || manageDSAit ||
1018 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1020 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1021 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1024 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1026 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1027 noop || ppolicy || preread || postread )
1029 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
1030 exit( EXIT_FAILURE );
1034 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
1035 exit( EXIT_FAILURE );
1038 #ifdef HAVE_CYRUS_SASL
1039 if( authmethod == LDAP_AUTH_SASL ) {
1040 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
1042 exit( EXIT_FAILURE );
1050 tool_conn_setup( int dont, void (*private_setup)( LDAP * ) )
1055 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
1056 != LBER_OPT_SUCCESS )
1059 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
1061 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
1062 != LDAP_OPT_SUCCESS )
1065 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
1070 (void) SIGNAL( SIGPIPE, SIG_IGN );
1074 SIGNAL( SIGINT, do_sig );
1080 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
1083 memset( &url, 0, sizeof(url));
1085 url.lud_scheme = "ldap";
1086 url.lud_host = ldaphost;
1087 url.lud_port = ldapport;
1088 url.lud_scope = LDAP_SCOPE_DEFAULT;
1090 ldapuri = ldap_url_desc2str( &url );
1092 } else if ( ldapuri != NULL ) {
1093 LDAPURLDesc *ludlist, **ludp;
1097 rc = ldap_url_parselist( &ludlist, ldapuri );
1098 if ( rc != LDAP_URL_SUCCESS ) {
1100 "Could not parse LDAP URI(s)=%s (%d)\n",
1102 exit( EXIT_FAILURE );
1105 for ( ludp = &ludlist; *ludp != NULL; ) {
1106 LDAPURLDesc *lud = *ludp;
1109 if ( lud->lud_dn != NULL && lud->lud_dn[ 0 ] != '\0' &&
1110 ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) )
1112 /* if no host but a DN is provided,
1113 * use DNS SRV to gather the host list
1114 * and turn it into a list of URIs
1115 * using the scheme provided */
1116 char *domain = NULL,
1120 len_proto = strlen( lud->lud_scheme );
1122 if ( ldap_dn2domain( lud->lud_dn, &domain )
1126 "DNS SRV: Could not turn "
1127 "DN=\"%s\" into a domain\n",
1132 rc = ldap_domain2hostlist( domain, &hostlist );
1135 "DNS SRV: Could not turn "
1136 "domain=%s into a hostlist\n",
1141 hosts = ldap_str2charray( hostlist, " " );
1142 if ( hosts == NULL ) {
1144 "DNS SRV: Could not parse "
1145 "hostlist=\"%s\"\n",
1150 for ( i = 0; hosts[ i ] != NULL; i++ )
1153 tmp = (char **)realloc( urls, sizeof( char * ) * ( nurls + i + 1 ) );
1154 if ( tmp == NULL ) {
1156 "DNS SRV: out of memory?\n" );
1160 urls[ nurls ] = NULL;
1162 for ( i = 0; hosts[ i ] != NULL; i++ ) {
1163 size_t len = len_proto
1165 + strlen( hosts[ i ] )
1168 urls[ nurls + i + 1 ] = NULL;
1169 urls[ nurls + i ] = (char *)malloc( sizeof( char ) * len );
1170 if ( urls[ nurls + i ] == NULL ) {
1172 "DNS SRV: out of memory?\n" );
1176 snprintf( urls[ nurls + i ], len, "%s://%s",
1177 lud->lud_scheme, hosts[ i ] );
1182 ber_memvfree( (void **)hosts );
1183 ber_memfree( hostlist );
1184 ber_memfree( domain );
1187 tmp = (char **)realloc( urls, sizeof( char * ) * ( nurls + 2 ) );
1188 if ( tmp == NULL ) {
1190 "DNS SRV: out of memory?\n" );
1194 urls[ nurls + 1 ] = NULL;
1196 urls[ nurls ] = ldap_url_desc2str( lud );
1197 if ( urls[ nurls ] == NULL ) {
1199 "DNS SRV: out of memory?\n" );
1205 *ludp = lud->lud_next;
1207 lud->lud_next = NULL;
1208 ldap_free_urldesc( lud );
1211 if ( ludlist != NULL ) {
1212 ldap_free_urllist( ludlist );
1213 exit( EXIT_FAILURE );
1215 } else if ( urls == NULL ) {
1216 exit( EXIT_FAILURE );
1219 ldap_memfree( ldapuri );
1220 ldapuri = ldap_charray2str( urls, " " );
1221 ber_memvfree( (void **)urls );
1225 fprintf( stderr, "ldap_initialize( %s )\n",
1226 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
1228 rc = ldap_initialize( &ld, ldapuri );
1229 if( rc != LDAP_SUCCESS ) {
1231 "Could not create LDAP session handle for URI=%s (%d): %s\n",
1232 ldapuri, rc, ldap_err2string(rc) );
1233 exit( EXIT_FAILURE );
1236 if( private_setup ) private_setup( ld );
1239 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
1240 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
1242 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
1243 referrals ? "on" : "off" );
1244 exit( EXIT_FAILURE );
1247 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
1248 != LDAP_OPT_SUCCESS )
1250 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
1252 exit( EXIT_FAILURE );
1256 rc = ldap_start_tls_s( ld, NULL, NULL );
1257 if ( rc != LDAP_SUCCESS ) {
1258 tool_perror( "ldap_start_tls", rc, NULL, NULL, NULL, NULL );
1259 if ( use_tls > 1 ) {
1260 exit( EXIT_FAILURE );
1265 if ( nettimeout.tv_sec > 0 ) {
1266 if ( ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &nettimeout )
1267 != LDAP_OPT_SUCCESS )
1269 fprintf( stderr, "Could not set LDAP_OPT_NETWORK_TIMEOUT %ld\n",
1270 (long)nettimeout.tv_sec );
1271 exit( EXIT_FAILURE );
1281 tool_bind( LDAP *ld )
1283 LDAPControl **sctrlsp = NULL;
1284 LDAPControl *sctrls[3];
1285 LDAPControl sctrl[3];
1288 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1291 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1292 c.ldctl_value.bv_val = NULL;
1293 c.ldctl_value.bv_len = 0;
1294 c.ldctl_iscritical = 0;
1296 sctrls[nsctrls] = &sctrl[nsctrls];
1297 sctrls[++nsctrls] = NULL;
1301 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1302 if ( sessionTracking ) {
1305 if (stValue.bv_val == NULL && st_value( ld, &stValue ) ) {
1306 exit( EXIT_FAILURE );
1309 c.ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1310 c.ldctl_iscritical = 0;
1311 ber_dupbv( &c.ldctl_value, &stValue );
1314 sctrls[nsctrls] = &sctrl[nsctrls];
1315 sctrls[++nsctrls] = NULL;
1317 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1323 assert( nsctrls < (int) (sizeof(sctrls)/sizeof(sctrls[0])) );
1325 if ( authmethod == LDAP_AUTH_SASL ) {
1326 #ifdef HAVE_CYRUS_SASL
1330 if( sasl_secprops != NULL ) {
1331 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
1332 (void *) sasl_secprops );
1334 if( rc != LDAP_OPT_SUCCESS ) {
1336 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
1338 exit( LDAP_LOCAL_ERROR );
1342 defaults = lutil_sasl_defaults( ld,
1349 rc = ldap_sasl_interactive_bind_s( ld, binddn, sasl_mech,
1351 NULL, sasl_flags, lutil_sasl_interact, defaults );
1353 lutil_sasl_freedefs( defaults );
1354 if( rc != LDAP_SUCCESS ) {
1355 tool_perror( "ldap_sasl_interactive_bind_s",
1356 rc, NULL, NULL, NULL, NULL );
1360 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1361 exit( LDAP_NOT_SUPPORTED );
1365 LDAPMessage *result;
1366 LDAPControl **ctrls;
1368 char *matched = NULL;
1376 rc = ldap_sasl_bind( ld, binddn, LDAP_SASL_SIMPLE, &passwd,
1377 sctrlsp, NULL, &msgid );
1378 if ( msgid == -1 ) {
1379 tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
1380 NULL, NULL, NULL, NULL );
1385 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 ) {
1386 tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
1387 exit( LDAP_LOCAL_ERROR );
1390 rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
1392 if ( rc != LDAP_SUCCESS ) {
1393 tool_perror( "ldap_bind parse result", rc, NULL, matched, info, refs );
1394 exit( LDAP_LOCAL_ERROR );
1397 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1398 if ( ctrls && ppolicy ) {
1400 int expire, grace, len = 0;
1401 LDAPPasswordPolicyError pErr = -1;
1403 ctrl = ldap_control_find( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
1406 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
1407 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1409 if ( pErr != PP_noError ){
1412 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1413 len = strlen( msgbuf );
1415 if ( expire >= 0 ) {
1416 sprintf( msgbuf+len,
1417 " (Password expires in %d seconds)",
1419 } else if ( grace >= 0 ) {
1420 sprintf( msgbuf+len,
1421 " (Password expired, %d grace logins remain)",
1429 ldap_controls_free( ctrls );
1432 if ( err != LDAP_SUCCESS
1434 || ( matched && matched[ 0 ] )
1435 || ( info && info[ 0 ] )
1438 tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1440 if( matched ) ber_memfree( matched );
1441 if( info ) ber_memfree( info );
1442 if( refs ) ber_memvfree( (void **)refs );
1444 if ( err != LDAP_SUCCESS ) exit( err );
1450 tool_unbind( LDAP *ld )
1452 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1454 if ( err != LDAP_OPT_SUCCESS ) {
1455 fprintf( stderr, "Could not unset controls\n");
1458 (void) ldap_unbind_ext( ld, NULL, NULL );
1462 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1464 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1466 int i = 0, j, crit = 0, err;
1467 LDAPControl c[16], **ctrls;
1471 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1473 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1477 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1482 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1484 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1485 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1487 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1489 || unknown_ctrls_num ) )
1494 ctrls = (LDAPControl**) malloc(sizeof(c) + (count + unknown_ctrls_num + 1)*sizeof(LDAPControl*));
1495 if ( ctrls == NULL ) {
1496 fprintf( stderr, "No memory\n" );
1497 exit( EXIT_FAILURE );
1501 if ( BER_BVISNULL( &assertionvalue ) ) {
1502 err = ldap_create_assertion_control_value( ld,
1503 assertion, &assertionvalue );
1506 "Unable to create assertion value "
1507 "\"%s\" (%d)\n", assertion, err );
1511 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1512 c[i].ldctl_value = assertionvalue;
1513 c[i].ldctl_iscritical = assertctl > 1;
1519 c[i].ldctl_value.bv_val = authzid;
1520 c[i].ldctl_value.bv_len = strlen( authzid );
1521 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1522 c[i].ldctl_iscritical = 1;
1527 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1528 /* NOTE: doesn't need an extra count because it's incompatible
1531 BerElementBuffer berbuf;
1532 BerElement *ber = (BerElement *)&berbuf;
1534 ber_init2( ber, NULL, LBER_USE_DER );
1536 if ( ber_printf( ber, "s", proxydn ) == -1 ) {
1537 exit( EXIT_FAILURE );
1540 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1541 exit( EXIT_FAILURE );
1544 c[i].ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1545 c[i].ldctl_iscritical = 1;
1549 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1552 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1553 BER_BVZERO( &c[i].ldctl_value );
1554 c[i].ldctl_iscritical = manageDIT > 1;
1559 if ( manageDSAit ) {
1560 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1561 BER_BVZERO( &c[i].ldctl_value );
1562 c[i].ldctl_iscritical = manageDSAit > 1;
1568 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1569 BER_BVZERO( &c[i].ldctl_value );
1570 c[i].ldctl_iscritical = noop > 1;
1575 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1577 c[i].ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1578 BER_BVZERO( &c[i].ldctl_value );
1579 c[i].ldctl_iscritical = 0;
1586 BerElementBuffer berbuf;
1587 BerElement *ber = (BerElement *)&berbuf;
1588 char **attrs = NULL;
1590 if( preread_attrs ) {
1591 attrs = ldap_str2charray( preread_attrs, "," );
1594 ber_init2( ber, NULL, LBER_USE_DER );
1596 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1597 fprintf( stderr, "preread attrs encode failed.\n" );
1598 exit( EXIT_FAILURE );
1601 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1603 fprintf( stderr, "preread flatten failed (%d)\n", err );
1604 exit( EXIT_FAILURE );
1607 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1608 c[i].ldctl_iscritical = preread > 1;
1612 if( attrs ) ldap_charray_free( attrs );
1616 BerElementBuffer berbuf;
1617 BerElement *ber = (BerElement *)&berbuf;
1618 char **attrs = NULL;
1620 if( postread_attrs ) {
1621 attrs = ldap_str2charray( postread_attrs, "," );
1624 ber_init2( ber, NULL, LBER_USE_DER );
1626 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1627 fprintf( stderr, "postread attrs encode failed.\n" );
1628 exit( EXIT_FAILURE );
1631 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1633 fprintf( stderr, "postread flatten failed (%d)\n", err );
1634 exit( EXIT_FAILURE );
1637 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1638 c[i].ldctl_iscritical = postread > 1;
1642 if( attrs ) ldap_charray_free( attrs );
1645 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1647 if ( chainingResolve > -1 ) {
1648 BerElementBuffer berbuf;
1649 BerElement *ber = (BerElement *)&berbuf;
1651 ber_init2( ber, NULL, LBER_USE_DER );
1653 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1656 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1657 exit( EXIT_FAILURE );
1660 if ( chainingContinuation > -1 ) {
1661 err = ber_printf( ber, "e", chainingContinuation );
1664 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1665 exit( EXIT_FAILURE );
1669 err = ber_printf( ber, /* { */ "N}" );
1672 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1673 exit( EXIT_FAILURE );
1676 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1677 exit( EXIT_FAILURE );
1681 BER_BVZERO( &c[i].ldctl_value );
1684 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1685 c[i].ldctl_iscritical = chaining > 1;
1689 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1691 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1692 if ( sessionTracking ) {
1693 if ( stValue.bv_val == NULL && st_value( ld, &stValue ) ) {
1694 exit( EXIT_FAILURE );
1697 c[i].ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1698 c[i].ldctl_iscritical = 0;
1699 ber_dupbv( &c[i].ldctl_value, &stValue );
1704 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1707 ctrls[i++] = extra_c++;
1709 for ( count = 0; count < unknown_ctrls_num; count++ ) {
1710 ctrls[i++] = &unknown_ctrls[count];
1714 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1716 if ( err != LDAP_OPT_SUCCESS ) {
1717 for ( j = 0; j < i; j++ ) {
1718 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1720 fprintf( stderr, "Could not set %scontrols\n",
1721 crit ? "critical " : "" );
1726 exit( EXIT_FAILURE );
1731 tool_check_abandon( LDAP *ld, int msgid )
1735 switch ( gotintr ) {
1737 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1738 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1739 rc, ldap_err2string( rc ) );
1743 rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
1744 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1745 rc, ldap_err2string( rc ) );
1749 /* just unbind, ignoring the request */
1757 print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
1762 tool_write_ldif( LDIF_PUT_COMMENT, "==> ",
1763 what->bv_val, what->bv_len );
1764 ber = ber_init( &ctrl->ldctl_value );
1765 if ( ber == NULL ) {
1769 } else if ( ber_scanf( ber, "{m{" /*}}*/, &bv ) == LBER_ERROR ) {
1774 tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1776 while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
1778 BerVarray vals = NULL;
1780 if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
1787 for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
1789 ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1790 bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
1793 ber_bvarray_free( vals );
1797 if ( ber != NULL ) {
1801 tool_write_ldif( LDIF_PUT_COMMENT, "<== ",
1802 what->bv_val, what->bv_len );
1808 print_preread( LDAP *ld, LDAPControl *ctrl )
1810 static struct berval what = BER_BVC( "preread" );
1812 return print_prepostread( ld, ctrl, &what );
1816 print_postread( LDAP *ld, LDAPControl *ctrl )
1818 static struct berval what = BER_BVC( "postread" );
1820 return print_prepostread( ld, ctrl, &what );
1824 print_paged_results( LDAP *ld, LDAPControl *ctrl )
1828 /* note: pr_cookie is being malloced; it's freed
1829 * the next time the control is sent, but the last
1830 * time it's not; we don't care too much, because
1831 * the last time an empty value is returned... */
1832 if ( ldap_parse_pageresponse_control( ld, ctrl, &estimate, &pr_cookie )
1839 /* FIXME: check buffer overflow */
1840 char buf[ BUFSIZ ], *ptr = buf;
1842 if ( estimate > 0 ) {
1843 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1844 "estimate=%d", estimate );
1847 if ( pr_cookie.bv_len > 0 ) {
1850 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
1851 pr_cookie.bv_len ) + 1;
1852 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
1854 bv.bv_len = lutil_b64_ntop(
1855 (unsigned char *) pr_cookie.bv_val,
1857 bv.bv_val, bv.bv_len );
1859 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1860 "%scookie=%s", ptr == buf ? "" : " ",
1863 ber_memfree( bv.bv_val );
1865 pr_morePagedResults = 1;
1868 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1869 "%scookie=", ptr == buf ? "" : " " );
1872 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1873 "pagedresults", buf, ptr - buf );
1880 print_sss( LDAP *ld, LDAPControl *ctrl )
1886 rc = ldap_parse_sortresponse_control( ld, ctrl, &err, &attr );
1887 if ( rc == LDAP_SUCCESS ) {
1889 rc = snprintf( buf, sizeof(buf), "(%d) %s %s",
1890 err, ldap_err2string(err), attr ? attr : "" );
1892 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1893 "sortResult", buf, rc );
1899 #ifdef LDAP_CONTROL_X_DEREF
1901 print_deref( LDAP *ld, LDAPControl *ctrl )
1903 LDAPDerefRes *drhead = NULL, *dr;
1906 rc = ldap_parse_derefresponse_control( ld, ctrl, &drhead );
1907 if ( rc != LDAP_SUCCESS ) {
1911 for ( dr = drhead; dr != NULL; dr = dr->next ) {
1916 len = strlen( dr->derefAttr ) + STRLENOF(": ");
1918 for ( dv = dr->attrVals; dv != NULL; dv = dv->next ) {
1919 if ( dv->vals != NULL ) {
1921 ber_len_t tlen = strlen(dv->type);
1923 for ( j = 0; dv->vals[ j ].bv_val != NULL; j++ ) {
1924 len += STRLENOF("<=>;") + tlen + dv->vals[ j ].bv_len;
1928 len += dr->derefVal.bv_len;
1929 buf = ldap_memalloc( len + 1 );
1930 if ( buf == NULL ) {
1931 rc = LDAP_NO_MEMORY;
1936 ptr = lutil_strcopy( ptr, dr->derefAttr );
1939 for ( dv = dr->attrVals; dv != NULL; dv = dv->next ) {
1940 if ( dv->vals != NULL ) {
1942 for ( j = 0; dv->vals[ j ].bv_val != NULL; j++ ) {
1944 ptr = lutil_strcopy( ptr, dv->type );
1946 ptr = lutil_strncopy( ptr, dv->vals[ j ].bv_val, dv->vals[ j ].bv_len );
1952 ptr = lutil_strncopy( ptr, dr->derefVal.bv_val, dr->derefVal.bv_len );
1955 tool_write_ldif( LDIF_PUT_COMMENT, NULL, buf, len );
1957 ldap_memfree( buf );
1963 ldap_derefresponse_free( drhead );
1969 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1971 print_ppolicy( LDAP *ld, LDAPControl *ctrl )
1973 int expire = 0, grace = 0, rc;
1974 LDAPPasswordPolicyError pperr;
1976 rc = ldap_parse_passwordpolicy_control( ld, ctrl,
1977 &expire, &grace, &pperr );
1978 if ( rc == LDAP_SUCCESS ) {
1979 char buf[ BUFSIZ ], *ptr = buf;
1981 if ( expire != -1 ) {
1982 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1983 "expire=%d", expire );
1986 if ( grace != -1 ) {
1987 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1988 "%sgrace=%d", ptr == buf ? "" : " ", grace );
1991 if ( pperr != PP_noError ) {
1992 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1993 "%serror=%d (%s)", ptr == buf ? "" : " ",
1995 ldap_passwordpolicy_err2txt( pperr ) );
1998 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1999 "ppolicy", buf, ptr - buf );
2006 void tool_print_ctrls(
2008 LDAPControl **ctrls )
2013 for ( i = 0; ctrls[i] != NULL; i++ ) {
2014 /* control: OID criticality base64value */
2015 struct berval b64 = BER_BVNULL;
2020 /* FIXME: there might be cases where a control has NULL OID;
2021 * this makes little sense, especially when returned by the
2022 * server, but libldap happily allows it */
2023 if ( ctrls[i]->ldctl_oid == NULL ) {
2028 len += strlen( ctrls[i]->ldctl_oid );
2030 /* add enough for space after OID and the critical value itself */
2031 len += ctrls[i]->ldctl_iscritical
2032 ? sizeof("true") : sizeof("false");
2034 /* convert to base64 */
2035 if ( !BER_BVISNULL( &ctrls[i]->ldctl_value ) ) {
2036 b64.bv_len = LUTIL_BASE64_ENCODE_LEN(
2037 ctrls[i]->ldctl_value.bv_len ) + 1;
2038 b64.bv_val = ber_memalloc( b64.bv_len + 1 );
2040 b64.bv_len = lutil_b64_ntop(
2041 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
2042 ctrls[i]->ldctl_value.bv_len,
2043 b64.bv_val, b64.bv_len );
2047 len += 1 + b64.bv_len;
2050 ptr = str = malloc( len + 1 );
2052 ptr = lutil_strcopy( ptr, ": " );
2054 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_oid );
2055 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_iscritical
2056 ? " true" : " false" );
2059 ptr = lutil_strcopy( ptr, " " );
2060 ptr = lutil_strcopy( ptr, b64.bv_val );
2064 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2065 "control", str, len );
2070 ber_memfree( b64.bv_val );
2073 /* known controls */
2074 for ( j = 0; tool_ctrl_response[j].oid != NULL; j++ ) {
2075 if ( strcmp( tool_ctrl_response[j].oid, ctrls[i]->ldctl_oid ) == 0 ) {
2076 if ( !tool_ctrl_response[j].mask & tool_type ) {
2077 /* this control should not appear
2078 * with this tool; warning? */
2084 if ( tool_ctrl_response[j].oid != NULL && tool_ctrl_response[j].func ) {
2085 (void)tool_ctrl_response[j].func( ld, ctrls[i] );
2091 tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
2095 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
2099 fputs( ldif, stdout );
2100 ber_memfree( ldif );
2106 tool_is_oid( const char *s )
2110 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2114 for ( ; s[ 0 ]; s++ ) {
2115 if ( s[ 0 ] == '.' ) {
2116 if ( s[ 1 ] == '\0' ) {
2123 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2127 if ( first == 1 && s[ 0 ] == '0' && s[ 1 ] != '.' ) {