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 },
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();
229 tool_common_usage( void )
231 static const char *const descriptions[] = {
232 N_(" -c continuous operation mode (do not stop on errors)\n"),
233 N_(" -d level set LDAP debugging level to `level'\n"),
234 N_(" -D binddn bind DN\n"),
235 N_(" -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
236 N_(" [!]assert=<filter> (a RFC 4515 Filter string)\n")
237 N_(" [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n")
238 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
240 /* non-advertized support for proxyDN */
241 N_(" [!]proxydn=<dn> (a RFC 4514 DN string)\n")
244 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
245 N_(" [!]chaining[=<resolveBehavior>[/<continuationBehavior>]]\n")
246 N_(" one of \"chainingPreferred\", \"chainingRequired\",\n")
247 N_(" \"referralsPreferred\", \"referralsRequired\"\n")
248 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
249 N_(" [!]manageDSAit\n")
251 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
254 N_(" [!]postread[=<attrs>] (a comma-separated attribute list)\n")
255 N_(" [!]preread[=<attrs>] (a comma-separated attribute list)\n")
257 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
258 N_(" [!]sessiontracking\n")
259 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
260 N_(" abandon, cancel, ignore (SIGINT sends abandon/cancel,\n"
261 " or ignores response; if critical, doesn't wait for SIGINT.\n"
262 " not really controls)\n")
263 N_(" -f file read operations from `file'\n"),
264 N_(" -h host LDAP server\n"),
265 N_(" -H URI LDAP Uniform Resource Identifier(s)\n"),
266 N_(" -I use SASL Interactive mode\n"),
267 N_(" -M enable Manage DSA IT control (-MM to make critical)\n"),
268 N_(" -n show what would be done but don't actually do it\n"),
269 N_(" -O props SASL security properties\n"),
270 N_(" -o <opt>[=<optparam] general options\n"),
271 N_(" nettimeout=<timeout> (in seconds, or \"none\" or \"max\")\n"),
272 N_(" -p port port on LDAP server\n"),
273 N_(" -P version protocol version (default: 3)\n"),
274 N_(" -Q use SASL Quiet mode\n"),
275 N_(" -R realm SASL realm\n"),
276 N_(" -U authcid SASL authentication identity\n"),
277 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
278 N_(" -V print version info (-VV only)\n"),
279 N_(" -w passwd bind password (for simple authentication)\n"),
280 N_(" -W prompt for bind password\n"),
281 N_(" -x Simple authentication\n"),
282 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
283 N_(" -y file Read password from file\n"),
284 N_(" -Y mech SASL mechanism\n"),
285 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
288 const char *const *cpp;
290 fputs( _("Common options:\n"), stderr );
291 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
292 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
293 fputs( _(*cpp), stderr );
306 fprintf( stderr, "%s: %s (%d)%s\n",
307 func, ldap_err2string( err ), err, extra ? extra : "" );
309 if ( matched && *matched ) {
310 fprintf( stderr, _("\tmatched DN: %s\n"), matched );
313 if ( info && *info ) {
314 fprintf( stderr, _("\tadditional info: %s\n"), info );
317 if ( refs && *refs ) {
319 fprintf( stderr, _("\treferrals:\n") );
320 for( i=0; refs[i]; i++ ) {
321 fprintf( stderr, "\t\t%s\n", refs[i] );
328 tool_args( int argc, char **argv )
332 while (( i = getopt( argc, argv, options )) != EOF ) {
334 char *control, *cvalue, *next;
336 case 'c': /* continuous operation mode */
343 ival = strtol( optarg, &next, 10 );
344 if (next == NULL || next[0] != '\0') {
345 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
350 case 'D': /* bind DN */
351 if( binddn != NULL ) {
352 fprintf( stderr, "%s: -D previously specified\n", prog );
353 exit( EXIT_FAILURE );
355 binddn = ber_strdup( optarg );
357 case 'e': /* general extensions (controls and such) */
358 /* should be extended to support comma separated list of
359 * [!]key[=value] parameters, e.g. -e !foo,bar=567
364 if( optarg[0] == '!' ) {
369 control = ber_strdup( optarg );
370 if ( (cvalue = strchr( control, '=' )) != NULL ) {
374 if ( strcasecmp( control, "assert" ) == 0 ) {
376 fprintf( stderr, "assert control previously specified\n");
377 exit( EXIT_FAILURE );
379 if( cvalue == NULL ) {
380 fprintf( stderr, "assert: control value expected\n" );
384 assertctl = 1 + crit;
386 assert( assertion == NULL );
389 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
390 if( authzid != NULL ) {
391 fprintf( stderr, "authzid control previously specified\n");
392 exit( EXIT_FAILURE );
394 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
395 if( proxydn != NULL ) {
396 fprintf( stderr, "authzid control incompatible with proxydn\n");
397 exit( EXIT_FAILURE );
399 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
400 if( cvalue == NULL ) {
401 fprintf( stderr, "authzid: control value expected\n" );
405 fprintf( stderr, "authzid: must be marked critical\n" );
409 assert( authzid == NULL );
412 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
413 } else if ( strcasecmp( control, "proxydn" ) == 0 ) {
414 if( proxydn != NULL ) {
415 fprintf( stderr, "proxydn control previously specified\n");
416 exit( EXIT_FAILURE );
418 if( authzid != NULL ) {
419 fprintf( stderr, "proxydn control incompatible with authzid\n");
420 exit( EXIT_FAILURE );
422 if( cvalue == NULL ) {
423 fprintf( stderr, "proxydn: control value expected\n" );
427 fprintf( stderr, "proxydn: must be marked critical\n" );
431 assert( proxydn == NULL );
433 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
435 } else if ( ( strcasecmp( control, "relax" ) == 0 ) ||
436 ( strcasecmp( control, "manageDIT" ) == 0 ) )
440 "relax control previously specified\n");
441 exit( EXIT_FAILURE );
443 if( cvalue != NULL ) {
445 "relax: no control value expected\n" );
449 manageDIT = 1 + crit;
451 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
454 "manageDSAit control previously specified\n");
455 exit( EXIT_FAILURE );
457 if( cvalue != NULL ) {
459 "manageDSAit: no control value expected\n" );
463 manageDSAit = 1 + crit;
465 } else if ( strcasecmp( control, "noop" ) == 0 ) {
467 fprintf( stderr, "noop control previously specified\n");
468 exit( EXIT_FAILURE );
470 if( cvalue != NULL ) {
471 fprintf( stderr, "noop: no control value expected\n" );
477 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
478 } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
480 fprintf( stderr, "ppolicy control previously specified\n");
481 exit( EXIT_FAILURE );
483 if( cvalue != NULL ) {
484 fprintf( stderr, "ppolicy: no control value expected\n" );
488 fprintf( stderr, "ppolicy: critical flag not allowed\n" );
495 } else if ( strcasecmp( control, "preread" ) == 0 ) {
497 fprintf( stderr, "preread control previously specified\n");
498 exit( EXIT_FAILURE );
502 preread_attrs = cvalue;
504 } else if ( strcasecmp( control, "postread" ) == 0 ) {
506 fprintf( stderr, "postread control previously specified\n");
507 exit( EXIT_FAILURE );
511 postread_attrs = cvalue;
513 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
514 } else if ( strcasecmp( control, "chaining" ) == 0 ) {
517 if ( cvalue != NULL ) {
520 continuation = strchr( cvalue, '/' );
521 if ( continuation ) {
522 /* FIXME: this makes sense only in searches */
523 *continuation++ = '\0';
524 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
525 chainingContinuation = LDAP_CHAINING_PREFERRED;
526 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
527 chainingContinuation = LDAP_CHAINING_REQUIRED;
528 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
529 chainingContinuation = LDAP_REFERRALS_PREFERRED;
530 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
531 chainingContinuation = LDAP_REFERRALS_REQUIRED;
534 "chaining behavior control "
535 "continuation value \"%s\" invalid\n",
537 exit( EXIT_FAILURE );
541 if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
542 chainingResolve = LDAP_CHAINING_PREFERRED;
543 } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
544 chainingResolve = LDAP_CHAINING_REQUIRED;
545 } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
546 chainingResolve = LDAP_REFERRALS_PREFERRED;
547 } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
548 chainingResolve = LDAP_REFERRALS_REQUIRED;
551 "chaining behavior control "
552 "resolve value \"%s\" invalid\n",
554 exit( EXIT_FAILURE );
557 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
559 /* this shouldn't go here, really; but it's a feature... */
560 } else if ( strcasecmp( control, "abandon" ) == 0 ) {
561 abcan = LDAP_REQ_ABANDON;
566 } else if ( strcasecmp( control, "cancel" ) == 0 ) {
567 abcan = LDAP_REQ_EXTENDED;
572 } else if ( strcasecmp( control, "ignore" ) == 0 ) {
578 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
579 } else if ( strcasecmp( control, "sessiontracking" ) == 0 ) {
580 if ( sessionTracking ) {
581 fprintf( stderr, "%s: session tracking can be only specified once\n", prog );
582 exit( EXIT_FAILURE );
585 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
587 } else if ( tool_is_oid( control ) ) {
588 LDAPControl *tmpctrls, ctrl;
590 tmpctrls = (LDAPControl *)realloc( unknown_ctrls,
591 (unknown_ctrls_num + 1)*sizeof( LDAPControl ) );
592 if ( tmpctrls == NULL ) {
593 fprintf( stderr, "%s: no memory?\n", prog );
594 exit( EXIT_FAILURE );
596 unknown_ctrls = tmpctrls;
597 ctrl.ldctl_oid = control;
598 ctrl.ldctl_value.bv_val = NULL;
599 ctrl.ldctl_value.bv_len = 0;
600 ctrl.ldctl_iscritical = crit;
602 if ( cvalue != NULL ) {
604 size_t len = strlen( cvalue );
607 bv.bv_len = LUTIL_BASE64_DECODE_LEN( len );
608 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
610 retcode = lutil_b64_pton( cvalue,
611 (unsigned char *)bv.bv_val,
614 if ( retcode == -1 || retcode > bv.bv_len ) {
615 fprintf( stderr, "Unable to parse value of general control %s\n",
621 ctrl.ldctl_value = bv;
624 unknown_ctrls[ unknown_ctrls_num ] = ctrl;
628 fprintf( stderr, "Invalid general control name: %s\n",
633 case 'f': /* read from file */
634 if( infile != NULL ) {
635 fprintf( stderr, "%s: -f previously specified\n", prog );
636 exit( EXIT_FAILURE );
638 infile = ber_strdup( optarg );
640 case 'h': /* ldap host */
641 if( ldaphost != NULL ) {
642 fprintf( stderr, "%s: -h previously specified\n", prog );
643 exit( EXIT_FAILURE );
645 ldaphost = ber_strdup( optarg );
647 case 'H': /* ldap URI */
648 if( ldapuri != NULL ) {
649 fprintf( stderr, "%s: -H previously specified\n", prog );
650 exit( EXIT_FAILURE );
652 ldapuri = ber_strdup( optarg );
655 #ifdef HAVE_CYRUS_SASL
656 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
657 fprintf( stderr, "%s: incompatible previous "
658 "authentication choice\n",
660 exit( EXIT_FAILURE );
662 authmethod = LDAP_AUTH_SASL;
663 sasl_flags = LDAP_SASL_INTERACTIVE;
666 fprintf( stderr, "%s: was not compiled with SASL support\n",
668 exit( EXIT_FAILURE );
671 /* enable Manage DSA IT */
674 case 'n': /* print operations, don't actually do them */
678 control = ber_strdup( optarg );
679 if ( (cvalue = strchr( control, '=' )) != NULL ) {
683 if ( strcasecmp( control, "nettimeout" ) == 0 ) {
684 if( nettimeout.tv_sec != -1 ) {
685 fprintf( stderr, "nettimeout option previously specified\n");
686 exit( EXIT_FAILURE );
688 if( cvalue == NULL || cvalue[0] == '\0' ) {
689 fprintf( stderr, "nettimeout: option value expected\n" );
692 if ( strcasecmp( cvalue, "none" ) == 0 ) {
693 nettimeout.tv_sec = 0;
694 } else if ( strcasecmp( cvalue, "max" ) == 0 ) {
695 nettimeout.tv_sec = LDAP_MAXINT;
697 ival = strtol( cvalue, &next, 10 );
698 if ( next == NULL || next[0] != '\0' ) {
700 _("Unable to parse network timeout \"%s\"\n"), cvalue );
701 exit( EXIT_FAILURE );
703 nettimeout.tv_sec = ival;
705 if( nettimeout.tv_sec < 0 || nettimeout.tv_sec > LDAP_MAXINT ) {
706 fprintf( stderr, _("%s: invalid network timeout (%ld) specified\n"),
707 prog, (long)nettimeout.tv_sec );
708 exit( EXIT_FAILURE );
711 fprintf( stderr, "Invalid general option name: %s\n",
717 #ifdef HAVE_CYRUS_SASL
718 if( sasl_secprops != NULL ) {
719 fprintf( stderr, "%s: -O previously specified\n", prog );
720 exit( EXIT_FAILURE );
722 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
723 fprintf( stderr, "%s: incompatible previous "
724 "authentication choice\n", prog );
725 exit( EXIT_FAILURE );
727 authmethod = LDAP_AUTH_SASL;
728 sasl_secprops = ber_strdup( optarg );
730 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
731 exit( EXIT_FAILURE );
736 fprintf( stderr, "%s: -p previously specified\n", prog );
737 exit( EXIT_FAILURE );
739 ival = strtol( optarg, &next, 10 );
740 if ( next == NULL || next[0] != '\0' ) {
741 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
742 exit( EXIT_FAILURE );
747 ival = strtol( optarg, &next, 10 );
748 if ( next == NULL || next[0] != '\0' ) {
749 fprintf( stderr, "%s: unabel to parse protocol version \"%s\"\n", prog, optarg );
750 exit( EXIT_FAILURE );
754 if( protocol == LDAP_VERSION3 ) {
755 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
757 exit( EXIT_FAILURE );
759 protocol = LDAP_VERSION2;
762 if( protocol == LDAP_VERSION2 ) {
763 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
765 exit( EXIT_FAILURE );
767 protocol = LDAP_VERSION3;
770 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
776 #ifdef HAVE_CYRUS_SASL
777 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
778 fprintf( stderr, "%s: incompatible previous "
779 "authentication choice\n",
781 exit( EXIT_FAILURE );
783 authmethod = LDAP_AUTH_SASL;
784 sasl_flags = LDAP_SASL_QUIET;
787 fprintf( stderr, "%s: not compiled with SASL support\n",
789 exit( EXIT_FAILURE );
792 #ifdef HAVE_CYRUS_SASL
793 if( sasl_realm != NULL ) {
794 fprintf( stderr, "%s: -R previously specified\n", prog );
795 exit( EXIT_FAILURE );
797 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
798 fprintf( stderr, "%s: incompatible previous "
799 "authentication choice\n",
801 exit( EXIT_FAILURE );
803 authmethod = LDAP_AUTH_SASL;
804 sasl_realm = ber_strdup( optarg );
806 fprintf( stderr, "%s: not compiled with SASL support\n",
808 exit( EXIT_FAILURE );
812 #ifdef HAVE_CYRUS_SASL
813 if( sasl_authc_id != NULL ) {
814 fprintf( stderr, "%s: -U previously specified\n", prog );
815 exit( EXIT_FAILURE );
817 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
818 fprintf( stderr, "%s: incompatible previous "
819 "authentication choice\n",
821 exit( EXIT_FAILURE );
823 authmethod = LDAP_AUTH_SASL;
824 sasl_authc_id = ber_strdup( optarg );
826 fprintf( stderr, "%s: not compiled with SASL support\n",
828 exit( EXIT_FAILURE );
831 case 'v': /* verbose mode */
834 case 'V': /* version */
837 case 'w': /* password */
838 passwd.bv_val = ber_strdup( optarg );
842 for( p = optarg; *p != '\0'; p++ ) {
846 passwd.bv_len = strlen( passwd.bv_val );
855 #ifdef HAVE_CYRUS_SASL
856 if( sasl_mech != NULL ) {
857 fprintf( stderr, "%s: -Y previously specified\n", prog );
858 exit( EXIT_FAILURE );
860 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
862 "%s: incompatible with authentication choice\n", prog );
863 exit( EXIT_FAILURE );
865 authmethod = LDAP_AUTH_SASL;
866 sasl_mech = ber_strdup( optarg );
868 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
869 exit( EXIT_FAILURE );
873 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
874 fprintf( stderr, "%s: incompatible with previous "
875 "authentication choice\n", prog );
876 exit( EXIT_FAILURE );
878 authmethod = LDAP_AUTH_SIMPLE;
881 #ifdef HAVE_CYRUS_SASL
882 if( sasl_authz_id != NULL ) {
883 fprintf( stderr, "%s: -X previously specified\n", prog );
884 exit( EXIT_FAILURE );
886 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
887 fprintf( stderr, "%s: -X incompatible with "
888 "authentication choice\n", prog );
889 exit( EXIT_FAILURE );
891 authmethod = LDAP_AUTH_SASL;
892 sasl_authz_id = ber_strdup( optarg );
894 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
895 exit( EXIT_FAILURE );
902 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
903 exit( EXIT_FAILURE );
907 if( handle_private_option( i ) ) break;
908 fprintf( stderr, "%s: unrecognized option -%c\n",
915 /* prevent bad linking */
917 api.ldapai_info_version = LDAP_API_INFO_VERSION;
919 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
920 != LDAP_OPT_SUCCESS )
922 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
923 exit( EXIT_FAILURE );
926 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
927 fprintf( stderr, "LDAP APIInfo version mismatch: "
928 "library %d, header %d\n",
929 api.ldapai_info_version, LDAP_API_INFO_VERSION );
930 exit( EXIT_FAILURE );
933 if( api.ldapai_api_version != LDAP_API_VERSION ) {
934 fprintf( stderr, "LDAP API version mismatch: "
935 "library %d, header %d\n",
936 api.ldapai_api_version, LDAP_API_VERSION );
937 exit( EXIT_FAILURE );
940 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
941 fprintf( stderr, "LDAP vendor name mismatch: "
942 "library %s, header %s\n",
943 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
944 exit( EXIT_FAILURE );
947 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
948 fprintf( stderr, "LDAP vendor version mismatch: "
949 "library %d, header %d\n",
950 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
951 exit( EXIT_FAILURE );
955 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
957 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
958 if (version > 1) exit( EXIT_SUCCESS );
961 ldap_memfree( api.ldapai_vendor_name );
962 ber_memvfree( (void **)api.ldapai_extensions );
966 protocol = LDAP_VERSION3;
968 if (authmethod == -1 && protocol > LDAP_VERSION2) {
969 #ifdef HAVE_CYRUS_SASL
970 authmethod = LDAP_AUTH_SASL;
972 authmethod = LDAP_AUTH_SIMPLE;
976 if( ldapuri == NULL ) {
977 if( ldapport && ( ldaphost == NULL )) {
978 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
979 exit( EXIT_FAILURE );
982 if( ldaphost != NULL ) {
983 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
984 exit( EXIT_FAILURE );
987 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
988 exit( EXIT_FAILURE );
992 if( protocol == LDAP_VERSION2 ) {
993 if( assertctl || authzid || manageDIT || manageDSAit ||
994 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
996 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
997 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1000 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1002 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1003 noop || ppolicy || preread || postread )
1005 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
1006 exit( EXIT_FAILURE );
1010 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
1011 exit( EXIT_FAILURE );
1014 #ifdef HAVE_CYRUS_SASL
1015 if( authmethod == LDAP_AUTH_SASL ) {
1016 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
1018 exit( EXIT_FAILURE );
1026 tool_conn_setup( int dont, void (*private_setup)( LDAP * ) )
1031 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
1032 != LBER_OPT_SUCCESS )
1035 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
1037 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
1038 != LDAP_OPT_SUCCESS )
1041 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
1046 (void) SIGNAL( SIGPIPE, SIG_IGN );
1050 SIGNAL( SIGINT, do_sig );
1056 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
1059 memset( &url, 0, sizeof(url));
1061 url.lud_scheme = "ldap";
1062 url.lud_host = ldaphost;
1063 url.lud_port = ldapport;
1064 url.lud_scope = LDAP_SCOPE_DEFAULT;
1066 ldapuri = ldap_url_desc2str( &url );
1068 } else if ( ldapuri != NULL ) {
1069 LDAPURLDesc *ludlist, **ludp;
1073 rc = ldap_url_parselist( &ludlist, ldapuri );
1074 if ( rc != LDAP_URL_SUCCESS ) {
1076 "Could not parse LDAP URI(s)=%s (%d)\n",
1078 exit( EXIT_FAILURE );
1081 for ( ludp = &ludlist; *ludp != NULL; ) {
1082 LDAPURLDesc *lud = *ludp;
1085 if ( lud->lud_dn != NULL && lud->lud_dn[ 0 ] != '\0' &&
1086 ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) )
1088 /* if no host but a DN is provided,
1089 * use DNS SRV to gather the host list
1090 * and turn it into a list of URIs
1091 * using the scheme provided */
1092 char *domain = NULL,
1096 len_proto = strlen( lud->lud_scheme );
1098 if ( ldap_dn2domain( lud->lud_dn, &domain )
1102 "DNS SRV: Could not turn "
1103 "DN=\"%s\" into a domain\n",
1108 rc = ldap_domain2hostlist( domain, &hostlist );
1111 "DNS SRV: Could not turn "
1112 "domain=%s into a hostlist\n",
1117 hosts = ldap_str2charray( hostlist, " " );
1118 if ( hosts == NULL ) {
1120 "DNS SRV: Could not parse "
1121 "hostlist=\"%s\"\n",
1126 for ( i = 0; hosts[ i ] != NULL; i++ )
1129 tmp = (char **)realloc( urls, sizeof( char * ) * ( nurls + i + 1 ) );
1130 if ( tmp == NULL ) {
1132 "DNS SRV: out of memory?\n" );
1136 urls[ nurls ] = NULL;
1138 for ( i = 0; hosts[ i ] != NULL; i++ ) {
1139 size_t len = len_proto
1141 + strlen( hosts[ i ] )
1144 urls[ nurls + i + 1 ] = NULL;
1145 urls[ nurls + i ] = (char *)malloc( sizeof( char ) * len );
1146 if ( urls[ nurls + i ] == NULL ) {
1148 "DNS SRV: out of memory?\n" );
1152 snprintf( urls[ nurls + i ], len, "%s://%s",
1153 lud->lud_scheme, hosts[ i ] );
1158 ber_memvfree( (void **)hosts );
1159 ber_memfree( hostlist );
1160 ber_memfree( domain );
1163 tmp = (char **)realloc( urls, sizeof( char * ) * ( nurls + 2 ) );
1164 if ( tmp == NULL ) {
1166 "DNS SRV: out of memory?\n" );
1170 urls[ nurls + 1 ] = NULL;
1172 urls[ nurls ] = ldap_url_desc2str( lud );
1173 if ( urls[ nurls ] == NULL ) {
1175 "DNS SRV: out of memory?\n" );
1181 *ludp = lud->lud_next;
1183 lud->lud_next = NULL;
1184 ldap_free_urldesc( lud );
1187 if ( ludlist != NULL ) {
1188 ldap_free_urllist( ludlist );
1189 exit( EXIT_FAILURE );
1191 } else if ( urls == NULL ) {
1192 exit( EXIT_FAILURE );
1195 ldap_memfree( ldapuri );
1196 ldapuri = ldap_charray2str( urls, " " );
1197 ber_memvfree( (void **)urls );
1201 fprintf( stderr, "ldap_initialize( %s )\n",
1202 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
1204 rc = ldap_initialize( &ld, ldapuri );
1205 if( rc != LDAP_SUCCESS ) {
1207 "Could not create LDAP session handle for URI=%s (%d): %s\n",
1208 ldapuri, rc, ldap_err2string(rc) );
1209 exit( EXIT_FAILURE );
1212 if( private_setup ) private_setup( ld );
1215 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
1216 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
1218 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
1219 referrals ? "on" : "off" );
1220 exit( EXIT_FAILURE );
1223 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
1224 != LDAP_OPT_SUCCESS )
1226 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
1228 exit( EXIT_FAILURE );
1232 rc = ldap_start_tls_s( ld, NULL, NULL );
1233 if ( rc != LDAP_SUCCESS ) {
1234 tool_perror( "ldap_start_tls", rc, NULL, NULL, NULL, NULL );
1235 if ( use_tls > 1 ) {
1236 exit( EXIT_FAILURE );
1241 if ( nettimeout.tv_sec > 0 ) {
1242 if ( ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &nettimeout )
1243 != LDAP_OPT_SUCCESS )
1245 fprintf( stderr, "Could not set LDAP_OPT_NETWORK_TIMEOUT %ld\n",
1246 (long)nettimeout.tv_sec );
1247 exit( EXIT_FAILURE );
1257 tool_bind( LDAP *ld )
1259 LDAPControl **sctrlsp = NULL;
1260 LDAPControl *sctrls[3];
1261 LDAPControl sctrl[3];
1264 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1267 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1268 c.ldctl_value.bv_val = NULL;
1269 c.ldctl_value.bv_len = 0;
1270 c.ldctl_iscritical = 0;
1272 sctrls[nsctrls] = &sctrl[nsctrls];
1273 sctrls[++nsctrls] = NULL;
1277 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1278 if ( sessionTracking ) {
1281 if (stValue.bv_val == NULL && st_value( ld, &stValue ) ) {
1282 exit( EXIT_FAILURE );
1285 c.ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1286 c.ldctl_iscritical = 0;
1287 ber_dupbv( &c.ldctl_value, &stValue );
1290 sctrls[nsctrls] = &sctrl[nsctrls];
1291 sctrls[++nsctrls] = NULL;
1293 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1299 assert( nsctrls < sizeof(sctrls)/sizeof(sctrls[0]) );
1301 if ( authmethod == LDAP_AUTH_SASL ) {
1302 #ifdef HAVE_CYRUS_SASL
1306 if( sasl_secprops != NULL ) {
1307 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
1308 (void *) sasl_secprops );
1310 if( rc != LDAP_OPT_SUCCESS ) {
1312 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
1314 exit( LDAP_LOCAL_ERROR );
1318 defaults = lutil_sasl_defaults( ld,
1325 rc = ldap_sasl_interactive_bind_s( ld, binddn, sasl_mech,
1327 NULL, sasl_flags, lutil_sasl_interact, defaults );
1329 lutil_sasl_freedefs( defaults );
1330 if( rc != LDAP_SUCCESS ) {
1331 tool_perror( "ldap_sasl_interactive_bind_s",
1332 rc, NULL, NULL, NULL, NULL );
1336 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1337 exit( LDAP_NOT_SUPPORTED );
1341 LDAPMessage *result;
1342 LDAPControl **ctrls;
1344 char *matched = NULL;
1352 rc = ldap_sasl_bind( ld, binddn, LDAP_SASL_SIMPLE, &passwd,
1353 sctrlsp, NULL, &msgid );
1354 if ( msgid == -1 ) {
1355 tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
1356 NULL, NULL, NULL, NULL );
1361 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 ) {
1362 tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
1363 exit( LDAP_LOCAL_ERROR );
1366 rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
1368 if ( rc != LDAP_SUCCESS ) {
1369 tool_perror( "ldap_bind parse result", rc, NULL, matched, info, refs );
1370 exit( LDAP_LOCAL_ERROR );
1373 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1374 if ( ctrls && ppolicy ) {
1376 int expire, grace, len = 0;
1377 LDAPPasswordPolicyError pErr = -1;
1379 ctrl = ldap_control_find( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
1382 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
1383 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1385 if ( pErr != PP_noError ){
1388 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1389 len = strlen( msgbuf );
1391 if ( expire >= 0 ) {
1392 sprintf( msgbuf+len,
1393 " (Password expires in %d seconds)",
1395 } else if ( grace >= 0 ) {
1396 sprintf( msgbuf+len,
1397 " (Password expired, %d grace logins remain)",
1405 ldap_controls_free( ctrls );
1408 if ( err != LDAP_SUCCESS
1410 || ( matched && matched[ 0 ] )
1411 || ( info && info[ 0 ] )
1414 tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1416 if( matched ) ber_memfree( matched );
1417 if( info ) ber_memfree( info );
1418 if( refs ) ber_memvfree( (void **)refs );
1420 if ( err != LDAP_SUCCESS ) exit( err );
1426 tool_unbind( LDAP *ld )
1428 int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1430 if ( err != LDAP_OPT_SUCCESS ) {
1431 fprintf( stderr, "Could not unset controls\n");
1434 (void) ldap_unbind_ext( ld, NULL, NULL );
1438 /* Set server controls. Add controls extra_c[0..count-1], if set. */
1440 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1442 int i = 0, j, crit = 0, err;
1443 LDAPControl c[16], **ctrls;
1447 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1449 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1453 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1458 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1460 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1461 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1463 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1465 || unknown_ctrls_num ) )
1470 ctrls = (LDAPControl**) malloc(sizeof(c) + (count + unknown_ctrls_num + 1)*sizeof(LDAPControl*));
1471 if ( ctrls == NULL ) {
1472 fprintf( stderr, "No memory\n" );
1473 exit( EXIT_FAILURE );
1477 BerElementBuffer berbuf;
1478 BerElement *ber = (BerElement *)&berbuf;
1480 if( assertion == NULL || *assertion == '\0' ) {
1481 fprintf( stderr, "Assertion=<empty>\n" );
1482 exit( EXIT_FAILURE );
1485 ber_init2( ber, NULL, LBER_USE_DER );
1487 err = ldap_pvt_put_filter( ber, assertion );
1489 fprintf( stderr, "assertion encode failed (%d)\n", err );
1490 exit( EXIT_FAILURE );
1493 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1495 fprintf( stderr, "assertion flatten failed (%d)\n", err );
1496 exit( EXIT_FAILURE );
1499 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1500 c[i].ldctl_iscritical = assertctl > 1;
1506 c[i].ldctl_value.bv_val = authzid;
1507 c[i].ldctl_value.bv_len = strlen( authzid );
1508 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1509 c[i].ldctl_iscritical = 1;
1514 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1515 /* NOTE: doesn't need an extra count because it's incompatible
1518 BerElementBuffer berbuf;
1519 BerElement *ber = (BerElement *)&berbuf;
1521 ber_init2( ber, NULL, LBER_USE_DER );
1523 if ( ber_printf( ber, "s", proxydn ) == LBER_ERROR ) {
1524 exit( EXIT_FAILURE );
1527 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1528 exit( EXIT_FAILURE );
1531 c[i].ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1532 c[i].ldctl_iscritical = 1;
1536 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1539 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1540 BER_BVZERO( &c[i].ldctl_value );
1541 c[i].ldctl_iscritical = manageDIT > 1;
1546 if ( manageDSAit ) {
1547 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1548 BER_BVZERO( &c[i].ldctl_value );
1549 c[i].ldctl_iscritical = manageDSAit > 1;
1555 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1556 BER_BVZERO( &c[i].ldctl_value );
1557 c[i].ldctl_iscritical = noop > 1;
1562 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1564 c[i].ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1565 BER_BVZERO( &c[i].ldctl_value );
1566 c[i].ldctl_iscritical = 0;
1573 char berbuf[LBER_ELEMENT_SIZEOF];
1574 BerElement *ber = (BerElement *)berbuf;
1575 char **attrs = NULL;
1577 if( preread_attrs ) {
1578 attrs = ldap_str2charray( preread_attrs, "," );
1581 ber_init2( ber, NULL, LBER_USE_DER );
1583 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1584 fprintf( stderr, "preread attrs encode failed.\n" );
1585 exit( EXIT_FAILURE );
1588 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1590 fprintf( stderr, "preread flatten failed (%d)\n", err );
1591 exit( EXIT_FAILURE );
1594 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1595 c[i].ldctl_iscritical = preread > 1;
1599 if( attrs ) ldap_charray_free( attrs );
1603 char berbuf[LBER_ELEMENT_SIZEOF];
1604 BerElement *ber = (BerElement *)berbuf;
1605 char **attrs = NULL;
1607 if( postread_attrs ) {
1608 attrs = ldap_str2charray( postread_attrs, "," );
1611 ber_init2( ber, NULL, LBER_USE_DER );
1613 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1614 fprintf( stderr, "postread attrs encode failed.\n" );
1615 exit( EXIT_FAILURE );
1618 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1620 fprintf( stderr, "postread flatten failed (%d)\n", err );
1621 exit( EXIT_FAILURE );
1624 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1625 c[i].ldctl_iscritical = postread > 1;
1629 if( attrs ) ldap_charray_free( attrs );
1632 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1634 if ( chainingResolve > -1 ) {
1635 BerElementBuffer berbuf;
1636 BerElement *ber = (BerElement *)&berbuf;
1638 ber_init2( ber, NULL, LBER_USE_DER );
1640 err = ber_printf( ber, "{e" /* } */, chainingResolve );
1643 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1644 exit( EXIT_FAILURE );
1647 if ( chainingContinuation > -1 ) {
1648 err = ber_printf( ber, "e", chainingContinuation );
1651 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1652 exit( EXIT_FAILURE );
1656 err = ber_printf( ber, /* { */ "N}" );
1659 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1660 exit( EXIT_FAILURE );
1663 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1664 exit( EXIT_FAILURE );
1668 BER_BVZERO( &c[i].ldctl_value );
1671 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1672 c[i].ldctl_iscritical = chaining > 1;
1676 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1678 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1679 if ( sessionTracking ) {
1680 if ( stValue.bv_val == NULL && st_value( ld, &stValue ) ) {
1681 exit( EXIT_FAILURE );
1684 c[i].ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1685 c[i].ldctl_iscritical = 0;
1686 ber_dupbv( &c[i].ldctl_value, &stValue );
1691 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1694 ctrls[i++] = extra_c++;
1696 for ( count = 0; count < unknown_ctrls_num; count++ ) {
1697 ctrls[i++] = &unknown_ctrls[count];
1701 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1703 if ( err != LDAP_OPT_SUCCESS ) {
1704 for ( j = 0; j < i; j++ ) {
1705 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1707 fprintf( stderr, "Could not set %scontrols\n",
1708 crit ? "critical " : "" );
1713 exit( EXIT_FAILURE );
1718 tool_check_abandon( LDAP *ld, int msgid )
1722 switch ( gotintr ) {
1723 case LDAP_REQ_EXTENDED:
1724 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1725 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1726 rc, ldap_err2string( rc ) );
1729 case LDAP_REQ_ABANDON:
1730 rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
1731 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1732 rc, ldap_err2string( rc ) );
1736 /* just unbind, ignoring the request */
1744 print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
1749 tool_write_ldif( LDIF_PUT_COMMENT, "==> ",
1750 what->bv_val, what->bv_len );
1751 ber = ber_init( &ctrl->ldctl_value );
1752 if ( ber == NULL ) {
1756 } else if ( ber_scanf( ber, "{m{" /*}}*/, &bv ) == LBER_ERROR ) {
1761 tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1763 while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
1765 BerVarray vals = NULL;
1767 if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
1774 for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
1776 ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1777 bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
1780 ber_bvarray_free( vals );
1784 if ( ber != NULL ) {
1788 tool_write_ldif( LDIF_PUT_COMMENT, "<== ",
1789 what->bv_val, what->bv_len );
1795 print_preread( LDAP *ld, LDAPControl *ctrl )
1797 static struct berval what = BER_BVC( "preread" );
1799 return print_prepostread( ld, ctrl, &what );
1803 print_postread( LDAP *ld, LDAPControl *ctrl )
1805 static struct berval what = BER_BVC( "postread" );
1807 return print_prepostread( ld, ctrl, &what );
1811 print_paged_results( LDAP *ld, LDAPControl *ctrl )
1815 /* note: pr_cookie is being malloced; it's freed
1816 * the next time the control is sent, but the last
1817 * time it's not; we don't care too much, because
1818 * the last time an empty value is returned... */
1819 if ( ldap_parse_pageresponse_control( ld, ctrl, &estimate, &pr_cookie )
1826 /* FIXME: check buffer overflow */
1827 char buf[ BUFSIZ ], *ptr = buf;
1829 if ( estimate > 0 ) {
1830 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1831 "estimate=%d", estimate );
1834 if ( pr_cookie.bv_len > 0 ) {
1837 bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
1838 pr_cookie.bv_len ) + 1;
1839 bv.bv_val = ber_memalloc( bv.bv_len + 1 );
1841 bv.bv_len = lutil_b64_ntop(
1842 (unsigned char *) pr_cookie.bv_val,
1844 bv.bv_val, bv.bv_len );
1846 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1847 "%scookie=%s", ptr == buf ? "" : " ",
1850 ber_memfree( bv.bv_val );
1852 pr_morePagedResults = 1;
1855 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1856 "%scookie=", ptr == buf ? "" : " " );
1859 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1860 "pagedresults", buf, ptr - buf );
1866 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1868 print_ppolicy( LDAP *ld, LDAPControl *ctrl )
1870 int expire = 0, grace = 0, rc;
1871 LDAPPasswordPolicyError pperr;
1873 rc = ldap_parse_passwordpolicy_control( ld, ctrl,
1874 &expire, &grace, &pperr );
1875 if ( rc == LDAP_SUCCESS ) {
1876 char buf[ BUFSIZ ], *ptr = buf;
1878 if ( expire != -1 ) {
1879 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1880 "expire=%d", expire );
1883 if ( grace != -1 ) {
1884 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1885 "%sgrace=%d", ptr == buf ? "" : " ", grace );
1888 if ( pperr != PP_noError ) {
1889 ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1890 "%serror=%d (%s)", ptr == buf ? "" : " ",
1892 ldap_passwordpolicy_err2txt( pperr ) );
1895 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1896 "ppolicy", buf, ptr - buf );
1903 void tool_print_ctrls(
1905 LDAPControl **ctrls )
1910 for ( i = 0; ctrls[i] != NULL; i++ ) {
1911 /* control: OID criticality base64value */
1912 struct berval b64 = BER_BVNULL;
1917 /* FIXME: there might be cases where a control has NULL OID;
1918 * this makes little sense, especially when returned by the
1919 * server, but libldap happily allows it */
1920 if ( ctrls[i]->ldctl_oid == NULL ) {
1925 len += strlen( ctrls[i]->ldctl_oid );
1927 /* add enough for space after OID and the critical value itself */
1928 len += ctrls[i]->ldctl_iscritical
1929 ? sizeof("true") : sizeof("false");
1931 /* convert to base64 */
1932 if ( !BER_BVISNULL( &ctrls[i]->ldctl_value ) ) {
1933 b64.bv_len = LUTIL_BASE64_ENCODE_LEN(
1934 ctrls[i]->ldctl_value.bv_len ) + 1;
1935 b64.bv_val = ber_memalloc( b64.bv_len + 1 );
1937 b64.bv_len = lutil_b64_ntop(
1938 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
1939 ctrls[i]->ldctl_value.bv_len,
1940 b64.bv_val, b64.bv_len );
1944 len += 1 + b64.bv_len;
1947 ptr = str = malloc( len + 1 );
1949 ptr = lutil_strcopy( ptr, ": " );
1951 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_oid );
1952 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_iscritical
1953 ? " true" : " false" );
1956 ptr = lutil_strcopy( ptr, " " );
1957 ptr = lutil_strcopy( ptr, b64.bv_val );
1961 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1962 "control", str, len );
1967 ber_memfree( b64.bv_val );
1970 /* known controls */
1971 for ( j = 0; tool_ctrl_response[j].oid != NULL; j++ ) {
1972 if ( strcmp( tool_ctrl_response[j].oid, ctrls[i]->ldctl_oid ) == 0 ) {
1973 if ( !tool_ctrl_response[j].mask & tool_type ) {
1974 /* this control should not appear
1975 * with this tool; warning? */
1981 if ( tool_ctrl_response[j].oid != NULL && tool_ctrl_response[j].func ) {
1982 (void)tool_ctrl_response[j].func( ld, ctrls[i] );
1988 tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
1992 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1996 fputs( ldif, stdout );
1997 ber_memfree( ldif );
2003 tool_is_oid( const char *s )
2007 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2011 for ( ; s[ 0 ]; s++ ) {
2012 if ( s[ 0 ] == '.' ) {
2013 if ( s[ 1 ] == '\0' ) {
2020 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2024 if ( first == 1 && s[ 0 ] == '0' && s[ 1 ] != '.' ) {