summaryrefslogtreecommitdiff
path: root/usr/src/cmd/ldap/common/ldaptest.c
blob: d6024b54908e679aaf73631254101ef939649770 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
/*
 * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <fcntl.h>
#include <unistd.h>

#include "lber.h"
#include "ldap.h"

#define MOD_USE_BVALS

#ifdef NEEDPROTOS
static void handle_result( LDAP *ld, LDAPMessage *lm );
static void print_ldap_result( LDAP *ld, LDAPMessage *lm, char *s );
static void print_search_entry( LDAP *ld, LDAPMessage *res );
static void free_list( char **list );
#else
static void handle_result();
static void print_ldap_result();
static void print_search_entry();
static void free_list();
#endif /* NEEDPROTOS */

#define NOCACHEERRMSG	"don't compile with -DNO_CACHE if you desire local caching"

char *dnsuffix;

static char *
getaline( char *line, int len, FILE *fp, char *prompt )
{
	printf(prompt);

	if ( fgets( line, len, fp ) == NULL )
		return( NULL );

	line[ strlen( line ) - 1 ] = '\0';

	return( line );
}

static char **
get_list( char *prompt )
{
	static char	buf[256];
	int		num;
	char		**result;

	num = 0;
	result = (char **) 0;
	while ( 1 ) {
		getaline( buf, sizeof(buf), stdin, prompt );

		if ( *buf == '\0' )
			break;

		if ( result == (char **) 0 )
			result = (char **) malloc( sizeof(char *) );
		else
			result = (char **) realloc( result,
			    sizeof(char *) * (num + 1) );

		result[num++] = (char *) strdup( buf );
	}
	if ( result == (char **) 0 )
		return( NULL );
	result = (char **) realloc( result, sizeof(char *) * (num + 1) );
	result[num] = NULL;

	return( result );
}


static void
free_list( char **list )
{
	int	i;

	if ( list != NULL ) {
		for ( i = 0; list[ i ] != NULL; ++i ) {
			free( list[ i ] );
		}
		free( (char *)list );
	}
}


#ifdef MOD_USE_BVALS
static int
file_read( char *path, struct berval *bv )
{
	FILE		*fp;
	long		rlen;
	int		eof;

	if (( fp = fopen( path, "r" )) == NULL ) {
	    	perror( path );
		return( -1 );
	}

	if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
		perror( path );
		fclose( fp );
		return( -1 );
	}

	bv->bv_len = ftell( fp );

	if (( bv->bv_val = (char *)malloc( bv->bv_len )) == NULL ) {
		perror( "malloc" );
		fclose( fp );
		return( -1 );
	}

	if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
		perror( path );
		fclose( fp );
		return( -1 );
	}

	rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
	eof = feof( fp );
	fclose( fp );

	if ( rlen != bv->bv_len ) {
		perror( path );
		free( bv->bv_val );
		return( -1 );
	}

	return( bv->bv_len );
}
#endif /* MOD_USE_BVALS */


static LDAPMod **
get_modlist( char *prompt1, char *prompt2, char *prompt3 )
{
	static char	buf[256];
	int		num;
	LDAPMod		tmp;
	LDAPMod		**result;
#ifdef MOD_USE_BVALS
	struct berval	**bvals;
#endif /* MOD_USE_BVALS */

	num = 0;
	result = NULL;
	while ( 1 ) {
		if ( prompt1 ) {
			getaline( buf, sizeof(buf), stdin, prompt1 );
			tmp.mod_op = atoi( buf );

			if ( tmp.mod_op == -1 || buf[0] == '\0' )
				break;
		}

		getaline( buf, sizeof(buf), stdin, prompt2 );
		if ( buf[0] == '\0' )
			break;
		tmp.mod_type = strdup( buf );

		tmp.mod_values = get_list( prompt3 );
#ifdef MOD_USE_BVALS
		if ( tmp.mod_values != NULL ) {
			int	i;

			for ( i = 0; tmp.mod_values[i] != NULL; ++i )
				;
			bvals = (struct berval **)calloc( i + 1,
			    sizeof( struct berval *));
			for ( i = 0; tmp.mod_values[i] != NULL; ++i ) {
				bvals[i] = (struct berval *)malloc(
				    sizeof( struct berval ));
				if ( strncmp( tmp.mod_values[i], "{FILE}",
				    6 ) == 0 ) {
					if ( file_read( tmp.mod_values[i] + 6,
					    bvals[i] ) < 0 ) {
						return( NULL );
					}
				} else {
					bvals[i]->bv_val = tmp.mod_values[i];
					bvals[i]->bv_len =
					    strlen( tmp.mod_values[i] );
				}
			}
			tmp.mod_bvalues = bvals;
			tmp.mod_op |= LDAP_MOD_BVALUES;
		}
#endif /* MOD_USE_BVALS */

		if ( result == NULL )
			result = (LDAPMod **) malloc( sizeof(LDAPMod *) );
		else
			result = (LDAPMod **) realloc( result,
			    sizeof(LDAPMod *) * (num + 1) );

		result[num] = (LDAPMod *) malloc( sizeof(LDAPMod) );
		*(result[num]) = tmp;	/* struct copy */
		num++;
	}
	if ( result == NULL )
		return( NULL );
	result = (LDAPMod **) realloc( result, sizeof(LDAPMod *) * (num + 1) );
	result[num] = NULL;

	return( result );
}


int
bind_prompt( LDAP *ld, char **dnp, char **passwdp, int *authmethodp,
	int freeit )
{
	static char	dn[256], passwd[256];

	if ( !freeit ) {
#ifdef KERBEROS
		getaline( dn, sizeof(dn), stdin,
		    "re-bind method (0->simple, 1->krbv41, 2->krbv42, 3->krbv41&2)? " );
		if (( *authmethodp = atoi( dn )) == 3 ) {
			*authmethodp = LDAP_AUTH_KRBV4;
		} else {
			*authmethodp |= 0x80;
		}
#else /* KERBEROS */
		*authmethodp = LDAP_AUTH_SIMPLE;
#endif /* KERBEROS */

		getaline( dn, sizeof(dn), stdin, "re-bind dn? " );
		strcat( dn, dnsuffix );
		*dnp = dn;

		if ( *authmethodp == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) {
			getaline( passwd, sizeof(passwd), stdin,
			    "re-bind password? " );
		} else {
			passwd[0] = '\0';
		}
		*passwdp = passwd;
	}

	return( LDAP_SUCCESS );
}


int
main(int argc, char **argv )
{
	LDAP	*ld;
	int		i, c, port, cldapflg, errflg, method, id, 
		msgtype, delrdn, theInt, sizelimit, err;
	char	line[256], command1, command2, command3;
	char	passwd[64], dn[256], rdn[64], attr[64], value[256];
	char	filter[256], *host, **types;
	char 	*mechanism;
	
	char	**exdn;
	char	*usage = "usage: %s [-u] [-h host] [-d level] [-s dnsuffix] [-p port] [-t file] [-T file]\n";
	int		bound, all, scope, attrsonly;
	LDAPMessage	*res;
	LDAPMod	**mods, **attrs;
	struct timeval	timeout, timelimit;
	char	*copyfname = NULL;
	int		copyoptions = 0, resultusetimelimit = 0;
	LDAPURLDesc	*ludp;
	struct berval bv, cred, *srvcrds = NULL;
	extern char	*optarg;
	extern int	optind;
	LDAPControl *ctrls[2];
	LDAPControl aCtrl;
	

#ifdef MACOS
	if (( argv = get_list( "cmd line arg?" )) == NULL ) {
		exit( 1 );
	}
	for ( argc = 0; argv[ argc ] != NULL; ++argc ) {
		;
	}
#endif /* MACOS */

	host = NULL;
	port = LDAP_PORT;
	dnsuffix = "";
	cldapflg = errflg = 0;
	ctrls[0] = &aCtrl;
	ctrls[1] = NULL;
	
	while (( c = getopt( argc, argv, "uh:d:s:p:t:T:" )) != -1 ) {
		switch( c ) {
		case 'u':
#ifdef CLDAP
			cldapflg++;
#else /* CLDAP */
			printf( "Compile with -DCLDAP for UDP support\n" );
#endif /* CLDAP */
			break;

		case 'd':
#ifdef LDAP_DEBUG
			ldap_debug = atoi( optarg );
			if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
				lber_debug = ldap_debug;
			}
#else
			printf( "Compile with -DLDAP_DEBUG for debugging\n" );
#endif
			break;

		case 'h':
			host = optarg;
			break;

		case 's':
			dnsuffix = optarg;
			break;

		case 'p':
			port = atoi( optarg );
			break;

#if !defined(MACOS) && !defined(DOS)
		case 't':	/* copy ber's to given file */
			copyfname = strdup( optarg );
			copyoptions = LBER_TO_FILE;
			break;

		case 'T':	/* only output ber's to given file */
			copyfname = strdup( optarg );
			copyoptions = (LBER_TO_FILE | LBER_TO_FILE_ONLY);
			break;
#endif

		default:
		    ++errflg;
		}
	}

	if ( host == NULL && optind == argc - 1 ) {
		host = argv[ optind ];
		++optind;
	}

	if ( errflg || optind < argc - 1 ) {
		fprintf( stderr, usage, argv[ 0 ] );
		exit( 1 );
	}
	
	printf( "%s( %s, %d )\n", cldapflg ? "cldap_open" : "ldap_init",
		host == NULL ? "(null)" : host, port );

	if ( cldapflg ) {
#ifdef CLDAP
		ld = cldap_open( host, port );
#endif /* CLDAP */
	} else {
		ld = ldap_init( host, port );
	}

	if ( ld == NULL ) {
		perror( "ldap_init" );
		exit(1);
	}

#if !defined(MACOS) && !defined(DOS)
	if ( copyfname != NULL ) {
		if ( (ld->ld_sb.sb_fd = open( copyfname, O_WRONLY | O_CREAT,
		    0600 ))  == -1 ) {
			perror( copyfname );
			exit ( 1 );
		}
		ld->ld_sb.sb_options = copyoptions;
	}
#endif

	bound = 0;
	timeout.tv_sec = 0;
	timeout.tv_usec = 0;
	timelimit.tv_sec = 0;
	timelimit.tv_usec = 0;
	
	(void) memset( line, '\0', sizeof(line) );
	while ( getaline( line, sizeof(line), stdin, "\ncommand? " ) != NULL ) {
		command1 = line[0];
		command2 = line[1];
		command3 = line[2];

		switch ( command1 ) {
		case 'a':	/* add or abandon */
			switch ( command2 ) {
			case 'd':	/* add */
				getaline( dn, sizeof(dn), stdin, "dn? " );
				strcat( dn, dnsuffix );
				if ( (attrs = get_modlist( NULL, "attr? ",
				    "value? " )) == NULL )
					break;
				if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){
					if ((err = ldap_add_ext( ld, dn, attrs, NULL, NULL, &id )) != LDAP_SUCCESS )
						printf( "Error in ldap_add_ext: %s\n", ldap_err2string(err) );
					else
						printf( "Add initiated with id %d\n", id );
				}
				else {
					if ( (id = ldap_add( ld, dn, attrs )) == -1 )
						ldap_perror( ld, "ldap_add" );
					else
						printf( "Add initiated with id %d\n", id );
				}
				
				break;

			case 'b':	/* abandon */
				getaline( line, sizeof(line), stdin, "msgid? " );
				id = atoi( line );
				if ( ldap_abandon( ld, id ) != 0 )
					ldap_perror( ld, "ldap_abandon" );
				else
					printf( "Abandon successful\n" );
				break;
			default:
				printf( "Possibilities: [ad]d, [ab]ort\n" );
			}
			break;

		case 'b':	/* asynch bind */
#ifdef KERBEROS
			getaline( line, sizeof(line), stdin,
			    "method (0->simple, 1->krbv41, 2->krbv42)? " );
			method = atoi( line ) | 0x80;
#else /* KERBEROS */
			method = LDAP_AUTH_SIMPLE;
#endif /* KERBEROS */
			getaline( dn, sizeof(dn), stdin, "dn? " );
			strcat( dn, dnsuffix );

			if ( method == LDAP_AUTH_SIMPLE && dn[0] != '\0' )
				getaline( passwd, sizeof(passwd), stdin,
				    "password? " );
			else
				passwd[0] = '\0';

			if ( ldap_bind( ld, dn, passwd, method ) == -1 ) {
				fprintf( stderr, "ldap_bind failed\n" );
				ldap_perror( ld, "ldap_bind" );
			} else {
				printf( "Bind initiated\n" );
				bound = 1;
			}
			break;

		case 'B':	/* synch bind */
#ifdef KERBEROS
			getaline( line, sizeof(line), stdin,
			    "method 0->simple 1->krbv41 2->krbv42 3->krb? " );
			method = atoi( line );
			if ( method == 3 )
				method = LDAP_AUTH_KRBV4;
			else
				method = method | 0x80;
#else /* KERBEROS */
			getaline( line, sizeof(line), stdin,
					 "method 0->simple, 1->SASL? ");
			method = atoi (line);
			if (method == 1){
				method = LDAP_AUTH_SASL;
				getaline( line, sizeof(line), stdin,
						 "mechanism 0->CRAM_MD5, 1->TLS? ");
				theInt = atoi(line);
				if (theInt == 0){
					mechanism = LDAP_SASL_CRAM_MD5;
				}
				else{
					mechanism = LDAP_SASL_X511_STRONG;
				}
			} else {
				method = LDAP_AUTH_SIMPLE;
			}
			
#endif /* KERBEROS */
			getaline( dn, sizeof(dn), stdin, "dn? " );
			strcat( dn, dnsuffix );

			if ( dn[0] != '\0' )
				getaline( passwd, sizeof(passwd), stdin,
				    "password? " );
			else
				passwd[0] = '\0';

			if (method == LDAP_AUTH_SIMPLE) {
				if ( ldap_bind_s( ld, dn, passwd, method ) !=
					 LDAP_SUCCESS ) {
					fprintf( stderr, "ldap_bind_s failed\n" );
					ldap_perror( ld, "ldap_bind_s" );
				} else {
					printf( "Bind successful\n" );
					bound = 1;
				}
			} else {
				if (strcmp(mechanism, LDAP_SASL_CRAM_MD5) == 0){
					cred.bv_val = passwd;
					cred.bv_len = strlen(passwd);
					
					if ( ldap_sasl_cram_md5_bind_s(ld, dn, &cred, NULL, NULL) != LDAP_SUCCESS ){
						fprintf( stderr, "ldap_sasl_cram_md5_bind_s failed\n" );
						ldap_perror( ld, "ldap_sasl_cram_md5_bind_s" );
					} else {
						printf ( "Bind successful\n");
						bound = 1;
					}
				} else {
					if (ldap_sasl_bind_s(ld, dn, mechanism, &cred, NULL, NULL, &srvcrds ) != LDAP_SUCCESS){
						fprintf( stderr, "ldap_sasl_bind_s failed\n" );
						ldap_perror( ld, "ldap_sasl_bind_s" );
					}
				}
			}
			break;

		case 'c':	/* compare */
			getaline( dn, sizeof(dn), stdin, "dn? " );
			strcat( dn, dnsuffix );
			getaline( attr, sizeof(attr), stdin, "attr? " );
			getaline( value, sizeof(value), stdin, "value? " );

			if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){
				bv.bv_val = value;
				bv.bv_len = strlen(value);
				if ((err = ldap_compare_ext( ld, dn, attr, &bv, NULL, NULL, &id )) != LDAP_SUCCESS )
					printf( "Error in ldap_compare_ext: %s\n", ldap_err2string(err) );
				else
					printf( "Compare initiated with id %d\n", id );
			} else {
				if ( (id = ldap_compare( ld, dn, attr, value )) == -1 )
					ldap_perror( ld, "ldap_compare" );
				else
					printf( "Compare initiated with id %d\n", id );
			}
			break;

		case 'd':	/* turn on debugging */
#ifdef LDAP_DEBUG
			getaline( line, sizeof(line), stdin, "debug level? " );
			ldap_debug = atoi( line );
			if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
				lber_debug = ldap_debug;
			}
#else
			printf( "Compile with -DLDAP_DEBUG for debugging\n" );
#endif
			break;

		case 'E':	/* explode a dn */
			getaline( line, sizeof(line), stdin, "dn? " );
			exdn = ldap_explode_dn( line, 0 );
			for ( i = 0; exdn != NULL && exdn[i] != NULL; i++ ) {
				printf( "\t%s\n", exdn[i] );
			}
			break;

		case 'g':	/* set next msgid */
			getaline( line, sizeof(line), stdin, "msgid? " );
			ld->ld_msgid = atoi( line );
			break;

		case 'v':	/* set version number */
			getaline( line, sizeof(line), stdin, "version? " );
			theInt = atoi(line);
			ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &theInt);
			break;

		case 'm':	/* modify or modifyrdn */
			if ( strncmp( line, "modify", 4 ) == 0 ) {
				getaline( dn, sizeof(dn), stdin, "dn? " );
				strcat( dn, dnsuffix );
				if ( (mods = get_modlist(
				    "mod (0=>add, 1=>delete, 2=>replace -1=>done)? ",
				    "attribute type? ", "attribute value? " ))
				    == NULL )
					break;
				if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){
					if ((err = ldap_modify_ext( ld, dn, mods, NULL, NULL, &id )) != LDAP_SUCCESS )
						printf( "Error in ldap_modify_ext: %s\n", ldap_err2string(err) );
					else
						printf( "Modify initiated with id %d\n", id );
				}
				else {
					if ( (id = ldap_modify( ld, dn, mods )) == -1 )
						ldap_perror( ld, "ldap_modify" );
					else
						printf( "Modify initiated with id %d\n", id );
				}
			} else if ( strncmp( line, "modrdn", 4 ) == 0 ) {
				getaline( dn, sizeof(dn), stdin, "dn? " );
				strcat( dn, dnsuffix );
				getaline( rdn, sizeof(rdn), stdin, "newrdn? " );
				getaline( line, sizeof(line), stdin, "delete old rdn (0=>no, 1=>yes)?");
				delrdn = atoi(line);
				if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){
					if ((err = ldap_rename(ld, dn, rdn, NULL, delrdn, NULL,NULL, &id)) != LDAP_SUCCESS){
						printf( "Error in ldap_rename (modrdn): %s\n", ldap_err2string(err));
					}
					else
						printf( "Modrdn initiated with id %d\n", id );
				}
				else {
					if ( (id = ldap_modrdn( ld, dn, rdn, delrdn )) == -1 )
						ldap_perror( ld, "ldap_modrdn" );
					else
						printf( "Modrdn initiated with id %d\n", id );
				}
			} else {
				printf( "Possibilities: [modi]fy, [modr]dn\n" );
			}
			break;

		case 'q':	/* quit */
#ifdef CLDAP
			if ( cldapflg )
				cldap_close( ld );
#endif /* CLDAP */
			if ( !cldapflg )
				ldap_unbind( ld );
			exit( 0 );
			break;

		case 'r':	/* result or remove */
			switch ( command3 ) {
			case 's':	/* result */
				getaline( line, sizeof(line), stdin,
				    "msgid (-1=>any)? " );
				if ( line[0] == '\0' )
					id = -1;
				else
					id = atoi( line );
				getaline( line, sizeof(line), stdin,
				    "all (0=>any, 1=>all)? " );
				if ( line[0] == '\0' )
					all = 1;
				else
					all = atoi( line );
				
				if (( msgtype = ldap_result( ld, id, all,
				    resultusetimelimit ? &timelimit : &timeout, &res )) < 1 ) {
					ldap_perror( ld, "ldap_result" );
					break;
				}
				printf( "\nresult: msgtype %d msgid %d\n",
				    msgtype, res->lm_msgid );
				handle_result( ld, res );
				if (all || msgtype == LDAP_RES_SEARCH_RESULT)
					resultusetimelimit = 0;
				res = NULLMSG;
				break;

			case 'm':	/* remove */
				getaline( dn, sizeof(dn), stdin, "dn? " );
				strcat( dn, dnsuffix );
				if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){
					if ((err = ldap_delete_ext( ld, dn, NULL, NULL, &id )) != LDAP_SUCCESS )
						printf( "Error in ldap_delete_ext: %s\n", ldap_err2string(err) );
					else
						printf( "Remove initiated with id %d\n", id );
				} else {
					if ( (id = ldap_delete( ld, dn )) == -1 )
						ldap_perror( ld, "ldap_delete" );
					else
						printf( "Remove initiated with id %d\n", id );
				}
				break;

			default:
				printf( "Possibilities: [rem]ove, [res]ult\n" );
				break;
			}
			break;

		case 's':	/* search */
			getaline( dn, sizeof(dn), stdin, "searchbase? " );
			strcat( dn, dnsuffix );
			getaline( line, sizeof(line), stdin,
			    "scope (0=Base, 1=One Level, 2=Subtree)? " );
			scope = atoi( line );
			getaline( filter, sizeof(filter), stdin,
			    "search filter (e.g. sn=jones)? " );
			types = get_list( "attrs to return? " );
			getaline( line, sizeof(line), stdin,
			    "attrsonly (0=attrs&values, 1=attrs only)? " );
			attrsonly = atoi( line );

			if ( cldapflg ) {
#ifdef CLDAP
			    getaline( line, sizeof(line), stdin,
				"Requestor DN (for logging)? " );
			    if ( cldap_search_s( ld, dn, scope, filter, types,
				    attrsonly, &res, line ) != 0 ) {
				ldap_perror( ld, "cldap_search_s" );
			    } else {
				printf( "\nresult: msgid %d\n",
				    res->lm_msgid );
				handle_result( ld, res );
				res = NULLMSG;
			    }
#endif /* CLDAP */
			} else {
				theInt = 0;
				if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){
					resultusetimelimit = 1;
					getaline( line, sizeof(line), stdin,
							 "ldap_search_ext (0=>no, 1=>yes - default: yes)? " );
					if (line[0] == '\0')
						theInt = 1;
					else
						theInt = atoi( line );
				}
				if (theInt){
					getaline(line, sizeof(line), stdin, "time limit?");
					timelimit.tv_sec = atoi(line);
					resultusetimelimit = 1;
					getaline(line, sizeof(line), stdin, "size limit?");
					sizelimit = atoi(line);
					if (( err = ldap_search_ext(ld, dn, scope, filter, types, attrsonly, NULL, NULL,
												&timelimit, sizelimit, &id)) != LDAP_SUCCESS){
						printf( "Error in ldap_search_ext: %s\n", ldap_err2string(err));
					} else {
						printf( "Search initiated with id %d\n", id );
					}
				} else {
					if (( id = ldap_search( ld, dn, scope, filter,
											types, attrsonly  )) == -1 ) {
						ldap_perror( ld, "ldap_search" );
					} else {
						printf( "Search initiated with id %d\n", id );
					}
				}
			}
			free_list( types );
			break;

		case 't':	/* set timeout value */
			getaline( line, sizeof(line), stdin, "timeout? " );
			timeout.tv_sec = atoi( line );
			break;

		case 'U':	/* set ufn search prefix */
			getaline( line, sizeof(line), stdin, "ufn prefix? " );
			ldap_ufn_setprefix( ld, line );
			break;

		case 'u':	/* user friendly search w/optional timeout */
			getaline( dn, sizeof(dn), stdin, "ufn? " );
			strcat( dn, dnsuffix );
			types = get_list( "attrs to return? " );
			getaline( line, sizeof(line), stdin,
			    "attrsonly (0=attrs&values, 1=attrs only)? " );
			attrsonly = atoi( line );

			if ( command2 == 't' ) {
				id = ldap_ufn_search_c( ld, dn, types,
				    attrsonly, &res, ldap_ufn_timeout,
				    &timeout );
			} else {
				id = ldap_ufn_search_s( ld, dn, types,
				    attrsonly, &res );
			}
			if ( res == NULL )
				ldap_perror( ld, "ldap_ufn_search" );
			else {
				printf( "\nresult: err %d\n", id );
				handle_result( ld, res );
				res = NULLMSG;
			}
			free_list( types );
			break;

		case 'l':	/* URL search */
			getaline( line, sizeof(line), stdin,
			    "attrsonly (0=attrs&values, 1=attrs only)? " );
			attrsonly = atoi( line );
			getaline( line, sizeof(line), stdin, "LDAP URL? " );
			if (( id = ldap_url_search( ld, line, attrsonly  ))
				== -1 ) {
			    ldap_perror( ld, "ldap_url_search" );
			} else {
			    printf( "URL search initiated with id %d\n", id );
			}
			break;

		case 'p':	/* parse LDAP URL */
			getaline( line, sizeof(line), stdin, "LDAP URL? " );
			if (( i = ldap_url_parse( line, &ludp )) != 0 ) {
			    fprintf( stderr, "ldap_url_parse: error %d\n", i );
			} else {
			    printf( "\t  host: " );
			    if ( ludp->lud_host == NULL ) {
				printf( "DEFAULT\n" );
			    } else {
				printf( "<%s>\n", ludp->lud_host );
			    }
			    printf( "\t  port: " );
			    if ( ludp->lud_port == 0 ) {
				printf( "DEFAULT\n" );
			    } else {
				printf( "%d\n", ludp->lud_port );
			    }
			    printf( "\t    dn: <%s>\n", ludp->lud_dn );
			    printf( "\t attrs:" );
			    if ( ludp->lud_attrs == NULL ) {
				printf( " ALL" );
			    } else {
				for ( i = 0; ludp->lud_attrs[ i ] != NULL; ++i ) {
				    printf( " <%s>", ludp->lud_attrs[ i ] );
				}
			    }
			    printf( "\n\t scope: %s\n", ludp->lud_scope == LDAP_SCOPE_UNKNOWN ? "DEFAULT (base)" : 
						ludp->lud_scope == LDAP_SCOPE_ONELEVEL ? "ONE" : 
						ludp->lud_scope == LDAP_SCOPE_BASE ? "BASE" :
						ludp->lud_scope == LDAP_SCOPE_SUBTREE ? "SUB" : "**invalid**" );
			    printf( "\tfilter: <%s>\n", ludp->lud_filter ? ludp->lud_filter : "NONE");
				if (ludp->lud_extensions){
					printf("\textensions: \n");
					for (i = 0; ludp->lud_extensions[i] != NULL; i++)
						printf("\t\t%s (%s)\n", ludp->lud_extensions[i]->lue_type, 
							   ludp->lud_extensions[i]->lue_iscritical ? "Critical" : "Non critical");
				}
				
			    ldap_free_urldesc( ludp );
			}
			    break;

		case 'n':	/* set dn suffix, for convenience */
			getaline( line, sizeof(line), stdin, "DN suffix? " );
			strcpy( dnsuffix, line );
			break;

		case 'e':	/* enable cache */
#ifdef NO_CACHE
			printf( NOCACHEERRMSG );
#else /* NO_CACHE */
			getaline( line, sizeof(line), stdin, "Cache timeout (secs)? " );
			i = atoi( line );
			getaline( line, sizeof(line), stdin, "Maximum memory to use (bytes)? " );
			if ( ldap_enable_cache( ld, i, atoi( line )) == 0 ) {
				printf( "local cache is on\n" ); 
			} else {
				printf( "ldap_enable_cache failed\n" ); 
			}
#endif /* NO_CACHE */
			break;

		case 'x':	/* uncache entry */
#ifdef NO_CACHE
			printf( NOCACHEERRMSG );
#else /* NO_CACHE */
			getaline( line, sizeof(line), stdin, "DN? " );
			ldap_uncache_entry( ld, line );
#endif /* NO_CACHE */
			break;

		case 'X':	/* uncache request */
#ifdef NO_CACHE
			printf( NOCACHEERRMSG );
#else /* NO_CACHE */
			getaline( line, sizeof(line), stdin, "request msgid? " );
			ldap_uncache_request( ld, atoi( line ));
#endif /* NO_CACHE */
			break;

		case 'o':	/* set ldap options */
			getaline( line, sizeof(line), stdin, "alias deref (0=never, 1=searching, 2=finding, 3=always)?" );
			theInt = atoi(line);
			ldap_set_option(ld, LDAP_OPT_DEREF, &theInt );
			getaline( line, sizeof(line), stdin, "timelimit?" );
			theInt = atoi(line);
			ldap_set_option(ld, LDAP_OPT_TIMELIMIT,  &theInt);
			getaline( line, sizeof(line), stdin, "sizelimit?" );
			theInt = atoi(line);
			ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &theInt);

			ld->ld_options = 0;

#ifdef STR_TRANSLATION
			getaline( line, sizeof(line), stdin,
				"Automatic translation of T.61 strings (0=no, 1=yes)?" );
			if ( atoi( line ) == 0 ) {
				ld->ld_lberoptions &= ~LBER_TRANSLATE_STRINGS;
			} else {
				ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
#ifdef LDAP_CHARSET_8859
				getaline( line, sizeof(line), stdin,
					"Translate to/from ISO-8859 (0=no, 1=yes?" );
				if ( atoi( line ) != 0 ) {
					ldap_set_string_translators( ld,
					    ldap_8859_to_t61,
					    ldap_t61_to_8859 );
				}
#endif /* LDAP_CHARSET_8859 */
			}
#endif /* STR_TRANSLATION */

#ifdef LDAP_DNS
			getaline( line, sizeof(line), stdin,
				"Use DN & DNS to determine where to send requests (0=no, 1=yes)?" );
			if ( atoi( line ) != 0 ) {
				ld->ld_options |= LDAP_OPT_DNS;
			}
#endif /* LDAP_DNS */

			getaline( line, sizeof(line), stdin,
				"Recognize and chase referrals (0=no, 1=yes)?" );
			if ( atoi( line ) != 0 ) {
				theInt = LDAP_OPT_ON;
				getaline( line, sizeof(line), stdin,
						 "Prompt for bind credentials when chasing referrals (0=no, 1=yes)?" );
				if ( atoi( line ) != 0 ) {
					ldap_set_option( ld, LDAP_OPT_REBIND_FN, bind_prompt );
				}
			} else {
				theInt = LDAP_OPT_OFF;
			}
			ldap_set_option(ld, LDAP_OPT_REFERRALS, &theInt);
			break;

		case 'k': /* Set some controls */
			getaline( line, sizeof(line), stdin, 
					 "Set control: (0 for none, 1 for ManageDSA, 2 for preferredLang, 3 for BAD)?");
			theInt = atoi(line);
			switch (theInt){
			case 0:
				ldap_set_option(ld, LDAP_OPT_SERVER_CONTROLS, NULL);
				break;
			case 1:
				aCtrl.ldctl_oid = "2.16.840.1.113730.3.4.2";
				aCtrl.ldctl_iscritical = 1;
				aCtrl.ldctl_value = NULL;
				ldap_set_option(ld, LDAP_OPT_SERVER_CONTROLS, ctrls);
				break;
			case 2:
				getaline( line, sizeof(line), stdin,
						 "Preferred Language Control : lang ?");
				aCtrl.ldctl_oid = "1.3.6.1.4.1.1466.20035";
				aCtrl.ldctl_iscritical = 1;
				bv.bv_val = strdup(line);
				bv.bv_len = strlen(line);
				aCtrl.ldctl_value = &bv;
				ldap_set_option(ld, LDAP_OPT_SERVER_CONTROLS, ctrls);
				break;
			default:
				getaline( line, sizeof(line), stdin,
						 "Bad Control is critical (0=false, 1=true)?");
				aCtrl.ldctl_oid = "1.1.1.1.1.1";
				aCtrl.ldctl_iscritical = atoi(line);
				aCtrl.ldctl_value = NULL;
				ldap_set_option(ld, LDAP_OPT_SERVER_CONTROLS, ctrls);
				break;
			}
			break;
		  
		case 'O':	/* set cache options */
#ifdef NO_CACHE
			printf( NOCACHEERRMSG );
#else /* NO_CACHE */
			getaline( line, sizeof(line), stdin, "cache errors (0=smart, 1=never, 2=always)?" );
			switch( atoi( line )) {
			case 0:
				ldap_set_cache_options( ld, 0 );
				break;
			case 1:
				ldap_set_cache_options( ld,
					LDAP_CACHE_OPT_CACHENOERRS );
				break;
			case 2:
				ldap_set_cache_options( ld,
					LDAP_CACHE_OPT_CACHEALLERRS );
				break;
			default:
				printf( "not a valid cache option\n" );
			}
#endif /* NO_CACHE */
			break;

		case '?':	/* help */
    printf( "Commands: [ad]d         [ab]andon         [b]ind\n" );
    printf( "          [B]ind async  [c]ompare         [l]URL search\n" );
    printf( "          [modi]fy      [modr]dn          [rem]ove\n" );
    printf( "          [res]ult      [s]earch          [q]uit/unbind\n\n" );
    printf( "          [u]fn search  [ut]fn search with timeout\n" );
    printf( "          [d]ebug       [e]nable cache    set ms[g]id\n" );
    printf( "          d[n]suffix    [t]imeout         [v]ersion\n" );
    printf( "          [U]fn prefix  [x]uncache entry  [X]uncache request\n" );
    printf( "          [?]help       [o]ptions         [O]cache options\n" );
    printf( "          [E]xplode dn  [p]arse LDAP URL\n" );
			break;

		default:
			printf( "Invalid command.  Type ? for help.\n" );
			break;
		}

		(void) memset( line, '\0', sizeof(line) );
	}

	return( 0 );
}

static void
handle_result( LDAP *ld, LDAPMessage *lm )
{
	switch ( lm->lm_msgtype ) {
	case LDAP_RES_COMPARE:
		printf( "Compare result\n" );
		print_ldap_result( ld, lm, "compare" );
		break;

	case LDAP_RES_SEARCH_RESULT:
		printf( "Search result\n" );
		print_ldap_result( ld, lm, "search" );
		break;

	case LDAP_RES_SEARCH_REFERENCE:
		printf( "Search reference\n" );
		print_search_entry( ld, lm );
		break;
		
	case LDAP_RES_SEARCH_ENTRY:
		printf( "Search entry\n" );
		print_search_entry( ld, lm );
		break;

	case LDAP_RES_ADD:
		printf( "Add result\n" );
		print_ldap_result( ld, lm, "add" );
		break;

	case LDAP_RES_DELETE:
		printf( "Delete result\n" );
		print_ldap_result( ld, lm, "delete" );
		break;

	case LDAP_RES_MODIFY:
		printf( "Modify result\n" );
		print_ldap_result( ld, lm, "modify" );
		break;

	case LDAP_RES_MODRDN:
		printf( "ModRDN result\n" );
		print_ldap_result( ld, lm, "modrdn" );
		break;

	case LDAP_RES_BIND:
		printf( "Bind result\n" );
		print_ldap_result( ld, lm, "bind" );
		break;

	default:
		printf( "Unknown result type 0x%x\n", lm->lm_msgtype );
		print_ldap_result( ld, lm, "unknown" );
	}
}

static void
print_ldap_result( LDAP *ld, LDAPMessage *lm, char *s )
{
	int rc, i;
	int errCode;
	char *matched = NULL, *errMsg = NULL, **referrals = NULL;
	LDAPControl **srvctrls = NULL;
	
	if ((rc = ldap_parse_result(ld, lm, &errCode, &matched, &errMsg, &referrals, &srvctrls, 0)) != LDAP_SUCCESS){
		fprintf(stderr, "%s: error while parsing result (%s)\n", s, ldap_err2string(rc));
		return;
	}
	

	fprintf(stderr, "%s: %s\n", s, ldap_err2string(errCode));
	if (errCode == LDAP_REFERRAL){
		fprintf(stderr, "\tReferrals returned: \n");
		for (i = 0; referrals[i] != NULL; i++)
			fprintf(stderr, "\t\t%s\n", referrals[i]);
	}
	if (errMsg && *errMsg)
		fprintf(stderr, "\tAdditional info: %s\n", errMsg);
	free(errMsg);
	if (NAME_ERROR(errCode) && matched && *matched){
		fprintf(stderr, "\tMatched DN: %s\n", matched);
		free(matched);
	}
	if (srvctrls != NULL){
		fprintf(stderr, "\tLDAPControls returned: \n");
		for (i=0;srvctrls[i] != NULL; i++)
			fprintf(stderr, "\t\t%s (%s)\n", srvctrls[i]->ldctl_oid, srvctrls[i]->ldctl_iscritical ? "Critical" : "Not critical");
	}
	return;
}

static void
print_search_entry( LDAP *ld, LDAPMessage *res )
{
	BerElement	*ber;
	char		*a, *dn, *ufn;
	struct berval	**vals;
	int		i;
	LDAPMessage	*e;

	for ( e = ldap_first_message( ld, res ); e != NULLMSG;
	    e = ldap_next_message( ld, e ) ) {
		if ( e->lm_msgtype == LDAP_RES_SEARCH_RESULT )
			break;

		dn = ldap_get_dn( ld, e );
		printf( "\tDN: %s\n", dn );
		
		ufn = ldap_dn2ufn( dn );
		printf( "\tUFN: %s\n", ufn );
		free( dn );
		free( ufn );
			
		if ( e->lm_msgtype == LDAP_RES_SEARCH_REFERENCE ){
			char **urls = ldap_get_reference_urls(ld, e);
			if (urls == NULL){
				printf("\t\tError with references: %s\n", ldap_err2string(ld->ld_errno));
			} else {
				for (i=0;urls[i] != NULL;i++)
					printf("\t\tURL: %s\n", urls[i]);
			}
		} else {
			for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL;
				  a = ldap_next_attribute( ld, e, ber ) ) {
				printf( "\t\tATTR: %s\n", a );
				if ( (vals = ldap_get_values_len( ld, e, a ))
					 == NULL ) {
					printf( "\t\t\t(no values)\n" );
				} else {
					for ( i = 0; vals[i] != NULL; i++ ) {
						int	j, nonascii;
						
						nonascii = 0;
						for ( j = 0; j < vals[i]->bv_len; j++ )
							if ( !isascii( vals[i]->bv_val[j] ) ) {
							nonascii = 1;
							break;
							}
						
						if ( nonascii ) {
							printf( "\t\t\tlength (%ld) (not ascii)\n", vals[i]->bv_len );
#ifdef BPRINT_NONASCII
							lber_bprint( vals[i]->bv_val,
										 vals[i]->bv_len );
#endif /* BPRINT_NONASCII */
							continue;
						}
						printf( "\t\t\tlength (%ld) %s\n",
								vals[i]->bv_len, vals[i]->bv_val );
					}
					ber_bvecfree( vals );
				}
			}
		}
	}
	
	if ( res->lm_msgtype == LDAP_RES_SEARCH_RESULT
	    || res->lm_chain != NULLMSG )
		print_ldap_result( ld, res, "search" );
}