10 #include <ac/unistd.h>
15 #include "ldap_defaults.h"
23 "usage: %s [options] filter [attributes...]\nwhere:\n"
24 " filter\tRFC-1558 compliant LDAP search filter\n"
25 " attributes\twhitespace-separated list of attributes to retrieve\n"
26 "\t\t1.1 -- no attributes\n"
27 "\t\t* -- all user attributes\n"
28 "\t\t+ -- all operational attributes\n"
29 "\t\tempty list -- all non-operational attributes\n"
31 " -n\t\tshow what would be done but don't actually search\n"
32 " -v\t\trun in verbose mode (diagnostics to standard output)\n"
33 " -t\t\twrite binary values to files in TMPDIR\n"
34 " -tt\t\twrite all values to files in TMPDIR\n"
35 " -T path\twrite files to directory specified by path (default: \"/tmp\")\n"
36 " -V prefix\tURL prefix for files (default: \"file://tmp/\"\n"
37 " -u\t\tinclude User Friendly entry names in the output\n"
38 " -A\t\tretrieve attribute names only (no values)\n"
39 " -B\t\tdo not suppress printing of binary values\n"
40 " -F sep\tprint `sep' instead of `=' between attribute names and values\n"
41 " -L\t\tprint entries in LDIF format (implies -B)\n"
42 " -LL\t\tprint entries in LDIF format without comments\n"
43 " -LLL\t\tprint entries in LDIF format without comments and version\n"
44 " -M\t\tenable Manage DSA IT control (-MM to make critical)\n"
45 " -R\t\tdo not automatically follow referrals\n"
46 " -S attr\tsort the results by attribute `attr'\n"
47 " -d level\tset LDAP debugging level to `level'\n"
48 " -f file\tperform sequence of searches listed in `file'\n"
49 " -b basedn\tbase dn for search\n"
50 " -s scope\tone of base, one, or sub (search scope)\n"
51 " -a deref\tone of never, always, search, or find (alias dereferencing)\n"
52 " -l limit\ttime limit (in seconds) for search\n"
53 " -z limit\tsize limit (in entries) for search\n"
54 " -D binddn\tbind dn\n"
55 " -w passwd\tbind passwd (for simple authentication)\n"
56 " -W\t\tprompt for bind passwd\n"
58 " -k\t\tuse Kerberos instead of Simple Password authentication\n"
60 " -h host\tldap server\n"
61 " -p port\tport on ldap server\n"
62 " -P version\tprocotol version (2 or 3)\n"
68 static void print_entry LDAP_P((
73 static int write_ldif LDAP_P((
79 static int dosearch LDAP_P((
89 #define URLPRE "file:/tmp/"
91 static char *tmpdir = NULL;
92 static char *urlpre = NULL;
94 static char *binddn = NULL;
95 static char *passwd = NULL;
96 static char *base = NULL;
97 static char *ldaphost = NULL;
98 static int ldapport = 0;
99 static char *sep = DEFSEP;
100 static char *sortattr = NULL;
101 static int skipsortattr = 0;
102 static int verbose, not, includeufn, binary, vals2tmp, ldif;
105 main( int argc, char **argv )
107 char *infile, *filtpattern, **attrs, line[ BUFSIZ ];
109 int rc, i, first, scope, deref, attrsonly, manageDSAit;
110 int referrals, timelimit, sizelimit, debug;
111 int authmethod, version, want_bindpw;
115 debug = verbose = binary = not = vals2tmp =
116 attrsonly = manageDSAit = ldif = want_bindpw = 0;
118 deref = referrals = sizelimit = timelimit = version = -1;
120 scope = LDAP_SCOPE_SUBTREE;
121 authmethod = LDAP_AUTH_SIMPLE;
123 while (( i = getopt( argc, argv,
124 "WKknuvtMRABLD:s:f:h:b:d:P:p:F:a:w:l:z:S:T:V:")) != EOF )
127 case 'n': /* do Not do any searches */
130 case 'v': /* verbose mode */
134 debug |= atoi( optarg );
136 case 'k': /* use kerberos bind */
138 authmethod = LDAP_AUTH_KRBV4;
140 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
143 case 'K': /* use kerberos bind, 1st part only */
145 authmethod = LDAP_AUTH_KRBV41;
147 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
150 case 'u': /* include UFN */
153 case 't': /* write attribute values to /tmp files */
157 /* enable Manage DSA IT */
160 case 'R': /* don't automatically chase referrals */
161 referrals = (int) LDAP_OPT_OFF;
163 case 'A': /* retrieve attribute names only -- no values */
166 case 'L': /* print entries in LDIF format */
168 /* fall through -- always allow binary when outputting LDIF */
169 case 'B': /* allow binary values to be printed */
172 case 's': /* search scope */
173 if ( strcasecmp( optarg, "base" ) == 0 ) {
174 scope = LDAP_SCOPE_BASE;
175 } else if ( strcasecmp( optarg, "one" ) == 0 ) {
176 scope = LDAP_SCOPE_ONELEVEL;
177 } else if ( strcasecmp( optarg, "sub" ) == 0 ) {
178 scope = LDAP_SCOPE_SUBTREE;
180 fprintf( stderr, "scope should be base, one, or sub\n" );
185 case 'a': /* set alias deref option */
186 if ( strcasecmp( optarg, "never" ) == 0 ) {
187 deref = LDAP_DEREF_NEVER;
188 } else if ( strcasecmp( optarg, "search" ) == 0 ) {
189 deref = LDAP_DEREF_SEARCHING;
190 } else if ( strcasecmp( optarg, "find" ) == 0 ) {
191 deref = LDAP_DEREF_FINDING;
192 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
193 deref = LDAP_DEREF_ALWAYS;
195 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
200 case 'T': /* field separator */
201 if( tmpdir ) free( tmpdir );
202 tmpdir = strdup( optarg );
204 case 'V': /* field separator */
205 if( urlpre ) free( urlpre );
206 urlpre = strdup( optarg );
208 case 'F': /* field separator */
209 sep = strdup( optarg );
211 case 'f': /* input file */
212 infile = strdup( optarg );
214 case 'h': /* ldap host */
215 ldaphost = strdup( optarg );
217 case 'b': /* searchbase */
218 base = strdup( optarg );
220 case 'D': /* bind DN */
221 binddn = strdup( optarg );
223 case 'p': /* ldap port */
224 ldapport = atoi( optarg );
226 case 'w': /* bind password */
227 passwd = strdup( optarg );
231 for( p = optarg; *p == '\0'; p++ ) {
236 case 'l': /* time limit */
237 timelimit = atoi( optarg );
239 case 'z': /* size limit */
240 sizelimit = atoi( optarg );
242 case 'S': /* sort attribute */
243 sortattr = strdup( optarg );
249 switch( atoi( optarg ) )
252 version = LDAP_VERSION2;
255 version = LDAP_VERSION3;
258 fprintf( stderr, "protocol version should be 2 or 3\n" );
267 if ( argc - optind < 1 ) {
271 filtpattern = strdup( argv[ optind ] );
273 if ( argv[ optind + 1 ] == NULL ) {
275 } else if ( sortattr == NULL || *sortattr == '\0' ) {
276 attrs = &argv[ optind + 1 ];
278 for ( i = optind + 1; i < argc; i++ ) {
279 if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
285 argv[ optind ] = sortattr;
289 attrs = &argv[ optind ];
292 if ( infile != NULL ) {
293 if ( infile[0] == '-' && infile[1] == '\0' ) {
295 } else if (( fp = fopen( infile, "r" )) == NULL ) {
302 && (tmpdir = getenv("TMPDIR")) == NULL
303 && (tmpdir = getenv("TMP")) == NULL
304 && (tmpdir = getenv("TEMP")) == NULL )
309 if( urlpre == NULL ) {
310 urlpre = malloc( sizeof("file:///") + strlen(tmpdir) );
312 if( urlpre == NULL ) {
317 sprintf( urlpre, "file:///%s/",
318 tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
320 /* urlpre should be URLized.... */
324 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
325 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
327 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
328 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
334 (void) SIGNAL( SIGPIPE, SIG_IGN );
338 fprintf( stderr, "ldap_init( %s, %d )\n",
339 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
343 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
344 perror( "ldap_init" );
345 return( EXIT_FAILURE );
349 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
351 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
353 if (timelimit != -1 &&
354 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
356 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
358 if (sizelimit != -1 &&
359 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
361 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
363 if (referrals != -1 &&
364 ldap_set_option( ld, LDAP_OPT_REFERRALS,
365 (referrals ? LDAP_OPT_ON : LDAP_OPT_OFF) ) != LDAP_OPT_SUCCESS )
367 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
368 referrals ? "on" : "off" );
372 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS )
374 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
378 passwd = getpass("Enter LDAP Password: ");
381 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
382 ldap_perror( ld, "ldap_bind" );
383 return( EXIT_FAILURE );
389 LDAPControl *ctrls[2];
393 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
394 c.ldctl_value.bv_val = NULL;
395 c.ldctl_value.bv_len = 0;
396 c.ldctl_iscritical = manageDSAit > 1;
398 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
400 if( err != LDAP_OPT_SUCCESS ) {
401 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
402 if( c.ldctl_iscritical ) {
403 exit( EXIT_FAILURE );
409 fprintf( stderr, "filter%s: %s\nreturning: ",
410 infile != NULL ? " pattern" : "",
413 if ( attrs == NULL ) {
416 for ( i = 0; attrs[ i ] != NULL; ++i ) {
417 fprintf( stderr, "%s ", attrs[ i ] );
420 fprintf( stderr, "\n" );
425 /* TEMPORARILY put the version in a comment many tools cannot yet a version attribute */
426 printf( "version: 1\n\n");
430 printf( "#\n# filter%s: %s\n# returning: ",
431 infile != NULL ? " pattern" : "",
434 if ( attrs == NULL ) {
437 for ( i = 0; attrs[ i ] != NULL; ++i ) {
438 printf( "%s ", attrs[ i ] );
445 if ( infile == NULL ) {
446 rc = dosearch( ld, base, scope, attrs, attrsonly, NULL, filtpattern );
451 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
452 line[ strlen( line ) - 1 ] = '\0';
458 rc = dosearch( ld, base, scope, attrs, attrsonly,
480 char filter[ BUFSIZ ];
481 int rc, first, matches;
482 LDAPMessage *res, *e;
484 if( filtpatt != NULL ) {
485 sprintf( filter, filtpatt, value );
488 fprintf( stderr, "filter is: (%s)\n", filter );
492 printf( "#\n# filter: %s\n#\n", filter );
496 sprintf( filter, "%s", value );
500 return( LDAP_SUCCESS );
503 if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
505 ldap_perror( ld, "ldap_search" );
507 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
514 while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
515 == LDAP_RES_SEARCH_ENTRY ) {
517 e = ldap_first_entry( ld, res );
523 print_entry( ld, e, attrsonly );
528 ldap_perror( ld, "ldap_result" );
531 if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
532 ldap_perror( ld, "ldap_search" );
534 if ( sortattr != NULL ) {
535 (void) ldap_sort_entries( ld, &res,
536 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
539 for ( e = ldap_first_entry( ld, res ); e != NULL;
540 e = ldap_next_entry( ld, e ) ) {
547 print_entry( ld, e, attrsonly );
552 printf( "%d matches\n", matches );
566 char tmpfname[ 256 ];
569 BerElement *ber = NULL;
570 struct berval **bvals;
573 dn = ldap_get_dn( ld, entry );
577 ufn = ldap_dn2ufn( dn );
578 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, strlen( ufn ));
581 write_ldif( LDIF_PUT_VALUE, "dn", dn, strlen( dn ));
583 printf( "%s\n", dn );
588 ufn = ldap_dn2ufn( dn );
591 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, strlen( ufn ));
593 printf( "%s\n", ufn );
597 if( ufn != NULL ) ldap_memfree( ufn );
600 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
601 a = ldap_next_attribute( ld, entry, ber ) )
603 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
609 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
614 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
615 for ( i = 0; bvals[i] != NULL; i++ ) {
616 if ( vals2tmp > 1 || ( vals2tmp
617 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
619 /* write value to file */
620 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
624 if ( mktemp( tmpfname ) == NULL ) {
629 if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
634 if ( fwrite( bvals[ i ]->bv_val,
635 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
644 sprintf( url, "%s%s", urlpre,
645 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
648 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
650 printf( "%s%s%s\n", a, sep, url );
656 write_ldif( LDIF_PUT_VALUE, a,
657 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
660 int notprint = !binary && !vals2tmp
661 && ldif_is_not_printable( bvals[i]->bv_val,
663 printf( "%s%s", a, sep );
664 puts( notprint ? "NOT PRINTABLE" : bvals[ i ]->bv_val );
669 ber_bvecfree( bvals );
680 write_ldif( int type, char *name, char *value, ber_len_t vallen )
684 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
688 fputs( ldif, stdout );