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>
29 #include <ac/setproctitle.h>
31 #ifdef HAVE_SYS_PARAM_H
32 #include <sys/param.h>
35 #ifdef HAVE_SYS_RESOURCE_H
36 #include <sys/resource.h>
43 #define ldap_debug debug
49 #include "ldap_defaults.h"
53 int ldap_syslog_level;
58 char *ldaphost = NULL;
60 int searchaliases = 1;
61 char *helpfile = GO500GW_HELPFILE;
62 char *filterfile = FILTERFILE;
63 char *templatefile = TEMPLATEFILE;
64 char *friendlyfile = FRIENDLYFILE;
65 int rdncount = GO500GW_RDNCOUNT;
67 static void usage ( char *name ) LDAP_GCCATTR((noreturn));
68 static int set_socket (int port);
69 static RETSIGTYPE wait4child(int sig);
70 static void do_queries (int s) LDAP_GCCATTR((noreturn));
71 static char *pick_oc ( char **oclist );
72 static int isnonleaf ( LDAP *ld, char **oclist, char *dn );
73 static void do_menu (LDAP *ld, FILE *fp, char *dn);
74 static void do_list (LDAP *ld, FILE *fp, char *dn);
75 static int isoc ( char **ocl, char *oc );
76 static int make_scope ( LDAP *ld, char *dn );
77 static void do_search (LDAP *ld, FILE *fp, char *query);
78 static int entry2textwrite( void *fp, char *buf, ber_len_t len );
79 static void do_read (LDAP *ld, FILE *fp, char *dn);
80 static void do_help (FILE *op);
81 static void do_sizelimit(FILE *fp, char type);
82 static void do_error (FILE *fp, char *s);
84 char myhost[MAXHOSTNAMELEN];
85 int myport = GO500GW_PORT;
90 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 );
95 main (int argc, char **argv )
103 struct sockaddr_in from;
106 #if defined( LDAP_PROCTITLE ) && !defined( HAVE_SETPROCTITLE )
107 /* for setproctitle */
112 while ( (i = getopt( argc, argv, "P:ad:f:h:lp:t:x:Ic:" )) != EOF ) {
114 case 'a': /* search aliases */
118 case 'd': /* debugging level */
119 debug |= atoi( optarg );
122 case 'f': /* ldap filter file */
123 filterfile = strdup( optarg );
126 case 'h': /* gopher help file */
127 helpfile = strdup( optarg );
130 case 'l': /* log to LOG_LOCAL3 */
134 case 'p': /* port to listen on */
135 port = atoi( optarg );
138 case 'P': /* port to connect to ldap server */
139 ldapport = atoi( optarg );
142 case 't': /* ldap template file */
143 templatefile = strdup( optarg );
146 case 'x': /* ldap server hostname */
147 ldaphost = strdup( optarg );
150 case 'I': /* run from inetd */
154 case 'c': /* count of DN components to show */
155 rdncount = atoi( optarg );
164 dtblsize = sysconf( _SC_OPEN_MAX );
165 #elif HAVE_GETDTABLESIZE
166 dtblsize = getdtablesize();
168 dtblsize = FD_SETSIZE;
172 if ( dtblsize > FD_SETSIZE ) {
173 dtblsize = FD_SETSIZE;
175 #endif /* FD_SETSIZE*/
179 #ifdef GO500GW_HOSTNAME
180 strcpy( myhost, GO500GW_HOSTNAME );
182 if ( myhost[0] == '\0' && gethostname( myhost, sizeof(myhost) )
184 perror( "gethostname" );
185 exit( EXIT_FAILURE );
189 /* detach if stderr is redirected or no debugging */
191 lutil_detach( debug && !isatty( 1 ), 1 );
193 if ( (myname = strrchr( argv[0], '/' )) == NULL )
194 myname = strdup( argv[0] );
196 myname = strdup( myname + 1 );
199 ber_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
200 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
204 (void) SIGNAL( SIGPIPE, SIG_IGN );
209 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL3 );
211 openlog( myname, OPENLOG_OPTIONS );
215 syslog( LOG_INFO, "initializing" );
217 /* set up the socket to listen on */
219 s = set_socket( port );
221 /* arrange to reap children */
222 (void) SIGNAL( SIGCHLD, wait4child );
226 fromlen = sizeof(from);
227 if ( getpeername( 0, (struct sockaddr *) &from, &fromlen )
229 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
230 sizeof(from.sin_addr.s_addr), AF_INET );
231 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
232 (hp == NULL) ? "unknown" : hp->h_name,
233 inet_ntoa( from.sin_addr ), 0 );
236 syslog( LOG_INFO, "connection from %s (%s)",
237 (hp == NULL) ? "unknown" : hp->h_name,
238 inet_ntoa( from.sin_addr ) );
241 #ifdef LDAP_PROCTITLE
242 setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
251 exit( EXIT_SUCCESS );
256 FD_SET( s, &readfds );
258 if ( (rc = select( dtblsize, &readfds, 0, 0 ,0 )) == -1 ) {
259 if ( debug ) perror( "select" );
261 } else if ( rc == 0 ) {
265 if ( ! FD_ISSET( s, &readfds ) )
268 fromlen = sizeof(from);
269 if ( (ns = accept( s, (struct sockaddr *) &from, &fromlen ))
271 if ( debug ) perror( "accept" );
272 exit( EXIT_FAILURE );
275 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
276 sizeof(from.sin_addr.s_addr), AF_INET );
279 syslog( LOG_INFO, "TCP connection from %s (%s)",
280 (hp == NULL) ? "unknown" : hp->h_name,
281 inet_ntoa( from.sin_addr ) );
284 switch( pid = fork() ) {
290 case -1: /* failed */
294 default: /* parent */
297 fprintf( stderr, "forked child %d\n", pid );
305 set_socket( int port )
308 struct sockaddr_in addr;
314 if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
316 exit( EXIT_FAILURE );
320 /* set option so clients can't keep us from coming back up */
322 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
323 sizeof(one) ) < 0 ) {
324 perror( "setsockopt" );
325 exit( EXIT_FAILURE );
329 /* enable keep alives */
331 if ( setsockopt( s, SOL_SOCKET, SO_KEEPALIVE, (char *) &one,
332 sizeof(one) ) < 0 ) {
333 perror( "setsockopt" );
334 exit( EXIT_FAILURE );
339 addr.sin_family = AF_INET;
340 addr.sin_addr.s_addr = htonl(INADDR_ANY);
341 addr.sin_port = htons( port );
342 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
344 exit( EXIT_FAILURE );
347 /* listen for connections */
348 if ( listen( s, 5 ) == -1 ) {
350 exit( EXIT_FAILURE );
354 printf( "go500gw listening on port %d\n", port );
360 wait4child( int sig )
363 WAITSTATUSTYPE status;
366 if ( debug ) printf( "parent: catching child status\n" );
369 while (waitpid ((pid_t) -1, (int *) NULL, WAIT_FLAGS) > 0)
372 while (wait4( (pid_t) -1, &status, WAIT_FLAGS, 0 ) > 0 )
376 (void) SIGNAL( SIGCHLD, wait4child );
382 char buf[1024], *query;
387 struct timeval timeout;
391 if ( (fp = fdopen( s, "a+")) == NULL ) {
393 exit( EXIT_FAILURE );
396 timeout.tv_sec = GO500GW_TIMEOUT;
399 FD_SET( fileno( fp ), &readfds );
401 if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
402 exit( EXIT_FAILURE );
404 if ( fgets( buf, sizeof(buf), fp ) == NULL )
405 exit( EXIT_FAILURE );
409 fprintf( stderr, "got %d bytes\n", len );
411 ber_bprint( buf, len );
416 if ( buf[len - 1] == '\n' )
419 if ( buf[len - 1] == '\r' )
425 /* strip off leading white space */
426 while ( isspace( (unsigned char) *query )) {
433 if ( *query == 'H' || *query == 'L' || *query == 'E' ) {
434 switch ( *query++ ) {
435 case 'H': /* help file */
439 case 'L': /* size limit explanation */
440 do_sizelimit( fp, *query );
443 case 'E': /* error explanation */
444 do_error( fp, query );
448 fprintf( fp, ".\r\n" );
451 exit( EXIT_SUCCESS );
455 if ( (ld = ldap_init( ldaphost, ldapport )) == NULL ) {
456 if ( debug ) perror( "ldap_init" );
457 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
458 LDAP_SERVER_DOWN, myhost, myport );
459 fprintf( fp, ".\r\n" );
461 exit( EXIT_FAILURE );
464 deref = LDAP_DEREF_ALWAYS;
465 if ( !searchaliases )
466 deref = LDAP_DEREF_FINDING;
468 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
470 if ( (rc = ldap_simple_bind_s( ld, NULL, NULL ))
472 if ( debug ) ldap_perror( ld, "ldap_simple_bind_s" );
473 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
474 rc, myhost, myport );
475 fprintf( fp, ".\r\n" );
477 exit( EXIT_FAILURE );
480 switch ( *query++ ) {
481 case 'R': /* read an entry */
482 do_read( ld, fp, query );
485 case 'S': /* search */
486 do_search( ld, fp, query );
489 case 'M': /* X.500 menu */
490 do_menu( ld, fp, query );
494 do_menu( ld, fp, "" );
498 fprintf( fp, ".\r\n" );
501 exit( EXIT_SUCCESS );
506 pick_oc( char **oclist )
510 if ( oclist == NULL )
513 for ( i = 0; oclist[i] != NULL; i++ ) {
514 if ( strcasecmp( oclist[i], "top" ) != 0 &&
515 strcasecmp( oclist[i], "quipuObject" ) != 0 &&
516 strcasecmp( oclist[i], "quipuNonLeafObject" ) != 0 )
524 isnonleaf( LDAP *ld, char **oclist, char *dn )
526 int i, quipuobject = 0;
528 if ( oclist == NULL )
531 for ( i = 0; oclist[i] != NULL; i++ ) {
532 if ( strcasecmp( oclist[i], "quipuObject" ) == 0 )
534 if ( strcasecmp( oclist[i], "quipuNonLeafObject" ) == 0 ||
535 strcasecmp( oclist[i], "externalNonLeafObject" ) == 0 )
540 * not a quipu thang - no easy way to tell leaves from nonleaves
541 * except by trying to search or list. ldap only lets us search.
544 /* expensive - just guess for now */
545 return( quipuobject ? 0 : 1 );
548 if ( !quipuobject ) {
550 struct timeval timeout;
551 LDAPMessage *res = NULL;
552 static char *attrs[] = { "objectClass", 0 };
555 timeout.tv_sec = GO500GW_TIMEOUT;
557 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
558 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
559 "(objectClass=*)", attrs, 0, &timeout, &res ))
560 == LDAP_SUCCESS || rc == LDAP_SIZELIMIT_EXCEEDED ) {
561 sizelimit = LDAP_NO_LIMIT;
562 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
564 numentries = ldap_count_entries( ld, res );
567 return( numentries == 1 ? 1 : 0 );
576 do_menu( LDAP *ld, FILE *fp, char *dn )
580 LDAPFriendlyMap *fm = NULL;
582 if ( strcmp( dn, "" ) != 0 ) {
583 s = ldap_explode_dn( dn, 1 );
586 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
589 fprintf( fp, "0Read %s entry\tR%s\t%s\t%d\r\n", rdn ? rdn: s[0],
590 dn, myhost, myport );
592 ldap_value_free( s );
594 fprintf( fp, "0About the Gopher to X.500 Gateway\tH\t%s\t%d\r\n",
598 fprintf( fp, "7Search %s\tS%s\t%s\t%d\r\n", rdn ? rdn : "root", dn,
601 do_list( ld, fp, dn );
603 ldap_free_friendlymap( &fm );
607 do_list( LDAP *ld, FILE *fp, char *dn )
610 LDAPMessage *e, *res;
611 struct timeval timeout;
612 LDAPFriendlyMap *fm = NULL;
613 static char *attrs[] = { "objectClass", 0 };
614 int deref = LDAP_DEREF_FINDING;
616 timeout.tv_sec = GO500GW_TIMEOUT;
619 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
621 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
622 "(!(objectClass=dSA))", attrs, 0, &timeout, &res )) != LDAP_SUCCESS
623 && rc != LDAP_SIZELIMIT_EXCEEDED ) {
624 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
625 rc, myhost, myport );
629 deref = LDAP_DEREF_ALWAYS;
630 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
632 if ( ldap_count_entries( ld, res ) < 1 ) {
636 #ifdef GO500GW_SORT_ATTR
637 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
641 for ( e = ldap_first_entry( ld, res ); e != NULL;
642 e = ldap_next_entry( ld, e ) ) {
646 dn = ldap_get_dn( ld, e );
647 s = ldap_explode_dn( dn, 1 );
648 oc = ldap_get_values( ld, e, "objectClass" );
651 if ( strcasecmp( doc, "country" ) == 0 ) {
652 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
660 if ( strncasecmp( rdn, "{ASN}", 5 ) != 0 ) {
661 if ( isnonleaf( ld, oc, dn ) ) {
662 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", rdn,
663 doc, dn, myhost, myport );
665 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", rdn,
666 doc, dn, myhost, myport );
671 ldap_value_free( s );
672 ldap_value_free( oc );
674 ldap_free_friendlymap( &fm );
676 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
677 fprintf( fp, "0A size limit was exceeded (explanation)\tLL\t%s\t%d\r\n",
683 isoc( char **ocl, char *oc )
687 for ( i = 0; ocl[i] != NULL; i++ ) {
688 if ( strcasecmp( ocl[i], oc ) == 0 )
696 make_scope( LDAP *ld, char *dn )
701 struct timeval timeout;
702 static char *attrs[] = { "objectClass", 0 };
704 if ( strcmp( dn, "" ) == 0 )
705 return( LDAP_SCOPE_ONELEVEL );
707 timeout.tv_sec = GO500GW_TIMEOUT;
709 if ( ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
710 attrs, 0, &timeout, &res ) != LDAP_SUCCESS ) {
714 oc = ldap_get_values( ld, ldap_first_entry( ld, res ), "objectClass" );
716 if ( isoc( oc, "organization" ) || isoc( oc, "organizationalUnit" ) )
717 scope = LDAP_SCOPE_SUBTREE;
719 scope = LDAP_SCOPE_ONELEVEL;
721 ldap_value_free( oc );
728 do_search( LDAP *ld, FILE *fp, char *query )
735 struct timeval timeout;
737 LDAPMessage *e, *res;
739 static char *attrs[] = { "objectClass", 0 };
741 if ( (filter = strchr( query, '\t' )) == NULL ) {
742 fprintf( fp, "3Missing filter!\r\n" );
743 exit( EXIT_FAILURE );
749 if ( strchr( filter, ',' ) != NULL ) {
750 ldap_ufn_setprefix( ld, base );
751 timeout.tv_sec = GO500GW_TIMEOUT;
753 ldap_ufn_timeout( (void *) &timeout );
755 deref = LDAP_DEREF_FINDING;
756 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
758 if ( (rc = ldap_ufn_search_s( ld, filter, attrs, 0, &res ))
759 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
761 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
762 rc, myhost, myport );
766 count = ldap_count_entries( ld, res );
769 if ( (scope = make_scope( ld, base )) == -1 ) {
770 fprintf( fp, "3Bad scope\r\n" );
771 exit( EXIT_FAILURE );
774 filtertype = (scope == LDAP_SCOPE_ONELEVEL ?
775 "go500gw onelevel" : "go500gw subtree");
776 deref = (scope == LDAP_SCOPE_ONELEVEL ?
777 LDAP_DEREF_FINDING : LDAP_DEREF_ALWAYS);
778 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
779 timeout.tv_sec = GO500GW_TIMEOUT;
782 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
783 fprintf( stderr, "Cannot open filter file (%s)\n",
785 exit( EXIT_FAILURE );
790 for ( fi = ldap_getfirstfilter( filtd, filtertype, filter );
791 fi != NULL; fi = ldap_getnextfilter( filtd ) )
793 if ( (rc = ldap_search_st( ld, base, scope,
794 fi->lfi_filter, attrs, 0, &timeout, &res ))
795 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
796 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
797 rc, myhost, myport );
800 if ( (count = ldap_count_entries( ld, res )) != 0 )
803 deref = LDAP_DEREF_ALWAYS;
804 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
805 ldap_getfilter_free( filtd );
817 e = ldap_first_entry( ld, res );
818 oc = ldap_get_values( ld, e, "objectClass" );
819 dn = ldap_get_dn( ld, e );
821 if ( isnonleaf( ld, oc, dn ) ) {
822 do_menu( ld, fp, dn );
829 ldap_value_free( oc );
832 #ifdef GO500GW_SORT_ATTR
833 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
836 for ( e = ldap_first_entry( ld, res ); e != NULL;
837 e = ldap_next_entry( ld, e ) ) {
841 dn = ldap_get_dn( ld, e );
842 s = ldap_explode_dn( dn, 1 );
843 oc = ldap_get_values( ld, e, "objectClass" );
845 if ( isnonleaf( ld, oc, dn ) )
846 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", s[0],
847 pick_oc( oc ), dn, myhost, myport );
849 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", s[0],
850 pick_oc( oc ), dn, myhost, myport );
853 ldap_value_free( s );
854 ldap_value_free( oc );
857 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
858 fprintf( fp, "0A size limit was exceeded (explanation)\tLS\t%s\t%d\r\n",
865 entry2textwrite( void *fp, char *buf, ber_len_t len )
867 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
871 do_read( LDAP *ld, FILE *fp, char *dn )
873 static struct ldap_disptmpl *tmpllist;
875 ldap_init_templates( templatefile, &tmpllist );
877 if ( ldap_entry2text_search( ld, dn, NULL, NULL, tmpllist, NULL, NULL,
878 entry2textwrite,(void *) fp, "\r\n", rdncount, 0 )
881 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
884 "0An error occurred (explanation)\t@%s\t%s\t%d\r\n",
885 ldap_err2string( ld_errno ), myhost, myport );
888 if ( tmpllist != NULL ) {
889 ldap_free_templates( tmpllist );
899 if ( (fp = fopen( helpfile, "r" )) == NULL ) {
900 fprintf( op, "Cannot access helpfile (%s)\r\n", helpfile );
904 while ( fgets( line, sizeof(line), fp ) != NULL ) {
905 line[ strlen( line ) - 1 ] = '\0';
907 fprintf( op, "%s\r\n", line );
914 do_sizelimit( FILE *fp, char type )
917 fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
918 fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
919 fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
920 fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
922 fprintf( fp, "Not all entries could be returned because a size limit was exceeded.\r\n" );
923 fprintf( fp, "There is no way to defeat this feature, but if you know who you are\r\n" );
924 fprintf( fp, "looking for, try choosing the \"Search\" option listed above and\r\n" );
925 fprintf( fp, "specifying the name of the person you want.\r\n" );
927 fprintf( fp, ".\r\n" );
931 do_error( FILE *fp, char *s )
937 fprintf( fp, "An error occurred searching X.500. The error code was %d\r\n", code );
938 fprintf( fp, "The corresponding error is: %s\r\n", ldap_err2string( code ) );
939 fprintf( fp, "No additional information is available\r\n" );
940 fprintf( fp, ".\r\n" );