3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/stdlib.h>
13 #include <ac/signal.h>
14 #include <ac/string.h>
15 #include <ac/unistd.h>
24 #include "ldap_defaults.h"
29 usage( const char *s )
32 "usage: %s [options] filter [attributes...]\nwhere:\n"
33 " filter\tRFC-1558 compliant LDAP search filter\n"
34 " attributes\twhitespace-separated list of attributes to retrieve\n"
35 "\t\t1.1 -- no attributes\n"
36 "\t\t* -- all user attributes\n"
37 "\t\t+ -- all operational attributes\n"
38 "\t\tempty list -- all non-operational attributes\n"
40 " -n\t\tshow what would be done but don't actually search\n"
41 " -v\t\trun in verbose mode (diagnostics to standard output)\n"
42 " -t\t\twrite binary values to files in TMPDIR\n"
43 " -tt\t\twrite all values to files in TMPDIR\n"
44 " -T path\twrite files to directory specified by path (default: \"/tmp\")\n"
45 " -V prefix\tURL prefix for files (default: \"file://tmp/\"\n"
46 " -u\t\tinclude User Friendly entry names in the output\n"
47 " -A\t\tretrieve attribute names only (no values)\n"
48 " -B\t\tdo not suppress printing of binary values\n"
49 " -F sep\tprint `sep' instead of `=' between attribute names and values\n"
50 " -L\t\tprint entries in LDIF format (implies -B)\n"
51 " -LL\t\tprint entries in LDIF format without comments\n"
52 " -LLL\t\tprint entries in LDIF format without comments and version\n"
53 " -M\t\tenable Manage DSA IT control (-MM to make critical)\n"
54 " -R\t\tdo not automatically follow referrals\n"
55 " -S attr\tsort the results by attribute `attr'\n"
56 " -d level\tset LDAP debugging level to `level'\n"
57 " -f file\tperform sequence of searches listed in `file'\n"
58 " -b basedn\tbase dn for search\n"
59 " -s scope\tone of base, one, or sub (search scope)\n"
60 " -a deref\tone of never, always, search, or find (alias dereferencing)\n"
61 " -l limit\ttime limit (in seconds) for search\n"
62 " -z limit\tsize limit (in entries) for search\n"
63 " -D binddn\tbind dn\n"
64 " -w passwd\tbind passwd (for simple authentication)\n"
65 " -W\t\tprompt for bind passwd\n"
66 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
67 " -k\t\tuse Kerberos instead of Simple Password authentication\n"
69 " -h host\tldap server\n"
70 " -p port\tport on ldap server\n"
71 " -P version\tprocotol version (2 or 3)\n"
77 static void print_entry LDAP_P((
82 static int write_ldif LDAP_P((
88 static int dosearch LDAP_P((
98 #define URLPRE "file:/tmp/"
100 static char *tmpdir = NULL;
101 static char *urlpre = NULL;
103 static char *binddn = NULL;
104 static char *passwd = NULL;
105 static char *base = NULL;
106 static char *ldaphost = NULL;
107 static int ldapport = 0;
108 static char *sep = DEFSEP;
109 static char *sortattr = NULL;
110 static int skipsortattr = 0;
111 static int verbose, not, includeufn, binary, vals2tmp, ldif;
114 main( int argc, char **argv )
116 char *infile, *filtpattern, **attrs, line[ BUFSIZ ];
118 int rc, i, first, scope, deref, attrsonly, manageDSAit;
119 int referrals, timelimit, sizelimit, debug;
120 int authmethod, version, want_bindpw;
124 debug = verbose = binary = not = vals2tmp =
125 attrsonly = manageDSAit = ldif = want_bindpw = 0;
127 deref = referrals = sizelimit = timelimit = version = -1;
129 scope = LDAP_SCOPE_SUBTREE;
130 authmethod = LDAP_AUTH_SIMPLE;
132 while (( i = getopt( argc, argv,
133 "WKknuvtMRABLD:s:f:h:b:d:P:p:F:a:w:l:z:S:T:V:")) != EOF )
136 case 'n': /* do Not do any searches */
139 case 'v': /* verbose mode */
143 debug |= atoi( optarg );
145 case 'k': /* use kerberos bind */
146 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
147 authmethod = LDAP_AUTH_KRBV4;
149 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
152 case 'K': /* use kerberos bind, 1st part only */
153 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
154 authmethod = LDAP_AUTH_KRBV41;
156 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
159 case 'u': /* include UFN */
162 case 't': /* write attribute values to /tmp files */
166 /* enable Manage DSA IT */
169 case 'R': /* don't automatically chase referrals */
170 referrals = (int) LDAP_OPT_OFF;
172 case 'A': /* retrieve attribute names only -- no values */
175 case 'L': /* print entries in LDIF format */
177 /* fall through -- always allow binary when outputting LDIF */
178 case 'B': /* allow binary values to be printed */
181 case 's': /* search scope */
182 if ( strcasecmp( optarg, "base" ) == 0 ) {
183 scope = LDAP_SCOPE_BASE;
184 } else if ( strcasecmp( optarg, "one" ) == 0 ) {
185 scope = LDAP_SCOPE_ONELEVEL;
186 } else if ( strcasecmp( optarg, "sub" ) == 0 ) {
187 scope = LDAP_SCOPE_SUBTREE;
189 fprintf( stderr, "scope should be base, one, or sub\n" );
194 case 'a': /* set alias deref option */
195 if ( strcasecmp( optarg, "never" ) == 0 ) {
196 deref = LDAP_DEREF_NEVER;
197 } else if ( strcasecmp( optarg, "search" ) == 0 ) {
198 deref = LDAP_DEREF_SEARCHING;
199 } else if ( strcasecmp( optarg, "find" ) == 0 ) {
200 deref = LDAP_DEREF_FINDING;
201 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
202 deref = LDAP_DEREF_ALWAYS;
204 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
209 case 'T': /* field separator */
210 if( tmpdir ) free( tmpdir );
211 tmpdir = strdup( optarg );
213 case 'V': /* field separator */
214 if( urlpre ) free( urlpre );
215 urlpre = strdup( optarg );
217 case 'F': /* field separator */
218 sep = strdup( optarg );
220 case 'f': /* input file */
221 infile = strdup( optarg );
223 case 'h': /* ldap host */
224 ldaphost = strdup( optarg );
226 case 'b': /* searchbase */
227 base = strdup( optarg );
229 case 'D': /* bind DN */
230 binddn = strdup( optarg );
232 case 'p': /* ldap port */
233 ldapport = atoi( optarg );
235 case 'w': /* bind password */
236 passwd = strdup( optarg );
240 for( p = optarg; *p == '\0'; p++ ) {
245 case 'l': /* time limit */
246 timelimit = atoi( optarg );
248 case 'z': /* size limit */
249 sizelimit = atoi( optarg );
251 case 'S': /* sort attribute */
252 sortattr = strdup( optarg );
258 switch( atoi( optarg ) )
261 version = LDAP_VERSION2;
264 version = LDAP_VERSION3;
267 fprintf( stderr, "protocol version should be 2 or 3\n" );
276 if( authmethod != LDAP_AUTH_SIMPLE ) {
277 if( version == LDAP_VERSION3 ) {
278 fprintf(stderr, "Kerberos requires LDAPv2\n");
281 version = LDAP_VERSION2;
285 if( version == LDAP_VERSION2 ) {
286 fprintf(stderr, "manage DSA control requires LDAPv3\n");
289 version = LDAP_VERSION3;
292 if ( argc - optind < 1 ) {
296 filtpattern = strdup( argv[ optind ] );
298 if ( argv[ optind + 1 ] == NULL ) {
300 } else if ( sortattr == NULL || *sortattr == '\0' ) {
301 attrs = &argv[ optind + 1 ];
303 for ( i = optind + 1; i < argc; i++ ) {
304 if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
310 argv[ optind ] = sortattr;
314 attrs = &argv[ optind ];
317 if ( infile != NULL ) {
318 if ( infile[0] == '-' && infile[1] == '\0' ) {
320 } else if (( fp = fopen( infile, "r" )) == NULL ) {
327 && (tmpdir = getenv("TMPDIR")) == NULL
328 && (tmpdir = getenv("TMP")) == NULL
329 && (tmpdir = getenv("TEMP")) == NULL )
334 if( urlpre == NULL ) {
335 urlpre = malloc( sizeof("file:///") + strlen(tmpdir) );
337 if( urlpre == NULL ) {
342 sprintf( urlpre, "file:///%s/",
343 tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
345 /* urlpre should be URLized.... */
349 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
350 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
352 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
353 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
359 (void) SIGNAL( SIGPIPE, SIG_IGN );
364 (ldapport ? "ldap_init( %s, %d )\n" : "ldap_init( %s, <DEFAULT> )\n"),
365 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
369 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
370 perror( "ldap_init" );
371 return( EXIT_FAILURE );
375 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
377 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
379 if (timelimit != -1 &&
380 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
382 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
384 if (sizelimit != -1 &&
385 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
387 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
389 if (referrals != -1 &&
390 ldap_set_option( ld, LDAP_OPT_REFERRALS,
391 (referrals ? LDAP_OPT_ON : LDAP_OPT_OFF) ) != LDAP_OPT_SUCCESS )
393 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
394 referrals ? "on" : "off" );
398 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS )
400 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
404 passwd = getpass("Enter LDAP Password: ");
407 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
408 ldap_perror( ld, "ldap_bind" );
409 return( EXIT_FAILURE );
415 LDAPControl *ctrls[2];
419 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
420 c.ldctl_value.bv_val = NULL;
421 c.ldctl_value.bv_len = 0;
422 c.ldctl_iscritical = manageDSAit > 1;
424 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
426 if( err != LDAP_OPT_SUCCESS ) {
427 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
428 if( c.ldctl_iscritical ) {
429 exit( EXIT_FAILURE );
435 fprintf( stderr, "filter%s: %s\nreturning: ",
436 infile != NULL ? " pattern" : "",
439 if ( attrs == NULL ) {
440 fprintf( stderr, "ALL" );
442 for ( i = 0; attrs[ i ] != NULL; ++i ) {
443 fprintf( stderr, "%s ", attrs[ i ] );
446 fprintf( stderr, "\n" );
451 printf( "version: 1\n\n");
455 printf( "#\n# filter%s: %s\n# returning: ",
456 infile != NULL ? " pattern" : "",
459 if ( attrs == NULL ) {
462 for ( i = 0; attrs[ i ] != NULL; ++i ) {
463 printf( "%s ", attrs[ i ] );
470 if ( infile == NULL ) {
471 rc = dosearch( ld, base, scope, attrs, attrsonly, NULL, filtpattern );
476 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
477 line[ strlen( line ) - 1 ] = '\0';
483 rc = dosearch( ld, base, scope, attrs, attrsonly,
505 char filter[ BUFSIZ ];
506 int rc, first, matches;
507 LDAPMessage *res, *e;
509 if( filtpatt != NULL ) {
510 sprintf( filter, filtpatt, value );
513 fprintf( stderr, "filter is: (%s)\n", filter );
517 printf( "#\n# filter: %s\n#\n", filter );
521 sprintf( filter, "%s", value );
525 return( LDAP_SUCCESS );
528 if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
530 ldap_perror( ld, "ldap_search" );
532 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
539 while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
540 == LDAP_RES_SEARCH_ENTRY ) {
542 e = ldap_first_entry( ld, res );
548 print_entry( ld, e, attrsonly );
553 ldap_perror( ld, "ldap_result" );
556 if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
557 ldap_perror( ld, "ldap_search" );
559 if ( sortattr != NULL ) {
560 (void) ldap_sort_entries( ld, &res,
561 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
564 for ( e = ldap_first_entry( ld, res ); e != NULL;
565 e = ldap_next_entry( ld, e ) ) {
572 print_entry( ld, e, attrsonly );
577 printf( "%d matches\n", matches );
592 char tmpfname[ 256 ];
595 BerElement *ber = NULL;
596 struct berval **bvals;
599 dn = ldap_get_dn( ld, entry );
603 ufn = ldap_dn2ufn( dn );
604 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, strlen( ufn ));
607 write_ldif( LDIF_PUT_VALUE, "dn", dn, strlen( dn ));
609 printf( "%s\n", dn );
614 ufn = ldap_dn2ufn( dn );
617 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, strlen( ufn ));
619 printf( "%s\n", ufn );
623 if( ufn != NULL ) ldap_memfree( ufn );
626 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
627 a = ldap_next_attribute( ld, entry, ber ) )
629 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
635 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
640 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
641 for ( i = 0; bvals[i] != NULL; i++ ) {
642 if ( vals2tmp > 1 || ( vals2tmp
643 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
645 /* write value to file */
646 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
650 if ( mktemp( tmpfname ) == NULL ) {
655 if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
660 if ( fwrite( bvals[ i ]->bv_val,
661 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
670 sprintf( url, "%s%s", urlpre,
671 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
674 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
676 printf( "%s%s%s\n", a, sep, url );
682 write_ldif( LDIF_PUT_VALUE, a,
683 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
686 int notprint = !binary && !vals2tmp
687 && ldif_is_not_printable( bvals[i]->bv_val,
689 printf( "%s%s", a, sep );
690 puts( notprint ? "NOT PRINTABLE" : bvals[ i ]->bv_val );
694 ber_bvecfree( bvals );
705 write_ldif( int type, char *name, char *value, ber_len_t vallen )
709 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
713 fputs( ldif, stdout );