]> git.sur5r.net Git - openldap/blob - clients/tools/ldapsearch.c
Use getpassphrase() instead of getpass() if available.
[openldap] / clients / tools / ldapsearch.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/stdlib.h>
12
13 #include <ac/ctype.h>
14 #include <ac/signal.h>
15 #include <ac/string.h>
16 #include <ac/unistd.h>
17 #include <ac/errno.h>
18 #include <sys/stat.h>
19
20 #ifdef HAVE_FCNTL_H
21 #include <fcntl.h>
22 #endif
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
25 #endif
26 #ifdef HAVE_IO_H
27 #include <io.h>
28 #endif
29
30 #include <ldap.h>
31
32 #include "ldif.h"
33 #include "ldap_defaults.h"
34
35 #define DEFSEP          "="
36
37 static void
38 usage( const char *s )
39 {
40         fprintf( stderr,
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"
48 "options:\n"
49 "       -a deref\tone of `never', `always', `search', or `find' (alias\n"
50 "               \tdereferencing)\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"
59 "               \tvalues\n"
60 "       -h host\t\tLDAP server\n"
61 "       -I\t\trequest SASL integrity checking (-II to make it\n"
62 "               \tcritical)\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"
69 "               \tversion\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"
80 "               \t\"/tmp\")\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"
91 ,               s );
92
93         exit( EXIT_FAILURE );
94 }
95
96 static void print_entry LDAP_P((
97         LDAP    *ld,
98         LDAPMessage     *entry,
99         int             attrsonly));
100
101 static int write_ldif LDAP_P((
102         int type,
103         char *name,
104         char *value,
105         ber_len_t vallen ));
106
107 static int dosearch LDAP_P((
108         LDAP    *ld,
109         char    *base,
110         int             scope,
111         char    **attrs,
112         int             attrsonly,
113         char    *filtpatt,
114         char    *value));
115
116 #define TMPDIR "/tmp"
117 #define URLPRE "file:/tmp/"
118
119 static char *tmpdir = NULL;
120 static char *urlpre = NULL;
121
122 static char     *binddn = NULL;
123 static struct berval passwd = { 0, 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;
133 #endif
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;
139
140 int
141 main( int argc, char **argv )
142 {
143         char            *infile, *filtpattern, **attrs, line[ BUFSIZ ];
144         FILE            *fp = NULL;
145         int                     rc, i, first, scope, deref, attrsonly, manageDSAit;
146         int                     referrals, timelimit, sizelimit, debug;
147         int             authmethod, version, want_bindpw;
148         LDAP            *ld;
149
150         infile = NULL;
151         debug = verbose = binary = not = vals2tmp =
152                 attrsonly = manageDSAit = ldif = want_bindpw = 0;
153
154         deref = referrals = sizelimit = timelimit = version = -1;
155
156         scope = LDAP_SCOPE_SUBTREE;
157         authmethod = LDAP_AUTH_SIMPLE;
158
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 )
161         {
162         switch( i ) {
163         case 'n':       /* do Not do any searches */
164                 ++not;
165                 break;
166         case 'v':       /* verbose mode */
167                 ++verbose;
168                 break;
169         case 'd':
170                 debug |= atoi( optarg );
171                 break;
172         case 'k':       /* use kerberos bind */
173 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
174                 authmethod = LDAP_AUTH_KRBV4;
175 #else
176                 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
177                 return( EXIT_FAILURE );
178 #endif
179                 break;
180         case 'K':       /* use kerberos bind, 1st part only */
181 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
182                 authmethod = LDAP_AUTH_KRBV41;
183 #else
184                 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
185                 return( EXIT_FAILURE );
186 #endif
187                 break;
188                 break;
189         case 'u':       /* include UFN */
190                 ++includeufn;
191                 break;
192         case 't':       /* write attribute values to /tmp files */
193                 ++vals2tmp;
194                 break;
195         case 'M':
196                 /* enable Manage DSA IT */
197                 manageDSAit++;
198                 break;
199         case 'R':       /* don't automatically chase referrals */
200                 referrals = (int) LDAP_OPT_OFF;
201                 break;
202         case 'A':       /* retrieve attribute names only -- no values */
203                 ++attrsonly;
204                 break;
205         case 'L':       /* print entries in LDIF format */
206                 ++ldif;
207                 /* fall through -- always allow binary when outputting LDIF */
208         case 'B':       /* allow binary values to be printed */
209                 ++binary;
210                 break;
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;
218                 } else {
219                 fprintf( stderr, "scope should be base, one, or sub\n" );
220                 usage( argv[ 0 ] );
221                 }
222                 break;
223
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;
233                 } else {
234                 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
235                 usage( argv[ 0 ] );
236                 }
237                 break;
238                 
239         case 'T':       /* field separator */
240                 if( tmpdir ) free( tmpdir );
241                 tmpdir = strdup( optarg );
242                 break;
243         case 'V':       /* field separator */
244                 if( urlpre ) free( urlpre );
245                 urlpre = strdup( optarg );
246                 break;
247         case 'F':       /* field separator */
248                 sep = strdup( optarg );
249                 break;
250         case 'f':       /* input file */
251                 infile = strdup( optarg );
252                 break;
253         case 'h':       /* ldap host */
254                 ldaphost = strdup( optarg );
255                 break;
256         case 'b':       /* searchbase */
257                 base = strdup( optarg );
258                 break;
259         case 'D':       /* bind DN */
260                 binddn = strdup( optarg );
261                 break;
262         case 'p':       /* ldap port */
263                 ldapport = atoi( optarg );
264                 break;
265         case 'w':       /* bind password */
266                 passwd.bv_val = strdup( optarg );
267                 {
268                         char* p;
269
270                         for( p = optarg; *p == '\0'; p++ ) {
271                                 *p = '*';
272                         }
273                 }
274                 passwd.bv_len = strlen( passwd.bv_val );
275                 break;
276         case 'l':       /* time limit */
277                 timelimit = atoi( optarg );
278                 break;
279         case 'z':       /* size limit */
280                 sizelimit = atoi( optarg );
281                 break;
282         case 'S':       /* sort attribute */
283                 sortattr = strdup( optarg );
284                 break;
285         case 'W':
286                 want_bindpw++;
287                 break;
288         case 'P':
289                 switch( atoi( optarg ) )
290                 {
291                 case 2:
292                         version = LDAP_VERSION2;
293                         break;
294                 case 3:
295                         version = LDAP_VERSION3;
296                         break;
297                 default:
298                         fprintf( stderr, "protocol version should be 2 or 3\n" );
299                         usage( argv[0] );
300                 }
301                 break;
302         case 'I':
303 #ifdef HAVE_CYRUS_SASL
304                 sasl_integrity++;
305                 authmethod = LDAP_AUTH_SASL;
306 #else
307                 fprintf( stderr, "%s was not compiled with SASL support\n",
308                         argv[0] );
309                 return( EXIT_FAILURE );
310 #endif
311                 break;
312         case 'E':
313 #ifdef HAVE_CYRUS_SASL
314                 sasl_privacy++;
315                 authmethod = LDAP_AUTH_SASL;
316 #else
317                 fprintf( stderr, "%s was not compiled with SASL support\n",
318                         argv[0] );
319                 return( EXIT_FAILURE );
320 #endif
321                 break;
322         case 'Y':
323 #ifdef HAVE_CYRUS_SASL
324                 if ( strcasecmp( optarg, "any" ) && strcmp( optarg, "*" ) ) {
325                         sasl_mech = strdup( optarg );
326                 }
327                 authmethod = LDAP_AUTH_SASL;
328 #else
329                 fprintf( stderr, "%s was not compiled with SASL support\n",
330                         argv[0] );
331                 return( EXIT_FAILURE );
332 #endif
333                 break;
334         case 'U':
335 #ifdef HAVE_CYRUS_SASL
336                 sasl_authc_id = strdup( optarg );
337                 authmethod = LDAP_AUTH_SASL;
338 #else
339                 fprintf( stderr, "%s was not compiled with SASL support\n",
340                         argv[0] );
341                 return( EXIT_FAILURE );
342 #endif
343                 break;
344         case 'X':
345 #ifdef HAVE_CYRUS_SASL
346                 sasl_authz_id = strdup( optarg );
347                 authmethod = LDAP_AUTH_SASL;
348 #else
349                 fprintf( stderr, "%s was not compiled with SASL support\n",
350                         argv[0] );
351                 return( EXIT_FAILURE );
352 #endif
353                 break;
354         case 'Z':
355 #ifdef HAVE_TLS
356                 use_tls++;
357 #else
358                 fprintf( stderr, "%s was not compiled with TLS support\n",
359                         argv[0] );
360                 return( EXIT_FAILURE );
361 #endif
362                 break;
363         default:
364                 usage( argv[0] );
365         }
366         }
367
368 #ifdef LDAP_LDIF
369         /* no alternative format */
370         if( ldif < 1 ) ldif = 1;
371 #endif
372
373         if ( ( authmethod == LDAP_AUTH_KRBV4 ) || ( authmethod ==
374                         LDAP_AUTH_KRBV41 ) ) {
375                 if( version > LDAP_VERSION2 ) {
376                         fprintf( stderr, "Kerberos requires LDAPv2\n" );
377                         return( EXIT_FAILURE );
378                 }
379                 version = LDAP_VERSION2;
380         }
381         else if ( authmethod == LDAP_AUTH_SASL ) {
382                 if( version != -1 && version != LDAP_VERSION3 ) {
383                         fprintf( stderr, "SASL requires LDAPv3\n" );
384                         return( EXIT_FAILURE );
385                 }
386                 version = LDAP_VERSION3;
387         }
388
389         if( manageDSAit ) {
390                 if( version != -1 && version != LDAP_VERSION3 ) {
391                         fprintf(stderr, "manage DSA control requires LDAPv3\n");
392                         return EXIT_FAILURE;
393                 }
394                 version = LDAP_VERSION3;
395         }
396
397         if( use_tls ) {
398                 if( version != -1 && version != LDAP_VERSION3 ) {
399                         fprintf(stderr, "Start TLS requires LDAPv3\n");
400                         return EXIT_FAILURE;
401                 }
402                 version = LDAP_VERSION3;
403         }
404
405         if ( argc - optind < 1 ) {
406                 usage( argv[ 0 ] );
407         }
408
409         filtpattern = strdup( argv[ optind ] );
410
411         if ( argv[ optind + 1 ] == NULL ) {
412                 attrs = NULL;
413         } else if ( sortattr == NULL || *sortattr == '\0' ) {
414                 attrs = &argv[ optind + 1 ];
415         } else {
416                 for ( i = optind + 1; i < argc; i++ ) {
417                         if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
418                                 break;
419                         }
420                 }
421                 if ( i == argc ) {
422                         skipsortattr = 1;
423                         argv[ optind ] = sortattr;
424                 } else {
425                         optind++;
426                 }
427                 attrs = &argv[ optind ];
428         }
429
430         if ( infile != NULL ) {
431                 if ( infile[0] == '-' && infile[1] == '\0' ) {
432                         fp = stdin;
433                 } else if (( fp = fopen( infile, "r" )) == NULL ) {
434                         perror( infile );
435                         return EXIT_FAILURE;
436                 }
437         }
438
439         if( tmpdir == NULL
440                 && (tmpdir = getenv("TMPDIR")) == NULL
441                 && (tmpdir = getenv("TMP")) == NULL
442                 && (tmpdir = getenv("TEMP")) == NULL )
443         {
444                 tmpdir = "/tmp";
445         }
446
447         if( urlpre == NULL ) {
448                 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
449
450                 if( urlpre == NULL ) {
451                         perror( "malloc" );
452                         return EXIT_FAILURE;
453                 }
454
455                 sprintf( urlpre, "file:///%s/",
456                         tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
457
458                 /* urlpre should be URLized.... */
459         }
460
461         if ( debug ) {
462                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
463                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
464                 }
465                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
466                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
467                 }
468                 ldif_debug = debug;
469         }
470
471 #ifdef SIGPIPE
472         (void) SIGNAL( SIGPIPE, SIG_IGN );
473 #endif
474
475         if ( verbose ) {
476                 fprintf( stderr,
477                         (ldapport ? "ldap_init( %s, %d )\n" : "ldap_init( %s, <DEFAULT> )\n"),
478                         (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
479                         ldapport );
480         }
481
482         if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
483                 perror( "ldap_init" );
484                 return( EXIT_FAILURE );
485         }
486
487         if (deref != -1 &&
488                 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
489         {
490                 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
491         }
492         if (timelimit != -1 &&
493                 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
494         {
495                 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
496         }
497         if (sizelimit != -1 &&
498                 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
499         {
500                 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
501         }
502         if (referrals != -1 &&
503                 ldap_set_option( ld, LDAP_OPT_REFERRALS,
504                                  (referrals ? LDAP_OPT_ON : LDAP_OPT_OFF) ) != LDAP_OPT_SUCCESS )
505         {
506                 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
507                         referrals ? "on" : "off" );
508         }
509
510         if (version != -1 &&
511                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS )
512         {
513                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
514         }
515
516         if ( use_tls && ldap_start_tls( ld, NULL, NULL ) != LDAP_SUCCESS ) {
517                 if ( use_tls > 1 ) {
518                         ldap_perror( ld, "ldap_start_tls" );
519                         return( EXIT_FAILURE );
520                 }
521         }
522
523         if (want_bindpw) {
524                 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
525                 passwd.bv_len = strlen( passwd.bv_val );
526         }
527
528         if ( authmethod == LDAP_AUTH_SASL ) {
529 #ifdef HAVE_CYRUS_SASL
530                 int     minssf = 0, maxssf = 0;
531
532                 if ( sasl_integrity > 0 )
533                         maxssf = 1;
534                 if ( sasl_integrity > 1 )
535                         minssf = 1;
536                 if ( sasl_privacy > 0 )
537                         maxssf = 100000; /* Something big value */
538                 if ( sasl_privacy > 1 )
539                         minssf = 56;
540                 
541                 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
542                                 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
543                         fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
544                                 "%d\n", minssf);
545                         return( EXIT_FAILURE );
546                 }
547                 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
548                                 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
549                         fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MAXSSF"
550                                 "%d\n", maxssf);
551                         return( EXIT_FAILURE );
552                 }
553                 
554                 rc = ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
555                                 sasl_authz_id, sasl_mech,
556                                 passwd.bv_len ? &passwd : NULL,
557                                 NULL, NULL );
558
559                 if( rc != LDAP_SUCCESS ) {
560                         ldap_perror( ld, "ldap_negotiated_sasl_bind_s" );
561                         return( EXIT_FAILURE );
562                 }
563 #else
564                 fprintf( stderr, "%s was not compiled with SASL support\n",
565                         argv[0] );
566                 return( EXIT_FAILURE );
567 #endif
568         }
569         else {
570                 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
571                                 != LDAP_SUCCESS ) {
572                         ldap_perror( ld, "ldap_bind" );
573                         return( EXIT_FAILURE );
574                 }
575         }
576
577         if ( manageDSAit ) {
578                 int err;
579                 LDAPControl c;
580                 LDAPControl *ctrls[2];
581                 ctrls[0] = &c;
582                 ctrls[1] = NULL;
583
584                 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
585                 c.ldctl_value.bv_val = NULL;
586                 c.ldctl_value.bv_len = 0;
587                 c.ldctl_iscritical = manageDSAit > 1;
588
589                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
590
591                 if( err != LDAP_OPT_SUCCESS ) {
592                         fprintf( stderr, "Could not set Manage DSA IT Control\n" );
593                         if( c.ldctl_iscritical ) {
594                                 exit( EXIT_FAILURE );
595                         }
596                 }
597         }
598
599         if ( verbose ) {
600                 fprintf( stderr, "filter%s: %s\nreturning: ",
601                         infile != NULL ? " pattern" : "",
602                         filtpattern );
603
604                 if ( attrs == NULL ) {
605                         fprintf( stderr, "ALL" );
606                 } else {
607                         for ( i = 0; attrs[ i ] != NULL; ++i ) {
608                                 fprintf( stderr, "%s ", attrs[ i ] );
609                         }
610                 }
611                 fprintf( stderr, "\n" );
612         }
613
614         if ( ldif ) {
615                 if (ldif < 3 ) {
616                         printf( "version: 1\n\n");
617                 }
618
619                 if (ldif < 2 ) {
620                         printf( "#\n# filter%s: %s\n# returning: ",
621                                 infile != NULL ? " pattern" : "",
622                                 filtpattern );
623
624                         if ( attrs == NULL ) {
625                                 printf( "ALL" );
626                         } else {
627                                 for ( i = 0; attrs[ i ] != NULL; ++i ) {
628                                         printf( "%s ", attrs[ i ] );
629                                 }
630                         }
631                         printf( "\n#\n\n" );
632                 }
633         }
634
635         if ( infile == NULL ) {
636                 rc = dosearch( ld, base, scope, attrs, attrsonly, NULL, filtpattern );
637
638         } else {
639                 rc = 0;
640                 first = 1;
641                 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
642                         line[ strlen( line ) - 1 ] = '\0';
643                         if ( !first ) {
644                                 putchar( '\n' );
645                         } else {
646                                 first = 0;
647                         }
648                         rc = dosearch( ld, base, scope, attrs, attrsonly,
649                                 filtpattern, line );
650                 }
651                 if ( fp != stdin ) {
652                         fclose( fp );
653                 }
654         }
655
656         ldap_unbind( ld );
657         return( rc );
658 }
659
660
661 static int dosearch(
662         LDAP    *ld,
663         char    *base,
664         int             scope,
665         char    **attrs,
666         int             attrsonly,
667         char    *filtpatt,
668         char    *value)
669 {
670         char            filter[ BUFSIZ ];
671         int                     rc, first, matches;
672         LDAPMessage             *res, *e;
673
674         if( filtpatt != NULL ) {
675                 sprintf( filter, filtpatt, value );
676
677                 if ( verbose ) {
678                         fprintf( stderr, "filter is: (%s)\n", filter );
679                 }
680
681                 if( ldif == 1 ) {
682                         printf( "#\n# filter: %s\n#\n", filter );
683                 }
684
685         } else {
686                 sprintf( filter, "%s", value );
687         }
688
689         if ( not ) {
690                 return( LDAP_SUCCESS );
691         }
692
693         if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
694                 int ld_errno;
695                 ldap_perror( ld, "ldap_search" );
696
697                 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
698                 return( ld_errno );
699         }
700
701         matches = 0;
702         first = 1;
703         res = NULL;
704         while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
705                 == LDAP_RES_SEARCH_ENTRY ) {
706         matches++;
707         e = ldap_first_entry( ld, res );
708         if ( !first ) {
709                 putchar( '\n' );
710         } else {
711                 first = 0;
712         }
713         print_entry( ld, e, attrsonly );
714         ldap_msgfree( res );
715         res = NULL;
716         }
717         if ( rc == -1 ) {
718         ldap_perror( ld, "ldap_result" );
719         return( rc );
720         }
721         if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
722                 ldap_perror( ld, "ldap_search" );
723         }
724         if ( sortattr != NULL ) {
725                 (void) ldap_sort_entries( ld, &res,
726                         ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
727                 matches = 0;
728                 first = 1;
729                 for ( e = ldap_first_entry( ld, res ); e != NULL;
730                         e = ldap_next_entry( ld, e ) ) {
731                 matches++;
732                 if ( !first ) {
733                         putchar( '\n' );
734                 } else {
735                         first = 0;
736                 }
737                 print_entry( ld, e, attrsonly );
738                 }
739         }
740
741         if ( verbose ) {
742                 printf( "%d matches\n", matches );
743         }
744
745         ldap_msgfree( res );
746         return( rc );
747 }
748
749
750 static void
751 print_entry(
752         LDAP    *ld,
753         LDAPMessage     *entry,
754         int             attrsonly)
755 {
756         char            *a, *dn, *ufn;
757         char    tmpfname[ 256 ];
758         char    url[ 256 ];
759         int                     i;
760         BerElement              *ber = NULL;
761         struct berval   **bvals;
762         FILE            *tmpfp;
763
764         dn = ldap_get_dn( ld, entry );
765         ufn = NULL;
766
767         if ( ldif == 1 ) {
768                 ufn = ldap_dn2ufn( dn );
769                 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, strlen( ufn ));
770         }
771         if ( ldif ) {
772                 write_ldif( LDIF_PUT_VALUE, "dn", dn, strlen( dn ));
773         } else {
774                 printf( "%s\n", dn );
775         }
776
777         if ( includeufn ) {
778                 if( ufn == NULL ) {
779                         ufn = ldap_dn2ufn( dn );
780                 }
781                 if ( ldif ) {
782                         write_ldif( LDIF_PUT_VALUE, "ufn", ufn, strlen( ufn ));
783                 } else {
784                         printf( "%s\n", ufn );
785                 }
786         }
787
788         if( ufn != NULL ) ldap_memfree( ufn );
789         ldap_memfree( dn );
790
791         for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
792                 a = ldap_next_attribute( ld, entry, ber ) )
793         {
794                 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
795                         continue;
796                 }
797
798                 if ( attrsonly ) {
799                         if ( ldif ) {
800                                 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
801                         } else {
802                                 printf( "%s\n", a );
803                         }
804
805                 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
806                         for ( i = 0; bvals[i] != NULL; i++ ) {
807                                 if ( vals2tmp > 1 || ( vals2tmp
808                                         && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
809                                 {
810                                         int tmpfd;
811                                         /* write value to file */
812                                         sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
813                                                 tmpdir, a );
814                                         tmpfp = NULL;
815
816                                         if ( mktemp( tmpfname ) == NULL ) {
817                                                 perror( tmpfname );
818                                                 continue;
819                                         }
820
821                                         if (( tmpfd = open( tmpfname, O_WRONLY|O_CREAT|O_EXCL, 0600 )) == -1 ) {
822                                                 perror( tmpfname );
823                                                 continue;
824                                         }
825
826                                         if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
827                                                 perror( tmpfname );
828                                                 continue;
829                                         }
830
831                                         if ( fwrite( bvals[ i ]->bv_val,
832                                                 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
833                                         {
834                                                 perror( tmpfname );
835                                                 fclose( tmpfp );
836                                                 continue;
837                                         }
838
839                                         fclose( tmpfp );
840
841                                         sprintf( url, "%s%s", urlpre,
842                                                 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
843
844                                         if ( ldif ) {
845                                                 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
846                                         } else {
847                                                 printf( "%s%s%s\n", a, sep, url );
848                                         }
849
850
851                                 } else {
852                                         if ( ldif ) {
853                                                 write_ldif( LDIF_PUT_VALUE, a,
854                                                         bvals[ i ]->bv_val, bvals[ i ]->bv_len );
855
856                                         } else {
857                                                 int notprint = !binary && !vals2tmp
858                                                         && ldif_is_not_printable( bvals[i]->bv_val,
859                                                                 bvals[i]->bv_len ); 
860                                                 printf( "%s%s", a, sep );
861                                                 puts( notprint ? "NOT PRINTABLE" : bvals[ i ]->bv_val );
862                                         }
863                                 }
864                         }
865                         ber_bvecfree( bvals );
866                 }
867         }
868
869         if( ber != NULL ) {
870                 ber_free( ber, 0 );
871         }
872 }
873
874
875 static int
876 write_ldif( int type, char *name, char *value, ber_len_t vallen )
877 {
878         char    *ldif;
879
880         if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
881                 return( -1 );
882         }
883
884         fputs( ldif, stdout );
885         ber_memfree( ldif );
886
887         return( 0 );
888 }