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>
40 #include "ldapconfig.h"
47 char *ldaphost = LDAPHOST;
48 int ldapport = LDAP_PORT;
49 int searchaliases = 1;
50 char *helpfile = GO500GW_HELPFILE;
51 char *filterfile = FILTERFILE;
52 char *templatefile = TEMPLATEFILE;
53 char *friendlyfile = FRIENDLYFILE;
54 int rdncount = GO500GW_RDNCOUNT;
57 static RETSIGTYPE wait4child();
64 static do_sizelimit();
66 extern int strcasecmp();
68 char myhost[MAXHOSTNAMELEN];
69 int myport = GO500GW_PORT;
74 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 );
88 struct sockaddr_in from;
90 RETSIGTYPE wait4child();
93 #if defined( LDAP_PROCTITLE ) && !defined( HAVE_SETPROCTITLE )
94 /* for setproctitle */
99 while ( (i = getopt( argc, argv, "P:ad:f:h:lp:t:x:Ic:" )) != EOF ) {
101 case 'a': /* search aliases */
105 case 'd': /* debugging level */
106 debug = atoi( optarg );
110 fprintf( stderr, "warning: ldap debugging requires LDAP_DEBUG\n" );
114 case 'f': /* ldap filter file */
115 filterfile = strdup( optarg );
118 case 'h': /* gopher help file */
119 helpfile = strdup( optarg );
122 case 'l': /* log to LOG_LOCAL3 */
126 case 'p': /* port to listen on */
127 port = atoi( optarg );
130 case 'P': /* port to connect to ldap server */
131 ldapport = atoi( optarg );
134 case 't': /* ldap template file */
135 templatefile = strdup( optarg );
138 case 'x': /* ldap server hostname */
139 ldaphost = strdup( optarg );
142 case 'I': /* run from inetd */
146 case 'c': /* count of DN components to show */
147 rdncount = atoi( optarg );
156 dtblsize = sysconf( _SC_OPEN_MAX );
157 #elif HAVE_GETDTABLESIZE
158 dtblsize = getdtablesize();
160 dtblsize = FD_SETSIZE;
164 if ( dtblsize > FD_SETSIZE ) {
165 dtblsize = FD_SETSIZE;
167 #endif /* FD_SETSIZE*/
171 #ifdef GO500GW_HOSTNAME
172 strcpy( myhost, GO500GW_HOSTNAME );
174 if ( myhost[0] == '\0' && gethostname( myhost, sizeof(myhost) )
176 perror( "gethostname" );
181 /* detach if stderr is redirected or no debugging */
183 (void) detach( debug );
185 if ( (myname = strrchr( argv[0], '/' )) == NULL )
186 myname = strdup( argv[0] );
188 myname = strdup( myname + 1 );
192 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL3 );
194 openlog( myname, OPENLOG_OPTIONS );
198 syslog( LOG_INFO, "initializing" );
200 /* set up the socket to listen on */
202 s = set_socket( port );
204 /* arrange to reap children */
205 (void) SIGNAL( SIGCHLD, wait4child );
209 fromlen = sizeof(from);
210 if ( getpeername( 0, (struct sockaddr *) &from, &fromlen )
212 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
213 sizeof(from.sin_addr.s_addr), AF_INET );
214 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
215 (hp == NULL) ? "unknown" : hp->h_name,
216 inet_ntoa( from.sin_addr ), 0 );
219 syslog( LOG_INFO, "connection from %s (%s)",
220 (hp == NULL) ? "unknown" : hp->h_name,
221 inet_ntoa( from.sin_addr ) );
224 #ifdef LDAP_PROCTITLE
225 setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
239 FD_SET( s, &readfds );
241 if ( (rc = select( dtblsize, &readfds, 0, 0 ,0 )) == -1 ) {
242 if ( debug ) perror( "select" );
244 } else if ( rc == 0 ) {
248 if ( ! FD_ISSET( s, &readfds ) )
251 fromlen = sizeof(from);
252 if ( (ns = accept( s, (struct sockaddr *) &from, &fromlen ))
254 if ( debug ) perror( "accept" );
258 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
259 sizeof(from.sin_addr.s_addr), AF_INET );
262 syslog( LOG_INFO, "TCP connection from %s (%s)",
263 (hp == NULL) ? "unknown" : hp->h_name,
264 inet_ntoa( from.sin_addr ) );
267 switch( pid = fork() ) {
273 case -1: /* failed */
277 default: /* parent */
280 fprintf( stderr, "forked child %d\n", pid );
287 static set_socket( port )
291 struct sockaddr_in addr;
297 if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
302 /* set option so clients can't keep us from coming back up */
304 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
305 sizeof(one) ) < 0 ) {
306 perror( "setsockopt" );
311 addr.sin_family = AF_INET;
312 addr.sin_addr.s_addr = INADDR_ANY;
313 addr.sin_port = htons( port );
314 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
319 /* listen for connections */
320 if ( listen( s, 5 ) == -1 ) {
326 printf( "go500gw listening on port %d\n", port );
335 WAITSTATUSTYPE status;
338 if ( debug ) printf( "parent: catching child status\n" );
341 while (waitpid ((pid_t) -1, NULL, WAIT_FLAGS) > 0)
344 while (wait3( &status, WAIT_FLAGS, 0 ) > 0 )
348 (void) SIGNAL( SIGCHLD, wait4child );
351 static do_queries( s )
354 char buf[1024], *query;
359 struct timeval timeout;
363 if ( (fp = fdopen( s, "a+")) == NULL ) {
368 timeout.tv_sec = GO500GW_TIMEOUT;
371 FD_SET( fileno( fp ), &readfds );
373 if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
376 if ( fgets( buf, sizeof(buf), fp ) == NULL )
381 fprintf( stderr, "got %d bytes\n", len );
383 lber_bprint( buf, len );
388 if ( buf[len - 1] == '\n' )
391 if ( buf[len - 1] == '\r' )
397 /* strip off leading white space */
398 while ( isspace( *query )) {
405 if ( *query == 'H' || *query == 'L' || *query == 'E' ) {
406 switch ( *query++ ) {
407 case 'H': /* help file */
411 case 'L': /* size limit explanation */
412 do_sizelimit( fp, *query );
415 case 'E': /* error explanation */
416 do_error( fp, query );
420 fprintf( fp, ".\r\n" );
427 if ( (ld = ldap_open( ldaphost, ldapport )) == NULL ) {
428 if ( debug ) perror( "ldap_open" );
429 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
430 LDAP_SERVER_DOWN, myhost, myport );
431 fprintf( fp, ".\r\n" );
436 deref = LDAP_DEREF_ALWAYS;
437 if ( !searchaliases )
438 deref = LDAP_DEREF_FINDING;
440 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
442 if ( (rc = ldap_simple_bind_s( ld, GO500GW_BINDDN, NULL ))
444 if ( debug ) ldap_perror( ld, "ldap_simple_bind_s" );
445 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
446 rc, myhost, myport );
447 fprintf( fp, ".\r\n" );
452 switch ( *query++ ) {
453 case 'R': /* read an entry */
454 do_read( ld, fp, query );
457 case 'S': /* search */
458 do_search( ld, fp, query, 1 );
461 case 'M': /* X.500 menu */
462 do_menu( ld, fp, query );
466 do_menu( ld, fp, "" );
470 fprintf( fp, ".\r\n" );
477 static char *pick_oc( oclist )
482 if ( oclist == NULL )
485 for ( i = 0; oclist[i] != NULL; i++ ) {
486 if ( strcasecmp( oclist[i], "top" ) != 0 &&
487 strcasecmp( oclist[i], "quipuObject" ) != 0 &&
488 strcasecmp( oclist[i], "quipuNonLeafObject" ) != 0 )
495 static isnonleaf( ld, oclist, dn )
500 int i, quipuobject = 0;
502 if ( oclist == NULL )
505 for ( i = 0; oclist[i] != NULL; i++ ) {
506 if ( strcasecmp( oclist[i], "quipuObject" ) == 0 )
508 if ( strcasecmp( oclist[i], "quipuNonLeafObject" ) == 0 ||
509 strcasecmp( oclist[i], "externalNonLeafObject" ) == 0 )
514 * not a quipu thang - no easy way to tell leaves from nonleaves
515 * except by trying to search or list. ldap only lets us search.
518 /* expensive - just guess for now */
519 return( quipuobject ? 0 : 1 );
522 if ( !quipuobject ) {
524 struct timeval timeout;
525 LDAPMessage *res = NULL;
526 static char *attrs[] = { "objectClass", 0 };
529 timeout.tv_sec = GO500GW_TIMEOUT;
531 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
532 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
533 "(objectClass=*)", attrs, 0, &timeout, &res ))
534 == LDAP_SUCCESS || rc == LDAP_SIZELIMIT_EXCEEDED ) {
535 sizelimit = LDAP_NO_LIMIT;
536 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
538 numentries = ldap_count_entries( ld, res );
541 return( numentries == 1 ? 1 : 0 );
549 static do_menu( ld, fp, dn )
556 FriendlyMap *fm = NULL;
558 if ( strcmp( dn, "" ) != 0 ) {
559 s = ldap_explode_dn( dn, 1 );
562 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
565 fprintf( fp, "0Read %s entry\tR%s\t%s\t%d\r\n", rdn ? rdn: s[0],
566 dn, myhost, myport );
568 ldap_value_free( s );
570 fprintf( fp, "0About the Gopher to X.500 Gateway\tH\t%s\t%d\r\n",
574 fprintf( fp, "7Search %s\tS%s\t%s\t%d\r\n", rdn ? rdn : "root", dn,
577 do_list( ld, fp, dn );
579 ldap_free_friendlymap( &fm );
582 static do_list( ld, fp, dn )
588 LDAPMessage *e, *res;
589 struct timeval timeout;
590 FriendlyMap *fm = NULL;
591 static char *attrs[] = { "objectClass", 0 };
592 int deref = LDAP_DEREF_FINDING;
594 timeout.tv_sec = GO500GW_TIMEOUT;
597 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
599 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
600 "(!(objectClass=dSA))", attrs, 0, &timeout, &res )) != LDAP_SUCCESS
601 && rc != LDAP_SIZELIMIT_EXCEEDED ) {
602 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
603 rc, myhost, myport );
607 deref = LDAP_DEREF_ALWAYS;
608 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
610 if ( ldap_count_entries( ld, res ) < 1 ) {
614 #ifdef GO500GW_SORT_ATTR
615 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
619 for ( e = ldap_first_entry( ld, res ); e != NULL;
620 e = ldap_next_entry( ld, e ) ) {
624 dn = ldap_get_dn( ld, e );
625 s = ldap_explode_dn( dn, 1 );
626 oc = ldap_get_values( ld, e, "objectClass" );
629 if ( strcasecmp( doc, "country" ) == 0 ) {
630 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
638 if ( strncasecmp( rdn, "{ASN}", 5 ) != 0 ) {
639 if ( isnonleaf( ld, oc, dn ) ) {
640 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", rdn,
641 doc, dn, myhost, myport );
643 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", rdn,
644 doc, dn, myhost, myport );
649 ldap_value_free( s );
650 ldap_value_free( oc );
652 ldap_free_friendlymap( &fm );
654 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
655 fprintf( fp, "0A size limit was exceeded (explanation)\tLL\t%s\t%d\r\n",
660 static isoc( ocl, oc )
666 for ( i = 0; ocl[i] != NULL; i++ ) {
667 if ( strcasecmp( ocl[i], oc ) == 0 )
674 static int make_scope( ld, dn )
681 struct timeval timeout;
682 static char *attrs[] = { "objectClass", 0 };
684 if ( strcmp( dn, "" ) == 0 )
685 return( LDAP_SCOPE_ONELEVEL );
687 timeout.tv_sec = GO500GW_TIMEOUT;
689 if ( ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
690 attrs, 0, &timeout, &res ) != LDAP_SUCCESS ) {
694 oc = ldap_get_values( ld, ldap_first_entry( ld, res ), "objectClass" );
696 if ( isoc( oc, "organization" ) || isoc( oc, "organizationalUnit" ) )
697 scope = LDAP_SCOPE_SUBTREE;
699 scope = LDAP_SCOPE_ONELEVEL;
701 ldap_value_free( oc );
707 static do_search( ld, fp, query )
717 struct timeval timeout;
719 LDAPMessage *e, *res;
721 static char *attrs[] = { "objectClass", 0 };
723 if ( (filter = strchr( query, '\t' )) == NULL ) {
724 fprintf( fp, "3Missing filter!\r\n" );
731 if ( strchr( filter, ',' ) != NULL ) {
732 ldap_ufn_setprefix( ld, base );
733 timeout.tv_sec = GO500GW_TIMEOUT;
735 ldap_ufn_timeout( (void *) &timeout );
737 deref = LDAP_DEREF_FINDING;
738 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
740 if ( (rc = ldap_ufn_search_s( ld, filter, attrs, 0, &res ))
741 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
743 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
744 rc, myhost, myport );
748 count = ldap_count_entries( ld, res );
751 if ( (scope = make_scope( ld, base )) == -1 ) {
752 fprintf( fp, "3Bad scope\r\n" );
756 filtertype = (scope == LDAP_SCOPE_ONELEVEL ?
757 "go500gw onelevel" : "go500gw subtree");
758 deref = (scope == LDAP_SCOPE_ONELEVEL ?
759 LDAP_DEREF_FINDING : LDAP_DEREF_ALWAYS);
760 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
761 timeout.tv_sec = GO500GW_TIMEOUT;
764 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
765 fprintf( stderr, "Cannot open filter file (%s)\n",
772 for ( fi = ldap_getfirstfilter( filtd, filtertype, filter );
773 fi != NULL; fi = ldap_getnextfilter( filtd ) )
775 if ( (rc = ldap_search_st( ld, base, scope,
776 fi->lfi_filter, attrs, 0, &timeout, &res ))
777 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
778 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
779 rc, myhost, myport );
782 if ( (count = ldap_count_entries( ld, res )) != 0 )
785 deref = LDAP_DEREF_ALWAYS;
786 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
787 ldap_getfilter_free( filtd );
800 e = ldap_first_entry( ld, res );
801 oc = ldap_get_values( ld, e, "objectClass" );
802 dn = ldap_get_dn( ld, e );
804 if ( isnonleaf( ld, oc, dn ) ) {
805 rc = do_menu( ld, fp, dn );
812 ldap_value_free( oc );
815 #ifdef GO500GW_SORT_ATTR
816 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
819 for ( e = ldap_first_entry( ld, res ); e != NULL;
820 e = ldap_next_entry( ld, e ) ) {
824 dn = ldap_get_dn( ld, e );
825 s = ldap_explode_dn( dn, 1 );
826 oc = ldap_get_values( ld, e, "objectClass" );
828 if ( isnonleaf( ld, oc, dn ) )
829 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", s[0],
830 pick_oc( oc ), dn, myhost, myport );
832 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", s[0],
833 pick_oc( oc ), dn, myhost, myport );
836 ldap_value_free( s );
837 ldap_value_free( oc );
840 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
841 fprintf( fp, "0A size limit was exceeded (explanation)\tLS\t%s\t%d\r\n",
848 entry2textwrite( void *fp, char *buf, int len )
850 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
853 static do_read( ld, fp, dn )
858 static struct ldap_disptmpl *tmpllist;
860 ldap_init_templates( templatefile, &tmpllist );
862 if ( ldap_entry2text_search( ld, dn, NULL, NULL, tmpllist, NULL, NULL,
863 entry2textwrite,(void *) fp, "\r\n", rdncount, 0 )
866 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
869 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
870 ld_errno, myhost, myport );
873 if ( tmpllist != NULL ) {
874 ldap_free_templates( tmpllist );
884 if ( (fp = fopen( helpfile, "r" )) == NULL ) {
885 fprintf( op, "Cannot access helpfile (%s)\r\n", helpfile );
889 while ( fgets( line, sizeof(line), fp ) != NULL ) {
890 line[ strlen( line ) - 1 ] = '\0';
892 fprintf( op, "%s\r\n", line );
898 static do_sizelimit( fp, type )
903 fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
904 fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
905 fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
906 fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
908 fprintf( fp, "Not all entries could be returned because a size limit was exceeded.\r\n" );
909 fprintf( fp, "There is no way to defeat this feature, but if you know who you are\r\n" );
910 fprintf( fp, "looking for, try choosing the \"Search\" option listed above and\r\n" );
911 fprintf( fp, "specifying the name of the person you want.\r\n" );
913 fprintf( fp, ".\r\n" );
916 static do_error( fp, s )
924 fprintf( fp, "An error occurred searching X.500. The error code was %d\r\n", code );
925 fprintf( fp, "The corresponding error is: %s\r\n", ldap_err2string( code ) );
926 fprintf( fp, "No additional information is available\r\n" );
927 fprintf( fp, ".\r\n" );