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>
25 #include "ldap_defaults.h"
30 usage( const char *s )
33 "usage: %s [options] filter [attributes...]\nwhere:\n"
34 " filter\tRFC-1558 compliant LDAP search filter\n"
35 " attributes\twhitespace-separated list of attributes to retrieve\n"
36 "\t\t1.1 -- no attributes\n"
37 "\t\t* -- all user attributes\n"
38 "\t\t+ -- all operational attributes\n"
39 "\t\tempty list -- all non-operational attributes\n"
41 " -a deref\tone of `never', `always', `search', or `find' (alias\n"
43 " -A\t\tretrieve attribute names only (no values)\n"
44 " -b basedn\tbase dn for search\n"
45 " -B\t\tdo not suppress printing of binary values\n"
46 " -d level\tset LDAP debugging level to `level'\n"
47 " -D binddn\tbind DN\n"
48 " -E\t\trequest SASL privacy (-EE to make it critical)\n"
49 " -f file\t\tperform sequence of searches listed in `file'\n"
50 " -F sep\t\tprint `sep' instead of `=' between attribute names and\n"
52 " -h host\t\tLDAP server\n"
53 " -I\t\trequest SASL integrity checking (-II to make it\n"
55 " -k\t\tuse Kerberos authentication\n"
56 " -K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
57 " -l limit\ttime limit (in seconds) for search\n"
58 " -L\t\tprint entries in LDIF format (implies -B)\n"
59 " -LL\t\tprint entries in LDIF format without comments\n"
60 " -LLL\t\tprint entries in LDIF format without comments and\n"
62 " -M\t\tenable Manage DSA IT control (-MM to make critical)\n"
63 " -n\t\tshow what would be done but don't actually search\n"
64 " -p port\t\tport on LDAP server\n"
65 " -P version\tprocotol version (2 or 3)\n"
66 " -R\t\tdo not automatically follow referrals\n"
67 " -s scope\tone of base, one, or sub (search scope)\n"
68 " -S attr\t\tsort the results by attribute `attr'\n"
69 " -t\t\twrite binary values to files in TMPDIR\n"
70 " -tt\t\twrite all values to files in TMPDIR\n"
71 " -T path\t\twrite files to directory specified by path (default:\n"
73 " -u\t\tinclude User Friendly entry names in the output\n"
74 " -U user\t\tSASL authentication identity (username)\n"
75 " -v\t\trun in verbose mode (diagnostics to standard output)\n"
76 " -V prefix\tURL prefix for files (default: \"file://tmp/\")\n"
77 " -w passwd\tbind passwd (for simple authentication)\n"
78 " -W\t\tprompt for bind passwd\n"
79 " -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
80 " -Y mech\t\tSASL mechanism\n"
81 " -z limit\tsize limit (in entries) for search\n"
82 " -Z\t\trequest the use of TLS (-ZZ to make it critical)\n"
88 static void print_entry LDAP_P((
93 static int write_ldif LDAP_P((
99 static int dosearch LDAP_P((
108 #define TMPDIR "/tmp"
109 #define URLPRE "file:/tmp/"
111 static char *tmpdir = NULL;
112 static char *urlpre = NULL;
114 static char *binddn = NULL;
115 static char *passwd = NULL;
116 static char *base = NULL;
117 static char *ldaphost = NULL;
118 static int ldapport = 0;
119 #ifdef HAVE_CYRUS_SASL
120 static char *sasl_authc_id = NULL;
121 static char *sasl_authz_id = NULL;
122 static char *sasl_mech = NULL;
123 static int sasl_integrity = 0;
124 static int sasl_privacy = 0;
126 static int use_tls = 0;
127 static char *sep = DEFSEP;
128 static char *sortattr = NULL;
129 static int skipsortattr = 0;
130 static int verbose, not, includeufn, binary, vals2tmp, ldif;
133 main( int argc, char **argv )
135 char *infile, *filtpattern, **attrs, line[ BUFSIZ ];
137 int rc, i, first, scope, deref, attrsonly, manageDSAit;
138 int referrals, timelimit, sizelimit, debug;
139 int authmethod, version, want_bindpw;
143 debug = verbose = binary = not = vals2tmp =
144 attrsonly = manageDSAit = ldif = want_bindpw = 0;
146 deref = referrals = sizelimit = timelimit = version = -1;
148 scope = LDAP_SCOPE_SUBTREE;
149 authmethod = LDAP_AUTH_SIMPLE;
151 while (( i = getopt( argc, argv,
152 "Aa:Bb:D:d:EF:f:h:IKkLl:MnP:p:RS:s:T:tU:uV:vWw:X:Y:Zz:")) != EOF )
155 case 'n': /* do Not do any searches */
158 case 'v': /* verbose mode */
162 debug |= atoi( optarg );
164 case 'k': /* use kerberos bind */
165 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
166 authmethod = LDAP_AUTH_KRBV4;
168 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
169 return( EXIT_FAILURE );
172 case 'K': /* use kerberos bind, 1st part only */
173 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
174 authmethod = LDAP_AUTH_KRBV41;
176 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
177 return( EXIT_FAILURE );
181 case 'u': /* include UFN */
184 case 't': /* write attribute values to /tmp files */
188 /* enable Manage DSA IT */
191 case 'R': /* don't automatically chase referrals */
192 referrals = (int) LDAP_OPT_OFF;
194 case 'A': /* retrieve attribute names only -- no values */
197 case 'L': /* print entries in LDIF format */
199 /* fall through -- always allow binary when outputting LDIF */
200 case 'B': /* allow binary values to be printed */
203 case 's': /* search scope */
204 if ( strcasecmp( optarg, "base" ) == 0 ) {
205 scope = LDAP_SCOPE_BASE;
206 } else if ( strcasecmp( optarg, "one" ) == 0 ) {
207 scope = LDAP_SCOPE_ONELEVEL;
208 } else if ( strcasecmp( optarg, "sub" ) == 0 ) {
209 scope = LDAP_SCOPE_SUBTREE;
211 fprintf( stderr, "scope should be base, one, or sub\n" );
216 case 'a': /* set alias deref option */
217 if ( strcasecmp( optarg, "never" ) == 0 ) {
218 deref = LDAP_DEREF_NEVER;
219 } else if ( strcasecmp( optarg, "search" ) == 0 ) {
220 deref = LDAP_DEREF_SEARCHING;
221 } else if ( strcasecmp( optarg, "find" ) == 0 ) {
222 deref = LDAP_DEREF_FINDING;
223 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
224 deref = LDAP_DEREF_ALWAYS;
226 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
231 case 'T': /* field separator */
232 if( tmpdir ) free( tmpdir );
233 tmpdir = strdup( optarg );
235 case 'V': /* field separator */
236 if( urlpre ) free( urlpre );
237 urlpre = strdup( optarg );
239 case 'F': /* field separator */
240 sep = strdup( optarg );
242 case 'f': /* input file */
243 infile = strdup( optarg );
245 case 'h': /* ldap host */
246 ldaphost = strdup( optarg );
248 case 'b': /* searchbase */
249 base = strdup( optarg );
251 case 'D': /* bind DN */
252 binddn = strdup( optarg );
254 case 'p': /* ldap port */
255 ldapport = atoi( optarg );
257 case 'w': /* bind password */
258 passwd = strdup( optarg );
262 for( p = optarg; *p == '\0'; p++ ) {
267 case 'l': /* time limit */
268 timelimit = atoi( optarg );
270 case 'z': /* size limit */
271 sizelimit = atoi( optarg );
273 case 'S': /* sort attribute */
274 sortattr = strdup( optarg );
280 switch( atoi( optarg ) )
283 version = LDAP_VERSION2;
286 version = LDAP_VERSION3;
289 fprintf( stderr, "protocol version should be 2 or 3\n" );
294 #ifdef HAVE_CYRUS_SASL
296 authmethod = LDAP_AUTH_SASL;
298 fprintf( stderr, "%s was not compiled with SASL support\n",
300 return( EXIT_FAILURE );
304 #ifdef HAVE_CYRUS_SASL
306 authmethod = LDAP_AUTH_SASL;
308 fprintf( stderr, "%s was not compiled with SASL support\n",
310 return( EXIT_FAILURE );
314 #ifdef HAVE_CYRUS_SASL
315 if ( strcasecmp( optarg, "any" ) && strcmp( optarg, "*" ) ) {
316 sasl_mech = strdup( optarg );
318 authmethod = LDAP_AUTH_SASL;
320 fprintf( stderr, "%s was not compiled with SASL support\n",
322 return( EXIT_FAILURE );
326 #ifdef HAVE_CYRUS_SASL
327 sasl_authc_id = strdup( optarg );
328 authmethod = LDAP_AUTH_SASL;
330 fprintf( stderr, "%s was not compiled with SASL support\n",
332 return( EXIT_FAILURE );
336 #ifdef HAVE_CYRUS_SASL
337 sasl_authz_id = strdup( optarg );
338 authmethod = LDAP_AUTH_SASL;
340 fprintf( stderr, "%s was not compiled with SASL support\n",
342 return( EXIT_FAILURE );
349 fprintf( stderr, "%s was not compiled with TLS support\n",
351 return( EXIT_FAILURE );
360 /* no alternative format */
361 if( ldif < 1 ) ldif = 1;
364 if ( ( authmethod == LDAP_AUTH_KRBV4 ) || ( authmethod ==
365 LDAP_AUTH_KRBV41 ) ) {
366 if( version != LDAP_VERSION2 ) {
368 fprintf( stderr, "Kerberos requires LDAPv2\n" );
369 return( EXIT_FAILURE );
372 else if ( authmethod == LDAP_AUTH_SASL ) {
373 if( version != LDAP_VERSION3 ) {
374 fprintf( stderr, "SASL requires LDAPv3\n" );
375 return( EXIT_FAILURE );
380 if( version != LDAP_VERSION3 ) {
381 fprintf(stderr, "manage DSA control requires LDAPv3\n");
386 if ( argc - optind < 1 ) {
390 filtpattern = strdup( argv[ optind ] );
392 if ( argv[ optind + 1 ] == NULL ) {
394 } else if ( sortattr == NULL || *sortattr == '\0' ) {
395 attrs = &argv[ optind + 1 ];
397 for ( i = optind + 1; i < argc; i++ ) {
398 if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
404 argv[ optind ] = sortattr;
408 attrs = &argv[ optind ];
411 if ( infile != NULL ) {
412 if ( infile[0] == '-' && infile[1] == '\0' ) {
414 } else if (( fp = fopen( infile, "r" )) == NULL ) {
421 && (tmpdir = getenv("TMPDIR")) == NULL
422 && (tmpdir = getenv("TMP")) == NULL
423 && (tmpdir = getenv("TEMP")) == NULL )
428 if( urlpre == NULL ) {
429 urlpre = malloc( sizeof("file:///") + strlen(tmpdir) );
431 if( urlpre == NULL ) {
436 sprintf( urlpre, "file:///%s/",
437 tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
439 /* urlpre should be URLized.... */
443 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
444 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
446 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
447 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
453 (void) SIGNAL( SIGPIPE, SIG_IGN );
458 (ldapport ? "ldap_init( %s, %d )\n" : "ldap_init( %s, <DEFAULT> )\n"),
459 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
463 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
464 perror( "ldap_init" );
465 return( EXIT_FAILURE );
469 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
471 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
473 if (timelimit != -1 &&
474 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
476 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
478 if (sizelimit != -1 &&
479 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
481 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
483 if (referrals != -1 &&
484 ldap_set_option( ld, LDAP_OPT_REFERRALS,
485 (referrals ? LDAP_OPT_ON : LDAP_OPT_OFF) ) != LDAP_OPT_SUCCESS )
487 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
488 referrals ? "on" : "off" );
492 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS )
494 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
497 if ( use_tls && ldap_start_tls( ld, NULL, NULL ) != LDAP_SUCCESS ) {
499 ldap_perror( ld, "ldap_start_tls" );
500 return( EXIT_FAILURE );
505 passwd = getpass("Enter LDAP Password: ");
508 if ( authmethod == LDAP_AUTH_SASL ) {
509 #ifdef HAVE_CYRUS_SASL
510 int minssf = 0, maxssf = 0;
512 if ( sasl_integrity > 0 )
514 if ( sasl_integrity > 1 )
516 if ( sasl_privacy > 0 )
517 maxssf = 100000; /* Something big value */
518 if ( sasl_privacy > 1 )
521 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
522 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
523 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
525 return( EXIT_FAILURE );
527 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
528 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
529 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MAXSSF"
531 return( EXIT_FAILURE );
534 if ( ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
535 sasl_authz_id, sasl_mech, NULL, NULL, NULL )
537 ldap_perror( ld, "ldap_sasl_bind" );
538 return( EXIT_FAILURE );
541 fprintf( stderr, "%s was not compiled with SASL support\n",
543 return( EXIT_FAILURE );
547 if ( ldap_bind_s( ld, binddn, passwd, authmethod )
549 ldap_perror( ld, "ldap_bind" );
550 return( EXIT_FAILURE );
557 LDAPControl *ctrls[2];
561 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
562 c.ldctl_value.bv_val = NULL;
563 c.ldctl_value.bv_len = 0;
564 c.ldctl_iscritical = manageDSAit > 1;
566 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
568 if( err != LDAP_OPT_SUCCESS ) {
569 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
570 if( c.ldctl_iscritical ) {
571 exit( EXIT_FAILURE );
577 fprintf( stderr, "filter%s: %s\nreturning: ",
578 infile != NULL ? " pattern" : "",
581 if ( attrs == NULL ) {
582 fprintf( stderr, "ALL" );
584 for ( i = 0; attrs[ i ] != NULL; ++i ) {
585 fprintf( stderr, "%s ", attrs[ i ] );
588 fprintf( stderr, "\n" );
593 printf( "version: 1\n\n");
597 printf( "#\n# filter%s: %s\n# returning: ",
598 infile != NULL ? " pattern" : "",
601 if ( attrs == NULL ) {
604 for ( i = 0; attrs[ i ] != NULL; ++i ) {
605 printf( "%s ", attrs[ i ] );
612 if ( infile == NULL ) {
613 rc = dosearch( ld, base, scope, attrs, attrsonly, NULL, filtpattern );
618 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
619 line[ strlen( line ) - 1 ] = '\0';
625 rc = dosearch( ld, base, scope, attrs, attrsonly,
647 char filter[ BUFSIZ ];
648 int rc, first, matches;
649 LDAPMessage *res, *e;
651 if( filtpatt != NULL ) {
652 sprintf( filter, filtpatt, value );
655 fprintf( stderr, "filter is: (%s)\n", filter );
659 printf( "#\n# filter: %s\n#\n", filter );
663 sprintf( filter, "%s", value );
667 return( LDAP_SUCCESS );
670 if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
672 ldap_perror( ld, "ldap_search" );
674 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
681 while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
682 == LDAP_RES_SEARCH_ENTRY ) {
684 e = ldap_first_entry( ld, res );
690 print_entry( ld, e, attrsonly );
695 ldap_perror( ld, "ldap_result" );
698 if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
699 ldap_perror( ld, "ldap_search" );
701 if ( sortattr != NULL ) {
702 (void) ldap_sort_entries( ld, &res,
703 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
706 for ( e = ldap_first_entry( ld, res ); e != NULL;
707 e = ldap_next_entry( ld, e ) ) {
714 print_entry( ld, e, attrsonly );
719 printf( "%d matches\n", matches );
734 char tmpfname[ 256 ];
737 BerElement *ber = NULL;
738 struct berval **bvals;
741 dn = ldap_get_dn( ld, entry );
745 ufn = ldap_dn2ufn( dn );
746 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, strlen( ufn ));
749 write_ldif( LDIF_PUT_VALUE, "dn", dn, strlen( dn ));
751 printf( "%s\n", dn );
756 ufn = ldap_dn2ufn( dn );
759 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, strlen( ufn ));
761 printf( "%s\n", ufn );
765 if( ufn != NULL ) ldap_memfree( ufn );
768 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
769 a = ldap_next_attribute( ld, entry, ber ) )
771 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
777 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
782 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
783 for ( i = 0; bvals[i] != NULL; i++ ) {
784 if ( vals2tmp > 1 || ( vals2tmp
785 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
787 /* write value to file */
788 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
792 if ( mktemp( tmpfname ) == NULL ) {
797 if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
802 if ( fwrite( bvals[ i ]->bv_val,
803 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
812 sprintf( url, "%s%s", urlpre,
813 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
816 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
818 printf( "%s%s%s\n", a, sep, url );
824 write_ldif( LDIF_PUT_VALUE, a,
825 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
828 int notprint = !binary && !vals2tmp
829 && ldif_is_not_printable( bvals[i]->bv_val,
831 printf( "%s%s", a, sep );
832 puts( notprint ? "NOT PRINTABLE" : bvals[ i ]->bv_val );
836 ber_bvecfree( bvals );
847 write_ldif( int type, char *name, char *value, ber_len_t vallen )
851 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
855 fputs( ldif, stdout );