]> git.sur5r.net Git - openldap/blob - clients/gopher/go500gw.c
725d2b1c538bef7b483e5c4d3bd106a31d56836c
[openldap] / clients / gopher / go500gw.c
1 /*
2  * Copyright (c) 1990 Regents of the University of Michigan.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of Michigan at Ann Arbor. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16 #include <ctype.h>
17 #include <signal.h>
18
19 #include <ac/time.h>
20 #include <ac/socket.h>
21 #include <ac/string.h>
22 #include <ac/syslog.h>
23 #include <ac/unistd.h>
24 #include <ac/wait.h>
25
26 #include <sys/resource.h>
27
28 #ifdef HAVE_SYS_PARAM_H
29 #include <sys/param.h>
30 #endif
31
32 #include "lber.h"
33 #include "ldap.h"
34 #include "disptmpl.h"
35
36 #include "ldapconfig.h"
37
38 int     debug;
39 int     dosyslog;
40 int     inetd;
41 int     dtblsize;
42
43 char            *ldaphost = LDAPHOST;
44 int             ldapport = LDAP_PORT;
45 int             searchaliases = 1;
46 char            *helpfile = GO500GW_HELPFILE;
47 char            *filterfile = FILTERFILE;
48 char            *templatefile = TEMPLATEFILE;
49 char            *friendlyfile = FRIENDLYFILE;
50 int             rdncount = GO500GW_RDNCOUNT;
51
52 static set_socket();
53 static RETSIGTYPE wait4child();
54 static do_queries();
55 static do_menu();
56 static do_list();
57 static do_search();
58 static do_read();
59 static do_help();
60 static do_sizelimit();
61 static do_error();
62 extern int strcasecmp();
63
64 char    myhost[MAXHOSTNAMELEN];
65 int     myport = GO500GW_PORT;
66
67 static usage( name )
68 char    *name;
69 {
70         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 );
71         exit( 1 );
72 }
73
74 main (argc, argv)
75 int     argc;
76 char    **argv;
77 {
78         int                     s, ns, rc;
79         int                     port = -1;
80         int                     i, pid;
81         char                    *myname;
82         fd_set                  readfds;
83         struct hostent          *hp;
84         struct sockaddr_in      from;
85         int                     fromlen;
86         RETSIGTYPE                      wait4child();
87         extern char             *optarg;
88         extern char             **Argv;
89         extern int              Argc;
90
91         /* for setproctitle */
92         Argv = argv;
93         Argc = argc;
94
95         while ( (i = getopt( argc, argv, "P:ad:f:h:lp:t:x:Ic:" )) != EOF ) {
96                 switch( i ) {
97                 case 'a':       /* search aliases */
98                         searchaliases = 0;
99                         break;
100
101                 case 'd':       /* debugging level */
102                         debug = atoi( optarg );
103 #ifdef LDAP_DEBUG
104                         ldap_debug = debug;
105 #else
106                         fprintf( stderr, "warning: ldap debugging requires LDAP_DEBUG\n" );
107 #endif
108                         break;
109
110                 case 'f':       /* ldap filter file */
111                         filterfile = strdup( optarg );
112                         break;
113
114                 case 'h':       /* gopher help file */
115                         helpfile = strdup( optarg );
116                         break;
117
118                 case 'l':       /* log to LOG_LOCAL3 */
119                         dosyslog = 1;
120                         break;
121
122                 case 'p':       /* port to listen on */
123                         port = atoi( optarg );
124                         break;
125
126                 case 'P':       /* port to connect to ldap server */
127                         ldapport = atoi( optarg );
128                         break;
129
130                 case 't':       /* ldap template file */
131                         templatefile = strdup( optarg );
132                         break;
133
134                 case 'x':       /* ldap server hostname */
135                         ldaphost = strdup( optarg );
136                         break;
137
138                 case 'I':       /* run from inetd */
139                         inetd = 1;
140                         break;
141
142                 case 'c':       /* count of DN components to show */
143                         rdncount = atoi( optarg );
144                         break;
145
146                 default:
147                         usage( argv[0] );
148                 }
149         }
150
151 #ifdef HAVE_SYSCONF
152         dtblsize = sysconf( _SC_OPEN_MAX );
153 #elif HAVE_GETDTABLESIZE
154         dtblsize = getdtablesize();
155 #else
156         dtblsize = 32;
157 #endif
158
159 #ifdef FD_SETSIZE
160         if ( dtblsize > FD_SETSIZE ) {
161                 dtblsize = FD_SETSIZE;
162         }
163 #endif  /* FD_SETSIZE*/
164
165
166
167 #ifdef GO500GW_HOSTNAME
168         strcpy( myhost, GO500GW_HOSTNAME );
169 #else
170         if ( myhost[0] == '\0' && gethostname( myhost, sizeof(myhost) )
171             == -1 ) {
172                 perror( "gethostname" );
173                 exit( 1 );
174         }
175 #endif
176
177         /* detach if stderr is redirected or no debugging */
178         if ( inetd == 0 )
179                 (void) detach( debug );
180
181         if ( (myname = strrchr( argv[0], '/' )) == NULL )
182                 myname = strdup( argv[0] );
183         else
184                 myname = strdup( myname + 1 );
185
186         if ( dosyslog ) {
187 #ifdef LOG_LOCAL3
188                 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL3 );
189 #else
190                 openlog( myname, OPENLOG_OPTIONS );
191 #endif
192         }
193         if ( dosyslog )
194                 syslog( LOG_INFO, "initializing" );
195
196         /* set up the socket to listen on */
197         if ( inetd == 0 ) {
198                 s = set_socket( port );
199
200                 /* arrange to reap children */
201                 (void) signal( SIGCHLD, (void *) wait4child );
202         }
203
204         if ( inetd ) {
205                 fromlen = sizeof(from);
206                 if ( getpeername( 0, (struct sockaddr *) &from, &fromlen )
207                     == 0 ) {
208                         hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
209                             sizeof(from.sin_addr.s_addr), AF_INET );
210                         Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
211                             (hp == NULL) ? "unknown" : hp->h_name,
212                             inet_ntoa( from.sin_addr ), 0 );
213
214                         if ( dosyslog ) {
215                                 syslog( LOG_INFO, "connection from %s (%s)",
216                                     (hp == NULL) ? "unknown" : hp->h_name,
217                                     inet_ntoa( from.sin_addr ) );
218                         }
219
220                         setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
221                             hp->h_name );
222                 }
223
224                 do_queries( 0 );
225
226                 close( 0 );
227
228                 exit( 0 );
229         }
230
231         for ( ;; ) {
232                 FD_ZERO( &readfds );
233                 FD_SET( s, &readfds );
234
235                 if ( (rc = select( dtblsize, &readfds, 0, 0 ,0 )) == -1 ) {
236                         if ( debug ) perror( "select" );
237                         continue;
238                 } else if ( rc == 0 ) {
239                         continue;
240                 }
241
242                 if ( ! FD_ISSET( s, &readfds ) )
243                         continue;
244
245                 fromlen = sizeof(from);
246                 if ( (ns = accept( s, (struct sockaddr *) &from, &fromlen ))
247                     == -1 ) {
248                         if ( debug ) perror( "accept" );
249                         exit( 1 );
250                 }
251
252                 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
253                     sizeof(from.sin_addr.s_addr), AF_INET );
254
255                 if ( dosyslog ) {
256                         syslog( LOG_INFO, "TCP connection from %s (%s)",
257                             (hp == NULL) ? "unknown" : hp->h_name,
258                             inet_ntoa( from.sin_addr ) );
259                 }
260
261                 switch( pid = fork() ) {
262                 case 0:         /* child */
263                         close( s );
264                         do_queries( ns );
265                         break;
266
267                 case -1:        /* failed */
268                         perror( "fork" );
269                         break;
270
271                 default:        /* parent */
272                         close( ns );
273                         if ( debug )
274                                 fprintf( stderr, "forked child %d\n", pid );
275                         break;
276                 }
277         }
278         /* NOT REACHED */
279 }
280
281 static set_socket( port )
282 int     port;
283 {
284         int                     s, one;
285         struct sockaddr_in      addr;
286
287         if ( port == -1 )
288                 port = GO500GW_PORT;
289         myport = port;
290
291         if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
292                 perror( "socket" );
293                 exit( 1 );
294         }
295
296         /* set option so clients can't keep us from coming back up */
297         one = 1;
298         if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
299             sizeof(one) ) < 0 ) {
300                 perror( "setsockopt" );
301                 exit( 1 );
302         }
303
304         /* bind to a name */
305         addr.sin_family = AF_INET;
306         addr.sin_addr.s_addr = INADDR_ANY;
307         addr.sin_port = htons( port );
308         if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
309                 perror( "bind" );
310                 exit( 1 );
311         }
312
313         /* listen for connections */
314         if ( listen( s, 5 ) == -1 ) {
315                 perror( "listen" );
316                 exit( 1 );
317         }
318
319         if ( debug )
320                 printf( "go500gw listening on port %d\n", port );
321
322         return( s );
323 }
324
325 static RETSIGTYPE
326 wait4child()
327 {
328 #ifndef HAVE_WAITPID
329         WAITSTATUSTYPE     status;
330 #endif
331
332         if ( debug ) printf( "parent: catching child status\n" );
333
334 #ifdef HAVE_WAITPID
335         while (waitpid ((pid_t) -1, NULL, WAIT_FLAGS) > 0)
336                 ;       /* NULL */
337 #else 
338         while (wait3( &status, WAIT_FLAGS, 0 ) > 0 )
339                 ;       /* NULL */
340 #endif
341
342         (void) signal( SIGCHLD, (void *) wait4child );
343 }
344
345 static do_queries( s )
346 int     s;
347 {
348         char            buf[1024], *query;
349         int             len;
350         FILE            *fp;
351         int             rc;
352         struct timeval  timeout;
353         fd_set          readfds;
354         LDAP            *ld;
355
356         if ( (fp = fdopen( s, "a+")) == NULL ) {
357                 perror( "fdopen" );
358                 exit( 1 );
359         }
360
361         timeout.tv_sec = GO500GW_TIMEOUT;
362         timeout.tv_usec = 0;
363         FD_ZERO( &readfds );
364         FD_SET( fileno( fp ), &readfds );
365
366         if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
367                 exit( 1 );
368
369         if ( fgets( buf, sizeof(buf), fp ) == NULL )
370                 exit( 1 );
371
372         len = strlen( buf );
373         if ( debug ) {
374                 fprintf( stderr, "got %d bytes\n", len );
375 #ifdef LDAP_DEBUG
376                 lber_bprint( buf, len );
377 #endif
378         }
379
380         /* strip of \r \n */
381         if ( buf[len - 1] == '\n' )
382                 buf[len - 1] = '\0';
383         len--;
384         if ( buf[len - 1] == '\r' )
385                 buf[len - 1] = '\0';
386         len--;
387
388         query = buf;
389
390         /* strip off leading white space */
391         while ( isspace( *query )) {
392                 ++query;
393                 --len;
394         }
395
396         rewind(fp);
397
398         if ( *query == 'H' || *query == 'L' || *query == 'E' ) {
399                 switch ( *query++ ) {
400                 case 'H':       /* help file */
401                         do_help( fp );
402                         break;
403
404                 case 'L':       /* size limit explanation */
405                         do_sizelimit( fp, *query );
406                         break;
407
408                 case 'E':       /* error explanation */
409                         do_error( fp, query );
410                         break;
411                 }
412
413                 fprintf( fp, ".\r\n" );
414                 rewind(fp);
415
416                 exit( 0 );
417                 /* NOT REACHED */
418         }
419
420         if ( (ld = ldap_open( ldaphost, ldapport )) == NULL ) {
421                 if ( debug ) perror( "ldap_open" );
422                 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
423                     LDAP_SERVER_DOWN, myhost, myport );
424                 fprintf( fp, ".\r\n" );
425                 rewind(fp);
426                 exit( 1 );
427         }
428
429         ld->ld_deref = LDAP_DEREF_ALWAYS;
430         if ( !searchaliases )
431                 ld->ld_deref = LDAP_DEREF_FINDING;
432
433         if ( (rc = ldap_simple_bind_s( ld, GO500GW_BINDDN, NULL ))
434             != LDAP_SUCCESS ) {
435                 if ( debug ) ldap_perror( ld, "ldap_simple_bind_s" );
436                 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
437                     rc, myhost, myport );
438                 fprintf( fp, ".\r\n" );
439                 rewind(fp);
440                 exit( 1 );
441         }
442
443         switch ( *query++ ) {
444         case 'R':       /* read an entry */
445                 do_read( ld, fp, query );
446                 break;
447
448         case 'S':       /* search */
449                 do_search( ld, fp, query, 1 );
450                 break;
451
452         case 'M':       /* X.500 menu */
453                 do_menu( ld, fp, query );
454                 break;
455
456         default:
457                 do_menu( ld, fp, "" );
458                 break;
459         }
460
461         fprintf( fp, ".\r\n" );
462         rewind(fp);
463
464         exit( 0 );
465         /* NOT REACHED */
466 }
467
468 static char *pick_oc( oclist )
469 char    **oclist;
470 {
471         int     i;
472
473         if ( oclist == NULL )
474                 return( "unknown" );
475
476         for ( i = 0; oclist[i] != NULL; i++ ) {
477                 if ( strcasecmp( oclist[i], "top" ) != 0 &&
478                     strcasecmp( oclist[i], "quipuObject" ) != 0 &&
479                     strcasecmp( oclist[i], "quipuNonLeafObject" ) != 0 )
480                         return( oclist[i] );
481         }
482
483         return( "unknown" );
484 }
485
486 static isnonleaf( ld, oclist, dn )
487 LDAP    *ld;
488 char    **oclist;
489 char    *dn;
490 {
491         int     i, quipuobject = 0;
492
493         if ( oclist == NULL )
494                 return( 0 );
495
496         for ( i = 0; oclist[i] != NULL; i++ ) {
497                 if ( strcasecmp( oclist[i], "quipuObject" ) == 0 )
498                         quipuobject = 1;
499                 if ( strcasecmp( oclist[i], "quipuNonLeafObject" ) == 0 ||
500                     strcasecmp( oclist[i], "externalNonLeafObject" ) == 0 )
501                         return( 1 );
502         }
503
504         /*
505          * not a quipu thang - no easy way to tell leaves from nonleaves
506          * except by trying to search or list.  ldap only lets us search.
507          */
508
509         /* expensive - just guess for now */
510         return( quipuobject ? 0 : 1 );
511
512 #ifdef notdef
513         if ( !quipuobject ) {
514                 int             rc, numentries;
515                 struct timeval  timeout;
516                 LDAPMessage     *res = NULL;
517                 static char     *attrs[] = { "objectClass", 0 };
518
519                 timeout.tv_sec = GO500GW_TIMEOUT;
520                 timeout.tv_usec = 0;
521                 ld->ld_sizelimit = 1;
522                 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
523                     "(objectClass=*)", attrs, 0, &timeout, &res ))
524                     == LDAP_SUCCESS || rc == LDAP_SIZELIMIT_EXCEEDED ) {
525                         ld->ld_sizelimit = LDAP_NO_LIMIT;
526
527                         numentries = ldap_count_entries( ld, res );
528                         if ( res != NULL )
529                                 ldap_msgfree( res );
530                         return( numentries == 1 ? 1 : 0 );
531                 }
532         }
533
534         return( 0 );
535 #endif
536 }
537
538 static do_menu( ld, fp, dn )
539 LDAP    *ld;
540 FILE    *fp;
541 char    *dn;
542 {
543         char            **s;
544         char            *rdn = NULL;
545         FriendlyMap     *fm = NULL;
546
547         if ( strcmp( dn, "" ) != 0 ) {
548                 s = ldap_explode_dn( dn, 1 );
549
550                 if ( s[1] == NULL )
551                         rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
552                 else
553                         rdn = s[0];
554                 fprintf( fp, "0Read %s entry\tR%s\t%s\t%d\r\n", rdn ? rdn: s[0],
555                     dn, myhost, myport );
556
557                 ldap_value_free( s );
558         } else {
559                 fprintf( fp, "0About the Gopher to X.500 Gateway\tH\t%s\t%d\r\n",
560                     myhost, myport );
561         }
562
563         fprintf( fp, "7Search %s\tS%s\t%s\t%d\r\n", rdn ? rdn : "root", dn,
564             myhost, myport );
565
566         do_list( ld, fp, dn );
567
568         ldap_free_friendlymap( &fm );
569 }
570
571 static do_list( ld, fp, dn )
572 LDAP    *ld;
573 FILE    *fp;
574 char    *dn;
575 {
576         int             rc;
577         LDAPMessage     *e, *res;
578         struct timeval  timeout;
579         FriendlyMap     *fm = NULL;
580         static char     *attrs[] = { "objectClass", 0 };
581
582         timeout.tv_sec = GO500GW_TIMEOUT;
583         timeout.tv_usec = 0;
584         ld->ld_deref = LDAP_DEREF_FINDING;
585         if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
586             "(!(objectClass=dSA))", attrs, 0, &timeout, &res )) != LDAP_SUCCESS
587             && rc != LDAP_SIZELIMIT_EXCEEDED ) {
588                 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
589                     rc, myhost, myport );
590                 return;
591         }
592         ld->ld_deref = LDAP_DEREF_ALWAYS;
593
594         if ( ldap_count_entries( ld, res ) < 1 ) {
595                 return;
596         }
597
598 #ifdef GO500GW_SORT_ATTR
599         ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
600 #endif
601
602         fm = NULL;
603         for ( e = ldap_first_entry( ld, res ); e != NULL;
604             e = ldap_next_entry( ld, e ) ) {
605                 char    **s, **oc;
606                 char    *rdn, *doc;
607
608                 dn = ldap_get_dn( ld, e );
609                 s = ldap_explode_dn( dn, 1 );
610                 oc = ldap_get_values( ld, e, "objectClass" );
611
612                 doc = pick_oc( oc );
613                 if ( strcasecmp( doc, "country" ) == 0 ) {
614                         rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
615                 } else {
616                         rdn = s[0];
617                 }
618                 if ( rdn == NULL ) {
619                         rdn = s[0];
620                 }
621
622                 if ( strncasecmp( rdn, "{ASN}", 5 ) != 0 ) {
623                         if ( isnonleaf( ld, oc, dn ) ) {
624                                 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", rdn,
625                                     doc, dn, myhost, myport );
626                         } else {
627                                 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", rdn,
628                                     doc, dn, myhost, myport );
629                         }
630                 }
631
632                 free( dn );
633                 ldap_value_free( s );
634                 ldap_value_free( oc );
635         }
636         ldap_free_friendlymap( &fm );
637
638         if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
639                 fprintf( fp, "0A size limit was exceeded (explanation)\tLL\t%s\t%d\r\n",
640                     myhost, myport );
641         }
642 }
643
644 static isoc( ocl, oc )
645 char    **ocl;
646 char    *oc;
647 {
648         int     i;
649
650         for ( i = 0; ocl[i] != NULL; i++ ) {
651                 if ( strcasecmp( ocl[i], oc ) == 0 )
652                         return( 1 );
653         }
654
655         return( 0 );
656 }
657
658 static int make_scope( ld, dn )
659 LDAP    *ld;
660 char    *dn;
661 {
662         int             scope;
663         char            **oc;
664         LDAPMessage     *res;
665         struct timeval  timeout;
666         static char     *attrs[] = { "objectClass", 0 };
667
668         if ( strcmp( dn, "" ) == 0 )
669                 return( LDAP_SCOPE_ONELEVEL );
670
671         timeout.tv_sec = GO500GW_TIMEOUT;
672         timeout.tv_usec = 0;
673         if ( ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
674             attrs, 0, &timeout, &res ) != LDAP_SUCCESS ) {
675                 return( -1 );
676         }
677
678         oc = ldap_get_values( ld, ldap_first_entry( ld, res ), "objectClass" );
679
680         if ( isoc( oc, "organization" ) || isoc( oc, "organizationalUnit" ) )
681                 scope = LDAP_SCOPE_SUBTREE;
682         else
683                 scope = LDAP_SCOPE_ONELEVEL;
684
685         ldap_value_free( oc );
686         ldap_msgfree( res );
687
688         return( scope );
689 }
690
691 static do_search( ld, fp, query )
692 LDAP    *ld;
693 FILE    *fp;
694 char    *query;
695 {
696         int             scope;
697         char            *base, *filter;
698         char            *filtertype;
699         int             count, rc;
700         struct timeval  timeout;
701         LDAPFiltInfo    *fi;
702         LDAPMessage     *e, *res;
703         LDAPFiltDesc    *filtd;
704         static char     *attrs[] = { "objectClass", 0 };
705
706         if ( (filter = strchr( query, '\t' )) == NULL ) {
707                 fprintf( fp, "3Missing filter!\r\n" );
708                 exit( 1 );
709         }
710         *filter++ = '\0';
711         base = query;
712
713 #ifdef GO500GW_UFN
714         if ( strchr( filter, ',' ) != NULL ) {
715                 ldap_ufn_setprefix( ld, base );
716                 timeout.tv_sec = GO500GW_TIMEOUT;
717                 timeout.tv_usec = 0;
718                 ldap_ufn_timeout( (void *) &timeout );
719                 ld->ld_deref = LDAP_DEREF_FINDING;
720
721                 if ( (rc = ldap_ufn_search_s( ld, filter, attrs, 0, &res ))
722                     != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
723                         fprintf(fp,
724                             "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
725                             rc, myhost, myport );
726                         return;
727                 }
728
729                 count = ldap_count_entries( ld, res );
730         } else {
731 #endif
732                 if ( (scope = make_scope( ld, base )) == -1 ) {
733                         fprintf( fp, "3Bad scope\r\n" );
734                         exit( 1 );
735                 }
736
737                 filtertype = (scope == LDAP_SCOPE_ONELEVEL ?
738                     "go500gw onelevel" : "go500gw subtree");
739                 ld->ld_deref = (scope == LDAP_SCOPE_ONELEVEL ?
740                     LDAP_DEREF_FINDING : LDAP_DEREF_ALWAYS);
741                 timeout.tv_sec = GO500GW_TIMEOUT;
742                 timeout.tv_usec = 0;
743
744                 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
745                         fprintf( stderr, "Cannot open filter file (%s)\n",
746                             filterfile );
747                         exit( 1 );
748                 }
749
750                 count = 0;
751                 res = NULL;
752                 for ( fi = ldap_getfirstfilter( filtd, filtertype, filter );
753                     fi != NULL; fi = ldap_getnextfilter( filtd ) )
754                 {
755                         if ( (rc = ldap_search_st( ld, base, scope,
756                             fi->lfi_filter, attrs, 0, &timeout, &res ))
757                             != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
758                                 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
759                                     rc, myhost, myport );
760                                 return( 1 );
761                         }
762                         if ( (count = ldap_count_entries( ld, res )) != 0 )
763                                 break;
764                 }
765                 ld->ld_deref = LDAP_DEREF_ALWAYS;
766                 ldap_getfilter_free( filtd );
767 #ifdef GO500GW_UFN
768         }
769 #endif
770
771         if ( count == 0 ) {
772                 return( 0 );
773         }
774
775         if ( count == 1 ) {
776                 char    *dn, **s, **oc;
777                 int     rc;
778
779                 e = ldap_first_entry( ld, res );
780                 oc = ldap_get_values( ld, e, "objectClass" );
781                 if ( isnonleaf( ld, oc, dn ) ) {
782                         dn = ldap_get_dn( ld, e );
783
784                         rc = do_menu( ld, fp, dn );
785
786                         free( dn );
787                         return( rc );
788                 }
789
790                 ldap_value_free( oc );
791         }
792
793 #ifdef GO500GW_SORT_ATTR
794         ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
795 #endif
796
797         for ( e = ldap_first_entry( ld, res ); e != NULL;
798             e = ldap_next_entry( ld, e ) ) {
799                 char    **s, **oc;
800                 char    *dn;
801
802                 dn = ldap_get_dn( ld, e );
803                 s = ldap_explode_dn( dn, 1 );
804                 oc = ldap_get_values( ld, e, "objectClass" );
805
806                 if ( isnonleaf( ld, oc, dn ) )
807                         fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", s[0],
808                             pick_oc( oc ), dn, myhost, myport );
809                 else
810                         fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", s[0],
811                             pick_oc( oc ), dn, myhost, myport );
812
813                 free( dn );
814                 ldap_value_free( s );
815                 ldap_value_free( oc );
816         }
817
818         if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
819                 fprintf( fp, "0A size limit was exceeded (explanation)\tLS\t%s\t%d\r\n",
820                     myhost, myport );
821         }
822 }
823
824
825 static int
826 entry2textwrite( void *fp, char *buf, int len )
827 {
828         return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
829 }
830
831 static do_read( ld, fp, dn )
832 LDAP    *ld;
833 FILE    *fp;
834 char    *dn;
835 {
836         static struct ldap_disptmpl *tmpllist;
837
838         ldap_init_templates( templatefile, &tmpllist );
839
840         if ( ldap_entry2text_search( ld, dn, NULL, NULL, tmpllist, NULL, NULL,
841             entry2textwrite,(void *) fp, "\r\n", rdncount, 0 )
842             != LDAP_SUCCESS ) {
843                 fprintf(fp,
844                     "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
845                     ld->ld_errno, myhost, myport );
846         }
847
848         if ( tmpllist != NULL ) {
849                 ldap_free_templates( tmpllist );
850         }
851 }
852
853 static do_help( op )
854 FILE    *op;
855 {
856         FILE    *fp;
857         char    line[BUFSIZ];
858
859         if ( (fp = fopen( helpfile, "r" )) == NULL ) {
860                 fprintf( op, "Cannot access helpfile (%s)\r\n", helpfile );
861                 return;
862         }
863
864         while ( fgets( line, sizeof(line), fp ) != NULL ) {
865                 line[ strlen( line ) - 1 ] = '\0';
866
867                 fprintf( op, "%s\r\n", line );
868         }
869
870         fclose( fp );
871 }
872
873 static do_sizelimit( fp, type )
874 FILE    *fp;
875 char    type;
876 {
877         if ( type == 'S' ) {
878                 fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
879                 fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
880                 fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
881                 fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
882         } else {
883                 fprintf( fp, "Not all entries could be returned because a size limit was exceeded.\r\n" );
884                 fprintf( fp, "There is no way to defeat this feature, but if you know who you are\r\n" );
885                 fprintf( fp, "looking for, try choosing the \"Search\" option listed above and\r\n" );
886                 fprintf( fp, "specifying the name of the person you want.\r\n" );
887         }
888         fprintf( fp, ".\r\n" );
889 }
890
891 static do_error( fp, s )
892 FILE    *fp;
893 char    *s;
894 {
895         int     code;
896
897         code = atoi( s );
898
899         fprintf( fp, "An error occurred searching X.500.  The error code was %d\r\n", code );
900         fprintf( fp, "The corresponding error is: %s\r\n", ldap_err2string( code ) );
901         fprintf( fp, "No additional information is available\r\n" );
902         fprintf( fp, ".\r\n" );
903 }