]> git.sur5r.net Git - openldap/blob - clients/tools/common.c
ITS#3755 silence strict-aliasing warnings
[openldap] / clients / tools / common.c
1 /* common.c - common routines for the ldap client tools */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2006 The OpenLDAP Foundation.
6  * Portions Copyright 2003 Kurt D. Zeilenga.
7  * Portions Copyright 2003 IBM Corporation.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
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>.
17  */
18 /* ACKNOWLEDGEMENTS:
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)
23  */
24
25 #include "portable.h"
26
27 #include <stdio.h>
28
29 #include <ac/stdlib.h>
30 #include <ac/signal.h>
31 #include <ac/string.h>
32 #include <ac/ctype.h>
33 #include <ac/unistd.h>
34 #include <ac/errno.h>
35
36 #ifdef HAVE_CYRUS_SASL
37 #ifdef HAVE_SASL_SASL_H
38 #include <sasl/sasl.h>
39 #else
40 #include <sasl.h>
41 #endif
42 #endif
43
44 #include <ldap.h>
45
46 #include "ldif.h"
47 #include "lutil.h"
48 #include "lutil_ldap.h"
49 #include "ldap_defaults.h"
50 #include "ldap_pvt.h"
51 #include "lber_pvt.h"
52
53 #include "common.h"
54
55 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
56 #if !LDAP_DEPRECATED
57 /* Necessary for old LDAPv2 Kerberos Bind methods */
58 LDAP_F( int )
59 ldap_bind LDAP_P((      /* deprecated */
60         LDAP *ld,
61         LDAP_CONST char *who,
62         LDAP_CONST char *passwd,
63         int authmethod ));
64 #endif
65 #endif
66
67 /* input-related vars */
68
69 /* misc. parameters */
70 tool_type_t     tool_type;
71 int             contoper = 0;
72 int             debug = 0;
73 char            *infile = NULL;
74 int             dont = 0;
75 int             referrals = 0;
76 int             verbose = 0;
77 int             ldif = 0;
78 char            *prog = NULL;
79
80 /* connection */
81 char            *ldapuri = NULL;
82 char            *ldaphost = NULL;
83 int             ldapport = 0;
84 int             use_tls = 0;
85 int             protocol = -1;
86 int             version = 0;
87
88 /* authc/authz */
89 int             authmethod = -1;
90 char            *binddn = NULL;
91 int             want_bindpw = 0;
92 struct berval   passwd = { 0, NULL };
93 char            *pw_file = NULL;
94 #ifdef HAVE_CYRUS_SASL
95 unsigned        sasl_flags = LDAP_SASL_AUTOMATIC;
96 char            *sasl_realm = NULL;
97 char            *sasl_authc_id = NULL;
98 char            *sasl_authz_id = NULL;
99 char            *sasl_mech = NULL;
100 char            *sasl_secprops = NULL;
101 #endif
102
103 /* controls */
104 int             assertctl;
105 char            *assertion = NULL;
106 char            *authzid = NULL;
107 /* support deprecated early version of proxyAuthz */
108 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ       "2.16.840.1.113730.3.4.12"
109 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
110 char            *proxydn = NULL;
111 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
112 int             manageDIT = 0;
113 int             manageDSAit = 0;
114 int             noop = 0;
115 int             ppolicy = 0;
116 int             preread = 0;
117 static char     *preread_attrs = NULL;
118 int             postread = 0;
119 static char     *postread_attrs = NULL;
120 ber_int_t       pr_morePagedResults = 1;
121 struct berval   pr_cookie = { 0, NULL };
122 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
123 int             chaining = 0;
124 static int      chainingResolve = -1;
125 static int      chainingContinuation = -1;
126 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
127
128 typedef int (*print_ctrl_fn)( LDAP *ld, LDAPControl *ctrl );
129
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 );
135 #endif
136
137 static struct tool_ctrls_t {
138         const char      *oid;
139         unsigned        mask;
140         print_ctrl_fn   func;
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 },
147 #endif
148         { NULL,                                         0,              NULL }
149 };
150
151 /* "features" */
152 static int      gotintr;
153 static int      abcan;
154
155 RETSIGTYPE
156 do_sig( int sig )
157 {
158         gotintr = abcan;
159 }
160
161 void
162 tool_init( tool_type_t type )
163 {
164         tool_type = type;
165         ldap_pvt_setlocale(LC_MESSAGES, "");
166         ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
167         ldap_pvt_textdomain(OPENLDAP_PACKAGE);
168 }
169
170 void
171 tool_destroy( void )
172 {
173 #ifdef HAVE_CYRUS_SASL
174         sasl_done();
175 #endif
176 #ifdef HAVE_TLS
177         ldap_pvt_tls_destroy();
178 #endif
179 }
180
181 void
182 tool_common_usage( void )
183 {
184         static const char *const descriptions[] = {
185 N_("  -c         continuous operation mode (do not stop on errors)\n"),
186 N_("  -C         chase referrals (anonymously)\n"),
187 N_("  -d level   set LDAP debugging level to `level'\n"),
188 N_("  -D binddn  bind DN\n"),
189 N_("  -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
190 N_("             [!]assert=<filter>     (a RFC 4515 Filter string)\n")
191 N_("             [!]authzid=<authzid>   (\"dn:<dn>\" or \"u:<user>\")\n")
192 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
193 #if 0
194                  /* non-advertized support for proxyDN */
195 N_("             [!]proxydn=<dn>        (a RFC 4514 DN string)\n")
196 #endif
197 #endif
198 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
199 N_("             [!]chaining[=<resolveBehavior>[/<continuationBehavior>]]\n")
200 N_("                     one of \"chainingPreferred\", \"chainingRequired\",\n")
201 N_("                     \"referralsPreferred\", \"referralsRequired\"\n")
202 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
203 N_("             [!]manageDSAit\n")
204 N_("             [!]noop\n")
205 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
206 N_("             ppolicy\n")
207 #endif
208 N_("             [!]postread[=<attrs>]  (a comma-separated attribute list)\n")
209 N_("             [!]preread[=<attrs>]   (a comma-separated attribute list)\n")
210 #ifdef LDAP_DEVEL
211 N_("             [!]relax\n")
212 #endif
213 N_("             abandon, cancel (SIGINT sends abandon/cancel; not really controls)\n"),
214 N_("  -f file    read operations from `file'\n"),
215 N_("  -h host    LDAP server\n"),
216 N_("  -H URI     LDAP Uniform Resource Indentifier(s)\n"),
217 N_("  -I         use SASL Interactive mode\n"),
218 N_("  -k         use Kerberos authentication\n"),
219 N_("  -K         like -k, but do only step 1 of the Kerberos bind\n"),
220 N_("  -M         enable Manage DSA IT control (-MM to make critical)\n"),
221 N_("  -n         show what would be done but don't actually do it\n"),
222 N_("  -O props   SASL security properties\n"),
223 N_("  -p port    port on LDAP server\n"),
224 N_("  -P version procotol version (default: 3)\n"),
225 N_("  -Q         use SASL Quiet mode\n"),
226 N_("  -R realm   SASL realm\n"),
227 N_("  -U authcid SASL authentication identity\n"),
228 N_("  -v         run in verbose mode (diagnostics to standard output)\n"),
229 N_("  -V         print version info (-VV only)\n"),
230 N_("  -w passwd  bind password (for simple authentication)\n"),
231 N_("  -W         prompt for bind password\n"),
232 N_("  -x         Simple authentication\n"),
233 N_("  -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
234 N_("  -y file    Read password from file\n"),
235 N_("  -Y mech    SASL mechanism\n"),
236 N_("  -Z         Start TLS request (-ZZ to require successful response)\n"),
237 NULL
238         };
239         const char *const *cpp;
240
241         fputs( _("Common options:\n"), stderr );
242         for( cpp = descriptions; *cpp != NULL; cpp++ ) {
243                 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
244                         fputs( _(*cpp), stderr );
245                 }
246         }
247 }
248
249 void tool_perror(
250         const char *func,
251         int err,
252         const char *extra,
253         const char *matched,
254         const char *info,
255         char **refs )
256 {
257         fprintf( stderr, "%s: %s (%d)%s\n",
258                 func, ldap_err2string( err ), err, extra ? extra : "" );
259
260         if ( matched && *matched ) {
261                 fprintf( stderr, _("\tmatched DN: %s\n"), matched );
262         }
263
264         if ( info && *info ) {
265                 fprintf( stderr, _("\tadditional info: %s\n"), info );
266         }
267
268         if ( refs && *refs ) {
269                 int i;
270                 fprintf( stderr, _("\treferrals:\n") );
271                 for( i=0; refs[i]; i++ ) {
272                         fprintf( stderr, "\t\t%s\n", refs[i] );
273                 }
274         }
275 }
276
277
278 void
279 tool_args( int argc, char **argv )
280 {
281         int i;
282
283         while (( i = getopt( argc, argv, options )) != EOF ) {
284                 int crit, ival;
285                 char *control, *cvalue, *next;
286                 switch( i ) {
287                 case 'c':       /* continuous operation mode */
288                         contoper++;
289                         break;
290                 case 'C':
291                         referrals++;
292                         break;
293                 case 'd':
294                         ival = strtol( optarg, &next, 10 );
295                         if (next == NULL || next[0] != '\0') {
296                                 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
297                                 exit(EXIT_FAILURE);
298                         }
299                         debug |= ival;
300                         break;
301                 case 'D':       /* bind DN */
302                         if( binddn != NULL ) {
303                                 fprintf( stderr, "%s: -D previously specified\n", prog );
304                                 exit( EXIT_FAILURE );
305                         }
306                         binddn = ber_strdup( optarg );
307                         break;
308                 case 'e': /* general extensions (controls and such) */
309                         /* should be extended to support comma separated list of
310                          *      [!]key[=value] parameters, e.g.  -e !foo,bar=567
311                          */
312
313                         crit = 0;
314                         cvalue = NULL;
315                         if( optarg[0] == '!' ) {
316                                 crit = 1;
317                                 optarg++;
318                         }
319
320                         control = ber_strdup( optarg );
321                         if ( (cvalue = strchr( control, '=' )) != NULL ) {
322                                 *cvalue++ = '\0';
323                         }
324
325                         if ( strcasecmp( control, "assert" ) == 0 ) {
326                                 if( assertctl ) {
327                                         fprintf( stderr, "assert control previously specified\n");
328                                         exit( EXIT_FAILURE );
329                                 }
330                                 if( cvalue == NULL ) {
331                                         fprintf( stderr, "assert: control value expected\n" );
332                                         usage();
333                                 }
334
335                                 assertctl = 1 + crit;
336
337                                 assert( assertion == NULL );
338                                 assertion = cvalue;
339
340                         } else if ( strcasecmp( control, "authzid" ) == 0 ) {
341                                 if( authzid != NULL ) {
342                                         fprintf( stderr, "authzid control previously specified\n");
343                                         exit( EXIT_FAILURE );
344                                 }
345 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
346                                 if( proxydn != NULL ) {
347                                         fprintf( stderr, "authzid control incompatible with proxydn\n");
348                                         exit( EXIT_FAILURE );
349                                 }
350 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
351                                 if( cvalue == NULL ) {
352                                         fprintf( stderr, "authzid: control value expected\n" );
353                                         usage();
354                                 }
355                                 if( !crit ) {
356                                         fprintf( stderr, "authzid: must be marked critical\n" );
357                                         usage();
358                                 }
359
360                                 assert( authzid == NULL );
361                                 authzid = cvalue;
362
363 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
364                         } else if ( strcasecmp( control, "proxydn" ) == 0 ) {
365                                 if( proxydn != NULL ) {
366                                         fprintf( stderr, "proxydn control previously specified\n");
367                                         exit( EXIT_FAILURE );
368                                 }
369                                 if( authzid != NULL ) {
370                                         fprintf( stderr, "proxydn control incompatible with authzid\n");
371                                         exit( EXIT_FAILURE );
372                                 }
373                                 if( cvalue == NULL ) {
374                                         fprintf( stderr, "proxydn: control value expected\n" );
375                                         usage();
376                                 }
377                                 if( !crit ) {
378                                         fprintf( stderr, "proxydn: must be marked critical\n" );
379                                         usage();
380                                 }
381
382                                 assert( proxydn == NULL );
383                                 proxydn = cvalue;
384 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
385
386                         } else if ( ( strcasecmp( control, "relax" ) == 0 ) ||
387                                 ( strcasecmp( control, "manageDIT" ) == 0 ) )
388                         {
389                                 if( manageDIT ) {
390                                         fprintf( stderr,
391                                                 "relax control previously specified\n");
392                                         exit( EXIT_FAILURE );
393                                 }
394                                 if( cvalue != NULL ) {
395                                         fprintf( stderr,
396                                                 "relax: no control value expected\n" );
397                                         usage();
398                                 }
399
400                                 manageDIT = 1 + crit;
401
402                         } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
403                                 if( manageDSAit ) {
404                                         fprintf( stderr,
405                                                 "manageDSAit control previously specified\n");
406                                         exit( EXIT_FAILURE );
407                                 }
408                                 if( cvalue != NULL ) {
409                                         fprintf( stderr,
410                                                 "manageDSAit: no control value expected\n" );
411                                         usage();
412                                 }
413
414                                 manageDSAit = 1 + crit;
415
416                         } else if ( strcasecmp( control, "noop" ) == 0 ) {
417                                 if( noop ) {
418                                         fprintf( stderr, "noop control previously specified\n");
419                                         exit( EXIT_FAILURE );
420                                 }
421                                 if( cvalue != NULL ) {
422                                         fprintf( stderr, "noop: no control value expected\n" );
423                                         usage();
424                                 }
425
426                                 noop = 1 + crit;
427
428 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
429                         } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
430                                 if( ppolicy ) {
431                                         fprintf( stderr, "ppolicy control previously specified\n");
432                                         exit( EXIT_FAILURE );
433                                 }
434                                 if( cvalue != NULL ) {
435                                         fprintf( stderr, "ppolicy: no control value expected\n" );
436                                         usage();
437                                 }
438                                 if( crit ) {
439                                         fprintf( stderr, "ppolicy: critical flag not allowed\n" );
440                                         usage();
441                                 }
442
443                                 ppolicy = 1;
444 #endif
445
446                         } else if ( strcasecmp( control, "preread" ) == 0 ) {
447                                 if( preread ) {
448                                         fprintf( stderr, "preread control previously specified\n");
449                                         exit( EXIT_FAILURE );
450                                 }
451
452                                 preread = 1 + crit;
453                                 preread_attrs = cvalue;
454
455                         } else if ( strcasecmp( control, "postread" ) == 0 ) {
456                                 if( postread ) {
457                                         fprintf( stderr, "postread control previously specified\n");
458                                         exit( EXIT_FAILURE );
459                                 }
460
461                                 postread = 1 + crit;
462                                 postread_attrs = cvalue;
463
464 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
465                         } else if ( strcasecmp( control, "chaining" ) == 0 ) {
466                                 chaining = 1 + crit;
467
468                                 if ( cvalue != NULL ) {
469                                         char    *continuation;
470
471                                         continuation = strchr( cvalue, '/' );
472                                         if ( continuation ) {
473                                                 /* FIXME: this makes sense only in searches */
474                                                 *continuation++ = '\0';
475                                                 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
476                                                         chainingContinuation = LDAP_CHAINING_PREFERRED;
477                                                 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
478                                                         chainingContinuation = LDAP_CHAINING_REQUIRED;
479                                                 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
480                                                         chainingContinuation = LDAP_REFERRALS_PREFERRED;
481                                                 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
482                                                         chainingContinuation = LDAP_REFERRALS_REQUIRED;
483                                                 } else {
484                                                         fprintf( stderr,
485                                                                 "chaining behavior control "
486                                                                 "continuation value \"%s\" invalid\n",
487                                                                 continuation );
488                                                         exit( EXIT_FAILURE );
489                                                 }
490                                         }
491         
492                                         if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
493                                                 chainingResolve = LDAP_CHAINING_PREFERRED;
494                                         } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
495                                                 chainingResolve = LDAP_CHAINING_REQUIRED;
496                                         } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
497                                                 chainingResolve = LDAP_REFERRALS_PREFERRED;
498                                         } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
499                                                 chainingResolve = LDAP_REFERRALS_REQUIRED;
500                                         } else {
501                                                 fprintf( stderr,
502                                                         "chaining behavior control "
503                                                         "resolve value \"%s\" invalid\n",
504                                                         cvalue);
505                                                 exit( EXIT_FAILURE );
506                                         }
507                                 }
508 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
509
510                         /* this shouldn't go here, really; but it's a feature... */
511                         } else if ( strcasecmp( control, "abandon" ) == 0 ) {
512                                 abcan = LDAP_REQ_ABANDON;
513
514                         } else if ( strcasecmp( control, "cancel" ) == 0 ) {
515                                 abcan = LDAP_REQ_EXTENDED;
516
517                         } else {
518                                 fprintf( stderr, "Invalid general control name: %s\n",
519                                         control );
520                                 usage();
521                         }
522                         break;
523                 case 'f':       /* read from file */
524                         if( infile != NULL ) {
525                                 fprintf( stderr, "%s: -f previously specified\n", prog );
526                                 exit( EXIT_FAILURE );
527                         }
528                         infile = ber_strdup( optarg );
529                         break;
530                 case 'h':       /* ldap host */
531                         if( ldaphost != NULL ) {
532                                 fprintf( stderr, "%s: -h previously specified\n", prog );
533                                 exit( EXIT_FAILURE );
534                         }
535                         ldaphost = ber_strdup( optarg );
536                         break;
537                 case 'H':       /* ldap URI */
538                         if( ldapuri != NULL ) {
539                                 fprintf( stderr, "%s: -H previously specified\n", prog );
540                                 exit( EXIT_FAILURE );
541                         }
542                         ldapuri = ber_strdup( optarg );
543                         break;
544                 case 'I':
545 #ifdef HAVE_CYRUS_SASL
546                         if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
547                                 fprintf( stderr, "%s: incompatible previous "
548                                         "authentication choice\n",
549                                         prog );
550                                 exit( EXIT_FAILURE );
551                         }
552                         authmethod = LDAP_AUTH_SASL;
553                         sasl_flags = LDAP_SASL_INTERACTIVE;
554                         break;
555 #else
556                         fprintf( stderr, "%s: was not compiled with SASL support\n",
557                                 prog );
558                         exit( EXIT_FAILURE );
559 #endif
560                 case 'k':       /* kerberos bind */
561 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
562                         if( authmethod != -1 ) {
563                                 fprintf( stderr, "%s: -k incompatible with previous "
564                                         "authentication choice\n", prog );
565                                 exit( EXIT_FAILURE );
566                         }
567                         authmethod = LDAP_AUTH_KRBV4;
568 #else
569                         fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
570                         exit( EXIT_FAILURE );
571 #endif
572                         break;
573                 case 'K':       /* kerberos bind, part one only */
574 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
575                         if( authmethod != -1 ) {
576                                 fprintf( stderr, "%s: incompatible with previous "
577                                         "authentication choice\n", prog );
578                                 exit( EXIT_FAILURE );
579                         }
580                         authmethod = LDAP_AUTH_KRBV41;
581 #else
582                         fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
583                         exit( EXIT_FAILURE );
584 #endif
585                         break;
586                 case 'M':
587                         /* enable Manage DSA IT */
588                         manageDSAit++;
589                         break;
590                 case 'n':       /* print operations, don't actually do them */
591                         dont++;
592                         break;
593                 case 'O':
594 #ifdef HAVE_CYRUS_SASL
595                         if( sasl_secprops != NULL ) {
596                                 fprintf( stderr, "%s: -O previously specified\n", prog );
597                                 exit( EXIT_FAILURE );
598                         }
599                         if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
600                                 fprintf( stderr, "%s: incompatible previous "
601                                         "authentication choice\n", prog );
602                                 exit( EXIT_FAILURE );
603                         }
604                         authmethod = LDAP_AUTH_SASL;
605                         sasl_secprops = ber_strdup( optarg );
606 #else
607                         fprintf( stderr, "%s: not compiled with SASL support\n", prog );
608                         exit( EXIT_FAILURE );
609 #endif
610                         break;
611                 case 'p':
612                         if( ldapport ) {
613                                 fprintf( stderr, "%s: -p previously specified\n", prog );
614                                 exit( EXIT_FAILURE );
615                         }
616                         ival = strtol( optarg, &next, 10 );
617                         if ( next == NULL || next[0] != '\0' ) {
618                                 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
619                                 exit( EXIT_FAILURE );
620                         }
621                         ldapport = ival;
622                         break;
623                 case 'P':
624                         ival = strtol( optarg, &next, 10 );
625                         if ( next == NULL || next[0] != '\0' ) {
626                                 fprintf( stderr, "%s: unabel to parse protocol version \"%s\"\n", prog, optarg );
627                                 exit( EXIT_FAILURE );
628                         }
629                         switch( ival ) {
630                         case 2:
631                                 if( protocol == LDAP_VERSION3 ) {
632                                         fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
633                                                 prog, protocol );
634                                         exit( EXIT_FAILURE );
635                                 }
636                                 protocol = LDAP_VERSION2;
637                                 break;
638                         case 3:
639                                 if( protocol == LDAP_VERSION2 ) {
640                                         fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
641                                                 prog, protocol );
642                                         exit( EXIT_FAILURE );
643                                 }
644                                 protocol = LDAP_VERSION3;
645                                 break;
646                         default:
647                                 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
648                                         prog );
649                                 usage();
650                         }
651                         break;
652                 case 'Q':
653 #ifdef HAVE_CYRUS_SASL
654                         if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
655                                 fprintf( stderr, "%s: incompatible previous "
656                                         "authentication choice\n",
657                                         prog );
658                                 exit( EXIT_FAILURE );
659                         }
660                         authmethod = LDAP_AUTH_SASL;
661                         sasl_flags = LDAP_SASL_QUIET;
662                         break;
663 #else
664                         fprintf( stderr, "%s: not compiled with SASL support\n",
665                                 prog );
666                         exit( EXIT_FAILURE );
667 #endif
668                 case 'R':
669 #ifdef HAVE_CYRUS_SASL
670                         if( sasl_realm != NULL ) {
671                                 fprintf( stderr, "%s: -R previously specified\n", prog );
672                                 exit( EXIT_FAILURE );
673                         }
674                         if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
675                                 fprintf( stderr, "%s: incompatible previous "
676                                         "authentication choice\n",
677                                         prog );
678                                 exit( EXIT_FAILURE );
679                         }
680                         authmethod = LDAP_AUTH_SASL;
681                         sasl_realm = ber_strdup( optarg );
682 #else
683                         fprintf( stderr, "%s: not compiled with SASL support\n",
684                                 prog );
685                         exit( EXIT_FAILURE );
686 #endif
687                         break;
688                 case 'U':
689 #ifdef HAVE_CYRUS_SASL
690                         if( sasl_authc_id != NULL ) {
691                                 fprintf( stderr, "%s: -U previously specified\n", prog );
692                                 exit( EXIT_FAILURE );
693                         }
694                         if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
695                                 fprintf( stderr, "%s: incompatible previous "
696                                         "authentication choice\n",
697                                         prog );
698                                 exit( EXIT_FAILURE );
699                         }
700                         authmethod = LDAP_AUTH_SASL;
701                         sasl_authc_id = ber_strdup( optarg );
702 #else
703                         fprintf( stderr, "%s: not compiled with SASL support\n",
704                                 prog );
705                         exit( EXIT_FAILURE );
706 #endif
707                         break;
708                 case 'v':       /* verbose mode */
709                         verbose++;
710                         break;
711                 case 'V':       /* version */
712                         version++;
713                         break;
714                 case 'w':       /* password */
715                         passwd.bv_val = ber_strdup( optarg );
716                         {
717                                 char* p;
718
719                                 for( p = optarg; *p != '\0'; p++ ) {
720                                         *p = '\0';
721                                 }
722                         }
723                         passwd.bv_len = strlen( passwd.bv_val );
724                         break;
725                 case 'W':
726                         want_bindpw++;
727                         break;
728                 case 'y':
729                         pw_file = optarg;
730                         break;
731                 case 'Y':
732 #ifdef HAVE_CYRUS_SASL
733                         if( sasl_mech != NULL ) {
734                                 fprintf( stderr, "%s: -Y previously specified\n", prog );
735                                 exit( EXIT_FAILURE );
736                         }
737                         if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
738                                 fprintf( stderr,
739                                         "%s: incompatible with authentication choice\n", prog );
740                                 exit( EXIT_FAILURE );
741                         }
742                         authmethod = LDAP_AUTH_SASL;
743                         sasl_mech = ber_strdup( optarg );
744 #else
745                         fprintf( stderr, "%s: not compiled with SASL support\n", prog );
746                         exit( EXIT_FAILURE );
747 #endif
748                         break;
749                 case 'x':
750                         if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
751                                 fprintf( stderr, "%s: incompatible with previous "
752                                         "authentication choice\n", prog );
753                                 exit( EXIT_FAILURE );
754                         }
755                         authmethod = LDAP_AUTH_SIMPLE;
756                         break;
757                 case 'X':
758 #ifdef HAVE_CYRUS_SASL
759                         if( sasl_authz_id != NULL ) {
760                                 fprintf( stderr, "%s: -X previously specified\n", prog );
761                                 exit( EXIT_FAILURE );
762                         }
763                         if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
764                                 fprintf( stderr, "%s: -X incompatible with "
765                                         "authentication choice\n", prog );
766                                 exit( EXIT_FAILURE );
767                         }
768                         authmethod = LDAP_AUTH_SASL;
769                         sasl_authz_id = ber_strdup( optarg );
770 #else
771                         fprintf( stderr, "%s: not compiled with SASL support\n", prog );
772                         exit( EXIT_FAILURE );
773 #endif
774                         break;
775                 case 'Z':
776 #ifdef HAVE_TLS
777                         use_tls++;
778 #else
779                         fprintf( stderr, "%s: not compiled with TLS support\n", prog );
780                         exit( EXIT_FAILURE );
781 #endif
782                         break;
783                 default:
784                         if( handle_private_option( i ) ) break;
785                         fprintf( stderr, "%s: unrecognized option -%c\n",
786                                 prog, optopt );
787                         usage();
788                 }
789         }
790
791         {
792                 /* prevent bad linking */
793                 LDAPAPIInfo api;
794                 api.ldapai_info_version = LDAP_API_INFO_VERSION;
795
796                 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
797                         != LDAP_OPT_SUCCESS )
798                 {
799                         fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
800                         exit( EXIT_FAILURE );
801                 }
802
803                 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
804                         fprintf( stderr, "LDAP APIInfo version mismatch: "
805                                 "library %d, header %d\n",
806                                 api.ldapai_info_version, LDAP_API_INFO_VERSION );
807                         exit( EXIT_FAILURE );
808                 }
809
810                 if( api.ldapai_api_version != LDAP_API_VERSION ) {
811                         fprintf( stderr, "LDAP API version mismatch: "
812                                 "library %d, header %d\n",
813                                 api.ldapai_api_version, LDAP_API_VERSION );
814                         exit( EXIT_FAILURE );
815                 }
816
817                 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
818                         fprintf( stderr, "LDAP vendor name mismatch: "
819                                 "library %s, header %s\n",
820                                 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
821                         exit( EXIT_FAILURE );
822                 }
823
824                 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
825                         fprintf( stderr, "LDAP vendor version mismatch: "
826                                 "library %d, header %d\n",
827                                 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
828                         exit( EXIT_FAILURE );
829                 }
830
831                 if (version) {
832                         fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
833                                 prog, __Version,
834                                 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
835                         if (version > 1) exit( EXIT_SUCCESS );
836                 }
837
838                 ldap_memfree( api.ldapai_vendor_name );
839                 ber_memvfree( (void **)api.ldapai_extensions );
840         }
841
842         if (protocol == -1)
843                 protocol = LDAP_VERSION3;
844
845         if (authmethod == -1 && protocol > LDAP_VERSION2) {
846 #ifdef HAVE_CYRUS_SASL
847                 authmethod = LDAP_AUTH_SASL;
848 #else
849                 authmethod = LDAP_AUTH_SIMPLE;
850 #endif
851         }
852
853         if( ldapuri == NULL ) {
854                 if( ldapport && ( ldaphost == NULL )) {
855                         fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
856                         exit( EXIT_FAILURE );
857                 }
858         } else {
859                 if( ldaphost != NULL ) {
860                         fprintf( stderr, "%s: -H incompatible with -h\n", prog );
861                         exit( EXIT_FAILURE );
862                 }
863                 if( ldapport ) {
864                         fprintf( stderr, "%s: -H incompatible with -p\n", prog );
865                         exit( EXIT_FAILURE );
866                 }
867         }
868
869         if( protocol == LDAP_VERSION2 ) {
870                 if( assertctl || authzid || manageDIT || manageDSAit ||
871 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
872                         proxydn ||
873 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
874 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
875                         chaining ||
876 #endif
877                         noop || ppolicy || preread || postread )
878                 {
879                         fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
880                         exit( EXIT_FAILURE );
881                 }
882 #ifdef HAVE_TLS
883                 if( use_tls ) {
884                         fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
885                         exit( EXIT_FAILURE );
886                 }
887 #endif
888 #ifdef HAVE_CYRUS_SASL
889                 if( authmethod == LDAP_AUTH_SASL ) {
890                         fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
891                                 prog );
892                         exit( EXIT_FAILURE );
893                 }
894 #endif
895
896 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
897         } else {
898                 if ( authmethod == LDAP_AUTH_KRBV4 || authmethod == LDAP_AUTH_KRBV41 ) {
899                         fprintf( stderr, "%s: -k/-K incompatible with LDAPv%d\n",
900                                 prog, protocol );
901                         exit( EXIT_FAILURE );
902                 }
903 #endif
904         }
905 }
906
907
908 LDAP *
909 tool_conn_setup( int dont, void (*private_setup)( LDAP * ) )
910 {
911         LDAP *ld = NULL;
912
913         if ( debug ) {
914                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
915                         != LBER_OPT_SUCCESS )
916                 {
917                         fprintf( stderr,
918                                 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
919                 }
920                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
921                         != LDAP_OPT_SUCCESS )
922                 {
923                         fprintf( stderr,
924                                 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
925                 }
926         }
927
928 #ifdef SIGPIPE
929         (void) SIGNAL( SIGPIPE, SIG_IGN );
930 #endif
931
932         if ( abcan ) {
933                 SIGNAL( SIGINT, do_sig );
934         }
935
936         if ( !dont ) {
937                 int rc;
938
939                 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
940                         /* construct URL */
941                         LDAPURLDesc url;
942                         memset( &url, 0, sizeof(url));
943
944                         url.lud_scheme = "ldap";
945                         url.lud_host = ldaphost;
946                         url.lud_port = ldapport;
947                         url.lud_scope = LDAP_SCOPE_DEFAULT;
948
949                         ldapuri = ldap_url_desc2str( &url );
950                 }
951
952                 if ( verbose ) {
953                         fprintf( stderr, "ldap_initialize( %s )\n",
954                                 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
955                 }
956                 rc = ldap_initialize( &ld, ldapuri );
957                 if( rc != LDAP_SUCCESS ) {
958                         fprintf( stderr,
959                                 "Could not create LDAP session handle for URI=%s (%d): %s\n",
960                                 ldapuri, rc, ldap_err2string(rc) );
961                         exit( EXIT_FAILURE );
962                 }
963
964                 if( private_setup ) private_setup( ld );
965
966                 /* referrals */
967                 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
968                         referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
969                 {
970                         fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
971                                 referrals ? "on" : "off" );
972                         exit( EXIT_FAILURE );
973                 }
974
975                 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
976                         != LDAP_OPT_SUCCESS )
977                 {
978                         fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
979                                 protocol );
980                         exit( EXIT_FAILURE );
981                 }
982
983                 if ( use_tls ) {
984                         rc = ldap_start_tls_s( ld, NULL, NULL );
985                         if ( rc != LDAP_SUCCESS ) {
986                                 tool_perror( "ldap_start_tls", rc, NULL, NULL, NULL, NULL );
987                                 if ( use_tls > 1 ) {
988                                         exit( EXIT_FAILURE );
989                                 }
990                         }
991                 }
992         }
993
994         return ld;
995 }
996
997
998 void
999 tool_bind( LDAP *ld )
1000 {
1001         LDAPControl     **sctrlsp = NULL;
1002         LDAPControl     *sctrls[2];
1003         int             nsctrls = 0;
1004
1005 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1006         LDAPControl c;
1007         if ( ppolicy ) {
1008                 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1009                 c.ldctl_value.bv_val = NULL;
1010                 c.ldctl_value.bv_len = 0;
1011                 c.ldctl_iscritical = 0;
1012                 sctrls[nsctrls] = &c;
1013                 sctrls[++nsctrls] = NULL;
1014         }
1015 #endif
1016
1017         if ( nsctrls ) {
1018                 sctrlsp = sctrls;
1019         }
1020
1021         assert( nsctrls < sizeof(sctrls)/sizeof(sctrls[0]) );
1022
1023         if ( authmethod == LDAP_AUTH_SASL ) {
1024 #ifdef HAVE_CYRUS_SASL
1025                 void *defaults;
1026                 int rc;
1027
1028                 if( sasl_secprops != NULL ) {
1029                         rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
1030                                 (void *) sasl_secprops );
1031
1032                         if( rc != LDAP_OPT_SUCCESS ) {
1033                                 fprintf( stderr,
1034                                         "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
1035                                         sasl_secprops );
1036                                 exit( LDAP_LOCAL_ERROR );
1037                         }
1038                 }
1039
1040                 defaults = lutil_sasl_defaults( ld,
1041                         sasl_mech,
1042                         sasl_realm,
1043                         sasl_authc_id,
1044                         passwd.bv_val,
1045                         sasl_authz_id );
1046
1047                 rc = ldap_sasl_interactive_bind_s( ld, binddn, sasl_mech,
1048                         sctrlsp,
1049                         NULL, sasl_flags, lutil_sasl_interact, defaults );
1050
1051                 lutil_sasl_freedefs( defaults );
1052                 if( rc != LDAP_SUCCESS ) {
1053                         tool_perror( "ldap_sasl_interactive_bind_s",
1054                                 rc, NULL, NULL, NULL, NULL );
1055                         exit( rc );
1056                 }
1057 #else
1058                 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1059                 exit( LDAP_NOT_SUPPORTED );
1060 #endif
1061         } else {
1062                 int msgid, err, rc;
1063                 LDAPMessage *result;
1064                 LDAPControl **ctrls;
1065                 char msgbuf[256];
1066                 char *matched = NULL;
1067                 char *info = NULL;
1068                 char **refs = NULL;
1069
1070                 msgbuf[0] = 0;
1071
1072 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
1073                 if ( authmethod == LDAP_AUTH_KRBV4 || authmethod == LDAP_AUTH_KRBV41 ) {
1074                         msgid = ldap_bind( ld, binddn, passwd.bv_val, authmethod );
1075                         if ( msgid == -1 ) {
1076                                 tool_perror( "ldap_bind", -1, NULL, NULL, NULL, NULL );
1077                                 exit( LDAP_LOCAL_ERROR );
1078                         }
1079                 } else
1080 #endif
1081                 {
1082                         /* simple bind */
1083                         rc = ldap_sasl_bind( ld, binddn, LDAP_SASL_SIMPLE, &passwd,
1084                                 sctrlsp, NULL, &msgid );
1085                         if ( msgid == -1 ) {
1086                                 tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
1087                                         NULL, NULL, NULL, NULL );
1088                                 exit( rc );
1089                         }
1090                 }
1091
1092                 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 ) {
1093                         tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
1094                         exit( LDAP_LOCAL_ERROR );
1095                 }
1096
1097                 rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
1098                         &ctrls, 1 );
1099                 if ( rc != LDAP_SUCCESS ) {
1100                         tool_perror( "ldap_bind parse result", rc, NULL, NULL, NULL, NULL );
1101                         exit( LDAP_LOCAL_ERROR );
1102                 }
1103
1104 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1105                 if ( ctrls && ppolicy ) {
1106                         LDAPControl *ctrl;
1107                         int expire, grace, len = 0;
1108                         LDAPPasswordPolicyError pErr = -1;
1109                         
1110                         ctrl = ldap_find_control( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
1111                                 ctrls );
1112
1113                         if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
1114                                 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1115                         {
1116                                 if ( pErr != PP_noError ){
1117                                         msgbuf[0] = ';';
1118                                         msgbuf[1] = ' ';
1119                                         strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1120                                         len = strlen( msgbuf );
1121                                 }
1122                                 if ( expire >= 0 ) {
1123                                         sprintf( msgbuf+len,
1124                                                 " (Password expires in %d seconds)",
1125                                                 expire );
1126                                 } else if ( grace >= 0 ) {
1127                                         sprintf( msgbuf+len,
1128                                                 " (Password expired, %d grace logins remain)",
1129                                                 grace );
1130                                 }
1131                         }
1132                 }
1133 #endif
1134
1135                 if ( ctrls ) {
1136                         ldap_controls_free( ctrls );
1137                 }
1138
1139                 if ( err != LDAP_SUCCESS
1140                         || msgbuf[0]
1141                         || ( matched && matched[ 0 ] )
1142                         || ( info && info[ 0 ] )
1143                         || refs )
1144                 {
1145                         tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1146
1147                         if( matched ) ber_memfree( matched );
1148                         if( info ) ber_memfree( info );
1149                         if( refs ) ber_memvfree( (void **)refs );
1150
1151                         if ( err != LDAP_SUCCESS ) exit( err );
1152                 }
1153         }
1154 }
1155
1156 void
1157 tool_unbind( LDAP *ld )
1158 {
1159         int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1160
1161         if ( err != LDAP_OPT_SUCCESS ) {
1162                 fprintf( stderr, "Could not unset controls\n");
1163         }
1164
1165         (void) ldap_unbind_ext( ld, NULL, NULL );
1166 }
1167
1168
1169 /* Set server controls.  Add controls extra_c[0..count-1], if set. */
1170 void
1171 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1172 {
1173         int i = 0, j, crit = 0, err;
1174         LDAPControl c[10], **ctrls;
1175
1176         if ( ! ( assertctl
1177                 || authzid
1178 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1179                 || proxydn
1180 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1181                 || manageDIT
1182                 || manageDSAit
1183                 || noop
1184                 || preread
1185                 || postread
1186 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1187                 || chaining
1188 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1189                 || count ) )
1190         {
1191                 return;
1192         }
1193
1194         ctrls = (LDAPControl**) malloc(sizeof(c) + (count+1)*sizeof(LDAPControl*));
1195         if ( ctrls == NULL ) {
1196                 fprintf( stderr, "No memory\n" );
1197                 exit( EXIT_FAILURE );
1198         }
1199
1200         if ( assertctl ) {
1201                 BerElementBuffer berbuf;
1202                 BerElement *ber = (BerElement *)&berbuf;
1203                 
1204                 if( assertion == NULL || *assertion == '\0' ) {
1205                         fprintf( stderr, "Assertion=<empty>\n" );
1206                         exit( EXIT_FAILURE );
1207                 }
1208
1209                 ber_init2( ber, NULL, LBER_USE_DER );
1210
1211                 err = ldap_pvt_put_filter( ber, assertion );
1212                 if( err < 0 ) {
1213                         fprintf( stderr, "assertion encode failed (%d)\n", err );
1214                         exit( EXIT_FAILURE );
1215                 }
1216
1217                 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1218                 if( err < 0 ) {
1219                         fprintf( stderr, "assertion flatten failed (%d)\n", err );
1220                         exit( EXIT_FAILURE );
1221                 }
1222
1223                 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1224                 c[i].ldctl_iscritical = assertctl > 1;
1225                 ctrls[i] = &c[i];
1226                 i++;
1227         }
1228
1229         if ( authzid ) {
1230                 c[i].ldctl_value.bv_val = authzid;
1231                 c[i].ldctl_value.bv_len = strlen( authzid );
1232                 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1233                 c[i].ldctl_iscritical = 1;
1234                 ctrls[i] = &c[i];
1235                 i++;
1236         }
1237
1238 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1239         /* NOTE: doesn't need an extra count because it's incompatible
1240          * with authzid */
1241         if ( proxydn ) {
1242                 BerElementBuffer berbuf;
1243                 BerElement *ber = (BerElement *)&berbuf;
1244                 
1245                 ber_init2( ber, NULL, LBER_USE_DER );
1246
1247                 if ( ber_printf( ber, "s", proxydn ) == LBER_ERROR ) {
1248                         exit( EXIT_FAILURE );
1249                 }
1250
1251                 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1252                         exit( EXIT_FAILURE );
1253                 }
1254
1255                 c[i].ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1256                 c[i].ldctl_iscritical = 1;
1257                 ctrls[i] = &c[i];
1258                 i++;
1259         }
1260 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1261
1262         if ( manageDIT ) {
1263                 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1264                 BER_BVZERO( &c[i].ldctl_value );
1265                 c[i].ldctl_iscritical = manageDIT > 1;
1266                 ctrls[i] = &c[i];
1267                 i++;
1268         }
1269
1270         if ( manageDSAit ) {
1271                 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1272                 BER_BVZERO( &c[i].ldctl_value );
1273                 c[i].ldctl_iscritical = manageDSAit > 1;
1274                 ctrls[i] = &c[i];
1275                 i++;
1276         }
1277
1278         if ( noop ) {
1279                 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1280                 BER_BVZERO( &c[i].ldctl_value );
1281                 c[i].ldctl_iscritical = noop > 1;
1282                 ctrls[i] = &c[i];
1283                 i++;
1284         }
1285
1286         if ( preread ) {
1287                 char berbuf[LBER_ELEMENT_SIZEOF];
1288                 BerElement *ber = (BerElement *)berbuf;
1289                 char **attrs = NULL;
1290
1291                 if( preread_attrs ) {
1292                         attrs = ldap_str2charray( preread_attrs, "," );
1293                 }
1294
1295                 ber_init2( ber, NULL, LBER_USE_DER );
1296
1297                 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1298                         fprintf( stderr, "preread attrs encode failed.\n" );
1299                         exit( EXIT_FAILURE );
1300                 }
1301
1302                 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1303                 if( err < 0 ) {
1304                         fprintf( stderr, "preread flatten failed (%d)\n", err );
1305                         exit( EXIT_FAILURE );
1306                 }
1307
1308                 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1309                 c[i].ldctl_iscritical = preread > 1;
1310                 ctrls[i] = &c[i];
1311                 i++;
1312
1313                 if( attrs ) ldap_charray_free( attrs );
1314         }
1315
1316         if ( postread ) {
1317                 char berbuf[LBER_ELEMENT_SIZEOF];
1318                 BerElement *ber = (BerElement *)berbuf;
1319                 char **attrs = NULL;
1320
1321                 if( postread_attrs ) {
1322                         attrs = ldap_str2charray( postread_attrs, "," );
1323                 }
1324
1325                 ber_init2( ber, NULL, LBER_USE_DER );
1326
1327                 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1328                         fprintf( stderr, "postread attrs encode failed.\n" );
1329                         exit( EXIT_FAILURE );
1330                 }
1331
1332                 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1333                 if( err < 0 ) {
1334                         fprintf( stderr, "postread flatten failed (%d)\n", err );
1335                         exit( EXIT_FAILURE );
1336                 }
1337
1338                 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1339                 c[i].ldctl_iscritical = postread > 1;
1340                 ctrls[i] = &c[i];
1341                 i++;
1342
1343                 if( attrs ) ldap_charray_free( attrs );
1344         }
1345
1346 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1347         if ( chaining ) {
1348                 if ( chainingResolve > -1 ) {
1349                         BerElementBuffer berbuf;
1350                         BerElement *ber = (BerElement *)&berbuf;
1351
1352                         ber_init2( ber, NULL, LBER_USE_DER );
1353
1354                         err = ber_printf( ber, "{e" /* } */, chainingResolve );
1355                         if ( err == -1 ) {
1356                                 ber_free( ber, 1 );
1357                                 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1358                                 exit( EXIT_FAILURE );
1359                         }
1360
1361                         if ( chainingContinuation > -1 ) {
1362                                 err = ber_printf( ber, "e", chainingContinuation );
1363                                 if ( err == -1 ) {
1364                                         ber_free( ber, 1 );
1365                                         fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1366                                         exit( EXIT_FAILURE );
1367                                 }
1368                         }
1369
1370                         err = ber_printf( ber, /* { */ "N}" );
1371                         if ( err == -1 ) {
1372                                 ber_free( ber, 1 );
1373                                 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1374                                 exit( EXIT_FAILURE );
1375                         }
1376
1377                         if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1378                                 exit( EXIT_FAILURE );
1379                         }
1380
1381                 } else {
1382                         BER_BVZERO( &c[i].ldctl_value );
1383                 }
1384
1385                 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1386                 c[i].ldctl_iscritical = chaining > 1;
1387                 ctrls[i] = &c[i];
1388                 i++;
1389         }
1390 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1391
1392         while ( count-- ) {
1393                 ctrls[i++] = extra_c++;
1394         }
1395         ctrls[i] = NULL;
1396
1397         err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1398
1399         if ( err != LDAP_OPT_SUCCESS ) {
1400                 for ( j = 0; j < i; j++ ) {
1401                         if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1402                 }
1403                 fprintf( stderr, "Could not set %scontrols\n",
1404                         crit ? "critical " : "" );
1405         }
1406
1407         free( ctrls );
1408         if ( crit ) {
1409                 exit( EXIT_FAILURE );
1410         }
1411 }
1412
1413 int
1414 tool_check_abandon( LDAP *ld, int msgid )
1415 {
1416         int     rc;
1417
1418         switch ( gotintr ) {
1419         case LDAP_REQ_EXTENDED:
1420                 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1421                 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1422                                 rc, ldap_err2string( rc ) );
1423                 return -1;
1424
1425         case LDAP_REQ_ABANDON:
1426                 rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
1427                 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1428                                 rc, ldap_err2string( rc ) );
1429                 return -1;
1430         }
1431
1432         return 0;
1433 }
1434
1435 static int
1436 print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
1437 {
1438         BerElement      *ber;
1439         struct berval   bv;
1440
1441         tool_write_ldif( LDIF_PUT_COMMENT, "==> ",
1442                 what->bv_val, what->bv_len );
1443         ber = ber_init( &ctrl->ldctl_value );
1444         if ( ber == NULL ) {
1445                 /* error? */
1446                 return 1;
1447
1448         } else if ( ber_scanf( ber, "{m{" /*}}*/, &bv ) == LBER_ERROR ) {
1449                 /* error? */
1450                 return 1;
1451
1452         } else {
1453                 tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1454
1455                 while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
1456                         int             i;
1457                         BerVarray       vals = NULL;
1458
1459                         if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
1460                                 vals == NULL )
1461                         {
1462                                 /* error? */
1463                                 return 1;
1464                         }
1465                 
1466                         for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
1467                                 tool_write_ldif(
1468                                         ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1469                                         bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
1470                         }
1471
1472                         ber_bvarray_free( vals );
1473                 }
1474         }
1475
1476         if ( ber != NULL ) {
1477                 ber_free( ber, 1 );
1478         }
1479
1480         tool_write_ldif( LDIF_PUT_COMMENT, "<== ",
1481                 what->bv_val, what->bv_len );
1482
1483         return 0;
1484 }
1485
1486 static int
1487 print_preread( LDAP *ld, LDAPControl *ctrl )
1488 {
1489         static struct berval what = BER_BVC( "preread" );
1490
1491         return print_prepostread( ld, ctrl, &what );
1492 }
1493
1494 static int
1495 print_postread( LDAP *ld, LDAPControl *ctrl )
1496 {
1497         static struct berval what = BER_BVC( "postread" );
1498
1499         return print_prepostread( ld, ctrl, &what );
1500 }
1501
1502 static int
1503 print_paged_results( LDAP *ld, LDAPControl *ctrl )
1504 {
1505         ber_int_t estimate;
1506
1507         /* note: pr_cookie is being malloced; it's freed
1508          * the next time the control is sent, but the last
1509          * time it's not; we don't care too much, because
1510          * the last time an empty value is returned... */
1511         if ( ldap_parse_pageresponse_control( ld, ctrl, &estimate, &pr_cookie )
1512                 != LDAP_SUCCESS )
1513         {
1514                 /* error? */
1515                 return 1;
1516
1517         } else {
1518                 /* FIXME: check buffer overflow */
1519                 char    buf[ BUFSIZ ], *ptr = buf;
1520
1521                 if ( estimate > 0 ) {
1522                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1523                                 "estimate=%d", estimate );
1524                 }
1525
1526                 if ( pr_cookie.bv_len > 0 ) {
1527                         struct berval   bv;
1528
1529                         bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
1530                                 pr_cookie.bv_len ) + 1;
1531                         bv.bv_val = ber_memalloc( bv.bv_len + 1 );
1532
1533                         bv.bv_len = lutil_b64_ntop(
1534                                 (unsigned char *) pr_cookie.bv_val,
1535                                 pr_cookie.bv_len,
1536                                 bv.bv_val, bv.bv_len );
1537
1538                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1539                                 "%scookie=%s", ptr == buf ? "" : " ",
1540                                 bv.bv_val );
1541
1542                         ber_memfree( bv.bv_val );
1543
1544                         pr_morePagedResults = 1;
1545
1546                 } else {
1547                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1548                                 "%scookie=", ptr == buf ? "" : " " );
1549                 }
1550
1551                 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1552                         "pagedresults", buf, ptr - buf );
1553         }
1554
1555         return 0;
1556 }
1557
1558 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1559 static int
1560 print_ppolicy( LDAP *ld, LDAPControl *ctrl )
1561 {
1562         int expire = 0, grace = 0, rc;
1563         LDAPPasswordPolicyError pperr;
1564
1565         rc = ldap_parse_passwordpolicy_control( ld, ctrl,
1566                 &expire, &grace, &pperr );
1567         if ( rc == LDAP_SUCCESS ) {
1568                 char    buf[ BUFSIZ ], *ptr = buf;
1569
1570                 if ( expire != -1 ) {
1571                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1572                                 "expire=%d", expire );
1573                 }
1574
1575                 if ( grace != -1 ) {
1576                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1577                                 "%sgrace=%d", ptr == buf ? "" : " ", grace );
1578                 }
1579
1580                 if ( pperr != PP_noError ) {
1581                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1582                                 "%serror=%s", ptr == buf ? "" : " ",
1583                                 ldap_passwordpolicy_err2txt( pperr ) );
1584                 }
1585
1586                 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1587                         "ppolicy", buf, ptr - buf );
1588         }
1589
1590         return rc;
1591 }
1592 #endif
1593
1594 void tool_print_ctrls(
1595         LDAP            *ld,
1596         LDAPControl     **ctrls )
1597 {
1598         int     i;
1599         char    *ptr;
1600
1601         for ( i = 0; ctrls[i] != NULL; i++ ) {
1602                 /* control: OID criticality base64value */
1603                 struct berval b64 = BER_BVNULL;
1604                 ber_len_t len;
1605                 char *str;
1606                 int j;
1607
1608                 len = ldif ? 2 : 0;
1609                 len += strlen( ctrls[i]->ldctl_oid );
1610
1611                 /* add enough for space after OID and the critical value itself */
1612                 len += ctrls[i]->ldctl_iscritical
1613                         ? sizeof("true") : sizeof("false");
1614
1615                 /* convert to base64 */
1616                 if ( ctrls[i]->ldctl_value.bv_len ) {
1617                         b64.bv_len = LUTIL_BASE64_ENCODE_LEN(
1618                                 ctrls[i]->ldctl_value.bv_len ) + 1;
1619                         b64.bv_val = ber_memalloc( b64.bv_len + 1 );
1620
1621                         b64.bv_len = lutil_b64_ntop(
1622                                 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
1623                                 ctrls[i]->ldctl_value.bv_len,
1624                                 b64.bv_val, b64.bv_len );
1625                 }
1626
1627                 if ( b64.bv_len ) {
1628                         len += 1 + b64.bv_len;
1629                 }
1630
1631                 ptr = str = malloc( len + 1 );
1632                 if ( ldif ) {
1633                         ptr = lutil_strcopy( ptr, ": " );
1634                 }
1635                 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_oid );
1636                 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_iscritical
1637                         ? " true" : " false" );
1638
1639                 if ( b64.bv_len ) {
1640                         ptr = lutil_strcopy( ptr, " " );
1641                         ptr = lutil_strcopy( ptr, b64.bv_val );
1642                 }
1643
1644                 if ( ldif < 2 ) {
1645                         tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1646                                 "control", str, len );
1647                 }
1648
1649                 free( str );
1650                 if ( b64.bv_len ) {
1651                         ber_memfree( b64.bv_val );
1652                 }
1653
1654                 /* known controls */
1655                 for ( j = 0; tool_ctrl_response[j].oid != NULL; j++ ) {
1656                         if ( strcmp( tool_ctrl_response[j].oid, ctrls[i]->ldctl_oid ) == 0 ) {
1657                                 if ( !tool_ctrl_response[j].mask & tool_type ) {
1658                                         /* this control should not appear
1659                                          * with this tool; warning? */
1660                                 }
1661                                 break;
1662                         }
1663                 }
1664
1665                 if ( tool_ctrl_response[j].oid != NULL && tool_ctrl_response[j].func ) {
1666                         (void)tool_ctrl_response[j].func( ld, ctrls[i] );
1667                 }
1668         }
1669 }
1670
1671 int
1672 tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
1673 {
1674         char    *ldif;
1675
1676         if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1677                 return( -1 );
1678         }
1679
1680         fputs( ldif, stdout );
1681         ber_memfree( ldif );
1682
1683         return( 0 );
1684 }
1685
1686 int
1687 tool_is_oid( const char *s )
1688 {
1689         int             first = 1;
1690
1691         if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
1692                 return 0;
1693         }
1694
1695         for ( ; s[ 0 ]; s++ ) {
1696                 if ( s[ 0 ] == '.' ) {
1697                         if ( s[ 1 ] == '\0' ) {
1698                                 return 0;
1699                         }
1700                         first = 1;
1701                         continue;
1702                 }
1703
1704                 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
1705                         return 0;
1706                 }
1707
1708                 if ( first == 1 && s[ 0 ] == '0' && s[ 1 ] != '.' ) {
1709                         return 0;
1710                 }
1711                 first = 0;
1712         }
1713
1714         return 1;
1715 }
1716