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 <sys/types.h>
20 #include <sys/param.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
27 #include <sys/resource.h>
31 #include <sys/select.h>
34 #include "ldapconfig.h"
38 #endif /* USE_SYSCONF */
45 char *ldaphost = LDAPHOST;
46 int ldapport = LDAP_PORT;
47 int searchaliases = 1;
48 char *helpfile = GO500GW_HELPFILE;
49 char *filterfile = FILTERFILE;
50 char *templatefile = TEMPLATEFILE;
51 char *friendlyfile = FRIENDLYFILE;
52 int rdncount = GO500GW_RDNCOUNT;
55 static SIG_FN wait4child();
62 static do_sizelimit();
64 extern int strcasecmp();
66 char myhost[MAXHOSTNAMELEN];
67 int myport = GO500GW_PORT;
72 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 );
86 struct sockaddr_in from;
93 /* for setproctitle */
97 while ( (i = getopt( argc, argv, "P:ad:f:h:lp:t:x:Ic:" )) != EOF ) {
99 case 'a': /* search aliases */
103 case 'd': /* debugging level */
104 debug = atoi( optarg );
108 fprintf( stderr, "warning: ldap debugging requires LDAP_DEBUG\n" );
112 case 'f': /* ldap filter file */
113 filterfile = strdup( optarg );
116 case 'h': /* gopher help file */
117 helpfile = strdup( optarg );
120 case 'l': /* log to LOG_LOCAL3 */
124 case 'p': /* port to listen on */
125 port = atoi( optarg );
128 case 'P': /* port to connect to ldap server */
129 ldapport = atoi( optarg );
132 case 't': /* ldap template file */
133 templatefile = strdup( optarg );
136 case 'x': /* ldap server hostname */
137 ldaphost = strdup( optarg );
140 case 'I': /* run from inetd */
144 case 'c': /* count of DN components to show */
145 rdncount = atoi( optarg );
154 dtblsize = sysconf( _SC_OPEN_MAX );
155 #else /* USE_SYSCONF */
156 dtblsize = getdtablesize();
157 #endif /* USE_SYSCONF */
159 #ifdef GO500GW_HOSTNAME
160 strcpy( myhost, GO500GW_HOSTNAME );
162 if ( myhost[0] == '\0' && gethostname( myhost, sizeof(myhost) )
164 perror( "gethostname" );
169 /* detach if stderr is redirected or no debugging */
171 (void) detach( debug );
173 if ( (myname = strrchr( argv[0], '/' )) == NULL )
174 myname = strdup( argv[0] );
176 myname = strdup( myname + 1 );
180 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL3 );
182 openlog( myname, OPENLOG_OPTIONS );
186 syslog( LOG_INFO, "initializing" );
188 /* set up the socket to listen on */
190 s = set_socket( port );
192 /* arrange to reap children */
193 (void) signal( SIGCHLD, (void *) wait4child );
197 fromlen = sizeof(from);
198 if ( getpeername( 0, (struct sockaddr *) &from, &fromlen )
200 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
201 sizeof(from.sin_addr.s_addr), AF_INET );
202 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
203 (hp == NULL) ? "unknown" : hp->h_name,
204 inet_ntoa( from.sin_addr ), 0 );
207 syslog( LOG_INFO, "connection from %s (%s)",
208 (hp == NULL) ? "unknown" : hp->h_name,
209 inet_ntoa( from.sin_addr ) );
212 setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
225 FD_SET( s, &readfds );
227 if ( (rc = select( dtblsize, &readfds, 0, 0 ,0 )) == -1 ) {
228 if ( debug ) perror( "select" );
230 } else if ( rc == 0 ) {
234 if ( ! FD_ISSET( s, &readfds ) )
237 fromlen = sizeof(from);
238 if ( (ns = accept( s, (struct sockaddr *) &from, &fromlen ))
240 if ( debug ) perror( "accept" );
244 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
245 sizeof(from.sin_addr.s_addr), AF_INET );
248 syslog( LOG_INFO, "TCP connection from %s (%s)",
249 (hp == NULL) ? "unknown" : hp->h_name,
250 inet_ntoa( from.sin_addr ) );
253 switch( pid = fork() ) {
259 case -1: /* failed */
263 default: /* parent */
266 fprintf( stderr, "forked child %d\n", pid );
273 static set_socket( port )
277 struct sockaddr_in addr;
283 if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
288 /* set option so clients can't keep us from coming back up */
290 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
291 sizeof(one) ) < 0 ) {
292 perror( "setsockopt" );
297 addr.sin_family = AF_INET;
298 addr.sin_addr.s_addr = INADDR_ANY;
299 addr.sin_port = htons( port );
300 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
305 /* listen for connections */
306 if ( listen( s, 5 ) == -1 ) {
312 printf( "go500gw listening on port %d\n", port );
320 WAITSTATUSTYPE status;
322 if ( debug ) printf( "parent: catching child status\n" );
324 while (waitpid ((pid_t) -1, 0, WAIT_FLAGS) > 0)
325 #else /* USE_WAITPID */
326 while ( wait3( &status, WAIT_FLAGS, 0 ) > 0 )
327 #endif /* USE_WAITPID */
330 (void) signal( SIGCHLD, (void *) wait4child );
333 static do_queries( s )
336 char buf[1024], *query;
340 struct timeval timeout;
344 if ( (fp = fdopen( s, "a+")) == NULL ) {
349 timeout.tv_sec = GO500GW_TIMEOUT;
352 FD_SET( fileno( fp ), &readfds );
354 if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
357 if ( fgets( buf, sizeof(buf), fp ) == NULL )
362 fprintf( stderr, "got %d bytes\n", len );
364 lber_bprint( buf, len );
369 if ( buf[len - 1] == '\n' )
372 if ( buf[len - 1] == '\r' )
378 /* strip off leading white space */
379 while ( isspace( *query )) {
386 if ( *query == 'H' || *query == 'L' || *query == 'E' ) {
387 switch ( *query++ ) {
388 case 'H': /* help file */
392 case 'L': /* size limit explanation */
393 do_sizelimit( fp, *query );
396 case 'E': /* error explanation */
397 do_error( fp, query );
401 fprintf( fp, ".\r\n" );
408 if ( (ld = ldap_open( ldaphost, ldapport )) == NULL ) {
409 if ( debug ) perror( "ldap_open" );
410 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
411 LDAP_SERVER_DOWN, myhost, myport );
412 fprintf( fp, ".\r\n" );
417 ld->ld_deref = LDAP_DEREF_ALWAYS;
418 if ( !searchaliases )
419 ld->ld_deref = LDAP_DEREF_FINDING;
421 if ( (rc = ldap_simple_bind_s( ld, GO500GW_BINDDN, NULL ))
423 if ( debug ) ldap_perror( ld, "ldap_simple_bind_s" );
424 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
425 rc, myhost, myport );
426 fprintf( fp, ".\r\n" );
431 switch ( *query++ ) {
432 case 'R': /* read an entry */
433 do_read( ld, fp, query );
436 case 'S': /* search */
437 do_search( ld, fp, query, 1 );
440 case 'M': /* X.500 menu */
441 do_menu( ld, fp, query );
445 do_menu( ld, fp, "" );
449 fprintf( fp, ".\r\n" );
456 static char *pick_oc( oclist )
461 if ( oclist == NULL )
464 for ( i = 0; oclist[i] != NULL; i++ ) {
465 if ( strcasecmp( oclist[i], "top" ) != 0 &&
466 strcasecmp( oclist[i], "quipuObject" ) != 0 &&
467 strcasecmp( oclist[i], "quipuNonLeafObject" ) != 0 )
474 static isnonleaf( ld, oclist, dn )
479 int i, quipuobject = 0;
481 if ( oclist == NULL )
484 for ( i = 0; oclist[i] != NULL; i++ ) {
485 if ( strcasecmp( oclist[i], "quipuObject" ) == 0 )
487 if ( strcasecmp( oclist[i], "quipuNonLeafObject" ) == 0 ||
488 strcasecmp( oclist[i], "externalNonLeafObject" ) == 0 )
493 * not a quipu thang - no easy way to tell leaves from nonleaves
494 * except by trying to search or list. ldap only lets us search.
497 /* expensive - just guess for now */
498 return( quipuobject ? 0 : 1 );
501 if ( !quipuobject ) {
503 struct timeval timeout;
504 LDAPMessage *res = NULL;
505 static char *attrs[] = { "objectClass", 0 };
507 timeout.tv_sec = GO500GW_TIMEOUT;
509 ld->ld_sizelimit = 1;
510 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
511 "(objectClass=*)", attrs, 0, &timeout, &res ))
512 == LDAP_SUCCESS || rc == LDAP_SIZELIMIT_EXCEEDED ) {
513 ld->ld_sizelimit = LDAP_NO_LIMIT;
515 numentries = ldap_count_entries( ld, res );
518 return( numentries == 1 ? 1 : 0 );
526 static do_menu( ld, fp, dn )
533 FriendlyMap *fm = NULL;
535 if ( strcmp( dn, "" ) != 0 ) {
536 s = ldap_explode_dn( dn, 1 );
539 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
542 fprintf( fp, "0Read %s entry\tR%s\t%s\t%d\r\n", rdn ? rdn: s[0],
543 dn, myhost, myport );
545 ldap_value_free( s );
547 fprintf( fp, "0About the Gopher to X.500 Gateway\tH\t%s\t%d\r\n",
551 fprintf( fp, "7Search %s\tS%s\t%s\t%d\r\n", rdn ? rdn : "root", dn,
554 do_list( ld, fp, dn );
556 ldap_free_friendlymap( &fm );
559 static do_list( ld, fp, dn )
565 LDAPMessage *e, *res;
566 struct timeval timeout;
567 FriendlyMap *fm = NULL;
568 static char *attrs[] = { "objectClass", 0 };
570 timeout.tv_sec = GO500GW_TIMEOUT;
572 ld->ld_deref = LDAP_DEREF_FINDING;
573 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
574 "(!(objectClass=dSA))", attrs, 0, &timeout, &res )) != LDAP_SUCCESS
575 && rc != LDAP_SIZELIMIT_EXCEEDED ) {
576 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
577 rc, myhost, myport );
580 ld->ld_deref = LDAP_DEREF_ALWAYS;
582 if ( ldap_count_entries( ld, res ) < 1 ) {
586 #ifdef GO500GW_SORT_ATTR
587 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
591 for ( e = ldap_first_entry( ld, res ); e != NULL;
592 e = ldap_next_entry( ld, e ) ) {
596 dn = ldap_get_dn( ld, e );
597 s = ldap_explode_dn( dn, 1 );
598 oc = ldap_get_values( ld, e, "objectClass" );
601 if ( strcasecmp( doc, "country" ) == 0 ) {
602 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
610 if ( strncasecmp( rdn, "{ASN}", 5 ) != 0 ) {
611 if ( isnonleaf( ld, oc, dn ) ) {
612 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", rdn,
613 doc, dn, myhost, myport );
615 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", rdn,
616 doc, dn, myhost, myport );
621 ldap_value_free( s );
622 ldap_value_free( oc );
624 ldap_free_friendlymap( &fm );
626 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
627 fprintf( fp, "0A size limit was exceeded (explanation)\tLL\t%s\t%d\r\n",
632 static isoc( ocl, oc )
638 for ( i = 0; ocl[i] != NULL; i++ ) {
639 if ( strcasecmp( ocl[i], oc ) == 0 )
646 static int make_scope( ld, dn )
653 struct timeval timeout;
654 static char *attrs[] = { "objectClass", 0 };
656 if ( strcmp( dn, "" ) == 0 )
657 return( LDAP_SCOPE_ONELEVEL );
659 timeout.tv_sec = GO500GW_TIMEOUT;
661 if ( ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
662 attrs, 0, &timeout, &res ) != LDAP_SUCCESS ) {
666 oc = ldap_get_values( ld, ldap_first_entry( ld, res ), "objectClass" );
668 if ( isoc( oc, "organization" ) || isoc( oc, "organizationalUnit" ) )
669 scope = LDAP_SCOPE_SUBTREE;
671 scope = LDAP_SCOPE_ONELEVEL;
673 ldap_value_free( oc );
679 static do_search( ld, fp, query )
688 struct timeval timeout;
690 LDAPMessage *e, *res;
692 static char *attrs[] = { "objectClass", 0 };
694 if ( (filter = strchr( query, '\t' )) == NULL ) {
695 fprintf( fp, "3Missing filter!\r\n" );
702 if ( strchr( filter, ',' ) != NULL ) {
703 ldap_ufn_setprefix( ld, base );
704 timeout.tv_sec = GO500GW_TIMEOUT;
706 ldap_ufn_timeout( (void *) &timeout );
707 ld->ld_deref = LDAP_DEREF_FINDING;
709 if ( (rc = ldap_ufn_search_s( ld, filter, attrs, 0, &res ))
710 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
712 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
713 rc, myhost, myport );
717 count = ldap_count_entries( ld, res );
720 if ( (scope = make_scope( ld, base )) == -1 ) {
721 fprintf( fp, "3Bad scope\r\n" );
725 filtertype = (scope == LDAP_SCOPE_ONELEVEL ?
726 "go500gw onelevel" : "go500gw subtree");
727 ld->ld_deref = (scope == LDAP_SCOPE_ONELEVEL ?
728 LDAP_DEREF_FINDING : LDAP_DEREF_ALWAYS);
729 timeout.tv_sec = GO500GW_TIMEOUT;
732 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
733 fprintf( stderr, "Cannot open filter file (%s)\n",
740 for ( fi = ldap_getfirstfilter( filtd, filtertype, filter );
741 fi != NULL; fi = ldap_getnextfilter( filtd ) )
743 if ( (rc = ldap_search_st( ld, base, scope,
744 fi->lfi_filter, attrs, 0, &timeout, &res ))
745 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
746 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
747 rc, myhost, myport );
750 if ( (count = ldap_count_entries( ld, res )) != 0 )
753 ld->ld_deref = LDAP_DEREF_ALWAYS;
754 ldap_getfilter_free( filtd );
767 e = ldap_first_entry( ld, res );
768 oc = ldap_get_values( ld, e, "objectClass" );
769 if ( isnonleaf( ld, oc, dn ) ) {
770 dn = ldap_get_dn( ld, e );
772 rc = do_menu( ld, fp, dn );
778 ldap_value_free( oc );
781 #ifdef GO500GW_SORT_ATTR
782 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
785 for ( e = ldap_first_entry( ld, res ); e != NULL;
786 e = ldap_next_entry( ld, e ) ) {
790 dn = ldap_get_dn( ld, e );
791 s = ldap_explode_dn( dn, 1 );
792 oc = ldap_get_values( ld, e, "objectClass" );
794 if ( isnonleaf( ld, oc, dn ) )
795 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", s[0],
796 pick_oc( oc ), dn, myhost, myport );
798 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", s[0],
799 pick_oc( oc ), dn, myhost, myport );
802 ldap_value_free( s );
803 ldap_value_free( oc );
806 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
807 fprintf( fp, "0A size limit was exceeded (explanation)\tLS\t%s\t%d\r\n",
814 entry2textwrite( void *fp, char *buf, int len )
816 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
819 static do_read( ld, fp, dn )
824 static struct ldap_disptmpl *tmpllist;
826 ldap_init_templates( templatefile, &tmpllist );
828 if ( ldap_entry2text_search( ld, dn, NULL, NULL, tmpllist, NULL, NULL,
829 entry2textwrite,(void *) fp, "\r\n", rdncount, 0 )
832 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
833 ld->ld_errno, myhost, myport );
836 if ( tmpllist != NULL ) {
837 ldap_free_templates( tmpllist );
847 if ( (fp = fopen( helpfile, "r" )) == NULL ) {
848 fprintf( op, "Cannot access helpfile (%s)\r\n", helpfile );
852 while ( fgets( line, sizeof(line), fp ) != NULL ) {
853 line[ strlen( line ) - 1 ] = '\0';
855 fprintf( op, "%s\r\n", line );
861 static do_sizelimit( fp, type )
866 fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
867 fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
868 fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
869 fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
871 fprintf( fp, "Not all entries could be returned because a size limit was exceeded.\r\n" );
872 fprintf( fp, "There is no way to defeat this feature, but if you know who you are\r\n" );
873 fprintf( fp, "looking for, try choosing the \"Search\" option listed above and\r\n" );
874 fprintf( fp, "specifying the name of the person you want.\r\n" );
876 fprintf( fp, ".\r\n" );
879 static do_error( fp, s )
887 fprintf( fp, "An error occurred searching X.500. The error code was %d\r\n", code );
888 fprintf( fp, "The corresponding error is: %s\r\n", ldap_err2string( code ) );
889 fprintf( fp, "No additional information is available\r\n" );
890 fprintf( fp, ".\r\n" );