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