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"
24 usage( const char *s )
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 );
343 (ldapport ? "ldap_init( %s, %d )\n" : "ldap_init( %s, <DEFAULT> )\n"),
344 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
348 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
349 perror( "ldap_init" );
350 return( EXIT_FAILURE );
354 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
356 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
358 if (timelimit != -1 &&
359 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
361 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
363 if (sizelimit != -1 &&
364 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
366 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
368 if (referrals != -1 &&
369 ldap_set_option( ld, LDAP_OPT_REFERRALS,
370 (referrals ? LDAP_OPT_ON : LDAP_OPT_OFF) ) != LDAP_OPT_SUCCESS )
372 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
373 referrals ? "on" : "off" );
377 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS )
379 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
383 passwd = getpass("Enter LDAP Password: ");
386 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
387 ldap_perror( ld, "ldap_bind" );
388 return( EXIT_FAILURE );
394 LDAPControl *ctrls[2];
398 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
399 c.ldctl_value.bv_val = NULL;
400 c.ldctl_value.bv_len = 0;
401 c.ldctl_iscritical = manageDSAit > 1;
403 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
405 if( err != LDAP_OPT_SUCCESS ) {
406 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
407 if( c.ldctl_iscritical ) {
408 exit( EXIT_FAILURE );
414 fprintf( stderr, "filter%s: %s\nreturning: ",
415 infile != NULL ? " pattern" : "",
418 if ( attrs == NULL ) {
419 fprintf( stderr, "ALL" );
421 for ( i = 0; attrs[ i ] != NULL; ++i ) {
422 fprintf( stderr, "%s ", attrs[ i ] );
425 fprintf( stderr, "\n" );
430 printf( "version: 1\n\n");
434 printf( "#\n# filter%s: %s\n# returning: ",
435 infile != NULL ? " pattern" : "",
438 if ( attrs == NULL ) {
441 for ( i = 0; attrs[ i ] != NULL; ++i ) {
442 printf( "%s ", attrs[ i ] );
449 if ( infile == NULL ) {
450 rc = dosearch( ld, base, scope, attrs, attrsonly, NULL, filtpattern );
455 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
456 line[ strlen( line ) - 1 ] = '\0';
462 rc = dosearch( ld, base, scope, attrs, attrsonly,
484 char filter[ BUFSIZ ];
485 int rc, first, matches;
486 LDAPMessage *res, *e;
488 if( filtpatt != NULL ) {
489 sprintf( filter, filtpatt, value );
492 fprintf( stderr, "filter is: (%s)\n", filter );
496 printf( "#\n# filter: %s\n#\n", filter );
500 sprintf( filter, "%s", value );
504 return( LDAP_SUCCESS );
507 if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
509 ldap_perror( ld, "ldap_search" );
511 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
518 while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
519 == LDAP_RES_SEARCH_ENTRY ) {
521 e = ldap_first_entry( ld, res );
527 print_entry( ld, e, attrsonly );
532 ldap_perror( ld, "ldap_result" );
535 if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
536 ldap_perror( ld, "ldap_search" );
538 if ( sortattr != NULL ) {
539 (void) ldap_sort_entries( ld, &res,
540 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
543 for ( e = ldap_first_entry( ld, res ); e != NULL;
544 e = ldap_next_entry( ld, e ) ) {
551 print_entry( ld, e, attrsonly );
556 printf( "%d matches\n", matches );
571 char tmpfname[ 256 ];
574 BerElement *ber = NULL;
575 struct berval **bvals;
578 dn = ldap_get_dn( ld, entry );
582 ufn = ldap_dn2ufn( dn );
583 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, strlen( ufn ));
586 write_ldif( LDIF_PUT_VALUE, "dn", dn, strlen( dn ));
588 printf( "%s\n", dn );
593 ufn = ldap_dn2ufn( dn );
596 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, strlen( ufn ));
598 printf( "%s\n", ufn );
602 if( ufn != NULL ) ldap_memfree( ufn );
605 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
606 a = ldap_next_attribute( ld, entry, ber ) )
608 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
614 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
619 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
620 for ( i = 0; bvals[i] != NULL; i++ ) {
621 if ( vals2tmp > 1 || ( vals2tmp
622 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
624 /* write value to file */
625 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
629 if ( mktemp( tmpfname ) == NULL ) {
634 if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
639 if ( fwrite( bvals[ i ]->bv_val,
640 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
649 sprintf( url, "%s%s", urlpre,
650 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
653 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
655 printf( "%s%s%s\n", a, sep, url );
661 write_ldif( LDIF_PUT_VALUE, a,
662 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
665 int notprint = !binary && !vals2tmp
666 && ldif_is_not_printable( bvals[i]->bv_val,
668 printf( "%s%s", a, sep );
669 puts( notprint ? "NOT PRINTABLE" : bvals[ i ]->bv_val );
673 ber_bvecfree( bvals );
684 write_ldif( int type, char *name, char *value, ber_len_t vallen )
688 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
692 fputs( ldif, stdout );