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.
17 #include <ac/stdlib.h>
20 #include <ac/signal.h>
21 #include <ac/socket.h>
22 #include <ac/string.h>
23 #include <ac/syslog.h>
25 #include <ac/unistd.h>
28 #include <ac/setproctitle.h>
30 #ifdef HAVE_SYS_PARAM_H
31 #include <sys/param.h>
34 #ifdef HAVE_SYS_RESOURCE_H
35 #include <sys/resource.h>
42 #define ldap_debug debug
48 #include "ldap_defaults.h"
52 int ldap_syslog_level;
57 char *ldaphost = NULL;
59 int searchaliases = 1;
60 char *helpfile = GO500GW_HELPFILE;
61 char *filterfile = FILTERFILE;
62 char *templatefile = TEMPLATEFILE;
63 char *friendlyfile = FRIENDLYFILE;
64 int rdncount = GO500GW_RDNCOUNT;
66 static void usage ( char *name ) LDAP_GCCATTR((noreturn));
67 static int set_socket (int port);
68 static RETSIGTYPE wait4child(int sig);
69 static void do_queries (int s) LDAP_GCCATTR((noreturn));
70 static char *pick_oc ( char **oclist );
71 static int isnonleaf ( LDAP *ld, char **oclist, char *dn );
72 static void do_menu (LDAP *ld, FILE *fp, char *dn);
73 static void do_list (LDAP *ld, FILE *fp, char *dn);
74 static int isoc ( char **ocl, char *oc );
75 static int make_scope ( LDAP *ld, char *dn );
76 static void do_search (LDAP *ld, FILE *fp, char *query);
77 static int entry2textwrite( void *fp, char *buf, ber_len_t len );
78 static void do_read (LDAP *ld, FILE *fp, char *dn);
79 static void do_help (FILE *op);
80 static void do_sizelimit(FILE *fp, char type);
81 static void do_error (FILE *fp, char *s);
83 char myhost[MAXHOSTNAMELEN];
84 int myport = GO500GW_PORT;
89 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 );
94 main (int argc, char **argv )
102 struct sockaddr_in from;
105 #if defined( LDAP_PROCTITLE ) && !defined( HAVE_SETPROCTITLE )
106 /* for setproctitle */
111 while ( (i = getopt( argc, argv, "P:ad:f:h:lp:t:x:Ic:" )) != EOF ) {
113 case 'a': /* search aliases */
117 case 'd': /* debugging level */
118 debug |= atoi( optarg );
121 case 'f': /* ldap filter file */
122 filterfile = strdup( optarg );
125 case 'h': /* gopher help file */
126 helpfile = strdup( optarg );
129 case 'l': /* log to LOG_LOCAL3 */
133 case 'p': /* port to listen on */
134 port = atoi( optarg );
137 case 'P': /* port to connect to ldap server */
138 ldapport = atoi( optarg );
141 case 't': /* ldap template file */
142 templatefile = strdup( optarg );
145 case 'x': /* ldap server hostname */
146 ldaphost = strdup( optarg );
149 case 'I': /* run from inetd */
153 case 'c': /* count of DN components to show */
154 rdncount = atoi( optarg );
163 dtblsize = sysconf( _SC_OPEN_MAX );
164 #elif HAVE_GETDTABLESIZE
165 dtblsize = getdtablesize();
167 dtblsize = FD_SETSIZE;
171 if ( dtblsize > FD_SETSIZE ) {
172 dtblsize = FD_SETSIZE;
174 #endif /* FD_SETSIZE*/
178 #ifdef GO500GW_HOSTNAME
179 strcpy( myhost, GO500GW_HOSTNAME );
181 if ( myhost[0] == '\0' && gethostname( myhost, sizeof(myhost) )
183 perror( "gethostname" );
184 exit( EXIT_FAILURE );
188 /* detach if stderr is redirected or no debugging */
190 lutil_detach( debug && !isatty( 1 ), 1 );
192 if ( (myname = strrchr( argv[0], '/' )) == NULL )
193 myname = strdup( argv[0] );
195 myname = strdup( myname + 1 );
198 ber_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
199 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
203 (void) SIGNAL( SIGPIPE, SIG_IGN );
208 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL3 );
210 openlog( myname, OPENLOG_OPTIONS );
214 syslog( LOG_INFO, "initializing" );
216 /* set up the socket to listen on */
218 s = set_socket( port );
220 /* arrange to reap children */
221 (void) SIGNAL( SIGCHLD, wait4child );
225 fromlen = sizeof(from);
226 if ( getpeername( 0, (struct sockaddr *) &from, &fromlen )
228 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
229 sizeof(from.sin_addr.s_addr), AF_INET );
230 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
231 (hp == NULL) ? "unknown" : hp->h_name,
232 inet_ntoa( from.sin_addr ), 0 );
235 syslog( LOG_INFO, "connection from %s (%s)",
236 (hp == NULL) ? "unknown" : hp->h_name,
237 inet_ntoa( from.sin_addr ) );
240 #ifdef LDAP_PROCTITLE
241 setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
250 exit( EXIT_SUCCESS );
255 FD_SET( s, &readfds );
257 if ( (rc = select( dtblsize, &readfds, 0, 0 ,0 )) == -1 ) {
258 if ( debug ) perror( "select" );
260 } else if ( rc == 0 ) {
264 if ( ! FD_ISSET( s, &readfds ) )
267 fromlen = sizeof(from);
268 if ( (ns = accept( s, (struct sockaddr *) &from, &fromlen ))
270 if ( debug ) perror( "accept" );
271 exit( EXIT_FAILURE );
274 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
275 sizeof(from.sin_addr.s_addr), AF_INET );
278 syslog( LOG_INFO, "TCP connection from %s (%s)",
279 (hp == NULL) ? "unknown" : hp->h_name,
280 inet_ntoa( from.sin_addr ) );
283 switch( pid = fork() ) {
289 case -1: /* failed */
293 default: /* parent */
296 fprintf( stderr, "forked child %d\n", pid );
304 set_socket( int port )
307 struct sockaddr_in addr;
313 if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
315 exit( EXIT_FAILURE );
319 /* set option so clients can't keep us from coming back up */
321 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
322 sizeof(one) ) < 0 ) {
323 perror( "setsockopt" );
324 exit( EXIT_FAILURE );
328 /* enable keep alives */
330 if ( setsockopt( s, SOL_SOCKET, SO_KEEPALIVE, (char *) &one,
331 sizeof(one) ) < 0 ) {
332 perror( "setsockopt" );
333 exit( EXIT_FAILURE );
338 addr.sin_family = AF_INET;
339 addr.sin_addr.s_addr = htonl(INADDR_ANY);
340 addr.sin_port = htons( port );
341 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
343 exit( EXIT_FAILURE );
346 /* listen for connections */
347 if ( listen( s, 5 ) == -1 ) {
349 exit( EXIT_FAILURE );
353 printf( "go500gw listening on port %d\n", port );
359 wait4child( int sig )
362 WAITSTATUSTYPE status;
365 if ( debug ) printf( "parent: catching child status\n" );
368 while (waitpid ((pid_t) -1, (int *) NULL, WAIT_FLAGS) > 0)
371 while (wait4( (pid_t) -1, &status, WAIT_FLAGS, 0 ) > 0 )
375 (void) SIGNAL( SIGCHLD, wait4child );
381 char buf[1024], *query;
386 struct timeval timeout;
390 if ( (fp = fdopen( s, "a+")) == NULL ) {
392 exit( EXIT_FAILURE );
395 timeout.tv_sec = GO500GW_TIMEOUT;
398 FD_SET( fileno( fp ), &readfds );
400 if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
401 exit( EXIT_FAILURE );
403 if ( fgets( buf, sizeof(buf), fp ) == NULL )
404 exit( EXIT_FAILURE );
408 fprintf( stderr, "got %d bytes\n", len );
410 ber_bprint( buf, len );
415 if ( buf[len - 1] == '\n' )
418 if ( buf[len - 1] == '\r' )
424 /* strip off leading white space */
425 while ( isspace( (unsigned char) *query )) {
432 if ( *query == 'H' || *query == 'L' || *query == 'E' ) {
433 switch ( *query++ ) {
434 case 'H': /* help file */
438 case 'L': /* size limit explanation */
439 do_sizelimit( fp, *query );
442 case 'E': /* error explanation */
443 do_error( fp, query );
447 fprintf( fp, ".\r\n" );
450 exit( EXIT_SUCCESS );
454 if ( (ld = ldap_init( ldaphost, ldapport )) == NULL ) {
455 if ( debug ) perror( "ldap_init" );
456 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
457 LDAP_SERVER_DOWN, myhost, myport );
458 fprintf( fp, ".\r\n" );
460 exit( EXIT_FAILURE );
463 deref = LDAP_DEREF_ALWAYS;
464 if ( !searchaliases )
465 deref = LDAP_DEREF_FINDING;
467 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
469 if ( (rc = ldap_simple_bind_s( ld, NULL, NULL ))
471 if ( debug ) ldap_perror( ld, "ldap_simple_bind_s" );
472 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
473 rc, myhost, myport );
474 fprintf( fp, ".\r\n" );
476 exit( EXIT_FAILURE );
479 switch ( *query++ ) {
480 case 'R': /* read an entry */
481 do_read( ld, fp, query );
484 case 'S': /* search */
485 do_search( ld, fp, query );
488 case 'M': /* X.500 menu */
489 do_menu( ld, fp, query );
493 do_menu( ld, fp, "" );
497 fprintf( fp, ".\r\n" );
500 exit( EXIT_SUCCESS );
505 pick_oc( char **oclist )
509 if ( oclist == NULL )
512 for ( i = 0; oclist[i] != NULL; i++ ) {
513 if ( strcasecmp( oclist[i], "top" ) != 0 &&
514 strcasecmp( oclist[i], "quipuObject" ) != 0 &&
515 strcasecmp( oclist[i], "quipuNonLeafObject" ) != 0 )
523 isnonleaf( LDAP *ld, char **oclist, char *dn )
525 int i, quipuobject = 0;
527 if ( oclist == NULL )
530 for ( i = 0; oclist[i] != NULL; i++ ) {
531 if ( strcasecmp( oclist[i], "quipuObject" ) == 0 )
533 if ( strcasecmp( oclist[i], "quipuNonLeafObject" ) == 0 ||
534 strcasecmp( oclist[i], "externalNonLeafObject" ) == 0 )
539 * not a quipu thang - no easy way to tell leaves from nonleaves
540 * except by trying to search or list. ldap only lets us search.
543 /* expensive - just guess for now */
544 return( quipuobject ? 0 : 1 );
547 if ( !quipuobject ) {
549 struct timeval timeout;
550 LDAPMessage *res = NULL;
551 static char *attrs[] = { "objectClass", 0 };
554 timeout.tv_sec = GO500GW_TIMEOUT;
556 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
557 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
558 "(objectClass=*)", attrs, 0, &timeout, &res ))
559 == LDAP_SUCCESS || rc == LDAP_SIZELIMIT_EXCEEDED ) {
560 sizelimit = LDAP_NO_LIMIT;
561 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
563 numentries = ldap_count_entries( ld, res );
566 return( numentries == 1 ? 1 : 0 );
575 do_menu( LDAP *ld, FILE *fp, char *dn )
579 LDAPFriendlyMap *fm = NULL;
581 if ( strcmp( dn, "" ) != 0 ) {
582 s = ldap_explode_dn( dn, 1 );
585 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
588 fprintf( fp, "0Read %s entry\tR%s\t%s\t%d\r\n", rdn ? rdn: s[0],
589 dn, myhost, myport );
591 ldap_value_free( s );
593 fprintf( fp, "0About the Gopher to X.500 Gateway\tH\t%s\t%d\r\n",
597 fprintf( fp, "7Search %s\tS%s\t%s\t%d\r\n", rdn ? rdn : "root", dn,
600 do_list( ld, fp, dn );
602 ldap_free_friendlymap( &fm );
606 do_list( LDAP *ld, FILE *fp, char *dn )
609 LDAPMessage *e, *res;
610 struct timeval timeout;
611 LDAPFriendlyMap *fm = NULL;
612 static char *attrs[] = { "objectClass", 0 };
613 int deref = LDAP_DEREF_FINDING;
615 timeout.tv_sec = GO500GW_TIMEOUT;
618 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
620 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
621 "(!(objectClass=dSA))", attrs, 0, &timeout, &res )) != LDAP_SUCCESS
622 && rc != LDAP_SIZELIMIT_EXCEEDED ) {
623 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
624 rc, myhost, myport );
628 deref = LDAP_DEREF_ALWAYS;
629 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
631 if ( ldap_count_entries( ld, res ) < 1 ) {
635 #ifdef GO500GW_SORT_ATTR
636 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
640 for ( e = ldap_first_entry( ld, res ); e != NULL;
641 e = ldap_next_entry( ld, e ) ) {
645 dn = ldap_get_dn( ld, e );
646 s = ldap_explode_dn( dn, 1 );
647 oc = ldap_get_values( ld, e, "objectClass" );
650 if ( strcasecmp( doc, "country" ) == 0 ) {
651 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
659 if ( strncasecmp( rdn, "{ASN}", 5 ) != 0 ) {
660 if ( isnonleaf( ld, oc, dn ) ) {
661 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", rdn,
662 doc, dn, myhost, myport );
664 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", rdn,
665 doc, dn, myhost, myport );
670 ldap_value_free( s );
671 ldap_value_free( oc );
673 ldap_free_friendlymap( &fm );
675 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
676 fprintf( fp, "0A size limit was exceeded (explanation)\tLL\t%s\t%d\r\n",
682 isoc( char **ocl, char *oc )
686 for ( i = 0; ocl[i] != NULL; i++ ) {
687 if ( strcasecmp( ocl[i], oc ) == 0 )
695 make_scope( LDAP *ld, char *dn )
700 struct timeval timeout;
701 static char *attrs[] = { "objectClass", 0 };
703 if ( strcmp( dn, "" ) == 0 )
704 return( LDAP_SCOPE_ONELEVEL );
706 timeout.tv_sec = GO500GW_TIMEOUT;
708 if ( ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
709 attrs, 0, &timeout, &res ) != LDAP_SUCCESS ) {
713 oc = ldap_get_values( ld, ldap_first_entry( ld, res ), "objectClass" );
715 if ( isoc( oc, "organization" ) || isoc( oc, "organizationalUnit" ) )
716 scope = LDAP_SCOPE_SUBTREE;
718 scope = LDAP_SCOPE_ONELEVEL;
720 ldap_value_free( oc );
727 do_search( LDAP *ld, FILE *fp, char *query )
734 struct timeval timeout;
736 LDAPMessage *e, *res;
738 static char *attrs[] = { "objectClass", 0 };
740 if ( (filter = strchr( query, '\t' )) == NULL ) {
741 fprintf( fp, "3Missing filter!\r\n" );
742 exit( EXIT_FAILURE );
748 if ( strchr( filter, ',' ) != NULL ) {
749 ldap_ufn_setprefix( ld, base );
750 timeout.tv_sec = GO500GW_TIMEOUT;
752 ldap_ufn_timeout( (void *) &timeout );
754 deref = LDAP_DEREF_FINDING;
755 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
757 if ( (rc = ldap_ufn_search_s( ld, filter, attrs, 0, &res ))
758 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
760 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
761 rc, myhost, myport );
765 count = ldap_count_entries( ld, res );
768 if ( (scope = make_scope( ld, base )) == -1 ) {
769 fprintf( fp, "3Bad scope\r\n" );
770 exit( EXIT_FAILURE );
773 filtertype = (scope == LDAP_SCOPE_ONELEVEL ?
774 "go500gw onelevel" : "go500gw subtree");
775 deref = (scope == LDAP_SCOPE_ONELEVEL ?
776 LDAP_DEREF_FINDING : LDAP_DEREF_ALWAYS);
777 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
778 timeout.tv_sec = GO500GW_TIMEOUT;
781 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
782 fprintf( stderr, "Cannot open filter file (%s)\n",
784 exit( EXIT_FAILURE );
789 for ( fi = ldap_getfirstfilter( filtd, filtertype, filter );
790 fi != NULL; fi = ldap_getnextfilter( filtd ) )
792 if ( (rc = ldap_search_st( ld, base, scope,
793 fi->lfi_filter, attrs, 0, &timeout, &res ))
794 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
795 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
796 rc, myhost, myport );
799 if ( (count = ldap_count_entries( ld, res )) != 0 )
802 deref = LDAP_DEREF_ALWAYS;
803 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
804 ldap_getfilter_free( filtd );
816 e = ldap_first_entry( ld, res );
817 oc = ldap_get_values( ld, e, "objectClass" );
818 dn = ldap_get_dn( ld, e );
820 if ( isnonleaf( ld, oc, dn ) ) {
821 do_menu( ld, fp, dn );
828 ldap_value_free( oc );
831 #ifdef GO500GW_SORT_ATTR
832 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
835 for ( e = ldap_first_entry( ld, res ); e != NULL;
836 e = ldap_next_entry( ld, e ) ) {
840 dn = ldap_get_dn( ld, e );
841 s = ldap_explode_dn( dn, 1 );
842 oc = ldap_get_values( ld, e, "objectClass" );
844 if ( isnonleaf( ld, oc, dn ) )
845 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", s[0],
846 pick_oc( oc ), dn, myhost, myport );
848 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", s[0],
849 pick_oc( oc ), dn, myhost, myport );
852 ldap_value_free( s );
853 ldap_value_free( oc );
856 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
857 fprintf( fp, "0A size limit was exceeded (explanation)\tLS\t%s\t%d\r\n",
864 entry2textwrite( void *fp, char *buf, ber_len_t len )
866 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
870 do_read( LDAP *ld, FILE *fp, char *dn )
872 static struct ldap_disptmpl *tmpllist;
874 ldap_init_templates( templatefile, &tmpllist );
876 if ( ldap_entry2text_search( ld, dn, NULL, NULL, tmpllist, NULL, NULL,
877 entry2textwrite,(void *) fp, "\r\n", rdncount, 0 )
880 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
883 "0An error occurred (explanation)\t@%s\t%s\t%d\r\n",
884 ldap_err2string( ld_errno ), myhost, myport );
887 if ( tmpllist != NULL ) {
888 ldap_free_templates( tmpllist );
898 if ( (fp = fopen( helpfile, "r" )) == NULL ) {
899 fprintf( op, "Cannot access helpfile (%s)\r\n", helpfile );
903 while ( fgets( line, sizeof(line), fp ) != NULL ) {
904 line[ strlen( line ) - 1 ] = '\0';
906 fprintf( op, "%s\r\n", line );
913 do_sizelimit( FILE *fp, char type )
916 fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
917 fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
918 fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
919 fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
921 fprintf( fp, "Not all entries could be returned because a size limit was exceeded.\r\n" );
922 fprintf( fp, "There is no way to defeat this feature, but if you know who you are\r\n" );
923 fprintf( fp, "looking for, try choosing the \"Search\" option listed above and\r\n" );
924 fprintf( fp, "specifying the name of the person you want.\r\n" );
926 fprintf( fp, ".\r\n" );
930 do_error( FILE *fp, char *s )
936 fprintf( fp, "An error occurred searching X.500. The error code was %d\r\n", code );
937 fprintf( fp, "The corresponding error is: %s\r\n", ldap_err2string( code ) );
938 fprintf( fp, "No additional information is available\r\n" );
939 fprintf( fp, ".\r\n" );