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