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