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>
20 #include "ldap_defaults.h"
25 usage( const char *s )
28 "usage: %s [options] filter [attributes...]\nwhere:\n"
29 " filter\tRFC-1558 compliant LDAP search filter\n"
30 " attributes\twhitespace-separated list of attributes to retrieve\n"
31 "\t\t1.1 -- no attributes\n"
32 "\t\t* -- all user attributes\n"
33 "\t\t+ -- all operational attributes\n"
34 "\t\tempty list -- all non-operational attributes\n"
36 " -n\t\tshow what would be done but don't actually search\n"
37 " -v\t\trun in verbose mode (diagnostics to standard output)\n"
38 " -t\t\twrite binary values to files in TMPDIR\n"
39 " -tt\t\twrite all values to files in TMPDIR\n"
40 " -T path\twrite files to directory specified by path (default: \"/tmp\")\n"
41 " -V prefix\tURL prefix for files (default: \"file://tmp/\"\n"
42 " -u\t\tinclude User Friendly entry names in the output\n"
43 " -A\t\tretrieve attribute names only (no values)\n"
44 " -B\t\tdo not suppress printing of binary values\n"
45 " -F sep\tprint `sep' instead of `=' between attribute names and values\n"
46 " -L\t\tprint entries in LDIF format (implies -B)\n"
47 " -LL\t\tprint entries in LDIF format without comments\n"
48 " -LLL\t\tprint entries in LDIF format without comments and version\n"
49 " -M\t\tenable Manage DSA IT control (-MM to make critical)\n"
50 " -R\t\tdo not automatically follow referrals\n"
51 " -S attr\tsort the results by attribute `attr'\n"
52 " -d level\tset LDAP debugging level to `level'\n"
53 " -f file\tperform sequence of searches listed in `file'\n"
54 " -b basedn\tbase dn for search\n"
55 " -s scope\tone of base, one, or sub (search scope)\n"
56 " -a deref\tone of never, always, search, or find (alias dereferencing)\n"
57 " -l limit\ttime limit (in seconds) for search\n"
58 " -z limit\tsize limit (in entries) for search\n"
59 " -D binddn\tbind dn\n"
60 " -w passwd\tbind passwd (for simple authentication)\n"
61 " -W\t\tprompt for bind passwd\n"
63 " -k\t\tuse Kerberos instead of Simple Password authentication\n"
65 " -h host\tldap server\n"
66 " -p port\tport on ldap server\n"
67 " -P version\tprocotol version (2 or 3)\n"
73 static void print_entry LDAP_P((
78 static int write_ldif LDAP_P((
84 static int dosearch LDAP_P((
94 #define URLPRE "file:/tmp/"
96 static char *tmpdir = NULL;
97 static char *urlpre = NULL;
99 static char *binddn = NULL;
100 static char *passwd = NULL;
101 static char *base = NULL;
102 static char *ldaphost = NULL;
103 static int ldapport = 0;
104 static char *sep = DEFSEP;
105 static char *sortattr = NULL;
106 static int skipsortattr = 0;
107 static int verbose, not, includeufn, binary, vals2tmp, ldif;
110 main( int argc, char **argv )
112 char *infile, *filtpattern, **attrs, line[ BUFSIZ ];
114 int rc, i, first, scope, deref, attrsonly, manageDSAit;
115 int referrals, timelimit, sizelimit, debug;
116 int authmethod, version, want_bindpw;
120 debug = verbose = binary = not = vals2tmp =
121 attrsonly = manageDSAit = ldif = want_bindpw = 0;
123 deref = referrals = sizelimit = timelimit = version = -1;
125 scope = LDAP_SCOPE_SUBTREE;
126 authmethod = LDAP_AUTH_SIMPLE;
128 while (( i = getopt( argc, argv,
129 "WKknuvtMRABLD:s:f:h:b:d:P:p:F:a:w:l:z:S:T:V:")) != EOF )
132 case 'n': /* do Not do any searches */
135 case 'v': /* verbose mode */
139 debug |= atoi( optarg );
141 case 'k': /* use kerberos bind */
143 authmethod = LDAP_AUTH_KRBV4;
145 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
148 case 'K': /* use kerberos bind, 1st part only */
150 authmethod = LDAP_AUTH_KRBV41;
152 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
155 case 'u': /* include UFN */
158 case 't': /* write attribute values to /tmp files */
162 /* enable Manage DSA IT */
165 case 'R': /* don't automatically chase referrals */
166 referrals = (int) LDAP_OPT_OFF;
168 case 'A': /* retrieve attribute names only -- no values */
171 case 'L': /* print entries in LDIF format */
173 /* fall through -- always allow binary when outputting LDIF */
174 case 'B': /* allow binary values to be printed */
177 case 's': /* search scope */
178 if ( strcasecmp( optarg, "base" ) == 0 ) {
179 scope = LDAP_SCOPE_BASE;
180 } else if ( strcasecmp( optarg, "one" ) == 0 ) {
181 scope = LDAP_SCOPE_ONELEVEL;
182 } else if ( strcasecmp( optarg, "sub" ) == 0 ) {
183 scope = LDAP_SCOPE_SUBTREE;
185 fprintf( stderr, "scope should be base, one, or sub\n" );
190 case 'a': /* set alias deref option */
191 if ( strcasecmp( optarg, "never" ) == 0 ) {
192 deref = LDAP_DEREF_NEVER;
193 } else if ( strcasecmp( optarg, "search" ) == 0 ) {
194 deref = LDAP_DEREF_SEARCHING;
195 } else if ( strcasecmp( optarg, "find" ) == 0 ) {
196 deref = LDAP_DEREF_FINDING;
197 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
198 deref = LDAP_DEREF_ALWAYS;
200 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
205 case 'T': /* field separator */
206 if( tmpdir ) free( tmpdir );
207 tmpdir = strdup( optarg );
209 case 'V': /* field separator */
210 if( urlpre ) free( urlpre );
211 urlpre = strdup( optarg );
213 case 'F': /* field separator */
214 sep = strdup( optarg );
216 case 'f': /* input file */
217 infile = strdup( optarg );
219 case 'h': /* ldap host */
220 ldaphost = strdup( optarg );
222 case 'b': /* searchbase */
223 base = strdup( optarg );
225 case 'D': /* bind DN */
226 binddn = strdup( optarg );
228 case 'p': /* ldap port */
229 ldapport = atoi( optarg );
231 case 'w': /* bind password */
232 passwd = strdup( optarg );
236 for( p = optarg; *p == '\0'; p++ ) {
241 case 'l': /* time limit */
242 timelimit = atoi( optarg );
244 case 'z': /* size limit */
245 sizelimit = atoi( optarg );
247 case 'S': /* sort attribute */
248 sortattr = strdup( optarg );
254 switch( atoi( optarg ) )
257 version = LDAP_VERSION2;
260 version = LDAP_VERSION3;
263 fprintf( stderr, "protocol version should be 2 or 3\n" );
272 if ( argc - optind < 1 ) {
276 filtpattern = strdup( argv[ optind ] );
278 if ( argv[ optind + 1 ] == NULL ) {
280 } else if ( sortattr == NULL || *sortattr == '\0' ) {
281 attrs = &argv[ optind + 1 ];
283 for ( i = optind + 1; i < argc; i++ ) {
284 if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
290 argv[ optind ] = sortattr;
294 attrs = &argv[ optind ];
297 if ( infile != NULL ) {
298 if ( infile[0] == '-' && infile[1] == '\0' ) {
300 } else if (( fp = fopen( infile, "r" )) == NULL ) {
307 && (tmpdir = getenv("TMPDIR")) == NULL
308 && (tmpdir = getenv("TMP")) == NULL
309 && (tmpdir = getenv("TEMP")) == NULL )
314 if( urlpre == NULL ) {
315 urlpre = malloc( sizeof("file:///") + strlen(tmpdir) );
317 if( urlpre == NULL ) {
322 sprintf( urlpre, "file:///%s/",
323 tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
325 /* urlpre should be URLized.... */
329 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
330 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
332 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
333 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
339 (void) SIGNAL( SIGPIPE, SIG_IGN );
344 (ldapport ? "ldap_init( %s, %d )\n" : "ldap_init( %s, <DEFAULT> )\n"),
345 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
349 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
350 perror( "ldap_init" );
351 return( EXIT_FAILURE );
355 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
357 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
359 if (timelimit != -1 &&
360 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
362 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
364 if (sizelimit != -1 &&
365 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
367 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
369 if (referrals != -1 &&
370 ldap_set_option( ld, LDAP_OPT_REFERRALS,
371 (referrals ? LDAP_OPT_ON : LDAP_OPT_OFF) ) != LDAP_OPT_SUCCESS )
373 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
374 referrals ? "on" : "off" );
378 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS )
380 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
384 passwd = getpass("Enter LDAP Password: ");
387 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
388 ldap_perror( ld, "ldap_bind" );
389 return( EXIT_FAILURE );
395 LDAPControl *ctrls[2];
399 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
400 c.ldctl_value.bv_val = NULL;
401 c.ldctl_value.bv_len = 0;
402 c.ldctl_iscritical = manageDSAit > 1;
404 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
406 if( err != LDAP_OPT_SUCCESS ) {
407 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
408 if( c.ldctl_iscritical ) {
409 exit( EXIT_FAILURE );
415 fprintf( stderr, "filter%s: %s\nreturning: ",
416 infile != NULL ? " pattern" : "",
419 if ( attrs == NULL ) {
420 fprintf( stderr, "ALL" );
422 for ( i = 0; attrs[ i ] != NULL; ++i ) {
423 fprintf( stderr, "%s ", attrs[ i ] );
426 fprintf( stderr, "\n" );
431 printf( "version: 1\n\n");
435 printf( "#\n# filter%s: %s\n# returning: ",
436 infile != NULL ? " pattern" : "",
439 if ( attrs == NULL ) {
442 for ( i = 0; attrs[ i ] != NULL; ++i ) {
443 printf( "%s ", attrs[ i ] );
450 if ( infile == NULL ) {
451 rc = dosearch( ld, base, scope, attrs, attrsonly, NULL, filtpattern );
456 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
457 line[ strlen( line ) - 1 ] = '\0';
463 rc = dosearch( ld, base, scope, attrs, attrsonly,
485 char filter[ BUFSIZ ];
486 int rc, first, matches;
487 LDAPMessage *res, *e;
489 if( filtpatt != NULL ) {
490 sprintf( filter, filtpatt, value );
493 fprintf( stderr, "filter is: (%s)\n", filter );
497 printf( "#\n# filter: %s\n#\n", filter );
501 sprintf( filter, "%s", value );
505 return( LDAP_SUCCESS );
508 if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
510 ldap_perror( ld, "ldap_search" );
512 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
519 while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
520 == LDAP_RES_SEARCH_ENTRY ) {
522 e = ldap_first_entry( ld, res );
528 print_entry( ld, e, attrsonly );
533 ldap_perror( ld, "ldap_result" );
536 if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
537 ldap_perror( ld, "ldap_search" );
539 if ( sortattr != NULL ) {
540 (void) ldap_sort_entries( ld, &res,
541 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
544 for ( e = ldap_first_entry( ld, res ); e != NULL;
545 e = ldap_next_entry( ld, e ) ) {
552 print_entry( ld, e, attrsonly );
557 printf( "%d matches\n", matches );
572 char tmpfname[ 256 ];
575 BerElement *ber = NULL;
576 struct berval **bvals;
579 dn = ldap_get_dn( ld, entry );
583 ufn = ldap_dn2ufn( dn );
584 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, strlen( ufn ));
587 write_ldif( LDIF_PUT_VALUE, "dn", dn, strlen( dn ));
589 printf( "%s\n", dn );
594 ufn = ldap_dn2ufn( dn );
597 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, strlen( ufn ));
599 printf( "%s\n", ufn );
603 if( ufn != NULL ) ldap_memfree( ufn );
606 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
607 a = ldap_next_attribute( ld, entry, ber ) )
609 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
615 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
620 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
621 for ( i = 0; bvals[i] != NULL; i++ ) {
622 if ( vals2tmp > 1 || ( vals2tmp
623 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
625 /* write value to file */
626 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
630 if ( mktemp( tmpfname ) == NULL ) {
635 if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
640 if ( fwrite( bvals[ i ]->bv_val,
641 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
650 sprintf( url, "%s%s", urlpre,
651 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
654 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
656 printf( "%s%s%s\n", a, sep, url );
662 write_ldif( LDIF_PUT_VALUE, a,
663 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
666 int notprint = !binary && !vals2tmp
667 && ldif_is_not_printable( bvals[i]->bv_val,
669 printf( "%s%s", a, sep );
670 puts( notprint ? "NOT PRINTABLE" : bvals[ i ]->bv_val );
674 ber_bvecfree( bvals );
685 write_ldif( int type, char *name, char *value, ber_len_t vallen )
689 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
693 fputs( ldif, stdout );