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.
18 #include <ac/signal.h>
19 #include <ac/socket.h>
20 #include <ac/string.h>
21 #include <ac/syslog.h>
23 #include <ac/unistd.h>
26 #include <sys/resource.h>
28 #ifdef HAVE_SYS_PARAM_H
29 #include <sys/param.h>
35 #if LDAP_VERSION < LDAP_VERSION3
36 /* quick fix until we have ldap_set_options */
37 #include "../libraries/libldap/ldap-int.h"
42 #include "ldapconfig.h"
49 char *ldaphost = LDAPHOST;
50 int ldapport = LDAP_PORT;
51 int searchaliases = 1;
52 char *helpfile = GO500GW_HELPFILE;
53 char *filterfile = FILTERFILE;
54 char *templatefile = TEMPLATEFILE;
55 char *friendlyfile = FRIENDLYFILE;
56 int rdncount = GO500GW_RDNCOUNT;
59 static RETSIGTYPE wait4child();
66 static do_sizelimit();
68 extern int strcasecmp();
70 char myhost[MAXHOSTNAMELEN];
71 int myport = GO500GW_PORT;
76 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 );
90 struct sockaddr_in from;
92 RETSIGTYPE wait4child();
97 /* for setproctitle */
101 while ( (i = getopt( argc, argv, "P:ad:f:h:lp:t:x:Ic:" )) != EOF ) {
103 case 'a': /* search aliases */
107 case 'd': /* debugging level */
108 debug = atoi( optarg );
112 fprintf( stderr, "warning: ldap debugging requires LDAP_DEBUG\n" );
116 case 'f': /* ldap filter file */
117 filterfile = strdup( optarg );
120 case 'h': /* gopher help file */
121 helpfile = strdup( optarg );
124 case 'l': /* log to LOG_LOCAL3 */
128 case 'p': /* port to listen on */
129 port = atoi( optarg );
132 case 'P': /* port to connect to ldap server */
133 ldapport = atoi( optarg );
136 case 't': /* ldap template file */
137 templatefile = strdup( optarg );
140 case 'x': /* ldap server hostname */
141 ldaphost = strdup( optarg );
144 case 'I': /* run from inetd */
148 case 'c': /* count of DN components to show */
149 rdncount = atoi( optarg );
158 dtblsize = sysconf( _SC_OPEN_MAX );
159 #elif HAVE_GETDTABLESIZE
160 dtblsize = getdtablesize();
162 dtblsize = FD_SETSIZE;
166 if ( dtblsize > FD_SETSIZE ) {
167 dtblsize = FD_SETSIZE;
169 #endif /* FD_SETSIZE*/
173 #ifdef GO500GW_HOSTNAME
174 strcpy( myhost, GO500GW_HOSTNAME );
176 if ( myhost[0] == '\0' && gethostname( myhost, sizeof(myhost) )
178 perror( "gethostname" );
183 /* detach if stderr is redirected or no debugging */
185 (void) detach( debug );
187 if ( (myname = strrchr( argv[0], '/' )) == NULL )
188 myname = strdup( argv[0] );
190 myname = strdup( myname + 1 );
194 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL3 );
196 openlog( myname, OPENLOG_OPTIONS );
200 syslog( LOG_INFO, "initializing" );
202 /* set up the socket to listen on */
204 s = set_socket( port );
206 /* arrange to reap children */
207 (void) SIGNAL( SIGCHLD, wait4child );
211 fromlen = sizeof(from);
212 if ( getpeername( 0, (struct sockaddr *) &from, &fromlen )
214 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
215 sizeof(from.sin_addr.s_addr), AF_INET );
216 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
217 (hp == NULL) ? "unknown" : hp->h_name,
218 inet_ntoa( from.sin_addr ), 0 );
221 syslog( LOG_INFO, "connection from %s (%s)",
222 (hp == NULL) ? "unknown" : hp->h_name,
223 inet_ntoa( from.sin_addr ) );
226 setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
239 FD_SET( s, &readfds );
241 if ( (rc = select( dtblsize, &readfds, 0, 0 ,0 )) == -1 ) {
242 if ( debug ) perror( "select" );
244 } else if ( rc == 0 ) {
248 if ( ! FD_ISSET( s, &readfds ) )
251 fromlen = sizeof(from);
252 if ( (ns = accept( s, (struct sockaddr *) &from, &fromlen ))
254 if ( debug ) perror( "accept" );
258 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
259 sizeof(from.sin_addr.s_addr), AF_INET );
262 syslog( LOG_INFO, "TCP connection from %s (%s)",
263 (hp == NULL) ? "unknown" : hp->h_name,
264 inet_ntoa( from.sin_addr ) );
267 switch( pid = fork() ) {
273 case -1: /* failed */
277 default: /* parent */
280 fprintf( stderr, "forked child %d\n", pid );
287 static set_socket( port )
291 struct sockaddr_in addr;
297 if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
302 /* set option so clients can't keep us from coming back up */
304 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
305 sizeof(one) ) < 0 ) {
306 perror( "setsockopt" );
311 addr.sin_family = AF_INET;
312 addr.sin_addr.s_addr = INADDR_ANY;
313 addr.sin_port = htons( port );
314 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
319 /* listen for connections */
320 if ( listen( s, 5 ) == -1 ) {
326 printf( "go500gw listening on port %d\n", port );
335 WAITSTATUSTYPE status;
338 if ( debug ) printf( "parent: catching child status\n" );
341 while (waitpid ((pid_t) -1, NULL, WAIT_FLAGS) > 0)
344 while (wait3( &status, WAIT_FLAGS, 0 ) > 0 )
348 (void) SIGNAL( SIGCHLD, wait4child );
351 static do_queries( s )
354 char buf[1024], *query;
358 struct timeval timeout;
362 if ( (fp = fdopen( s, "a+")) == NULL ) {
367 timeout.tv_sec = GO500GW_TIMEOUT;
370 FD_SET( fileno( fp ), &readfds );
372 if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
375 if ( fgets( buf, sizeof(buf), fp ) == NULL )
380 fprintf( stderr, "got %d bytes\n", len );
382 lber_bprint( buf, len );
387 if ( buf[len - 1] == '\n' )
390 if ( buf[len - 1] == '\r' )
396 /* strip off leading white space */
397 while ( isspace( *query )) {
404 if ( *query == 'H' || *query == 'L' || *query == 'E' ) {
405 switch ( *query++ ) {
406 case 'H': /* help file */
410 case 'L': /* size limit explanation */
411 do_sizelimit( fp, *query );
414 case 'E': /* error explanation */
415 do_error( fp, query );
419 fprintf( fp, ".\r\n" );
426 if ( (ld = ldap_open( ldaphost, ldapport )) == NULL ) {
427 if ( debug ) perror( "ldap_open" );
428 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
429 LDAP_SERVER_DOWN, myhost, myport );
430 fprintf( fp, ".\r\n" );
435 ld->ld_deref = LDAP_DEREF_ALWAYS;
436 if ( !searchaliases )
437 ld->ld_deref = LDAP_DEREF_FINDING;
439 if ( (rc = ldap_simple_bind_s( ld, GO500GW_BINDDN, NULL ))
441 if ( debug ) ldap_perror( ld, "ldap_simple_bind_s" );
442 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
443 rc, myhost, myport );
444 fprintf( fp, ".\r\n" );
449 switch ( *query++ ) {
450 case 'R': /* read an entry */
451 do_read( ld, fp, query );
454 case 'S': /* search */
455 do_search( ld, fp, query, 1 );
458 case 'M': /* X.500 menu */
459 do_menu( ld, fp, query );
463 do_menu( ld, fp, "" );
467 fprintf( fp, ".\r\n" );
474 static char *pick_oc( oclist )
479 if ( oclist == NULL )
482 for ( i = 0; oclist[i] != NULL; i++ ) {
483 if ( strcasecmp( oclist[i], "top" ) != 0 &&
484 strcasecmp( oclist[i], "quipuObject" ) != 0 &&
485 strcasecmp( oclist[i], "quipuNonLeafObject" ) != 0 )
492 static isnonleaf( ld, oclist, dn )
497 int i, quipuobject = 0;
499 if ( oclist == NULL )
502 for ( i = 0; oclist[i] != NULL; i++ ) {
503 if ( strcasecmp( oclist[i], "quipuObject" ) == 0 )
505 if ( strcasecmp( oclist[i], "quipuNonLeafObject" ) == 0 ||
506 strcasecmp( oclist[i], "externalNonLeafObject" ) == 0 )
511 * not a quipu thang - no easy way to tell leaves from nonleaves
512 * except by trying to search or list. ldap only lets us search.
515 /* expensive - just guess for now */
516 return( quipuobject ? 0 : 1 );
519 if ( !quipuobject ) {
521 struct timeval timeout;
522 LDAPMessage *res = NULL;
523 static char *attrs[] = { "objectClass", 0 };
525 timeout.tv_sec = GO500GW_TIMEOUT;
527 ld->ld_sizelimit = 1;
528 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
529 "(objectClass=*)", attrs, 0, &timeout, &res ))
530 == LDAP_SUCCESS || rc == LDAP_SIZELIMIT_EXCEEDED ) {
531 ld->ld_sizelimit = LDAP_NO_LIMIT;
533 numentries = ldap_count_entries( ld, res );
536 return( numentries == 1 ? 1 : 0 );
544 static do_menu( ld, fp, dn )
551 FriendlyMap *fm = NULL;
553 if ( strcmp( dn, "" ) != 0 ) {
554 s = ldap_explode_dn( dn, 1 );
557 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
560 fprintf( fp, "0Read %s entry\tR%s\t%s\t%d\r\n", rdn ? rdn: s[0],
561 dn, myhost, myport );
563 ldap_value_free( s );
565 fprintf( fp, "0About the Gopher to X.500 Gateway\tH\t%s\t%d\r\n",
569 fprintf( fp, "7Search %s\tS%s\t%s\t%d\r\n", rdn ? rdn : "root", dn,
572 do_list( ld, fp, dn );
574 ldap_free_friendlymap( &fm );
577 static do_list( ld, fp, dn )
583 LDAPMessage *e, *res;
584 struct timeval timeout;
585 FriendlyMap *fm = NULL;
586 static char *attrs[] = { "objectClass", 0 };
588 timeout.tv_sec = GO500GW_TIMEOUT;
590 ld->ld_deref = LDAP_DEREF_FINDING;
591 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
592 "(!(objectClass=dSA))", attrs, 0, &timeout, &res )) != LDAP_SUCCESS
593 && rc != LDAP_SIZELIMIT_EXCEEDED ) {
594 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
595 rc, myhost, myport );
598 ld->ld_deref = LDAP_DEREF_ALWAYS;
600 if ( ldap_count_entries( ld, res ) < 1 ) {
604 #ifdef GO500GW_SORT_ATTR
605 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
609 for ( e = ldap_first_entry( ld, res ); e != NULL;
610 e = ldap_next_entry( ld, e ) ) {
614 dn = ldap_get_dn( ld, e );
615 s = ldap_explode_dn( dn, 1 );
616 oc = ldap_get_values( ld, e, "objectClass" );
619 if ( strcasecmp( doc, "country" ) == 0 ) {
620 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
628 if ( strncasecmp( rdn, "{ASN}", 5 ) != 0 ) {
629 if ( isnonleaf( ld, oc, dn ) ) {
630 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", rdn,
631 doc, dn, myhost, myport );
633 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", rdn,
634 doc, dn, myhost, myport );
639 ldap_value_free( s );
640 ldap_value_free( oc );
642 ldap_free_friendlymap( &fm );
644 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
645 fprintf( fp, "0A size limit was exceeded (explanation)\tLL\t%s\t%d\r\n",
650 static isoc( ocl, oc )
656 for ( i = 0; ocl[i] != NULL; i++ ) {
657 if ( strcasecmp( ocl[i], oc ) == 0 )
664 static int make_scope( ld, dn )
671 struct timeval timeout;
672 static char *attrs[] = { "objectClass", 0 };
674 if ( strcmp( dn, "" ) == 0 )
675 return( LDAP_SCOPE_ONELEVEL );
677 timeout.tv_sec = GO500GW_TIMEOUT;
679 if ( ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
680 attrs, 0, &timeout, &res ) != LDAP_SUCCESS ) {
684 oc = ldap_get_values( ld, ldap_first_entry( ld, res ), "objectClass" );
686 if ( isoc( oc, "organization" ) || isoc( oc, "organizationalUnit" ) )
687 scope = LDAP_SCOPE_SUBTREE;
689 scope = LDAP_SCOPE_ONELEVEL;
691 ldap_value_free( oc );
697 static do_search( ld, fp, query )
706 struct timeval timeout;
708 LDAPMessage *e, *res;
710 static char *attrs[] = { "objectClass", 0 };
712 if ( (filter = strchr( query, '\t' )) == NULL ) {
713 fprintf( fp, "3Missing filter!\r\n" );
720 if ( strchr( filter, ',' ) != NULL ) {
721 ldap_ufn_setprefix( ld, base );
722 timeout.tv_sec = GO500GW_TIMEOUT;
724 ldap_ufn_timeout( (void *) &timeout );
725 ld->ld_deref = LDAP_DEREF_FINDING;
727 if ( (rc = ldap_ufn_search_s( ld, filter, attrs, 0, &res ))
728 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
730 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
731 rc, myhost, myport );
735 count = ldap_count_entries( ld, res );
738 if ( (scope = make_scope( ld, base )) == -1 ) {
739 fprintf( fp, "3Bad scope\r\n" );
743 filtertype = (scope == LDAP_SCOPE_ONELEVEL ?
744 "go500gw onelevel" : "go500gw subtree");
745 ld->ld_deref = (scope == LDAP_SCOPE_ONELEVEL ?
746 LDAP_DEREF_FINDING : LDAP_DEREF_ALWAYS);
747 timeout.tv_sec = GO500GW_TIMEOUT;
750 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
751 fprintf( stderr, "Cannot open filter file (%s)\n",
758 for ( fi = ldap_getfirstfilter( filtd, filtertype, filter );
759 fi != NULL; fi = ldap_getnextfilter( filtd ) )
761 if ( (rc = ldap_search_st( ld, base, scope,
762 fi->lfi_filter, attrs, 0, &timeout, &res ))
763 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
764 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
765 rc, myhost, myport );
768 if ( (count = ldap_count_entries( ld, res )) != 0 )
771 ld->ld_deref = LDAP_DEREF_ALWAYS;
772 ldap_getfilter_free( filtd );
785 e = ldap_first_entry( ld, res );
786 oc = ldap_get_values( ld, e, "objectClass" );
787 dn = ldap_get_dn( ld, e );
789 if ( isnonleaf( ld, oc, dn ) ) {
790 rc = do_menu( ld, fp, dn );
797 ldap_value_free( oc );
800 #ifdef GO500GW_SORT_ATTR
801 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
804 for ( e = ldap_first_entry( ld, res ); e != NULL;
805 e = ldap_next_entry( ld, e ) ) {
809 dn = ldap_get_dn( ld, e );
810 s = ldap_explode_dn( dn, 1 );
811 oc = ldap_get_values( ld, e, "objectClass" );
813 if ( isnonleaf( ld, oc, dn ) )
814 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", s[0],
815 pick_oc( oc ), dn, myhost, myport );
817 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", s[0],
818 pick_oc( oc ), dn, myhost, myport );
821 ldap_value_free( s );
822 ldap_value_free( oc );
825 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
826 fprintf( fp, "0A size limit was exceeded (explanation)\tLS\t%s\t%d\r\n",
833 entry2textwrite( void *fp, char *buf, int len )
835 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
838 static do_read( ld, fp, dn )
843 static struct ldap_disptmpl *tmpllist;
845 ldap_init_templates( templatefile, &tmpllist );
847 if ( ldap_entry2text_search( ld, dn, NULL, NULL, tmpllist, NULL, NULL,
848 entry2textwrite,(void *) fp, "\r\n", rdncount, 0 )
851 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
852 ld->ld_errno, myhost, myport );
855 if ( tmpllist != NULL ) {
856 ldap_free_templates( tmpllist );
866 if ( (fp = fopen( helpfile, "r" )) == NULL ) {
867 fprintf( op, "Cannot access helpfile (%s)\r\n", helpfile );
871 while ( fgets( line, sizeof(line), fp ) != NULL ) {
872 line[ strlen( line ) - 1 ] = '\0';
874 fprintf( op, "%s\r\n", line );
880 static do_sizelimit( fp, type )
885 fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
886 fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
887 fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
888 fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
890 fprintf( fp, "Not all entries could be returned because a size limit was exceeded.\r\n" );
891 fprintf( fp, "There is no way to defeat this feature, but if you know who you are\r\n" );
892 fprintf( fp, "looking for, try choosing the \"Search\" option listed above and\r\n" );
893 fprintf( fp, "specifying the name of the person you want.\r\n" );
895 fprintf( fp, ".\r\n" );
898 static do_error( fp, s )
906 fprintf( fp, "An error occurred searching X.500. The error code was %d\r\n", code );
907 fprintf( fp, "The corresponding error is: %s\r\n", ldap_err2string( code ) );
908 fprintf( fp, "No additional information is available\r\n" );
909 fprintf( fp, ".\r\n" );