3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 /* common.c - common routines for the ldap client tools */
12 #include <ac/stdlib.h>
13 #include <ac/signal.h>
14 #include <ac/string.h>
15 #include <ac/unistd.h>
20 #include "lutil_ldap.h"
31 char *ldaphost = NULL;
33 #ifdef HAVE_CYRUS_SASL
34 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
35 char *sasl_realm = NULL;
36 char *sasl_authc_id = NULL;
37 char *sasl_authz_id = NULL;
38 char *sasl_mech = NULL;
39 char *sasl_secprops = NULL;
49 struct berval passwd = { 0, NULL };
62 setlocale(LC_MESSAGES,"");
63 bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
64 textdomain(OPENLDAP_PACKAGE);
68 tool_common_usage( void )
70 static const char *const descriptions[] = {
71 N_(" -c continuous operation mode (do not stop on errors)\n"),
72 N_(" -C chase referrals\n"),
73 N_(" -d level set LDAP debugging level to `level'\n"),
74 N_(" -D binddn bind DN\n"),
75 N_(" -e [!]<ctrl>[=<ctrlparam>] general controls (! indicates criticality)\n")
76 N_(" [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n")
77 N_(" [!]manageDSAit (alternate form, see -M)\n")
79 N_(" -f file read operations from `file'\n"),
80 N_(" -h host LDAP server\n"),
81 N_(" -H URI LDAP Uniform Resource Indentifier(s)\n"),
82 N_(" -I use SASL Interactive mode\n"),
83 N_(" -k use Kerberos authentication\n"),
84 N_(" -K like -k, but do only step 1 of the Kerberos bind\n"),
85 N_(" -M enable Manage DSA IT control (-MM to make critical)\n"),
86 N_(" -n show what would be done but don't actually do it\n"),
87 N_(" -O props SASL security properties\n"),
88 N_(" -p port port on LDAP server\n"),
89 N_(" -P version procotol version (default: 3)\n"),
90 N_(" -Q use SASL Quiet mode\n"),
91 N_(" -R realm SASL realm\n"),
92 N_(" -U authcid SASL authentication identity\n"),
93 N_(" -v run in verbose mode (diagnostics to standard output)\n"),
94 N_(" -V print version info (-VV only)\n"),
95 N_(" -w passwd bind password (for simple authentication)\n"),
96 N_(" -W prompt for bind password\n"),
97 N_(" -x Simple authentication\n"),
98 N_(" -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"),
99 N_(" -y file Read password from file\n"),
100 N_(" -Y mech SASL mechanism\n"),
101 N_(" -Z Start TLS request (-ZZ to require successful response)\n"),
104 const char *const *cpp;
106 fputs( _("Common options:\n"), stderr );
107 for( cpp = descriptions; *cpp != NULL; cpp++ ) {
108 if( strchr( options, (*cpp)[3] ) ) {
109 fputs( _(*cpp), stderr );
116 tool_args( int argc, char **argv )
119 while (( i = getopt( argc, argv, options )) != EOF )
122 char *control, *cvalue;
124 case 'c': /* continuous operation mode */
131 debug |= atoi( optarg );
133 case 'D': /* bind DN */
134 if( binddn != NULL ) {
135 fprintf( stderr, "%s: -D previously specified\n", prog );
136 exit( EXIT_FAILURE );
138 binddn = ber_strdup( optarg );
140 case 'e': /* general controls */
141 /* should be extended to support comma separated list of
142 * [!]key[=value] parameters, e.g. -e !foo,bar=567
147 if( optarg[0] == '!' ) {
152 control = ber_strdup( optarg );
153 if ( (cvalue = strchr( control, '=' )) != NULL ) {
157 if ( strcasecmp( control, "authzid" ) == 0 ) {
158 if( authzid != NULL ) {
159 fprintf( stderr, "authzid control previously specified\n");
160 exit( EXIT_FAILURE );
162 if( cvalue == NULL ) {
163 fprintf( stderr, "authzid: control value expected\n" );
167 fprintf( stderr, "authzid: must be marked critical\n" );
171 assert( authzid == NULL );
174 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
177 "manageDSAit control previously specified\n");
178 exit( EXIT_FAILURE );
180 if( cvalue != NULL ) {
182 "manageDSAit: no control value expected\n" );
186 manageDSAit = 1 + crit;
188 } else if ( strcasecmp( control, "noop" ) == 0 ) {
190 fprintf( stderr, "noop control previously specified\n");
191 exit( EXIT_FAILURE );
193 if( cvalue != NULL ) {
194 fprintf( stderr, "noop: no control value expected\n" );
201 fprintf( stderr, "Invalid general control name: %s\n",
206 case 'f': /* read from file */
207 if( infile != NULL ) {
208 fprintf( stderr, "%s: -f previously specified\n", prog );
209 exit( EXIT_FAILURE );
211 infile = ber_strdup( optarg );
213 case 'h': /* ldap host */
214 if( ldaphost != NULL ) {
215 fprintf( stderr, "%s: -h previously specified\n", prog );
216 exit( EXIT_FAILURE );
218 ldaphost = ber_strdup( optarg );
220 case 'H': /* ldap URI */
221 if( ldapuri != NULL ) {
222 fprintf( stderr, "%s: -H previously specified\n", prog );
223 exit( EXIT_FAILURE );
225 ldapuri = ber_strdup( optarg );
228 #ifdef HAVE_CYRUS_SASL
229 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
230 fprintf( stderr, "%s: incompatible previous "
231 "authentication choice\n",
233 exit( EXIT_FAILURE );
235 authmethod = LDAP_AUTH_SASL;
236 sasl_flags = LDAP_SASL_INTERACTIVE;
239 fprintf( stderr, "%s: was not compiled with SASL support\n",
241 exit( EXIT_FAILURE );
243 case 'k': /* kerberos bind */
244 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
245 if( authmethod != -1 ) {
246 fprintf( stderr, "%s: -k incompatible with previous "
247 "authentication choice\n", prog );
248 exit( EXIT_FAILURE );
250 authmethod = LDAP_AUTH_KRBV4;
252 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
253 exit( EXIT_FAILURE );
256 case 'K': /* kerberos bind, part one only */
257 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
258 if( authmethod != -1 ) {
259 fprintf( stderr, "%s: incompatible with previous "
260 "authentication choice\n", prog );
261 exit( EXIT_FAILURE );
263 authmethod = LDAP_AUTH_KRBV41;
265 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
266 exit( EXIT_FAILURE );
270 /* enable Manage DSA IT */
273 case 'n': /* print operations, don't actually do them */
277 #ifdef HAVE_CYRUS_SASL
278 if( sasl_secprops != NULL ) {
279 fprintf( stderr, "%s: -O previously specified\n", prog );
280 exit( EXIT_FAILURE );
282 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
283 fprintf( stderr, "%s: incompatible previous "
284 "authentication choice\n", prog );
285 exit( EXIT_FAILURE );
287 authmethod = LDAP_AUTH_SASL;
288 sasl_secprops = ber_strdup( optarg );
290 fprintf( stderr, "%s: not compiled with SASL support\n",
292 exit( EXIT_FAILURE );
297 fprintf( stderr, "%s: -p previously specified\n", prog );
298 exit( EXIT_FAILURE );
300 ldapport = atoi( optarg );
303 switch( atoi(optarg) ) {
305 if( protocol == LDAP_VERSION3 ) {
306 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
308 exit( EXIT_FAILURE );
310 protocol = LDAP_VERSION2;
313 if( protocol == LDAP_VERSION2 ) {
314 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
316 exit( EXIT_FAILURE );
318 protocol = LDAP_VERSION3;
321 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
327 #ifdef HAVE_CYRUS_SASL
328 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
329 fprintf( stderr, "%s: incompatible previous "
330 "authentication choice\n",
332 exit( EXIT_FAILURE );
334 authmethod = LDAP_AUTH_SASL;
335 sasl_flags = LDAP_SASL_QUIET;
338 fprintf( stderr, "%s: not compiled with SASL support\n",
340 exit( EXIT_FAILURE );
343 #ifdef HAVE_CYRUS_SASL
344 if( sasl_realm != NULL ) {
345 fprintf( stderr, "%s: -R previously specified\n", prog );
346 exit( EXIT_FAILURE );
348 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
349 fprintf( stderr, "%s: incompatible previous "
350 "authentication choice\n",
352 exit( EXIT_FAILURE );
354 authmethod = LDAP_AUTH_SASL;
355 sasl_realm = ber_strdup( optarg );
357 fprintf( stderr, "%s: not compiled with SASL support\n",
359 exit( EXIT_FAILURE );
363 #ifdef HAVE_CYRUS_SASL
364 if( sasl_authc_id != NULL ) {
365 fprintf( stderr, "%s: -U previously specified\n", prog );
366 exit( EXIT_FAILURE );
368 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
369 fprintf( stderr, "%s: incompatible previous "
370 "authentication choice\n",
372 exit( EXIT_FAILURE );
374 authmethod = LDAP_AUTH_SASL;
375 sasl_authc_id = ber_strdup( optarg );
377 fprintf( stderr, "%s: not compiled with SASL support\n",
379 exit( EXIT_FAILURE );
382 case 'v': /* verbose mode */
385 case 'V': /* version */
388 case 'w': /* password */
389 passwd.bv_val = ber_strdup( optarg );
393 for( p = optarg; *p != '\0'; p++ ) {
397 passwd.bv_len = strlen( passwd.bv_val );
406 #ifdef HAVE_CYRUS_SASL
407 if( sasl_mech != NULL ) {
408 fprintf( stderr, "%s: -Y previously specified\n", prog );
409 exit( EXIT_FAILURE );
411 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
412 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
413 exit( EXIT_FAILURE );
415 authmethod = LDAP_AUTH_SASL;
416 sasl_mech = ber_strdup( optarg );
418 fprintf( stderr, "%s: not compiled with SASL support\n",
420 exit( EXIT_FAILURE );
424 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
425 fprintf( stderr, "%s: incompatible with previous "
426 "authentication choice\n", prog );
427 exit( EXIT_FAILURE );
429 authmethod = LDAP_AUTH_SIMPLE;
432 #ifdef HAVE_CYRUS_SASL
433 if( sasl_authz_id != NULL ) {
434 fprintf( stderr, "%s: -X previously specified\n", prog );
435 exit( EXIT_FAILURE );
437 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
438 fprintf( stderr, "%s: -X incompatible with "
439 "authentication choice\n", prog );
440 exit( EXIT_FAILURE );
442 authmethod = LDAP_AUTH_SASL;
443 sasl_authz_id = ber_strdup( optarg );
445 fprintf( stderr, "%s: not compiled with SASL support\n", prog );
446 exit( EXIT_FAILURE );
453 fprintf( stderr, "%s: not compiled with TLS support\n", prog );
454 exit( EXIT_FAILURE );
458 if( handle_private_option( i ) )
460 fprintf( stderr, "%s: unrecognized option -%c\n",
467 /* prevent bad linking */
469 api.ldapai_info_version = LDAP_API_INFO_VERSION;
471 if ( ldap_get_option(NULL, LDAP_OPT_API_INFO, &api)
472 != LDAP_OPT_SUCCESS )
474 fprintf( stderr, "%s: ldap_get_option(API_INFO) failed\n", prog );
475 exit( EXIT_FAILURE );
478 if (api.ldapai_info_version != LDAP_API_INFO_VERSION) {
479 fprintf( stderr, "LDAP APIInfo version mismatch: "
480 "got %d, expected %d\n",
481 api.ldapai_info_version, LDAP_API_INFO_VERSION );
482 exit( EXIT_FAILURE );
485 if( api.ldapai_api_version != LDAP_API_VERSION ) {
486 fprintf( stderr, "LDAP API version mismatch: "
487 "got %d, expected %d\n",
488 api.ldapai_api_version, LDAP_API_VERSION );
489 exit( EXIT_FAILURE );
492 if( strcmp(api.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {
493 fprintf( stderr, "LDAP vendor name mismatch: "
494 "got %s, expected %s\n",
495 api.ldapai_vendor_name, LDAP_VENDOR_NAME );
496 exit( EXIT_FAILURE );
499 if( api.ldapai_vendor_version != LDAP_VENDOR_VERSION ) {
500 fprintf( stderr, "LDAP vendor version mismatch: "
501 "got %d, expected %d\n",
502 api.ldapai_vendor_version, LDAP_VENDOR_VERSION );
503 exit( EXIT_FAILURE );
507 fprintf( stderr, "%s: %s\t(LDAP library: %s %d)\n",
509 LDAP_VENDOR_NAME, LDAP_VENDOR_VERSION );
510 if (version > 1) exit( EXIT_SUCCESS );
515 protocol = LDAP_VERSION3;
517 if (authmethod == -1 && protocol > LDAP_VERSION2) {
518 #ifdef HAVE_CYRUS_SASL
519 authmethod = LDAP_AUTH_SASL;
521 authmethod = LDAP_AUTH_SIMPLE;
525 if( ldapuri != NULL ) {
526 if( ldaphost != NULL ) {
527 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
528 exit( EXIT_FAILURE );
531 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
532 exit( EXIT_FAILURE );
535 if( protocol == LDAP_VERSION2 ) {
536 if( authzid || manageDSAit || noop ) {
537 fprintf( stderr, "%s: -e/-M incompatible with LDAPv2\n", prog );
538 exit( EXIT_FAILURE );
542 fprintf( stderr, "%s: -Z incompatible with LDAPv2\n", prog );
543 exit( EXIT_FAILURE );
546 #ifdef HAVE_CYRUS_SASL
547 if( authmethod == LDAP_AUTH_SASL ) {
548 fprintf( stderr, "%s: -[IOQRUXY] incompatible with LDAPv2\n",
550 exit( EXIT_FAILURE );
554 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
555 if ( authmethod == LDAP_AUTH_KRBV4 || authmethod == LDAP_AUTH_KRBV41 ) {
556 fprintf( stderr, "%s: -k/-K incompatible with LDAPv%d\n",
558 exit( EXIT_FAILURE );
566 tool_conn_setup( int not, void (*private_setup)( LDAP * ) )
571 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
572 != LBER_OPT_SUCCESS ) {
573 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
575 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
576 != LDAP_OPT_SUCCESS ) {
577 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
582 (void) SIGNAL( SIGPIPE, SIG_IGN );
586 /* connect to server */
587 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
589 fprintf( stderr, "ldap_init( %s, %d )\n",
590 ldaphost != NULL ? ldaphost : "<DEFAULT>",
594 ld = ldap_init( ldaphost, ldapport );
596 char buf[20 + sizeof(": ldap_init")];
597 sprintf( buf, "%.20s: ldap_init", prog );
599 exit( EXIT_FAILURE );
605 fprintf( stderr, "ldap_initialize( %s )\n",
606 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
608 rc = ldap_initialize( &ld, ldapuri );
609 if( rc != LDAP_SUCCESS ) {
610 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
611 rc, ldap_err2string(rc) );
612 exit( EXIT_FAILURE );
620 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
621 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF )
622 != LDAP_OPT_SUCCESS )
624 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
625 referrals ? "on" : "off" );
626 exit( EXIT_FAILURE );
629 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
630 != LDAP_OPT_SUCCESS )
632 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
634 exit( EXIT_FAILURE );
638 ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
639 ldap_perror( ld, "ldap_start_tls" );
641 exit( EXIT_FAILURE );
651 tool_bind( LDAP *ld )
653 if ( authmethod == LDAP_AUTH_SASL ) {
654 #ifdef HAVE_CYRUS_SASL
658 if( sasl_secprops != NULL ) {
659 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
660 (void *) sasl_secprops );
662 if( rc != LDAP_OPT_SUCCESS ) {
664 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
666 exit( EXIT_FAILURE );
670 defaults = lutil_sasl_defaults( ld,
677 rc = ldap_sasl_interactive_bind_s( ld, binddn,
678 sasl_mech, NULL, NULL,
679 sasl_flags, lutil_sasl_interact, defaults );
681 lutil_sasl_freedefs( defaults );
682 if( rc != LDAP_SUCCESS ) {
683 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
684 exit( EXIT_FAILURE );
687 fprintf( stderr, "%s: not compiled with SASL support\n",
689 exit( EXIT_FAILURE );
692 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
694 ldap_perror( ld, "ldap_bind" );
695 exit( EXIT_FAILURE );
701 /* Set server controls. Add controls extra_c[0..count-1], if set. */
703 tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
705 int i = 0, j, crit = 0, err;
706 LDAPControl c[3], **ctrls;
708 ctrls = (LDAPControl**) malloc(sizeof(c) + (count+1)*sizeof(LDAPControl*));
709 if ( ctrls == NULL ) {
710 fprintf( stderr, "No memory\n" );
711 exit( EXIT_FAILURE );
715 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
716 c[i].ldctl_value.bv_val = authzid;
717 c[i].ldctl_value.bv_len = strlen( authzid );
718 c[i].ldctl_iscritical = 1;
724 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
725 c[i].ldctl_value.bv_val = NULL;
726 c[i].ldctl_value.bv_len = 0;
727 c[i].ldctl_iscritical = manageDSAit > 1;
733 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
734 c[i].ldctl_value.bv_val = NULL;
735 c[i].ldctl_value.bv_len = 0;
736 c[i].ldctl_iscritical = noop > 1;
742 ctrls[i++] = extra_c++;
745 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
747 if ( err != LDAP_OPT_SUCCESS ) {
748 for ( j = 0; j < i; j++ )
749 if ( ctrls[j]->ldctl_iscritical )
751 fprintf( stderr, "Could not set %scontrols\n",
752 crit ? "critical " : "" );
757 exit( EXIT_FAILURE );