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"
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 */
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 */
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 ( argc - optind < 1 ) {
280 filtpattern = strdup( argv[ optind ] );
282 if ( argv[ optind + 1 ] == NULL ) {
284 } else if ( sortattr == NULL || *sortattr == '\0' ) {
285 attrs = &argv[ optind + 1 ];
287 for ( i = optind + 1; i < argc; i++ ) {
288 if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
294 argv[ optind ] = sortattr;
298 attrs = &argv[ optind ];
301 if ( infile != NULL ) {
302 if ( infile[0] == '-' && infile[1] == '\0' ) {
304 } else if (( fp = fopen( infile, "r" )) == NULL ) {
311 && (tmpdir = getenv("TMPDIR")) == NULL
312 && (tmpdir = getenv("TMP")) == NULL
313 && (tmpdir = getenv("TEMP")) == NULL )
318 if( urlpre == NULL ) {
319 urlpre = malloc( sizeof("file:///") + strlen(tmpdir) );
321 if( urlpre == NULL ) {
326 sprintf( urlpre, "file:///%s/",
327 tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
329 /* urlpre should be URLized.... */
333 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
334 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
336 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
337 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
343 (void) SIGNAL( SIGPIPE, SIG_IGN );
348 (ldapport ? "ldap_init( %s, %d )\n" : "ldap_init( %s, <DEFAULT> )\n"),
349 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
353 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
354 perror( "ldap_init" );
355 return( EXIT_FAILURE );
359 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
361 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
363 if (timelimit != -1 &&
364 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
366 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
368 if (sizelimit != -1 &&
369 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
371 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
373 if (referrals != -1 &&
374 ldap_set_option( ld, LDAP_OPT_REFERRALS,
375 (referrals ? LDAP_OPT_ON : LDAP_OPT_OFF) ) != LDAP_OPT_SUCCESS )
377 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
378 referrals ? "on" : "off" );
382 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS )
384 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
388 passwd = getpass("Enter LDAP Password: ");
391 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
392 ldap_perror( ld, "ldap_bind" );
393 return( EXIT_FAILURE );
399 LDAPControl *ctrls[2];
403 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
404 c.ldctl_value.bv_val = NULL;
405 c.ldctl_value.bv_len = 0;
406 c.ldctl_iscritical = manageDSAit > 1;
408 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
410 if( err != LDAP_OPT_SUCCESS ) {
411 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
412 if( c.ldctl_iscritical ) {
413 exit( EXIT_FAILURE );
419 fprintf( stderr, "filter%s: %s\nreturning: ",
420 infile != NULL ? " pattern" : "",
423 if ( attrs == NULL ) {
424 fprintf( stderr, "ALL" );
426 for ( i = 0; attrs[ i ] != NULL; ++i ) {
427 fprintf( stderr, "%s ", attrs[ i ] );
430 fprintf( stderr, "\n" );
435 printf( "version: 1\n\n");
439 printf( "#\n# filter%s: %s\n# returning: ",
440 infile != NULL ? " pattern" : "",
443 if ( attrs == NULL ) {
446 for ( i = 0; attrs[ i ] != NULL; ++i ) {
447 printf( "%s ", attrs[ i ] );
454 if ( infile == NULL ) {
455 rc = dosearch( ld, base, scope, attrs, attrsonly, NULL, filtpattern );
460 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
461 line[ strlen( line ) - 1 ] = '\0';
467 rc = dosearch( ld, base, scope, attrs, attrsonly,
489 char filter[ BUFSIZ ];
490 int rc, first, matches;
491 LDAPMessage *res, *e;
493 if( filtpatt != NULL ) {
494 sprintf( filter, filtpatt, value );
497 fprintf( stderr, "filter is: (%s)\n", filter );
501 printf( "#\n# filter: %s\n#\n", filter );
505 sprintf( filter, "%s", value );
509 return( LDAP_SUCCESS );
512 if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
514 ldap_perror( ld, "ldap_search" );
516 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
523 while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
524 == LDAP_RES_SEARCH_ENTRY ) {
526 e = ldap_first_entry( ld, res );
532 print_entry( ld, e, attrsonly );
537 ldap_perror( ld, "ldap_result" );
540 if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
541 ldap_perror( ld, "ldap_search" );
543 if ( sortattr != NULL ) {
544 (void) ldap_sort_entries( ld, &res,
545 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
548 for ( e = ldap_first_entry( ld, res ); e != NULL;
549 e = ldap_next_entry( ld, e ) ) {
556 print_entry( ld, e, attrsonly );
561 printf( "%d matches\n", matches );
576 char tmpfname[ 256 ];
579 BerElement *ber = NULL;
580 struct berval **bvals;
583 dn = ldap_get_dn( ld, entry );
587 ufn = ldap_dn2ufn( dn );
588 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, strlen( ufn ));
591 write_ldif( LDIF_PUT_VALUE, "dn", dn, strlen( dn ));
593 printf( "%s\n", dn );
598 ufn = ldap_dn2ufn( dn );
601 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, strlen( ufn ));
603 printf( "%s\n", ufn );
607 if( ufn != NULL ) ldap_memfree( ufn );
610 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
611 a = ldap_next_attribute( ld, entry, ber ) )
613 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
619 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
624 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
625 for ( i = 0; bvals[i] != NULL; i++ ) {
626 if ( vals2tmp > 1 || ( vals2tmp
627 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
629 /* write value to file */
630 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
634 if ( mktemp( tmpfname ) == NULL ) {
639 if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
644 if ( fwrite( bvals[ i ]->bv_val,
645 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
654 sprintf( url, "%s%s", urlpre,
655 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
658 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
660 printf( "%s%s%s\n", a, sep, url );
666 write_ldif( LDIF_PUT_VALUE, a,
667 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
670 int notprint = !binary && !vals2tmp
671 && ldif_is_not_printable( bvals[i]->bv_val,
673 printf( "%s%s", a, sep );
674 puts( notprint ? "NOT PRINTABLE" : bvals[ i ]->bv_val );
678 ber_bvecfree( bvals );
689 write_ldif( int type, char *name, char *value, ber_len_t vallen )
693 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
697 fputs( ldif, stdout );