3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/stdlib.h>
14 #include <ac/signal.h>
15 #include <ac/string.h>
16 #include <ac/unistd.h>
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
33 #include "ldap_defaults.h"
38 usage( const char *s )
41 "usage: %s [options] filter [attributes...]\nwhere:\n"
42 " filter\tRFC-1558 compliant LDAP search filter\n"
43 " attributes\twhitespace-separated list of attributes to retrieve\n"
44 "\t\t1.1 -- no attributes\n"
45 "\t\t* -- all user attributes\n"
46 "\t\t+ -- all operational attributes\n"
47 "\t\tempty list -- all non-operational attributes\n"
49 " -a deref\tone of `never', `always', `search', or `find' (alias\n"
51 " -A\t\tretrieve attribute names only (no values)\n"
52 " -b basedn\tbase dn for search\n"
53 " -B\t\tdo not suppress printing of binary values\n"
54 " -d level\tset LDAP debugging level to `level'\n"
55 " -D binddn\tbind DN\n"
56 " -E\t\trequest SASL privacy (-EE to make it critical)\n"
57 " -f file\t\tperform sequence of searches listed in `file'\n"
58 " -F sep\t\tprint `sep' instead of `=' between attribute names and\n"
60 " -h host\t\tLDAP server\n"
61 " -I\t\trequest SASL integrity checking (-II to make it\n"
63 " -k\t\tuse Kerberos authentication\n"
64 " -K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
65 " -l limit\ttime limit (in seconds) for search\n"
66 " -L\t\tprint entries in LDIF format (implies -B)\n"
67 " -LL\t\tprint entries in LDIF format without comments\n"
68 " -LLL\t\tprint entries in LDIF format without comments and\n"
70 " -M\t\tenable Manage DSA IT control (-MM to make critical)\n"
71 " -n\t\tshow what would be done but don't actually search\n"
72 " -p port\t\tport on LDAP server\n"
73 " -P version\tprocotol version (2 or 3)\n"
74 " -R\t\tdo not automatically follow referrals\n"
75 " -s scope\tone of base, one, or sub (search scope)\n"
76 " -S attr\t\tsort the results by attribute `attr'\n"
77 " -t\t\twrite binary values to files in TMPDIR\n"
78 " -tt\t\twrite all values to files in TMPDIR\n"
79 " -T path\t\twrite files to directory specified by path (default:\n"
81 " -u\t\tinclude User Friendly entry names in the output\n"
82 " -U user\t\tSASL authentication identity (username)\n"
83 " -v\t\trun in verbose mode (diagnostics to standard output)\n"
84 " -V prefix\tURL prefix for files (default: \"file://tmp/\")\n"
85 " -w passwd\tbind passwd (for simple authentication)\n"
86 " -W\t\tprompt for bind passwd\n"
87 " -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
88 " -Y mech\t\tSASL mechanism\n"
89 " -z limit\tsize limit (in entries) for search\n"
90 " -Z\t\trequest the use of TLS (-ZZ to make it critical)\n"
96 static void print_entry LDAP_P((
101 static int write_ldif LDAP_P((
107 static int dosearch LDAP_P((
116 #define TMPDIR "/tmp"
117 #define URLPRE "file:/tmp/"
119 static char *tmpdir = NULL;
120 static char *urlpre = NULL;
122 static char *binddn = NULL;
123 static struct berval passwd = { 0, NULL };
124 static char *base = NULL;
125 static char *ldaphost = NULL;
126 static int ldapport = 0;
127 #ifdef HAVE_CYRUS_SASL
128 static char *sasl_authc_id = NULL;
129 static char *sasl_authz_id = NULL;
130 static char *sasl_mech = NULL;
131 static int sasl_integrity = 0;
132 static int sasl_privacy = 0;
134 static int use_tls = 0;
135 static char *sep = DEFSEP;
136 static char *sortattr = NULL;
137 static int skipsortattr = 0;
138 static int verbose, not, includeufn, binary, vals2tmp, ldif;
141 main( int argc, char **argv )
143 char *infile, *filtpattern, **attrs, line[ BUFSIZ ];
145 int rc, i, first, scope, deref, attrsonly, manageDSAit;
146 int referrals, timelimit, sizelimit, debug;
147 int authmethod, version, want_bindpw;
151 debug = verbose = binary = not = vals2tmp =
152 attrsonly = manageDSAit = ldif = want_bindpw = 0;
154 deref = sizelimit = timelimit = version = -1;
156 /* default should be off */
159 scope = LDAP_SCOPE_SUBTREE;
160 authmethod = LDAP_AUTH_SIMPLE;
162 while (( i = getopt( argc, argv,
163 "Aa:Bb:D:d:EF:f:h:IKkLl:MnP:p:RS:s:T:tU:uV:vWw:X:Y:Zz:")) != EOF )
166 case 'n': /* do Not do any searches */
169 case 'v': /* verbose mode */
173 debug |= atoi( optarg );
175 case 'k': /* use kerberos bind */
176 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
177 authmethod = LDAP_AUTH_KRBV4;
179 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
180 return( EXIT_FAILURE );
183 case 'K': /* use kerberos bind, 1st part only */
184 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
185 authmethod = LDAP_AUTH_KRBV41;
187 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
188 return( EXIT_FAILURE );
192 case 'u': /* include UFN */
195 case 't': /* write attribute values to /tmp files */
199 /* enable Manage DSA IT */
202 case 'R': /* don't automatically chase referrals */
205 case 'A': /* retrieve attribute names only -- no values */
208 case 'L': /* print entries in LDIF format */
210 /* fall through -- always allow binary when outputting LDIF */
211 case 'B': /* allow binary values to be printed */
214 case 's': /* search scope */
215 if ( strcasecmp( optarg, "base" ) == 0 ) {
216 scope = LDAP_SCOPE_BASE;
217 } else if ( strcasecmp( optarg, "one" ) == 0 ) {
218 scope = LDAP_SCOPE_ONELEVEL;
219 } else if ( strcasecmp( optarg, "sub" ) == 0 ) {
220 scope = LDAP_SCOPE_SUBTREE;
222 fprintf( stderr, "scope should be base, one, or sub\n" );
227 case 'a': /* set alias deref option */
228 if ( strcasecmp( optarg, "never" ) == 0 ) {
229 deref = LDAP_DEREF_NEVER;
230 } else if ( strcasecmp( optarg, "search" ) == 0 ) {
231 deref = LDAP_DEREF_SEARCHING;
232 } else if ( strcasecmp( optarg, "find" ) == 0 ) {
233 deref = LDAP_DEREF_FINDING;
234 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
235 deref = LDAP_DEREF_ALWAYS;
237 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
242 case 'T': /* field separator */
243 if( tmpdir ) free( tmpdir );
244 tmpdir = strdup( optarg );
246 case 'V': /* field separator */
247 if( urlpre ) free( urlpre );
248 urlpre = strdup( optarg );
250 case 'F': /* field separator */
251 sep = strdup( optarg );
253 case 'f': /* input file */
254 infile = strdup( optarg );
256 case 'h': /* ldap host */
257 ldaphost = strdup( optarg );
259 case 'b': /* searchbase */
260 base = strdup( optarg );
262 case 'D': /* bind DN */
263 binddn = strdup( optarg );
265 case 'p': /* ldap port */
266 ldapport = atoi( optarg );
268 case 'w': /* bind password */
269 passwd.bv_val = strdup( optarg );
273 for( p = optarg; *p == '\0'; p++ ) {
277 passwd.bv_len = strlen( passwd.bv_val );
279 case 'l': /* time limit */
280 timelimit = atoi( optarg );
282 case 'z': /* size limit */
283 sizelimit = atoi( optarg );
285 case 'S': /* sort attribute */
286 sortattr = strdup( optarg );
292 switch( atoi( optarg ) )
295 version = LDAP_VERSION2;
298 version = LDAP_VERSION3;
301 fprintf( stderr, "protocol version should be 2 or 3\n" );
306 #ifdef HAVE_CYRUS_SASL
308 authmethod = LDAP_AUTH_SASL;
310 fprintf( stderr, "%s was not compiled with SASL support\n",
312 return( EXIT_FAILURE );
316 #ifdef HAVE_CYRUS_SASL
318 authmethod = LDAP_AUTH_SASL;
320 fprintf( stderr, "%s was not compiled with SASL support\n",
322 return( EXIT_FAILURE );
326 #ifdef HAVE_CYRUS_SASL
327 if ( strcasecmp( optarg, "any" ) && strcmp( optarg, "*" ) ) {
328 sasl_mech = strdup( optarg );
330 authmethod = LDAP_AUTH_SASL;
332 fprintf( stderr, "%s was not compiled with SASL support\n",
334 return( EXIT_FAILURE );
338 #ifdef HAVE_CYRUS_SASL
339 sasl_authc_id = strdup( optarg );
340 authmethod = LDAP_AUTH_SASL;
342 fprintf( stderr, "%s was not compiled with SASL support\n",
344 return( EXIT_FAILURE );
348 #ifdef HAVE_CYRUS_SASL
349 sasl_authz_id = strdup( optarg );
350 authmethod = LDAP_AUTH_SASL;
352 fprintf( stderr, "%s was not compiled with SASL support\n",
354 return( EXIT_FAILURE );
361 fprintf( stderr, "%s was not compiled with TLS support\n",
363 return( EXIT_FAILURE );
372 /* no alternative format */
373 if( ldif < 1 ) ldif = 1;
376 if ( ( authmethod == LDAP_AUTH_KRBV4 ) || ( authmethod ==
377 LDAP_AUTH_KRBV41 ) ) {
378 if( version > LDAP_VERSION2 ) {
379 fprintf( stderr, "Kerberos requires LDAPv2\n" );
380 return( EXIT_FAILURE );
382 version = LDAP_VERSION2;
384 else if ( authmethod == LDAP_AUTH_SASL ) {
385 if( version != -1 && version != LDAP_VERSION3 ) {
386 fprintf( stderr, "SASL requires LDAPv3\n" );
387 return( EXIT_FAILURE );
389 version = LDAP_VERSION3;
393 if( version != -1 && version != LDAP_VERSION3 ) {
394 fprintf(stderr, "manage DSA control requires LDAPv3\n");
397 version = LDAP_VERSION3;
401 if( version != -1 && version != LDAP_VERSION3 ) {
402 fprintf(stderr, "Start TLS requires LDAPv3\n");
405 version = LDAP_VERSION3;
408 if ( argc - optind < 1 ) {
412 filtpattern = strdup( argv[ optind ] );
414 if ( argv[ optind + 1 ] == NULL ) {
416 } else if ( sortattr == NULL || *sortattr == '\0' ) {
417 attrs = &argv[ optind + 1 ];
419 for ( i = optind + 1; i < argc; i++ ) {
420 if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
426 argv[ optind ] = sortattr;
430 attrs = &argv[ optind ];
433 if ( infile != NULL ) {
434 if ( infile[0] == '-' && infile[1] == '\0' ) {
436 } else if (( fp = fopen( infile, "r" )) == NULL ) {
443 && (tmpdir = getenv("TMPDIR")) == NULL
444 && (tmpdir = getenv("TMP")) == NULL
445 && (tmpdir = getenv("TEMP")) == NULL )
450 if( urlpre == NULL ) {
451 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
453 if( urlpre == NULL ) {
458 sprintf( urlpre, "file:///%s/",
459 tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
461 /* urlpre should be URLized.... */
465 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
466 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
468 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
469 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
475 (void) SIGNAL( SIGPIPE, SIG_IGN );
480 (ldapport ? "ldap_init( %s, %d )\n" : "ldap_init( %s, <DEFAULT> )\n"),
481 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
485 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
486 perror( "ldap_init" );
487 return( EXIT_FAILURE );
491 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
493 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
495 if (timelimit != -1 &&
496 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
498 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
500 if (sizelimit != -1 &&
501 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
503 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
505 if (referrals != -1 &&
506 ldap_set_option( ld, LDAP_OPT_REFERRALS,
507 (referrals ? LDAP_OPT_ON : LDAP_OPT_OFF) ) != LDAP_OPT_SUCCESS )
509 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
510 referrals ? "on" : "off" );
514 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS )
516 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
519 if ( use_tls && ldap_start_tls( ld, NULL, NULL ) != LDAP_SUCCESS ) {
521 ldap_perror( ld, "ldap_start_tls" );
522 return( EXIT_FAILURE );
527 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
528 passwd.bv_len = strlen( passwd.bv_val );
531 if ( authmethod == LDAP_AUTH_SASL ) {
532 #ifdef HAVE_CYRUS_SASL
533 int minssf = 0, maxssf = 0;
535 if ( sasl_integrity > 0 )
537 if ( sasl_integrity > 1 )
539 if ( sasl_privacy > 0 )
540 maxssf = 100000; /* Something big value */
541 if ( sasl_privacy > 1 )
544 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
545 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
546 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
548 return( EXIT_FAILURE );
550 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
551 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
552 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MAXSSF"
554 return( EXIT_FAILURE );
557 rc = ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
558 sasl_authz_id, sasl_mech,
559 passwd.bv_len ? &passwd : NULL,
562 if( rc != LDAP_SUCCESS ) {
563 ldap_perror( ld, "ldap_negotiated_sasl_bind_s" );
564 return( EXIT_FAILURE );
567 fprintf( stderr, "%s was not compiled with SASL support\n",
569 return( EXIT_FAILURE );
573 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
575 ldap_perror( ld, "ldap_bind" );
576 return( EXIT_FAILURE );
583 LDAPControl *ctrls[2];
587 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
588 c.ldctl_value.bv_val = NULL;
589 c.ldctl_value.bv_len = 0;
590 c.ldctl_iscritical = manageDSAit > 1;
592 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
594 if( err != LDAP_OPT_SUCCESS ) {
595 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
596 if( c.ldctl_iscritical ) {
597 exit( EXIT_FAILURE );
603 fprintf( stderr, "filter%s: %s\nreturning: ",
604 infile != NULL ? " pattern" : "",
607 if ( attrs == NULL ) {
608 fprintf( stderr, "ALL" );
610 for ( i = 0; attrs[ i ] != NULL; ++i ) {
611 fprintf( stderr, "%s ", attrs[ i ] );
614 fprintf( stderr, "\n" );
619 printf( "version: 1\n\n");
623 printf( "#\n# filter%s: %s\n# returning: ",
624 infile != NULL ? " pattern" : "",
627 if ( attrs == NULL ) {
630 for ( i = 0; attrs[ i ] != NULL; ++i ) {
631 printf( "%s ", attrs[ i ] );
638 if ( infile == NULL ) {
639 rc = dosearch( ld, base, scope, attrs, attrsonly, NULL, filtpattern );
644 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
645 line[ strlen( line ) - 1 ] = '\0';
651 rc = dosearch( ld, base, scope, attrs, attrsonly,
673 char filter[ BUFSIZ ];
674 int rc, first, matches;
675 LDAPMessage *res, *e;
677 if( filtpatt != NULL ) {
678 sprintf( filter, filtpatt, value );
681 fprintf( stderr, "filter is: (%s)\n", filter );
685 printf( "#\n# filter: %s\n#\n", filter );
689 sprintf( filter, "%s", value );
693 return( LDAP_SUCCESS );
696 if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
698 ldap_perror( ld, "ldap_search" );
700 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
707 while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
708 == LDAP_RES_SEARCH_ENTRY ) {
710 e = ldap_first_entry( ld, res );
716 print_entry( ld, e, attrsonly );
721 ldap_perror( ld, "ldap_result" );
724 if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
725 ldap_perror( ld, "ldap_search" );
727 if ( sortattr != NULL ) {
728 (void) ldap_sort_entries( ld, &res,
729 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
732 for ( e = ldap_first_entry( ld, res ); e != NULL;
733 e = ldap_next_entry( ld, e ) ) {
740 print_entry( ld, e, attrsonly );
745 printf( "%d matches\n", matches );
760 char tmpfname[ 256 ];
763 BerElement *ber = NULL;
764 struct berval **bvals;
767 dn = ldap_get_dn( ld, entry );
771 ufn = ldap_dn2ufn( dn );
772 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, strlen( ufn ));
775 write_ldif( LDIF_PUT_VALUE, "dn", dn, strlen( dn ));
777 printf( "%s\n", dn );
782 ufn = ldap_dn2ufn( dn );
785 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, strlen( ufn ));
787 printf( "%s\n", ufn );
791 if( ufn != NULL ) ldap_memfree( ufn );
794 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
795 a = ldap_next_attribute( ld, entry, ber ) )
797 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
803 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
808 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
809 for ( i = 0; bvals[i] != NULL; i++ ) {
810 if ( vals2tmp > 1 || ( vals2tmp
811 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
814 /* write value to file */
815 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
819 if ( mktemp( tmpfname ) == NULL ) {
824 if (( tmpfd = open( tmpfname, O_WRONLY|O_CREAT|O_EXCL, 0600 )) == -1 ) {
829 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
834 if ( fwrite( bvals[ i ]->bv_val,
835 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
844 sprintf( url, "%s%s", urlpre,
845 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
848 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
850 printf( "%s%s%s\n", a, sep, url );
856 write_ldif( LDIF_PUT_VALUE, a,
857 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
860 int notprint = !binary && !vals2tmp
861 && ldif_is_not_printable( bvals[i]->bv_val,
863 printf( "%s%s", a, sep );
864 puts( notprint ? "NOT PRINTABLE" : bvals[ i ]->bv_val );
868 ber_bvecfree( bvals );
879 write_ldif( int type, char *name, char *value, ber_len_t vallen )
883 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
887 fputs( ldif, stdout );