3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/stdlib.h>
14 #include <ac/signal.h>
15 #include <ac/string.h>
16 #include <ac/unistd.h>
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
33 #include "ldap_defaults.h"
38 usage( const char *s )
41 "usage: %s [options] filter [attributes...]\nwhere:\n"
42 " filter\tRFC-1558 compliant LDAP search filter\n"
43 " attributes\twhitespace-separated list of attributes to retrieve\n"
44 "\t\t1.1 -- no attributes\n"
45 "\t\t* -- all user attributes\n"
46 "\t\t+ -- all operational attributes\n"
47 "\t\tempty list -- all non-operational attributes\n"
49 " -a deref\tone of `never', `always', `search', or `find' (alias\n"
51 " -A\t\tretrieve attribute names only (no values)\n"
52 " -b basedn\tbase dn for search\n"
53 " -B\t\tdo not suppress printing of binary values\n"
54 " -d level\tset LDAP debugging level to `level'\n"
55 " -D binddn\tbind DN\n"
56 " -E\t\trequest SASL privacy (-EE to make it critical)\n"
57 " -f file\t\tperform sequence of searches listed in `file'\n"
58 " -F sep\t\tprint `sep' instead of `=' between attribute names and\n"
60 " -h host\t\tLDAP server\n"
61 " -I\t\trequest SASL integrity checking (-II to make it\n"
63 " -k\t\tuse Kerberos authentication\n"
64 " -K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
65 " -l limit\ttime limit (in seconds) for search\n"
66 " -L\t\tprint entries in LDIF format (implies -B)\n"
67 " -LL\t\tprint entries in LDIF format without comments\n"
68 " -LLL\t\tprint entries in LDIF format without comments and\n"
70 " -M\t\tenable Manage DSA IT control (-MM to make critical)\n"
71 " -n\t\tshow what would be done but don't actually search\n"
72 " -p port\t\tport on LDAP server\n"
73 " -P version\tprocotol version (2 or 3)\n"
74 " -R\t\tdo not automatically follow referrals\n"
75 " -s scope\tone of base, one, or sub (search scope)\n"
76 " -S attr\t\tsort the results by attribute `attr'\n"
77 " -t\t\twrite binary values to files in TMPDIR\n"
78 " -tt\t\twrite all values to files in TMPDIR\n"
79 " -T path\t\twrite files to directory specified by path (default:\n"
81 " -u\t\tinclude User Friendly entry names in the output\n"
82 " -U user\t\tSASL authentication identity (username)\n"
83 " -v\t\trun in verbose mode (diagnostics to standard output)\n"
84 " -V prefix\tURL prefix for files (default: \"file://tmp/\")\n"
85 " -w passwd\tbind passwd (for simple authentication)\n"
86 " -W\t\tprompt for bind passwd\n"
87 " -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
88 " -Y mech\t\tSASL mechanism\n"
89 " -z limit\tsize limit (in entries) for search\n"
90 " -Z\t\trequest the use of TLS (-ZZ to make it critical)\n"
96 static void print_entry LDAP_P((
101 static int write_ldif LDAP_P((
107 static int dosearch LDAP_P((
116 #define TMPDIR "/tmp"
117 #define URLPRE "file:/tmp/"
119 static char *tmpdir = NULL;
120 static char *urlpre = NULL;
122 static char *binddn = NULL;
123 static char *passwd = NULL;
124 static char *base = NULL;
125 static char *ldaphost = NULL;
126 static int ldapport = 0;
127 #ifdef HAVE_CYRUS_SASL
128 static char *sasl_authc_id = NULL;
129 static char *sasl_authz_id = NULL;
130 static char *sasl_mech = NULL;
131 static int sasl_integrity = 0;
132 static int sasl_privacy = 0;
134 static int use_tls = 0;
135 static char *sep = DEFSEP;
136 static char *sortattr = NULL;
137 static int skipsortattr = 0;
138 static int verbose, not, includeufn, binary, vals2tmp, ldif;
141 main( int argc, char **argv )
143 char *infile, *filtpattern, **attrs, line[ BUFSIZ ];
145 int rc, i, first, scope, deref, attrsonly, manageDSAit;
146 int referrals, timelimit, sizelimit, debug;
147 int authmethod, version, want_bindpw;
151 debug = verbose = binary = not = vals2tmp =
152 attrsonly = manageDSAit = ldif = want_bindpw = 0;
154 deref = referrals = sizelimit = timelimit = version = -1;
156 scope = LDAP_SCOPE_SUBTREE;
157 authmethod = LDAP_AUTH_SIMPLE;
159 while (( i = getopt( argc, argv,
160 "Aa:Bb:D:d:EF:f:h:IKkLl:MnP:p:RS:s:T:tU:uV:vWw:X:Y:Zz:")) != EOF )
163 case 'n': /* do Not do any searches */
166 case 'v': /* verbose mode */
170 debug |= atoi( optarg );
172 case 'k': /* use kerberos bind */
173 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
174 authmethod = LDAP_AUTH_KRBV4;
176 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
177 return( EXIT_FAILURE );
180 case 'K': /* use kerberos bind, 1st part only */
181 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
182 authmethod = LDAP_AUTH_KRBV41;
184 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
185 return( EXIT_FAILURE );
189 case 'u': /* include UFN */
192 case 't': /* write attribute values to /tmp files */
196 /* enable Manage DSA IT */
199 case 'R': /* don't automatically chase referrals */
200 referrals = (int) LDAP_OPT_OFF;
202 case 'A': /* retrieve attribute names only -- no values */
205 case 'L': /* print entries in LDIF format */
207 /* fall through -- always allow binary when outputting LDIF */
208 case 'B': /* allow binary values to be printed */
211 case 's': /* search scope */
212 if ( strcasecmp( optarg, "base" ) == 0 ) {
213 scope = LDAP_SCOPE_BASE;
214 } else if ( strcasecmp( optarg, "one" ) == 0 ) {
215 scope = LDAP_SCOPE_ONELEVEL;
216 } else if ( strcasecmp( optarg, "sub" ) == 0 ) {
217 scope = LDAP_SCOPE_SUBTREE;
219 fprintf( stderr, "scope should be base, one, or sub\n" );
224 case 'a': /* set alias deref option */
225 if ( strcasecmp( optarg, "never" ) == 0 ) {
226 deref = LDAP_DEREF_NEVER;
227 } else if ( strcasecmp( optarg, "search" ) == 0 ) {
228 deref = LDAP_DEREF_SEARCHING;
229 } else if ( strcasecmp( optarg, "find" ) == 0 ) {
230 deref = LDAP_DEREF_FINDING;
231 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
232 deref = LDAP_DEREF_ALWAYS;
234 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
239 case 'T': /* field separator */
240 if( tmpdir ) free( tmpdir );
241 tmpdir = strdup( optarg );
243 case 'V': /* field separator */
244 if( urlpre ) free( urlpre );
245 urlpre = strdup( optarg );
247 case 'F': /* field separator */
248 sep = strdup( optarg );
250 case 'f': /* input file */
251 infile = strdup( optarg );
253 case 'h': /* ldap host */
254 ldaphost = strdup( optarg );
256 case 'b': /* searchbase */
257 base = strdup( optarg );
259 case 'D': /* bind DN */
260 binddn = strdup( optarg );
262 case 'p': /* ldap port */
263 ldapport = atoi( optarg );
265 case 'w': /* bind password */
266 passwd = strdup( optarg );
270 for( p = optarg; *p == '\0'; p++ ) {
275 case 'l': /* time limit */
276 timelimit = atoi( optarg );
278 case 'z': /* size limit */
279 sizelimit = atoi( optarg );
281 case 'S': /* sort attribute */
282 sortattr = strdup( optarg );
288 switch( atoi( optarg ) )
291 version = LDAP_VERSION2;
294 version = LDAP_VERSION3;
297 fprintf( stderr, "protocol version should be 2 or 3\n" );
302 #ifdef HAVE_CYRUS_SASL
304 authmethod = LDAP_AUTH_SASL;
306 fprintf( stderr, "%s was not compiled with SASL support\n",
308 return( EXIT_FAILURE );
312 #ifdef HAVE_CYRUS_SASL
314 authmethod = LDAP_AUTH_SASL;
316 fprintf( stderr, "%s was not compiled with SASL support\n",
318 return( EXIT_FAILURE );
322 #ifdef HAVE_CYRUS_SASL
323 if ( strcasecmp( optarg, "any" ) && strcmp( optarg, "*" ) ) {
324 sasl_mech = strdup( optarg );
326 authmethod = LDAP_AUTH_SASL;
328 fprintf( stderr, "%s was not compiled with SASL support\n",
330 return( EXIT_FAILURE );
334 #ifdef HAVE_CYRUS_SASL
335 sasl_authc_id = strdup( optarg );
336 authmethod = LDAP_AUTH_SASL;
338 fprintf( stderr, "%s was not compiled with SASL support\n",
340 return( EXIT_FAILURE );
344 #ifdef HAVE_CYRUS_SASL
345 sasl_authz_id = strdup( optarg );
346 authmethod = LDAP_AUTH_SASL;
348 fprintf( stderr, "%s was not compiled with SASL support\n",
350 return( EXIT_FAILURE );
357 fprintf( stderr, "%s was not compiled with TLS support\n",
359 return( EXIT_FAILURE );
368 /* no alternative format */
369 if( ldif < 1 ) ldif = 1;
372 if ( ( authmethod == LDAP_AUTH_KRBV4 ) || ( authmethod ==
373 LDAP_AUTH_KRBV41 ) ) {
374 if( version > LDAP_VERSION2 ) {
375 fprintf( stderr, "Kerberos requires LDAPv2\n" );
376 return( EXIT_FAILURE );
378 version = LDAP_VERSION2;
380 else if ( authmethod == LDAP_AUTH_SASL ) {
381 if( version != -1 || version != LDAP_VERSION3 ) {
382 fprintf( stderr, "SASL requires LDAPv3\n" );
383 return( EXIT_FAILURE );
385 version = LDAP_VERSION3;
389 if( version != -1 || version != LDAP_VERSION3 ) {
390 fprintf(stderr, "manage DSA control requires LDAPv3\n");
393 version = LDAP_VERSION3;
397 if( version != -1 || version != LDAP_VERSION3 ) {
398 fprintf(stderr, "Start TLS requires LDAPv3\n");
401 version = LDAP_VERSION3;
404 if ( argc - optind < 1 ) {
408 filtpattern = strdup( argv[ optind ] );
410 if ( argv[ optind + 1 ] == NULL ) {
412 } else if ( sortattr == NULL || *sortattr == '\0' ) {
413 attrs = &argv[ optind + 1 ];
415 for ( i = optind + 1; i < argc; i++ ) {
416 if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
422 argv[ optind ] = sortattr;
426 attrs = &argv[ optind ];
429 if ( infile != NULL ) {
430 if ( infile[0] == '-' && infile[1] == '\0' ) {
432 } else if (( fp = fopen( infile, "r" )) == NULL ) {
439 && (tmpdir = getenv("TMPDIR")) == NULL
440 && (tmpdir = getenv("TMP")) == NULL
441 && (tmpdir = getenv("TEMP")) == NULL )
446 if( urlpre == NULL ) {
447 urlpre = malloc( sizeof("file:///") + strlen(tmpdir) );
449 if( urlpre == NULL ) {
454 sprintf( urlpre, "file:///%s/",
455 tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
457 /* urlpre should be URLized.... */
461 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
462 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
464 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
465 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
471 (void) SIGNAL( SIGPIPE, SIG_IGN );
476 (ldapport ? "ldap_init( %s, %d )\n" : "ldap_init( %s, <DEFAULT> )\n"),
477 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
481 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
482 perror( "ldap_init" );
483 return( EXIT_FAILURE );
487 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
489 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
491 if (timelimit != -1 &&
492 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
494 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
496 if (sizelimit != -1 &&
497 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
499 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
501 if (referrals != -1 &&
502 ldap_set_option( ld, LDAP_OPT_REFERRALS,
503 (referrals ? LDAP_OPT_ON : LDAP_OPT_OFF) ) != LDAP_OPT_SUCCESS )
505 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
506 referrals ? "on" : "off" );
510 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS )
512 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
515 if ( use_tls && ldap_start_tls( ld, NULL, NULL ) != LDAP_SUCCESS ) {
517 ldap_perror( ld, "ldap_start_tls" );
518 return( EXIT_FAILURE );
523 passwd = getpass("Enter LDAP Password: ");
526 if ( authmethod == LDAP_AUTH_SASL ) {
527 #ifdef HAVE_CYRUS_SASL
528 int minssf = 0, maxssf = 0;
530 if ( sasl_integrity > 0 )
532 if ( sasl_integrity > 1 )
534 if ( sasl_privacy > 0 )
535 maxssf = 100000; /* Something big value */
536 if ( sasl_privacy > 1 )
539 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
540 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
541 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
543 return( EXIT_FAILURE );
545 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
546 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
547 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MAXSSF"
549 return( EXIT_FAILURE );
552 if ( ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
553 sasl_authz_id, sasl_mech, NULL, NULL, NULL )
555 ldap_perror( ld, "ldap_sasl_bind" );
556 return( EXIT_FAILURE );
559 fprintf( stderr, "%s was not compiled with SASL support\n",
561 return( EXIT_FAILURE );
565 if ( ldap_bind_s( ld, binddn, passwd, authmethod )
567 ldap_perror( ld, "ldap_bind" );
568 return( EXIT_FAILURE );
575 LDAPControl *ctrls[2];
579 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
580 c.ldctl_value.bv_val = NULL;
581 c.ldctl_value.bv_len = 0;
582 c.ldctl_iscritical = manageDSAit > 1;
584 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
586 if( err != LDAP_OPT_SUCCESS ) {
587 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
588 if( c.ldctl_iscritical ) {
589 exit( EXIT_FAILURE );
595 fprintf( stderr, "filter%s: %s\nreturning: ",
596 infile != NULL ? " pattern" : "",
599 if ( attrs == NULL ) {
600 fprintf( stderr, "ALL" );
602 for ( i = 0; attrs[ i ] != NULL; ++i ) {
603 fprintf( stderr, "%s ", attrs[ i ] );
606 fprintf( stderr, "\n" );
611 printf( "version: 1\n\n");
615 printf( "#\n# filter%s: %s\n# returning: ",
616 infile != NULL ? " pattern" : "",
619 if ( attrs == NULL ) {
622 for ( i = 0; attrs[ i ] != NULL; ++i ) {
623 printf( "%s ", attrs[ i ] );
630 if ( infile == NULL ) {
631 rc = dosearch( ld, base, scope, attrs, attrsonly, NULL, filtpattern );
636 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
637 line[ strlen( line ) - 1 ] = '\0';
643 rc = dosearch( ld, base, scope, attrs, attrsonly,
665 char filter[ BUFSIZ ];
666 int rc, first, matches;
667 LDAPMessage *res, *e;
669 if( filtpatt != NULL ) {
670 sprintf( filter, filtpatt, value );
673 fprintf( stderr, "filter is: (%s)\n", filter );
677 printf( "#\n# filter: %s\n#\n", filter );
681 sprintf( filter, "%s", value );
685 return( LDAP_SUCCESS );
688 if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
690 ldap_perror( ld, "ldap_search" );
692 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
699 while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
700 == LDAP_RES_SEARCH_ENTRY ) {
702 e = ldap_first_entry( ld, res );
708 print_entry( ld, e, attrsonly );
713 ldap_perror( ld, "ldap_result" );
716 if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
717 ldap_perror( ld, "ldap_search" );
719 if ( sortattr != NULL ) {
720 (void) ldap_sort_entries( ld, &res,
721 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
724 for ( e = ldap_first_entry( ld, res ); e != NULL;
725 e = ldap_next_entry( ld, e ) ) {
732 print_entry( ld, e, attrsonly );
737 printf( "%d matches\n", matches );
752 char tmpfname[ 256 ];
755 BerElement *ber = NULL;
756 struct berval **bvals;
759 dn = ldap_get_dn( ld, entry );
763 ufn = ldap_dn2ufn( dn );
764 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, strlen( ufn ));
767 write_ldif( LDIF_PUT_VALUE, "dn", dn, strlen( dn ));
769 printf( "%s\n", dn );
774 ufn = ldap_dn2ufn( dn );
777 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, strlen( ufn ));
779 printf( "%s\n", ufn );
783 if( ufn != NULL ) ldap_memfree( ufn );
786 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
787 a = ldap_next_attribute( ld, entry, ber ) )
789 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
795 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
800 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
801 for ( i = 0; bvals[i] != NULL; i++ ) {
802 if ( vals2tmp > 1 || ( vals2tmp
803 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
806 /* write value to file */
807 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
811 if ( mktemp( tmpfname ) == NULL ) {
816 if (( tmpfd = open( tmpfname, O_WRONLY|O_CREAT|O_EXCL, 0600 )) == -1 ) {
821 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
826 if ( fwrite( bvals[ i ]->bv_val,
827 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
836 sprintf( url, "%s%s", urlpre,
837 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
840 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
842 printf( "%s%s%s\n", a, sep, url );
848 write_ldif( LDIF_PUT_VALUE, a,
849 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
852 int notprint = !binary && !vals2tmp
853 && ldif_is_not_printable( bvals[i]->bv_val,
855 printf( "%s%s", a, sep );
856 puts( notprint ? "NOT PRINTABLE" : bvals[ i ]->bv_val );
860 ber_bvecfree( bvals );
871 write_ldif( int type, char *name, char *value, ber_len_t vallen )
875 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
879 fputs( ldif, stdout );