go500gw.c 19.2 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
2
3
4
5
6
7
8
9
10
11
12
/*
 * Copyright (c) 1990 Regents of the University of Michigan.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that this notice is preserved and that due credit is given
 * to the University of Michigan at Ann Arbor. The name of the University
 * may not be used to endorse or promote products derived from this
 * software without specific prior written permission. This software
 * is provided ``as is'' without express or implied warranty.
 */

Kurt Zeilenga's avatar
Kurt Zeilenga committed
13
14
#include "portable.h"

Kurt Zeilenga's avatar
Kurt Zeilenga committed
15
16
17
18
#include <stdio.h>
#include <ctype.h>
#include <signal.h>

Kurt Zeilenga's avatar
Kurt Zeilenga committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <ac/time.h>
#include <ac/socket.h>
#include <ac/string.h>
#include <ac/syslog.h>
#include <ac/unistd.h>
#include <ac/wait.h>

#include <sys/resource.h>

#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif

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

#include "ldapconfig.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

int	debug;
int	dosyslog;
int	inetd;
int	dtblsize;

char		*ldaphost = LDAPHOST;
int		ldapport = LDAP_PORT;
int		searchaliases = 1;
char		*helpfile = GO500GW_HELPFILE;
char		*filterfile = FILTERFILE;
char		*templatefile = TEMPLATEFILE;
char		*friendlyfile = FRIENDLYFILE;
int		rdncount = GO500GW_RDNCOUNT;

static set_socket();
Kurt Zeilenga's avatar
Kurt Zeilenga committed
53
static RETSIGTYPE wait4child();
Kurt Zeilenga's avatar
Kurt Zeilenga committed
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
static do_queries();
static do_menu();
static do_list();
static do_search();
static do_read();
static do_help();
static do_sizelimit();
static do_error();
extern int strcasecmp();

char	myhost[MAXHOSTNAMELEN];
int	myport = GO500GW_PORT;

static usage( name )
char	*name;
{
	fprintf( stderr, "usage: %s [-d debuglevel] [-I] [-p port] [-P ldapport] [-l]\r\n\t[-x ldaphost] [-a] [-h helpfile] [-f filterfile] [-t templatefile] [-c rdncount]\r\n", name );
	exit( 1 );
}

main (argc, argv)
int	argc;
char	**argv;
{
	int			s, ns, rc;
	int			port = -1;
	int			i, pid;
	char			*myname;
	fd_set			readfds;
	struct hostent		*hp;
	struct sockaddr_in	from;
	int			fromlen;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
86
	RETSIGTYPE			wait4child();
Kurt Zeilenga's avatar
Kurt Zeilenga committed
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
	extern char		*optarg;
	extern char		**Argv;
	extern int		Argc;

	/* for setproctitle */
        Argv = argv;
        Argc = argc;

	while ( (i = getopt( argc, argv, "P:ad:f:h:lp:t:x:Ic:" )) != EOF ) {
		switch( i ) {
		case 'a':	/* search aliases */
			searchaliases = 0;
			break;

		case 'd':	/* debugging level */
			debug = atoi( optarg );
#ifdef LDAP_DEBUG
			ldap_debug = debug;
#else
			fprintf( stderr, "warning: ldap debugging requires LDAP_DEBUG\n" );
#endif
			break;

		case 'f':	/* ldap filter file */
			filterfile = strdup( optarg );
			break;

		case 'h':	/* gopher help file */
			helpfile = strdup( optarg );
			break;

		case 'l':	/* log to LOG_LOCAL3 */
			dosyslog = 1;
			break;

		case 'p':	/* port to listen on */
			port = atoi( optarg );
			break;

		case 'P':	/* port to connect to ldap server */
			ldapport = atoi( optarg );
			break;

		case 't':	/* ldap template file */
			templatefile = strdup( optarg );
			break;

		case 'x':	/* ldap server hostname */
			ldaphost = strdup( optarg );
			break;

		case 'I':	/* run from inetd */
			inetd = 1;
			break;

		case 'c':	/* count of DN components to show */
			rdncount = atoi( optarg );
			break;

		default:
			usage( argv[0] );
		}
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
151
#ifdef HAVE_SYSCONF
Kurt Zeilenga's avatar
Kurt Zeilenga committed
152
	dtblsize = sysconf( _SC_OPEN_MAX );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
153
#elif HAVE_GETDTABLESIZE
Kurt Zeilenga's avatar
Kurt Zeilenga committed
154
	dtblsize = getdtablesize();
Kurt Zeilenga's avatar
Kurt Zeilenga committed
155
156
157
158
159
160
161
162
163
164
165
#else
	dtblsize = 32;
#endif

#ifdef FD_SETSIZE
	if ( dtblsize > FD_SETSIZE ) {
		dtblsize = FD_SETSIZE;
	}
#endif	/* FD_SETSIZE*/


Kurt Zeilenga's avatar
Kurt Zeilenga committed
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

#ifdef GO500GW_HOSTNAME
	strcpy( myhost, GO500GW_HOSTNAME );
#else
	if ( myhost[0] == '\0' && gethostname( myhost, sizeof(myhost) )
	    == -1 ) {
		perror( "gethostname" );
		exit( 1 );
	}
#endif

	/* detach if stderr is redirected or no debugging */
	if ( inetd == 0 )
		(void) detach( debug );

	if ( (myname = strrchr( argv[0], '/' )) == NULL )
		myname = strdup( argv[0] );
	else
		myname = strdup( myname + 1 );

	if ( dosyslog ) {
#ifdef LOG_LOCAL3
		openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL3 );
#else
		openlog( myname, OPENLOG_OPTIONS );
#endif
	}
	if ( dosyslog )
		syslog( LOG_INFO, "initializing" );

	/* set up the socket to listen on */
	if ( inetd == 0 ) {
		s = set_socket( port );

		/* arrange to reap children */
		(void) signal( SIGCHLD, (void *) wait4child );
	}

	if ( inetd ) {
		fromlen = sizeof(from);
		if ( getpeername( 0, (struct sockaddr *) &from, &fromlen )
		    == 0 ) {
			hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
			    sizeof(from.sin_addr.s_addr), AF_INET );
			Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
			    (hp == NULL) ? "unknown" : hp->h_name,
			    inet_ntoa( from.sin_addr ), 0 );

			if ( dosyslog ) {
				syslog( LOG_INFO, "connection from %s (%s)",
				    (hp == NULL) ? "unknown" : hp->h_name,
				    inet_ntoa( from.sin_addr ) );
			}

			setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
			    hp->h_name );
		}

		do_queries( 0 );

		close( 0 );

		exit( 0 );
	}

	for ( ;; ) {
		FD_ZERO( &readfds );
		FD_SET( s, &readfds );

		if ( (rc = select( dtblsize, &readfds, 0, 0 ,0 )) == -1 ) {
			if ( debug ) perror( "select" );
			continue;
		} else if ( rc == 0 ) {
			continue;
		}

		if ( ! FD_ISSET( s, &readfds ) )
			continue;

		fromlen = sizeof(from);
		if ( (ns = accept( s, (struct sockaddr *) &from, &fromlen ))
		    == -1 ) {
			if ( debug ) perror( "accept" );
			exit( 1 );
		}

		hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
		    sizeof(from.sin_addr.s_addr), AF_INET );

		if ( dosyslog ) {
			syslog( LOG_INFO, "TCP connection from %s (%s)",
			    (hp == NULL) ? "unknown" : hp->h_name,
			    inet_ntoa( from.sin_addr ) );
		}

		switch( pid = fork() ) {
		case 0:		/* child */
			close( s );
			do_queries( ns );
			break;

		case -1:	/* failed */
			perror( "fork" );
			break;

		default:	/* parent */
			close( ns );
			if ( debug )
				fprintf( stderr, "forked child %d\n", pid );
			break;
		}
	}
	/* NOT REACHED */
}

static set_socket( port )
int	port;
{
	int			s, one;
	struct sockaddr_in	addr;

	if ( port == -1 )
		port = GO500GW_PORT;
	myport = port;

	if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
                perror( "socket" );
                exit( 1 );
        }

        /* set option so clients can't keep us from coming back up */
	one = 1;
        if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
	    sizeof(one) ) < 0 ) {
                perror( "setsockopt" );
                exit( 1 );
        }

        /* bind to a name */
        addr.sin_family = AF_INET;
        addr.sin_addr.s_addr = INADDR_ANY;
        addr.sin_port = htons( port );
        if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
                perror( "bind" );
                exit( 1 );
        }

	/* listen for connections */
        if ( listen( s, 5 ) == -1 ) {
                perror( "listen" );
                exit( 1 );
        }

        if ( debug )
		printf( "go500gw listening on port %d\n", port );

	return( s );
}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
325
static RETSIGTYPE
Kurt Zeilenga's avatar
Kurt Zeilenga committed
326
327
wait4child()
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
328
329
330
331
332
333
334
335
336
337
338
339
340
#ifndef HAVE_WAITPID
	WAITSTATUSTYPE     status;
#endif

	if ( debug ) printf( "parent: catching child status\n" );

#ifdef HAVE_WAITPID
	while (waitpid ((pid_t) -1, NULL, WAIT_FLAGS) > 0)
		;	/* NULL */
#else 
	while (wait3( &status, WAIT_FLAGS, 0 ) > 0 )
		;	/* NULL */
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
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

	(void) signal( SIGCHLD, (void *) wait4child );
}

static do_queries( s )
int	s;
{
	char		buf[1024], *query;
	int		len;
	FILE		*fp;
	int		rc;
	struct timeval	timeout;
	fd_set		readfds;
	LDAP		*ld;

	if ( (fp = fdopen( s, "a+")) == NULL ) {
		perror( "fdopen" );
		exit( 1 );
	}

	timeout.tv_sec = GO500GW_TIMEOUT;
	timeout.tv_usec = 0;
	FD_ZERO( &readfds );
	FD_SET( fileno( fp ), &readfds );

	if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
		exit( 1 );

	if ( fgets( buf, sizeof(buf), fp ) == NULL )
		exit( 1 );

	len = strlen( buf );
	if ( debug ) {
		fprintf( stderr, "got %d bytes\n", len );
#ifdef LDAP_DEBUG
		lber_bprint( buf, len );
#endif
	}

	/* strip of \r \n */
	if ( buf[len - 1] == '\n' )
		buf[len - 1] = '\0';
	len--;
	if ( buf[len - 1] == '\r' )
		buf[len - 1] = '\0';
	len--;

	query = buf;

	/* strip off leading white space */
	while ( isspace( *query )) {
		++query;
		--len;
	}

	rewind(fp);

	if ( *query == 'H' || *query == 'L' || *query == 'E' ) {
		switch ( *query++ ) {
		case 'H':	/* help file */
			do_help( fp );
			break;

		case 'L':	/* size limit explanation */
			do_sizelimit( fp, *query );
			break;

		case 'E':	/* error explanation */
			do_error( fp, query );
			break;
		}

		fprintf( fp, ".\r\n" );
		rewind(fp);

		exit( 0 );
		/* NOT REACHED */
	}

	if ( (ld = ldap_open( ldaphost, ldapport )) == NULL ) {
		if ( debug ) perror( "ldap_open" );
		fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
		    LDAP_SERVER_DOWN, myhost, myport );
		fprintf( fp, ".\r\n" );
		rewind(fp);
		exit( 1 );
	}

	ld->ld_deref = LDAP_DEREF_ALWAYS;
	if ( !searchaliases )
		ld->ld_deref = LDAP_DEREF_FINDING;

	if ( (rc = ldap_simple_bind_s( ld, GO500GW_BINDDN, NULL ))
	    != LDAP_SUCCESS ) {
		if ( debug ) ldap_perror( ld, "ldap_simple_bind_s" );
		fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
		    rc, myhost, myport );
		fprintf( fp, ".\r\n" );
		rewind(fp);
		exit( 1 );
	}

	switch ( *query++ ) {
	case 'R':	/* read an entry */
		do_read( ld, fp, query );
		break;

	case 'S':	/* search */
		do_search( ld, fp, query, 1 );
		break;

	case 'M':	/* X.500 menu */
		do_menu( ld, fp, query );
		break;

	default:
		do_menu( ld, fp, "" );
		break;
	}

	fprintf( fp, ".\r\n" );
	rewind(fp);

	exit( 0 );
	/* NOT REACHED */
}

static char *pick_oc( oclist )
char	**oclist;
{
	int	i;

	if ( oclist == NULL )
		return( "unknown" );

	for ( i = 0; oclist[i] != NULL; i++ ) {
		if ( strcasecmp( oclist[i], "top" ) != 0 &&
		    strcasecmp( oclist[i], "quipuObject" ) != 0 &&
		    strcasecmp( oclist[i], "quipuNonLeafObject" ) != 0 )
			return( oclist[i] );
	}

	return( "unknown" );
}

static isnonleaf( ld, oclist, dn )
LDAP	*ld;
char	**oclist;
char	*dn;
{
	int	i, quipuobject = 0;

	if ( oclist == NULL )
		return( 0 );

	for ( i = 0; oclist[i] != NULL; i++ ) {
		if ( strcasecmp( oclist[i], "quipuObject" ) == 0 )
			quipuobject = 1;
		if ( strcasecmp( oclist[i], "quipuNonLeafObject" ) == 0 ||
		    strcasecmp( oclist[i], "externalNonLeafObject" ) == 0 )
			return( 1 );
	}

	/*
	 * not a quipu thang - no easy way to tell leaves from nonleaves
	 * except by trying to search or list.  ldap only lets us search.
	 */

	/* expensive - just guess for now */
	return( quipuobject ? 0 : 1 );

#ifdef notdef
	if ( !quipuobject ) {
		int		rc, numentries;
		struct timeval	timeout;
		LDAPMessage	*res = NULL;
		static char	*attrs[] = { "objectClass", 0 };

		timeout.tv_sec = GO500GW_TIMEOUT;
		timeout.tv_usec = 0;
		ld->ld_sizelimit = 1;
		if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
		    "(objectClass=*)", attrs, 0, &timeout, &res ))
		    == LDAP_SUCCESS || rc == LDAP_SIZELIMIT_EXCEEDED ) {
			ld->ld_sizelimit = LDAP_NO_LIMIT;

			numentries = ldap_count_entries( ld, res );
			if ( res != NULL )
				ldap_msgfree( res );
			return( numentries == 1 ? 1 : 0 );
		}
	}

	return( 0 );
#endif
}

static do_menu( ld, fp, dn )
LDAP	*ld;
FILE	*fp;
char	*dn;
{
	char		**s;
	char		*rdn = NULL;
	FriendlyMap	*fm = NULL;

	if ( strcmp( dn, "" ) != 0 ) {
		s = ldap_explode_dn( dn, 1 );

		if ( s[1] == NULL )
			rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
		else
			rdn = s[0];
		fprintf( fp, "0Read %s entry\tR%s\t%s\t%d\r\n", rdn ? rdn: s[0],
		    dn, myhost, myport );

		ldap_value_free( s );
	} else {
		fprintf( fp, "0About the Gopher to X.500 Gateway\tH\t%s\t%d\r\n",
		    myhost, myport );
	}

	fprintf( fp, "7Search %s\tS%s\t%s\t%d\r\n", rdn ? rdn : "root", dn,
	    myhost, myport );

	do_list( ld, fp, dn );

	ldap_free_friendlymap( &fm );
}

static do_list( ld, fp, dn )
LDAP	*ld;
FILE	*fp;
char	*dn;
{
	int		rc;
	LDAPMessage	*e, *res;
	struct timeval	timeout;
	FriendlyMap	*fm = NULL;
	static char	*attrs[] = { "objectClass", 0 };

	timeout.tv_sec = GO500GW_TIMEOUT;
	timeout.tv_usec = 0;
	ld->ld_deref = LDAP_DEREF_FINDING;
	if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
	    "(!(objectClass=dSA))", attrs, 0, &timeout, &res )) != LDAP_SUCCESS
	    && rc != LDAP_SIZELIMIT_EXCEEDED ) {
		fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
		    rc, myhost, myport );
		return;
	}
	ld->ld_deref = LDAP_DEREF_ALWAYS;

	if ( ldap_count_entries( ld, res ) < 1 ) {
		return;
	}

#ifdef GO500GW_SORT_ATTR
	ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
#endif

	fm = NULL;
	for ( e = ldap_first_entry( ld, res ); e != NULL;
	    e = ldap_next_entry( ld, e ) ) {
		char	**s, **oc;
		char	*rdn, *doc;

		dn = ldap_get_dn( ld, e );
		s = ldap_explode_dn( dn, 1 );
		oc = ldap_get_values( ld, e, "objectClass" );

		doc = pick_oc( oc );
		if ( strcasecmp( doc, "country" ) == 0 ) {
			rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
		} else {
			rdn = s[0];
		}
		if ( rdn == NULL ) {
			rdn = s[0];
		}

		if ( strncasecmp( rdn, "{ASN}", 5 ) != 0 ) {
			if ( isnonleaf( ld, oc, dn ) ) {
				fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", rdn,
				    doc, dn, myhost, myport );
			} else {
				fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", rdn,
				    doc, dn, myhost, myport );
			}
		}

		free( dn );
		ldap_value_free( s );
		ldap_value_free( oc );
	}
	ldap_free_friendlymap( &fm );

	if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
		fprintf( fp, "0A size limit was exceeded (explanation)\tLL\t%s\t%d\r\n",
		    myhost, myport );
	}
}

static isoc( ocl, oc )
char	**ocl;
char	*oc;
{
	int	i;

	for ( i = 0; ocl[i] != NULL; i++ ) {
		if ( strcasecmp( ocl[i], oc ) == 0 )
			return( 1 );
	}

	return( 0 );
}

static int make_scope( ld, dn )
LDAP	*ld;
char	*dn;
{
	int		scope;
	char		**oc;
	LDAPMessage	*res;
	struct timeval	timeout;
	static char	*attrs[] = { "objectClass", 0 };

	if ( strcmp( dn, "" ) == 0 )
		return( LDAP_SCOPE_ONELEVEL );

	timeout.tv_sec = GO500GW_TIMEOUT;
	timeout.tv_usec = 0;
	if ( ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
	    attrs, 0, &timeout, &res ) != LDAP_SUCCESS ) {
		return( -1 );
	}

	oc = ldap_get_values( ld, ldap_first_entry( ld, res ), "objectClass" );

	if ( isoc( oc, "organization" ) || isoc( oc, "organizationalUnit" ) )
		scope = LDAP_SCOPE_SUBTREE;
	else
		scope = LDAP_SCOPE_ONELEVEL;

	ldap_value_free( oc );
	ldap_msgfree( res );

	return( scope );
}

static do_search( ld, fp, query )
LDAP	*ld;
FILE	*fp;
char	*query;
{
	int		scope;
	char		*base, *filter;
	char		*filtertype;
	int		count, rc;
	struct timeval	timeout;
	LDAPFiltInfo	*fi;
	LDAPMessage	*e, *res;
	LDAPFiltDesc	*filtd;
	static char	*attrs[] = { "objectClass", 0 };

	if ( (filter = strchr( query, '\t' )) == NULL ) {
		fprintf( fp, "3Missing filter!\r\n" );
		exit( 1 );
	}
	*filter++ = '\0';
	base = query;

#ifdef GO500GW_UFN
	if ( strchr( filter, ',' ) != NULL ) {
		ldap_ufn_setprefix( ld, base );
		timeout.tv_sec = GO500GW_TIMEOUT;
		timeout.tv_usec = 0;
		ldap_ufn_timeout( (void *) &timeout );
		ld->ld_deref = LDAP_DEREF_FINDING;

		if ( (rc = ldap_ufn_search_s( ld, filter, attrs, 0, &res ))
		    != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
			fprintf(fp,
			    "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
			    rc, myhost, myport );
			return;
		}

		count = ldap_count_entries( ld, res );
	} else {
#endif
		if ( (scope = make_scope( ld, base )) == -1 ) {
			fprintf( fp, "3Bad scope\r\n" );
			exit( 1 );
		}

		filtertype = (scope == LDAP_SCOPE_ONELEVEL ?
		    "go500gw onelevel" : "go500gw subtree");
		ld->ld_deref = (scope == LDAP_SCOPE_ONELEVEL ?
		    LDAP_DEREF_FINDING : LDAP_DEREF_ALWAYS);
		timeout.tv_sec = GO500GW_TIMEOUT;
		timeout.tv_usec = 0;

		if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
			fprintf( stderr, "Cannot open filter file (%s)\n",
			    filterfile );
			exit( 1 );
		}

		count = 0;
		res = NULL;
		for ( fi = ldap_getfirstfilter( filtd, filtertype, filter );
		    fi != NULL; fi = ldap_getnextfilter( filtd ) )
		{
			if ( (rc = ldap_search_st( ld, base, scope,
			    fi->lfi_filter, attrs, 0, &timeout, &res ))
			    != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
				fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
				    rc, myhost, myport );
				return( 1 );
			}
			if ( (count = ldap_count_entries( ld, res )) != 0 )
				break;
		}
		ld->ld_deref = LDAP_DEREF_ALWAYS;
		ldap_getfilter_free( filtd );
#ifdef GO500GW_UFN
	}
#endif

	if ( count == 0 ) {
		return( 0 );
	}

	if ( count == 1 ) {
		char	*dn, **s, **oc;
		int	rc;

		e = ldap_first_entry( ld, res );
		oc = ldap_get_values( ld, e, "objectClass" );
		if ( isnonleaf( ld, oc, dn ) ) {
			dn = ldap_get_dn( ld, e );

			rc = do_menu( ld, fp, dn );

			free( dn );
			return( rc );
		}

		ldap_value_free( oc );
	}

#ifdef GO500GW_SORT_ATTR
	ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
#endif

	for ( e = ldap_first_entry( ld, res ); e != NULL;
	    e = ldap_next_entry( ld, e ) ) {
		char	**s, **oc;
		char	*dn;

		dn = ldap_get_dn( ld, e );
		s = ldap_explode_dn( dn, 1 );
		oc = ldap_get_values( ld, e, "objectClass" );

		if ( isnonleaf( ld, oc, dn ) )
			fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", s[0],
			    pick_oc( oc ), dn, myhost, myport );
		else
			fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", s[0],
			    pick_oc( oc ), dn, myhost, myport );

		free( dn );
		ldap_value_free( s );
		ldap_value_free( oc );
	}

	if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
		fprintf( fp, "0A size limit was exceeded (explanation)\tLS\t%s\t%d\r\n",
		    myhost, myport );
	}
}


static int
entry2textwrite( void *fp, char *buf, int len )
{
        return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
}

static do_read( ld, fp, dn )
LDAP	*ld;
FILE	*fp;
char	*dn;
{
	static struct ldap_disptmpl *tmpllist;

	ldap_init_templates( templatefile, &tmpllist );

	if ( ldap_entry2text_search( ld, dn, NULL, NULL, tmpllist, NULL, NULL,
	    entry2textwrite,(void *) fp, "\r\n", rdncount, 0 )
	    != LDAP_SUCCESS ) {
		fprintf(fp,
		    "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
		    ld->ld_errno, myhost, myport );
	}

	if ( tmpllist != NULL ) {
		ldap_free_templates( tmpllist );
	}
}

static do_help( op )
FILE	*op;
{
	FILE	*fp;
	char	line[BUFSIZ];

	if ( (fp = fopen( helpfile, "r" )) == NULL ) {
		fprintf( op, "Cannot access helpfile (%s)\r\n", helpfile );
		return;
	}

	while ( fgets( line, sizeof(line), fp ) != NULL ) {
		line[ strlen( line ) - 1 ] = '\0';

		fprintf( op, "%s\r\n", line );
	}

	fclose( fp );
}

static do_sizelimit( fp, type )
FILE	*fp;
char	type;
{
	if ( type == 'S' ) {
		fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
		fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
		fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
		fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
	} else {
		fprintf( fp, "Not all entries could be returned because a size limit was exceeded.\r\n" );
		fprintf( fp, "There is no way to defeat this feature, but if you know who you are\r\n" );
		fprintf( fp, "looking for, try choosing the \"Search\" option listed above and\r\n" );
		fprintf( fp, "specifying the name of the person you want.\r\n" );
	}
	fprintf( fp, ".\r\n" );
}

static do_error( fp, s )
FILE	*fp;
char	*s;
{
	int	code;

	code = atoi( s );

	fprintf( fp, "An error occurred searching X.500.  The error code was %d\r\n", code );
	fprintf( fp, "The corresponding error is: %s\r\n", ldap_err2string( code ) );
	fprintf( fp, "No additional information is available\r\n" );
	fprintf( fp, ".\r\n" );
}