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-2005 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>
32 #include <ac/unistd.h>
37 #include "lutil_ldap.h"
38 #include "ldap_defaults.h"
51 char *ldaphost = NULL;
53 #ifdef HAVE_CYRUS_SASL
54 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
55 char *sasl_realm = NULL;
56 char *sasl_authc_id = NULL;
57 char *sasl_authz_id = NULL;
58 char *sasl_mech = NULL;
59 char *sasl_secprops = NULL;
64 char *assertion = NULL;
70 char *preread_attrs = NULL;
72 char *postread_attrs = NULL;
76 struct berval passwd = { 0, NULL };
89 ldap_pvt_setlocale(LC_MESSAGES, "");
90 ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
91 ldap_pvt_textdomain(OPENLDAP_PACKAGE);
95 tool_common_usage( void )
97 static const char *const descriptions[] = {
98 N_(" -c continuous operation mode (do not stop on errors)\n"),
99 N_(" -C chase referrals (anonymously)\n"),
100 N_(" -d level set LDAP debugging level to `level'\n"),
101 N_(" -D binddn bind DN\n"),
102 N_(" -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
103 N_(" [!]assert=<filter> (an RFC 2254 Filter)\n")
104 N_(" [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n")
105 N_(" [!]manageDSAit\n")
107 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
110 N_(" [!]postread[=<attrs>] (a comma-separated attribute list)\n")
111 N_(" [!]preread[=<attrs>] (a comma-separated attribute list)\n"),
112 N_(" -f file read operations from `file'\n"),
113 N_(" -h host LDAP server\n"),
114 N_(" -H URI LDAP Uniform Resource Indentifier(s)\n"),
115 N_(" -I use SASL Interactive mode\n"),
116 N_(" -k use Kerberos authentication\n"),
117 N_(" -K like -k, but do only step 1 of the Kerberos bind\n"),
118 N_(" -M enable Manage DSA IT control (-MM to make critical)\n"),
119 N_(" -n show what would be done but don't actually do it\n"),
120 N_(" -O props SASL security properties\n"),
121 N_(" -p port port on LDAP server\n"),
122 N_(" -P version procotol version (default: 3)\n"),
123 N_(" -Q use SASL Quiet mode\n"),
124 N_(" -R realm SASL realm\n"),
125 N_(" -U authcid SASL authentication identity\n"),
126 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
127 N_(" -V print version info (-VV only)\n"),
128 N_(" -w passwd bind password (for simple authentication)\n"),
129 N_(" -W prompt for bind password\n"),
130 N_(" -x Simple authentication\n"),
131 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
132 N_(" -y file Read password from file\n"),
133 N_(" -Y mech SASL mechanism\n"),
134 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
137 const char *const *cpp;
139 fputs( _("Common options:\n"), stderr );
140 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
141 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
142 fputs( _(*cpp), stderr );
149 tool_args( int argc, char **argv )
153 while (( i = getopt( argc, argv, options )) != EOF ) {
155 char *control, *cvalue, *next;
157 case 'c': /* continuous operation mode */
164 ival = strtol( optarg, &next, 10 );
165 if (next == NULL || next[0] != '\0') {
166 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
171 case 'D': /* bind DN */
172 if( binddn != NULL ) {
173 fprintf( stderr, "%s: -D previously specified\n", prog );
174 exit( EXIT_FAILURE );
176 binddn = ber_strdup( optarg );
178 case 'e': /* general extensions (controls and such) */
179 /* should be extended to support comma separated list of
180 * [!]key[=value] parameters, e.g. -e !foo,bar=567
185 if( optarg[0] == '!' ) {
190 control = ber_strdup( optarg );
191 if ( (cvalue = strchr( control, '=' )) != NULL ) {
195 if ( strcasecmp( control, "assert" ) == 0 ) {
197 fprintf( stderr, "assert control previously specified\n");
198 exit( EXIT_FAILURE );
200 if( cvalue == NULL ) {
201 fprintf( stderr, "assert: control value expected\n" );
205 assertctl = 1 + crit;
207 assert( assertion == NULL );
210 } else if ( strcasecmp( control, "authzid" ) == 0 ) {
211 if( authzid != NULL ) {
212 fprintf( stderr, "authzid control previously specified\n");
213 exit( EXIT_FAILURE );
215 if( cvalue == NULL ) {
216 fprintf( stderr, "authzid: control value expected\n" );
220 fprintf( stderr, "authzid: must be marked critical\n" );
224 assert( authzid == NULL );
227 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
230 "manageDSAit control previously specified\n");
231 exit( EXIT_FAILURE );
233 if( cvalue != NULL ) {
235 "manageDSAit: no control value expected\n" );
239 manageDSAit = 1 + crit;
241 } else if ( strcasecmp( control, "noop" ) == 0 ) {
243 fprintf( stderr, "noop control previously specified\n");
244 exit( EXIT_FAILURE );
246 if( cvalue != NULL ) {
247 fprintf( stderr, "noop: no control value expected\n" );
253 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
254 } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
256 fprintf( stderr, "ppolicy control previously specified\n");
257 exit( EXIT_FAILURE );
259 if( cvalue != NULL ) {
260 fprintf( stderr, "ppolicy: no control value expected\n" );
264 fprintf( stderr, "ppolicy: critical flag not allowed\n" );
271 } else if ( strcasecmp( control, "preread" ) == 0 ) {
273 fprintf( stderr, "preread control previously specified\n");
274 exit( EXIT_FAILURE );
278 preread_attrs = cvalue;
280 } else if ( strcasecmp( control, "postread" ) == 0 ) {
282 fprintf( stderr, "postread control previously specified\n");
283 exit( EXIT_FAILURE );
287 postread_attrs = cvalue;
290 fprintf( stderr, "Invalid general control name: %s\n",
295 case 'f': /* read from file */
296 if( infile != NULL ) {
297 fprintf( stderr, "%s: -f previously specified\n", prog );
298 exit( EXIT_FAILURE );
300 infile = ber_strdup( optarg );
302 case 'h': /* ldap host */
303 if( ldaphost != NULL ) {
304 fprintf( stderr, "%s: -h previously specified\n", prog );
305 exit( EXIT_FAILURE );
307 ldaphost = ber_strdup( optarg );
309 case 'H': /* ldap URI */
310 if( ldapuri != NULL ) {
311 fprintf( stderr, "%s: -H previously specified\n", prog );
312 exit( EXIT_FAILURE );
314 ldapuri = ber_strdup( optarg );
317 #ifdef HAVE_CYRUS_SASL
318 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
319 fprintf( stderr, "%s: incompatible previous "
320 "authentication choice\n",
322 exit( EXIT_FAILURE );
324 authmethod = LDAP_AUTH_SASL;
325 sasl_flags = LDAP_SASL_INTERACTIVE;
328 fprintf( stderr, "%s: was not compiled with SASL support\n",
330 exit( EXIT_FAILURE );
332 case 'k': /* kerberos bind */
333 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
334 if( authmethod != -1 ) {
335 fprintf( stderr, "%s: -k incompatible with previous "
336 "authentication choice\n", prog );
337 exit( EXIT_FAILURE );
339 authmethod = LDAP_AUTH_KRBV4;
341 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
342 exit( EXIT_FAILURE );
345 case 'K': /* kerberos bind, part one only */
346 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
347 if( authmethod != -1 ) {
348 fprintf( stderr, "%s: incompatible with previous "
349 "authentication choice\n", prog );
350 exit( EXIT_FAILURE );
352 authmethod = LDAP_AUTH_KRBV41;
354 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
355 exit( EXIT_FAILURE );
359 /* enable Manage DSA IT */
362 case 'n': /* print operations, don't actually do them */
366 #ifdef HAVE_CYRUS_SASL
367 if( sasl_secprops != NULL ) {
368 fprintf( stderr, "%s: -O previously specified\n", prog );
369 exit( EXIT_FAILURE );
371 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
372 fprintf( stderr, "%s: incompatible previous "
373 "authentication choice\n", prog );
374 exit( EXIT_FAILURE );
376 authmethod = LDAP_AUTH_SASL;
377 sasl_secprops = ber_strdup( optarg );
379 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
380 exit( EXIT_FAILURE );
385 fprintf( stderr, "%s: -p previously specified\n", prog );
386 exit( EXIT_FAILURE );
388 ival = strtol( optarg, &next, 10 );
389 if ( next == NULL || next[0] != '\0' ) {
390 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
391 exit( EXIT_FAILURE );
396 ival = strtol( optarg, &next, 10 );
397 if ( next == NULL || next[0] != '\0' ) {
398 fprintf( stderr, "%s: unabel to parse protocol version \"%s\"\n", prog, optarg );
399 exit( EXIT_FAILURE );
403 if( protocol == LDAP_VERSION3 ) {
404 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
406 exit( EXIT_FAILURE );
408 protocol = LDAP_VERSION2;
411 if( protocol == LDAP_VERSION2 ) {
412 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
414 exit( EXIT_FAILURE );
416 protocol = LDAP_VERSION3;
419 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
425 #ifdef HAVE_CYRUS_SASL
426 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
427 fprintf( stderr, "%s: incompatible previous "
428 "authentication choice\n",
430 exit( EXIT_FAILURE );
432 authmethod = LDAP_AUTH_SASL;
433 sasl_flags = LDAP_SASL_QUIET;
436 fprintf( stderr, "%s: not compiled with SASL support\n",
438 exit( EXIT_FAILURE );
441 #ifdef HAVE_CYRUS_SASL
442 if( sasl_realm != NULL ) {
443 fprintf( stderr, "%s: -R previously specified\n", prog );
444 exit( EXIT_FAILURE );
446 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
447 fprintf( stderr, "%s: incompatible previous "
448 "authentication choice\n",
450 exit( EXIT_FAILURE );
452 authmethod = LDAP_AUTH_SASL;
453 sasl_realm = ber_strdup( optarg );
455 fprintf( stderr, "%s: not compiled with SASL support\n",
457 exit( EXIT_FAILURE );
461 #ifdef HAVE_CYRUS_SASL
462 if( sasl_authc_id != NULL ) {
463 fprintf( stderr, "%s: -U previously specified\n", prog );
464 exit( EXIT_FAILURE );
466 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
467 fprintf( stderr, "%s: incompatible previous "
468 "authentication choice\n",
470 exit( EXIT_FAILURE );
472 authmethod = LDAP_AUTH_SASL;
473 sasl_authc_id = ber_strdup( optarg );
475 fprintf( stderr, "%s: not compiled with SASL support\n",
477 exit( EXIT_FAILURE );
480 case 'v': /* verbose mode */
483 case 'V': /* version */
486 case 'w': /* password */
487 passwd.bv_val = ber_strdup( optarg );
491 for( p = optarg; *p != '\0'; p++ ) {
495 passwd.bv_len = strlen( passwd.bv_val );
504 #ifdef HAVE_CYRUS_SASL
505 if( sasl_mech != NULL ) {
506 fprintf( stderr, "%s: -Y previously specified\n", prog );
507 exit( EXIT_FAILURE );
509 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
511 "%s: incompatible with authentication choice\n", prog );
512 exit( EXIT_FAILURE );
514 authmethod = LDAP_AUTH_SASL;
515 sasl_mech = ber_strdup( optarg );
517 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
518 exit( EXIT_FAILURE );
522 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
523 fprintf( stderr, "%s: incompatible with previous "
524 "authentication choice\n", prog );
525 exit( EXIT_FAILURE );
527 authmethod = LDAP_AUTH_SIMPLE;
530 #ifdef HAVE_CYRUS_SASL
531 if( sasl_authz_id != NULL ) {
532 fprintf( stderr, "%s: -X previously specified\n", prog );
533 exit( EXIT_FAILURE );
535 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
536 fprintf( stderr, "%s: -X incompatible with "
537 "authentication choice\n", prog );
538 exit( EXIT_FAILURE );
540 authmethod = LDAP_AUTH_SASL;
541 sasl_authz_id = ber_strdup( optarg );
543 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
544 exit( EXIT_FAILURE );
551 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
552 exit( EXIT_FAILURE );
556 if( handle_private_option( i ) ) break;
557 fprintf( stderr, "%s: unrecognized option -%c\n",
564 /* prevent bad linking */
566 api.ldapai_info_version = LDAP_API_INFO_VERSION;
568 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
569 != LDAP_OPT_SUCCESS )
571 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
572 exit( EXIT_FAILURE );
575 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
576 fprintf( stderr, "LDAP APIInfo version mismatch: "
577 "got %d, expected %d\n",
578 api.ldapai_info_version, LDAP_API_INFO_VERSION );
579 exit( EXIT_FAILURE );
582 if( api.ldapai_api_version != LDAP_API_VERSION ) {
583 fprintf( stderr, "LDAP API version mismatch: "
584 "got %d, expected %d\n",
585 api.ldapai_api_version, LDAP_API_VERSION );
586 exit( EXIT_FAILURE );
589 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
590 fprintf( stderr, "LDAP vendor name mismatch: "
591 "got %s, expected %s\n",
592 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
593 exit( EXIT_FAILURE );
596 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
597 fprintf( stderr, "LDAP vendor version mismatch: "
598 "got %d, expected %d\n",
599 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
600 exit( EXIT_FAILURE );
604 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
606 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
607 if (version > 1) exit( EXIT_SUCCESS );
612 protocol = LDAP_VERSION3;
614 if (authmethod == -1 && protocol > LDAP_VERSION2) {
615 #ifdef HAVE_CYRUS_SASL
616 authmethod = LDAP_AUTH_SASL;
618 authmethod = LDAP_AUTH_SIMPLE;
622 if( ldapuri == NULL ) {
623 if( ldapport && ( ldaphost == NULL )) {
624 fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
625 exit( EXIT_FAILURE );
628 if( ldaphost != NULL ) {
629 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
630 exit( EXIT_FAILURE );
633 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
634 exit( EXIT_FAILURE );
637 if( protocol == LDAP_VERSION2 ) {
638 if( authzid || manageDSAit || noop || ppolicy ) {
639 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
640 exit( EXIT_FAILURE );
644 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
645 exit( EXIT_FAILURE );
648 #ifdef HAVE_CYRUS_SASL
649 if( authmethod == LDAP_AUTH_SASL ) {
650 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
652 exit( EXIT_FAILURE );
656 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
657 if ( authmethod == LDAP_AUTH_KRBV4 || authmethod == LDAP_AUTH_KRBV41 ) {
658 fprintf( stderr, "%s: -k/-K incompatible with LDAPv%d\n",
660 exit( EXIT_FAILURE );
668 tool_conn_setup( int not, void (*private_setup)( LDAP * ) )
673 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
674 != LBER_OPT_SUCCESS )
677 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
679 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
680 != LDAP_OPT_SUCCESS )
683 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
688 (void) SIGNAL( SIGPIPE, SIG_IGN );
694 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
697 memset( &url, 0, sizeof(url));
699 url.lud_scheme = "ldap";
700 url.lud_host = ldaphost;
701 url.lud_port = ldapport;
702 url.lud_scope = LDAP_SCOPE_DEFAULT;
704 ldapuri = ldap_url_desc2str( &url );
708 fprintf( stderr, "ldap_initialize( %s )\n",
709 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
711 rc = ldap_initialize( &ld, ldapuri );
712 if( rc != LDAP_SUCCESS ) {
714 "Could not create LDAP session handle for URI=%s (%d): %s\n",
715 ldapuri, rc, ldap_err2string(rc) );
716 exit( EXIT_FAILURE );
719 if( private_setup ) private_setup( ld );
722 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
723 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
725 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
726 referrals ? "on" : "off" );
727 exit( EXIT_FAILURE );
730 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
731 != LDAP_OPT_SUCCESS )
733 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
735 exit( EXIT_FAILURE );
739 ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ))
741 ldap_perror( ld, "ldap_start_tls" );
743 exit( EXIT_FAILURE );
753 tool_bind( LDAP *ld )
755 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
757 LDAPControl *ctrls[2], c;
758 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
759 c.ldctl_value.bv_val = NULL;
760 c.ldctl_value.bv_len = 0;
761 c.ldctl_iscritical = 0;
764 ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
768 if ( authmethod == LDAP_AUTH_SASL ) {
769 #ifdef HAVE_CYRUS_SASL
773 if( sasl_secprops != NULL ) {
774 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
775 (void *) sasl_secprops );
777 if( rc != LDAP_OPT_SUCCESS ) {
779 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
781 exit( EXIT_FAILURE );
785 defaults = lutil_sasl_defaults( ld,
792 rc = ldap_sasl_interactive_bind_s( ld, binddn,
793 sasl_mech, NULL, NULL,
794 sasl_flags, lutil_sasl_interact, defaults );
796 lutil_sasl_freedefs( defaults );
797 if( rc != LDAP_SUCCESS ) {
798 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
799 exit( EXIT_FAILURE );
802 fprintf( stderr, "%s: not compiled with SASL support\n",
804 exit( EXIT_FAILURE );
814 if (( msgid = ldap_bind( ld, binddn, passwd.bv_val, authmethod )) == -1 )
816 ldap_perror( ld, "ldap_bind" );
817 exit( EXIT_FAILURE );
820 if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) {
821 ldap_perror( ld, "ldap_result" );
822 exit( EXIT_FAILURE );
825 if ( ldap_parse_result( ld, result, &err, NULL, NULL, NULL,
826 &ctrls, 1 ) != LDAP_SUCCESS ) {
827 ldap_perror( ld, "ldap_bind parse result" );
828 exit( EXIT_FAILURE );
831 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
832 if ( ctrls && ppolicy ) {
834 int expire, grace, len = 0;
835 LDAPPasswordPolicyError pErr = -1;
837 ctrl = ldap_find_control( LDAP_CONTROL_PASSWORDPOLICYRESPONSE, ctrls );
838 if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
839 &expire, &grace, &pErr ) == LDAP_SUCCESS ) {
840 if ( pErr != PP_noError ){
843 strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
844 len = strlen( msgbuf );
847 sprintf( msgbuf+len, " (Password expires in %d seconds)", expire );
848 } else if ( grace >= 0 ) {
849 sprintf( msgbuf+len, " (Password expired, %d grace logins remain)", grace );
854 if ( err != LDAP_SUCCESS || msgbuf[0] ) {
855 fprintf( stderr, "ldap_bind: %s%s\n", ldap_err2string( err ),
857 if ( err != LDAP_SUCCESS ) {
858 exit( EXIT_FAILURE );
865 /* Set server controls. Add controls extra_c[0..count-1], if set. */
867 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
869 int i = 0, j, crit = 0, err;
870 LDAPControl c[8], **ctrls;
872 ctrls = (LDAPControl**) malloc(sizeof(c) + (count+1)*sizeof(LDAPControl*));
873 if ( ctrls == NULL ) {
874 fprintf( stderr, "No memory\n" );
875 exit( EXIT_FAILURE );
879 BerElementBuffer berbuf;
880 BerElement *ber = (BerElement *)&berbuf;
882 if( assertion == NULL || *assertion == '\0' ) {
883 fprintf( stderr, "Assertion=<empty>\n" );
884 exit( EXIT_FAILURE );
887 ber_init2( ber, NULL, LBER_USE_DER );
889 err = ldap_pvt_put_filter( ber, assertion );
891 fprintf( stderr, "assertion encode failed (%d)\n", err );
892 exit( EXIT_FAILURE );
895 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
897 fprintf( stderr, "assertion flatten failed (%d)\n", err );
898 exit( EXIT_FAILURE );
901 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
902 c[i].ldctl_iscritical = assertctl > 1;
908 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
909 c[i].ldctl_value.bv_val = authzid;
910 c[i].ldctl_value.bv_len = strlen( authzid );
911 c[i].ldctl_iscritical = 1;
917 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
918 c[i].ldctl_value.bv_val = NULL;
919 c[i].ldctl_value.bv_len = 0;
920 c[i].ldctl_iscritical = manageDSAit > 1;
926 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
927 c[i].ldctl_value.bv_val = NULL;
928 c[i].ldctl_value.bv_len = 0;
929 c[i].ldctl_iscritical = noop > 1;
935 char berbuf[LBER_ELEMENT_SIZEOF];
936 BerElement *ber = (BerElement *)berbuf;
939 if( preread_attrs ) {
940 attrs = ldap_str2charray( preread_attrs, "," );
943 ber_init2( ber, NULL, LBER_USE_DER );
945 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
946 fprintf( stderr, "preread attrs encode failed.\n" );
947 exit( EXIT_FAILURE );
950 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
952 fprintf( stderr, "preread flatten failed (%d)\n", err );
953 exit( EXIT_FAILURE );
956 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
957 c[i].ldctl_iscritical = preread > 1;
961 if( attrs ) ldap_charray_free( attrs );
965 char berbuf[LBER_ELEMENT_SIZEOF];
966 BerElement *ber = (BerElement *)berbuf;
969 if( postread_attrs ) {
970 attrs = ldap_str2charray( postread_attrs, "," );
973 ber_init2( ber, NULL, LBER_USE_DER );
975 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
976 fprintf( stderr, "postread attrs encode failed.\n" );
977 exit( EXIT_FAILURE );
980 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
982 fprintf( stderr, "postread flatten failed (%d)\n", err );
983 exit( EXIT_FAILURE );
986 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
987 c[i].ldctl_iscritical = postread > 1;
991 if( attrs ) ldap_charray_free( attrs );
995 ctrls[i++] = extra_c++;
999 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1001 if ( err != LDAP_OPT_SUCCESS ) {
1002 for ( j = 0; j < i; j++ ) {
1003 if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1005 fprintf( stderr, "Could not set %scontrols\n",
1006 crit ? "critical " : "" );
1011 exit( EXIT_FAILURE );