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.
16 #include <sys/types.h>
17 #include <sys/param.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
24 #include <sys/resource.h>
28 #include <sys/select.h>
33 #endif /* USE_SYSCONF */
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 SIG_FN 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;
95 /* 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 #else /* USE_SYSCONF */
158 dtblsize = getdtablesize();
159 #endif /* USE_SYSCONF */
162 if ( dtblsize > FD_SETSIZE ) {
163 dtblsize = FD_SETSIZE;
165 #endif /* FD_SETSIZE*/
169 #ifdef GO500GW_HOSTNAME
170 strcpy( myhost, GO500GW_HOSTNAME );
172 if ( myhost[0] == '\0' && gethostname( myhost, sizeof(myhost) )
174 perror( "gethostname" );
179 /* detach if stderr is redirected or no debugging */
181 (void) detach( debug );
183 if ( (myname = strrchr( argv[0], '/' )) == NULL )
184 myname = strdup( argv[0] );
186 myname = strdup( myname + 1 );
190 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL3 );
192 openlog( myname, OPENLOG_OPTIONS );
196 syslog( LOG_INFO, "initializing" );
198 /* set up the socket to listen on */
200 s = set_socket( port );
202 /* arrange to reap children */
203 (void) signal( SIGCHLD, (void *) wait4child );
207 fromlen = sizeof(from);
208 if ( getpeername( 0, (struct sockaddr *) &from, &fromlen )
210 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
211 sizeof(from.sin_addr.s_addr), AF_INET );
212 Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
213 (hp == NULL) ? "unknown" : hp->h_name,
214 inet_ntoa( from.sin_addr ), 0 );
217 syslog( LOG_INFO, "connection from %s (%s)",
218 (hp == NULL) ? "unknown" : hp->h_name,
219 inet_ntoa( from.sin_addr ) );
222 setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
235 FD_SET( s, &readfds );
237 if ( (rc = select( dtblsize, &readfds, 0, 0 ,0 )) == -1 ) {
238 if ( debug ) perror( "select" );
240 } else if ( rc == 0 ) {
244 if ( ! FD_ISSET( s, &readfds ) )
247 fromlen = sizeof(from);
248 if ( (ns = accept( s, (struct sockaddr *) &from, &fromlen ))
250 if ( debug ) perror( "accept" );
254 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
255 sizeof(from.sin_addr.s_addr), AF_INET );
258 syslog( LOG_INFO, "TCP connection from %s (%s)",
259 (hp == NULL) ? "unknown" : hp->h_name,
260 inet_ntoa( from.sin_addr ) );
263 switch( pid = fork() ) {
269 case -1: /* failed */
273 default: /* parent */
276 fprintf( stderr, "forked child %d\n", pid );
283 static set_socket( port )
287 struct sockaddr_in addr;
293 if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
298 /* set option so clients can't keep us from coming back up */
300 if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
301 sizeof(one) ) < 0 ) {
302 perror( "setsockopt" );
307 addr.sin_family = AF_INET;
308 addr.sin_addr.s_addr = INADDR_ANY;
309 addr.sin_port = htons( port );
310 if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
315 /* listen for connections */
316 if ( listen( s, 5 ) == -1 ) {
322 printf( "go500gw listening on port %d\n", port );
330 WAITSTATUSTYPE status;
332 if ( debug ) printf( "parent: catching child status\n" );
334 while (waitpid ((pid_t) -1, 0, WAIT_FLAGS) > 0)
335 #else /* USE_WAITPID */
336 while ( wait3( &status, WAIT_FLAGS, 0 ) > 0 )
337 #endif /* USE_WAITPID */
340 (void) signal( SIGCHLD, (void *) wait4child );
343 static do_queries( s )
346 char buf[1024], *query;
350 struct timeval timeout;
354 if ( (fp = fdopen( s, "a+")) == NULL ) {
359 timeout.tv_sec = GO500GW_TIMEOUT;
362 FD_SET( fileno( fp ), &readfds );
364 if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
367 if ( fgets( buf, sizeof(buf), fp ) == NULL )
372 fprintf( stderr, "got %d bytes\n", len );
374 lber_bprint( buf, len );
379 if ( buf[len - 1] == '\n' )
382 if ( buf[len - 1] == '\r' )
388 /* strip off leading white space */
389 while ( isspace( *query )) {
396 if ( *query == 'H' || *query == 'L' || *query == 'E' ) {
397 switch ( *query++ ) {
398 case 'H': /* help file */
402 case 'L': /* size limit explanation */
403 do_sizelimit( fp, *query );
406 case 'E': /* error explanation */
407 do_error( fp, query );
411 fprintf( fp, ".\r\n" );
418 if ( (ld = ldap_open( ldaphost, ldapport )) == NULL ) {
419 if ( debug ) perror( "ldap_open" );
420 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
421 LDAP_SERVER_DOWN, myhost, myport );
422 fprintf( fp, ".\r\n" );
427 ld->ld_deref = LDAP_DEREF_ALWAYS;
428 if ( !searchaliases )
429 ld->ld_deref = LDAP_DEREF_FINDING;
431 if ( (rc = ldap_simple_bind_s( ld, GO500GW_BINDDN, NULL ))
433 if ( debug ) ldap_perror( ld, "ldap_simple_bind_s" );
434 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
435 rc, myhost, myport );
436 fprintf( fp, ".\r\n" );
441 switch ( *query++ ) {
442 case 'R': /* read an entry */
443 do_read( ld, fp, query );
446 case 'S': /* search */
447 do_search( ld, fp, query, 1 );
450 case 'M': /* X.500 menu */
451 do_menu( ld, fp, query );
455 do_menu( ld, fp, "" );
459 fprintf( fp, ".\r\n" );
466 static char *pick_oc( oclist )
471 if ( oclist == NULL )
474 for ( i = 0; oclist[i] != NULL; i++ ) {
475 if ( strcasecmp( oclist[i], "top" ) != 0 &&
476 strcasecmp( oclist[i], "quipuObject" ) != 0 &&
477 strcasecmp( oclist[i], "quipuNonLeafObject" ) != 0 )
484 static isnonleaf( ld, oclist, dn )
489 int i, quipuobject = 0;
491 if ( oclist == NULL )
494 for ( i = 0; oclist[i] != NULL; i++ ) {
495 if ( strcasecmp( oclist[i], "quipuObject" ) == 0 )
497 if ( strcasecmp( oclist[i], "quipuNonLeafObject" ) == 0 ||
498 strcasecmp( oclist[i], "externalNonLeafObject" ) == 0 )
503 * not a quipu thang - no easy way to tell leaves from nonleaves
504 * except by trying to search or list. ldap only lets us search.
507 /* expensive - just guess for now */
508 return( quipuobject ? 0 : 1 );
511 if ( !quipuobject ) {
513 struct timeval timeout;
514 LDAPMessage *res = NULL;
515 static char *attrs[] = { "objectClass", 0 };
517 timeout.tv_sec = GO500GW_TIMEOUT;
519 ld->ld_sizelimit = 1;
520 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
521 "(objectClass=*)", attrs, 0, &timeout, &res ))
522 == LDAP_SUCCESS || rc == LDAP_SIZELIMIT_EXCEEDED ) {
523 ld->ld_sizelimit = LDAP_NO_LIMIT;
525 numentries = ldap_count_entries( ld, res );
528 return( numentries == 1 ? 1 : 0 );
536 static do_menu( ld, fp, dn )
543 FriendlyMap *fm = NULL;
545 if ( strcmp( dn, "" ) != 0 ) {
546 s = ldap_explode_dn( dn, 1 );
549 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
552 fprintf( fp, "0Read %s entry\tR%s\t%s\t%d\r\n", rdn ? rdn: s[0],
553 dn, myhost, myport );
555 ldap_value_free( s );
557 fprintf( fp, "0About the Gopher to X.500 Gateway\tH\t%s\t%d\r\n",
561 fprintf( fp, "7Search %s\tS%s\t%s\t%d\r\n", rdn ? rdn : "root", dn,
564 do_list( ld, fp, dn );
566 ldap_free_friendlymap( &fm );
569 static do_list( ld, fp, dn )
575 LDAPMessage *e, *res;
576 struct timeval timeout;
577 FriendlyMap *fm = NULL;
578 static char *attrs[] = { "objectClass", 0 };
580 timeout.tv_sec = GO500GW_TIMEOUT;
582 ld->ld_deref = LDAP_DEREF_FINDING;
583 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
584 "(!(objectClass=dSA))", attrs, 0, &timeout, &res )) != LDAP_SUCCESS
585 && rc != LDAP_SIZELIMIT_EXCEEDED ) {
586 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
587 rc, myhost, myport );
590 ld->ld_deref = LDAP_DEREF_ALWAYS;
592 if ( ldap_count_entries( ld, res ) < 1 ) {
596 #ifdef GO500GW_SORT_ATTR
597 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
601 for ( e = ldap_first_entry( ld, res ); e != NULL;
602 e = ldap_next_entry( ld, e ) ) {
606 dn = ldap_get_dn( ld, e );
607 s = ldap_explode_dn( dn, 1 );
608 oc = ldap_get_values( ld, e, "objectClass" );
611 if ( strcasecmp( doc, "country" ) == 0 ) {
612 rdn = ldap_friendly_name( friendlyfile, s[0], &fm );
620 if ( strncasecmp( rdn, "{ASN}", 5 ) != 0 ) {
621 if ( isnonleaf( ld, oc, dn ) ) {
622 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", rdn,
623 doc, dn, myhost, myport );
625 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", rdn,
626 doc, dn, myhost, myport );
631 ldap_value_free( s );
632 ldap_value_free( oc );
634 ldap_free_friendlymap( &fm );
636 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
637 fprintf( fp, "0A size limit was exceeded (explanation)\tLL\t%s\t%d\r\n",
642 static isoc( ocl, oc )
648 for ( i = 0; ocl[i] != NULL; i++ ) {
649 if ( strcasecmp( ocl[i], oc ) == 0 )
656 static int make_scope( ld, dn )
663 struct timeval timeout;
664 static char *attrs[] = { "objectClass", 0 };
666 if ( strcmp( dn, "" ) == 0 )
667 return( LDAP_SCOPE_ONELEVEL );
669 timeout.tv_sec = GO500GW_TIMEOUT;
671 if ( ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
672 attrs, 0, &timeout, &res ) != LDAP_SUCCESS ) {
676 oc = ldap_get_values( ld, ldap_first_entry( ld, res ), "objectClass" );
678 if ( isoc( oc, "organization" ) || isoc( oc, "organizationalUnit" ) )
679 scope = LDAP_SCOPE_SUBTREE;
681 scope = LDAP_SCOPE_ONELEVEL;
683 ldap_value_free( oc );
689 static do_search( ld, fp, query )
698 struct timeval timeout;
700 LDAPMessage *e, *res;
702 static char *attrs[] = { "objectClass", 0 };
704 if ( (filter = strchr( query, '\t' )) == NULL ) {
705 fprintf( fp, "3Missing filter!\r\n" );
712 if ( strchr( filter, ',' ) != NULL ) {
713 ldap_ufn_setprefix( ld, base );
714 timeout.tv_sec = GO500GW_TIMEOUT;
716 ldap_ufn_timeout( (void *) &timeout );
717 ld->ld_deref = LDAP_DEREF_FINDING;
719 if ( (rc = ldap_ufn_search_s( ld, filter, attrs, 0, &res ))
720 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
722 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
723 rc, myhost, myport );
727 count = ldap_count_entries( ld, res );
730 if ( (scope = make_scope( ld, base )) == -1 ) {
731 fprintf( fp, "3Bad scope\r\n" );
735 filtertype = (scope == LDAP_SCOPE_ONELEVEL ?
736 "go500gw onelevel" : "go500gw subtree");
737 ld->ld_deref = (scope == LDAP_SCOPE_ONELEVEL ?
738 LDAP_DEREF_FINDING : LDAP_DEREF_ALWAYS);
739 timeout.tv_sec = GO500GW_TIMEOUT;
742 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
743 fprintf( stderr, "Cannot open filter file (%s)\n",
750 for ( fi = ldap_getfirstfilter( filtd, filtertype, filter );
751 fi != NULL; fi = ldap_getnextfilter( filtd ) )
753 if ( (rc = ldap_search_st( ld, base, scope,
754 fi->lfi_filter, attrs, 0, &timeout, &res ))
755 != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
756 fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n",
757 rc, myhost, myport );
760 if ( (count = ldap_count_entries( ld, res )) != 0 )
763 ld->ld_deref = LDAP_DEREF_ALWAYS;
764 ldap_getfilter_free( filtd );
777 e = ldap_first_entry( ld, res );
778 oc = ldap_get_values( ld, e, "objectClass" );
779 dn = ldap_get_dn( ld, e );
781 if ( isnonleaf( ld, oc, dn ) ) {
782 rc = do_menu( ld, fp, dn );
789 ldap_value_free( oc );
792 #ifdef GO500GW_SORT_ATTR
793 ldap_sort_entries( ld, &res, GO500GW_SORT_ATTR, strcasecmp );
796 for ( e = ldap_first_entry( ld, res ); e != NULL;
797 e = ldap_next_entry( ld, e ) ) {
801 dn = ldap_get_dn( ld, e );
802 s = ldap_explode_dn( dn, 1 );
803 oc = ldap_get_values( ld, e, "objectClass" );
805 if ( isnonleaf( ld, oc, dn ) )
806 fprintf( fp, "1%s (%s)\tM%s\t%s\t%d\r\n", s[0],
807 pick_oc( oc ), dn, myhost, myport );
809 fprintf( fp, "0%s (%s)\tR%s\t%s\t%d\r\n", s[0],
810 pick_oc( oc ), dn, myhost, myport );
813 ldap_value_free( s );
814 ldap_value_free( oc );
817 if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
818 fprintf( fp, "0A size limit was exceeded (explanation)\tLS\t%s\t%d\r\n",
825 entry2textwrite( void *fp, char *buf, int len )
827 return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
830 static do_read( ld, fp, dn )
835 static struct ldap_disptmpl *tmpllist;
837 ldap_init_templates( templatefile, &tmpllist );
839 if ( ldap_entry2text_search( ld, dn, NULL, NULL, tmpllist, NULL, NULL,
840 entry2textwrite,(void *) fp, "\r\n", rdncount, 0 )
843 "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
844 ld->ld_errno, myhost, myport );
847 if ( tmpllist != NULL ) {
848 ldap_free_templates( tmpllist );
858 if ( (fp = fopen( helpfile, "r" )) == NULL ) {
859 fprintf( op, "Cannot access helpfile (%s)\r\n", helpfile );
863 while ( fgets( line, sizeof(line), fp ) != NULL ) {
864 line[ strlen( line ) - 1 ] = '\0';
866 fprintf( op, "%s\r\n", line );
872 static do_sizelimit( fp, type )
877 fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
878 fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
879 fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
880 fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
882 fprintf( fp, "Not all entries could be returned because a size limit was exceeded.\r\n" );
883 fprintf( fp, "There is no way to defeat this feature, but if you know who you are\r\n" );
884 fprintf( fp, "looking for, try choosing the \"Search\" option listed above and\r\n" );
885 fprintf( fp, "specifying the name of the person you want.\r\n" );
887 fprintf( fp, ".\r\n" );
890 static do_error( fp, s )
898 fprintf( fp, "An error occurred searching X.500. The error code was %d\r\n", code );
899 fprintf( fp, "The corresponding error is: %s\r\n", ldap_err2string( code ) );
900 fprintf( fp, "No additional information is available\r\n" );
901 fprintf( fp, ".\r\n" );