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-2003 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>.
18 /* This file was initially created by Hallvard B. Furuseth based (in
19 * part) upon argument parsing code for individual tools located in
20 * this directory. Additional contributors include:
21 * Kurt D. Zeilenga (additional common argument and control support)
28 #include <ac/stdlib.h>
29 #include <ac/signal.h>
30 #include <ac/string.h>
31 #include <ac/unistd.h>
36 #include "lutil_ldap.h"
37 #include "ldap_defaults.h"
50 char *ldaphost = NULL;
52 #ifdef HAVE_CYRUS_SASL
53 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
54 char *sasl_realm = NULL;
55 char *sasl_authc_id = NULL;
56 char *sasl_authz_id = NULL;
57 char *sasl_mech = NULL;
58 char *sasl_secprops = NULL;
63 char *assertion = NULL;
68 char *preread_attrs = NULL;
70 char *postread_attrs = NULL;
74 struct berval passwd = { 0, NULL };
87 ldap_pvt_setlocale(LC_MESSAGES, "");
88 ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
89 ldap_pvt_textdomain(OPENLDAP_PACKAGE);
93 tool_common_usage( void )
95 static const char *const descriptions[] = {
96 N_(" -c continuous operation mode (do not stop on errors)\n"),
97 N_(" -C chase referrals\n"),
98 N_(" -d level set LDAP debugging level to `level'\n"),
99 N_(" -D binddn bind DN\n"),
100 N_(" -e [!]<ctrl>[=<ctrlparam>] general controls (! indicates criticality)\n")
101 N_(" [!]assert=<filter> (an RFC 2254 Filter)\n")
102 N_(" [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n")
103 N_(" [!]manageDSAit\n")
105 N_(" [!]postread[=<attrs>] (a comma-separated attribute list)\n"),
106 N_(" [!]preread[=<attrs>] (a comma-separated attribute list)\n"),
107 N_(" -f file read operations from `file'\n"),
108 N_(" -h host LDAP server\n"),
109 N_(" -H URI LDAP Uniform Resource Indentifier(s)\n"),
110 N_(" -I use SASL Interactive mode\n"),
111 N_(" -k use Kerberos authentication\n"),
112 N_(" -K like -k, but do only step 1 of the Kerberos bind\n"),
113 N_(" -M enable Manage DSA IT control (-MM to make critical)\n"),
114 N_(" -n show what would be done but don't actually do it\n"),
115 N_(" -O props SASL security properties\n"),
116 N_(" -p port port on LDAP server\n"),
117 N_(" -P version procotol version (default: 3)\n"),
118 N_(" -Q use SASL Quiet mode\n"),
119 N_(" -R realm SASL realm\n"),
120 N_(" -U authcid SASL authentication identity\n"),
121 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
122 N_(" -V print version info (-VV only)\n"),
123 N_(" -w passwd bind password (for simple authentication)\n"),
124 N_(" -W prompt for bind password\n"),
125 N_(" -x Simple authentication\n"),
126 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
127 N_(" -y file Read password from file\n"),
128 N_(" -Y mech SASL mechanism\n"),
129 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
132 const char *const *cpp;
134 fputs( _("Common options:\n"), stderr );
135 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
136 if( strchr( options, (*cpp)[3] ) ) {
137 fputs( _(*cpp), stderr );
144 tool_args( int argc, char **argv )
148 while (( i = getopt( argc, argv, options )) != EOF ) {
150 char *control, *cvalue;
152 case 'c': /* continuous operation mode */
159 debug |= atoi( optarg );
161 case 'D': /* bind DN */
162 if( binddn != NULL ) {
163 fprintf( stderr, "%s: -D previously specified\n", prog );
164 exit( EXIT_FAILURE );
166 binddn = ber_strdup( optarg );
168 case 'e': /* general controls */
169 /* should be extended to support comma separated list of
170 * [!]key[=value] parameters, e.g. -e !foo,bar=567
175 if( optarg[0] == '!' ) {
180 control = ber_strdup( optarg );
181 if ( (cvalue = strchr( control, '=' )) != NULL ) {
185 if ( strcasecmp( control, "assert" ) == 0 ) {
187 fprintf( stderr, "assert control previously specified\n");
188 exit( EXIT_FAILURE );
190 if( cvalue == NULL ) {
191 fprintf( stderr, "assert: control value expected\n" );
195 assertctl = 1 + crit;
197 assert( assertion == NULL );
200 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
201 if( authzid != NULL ) {
202 fprintf( stderr, "authzid control previously specified\n");
203 exit( EXIT_FAILURE );
205 if( cvalue == NULL ) {
206 fprintf( stderr, "authzid: control value expected\n" );
210 fprintf( stderr, "authzid: must be marked critical\n" );
214 assert( authzid == NULL );
217 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
220 "manageDSAit control previously specified\n");
221 exit( EXIT_FAILURE );
223 if( cvalue != NULL ) {
225 "manageDSAit: no control value expected\n" );
229 manageDSAit = 1 + crit;
231 } else if ( strcasecmp( control, "noop" ) == 0 ) {
233 fprintf( stderr, "noop control previously specified\n");
234 exit( EXIT_FAILURE );
236 if( cvalue != NULL ) {
237 fprintf( stderr, "noop: no control value expected\n" );
243 } else if ( strcasecmp( control, "preread" ) == 0 ) {
245 fprintf( stderr, "preread control previously specified\n");
246 exit( EXIT_FAILURE );
250 preread_attrs = cvalue;
252 } else if ( strcasecmp( control, "postread" ) == 0 ) {
254 fprintf( stderr, "postread control previously specified\n");
255 exit( EXIT_FAILURE );
259 postread_attrs = cvalue;
262 fprintf( stderr, "Invalid general control name: %s\n",
267 case 'f': /* read from file */
268 if( infile != NULL ) {
269 fprintf( stderr, "%s: -f previously specified\n", prog );
270 exit( EXIT_FAILURE );
272 infile = ber_strdup( optarg );
274 case 'h': /* ldap host */
275 if( ldaphost != NULL ) {
276 fprintf( stderr, "%s: -h previously specified\n", prog );
277 exit( EXIT_FAILURE );
279 ldaphost = ber_strdup( optarg );
281 case 'H': /* ldap URI */
282 if( ldapuri != NULL ) {
283 fprintf( stderr, "%s: -H previously specified\n", prog );
284 exit( EXIT_FAILURE );
286 ldapuri = ber_strdup( optarg );
289 #ifdef HAVE_CYRUS_SASL
290 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
291 fprintf( stderr, "%s: incompatible previous "
292 "authentication choice\n",
294 exit( EXIT_FAILURE );
296 authmethod = LDAP_AUTH_SASL;
297 sasl_flags = LDAP_SASL_INTERACTIVE;
300 fprintf( stderr, "%s: was not compiled with SASL support\n",
302 exit( EXIT_FAILURE );
304 case 'k': /* kerberos bind */
305 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
306 if( authmethod != -1 ) {
307 fprintf( stderr, "%s: -k incompatible with previous "
308 "authentication choice\n", prog );
309 exit( EXIT_FAILURE );
311 authmethod = LDAP_AUTH_KRBV4;
313 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
314 exit( EXIT_FAILURE );
317 case 'K': /* kerberos bind, part one only */
318 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
319 if( authmethod != -1 ) {
320 fprintf( stderr, "%s: incompatible with previous "
321 "authentication choice\n", prog );
322 exit( EXIT_FAILURE );
324 authmethod = LDAP_AUTH_KRBV41;
326 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
327 exit( EXIT_FAILURE );
331 /* enable Manage DSA IT */
334 case 'n': /* print operations, don't actually do them */
338 #ifdef HAVE_CYRUS_SASL
339 if( sasl_secprops != NULL ) {
340 fprintf( stderr, "%s: -O previously specified\n", prog );
341 exit( EXIT_FAILURE );
343 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
344 fprintf( stderr, "%s: incompatible previous "
345 "authentication choice\n", prog );
346 exit( EXIT_FAILURE );
348 authmethod = LDAP_AUTH_SASL;
349 sasl_secprops = ber_strdup( optarg );
351 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
352 exit( EXIT_FAILURE );
357 fprintf( stderr, "%s: -p previously specified\n", prog );
358 exit( EXIT_FAILURE );
360 ldapport = atoi( optarg );
363 switch( atoi(optarg) ) {
365 if( protocol == LDAP_VERSION3 ) {
366 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
368 exit( EXIT_FAILURE );
370 protocol = LDAP_VERSION2;
373 if( protocol == LDAP_VERSION2 ) {
374 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
376 exit( EXIT_FAILURE );
378 protocol = LDAP_VERSION3;
381 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
387 #ifdef HAVE_CYRUS_SASL
388 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
389 fprintf( stderr, "%s: incompatible previous "
390 "authentication choice\n",
392 exit( EXIT_FAILURE );
394 authmethod = LDAP_AUTH_SASL;
395 sasl_flags = LDAP_SASL_QUIET;
398 fprintf( stderr, "%s: not compiled with SASL support\n",
400 exit( EXIT_FAILURE );
403 #ifdef HAVE_CYRUS_SASL
404 if( sasl_realm != NULL ) {
405 fprintf( stderr, "%s: -R previously specified\n", prog );
406 exit( EXIT_FAILURE );
408 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
409 fprintf( stderr, "%s: incompatible previous "
410 "authentication choice\n",
412 exit( EXIT_FAILURE );
414 authmethod = LDAP_AUTH_SASL;
415 sasl_realm = ber_strdup( optarg );
417 fprintf( stderr, "%s: not compiled with SASL support\n",
419 exit( EXIT_FAILURE );
423 #ifdef HAVE_CYRUS_SASL
424 if( sasl_authc_id != NULL ) {
425 fprintf( stderr, "%s: -U previously specified\n", prog );
426 exit( EXIT_FAILURE );
428 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
429 fprintf( stderr, "%s: incompatible previous "
430 "authentication choice\n",
432 exit( EXIT_FAILURE );
434 authmethod = LDAP_AUTH_SASL;
435 sasl_authc_id = ber_strdup( optarg );
437 fprintf( stderr, "%s: not compiled with SASL support\n",
439 exit( EXIT_FAILURE );
442 case 'v': /* verbose mode */
445 case 'V': /* version */
448 case 'w': /* password */
449 passwd.bv_val = ber_strdup( optarg );
453 for( p = optarg; *p != '\0'; p++ ) {
457 passwd.bv_len = strlen( passwd.bv_val );
466 #ifdef HAVE_CYRUS_SASL
467 if( sasl_mech != NULL ) {
468 fprintf( stderr, "%s: -Y previously specified\n", prog );
469 exit( EXIT_FAILURE );
471 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
473 "%s: incompatible with authentication choice\n", prog );
474 exit( EXIT_FAILURE );
476 authmethod = LDAP_AUTH_SASL;
477 sasl_mech = ber_strdup( optarg );
479 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
480 exit( EXIT_FAILURE );
484 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
485 fprintf( stderr, "%s: incompatible with previous "
486 "authentication choice\n", prog );
487 exit( EXIT_FAILURE );
489 authmethod = LDAP_AUTH_SIMPLE;
492 #ifdef HAVE_CYRUS_SASL
493 if( sasl_authz_id != NULL ) {
494 fprintf( stderr, "%s: -X previously specified\n", prog );
495 exit( EXIT_FAILURE );
497 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
498 fprintf( stderr, "%s: -X incompatible with "
499 "authentication choice\n", prog );
500 exit( EXIT_FAILURE );
502 authmethod = LDAP_AUTH_SASL;
503 sasl_authz_id = ber_strdup( optarg );
505 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
506 exit( EXIT_FAILURE );
513 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
514 exit( EXIT_FAILURE );
518 if( handle_private_option( i ) ) break;
519 fprintf( stderr, "%s: unrecognized option -%c\n",
526 /* prevent bad linking */
528 api.ldapai_info_version = LDAP_API_INFO_VERSION;
530 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
531 != LDAP_OPT_SUCCESS )
533 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
534 exit( EXIT_FAILURE );
537 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
538 fprintf( stderr, "LDAP APIInfo version mismatch: "
539 "got %d, expected %d\n",
540 api.ldapai_info_version, LDAP_API_INFO_VERSION );
541 exit( EXIT_FAILURE );
544 if( api.ldapai_api_version != LDAP_API_VERSION ) {
545 fprintf( stderr, "LDAP API version mismatch: "
546 "got %d, expected %d\n",
547 api.ldapai_api_version, LDAP_API_VERSION );
548 exit( EXIT_FAILURE );
551 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
552 fprintf( stderr, "LDAP vendor name mismatch: "
553 "got %s, expected %s\n",
554 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
555 exit( EXIT_FAILURE );
558 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
559 fprintf( stderr, "LDAP vendor version mismatch: "
560 "got %d, expected %d\n",
561 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
562 exit( EXIT_FAILURE );
566 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
568 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
569 if (version > 1) exit( EXIT_SUCCESS );
574 protocol = LDAP_VERSION3;
576 if (authmethod == -1 && protocol > LDAP_VERSION2) {
577 #ifdef HAVE_CYRUS_SASL
578 authmethod = LDAP_AUTH_SASL;
580 authmethod = LDAP_AUTH_SIMPLE;
584 if( ldapuri == NULL ) {
585 if( ldapport && ( ldaphost == NULL )) {
586 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
587 exit( EXIT_FAILURE );
590 if( ldaphost != NULL ) {
591 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
592 exit( EXIT_FAILURE );
595 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
596 exit( EXIT_FAILURE );
599 if( protocol == LDAP_VERSION2 ) {
600 if( authzid || manageDSAit || noop ) {
601 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
602 exit( EXIT_FAILURE );
606 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
607 exit( EXIT_FAILURE );
610 #ifdef HAVE_CYRUS_SASL
611 if( authmethod == LDAP_AUTH_SASL ) {
612 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
614 exit( EXIT_FAILURE );
618 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
619 if ( authmethod == LDAP_AUTH_KRBV4 || authmethod == LDAP_AUTH_KRBV41 ) {
620 fprintf( stderr, "%s: -k/-K incompatible with LDAPv%d\n",
622 exit( EXIT_FAILURE );
630 tool_conn_setup( int not, void (*private_setup)( LDAP * ) )
635 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
636 != LBER_OPT_SUCCESS )
638 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
640 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
641 != LDAP_OPT_SUCCESS )
643 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
648 (void) SIGNAL( SIGPIPE, SIG_IGN );
652 /* connect to server */
653 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
655 fprintf( stderr, "ldap_init( %s, %d )\n",
656 ldaphost != NULL ? ldaphost : "<DEFAULT>",
660 ld = ldap_init( ldaphost, ldapport );
662 char buf[20 + sizeof(": ldap_init")];
663 sprintf( buf, "%.20s: ldap_init", prog );
665 exit( EXIT_FAILURE );
671 fprintf( stderr, "ldap_initialize( %s )\n",
672 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
674 rc = ldap_initialize( &ld, ldapuri );
675 if( rc != LDAP_SUCCESS ) {
677 "Could not create LDAP session handle (%d): %s\n",
678 rc, ldap_err2string(rc) );
679 exit( EXIT_FAILURE );
683 if( private_setup ) private_setup( ld );
686 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
687 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
689 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
690 referrals ? "on" : "off" );
691 exit( EXIT_FAILURE );
694 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
695 != LDAP_OPT_SUCCESS )
697 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
699 exit( EXIT_FAILURE );
703 ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ))
705 ldap_perror( ld, "ldap_start_tls" );
707 exit( EXIT_FAILURE );
717 tool_bind( LDAP *ld )
719 if ( authmethod == LDAP_AUTH_SASL ) {
720 #ifdef HAVE_CYRUS_SASL
724 if( sasl_secprops != NULL ) {
725 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
726 (void *) sasl_secprops );
728 if( rc != LDAP_OPT_SUCCESS ) {
730 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
732 exit( EXIT_FAILURE );
736 defaults = lutil_sasl_defaults( ld,
743 rc = ldap_sasl_interactive_bind_s( ld, binddn,
744 sasl_mech, NULL, NULL,
745 sasl_flags, lutil_sasl_interact, defaults );
747 lutil_sasl_freedefs( defaults );
748 if( rc != LDAP_SUCCESS ) {
749 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
750 exit( EXIT_FAILURE );
753 fprintf( stderr, "%s: not compiled with SASL support\n",
755 exit( EXIT_FAILURE );
758 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
761 ldap_perror( ld, "ldap_bind" );
762 exit( EXIT_FAILURE );
768 /* Set server controls. Add controls extra_c[0..count-1], if set. */
770 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
772 int i = 0, j, crit = 0, err;
773 LDAPControl c[6], **ctrls;
775 ctrls = (LDAPControl**) malloc(sizeof(c) + (count+1)*sizeof(LDAPControl*));
776 if ( ctrls == NULL ) {
777 fprintf( stderr, "No memory\n" );
778 exit( EXIT_FAILURE );
782 BerElementBuffer berbuf;
783 BerElement *ber = (BerElement *)&berbuf;
785 if( assertion == NULL || *assertion == '\0' ) {
786 fprintf( stderr, "Assertion=<empty>\n" );
787 exit( EXIT_FAILURE );
790 ber_init2( ber, NULL, LBER_USE_DER );
792 err = ldap_pvt_put_filter( ber, assertion );
794 fprintf( stderr, "assertion encode failed (%d)\n", err );
795 exit( EXIT_FAILURE );
798 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
800 fprintf( stderr, "assertion flatten failed (%d)\n", err );
801 exit( EXIT_FAILURE );
804 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
805 c[i].ldctl_iscritical = assertctl > 1;
811 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
812 c[i].ldctl_value.bv_val = authzid;
813 c[i].ldctl_value.bv_len = strlen( authzid );
814 c[i].ldctl_iscritical = 1;
820 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
821 c[i].ldctl_value.bv_val = NULL;
822 c[i].ldctl_value.bv_len = 0;
823 c[i].ldctl_iscritical = manageDSAit > 1;
829 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
830 c[i].ldctl_value.bv_val = NULL;
831 c[i].ldctl_value.bv_len = 0;
832 c[i].ldctl_iscritical = noop > 1;
838 char berbuf[LBER_ELEMENT_SIZEOF];
839 BerElement *ber = (BerElement *)berbuf;
842 if( preread_attrs ) {
843 attrs = ldap_str2charray( preread_attrs, "," );
846 ber_init2( ber, NULL, LBER_USE_DER );
848 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
849 fprintf( stderr, "preread attrs encode failed.\n" );
850 exit( EXIT_FAILURE );
853 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
855 fprintf( stderr, "preread flatten failed (%d)\n", err );
856 exit( EXIT_FAILURE );
859 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
860 c[i].ldctl_iscritical = preread > 1;
864 if( attrs ) ldap_charray_free( attrs );
868 char berbuf[LBER_ELEMENT_SIZEOF];
869 BerElement *ber = (BerElement *)berbuf;
872 if( postread_attrs ) {
873 attrs = ldap_str2charray( postread_attrs, "," );
876 ber_init2( ber, NULL, LBER_USE_DER );
878 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
879 fprintf( stderr, "postread attrs encode failed.\n" );
880 exit( EXIT_FAILURE );
883 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
885 fprintf( stderr, "postread flatten failed (%d)\n", err );
886 exit( EXIT_FAILURE );
889 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
890 c[i].ldctl_iscritical = postread > 1;
894 if( attrs ) ldap_charray_free( attrs );
898 ctrls[i++] = extra_c++;
902 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
904 if ( err != LDAP_OPT_SUCCESS ) {
905 for ( j = 0; j < i; j++ ) {
906 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
908 fprintf( stderr, "Could not set %scontrols\n",
909 crit ? "critical " : "" );
914 exit( EXIT_FAILURE );