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 );
139 static struct tool_ctrls_t {
143 } tool_ctrl_response[] = {
144 { LDAP_CONTROL_PRE_READ, TOOL_ALL, print_preread },
145 { LDAP_CONTROL_POST_READ, TOOL_ALL, print_postread },
146 { LDAP_CONTROL_PAGEDRESULTS, TOOL_SEARCH, print_paged_results },
147 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
148 { LDAP_CONTROL_PASSWORDPOLICYRESPONSE, TOOL_ALL, print_ppolicy },
150 { LDAP_CONTROL_SORTRESPONSE, TOOL_SEARCH, print_sss },
155 enum { Intr_None = 0, Intr_Abandon, Intr_Cancel, Intr_Ignore };
156 static volatile sig_atomic_t gotintr, abcan;
159 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
161 st_value( LDAP *ld, struct berval *value )
163 char *ip = NULL, *name = NULL;
164 struct berval id = { 0 };
165 char namebuf[ MAXHOSTNAMELEN ];
167 if ( gethostname( namebuf, sizeof( namebuf ) ) == 0 ) {
173 h = gethostbyname( name );
175 AC_MEMCPY( &addr, h->h_addr, sizeof( addr ) );
176 ip = inet_ntoa( addr );
180 #ifdef HAVE_CYRUS_SASL
181 if ( sasl_authz_id != NULL ) {
182 ber_str2bv( sasl_authz_id, 0, 0, &id );
184 } else if ( sasl_authc_id != NULL ) {
185 ber_str2bv( sasl_authc_id, 0, 0, &id );
188 #endif /* HAVE_CYRUS_SASL */
189 if ( binddn != NULL ) {
190 ber_str2bv( binddn, 0, 0, &id );
193 if ( ldap_create_session_tracking_value( ld,
194 ip, name, LDAP_CONTROL_X_SESSION_TRACKING_USERNAME,
197 fprintf( stderr, _("Session tracking control encoding error!\n") );
203 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
212 tool_init( tool_type_t type )
215 ldap_pvt_setlocale(LC_MESSAGES, "");
216 ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
217 ldap_pvt_textdomain(OPENLDAP_PACKAGE);
223 #ifdef HAVE_CYRUS_SASL
227 ldap_pvt_tls_destroy();
230 if ( ldapuri != NULL ) {
231 ber_memfree( ldapuri );
235 if ( pr_cookie.bv_val != NULL ) {
236 ber_memfree( pr_cookie.bv_val );
237 pr_cookie.bv_val = NULL;
238 pr_cookie.bv_len = 0;
243 tool_common_usage( void )
245 static const char *const descriptions[] = {
246 N_(" -c continuous operation mode (do not stop on errors)\n"),
247 N_(" -d level set LDAP debugging level to `level'\n"),
248 N_(" -D binddn bind DN\n"),
249 N_(" -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
250 N_(" [!]assert=<filter> (a RFC 4515 Filter string)\n")
251 N_(" [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n")
252 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
254 /* non-advertized support for proxyDN */
255 N_(" [!]proxydn=<dn> (a RFC 4514 DN string)\n")
258 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
259 N_(" [!]chaining[=<resolveBehavior>[/<continuationBehavior>]]\n")
260 N_(" one of \"chainingPreferred\", \"chainingRequired\",\n")
261 N_(" \"referralsPreferred\", \"referralsRequired\"\n")
262 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
263 N_(" [!]manageDSAit\n")
265 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
268 N_(" [!]postread[=<attrs>] (a comma-separated attribute list)\n")
269 N_(" [!]preread[=<attrs>] (a comma-separated attribute list)\n")
271 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
272 N_(" [!]sessiontracking\n")
273 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
274 N_(" abandon, cancel, ignore (SIGINT sends abandon/cancel,\n"
275 " or ignores response; if critical, doesn't wait for SIGINT.\n"
276 " not really controls)\n")
277 N_(" -f file read operations from `file'\n"),
278 N_(" -h host LDAP server\n"),
279 N_(" -H URI LDAP Uniform Resource Identifier(s)\n"),
280 N_(" -I use SASL Interactive mode\n"),
281 N_(" -M enable Manage DSA IT control (-MM to make critical)\n"),
282 N_(" -n show what would be done but don't actually do it\n"),
283 N_(" -O props SASL security properties\n"),
284 N_(" -o <opt>[=<optparam] general options\n"),
285 N_(" nettimeout=<timeout> (in seconds, or \"none\" or \"max\")\n"),
286 N_(" -p port port on LDAP server\n"),
287 N_(" -P version protocol version (default: 3)\n"),
288 N_(" -Q use SASL Quiet mode\n"),
289 N_(" -R realm SASL realm\n"),
290 N_(" -U authcid SASL authentication identity\n"),
291 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
292 N_(" -V print version info (-VV only)\n"),
293 N_(" -w passwd bind password (for simple authentication)\n"),
294 N_(" -W prompt for bind password\n"),
295 N_(" -x Simple authentication\n"),
296 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
297 N_(" -y file Read password from file\n"),
298 N_(" -Y mech SASL mechanism\n"),
299 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
302 const char *const *cpp;
304 fputs( _("Common options:\n"), stderr );
305 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
306 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
307 fputs( _(*cpp), stderr );
320 fprintf( stderr, "%s: %s (%d)%s\n",
321 func, ldap_err2string( err ), err, extra ? extra : "" );
323 if ( matched && *matched ) {
324 fprintf( stderr, _("\tmatched DN: %s\n"), matched );
327 if ( info && *info ) {
328 fprintf( stderr, _("\tadditional info: %s\n"), info );
331 if ( refs && *refs ) {
333 fprintf( stderr, _("\treferrals:\n") );
334 for( i=0; refs[i]; i++ ) {
335 fprintf( stderr, "\t\t%s\n", refs[i] );
342 tool_args( int argc, char **argv )
346 while (( i = getopt( argc, argv, options )) != EOF ) {
348 char *control, *cvalue, *next;
350 case 'c': /* continuous operation mode */
357 ival = strtol( optarg, &next, 10 );
358 if (next == NULL || next[0] != '\0') {
359 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
364 case 'D': /* bind DN */
365 if( binddn != NULL ) {
366 fprintf( stderr, "%s: -D previously specified\n", prog );
367 exit( EXIT_FAILURE );
369 binddn = ber_strdup( optarg );
371 case 'e': /* general extensions (controls and such) */
372 /* should be extended to support comma separated list of
373 * [!]key[=value] parameters, e.g. -e !foo,bar=567
378 if( optarg[0] == '!' ) {
383 control = ber_strdup( optarg );
384 if ( (cvalue = strchr( control, '=' )) != NULL ) {
388 if ( strcasecmp( control, "assert" ) == 0 ) {
390 fprintf( stderr, "assert control previously specified\n");
391 exit( EXIT_FAILURE );
393 if( cvalue == NULL ) {
394 fprintf( stderr, "assert: control value expected\n" );
398 assertctl = 1 + crit;
400 assert( assertion == NULL );
403 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
404 if( authzid != NULL ) {
405 fprintf( stderr, "authzid control previously specified\n");
406 exit( EXIT_FAILURE );
408 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
409 if( proxydn != NULL ) {
410 fprintf( stderr, "authzid control incompatible with proxydn\n");
411 exit( EXIT_FAILURE );
413 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
414 if( cvalue == NULL ) {
415 fprintf( stderr, "authzid: control value expected\n" );
419 fprintf( stderr, "authzid: must be marked critical\n" );
423 assert( authzid == NULL );
426 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
427 } else if ( strcasecmp( control, "proxydn" ) == 0 ) {
428 if( proxydn != NULL ) {
429 fprintf( stderr, "proxydn control previously specified\n");
430 exit( EXIT_FAILURE );
432 if( authzid != NULL ) {
433 fprintf( stderr, "proxydn control incompatible with authzid\n");
434 exit( EXIT_FAILURE );
436 if( cvalue == NULL ) {
437 fprintf( stderr, "proxydn: control value expected\n" );
441 fprintf( stderr, "proxydn: must be marked critical\n" );
445 assert( proxydn == NULL );
447 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
449 } else if ( ( strcasecmp( control, "relax" ) == 0 ) ||
450 ( strcasecmp( control, "manageDIT" ) == 0 ) )
454 "relax control previously specified\n");
455 exit( EXIT_FAILURE );
457 if( cvalue != NULL ) {
459 "relax: no control value expected\n" );
463 manageDIT = 1 + crit;
465 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
468 "manageDSAit control previously specified\n");
469 exit( EXIT_FAILURE );
471 if( cvalue != NULL ) {
473 "manageDSAit: no control value expected\n" );
477 manageDSAit = 1 + crit;
479 } else if ( strcasecmp( control, "noop" ) == 0 ) {
481 fprintf( stderr, "noop control previously specified\n");
482 exit( EXIT_FAILURE );
484 if( cvalue != NULL ) {
485 fprintf( stderr, "noop: no control value expected\n" );
491 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
492 } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
494 fprintf( stderr, "ppolicy control previously specified\n");
495 exit( EXIT_FAILURE );
497 if( cvalue != NULL ) {
498 fprintf( stderr, "ppolicy: no control value expected\n" );
502 fprintf( stderr, "ppolicy: critical flag not allowed\n" );
509 } else if ( strcasecmp( control, "preread" ) == 0 ) {
511 fprintf( stderr, "preread control previously specified\n");
512 exit( EXIT_FAILURE );
516 preread_attrs = cvalue;
518 } else if ( strcasecmp( control, "postread" ) == 0 ) {
520 fprintf( stderr, "postread control previously specified\n");
521 exit( EXIT_FAILURE );
525 postread_attrs = cvalue;
527 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
528 } else if ( strcasecmp( control, "chaining" ) == 0 ) {
531 if ( cvalue != NULL ) {
534 continuation = strchr( cvalue, '/' );
535 if ( continuation ) {
536 /* FIXME: this makes sense only in searches */
537 *continuation++ = '\0';
538 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
539 chainingContinuation = LDAP_CHAINING_PREFERRED;
540 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
541 chainingContinuation = LDAP_CHAINING_REQUIRED;
542 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
543 chainingContinuation = LDAP_REFERRALS_PREFERRED;
544 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
545 chainingContinuation = LDAP_REFERRALS_REQUIRED;
548 "chaining behavior control "
549 "continuation value \"%s\" invalid\n",
551 exit( EXIT_FAILURE );
555 if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
556 chainingResolve = LDAP_CHAINING_PREFERRED;
557 } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
558 chainingResolve = LDAP_CHAINING_REQUIRED;
559 } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
560 chainingResolve = LDAP_REFERRALS_PREFERRED;
561 } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
562 chainingResolve = LDAP_REFERRALS_REQUIRED;
565 "chaining behavior control "
566 "resolve value \"%s\" invalid\n",
568 exit( EXIT_FAILURE );
571 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
573 /* this shouldn't go here, really; but it's a feature... */
574 } else if ( strcasecmp( control, "abandon" ) == 0 ) {
575 abcan = Intr_Abandon;
580 } else if ( strcasecmp( control, "cancel" ) == 0 ) {
586 } else if ( strcasecmp( control, "ignore" ) == 0 ) {
592 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
593 } else if ( strcasecmp( control, "sessiontracking" ) == 0 ) {
594 if ( sessionTracking ) {
595 fprintf( stderr, "%s: session tracking can be only specified once\n", prog );
596 exit( EXIT_FAILURE );
599 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
601 } else if ( tool_is_oid( control ) ) {
602 LDAPControl *tmpctrls, ctrl;
604 tmpctrls = (LDAPControl *)realloc( unknown_ctrls,
605 (unknown_ctrls_num + 1)*sizeof( LDAPControl ) );
606 if ( tmpctrls == NULL ) {
607 fprintf( stderr, "%s: no memory?\n", prog );
608 exit( EXIT_FAILURE );
610 unknown_ctrls = tmpctrls;
611 ctrl.ldctl_oid = control;
612 ctrl.ldctl_value.bv_val = NULL;
613 ctrl.ldctl_value.bv_len = 0;
614 ctrl.ldctl_iscritical = crit;
616 if ( cvalue != NULL ) {
618 size_t len = strlen( cvalue );
621 bv.bv_len = LUTIL_BASE64_DECODE_LEN( len );
622 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
624 retcode = lutil_b64_pton( cvalue,
625 (unsigned char *)bv.bv_val,
628 if ( retcode == -1 || retcode > bv.bv_len ) {
629 fprintf( stderr, "Unable to parse value of general control %s\n",
635 ctrl.ldctl_value = bv;
638 unknown_ctrls[ unknown_ctrls_num ] = ctrl;
642 fprintf( stderr, "Invalid general control name: %s\n",
647 case 'f': /* read from file */
648 if( infile != NULL ) {
649 fprintf( stderr, "%s: -f previously specified\n", prog );
650 exit( EXIT_FAILURE );
652 infile = ber_strdup( optarg );
654 case 'h': /* ldap host */
655 if( ldaphost != NULL ) {
656 fprintf( stderr, "%s: -h previously specified\n", prog );
657 exit( EXIT_FAILURE );
659 ldaphost = ber_strdup( optarg );
661 case 'H': /* ldap URI */
662 if( ldapuri != NULL ) {
663 fprintf( stderr, "%s: -H previously specified\n", prog );
664 exit( EXIT_FAILURE );
666 ldapuri = ber_strdup( optarg );
669 #ifdef HAVE_CYRUS_SASL
670 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
671 fprintf( stderr, "%s: incompatible previous "
672 "authentication choice\n",
674 exit( EXIT_FAILURE );
676 authmethod = LDAP_AUTH_SASL;
677 sasl_flags = LDAP_SASL_INTERACTIVE;
680 fprintf( stderr, "%s: was not compiled with SASL support\n",
682 exit( EXIT_FAILURE );
685 /* enable Manage DSA IT */
688 case 'n': /* print operations, don't actually do them */
692 control = ber_strdup( optarg );
693 if ( (cvalue = strchr( control, '=' )) != NULL ) {
697 if ( strcasecmp( control, "nettimeout" ) == 0 ) {
698 if( nettimeout.tv_sec != -1 ) {
699 fprintf( stderr, "nettimeout option previously specified\n");
700 exit( EXIT_FAILURE );
702 if( cvalue == NULL || cvalue[0] == '\0' ) {
703 fprintf( stderr, "nettimeout: option value expected\n" );
706 if ( strcasecmp( cvalue, "none" ) == 0 ) {
707 nettimeout.tv_sec = 0;
708 } else if ( strcasecmp( cvalue, "max" ) == 0 ) {
709 nettimeout.tv_sec = LDAP_MAXINT;
711 ival = strtol( cvalue, &next, 10 );
712 if ( next == NULL || next[0] != '\0' ) {
714 _("Unable to parse network timeout \"%s\"\n"), cvalue );
715 exit( EXIT_FAILURE );
717 nettimeout.tv_sec = ival;
719 if( nettimeout.tv_sec < 0 || nettimeout.tv_sec > LDAP_MAXINT ) {
720 fprintf( stderr, _("%s: invalid network timeout (%ld) specified\n"),
721 prog, (long)nettimeout.tv_sec );
722 exit( EXIT_FAILURE );
725 fprintf( stderr, "Invalid general option name: %s\n",
731 #ifdef HAVE_CYRUS_SASL
732 if( sasl_secprops != NULL ) {
733 fprintf( stderr, "%s: -O previously specified\n", prog );
734 exit( EXIT_FAILURE );
736 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
737 fprintf( stderr, "%s: incompatible previous "
738 "authentication choice\n", prog );
739 exit( EXIT_FAILURE );
741 authmethod = LDAP_AUTH_SASL;
742 sasl_secprops = ber_strdup( optarg );
744 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
745 exit( EXIT_FAILURE );
750 fprintf( stderr, "%s: -p previously specified\n", prog );
751 exit( EXIT_FAILURE );
753 ival = strtol( optarg, &next, 10 );
754 if ( next == NULL || next[0] != '\0' ) {
755 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
756 exit( EXIT_FAILURE );
761 ival = strtol( optarg, &next, 10 );
762 if ( next == NULL || next[0] != '\0' ) {
763 fprintf( stderr, "%s: unable to parse protocol version \"%s\"\n", prog, optarg );
764 exit( EXIT_FAILURE );
768 if( protocol == LDAP_VERSION3 ) {
769 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
771 exit( EXIT_FAILURE );
773 protocol = LDAP_VERSION2;
776 if( protocol == LDAP_VERSION2 ) {
777 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
779 exit( EXIT_FAILURE );
781 protocol = LDAP_VERSION3;
784 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
790 #ifdef HAVE_CYRUS_SASL
791 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
792 fprintf( stderr, "%s: incompatible previous "
793 "authentication choice\n",
795 exit( EXIT_FAILURE );
797 authmethod = LDAP_AUTH_SASL;
798 sasl_flags = LDAP_SASL_QUIET;
801 fprintf( stderr, "%s: not compiled with SASL support\n",
803 exit( EXIT_FAILURE );
806 #ifdef HAVE_CYRUS_SASL
807 if( sasl_realm != NULL ) {
808 fprintf( stderr, "%s: -R previously specified\n", prog );
809 exit( EXIT_FAILURE );
811 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
812 fprintf( stderr, "%s: incompatible previous "
813 "authentication choice\n",
815 exit( EXIT_FAILURE );
817 authmethod = LDAP_AUTH_SASL;
818 sasl_realm = ber_strdup( optarg );
820 fprintf( stderr, "%s: not compiled with SASL support\n",
822 exit( EXIT_FAILURE );
826 #ifdef HAVE_CYRUS_SASL
827 if( sasl_authc_id != NULL ) {
828 fprintf( stderr, "%s: -U previously specified\n", prog );
829 exit( EXIT_FAILURE );
831 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
832 fprintf( stderr, "%s: incompatible previous "
833 "authentication choice\n",
835 exit( EXIT_FAILURE );
837 authmethod = LDAP_AUTH_SASL;
838 sasl_authc_id = ber_strdup( optarg );
840 fprintf( stderr, "%s: not compiled with SASL support\n",
842 exit( EXIT_FAILURE );
845 case 'v': /* verbose mode */
848 case 'V': /* version */
851 case 'w': /* password */
852 passwd.bv_val = ber_strdup( optarg );
856 for( p = optarg; *p != '\0'; p++ ) {
860 passwd.bv_len = strlen( passwd.bv_val );
869 #ifdef HAVE_CYRUS_SASL
870 if( sasl_mech != NULL ) {
871 fprintf( stderr, "%s: -Y previously specified\n", prog );
872 exit( EXIT_FAILURE );
874 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
876 "%s: incompatible with authentication choice\n", prog );
877 exit( EXIT_FAILURE );
879 authmethod = LDAP_AUTH_SASL;
880 sasl_mech = ber_strdup( optarg );
882 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
883 exit( EXIT_FAILURE );
887 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
888 fprintf( stderr, "%s: incompatible with previous "
889 "authentication choice\n", prog );
890 exit( EXIT_FAILURE );
892 authmethod = LDAP_AUTH_SIMPLE;
895 #ifdef HAVE_CYRUS_SASL
896 if( sasl_authz_id != NULL ) {
897 fprintf( stderr, "%s: -X previously specified\n", prog );
898 exit( EXIT_FAILURE );
900 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
901 fprintf( stderr, "%s: -X incompatible with "
902 "authentication choice\n", prog );
903 exit( EXIT_FAILURE );
905 authmethod = LDAP_AUTH_SASL;
906 sasl_authz_id = ber_strdup( optarg );
908 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
909 exit( EXIT_FAILURE );
916 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
917 exit( EXIT_FAILURE );
921 if( handle_private_option( i ) ) break;
922 fprintf( stderr, "%s: unrecognized option -%c\n",
929 /* prevent bad linking */
931 api.ldapai_info_version = LDAP_API_INFO_VERSION;
933 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
934 != LDAP_OPT_SUCCESS )
936 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
937 exit( EXIT_FAILURE );
940 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
941 fprintf( stderr, "LDAP APIInfo version mismatch: "
942 "library %d, header %d\n",
943 api.ldapai_info_version, LDAP_API_INFO_VERSION );
944 exit( EXIT_FAILURE );
947 if( api.ldapai_api_version != LDAP_API_VERSION ) {
948 fprintf( stderr, "LDAP API version mismatch: "
949 "library %d, header %d\n",
950 api.ldapai_api_version, LDAP_API_VERSION );
951 exit( EXIT_FAILURE );
954 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
955 fprintf( stderr, "LDAP vendor name mismatch: "
956 "library %s, header %s\n",
957 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
958 exit( EXIT_FAILURE );
961 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
962 fprintf( stderr, "LDAP vendor version mismatch: "
963 "library %d, header %d\n",
964 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
965 exit( EXIT_FAILURE );
969 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
971 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
972 if (version > 1) exit( EXIT_SUCCESS );
975 ldap_memfree( api.ldapai_vendor_name );
976 ber_memvfree( (void **)api.ldapai_extensions );
980 protocol = LDAP_VERSION3;
982 if (authmethod == -1 && protocol > LDAP_VERSION2) {
983 #ifdef HAVE_CYRUS_SASL
984 authmethod = LDAP_AUTH_SASL;
986 authmethod = LDAP_AUTH_SIMPLE;
990 if( ldapuri == NULL ) {
991 if( ldapport && ( ldaphost == NULL )) {
992 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
993 exit( EXIT_FAILURE );
996 if( ldaphost != NULL ) {
997 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
998 exit( EXIT_FAILURE );
1001 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
1002 exit( EXIT_FAILURE );
1006 if( protocol == LDAP_VERSION2 ) {
1007 if( assertctl || authzid || manageDIT || manageDSAit ||
1008 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1010 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1011 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1014 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1016 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1017 noop || ppolicy || preread || postread )
1019 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
1020 exit( EXIT_FAILURE );
1024 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
1025 exit( EXIT_FAILURE );
1028 #ifdef HAVE_CYRUS_SASL
1029 if( authmethod == LDAP_AUTH_SASL ) {
1030 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
1032 exit( EXIT_FAILURE );
1040 tool_conn_setup( int dont, void (*private_setup)( LDAP * ) )
1045 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
1046 != LBER_OPT_SUCCESS )
1049 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
1051 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
1052 != LDAP_OPT_SUCCESS )
1055 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
1060 (void) SIGNAL( SIGPIPE, SIG_IGN );
1064 SIGNAL( SIGINT, do_sig );
1070 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
1073 memset( &url, 0, sizeof(url));
1075 url.lud_scheme = "ldap";
1076 url.lud_host = ldaphost;
1077 url.lud_port = ldapport;
1078 url.lud_scope = LDAP_SCOPE_DEFAULT;
1080 ldapuri = ldap_url_desc2str( &url );
1082 } else if ( ldapuri != NULL ) {
1083 LDAPURLDesc *ludlist, **ludp;
1087 rc = ldap_url_parselist( &ludlist, ldapuri );
1088 if ( rc != LDAP_URL_SUCCESS ) {
1090 "Could not parse LDAP URI(s)=%s (%d)\n",
1092 exit( EXIT_FAILURE );
1095 for ( ludp = &ludlist; *ludp != NULL; ) {
1096 LDAPURLDesc *lud = *ludp;
1099 if ( lud->lud_dn != NULL && lud->lud_dn[ 0 ] != '\0' &&
1100 ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) )
1102 /* if no host but a DN is provided,
1103 * use DNS SRV to gather the host list
1104 * and turn it into a list of URIs
1105 * using the scheme provided */
1106 char *domain = NULL,
1110 len_proto = strlen( lud->lud_scheme );
1112 if ( ldap_dn2domain( lud->lud_dn, &domain )
1116 "DNS SRV: Could not turn "
1117 "DN=\"%s\" into a domain\n",
1122 rc = ldap_domain2hostlist( domain, &hostlist );
1125 "DNS SRV: Could not turn "
1126 "domain=%s into a hostlist\n",
1131 hosts = ldap_str2charray( hostlist, " " );
1132 if ( hosts == NULL ) {
1134 "DNS SRV: Could not parse "
1135 "hostlist=\"%s\"\n",
1140 for ( i = 0; hosts[ i ] != NULL; i++ )
1143 tmp = (char **)realloc( urls, sizeof( char * ) * ( nurls + i + 1 ) );
1144 if ( tmp == NULL ) {
1146 "DNS SRV: out of memory?\n" );
1150 urls[ nurls ] = NULL;
1152 for ( i = 0; hosts[ i ] != NULL; i++ ) {
1153 size_t len = len_proto
1155 + strlen( hosts[ i ] )
1158 urls[ nurls + i + 1 ] = NULL;
1159 urls[ nurls + i ] = (char *)malloc( sizeof( char ) * len );
1160 if ( urls[ nurls + i ] == NULL ) {
1162 "DNS SRV: out of memory?\n" );
1166 snprintf( urls[ nurls + i ], len, "%s://%s",
1167 lud->lud_scheme, hosts[ i ] );
1172 ber_memvfree( (void **)hosts );
1173 ber_memfree( hostlist );
1174 ber_memfree( domain );
1177 tmp = (char **)realloc( urls, sizeof( char * ) * ( nurls + 2 ) );
1178 if ( tmp == NULL ) {
1180 "DNS SRV: out of memory?\n" );
1184 urls[ nurls + 1 ] = NULL;
1186 urls[ nurls ] = ldap_url_desc2str( lud );
1187 if ( urls[ nurls ] == NULL ) {
1189 "DNS SRV: out of memory?\n" );
1195 *ludp = lud->lud_next;
1197 lud->lud_next = NULL;
1198 ldap_free_urldesc( lud );
1201 if ( ludlist != NULL ) {
1202 ldap_free_urllist( ludlist );
1203 exit( EXIT_FAILURE );
1205 } else if ( urls == NULL ) {
1206 exit( EXIT_FAILURE );
1209 ldap_memfree( ldapuri );
1210 ldapuri = ldap_charray2str( urls, " " );
1211 ber_memvfree( (void **)urls );
1215 fprintf( stderr, "ldap_initialize( %s )\n",
1216 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
1218 rc = ldap_initialize( &ld, ldapuri );
1219 if( rc != LDAP_SUCCESS ) {
1221 "Could not create LDAP session handle for URI=%s (%d): %s\n",
1222 ldapuri, rc, ldap_err2string(rc) );
1223 exit( EXIT_FAILURE );
1226 if( private_setup ) private_setup( ld );
1229 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
1230 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
1232 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
1233 referrals ? "on" : "off" );
1234 exit( EXIT_FAILURE );
1237 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
1238 != LDAP_OPT_SUCCESS )
1240 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
1242 exit( EXIT_FAILURE );
1246 rc = ldap_start_tls_s( ld, NULL, NULL );
1247 if ( rc != LDAP_SUCCESS ) {
1248 tool_perror( "ldap_start_tls", rc, NULL, NULL, NULL, NULL );
1249 if ( use_tls > 1 ) {
1250 exit( EXIT_FAILURE );
1255 if ( nettimeout.tv_sec > 0 ) {
1256 if ( ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &nettimeout )
1257 != LDAP_OPT_SUCCESS )
1259 fprintf( stderr, "Could not set LDAP_OPT_NETWORK_TIMEOUT %ld\n",
1260 (long)nettimeout.tv_sec );
1261 exit( EXIT_FAILURE );
1271 tool_bind( LDAP *ld )
1273 LDAPControl **sctrlsp = NULL;
1274 LDAPControl *sctrls[3];
1275 LDAPControl sctrl[3];
1278 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1281 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1282 c.ldctl_value.bv_val = NULL;
1283 c.ldctl_value.bv_len = 0;
1284 c.ldctl_iscritical = 0;
1286 sctrls[nsctrls] = &sctrl[nsctrls];
1287 sctrls[++nsctrls] = NULL;
1291 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1292 if ( sessionTracking ) {
1295 if (stValue.bv_val == NULL && st_value( ld, &stValue ) ) {
1296 exit( EXIT_FAILURE );
1299 c.ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1300 c.ldctl_iscritical = 0;
1301 ber_dupbv( &c.ldctl_value, &stValue );
1304 sctrls[nsctrls] = &sctrl[nsctrls];
1305 sctrls[++nsctrls] = NULL;
1307 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1313 assert( nsctrls < sizeof(sctrls)/sizeof(sctrls[0]) );
1315 if ( authmethod == LDAP_AUTH_SASL ) {
1316 #ifdef HAVE_CYRUS_SASL
1320 if( sasl_secprops != NULL ) {
1321 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
1322 (void *) sasl_secprops );
1324 if( rc != LDAP_OPT_SUCCESS ) {
1326 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
1328 exit( LDAP_LOCAL_ERROR );
1332 defaults = lutil_sasl_defaults( ld,
1339 rc = ldap_sasl_interactive_bind_s( ld, binddn, sasl_mech,
1341 NULL, sasl_flags, lutil_sasl_interact, defaults );
1343 lutil_sasl_freedefs( defaults );
1344 if( rc != LDAP_SUCCESS ) {
1345 tool_perror( "ldap_sasl_interactive_bind_s",
1346 rc, NULL, NULL, NULL, NULL );
1350 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1351 exit( LDAP_NOT_SUPPORTED );
1355 LDAPMessage *result;
1356 LDAPControl **ctrls;
1358 char *matched = NULL;
1366 rc = ldap_sasl_bind( ld, binddn, LDAP_SASL_SIMPLE, &passwd,
1367 sctrlsp, NULL, &msgid );
1368 if ( msgid == -1 ) {
1369 tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
1370 NULL, NULL, NULL, NULL );
1375 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 ) {
1376 tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
1377 exit( LDAP_LOCAL_ERROR );
1380 rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
1382 if ( rc != LDAP_SUCCESS ) {
1383 tool_perror( "ldap_bind parse result", rc, NULL, matched, info, refs );
1384 exit( LDAP_LOCAL_ERROR );
1387 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1388 if ( ctrls && ppolicy ) {
1390 int expire, grace, len = 0;
1391 LDAPPasswordPolicyError pErr = -1;
1393 ctrl = ldap_control_find( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
1396 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
1397 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1399 if ( pErr != PP_noError ){
1402 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1403 len = strlen( msgbuf );
1405 if ( expire >= 0 ) {
1406 sprintf( msgbuf+len,
1407 " (Password expires in %d seconds)",
1409 } else if ( grace >= 0 ) {
1410 sprintf( msgbuf+len,
1411 " (Password expired, %d grace logins remain)",
1419 ldap_controls_free( ctrls );
1422 if ( err != LDAP_SUCCESS
1424 || ( matched && matched[ 0 ] )
1425 || ( info && info[ 0 ] )
1428 tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1430 if( matched ) ber_memfree( matched );
1431 if( info ) ber_memfree( info );
1432 if( refs ) ber_memvfree( (void **)refs );
1434 if ( err != LDAP_SUCCESS ) exit( err );
1440 tool_unbind( LDAP *ld )
1442 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1444 if ( err != LDAP_OPT_SUCCESS ) {
1445 fprintf( stderr, "Could not unset controls\n");
1448 (void) ldap_unbind_ext( ld, NULL, NULL );
1452 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1454 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1456 int i = 0, j, crit = 0, err;
1457 LDAPControl c[16], **ctrls;
1461 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1463 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1467 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1472 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1474 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1475 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1477 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1479 || unknown_ctrls_num ) )
1484 ctrls = (LDAPControl**) malloc(sizeof(c) + (count + unknown_ctrls_num + 1)*sizeof(LDAPControl*));
1485 if ( ctrls == NULL ) {
1486 fprintf( stderr, "No memory\n" );
1487 exit( EXIT_FAILURE );
1491 if ( BER_BVISNULL( &assertionvalue ) ) {
1492 err = ldap_create_assertion_control_value( ld,
1493 assertion, &assertionvalue );
1496 "Unable to create assertion value "
1497 "\"%s\" (%d)\n", assertion, err );
1501 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1502 c[i].ldctl_value = assertionvalue;
1503 c[i].ldctl_iscritical = assertctl > 1;
1509 c[i].ldctl_value.bv_val = authzid;
1510 c[i].ldctl_value.bv_len = strlen( authzid );
1511 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1512 c[i].ldctl_iscritical = 1;
1517 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1518 /* NOTE: doesn't need an extra count because it's incompatible
1521 BerElementBuffer berbuf;
1522 BerElement *ber = (BerElement *)&berbuf;
1524 ber_init2( ber, NULL, LBER_USE_DER );
1526 if ( ber_printf( ber, "s", proxydn ) == LBER_ERROR ) {
1527 exit( EXIT_FAILURE );
1530 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1531 exit( EXIT_FAILURE );
1534 c[i].ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1535 c[i].ldctl_iscritical = 1;
1539 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1542 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1543 BER_BVZERO( &c[i].ldctl_value );
1544 c[i].ldctl_iscritical = manageDIT > 1;
1549 if ( manageDSAit ) {
1550 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1551 BER_BVZERO( &c[i].ldctl_value );
1552 c[i].ldctl_iscritical = manageDSAit > 1;
1558 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1559 BER_BVZERO( &c[i].ldctl_value );
1560 c[i].ldctl_iscritical = noop > 1;
1565 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1567 c[i].ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1568 BER_BVZERO( &c[i].ldctl_value );
1569 c[i].ldctl_iscritical = 0;
1576 char berbuf[LBER_ELEMENT_SIZEOF];
1577 BerElement *ber = (BerElement *)berbuf;
1578 char **attrs = NULL;
1580 if( preread_attrs ) {
1581 attrs = ldap_str2charray( preread_attrs, "," );
1584 ber_init2( ber, NULL, LBER_USE_DER );
1586 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1587 fprintf( stderr, "preread attrs encode failed.\n" );
1588 exit( EXIT_FAILURE );
1591 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1593 fprintf( stderr, "preread flatten failed (%d)\n", err );
1594 exit( EXIT_FAILURE );
1597 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1598 c[i].ldctl_iscritical = preread > 1;
1602 if( attrs ) ldap_charray_free( attrs );
1606 char berbuf[LBER_ELEMENT_SIZEOF];
1607 BerElement *ber = (BerElement *)berbuf;
1608 char **attrs = NULL;
1610 if( postread_attrs ) {
1611 attrs = ldap_str2charray( postread_attrs, "," );
1614 ber_init2( ber, NULL, LBER_USE_DER );
1616 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1617 fprintf( stderr, "postread attrs encode failed.\n" );
1618 exit( EXIT_FAILURE );
1621 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1623 fprintf( stderr, "postread flatten failed (%d)\n", err );
1624 exit( EXIT_FAILURE );
1627 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1628 c[i].ldctl_iscritical = postread > 1;
1632 if( attrs ) ldap_charray_free( attrs );
1635 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1637 if ( chainingResolve > -1 ) {
1638 BerElementBuffer berbuf;
1639 BerElement *ber = (BerElement *)&berbuf;
1641 ber_init2( ber, NULL, LBER_USE_DER );
1643 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1646 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1647 exit( EXIT_FAILURE );
1650 if ( chainingContinuation > -1 ) {
1651 err = ber_printf( ber, "e", chainingContinuation );
1654 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1655 exit( EXIT_FAILURE );
1659 err = ber_printf( ber, /* { */ "N}" );
1662 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1663 exit( EXIT_FAILURE );
1666 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1667 exit( EXIT_FAILURE );
1671 BER_BVZERO( &c[i].ldctl_value );
1674 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1675 c[i].ldctl_iscritical = chaining > 1;
1679 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1681 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1682 if ( sessionTracking ) {
1683 if ( stValue.bv_val == NULL && st_value( ld, &stValue ) ) {
1684 exit( EXIT_FAILURE );
1687 c[i].ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1688 c[i].ldctl_iscritical = 0;
1689 ber_dupbv( &c[i].ldctl_value, &stValue );
1694 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1697 ctrls[i++] = extra_c++;
1699 for ( count = 0; count < unknown_ctrls_num; count++ ) {
1700 ctrls[i++] = &unknown_ctrls[count];
1704 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1706 if ( err != LDAP_OPT_SUCCESS ) {
1707 for ( j = 0; j < i; j++ ) {
1708 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1710 fprintf( stderr, "Could not set %scontrols\n",
1711 crit ? "critical " : "" );
1716 exit( EXIT_FAILURE );
1721 tool_check_abandon( LDAP *ld, int msgid )
1725 switch ( gotintr ) {
1727 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1728 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1729 rc, ldap_err2string( rc ) );
1733 rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
1734 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1735 rc, ldap_err2string( rc ) );
1739 /* just unbind, ignoring the request */
1747 print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
1752 tool_write_ldif( LDIF_PUT_COMMENT, "==> ",
1753 what->bv_val, what->bv_len );
1754 ber = ber_init( &ctrl->ldctl_value );
1755 if ( ber == NULL ) {
1759 } else if ( ber_scanf( ber, "{m{" /*}}*/, &bv ) == LBER_ERROR ) {
1764 tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1766 while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
1768 BerVarray vals = NULL;
1770 if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
1777 for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
1779 ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1780 bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
1783 ber_bvarray_free( vals );
1787 if ( ber != NULL ) {
1791 tool_write_ldif( LDIF_PUT_COMMENT, "<== ",
1792 what->bv_val, what->bv_len );
1798 print_preread( LDAP *ld, LDAPControl *ctrl )
1800 static struct berval what = BER_BVC( "preread" );
1802 return print_prepostread( ld, ctrl, &what );
1806 print_postread( LDAP *ld, LDAPControl *ctrl )
1808 static struct berval what = BER_BVC( "postread" );
1810 return print_prepostread( ld, ctrl, &what );
1814 print_paged_results( LDAP *ld, LDAPControl *ctrl )
1818 /* note: pr_cookie is being malloced; it's freed
1819 * the next time the control is sent, but the last
1820 * time it's not; we don't care too much, because
1821 * the last time an empty value is returned... */
1822 if ( ldap_parse_pageresponse_control( ld, ctrl, &estimate, &pr_cookie )
1829 /* FIXME: check buffer overflow */
1830 char buf[ BUFSIZ ], *ptr = buf;
1832 if ( estimate > 0 ) {
1833 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1834 "estimate=%d", estimate );
1837 if ( pr_cookie.bv_len > 0 ) {
1840 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
1841 pr_cookie.bv_len ) + 1;
1842 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
1844 bv.bv_len = lutil_b64_ntop(
1845 (unsigned char *) pr_cookie.bv_val,
1847 bv.bv_val, bv.bv_len );
1849 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1850 "%scookie=%s", ptr == buf ? "" : " ",
1853 ber_memfree( bv.bv_val );
1855 pr_morePagedResults = 1;
1858 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1859 "%scookie=", ptr == buf ? "" : " " );
1862 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1863 "pagedresults", buf, ptr - buf );
1870 print_sss( LDAP *ld, LDAPControl *ctrl )
1876 rc = ldap_parse_sortresponse_control( ld, ctrl, &err, &attr );
1877 if ( rc == LDAP_SUCCESS ) {
1879 rc = snprintf( buf, sizeof(buf), "(%d) %s %s",
1880 err, ldap_err2string(err), attr ? attr : "" );
1882 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1883 "sortResult", buf, rc );
1889 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1891 print_ppolicy( LDAP *ld, LDAPControl *ctrl )
1893 int expire = 0, grace = 0, rc;
1894 LDAPPasswordPolicyError pperr;
1896 rc = ldap_parse_passwordpolicy_control( ld, ctrl,
1897 &expire, &grace, &pperr );
1898 if ( rc == LDAP_SUCCESS ) {
1899 char buf[ BUFSIZ ], *ptr = buf;
1901 if ( expire != -1 ) {
1902 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1903 "expire=%d", expire );
1906 if ( grace != -1 ) {
1907 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1908 "%sgrace=%d", ptr == buf ? "" : " ", grace );
1911 if ( pperr != PP_noError ) {
1912 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1913 "%serror=%d (%s)", ptr == buf ? "" : " ",
1915 ldap_passwordpolicy_err2txt( pperr ) );
1918 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1919 "ppolicy", buf, ptr - buf );
1926 void tool_print_ctrls(
1928 LDAPControl **ctrls )
1933 for ( i = 0; ctrls[i] != NULL; i++ ) {
1934 /* control: OID criticality base64value */
1935 struct berval b64 = BER_BVNULL;
1940 /* FIXME: there might be cases where a control has NULL OID;
1941 * this makes little sense, especially when returned by the
1942 * server, but libldap happily allows it */
1943 if ( ctrls[i]->ldctl_oid == NULL ) {
1948 len += strlen( ctrls[i]->ldctl_oid );
1950 /* add enough for space after OID and the critical value itself */
1951 len += ctrls[i]->ldctl_iscritical
1952 ? sizeof("true") : sizeof("false");
1954 /* convert to base64 */
1955 if ( !BER_BVISNULL( &ctrls[i]->ldctl_value ) ) {
1956 b64.bv_len = LUTIL_BASE64_ENCODE_LEN(
1957 ctrls[i]->ldctl_value.bv_len ) + 1;
1958 b64.bv_val = ber_memalloc( b64.bv_len + 1 );
1960 b64.bv_len = lutil_b64_ntop(
1961 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
1962 ctrls[i]->ldctl_value.bv_len,
1963 b64.bv_val, b64.bv_len );
1967 len += 1 + b64.bv_len;
1970 ptr = str = malloc( len + 1 );
1972 ptr = lutil_strcopy( ptr, ": " );
1974 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_oid );
1975 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_iscritical
1976 ? " true" : " false" );
1979 ptr = lutil_strcopy( ptr, " " );
1980 ptr = lutil_strcopy( ptr, b64.bv_val );
1984 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1985 "control", str, len );
1990 ber_memfree( b64.bv_val );
1993 /* known controls */
1994 for ( j = 0; tool_ctrl_response[j].oid != NULL; j++ ) {
1995 if ( strcmp( tool_ctrl_response[j].oid, ctrls[i]->ldctl_oid ) == 0 ) {
1996 if ( !tool_ctrl_response[j].mask & tool_type ) {
1997 /* this control should not appear
1998 * with this tool; warning? */
2004 if ( tool_ctrl_response[j].oid != NULL && tool_ctrl_response[j].func ) {
2005 (void)tool_ctrl_response[j].func( ld, ctrls[i] );
2011 tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
2015 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
2019 fputs( ldif, stdout );
2020 ber_memfree( ldif );
2026 tool_is_oid( const char *s )
2030 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2034 for ( ; s[ 0 ]; s++ ) {
2035 if ( s[ 0 ] == '.' ) {
2036 if ( s[ 1 ] == '\0' ) {
2043 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2047 if ( first == 1 && s[ 0 ] == '0' && s[ 1 ] != '.' ) {