3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/stdlib.h>
14 #include <ac/signal.h>
15 #include <ac/string.h>
16 #include <ac/unistd.h>
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
34 #include "lutil_ldap.h"
35 #include "ldap_defaults.h"
38 usage( const char *s )
41 "usage: %s [options] [filter [attributes...]]\nwhere:\n"
42 "\tfilter\tRFC-2254 compliant LDAP search filter\n"
43 "\tattributes\twhitespace-separated list of attribute descriptions\n"
44 "\t which may include:\n"
45 "\t\t1.1 -- no attributes\n"
46 "\t\t* -- all user attributes\n"
47 "\t\t+ -- all operational attributes\n"
50 "\t-a deref\tdereference aliases: never (default), always, search, or find\n"
51 "\t-A\t\tretrieve attribute names only (no values)\n"
52 "\t-b basedn\tbase dn for search\n"
53 "\t-l limit\ttime limit (in seconds) for search\n"
54 "\t-L\t\tprint responses in LDIFv1 format\n"
55 "\t-LL\t\tprint responses in LDIF format without comments\n"
56 "\t-LLL\t\tprint responses in LDIF format without comments\n"
58 "\t-s scope\tone of base, one, or sub (search scope)\n"
59 "\t-S attr\t\tsort the results by attribute `attr'\n"
60 "\t-t\t\twrite binary values to files in temporary directory\n"
61 "\t-tt\t\twrite all values to files in temporary directory\n"
62 "\t-T path\t\twrite files to directory specified by path (default:\n"
63 "\t\t\t\"" LDAP_TMPDIR "\")\n"
64 "\t-u\t\tinclude User Friendly entry names in the output\n"
67 "\t-d level\tset LDAP debugging level to `level'\n"
68 "\t-D binddn\tbind DN\n"
69 "\t-f file\t\tread operations from `file'\n"
70 "\t-h host\t\tLDAP server\n"
71 "\t-I\t\tuse SASL Interactive mode\n"
72 "\t-k\t\tuse Kerberos authentication\n"
73 "\t-K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
74 "\t-M\t\tenable Manage DSA IT control (-MM to make critical)\n"
75 "\t-n\t\tshow what would be done but don't actually search\n"
76 "\t-O secprops\tSASL security properties\n"
77 "\t-p port\t\tport on LDAP server\n"
78 "\t-P version\tprocotol version (default: 3)\n"
79 "\t-Q\t\tuse SASL Quiet mode\n"
80 "\t-R realm\tSASL realm\n"
81 "\t-U user\t\tSASL authentication identity (username)\n"
82 "\t-v\t\trun in verbose mode (diagnostics to standard output)\n"
83 "\t-V prefix\tURL prefix for files (default: \"" LDAP_FILE_URI_PREFIX ")\n"
84 "\t-w passwd\tbind passwd (for simple authentication)\n"
85 "\t-W\t\tprompt for bind passwd\n"
86 "\t-x\t\tSimple authentication\n"
87 "\t-X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
88 "\t-Y mech\t\tSASL mechanism\n"
89 "\t-z limit\tsize limit (in entries) for search\n"
90 "\t-Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
96 static void print_entry LDAP_P((
101 static void print_reference(
103 LDAPMessage *reference );
105 static void print_extended(
107 LDAPMessage *extended );
109 static void print_partial(
111 LDAPMessage *partial );
113 static int print_result(
118 static void print_ctrls(
119 LDAPControl **ctrls );
121 static int write_ldif LDAP_P((
127 static int dosearch LDAP_P((
135 LDAPControl **sctrls,
136 LDAPControl **cctrls,
137 struct timeval *timelimit,
140 static char *tmpdir = NULL;
141 static char *urlpre = NULL;
143 static char *binddn = NULL;
144 static struct berval passwd = { 0, NULL };
145 static char *base = NULL;
146 static char *ldaphost = NULL;
147 static int ldapport = 0;
148 #ifdef HAVE_CYRUS_SASL
149 static unsigned sasl_flags = LUTIL_SASL_AUTOMATIC;
150 static char *sasl_realm = NULL;
151 static char *sasl_authc_id = NULL;
152 static char *sasl_authz_id = NULL;
153 static char *sasl_mech = NULL;
154 static char *sasl_secprops = NULL;
156 static int use_tls = 0;
157 static char *sortattr = NULL;
158 static int verbose, not, includeufn, vals2tmp, ldif;
161 main( int argc, char **argv )
163 char *prog, *infile, *filtpattern, **attrs, line[BUFSIZ];
165 int rc, i, first, scope, deref, attrsonly, manageDSAit;
166 int referrals, timelimit, sizelimit, debug;
167 int authmethod, version, want_bindpw;
171 debug = verbose = not = vals2tmp = referrals =
172 attrsonly = manageDSAit = ldif = want_bindpw = 0;
174 deref = sizelimit = timelimit = version = -1;
176 scope = LDAP_SCOPE_SUBTREE;
179 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : ++prog;
181 while (( i = getopt( argc, argv,
182 "Aa:b:f:Ll:S:s:T:tuV:z:" "Cd:D:h:IkKMnO:p:P:QRU:vw:WxX:Y:Z")) != EOF )
186 case 'a': /* set alias deref option */
187 if ( strcasecmp( optarg, "never" ) == 0 ) {
188 deref = LDAP_DEREF_NEVER;
189 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
190 deref = LDAP_DEREF_SEARCHING;
191 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
192 deref = LDAP_DEREF_FINDING;
193 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
194 deref = LDAP_DEREF_ALWAYS;
196 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
200 case 'A': /* retrieve attribute names only -- no values */
203 case 'b': /* search base */
204 base = strdup( optarg );
206 case 'f': /* input file */
207 if( infile != NULL ) {
208 fprintf( stderr, "%s: -f previously specified\n" );
211 infile = strdup( optarg );
213 case 'l': /* time limit */
214 timelimit = atoi( optarg );
216 case 'L': /* print entries in LDIF format */
219 case 's': /* search scope */
220 if ( strcasecmp( optarg, "base" ) == 0 ) {
221 scope = LDAP_SCOPE_BASE;
222 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
223 scope = LDAP_SCOPE_ONELEVEL;
224 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
225 scope = LDAP_SCOPE_SUBTREE;
227 fprintf( stderr, "scope should be base, one, or sub\n" );
231 case 'S': /* sort attribute */
232 sortattr = strdup( optarg );
234 case 'u': /* include UFN */
237 case 't': /* write attribute values to TMPDIR files */
240 case 'T': /* tmpdir */
241 if( tmpdir ) free( tmpdir );
242 tmpdir = strdup( optarg );
244 case 'V': /* uri prefix */
245 if( urlpre ) free( urlpre );
246 urlpre = strdup( optarg );
248 case 'z': /* size limit */
249 sizelimit = atoi( optarg );
257 debug |= atoi( optarg );
259 case 'D': /* bind DN */
260 if( binddn != NULL ) {
261 fprintf( stderr, "%s: -D previously specified\n" );
264 binddn = strdup( optarg );
266 case 'h': /* ldap host */
267 if( ldaphost != NULL ) {
268 fprintf( stderr, "%s: -h previously specified\n" );
271 ldaphost = strdup( optarg );
273 case 'k': /* kerberos bind */
274 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
275 if( version > LDAP_VERSION2 ) {
276 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
281 if( authmethod != -1 ) {
282 fprintf( stderr, "%s: -k incompatible with previous "
283 "authentication choice\n", prog );
287 authmethod = LDAP_AUTH_KRBV4;
289 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
293 case 'K': /* kerberos bind, part one only */
294 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
295 if( version > LDAP_VERSION2 ) {
296 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
300 if( authmethod != -1 ) {
301 fprintf( stderr, "%s: incompatible with previous "
302 "authentication choice\n", prog );
306 authmethod = LDAP_AUTH_KRBV41;
308 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
309 return( EXIT_FAILURE );
313 /* enable Manage DSA IT */
314 if( version == LDAP_VERSION2 ) {
315 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
320 version = LDAP_VERSION3;
322 case 'n': /* print deletes, don't actually do them */
326 #ifdef HAVE_CYRUS_SASL
327 if( sasl_secprops != NULL ) {
328 fprintf( stderr, "%s: -O previously specified\n" );
331 if( version == LDAP_VERSION2 ) {
332 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
336 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
337 fprintf( stderr, "%s: incompatible previous "
338 "authentication choice\n", prog );
341 authmethod = LDAP_AUTH_SASL;
342 version = LDAP_VERSION3;
343 sasl_secprops = strdup( optarg );
345 fprintf( stderr, "%s: not compiled with SASL support\n",
347 return( EXIT_FAILURE );
352 fprintf( stderr, "%s: -p previously specified\n" );
355 ldapport = atoi( optarg );
358 switch( atoi(optarg) ) {
360 if( version == LDAP_VERSION3 ) {
361 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
365 version = LDAP_VERSION2;
368 if( version == LDAP_VERSION2 ) {
369 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
373 version = LDAP_VERSION3;
376 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
379 return( EXIT_FAILURE );
382 #ifdef HAVE_CYRUS_SASL
383 if( version == LDAP_VERSION2 ) {
384 fprintf( stderr, "%s: -Q incompatible with version %d\n",
388 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
389 fprintf( stderr, "%s: incompatible previous "
390 "authentication choice\n",
394 authmethod = LDAP_AUTH_SASL;
395 version = LDAP_VERSION3;
396 sasl_flags = LUTIL_SASL_QUIET;
398 fprintf( stderr, "%s: was not compiled with SASL support\n",
400 return( EXIT_FAILURE );
403 #ifdef HAVE_CYRUS_SASL
404 if( sasl_realm != NULL ) {
405 fprintf( stderr, "%s: -R previously specified\n" );
408 if( version == LDAP_VERSION2 ) {
409 fprintf( stderr, "%s: -R incompatible with version %d\n",
413 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
414 fprintf( stderr, "%s: incompatible previous "
415 "authentication choice\n",
419 authmethod = LDAP_AUTH_SASL;
420 version = LDAP_VERSION3;
421 sasl_realm = strdup( optarg );
423 fprintf( stderr, "%s: was not compiled with SASL support\n",
425 return( EXIT_FAILURE );
429 #ifdef HAVE_CYRUS_SASL
430 if( sasl_authc_id != NULL ) {
431 fprintf( stderr, "%s: -U previously specified\n" );
434 if( version == LDAP_VERSION2 ) {
435 fprintf( stderr, "%s: -U incompatible with version %d\n",
439 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
440 fprintf( stderr, "%s: incompatible previous "
441 "authentication choice\n",
445 authmethod = LDAP_AUTH_SASL;
446 version = LDAP_VERSION3;
447 sasl_authc_id = strdup( optarg );
449 fprintf( stderr, "%s: was not compiled with SASL support\n",
451 return( EXIT_FAILURE );
454 case 'v': /* verbose mode */
457 case 'w': /* password */
458 passwd.bv_val = strdup( optarg );
462 for( p = optarg; *p == '\0'; p++ ) {
466 passwd.bv_len = strlen( passwd.bv_val );
472 #ifdef HAVE_CYRUS_SASL
473 if( sasl_mech != NULL ) {
474 fprintf( stderr, "%s: -Y previously specified\n" );
477 if( version == LDAP_VERSION2 ) {
478 fprintf( stderr, "%s: -Y incompatible with version %d\n",
482 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
483 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
486 authmethod = LDAP_AUTH_SASL;
487 version = LDAP_VERSION3;
488 sasl_mech = strdup( optarg );
490 fprintf( stderr, "%s: was not compiled with SASL support\n",
492 return( EXIT_FAILURE );
496 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
497 fprintf( stderr, "%s: incompatible with previous "
498 "authentication choice\n", prog );
501 authmethod = LDAP_AUTH_SIMPLE;
504 #ifdef HAVE_CYRUS_SASL
505 if( sasl_authz_id != NULL ) {
506 fprintf( stderr, "%s: -X previously specified\n" );
509 if( version == LDAP_VERSION2 ) {
510 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
514 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
515 fprintf( stderr, "%s: -X incompatible with "
516 "authentication choice\n", prog );
519 authmethod = LDAP_AUTH_SASL;
520 version = LDAP_VERSION3;
521 sasl_authz_id = strdup( optarg );
523 fprintf( stderr, "%s: not compiled with SASL support\n",
525 return( EXIT_FAILURE );
530 if( version == LDAP_VERSION2 ) {
531 fprintf( stderr, "%s -Z incompatible with version %d\n",
535 version = LDAP_VERSION3;
538 fprintf( stderr, "%s: not compiled with TLS support\n",
540 return( EXIT_FAILURE );
544 fprintf( stderr, "%s: unrecongized option -%c\n",
551 version = LDAP_VERSION3;
553 if (authmethod == -1 && version > LDAP_VERSION2) {
554 #ifdef HAVE_CYRUS_SASL
555 authmethod = LDAP_AUTH_SASL;
557 authmethod = LDAP_AUTH_SIMPLE;
561 if (( argc - optind < 1 ) ||
562 ( strchr( argv[optind], '=' ) == NULL ) )
564 filtpattern = "(objectclass=*)";
566 filtpattern = strdup( argv[optind++] );
569 if ( argv[optind] == NULL ) {
571 } else if ( sortattr == NULL || *sortattr == '\0' ) {
572 attrs = &argv[optind];
575 if ( infile != NULL ) {
576 if ( infile[0] == '-' && infile[1] == '\0' ) {
578 } else if (( fp = fopen( infile, "r" )) == NULL ) {
585 && (tmpdir = getenv("TMPDIR")) == NULL
586 && (tmpdir = getenv("TMP")) == NULL
587 && (tmpdir = getenv("TEMP")) == NULL )
589 tmpdir = LDAP_TMPDIR;
592 if( urlpre == NULL ) {
593 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
595 if( urlpre == NULL ) {
600 sprintf( urlpre, "file:///%s/",
601 tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
603 /* urlpre should be URLized.... */
607 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
608 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
610 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
611 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
617 (void) SIGNAL( SIGPIPE, SIG_IGN );
622 (ldapport ? "ldap_init( %s, %d )\n" : "ldap_init( %s, <DEFAULT> )\n"),
623 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
627 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
628 perror( "ldap_init" );
633 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
635 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
638 if (timelimit != -1 &&
639 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
641 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
644 if (sizelimit != -1 &&
645 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
647 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
652 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
653 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
655 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
656 referrals ? "on" : "off" );
660 if (version == -1 ) {
664 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
665 != LDAP_OPT_SUCCESS )
667 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
672 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
674 ldap_perror( ld, "ldap_start_tls" );
677 fprintf( stderr, "WARNING: could not start TLS\n" );
681 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
682 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
685 if ( authmethod == LDAP_AUTH_SASL ) {
686 #ifdef HAVE_CYRUS_SASL
689 if( sasl_secprops != NULL ) {
690 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
691 (void *) sasl_secprops );
693 if( rc != LDAP_OPT_SUCCESS ) {
695 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
697 return( EXIT_FAILURE );
701 defaults = lutil_sasl_defaults( ld, sasl_flags,
708 rc = ldap_sasl_interactive_bind_s( ld, binddn,
709 sasl_mech, NULL, NULL,
710 lutil_sasl_interact, defaults );
712 if( rc != LDAP_SUCCESS ) {
713 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
714 return( EXIT_FAILURE );
717 fprintf( stderr, "%s was not compiled with SASL support\n",
719 return( EXIT_FAILURE );
722 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
724 ldap_perror( ld, "ldap_bind" );
725 return( EXIT_FAILURE );
732 LDAPControl *ctrls[2];
736 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
737 c.ldctl_value.bv_val = NULL;
738 c.ldctl_value.bv_len = 0;
739 c.ldctl_iscritical = manageDSAit > 1;
741 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
743 if( err != LDAP_OPT_SUCCESS ) {
744 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
745 c.ldctl_iscritical ? "critical " : "" );
746 if( c.ldctl_iscritical ) {
747 exit( EXIT_FAILURE );
753 fprintf( stderr, "filter%s: %s\nrequesting: ",
754 infile != NULL ? " pattern" : "",
757 if ( attrs == NULL ) {
758 fprintf( stderr, "ALL" );
760 for ( i = 0; attrs[ i ] != NULL; ++i ) {
761 fprintf( stderr, "%s ", attrs[ i ] );
764 fprintf( stderr, "\n" );
768 printf( "version: %d\n\n", ldif ? 1 : 2 );
772 printf( "#\n# filter%s: %s\n# requesting: ",
773 infile != NULL ? " pattern" : "",
776 if ( attrs == NULL ) {
779 for ( i = 0; attrs[ i ] != NULL; ++i ) {
780 printf( "%s ", attrs[ i ] );
785 printf("\n# with manageDSAit %scontrol",
786 manageDSAit > 1 ? "critical " : "" );
792 if ( infile == NULL ) {
793 rc = dosearch( ld, base, scope, NULL, filtpattern,
794 attrs, attrsonly, NULL, NULL, NULL, -1 );
799 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
800 line[ strlen( line ) - 1 ] = '\0';
806 rc = dosearch( ld, base, scope, filtpattern, line,
807 attrs, attrsonly, NULL, NULL, NULL, -1 );
827 LDAPControl **sctrls,
828 LDAPControl **cctrls,
829 struct timeval *timelimit,
832 char filter[ BUFSIZ ];
839 LDAPMessage *res, *msg;
842 if( filtpatt != NULL ) {
843 sprintf( filter, filtpatt, value );
846 fprintf( stderr, "filter is: (%s)\n", filter );
850 printf( "#\n# filter: %s\n#\n", filter );
854 sprintf( filter, "%s", value );
861 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
862 sctrls, cctrls, timelimit, sizelimit, &msgid );
864 if( rc != LDAP_SUCCESS ) {
865 fprintf( stderr, "ldapsearch: ldap_search_ext: %s (%d)\n",
866 ldap_err2string( rc ), rc );
870 nresponses = nentries = nreferences = nextended = npartial = 0;
874 while ((rc = ldap_result( ld, LDAP_RES_ANY,
875 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
879 (void) ldap_sort_entries( ld, &res,
880 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
883 for ( msg = ldap_first_message( ld, res );
885 msg = ldap_next_message( ld, msg ) )
887 if( nresponses++ ) putchar('\n');
889 switch( ldap_msgtype( msg ) ) {
890 case LDAP_RES_SEARCH_ENTRY:
892 print_entry( ld, msg, attrsonly );
895 case LDAP_RES_SEARCH_REFERENCE:
897 print_reference( ld, msg );
900 case LDAP_RES_EXTENDED:
902 print_extended( ld, msg );
904 if( ldap_msgid( msg ) == 0 ) {
905 /* unsolicited extended operation */
910 case LDAP_RES_EXTENDED_PARTIAL:
912 print_partial( ld, msg );
915 case LDAP_RES_SEARCH_RESULT:
916 rc = print_result( ld, msg, 1 );
925 ldap_perror( ld, "ldap_result" );
931 printf( "\n# numResponses: %d\n", nresponses );
932 if( nentries ) printf( "# numEntries: %d\n", nentries );
933 if( nextended ) printf( "# numExtended: %d\n", nextended );
934 if( npartial ) printf( "# numPartial: %d\n", npartial );
935 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
948 char tmpfname[ 256 ];
951 BerElement *ber = NULL;
952 struct berval **bvals;
953 LDAPControl **ctrls = NULL;
956 dn = ldap_get_dn( ld, entry );
960 ufn = ldap_dn2ufn( dn );
961 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
963 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
965 rc = ldap_get_entry_controls( ld, entry, &ctrls );
967 if( rc != LDAP_SUCCESS ) {
968 fprintf(stderr, "print_entry: %d\n", rc );
969 ldap_perror( ld, "ldap_get_entry_controls" );
970 exit( EXIT_FAILURE );
974 print_ctrls( ctrls );
975 ldap_controls_free( ctrls );
980 ufn = ldap_dn2ufn( dn );
982 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
985 if( ufn != NULL ) ldap_memfree( ufn );
988 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
989 a = ldap_next_attribute( ld, entry, ber ) )
992 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
994 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
995 for ( i = 0; bvals[i] != NULL; i++ ) {
996 if ( vals2tmp > 1 || ( vals2tmp
997 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1000 /* write value to file */
1001 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1005 if ( mktemp( tmpfname ) == NULL ) {
1010 if (( tmpfd = open( tmpfname, O_WRONLY|O_CREAT|O_EXCL, 0600 )) == -1 ) {
1015 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1020 if ( fwrite( bvals[ i ]->bv_val,
1021 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1030 sprintf( url, "%s%s", urlpre,
1031 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1033 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1036 write_ldif( LDIF_PUT_VALUE, a,
1037 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1040 ber_bvecfree( bvals );
1049 static void print_reference(
1051 LDAPMessage *reference )
1055 LDAPControl **ctrls;
1058 printf("# search reference\n");
1061 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1063 if( rc != LDAP_SUCCESS ) {
1064 ldap_perror(ld, "ldap_parse_reference");
1065 exit( EXIT_FAILURE );
1070 for( i=0; refs[i] != NULL; i++ ) {
1071 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1072 "ref", refs[i], strlen(refs[i]) );
1074 ber_memvfree( (void **) refs );
1078 print_ctrls( ctrls );
1079 ldap_controls_free( ctrls );
1083 static void print_extended(
1085 LDAPMessage *extended )
1088 char *retoid = NULL;
1089 struct berval *retdata = NULL;
1092 printf("# extended result response\n");
1095 rc = ldap_parse_extended_result( ld, extended,
1096 &retoid, &retdata, 0 );
1098 if( rc != LDAP_SUCCESS ) {
1099 ldap_perror(ld, "ldap_parse_extended_result");
1100 exit( EXIT_FAILURE );
1103 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1104 "extended", retoid, retoid ? strlen(retoid) : 0 );
1105 ber_memfree( retoid );
1108 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1109 "data", retdata->bv_val, retdata->bv_len );
1110 ber_bvfree( retdata );
1113 print_result( ld, extended, 0 );
1116 static void print_partial(
1118 LDAPMessage *partial )
1121 char *retoid = NULL;
1122 struct berval *retdata = NULL;
1123 LDAPControl **ctrls = NULL;
1126 printf("# extended partial response\n");
1129 rc = ldap_parse_extended_partial( ld, partial,
1130 &retoid, &retdata, &ctrls, 0 );
1132 if( rc != LDAP_SUCCESS ) {
1133 ldap_perror(ld, "ldap_parse_extended_partial");
1134 exit( EXIT_FAILURE );
1137 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1138 "partial", retoid, retoid ? strlen(retoid) : 0 );
1140 ber_memfree( retoid );
1143 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1145 retdata->bv_val, retdata->bv_len );
1147 ber_bvfree( retdata );
1151 print_ctrls( ctrls );
1152 ldap_controls_free( ctrls );
1156 static int print_result(
1158 LDAPMessage *result, int search )
1163 char *matcheddn = NULL;
1166 LDAPControl **ctrls = NULL;
1170 printf("# search result\n");
1173 printf("%s: %d\n", "search", ldap_msgid(result) );
1177 rc = ldap_parse_result( ld, result,
1178 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1180 if( rc != LDAP_SUCCESS ) {
1181 ldap_perror(ld, "ldap_parse_result");
1182 exit( EXIT_FAILURE );
1187 printf( "result: %d %s\n", err, ldap_err2string(err) );
1189 } else if ( err != LDAP_SUCCESS ) {
1190 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1193 if( matcheddn && *matcheddn ) {
1195 write_ldif( LDIF_PUT_VALUE,
1196 "matchedDN", matcheddn, strlen(matcheddn) );
1198 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1201 ber_memfree( matcheddn );
1204 if( text && *text ) {
1206 write_ldif( LDIF_PUT_TEXT, "text",
1207 text, strlen(text) );
1209 fprintf( stderr, "Additional information: %s\n", text );
1212 ber_memfree( text );
1217 for( i=0; refs[i] != NULL; i++ ) {
1219 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1221 fprintf( stderr, "Referral: %s\n", refs[i] );
1225 ber_memvfree( (void **) refs );
1229 print_ctrls( ctrls );
1230 ldap_controls_free( ctrls );
1236 void print_ctrls( LDAPControl **ctrls ) {
1238 for(i=0; ctrls[i] != NULL; i++ ) {
1239 /* control: OID criticality base64value */
1240 struct berval *b64 = NULL;
1244 len = strlen( ctrls[i]->ldctl_oid );
1246 /* add enough for space after OID and the critical value itself */
1247 len += ctrls[i]->ldctl_iscritical
1248 ? sizeof("true") : sizeof("false");
1250 /* convert to base64 */
1251 if( ctrls[i]->ldctl_value.bv_len ) {
1252 b64 = ber_memalloc( sizeof(struct berval) );
1254 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1255 ctrls[i]->ldctl_value.bv_len ) + 1;
1256 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1258 b64->bv_len = lutil_b64_ntop(
1259 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1260 b64->bv_val, b64->bv_len );
1264 len += 1 + b64->bv_len;
1267 str = malloc( len + 1 );
1268 strcpy( str, ctrls[i]->ldctl_oid );
1269 strcat( str, ctrls[i]->ldctl_iscritical
1270 ? " true" : " false" );
1274 strcat(str, b64->bv_val );
1277 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1278 "control", str, len );
1286 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1290 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1294 fputs( ldif, stdout );
1295 ber_memfree( ldif );