]> git.sur5r.net Git - openldap/blob - clients/tools/common.c
Use version strings provided by build system for -V
[openldap] / clients / tools / common.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* common.c - common routines for the ldap client tools */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/stdlib.h>
13 #include <ac/signal.h>
14 #include <ac/string.h>
15 #include <ac/unistd.h>
16 #include <ac/errno.h>
17
18 #include <ldap.h>
19
20 #include "lutil_ldap.h"
21
22 #include "common.h"
23
24
25 int   authmethod = -1;
26 char *binddn = NULL;
27 int   contoper = 0;
28 int   debug = 0;
29 char *infile = NULL;
30 char *ldapuri = NULL;
31 char *ldaphost = NULL;
32 int   ldapport = 0;
33 #ifdef HAVE_CYRUS_SASL
34 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
35 char    *sasl_realm = NULL;
36 char    *sasl_authc_id = NULL;
37 char    *sasl_authz_id = NULL;
38 char    *sasl_mech = NULL;
39 char    *sasl_secprops = NULL;
40 #endif
41 int   use_tls = 0;
42
43 char *authzid = NULL;
44 int   manageDSAit = 0;
45 int   noop = 0;
46
47 int   not = 0;
48 int   want_bindpw = 0;
49 struct berval passwd = { 0, NULL };
50 char *pw_file = NULL;
51 int   referrals = 0;
52 int   protocol = -1;
53 int   verbose = 0;
54 int   version = 0;
55
56 /* Set in main() */
57 char *prog = NULL;
58
59 void
60 tool_common_usage( void )
61 {
62         static const char *const descriptions[] = {
63 "  -c         continuous operation mode (do not stop on errors)\n",
64 "  -C         chase referrals\n",
65 "  -d level   set LDAP debugging level to `level'\n",
66 "  -D binddn  bind DN\n",
67 "  -e [!]<ctrl>[=<ctrlparam>] general controls (! indicates criticality)\n"
68 "             [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n"
69 "             [!]manageDSAit       (alternate form, see -M)\n"
70 "             [!]noop\n",
71 "  -f file    read operations from `file'\n",
72 "  -h host    LDAP server\n",
73 "  -H URI     LDAP Uniform Resource Indentifier(s)\n",
74 "  -I         use SASL Interactive mode\n",
75 "  -k         use Kerberos authentication\n",
76 "  -K         like -k, but do only step 1 of the Kerberos bind\n",
77 "  -M         enable Manage DSA IT control (-MM to make critical)\n",
78 "  -n         show what would be done but don't actually do it\n",
79 "  -O props   SASL security properties\n",
80 "  -p port    port on LDAP server\n",
81 "  -P version procotol version (default: 3)\n",
82 "  -Q         use SASL Quiet mode\n",
83 "  -R realm   SASL realm\n",
84 "  -U authcid SASL authentication identity\n",
85 "  -v         run in verbose mode (diagnostics to standard output)\n",
86 "  -V         print version info (-VV only)\n",
87 "  -w passwd  bind passwd (for simple authentication)\n",
88 "  -W         prompt for bind passwd\n",
89 "  -x         Simple authentication\n",
90 "  -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n",
91 "  -y file    Read passwd from file\n",
92 "  -Y mech    SASL mechanism\n",
93 "  -Z         Start TLS request (-ZZ to require successful response)\n",
94 NULL
95         };
96         const char *const *cpp;
97
98         fputs( "Common options:\n", stderr );
99         for( cpp = descriptions; *cpp != NULL; cpp++ ) {
100                 if( strchr( options, (*cpp)[3] ) ) {
101                         fputs( *cpp, stderr );
102                 }
103         }
104 }
105
106
107 void
108 tool_args( int argc, char **argv )
109 {
110         int i;
111     while (( i = getopt( argc, argv, options )) != EOF )
112         {
113                 int crit;
114                 char *control, *cvalue;
115                 switch( i ) {
116                 case 'c':       /* continuous operation mode */
117                         contoper = 1;
118                         break;
119                 case 'C':
120                         referrals = 1;
121                         break;
122                 case 'd':
123                         debug |= atoi( optarg );
124                         break;
125                 case 'D':       /* bind DN */
126                         if( binddn != NULL ) {
127                                 fprintf( stderr, "%s: -D previously specified\n", prog );
128                                 exit( EXIT_FAILURE );
129                         }
130                         binddn = ber_strdup( optarg );
131                         break;
132                 case 'e': /* general controls */
133                         /* should be extended to support comma separated list of
134                          *      [!]key[=value] parameters, e.g.  -e !foo,bar=567
135                          */
136
137                         crit = 0;
138                         cvalue = NULL;
139                         if( optarg[0] == '!' ) {
140                                 crit = 1;
141                                 optarg++;
142                         }
143
144                         control = ber_strdup( optarg );
145                         if ( (cvalue = strchr( control, '=' )) != NULL ) {
146                                 *cvalue++ = '\0';
147                         }
148
149                         if ( strcasecmp( control, "authzid" ) == 0 ) {
150                                 if( authzid != NULL ) {
151                                         fprintf( stderr, "authzid control previously specified\n");
152                                         exit( EXIT_FAILURE );
153                                 }
154                                 if( cvalue == NULL ) {
155                                         fprintf( stderr, "authzid: control value expected\n" );
156                                         usage();
157                                 }
158                                 if( !crit ) {
159                                         fprintf( stderr, "authzid: must be marked critical\n" );
160                                         usage();
161                                 }
162
163                                 assert( authzid == NULL );
164                                 authzid = cvalue;
165
166                         } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
167                                 if( manageDSAit ) {
168                                         fprintf( stderr,
169                                                  "manageDSAit control previously specified\n");
170                                         exit( EXIT_FAILURE );
171                                 }
172                                 if( cvalue != NULL ) {
173                                         fprintf( stderr,
174                                                  "manageDSAit: no control value expected\n" );
175                                         usage();
176                                 }
177
178                                 manageDSAit = 1 + crit;
179
180                         } else if ( strcasecmp( control, "noop" ) == 0 ) {
181                                 if( noop ) {
182                                         fprintf( stderr, "noop control previously specified\n");
183                                         exit( EXIT_FAILURE );
184                                 }
185                                 if( cvalue != NULL ) {
186                                         fprintf( stderr, "noop: no control value expected\n" );
187                                         usage();
188                                 }
189
190                                 noop = 1 + crit;
191
192                         } else {
193                                 fprintf( stderr, "Invalid general control name: %s\n",
194                                          control );
195                                 usage();
196                         }
197                         break;
198                 case 'f':       /* read from file */
199                         if( infile != NULL ) {
200                                 fprintf( stderr, "%s: -f previously specified\n", prog );
201                                 exit( EXIT_FAILURE );
202                         }
203                         infile = ber_strdup( optarg );
204                         break;
205                 case 'h':       /* ldap host */
206                         if( ldaphost != NULL ) {
207                                 fprintf( stderr, "%s: -h previously specified\n", prog );
208                                 exit( EXIT_FAILURE );
209                         }
210                         ldaphost = ber_strdup( optarg );
211                         break;
212                 case 'H':       /* ldap URI */
213                         if( ldapuri != NULL ) {
214                                 fprintf( stderr, "%s: -H previously specified\n", prog );
215                                 exit( EXIT_FAILURE );
216                         }
217                         ldapuri = ber_strdup( optarg );
218                         break;
219                 case 'I':
220 #ifdef HAVE_CYRUS_SASL
221                         if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
222                                 fprintf( stderr, "%s: incompatible previous "
223                                                  "authentication choice\n",
224                                                  prog );
225                                 exit( EXIT_FAILURE );
226                         }
227                         authmethod = LDAP_AUTH_SASL;
228                         sasl_flags = LDAP_SASL_INTERACTIVE;
229                         break;
230 #else
231                         fprintf( stderr, "%s: was not compiled with SASL support\n",
232                                          prog );
233                         exit( EXIT_FAILURE );
234 #endif
235                 case 'k':       /* kerberos bind */
236 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
237                         if( authmethod != -1 ) {
238                                 fprintf( stderr, "%s: -k incompatible with previous "
239                                                  "authentication choice\n", prog );
240                                 exit( EXIT_FAILURE );
241                         }
242                         authmethod = LDAP_AUTH_KRBV4;
243 #else
244                         fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
245                         exit( EXIT_FAILURE );
246 #endif
247                         break;
248                 case 'K':       /* kerberos bind, part one only */
249 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
250                         if( authmethod != -1 ) {
251                                 fprintf( stderr, "%s: incompatible with previous "
252                                                  "authentication choice\n", prog );
253                                 exit( EXIT_FAILURE );
254                         }
255                         authmethod = LDAP_AUTH_KRBV41;
256 #else
257                         fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
258                         exit( EXIT_FAILURE );
259 #endif
260                         break;
261                 case 'M':
262                         /* enable Manage DSA IT */
263                         manageDSAit = 1;
264                         break;
265                 case 'n':       /* print operations, don't actually do them */
266                         not = 1;
267                         break;
268                 case 'O':
269 #ifdef HAVE_CYRUS_SASL
270                         if( sasl_secprops != NULL ) {
271                                 fprintf( stderr, "%s: -O previously specified\n", prog );
272                                 exit( EXIT_FAILURE );
273                         }
274                         if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
275                                 fprintf( stderr, "%s: incompatible previous "
276                                                  "authentication choice\n", prog );
277                                 exit( EXIT_FAILURE );
278                         }
279                         authmethod = LDAP_AUTH_SASL;
280                         sasl_secprops = ber_strdup( optarg );
281 #else
282                         fprintf( stderr, "%s: not compiled with SASL support\n",
283                                          prog );
284                         exit( EXIT_FAILURE );
285 #endif
286                         break;
287                 case 'p':
288                         if( ldapport ) {
289                                 fprintf( stderr, "%s: -p previously specified\n", prog );
290                                 exit( EXIT_FAILURE );
291                         }
292                         ldapport = atoi( optarg );
293                         break;
294                 case 'P':
295                         switch( atoi(optarg) ) {
296                         case 2:
297                                 if( protocol == LDAP_VERSION3 ) {
298                                         fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
299                                                          prog, protocol );
300                                         exit( EXIT_FAILURE );
301                                 }
302                                 protocol = LDAP_VERSION2;
303                                 break;
304                         case 3:
305                                 if( protocol == LDAP_VERSION2 ) {
306                                         fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
307                                                          prog, protocol );
308                                         exit( EXIT_FAILURE );
309                                 }
310                                 protocol = LDAP_VERSION3;
311                                 break;
312                         default:
313                                 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
314                                                  prog );
315                                 usage();
316                         }
317                         break;
318                 case 'Q':
319 #ifdef HAVE_CYRUS_SASL
320                         if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
321                                 fprintf( stderr, "%s: incompatible previous "
322                                                  "authentication choice\n",
323                                                  prog );
324                                 exit( EXIT_FAILURE );
325                         }
326                         authmethod = LDAP_AUTH_SASL;
327                         sasl_flags = LDAP_SASL_QUIET;
328                         break;
329 #else
330                         fprintf( stderr, "%s: not compiled with SASL support\n",
331                                          prog );
332                         exit( EXIT_FAILURE );
333 #endif
334                 case 'R':
335 #ifdef HAVE_CYRUS_SASL
336                         if( sasl_realm != NULL ) {
337                                 fprintf( stderr, "%s: -R previously specified\n", prog );
338                                 exit( EXIT_FAILURE );
339                         }
340                         if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
341                                 fprintf( stderr, "%s: incompatible previous "
342                                                  "authentication choice\n",
343                                                  prog );
344                                 exit( EXIT_FAILURE );
345                         }
346                         authmethod = LDAP_AUTH_SASL;
347                         sasl_realm = ber_strdup( optarg );
348 #else
349                         fprintf( stderr, "%s: not compiled with SASL support\n",
350                                          prog );
351                         exit( EXIT_FAILURE );
352 #endif
353                         break;
354                 case 'U':
355 #ifdef HAVE_CYRUS_SASL
356                         if( sasl_authc_id != NULL ) {
357                                 fprintf( stderr, "%s: -U previously specified\n", prog );
358                                 exit( EXIT_FAILURE );
359                         }
360                         if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
361                                 fprintf( stderr, "%s: incompatible previous "
362                                                  "authentication choice\n",
363                                                  prog );
364                                 exit( EXIT_FAILURE );
365                         }
366                         authmethod = LDAP_AUTH_SASL;
367                         sasl_authc_id = ber_strdup( optarg );
368 #else
369                         fprintf( stderr, "%s: not compiled with SASL support\n",
370                                          prog );
371                         exit( EXIT_FAILURE );
372 #endif
373                         break;
374                 case 'v':       /* verbose mode */
375                         verbose = 1;
376                         break;
377                 case 'V':       /* version */
378                         version++;
379                         break;
380                 case 'w':       /* password */
381                         passwd.bv_val = ber_strdup( optarg );
382                         {
383                                 char* p;
384
385                                 for( p = optarg; *p != '\0'; p++ ) {
386                                         *p = '\0';
387                                 }
388                         }
389                         passwd.bv_len = strlen( passwd.bv_val );
390                         break;
391                 case 'W':
392                         want_bindpw = 1;
393                         break;
394                 case 'y':
395                         pw_file = optarg;
396                         break;
397                 case 'Y':
398 #ifdef HAVE_CYRUS_SASL
399                         if( sasl_mech != NULL ) {
400                                 fprintf( stderr, "%s: -Y previously specified\n", prog );
401                                 exit( EXIT_FAILURE );
402                         }
403                         if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
404                                 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
405                                 exit( EXIT_FAILURE );
406                         }
407                         authmethod = LDAP_AUTH_SASL;
408                         sasl_mech = ber_strdup( optarg );
409 #else
410                         fprintf( stderr, "%s: not compiled with SASL support\n",
411                                          prog );
412                         exit( EXIT_FAILURE );
413 #endif
414                         break;
415                 case 'x':
416                         if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
417                                 fprintf( stderr, "%s: incompatible with previous "
418                                                  "authentication choice\n", prog );
419                                 exit( EXIT_FAILURE );
420                         }
421                         authmethod = LDAP_AUTH_SIMPLE;
422                         break;
423                 case 'X':
424 #ifdef HAVE_CYRUS_SASL
425                         if( sasl_authz_id != NULL ) {
426                                 fprintf( stderr, "%s: -X previously specified\n", prog );
427                                 exit( EXIT_FAILURE );
428                         }
429                         if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
430                                 fprintf( stderr, "%s: -X incompatible with "
431                                                  "authentication choice\n", prog );
432                                 exit( EXIT_FAILURE );
433                         }
434                         authmethod = LDAP_AUTH_SASL;
435                         sasl_authz_id = ber_strdup( optarg );
436 #else
437                         fprintf( stderr, "%s: not compiled with SASL support\n", prog );
438                         exit( EXIT_FAILURE );
439 #endif
440                         break;
441                 case 'Z':
442 #ifdef HAVE_TLS
443                         use_tls = 1;
444 #else
445                         fprintf( stderr, "%s: not compiled with TLS support\n", prog );
446                         exit( EXIT_FAILURE );
447 #endif
448                         break;
449                 default:
450                         if( handle_private_option( i ) )
451                                 break;
452                         fprintf( stderr, "%s: unrecognized option -%c\n",
453                                          prog, optopt );
454                         usage();
455                 }
456     }
457
458         if (version) {
459                 fprintf( stderr, "%s: %s", prog, __Version );
460                 if (version > 1) exit( EXIT_SUCCESS );
461         }
462
463         if (protocol == -1)
464                 protocol = LDAP_VERSION3;
465
466         if (authmethod == -1 && protocol > LDAP_VERSION2) {
467 #ifdef HAVE_CYRUS_SASL
468                 authmethod = LDAP_AUTH_SASL;
469 #else
470                 authmethod = LDAP_AUTH_SIMPLE;
471 #endif
472         }
473
474         if( ldapuri != NULL ) {
475                 if( ldaphost != NULL ) {
476                         fprintf( stderr, "%s: -H incompatible with -h\n", prog );
477                         exit( EXIT_FAILURE );
478                 }
479                 if( ldapport ) {
480                         fprintf( stderr, "%s: -H incompatible with -p\n", prog );
481                         exit( EXIT_FAILURE );
482                 }
483         }
484         if( protocol == LDAP_VERSION2 ) {
485                 if( authzid || manageDSAit || noop ) {
486                         fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
487                         exit( EXIT_FAILURE );
488                 }
489 #ifdef HAVE_TLS
490                 if( use_tls ) {
491                         fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
492                         exit( EXIT_FAILURE );
493                 }
494 #endif
495 #ifdef HAVE_CYRUS_SASL
496                 if( authmethod == LDAP_AUTH_SASL ) {
497                         fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
498                                  prog );
499                         exit( EXIT_FAILURE );
500                 }
501 #endif
502         } else {
503 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
504                 if ( authmethod = LDAP_AUTH_KRBV4 || authmethod == LDAP_AUTH_KRBV41 ) {
505                         fprintf( stderr, "%s: -k/-K incompatible with LDAPv%d\n",
506                                  prog, protocol );
507                         exit( EXIT_FAILURE );
508                 }
509 #endif
510         }
511 }
512
513
514 LDAP *
515 tool_conn_setup( int not, void (*private_setup)( LDAP * ) )
516 {
517         LDAP *ld = NULL;
518
519         if ( debug ) {
520                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
521                     != LBER_OPT_SUCCESS ) {
522                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
523                 }
524                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
525                     != LDAP_OPT_SUCCESS ) {
526                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
527                 }
528         }
529
530 #ifdef SIGPIPE
531         (void) SIGNAL( SIGPIPE, SIG_IGN );
532 #endif
533
534         if ( !not ) {
535                 /* connect to server */
536                 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
537                         if ( verbose ) {
538                                 fprintf( stderr, "ldap_init( %s, %d )\n",
539                                          ldaphost != NULL ? ldaphost : "<DEFAULT>",
540                                          ldapport );
541                         }
542
543                         ld = ldap_init( ldaphost, ldapport );
544                         if( ld == NULL ) {
545                                 char buf[20 + sizeof(": ldap_init")];
546                                 sprintf( buf, "%.20s: ldap_init", prog );
547                                 perror( buf );
548                                 exit( EXIT_FAILURE );
549                         }
550
551                 } else {
552                         int rc;
553                         if ( verbose ) {
554                                 fprintf( stderr, "ldap_initialize( %s )\n",
555                                          ldapuri != NULL ? ldapuri : "<DEFAULT>" );
556                         }
557                         rc = ldap_initialize( &ld, ldapuri );
558                         if( rc != LDAP_SUCCESS ) {
559                                 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
560                                          rc, ldap_err2string(rc) );
561                                 exit( EXIT_FAILURE );
562                         }
563                 }
564
565                 if( private_setup )
566                         private_setup( ld );
567
568                 /* referrals */
569                 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
570                                          referrals ? LDAP_OPT_ON : LDAP_OPT_OFF )
571                         != LDAP_OPT_SUCCESS )
572                 {
573                         fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
574                                  referrals ? "on" : "off" );
575                         exit( EXIT_FAILURE );
576                 }
577
578                 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
579                     != LDAP_OPT_SUCCESS )
580                 {
581                         fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
582                                  protocol );
583                         exit( EXIT_FAILURE );
584                 }
585
586                 if ( use_tls &&
587                      ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
588                         ldap_perror( ld, "ldap_start_tls" );
589                         if ( use_tls > 1 ) {
590                                 exit( EXIT_FAILURE );
591                         }
592                 }
593         }
594
595         return ld;
596 }
597
598
599 void
600 tool_bind( LDAP *ld )
601 {
602         if ( authmethod == LDAP_AUTH_SASL ) {
603 #ifdef HAVE_CYRUS_SASL
604                 void *defaults;
605                 int rc;
606
607                 if( sasl_secprops != NULL ) {
608                         rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
609                                 (void *) sasl_secprops );
610
611                         if( rc != LDAP_OPT_SUCCESS ) {
612                                 fprintf( stderr,
613                                         "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
614                                         sasl_secprops );
615                                 exit( EXIT_FAILURE );
616                         }
617                 }
618
619                 defaults = lutil_sasl_defaults( ld,
620                         sasl_mech,
621                         sasl_realm,
622                         sasl_authc_id,
623                         passwd.bv_val,
624                         sasl_authz_id );
625
626                 rc = ldap_sasl_interactive_bind_s( ld, binddn,
627                         sasl_mech, NULL, NULL,
628                         sasl_flags, lutil_sasl_interact, defaults );
629
630                 if( rc != LDAP_SUCCESS ) {
631                         ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
632                         exit( EXIT_FAILURE );
633                 }
634 #else
635                 fprintf( stderr, "%s: not compiled with SASL support\n",
636                         prog );
637                 exit( EXIT_FAILURE );
638 #endif
639         } else {
640                 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
641                      != LDAP_SUCCESS ) {
642                         ldap_perror( ld, "ldap_bind" );
643                         exit( EXIT_FAILURE );
644                 }
645         }
646 }
647
648
649 /* Set server controls.  Add controls extra_c[0..count-1], if set. */
650 void
651 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
652 {
653         int i = 0, j, crit = 0, err;
654         LDAPControl c[3], **ctrls;
655
656         ctrls = (LDAPControl **)malloc( sizeof(c) + (count + 1)*sizeof(LDAPControl *) );
657         if ( ctrls == NULL ) {
658                 fprintf( stderr, "No memory\n" );
659                 exit( EXIT_FAILURE );
660         }
661
662         if ( authzid ) {
663                 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
664                 c[i].ldctl_value.bv_val = authzid;
665                 c[i].ldctl_value.bv_len = strlen( authzid );
666                 c[i].ldctl_iscritical = 1;
667                 ctrls[i] = &c[i];
668                 i++;
669         }
670
671         if ( manageDSAit ) {
672                 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
673                 c[i].ldctl_value.bv_val = NULL;
674                 c[i].ldctl_value.bv_len = 0;
675                 c[i].ldctl_iscritical = manageDSAit > 1;
676                 ctrls[i] = &c[i];
677                 i++;
678         }
679
680         if ( noop ) {
681                 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
682                 c[i].ldctl_value.bv_val = NULL;
683                 c[i].ldctl_value.bv_len = 0;
684                 c[i].ldctl_iscritical = noop > 1;
685                 ctrls[i] = &c[i];
686                 i++;
687         }
688         
689         while ( count-- )
690                 ctrls[i++] = extra_c++;
691         ctrls[i] = NULL;
692
693         err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
694
695         if ( err != LDAP_OPT_SUCCESS ) {
696                 for ( j = 0; j < i; j++ )
697                         if ( ctrls[j]->ldctl_iscritical )
698                                 crit = 1;
699                 fprintf( stderr, "Could not set %scontrols\n",
700                                  crit ? "critical " : "" );
701         }
702
703         free( ctrls );
704         if ( crit ) {
705                 exit( EXIT_FAILURE );
706         }
707 }