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