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>
45 #include "ldapconfig.h"
52 char *ldaphost = NULL;
54 int searchaliases = 1;
55 char *helpfile = GO500GW_HELPFILE;
56 char *filterfile = FILTERFILE;
57 char *templatefile = TEMPLATEFILE;
58 char *friendlyfile = FRIENDLYFILE;
59 int rdncount = GO500GW_RDNCOUNT;
61 static void usage ( char *name );
62 static int set_socket (int port);
63 static RETSIGTYPE wait4child(int sig);
64 static void do_queries (int s);
65 static char *pick_oc ( char **oclist );
66 static int isnonleaf ( LDAP *ld, char **oclist, char *dn );
67 static void do_menu (LDAP *ld, FILE *fp, char *dn);
68 static void do_list (LDAP *ld, FILE *fp, char *dn);
69 static int isoc ( char **ocl, char *oc );
70 static int make_scope ( LDAP *ld, char *dn );
71 static void do_search (LDAP *ld, FILE *fp, char *query);
72 static int entry2textwrite( void *fp, char *buf, int len );
73 static void do_read (LDAP *ld, FILE *fp, char *dn);
74 static void do_help (FILE *op);
75 static void do_sizelimit(FILE *fp, char type);
76 static void do_error (FILE *fp, char *s);
78 char myhost[MAXHOSTNAMELEN];
79 int myport = GO500GW_PORT;
84 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 );
89 main (int argc, char **argv )
97 struct sockaddr_in from;
100 #if defined( LDAP_PROCTITLE ) && !defined( HAVE_SETPROCTITLE )
101 /* for setproctitle */
106 while ( (i = getopt( argc, argv, "P:ad:f:h:lp:t:x:Ic:" )) != EOF ) {
108 case 'a': /* search aliases */
112 case 'd': /* debugging level */
113 debug = atoi( optarg );
117 fprintf( stderr, "warning: ldap debugging requires LDAP_DEBUG\n" );
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" );
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 );
199 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL3 );
201 openlog( myname, OPENLOG_OPTIONS );
205 syslog( LOG_INFO, "initializing" );
207 /* set up the socket to listen on */
209 s = set_socket( port );
211 /* arrange to reap children */
212 (void) SIGNAL( SIGCHLD, wait4child );
216 fromlen = sizeof(from);
217 if ( getpeername( 0, (struct sockaddr *) &from, &fromlen )
219 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
220 sizeof(from.sin_addr.s_addr), AF_INET );
221 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
222 (hp == NULL) ? "unknown" : hp->h_name,
223 inet_ntoa( from.sin_addr ), 0 );
226 syslog( LOG_INFO, "connection from %s (%s)",
227 (hp == NULL) ? "unknown" : hp->h_name,
228 inet_ntoa( from.sin_addr ) );
231 #ifdef LDAP_PROCTITLE
232 setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
246 FD_SET( s, &readfds );
248 if ( (rc = select( dtblsize, &readfds, 0, 0 ,0 )) == -1 ) {
249 if ( debug ) perror( "select" );
251 } else if ( rc == 0 ) {
255 if ( ! FD_ISSET( s, &readfds ) )
258 fromlen = sizeof(from);
259 if ( (ns = accept( s, (struct sockaddr *) &from, &fromlen ))
261 if ( debug ) perror( "accept" );
265 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
266 sizeof(from.sin_addr.s_addr), AF_INET );
269 syslog( LOG_INFO, "TCP connection from %s (%s)",
270 (hp == NULL) ? "unknown" : hp->h_name,
271 inet_ntoa( from.sin_addr ) );
274 switch( pid = fork() ) {
280 case -1: /* failed */
284 default: /* parent */
287 fprintf( stderr, "forked child %d\n", pid );
295 set_socket( int port )
298 struct sockaddr_in addr;
304 if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
309 /* set option so clients can't keep us from coming back up */
311 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
312 sizeof(one) ) < 0 ) {
313 perror( "setsockopt" );
318 addr.sin_family = AF_INET;
319 addr.sin_addr.s_addr = INADDR_ANY;
320 addr.sin_port = htons( port );
321 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
326 /* listen for connections */
327 if ( listen( s, 5 ) == -1 ) {
333 printf( "go500gw listening on port %d\n", port );
339 wait4child( int sig )
342 WAITSTATUSTYPE status;
345 if ( debug ) printf( "parent: catching child status\n" );
348 while (waitpid ((pid_t) -1, NULL, WAIT_FLAGS) > 0)
351 while (wait3( &status, WAIT_FLAGS, 0 ) > 0 )
355 (void) SIGNAL( SIGCHLD, wait4child );
361 char buf[1024], *query;
366 struct timeval timeout;
370 if ( (fp = fdopen( s, "a+")) == NULL ) {
375 timeout.tv_sec = GO500GW_TIMEOUT;
378 FD_SET( fileno( fp ), &readfds );
380 if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
383 if ( fgets( buf, sizeof(buf), fp ) == NULL )
388 fprintf( stderr, "got %d bytes\n", len );
390 lber_bprint( buf, len );
395 if ( buf[len - 1] == '\n' )
398 if ( buf[len - 1] == '\r' )
404 /* strip off leading white space */
405 while ( isspace( *query )) {
412 if ( *query == 'H' || *query == 'L' || *query == 'E' ) {
413 switch ( *query++ ) {
414 case 'H': /* help file */
418 case 'L': /* size limit explanation */
419 do_sizelimit( fp, *query );
422 case 'E': /* error explanation */
423 do_error( fp, query );
427 fprintf( fp, ".\r\n" );
434 if ( (ld = ldap_open( ldaphost, ldapport )) == NULL ) {
435 if ( debug ) perror( "ldap_open" );
436 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
437 LDAP_SERVER_DOWN, myhost, myport );
438 fprintf( fp, ".\r\n" );
443 deref = LDAP_DEREF_ALWAYS;
444 if ( !searchaliases )
445 deref = LDAP_DEREF_FINDING;
447 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
449 if ( (rc = ldap_simple_bind_s( ld, NULL, NULL ))
451 if ( debug ) ldap_perror( ld, "ldap_simple_bind_s" );
452 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
453 rc, myhost, myport );
454 fprintf( fp, ".\r\n" );
459 switch ( *query++ ) {
460 case 'R': /* read an entry */
461 do_read( ld, fp, query );
464 case 'S': /* search */
465 do_search( ld, fp, query );
468 case 'M': /* X.500 menu */
469 do_menu( ld, fp, query );
473 do_menu( ld, fp, "" );
477 fprintf( fp, ".\r\n" );
485 pick_oc( char **oclist )
489 if ( oclist == NULL )
492 for ( i = 0; oclist[i] != NULL; i++ ) {
493 if ( strcasecmp( oclist[i], "top" ) != 0 &&
494 strcasecmp( oclist[i], "quipuObject" ) != 0 &&
495 strcasecmp( oclist[i], "quipuNonLeafObject" ) != 0 )
503 isnonleaf( LDAP *ld, char **oclist, char *dn )
505 int i, quipuobject = 0;
507 if ( oclist == NULL )
510 for ( i = 0; oclist[i] != NULL; i++ ) {
511 if ( strcasecmp( oclist[i], "quipuObject" ) == 0 )
513 if ( strcasecmp( oclist[i], "quipuNonLeafObject" ) == 0 ||
514 strcasecmp( oclist[i], "externalNonLeafObject" ) == 0 )
519 * not a quipu thang - no easy way to tell leaves from nonleaves
520 * except by trying to search or list. ldap only lets us search.
523 /* expensive - just guess for now */
524 return( quipuobject ? 0 : 1 );
527 if ( !quipuobject ) {
529 struct timeval timeout;
530 LDAPMessage *res = NULL;
531 static char *attrs[] = { "objectClass", 0 };
534 timeout.tv_sec = GO500GW_TIMEOUT;
536 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
537 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
538 "(objectClass=*)", attrs, 0, &timeout, &res ))
539 == LDAP_SUCCESS || rc == LDAP_SIZELIMIT_EXCEEDED ) {
540 sizelimit = LDAP_NO_LIMIT;
541 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
543 numentries = ldap_count_entries( ld, res );
546 return( numentries == 1 ? 1 : 0 );
555 do_menu( LDAP *ld, FILE *fp, char *dn )
559 FriendlyMap *fm = NULL;
561 if ( strcmp( dn, "" ) != 0 ) {
562 s = ldap_explode_dn( dn, 1 );
565 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
568 fprintf( fp, "0Read %s entry\tR%s\t%s\t%d\r\n", rdn ? rdn: s[0],
569 dn, myhost, myport );
571 ldap_value_free( s );
573 fprintf( fp, "0About the Gopher to X.500 Gateway\tH\t%s\t%d\r\n",
577 fprintf( fp, "7Search %s\tS%s\t%s\t%d\r\n", rdn ? rdn : "root", dn,
580 do_list( ld, fp, dn );
582 ldap_free_friendlymap( &fm );
586 do_list( LDAP *ld, FILE *fp, char *dn )
589 LDAPMessage *e, *res;
590 struct timeval timeout;
591 FriendlyMap *fm = NULL;
592 static char *attrs[] = { "objectClass", 0 };
593 int deref = LDAP_DEREF_FINDING;
595 timeout.tv_sec = GO500GW_TIMEOUT;
598 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
600 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
601 "(!(objectClass=dSA))", attrs, 0, &timeout, &res )) != LDAP_SUCCESS
602 && rc != LDAP_SIZELIMIT_EXCEEDED ) {
603 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
604 rc, myhost, myport );
608 deref = LDAP_DEREF_ALWAYS;
609 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
611 if ( ldap_count_entries( ld, res ) < 1 ) {
615 #ifdef GO500GW_SORT_ATTR
616 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
620 for ( e = ldap_first_entry( ld, res ); e != NULL;
621 e = ldap_next_entry( ld, e ) ) {
625 dn = ldap_get_dn( ld, e );
626 s = ldap_explode_dn( dn, 1 );
627 oc = ldap_get_values( ld, e, "objectClass" );
630 if ( strcasecmp( doc, "country" ) == 0 ) {
631 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
639 if ( strncasecmp( rdn, "{ASN}", 5 ) != 0 ) {
640 if ( isnonleaf( ld, oc, dn ) ) {
641 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", rdn,
642 doc, dn, myhost, myport );
644 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", rdn,
645 doc, dn, myhost, myport );
650 ldap_value_free( s );
651 ldap_value_free( oc );
653 ldap_free_friendlymap( &fm );
655 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
656 fprintf( fp, "0A size limit was exceeded (explanation)\tLL\t%s\t%d\r\n",
662 isoc( char **ocl, char *oc )
666 for ( i = 0; ocl[i] != NULL; i++ ) {
667 if ( strcasecmp( ocl[i], oc ) == 0 )
675 make_scope( LDAP *ld, char *dn )
680 struct timeval timeout;
681 static char *attrs[] = { "objectClass", 0 };
683 if ( strcmp( dn, "" ) == 0 )
684 return( LDAP_SCOPE_ONELEVEL );
686 timeout.tv_sec = GO500GW_TIMEOUT;
688 if ( ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
689 attrs, 0, &timeout, &res ) != LDAP_SUCCESS ) {
693 oc = ldap_get_values( ld, ldap_first_entry( ld, res ), "objectClass" );
695 if ( isoc( oc, "organization" ) || isoc( oc, "organizationalUnit" ) )
696 scope = LDAP_SCOPE_SUBTREE;
698 scope = LDAP_SCOPE_ONELEVEL;
700 ldap_value_free( oc );
707 do_search( LDAP *ld, FILE *fp, char *query )
714 struct timeval timeout;
716 LDAPMessage *e, *res;
718 static char *attrs[] = { "objectClass", 0 };
720 if ( (filter = strchr( query, '\t' )) == NULL ) {
721 fprintf( fp, "3Missing filter!\r\n" );
728 if ( strchr( filter, ',' ) != NULL ) {
729 ldap_ufn_setprefix( ld, base );
730 timeout.tv_sec = GO500GW_TIMEOUT;
732 ldap_ufn_timeout( (void *) &timeout );
734 deref = LDAP_DEREF_FINDING;
735 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
737 if ( (rc = ldap_ufn_search_s( ld, filter, attrs, 0, &res ))
738 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
740 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
741 rc, myhost, myport );
745 count = ldap_count_entries( ld, res );
748 if ( (scope = make_scope( ld, base )) == -1 ) {
749 fprintf( fp, "3Bad scope\r\n" );
753 filtertype = (scope == LDAP_SCOPE_ONELEVEL ?
754 "go500gw onelevel" : "go500gw subtree");
755 deref = (scope == LDAP_SCOPE_ONELEVEL ?
756 LDAP_DEREF_FINDING : LDAP_DEREF_ALWAYS);
757 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
758 timeout.tv_sec = GO500GW_TIMEOUT;
761 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
762 fprintf( stderr, "Cannot open filter file (%s)\n",
769 for ( fi = ldap_getfirstfilter( filtd, filtertype, filter );
770 fi != NULL; fi = ldap_getnextfilter( filtd ) )
772 if ( (rc = ldap_search_st( ld, base, scope,
773 fi->lfi_filter, attrs, 0, &timeout, &res ))
774 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
775 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
776 rc, myhost, myport );
779 if ( (count = ldap_count_entries( ld, res )) != 0 )
782 deref = LDAP_DEREF_ALWAYS;
783 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
784 ldap_getfilter_free( filtd );
797 e = ldap_first_entry( ld, res );
798 oc = ldap_get_values( ld, e, "objectClass" );
799 dn = ldap_get_dn( ld, e );
801 if ( isnonleaf( ld, oc, dn ) ) {
802 do_menu( ld, fp, dn );
809 ldap_value_free( oc );
812 #ifdef GO500GW_SORT_ATTR
813 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
816 for ( e = ldap_first_entry( ld, res ); e != NULL;
817 e = ldap_next_entry( ld, e ) ) {
821 dn = ldap_get_dn( ld, e );
822 s = ldap_explode_dn( dn, 1 );
823 oc = ldap_get_values( ld, e, "objectClass" );
825 if ( isnonleaf( ld, oc, dn ) )
826 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", s[0],
827 pick_oc( oc ), dn, myhost, myport );
829 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", s[0],
830 pick_oc( oc ), dn, myhost, myport );
833 ldap_value_free( s );
834 ldap_value_free( oc );
837 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
838 fprintf( fp, "0A size limit was exceeded (explanation)\tLS\t%s\t%d\r\n",
845 entry2textwrite( void *fp, char *buf, int len )
847 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
851 do_read( LDAP *ld, FILE *fp, char *dn )
853 static struct ldap_disptmpl *tmpllist;
855 ldap_init_templates( templatefile, &tmpllist );
857 if ( ldap_entry2text_search( ld, dn, NULL, NULL, tmpllist, NULL, NULL,
858 entry2textwrite,(void *) fp, "\r\n", rdncount, 0 )
861 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
864 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
865 ld_errno, myhost, myport );
868 if ( tmpllist != NULL ) {
869 ldap_free_templates( tmpllist );
879 if ( (fp = fopen( helpfile, "r" )) == NULL ) {
880 fprintf( op, "Cannot access helpfile (%s)\r\n", helpfile );
884 while ( fgets( line, sizeof(line), fp ) != NULL ) {
885 line[ strlen( line ) - 1 ] = '\0';
887 fprintf( op, "%s\r\n", line );
894 do_sizelimit( FILE *fp, char type )
897 fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
898 fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
899 fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
900 fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
902 fprintf( fp, "Not all entries could be returned because a size limit was exceeded.\r\n" );
903 fprintf( fp, "There is no way to defeat this feature, but if you know who you are\r\n" );
904 fprintf( fp, "looking for, try choosing the \"Search\" option listed above and\r\n" );
905 fprintf( fp, "specifying the name of the person you want.\r\n" );
907 fprintf( fp, ".\r\n" );
911 do_error( FILE *fp, char *s )
917 fprintf( fp, "An error occurred searching X.500. The error code was %d\r\n", code );
918 fprintf( fp, "The corresponding error is: %s\r\n", ldap_err2string( code ) );
919 fprintf( fp, "No additional information is available\r\n" );
920 fprintf( fp, ".\r\n" );