2 * Copyright (c) 1990 Regents of the University of Michigan.
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.
19 #include <ac/signal.h>
20 #include <ac/socket.h>
21 #include <ac/string.h>
22 #include <ac/syslog.h>
24 #include <ac/unistd.h>
27 #include <ac/setproctitle.h>
29 #ifdef HAVE_SYS_PARAM_H
30 #include <sys/param.h>
33 #ifdef HAVE_SYS_RESOURCE_H
34 #include <sys/resource.h>
41 #define ldap_debug debug
48 #include "ldapconfig.h"
55 char *ldaphost = NULL;
57 int searchaliases = 1;
58 char *helpfile = GO500GW_HELPFILE;
59 char *filterfile = FILTERFILE;
60 char *templatefile = TEMPLATEFILE;
61 char *friendlyfile = FRIENDLYFILE;
62 int rdncount = GO500GW_RDNCOUNT;
64 static void usage ( char *name );
65 static int set_socket (int port);
66 static RETSIGTYPE wait4child(int sig);
67 static void do_queries (int s);
68 static char *pick_oc ( char **oclist );
69 static int isnonleaf ( LDAP *ld, char **oclist, char *dn );
70 static void do_menu (LDAP *ld, FILE *fp, char *dn);
71 static void do_list (LDAP *ld, FILE *fp, char *dn);
72 static int isoc ( char **ocl, char *oc );
73 static int make_scope ( LDAP *ld, char *dn );
74 static void do_search (LDAP *ld, FILE *fp, char *query);
75 static int entry2textwrite( void *fp, char *buf, int len );
76 static void do_read (LDAP *ld, FILE *fp, char *dn);
77 static void do_help (FILE *op);
78 static void do_sizelimit(FILE *fp, char type);
79 static void do_error (FILE *fp, char *s);
81 char myhost[MAXHOSTNAMELEN];
82 int myport = GO500GW_PORT;
87 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 );
92 main (int argc, char **argv )
100 struct sockaddr_in from;
103 #if defined( LDAP_PROCTITLE ) && !defined( HAVE_SETPROCTITLE )
104 /* for setproctitle */
109 while ( (i = getopt( argc, argv, "P:ad:f:h:lp:t:x:Ic:" )) != EOF ) {
111 case 'a': /* search aliases */
115 case 'd': /* debugging level */
116 debug |= atoi( optarg );
119 case 'f': /* ldap filter file */
120 filterfile = strdup( optarg );
123 case 'h': /* gopher help file */
124 helpfile = strdup( optarg );
127 case 'l': /* log to LOG_LOCAL3 */
131 case 'p': /* port to listen on */
132 port = atoi( optarg );
135 case 'P': /* port to connect to ldap server */
136 ldapport = atoi( optarg );
139 case 't': /* ldap template file */
140 templatefile = strdup( optarg );
143 case 'x': /* ldap server hostname */
144 ldaphost = strdup( optarg );
147 case 'I': /* run from inetd */
151 case 'c': /* count of DN components to show */
152 rdncount = atoi( optarg );
161 dtblsize = sysconf( _SC_OPEN_MAX );
162 #elif HAVE_GETDTABLESIZE
163 dtblsize = getdtablesize();
165 dtblsize = FD_SETSIZE;
169 if ( dtblsize > FD_SETSIZE ) {
170 dtblsize = FD_SETSIZE;
172 #endif /* FD_SETSIZE*/
176 #ifdef GO500GW_HOSTNAME
177 strcpy( myhost, GO500GW_HOSTNAME );
179 if ( myhost[0] == '\0' && gethostname( myhost, sizeof(myhost) )
181 perror( "gethostname" );
186 /* detach if stderr is redirected or no debugging */
188 lutil_detach( debug && !isatty( 1 ), 1 );
190 if ( (myname = strrchr( argv[0], '/' )) == NULL )
191 myname = strdup( argv[0] );
193 myname = strdup( myname + 1 );
196 lber_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
197 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
201 (void) SIGNAL( SIGPIPE, SIG_IGN );
206 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL3 );
208 openlog( myname, OPENLOG_OPTIONS );
212 syslog( LOG_INFO, "initializing" );
214 /* set up the socket to listen on */
216 s = set_socket( port );
218 /* arrange to reap children */
219 (void) SIGNAL( SIGCHLD, wait4child );
223 fromlen = sizeof(from);
224 if ( getpeername( 0, (struct sockaddr *) &from, &fromlen )
226 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
227 sizeof(from.sin_addr.s_addr), AF_INET );
228 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
229 (hp == NULL) ? "unknown" : hp->h_name,
230 inet_ntoa( from.sin_addr ), 0 );
233 syslog( LOG_INFO, "connection from %s (%s)",
234 (hp == NULL) ? "unknown" : hp->h_name,
235 inet_ntoa( from.sin_addr ) );
238 #ifdef LDAP_PROCTITLE
239 setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
253 FD_SET( s, &readfds );
255 if ( (rc = select( dtblsize, &readfds, 0, 0 ,0 )) == -1 ) {
256 if ( debug ) perror( "select" );
258 } else if ( rc == 0 ) {
262 if ( ! FD_ISSET( s, &readfds ) )
265 fromlen = sizeof(from);
266 if ( (ns = accept( s, (struct sockaddr *) &from, &fromlen ))
268 if ( debug ) perror( "accept" );
272 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
273 sizeof(from.sin_addr.s_addr), AF_INET );
276 syslog( LOG_INFO, "TCP connection from %s (%s)",
277 (hp == NULL) ? "unknown" : hp->h_name,
278 inet_ntoa( from.sin_addr ) );
281 switch( pid = fork() ) {
287 case -1: /* failed */
291 default: /* parent */
294 fprintf( stderr, "forked child %d\n", pid );
302 set_socket( int port )
305 struct sockaddr_in addr;
311 if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
316 /* set option so clients can't keep us from coming back up */
318 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
319 sizeof(one) ) < 0 ) {
320 perror( "setsockopt" );
325 addr.sin_family = AF_INET;
326 addr.sin_addr.s_addr = INADDR_ANY;
327 addr.sin_port = htons( port );
328 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
333 /* listen for connections */
334 if ( listen( s, 5 ) == -1 ) {
340 printf( "go500gw listening on port %d\n", port );
346 wait4child( int sig )
349 WAITSTATUSTYPE status;
352 if ( debug ) printf( "parent: catching child status\n" );
355 while (waitpid ((pid_t) -1, (int *) NULL, WAIT_FLAGS) > 0)
358 while (wait4( (pid_t) -1, &status, WAIT_FLAGS, 0 ) > 0 )
362 (void) SIGNAL( SIGCHLD, wait4child );
368 char buf[1024], *query;
373 struct timeval timeout;
377 if ( (fp = fdopen( s, "a+")) == NULL ) {
382 timeout.tv_sec = GO500GW_TIMEOUT;
385 FD_SET( fileno( fp ), &readfds );
387 if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
390 if ( fgets( buf, sizeof(buf), fp ) == NULL )
395 fprintf( stderr, "got %d bytes\n", len );
397 lber_bprint( buf, len );
402 if ( buf[len - 1] == '\n' )
405 if ( buf[len - 1] == '\r' )
411 /* strip off leading white space */
412 while ( isspace( *query )) {
419 if ( *query == 'H' || *query == 'L' || *query == 'E' ) {
420 switch ( *query++ ) {
421 case 'H': /* help file */
425 case 'L': /* size limit explanation */
426 do_sizelimit( fp, *query );
429 case 'E': /* error explanation */
430 do_error( fp, query );
434 fprintf( fp, ".\r\n" );
441 if ( (ld = ldap_open( ldaphost, ldapport )) == NULL ) {
442 if ( debug ) perror( "ldap_open" );
443 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
444 LDAP_SERVER_DOWN, myhost, myport );
445 fprintf( fp, ".\r\n" );
450 deref = LDAP_DEREF_ALWAYS;
451 if ( !searchaliases )
452 deref = LDAP_DEREF_FINDING;
454 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
456 if ( (rc = ldap_simple_bind_s( ld, NULL, NULL ))
458 if ( debug ) ldap_perror( ld, "ldap_simple_bind_s" );
459 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
460 rc, myhost, myport );
461 fprintf( fp, ".\r\n" );
466 switch ( *query++ ) {
467 case 'R': /* read an entry */
468 do_read( ld, fp, query );
471 case 'S': /* search */
472 do_search( ld, fp, query );
475 case 'M': /* X.500 menu */
476 do_menu( ld, fp, query );
480 do_menu( ld, fp, "" );
484 fprintf( fp, ".\r\n" );
492 pick_oc( char **oclist )
496 if ( oclist == NULL )
499 for ( i = 0; oclist[i] != NULL; i++ ) {
500 if ( strcasecmp( oclist[i], "top" ) != 0 &&
501 strcasecmp( oclist[i], "quipuObject" ) != 0 &&
502 strcasecmp( oclist[i], "quipuNonLeafObject" ) != 0 )
510 isnonleaf( LDAP *ld, char **oclist, char *dn )
512 int i, quipuobject = 0;
514 if ( oclist == NULL )
517 for ( i = 0; oclist[i] != NULL; i++ ) {
518 if ( strcasecmp( oclist[i], "quipuObject" ) == 0 )
520 if ( strcasecmp( oclist[i], "quipuNonLeafObject" ) == 0 ||
521 strcasecmp( oclist[i], "externalNonLeafObject" ) == 0 )
526 * not a quipu thang - no easy way to tell leaves from nonleaves
527 * except by trying to search or list. ldap only lets us search.
530 /* expensive - just guess for now */
531 return( quipuobject ? 0 : 1 );
534 if ( !quipuobject ) {
536 struct timeval timeout;
537 LDAPMessage *res = NULL;
538 static char *attrs[] = { "objectClass", 0 };
541 timeout.tv_sec = GO500GW_TIMEOUT;
543 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
544 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
545 "(objectClass=*)", attrs, 0, &timeout, &res ))
546 == LDAP_SUCCESS || rc == LDAP_SIZELIMIT_EXCEEDED ) {
547 sizelimit = LDAP_NO_LIMIT;
548 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
550 numentries = ldap_count_entries( ld, res );
553 return( numentries == 1 ? 1 : 0 );
562 do_menu( LDAP *ld, FILE *fp, char *dn )
566 LDAPFriendlyMap *fm = NULL;
568 if ( strcmp( dn, "" ) != 0 ) {
569 s = ldap_explode_dn( dn, 1 );
572 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
575 fprintf( fp, "0Read %s entry\tR%s\t%s\t%d\r\n", rdn ? rdn: s[0],
576 dn, myhost, myport );
578 ldap_value_free( s );
580 fprintf( fp, "0About the Gopher to X.500 Gateway\tH\t%s\t%d\r\n",
584 fprintf( fp, "7Search %s\tS%s\t%s\t%d\r\n", rdn ? rdn : "root", dn,
587 do_list( ld, fp, dn );
589 ldap_free_friendlymap( &fm );
593 do_list( LDAP *ld, FILE *fp, char *dn )
596 LDAPMessage *e, *res;
597 struct timeval timeout;
598 LDAPFriendlyMap *fm = NULL;
599 static char *attrs[] = { "objectClass", 0 };
600 int deref = LDAP_DEREF_FINDING;
602 timeout.tv_sec = GO500GW_TIMEOUT;
605 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
607 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
608 "(!(objectClass=dSA))", attrs, 0, &timeout, &res )) != LDAP_SUCCESS
609 && rc != LDAP_SIZELIMIT_EXCEEDED ) {
610 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
611 rc, myhost, myport );
615 deref = LDAP_DEREF_ALWAYS;
616 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
618 if ( ldap_count_entries( ld, res ) < 1 ) {
622 #ifdef GO500GW_SORT_ATTR
623 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
627 for ( e = ldap_first_entry( ld, res ); e != NULL;
628 e = ldap_next_entry( ld, e ) ) {
632 dn = ldap_get_dn( ld, e );
633 s = ldap_explode_dn( dn, 1 );
634 oc = ldap_get_values( ld, e, "objectClass" );
637 if ( strcasecmp( doc, "country" ) == 0 ) {
638 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
646 if ( strncasecmp( rdn, "{ASN}", 5 ) != 0 ) {
647 if ( isnonleaf( ld, oc, dn ) ) {
648 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", rdn,
649 doc, dn, myhost, myport );
651 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", rdn,
652 doc, dn, myhost, myport );
657 ldap_value_free( s );
658 ldap_value_free( oc );
660 ldap_free_friendlymap( &fm );
662 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
663 fprintf( fp, "0A size limit was exceeded (explanation)\tLL\t%s\t%d\r\n",
669 isoc( char **ocl, char *oc )
673 for ( i = 0; ocl[i] != NULL; i++ ) {
674 if ( strcasecmp( ocl[i], oc ) == 0 )
682 make_scope( LDAP *ld, char *dn )
687 struct timeval timeout;
688 static char *attrs[] = { "objectClass", 0 };
690 if ( strcmp( dn, "" ) == 0 )
691 return( LDAP_SCOPE_ONELEVEL );
693 timeout.tv_sec = GO500GW_TIMEOUT;
695 if ( ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
696 attrs, 0, &timeout, &res ) != LDAP_SUCCESS ) {
700 oc = ldap_get_values( ld, ldap_first_entry( ld, res ), "objectClass" );
702 if ( isoc( oc, "organization" ) || isoc( oc, "organizationalUnit" ) )
703 scope = LDAP_SCOPE_SUBTREE;
705 scope = LDAP_SCOPE_ONELEVEL;
707 ldap_value_free( oc );
714 do_search( LDAP *ld, FILE *fp, char *query )
721 struct timeval timeout;
723 LDAPMessage *e, *res;
725 static char *attrs[] = { "objectClass", 0 };
727 if ( (filter = strchr( query, '\t' )) == NULL ) {
728 fprintf( fp, "3Missing filter!\r\n" );
735 if ( strchr( filter, ',' ) != NULL ) {
736 ldap_ufn_setprefix( ld, base );
737 timeout.tv_sec = GO500GW_TIMEOUT;
739 ldap_ufn_timeout( (void *) &timeout );
741 deref = LDAP_DEREF_FINDING;
742 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
744 if ( (rc = ldap_ufn_search_s( ld, filter, attrs, 0, &res ))
745 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
747 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
748 rc, myhost, myport );
752 count = ldap_count_entries( ld, res );
755 if ( (scope = make_scope( ld, base )) == -1 ) {
756 fprintf( fp, "3Bad scope\r\n" );
760 filtertype = (scope == LDAP_SCOPE_ONELEVEL ?
761 "go500gw onelevel" : "go500gw subtree");
762 deref = (scope == LDAP_SCOPE_ONELEVEL ?
763 LDAP_DEREF_FINDING : LDAP_DEREF_ALWAYS);
764 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
765 timeout.tv_sec = GO500GW_TIMEOUT;
768 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
769 fprintf( stderr, "Cannot open filter file (%s)\n",
776 for ( fi = ldap_getfirstfilter( filtd, filtertype, filter );
777 fi != NULL; fi = ldap_getnextfilter( filtd ) )
779 if ( (rc = ldap_search_st( ld, base, scope,
780 fi->lfi_filter, attrs, 0, &timeout, &res ))
781 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
782 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
783 rc, myhost, myport );
786 if ( (count = ldap_count_entries( ld, res )) != 0 )
789 deref = LDAP_DEREF_ALWAYS;
790 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
791 ldap_getfilter_free( filtd );
804 e = ldap_first_entry( ld, res );
805 oc = ldap_get_values( ld, e, "objectClass" );
806 dn = ldap_get_dn( ld, e );
808 if ( isnonleaf( ld, oc, dn ) ) {
809 do_menu( ld, fp, dn );
816 ldap_value_free( oc );
819 #ifdef GO500GW_SORT_ATTR
820 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
823 for ( e = ldap_first_entry( ld, res ); e != NULL;
824 e = ldap_next_entry( ld, e ) ) {
828 dn = ldap_get_dn( ld, e );
829 s = ldap_explode_dn( dn, 1 );
830 oc = ldap_get_values( ld, e, "objectClass" );
832 if ( isnonleaf( ld, oc, dn ) )
833 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", s[0],
834 pick_oc( oc ), dn, myhost, myport );
836 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", s[0],
837 pick_oc( oc ), dn, myhost, myport );
840 ldap_value_free( s );
841 ldap_value_free( oc );
844 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
845 fprintf( fp, "0A size limit was exceeded (explanation)\tLS\t%s\t%d\r\n",
852 entry2textwrite( void *fp, char *buf, int len )
854 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
858 do_read( LDAP *ld, FILE *fp, char *dn )
860 static struct ldap_disptmpl *tmpllist;
862 ldap_init_templates( templatefile, &tmpllist );
864 if ( ldap_entry2text_search( ld, dn, NULL, NULL, tmpllist, NULL, NULL,
865 entry2textwrite,(void *) fp, "\r\n", rdncount, 0 )
868 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
871 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
872 ld_errno, myhost, myport );
875 if ( tmpllist != NULL ) {
876 ldap_free_templates( tmpllist );
886 if ( (fp = fopen( helpfile, "r" )) == NULL ) {
887 fprintf( op, "Cannot access helpfile (%s)\r\n", helpfile );
891 while ( fgets( line, sizeof(line), fp ) != NULL ) {
892 line[ strlen( line ) - 1 ] = '\0';
894 fprintf( op, "%s\r\n", line );
901 do_sizelimit( FILE *fp, char type )
904 fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
905 fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
906 fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
907 fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
909 fprintf( fp, "Not all entries could be returned because a size limit was exceeded.\r\n" );
910 fprintf( fp, "There is no way to defeat this feature, but if you know who you are\r\n" );
911 fprintf( fp, "looking for, try choosing the \"Search\" option listed above and\r\n" );
912 fprintf( fp, "specifying the name of the person you want.\r\n" );
914 fprintf( fp, ".\r\n" );
918 do_error( FILE *fp, char *s )
924 fprintf( fp, "An error occurred searching X.500. The error code was %d\r\n", code );
925 fprintf( fp, "The corresponding error is: %s\r\n", ldap_err2string( code ) );
926 fprintf( fp, "No additional information is available\r\n" );
927 fprintf( fp, ".\r\n" );