2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/signal.h>
13 #include <ac/string.h>
14 #include <ac/unistd.h>
19 #include "ldap_defaults.h"
27 "usage: %s [options] filter [attributes...]\nwhere:\n"
28 " filter\tRFC-1558 compliant LDAP search filter\n"
29 " attributes\twhitespace-separated list of attributes to retrieve\n"
30 "\t\t1.1 -- no attributes\n"
31 "\t\t* -- all user attributes\n"
32 "\t\t+ -- all operational attributes\n"
33 "\t\tempty list -- all non-operational attributes\n"
35 " -n\t\tshow what would be done but don't actually search\n"
36 " -v\t\trun in verbose mode (diagnostics to standard output)\n"
37 " -t\t\twrite binary values to files in TMPDIR\n"
38 " -tt\t\twrite all values to files in TMPDIR\n"
39 " -T path\twrite files to directory specified by path (default: \"/tmp\")\n"
40 " -V prefix\tURL prefix for files (default: \"file://tmp/\"\n"
41 " -u\t\tinclude User Friendly entry names in the output\n"
42 " -A\t\tretrieve attribute names only (no values)\n"
43 " -B\t\tdo not suppress printing of binary values\n"
44 " -F sep\tprint `sep' instead of `=' between attribute names and values\n"
45 " -L\t\tprint entries in LDIF format (implies -B)\n"
46 " -LL\t\tprint entries in LDIF format without comments\n"
47 " -LLL\t\tprint entries in LDIF format without comments and version\n"
48 " -M\t\tenable Manage DSA IT control (-MM to make critical)\n"
49 " -R\t\tdo not automatically follow referrals\n"
50 " -S attr\tsort the results by attribute `attr'\n"
51 " -d level\tset LDAP debugging level to `level'\n"
52 " -f file\tperform sequence of searches listed in `file'\n"
53 " -b basedn\tbase dn for search\n"
54 " -s scope\tone of base, one, or sub (search scope)\n"
55 " -a deref\tone of never, always, search, or find (alias dereferencing)\n"
56 " -l limit\ttime limit (in seconds) for search\n"
57 " -z limit\tsize limit (in entries) for search\n"
58 " -D binddn\tbind dn\n"
59 " -w passwd\tbind passwd (for simple authentication)\n"
60 " -W\t\tprompt for bind passwd\n"
62 " -k\t\tuse Kerberos instead of Simple Password authentication\n"
64 " -h host\tldap server\n"
65 " -p port\tport on ldap server\n"
66 " -P version\tprocotol version (2 or 3)\n"
72 static void print_entry LDAP_P((
77 static int write_ldif LDAP_P((
83 static int dosearch LDAP_P((
93 #define URLPRE "file:/tmp/"
95 static char *tmpdir = NULL;
96 static char *urlpre = NULL;
98 static char *binddn = NULL;
99 static char *passwd = NULL;
100 static char *base = NULL;
101 static char *ldaphost = NULL;
102 static int ldapport = 0;
103 static char *sep = DEFSEP;
104 static char *sortattr = NULL;
105 static int skipsortattr = 0;
106 static int verbose, not, includeufn, binary, vals2tmp, ldif;
109 main( int argc, char **argv )
111 char *infile, *filtpattern, **attrs, line[ BUFSIZ ];
113 int rc, i, first, scope, deref, attrsonly, manageDSAit;
114 int referrals, timelimit, sizelimit, debug;
115 int authmethod, version, want_bindpw;
119 debug = verbose = binary = not = vals2tmp =
120 attrsonly = manageDSAit = ldif = want_bindpw = 0;
122 deref = referrals = sizelimit = timelimit = version = -1;
124 scope = LDAP_SCOPE_SUBTREE;
125 authmethod = LDAP_AUTH_SIMPLE;
127 while (( i = getopt( argc, argv,
128 "WKknuvtMRABLD:s:f:h:b:d:P:p:F:a:w:l:z:S:T:V:")) != EOF )
131 case 'n': /* do Not do any searches */
134 case 'v': /* verbose mode */
138 debug |= atoi( optarg );
140 case 'k': /* use kerberos bind */
142 authmethod = LDAP_AUTH_KRBV4;
144 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
147 case 'K': /* use kerberos bind, 1st part only */
149 authmethod = LDAP_AUTH_KRBV41;
151 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
154 case 'u': /* include UFN */
157 case 't': /* write attribute values to /tmp files */
161 /* enable Manage DSA IT */
164 case 'R': /* don't automatically chase referrals */
165 referrals = (int) LDAP_OPT_OFF;
167 case 'A': /* retrieve attribute names only -- no values */
170 case 'L': /* print entries in LDIF format */
172 /* fall through -- always allow binary when outputting LDIF */
173 case 'B': /* allow binary values to be printed */
176 case 's': /* search scope */
177 if ( strcasecmp( optarg, "base" ) == 0 ) {
178 scope = LDAP_SCOPE_BASE;
179 } else if ( strcasecmp( optarg, "one" ) == 0 ) {
180 scope = LDAP_SCOPE_ONELEVEL;
181 } else if ( strcasecmp( optarg, "sub" ) == 0 ) {
182 scope = LDAP_SCOPE_SUBTREE;
184 fprintf( stderr, "scope should be base, one, or sub\n" );
189 case 'a': /* set alias deref option */
190 if ( strcasecmp( optarg, "never" ) == 0 ) {
191 deref = LDAP_DEREF_NEVER;
192 } else if ( strcasecmp( optarg, "search" ) == 0 ) {
193 deref = LDAP_DEREF_SEARCHING;
194 } else if ( strcasecmp( optarg, "find" ) == 0 ) {
195 deref = LDAP_DEREF_FINDING;
196 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
197 deref = LDAP_DEREF_ALWAYS;
199 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
204 case 'T': /* field separator */
205 if( tmpdir ) free( tmpdir );
206 tmpdir = strdup( optarg );
208 case 'V': /* field separator */
209 if( urlpre ) free( urlpre );
210 urlpre = strdup( optarg );
212 case 'F': /* field separator */
213 sep = strdup( optarg );
215 case 'f': /* input file */
216 infile = strdup( optarg );
218 case 'h': /* ldap host */
219 ldaphost = strdup( optarg );
221 case 'b': /* searchbase */
222 base = strdup( optarg );
224 case 'D': /* bind DN */
225 binddn = strdup( optarg );
227 case 'p': /* ldap port */
228 ldapport = atoi( optarg );
230 case 'w': /* bind password */
231 passwd = strdup( optarg );
235 for( p = optarg; *p == '\0'; p++ ) {
240 case 'l': /* time limit */
241 timelimit = atoi( optarg );
243 case 'z': /* size limit */
244 sizelimit = atoi( optarg );
246 case 'S': /* sort attribute */
247 sortattr = strdup( optarg );
253 switch( atoi( optarg ) )
256 version = LDAP_VERSION2;
259 version = LDAP_VERSION3;
262 fprintf( stderr, "protocol version should be 2 or 3\n" );
271 if ( argc - optind < 1 ) {
275 filtpattern = strdup( argv[ optind ] );
277 if ( argv[ optind + 1 ] == NULL ) {
279 } else if ( sortattr == NULL || *sortattr == '\0' ) {
280 attrs = &argv[ optind + 1 ];
282 for ( i = optind + 1; i < argc; i++ ) {
283 if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
289 argv[ optind ] = sortattr;
293 attrs = &argv[ optind ];
296 if ( infile != NULL ) {
297 if ( infile[0] == '-' && infile[1] == '\0' ) {
299 } else if (( fp = fopen( infile, "r" )) == NULL ) {
306 && (tmpdir = getenv("TMPDIR")) == NULL
307 && (tmpdir = getenv("TMP")) == NULL
308 && (tmpdir = getenv("TEMP")) == NULL )
313 if( urlpre == NULL ) {
314 urlpre = malloc( sizeof("file:///") + strlen(tmpdir) );
316 if( urlpre == NULL ) {
321 sprintf( urlpre, "file:///%s/",
322 tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
324 /* urlpre should be URLized.... */
328 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
329 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
331 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
332 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
338 (void) SIGNAL( SIGPIPE, SIG_IGN );
342 fprintf( stderr, "ldap_init( %s, %d )\n",
343 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
347 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
348 perror( "ldap_init" );
349 return( EXIT_FAILURE );
353 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
355 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
357 if (timelimit != -1 &&
358 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
360 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
362 if (sizelimit != -1 &&
363 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
365 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
367 if (referrals != -1 &&
368 ldap_set_option( ld, LDAP_OPT_REFERRALS,
369 (referrals ? LDAP_OPT_ON : LDAP_OPT_OFF) ) != LDAP_OPT_SUCCESS )
371 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
372 referrals ? "on" : "off" );
376 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS )
378 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
382 passwd = getpass("Enter LDAP Password: ");
385 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
386 ldap_perror( ld, "ldap_bind" );
387 return( EXIT_FAILURE );
393 LDAPControl *ctrls[2];
397 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
398 c.ldctl_value.bv_val = NULL;
399 c.ldctl_value.bv_len = 0;
400 c.ldctl_iscritical = manageDSAit > 1;
402 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
404 if( err != LDAP_OPT_SUCCESS ) {
405 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
406 if( c.ldctl_iscritical ) {
407 exit( EXIT_FAILURE );
413 fprintf( stderr, "filter%s: %s\nreturning: ",
414 infile != NULL ? " pattern" : "",
417 if ( attrs == NULL ) {
420 for ( i = 0; attrs[ i ] != NULL; ++i ) {
421 fprintf( stderr, "%s ", attrs[ i ] );
424 fprintf( stderr, "\n" );
429 printf( "version: 1\n\n");
433 printf( "#\n# filter%s: %s\n# returning: ",
434 infile != NULL ? " pattern" : "",
437 if ( attrs == NULL ) {
440 for ( i = 0; attrs[ i ] != NULL; ++i ) {
441 printf( "%s ", attrs[ i ] );
448 if ( infile == NULL ) {
449 rc = dosearch( ld, base, scope, attrs, attrsonly, NULL, filtpattern );
454 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
455 line[ strlen( line ) - 1 ] = '\0';
461 rc = dosearch( ld, base, scope, attrs, attrsonly,
483 char filter[ BUFSIZ ];
484 int rc, first, matches;
485 LDAPMessage *res, *e;
487 if( filtpatt != NULL ) {
488 sprintf( filter, filtpatt, value );
491 fprintf( stderr, "filter is: (%s)\n", filter );
495 printf( "#\n# filter: %s\n#\n", filter );
499 sprintf( filter, "%s", value );
503 return( LDAP_SUCCESS );
506 if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
508 ldap_perror( ld, "ldap_search" );
510 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
517 while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
518 == LDAP_RES_SEARCH_ENTRY ) {
520 e = ldap_first_entry( ld, res );
526 print_entry( ld, e, attrsonly );
531 ldap_perror( ld, "ldap_result" );
534 if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
535 ldap_perror( ld, "ldap_search" );
537 if ( sortattr != NULL ) {
538 (void) ldap_sort_entries( ld, &res,
539 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
542 for ( e = ldap_first_entry( ld, res ); e != NULL;
543 e = ldap_next_entry( ld, e ) ) {
550 print_entry( ld, e, attrsonly );
555 printf( "%d matches\n", matches );
569 char tmpfname[ 256 ];
572 BerElement *ber = NULL;
573 struct berval **bvals;
576 dn = ldap_get_dn( ld, entry );
580 ufn = ldap_dn2ufn( dn );
581 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, strlen( ufn ));
584 write_ldif( LDIF_PUT_VALUE, "dn", dn, strlen( dn ));
586 printf( "%s\n", dn );
591 ufn = ldap_dn2ufn( dn );
594 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, strlen( ufn ));
596 printf( "%s\n", ufn );
600 if( ufn != NULL ) ldap_memfree( ufn );
603 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
604 a = ldap_next_attribute( ld, entry, ber ) )
606 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
612 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
617 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
618 for ( i = 0; bvals[i] != NULL; i++ ) {
619 if ( vals2tmp > 1 || ( vals2tmp
620 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
622 /* write value to file */
623 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
627 if ( mktemp( tmpfname ) == NULL ) {
632 if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
637 if ( fwrite( bvals[ i ]->bv_val,
638 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
647 sprintf( url, "%s%s", urlpre,
648 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
651 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
653 printf( "%s%s%s\n", a, sep, url );
659 write_ldif( LDIF_PUT_VALUE, a,
660 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
663 int notprint = !binary && !vals2tmp
664 && ldif_is_not_printable( bvals[i]->bv_val,
666 printf( "%s%s", a, sep );
667 puts( notprint ? "NOT PRINTABLE" : bvals[ i ]->bv_val );
672 ber_bvecfree( bvals );
683 write_ldif( int type, char *name, char *value, ber_len_t vallen )
687 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
691 fputs( ldif, stdout );