]> git.sur5r.net Git - openldap/blob - clients/tools/common.c
5eef356604ac4cd51653f8093afc58ae63717119
[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-2010 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 #include <ac/socket.h>
37
38 #ifdef HAVE_CYRUS_SASL
39 #ifdef HAVE_SASL_SASL_H
40 #include <sasl/sasl.h>
41 #else
42 #include <sasl.h>
43 #endif
44 #endif
45
46 #include <ldap.h>
47
48 #include "ldif.h"
49 #include "lutil.h"
50 #include "lutil_ldap.h"
51 #include "ldap_defaults.h"
52 #include "ldap_pvt.h"
53 #include "lber_pvt.h"
54
55 #include "common.h"
56
57 /* input-related vars */
58
59 /* misc. parameters */
60 tool_type_t     tool_type;
61 int             contoper = 0;
62 int             debug = 0;
63 char            *infile = NULL;
64 int             dont = 0;
65 int             nocanon = 0;
66 int             referrals = 0;
67 int             verbose = 0;
68 int             ldif = 0;
69 ber_len_t       ldif_wrap = LDIF_LINE_WIDTH;
70 char            *prog = NULL;
71
72 /* connection */
73 char            *ldapuri = NULL;
74 char            *ldaphost = NULL;
75 int             ldapport = 0;
76 int             use_tls = 0;
77 int             protocol = -1;
78 int             version = 0;
79
80 /* authc/authz */
81 int             authmethod = -1;
82 char            *binddn = NULL;
83 int             want_bindpw = 0;
84 struct berval   passwd = { 0, NULL };
85 char            *pw_file = NULL;
86 #ifdef HAVE_CYRUS_SASL
87 unsigned        sasl_flags = LDAP_SASL_AUTOMATIC;
88 char            *sasl_realm = NULL;
89 char            *sasl_authc_id = NULL;
90 char            *sasl_authz_id = NULL;
91 char            *sasl_mech = NULL;
92 char            *sasl_secprops = NULL;
93 #endif
94
95 /* controls */
96 int             assertctl;
97 char            *assertion = NULL;
98 struct berval   assertionvalue = BER_BVNULL;
99 char            *authzid = NULL;
100 /* support deprecated early version of proxyAuthz */
101 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ       "2.16.840.1.113730.3.4.12"
102 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
103 char            *proxydn = NULL;
104 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
105 int             manageDIT = 0;
106 int             manageDSAit = 0;
107 int             noop = 0;
108 int             ppolicy = 0;
109 int             preread = 0;
110 static char     *preread_attrs = NULL;
111 int             postread = 0;
112 static char     *postread_attrs = NULL;
113 ber_int_t       pr_morePagedResults = 1;
114 struct berval   pr_cookie = { 0, NULL };
115 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
116 int             chaining = 0;
117 static int      chainingResolve = -1;
118 static int      chainingContinuation = -1;
119 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
120 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
121 static int      sessionTracking = 0;
122 struct berval   stValue;
123 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
124 ber_int_t vlvPos;
125 ber_int_t vlvCount;
126 struct berval *vlvContext;
127
128 LDAPControl     *unknown_ctrls = NULL;
129 int             unknown_ctrls_num = 0;
130
131 /* options */
132 struct timeval  nettimeout = { -1 , 0 };
133
134 typedef int (*print_ctrl_fn)( LDAP *ld, LDAPControl *ctrl );
135
136 static int print_preread( LDAP *ld, LDAPControl *ctrl );
137 static int print_postread( LDAP *ld, LDAPControl *ctrl );
138 static int print_paged_results( LDAP *ld, LDAPControl *ctrl );
139 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
140 static int print_ppolicy( LDAP *ld, LDAPControl *ctrl );
141 #endif
142 static int print_sss( LDAP *ld, LDAPControl *ctrl );
143 static int print_vlv( LDAP *ld, LDAPControl *ctrl );
144 #ifdef LDAP_CONTROL_X_DEREF
145 static int print_deref( LDAP *ld, LDAPControl *ctrl );
146 #endif
147 #ifdef LDAP_CONTROL_X_WHATFAILED
148 static int print_whatfailed( LDAP *ld, LDAPControl *ctrl );
149 #endif
150
151 static struct tool_ctrls_t {
152         const char      *oid;
153         unsigned        mask;
154         print_ctrl_fn   func;
155 } tool_ctrl_response[] = {
156         { LDAP_CONTROL_PRE_READ,                        TOOL_ALL,       print_preread },
157         { LDAP_CONTROL_POST_READ,                       TOOL_ALL,       print_postread },
158         { LDAP_CONTROL_PAGEDRESULTS,                    TOOL_SEARCH,    print_paged_results },
159 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
160         { LDAP_CONTROL_PASSWORDPOLICYRESPONSE,          TOOL_ALL,       print_ppolicy },
161 #endif
162         { LDAP_CONTROL_SORTRESPONSE,    TOOL_SEARCH,    print_sss },
163         { LDAP_CONTROL_VLVRESPONSE,             TOOL_SEARCH,    print_vlv },
164 #ifdef LDAP_CONTROL_X_DEREF
165         { LDAP_CONTROL_X_DEREF,                         TOOL_SEARCH,    print_deref },
166 #endif
167 #ifdef LDAP_CONTROL_X_WHATFAILED
168         { LDAP_CONTROL_X_WHATFAILED,                    TOOL_ALL,       print_whatfailed },
169 #endif
170         { NULL,                                         0,              NULL }
171 };
172
173 /* "features" */
174 enum { Intr_None = 0, Intr_Abandon, Intr_Cancel, Intr_Ignore }; 
175 static volatile sig_atomic_t    gotintr, abcan;
176
177
178 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
179 static int
180 st_value( LDAP *ld, struct berval *value )
181 {
182         char            *ip = NULL, *name = NULL;
183         struct berval   id = { 0 };
184         char            namebuf[ MAXHOSTNAMELEN ];
185
186         if ( gethostname( namebuf, sizeof( namebuf ) ) == 0 ) {
187                 struct hostent  *h;
188                 struct in_addr  addr;
189
190                 name = namebuf;
191
192                 h = gethostbyname( name );
193                 if ( h != NULL ) {
194                         AC_MEMCPY( &addr, h->h_addr, sizeof( addr ) );
195                         ip = inet_ntoa( addr );
196                 }
197         }
198
199 #ifdef HAVE_CYRUS_SASL
200         if ( sasl_authz_id != NULL ) {
201                 ber_str2bv( sasl_authz_id, 0, 0, &id );
202
203         } else if ( sasl_authc_id != NULL ) {
204                 ber_str2bv( sasl_authc_id, 0, 0, &id );
205
206         } else 
207 #endif /* HAVE_CYRUS_SASL */
208         if ( binddn != NULL ) {
209                 ber_str2bv( binddn, 0, 0, &id );
210         }
211
212         if ( ldap_create_session_tracking_value( ld,
213                 ip, name, LDAP_CONTROL_X_SESSION_TRACKING_USERNAME,
214                 &id, &stValue ) )
215         {
216                 fprintf( stderr, _("Session tracking control encoding error!\n") );
217                 return -1;
218         }
219
220         return 0;
221 }
222 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
223
224 RETSIGTYPE
225 do_sig( int sig )
226 {
227         gotintr = abcan;
228 }
229
230 void
231 tool_init( tool_type_t type )
232 {
233         tool_type = type;
234         ldap_pvt_setlocale(LC_MESSAGES, "");
235         ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
236         ldap_pvt_textdomain(OPENLDAP_PACKAGE);
237 }
238
239 void
240 tool_destroy( void )
241 {
242 #ifdef HAVE_CYRUS_SASL
243         sasl_done();
244 #endif
245 #ifdef HAVE_TLS
246         ldap_pvt_tls_destroy();
247 #endif
248
249         if ( ldapuri != NULL ) {
250                 ber_memfree( ldapuri );
251                 ldapuri = NULL;
252         }
253
254         if ( pr_cookie.bv_val != NULL ) {
255                 ber_memfree( pr_cookie.bv_val );
256                 pr_cookie.bv_val = NULL;
257                 pr_cookie.bv_len = 0;
258         }
259
260         if ( binddn != NULL ) {
261                 ber_memfree( binddn );
262         }
263
264         if ( passwd.bv_val != NULL ) {
265                 ber_memfree( passwd.bv_val );
266         }
267
268         if ( infile != NULL ) {
269                 ber_memfree( infile );
270         }
271 }
272
273 void
274 tool_common_usage( void )
275 {
276         static const char *const descriptions[] = {
277 N_("  -d level   set LDAP debugging level to `level'\n"),
278 N_("  -D binddn  bind DN\n"),
279 N_("  -e [!]<ext>[=<extparam>] general extensions (! indicates criticality)\n")
280 N_("             [!]assert=<filter>     (RFC 4528; a RFC 4515 Filter string)\n")
281 N_("             [!]authzid=<authzid>   (RFC 4370; \"dn:<dn>\" or \"u:<user>\")\n")
282 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
283 #if 0
284                  /* non-advertized support for proxyDN */
285 N_("             [!]proxydn=<dn>        (a RFC 4514 DN string)\n")
286 #endif
287 #endif
288 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
289 N_("             [!]chaining[=<resolveBehavior>[/<continuationBehavior>]]\n")
290 N_("                     one of \"chainingPreferred\", \"chainingRequired\",\n")
291 N_("                     \"referralsPreferred\", \"referralsRequired\"\n")
292 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
293 N_("             [!]manageDSAit         (RFC 3296)\n")
294 N_("             [!]noop\n")
295 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
296 N_("             ppolicy\n")
297 #endif
298 N_("             [!]postread[=<attrs>]  (RFC 4527; comma-separated attr list)\n")
299 N_("             [!]preread[=<attrs>]   (RFC 4527; comma-separated attr list)\n")
300 N_("             [!]relax\n")
301 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
302 N_("             [!]sessiontracking\n")
303 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
304 N_("             abandon, cancel, ignore (SIGINT sends abandon/cancel,\n"
305    "             or ignores response; if critical, doesn't wait for SIGINT.\n"
306    "             not really controls)\n")
307 N_("  -h host    LDAP server\n"),
308 N_("  -H URI     LDAP Uniform Resource Identifier(s)\n"),
309 N_("  -I         use SASL Interactive mode\n"),
310 N_("  -n         show what would be done but don't actually do it\n"),
311 N_("  -N         do not use reverse DNS to canonicalize SASL host name\n"),
312 N_("  -O props   SASL security properties\n"),
313 N_("  -o <opt>[=<optparam] general options\n"),
314 N_("             nettimeout=<timeout> (in seconds, or \"none\" or \"max\")\n"),
315 N_("             ldif-wrap=<width> (in columns, or \"no\" for no wrapping)\n"),
316 N_("  -p port    port on LDAP server\n"),
317 N_("  -Q         use SASL Quiet mode\n"),
318 N_("  -R realm   SASL realm\n"),
319 N_("  -U authcid SASL authentication identity\n"),
320 N_("  -v         run in verbose mode (diagnostics to standard output)\n"),
321 N_("  -V         print version info (-VV only)\n"),
322 N_("  -w passwd  bind password (for simple authentication)\n"),
323 N_("  -W         prompt for bind password\n"),
324 N_("  -x         Simple authentication\n"),
325 N_("  -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
326 N_("  -y file    Read password from file\n"),
327 N_("  -Y mech    SASL mechanism\n"),
328 N_("  -Z         Start TLS request (-ZZ to require successful response)\n"),
329 NULL
330         };
331         const char *const *cpp;
332
333         fputs( _("Common options:\n"), stderr );
334         for( cpp = descriptions; *cpp != NULL; cpp++ ) {
335                 if( strchr( options, (*cpp)[3] ) || (*cpp)[3] == ' ' ) {
336                         fputs( _(*cpp), stderr );
337                 }
338         }
339 }
340
341 void tool_perror(
342         const char *func,
343         int err,
344         const char *extra,
345         const char *matched,
346         const char *info,
347         char **refs )
348 {
349         fprintf( stderr, "%s: %s (%d)%s\n",
350                 func, ldap_err2string( err ), err, extra ? extra : "" );
351
352         if ( matched && *matched ) {
353                 fprintf( stderr, _("\tmatched DN: %s\n"), matched );
354         }
355
356         if ( info && *info ) {
357                 fprintf( stderr, _("\tadditional info: %s\n"), info );
358         }
359
360         if ( refs && *refs ) {
361                 int i;
362                 fprintf( stderr, _("\treferrals:\n") );
363                 for( i=0; refs[i]; i++ ) {
364                         fprintf( stderr, "\t\t%s\n", refs[i] );
365                 }
366         }
367 }
368
369
370 void
371 tool_args( int argc, char **argv )
372 {
373         int i;
374
375         while (( i = getopt( argc, argv, options )) != EOF ) {
376                 int crit, ival;
377                 char *control, *cvalue, *next;
378                 switch( i ) {
379                 case 'c':       /* continuous operation mode */
380                         contoper++;
381                         break;
382                 case 'C':
383                         referrals++;
384                         break;
385                 case 'd':
386                         ival = strtol( optarg, &next, 10 );
387                         if (next == NULL || next[0] != '\0') {
388                                 fprintf( stderr, "%s: unable to parse debug value \"%s\"\n", prog, optarg);
389                                 exit(EXIT_FAILURE);
390                         }
391                         debug |= ival;
392                         break;
393                 case 'D':       /* bind DN */
394                         if( binddn != NULL ) {
395                                 fprintf( stderr, "%s: -D previously specified\n", prog );
396                                 exit( EXIT_FAILURE );
397                         }
398                         binddn = ber_strdup( optarg );
399                         break;
400                 case 'e':       /* general extensions (controls and such) */
401                         /* should be extended to support comma separated list of
402                          *      [!]key[=value] parameters, e.g.  -e !foo,bar=567
403                          */
404
405                         crit = 0;
406                         cvalue = NULL;
407                         if( optarg[0] == '!' ) {
408                                 crit = 1;
409                                 optarg++;
410                         }
411
412                         control = ber_strdup( optarg );
413                         if ( (cvalue = strchr( control, '=' )) != NULL ) {
414                                 *cvalue++ = '\0';
415                         }
416
417                         if ( strcasecmp( control, "assert" ) == 0 ) {
418                                 if( assertctl ) {
419                                         fprintf( stderr, "assert control previously specified\n");
420                                         exit( EXIT_FAILURE );
421                                 }
422                                 if( cvalue == NULL ) {
423                                         fprintf( stderr, "assert: control value expected\n" );
424                                         usage();
425                                 }
426
427                                 assertctl = 1 + crit;
428
429                                 assert( assertion == NULL );
430                                 assertion = cvalue;
431
432                         } else if ( strcasecmp( control, "authzid" ) == 0 ) {
433                                 if( authzid != NULL ) {
434                                         fprintf( stderr, "authzid control previously specified\n");
435                                         exit( EXIT_FAILURE );
436                                 }
437 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
438                                 if( proxydn != NULL ) {
439                                         fprintf( stderr, "authzid control incompatible with proxydn\n");
440                                         exit( EXIT_FAILURE );
441                                 }
442 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
443                                 if( cvalue == NULL ) {
444                                         fprintf( stderr, "authzid: control value expected\n" );
445                                         usage();
446                                 }
447                                 if( !crit ) {
448                                         fprintf( stderr, "authzid: must be marked critical\n" );
449                                         usage();
450                                 }
451
452                                 assert( authzid == NULL );
453                                 authzid = cvalue;
454
455 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
456                         } else if ( strcasecmp( control, "proxydn" ) == 0 ) {
457                                 if( proxydn != NULL ) {
458                                         fprintf( stderr, "proxydn control previously specified\n");
459                                         exit( EXIT_FAILURE );
460                                 }
461                                 if( authzid != NULL ) {
462                                         fprintf( stderr, "proxydn control incompatible with authzid\n");
463                                         exit( EXIT_FAILURE );
464                                 }
465                                 if( cvalue == NULL ) {
466                                         fprintf( stderr, "proxydn: control value expected\n" );
467                                         usage();
468                                 }
469                                 if( !crit ) {
470                                         fprintf( stderr, "proxydn: must be marked critical\n" );
471                                         usage();
472                                 }
473
474                                 assert( proxydn == NULL );
475                                 proxydn = cvalue;
476 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
477
478                         } else if ( ( strcasecmp( control, "relax" ) == 0 ) ||
479                                 ( strcasecmp( control, "manageDIT" ) == 0 ) )
480                         {
481                                 if( manageDIT ) {
482                                         fprintf( stderr,
483                                                 "relax control previously specified\n");
484                                         exit( EXIT_FAILURE );
485                                 }
486                                 if( cvalue != NULL ) {
487                                         fprintf( stderr,
488                                                 "relax: no control value expected\n" );
489                                         usage();
490                                 }
491
492                                 manageDIT = 1 + crit;
493
494                         } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
495                                 if( manageDSAit ) {
496                                         fprintf( stderr,
497                                                 "manageDSAit control previously specified\n");
498                                         exit( EXIT_FAILURE );
499                                 }
500                                 if( cvalue != NULL ) {
501                                         fprintf( stderr,
502                                                 "manageDSAit: no control value expected\n" );
503                                         usage();
504                                 }
505
506                                 manageDSAit = 1 + crit;
507
508                         } else if ( strcasecmp( control, "noop" ) == 0 ) {
509                                 if( noop ) {
510                                         fprintf( stderr, "noop control previously specified\n");
511                                         exit( EXIT_FAILURE );
512                                 }
513                                 if( cvalue != NULL ) {
514                                         fprintf( stderr, "noop: no control value expected\n" );
515                                         usage();
516                                 }
517
518                                 noop = 1 + crit;
519
520 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
521                         } else if ( strcasecmp( control, "ppolicy" ) == 0 ) {
522                                 if( ppolicy ) {
523                                         fprintf( stderr, "ppolicy control previously specified\n");
524                                         exit( EXIT_FAILURE );
525                                 }
526                                 if( cvalue != NULL ) {
527                                         fprintf( stderr, "ppolicy: no control value expected\n" );
528                                         usage();
529                                 }
530                                 if( crit ) {
531                                         fprintf( stderr, "ppolicy: critical flag not allowed\n" );
532                                         usage();
533                                 }
534
535                                 ppolicy = 1;
536 #endif
537
538                         } else if ( strcasecmp( control, "preread" ) == 0 ) {
539                                 if( preread ) {
540                                         fprintf( stderr, "preread control previously specified\n");
541                                         exit( EXIT_FAILURE );
542                                 }
543
544                                 preread = 1 + crit;
545                                 preread_attrs = cvalue;
546
547                         } else if ( strcasecmp( control, "postread" ) == 0 ) {
548                                 if( postread ) {
549                                         fprintf( stderr, "postread control previously specified\n");
550                                         exit( EXIT_FAILURE );
551                                 }
552
553                                 postread = 1 + crit;
554                                 postread_attrs = cvalue;
555
556 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
557                         } else if ( strcasecmp( control, "chaining" ) == 0 ) {
558                                 chaining = 1 + crit;
559
560                                 if ( cvalue != NULL ) {
561                                         char    *continuation;
562
563                                         continuation = strchr( cvalue, '/' );
564                                         if ( continuation ) {
565                                                 /* FIXME: this makes sense only in searches */
566                                                 *continuation++ = '\0';
567                                                 if ( strcasecmp( continuation, "chainingPreferred" ) == 0 ) {
568                                                         chainingContinuation = LDAP_CHAINING_PREFERRED;
569                                                 } else if ( strcasecmp( continuation, "chainingRequired" ) == 0 ) {
570                                                         chainingContinuation = LDAP_CHAINING_REQUIRED;
571                                                 } else if ( strcasecmp( continuation, "referralsPreferred" ) == 0 ) {
572                                                         chainingContinuation = LDAP_REFERRALS_PREFERRED;
573                                                 } else if ( strcasecmp( continuation, "referralsRequired" ) == 0 ) {
574                                                         chainingContinuation = LDAP_REFERRALS_REQUIRED;
575                                                 } else {
576                                                         fprintf( stderr,
577                                                                 "chaining behavior control "
578                                                                 "continuation value \"%s\" invalid\n",
579                                                                 continuation );
580                                                         exit( EXIT_FAILURE );
581                                                 }
582                                         }
583         
584                                         if ( strcasecmp( cvalue, "chainingPreferred" ) == 0 ) {
585                                                 chainingResolve = LDAP_CHAINING_PREFERRED;
586                                         } else if ( strcasecmp( cvalue, "chainingRequired" ) == 0 ) {
587                                                 chainingResolve = LDAP_CHAINING_REQUIRED;
588                                         } else if ( strcasecmp( cvalue, "referralsPreferred" ) == 0 ) {
589                                                 chainingResolve = LDAP_REFERRALS_PREFERRED;
590                                         } else if ( strcasecmp( cvalue, "referralsRequired" ) == 0 ) {
591                                                 chainingResolve = LDAP_REFERRALS_REQUIRED;
592                                         } else {
593                                                 fprintf( stderr,
594                                                         "chaining behavior control "
595                                                         "resolve value \"%s\" invalid\n",
596                                                         cvalue);
597                                                 exit( EXIT_FAILURE );
598                                         }
599                                 }
600 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
601
602 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
603                         } else if ( strcasecmp( control, "sessiontracking" ) == 0 ) {
604                                 if ( sessionTracking ) {
605                                         fprintf( stderr, "%s: session tracking can be only specified once\n", prog );
606                                         exit( EXIT_FAILURE );
607                                 }
608                                 sessionTracking = 1;
609                                 if( crit ) {
610                                         fprintf( stderr, "sessiontracking: critical flag not allowed\n" );
611                                         usage();
612                                 }
613 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
614
615                         /* this shouldn't go here, really; but it's a feature... */
616                         } else if ( strcasecmp( control, "abandon" ) == 0 ) {
617                                 abcan = Intr_Abandon;
618                                 if ( crit ) {
619                                         gotintr = abcan;
620                                 }
621
622                         } else if ( strcasecmp( control, "cancel" ) == 0 ) {
623                                 abcan = Intr_Cancel;
624                                 if ( crit ) {
625                                         gotintr = abcan;
626                                 }
627
628                         } else if ( strcasecmp( control, "ignore" ) == 0 ) {
629                                 abcan = Intr_Ignore;
630                                 if ( crit ) {
631                                         gotintr = abcan;
632                                 }
633
634                         } else if ( tool_is_oid( control ) ) {
635                                 LDAPControl     *tmpctrls, ctrl;
636
637                                 tmpctrls = (LDAPControl *)ber_memrealloc( unknown_ctrls,
638                                         (unknown_ctrls_num + 1)*sizeof( LDAPControl ) );
639                                 if ( tmpctrls == NULL ) {
640                                         fprintf( stderr, "%s: no memory?\n", prog );
641                                         exit( EXIT_FAILURE );
642                                 }
643                                 unknown_ctrls = tmpctrls;
644                                 ctrl.ldctl_oid = control;
645                                 ctrl.ldctl_value.bv_val = NULL;
646                                 ctrl.ldctl_value.bv_len = 0;
647                                 ctrl.ldctl_iscritical = crit;
648
649                                 if ( cvalue != NULL ) {
650                                         struct berval   bv;
651                                         size_t          len = strlen( cvalue );
652                                         int             retcode;
653
654                                         bv.bv_len = LUTIL_BASE64_DECODE_LEN( len );
655                                         bv.bv_val = ber_memalloc( bv.bv_len + 1 );
656
657                                         retcode = lutil_b64_pton( cvalue,
658                                                 (unsigned char *)bv.bv_val,
659                                                 bv.bv_len );
660
661                                         if ( retcode == -1 || (unsigned) retcode > bv.bv_len ) {
662                                                 fprintf( stderr, "Unable to parse value of general control %s\n",
663                                                         control );
664                                                 usage();
665                                         }
666
667                                         bv.bv_len = retcode;
668                                         ctrl.ldctl_value = bv;
669                                 }
670
671                                 unknown_ctrls[ unknown_ctrls_num ] = ctrl;
672                                 unknown_ctrls_num++;
673
674                         } else {
675                                 fprintf( stderr, "Invalid general control name: %s\n",
676                                         control );
677                                 usage();
678                         }
679                         break;
680                 case 'f':       /* read from file */
681                         if( infile != NULL ) {
682                                 fprintf( stderr, "%s: -f previously specified\n", prog );
683                                 exit( EXIT_FAILURE );
684                         }
685                         infile = ber_strdup( optarg );
686                         break;
687                 case 'h':       /* ldap host */
688                         if( ldaphost != NULL ) {
689                                 fprintf( stderr, "%s: -h previously specified\n", prog );
690                                 exit( EXIT_FAILURE );
691                         }
692                         ldaphost = ber_strdup( optarg );
693                         break;
694                 case 'H':       /* ldap URI */
695                         if( ldapuri != NULL ) {
696                                 fprintf( stderr, "%s: -H previously specified\n", prog );
697                                 exit( EXIT_FAILURE );
698                         }
699                         ldapuri = ber_strdup( optarg );
700                         break;
701                 case 'I':
702 #ifdef HAVE_CYRUS_SASL
703                         if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
704                                 fprintf( stderr, "%s: incompatible previous "
705                                         "authentication choice\n",
706                                         prog );
707                                 exit( EXIT_FAILURE );
708                         }
709                         authmethod = LDAP_AUTH_SASL;
710                         sasl_flags = LDAP_SASL_INTERACTIVE;
711                         break;
712 #else
713                         fprintf( stderr, "%s: was not compiled with SASL support\n",
714                                 prog );
715                         exit( EXIT_FAILURE );
716 #endif
717                 case 'M':
718                         /* enable Manage DSA IT */
719                         manageDSAit++;
720                         break;
721                 case 'n':       /* print operations, don't actually do them */
722                         dont++;
723                         break;
724                 case 'N':
725                         nocanon++;
726                         break;
727                 case 'o':
728                         control = ber_strdup( optarg );
729                         if ( (cvalue = strchr( control, '=' )) != NULL ) {
730                                 *cvalue++ = '\0';
731                         }
732
733                         if ( strcasecmp( control, "nettimeout" ) == 0 ) {
734                                 if( nettimeout.tv_sec != -1 ) {
735                                         fprintf( stderr, "nettimeout option previously specified\n");
736                                         exit( EXIT_FAILURE );
737                                 }
738                                 if( cvalue == NULL || cvalue[0] == '\0' ) {
739                                         fprintf( stderr, "nettimeout: option value expected\n" );
740                                         usage();
741                                 }
742                                 if ( strcasecmp( cvalue, "none" ) == 0 ) {
743                                         nettimeout.tv_sec = 0;
744                                 } else if ( strcasecmp( cvalue, "max" ) == 0 ) {
745                                         nettimeout.tv_sec = LDAP_MAXINT;
746                                 } else {
747                                         ival = strtol( cvalue, &next, 10 );
748                                         if ( next == NULL || next[0] != '\0' ) {
749                                                 fprintf( stderr,
750                                                         _("Unable to parse network timeout \"%s\"\n"), cvalue );
751                                                 exit( EXIT_FAILURE );
752                                         }
753                                         nettimeout.tv_sec = ival;
754                                 }
755                                 if( nettimeout.tv_sec < 0 || nettimeout.tv_sec > LDAP_MAXINT ) {
756                                         fprintf( stderr, _("%s: invalid network timeout (%ld) specified\n"),
757                                                 prog, (long)nettimeout.tv_sec );
758                                         exit( EXIT_FAILURE );
759                                 }
760
761                         } else if ( strcasecmp( control, "ldif-wrap" ) == 0 ) {
762                                 if ( cvalue == 0 ) {
763                                         ldif_wrap = LDIF_LINE_WIDTH;
764
765                                 } else if ( strcasecmp( cvalue, "no" ) == 0 ) {
766                                         ldif_wrap = LDIF_LINE_WIDTH_MAX;
767
768                                 } else {
769                                         unsigned int u;
770                                         if ( lutil_atou( &u, cvalue ) ) {
771                                                 fprintf( stderr,
772                                                         _("Unable to parse ldif-wrap=\"%s\"\n"), cvalue );
773                                                 exit( EXIT_FAILURE );
774                                         }
775                                         ldif_wrap = (ber_len_t)u;
776                                 }
777
778                         } else {
779                                 fprintf( stderr, "Invalid general option name: %s\n",
780                                         control );
781                                 usage();
782                         }
783                         ber_memfree(control);
784                         break;
785                 case 'O':
786 #ifdef HAVE_CYRUS_SASL
787                         if( sasl_secprops != NULL ) {
788                                 fprintf( stderr, "%s: -O previously specified\n", prog );
789                                 exit( EXIT_FAILURE );
790                         }
791                         if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
792                                 fprintf( stderr, "%s: incompatible previous "
793                                         "authentication choice\n", prog );
794                                 exit( EXIT_FAILURE );
795                         }
796                         authmethod = LDAP_AUTH_SASL;
797                         sasl_secprops = ber_strdup( optarg );
798 #else
799                         fprintf( stderr, "%s: not compiled with SASL support\n", prog );
800                         exit( EXIT_FAILURE );
801 #endif
802                         break;
803                 case 'p':
804                         if( ldapport ) {
805                                 fprintf( stderr, "%s: -p previously specified\n", prog );
806                                 exit( EXIT_FAILURE );
807                         }
808                         ival = strtol( optarg, &next, 10 );
809                         if ( next == NULL || next[0] != '\0' ) {
810                                 fprintf( stderr, "%s: unable to parse port number \"%s\"\n", prog, optarg );
811                                 exit( EXIT_FAILURE );
812                         }
813                         ldapport = ival;
814                         break;
815                 case 'P':
816                         ival = strtol( optarg, &next, 10 );
817                         if ( next == NULL || next[0] != '\0' ) {
818                                 fprintf( stderr, "%s: unable to parse protocol version \"%s\"\n", prog, optarg );
819                                 exit( EXIT_FAILURE );
820                         }
821                         switch( ival ) {
822                         case 2:
823                                 if( protocol == LDAP_VERSION3 ) {
824                                         fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
825                                                 prog, protocol );
826                                         exit( EXIT_FAILURE );
827                                 }
828                                 protocol = LDAP_VERSION2;
829                                 break;
830                         case 3:
831                                 if( protocol == LDAP_VERSION2 ) {
832                                         fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
833                                                 prog, protocol );
834                                         exit( EXIT_FAILURE );
835                                 }
836                                 protocol = LDAP_VERSION3;
837                                 break;
838                         default:
839                                 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
840                                         prog );
841                                 usage();
842                         }
843                         break;
844                 case 'Q':
845 #ifdef HAVE_CYRUS_SASL
846                         if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
847                                 fprintf( stderr, "%s: incompatible previous "
848                                         "authentication choice\n",
849                                         prog );
850                                 exit( EXIT_FAILURE );
851                         }
852                         authmethod = LDAP_AUTH_SASL;
853                         sasl_flags = LDAP_SASL_QUIET;
854                         break;
855 #else
856                         fprintf( stderr, "%s: not compiled with SASL support\n",
857                                 prog );
858                         exit( EXIT_FAILURE );
859 #endif
860                 case 'R':
861 #ifdef HAVE_CYRUS_SASL
862                         if( sasl_realm != NULL ) {
863                                 fprintf( stderr, "%s: -R previously specified\n", prog );
864                                 exit( EXIT_FAILURE );
865                         }
866                         if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
867                                 fprintf( stderr, "%s: incompatible previous "
868                                         "authentication choice\n",
869                                         prog );
870                                 exit( EXIT_FAILURE );
871                         }
872                         authmethod = LDAP_AUTH_SASL;
873                         sasl_realm = ber_strdup( optarg );
874 #else
875                         fprintf( stderr, "%s: not compiled with SASL support\n",
876                                 prog );
877                         exit( EXIT_FAILURE );
878 #endif
879                         break;
880                 case 'U':
881 #ifdef HAVE_CYRUS_SASL
882                         if( sasl_authc_id != NULL ) {
883                                 fprintf( stderr, "%s: -U previously specified\n", prog );
884                                 exit( EXIT_FAILURE );
885                         }
886                         if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
887                                 fprintf( stderr, "%s: incompatible previous "
888                                         "authentication choice\n",
889                                         prog );
890                                 exit( EXIT_FAILURE );
891                         }
892                         authmethod = LDAP_AUTH_SASL;
893                         sasl_authc_id = ber_strdup( optarg );
894 #else
895                         fprintf( stderr, "%s: not compiled with SASL support\n",
896                                 prog );
897                         exit( EXIT_FAILURE );
898 #endif
899                         break;
900                 case 'v':       /* verbose mode */
901                         verbose++;
902                         break;
903                 case 'V':       /* version */
904                         version++;
905                         break;
906                 case 'w':       /* password */
907                         passwd.bv_val = ber_strdup( optarg );
908                         {
909                                 char* p;
910
911                                 for( p = optarg; *p != '\0'; p++ ) {
912                                         *p = '\0';
913                                 }
914                         }
915                         passwd.bv_len = strlen( passwd.bv_val );
916                         break;
917                 case 'W':
918                         want_bindpw++;
919                         break;
920                 case 'y':
921                         pw_file = optarg;
922                         break;
923                 case 'Y':
924 #ifdef HAVE_CYRUS_SASL
925                         if( sasl_mech != NULL ) {
926                                 fprintf( stderr, "%s: -Y previously specified\n", prog );
927                                 exit( EXIT_FAILURE );
928                         }
929                         if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
930                                 fprintf( stderr,
931                                         "%s: incompatible with authentication choice\n", prog );
932                                 exit( EXIT_FAILURE );
933                         }
934                         authmethod = LDAP_AUTH_SASL;
935                         sasl_mech = ber_strdup( optarg );
936 #else
937                         fprintf( stderr, "%s: not compiled with SASL support\n", prog );
938                         exit( EXIT_FAILURE );
939 #endif
940                         break;
941                 case 'x':
942                         if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
943                                 fprintf( stderr, "%s: incompatible with previous "
944                                         "authentication choice\n", prog );
945                                 exit( EXIT_FAILURE );
946                         }
947                         authmethod = LDAP_AUTH_SIMPLE;
948                         break;
949                 case 'X':
950 #ifdef HAVE_CYRUS_SASL
951                         if( sasl_authz_id != NULL ) {
952                                 fprintf( stderr, "%s: -X previously specified\n", prog );
953                                 exit( EXIT_FAILURE );
954                         }
955                         if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
956                                 fprintf( stderr, "%s: -X incompatible with "
957                                         "authentication choice\n", prog );
958                                 exit( EXIT_FAILURE );
959                         }
960                         authmethod = LDAP_AUTH_SASL;
961                         sasl_authz_id = ber_strdup( optarg );
962 #else
963                         fprintf( stderr, "%s: not compiled with SASL support\n", prog );
964                         exit( EXIT_FAILURE );
965 #endif
966                         break;
967                 case 'Z':
968 #ifdef HAVE_TLS
969                         use_tls++;
970 #else
971                         fprintf( stderr, "%s: not compiled with TLS support\n", prog );
972                         exit( EXIT_FAILURE );
973 #endif
974                         break;
975                 default:
976                         if( handle_private_option( i ) ) break;
977                         fprintf( stderr, "%s: unrecognized option -%c\n",
978                                 prog, optopt );
979                         usage();
980                 }
981         }
982
983         {
984                 /* prevent bad linking */
985                 LDAPAPIInfo api;
986                 api.ldapai_info_version = LDAP_API_INFO_VERSION;
987
988                 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
989                         != LDAP_OPT_SUCCESS )
990                 {
991                         fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
992                         exit( EXIT_FAILURE );
993                 }
994
995                 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
996                         fprintf( stderr, "LDAP APIInfo version mismatch: "
997                                 "library %d, header %d\n",
998                                 api.ldapai_info_version, LDAP_API_INFO_VERSION );
999                         exit( EXIT_FAILURE );
1000                 }
1001
1002                 if( api.ldapai_api_version != LDAP_API_VERSION ) {
1003                         fprintf( stderr, "LDAP API version mismatch: "
1004                                 "library %d, header %d\n",
1005                                 api.ldapai_api_version, LDAP_API_VERSION );
1006                         exit( EXIT_FAILURE );
1007                 }
1008
1009                 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
1010                         fprintf( stderr, "LDAP vendor name mismatch: "
1011                                 "library %s, header %s\n",
1012                                 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
1013                         exit( EXIT_FAILURE );
1014                 }
1015
1016                 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
1017                         fprintf( stderr, "LDAP vendor version mismatch: "
1018                                 "library %d, header %d\n",
1019                                 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
1020                         exit( EXIT_FAILURE );
1021                 }
1022
1023                 if (version) {
1024                         fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
1025                                 prog, __Version,
1026                                 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
1027                         if (version > 1) exit( EXIT_SUCCESS );
1028                 }
1029
1030                 ldap_memfree( api.ldapai_vendor_name );
1031                 ber_memvfree( (void **)api.ldapai_extensions );
1032         }
1033
1034         if (protocol == -1)
1035                 protocol = LDAP_VERSION3;
1036
1037         if (authmethod == -1 && protocol > LDAP_VERSION2) {
1038 #ifdef HAVE_CYRUS_SASL
1039                 if ( binddn != NULL ) {
1040                         authmethod = LDAP_AUTH_SIMPLE;
1041                 } else {
1042                         authmethod = LDAP_AUTH_SASL;
1043                 }
1044 #else
1045                 authmethod = LDAP_AUTH_SIMPLE;
1046 #endif
1047         }
1048
1049         if( ldapuri == NULL ) {
1050                 if( ldapport && ( ldaphost == NULL )) {
1051                         fprintf( stderr, "%s: -p without -h is invalid.\n", prog );
1052                         exit( EXIT_FAILURE );
1053                 }
1054         } else {
1055                 if( ldaphost != NULL ) {
1056                         fprintf( stderr, "%s: -H incompatible with -h\n", prog );
1057                         exit( EXIT_FAILURE );
1058                 }
1059                 if( ldapport ) {
1060                         fprintf( stderr, "%s: -H incompatible with -p\n", prog );
1061                         exit( EXIT_FAILURE );
1062                 }
1063         }
1064
1065         if( protocol == LDAP_VERSION2 ) {
1066                 if( assertctl || authzid || manageDIT || manageDSAit ||
1067 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1068                         proxydn ||
1069 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1070 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1071                         chaining ||
1072 #endif
1073 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1074                         sessionTracking ||
1075 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1076                         noop || ppolicy || preread || postread )
1077                 {
1078                         fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
1079                         exit( EXIT_FAILURE );
1080                 }
1081 #ifdef HAVE_TLS
1082                 if( use_tls ) {
1083                         fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
1084                         exit( EXIT_FAILURE );
1085                 }
1086 #endif
1087 #ifdef HAVE_CYRUS_SASL
1088                 if( authmethod == LDAP_AUTH_SASL ) {
1089                         fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
1090                                 prog );
1091                         exit( EXIT_FAILURE );
1092                 }
1093 #endif
1094         }
1095 }
1096
1097
1098 LDAP *
1099 tool_conn_setup( int dont, void (*private_setup)( LDAP * ) )
1100 {
1101         LDAP *ld = NULL;
1102
1103         if ( debug ) {
1104                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
1105                         != LBER_OPT_SUCCESS )
1106                 {
1107                         fprintf( stderr,
1108                                 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
1109                 }
1110                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
1111                         != LDAP_OPT_SUCCESS )
1112                 {
1113                         fprintf( stderr,
1114                                 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
1115                 }
1116         }
1117
1118 #ifdef SIGPIPE
1119         (void) SIGNAL( SIGPIPE, SIG_IGN );
1120 #endif
1121
1122         if ( abcan ) {
1123                 SIGNAL( SIGINT, do_sig );
1124         }
1125
1126         if ( !dont ) {
1127                 int rc;
1128
1129                 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
1130                         /* construct URL */
1131                         LDAPURLDesc url;
1132                         memset( &url, 0, sizeof(url));
1133
1134                         url.lud_scheme = "ldap";
1135                         url.lud_host = ldaphost;
1136                         url.lud_port = ldapport;
1137                         url.lud_scope = LDAP_SCOPE_DEFAULT;
1138
1139                         ldapuri = ldap_url_desc2str( &url );
1140
1141                 } else if ( ldapuri != NULL ) {
1142                         LDAPURLDesc     *ludlist, **ludp;
1143                         char            **urls = NULL;
1144                         int             nurls = 0;
1145
1146                         rc = ldap_url_parselist( &ludlist, ldapuri );
1147                         if ( rc != LDAP_URL_SUCCESS ) {
1148                                 fprintf( stderr,
1149                                         "Could not parse LDAP URI(s)=%s (%d)\n",
1150                                         ldapuri, rc );
1151                                 exit( EXIT_FAILURE );
1152                         }
1153
1154                         for ( ludp = &ludlist; *ludp != NULL; ) {
1155                                 LDAPURLDesc     *lud = *ludp;
1156                                 char            **tmp;
1157
1158                                 if ( lud->lud_dn != NULL && lud->lud_dn[ 0 ] != '\0' &&
1159                                         ( lud->lud_host == NULL || lud->lud_host[0] == '\0' ) )
1160                                 {
1161                                         /* if no host but a DN is provided,
1162                                          * use DNS SRV to gather the host list
1163                                          * and turn it into a list of URIs
1164                                          * using the scheme provided */
1165                                         char    *domain = NULL,
1166                                                 *hostlist = NULL,
1167                                                 **hosts = NULL;
1168                                         int     i,
1169                                                 len_proto = strlen( lud->lud_scheme );
1170
1171                                         if ( ldap_dn2domain( lud->lud_dn, &domain )
1172                                                 || domain == NULL )
1173                                         {
1174                                                 fprintf( stderr,
1175                                                         "DNS SRV: Could not turn "
1176                                                         "DN=\"%s\" into a domain\n",
1177                                                         lud->lud_dn );
1178                                                 goto dnssrv_free;
1179                                         }
1180                                         
1181                                         rc = ldap_domain2hostlist( domain, &hostlist );
1182                                         if ( rc ) {
1183                                                 fprintf( stderr,
1184                                                         "DNS SRV: Could not turn "
1185                                                         "domain=%s into a hostlist\n",
1186                                                         domain );
1187                                                 goto dnssrv_free;
1188                                         }
1189
1190                                         hosts = ldap_str2charray( hostlist, " " );
1191                                         if ( hosts == NULL ) {
1192                                                 fprintf( stderr,
1193                                                         "DNS SRV: Could not parse "
1194                                                         "hostlist=\"%s\"\n",
1195                                                         hostlist );
1196                                                 goto dnssrv_free;
1197                                         }
1198
1199                                         for ( i = 0; hosts[ i ] != NULL; i++ )
1200                                                 /* count'em */ ;
1201
1202                                         tmp = (char **)ber_memrealloc( urls, sizeof( char * ) * ( nurls + i + 1 ) );
1203                                         if ( tmp == NULL ) {
1204                                                 fprintf( stderr,
1205                                                         "DNS SRV: out of memory?\n" );
1206                                                 goto dnssrv_free;
1207                                         }
1208                                         urls = tmp;
1209                                         urls[ nurls ] = NULL;
1210
1211                                         for ( i = 0; hosts[ i ] != NULL; i++ ) {
1212                                                 size_t  len = len_proto
1213                                                         + STRLENOF( "://" )
1214                                                         + strlen( hosts[ i ] )
1215                                                         + 1;
1216
1217                                                 urls[ nurls + i + 1 ] = NULL;
1218                                                 urls[ nurls + i ] = (char *)malloc( sizeof( char ) * len );
1219                                                 if ( urls[ nurls + i ] == NULL ) {
1220                                                         fprintf( stderr,
1221                                                                 "DNS SRV: out of memory?\n" );
1222                                                         goto dnssrv_free;
1223                                                 }
1224
1225                                                 snprintf( urls[ nurls + i ], len, "%s://%s",
1226                                                         lud->lud_scheme, hosts[ i ] );
1227                                         }
1228                                         nurls += i;
1229
1230 dnssrv_free:;
1231                                         ber_memvfree( (void **)hosts );
1232                                         ber_memfree( hostlist );
1233                                         ber_memfree( domain );
1234
1235                                 } else {
1236                                         tmp = (char **)ber_memrealloc( urls, sizeof( char * ) * ( nurls + 2 ) );
1237                                         if ( tmp == NULL ) {
1238                                                 fprintf( stderr,
1239                                                         "DNS SRV: out of memory?\n" );
1240                                                 break;
1241                                         }
1242                                         urls = tmp;
1243                                         urls[ nurls + 1 ] = NULL;
1244
1245                                         urls[ nurls ] = ldap_url_desc2str( lud );
1246                                         if ( urls[ nurls ] == NULL ) {
1247                                                 fprintf( stderr,
1248                                                         "DNS SRV: out of memory?\n" );
1249                                                 break;
1250                                         }
1251                                         nurls++;
1252                                 }
1253
1254                                 *ludp = lud->lud_next;
1255
1256                                 lud->lud_next = NULL;
1257                                 ldap_free_urldesc( lud );
1258                         }
1259
1260                         if ( ludlist != NULL ) {
1261                                 ldap_free_urllist( ludlist );
1262                                 exit( EXIT_FAILURE );
1263
1264                         } else if ( urls == NULL ) {
1265                                 exit( EXIT_FAILURE );
1266                         }
1267
1268                         ldap_memfree( ldapuri );
1269                         ldapuri = ldap_charray2str( urls, " " );
1270                         ber_memvfree( (void **)urls );
1271                 }
1272
1273                 if ( verbose ) {
1274                         fprintf( stderr, "ldap_initialize( %s )\n",
1275                                 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
1276                 }
1277                 rc = ldap_initialize( &ld, ldapuri );
1278                 if( rc != LDAP_SUCCESS ) {
1279                         fprintf( stderr,
1280                                 "Could not create LDAP session handle for URI=%s (%d): %s\n",
1281                                 ldapuri, rc, ldap_err2string(rc) );
1282                         exit( EXIT_FAILURE );
1283                 }
1284
1285                 if( private_setup ) private_setup( ld );
1286
1287                 /* referrals */
1288                 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
1289                         referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
1290                 {
1291                         fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
1292                                 referrals ? "on" : "off" );
1293                         exit( EXIT_FAILURE );
1294                 }
1295
1296 #ifdef HAVE_CYRUS_SASL
1297                 /* canon */
1298                 if( ldap_set_option( ld, LDAP_OPT_X_SASL_NOCANON,
1299                         nocanon ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
1300                 {
1301                         fprintf( stderr, "Could not set LDAP_OPT_X_SASL_NOCANON %s\n",
1302                                 nocanon ? "on" : "off" );
1303                         exit( EXIT_FAILURE );
1304                 }
1305 #endif
1306                 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
1307                         != LDAP_OPT_SUCCESS )
1308                 {
1309                         fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
1310                                 protocol );
1311                         exit( EXIT_FAILURE );
1312                 }
1313
1314                 if ( use_tls ) {
1315                         rc = ldap_start_tls_s( ld, NULL, NULL );
1316                         if ( rc != LDAP_SUCCESS ) {
1317                                 char *msg=NULL;
1318                                 ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&msg);
1319                                 tool_perror( "ldap_start_tls", rc, NULL, NULL, msg, NULL );
1320                                 ldap_memfree(msg);
1321                                 if ( use_tls > 1 ) {
1322                                         exit( EXIT_FAILURE );
1323                                 }
1324                         }
1325                 }
1326
1327                 if ( nettimeout.tv_sec > 0 ) {
1328                         if ( ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (void *) &nettimeout )
1329                                 != LDAP_OPT_SUCCESS )
1330                         {
1331                                 fprintf( stderr, "Could not set LDAP_OPT_NETWORK_TIMEOUT %ld\n",
1332                                         (long)nettimeout.tv_sec );
1333                                 exit( EXIT_FAILURE );
1334                         }
1335                 }
1336         }
1337
1338         return ld;
1339 }
1340
1341
1342 void
1343 tool_bind( LDAP *ld )
1344 {
1345         LDAPControl     **sctrlsp = NULL;
1346         LDAPControl     *sctrls[3];
1347         LDAPControl     sctrl[3];
1348         int             nsctrls = 0;
1349
1350 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1351         if ( ppolicy ) {
1352                 LDAPControl c;
1353                 c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1354                 c.ldctl_value.bv_val = NULL;
1355                 c.ldctl_value.bv_len = 0;
1356                 c.ldctl_iscritical = 0;
1357                 sctrl[nsctrls] = c;
1358                 sctrls[nsctrls] = &sctrl[nsctrls];
1359                 sctrls[++nsctrls] = NULL;
1360         }
1361 #endif
1362
1363 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1364         if ( sessionTracking ) {
1365                 LDAPControl c;
1366
1367                 if (stValue.bv_val == NULL && st_value( ld, &stValue ) ) {
1368                         exit( EXIT_FAILURE );
1369                 }
1370
1371                 c.ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1372                 c.ldctl_iscritical = 0;
1373                 ber_dupbv( &c.ldctl_value, &stValue );
1374
1375                 sctrl[nsctrls] = c;
1376                 sctrls[nsctrls] = &sctrl[nsctrls];
1377                 sctrls[++nsctrls] = NULL;
1378         }
1379 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1380
1381         if ( nsctrls ) {
1382                 sctrlsp = sctrls;
1383         }
1384
1385         assert( nsctrls < (int) (sizeof(sctrls)/sizeof(sctrls[0])) );
1386
1387         if ( pw_file || want_bindpw ) {
1388                 assert( passwd.bv_val == NULL && passwd.bv_len == 0 );
1389
1390                 if ( pw_file ) {
1391                         if ( lutil_get_filed_password( pw_file, &passwd ) ) {
1392                                 exit( EXIT_FAILURE );
1393                         }
1394
1395                 } else {
1396                         char *pw = getpassphrase( _("Enter LDAP Password: ") );
1397                         if ( pw ) {
1398                                 passwd.bv_val = ber_strdup( pw );
1399                                 passwd.bv_len = strlen( passwd.bv_val );
1400                         }
1401                 }
1402         }
1403
1404         if ( authmethod == LDAP_AUTH_SASL ) {
1405 #ifdef HAVE_CYRUS_SASL
1406                 void *defaults;
1407                 int rc;
1408
1409                 if( sasl_secprops != NULL ) {
1410                         rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
1411                                 (void *) sasl_secprops );
1412
1413                         if( rc != LDAP_OPT_SUCCESS ) {
1414                                 fprintf( stderr,
1415                                         "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
1416                                         sasl_secprops );
1417                                 exit( LDAP_LOCAL_ERROR );
1418                         }
1419                 }
1420
1421                 defaults = lutil_sasl_defaults( ld,
1422                         sasl_mech,
1423                         sasl_realm,
1424                         sasl_authc_id,
1425                         passwd.bv_val,
1426                         sasl_authz_id );
1427
1428                 rc = ldap_sasl_interactive_bind_s( ld, binddn, sasl_mech,
1429                         sctrlsp,
1430                         NULL, sasl_flags, lutil_sasl_interact, defaults );
1431
1432                 lutil_sasl_freedefs( defaults );
1433                 if( rc != LDAP_SUCCESS ) {
1434                         char *msg=NULL;
1435                         ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&msg);
1436                         tool_perror( "ldap_sasl_interactive_bind_s",
1437                                 rc, NULL, NULL, msg, NULL );
1438                         ldap_memfree(msg);
1439                         exit( rc );
1440                 }
1441 #else
1442                 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
1443                 exit( LDAP_NOT_SUPPORTED );
1444 #endif
1445         } else {
1446                 int msgid, err, rc;
1447                 LDAPMessage *result;
1448                 LDAPControl **ctrls;
1449                 char msgbuf[256];
1450                 char *matched = NULL;
1451                 char *info = NULL;
1452                 char **refs = NULL;
1453
1454                 msgbuf[0] = 0;
1455
1456                 {
1457                         /* simple bind */
1458                         rc = ldap_sasl_bind( ld, binddn, LDAP_SASL_SIMPLE, &passwd,
1459                                 sctrlsp, NULL, &msgid );
1460                         if ( msgid == -1 ) {
1461                                 tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
1462                                         NULL, NULL, NULL, NULL );
1463                                 exit( rc );
1464                         }
1465                 }
1466
1467                 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result );
1468                 if ( rc == -1 ) {
1469                         tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
1470                         exit( LDAP_LOCAL_ERROR );
1471                 }
1472
1473                 if ( rc == 0 ) {
1474                         tool_perror( "ldap_result", LDAP_TIMEOUT, NULL, NULL, NULL, NULL );
1475                         exit( LDAP_LOCAL_ERROR );
1476                 }
1477
1478                 rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
1479                         &ctrls, 1 );
1480                 if ( rc != LDAP_SUCCESS ) {
1481                         tool_perror( "ldap_bind parse result", rc, NULL, matched, info, refs );
1482                         exit( LDAP_LOCAL_ERROR );
1483                 }
1484
1485 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1486                 if ( ctrls && ppolicy ) {
1487                         LDAPControl *ctrl;
1488                         int expire, grace, len = 0;
1489                         LDAPPasswordPolicyError pErr = -1;
1490                         
1491                         ctrl = ldap_control_find( LDAP_CONTROL_PASSWORDPOLICYRESPONSE,
1492                                 ctrls, NULL );
1493
1494                         if ( ctrl && ldap_parse_passwordpolicy_control( ld, ctrl,
1495                                 &expire, &grace, &pErr ) == LDAP_SUCCESS )
1496                         {
1497                                 if ( pErr != PP_noError ){
1498                                         msgbuf[0] = ';';
1499                                         msgbuf[1] = ' ';
1500                                         strcpy( msgbuf+2, ldap_passwordpolicy_err2txt( pErr ));
1501                                         len = strlen( msgbuf );
1502                                 }
1503                                 if ( expire >= 0 ) {
1504                                         sprintf( msgbuf+len,
1505                                                 " (Password expires in %d seconds)",
1506                                                 expire );
1507                                 } else if ( grace >= 0 ) {
1508                                         sprintf( msgbuf+len,
1509                                                 " (Password expired, %d grace logins remain)",
1510                                                 grace );
1511                                 }
1512                         }
1513                 }
1514 #endif
1515
1516                 if ( ctrls ) {
1517                         ldap_controls_free( ctrls );
1518                 }
1519
1520                 if ( err != LDAP_SUCCESS
1521                         || msgbuf[0]
1522                         || ( matched && matched[ 0 ] )
1523                         || ( info && info[ 0 ] )
1524                         || refs )
1525                 {
1526                         tool_perror( "ldap_bind", err, msgbuf, matched, info, refs );
1527
1528                         if( matched ) ber_memfree( matched );
1529                         if( info ) ber_memfree( info );
1530                         if( refs ) ber_memvfree( (void **)refs );
1531
1532                         if ( err != LDAP_SUCCESS ) exit( err );
1533                 }
1534         }
1535 }
1536
1537 void
1538 tool_unbind( LDAP *ld )
1539 {
1540         int err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
1541
1542         if ( err != LDAP_OPT_SUCCESS ) {
1543                 fprintf( stderr, "Could not unset controls\n");
1544         }
1545
1546         (void) ldap_unbind_ext( ld, NULL, NULL );
1547 }
1548
1549
1550 /* Set server controls.  Add controls extra_c[0..count-1], if set. */
1551 void
1552 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
1553 {
1554         int i = 0, j, crit = 0, err;
1555         LDAPControl c[16], **ctrls;
1556
1557         if ( ! ( assertctl
1558                 || authzid
1559 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1560                 || proxydn
1561 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1562                 || manageDIT
1563                 || manageDSAit
1564                 || noop
1565 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1566                 || ppolicy
1567 #endif
1568                 || preread
1569                 || postread
1570 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1571                 || chaining
1572 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1573 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1574                 || sessionTracking
1575 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1576                 || count
1577                 || unknown_ctrls_num ) )
1578         {
1579                 return;
1580         }
1581
1582         ctrls = (LDAPControl**) malloc(sizeof(c) + (count + unknown_ctrls_num + 1)*sizeof(LDAPControl*));
1583         if ( ctrls == NULL ) {
1584                 fprintf( stderr, "No memory\n" );
1585                 exit( EXIT_FAILURE );
1586         }
1587
1588         if ( assertctl ) {
1589                 if ( BER_BVISNULL( &assertionvalue ) ) {
1590                         err = ldap_create_assertion_control_value( ld,
1591                                 assertion, &assertionvalue );
1592                         if ( err ) {
1593                                 fprintf( stderr,
1594                                         "Unable to create assertion value "
1595                                         "\"%s\" (%d)\n", assertion, err );
1596                         }
1597                 }
1598
1599                 c[i].ldctl_oid = LDAP_CONTROL_ASSERT;
1600                 c[i].ldctl_value = assertionvalue;
1601                 c[i].ldctl_iscritical = assertctl > 1;
1602                 ctrls[i] = &c[i];
1603                 i++;
1604         }
1605
1606         if ( authzid ) {
1607                 c[i].ldctl_value.bv_val = authzid;
1608                 c[i].ldctl_value.bv_len = strlen( authzid );
1609                 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1610                 c[i].ldctl_iscritical = 1;
1611                 ctrls[i] = &c[i];
1612                 i++;
1613         }
1614
1615 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
1616         /* NOTE: doesn't need an extra count because it's incompatible
1617          * with authzid */
1618         if ( proxydn ) {
1619                 BerElementBuffer berbuf;
1620                 BerElement *ber = (BerElement *)&berbuf;
1621                 
1622                 ber_init2( ber, NULL, LBER_USE_DER );
1623
1624                 if ( ber_printf( ber, "s", proxydn ) == -1 ) {
1625                         exit( EXIT_FAILURE );
1626                 }
1627
1628                 if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1629                         exit( EXIT_FAILURE );
1630                 }
1631
1632                 c[i].ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1633                 c[i].ldctl_iscritical = 1;
1634                 ctrls[i] = &c[i];
1635                 i++;
1636         }
1637 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
1638
1639         if ( manageDIT ) {
1640                 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDIT;
1641                 BER_BVZERO( &c[i].ldctl_value );
1642                 c[i].ldctl_iscritical = manageDIT > 1;
1643                 ctrls[i] = &c[i];
1644                 i++;
1645         }
1646
1647         if ( manageDSAit ) {
1648                 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1649                 BER_BVZERO( &c[i].ldctl_value );
1650                 c[i].ldctl_iscritical = manageDSAit > 1;
1651                 ctrls[i] = &c[i];
1652                 i++;
1653         }
1654
1655         if ( noop ) {
1656                 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1657                 BER_BVZERO( &c[i].ldctl_value );
1658                 c[i].ldctl_iscritical = noop > 1;
1659                 ctrls[i] = &c[i];
1660                 i++;
1661         }
1662
1663 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
1664         if ( ppolicy ) {
1665                 c[i].ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
1666                 BER_BVZERO( &c[i].ldctl_value );
1667                 c[i].ldctl_iscritical = 0;
1668                 ctrls[i] = &c[i];
1669                 i++;
1670         }
1671 #endif
1672
1673         if ( preread ) {
1674                 BerElementBuffer berbuf;
1675                 BerElement *ber = (BerElement *)&berbuf;
1676                 char **attrs = NULL;
1677
1678                 if( preread_attrs ) {
1679                         attrs = ldap_str2charray( preread_attrs, "," );
1680                 }
1681
1682                 ber_init2( ber, NULL, LBER_USE_DER );
1683
1684                 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1685                         fprintf( stderr, "preread attrs encode failed.\n" );
1686                         exit( EXIT_FAILURE );
1687                 }
1688
1689                 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1690                 if( err < 0 ) {
1691                         fprintf( stderr, "preread flatten failed (%d)\n", err );
1692                         exit( EXIT_FAILURE );
1693                 }
1694
1695                 c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
1696                 c[i].ldctl_iscritical = preread > 1;
1697                 ctrls[i] = &c[i];
1698                 i++;
1699
1700                 if( attrs ) ldap_charray_free( attrs );
1701         }
1702
1703         if ( postread ) {
1704                 BerElementBuffer berbuf;
1705                 BerElement *ber = (BerElement *)&berbuf;
1706                 char **attrs = NULL;
1707
1708                 if( postread_attrs ) {
1709                         attrs = ldap_str2charray( postread_attrs, "," );
1710                 }
1711
1712                 ber_init2( ber, NULL, LBER_USE_DER );
1713
1714                 if( ber_printf( ber, "{v}", attrs ) == -1 ) {
1715                         fprintf( stderr, "postread attrs encode failed.\n" );
1716                         exit( EXIT_FAILURE );
1717                 }
1718
1719                 err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
1720                 if( err < 0 ) {
1721                         fprintf( stderr, "postread flatten failed (%d)\n", err );
1722                         exit( EXIT_FAILURE );
1723                 }
1724
1725                 c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
1726                 c[i].ldctl_iscritical = postread > 1;
1727                 ctrls[i] = &c[i];
1728                 i++;
1729
1730                 if( attrs ) ldap_charray_free( attrs );
1731         }
1732
1733 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1734         if ( chaining ) {
1735                 if ( chainingResolve > -1 ) {
1736                         BerElementBuffer berbuf;
1737                         BerElement *ber = (BerElement *)&berbuf;
1738
1739                         ber_init2( ber, NULL, LBER_USE_DER );
1740
1741                         err = ber_printf( ber, "{e" /* } */, chainingResolve );
1742                         if ( err == -1 ) {
1743                                 ber_free( ber, 1 );
1744                                 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1745                                 exit( EXIT_FAILURE );
1746                         }
1747
1748                         if ( chainingContinuation > -1 ) {
1749                                 err = ber_printf( ber, "e", chainingContinuation );
1750                                 if ( err == -1 ) {
1751                                         ber_free( ber, 1 );
1752                                         fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1753                                         exit( EXIT_FAILURE );
1754                                 }
1755                         }
1756
1757                         err = ber_printf( ber, /* { */ "N}" );
1758                         if ( err == -1 ) {
1759                                 ber_free( ber, 1 );
1760                                 fprintf( stderr, _("Chaining behavior control encoding error!\n") );
1761                                 exit( EXIT_FAILURE );
1762                         }
1763
1764                         if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
1765                                 exit( EXIT_FAILURE );
1766                         }
1767
1768                 } else {
1769                         BER_BVZERO( &c[i].ldctl_value );
1770                 }
1771
1772                 c[i].ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1773                 c[i].ldctl_iscritical = chaining > 1;
1774                 ctrls[i] = &c[i];
1775                 i++;
1776         }
1777 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1778
1779 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
1780         if ( sessionTracking ) {
1781                 if ( stValue.bv_val == NULL && st_value( ld, &stValue ) ) {
1782                         exit( EXIT_FAILURE );
1783                 }
1784
1785                 c[i].ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1786                 c[i].ldctl_iscritical = 0;
1787                 ber_dupbv( &c[i].ldctl_value, &stValue );
1788
1789                 ctrls[i] = &c[i];
1790                 i++;
1791         }
1792 #endif /* LDAP_CONTROL_X_SESSION_TRACKING */
1793
1794         while ( count-- ) {
1795                 ctrls[i++] = extra_c++;
1796         }
1797         for ( count = 0; count < unknown_ctrls_num; count++ ) {
1798                 ctrls[i++] = &unknown_ctrls[count];
1799         }
1800         ctrls[i] = NULL;
1801
1802         err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1803
1804         if ( err != LDAP_OPT_SUCCESS ) {
1805                 for ( j = 0; j < i; j++ ) {
1806                         if ( ctrls[j]->ldctl_iscritical ) crit = 1;
1807                 }
1808                 fprintf( stderr, "Could not set %scontrols\n",
1809                         crit ? "critical " : "" );
1810         }
1811
1812         free( ctrls );
1813         if ( crit ) {
1814                 exit( EXIT_FAILURE );
1815         }
1816 }
1817
1818 int
1819 tool_check_abandon( LDAP *ld, int msgid )
1820 {
1821         int     rc;
1822
1823         switch ( gotintr ) {
1824         case Intr_Cancel:
1825                 rc = ldap_cancel_s( ld, msgid, NULL, NULL );
1826                 fprintf( stderr, "got interrupt, cancel got %d: %s\n",
1827                                 rc, ldap_err2string( rc ) );
1828                 return -1;
1829
1830         case Intr_Abandon:
1831                 rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
1832                 fprintf( stderr, "got interrupt, abandon got %d: %s\n",
1833                                 rc, ldap_err2string( rc ) );
1834                 return -1;
1835
1836         case Intr_Ignore:
1837                 /* just unbind, ignoring the request */
1838                 return -1;
1839         }
1840
1841         return 0;
1842 }
1843
1844 static int
1845 print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
1846 {
1847         BerElement      *ber;
1848         struct berval   bv;
1849
1850         tool_write_ldif( LDIF_PUT_COMMENT, "==> ",
1851                 what->bv_val, what->bv_len );
1852         ber = ber_init( &ctrl->ldctl_value );
1853         if ( ber == NULL ) {
1854                 /* error? */
1855                 return 1;
1856
1857         } else if ( ber_scanf( ber, "{m{" /*}}*/, &bv ) == LBER_ERROR ) {
1858                 /* error? */
1859                 return 1;
1860
1861         } else {
1862                 tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1863
1864                 while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
1865                         int             i;
1866                         BerVarray       vals = NULL;
1867                         char            *str = NULL;
1868
1869                         if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
1870                                 vals == NULL )
1871                         {
1872                                 /* error? */
1873                                 return 1;
1874                         }
1875
1876                         if ( ldif ) {
1877                                 char *ptr;
1878
1879                                 str = malloc( bv.bv_len + STRLENOF(": ") + 1 );
1880
1881                                 ptr = str;
1882                                 ptr = lutil_strncopy( ptr, bv.bv_val, bv.bv_len );
1883                                 ptr = lutil_strcopy( ptr, ": " );
1884                         }
1885                 
1886                         for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
1887                                 tool_write_ldif(
1888                                         ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1889                                         ldif ? str : bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
1890                         }
1891
1892                         ber_bvarray_free( vals );
1893                         if ( str ) free( str );
1894                 }
1895         }
1896
1897         if ( ber != NULL ) {
1898                 ber_free( ber, 1 );
1899         }
1900
1901         tool_write_ldif( LDIF_PUT_COMMENT, "<== ",
1902                 what->bv_val, what->bv_len );
1903
1904         return 0;
1905 }
1906
1907 static int
1908 print_preread( LDAP *ld, LDAPControl *ctrl )
1909 {
1910         static struct berval what = BER_BVC( "preread" );
1911
1912         return print_prepostread( ld, ctrl, &what );
1913 }
1914
1915 static int
1916 print_postread( LDAP *ld, LDAPControl *ctrl )
1917 {
1918         static struct berval what = BER_BVC( "postread" );
1919
1920         return print_prepostread( ld, ctrl, &what );
1921 }
1922
1923 static int
1924 print_paged_results( LDAP *ld, LDAPControl *ctrl )
1925 {
1926         ber_int_t estimate;
1927
1928         /* note: pr_cookie is being malloced; it's freed
1929          * the next time the control is sent, but the last
1930          * time it's not; we don't care too much, because
1931          * the last time an empty value is returned... */
1932         if ( ldap_parse_pageresponse_control( ld, ctrl, &estimate, &pr_cookie )
1933                 != LDAP_SUCCESS )
1934         {
1935                 /* error? */
1936                 return 1;
1937
1938         } else {
1939                 /* FIXME: check buffer overflow */
1940                 char    buf[ BUFSIZ ], *ptr = buf;
1941
1942                 if ( estimate > 0 ) {
1943                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1944                                 "estimate=%d", estimate );
1945                 }
1946
1947                 if ( pr_cookie.bv_len > 0 ) {
1948                         struct berval   bv;
1949
1950                         bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
1951                                 pr_cookie.bv_len ) + 1;
1952                         bv.bv_val = ber_memalloc( bv.bv_len + 1 );
1953
1954                         bv.bv_len = lutil_b64_ntop(
1955                                 (unsigned char *) pr_cookie.bv_val,
1956                                 pr_cookie.bv_len,
1957                                 bv.bv_val, bv.bv_len );
1958
1959                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1960                                 "%scookie=%s", ptr == buf ? "" : " ",
1961                                 bv.bv_val );
1962
1963                         ber_memfree( bv.bv_val );
1964
1965                         pr_morePagedResults = 1;
1966
1967                 } else {
1968                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
1969                                 "%scookie=", ptr == buf ? "" : " " );
1970                 }
1971
1972                 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1973                         ldif ? "pagedresults: " : "pagedresults",
1974                         buf, ptr - buf );
1975         }
1976
1977         return 0;
1978 }
1979
1980 static int
1981 print_sss( LDAP *ld, LDAPControl *ctrl )
1982 {
1983         int rc;
1984         ber_int_t err;
1985         char *attr;
1986
1987         rc = ldap_parse_sortresponse_control( ld, ctrl, &err, &attr );
1988         if ( rc == LDAP_SUCCESS ) {
1989                 char buf[ BUFSIZ ];
1990                 rc = snprintf( buf, sizeof(buf), "(%d) %s%s%s",
1991                         err, ldap_err2string(err), attr ? " " : "", attr ? attr : "" );
1992
1993                 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1994                         ldif ? "sortResult: " : "sortResult", buf, rc );
1995         }
1996
1997         return rc;
1998 }
1999
2000 static int
2001 print_vlv( LDAP *ld, LDAPControl *ctrl )
2002 {
2003         int rc;
2004         ber_int_t err;
2005         struct berval bv;
2006
2007         rc = ldap_parse_vlvresponse_control( ld, ctrl, &vlvPos, &vlvCount,
2008                 &vlvContext, &err );
2009         if ( rc == LDAP_SUCCESS ) {
2010                 char buf[ BUFSIZ ];
2011
2012                 if ( vlvContext && vlvContext->bv_len > 0 ) {
2013                         bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
2014                                 vlvContext->bv_len ) + 1;
2015                         bv.bv_val = ber_memalloc( bv.bv_len + 1 );
2016
2017                         bv.bv_len = lutil_b64_ntop(
2018                                 (unsigned char *) vlvContext->bv_val,
2019                                 vlvContext->bv_len,
2020                                 bv.bv_val, bv.bv_len );
2021                 } else {
2022                         bv.bv_val = "";
2023                         bv.bv_len = 0;
2024                 }
2025
2026                 rc = snprintf( buf, sizeof(buf), "pos=%d count=%d context=%s (%d) %s",
2027                         vlvPos, vlvCount, bv.bv_val,
2028                         err, ldap_err2string(err));
2029
2030                 if ( bv.bv_len )
2031                         ber_memfree( bv.bv_val );
2032
2033                 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2034                         ldif ? "vlvResult" : "vlvResult", buf, rc );
2035         }
2036
2037         return rc;
2038 }
2039
2040 #ifdef LDAP_CONTROL_X_DEREF
2041 static int
2042 print_deref( LDAP *ld, LDAPControl *ctrl )
2043 {
2044         LDAPDerefRes    *drhead = NULL, *dr;
2045         int             rc;
2046
2047         rc = ldap_parse_derefresponse_control( ld, ctrl, &drhead );
2048         if ( rc != LDAP_SUCCESS ) {
2049                 return rc;
2050         }
2051
2052         for ( dr = drhead; dr != NULL; dr = dr->next ) {
2053                 LDAPDerefVal    *dv;
2054                 ber_len_t       len;
2055                 char            *buf, *ptr;
2056
2057                 len = strlen( dr->derefAttr ) + STRLENOF(": ");
2058
2059                 for ( dv = dr->attrVals; dv != NULL; dv = dv->next ) {
2060                         if ( dv->vals != NULL ) {
2061                                 int j;
2062                                 ber_len_t tlen = strlen(dv->type);
2063
2064                                 for ( j = 0; dv->vals[ j ].bv_val != NULL; j++ ) {
2065                                         len += STRLENOF("<:=>;") + tlen + 4*((dv->vals[ j ].bv_len - 1)/3 + 1);
2066                                 }
2067                         }
2068                 }
2069                 len += dr->derefVal.bv_len + STRLENOF("\n");
2070                 buf = ldap_memalloc( len + 1 );
2071                 if ( buf == NULL ) {
2072                         rc = LDAP_NO_MEMORY;
2073                         goto done;
2074                 }
2075
2076                 ptr = buf;
2077                 ptr = lutil_strcopy( ptr, dr->derefAttr );
2078                 *ptr++ = ':';
2079                 *ptr++ = ' ';
2080                 for ( dv = dr->attrVals; dv != NULL; dv = dv->next ) {
2081                         if ( dv->vals != NULL ) {
2082                                 int j;
2083                                 for ( j = 0; dv->vals[ j ].bv_val != NULL; j++ ) {
2084                                         int k = ldif_is_not_printable( dv->vals[ j ].bv_val, dv->vals[ j ].bv_len );
2085
2086                                         *ptr++ = '<';
2087                                         ptr = lutil_strcopy( ptr, dv->type );
2088                                         if ( k ) {
2089                                                 *ptr++ = ':';
2090                                         }
2091                                         *ptr++ = '=';
2092                                         if ( k ) {
2093                                                 k = lutil_b64_ntop(
2094                                                         (unsigned char *) dv->vals[ j ].bv_val,
2095                                                         dv->vals[ j ].bv_len,
2096                                                         ptr, buf + len - ptr );
2097                                                 assert( k >= 0 );
2098                                                 ptr += k;
2099                                                 
2100                                         } else {
2101                                                 ptr = lutil_memcopy( ptr, dv->vals[ j ].bv_val, dv->vals[ j ].bv_len );
2102                                         }
2103                                         *ptr++ = '>';
2104                                         *ptr++ = ';';
2105                                 }
2106                         }
2107                 }
2108                 ptr = lutil_strncopy( ptr, dr->derefVal.bv_val, dr->derefVal.bv_len );
2109                 *ptr++ = '\n';
2110                 *ptr = '\0';
2111                 assert( ptr <= buf + len );
2112
2113                 tool_write_ldif( LDIF_PUT_COMMENT, NULL, buf, ptr - buf);
2114
2115                 ldap_memfree( buf );
2116         }
2117
2118         rc = LDAP_SUCCESS;
2119
2120 done:;
2121         ldap_derefresponse_free( drhead );
2122
2123         return rc;
2124 }
2125 #endif
2126
2127 #ifdef LDAP_CONTROL_X_WHATFAILED
2128 static int
2129 print_whatfailed( LDAP *ld, LDAPControl *ctrl )
2130 {
2131         BerElement *ber;
2132         ber_tag_t tag;
2133         ber_len_t siz;
2134         BerVarray bva = NULL;
2135
2136         /* Create a BerElement from the berval returned in the control. */
2137         ber = ber_init( &ctrl->ldctl_value );
2138
2139         if ( ber == NULL ) {
2140                 return LDAP_NO_MEMORY;
2141         }
2142
2143         siz = sizeof(struct berval);
2144         tag = ber_scanf( ber, "[M]", &bva, &siz, 0 );
2145         if ( tag != LBER_ERROR ) {
2146                 int i;
2147
2148                 tool_write_ldif( LDIF_PUT_COMMENT, " what failed:", NULL, 0 );
2149
2150                 for ( i = 0; bva[i].bv_val != NULL; i++ ) {
2151                         tool_write_ldif( LDIF_PUT_COMMENT, NULL, bva[i].bv_val, bva[i].bv_len );
2152                 }
2153
2154                 ldap_memfree( bva );
2155         }
2156
2157         ber_free( ber, 1 );
2158
2159
2160         return 0;
2161 }
2162 #endif
2163
2164 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
2165 static int
2166 print_ppolicy( LDAP *ld, LDAPControl *ctrl )
2167 {
2168         int expire = 0, grace = 0, rc;
2169         LDAPPasswordPolicyError pperr;
2170
2171         rc = ldap_parse_passwordpolicy_control( ld, ctrl,
2172                 &expire, &grace, &pperr );
2173         if ( rc == LDAP_SUCCESS ) {
2174                 char    buf[ BUFSIZ ], *ptr = buf;
2175
2176                 if ( expire != -1 ) {
2177                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2178                                 "expire=%d", expire );
2179                 }
2180
2181                 if ( grace != -1 ) {
2182                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2183                                 "%sgrace=%d", ptr == buf ? "" : " ", grace );
2184                 }
2185
2186                 if ( pperr != PP_noError ) {
2187                         ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
2188                                 "%serror=%d (%s)", ptr == buf ? "" : " ",
2189                                 pperr,
2190                                 ldap_passwordpolicy_err2txt( pperr ) );
2191                 }
2192
2193                 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2194                         ldif ? "ppolicy: " : "ppolicy", buf, ptr - buf );
2195         }
2196
2197         return rc;
2198 }
2199 #endif
2200
2201 void tool_print_ctrls(
2202         LDAP            *ld,
2203         LDAPControl     **ctrls )
2204 {
2205         int     i;
2206         char    *ptr;
2207
2208         for ( i = 0; ctrls[i] != NULL; i++ ) {
2209                 /* control: OID criticality base64value */
2210                 struct berval b64 = BER_BVNULL;
2211                 ber_len_t len;
2212                 char *str;
2213                 int j;
2214
2215                 /* FIXME: there might be cases where a control has NULL OID;
2216                  * this makes little sense, especially when returned by the
2217                  * server, but libldap happily allows it */
2218                 if ( ctrls[i]->ldctl_oid == NULL ) {
2219                         continue;
2220                 }
2221
2222                 len = ldif ? 2 : 0;
2223                 len += strlen( ctrls[i]->ldctl_oid );
2224
2225                 /* add enough for space after OID and the critical value itself */
2226                 len += ctrls[i]->ldctl_iscritical
2227                         ? sizeof("true") : sizeof("false");
2228
2229                 /* convert to base64 */
2230                 if ( !BER_BVISNULL( &ctrls[i]->ldctl_value ) ) {
2231                         b64.bv_len = LUTIL_BASE64_ENCODE_LEN(
2232                                 ctrls[i]->ldctl_value.bv_len ) + 1;
2233                         b64.bv_val = ber_memalloc( b64.bv_len + 1 );
2234
2235                         b64.bv_len = lutil_b64_ntop(
2236                                 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
2237                                 ctrls[i]->ldctl_value.bv_len,
2238                                 b64.bv_val, b64.bv_len );
2239                 }
2240
2241                 if ( b64.bv_len ) {
2242                         len += 1 + b64.bv_len;
2243                 }
2244
2245                 ptr = str = malloc( len + 1 );
2246                 if ( ldif ) {
2247                         ptr = lutil_strcopy( ptr, ": " );
2248                 }
2249                 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_oid );
2250                 ptr = lutil_strcopy( ptr, ctrls[i]->ldctl_iscritical
2251                         ? " true" : " false" );
2252
2253                 if ( b64.bv_len ) {
2254                         ptr = lutil_strcopy( ptr, " " );
2255                         ptr = lutil_strcopy( ptr, b64.bv_val );
2256                 }
2257
2258                 if ( ldif < 2 ) {
2259                         tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
2260                                 "control", str, len );
2261                 }
2262
2263                 free( str );
2264                 if ( b64.bv_len ) {
2265                         ber_memfree( b64.bv_val );
2266                 }
2267
2268                 /* known controls */
2269                 for ( j = 0; tool_ctrl_response[j].oid != NULL; j++ ) {
2270                         if ( strcmp( tool_ctrl_response[j].oid, ctrls[i]->ldctl_oid ) == 0 ) {
2271                                 if ( !tool_ctrl_response[j].mask & tool_type ) {
2272                                         /* this control should not appear
2273                                          * with this tool; warning? */
2274                                 }
2275                                 break;
2276                         }
2277                 }
2278
2279                 if ( tool_ctrl_response[j].oid != NULL && tool_ctrl_response[j].func ) {
2280                         (void)tool_ctrl_response[j].func( ld, ctrls[i] );
2281                 }
2282         }
2283 }
2284
2285 int
2286 tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
2287 {
2288         char    *ldif;
2289
2290         if (( ldif = ldif_put_wrap( type, name, value, vallen, ldif_wrap )) == NULL ) {
2291                 return( -1 );
2292         }
2293
2294         fputs( ldif, stdout );
2295         ber_memfree( ldif );
2296
2297         return( 0 );
2298 }
2299
2300 int
2301 tool_is_oid( const char *s )
2302 {
2303         int             first = 1;
2304
2305         if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2306                 return 0;
2307         }
2308
2309         for ( ; s[ 0 ]; s++ ) {
2310                 if ( s[ 0 ] == '.' ) {
2311                         if ( s[ 1 ] == '\0' ) {
2312                                 return 0;
2313                         }
2314                         first = 1;
2315                         continue;
2316                 }
2317
2318                 if ( !isdigit( (unsigned char) s[ 0 ] ) ) {
2319                         return 0;
2320                 }
2321
2322                 if ( first == 1 && s[ 0 ] == '0' && s[ 1 ] != '.' ) {
2323                         return 0;
2324                 }
2325                 first = 0;
2326         }
2327
2328         return 1;
2329 }
2330