3 * Copyright (c) 1990 Regents of the University of Michigan.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that this notice is preserved and that due credit is given
8 * to the University of Michigan at Ann Arbor. The name of the University
9 * may not be used to endorse or promote products derived from this
10 * software without specific prior written permission. This software
11 * is provided ``as is'' without express or implied warranty.
18 #include <ac/stdlib.h>
21 #include <ac/signal.h>
22 #include <ac/socket.h>
23 #include <ac/string.h>
24 #include <ac/syslog.h>
26 #include <ac/unistd.h>
30 #include <ac/setproctitle.h>
32 #ifdef HAVE_SYS_RESOURCE_H
33 #include <sys/resource.h>
40 #define ldap_debug debug
46 #include "ldap_defaults.h"
50 int ldap_syslog_level;
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 ) LDAP_GCCATTR((noreturn));
65 static int set_socket (int port);
66 static RETSIGTYPE wait4child(int sig);
67 static void do_queries (int s) LDAP_GCCATTR((noreturn));
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, ber_len_t 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" );
182 exit( EXIT_FAILURE );
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 ber_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),
227 sizeof(from.sin_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 ) :
248 exit( EXIT_SUCCESS );
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" );
269 exit( EXIT_FAILURE );
272 hp = gethostbyaddr( (char *) &(from.sin_addr),
273 sizeof(from.sin_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 ) {
313 exit( EXIT_FAILURE );
317 /* set option so clients can't keep us from coming back up */
319 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
320 sizeof(one) ) < 0 ) {
321 perror( "setsockopt" );
322 exit( EXIT_FAILURE );
326 /* enable keep alives */
328 if ( setsockopt( s, SOL_SOCKET, SO_KEEPALIVE, (char *) &one,
329 sizeof(one) ) < 0 ) {
330 perror( "setsockopt" );
331 exit( EXIT_FAILURE );
336 addr.sin_family = AF_INET;
337 addr.sin_addr.s_addr = htonl(INADDR_ANY);
338 addr.sin_port = htons( port );
339 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
341 exit( EXIT_FAILURE );
344 /* listen for connections */
345 if ( listen( s, 5 ) == -1 ) {
347 exit( EXIT_FAILURE );
351 printf( "go500gw listening on port %d\n", port );
357 wait4child( int sig )
360 WAITSTATUSTYPE status;
363 if ( debug ) printf( "parent: catching child status\n" );
366 while (waitpid ((pid_t) -1, (int *) NULL, WAIT_FLAGS) > 0)
369 while (wait4( (pid_t) -1, &status, WAIT_FLAGS, 0 ) > 0 )
373 (void) SIGNAL( SIGCHLD, wait4child );
379 char buf[1024], *query;
384 struct timeval timeout;
388 if ( (fp = fdopen( s, "a+")) == NULL ) {
390 exit( EXIT_FAILURE );
393 timeout.tv_sec = GO500GW_TIMEOUT;
396 FD_SET( fileno( fp ), &readfds );
398 if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
399 exit( EXIT_FAILURE );
401 if ( fgets( buf, sizeof(buf), fp ) == NULL )
402 exit( EXIT_FAILURE );
406 fprintf( stderr, "got %d bytes\n", len );
408 ber_bprint( buf, len );
413 if ( buf[len - 1] == '\n' )
416 if ( buf[len - 1] == '\r' )
422 /* strip off leading white space */
423 while ( isspace( (unsigned char) *query )) {
430 if ( *query == 'H' || *query == 'L' || *query == 'E' ) {
431 switch ( *query++ ) {
432 case 'H': /* help file */
436 case 'L': /* size limit explanation */
437 do_sizelimit( fp, *query );
440 case 'E': /* error explanation */
441 do_error( fp, query );
445 fprintf( fp, ".\r\n" );
448 exit( EXIT_SUCCESS );
452 if ( (ld = ldap_init( ldaphost, ldapport )) == NULL ) {
453 if ( debug ) perror( "ldap_init" );
454 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
455 LDAP_SERVER_DOWN, myhost, myport );
456 fprintf( fp, ".\r\n" );
458 exit( EXIT_FAILURE );
461 deref = LDAP_DEREF_ALWAYS;
462 if ( !searchaliases )
463 deref = LDAP_DEREF_FINDING;
465 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
467 if ( (rc = ldap_simple_bind_s( ld, NULL, NULL ))
469 if ( debug ) ldap_perror( ld, "ldap_simple_bind_s" );
470 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
471 rc, myhost, myport );
472 fprintf( fp, ".\r\n" );
474 exit( EXIT_FAILURE );
477 switch ( *query++ ) {
478 case 'R': /* read an entry */
479 do_read( ld, fp, query );
482 case 'S': /* search */
483 do_search( ld, fp, query );
486 case 'M': /* X.500 menu */
487 do_menu( ld, fp, query );
491 do_menu( ld, fp, "" );
495 fprintf( fp, ".\r\n" );
498 exit( EXIT_SUCCESS );
503 pick_oc( char **oclist )
507 if ( oclist == NULL )
510 for ( i = 0; oclist[i] != NULL; i++ ) {
511 if ( strcasecmp( oclist[i], "top" ) != 0 &&
512 strcasecmp( oclist[i], "quipuObject" ) != 0 &&
513 strcasecmp( oclist[i], "quipuNonLeafObject" ) != 0 )
521 isnonleaf( LDAP *ld, char **oclist, char *dn )
523 int i, quipuobject = 0;
525 if ( oclist == NULL )
528 for ( i = 0; oclist[i] != NULL; i++ ) {
529 if ( strcasecmp( oclist[i], "quipuObject" ) == 0 )
531 if ( strcasecmp( oclist[i], "quipuNonLeafObject" ) == 0 ||
532 strcasecmp( oclist[i], "externalNonLeafObject" ) == 0 )
537 * not a quipu thang - no easy way to tell leaves from nonleaves
538 * except by trying to search or list. ldap only lets us search.
541 /* expensive - just guess for now */
542 return( quipuobject ? 0 : 1 );
545 if ( !quipuobject ) {
547 struct timeval timeout;
548 LDAPMessage *res = NULL;
549 static char *attrs[] = { "objectClass", 0 };
552 timeout.tv_sec = GO500GW_TIMEOUT;
554 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
555 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
556 "(objectClass=*)", attrs, 0, &timeout, &res ))
557 == LDAP_SUCCESS || rc == LDAP_SIZELIMIT_EXCEEDED ) {
558 sizelimit = LDAP_NO_LIMIT;
559 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
561 numentries = ldap_count_entries( ld, res );
564 return( numentries == 1 ? 1 : 0 );
573 do_menu( LDAP *ld, FILE *fp, char *dn )
577 LDAPFriendlyMap *fm = NULL;
579 if ( strcmp( dn, "" ) != 0 ) {
580 s = ldap_explode_dn( dn, 1 );
583 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
586 fprintf( fp, "0Read %s entry\tR%s\t%s\t%d\r\n", rdn ? rdn: s[0],
587 dn, myhost, myport );
589 ldap_value_free( s );
591 fprintf( fp, "0About the Gopher to X.500 Gateway\tH\t%s\t%d\r\n",
595 fprintf( fp, "7Search %s\tS%s\t%s\t%d\r\n", rdn ? rdn : "root", dn,
598 do_list( ld, fp, dn );
600 ldap_free_friendlymap( &fm );
604 do_list( LDAP *ld, FILE *fp, char *dn )
607 LDAPMessage *e, *res;
608 struct timeval timeout;
609 LDAPFriendlyMap *fm = NULL;
610 static char *attrs[] = { "objectClass", 0 };
611 int deref = LDAP_DEREF_FINDING;
613 timeout.tv_sec = GO500GW_TIMEOUT;
616 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
618 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
619 "(!(objectClass=dSA))", attrs, 0, &timeout, &res )) != LDAP_SUCCESS
620 && rc != LDAP_SIZELIMIT_EXCEEDED ) {
621 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
622 rc, myhost, myport );
626 deref = LDAP_DEREF_ALWAYS;
627 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
629 if ( ldap_count_entries( ld, res ) < 1 ) {
633 #ifdef GO500GW_SORT_ATTR
634 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
638 for ( e = ldap_first_entry( ld, res ); e != NULL;
639 e = ldap_next_entry( ld, e ) ) {
643 dn = ldap_get_dn( ld, e );
644 s = ldap_explode_dn( dn, 1 );
645 oc = ldap_get_values( ld, e, "objectClass" );
648 if ( strcasecmp( doc, "country" ) == 0 ) {
649 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
657 if ( strncasecmp( rdn, "{ASN}", 5 ) != 0 ) {
658 if ( isnonleaf( ld, oc, dn ) ) {
659 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", rdn,
660 doc, dn, myhost, myport );
662 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", rdn,
663 doc, dn, myhost, myport );
668 ldap_value_free( s );
669 ldap_value_free( oc );
671 ldap_free_friendlymap( &fm );
673 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
674 fprintf( fp, "0A size limit was exceeded (explanation)\tLL\t%s\t%d\r\n",
680 isoc( char **ocl, char *oc )
684 for ( i = 0; ocl[i] != NULL; i++ ) {
685 if ( strcasecmp( ocl[i], oc ) == 0 )
693 make_scope( LDAP *ld, char *dn )
698 struct timeval timeout;
699 static char *attrs[] = { "objectClass", 0 };
701 if ( strcmp( dn, "" ) == 0 )
702 return( LDAP_SCOPE_ONELEVEL );
704 timeout.tv_sec = GO500GW_TIMEOUT;
706 if ( ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
707 attrs, 0, &timeout, &res ) != LDAP_SUCCESS ) {
711 oc = ldap_get_values( ld, ldap_first_entry( ld, res ), "objectClass" );
713 if ( isoc( oc, "organization" ) || isoc( oc, "organizationalUnit" ) )
714 scope = LDAP_SCOPE_SUBTREE;
716 scope = LDAP_SCOPE_ONELEVEL;
718 ldap_value_free( oc );
725 do_search( LDAP *ld, FILE *fp, char *query )
732 struct timeval timeout;
734 LDAPMessage *e, *res;
736 static char *attrs[] = { "objectClass", 0 };
738 if ( (filter = strchr( query, '\t' )) == NULL ) {
739 fprintf( fp, "3Missing filter!\r\n" );
740 exit( EXIT_FAILURE );
746 if ( strchr( filter, ',' ) != NULL ) {
747 ldap_ufn_setprefix( ld, base );
748 timeout.tv_sec = GO500GW_TIMEOUT;
750 ldap_ufn_timeout( (void *) &timeout );
752 deref = LDAP_DEREF_FINDING;
753 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
755 if ( (rc = ldap_ufn_search_s( ld, filter, attrs, 0, &res ))
756 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
758 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
759 rc, myhost, myport );
763 count = ldap_count_entries( ld, res );
766 if ( (scope = make_scope( ld, base )) == -1 ) {
767 fprintf( fp, "3Bad scope\r\n" );
768 exit( EXIT_FAILURE );
771 filtertype = (scope == LDAP_SCOPE_ONELEVEL ?
772 "go500gw onelevel" : "go500gw subtree");
773 deref = (scope == LDAP_SCOPE_ONELEVEL ?
774 LDAP_DEREF_FINDING : LDAP_DEREF_ALWAYS);
775 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
776 timeout.tv_sec = GO500GW_TIMEOUT;
779 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
780 fprintf( stderr, "Cannot open filter file (%s)\n",
782 exit( EXIT_FAILURE );
787 for ( fi = ldap_getfirstfilter( filtd, filtertype, filter );
788 fi != NULL; fi = ldap_getnextfilter( filtd ) )
790 if ( (rc = ldap_search_st( ld, base, scope,
791 fi->lfi_filter, attrs, 0, &timeout, &res ))
792 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
793 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
794 rc, myhost, myport );
797 if ( (count = ldap_count_entries( ld, res )) != 0 )
800 deref = LDAP_DEREF_ALWAYS;
801 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
802 ldap_getfilter_free( filtd );
814 e = ldap_first_entry( ld, res );
815 oc = ldap_get_values( ld, e, "objectClass" );
816 dn = ldap_get_dn( ld, e );
818 if ( isnonleaf( ld, oc, dn ) ) {
819 do_menu( ld, fp, dn );
826 ldap_value_free( oc );
829 #ifdef GO500GW_SORT_ATTR
830 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
833 for ( e = ldap_first_entry( ld, res ); e != NULL;
834 e = ldap_next_entry( ld, e ) ) {
838 dn = ldap_get_dn( ld, e );
839 s = ldap_explode_dn( dn, 1 );
840 oc = ldap_get_values( ld, e, "objectClass" );
842 if ( isnonleaf( ld, oc, dn ) )
843 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", s[0],
844 pick_oc( oc ), dn, myhost, myport );
846 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", s[0],
847 pick_oc( oc ), dn, myhost, myport );
850 ldap_value_free( s );
851 ldap_value_free( oc );
854 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
855 fprintf( fp, "0A size limit was exceeded (explanation)\tLS\t%s\t%d\r\n",
862 entry2textwrite( void *fp, char *buf, ber_len_t len )
864 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
868 do_read( LDAP *ld, FILE *fp, char *dn )
870 static struct ldap_disptmpl *tmpllist;
872 ldap_init_templates( templatefile, &tmpllist );
874 if ( ldap_entry2text_search( ld, dn, NULL, NULL, tmpllist, NULL, NULL,
875 entry2textwrite,(void *) fp, "\r\n", rdncount, 0 )
878 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
881 "0An error occurred (explanation)\t@%s\t%s\t%d\r\n",
882 ldap_err2string( ld_errno ), myhost, myport );
885 if ( tmpllist != NULL ) {
886 ldap_free_templates( tmpllist );
896 if ( (fp = fopen( helpfile, "r" )) == NULL ) {
897 fprintf( op, "Cannot access helpfile (%s)\r\n", helpfile );
901 while ( fgets( line, sizeof(line), fp ) != NULL ) {
902 line[ strlen( line ) - 1 ] = '\0';
904 fprintf( op, "%s\r\n", line );
911 do_sizelimit( FILE *fp, char type )
914 fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
915 fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
916 fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
917 fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
919 fprintf( fp, "Not all entries could be returned because a size limit was exceeded.\r\n" );
920 fprintf( fp, "There is no way to defeat this feature, but if you know who you are\r\n" );
921 fprintf( fp, "looking for, try choosing the \"Search\" option listed above and\r\n" );
922 fprintf( fp, "specifying the name of the person you want.\r\n" );
924 fprintf( fp, ".\r\n" );
928 do_error( FILE *fp, char *s )
934 fprintf( fp, "An error occurred searching X.500. The error code was %d\r\n", code );
935 fprintf( fp, "The corresponding error is: %s\r\n", ldap_err2string( code ) );
936 fprintf( fp, "No additional information is available\r\n" );
937 fprintf( fp, ".\r\n" );