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;
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 },
152 enum { Intr_None = 0, Intr_Abandon, Intr_Cancel, Intr_Ignore };
153 static volatile sig_atomic_t gotintr, abcan;
156 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
158 st_value( LDAP *ld, struct berval *value )
160 char *ip = NULL, *name = NULL;
161 struct berval id = { 0 };
162 char namebuf[ MAXHOSTNAMELEN ];
164 if ( gethostname( namebuf, sizeof( namebuf ) ) == 0 ) {
170 h = gethostbyname( name );
172 AC_MEMCPY( &addr, h->h_addr, sizeof( addr ) );
173 ip = inet_ntoa( addr );
177 #ifdef HAVE_CYRUS_SASL
178 if ( sasl_authz_id != NULL ) {
179 ber_str2bv( sasl_authz_id, 0, 0, &id );
181 } else if ( sasl_authc_id != NULL ) {
182 ber_str2bv( sasl_authc_id, 0, 0, &id );
185 #endif /* HAVE_CYRUS_SASL */
186 if ( binddn != NULL ) {
187 ber_str2bv( binddn, 0, 0, &id );
190 if ( ldap_create_session_tracking_value( ld,
191 ip, name, LDAP_CONTROL_X_SESSION_TRACKING_USERNAME,
194 fprintf( stderr, _("Session tracking control encoding error!\n") );
200 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
209 tool_init( tool_type_t type )
212 ldap_pvt_setlocale(LC_MESSAGES, "");
213 ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
214 ldap_pvt_textdomain(OPENLDAP_PACKAGE);
220 #ifdef HAVE_CYRUS_SASL
224 ldap_pvt_tls_destroy();
227 if ( ldapuri != NULL ) {
228 ber_memfree( ldapuri );
232 if ( pr_cookie.bv_val != NULL ) {
233 ber_memfree( pr_cookie.bv_val );
234 pr_cookie.bv_val = NULL;
235 pr_cookie.bv_len = 0;
240 tool_common_usage( void )
242 static const char *const descriptions[] = {
243 N_(" -c continuous operation mode (do not stop on errors)\n"),
244 N_(" -d level set LDAP debugging level to `level'\n"),
245 N_(" -D binddn bind DN\n"),
246 N_(" -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
247 N_(" [!]assert=<filter> (a RFC 4515 Filter string)\n")
248 N_(" [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n")
249 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
251 /* non-advertized support for proxyDN */
252 N_(" [!]proxydn=<dn> (a RFC 4514 DN string)\n")
255 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
256 N_(" [!]chaining[=<resolveBehavior>[/<continuationBehavior>]]\n")
257 N_(" one of \"chainingPreferred\", \"chainingRequired\",\n")
258 N_(" \"referralsPreferred\", \"referralsRequired\"\n")
259 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
260 N_(" [!]manageDSAit\n")
262 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
265 N_(" [!]postread[=<attrs>] (a comma-separated attribute list)\n")
266 N_(" [!]preread[=<attrs>] (a comma-separated attribute list)\n")
268 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
269 N_(" [!]sessiontracking\n")
270 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
271 N_(" abandon, cancel, ignore (SIGINT sends abandon/cancel,\n"
272 " or ignores response; if critical, doesn't wait for SIGINT.\n"
273 " not really controls)\n")
274 N_(" -f file read operations from `file'\n"),
275 N_(" -h host LDAP server\n"),
276 N_(" -H URI LDAP Uniform Resource Identifier(s)\n"),
277 N_(" -I use SASL Interactive mode\n"),
278 N_(" -M enable Manage DSA IT control (-MM to make critical)\n"),
279 N_(" -n show what would be done but don't actually do it\n"),
280 N_(" -O props SASL security properties\n"),
281 N_(" -o <opt>[=<optparam] general options\n"),
282 N_(" nettimeout=<timeout> (in seconds, or \"none\" or \"max\")\n"),
283 N_(" -p port port on LDAP server\n"),
284 N_(" -P version protocol version (default: 3)\n"),
285 N_(" -Q use SASL Quiet mode\n"),
286 N_(" -R realm SASL realm\n"),
287 N_(" -U authcid SASL authentication identity\n"),
288 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
289 N_(" -V print version info (-VV only)\n"),
290 N_(" -w passwd bind password (for simple authentication)\n"),
291 N_(" -W prompt for bind password\n"),
292 N_(" -x Simple authentication\n"),
293 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
294 N_(" -y file Read password from file\n"),
295 N_(" -Y mech SASL mechanism\n"),
296 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
299 const char *const *cpp;
301 fputs( _("Common options:\n"), stderr );
302 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
303 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
304 fputs( _(*cpp), stderr );
317 fprintf( stderr, "%s: %s (%d)%s\n",
318 func, ldap_err2string( err ), err, extra ? extra : "" );
320 if ( matched && *matched ) {
321 fprintf( stderr, _("\tmatched DN: %s\n"), matched );
324 if ( info && *info ) {
325 fprintf( stderr, _("\tadditional info: %s\n"), info );
328 if ( refs && *refs ) {
330 fprintf( stderr, _("\treferrals:\n") );
331 for( i=0; refs[i]; i++ ) {
332 fprintf( stderr, "\t\t%s\n", refs[i] );
339 tool_args( int argc, char **argv )
343 while (( i = getopt( argc, argv, options )) != EOF ) {
345 char *control, *cvalue, *next;
347 case 'c': /* continuous operation mode */
354 ival = strtol( optarg, &next, 10 );
355 if (next == NULL || next[0] != '\0') {
356 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
361 case 'D': /* bind DN */
362 if( binddn != NULL ) {
363 fprintf( stderr, "%s: -D previously specified\n", prog );
364 exit( EXIT_FAILURE );
366 binddn = ber_strdup( optarg );
368 case 'e': /* general extensions (controls and such) */
369 /* should be extended to support comma separated list of
370 * [!]key[=value] parameters, e.g. -e !foo,bar=567
375 if( optarg[0] == '!' ) {
380 control = ber_strdup( optarg );
381 if ( (cvalue = strchr( control, '=' )) != NULL ) {
385 if ( strcasecmp( control, "assert" ) == 0 ) {
387 fprintf( stderr, "assert control previously specified\n");
388 exit( EXIT_FAILURE );
390 if( cvalue == NULL ) {
391 fprintf( stderr, "assert: control value expected\n" );
395 assertctl = 1 + crit;
397 assert( assertion == NULL );
400 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
401 if( authzid != NULL ) {
402 fprintf( stderr, "authzid control previously specified\n");
403 exit( EXIT_FAILURE );
405 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
406 if( proxydn != NULL ) {
407 fprintf( stderr, "authzid control incompatible with proxydn\n");
408 exit( EXIT_FAILURE );
410 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
411 if( cvalue == NULL ) {
412 fprintf( stderr, "authzid: control value expected\n" );
416 fprintf( stderr, "authzid: must be marked critical\n" );
420 assert( authzid == NULL );
423 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
424 } else if ( strcasecmp( control, "proxydn" ) == 0 ) {
425 if( proxydn != NULL ) {
426 fprintf( stderr, "proxydn control previously specified\n");
427 exit( EXIT_FAILURE );
429 if( authzid != NULL ) {
430 fprintf( stderr, "proxydn control incompatible with authzid\n");
431 exit( EXIT_FAILURE );
433 if( cvalue == NULL ) {
434 fprintf( stderr, "proxydn: control value expected\n" );
438 fprintf( stderr, "proxydn: must be marked critical\n" );
442 assert( proxydn == NULL );
444 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
446 } else if ( ( strcasecmp( control, "relax" ) == 0 ) ||
447 ( strcasecmp( control, "manageDIT" ) == 0 ) )
451 "relax control previously specified\n");
452 exit( EXIT_FAILURE );
454 if( cvalue != NULL ) {
456 "relax: no control value expected\n" );
460 manageDIT = 1 + crit;
462 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
465 "manageDSAit control previously specified\n");
466 exit( EXIT_FAILURE );
468 if( cvalue != NULL ) {
470 "manageDSAit: no control value expected\n" );
474 manageDSAit = 1 + crit;
476 } else if ( strcasecmp( control, "noop" ) == 0 ) {
478 fprintf( stderr, "noop control previously specified\n");
479 exit( EXIT_FAILURE );
481 if( cvalue != NULL ) {
482 fprintf( stderr, "noop: no control value expected\n" );
488 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
489 } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
491 fprintf( stderr, "ppolicy control previously specified\n");
492 exit( EXIT_FAILURE );
494 if( cvalue != NULL ) {
495 fprintf( stderr, "ppolicy: no control value expected\n" );
499 fprintf( stderr, "ppolicy: critical flag not allowed\n" );
506 } else if ( strcasecmp( control, "preread" ) == 0 ) {
508 fprintf( stderr, "preread control previously specified\n");
509 exit( EXIT_FAILURE );
513 preread_attrs = cvalue;
515 } else if ( strcasecmp( control, "postread" ) == 0 ) {
517 fprintf( stderr, "postread control previously specified\n");
518 exit( EXIT_FAILURE );
522 postread_attrs = cvalue;
524 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
525 } else if ( strcasecmp( control, "chaining" ) == 0 ) {
528 if ( cvalue != NULL ) {
531 continuation = strchr( cvalue, '/' );
532 if ( continuation ) {
533 /* FIXME: this makes sense only in searches */
534 *continuation++ = '\0';
535 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
536 chainingContinuation = LDAP_CHAINING_PREFERRED;
537 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
538 chainingContinuation = LDAP_CHAINING_REQUIRED;
539 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
540 chainingContinuation = LDAP_REFERRALS_PREFERRED;
541 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
542 chainingContinuation = LDAP_REFERRALS_REQUIRED;
545 "chaining behavior control "
546 "continuation value \"%s\" invalid\n",
548 exit( EXIT_FAILURE );
552 if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
553 chainingResolve = LDAP_CHAINING_PREFERRED;
554 } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
555 chainingResolve = LDAP_CHAINING_REQUIRED;
556 } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
557 chainingResolve = LDAP_REFERRALS_PREFERRED;
558 } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
559 chainingResolve = LDAP_REFERRALS_REQUIRED;
562 "chaining behavior control "
563 "resolve value \"%s\" invalid\n",
565 exit( EXIT_FAILURE );
568 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
570 /* this shouldn't go here, really; but it's a feature... */
571 } else if ( strcasecmp( control, "abandon" ) == 0 ) {
572 abcan = Intr_Abandon;
577 } else if ( strcasecmp( control, "cancel" ) == 0 ) {
583 } else if ( strcasecmp( control, "ignore" ) == 0 ) {
589 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
590 } else if ( strcasecmp( control, "sessiontracking" ) == 0 ) {
591 if ( sessionTracking ) {
592 fprintf( stderr, "%s: session tracking can be only specified once\n", prog );
593 exit( EXIT_FAILURE );
596 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
598 } else if ( tool_is_oid( control ) ) {
599 LDAPControl *tmpctrls, ctrl;
601 tmpctrls = (LDAPControl *)realloc( unknown_ctrls,
602 (unknown_ctrls_num + 1)*sizeof( LDAPControl ) );
603 if ( tmpctrls == NULL ) {
604 fprintf( stderr, "%s: no memory?\n", prog );
605 exit( EXIT_FAILURE );
607 unknown_ctrls = tmpctrls;
608 ctrl.ldctl_oid = control;
609 ctrl.ldctl_value.bv_val = NULL;
610 ctrl.ldctl_value.bv_len = 0;
611 ctrl.ldctl_iscritical = crit;
613 if ( cvalue != NULL ) {
615 size_t len = strlen( cvalue );
618 bv.bv_len = LUTIL_BASE64_DECODE_LEN( len );
619 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
621 retcode = lutil_b64_pton( cvalue,
622 (unsigned char *)bv.bv_val,
625 if ( retcode == -1 || retcode > bv.bv_len ) {
626 fprintf( stderr, "Unable to parse value of general control %s\n",
632 ctrl.ldctl_value = bv;
635 unknown_ctrls[ unknown_ctrls_num ] = ctrl;
639 fprintf( stderr, "Invalid general control name: %s\n",
644 case 'f': /* read from file */
645 if( infile != NULL ) {
646 fprintf( stderr, "%s: -f previously specified\n", prog );
647 exit( EXIT_FAILURE );
649 infile = ber_strdup( optarg );
651 case 'h': /* ldap host */
652 if( ldaphost != NULL ) {
653 fprintf( stderr, "%s: -h previously specified\n", prog );
654 exit( EXIT_FAILURE );
656 ldaphost = ber_strdup( optarg );
658 case 'H': /* ldap URI */
659 if( ldapuri != NULL ) {
660 fprintf( stderr, "%s: -H previously specified\n", prog );
661 exit( EXIT_FAILURE );
663 ldapuri = ber_strdup( optarg );
666 #ifdef HAVE_CYRUS_SASL
667 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
668 fprintf( stderr, "%s: incompatible previous "
669 "authentication choice\n",
671 exit( EXIT_FAILURE );
673 authmethod = LDAP_AUTH_SASL;
674 sasl_flags = LDAP_SASL_INTERACTIVE;
677 fprintf( stderr, "%s: was not compiled with SASL support\n",
679 exit( EXIT_FAILURE );
682 /* enable Manage DSA IT */
685 case 'n': /* print operations, don't actually do them */
689 control = ber_strdup( optarg );
690 if ( (cvalue = strchr( control, '=' )) != NULL ) {
694 if ( strcasecmp( control, "nettimeout" ) == 0 ) {
695 if( nettimeout.tv_sec != -1 ) {
696 fprintf( stderr, "nettimeout option previously specified\n");
697 exit( EXIT_FAILURE );
699 if( cvalue == NULL || cvalue[0] == '\0' ) {
700 fprintf( stderr, "nettimeout: option value expected\n" );
703 if ( strcasecmp( cvalue, "none" ) == 0 ) {
704 nettimeout.tv_sec = 0;
705 } else if ( strcasecmp( cvalue, "max" ) == 0 ) {
706 nettimeout.tv_sec = LDAP_MAXINT;
708 ival = strtol( cvalue, &next, 10 );
709 if ( next == NULL || next[0] != '\0' ) {
711 _("Unable to parse network timeout \"%s\"\n"), cvalue );
712 exit( EXIT_FAILURE );
714 nettimeout.tv_sec = ival;
716 if( nettimeout.tv_sec < 0 || nettimeout.tv_sec > LDAP_MAXINT ) {
717 fprintf( stderr, _("%s: invalid network timeout (%ld) specified\n"),
718 prog, (long)nettimeout.tv_sec );
719 exit( EXIT_FAILURE );
722 fprintf( stderr, "Invalid general option name: %s\n",
728 #ifdef HAVE_CYRUS_SASL
729 if( sasl_secprops != NULL ) {
730 fprintf( stderr, "%s: -O previously specified\n", prog );
731 exit( EXIT_FAILURE );
733 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
734 fprintf( stderr, "%s: incompatible previous "
735 "authentication choice\n", prog );
736 exit( EXIT_FAILURE );
738 authmethod = LDAP_AUTH_SASL;
739 sasl_secprops = ber_strdup( optarg );
741 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
742 exit( EXIT_FAILURE );
747 fprintf( stderr, "%s: -p previously specified\n", prog );
748 exit( EXIT_FAILURE );
750 ival = strtol( optarg, &next, 10 );
751 if ( next == NULL || next[0] != '\0' ) {
752 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
753 exit( EXIT_FAILURE );
758 ival = strtol( optarg, &next, 10 );
759 if ( next == NULL || next[0] != '\0' ) {
760 fprintf( stderr, "%s: unable to parse protocol version \"%s\"\n", prog, optarg );
761 exit( EXIT_FAILURE );
765 if( protocol == LDAP_VERSION3 ) {
766 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
768 exit( EXIT_FAILURE );
770 protocol = LDAP_VERSION2;
773 if( protocol == LDAP_VERSION2 ) {
774 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
776 exit( EXIT_FAILURE );
778 protocol = LDAP_VERSION3;
781 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
787 #ifdef HAVE_CYRUS_SASL
788 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
789 fprintf( stderr, "%s: incompatible previous "
790 "authentication choice\n",
792 exit( EXIT_FAILURE );
794 authmethod = LDAP_AUTH_SASL;
795 sasl_flags = LDAP_SASL_QUIET;
798 fprintf( stderr, "%s: not compiled with SASL support\n",
800 exit( EXIT_FAILURE );
803 #ifdef HAVE_CYRUS_SASL
804 if( sasl_realm != NULL ) {
805 fprintf( stderr, "%s: -R previously specified\n", prog );
806 exit( EXIT_FAILURE );
808 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
809 fprintf( stderr, "%s: incompatible previous "
810 "authentication choice\n",
812 exit( EXIT_FAILURE );
814 authmethod = LDAP_AUTH_SASL;
815 sasl_realm = ber_strdup( optarg );
817 fprintf( stderr, "%s: not compiled with SASL support\n",
819 exit( EXIT_FAILURE );
823 #ifdef HAVE_CYRUS_SASL
824 if( sasl_authc_id != NULL ) {
825 fprintf( stderr, "%s: -U previously specified\n", prog );
826 exit( EXIT_FAILURE );
828 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
829 fprintf( stderr, "%s: incompatible previous "
830 "authentication choice\n",
832 exit( EXIT_FAILURE );
834 authmethod = LDAP_AUTH_SASL;
835 sasl_authc_id = ber_strdup( optarg );
837 fprintf( stderr, "%s: not compiled with SASL support\n",
839 exit( EXIT_FAILURE );
842 case 'v': /* verbose mode */
845 case 'V': /* version */
848 case 'w': /* password */
849 passwd.bv_val = ber_strdup( optarg );
853 for( p = optarg; *p != '\0'; p++ ) {
857 passwd.bv_len = strlen( passwd.bv_val );
866 #ifdef HAVE_CYRUS_SASL
867 if( sasl_mech != NULL ) {
868 fprintf( stderr, "%s: -Y previously specified\n", prog );
869 exit( EXIT_FAILURE );
871 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
873 "%s: incompatible with authentication choice\n", prog );
874 exit( EXIT_FAILURE );
876 authmethod = LDAP_AUTH_SASL;
877 sasl_mech = ber_strdup( optarg );
879 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
880 exit( EXIT_FAILURE );
884 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
885 fprintf( stderr, "%s: incompatible with previous "
886 "authentication choice\n", prog );
887 exit( EXIT_FAILURE );
889 authmethod = LDAP_AUTH_SIMPLE;
892 #ifdef HAVE_CYRUS_SASL
893 if( sasl_authz_id != NULL ) {
894 fprintf( stderr, "%s: -X previously specified\n", prog );
895 exit( EXIT_FAILURE );
897 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
898 fprintf( stderr, "%s: -X incompatible with "
899 "authentication choice\n", prog );
900 exit( EXIT_FAILURE );
902 authmethod = LDAP_AUTH_SASL;
903 sasl_authz_id = ber_strdup( optarg );
905 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
906 exit( EXIT_FAILURE );
913 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
914 exit( EXIT_FAILURE );
918 if( handle_private_option( i ) ) break;
919 fprintf( stderr, "%s: unrecognized option -%c\n",
926 /* prevent bad linking */
928 api.ldapai_info_version = LDAP_API_INFO_VERSION;
930 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
931 != LDAP_OPT_SUCCESS )
933 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
934 exit( EXIT_FAILURE );
937 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
938 fprintf( stderr, "LDAP APIInfo version mismatch: "
939 "library %d, header %d\n",
940 api.ldapai_info_version, LDAP_API_INFO_VERSION );
941 exit( EXIT_FAILURE );
944 if( api.ldapai_api_version != LDAP_API_VERSION ) {
945 fprintf( stderr, "LDAP API version mismatch: "
946 "library %d, header %d\n",
947 api.ldapai_api_version, LDAP_API_VERSION );
948 exit( EXIT_FAILURE );
951 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
952 fprintf( stderr, "LDAP vendor name mismatch: "
953 "library %s, header %s\n",
954 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
955 exit( EXIT_FAILURE );
958 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
959 fprintf( stderr, "LDAP vendor version mismatch: "
960 "library %d, header %d\n",
961 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
962 exit( EXIT_FAILURE );
966 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
968 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
969 if (version > 1) exit( EXIT_SUCCESS );
972 ldap_memfree( api.ldapai_vendor_name );
973 ber_memvfree( (void **)api.ldapai_extensions );
977 protocol = LDAP_VERSION3;
979 if (authmethod == -1 && protocol > LDAP_VERSION2) {
980 #ifdef HAVE_CYRUS_SASL
981 authmethod = LDAP_AUTH_SASL;
983 authmethod = LDAP_AUTH_SIMPLE;
987 if( ldapuri == NULL ) {
988 if( ldapport && ( ldaphost == NULL )) {
989 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
990 exit( EXIT_FAILURE );
993 if( ldaphost != NULL ) {
994 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
995 exit( EXIT_FAILURE );
998 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
999 exit( EXIT_FAILURE );
1003 if( protocol == LDAP_VERSION2 ) {
1004 if( assertctl || authzid || manageDIT || manageDSAit ||
1005 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1007 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1008 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1011 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1013 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1014 noop || ppolicy || preread || postread )
1016 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
1017 exit( EXIT_FAILURE );
1021 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
1022 exit( EXIT_FAILURE );
1025 #ifdef HAVE_CYRUS_SASL
1026 if( authmethod == LDAP_AUTH_SASL ) {
1027 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
1029 exit( EXIT_FAILURE );
1037 tool_conn_setup( int dont, void (*private_setup)( LDAP * ) )
1042 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
1043 != LBER_OPT_SUCCESS )
1046 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
1048 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
1049 != LDAP_OPT_SUCCESS )
1052 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
1057 (void) SIGNAL( SIGPIPE, SIG_IGN );
1061 SIGNAL( SIGINT, do_sig );
1067 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
1070 memset( &url, 0, sizeof(url));
1072 url.lud_scheme = "ldap";
1073 url.lud_host = ldaphost;
1074 url.lud_port = ldapport;
1075 url.lud_scope = LDAP_SCOPE_DEFAULT;
1077 ldapuri = ldap_url_desc2str( &url );
1079 } else if ( ldapuri != NULL ) {
1080 LDAPURLDesc *ludlist, **ludp;
1084 rc = ldap_url_parselist( &ludlist, ldapuri );
1085 if ( rc != LDAP_URL_SUCCESS ) {
1087 "Could not parse LDAP URI(s)=%s (%d)\n",
1089 exit( EXIT_FAILURE );
1092 for ( ludp = &ludlist; *ludp != NULL; ) {
1093 LDAPURLDesc *lud = *ludp;
1096 if ( lud->lud_dn != NULL && lud->lud_dn[ 0 ] != '\0' &&
1097 ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) )
1099 /* if no host but a DN is provided,
1100 * use DNS SRV to gather the host list
1101 * and turn it into a list of URIs
1102 * using the scheme provided */
1103 char *domain = NULL,
1107 len_proto = strlen( lud->lud_scheme );
1109 if ( ldap_dn2domain( lud->lud_dn, &domain )
1113 "DNS SRV: Could not turn "
1114 "DN=\"%s\" into a domain\n",
1119 rc = ldap_domain2hostlist( domain, &hostlist );
1122 "DNS SRV: Could not turn "
1123 "domain=%s into a hostlist\n",
1128 hosts = ldap_str2charray( hostlist, " " );
1129 if ( hosts == NULL ) {
1131 "DNS SRV: Could not parse "
1132 "hostlist=\"%s\"\n",
1137 for ( i = 0; hosts[ i ] != NULL; i++ )
1140 tmp = (char **)realloc( urls, sizeof( char * ) * ( nurls + i + 1 ) );
1141 if ( tmp == NULL ) {
1143 "DNS SRV: out of memory?\n" );
1147 urls[ nurls ] = NULL;
1149 for ( i = 0; hosts[ i ] != NULL; i++ ) {
1150 size_t len = len_proto
1152 + strlen( hosts[ i ] )
1155 urls[ nurls + i + 1 ] = NULL;
1156 urls[ nurls + i ] = (char *)malloc( sizeof( char ) * len );
1157 if ( urls[ nurls + i ] == NULL ) {
1159 "DNS SRV: out of memory?\n" );
1163 snprintf( urls[ nurls + i ], len, "%s://%s",
1164 lud->lud_scheme, hosts[ i ] );
1169 ber_memvfree( (void **)hosts );
1170 ber_memfree( hostlist );
1171 ber_memfree( domain );
1174 tmp = (char **)realloc( urls, sizeof( char * ) * ( nurls + 2 ) );
1175 if ( tmp == NULL ) {
1177 "DNS SRV: out of memory?\n" );
1181 urls[ nurls + 1 ] = NULL;
1183 urls[ nurls ] = ldap_url_desc2str( lud );
1184 if ( urls[ nurls ] == NULL ) {
1186 "DNS SRV: out of memory?\n" );
1192 *ludp = lud->lud_next;
1194 lud->lud_next = NULL;
1195 ldap_free_urldesc( lud );
1198 if ( ludlist != NULL ) {
1199 ldap_free_urllist( ludlist );
1200 exit( EXIT_FAILURE );
1202 } else if ( urls == NULL ) {
1203 exit( EXIT_FAILURE );
1206 ldap_memfree( ldapuri );
1207 ldapuri = ldap_charray2str( urls, " " );
1208 ber_memvfree( (void **)urls );
1212 fprintf( stderr, "ldap_initialize( %s )\n",
1213 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
1215 rc = ldap_initialize( &ld, ldapuri );
1216 if( rc != LDAP_SUCCESS ) {
1218 "Could not create LDAP session handle for URI=%s (%d): %s\n",
1219 ldapuri, rc, ldap_err2string(rc) );
1220 exit( EXIT_FAILURE );
1223 if( private_setup ) private_setup( ld );
1226 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
1227 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
1229 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
1230 referrals ? "on" : "off" );
1231 exit( EXIT_FAILURE );
1234 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
1235 != LDAP_OPT_SUCCESS )
1237 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
1239 exit( EXIT_FAILURE );
1243 rc = ldap_start_tls_s( ld, NULL, NULL );
1244 if ( rc != LDAP_SUCCESS ) {
1245 tool_perror( "ldap_start_tls", rc, NULL, NULL, NULL, NULL );
1246 if ( use_tls > 1 ) {
1247 exit( EXIT_FAILURE );
1252 if ( nettimeout.tv_sec > 0 ) {
1253 if ( ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &nettimeout )
1254 != LDAP_OPT_SUCCESS )
1256 fprintf( stderr, "Could not set LDAP_OPT_NETWORK_TIMEOUT %ld\n",
1257 (long)nettimeout.tv_sec );
1258 exit( EXIT_FAILURE );
1268 tool_bind( LDAP *ld )
1270 LDAPControl **sctrlsp = NULL;
1271 LDAPControl *sctrls[3];
1272 LDAPControl sctrl[3];
1275 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1278 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1279 c.ldctl_value.bv_val = NULL;
1280 c.ldctl_value.bv_len = 0;
1281 c.ldctl_iscritical = 0;
1283 sctrls[nsctrls] = &sctrl[nsctrls];
1284 sctrls[++nsctrls] = NULL;
1288 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1289 if ( sessionTracking ) {
1292 if (stValue.bv_val == NULL && st_value( ld, &stValue ) ) {
1293 exit( EXIT_FAILURE );
1296 c.ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1297 c.ldctl_iscritical = 0;
1298 ber_dupbv( &c.ldctl_value, &stValue );
1301 sctrls[nsctrls] = &sctrl[nsctrls];
1302 sctrls[++nsctrls] = NULL;
1304 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1310 assert( nsctrls < sizeof(sctrls)/sizeof(sctrls[0]) );
1312 if ( authmethod == LDAP_AUTH_SASL ) {
1313 #ifdef HAVE_CYRUS_SASL
1317 if( sasl_secprops != NULL ) {
1318 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
1319 (void *) sasl_secprops );
1321 if( rc != LDAP_OPT_SUCCESS ) {
1323 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
1325 exit( LDAP_LOCAL_ERROR );
1329 defaults = lutil_sasl_defaults( ld,
1336 rc = ldap_sasl_interactive_bind_s( ld, binddn, sasl_mech,
1338 NULL, sasl_flags, lutil_sasl_interact, defaults );
1340 lutil_sasl_freedefs( defaults );
1341 if( rc != LDAP_SUCCESS ) {
1342 tool_perror( "ldap_sasl_interactive_bind_s",
1343 rc, NULL, NULL, NULL, NULL );
1347 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1348 exit( LDAP_NOT_SUPPORTED );
1352 LDAPMessage *result;
1353 LDAPControl **ctrls;
1355 char *matched = NULL;
1363 rc = ldap_sasl_bind( ld, binddn, LDAP_SASL_SIMPLE, &passwd,
1364 sctrlsp, NULL, &msgid );
1365 if ( msgid == -1 ) {
1366 tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
1367 NULL, NULL, NULL, NULL );
1372 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 ) {
1373 tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
1374 exit( LDAP_LOCAL_ERROR );
1377 rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
1379 if ( rc != LDAP_SUCCESS ) {
1380 tool_perror( "ldap_bind parse result", rc, NULL, matched, info, refs );
1381 exit( LDAP_LOCAL_ERROR );
1384 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1385 if ( ctrls && ppolicy ) {
1387 int expire, grace, len = 0;
1388 LDAPPasswordPolicyError pErr = -1;
1390 ctrl = ldap_control_find( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
1393 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
1394 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1396 if ( pErr != PP_noError ){
1399 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1400 len = strlen( msgbuf );
1402 if ( expire >= 0 ) {
1403 sprintf( msgbuf+len,
1404 " (Password expires in %d seconds)",
1406 } else if ( grace >= 0 ) {
1407 sprintf( msgbuf+len,
1408 " (Password expired, %d grace logins remain)",
1416 ldap_controls_free( ctrls );
1419 if ( err != LDAP_SUCCESS
1421 || ( matched && matched[ 0 ] )
1422 || ( info && info[ 0 ] )
1425 tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1427 if( matched ) ber_memfree( matched );
1428 if( info ) ber_memfree( info );
1429 if( refs ) ber_memvfree( (void **)refs );
1431 if ( err != LDAP_SUCCESS ) exit( err );
1437 tool_unbind( LDAP *ld )
1439 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1441 if ( err != LDAP_OPT_SUCCESS ) {
1442 fprintf( stderr, "Could not unset controls\n");
1445 (void) ldap_unbind_ext( ld, NULL, NULL );
1449 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1451 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1453 int i = 0, j, crit = 0, err;
1454 LDAPControl c[16], **ctrls;
1458 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1460 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1464 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1469 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1471 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1472 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1474 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1476 || unknown_ctrls_num ) )
1481 ctrls = (LDAPControl**) malloc(sizeof(c) + (count + unknown_ctrls_num + 1)*sizeof(LDAPControl*));
1482 if ( ctrls == NULL ) {
1483 fprintf( stderr, "No memory\n" );
1484 exit( EXIT_FAILURE );
1488 BerElementBuffer berbuf;
1489 BerElement *ber = (BerElement *)&berbuf;
1491 if( assertion == NULL || *assertion == '\0' ) {
1492 fprintf( stderr, "Assertion=<empty>\n" );
1493 exit( EXIT_FAILURE );
1496 ber_init2( ber, NULL, LBER_USE_DER );
1498 err = ldap_pvt_put_filter( ber, assertion );
1500 fprintf( stderr, "assertion encode failed (%d)\n", err );
1501 exit( EXIT_FAILURE );
1504 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1506 fprintf( stderr, "assertion flatten failed (%d)\n", err );
1507 exit( EXIT_FAILURE );
1510 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1511 c[i].ldctl_iscritical = assertctl > 1;
1517 c[i].ldctl_value.bv_val = authzid;
1518 c[i].ldctl_value.bv_len = strlen( authzid );
1519 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1520 c[i].ldctl_iscritical = 1;
1525 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1526 /* NOTE: doesn't need an extra count because it's incompatible
1529 BerElementBuffer berbuf;
1530 BerElement *ber = (BerElement *)&berbuf;
1532 ber_init2( ber, NULL, LBER_USE_DER );
1534 if ( ber_printf( ber, "s", proxydn ) == LBER_ERROR ) {
1535 exit( EXIT_FAILURE );
1538 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1539 exit( EXIT_FAILURE );
1542 c[i].ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1543 c[i].ldctl_iscritical = 1;
1547 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1550 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1551 BER_BVZERO( &c[i].ldctl_value );
1552 c[i].ldctl_iscritical = manageDIT > 1;
1557 if ( manageDSAit ) {
1558 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1559 BER_BVZERO( &c[i].ldctl_value );
1560 c[i].ldctl_iscritical = manageDSAit > 1;
1566 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1567 BER_BVZERO( &c[i].ldctl_value );
1568 c[i].ldctl_iscritical = noop > 1;
1573 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1575 c[i].ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1576 BER_BVZERO( &c[i].ldctl_value );
1577 c[i].ldctl_iscritical = 0;
1584 char berbuf[LBER_ELEMENT_SIZEOF];
1585 BerElement *ber = (BerElement *)berbuf;
1586 char **attrs = NULL;
1588 if( preread_attrs ) {
1589 attrs = ldap_str2charray( preread_attrs, "," );
1592 ber_init2( ber, NULL, LBER_USE_DER );
1594 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1595 fprintf( stderr, "preread attrs encode failed.\n" );
1596 exit( EXIT_FAILURE );
1599 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1601 fprintf( stderr, "preread flatten failed (%d)\n", err );
1602 exit( EXIT_FAILURE );
1605 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1606 c[i].ldctl_iscritical = preread > 1;
1610 if( attrs ) ldap_charray_free( attrs );
1614 char berbuf[LBER_ELEMENT_SIZEOF];
1615 BerElement *ber = (BerElement *)berbuf;
1616 char **attrs = NULL;
1618 if( postread_attrs ) {
1619 attrs = ldap_str2charray( postread_attrs, "," );
1622 ber_init2( ber, NULL, LBER_USE_DER );
1624 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1625 fprintf( stderr, "postread attrs encode failed.\n" );
1626 exit( EXIT_FAILURE );
1629 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1631 fprintf( stderr, "postread flatten failed (%d)\n", err );
1632 exit( EXIT_FAILURE );
1635 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1636 c[i].ldctl_iscritical = postread > 1;
1640 if( attrs ) ldap_charray_free( attrs );
1643 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1645 if ( chainingResolve > -1 ) {
1646 BerElementBuffer berbuf;
1647 BerElement *ber = (BerElement *)&berbuf;
1649 ber_init2( ber, NULL, LBER_USE_DER );
1651 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1654 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1655 exit( EXIT_FAILURE );
1658 if ( chainingContinuation > -1 ) {
1659 err = ber_printf( ber, "e", chainingContinuation );
1662 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1663 exit( EXIT_FAILURE );
1667 err = ber_printf( ber, /* { */ "N}" );
1670 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1671 exit( EXIT_FAILURE );
1674 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1675 exit( EXIT_FAILURE );
1679 BER_BVZERO( &c[i].ldctl_value );
1682 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1683 c[i].ldctl_iscritical = chaining > 1;
1687 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1689 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1690 if ( sessionTracking ) {
1691 if ( stValue.bv_val == NULL && st_value( ld, &stValue ) ) {
1692 exit( EXIT_FAILURE );
1695 c[i].ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1696 c[i].ldctl_iscritical = 0;
1697 ber_dupbv( &c[i].ldctl_value, &stValue );
1702 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1705 ctrls[i++] = extra_c++;
1707 for ( count = 0; count < unknown_ctrls_num; count++ ) {
1708 ctrls[i++] = &unknown_ctrls[count];
1712 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1714 if ( err != LDAP_OPT_SUCCESS ) {
1715 for ( j = 0; j < i; j++ ) {
1716 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1718 fprintf( stderr, "Could not set %scontrols\n",
1719 crit ? "critical " : "" );
1724 exit( EXIT_FAILURE );
1729 tool_check_abandon( LDAP *ld, int msgid )
1733 switch ( gotintr ) {
1735 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1736 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1737 rc, ldap_err2string( rc ) );
1741 rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
1742 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1743 rc, ldap_err2string( rc ) );
1747 /* just unbind, ignoring the request */
1755 print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
1760 tool_write_ldif( LDIF_PUT_COMMENT, "==> ",
1761 what->bv_val, what->bv_len );
1762 ber = ber_init( &ctrl->ldctl_value );
1763 if ( ber == NULL ) {
1767 } else if ( ber_scanf( ber, "{m{" /*}}*/, &bv ) == LBER_ERROR ) {
1772 tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1774 while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
1776 BerVarray vals = NULL;
1778 if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
1785 for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
1787 ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1788 bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
1791 ber_bvarray_free( vals );
1795 if ( ber != NULL ) {
1799 tool_write_ldif( LDIF_PUT_COMMENT, "<== ",
1800 what->bv_val, what->bv_len );
1806 print_preread( LDAP *ld, LDAPControl *ctrl )
1808 static struct berval what = BER_BVC( "preread" );
1810 return print_prepostread( ld, ctrl, &what );
1814 print_postread( LDAP *ld, LDAPControl *ctrl )
1816 static struct berval what = BER_BVC( "postread" );
1818 return print_prepostread( ld, ctrl, &what );
1822 print_paged_results( LDAP *ld, LDAPControl *ctrl )
1826 /* note: pr_cookie is being malloced; it's freed
1827 * the next time the control is sent, but the last
1828 * time it's not; we don't care too much, because
1829 * the last time an empty value is returned... */
1830 if ( ldap_parse_pageresponse_control( ld, ctrl, &estimate, &pr_cookie )
1837 /* FIXME: check buffer overflow */
1838 char buf[ BUFSIZ ], *ptr = buf;
1840 if ( estimate > 0 ) {
1841 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1842 "estimate=%d", estimate );
1845 if ( pr_cookie.bv_len > 0 ) {
1848 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
1849 pr_cookie.bv_len ) + 1;
1850 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
1852 bv.bv_len = lutil_b64_ntop(
1853 (unsigned char *) pr_cookie.bv_val,
1855 bv.bv_val, bv.bv_len );
1857 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1858 "%scookie=%s", ptr == buf ? "" : " ",
1861 ber_memfree( bv.bv_val );
1863 pr_morePagedResults = 1;
1866 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1867 "%scookie=", ptr == buf ? "" : " " );
1870 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1871 "pagedresults", buf, ptr - buf );
1877 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1879 print_ppolicy( LDAP *ld, LDAPControl *ctrl )
1881 int expire = 0, grace = 0, rc;
1882 LDAPPasswordPolicyError pperr;
1884 rc = ldap_parse_passwordpolicy_control( ld, ctrl,
1885 &expire, &grace, &pperr );
1886 if ( rc == LDAP_SUCCESS ) {
1887 char buf[ BUFSIZ ], *ptr = buf;
1889 if ( expire != -1 ) {
1890 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1891 "expire=%d", expire );
1894 if ( grace != -1 ) {
1895 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1896 "%sgrace=%d", ptr == buf ? "" : " ", grace );
1899 if ( pperr != PP_noError ) {
1900 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1901 "%serror=%d (%s)", ptr == buf ? "" : " ",
1903 ldap_passwordpolicy_err2txt( pperr ) );
1906 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1907 "ppolicy", buf, ptr - buf );
1914 void tool_print_ctrls(
1916 LDAPControl **ctrls )
1921 for ( i = 0; ctrls[i] != NULL; i++ ) {
1922 /* control: OID criticality base64value */
1923 struct berval b64 = BER_BVNULL;
1928 /* FIXME: there might be cases where a control has NULL OID;
1929 * this makes little sense, especially when returned by the
1930 * server, but libldap happily allows it */
1931 if ( ctrls[i]->ldctl_oid == NULL ) {
1936 len += strlen( ctrls[i]->ldctl_oid );
1938 /* add enough for space after OID and the critical value itself */
1939 len += ctrls[i]->ldctl_iscritical
1940 ? sizeof("true") : sizeof("false");
1942 /* convert to base64 */
1943 if ( !BER_BVISNULL( &ctrls[i]->ldctl_value ) ) {
1944 b64.bv_len = LUTIL_BASE64_ENCODE_LEN(
1945 ctrls[i]->ldctl_value.bv_len ) + 1;
1946 b64.bv_val = ber_memalloc( b64.bv_len + 1 );
1948 b64.bv_len = lutil_b64_ntop(
1949 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
1950 ctrls[i]->ldctl_value.bv_len,
1951 b64.bv_val, b64.bv_len );
1955 len += 1 + b64.bv_len;
1958 ptr = str = malloc( len + 1 );
1960 ptr = lutil_strcopy( ptr, ": " );
1962 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_oid );
1963 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_iscritical
1964 ? " true" : " false" );
1967 ptr = lutil_strcopy( ptr, " " );
1968 ptr = lutil_strcopy( ptr, b64.bv_val );
1972 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1973 "control", str, len );
1978 ber_memfree( b64.bv_val );
1981 /* known controls */
1982 for ( j = 0; tool_ctrl_response[j].oid != NULL; j++ ) {
1983 if ( strcmp( tool_ctrl_response[j].oid, ctrls[i]->ldctl_oid ) == 0 ) {
1984 if ( !tool_ctrl_response[j].mask & tool_type ) {
1985 /* this control should not appear
1986 * with this tool; warning? */
1992 if ( tool_ctrl_response[j].oid != NULL && tool_ctrl_response[j].func ) {
1993 (void)tool_ctrl_response[j].func( ld, ctrls[i] );
1999 tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
2003 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
2007 fputs( ldif, stdout );
2008 ber_memfree( ldif );
2014 tool_is_oid( const char *s )
2018 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2022 for ( ; s[ 0 ]; s++ ) {
2023 if ( s[ 0 ] == '.' ) {
2024 if ( s[ 1 ] == '\0' ) {
2031 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2035 if ( first == 1 && s[ 0 ] == '0' && s[ 1 ] != '.' ) {