]> git.sur5r.net Git - openldap/blob - clients/tools/ldapsearch.c
Very crude LDIF changes:
[openldap] / clients / tools / ldapsearch.c
1 #include "portable.h"
2
3 #include <stdio.h>
4
5 #include <ac/stdlib.h>
6
7 #include <ac/ctype.h>
8 #include <ac/signal.h>
9 #include <ac/string.h>
10 #include <ac/unistd.h>
11
12 #include <lber.h>
13 #include <ldap.h>
14 #include <ldif.h>
15
16 #define DEFSEP          "="
17
18
19 static void
20 usage( char *s )
21 {
22     fprintf( stderr, "usage: %s [options] filter [attributes...]\nwhere:\n", s );
23     fprintf( stderr, "    filter\tRFC-1558 compliant LDAP search filter\n" );
24     fprintf( stderr, "    attributes\twhitespace-separated list of attributes to retrieve\n" );
25     fprintf( stderr, "\t\t*          -- all user attributes\n" );
26     fprintf( stderr, "\t\tempty list -- all non-operational attributes\n" );
27     fprintf( stderr, "\t\t1.1        -- no attributes\n" );
28     fprintf( stderr, "options:\n" );
29     fprintf( stderr, "    -n\t\tshow what would be done but don't actually search\n" );
30     fprintf( stderr, "    -v\t\trun in verbose mode (diagnostics to standard output)\n" );
31     fprintf( stderr, "    -t\t\twrite values to files in /tmp\n" );
32     fprintf( stderr, "    -u\t\tinclude User Friendly entry names in the output\n" );
33     fprintf( stderr, "    -A\t\tretrieve attribute names only (no values)\n" );
34     fprintf( stderr, "    -B\t\tdo not suppress printing of non-ASCII values\n" );
35     fprintf( stderr, "    -L\t\tprint entries in LDIF format (-B is implied)\n" );
36     fprintf( stderr, "    -M\t\tenable Manage DSA IT control (-MM implies critical)\n" );
37     fprintf( stderr, "    -R\t\tdo not automatically follow referrals\n" );
38     fprintf( stderr, "    -d level\tset LDAP debugging level to `level'\n" );
39     fprintf( stderr, "    -F sep\tprint `sep' instead of `=' between attribute names and values\n" );
40     fprintf( stderr, "    -S attr\tsort the results by attribute `attr'\n" );
41     fprintf( stderr, "    -f file\tperform sequence of searches listed in `file'\n" );
42     fprintf( stderr, "    -b basedn\tbase dn for search\n" );
43     fprintf( stderr, "    -s scope\tone of base, one, or sub (search scope)\n" );
44     fprintf( stderr, "    -a deref\tone of never, always, search, or find (alias dereferencing)\n" );
45     fprintf( stderr, "    -l time lim\ttime limit (in seconds) for search\n" );
46     fprintf( stderr, "    -z size lim\tsize limit (in entries) for search\n" );
47     fprintf( stderr, "    -D binddn\tbind dn\n" );
48     fprintf( stderr, "    -w passwd\tbind passwd (for simple authentication)\n" );
49     fprintf( stderr, "    -W\t\tprompt for bind passwd\n" );
50 #ifdef HAVE_KERBEROS
51     fprintf( stderr, "    -k\t\tuse Kerberos instead of Simple Password authentication\n" );
52 #endif
53     fprintf( stderr, "    -h host\tldap server\n" );
54     fprintf( stderr, "    -p port\tport on ldap server\n" );
55     fprintf( stderr, "    -P version\tprocotol version (2 or 3)\n" );
56     exit( EXIT_FAILURE );
57 }
58
59 static void print_entry LDAP_P((
60     LDAP        *ld,
61     LDAPMessage *entry,
62     int         attrsonly));
63
64 static int write_ldif_value LDAP_P((
65         char *type,
66         char *value,
67         unsigned long vallen ));
68
69 static int dosearch LDAP_P((
70         LDAP    *ld,
71     char        *base,
72     int         scope,
73     char        **attrs,
74     int         attrsonly,
75     char        *filtpatt,
76     char        *value));
77
78 static char     *binddn = NULL;
79 static char     *passwd = NULL;
80 static char     *base = NULL;
81 static char     *ldaphost = NULL;
82 static int      ldapport = 0;
83 static char     *sep = DEFSEP;
84 static char     *sortattr = NULL;
85 static int      skipsortattr = 0;
86 static int      verbose, not, includeufn, allow_binary, vals2tmp, ldif;
87
88 int
89 main( int argc, char **argv )
90 {
91     char                *infile, *filtpattern, **attrs, line[ BUFSIZ ];
92     FILE                *fp;
93     int                 rc, i, first, scope, deref, attrsonly, manageDSAit;
94     int                 referrals, timelimit, sizelimit, debug;
95         int             authmethod, version, want_bindpw;
96     LDAP                *ld;
97
98     infile = NULL;
99     debug = verbose = allow_binary = not = vals2tmp =
100             attrsonly = manageDSAit = ldif = want_bindpw = 0;
101
102         deref = referrals = sizelimit = timelimit = version = -1;
103
104         scope = LDAP_SCOPE_SUBTREE;
105     authmethod = LDAP_AUTH_SIMPLE;
106
107     while (( i = getopt( argc, argv, "WKknuvtMRABLD:s:f:h:b:d:P:p:F:a:w:l:z:S:")) != EOF ) {
108         switch( i ) {
109         case 'n':       /* do Not do any searches */
110             ++not;
111             break;
112         case 'v':       /* verbose mode */
113             ++verbose;
114             break;
115         case 'd':
116             debug |= atoi( optarg );
117             break;
118         case 'k':       /* use kerberos bind */
119 #ifdef HAVE_KERBEROS
120                 authmethod = LDAP_AUTH_KRBV4;
121 #else
122                 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
123 #endif
124             break;
125         case 'K':       /* use kerberos bind, 1st part only */
126 #ifdef HAVE_KERBEROS
127                 authmethod = LDAP_AUTH_KRBV41;
128 #else
129                 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
130 #endif
131             break;
132         case 'u':       /* include UFN */
133             ++includeufn;
134             break;
135         case 't':       /* write attribute values to /tmp files */
136             ++vals2tmp;
137             break;
138         case 'M':
139                 /* enable Manage DSA IT */
140                 manageDSAit++;
141                 break;
142         case 'R':       /* don't automatically chase referrals */
143                 referrals = (int) LDAP_OPT_OFF;
144             break;
145         case 'A':       /* retrieve attribute names only -- no values */
146             ++attrsonly;
147             break;
148         case 'L':       /* print entries in LDIF format */
149             ++ldif;
150             /* fall through -- always allow binary when outputting LDIF */
151         case 'B':       /* allow binary values to be printed */
152             ++allow_binary;
153             break;
154         case 's':       /* search scope */
155             if ( strcasecmp( optarg, "base" ) == 0 ) {
156                 scope = LDAP_SCOPE_BASE;
157             } else if ( strcasecmp( optarg, "one" ) == 0 ) {
158                 scope = LDAP_SCOPE_ONELEVEL;
159             } else if ( strcasecmp( optarg, "sub" ) == 0 ) {
160                 scope = LDAP_SCOPE_SUBTREE;
161             } else {
162                 fprintf( stderr, "scope should be base, one, or sub\n" );
163                 usage( argv[ 0 ] );
164             }
165             break;
166
167         case 'a':       /* set alias deref option */
168             if ( strcasecmp( optarg, "never" ) == 0 ) {
169                 deref = LDAP_DEREF_NEVER;
170             } else if ( strcasecmp( optarg, "search" ) == 0 ) {
171                 deref = LDAP_DEREF_SEARCHING;
172             } else if ( strcasecmp( optarg, "find" ) == 0 ) {
173                 deref = LDAP_DEREF_FINDING;
174             } else if ( strcasecmp( optarg, "always" ) == 0 ) {
175                 deref = LDAP_DEREF_ALWAYS;
176             } else {
177                 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
178                 usage( argv[ 0 ] );
179             }
180             break;
181             
182         case 'F':       /* field separator */
183             sep = strdup( optarg );
184             break;
185         case 'f':       /* input file */
186             infile = strdup( optarg );
187             break;
188         case 'h':       /* ldap host */
189             ldaphost = strdup( optarg );
190             break;
191         case 'b':       /* searchbase */
192             base = strdup( optarg );
193             break;
194         case 'D':       /* bind DN */
195             binddn = strdup( optarg );
196             break;
197         case 'p':       /* ldap port */
198             ldapport = atoi( optarg );
199             break;
200         case 'w':       /* bind password */
201             passwd = strdup( optarg );
202                 {
203                         char* p;
204
205                         for( p = optarg; *p == '\0'; p++ ) {
206                                 *p = '*';
207                         }
208                 }
209             break;
210         case 'l':       /* time limit */
211             timelimit = atoi( optarg );
212             break;
213         case 'z':       /* size limit */
214             sizelimit = atoi( optarg );
215             break;
216         case 'S':       /* sort attribute */
217             sortattr = strdup( optarg );
218             break;
219         case 'W':
220                 want_bindpw++;
221                 break;
222         case 'P':
223                 switch( atoi( optarg ) )
224                 {
225                 case 2:
226                         version = LDAP_VERSION2;
227                         break;
228                 case 3:
229                         version = LDAP_VERSION3;
230                         break;
231                 default:
232                         fprintf( stderr, "protocol version should be 2 or 3\n" );
233                         usage( argv[0] );
234                 }
235                 break;
236         default:
237             usage( argv[0] );
238         }
239     }
240
241     if ( argc - optind < 1 ) {
242         usage( argv[ 0 ] );
243     }
244     filtpattern = strdup( argv[ optind ] );
245     if ( argv[ optind + 1 ] == NULL ) {
246         attrs = NULL;
247     } else if ( sortattr == NULL || *sortattr == '\0' ) {
248         attrs = &argv[ optind + 1 ];
249     } else {
250         for ( i = optind + 1; i < argc; i++ ) {
251             if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
252                 break;
253             }
254         }
255         if ( i == argc ) {
256                 skipsortattr = 1;
257                 argv[ optind ] = sortattr;
258         } else {
259                 optind++;
260         }
261         attrs = &argv[ optind ];
262     }
263
264     if ( infile != NULL ) {
265         if ( infile[0] == '-' && infile[1] == '\0' ) {
266             fp = stdin;
267         } else if (( fp = fopen( infile, "r" )) == NULL ) {
268             perror( infile );
269             return( EXIT_FAILURE );
270         }
271     }
272
273         if ( debug ) {
274                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
275                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
276                 }
277                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
278                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
279                 }
280                 ldif_debug = debug;
281         }
282
283 #ifdef SIGPIPE
284         (void) SIGNAL( SIGPIPE, SIG_IGN );
285 #endif
286
287     if ( verbose ) {
288         fprintf( stderr, "ldap_init( %s, %d )\n",
289                 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
290                 ldapport );
291     }
292
293     if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
294         perror( "ldap_init" );
295         return( EXIT_FAILURE );
296     }
297
298         if (deref != -1 &&
299                 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
300         {
301                 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
302         }
303         if (timelimit != -1 &&
304                 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
305         {
306                 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
307         }
308         if (sizelimit != -1 &&
309                 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
310         {
311                 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
312         }
313         if (referrals != -1 &&
314                 ldap_set_option( ld, LDAP_OPT_REFERRALS,
315                                  (referrals ? LDAP_OPT_ON : LDAP_OPT_OFF) ) != LDAP_OPT_SUCCESS )
316         {
317                 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
318                         referrals ? "on" : "off" );
319         }
320
321         if (version != -1 &&
322                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS )
323         {
324                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
325         }
326
327         if (want_bindpw)
328                 passwd = getpass("Enter LDAP Password: ");
329
330     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
331         ldap_perror( ld, "ldap_bind" );
332         return( EXIT_FAILURE );
333     }
334
335     if ( verbose ) {
336         fprintf( stderr, "filter pattern: %s\nreturning: ", filtpattern );
337         if ( attrs == NULL ) {
338             printf( "ALL" );
339         } else {
340             for ( i = 0; attrs[ i ] != NULL; ++i ) {
341                 fprintf( stderr, "%s ", attrs[ i ] );
342             }
343         }
344         fprintf( stderr, "\n" );
345     }
346
347         if ( manageDSAit ) {
348                 int err;
349                 LDAPControl c;
350                 LDAPControl *ctrls[2];
351                 ctrls[0] = &c;
352                 ctrls[1] = NULL;
353
354                 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
355                 c.ldctl_value.bv_val = NULL;
356                 c.ldctl_value.bv_len = 0;
357                 c.ldctl_iscritical = manageDSAit > 1;
358
359                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
360
361                 if( err != LDAP_OPT_SUCCESS ) {
362                         fprintf( stderr, "Could not set Manage DSA IT Control\n" );
363                         if( c.ldctl_iscritical ) {
364                                 exit( EXIT_FAILURE );
365                         }
366                 }
367         }
368
369     if ( infile == NULL ) {
370         rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern, "" );
371     } else {
372         rc = 0;
373         first = 1;
374         while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
375             line[ strlen( line ) - 1 ] = '\0';
376             if ( !first ) {
377                 putchar( '\n' );
378             } else {
379                 first = 0;
380             }
381             rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern,
382                     line );
383         }
384         if ( fp != stdin ) {
385             fclose( fp );
386         }
387     }
388
389     ldap_unbind( ld );
390
391
392         return( rc );
393 }
394
395
396 static int dosearch(
397         LDAP    *ld,
398     char        *base,
399     int         scope,
400     char        **attrs,
401     int         attrsonly,
402     char        *filtpatt,
403     char        *value)
404 {
405     char                filter[ BUFSIZ ];
406     int                 rc, first, matches;
407     LDAPMessage         *res, *e;
408
409     sprintf( filter, filtpatt, value );
410
411     if ( verbose ) {
412         fprintf( stderr, "filter is: (%s)\n", filter );
413     }
414
415     if ( not ) {
416         return( LDAP_SUCCESS );
417     }
418
419     if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
420                 int ld_errno;
421                 ldap_perror( ld, "ldap_search" );
422
423                 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
424                 return( ld_errno );
425     }
426
427     matches = 0;
428     first = 1;
429     res = NULL;
430     while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
431             == LDAP_RES_SEARCH_ENTRY ) {
432         matches++;
433         e = ldap_first_entry( ld, res );
434         if ( !first ) {
435             putchar( '\n' );
436         } else {
437             first = 0;
438         }
439         print_entry( ld, e, attrsonly );
440         ldap_msgfree( res );
441         res = NULL;
442     }
443     if ( rc == -1 ) {
444         ldap_perror( ld, "ldap_result" );
445         return( rc );
446     }
447     if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
448         ldap_perror( ld, "ldap_search" );
449     }
450     if ( sortattr != NULL ) {
451             (void) ldap_sort_entries( ld, &res,
452                     ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
453             matches = 0;
454             first = 1;
455             for ( e = ldap_first_entry( ld, res ); e != NULL;
456                     e = ldap_next_entry( ld, e ) ) {
457                 matches++;
458                 if ( !first ) {
459                     putchar( '\n' );
460                 } else {
461                     first = 0;
462                 }
463                 print_entry( ld, e, attrsonly );
464             }
465     }
466
467     if ( verbose ) {
468         printf( "%d matches\n", matches );
469     }
470
471     ldap_msgfree( res );
472     return( rc );
473 }
474
475
476 void print_entry(
477     LDAP        *ld,
478     LDAPMessage *entry,
479     int         attrsonly)
480 {
481     char                *a, *dn, *ufn, tmpfname[ 64 ];
482     int                 i, j, notascii;
483     BerElement          *ber = NULL;
484     struct berval       **bvals;
485     FILE                *tmpfp;
486
487     dn = ldap_get_dn( ld, entry );
488     if ( ldif ) {
489         write_ldif_value( "dn", dn, strlen( dn ));
490     } else {
491         printf( "%s\n", dn );
492     }
493     if ( includeufn ) {
494         ufn = ldap_dn2ufn( dn );
495         if ( ldif ) {
496             write_ldif_value( "ufn", ufn, strlen( ufn ));
497         } else {
498             printf( "%s\n", ufn );
499         }
500         ldap_memfree( ufn );
501     }
502     ldap_memfree( dn );
503
504     for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
505             a = ldap_next_attribute( ld, entry, ber ) ) {
506         if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
507             continue;
508         }
509         if ( attrsonly ) {
510             if ( ldif ) {
511                 write_ldif_value( a, "", 0 );
512             } else {
513                 printf( "%s\n", a );
514             }
515         } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
516             for ( i = 0; bvals[i] != NULL; i++ ) {
517                 if ( vals2tmp ) {
518                     sprintf( tmpfname, "/tmp/ldapsearch-%s-XXXXXX", a );
519                     tmpfp = NULL;
520
521                     if ( mktemp( tmpfname ) == NULL ) {
522                         perror( tmpfname );
523                     } else if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
524                         perror( tmpfname );
525                     } else if ( fwrite( bvals[ i ]->bv_val,
526                             bvals[ i ]->bv_len, 1, tmpfp ) == 0 ) {
527                         perror( tmpfname );
528                     } else if ( ldif ) {
529                         write_ldif_value( a, tmpfname, strlen( tmpfname ));
530                     } else {
531                         printf( "%s%s%s\n", a, sep, tmpfname );
532                     }
533
534                     if ( tmpfp != NULL ) {
535                         fclose( tmpfp );
536                     }
537                 } else {
538                     notascii = 0;
539                     if ( !allow_binary ) {
540                         for ( j = 0; (unsigned long) j < bvals[ i ]->bv_len; ++j ) {
541                             if ( !isascii( bvals[ i ]->bv_val[ j ] )) {
542                                 notascii = 1;
543                                 break;
544                             }
545                         }
546                     }
547
548                     if ( ldif ) {
549                         write_ldif_value( a, bvals[ i ]->bv_val,
550                                 bvals[ i ]->bv_len );
551                     } else {
552                         printf( "%s%s%s\n", a, sep,
553                                 notascii ? "NOT ASCII" : bvals[ i ]->bv_val );
554                     }
555                 }
556             }
557             ber_bvecfree( bvals );
558         }
559     }
560
561         if( ber != NULL ) {
562                 ber_free( ber, 0 );
563         }
564 }
565
566
567 int
568 write_ldif_value( char *type, char *value, unsigned long vallen )
569 {
570     char        *ldif;
571
572     if (( ldif = ldif_type_and_value( type, value, (int)vallen )) == NULL ) {
573         return( -1 );
574     }
575
576     fputs( ldif, stdout );
577     ber_memfree( ldif );
578
579     return( 0 );
580 }