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>
41 #define ldap_debug debug
48 #include "ldapconfig.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 );
67 static int set_socket (int port);
68 static RETSIGTYPE wait4child(int sig);
69 static void do_queries (int s);
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, int 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" );
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 lber_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 ) :
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" );
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 ) {
318 /* set option so clients can't keep us from coming back up */
320 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
321 sizeof(one) ) < 0 ) {
322 perror( "setsockopt" );
327 addr.sin_family = AF_INET;
328 addr.sin_addr.s_addr = INADDR_ANY;
329 addr.sin_port = htons( port );
330 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
335 /* listen for connections */
336 if ( listen( s, 5 ) == -1 ) {
342 printf( "go500gw listening on port %d\n", port );
348 wait4child( int sig )
351 WAITSTATUSTYPE status;
354 if ( debug ) printf( "parent: catching child status\n" );
357 while (waitpid ((pid_t) -1, (int *) NULL, WAIT_FLAGS) > 0)
360 while (wait4( (pid_t) -1, &status, WAIT_FLAGS, 0 ) > 0 )
364 (void) SIGNAL( SIGCHLD, wait4child );
370 char buf[1024], *query;
375 struct timeval timeout;
379 if ( (fp = fdopen( s, "a+")) == NULL ) {
384 timeout.tv_sec = GO500GW_TIMEOUT;
387 FD_SET( fileno( fp ), &readfds );
389 if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
392 if ( fgets( buf, sizeof(buf), fp ) == NULL )
397 fprintf( stderr, "got %d bytes\n", len );
399 lber_bprint( buf, len );
404 if ( buf[len - 1] == '\n' )
407 if ( buf[len - 1] == '\r' )
413 /* strip off leading white space */
414 while ( isspace( (unsigned char) *query )) {
421 if ( *query == 'H' || *query == 'L' || *query == 'E' ) {
422 switch ( *query++ ) {
423 case 'H': /* help file */
427 case 'L': /* size limit explanation */
428 do_sizelimit( fp, *query );
431 case 'E': /* error explanation */
432 do_error( fp, query );
436 fprintf( fp, ".\r\n" );
443 if ( (ld = ldap_open( ldaphost, ldapport )) == NULL ) {
444 if ( debug ) perror( "ldap_open" );
445 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
446 LDAP_SERVER_DOWN, myhost, myport );
447 fprintf( fp, ".\r\n" );
452 deref = LDAP_DEREF_ALWAYS;
453 if ( !searchaliases )
454 deref = LDAP_DEREF_FINDING;
456 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
458 if ( (rc = ldap_simple_bind_s( ld, NULL, NULL ))
460 if ( debug ) ldap_perror( ld, "ldap_simple_bind_s" );
461 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
462 rc, myhost, myport );
463 fprintf( fp, ".\r\n" );
468 switch ( *query++ ) {
469 case 'R': /* read an entry */
470 do_read( ld, fp, query );
473 case 'S': /* search */
474 do_search( ld, fp, query );
477 case 'M': /* X.500 menu */
478 do_menu( ld, fp, query );
482 do_menu( ld, fp, "" );
486 fprintf( fp, ".\r\n" );
494 pick_oc( char **oclist )
498 if ( oclist == NULL )
501 for ( i = 0; oclist[i] != NULL; i++ ) {
502 if ( strcasecmp( oclist[i], "top" ) != 0 &&
503 strcasecmp( oclist[i], "quipuObject" ) != 0 &&
504 strcasecmp( oclist[i], "quipuNonLeafObject" ) != 0 )
512 isnonleaf( LDAP *ld, char **oclist, char *dn )
514 int i, quipuobject = 0;
516 if ( oclist == NULL )
519 for ( i = 0; oclist[i] != NULL; i++ ) {
520 if ( strcasecmp( oclist[i], "quipuObject" ) == 0 )
522 if ( strcasecmp( oclist[i], "quipuNonLeafObject" ) == 0 ||
523 strcasecmp( oclist[i], "externalNonLeafObject" ) == 0 )
528 * not a quipu thang - no easy way to tell leaves from nonleaves
529 * except by trying to search or list. ldap only lets us search.
532 /* expensive - just guess for now */
533 return( quipuobject ? 0 : 1 );
536 if ( !quipuobject ) {
538 struct timeval timeout;
539 LDAPMessage *res = NULL;
540 static char *attrs[] = { "objectClass", 0 };
543 timeout.tv_sec = GO500GW_TIMEOUT;
545 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
546 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
547 "(objectClass=*)", attrs, 0, &timeout, &res ))
548 == LDAP_SUCCESS || rc == LDAP_SIZELIMIT_EXCEEDED ) {
549 sizelimit = LDAP_NO_LIMIT;
550 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
552 numentries = ldap_count_entries( ld, res );
555 return( numentries == 1 ? 1 : 0 );
564 do_menu( LDAP *ld, FILE *fp, char *dn )
568 LDAPFriendlyMap *fm = NULL;
570 if ( strcmp( dn, "" ) != 0 ) {
571 s = ldap_explode_dn( dn, 1 );
574 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
577 fprintf( fp, "0Read %s entry\tR%s\t%s\t%d\r\n", rdn ? rdn: s[0],
578 dn, myhost, myport );
580 ldap_value_free( s );
582 fprintf( fp, "0About the Gopher to X.500 Gateway\tH\t%s\t%d\r\n",
586 fprintf( fp, "7Search %s\tS%s\t%s\t%d\r\n", rdn ? rdn : "root", dn,
589 do_list( ld, fp, dn );
591 ldap_free_friendlymap( &fm );
595 do_list( LDAP *ld, FILE *fp, char *dn )
598 LDAPMessage *e, *res;
599 struct timeval timeout;
600 LDAPFriendlyMap *fm = NULL;
601 static char *attrs[] = { "objectClass", 0 };
602 int deref = LDAP_DEREF_FINDING;
604 timeout.tv_sec = GO500GW_TIMEOUT;
607 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
609 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
610 "(!(objectClass=dSA))", attrs, 0, &timeout, &res )) != LDAP_SUCCESS
611 && rc != LDAP_SIZELIMIT_EXCEEDED ) {
612 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
613 rc, myhost, myport );
617 deref = LDAP_DEREF_ALWAYS;
618 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
620 if ( ldap_count_entries( ld, res ) < 1 ) {
624 #ifdef GO500GW_SORT_ATTR
625 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
629 for ( e = ldap_first_entry( ld, res ); e != NULL;
630 e = ldap_next_entry( ld, e ) ) {
634 dn = ldap_get_dn( ld, e );
635 s = ldap_explode_dn( dn, 1 );
636 oc = ldap_get_values( ld, e, "objectClass" );
639 if ( strcasecmp( doc, "country" ) == 0 ) {
640 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
648 if ( strncasecmp( rdn, "{ASN}", 5 ) != 0 ) {
649 if ( isnonleaf( ld, oc, dn ) ) {
650 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", rdn,
651 doc, dn, myhost, myport );
653 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", rdn,
654 doc, dn, myhost, myport );
659 ldap_value_free( s );
660 ldap_value_free( oc );
662 ldap_free_friendlymap( &fm );
664 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
665 fprintf( fp, "0A size limit was exceeded (explanation)\tLL\t%s\t%d\r\n",
671 isoc( char **ocl, char *oc )
675 for ( i = 0; ocl[i] != NULL; i++ ) {
676 if ( strcasecmp( ocl[i], oc ) == 0 )
684 make_scope( LDAP *ld, char *dn )
689 struct timeval timeout;
690 static char *attrs[] = { "objectClass", 0 };
692 if ( strcmp( dn, "" ) == 0 )
693 return( LDAP_SCOPE_ONELEVEL );
695 timeout.tv_sec = GO500GW_TIMEOUT;
697 if ( ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
698 attrs, 0, &timeout, &res ) != LDAP_SUCCESS ) {
702 oc = ldap_get_values( ld, ldap_first_entry( ld, res ), "objectClass" );
704 if ( isoc( oc, "organization" ) || isoc( oc, "organizationalUnit" ) )
705 scope = LDAP_SCOPE_SUBTREE;
707 scope = LDAP_SCOPE_ONELEVEL;
709 ldap_value_free( oc );
716 do_search( LDAP *ld, FILE *fp, char *query )
723 struct timeval timeout;
725 LDAPMessage *e, *res;
727 static char *attrs[] = { "objectClass", 0 };
729 if ( (filter = strchr( query, '\t' )) == NULL ) {
730 fprintf( fp, "3Missing filter!\r\n" );
737 if ( strchr( filter, ',' ) != NULL ) {
738 ldap_ufn_setprefix( ld, base );
739 timeout.tv_sec = GO500GW_TIMEOUT;
741 ldap_ufn_timeout( (void *) &timeout );
743 deref = LDAP_DEREF_FINDING;
744 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
746 if ( (rc = ldap_ufn_search_s( ld, filter, attrs, 0, &res ))
747 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
749 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
750 rc, myhost, myport );
754 count = ldap_count_entries( ld, res );
757 if ( (scope = make_scope( ld, base )) == -1 ) {
758 fprintf( fp, "3Bad scope\r\n" );
762 filtertype = (scope == LDAP_SCOPE_ONELEVEL ?
763 "go500gw onelevel" : "go500gw subtree");
764 deref = (scope == LDAP_SCOPE_ONELEVEL ?
765 LDAP_DEREF_FINDING : LDAP_DEREF_ALWAYS);
766 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
767 timeout.tv_sec = GO500GW_TIMEOUT;
770 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
771 fprintf( stderr, "Cannot open filter file (%s)\n",
778 for ( fi = ldap_getfirstfilter( filtd, filtertype, filter );
779 fi != NULL; fi = ldap_getnextfilter( filtd ) )
781 if ( (rc = ldap_search_st( ld, base, scope,
782 fi->lfi_filter, attrs, 0, &timeout, &res ))
783 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
784 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
785 rc, myhost, myport );
788 if ( (count = ldap_count_entries( ld, res )) != 0 )
791 deref = LDAP_DEREF_ALWAYS;
792 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
793 ldap_getfilter_free( filtd );
805 e = ldap_first_entry( ld, res );
806 oc = ldap_get_values( ld, e, "objectClass" );
807 dn = ldap_get_dn( ld, e );
809 if ( isnonleaf( ld, oc, dn ) ) {
810 do_menu( ld, fp, dn );
817 ldap_value_free( oc );
820 #ifdef GO500GW_SORT_ATTR
821 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
824 for ( e = ldap_first_entry( ld, res ); e != NULL;
825 e = ldap_next_entry( ld, e ) ) {
829 dn = ldap_get_dn( ld, e );
830 s = ldap_explode_dn( dn, 1 );
831 oc = ldap_get_values( ld, e, "objectClass" );
833 if ( isnonleaf( ld, oc, dn ) )
834 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", s[0],
835 pick_oc( oc ), dn, myhost, myport );
837 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", s[0],
838 pick_oc( oc ), dn, myhost, myport );
841 ldap_value_free( s );
842 ldap_value_free( oc );
845 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
846 fprintf( fp, "0A size limit was exceeded (explanation)\tLS\t%s\t%d\r\n",
853 entry2textwrite( void *fp, char *buf, int len )
855 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
859 do_read( LDAP *ld, FILE *fp, char *dn )
861 static struct ldap_disptmpl *tmpllist;
863 ldap_init_templates( templatefile, &tmpllist );
865 if ( ldap_entry2text_search( ld, dn, NULL, NULL, tmpllist, NULL, NULL,
866 entry2textwrite,(void *) fp, "\r\n", rdncount, 0 )
869 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
872 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
873 ld_errno, myhost, myport );
876 if ( tmpllist != NULL ) {
877 ldap_free_templates( tmpllist );
887 if ( (fp = fopen( helpfile, "r" )) == NULL ) {
888 fprintf( op, "Cannot access helpfile (%s)\r\n", helpfile );
892 while ( fgets( line, sizeof(line), fp ) != NULL ) {
893 line[ strlen( line ) - 1 ] = '\0';
895 fprintf( op, "%s\r\n", line );
902 do_sizelimit( FILE *fp, char type )
905 fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
906 fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
907 fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
908 fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
910 fprintf( fp, "Not all entries could be returned because a size limit was exceeded.\r\n" );
911 fprintf( fp, "There is no way to defeat this feature, but if you know who you are\r\n" );
912 fprintf( fp, "looking for, try choosing the \"Search\" option listed above and\r\n" );
913 fprintf( fp, "specifying the name of the person you want.\r\n" );
915 fprintf( fp, ".\r\n" );
919 do_error( FILE *fp, char *s )
925 fprintf( fp, "An error occurred searching X.500. The error code was %d\r\n", code );
926 fprintf( fp, "The corresponding error is: %s\r\n", ldap_err2string( code ) );
927 fprintf( fp, "No additional information is available\r\n" );
928 fprintf( fp, ".\r\n" );