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 <ac/setproctitle.h>
28 #include <sys/resource.h>
30 #ifdef HAVE_SYS_PARAM_H
31 #include <sys/param.h>
41 #include "ldapconfig.h"
48 char *ldaphost = NULL;
50 int searchaliases = 1;
51 char *helpfile = GO500GW_HELPFILE;
52 char *filterfile = FILTERFILE;
53 char *templatefile = TEMPLATEFILE;
54 char *friendlyfile = FRIENDLYFILE;
55 int rdncount = GO500GW_RDNCOUNT;
58 static RETSIGTYPE wait4child();
65 static do_sizelimit();
67 extern int strcasecmp();
69 char myhost[MAXHOSTNAMELEN];
70 int myport = GO500GW_PORT;
75 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 );
89 struct sockaddr_in from;
91 RETSIGTYPE wait4child();
94 #if defined( LDAP_PROCTITLE ) && !defined( HAVE_SETPROCTITLE )
95 /* for setproctitle */
100 while ( (i = getopt( argc, argv, "P:ad:f:h:lp:t:x:Ic:" )) != EOF ) {
102 case 'a': /* search aliases */
106 case 'd': /* debugging level */
107 debug = atoi( optarg );
111 fprintf( stderr, "warning: ldap debugging requires LDAP_DEBUG\n" );
115 case 'f': /* ldap filter file */
116 filterfile = strdup( optarg );
119 case 'h': /* gopher help file */
120 helpfile = strdup( optarg );
123 case 'l': /* log to LOG_LOCAL3 */
127 case 'p': /* port to listen on */
128 port = atoi( optarg );
131 case 'P': /* port to connect to ldap server */
132 ldapport = atoi( optarg );
135 case 't': /* ldap template file */
136 templatefile = strdup( optarg );
139 case 'x': /* ldap server hostname */
140 ldaphost = strdup( optarg );
143 case 'I': /* run from inetd */
147 case 'c': /* count of DN components to show */
148 rdncount = atoi( optarg );
157 dtblsize = sysconf( _SC_OPEN_MAX );
158 #elif HAVE_GETDTABLESIZE
159 dtblsize = getdtablesize();
161 dtblsize = FD_SETSIZE;
165 if ( dtblsize > FD_SETSIZE ) {
166 dtblsize = FD_SETSIZE;
168 #endif /* FD_SETSIZE*/
172 #ifdef GO500GW_HOSTNAME
173 strcpy( myhost, GO500GW_HOSTNAME );
175 if ( myhost[0] == '\0' && gethostname( myhost, sizeof(myhost) )
177 perror( "gethostname" );
182 /* detach if stderr is redirected or no debugging */
184 lutil_detach( debug && !isatty( 1 ), 1 );
186 if ( (myname = strrchr( argv[0], '/' )) == NULL )
187 myname = strdup( argv[0] );
189 myname = strdup( myname + 1 );
193 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL3 );
195 openlog( myname, OPENLOG_OPTIONS );
199 syslog( LOG_INFO, "initializing" );
201 /* set up the socket to listen on */
203 s = set_socket( port );
205 /* arrange to reap children */
206 (void) SIGNAL( SIGCHLD, wait4child );
210 fromlen = sizeof(from);
211 if ( getpeername( 0, (struct sockaddr *) &from, &fromlen )
213 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
214 sizeof(from.sin_addr.s_addr), AF_INET );
215 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
216 (hp == NULL) ? "unknown" : hp->h_name,
217 inet_ntoa( from.sin_addr ), 0 );
220 syslog( LOG_INFO, "connection from %s (%s)",
221 (hp == NULL) ? "unknown" : hp->h_name,
222 inet_ntoa( from.sin_addr ) );
225 #ifdef LDAP_PROCTITLE
226 setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
240 FD_SET( s, &readfds );
242 if ( (rc = select( dtblsize, &readfds, 0, 0 ,0 )) == -1 ) {
243 if ( debug ) perror( "select" );
245 } else if ( rc == 0 ) {
249 if ( ! FD_ISSET( s, &readfds ) )
252 fromlen = sizeof(from);
253 if ( (ns = accept( s, (struct sockaddr *) &from, &fromlen ))
255 if ( debug ) perror( "accept" );
259 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
260 sizeof(from.sin_addr.s_addr), AF_INET );
263 syslog( LOG_INFO, "TCP connection from %s (%s)",
264 (hp == NULL) ? "unknown" : hp->h_name,
265 inet_ntoa( from.sin_addr ) );
268 switch( pid = fork() ) {
274 case -1: /* failed */
278 default: /* parent */
281 fprintf( stderr, "forked child %d\n", pid );
288 static set_socket( port )
292 struct sockaddr_in addr;
298 if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
303 /* set option so clients can't keep us from coming back up */
305 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
306 sizeof(one) ) < 0 ) {
307 perror( "setsockopt" );
312 addr.sin_family = AF_INET;
313 addr.sin_addr.s_addr = INADDR_ANY;
314 addr.sin_port = htons( port );
315 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
320 /* listen for connections */
321 if ( listen( s, 5 ) == -1 ) {
327 printf( "go500gw listening on port %d\n", port );
336 WAITSTATUSTYPE status;
339 if ( debug ) printf( "parent: catching child status\n" );
342 while (waitpid ((pid_t) -1, NULL, WAIT_FLAGS) > 0)
345 while (wait3( &status, WAIT_FLAGS, 0 ) > 0 )
349 (void) SIGNAL( SIGCHLD, wait4child );
352 static do_queries( s )
355 char buf[1024], *query;
360 struct timeval timeout;
364 if ( (fp = fdopen( s, "a+")) == NULL ) {
369 timeout.tv_sec = GO500GW_TIMEOUT;
372 FD_SET( fileno( fp ), &readfds );
374 if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
377 if ( fgets( buf, sizeof(buf), fp ) == NULL )
382 fprintf( stderr, "got %d bytes\n", len );
384 lber_bprint( buf, len );
389 if ( buf[len - 1] == '\n' )
392 if ( buf[len - 1] == '\r' )
398 /* strip off leading white space */
399 while ( isspace( *query )) {
406 if ( *query == 'H' || *query == 'L' || *query == 'E' ) {
407 switch ( *query++ ) {
408 case 'H': /* help file */
412 case 'L': /* size limit explanation */
413 do_sizelimit( fp, *query );
416 case 'E': /* error explanation */
417 do_error( fp, query );
421 fprintf( fp, ".\r\n" );
428 if ( (ld = ldap_open( ldaphost, ldapport )) == NULL ) {
429 if ( debug ) perror( "ldap_open" );
430 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
431 LDAP_SERVER_DOWN, myhost, myport );
432 fprintf( fp, ".\r\n" );
437 deref = LDAP_DEREF_ALWAYS;
438 if ( !searchaliases )
439 deref = LDAP_DEREF_FINDING;
441 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
443 if ( (rc = ldap_simple_bind_s( ld, NULL, NULL ))
445 if ( debug ) ldap_perror( ld, "ldap_simple_bind_s" );
446 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
447 rc, myhost, myport );
448 fprintf( fp, ".\r\n" );
453 switch ( *query++ ) {
454 case 'R': /* read an entry */
455 do_read( ld, fp, query );
458 case 'S': /* search */
459 do_search( ld, fp, query, 1 );
462 case 'M': /* X.500 menu */
463 do_menu( ld, fp, query );
467 do_menu( ld, fp, "" );
471 fprintf( fp, ".\r\n" );
478 static char *pick_oc( oclist )
483 if ( oclist == NULL )
486 for ( i = 0; oclist[i] != NULL; i++ ) {
487 if ( strcasecmp( oclist[i], "top" ) != 0 &&
488 strcasecmp( oclist[i], "quipuObject" ) != 0 &&
489 strcasecmp( oclist[i], "quipuNonLeafObject" ) != 0 )
496 static isnonleaf( ld, oclist, dn )
501 int i, quipuobject = 0;
503 if ( oclist == NULL )
506 for ( i = 0; oclist[i] != NULL; i++ ) {
507 if ( strcasecmp( oclist[i], "quipuObject" ) == 0 )
509 if ( strcasecmp( oclist[i], "quipuNonLeafObject" ) == 0 ||
510 strcasecmp( oclist[i], "externalNonLeafObject" ) == 0 )
515 * not a quipu thang - no easy way to tell leaves from nonleaves
516 * except by trying to search or list. ldap only lets us search.
519 /* expensive - just guess for now */
520 return( quipuobject ? 0 : 1 );
523 if ( !quipuobject ) {
525 struct timeval timeout;
526 LDAPMessage *res = NULL;
527 static char *attrs[] = { "objectClass", 0 };
530 timeout.tv_sec = GO500GW_TIMEOUT;
532 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
533 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
534 "(objectClass=*)", attrs, 0, &timeout, &res ))
535 == LDAP_SUCCESS || rc == LDAP_SIZELIMIT_EXCEEDED ) {
536 sizelimit = LDAP_NO_LIMIT;
537 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
539 numentries = ldap_count_entries( ld, res );
542 return( numentries == 1 ? 1 : 0 );
550 static do_menu( ld, fp, dn )
557 FriendlyMap *fm = NULL;
559 if ( strcmp( dn, "" ) != 0 ) {
560 s = ldap_explode_dn( dn, 1 );
563 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
566 fprintf( fp, "0Read %s entry\tR%s\t%s\t%d\r\n", rdn ? rdn: s[0],
567 dn, myhost, myport );
569 ldap_value_free( s );
571 fprintf( fp, "0About the Gopher to X.500 Gateway\tH\t%s\t%d\r\n",
575 fprintf( fp, "7Search %s\tS%s\t%s\t%d\r\n", rdn ? rdn : "root", dn,
578 do_list( ld, fp, dn );
580 ldap_free_friendlymap( &fm );
583 static do_list( ld, fp, dn )
589 LDAPMessage *e, *res;
590 struct timeval timeout;
591 FriendlyMap *fm = NULL;
592 static char *attrs[] = { "objectClass", 0 };
593 int deref = LDAP_DEREF_FINDING;
595 timeout.tv_sec = GO500GW_TIMEOUT;
598 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
600 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
601 "(!(objectClass=dSA))", attrs, 0, &timeout, &res )) != LDAP_SUCCESS
602 && rc != LDAP_SIZELIMIT_EXCEEDED ) {
603 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
604 rc, myhost, myport );
608 deref = LDAP_DEREF_ALWAYS;
609 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
611 if ( ldap_count_entries( ld, res ) < 1 ) {
615 #ifdef GO500GW_SORT_ATTR
616 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
620 for ( e = ldap_first_entry( ld, res ); e != NULL;
621 e = ldap_next_entry( ld, e ) ) {
625 dn = ldap_get_dn( ld, e );
626 s = ldap_explode_dn( dn, 1 );
627 oc = ldap_get_values( ld, e, "objectClass" );
630 if ( strcasecmp( doc, "country" ) == 0 ) {
631 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
639 if ( strncasecmp( rdn, "{ASN}", 5 ) != 0 ) {
640 if ( isnonleaf( ld, oc, dn ) ) {
641 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", rdn,
642 doc, dn, myhost, myport );
644 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", rdn,
645 doc, dn, myhost, myport );
650 ldap_value_free( s );
651 ldap_value_free( oc );
653 ldap_free_friendlymap( &fm );
655 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
656 fprintf( fp, "0A size limit was exceeded (explanation)\tLL\t%s\t%d\r\n",
661 static isoc( ocl, oc )
667 for ( i = 0; ocl[i] != NULL; i++ ) {
668 if ( strcasecmp( ocl[i], oc ) == 0 )
675 static int make_scope( ld, dn )
682 struct timeval timeout;
683 static char *attrs[] = { "objectClass", 0 };
685 if ( strcmp( dn, "" ) == 0 )
686 return( LDAP_SCOPE_ONELEVEL );
688 timeout.tv_sec = GO500GW_TIMEOUT;
690 if ( ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
691 attrs, 0, &timeout, &res ) != LDAP_SUCCESS ) {
695 oc = ldap_get_values( ld, ldap_first_entry( ld, res ), "objectClass" );
697 if ( isoc( oc, "organization" ) || isoc( oc, "organizationalUnit" ) )
698 scope = LDAP_SCOPE_SUBTREE;
700 scope = LDAP_SCOPE_ONELEVEL;
702 ldap_value_free( oc );
708 static do_search( ld, fp, query )
718 struct timeval timeout;
720 LDAPMessage *e, *res;
722 static char *attrs[] = { "objectClass", 0 };
724 if ( (filter = strchr( query, '\t' )) == NULL ) {
725 fprintf( fp, "3Missing filter!\r\n" );
732 if ( strchr( filter, ',' ) != NULL ) {
733 ldap_ufn_setprefix( ld, base );
734 timeout.tv_sec = GO500GW_TIMEOUT;
736 ldap_ufn_timeout( (void *) &timeout );
738 deref = LDAP_DEREF_FINDING;
739 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
741 if ( (rc = ldap_ufn_search_s( ld, filter, attrs, 0, &res ))
742 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
744 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
745 rc, myhost, myport );
749 count = ldap_count_entries( ld, res );
752 if ( (scope = make_scope( ld, base )) == -1 ) {
753 fprintf( fp, "3Bad scope\r\n" );
757 filtertype = (scope == LDAP_SCOPE_ONELEVEL ?
758 "go500gw onelevel" : "go500gw subtree");
759 deref = (scope == LDAP_SCOPE_ONELEVEL ?
760 LDAP_DEREF_FINDING : LDAP_DEREF_ALWAYS);
761 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
762 timeout.tv_sec = GO500GW_TIMEOUT;
765 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
766 fprintf( stderr, "Cannot open filter file (%s)\n",
773 for ( fi = ldap_getfirstfilter( filtd, filtertype, filter );
774 fi != NULL; fi = ldap_getnextfilter( filtd ) )
776 if ( (rc = ldap_search_st( ld, base, scope,
777 fi->lfi_filter, attrs, 0, &timeout, &res ))
778 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
779 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
780 rc, myhost, myport );
783 if ( (count = ldap_count_entries( ld, res )) != 0 )
786 deref = LDAP_DEREF_ALWAYS;
787 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
788 ldap_getfilter_free( filtd );
801 e = ldap_first_entry( ld, res );
802 oc = ldap_get_values( ld, e, "objectClass" );
803 dn = ldap_get_dn( ld, e );
805 if ( isnonleaf( ld, oc, dn ) ) {
806 rc = do_menu( ld, fp, dn );
813 ldap_value_free( oc );
816 #ifdef GO500GW_SORT_ATTR
817 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
820 for ( e = ldap_first_entry( ld, res ); e != NULL;
821 e = ldap_next_entry( ld, e ) ) {
825 dn = ldap_get_dn( ld, e );
826 s = ldap_explode_dn( dn, 1 );
827 oc = ldap_get_values( ld, e, "objectClass" );
829 if ( isnonleaf( ld, oc, dn ) )
830 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", s[0],
831 pick_oc( oc ), dn, myhost, myport );
833 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", s[0],
834 pick_oc( oc ), dn, myhost, myport );
837 ldap_value_free( s );
838 ldap_value_free( oc );
841 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
842 fprintf( fp, "0A size limit was exceeded (explanation)\tLS\t%s\t%d\r\n",
849 entry2textwrite( void *fp, char *buf, int len )
851 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
854 static do_read( ld, fp, dn )
859 static struct ldap_disptmpl *tmpllist;
861 ldap_init_templates( templatefile, &tmpllist );
863 if ( ldap_entry2text_search( ld, dn, NULL, NULL, tmpllist, NULL, NULL,
864 entry2textwrite,(void *) fp, "\r\n", rdncount, 0 )
867 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
870 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
871 ld_errno, myhost, myport );
874 if ( tmpllist != NULL ) {
875 ldap_free_templates( tmpllist );
885 if ( (fp = fopen( helpfile, "r" )) == NULL ) {
886 fprintf( op, "Cannot access helpfile (%s)\r\n", helpfile );
890 while ( fgets( line, sizeof(line), fp ) != NULL ) {
891 line[ strlen( line ) - 1 ] = '\0';
893 fprintf( op, "%s\r\n", line );
899 static do_sizelimit( fp, type )
904 fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
905 fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
906 fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
907 fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
909 fprintf( fp, "Not all entries could be returned because a size limit was exceeded.\r\n" );
910 fprintf( fp, "There is no way to defeat this feature, but if you know who you are\r\n" );
911 fprintf( fp, "looking for, try choosing the \"Search\" option listed above and\r\n" );
912 fprintf( fp, "specifying the name of the person you want.\r\n" );
914 fprintf( fp, ".\r\n" );
917 static do_error( fp, 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" );