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 */
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 );
328 WAITSTATUSTYPE status;
330 if ( debug ) printf( "parent: catching child status\n" );
332 while (waitpid ((pid_t) -1, 0, WAIT_FLAGS) > 0)
333 #else /* USE_WAITPID */
334 while ( wait3( &status, WAIT_FLAGS, 0 ) > 0 )
335 #endif /* USE_WAITPID */
338 (void) signal( SIGCHLD, (void *) wait4child );
341 static do_queries( s )
344 char buf[1024], *query;
348 struct timeval timeout;
352 if ( (fp = fdopen( s, "a+")) == NULL ) {
357 timeout.tv_sec = GO500GW_TIMEOUT;
360 FD_SET( fileno( fp ), &readfds );
362 if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
365 if ( fgets( buf, sizeof(buf), fp ) == NULL )
370 fprintf( stderr, "got %d bytes\n", len );
372 lber_bprint( buf, len );
377 if ( buf[len - 1] == '\n' )
380 if ( buf[len - 1] == '\r' )
386 /* strip off leading white space */
387 while ( isspace( *query )) {
394 if ( *query == 'H' || *query == 'L' || *query == 'E' ) {
395 switch ( *query++ ) {
396 case 'H': /* help file */
400 case 'L': /* size limit explanation */
401 do_sizelimit( fp, *query );
404 case 'E': /* error explanation */
405 do_error( fp, query );
409 fprintf( fp, ".\r\n" );
416 if ( (ld = ldap_open( ldaphost, ldapport )) == NULL ) {
417 if ( debug ) perror( "ldap_open" );
418 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
419 LDAP_SERVER_DOWN, myhost, myport );
420 fprintf( fp, ".\r\n" );
425 ld->ld_deref = LDAP_DEREF_ALWAYS;
426 if ( !searchaliases )
427 ld->ld_deref = LDAP_DEREF_FINDING;
429 if ( (rc = ldap_simple_bind_s( ld, GO500GW_BINDDN, NULL ))
431 if ( debug ) ldap_perror( ld, "ldap_simple_bind_s" );
432 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
433 rc, myhost, myport );
434 fprintf( fp, ".\r\n" );
439 switch ( *query++ ) {
440 case 'R': /* read an entry */
441 do_read( ld, fp, query );
444 case 'S': /* search */
445 do_search( ld, fp, query, 1 );
448 case 'M': /* X.500 menu */
449 do_menu( ld, fp, query );
453 do_menu( ld, fp, "" );
457 fprintf( fp, ".\r\n" );
464 static char *pick_oc( oclist )
469 if ( oclist == NULL )
472 for ( i = 0; oclist[i] != NULL; i++ ) {
473 if ( strcasecmp( oclist[i], "top" ) != 0 &&
474 strcasecmp( oclist[i], "quipuObject" ) != 0 &&
475 strcasecmp( oclist[i], "quipuNonLeafObject" ) != 0 )
482 static isnonleaf( ld, oclist, dn )
487 int i, quipuobject = 0;
489 if ( oclist == NULL )
492 for ( i = 0; oclist[i] != NULL; i++ ) {
493 if ( strcasecmp( oclist[i], "quipuObject" ) == 0 )
495 if ( strcasecmp( oclist[i], "quipuNonLeafObject" ) == 0 ||
496 strcasecmp( oclist[i], "externalNonLeafObject" ) == 0 )
501 * not a quipu thang - no easy way to tell leaves from nonleaves
502 * except by trying to search or list. ldap only lets us search.
505 /* expensive - just guess for now */
506 return( quipuobject ? 0 : 1 );
509 if ( !quipuobject ) {
511 struct timeval timeout;
512 LDAPMessage *res = NULL;
513 static char *attrs[] = { "objectClass", 0 };
515 timeout.tv_sec = GO500GW_TIMEOUT;
517 ld->ld_sizelimit = 1;
518 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
519 "(objectClass=*)", attrs, 0, &timeout, &res ))
520 == LDAP_SUCCESS || rc == LDAP_SIZELIMIT_EXCEEDED ) {
521 ld->ld_sizelimit = LDAP_NO_LIMIT;
523 numentries = ldap_count_entries( ld, res );
526 return( numentries == 1 ? 1 : 0 );
534 static do_menu( ld, fp, dn )
541 FriendlyMap *fm = NULL;
543 if ( strcmp( dn, "" ) != 0 ) {
544 s = ldap_explode_dn( dn, 1 );
547 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
550 fprintf( fp, "0Read %s entry\tR%s\t%s\t%d\r\n", rdn ? rdn: s[0],
551 dn, myhost, myport );
553 ldap_value_free( s );
555 fprintf( fp, "0About the Gopher to X.500 Gateway\tH\t%s\t%d\r\n",
559 fprintf( fp, "7Search %s\tS%s\t%s\t%d\r\n", rdn ? rdn : "root", dn,
562 do_list( ld, fp, dn );
564 ldap_free_friendlymap( &fm );
567 static do_list( ld, fp, dn )
573 LDAPMessage *e, *res;
574 struct timeval timeout;
575 FriendlyMap *fm = NULL;
576 static char *attrs[] = { "objectClass", 0 };
578 timeout.tv_sec = GO500GW_TIMEOUT;
580 ld->ld_deref = LDAP_DEREF_FINDING;
581 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
582 "(!(objectClass=dSA))", attrs, 0, &timeout, &res )) != LDAP_SUCCESS
583 && rc != LDAP_SIZELIMIT_EXCEEDED ) {
584 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
585 rc, myhost, myport );
588 ld->ld_deref = LDAP_DEREF_ALWAYS;
590 if ( ldap_count_entries( ld, res ) < 1 ) {
594 #ifdef GO500GW_SORT_ATTR
595 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
599 for ( e = ldap_first_entry( ld, res ); e != NULL;
600 e = ldap_next_entry( ld, e ) ) {
604 dn = ldap_get_dn( ld, e );
605 s = ldap_explode_dn( dn, 1 );
606 oc = ldap_get_values( ld, e, "objectClass" );
609 if ( strcasecmp( doc, "country" ) == 0 ) {
610 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
618 if ( strncasecmp( rdn, "{ASN}", 5 ) != 0 ) {
619 if ( isnonleaf( ld, oc, dn ) ) {
620 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", rdn,
621 doc, dn, myhost, myport );
623 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", rdn,
624 doc, dn, myhost, myport );
629 ldap_value_free( s );
630 ldap_value_free( oc );
632 ldap_free_friendlymap( &fm );
634 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
635 fprintf( fp, "0A size limit was exceeded (explanation)\tLL\t%s\t%d\r\n",
640 static isoc( ocl, oc )
646 for ( i = 0; ocl[i] != NULL; i++ ) {
647 if ( strcasecmp( ocl[i], oc ) == 0 )
654 static int make_scope( ld, dn )
661 struct timeval timeout;
662 static char *attrs[] = { "objectClass", 0 };
664 if ( strcmp( dn, "" ) == 0 )
665 return( LDAP_SCOPE_ONELEVEL );
667 timeout.tv_sec = GO500GW_TIMEOUT;
669 if ( ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
670 attrs, 0, &timeout, &res ) != LDAP_SUCCESS ) {
674 oc = ldap_get_values( ld, ldap_first_entry( ld, res ), "objectClass" );
676 if ( isoc( oc, "organization" ) || isoc( oc, "organizationalUnit" ) )
677 scope = LDAP_SCOPE_SUBTREE;
679 scope = LDAP_SCOPE_ONELEVEL;
681 ldap_value_free( oc );
687 static do_search( ld, fp, query )
696 struct timeval timeout;
698 LDAPMessage *e, *res;
700 static char *attrs[] = { "objectClass", 0 };
702 if ( (filter = strchr( query, '\t' )) == NULL ) {
703 fprintf( fp, "3Missing filter!\r\n" );
710 if ( strchr( filter, ',' ) != NULL ) {
711 ldap_ufn_setprefix( ld, base );
712 timeout.tv_sec = GO500GW_TIMEOUT;
714 ldap_ufn_timeout( (void *) &timeout );
715 ld->ld_deref = LDAP_DEREF_FINDING;
717 if ( (rc = ldap_ufn_search_s( ld, filter, attrs, 0, &res ))
718 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
720 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
721 rc, myhost, myport );
725 count = ldap_count_entries( ld, res );
728 if ( (scope = make_scope( ld, base )) == -1 ) {
729 fprintf( fp, "3Bad scope\r\n" );
733 filtertype = (scope == LDAP_SCOPE_ONELEVEL ?
734 "go500gw onelevel" : "go500gw subtree");
735 ld->ld_deref = (scope == LDAP_SCOPE_ONELEVEL ?
736 LDAP_DEREF_FINDING : LDAP_DEREF_ALWAYS);
737 timeout.tv_sec = GO500GW_TIMEOUT;
740 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
741 fprintf( stderr, "Cannot open filter file (%s)\n",
748 for ( fi = ldap_getfirstfilter( filtd, filtertype, filter );
749 fi != NULL; fi = ldap_getnextfilter( filtd ) )
751 if ( (rc = ldap_search_st( ld, base, scope,
752 fi->lfi_filter, attrs, 0, &timeout, &res ))
753 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
754 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
755 rc, myhost, myport );
758 if ( (count = ldap_count_entries( ld, res )) != 0 )
761 ld->ld_deref = LDAP_DEREF_ALWAYS;
762 ldap_getfilter_free( filtd );
775 e = ldap_first_entry( ld, res );
776 oc = ldap_get_values( ld, e, "objectClass" );
777 if ( isnonleaf( ld, oc, dn ) ) {
778 dn = ldap_get_dn( ld, e );
780 rc = do_menu( ld, fp, dn );
786 ldap_value_free( oc );
789 #ifdef GO500GW_SORT_ATTR
790 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
793 for ( e = ldap_first_entry( ld, res ); e != NULL;
794 e = ldap_next_entry( ld, e ) ) {
798 dn = ldap_get_dn( ld, e );
799 s = ldap_explode_dn( dn, 1 );
800 oc = ldap_get_values( ld, e, "objectClass" );
802 if ( isnonleaf( ld, oc, dn ) )
803 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", s[0],
804 pick_oc( oc ), dn, myhost, myport );
806 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", s[0],
807 pick_oc( oc ), dn, myhost, myport );
810 ldap_value_free( s );
811 ldap_value_free( oc );
814 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
815 fprintf( fp, "0A size limit was exceeded (explanation)\tLS\t%s\t%d\r\n",
822 entry2textwrite( void *fp, char *buf, int len )
824 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
827 static do_read( ld, fp, dn )
832 static struct ldap_disptmpl *tmpllist;
834 ldap_init_templates( templatefile, &tmpllist );
836 if ( ldap_entry2text_search( ld, dn, NULL, NULL, tmpllist, NULL, NULL,
837 entry2textwrite,(void *) fp, "\r\n", rdncount, 0 )
840 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
841 ld->ld_errno, myhost, myport );
844 if ( tmpllist != NULL ) {
845 ldap_free_templates( tmpllist );
855 if ( (fp = fopen( helpfile, "r" )) == NULL ) {
856 fprintf( op, "Cannot access helpfile (%s)\r\n", helpfile );
860 while ( fgets( line, sizeof(line), fp ) != NULL ) {
861 line[ strlen( line ) - 1 ] = '\0';
863 fprintf( op, "%s\r\n", line );
869 static do_sizelimit( fp, type )
874 fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
875 fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
876 fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
877 fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
879 fprintf( fp, "Not all entries could be returned because a size limit was exceeded.\r\n" );
880 fprintf( fp, "There is no way to defeat this feature, but if you know who you are\r\n" );
881 fprintf( fp, "looking for, try choosing the \"Search\" option listed above and\r\n" );
882 fprintf( fp, "specifying the name of the person you want.\r\n" );
884 fprintf( fp, ".\r\n" );
887 static do_error( fp, s )
895 fprintf( fp, "An error occurred searching X.500. The error code was %d\r\n", code );
896 fprintf( fp, "The corresponding error is: %s\r\n", ldap_err2string( code ) );
897 fprintf( fp, "No additional information is available\r\n" );
898 fprintf( fp, ".\r\n" );