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.
20 #include <ac/socket.h>
21 #include <ac/string.h>
22 #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>
36 #include "ldapconfig.h"
43 char *ldaphost = LDAPHOST;
44 int ldapport = LDAP_PORT;
45 int searchaliases = 1;
46 char *helpfile = GO500GW_HELPFILE;
47 char *filterfile = FILTERFILE;
48 char *templatefile = TEMPLATEFILE;
49 char *friendlyfile = FRIENDLYFILE;
50 int rdncount = GO500GW_RDNCOUNT;
53 static RETSIGTYPE wait4child();
60 static do_sizelimit();
62 extern int strcasecmp();
64 char myhost[MAXHOSTNAMELEN];
65 int myport = GO500GW_PORT;
70 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 );
84 struct sockaddr_in from;
86 RETSIGTYPE wait4child();
91 /* for setproctitle */
95 while ( (i = getopt( argc, argv, "P:ad:f:h:lp:t:x:Ic:" )) != EOF ) {
97 case 'a': /* search aliases */
101 case 'd': /* debugging level */
102 debug = atoi( optarg );
106 fprintf( stderr, "warning: ldap debugging requires LDAP_DEBUG\n" );
110 case 'f': /* ldap filter file */
111 filterfile = strdup( optarg );
114 case 'h': /* gopher help file */
115 helpfile = strdup( optarg );
118 case 'l': /* log to LOG_LOCAL3 */
122 case 'p': /* port to listen on */
123 port = atoi( optarg );
126 case 'P': /* port to connect to ldap server */
127 ldapport = atoi( optarg );
130 case 't': /* ldap template file */
131 templatefile = strdup( optarg );
134 case 'x': /* ldap server hostname */
135 ldaphost = strdup( optarg );
138 case 'I': /* run from inetd */
142 case 'c': /* count of DN components to show */
143 rdncount = atoi( optarg );
152 dtblsize = sysconf( _SC_OPEN_MAX );
153 #elif HAVE_GETDTABLESIZE
154 dtblsize = getdtablesize();
156 dtblsize = FD_SETSIZE;
160 if ( dtblsize > FD_SETSIZE ) {
161 dtblsize = FD_SETSIZE;
163 #endif /* FD_SETSIZE*/
167 #ifdef GO500GW_HOSTNAME
168 strcpy( myhost, GO500GW_HOSTNAME );
170 if ( myhost[0] == '\0' && gethostname( myhost, sizeof(myhost) )
172 perror( "gethostname" );
177 /* detach if stderr is redirected or no debugging */
179 (void) detach( debug );
181 if ( (myname = strrchr( argv[0], '/' )) == NULL )
182 myname = strdup( argv[0] );
184 myname = strdup( myname + 1 );
188 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL3 );
190 openlog( myname, OPENLOG_OPTIONS );
194 syslog( LOG_INFO, "initializing" );
196 /* set up the socket to listen on */
198 s = set_socket( port );
200 /* arrange to reap children */
201 (void) signal( SIGCHLD, (void *) wait4child );
205 fromlen = sizeof(from);
206 if ( getpeername( 0, (struct sockaddr *) &from, &fromlen )
208 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
209 sizeof(from.sin_addr.s_addr), AF_INET );
210 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
211 (hp == NULL) ? "unknown" : hp->h_name,
212 inet_ntoa( from.sin_addr ), 0 );
215 syslog( LOG_INFO, "connection from %s (%s)",
216 (hp == NULL) ? "unknown" : hp->h_name,
217 inet_ntoa( from.sin_addr ) );
220 setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
233 FD_SET( s, &readfds );
235 if ( (rc = select( dtblsize, &readfds, 0, 0 ,0 )) == -1 ) {
236 if ( debug ) perror( "select" );
238 } else if ( rc == 0 ) {
242 if ( ! FD_ISSET( s, &readfds ) )
245 fromlen = sizeof(from);
246 if ( (ns = accept( s, (struct sockaddr *) &from, &fromlen ))
248 if ( debug ) perror( "accept" );
252 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
253 sizeof(from.sin_addr.s_addr), AF_INET );
256 syslog( LOG_INFO, "TCP connection from %s (%s)",
257 (hp == NULL) ? "unknown" : hp->h_name,
258 inet_ntoa( from.sin_addr ) );
261 switch( pid = fork() ) {
267 case -1: /* failed */
271 default: /* parent */
274 fprintf( stderr, "forked child %d\n", pid );
281 static set_socket( port )
285 struct sockaddr_in addr;
291 if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
296 /* set option so clients can't keep us from coming back up */
298 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
299 sizeof(one) ) < 0 ) {
300 perror( "setsockopt" );
305 addr.sin_family = AF_INET;
306 addr.sin_addr.s_addr = INADDR_ANY;
307 addr.sin_port = htons( port );
308 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
313 /* listen for connections */
314 if ( listen( s, 5 ) == -1 ) {
320 printf( "go500gw listening on port %d\n", port );
329 WAITSTATUSTYPE status;
332 if ( debug ) printf( "parent: catching child status\n" );
335 while (waitpid ((pid_t) -1, NULL, WAIT_FLAGS) > 0)
338 while (wait3( &status, WAIT_FLAGS, 0 ) > 0 )
342 (void) signal( SIGCHLD, (void *) wait4child );
345 static do_queries( s )
348 char buf[1024], *query;
352 struct timeval timeout;
356 if ( (fp = fdopen( s, "a+")) == NULL ) {
361 timeout.tv_sec = GO500GW_TIMEOUT;
364 FD_SET( fileno( fp ), &readfds );
366 if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
369 if ( fgets( buf, sizeof(buf), fp ) == NULL )
374 fprintf( stderr, "got %d bytes\n", len );
376 lber_bprint( buf, len );
381 if ( buf[len - 1] == '\n' )
384 if ( buf[len - 1] == '\r' )
390 /* strip off leading white space */
391 while ( isspace( *query )) {
398 if ( *query == 'H' || *query == 'L' || *query == 'E' ) {
399 switch ( *query++ ) {
400 case 'H': /* help file */
404 case 'L': /* size limit explanation */
405 do_sizelimit( fp, *query );
408 case 'E': /* error explanation */
409 do_error( fp, query );
413 fprintf( fp, ".\r\n" );
420 if ( (ld = ldap_open( ldaphost, ldapport )) == NULL ) {
421 if ( debug ) perror( "ldap_open" );
422 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
423 LDAP_SERVER_DOWN, myhost, myport );
424 fprintf( fp, ".\r\n" );
429 ld->ld_deref = LDAP_DEREF_ALWAYS;
430 if ( !searchaliases )
431 ld->ld_deref = LDAP_DEREF_FINDING;
433 if ( (rc = ldap_simple_bind_s( ld, GO500GW_BINDDN, NULL ))
435 if ( debug ) ldap_perror( ld, "ldap_simple_bind_s" );
436 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
437 rc, myhost, myport );
438 fprintf( fp, ".\r\n" );
443 switch ( *query++ ) {
444 case 'R': /* read an entry */
445 do_read( ld, fp, query );
448 case 'S': /* search */
449 do_search( ld, fp, query, 1 );
452 case 'M': /* X.500 menu */
453 do_menu( ld, fp, query );
457 do_menu( ld, fp, "" );
461 fprintf( fp, ".\r\n" );
468 static char *pick_oc( oclist )
473 if ( oclist == NULL )
476 for ( i = 0; oclist[i] != NULL; i++ ) {
477 if ( strcasecmp( oclist[i], "top" ) != 0 &&
478 strcasecmp( oclist[i], "quipuObject" ) != 0 &&
479 strcasecmp( oclist[i], "quipuNonLeafObject" ) != 0 )
486 static isnonleaf( ld, oclist, dn )
491 int i, quipuobject = 0;
493 if ( oclist == NULL )
496 for ( i = 0; oclist[i] != NULL; i++ ) {
497 if ( strcasecmp( oclist[i], "quipuObject" ) == 0 )
499 if ( strcasecmp( oclist[i], "quipuNonLeafObject" ) == 0 ||
500 strcasecmp( oclist[i], "externalNonLeafObject" ) == 0 )
505 * not a quipu thang - no easy way to tell leaves from nonleaves
506 * except by trying to search or list. ldap only lets us search.
509 /* expensive - just guess for now */
510 return( quipuobject ? 0 : 1 );
513 if ( !quipuobject ) {
515 struct timeval timeout;
516 LDAPMessage *res = NULL;
517 static char *attrs[] = { "objectClass", 0 };
519 timeout.tv_sec = GO500GW_TIMEOUT;
521 ld->ld_sizelimit = 1;
522 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
523 "(objectClass=*)", attrs, 0, &timeout, &res ))
524 == LDAP_SUCCESS || rc == LDAP_SIZELIMIT_EXCEEDED ) {
525 ld->ld_sizelimit = LDAP_NO_LIMIT;
527 numentries = ldap_count_entries( ld, res );
530 return( numentries == 1 ? 1 : 0 );
538 static do_menu( ld, fp, dn )
545 FriendlyMap *fm = NULL;
547 if ( strcmp( dn, "" ) != 0 ) {
548 s = ldap_explode_dn( dn, 1 );
551 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
554 fprintf( fp, "0Read %s entry\tR%s\t%s\t%d\r\n", rdn ? rdn: s[0],
555 dn, myhost, myport );
557 ldap_value_free( s );
559 fprintf( fp, "0About the Gopher to X.500 Gateway\tH\t%s\t%d\r\n",
563 fprintf( fp, "7Search %s\tS%s\t%s\t%d\r\n", rdn ? rdn : "root", dn,
566 do_list( ld, fp, dn );
568 ldap_free_friendlymap( &fm );
571 static do_list( ld, fp, dn )
577 LDAPMessage *e, *res;
578 struct timeval timeout;
579 FriendlyMap *fm = NULL;
580 static char *attrs[] = { "objectClass", 0 };
582 timeout.tv_sec = GO500GW_TIMEOUT;
584 ld->ld_deref = LDAP_DEREF_FINDING;
585 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
586 "(!(objectClass=dSA))", attrs, 0, &timeout, &res )) != LDAP_SUCCESS
587 && rc != LDAP_SIZELIMIT_EXCEEDED ) {
588 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
589 rc, myhost, myport );
592 ld->ld_deref = LDAP_DEREF_ALWAYS;
594 if ( ldap_count_entries( ld, res ) < 1 ) {
598 #ifdef GO500GW_SORT_ATTR
599 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
603 for ( e = ldap_first_entry( ld, res ); e != NULL;
604 e = ldap_next_entry( ld, e ) ) {
608 dn = ldap_get_dn( ld, e );
609 s = ldap_explode_dn( dn, 1 );
610 oc = ldap_get_values( ld, e, "objectClass" );
613 if ( strcasecmp( doc, "country" ) == 0 ) {
614 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
622 if ( strncasecmp( rdn, "{ASN}", 5 ) != 0 ) {
623 if ( isnonleaf( ld, oc, dn ) ) {
624 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", rdn,
625 doc, dn, myhost, myport );
627 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", rdn,
628 doc, dn, myhost, myport );
633 ldap_value_free( s );
634 ldap_value_free( oc );
636 ldap_free_friendlymap( &fm );
638 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
639 fprintf( fp, "0A size limit was exceeded (explanation)\tLL\t%s\t%d\r\n",
644 static isoc( ocl, oc )
650 for ( i = 0; ocl[i] != NULL; i++ ) {
651 if ( strcasecmp( ocl[i], oc ) == 0 )
658 static int make_scope( ld, dn )
665 struct timeval timeout;
666 static char *attrs[] = { "objectClass", 0 };
668 if ( strcmp( dn, "" ) == 0 )
669 return( LDAP_SCOPE_ONELEVEL );
671 timeout.tv_sec = GO500GW_TIMEOUT;
673 if ( ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
674 attrs, 0, &timeout, &res ) != LDAP_SUCCESS ) {
678 oc = ldap_get_values( ld, ldap_first_entry( ld, res ), "objectClass" );
680 if ( isoc( oc, "organization" ) || isoc( oc, "organizationalUnit" ) )
681 scope = LDAP_SCOPE_SUBTREE;
683 scope = LDAP_SCOPE_ONELEVEL;
685 ldap_value_free( oc );
691 static do_search( ld, fp, query )
700 struct timeval timeout;
702 LDAPMessage *e, *res;
704 static char *attrs[] = { "objectClass", 0 };
706 if ( (filter = strchr( query, '\t' )) == NULL ) {
707 fprintf( fp, "3Missing filter!\r\n" );
714 if ( strchr( filter, ',' ) != NULL ) {
715 ldap_ufn_setprefix( ld, base );
716 timeout.tv_sec = GO500GW_TIMEOUT;
718 ldap_ufn_timeout( (void *) &timeout );
719 ld->ld_deref = LDAP_DEREF_FINDING;
721 if ( (rc = ldap_ufn_search_s( ld, filter, attrs, 0, &res ))
722 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
724 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
725 rc, myhost, myport );
729 count = ldap_count_entries( ld, res );
732 if ( (scope = make_scope( ld, base )) == -1 ) {
733 fprintf( fp, "3Bad scope\r\n" );
737 filtertype = (scope == LDAP_SCOPE_ONELEVEL ?
738 "go500gw onelevel" : "go500gw subtree");
739 ld->ld_deref = (scope == LDAP_SCOPE_ONELEVEL ?
740 LDAP_DEREF_FINDING : LDAP_DEREF_ALWAYS);
741 timeout.tv_sec = GO500GW_TIMEOUT;
744 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
745 fprintf( stderr, "Cannot open filter file (%s)\n",
752 for ( fi = ldap_getfirstfilter( filtd, filtertype, filter );
753 fi != NULL; fi = ldap_getnextfilter( filtd ) )
755 if ( (rc = ldap_search_st( ld, base, scope,
756 fi->lfi_filter, attrs, 0, &timeout, &res ))
757 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
758 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
759 rc, myhost, myport );
762 if ( (count = ldap_count_entries( ld, res )) != 0 )
765 ld->ld_deref = LDAP_DEREF_ALWAYS;
766 ldap_getfilter_free( filtd );
779 e = ldap_first_entry( ld, res );
780 oc = ldap_get_values( ld, e, "objectClass" );
781 if ( isnonleaf( ld, oc, dn ) ) {
782 dn = ldap_get_dn( ld, e );
784 rc = do_menu( ld, fp, dn );
790 ldap_value_free( oc );
793 #ifdef GO500GW_SORT_ATTR
794 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
797 for ( e = ldap_first_entry( ld, res ); e != NULL;
798 e = ldap_next_entry( ld, e ) ) {
802 dn = ldap_get_dn( ld, e );
803 s = ldap_explode_dn( dn, 1 );
804 oc = ldap_get_values( ld, e, "objectClass" );
806 if ( isnonleaf( ld, oc, dn ) )
807 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", s[0],
808 pick_oc( oc ), dn, myhost, myport );
810 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", s[0],
811 pick_oc( oc ), dn, myhost, myport );
814 ldap_value_free( s );
815 ldap_value_free( oc );
818 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
819 fprintf( fp, "0A size limit was exceeded (explanation)\tLS\t%s\t%d\r\n",
826 entry2textwrite( void *fp, char *buf, int len )
828 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
831 static do_read( ld, fp, dn )
836 static struct ldap_disptmpl *tmpllist;
838 ldap_init_templates( templatefile, &tmpllist );
840 if ( ldap_entry2text_search( ld, dn, NULL, NULL, tmpllist, NULL, NULL,
841 entry2textwrite,(void *) fp, "\r\n", rdncount, 0 )
844 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
845 ld->ld_errno, myhost, myport );
848 if ( tmpllist != NULL ) {
849 ldap_free_templates( tmpllist );
859 if ( (fp = fopen( helpfile, "r" )) == NULL ) {
860 fprintf( op, "Cannot access helpfile (%s)\r\n", helpfile );
864 while ( fgets( line, sizeof(line), fp ) != NULL ) {
865 line[ strlen( line ) - 1 ] = '\0';
867 fprintf( op, "%s\r\n", line );
873 static do_sizelimit( fp, type )
878 fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
879 fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
880 fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
881 fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
883 fprintf( fp, "Not all entries could be returned because a size limit was exceeded.\r\n" );
884 fprintf( fp, "There is no way to defeat this feature, but if you know who you are\r\n" );
885 fprintf( fp, "looking for, try choosing the \"Search\" option listed above and\r\n" );
886 fprintf( fp, "specifying the name of the person you want.\r\n" );
888 fprintf( fp, ".\r\n" );
891 static do_error( fp, s )
899 fprintf( fp, "An error occurred searching X.500. The error code was %d\r\n", code );
900 fprintf( fp, "The corresponding error is: %s\r\n", ldap_err2string( code ) );
901 fprintf( fp, "No additional information is available\r\n" );
902 fprintf( fp, ".\r\n" );