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-2007 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;
97 /* support deprecated early version of proxyAuthz */
98 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ "2.16.840.1.113730.3.4.12"
99 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
100 char *proxydn = NULL;
101 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
107 static char *preread_attrs = NULL;
109 static char *postread_attrs = NULL;
110 ber_int_t pr_morePagedResults = 1;
111 struct berval pr_cookie = { 0, NULL };
112 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
114 static int chainingResolve = -1;
115 static int chainingContinuation = -1;
116 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
117 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
118 static int sessionTracking = 0;
119 struct berval stValue;
120 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
122 LDAPControl *unknown_ctrls = NULL;
123 int unknown_ctrls_num = 0;
126 struct timeval nettimeout = { -1 , 0 };
128 typedef int (*print_ctrl_fn)( LDAP *ld, LDAPControl *ctrl );
130 static int print_preread( LDAP *ld, LDAPControl *ctrl );
131 static int print_postread( LDAP *ld, LDAPControl *ctrl );
132 static int print_paged_results( LDAP *ld, LDAPControl *ctrl );
133 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
134 static int print_ppolicy( LDAP *ld, LDAPControl *ctrl );
137 static struct tool_ctrls_t {
141 } tool_ctrl_response[] = {
142 { LDAP_CONTROL_PRE_READ, TOOL_ALL, print_preread },
143 { LDAP_CONTROL_POST_READ, TOOL_ALL, print_postread },
144 { LDAP_CONTROL_PAGEDRESULTS, TOOL_SEARCH, print_paged_results },
145 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
146 { LDAP_CONTROL_PASSWORDPOLICYRESPONSE, TOOL_ALL, print_ppolicy },
162 tool_init( tool_type_t type )
165 ldap_pvt_setlocale(LC_MESSAGES, "");
166 ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
167 ldap_pvt_textdomain(OPENLDAP_PACKAGE);
173 #ifdef HAVE_CYRUS_SASL
177 ldap_pvt_tls_destroy();
182 tool_common_usage( void )
184 static const char *const descriptions[] = {
185 N_(" -c continuous operation mode (do not stop on errors)\n"),
186 N_(" -d level set LDAP debugging level to `level'\n"),
187 N_(" -D binddn bind DN\n"),
188 N_(" -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
189 N_(" [!]assert=<filter> (a RFC 4515 Filter string)\n")
190 N_(" [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n")
191 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
193 /* non-advertized support for proxyDN */
194 N_(" [!]proxydn=<dn> (a RFC 4514 DN string)\n")
197 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
198 N_(" [!]chaining[=<resolveBehavior>[/<continuationBehavior>]]\n")
199 N_(" one of \"chainingPreferred\", \"chainingRequired\",\n")
200 N_(" \"referralsPreferred\", \"referralsRequired\"\n")
201 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
202 N_(" [!]manageDSAit\n")
204 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
207 N_(" [!]postread[=<attrs>] (a comma-separated attribute list)\n")
208 N_(" [!]preread[=<attrs>] (a comma-separated attribute list)\n")
212 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
213 N_(" [!]sessiontracking\n")
214 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
215 N_(" abandon, cancel, ignore (SIGINT sends abandon/cancel,\n"
216 " or ignores response; if critical, doesn't wait for SIGINT.\n"
217 " not really controls)\n")
218 N_(" -f file read operations from `file'\n"),
219 N_(" -h host LDAP server\n"),
220 N_(" -H URI LDAP Uniform Resource Identifier(s)\n"),
221 N_(" -I use SASL Interactive mode\n"),
222 N_(" -M enable Manage DSA IT control (-MM to make critical)\n"),
223 N_(" -n show what would be done but don't actually do it\n"),
224 N_(" -O props SASL security properties\n"),
225 N_(" -o <opt>[=<optparam] general options\n"),
226 N_(" nettimeout=<timeout> (in seconds, or \"none\" or \"max\")\n"),
227 N_(" -p port port on LDAP server\n"),
228 N_(" -P version protocol version (default: 3)\n"),
229 N_(" -Q use SASL Quiet mode\n"),
230 N_(" -R realm SASL realm\n"),
231 N_(" -U authcid SASL authentication identity\n"),
232 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
233 N_(" -V print version info (-VV only)\n"),
234 N_(" -w passwd bind password (for simple authentication)\n"),
235 N_(" -W prompt for bind password\n"),
236 N_(" -x Simple authentication\n"),
237 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
238 N_(" -y file Read password from file\n"),
239 N_(" -Y mech SASL mechanism\n"),
240 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
243 const char *const *cpp;
245 fputs( _("Common options:\n"), stderr );
246 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
247 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
248 fputs( _(*cpp), stderr );
261 fprintf( stderr, "%s: %s (%d)%s\n",
262 func, ldap_err2string( err ), err, extra ? extra : "" );
264 if ( matched && *matched ) {
265 fprintf( stderr, _("\tmatched DN: %s\n"), matched );
268 if ( info && *info ) {
269 fprintf( stderr, _("\tadditional info: %s\n"), info );
272 if ( refs && *refs ) {
274 fprintf( stderr, _("\treferrals:\n") );
275 for( i=0; refs[i]; i++ ) {
276 fprintf( stderr, "\t\t%s\n", refs[i] );
283 tool_args( int argc, char **argv )
287 while (( i = getopt( argc, argv, options )) != EOF ) {
289 char *control, *cvalue, *next;
291 case 'c': /* continuous operation mode */
298 ival = strtol( optarg, &next, 10 );
299 if (next == NULL || next[0] != '\0') {
300 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
305 case 'D': /* bind DN */
306 if( binddn != NULL ) {
307 fprintf( stderr, "%s: -D previously specified\n", prog );
308 exit( EXIT_FAILURE );
310 binddn = ber_strdup( optarg );
312 case 'e': /* general extensions (controls and such) */
313 /* should be extended to support comma separated list of
314 * [!]key[=value] parameters, e.g. -e !foo,bar=567
319 if( optarg[0] == '!' ) {
324 control = ber_strdup( optarg );
325 if ( (cvalue = strchr( control, '=' )) != NULL ) {
329 if ( strcasecmp( control, "assert" ) == 0 ) {
331 fprintf( stderr, "assert control previously specified\n");
332 exit( EXIT_FAILURE );
334 if( cvalue == NULL ) {
335 fprintf( stderr, "assert: control value expected\n" );
339 assertctl = 1 + crit;
341 assert( assertion == NULL );
344 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
345 if( authzid != NULL ) {
346 fprintf( stderr, "authzid control previously specified\n");
347 exit( EXIT_FAILURE );
349 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
350 if( proxydn != NULL ) {
351 fprintf( stderr, "authzid control incompatible with proxydn\n");
352 exit( EXIT_FAILURE );
354 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
355 if( cvalue == NULL ) {
356 fprintf( stderr, "authzid: control value expected\n" );
360 fprintf( stderr, "authzid: must be marked critical\n" );
364 assert( authzid == NULL );
367 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
368 } else if ( strcasecmp( control, "proxydn" ) == 0 ) {
369 if( proxydn != NULL ) {
370 fprintf( stderr, "proxydn control previously specified\n");
371 exit( EXIT_FAILURE );
373 if( authzid != NULL ) {
374 fprintf( stderr, "proxydn control incompatible with authzid\n");
375 exit( EXIT_FAILURE );
377 if( cvalue == NULL ) {
378 fprintf( stderr, "proxydn: control value expected\n" );
382 fprintf( stderr, "proxydn: must be marked critical\n" );
386 assert( proxydn == NULL );
388 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
390 } else if ( ( strcasecmp( control, "relax" ) == 0 ) ||
391 ( strcasecmp( control, "manageDIT" ) == 0 ) )
395 "relax control previously specified\n");
396 exit( EXIT_FAILURE );
398 if( cvalue != NULL ) {
400 "relax: no control value expected\n" );
404 manageDIT = 1 + crit;
406 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
409 "manageDSAit control previously specified\n");
410 exit( EXIT_FAILURE );
412 if( cvalue != NULL ) {
414 "manageDSAit: no control value expected\n" );
418 manageDSAit = 1 + crit;
420 } else if ( strcasecmp( control, "noop" ) == 0 ) {
422 fprintf( stderr, "noop control previously specified\n");
423 exit( EXIT_FAILURE );
425 if( cvalue != NULL ) {
426 fprintf( stderr, "noop: no control value expected\n" );
432 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
433 } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
435 fprintf( stderr, "ppolicy control previously specified\n");
436 exit( EXIT_FAILURE );
438 if( cvalue != NULL ) {
439 fprintf( stderr, "ppolicy: no control value expected\n" );
443 fprintf( stderr, "ppolicy: critical flag not allowed\n" );
450 } else if ( strcasecmp( control, "preread" ) == 0 ) {
452 fprintf( stderr, "preread control previously specified\n");
453 exit( EXIT_FAILURE );
457 preread_attrs = cvalue;
459 } else if ( strcasecmp( control, "postread" ) == 0 ) {
461 fprintf( stderr, "postread control previously specified\n");
462 exit( EXIT_FAILURE );
466 postread_attrs = cvalue;
468 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
469 } else if ( strcasecmp( control, "chaining" ) == 0 ) {
472 if ( cvalue != NULL ) {
475 continuation = strchr( cvalue, '/' );
476 if ( continuation ) {
477 /* FIXME: this makes sense only in searches */
478 *continuation++ = '\0';
479 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
480 chainingContinuation = LDAP_CHAINING_PREFERRED;
481 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
482 chainingContinuation = LDAP_CHAINING_REQUIRED;
483 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
484 chainingContinuation = LDAP_REFERRALS_PREFERRED;
485 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
486 chainingContinuation = LDAP_REFERRALS_REQUIRED;
489 "chaining behavior control "
490 "continuation value \"%s\" invalid\n",
492 exit( EXIT_FAILURE );
496 if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
497 chainingResolve = LDAP_CHAINING_PREFERRED;
498 } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
499 chainingResolve = LDAP_CHAINING_REQUIRED;
500 } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
501 chainingResolve = LDAP_REFERRALS_PREFERRED;
502 } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
503 chainingResolve = LDAP_REFERRALS_REQUIRED;
506 "chaining behavior control "
507 "resolve value \"%s\" invalid\n",
509 exit( EXIT_FAILURE );
512 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
514 /* this shouldn't go here, really; but it's a feature... */
515 } else if ( strcasecmp( control, "abandon" ) == 0 ) {
516 abcan = LDAP_REQ_ABANDON;
521 } else if ( strcasecmp( control, "cancel" ) == 0 ) {
522 abcan = LDAP_REQ_EXTENDED;
527 } else if ( strcasecmp( control, "ignore" ) == 0 ) {
533 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
534 } else if ( strcasecmp( control, "sessiontracking" ) == 0 ) {
535 if ( sessionTracking ) {
536 fprintf( stderr, "%s: session tracking can be only specified once\n", prog );
537 exit( EXIT_FAILURE );
540 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
542 } else if ( tool_is_oid( control ) ) {
543 LDAPControl *tmpctrls, ctrl;
545 tmpctrls = (LDAPControl *)realloc( unknown_ctrls,
546 (unknown_ctrls_num + 1)*sizeof( LDAPControl ) );
547 if ( tmpctrls == NULL ) {
548 fprintf( stderr, "%s: no memory?\n", prog );
549 exit( EXIT_FAILURE );
551 unknown_ctrls = tmpctrls;
552 ctrl.ldctl_oid = control;
553 ctrl.ldctl_value.bv_val = NULL;
554 ctrl.ldctl_value.bv_len = 0;
555 ctrl.ldctl_iscritical = crit;
557 if ( cvalue != NULL ) {
559 size_t len = strlen( cvalue );
562 bv.bv_len = LUTIL_BASE64_DECODE_LEN( len );
563 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
565 retcode = lutil_b64_pton( cvalue,
566 (unsigned char *)bv.bv_val,
569 if ( retcode == -1 || retcode > bv.bv_len ) {
570 fprintf( stderr, "Unable to parse value of general control %s\n",
576 ctrl.ldctl_value = bv;
579 unknown_ctrls[ unknown_ctrls_num ] = ctrl;
583 fprintf( stderr, "Invalid general control name: %s\n",
588 case 'f': /* read from file */
589 if( infile != NULL ) {
590 fprintf( stderr, "%s: -f previously specified\n", prog );
591 exit( EXIT_FAILURE );
593 infile = ber_strdup( optarg );
595 case 'h': /* ldap host */
596 if( ldaphost != NULL ) {
597 fprintf( stderr, "%s: -h previously specified\n", prog );
598 exit( EXIT_FAILURE );
600 ldaphost = ber_strdup( optarg );
602 case 'H': /* ldap URI */
603 if( ldapuri != NULL ) {
604 fprintf( stderr, "%s: -H previously specified\n", prog );
605 exit( EXIT_FAILURE );
607 ldapuri = ber_strdup( optarg );
610 #ifdef HAVE_CYRUS_SASL
611 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
612 fprintf( stderr, "%s: incompatible previous "
613 "authentication choice\n",
615 exit( EXIT_FAILURE );
617 authmethod = LDAP_AUTH_SASL;
618 sasl_flags = LDAP_SASL_INTERACTIVE;
621 fprintf( stderr, "%s: was not compiled with SASL support\n",
623 exit( EXIT_FAILURE );
626 /* enable Manage DSA IT */
629 case 'n': /* print operations, don't actually do them */
633 control = ber_strdup( optarg );
634 if ( (cvalue = strchr( control, '=' )) != NULL ) {
638 if ( strcasecmp( control, "nettimeout" ) == 0 ) {
639 if( nettimeout.tv_sec != -1 ) {
640 fprintf( stderr, "nettimeout option previously specified\n");
641 exit( EXIT_FAILURE );
643 if( cvalue == NULL || cvalue[0] == '\0' ) {
644 fprintf( stderr, "nettimeout: option value expected\n" );
647 if ( strcasecmp( cvalue, "none" ) == 0 ) {
648 nettimeout.tv_sec = 0;
649 } else if ( strcasecmp( cvalue, "max" ) == 0 ) {
650 nettimeout.tv_sec = LDAP_MAXINT;
652 ival = strtol( cvalue, &next, 10 );
653 if ( next == NULL || next[0] != '\0' ) {
655 _("Unable to parse network timeout \"%s\"\n"), cvalue );
656 exit( EXIT_FAILURE );
658 nettimeout.tv_sec = ival;
660 if( nettimeout.tv_sec < 0 || nettimeout.tv_sec > LDAP_MAXINT ) {
661 fprintf( stderr, _("%s: invalid network timeout (%ld) specified\n"),
662 prog, (long)nettimeout.tv_sec );
663 exit( EXIT_FAILURE );
666 fprintf( stderr, "Invalid general option name: %s\n",
672 #ifdef HAVE_CYRUS_SASL
673 if( sasl_secprops != NULL ) {
674 fprintf( stderr, "%s: -O previously specified\n", prog );
675 exit( EXIT_FAILURE );
677 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
678 fprintf( stderr, "%s: incompatible previous "
679 "authentication choice\n", prog );
680 exit( EXIT_FAILURE );
682 authmethod = LDAP_AUTH_SASL;
683 sasl_secprops = ber_strdup( optarg );
685 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
686 exit( EXIT_FAILURE );
691 fprintf( stderr, "%s: -p previously specified\n", prog );
692 exit( EXIT_FAILURE );
694 ival = strtol( optarg, &next, 10 );
695 if ( next == NULL || next[0] != '\0' ) {
696 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
697 exit( EXIT_FAILURE );
702 ival = strtol( optarg, &next, 10 );
703 if ( next == NULL || next[0] != '\0' ) {
704 fprintf( stderr, "%s: unabel to parse protocol version \"%s\"\n", prog, optarg );
705 exit( EXIT_FAILURE );
709 if( protocol == LDAP_VERSION3 ) {
710 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
712 exit( EXIT_FAILURE );
714 protocol = LDAP_VERSION2;
717 if( protocol == LDAP_VERSION2 ) {
718 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
720 exit( EXIT_FAILURE );
722 protocol = LDAP_VERSION3;
725 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
731 #ifdef HAVE_CYRUS_SASL
732 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
733 fprintf( stderr, "%s: incompatible previous "
734 "authentication choice\n",
736 exit( EXIT_FAILURE );
738 authmethod = LDAP_AUTH_SASL;
739 sasl_flags = LDAP_SASL_QUIET;
742 fprintf( stderr, "%s: not compiled with SASL support\n",
744 exit( EXIT_FAILURE );
747 #ifdef HAVE_CYRUS_SASL
748 if( sasl_realm != NULL ) {
749 fprintf( stderr, "%s: -R previously specified\n", prog );
750 exit( EXIT_FAILURE );
752 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
753 fprintf( stderr, "%s: incompatible previous "
754 "authentication choice\n",
756 exit( EXIT_FAILURE );
758 authmethod = LDAP_AUTH_SASL;
759 sasl_realm = ber_strdup( optarg );
761 fprintf( stderr, "%s: not compiled with SASL support\n",
763 exit( EXIT_FAILURE );
767 #ifdef HAVE_CYRUS_SASL
768 if( sasl_authc_id != NULL ) {
769 fprintf( stderr, "%s: -U previously specified\n", prog );
770 exit( EXIT_FAILURE );
772 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
773 fprintf( stderr, "%s: incompatible previous "
774 "authentication choice\n",
776 exit( EXIT_FAILURE );
778 authmethod = LDAP_AUTH_SASL;
779 sasl_authc_id = ber_strdup( optarg );
781 fprintf( stderr, "%s: not compiled with SASL support\n",
783 exit( EXIT_FAILURE );
786 case 'v': /* verbose mode */
789 case 'V': /* version */
792 case 'w': /* password */
793 passwd.bv_val = ber_strdup( optarg );
797 for( p = optarg; *p != '\0'; p++ ) {
801 passwd.bv_len = strlen( passwd.bv_val );
810 #ifdef HAVE_CYRUS_SASL
811 if( sasl_mech != NULL ) {
812 fprintf( stderr, "%s: -Y previously specified\n", prog );
813 exit( EXIT_FAILURE );
815 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
817 "%s: incompatible with authentication choice\n", prog );
818 exit( EXIT_FAILURE );
820 authmethod = LDAP_AUTH_SASL;
821 sasl_mech = ber_strdup( optarg );
823 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
824 exit( EXIT_FAILURE );
828 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
829 fprintf( stderr, "%s: incompatible with previous "
830 "authentication choice\n", prog );
831 exit( EXIT_FAILURE );
833 authmethod = LDAP_AUTH_SIMPLE;
836 #ifdef HAVE_CYRUS_SASL
837 if( sasl_authz_id != NULL ) {
838 fprintf( stderr, "%s: -X previously specified\n", prog );
839 exit( EXIT_FAILURE );
841 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
842 fprintf( stderr, "%s: -X incompatible with "
843 "authentication choice\n", prog );
844 exit( EXIT_FAILURE );
846 authmethod = LDAP_AUTH_SASL;
847 sasl_authz_id = ber_strdup( optarg );
849 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
850 exit( EXIT_FAILURE );
857 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
858 exit( EXIT_FAILURE );
862 if( handle_private_option( i ) ) break;
863 fprintf( stderr, "%s: unrecognized option -%c\n",
870 /* prevent bad linking */
872 api.ldapai_info_version = LDAP_API_INFO_VERSION;
874 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
875 != LDAP_OPT_SUCCESS )
877 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
878 exit( EXIT_FAILURE );
881 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
882 fprintf( stderr, "LDAP APIInfo version mismatch: "
883 "library %d, header %d\n",
884 api.ldapai_info_version, LDAP_API_INFO_VERSION );
885 exit( EXIT_FAILURE );
888 if( api.ldapai_api_version != LDAP_API_VERSION ) {
889 fprintf( stderr, "LDAP API version mismatch: "
890 "library %d, header %d\n",
891 api.ldapai_api_version, LDAP_API_VERSION );
892 exit( EXIT_FAILURE );
895 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
896 fprintf( stderr, "LDAP vendor name mismatch: "
897 "library %s, header %s\n",
898 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
899 exit( EXIT_FAILURE );
902 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
903 fprintf( stderr, "LDAP vendor version mismatch: "
904 "library %d, header %d\n",
905 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
906 exit( EXIT_FAILURE );
910 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
912 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
913 if (version > 1) exit( EXIT_SUCCESS );
916 ldap_memfree( api.ldapai_vendor_name );
917 ber_memvfree( (void **)api.ldapai_extensions );
921 protocol = LDAP_VERSION3;
923 if (authmethod == -1 && protocol > LDAP_VERSION2) {
924 #ifdef HAVE_CYRUS_SASL
925 authmethod = LDAP_AUTH_SASL;
927 authmethod = LDAP_AUTH_SIMPLE;
931 if( ldapuri == NULL ) {
932 if( ldapport && ( ldaphost == NULL )) {
933 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
934 exit( EXIT_FAILURE );
937 if( ldaphost != NULL ) {
938 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
939 exit( EXIT_FAILURE );
942 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
943 exit( EXIT_FAILURE );
947 if( protocol == LDAP_VERSION2 ) {
948 if( assertctl || authzid || manageDIT || manageDSAit ||
949 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
951 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
952 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
955 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
957 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
958 noop || ppolicy || preread || postread )
960 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
961 exit( EXIT_FAILURE );
965 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
966 exit( EXIT_FAILURE );
969 #ifdef HAVE_CYRUS_SASL
970 if( authmethod == LDAP_AUTH_SASL ) {
971 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
973 exit( EXIT_FAILURE );
981 tool_conn_setup( int dont, void (*private_setup)( LDAP * ) )
986 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
987 != LBER_OPT_SUCCESS )
990 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
992 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
993 != LDAP_OPT_SUCCESS )
996 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
1001 (void) SIGNAL( SIGPIPE, SIG_IGN );
1005 SIGNAL( SIGINT, do_sig );
1011 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
1014 memset( &url, 0, sizeof(url));
1016 url.lud_scheme = "ldap";
1017 url.lud_host = ldaphost;
1018 url.lud_port = ldapport;
1019 url.lud_scope = LDAP_SCOPE_DEFAULT;
1021 ldapuri = ldap_url_desc2str( &url );
1023 } else if ( ldapuri != NULL ) {
1024 LDAPURLDesc *ludlist, **ludp;
1028 rc = ldap_url_parselist( &ludlist, ldapuri );
1029 if ( rc != LDAP_URL_SUCCESS ) {
1031 "Could not parse LDAP URI(s)=%s (%d)\n",
1033 exit( EXIT_FAILURE );
1036 for ( ludp = &ludlist; *ludp != NULL; ) {
1037 LDAPURLDesc *lud = *ludp;
1040 if ( lud->lud_dn != NULL && lud->lud_dn[ 0 ] != '\0' &&
1041 ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) )
1043 /* if no host but a DN is provided,
1044 * use DNS SRV to gather the host list
1045 * and turn it into a list of URIs
1046 * using the scheme provided */
1047 char *domain = NULL,
1051 len_proto = strlen( lud->lud_scheme );
1053 if ( ldap_dn2domain( lud->lud_dn, &domain )
1057 "DNS SRV: Could not turn "
1058 "DN=\"%s\" into a domain\n",
1063 rc = ldap_domain2hostlist( domain, &hostlist );
1066 "DNS SRV: Could not turn "
1067 "domain=%s into a hostlist\n",
1072 hosts = ldap_str2charray( hostlist, " " );
1073 if ( hosts == NULL ) {
1075 "DNS SRV: Could not parse "
1076 "hostlist=\"%s\"\n",
1081 for ( i = 0; hosts[ i ] != NULL; i++ )
1084 tmp = (char **)realloc( urls, sizeof( char * ) * ( nurls + i + 1 ) );
1085 if ( tmp == NULL ) {
1087 "DNS SRV: out of memory?\n" );
1091 urls[ nurls ] = NULL;
1093 for ( i = 0; hosts[ i ] != NULL; i++ ) {
1094 size_t len = len_proto
1096 + strlen( hosts[ i ] )
1099 urls[ nurls + i + 1 ] = NULL;
1100 urls[ nurls + i ] = (char *)malloc( sizeof( char ) * len );
1101 if ( urls[ nurls + i ] == NULL ) {
1103 "DNS SRV: out of memory?\n" );
1107 snprintf( urls[ nurls + i ], len, "%s://%s",
1108 lud->lud_scheme, hosts[ i ] );
1113 ber_memvfree( (void **)hosts );
1114 ber_memfree( hostlist );
1115 ber_memfree( domain );
1118 tmp = (char **)realloc( urls, sizeof( char * ) * ( nurls + 2 ) );
1119 if ( tmp == NULL ) {
1121 "DNS SRV: out of memory?\n" );
1125 urls[ nurls + 1 ] = NULL;
1127 urls[ nurls ] = ldap_url_desc2str( lud );
1128 if ( urls[ nurls ] == NULL ) {
1130 "DNS SRV: out of memory?\n" );
1136 *ludp = lud->lud_next;
1138 lud->lud_next = NULL;
1139 ldap_free_urldesc( lud );
1142 if ( ludlist != NULL ) {
1143 ldap_free_urllist( ludlist );
1144 exit( EXIT_FAILURE );
1146 } else if ( urls == NULL ) {
1147 exit( EXIT_FAILURE );
1150 ldap_memfree( ldapuri );
1151 ldapuri = ldap_charray2str( urls, " " );
1152 ber_memvfree( (void **)urls );
1156 fprintf( stderr, "ldap_initialize( %s )\n",
1157 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
1159 rc = ldap_initialize( &ld, ldapuri );
1160 if( rc != LDAP_SUCCESS ) {
1162 "Could not create LDAP session handle for URI=%s (%d): %s\n",
1163 ldapuri, rc, ldap_err2string(rc) );
1164 exit( EXIT_FAILURE );
1167 if( private_setup ) private_setup( ld );
1170 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
1171 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
1173 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
1174 referrals ? "on" : "off" );
1175 exit( EXIT_FAILURE );
1178 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
1179 != LDAP_OPT_SUCCESS )
1181 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
1183 exit( EXIT_FAILURE );
1187 rc = ldap_start_tls_s( ld, NULL, NULL );
1188 if ( rc != LDAP_SUCCESS ) {
1189 tool_perror( "ldap_start_tls", rc, NULL, NULL, NULL, NULL );
1190 if ( use_tls > 1 ) {
1191 exit( EXIT_FAILURE );
1196 if ( nettimeout.tv_sec > 0 ) {
1197 if ( ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &nettimeout )
1198 != LDAP_OPT_SUCCESS )
1200 fprintf( stderr, "Could not set LDAP_OPT_NETWORK_TIMEOUT %ld\n",
1201 (long)nettimeout.tv_sec );
1202 exit( EXIT_FAILURE );
1212 tool_bind( LDAP *ld )
1214 LDAPControl **sctrlsp = NULL;
1215 LDAPControl *sctrls[2];
1218 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1221 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1222 c.ldctl_value.bv_val = NULL;
1223 c.ldctl_value.bv_len = 0;
1224 c.ldctl_iscritical = 0;
1225 sctrls[nsctrls] = &c;
1226 sctrls[++nsctrls] = NULL;
1234 assert( nsctrls < sizeof(sctrls)/sizeof(sctrls[0]) );
1236 if ( authmethod == LDAP_AUTH_SASL ) {
1237 #ifdef HAVE_CYRUS_SASL
1241 if( sasl_secprops != NULL ) {
1242 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
1243 (void *) sasl_secprops );
1245 if( rc != LDAP_OPT_SUCCESS ) {
1247 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
1249 exit( LDAP_LOCAL_ERROR );
1253 defaults = lutil_sasl_defaults( ld,
1260 rc = ldap_sasl_interactive_bind_s( ld, binddn, sasl_mech,
1262 NULL, sasl_flags, lutil_sasl_interact, defaults );
1264 lutil_sasl_freedefs( defaults );
1265 if( rc != LDAP_SUCCESS ) {
1266 tool_perror( "ldap_sasl_interactive_bind_s",
1267 rc, NULL, NULL, NULL, NULL );
1271 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1272 exit( LDAP_NOT_SUPPORTED );
1276 LDAPMessage *result;
1277 LDAPControl **ctrls;
1279 char *matched = NULL;
1287 rc = ldap_sasl_bind( ld, binddn, LDAP_SASL_SIMPLE, &passwd,
1288 sctrlsp, NULL, &msgid );
1289 if ( msgid == -1 ) {
1290 tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
1291 NULL, NULL, NULL, NULL );
1296 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 ) {
1297 tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
1298 exit( LDAP_LOCAL_ERROR );
1301 rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
1303 if ( rc != LDAP_SUCCESS ) {
1304 tool_perror( "ldap_bind parse result", rc, NULL, matched, info, refs );
1305 exit( LDAP_LOCAL_ERROR );
1308 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1309 if ( ctrls && ppolicy ) {
1311 int expire, grace, len = 0;
1312 LDAPPasswordPolicyError pErr = -1;
1314 ctrl = ldap_find_control( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
1317 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
1318 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1320 if ( pErr != PP_noError ){
1323 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1324 len = strlen( msgbuf );
1326 if ( expire >= 0 ) {
1327 sprintf( msgbuf+len,
1328 " (Password expires in %d seconds)",
1330 } else if ( grace >= 0 ) {
1331 sprintf( msgbuf+len,
1332 " (Password expired, %d grace logins remain)",
1340 ldap_controls_free( ctrls );
1343 if ( err != LDAP_SUCCESS
1345 || ( matched && matched[ 0 ] )
1346 || ( info && info[ 0 ] )
1349 tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1351 if( matched ) ber_memfree( matched );
1352 if( info ) ber_memfree( info );
1353 if( refs ) ber_memvfree( (void **)refs );
1355 if ( err != LDAP_SUCCESS ) exit( err );
1361 tool_unbind( LDAP *ld )
1363 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1365 if ( err != LDAP_OPT_SUCCESS ) {
1366 fprintf( stderr, "Could not unset controls\n");
1369 (void) ldap_unbind_ext( ld, NULL, NULL );
1373 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1375 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1377 int i = 0, j, crit = 0, err;
1378 LDAPControl c[16], **ctrls;
1382 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1384 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1388 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1393 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1395 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1396 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1398 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1400 || unknown_ctrls_num ) )
1405 ctrls = (LDAPControl**) malloc(sizeof(c) + (count + unknown_ctrls_num + 1)*sizeof(LDAPControl*));
1406 if ( ctrls == NULL ) {
1407 fprintf( stderr, "No memory\n" );
1408 exit( EXIT_FAILURE );
1412 BerElementBuffer berbuf;
1413 BerElement *ber = (BerElement *)&berbuf;
1415 if( assertion == NULL || *assertion == '\0' ) {
1416 fprintf( stderr, "Assertion=<empty>\n" );
1417 exit( EXIT_FAILURE );
1420 ber_init2( ber, NULL, LBER_USE_DER );
1422 err = ldap_pvt_put_filter( ber, assertion );
1424 fprintf( stderr, "assertion encode failed (%d)\n", err );
1425 exit( EXIT_FAILURE );
1428 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1430 fprintf( stderr, "assertion flatten failed (%d)\n", err );
1431 exit( EXIT_FAILURE );
1434 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1435 c[i].ldctl_iscritical = assertctl > 1;
1441 c[i].ldctl_value.bv_val = authzid;
1442 c[i].ldctl_value.bv_len = strlen( authzid );
1443 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1444 c[i].ldctl_iscritical = 1;
1449 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1450 /* NOTE: doesn't need an extra count because it's incompatible
1453 BerElementBuffer berbuf;
1454 BerElement *ber = (BerElement *)&berbuf;
1456 ber_init2( ber, NULL, LBER_USE_DER );
1458 if ( ber_printf( ber, "s", proxydn ) == LBER_ERROR ) {
1459 exit( EXIT_FAILURE );
1462 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1463 exit( EXIT_FAILURE );
1466 c[i].ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1467 c[i].ldctl_iscritical = 1;
1471 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1474 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1475 BER_BVZERO( &c[i].ldctl_value );
1476 c[i].ldctl_iscritical = manageDIT > 1;
1481 if ( manageDSAit ) {
1482 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1483 BER_BVZERO( &c[i].ldctl_value );
1484 c[i].ldctl_iscritical = manageDSAit > 1;
1490 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1491 BER_BVZERO( &c[i].ldctl_value );
1492 c[i].ldctl_iscritical = noop > 1;
1497 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1499 c[i].ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1500 BER_BVZERO( &c[i].ldctl_value );
1501 c[i].ldctl_iscritical = 0;
1508 char berbuf[LBER_ELEMENT_SIZEOF];
1509 BerElement *ber = (BerElement *)berbuf;
1510 char **attrs = NULL;
1512 if( preread_attrs ) {
1513 attrs = ldap_str2charray( preread_attrs, "," );
1516 ber_init2( ber, NULL, LBER_USE_DER );
1518 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1519 fprintf( stderr, "preread attrs encode failed.\n" );
1520 exit( EXIT_FAILURE );
1523 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1525 fprintf( stderr, "preread flatten failed (%d)\n", err );
1526 exit( EXIT_FAILURE );
1529 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1530 c[i].ldctl_iscritical = preread > 1;
1534 if( attrs ) ldap_charray_free( attrs );
1538 char berbuf[LBER_ELEMENT_SIZEOF];
1539 BerElement *ber = (BerElement *)berbuf;
1540 char **attrs = NULL;
1542 if( postread_attrs ) {
1543 attrs = ldap_str2charray( postread_attrs, "," );
1546 ber_init2( ber, NULL, LBER_USE_DER );
1548 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1549 fprintf( stderr, "postread attrs encode failed.\n" );
1550 exit( EXIT_FAILURE );
1553 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1555 fprintf( stderr, "postread flatten failed (%d)\n", err );
1556 exit( EXIT_FAILURE );
1559 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1560 c[i].ldctl_iscritical = postread > 1;
1564 if( attrs ) ldap_charray_free( attrs );
1567 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1569 if ( chainingResolve > -1 ) {
1570 BerElementBuffer berbuf;
1571 BerElement *ber = (BerElement *)&berbuf;
1573 ber_init2( ber, NULL, LBER_USE_DER );
1575 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1578 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1579 exit( EXIT_FAILURE );
1582 if ( chainingContinuation > -1 ) {
1583 err = ber_printf( ber, "e", chainingContinuation );
1586 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1587 exit( EXIT_FAILURE );
1591 err = ber_printf( ber, /* { */ "N}" );
1594 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1595 exit( EXIT_FAILURE );
1598 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1599 exit( EXIT_FAILURE );
1603 BER_BVZERO( &c[i].ldctl_value );
1606 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1607 c[i].ldctl_iscritical = chaining > 1;
1611 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1613 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1614 if ( sessionTracking ) {
1615 if ( stValue.bv_val == NULL ) {
1616 char *ip = NULL, *name = NULL;
1617 struct berval id = { 0 };
1618 char namebuf[ HOST_NAME_MAX ];
1620 if ( gethostname( namebuf, sizeof( namebuf ) ) == 0 ) {
1622 struct in_addr addr;
1626 h = gethostbyname( name );
1628 AC_MEMCPY( &addr, h->h_addr, sizeof( addr ) );
1629 ip = inet_ntoa( addr );
1633 #ifdef HAVE_CYRUS_SASL
1634 if ( sasl_authz_id != NULL ) {
1635 ber_str2bv( sasl_authz_id, 0, 0, &id );
1637 } else if ( sasl_authc_id != NULL ) {
1638 ber_str2bv( sasl_authc_id, 0, 0, &id );
1641 #endif /* HAVE_CYRUS_SASL */
1642 if ( binddn != NULL ) {
1643 ber_str2bv( binddn, 0, 0, &id );
1646 ber_str2bv( "anonymous", 0, 0, &id );
1649 if ( ldap_create_session_tracking_value( ld,
1650 ip, name, "1.3.6.1.4.1.21008.108.63.1.3", &id,
1653 fprintf( stderr, _("Session tracking control encoding error!\n") );
1654 exit( EXIT_FAILURE );
1658 c[i].ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1659 c[i].ldctl_iscritical = 0;
1660 ber_dupbv( &c[i].ldctl_value, &stValue );
1664 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1667 ctrls[i++] = extra_c++;
1669 for ( count = 0; count < unknown_ctrls_num; count++ ) {
1670 ctrls[i++] = &unknown_ctrls[count];
1674 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1676 if ( err != LDAP_OPT_SUCCESS ) {
1677 for ( j = 0; j < i; j++ ) {
1678 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1680 fprintf( stderr, "Could not set %scontrols\n",
1681 crit ? "critical " : "" );
1686 exit( EXIT_FAILURE );
1691 tool_check_abandon( LDAP *ld, int msgid )
1695 switch ( gotintr ) {
1696 case LDAP_REQ_EXTENDED:
1697 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1698 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1699 rc, ldap_err2string( rc ) );
1702 case LDAP_REQ_ABANDON:
1703 rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
1704 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1705 rc, ldap_err2string( rc ) );
1709 /* just unbind, ignoring the request */
1717 print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
1722 tool_write_ldif( LDIF_PUT_COMMENT, "==> ",
1723 what->bv_val, what->bv_len );
1724 ber = ber_init( &ctrl->ldctl_value );
1725 if ( ber == NULL ) {
1729 } else if ( ber_scanf( ber, "{m{" /*}}*/, &bv ) == LBER_ERROR ) {
1734 tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1736 while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
1738 BerVarray vals = NULL;
1740 if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
1747 for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
1749 ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1750 bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
1753 ber_bvarray_free( vals );
1757 if ( ber != NULL ) {
1761 tool_write_ldif( LDIF_PUT_COMMENT, "<== ",
1762 what->bv_val, what->bv_len );
1768 print_preread( LDAP *ld, LDAPControl *ctrl )
1770 static struct berval what = BER_BVC( "preread" );
1772 return print_prepostread( ld, ctrl, &what );
1776 print_postread( LDAP *ld, LDAPControl *ctrl )
1778 static struct berval what = BER_BVC( "postread" );
1780 return print_prepostread( ld, ctrl, &what );
1784 print_paged_results( LDAP *ld, LDAPControl *ctrl )
1788 /* note: pr_cookie is being malloced; it's freed
1789 * the next time the control is sent, but the last
1790 * time it's not; we don't care too much, because
1791 * the last time an empty value is returned... */
1792 if ( ldap_parse_pageresponse_control( ld, ctrl, &estimate, &pr_cookie )
1799 /* FIXME: check buffer overflow */
1800 char buf[ BUFSIZ ], *ptr = buf;
1802 if ( estimate > 0 ) {
1803 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1804 "estimate=%d", estimate );
1807 if ( pr_cookie.bv_len > 0 ) {
1810 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
1811 pr_cookie.bv_len ) + 1;
1812 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
1814 bv.bv_len = lutil_b64_ntop(
1815 (unsigned char *) pr_cookie.bv_val,
1817 bv.bv_val, bv.bv_len );
1819 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1820 "%scookie=%s", ptr == buf ? "" : " ",
1823 ber_memfree( bv.bv_val );
1825 pr_morePagedResults = 1;
1828 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1829 "%scookie=", ptr == buf ? "" : " " );
1832 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1833 "pagedresults", buf, ptr - buf );
1839 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1841 print_ppolicy( LDAP *ld, LDAPControl *ctrl )
1843 int expire = 0, grace = 0, rc;
1844 LDAPPasswordPolicyError pperr;
1846 rc = ldap_parse_passwordpolicy_control( ld, ctrl,
1847 &expire, &grace, &pperr );
1848 if ( rc == LDAP_SUCCESS ) {
1849 char buf[ BUFSIZ ], *ptr = buf;
1851 if ( expire != -1 ) {
1852 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1853 "expire=%d", expire );
1856 if ( grace != -1 ) {
1857 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1858 "%sgrace=%d", ptr == buf ? "" : " ", grace );
1861 if ( pperr != PP_noError ) {
1862 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1863 "%serror=%d (%s)", ptr == buf ? "" : " ",
1865 ldap_passwordpolicy_err2txt( pperr ) );
1868 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1869 "ppolicy", buf, ptr - buf );
1876 void tool_print_ctrls(
1878 LDAPControl **ctrls )
1883 for ( i = 0; ctrls[i] != NULL; i++ ) {
1884 /* control: OID criticality base64value */
1885 struct berval b64 = BER_BVNULL;
1890 /* FIXME: there might be cases where a control has NULL OID;
1891 * this makes little sense, especially when returned by the
1892 * server, but libldap happily allows it */
1893 if ( ctrls[i]->ldctl_oid == NULL ) {
1898 len += strlen( ctrls[i]->ldctl_oid );
1900 /* add enough for space after OID and the critical value itself */
1901 len += ctrls[i]->ldctl_iscritical
1902 ? sizeof("true") : sizeof("false");
1904 /* convert to base64 */
1905 if ( !BER_BVISNULL( &ctrls[i]->ldctl_value ) ) {
1906 b64.bv_len = LUTIL_BASE64_ENCODE_LEN(
1907 ctrls[i]->ldctl_value.bv_len ) + 1;
1908 b64.bv_val = ber_memalloc( b64.bv_len + 1 );
1910 b64.bv_len = lutil_b64_ntop(
1911 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
1912 ctrls[i]->ldctl_value.bv_len,
1913 b64.bv_val, b64.bv_len );
1917 len += 1 + b64.bv_len;
1920 ptr = str = malloc( len + 1 );
1922 ptr = lutil_strcopy( ptr, ": " );
1924 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_oid );
1925 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_iscritical
1926 ? " true" : " false" );
1929 ptr = lutil_strcopy( ptr, " " );
1930 ptr = lutil_strcopy( ptr, b64.bv_val );
1934 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1935 "control", str, len );
1940 ber_memfree( b64.bv_val );
1943 /* known controls */
1944 for ( j = 0; tool_ctrl_response[j].oid != NULL; j++ ) {
1945 if ( strcmp( tool_ctrl_response[j].oid, ctrls[i]->ldctl_oid ) == 0 ) {
1946 if ( !tool_ctrl_response[j].mask & tool_type ) {
1947 /* this control should not appear
1948 * with this tool; warning? */
1954 if ( tool_ctrl_response[j].oid != NULL && tool_ctrl_response[j].func ) {
1955 (void)tool_ctrl_response[j].func( ld, ctrls[i] );
1961 tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
1965 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1969 fputs( ldif, stdout );
1970 ber_memfree( ldif );
1976 tool_is_oid( const char *s )
1980 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
1984 for ( ; s[ 0 ]; s++ ) {
1985 if ( s[ 0 ] == '.' ) {
1986 if ( s[ 1 ] == '\0' ) {
1993 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
1997 if ( first == 1 && s[ 0 ] == '0' && s[ 1 ] != '.' ) {