X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=clients%2Fgopher%2Fgo500gw.c;h=c1c5ab4a9ef47c556e3823f66d3ce195aedeb13f;hb=403f4479bc9f9a864122d4aeecf7284408918302;hp=bf25933d3fda1d61f6a4e5a060a593510e240411;hpb=f3db3e0a8f1ddcb5af7d0de35c827048ebfa562d;p=openldap diff --git a/clients/gopher/go500gw.c b/clients/gopher/go500gw.c index bf25933d3f..c1c5ab4a9e 100644 --- a/clients/gopher/go500gw.c +++ b/clients/gopher/go500gw.c @@ -1,3 +1,4 @@ +/* $OpenLDAP$ */ /* * Copyright (c) 1990 Regents of the University of Michigan. * All rights reserved. @@ -14,6 +15,8 @@ #include +#include + #include #include #include @@ -25,28 +28,35 @@ #include -#include - #ifdef HAVE_SYS_PARAM_H #include #endif -#include "lber.h" -#include "ldap.h" +#ifdef HAVE_SYS_RESOURCE_H +#include +#endif + + +#include +#include + +#define ldap_debug debug #include "ldap_log.h" + #include "lutil.h" -#include "disptmpl.h" -#include "ldapconfig.h" +#include "ldap_defaults.h" int debug; +int ldap_syslog; +int ldap_syslog_level; int dosyslog; int inetd; int dtblsize; -char *ldaphost = LDAPHOST; -int ldapport = LDAP_PORT; +char *ldaphost = NULL; +int ldapport = 0; int searchaliases = 1; char *helpfile = GO500GW_HELPFILE; char *filterfile = FILTERFILE; @@ -54,31 +64,35 @@ char *templatefile = TEMPLATEFILE; char *friendlyfile = FRIENDLYFILE; int rdncount = GO500GW_RDNCOUNT; -static set_socket(); -static RETSIGTYPE wait4child(); -static do_queries(); -static do_menu(); -static do_list(); -static do_search(); -static do_read(); -static do_help(); -static do_sizelimit(); -static do_error(); -extern int strcasecmp(); +static void usage ( char *name ) LDAP_GCCATTR((noreturn)); +static int set_socket (int port); +static RETSIGTYPE wait4child(int sig); +static void do_queries (int s) LDAP_GCCATTR((noreturn)); +static char *pick_oc ( char **oclist ); +static int isnonleaf ( LDAP *ld, char **oclist, char *dn ); +static void do_menu (LDAP *ld, FILE *fp, char *dn); +static void do_list (LDAP *ld, FILE *fp, char *dn); +static int isoc ( char **ocl, char *oc ); +static int make_scope ( LDAP *ld, char *dn ); +static void do_search (LDAP *ld, FILE *fp, char *query); +static int entry2textwrite( void *fp, char *buf, ber_len_t len ); +static void do_read (LDAP *ld, FILE *fp, char *dn); +static void do_help (FILE *op); +static void do_sizelimit(FILE *fp, char type); +static void do_error (FILE *fp, char *s); char myhost[MAXHOSTNAMELEN]; int myport = GO500GW_PORT; -static usage( name ) -char *name; +static void +usage( char *name ) { 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 ); - exit( 1 ); + exit( EXIT_FAILURE ); } -main (argc, argv) -int argc; -char **argv; +int +main (int argc, char **argv ) { int s, ns, rc; int port = -1; @@ -87,9 +101,7 @@ char **argv; fd_set readfds; struct hostent *hp; struct sockaddr_in from; - int fromlen; - RETSIGTYPE wait4child(); - extern char *optarg; + socklen_t fromlen; #if defined( LDAP_PROCTITLE ) && !defined( HAVE_SETPROCTITLE ) /* for setproctitle */ @@ -104,12 +116,7 @@ char **argv; break; case 'd': /* debugging level */ - debug = atoi( optarg ); -#ifdef LDAP_DEBUG - ldap_debug = debug; -#else - fprintf( stderr, "warning: ldap debugging requires LDAP_DEBUG\n" ); -#endif + debug |= atoi( optarg ); break; case 'f': /* ldap filter file */ @@ -175,7 +182,7 @@ char **argv; if ( myhost[0] == '\0' && gethostname( myhost, sizeof(myhost) ) == -1 ) { perror( "gethostname" ); - exit( 1 ); + exit( EXIT_FAILURE ); } #endif @@ -188,6 +195,15 @@ char **argv; else myname = strdup( myname + 1 ); + if ( debug ) { + ber_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug); + ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug); + } + +#ifdef SIGPIPE + (void) SIGNAL( SIGPIPE, SIG_IGN ); +#endif + if ( dosyslog ) { #ifdef LOG_LOCAL3 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL3 ); @@ -230,9 +246,9 @@ char **argv; do_queries( 0 ); - close( 0 ); + tcp_close( 0 ); - exit( 0 ); + exit( EXIT_SUCCESS ); } for ( ;; ) { @@ -253,7 +269,7 @@ char **argv; if ( (ns = accept( s, (struct sockaddr *) &from, &fromlen )) == -1 ) { if ( debug ) perror( "accept" ); - exit( 1 ); + exit( EXIT_FAILURE ); } hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr), @@ -267,7 +283,7 @@ char **argv; switch( pid = fork() ) { case 0: /* child */ - close( s ); + tcp_close( s ); do_queries( ns ); break; @@ -276,7 +292,7 @@ char **argv; break; default: /* parent */ - close( ns ); + tcp_close( ns ); if ( debug ) fprintf( stderr, "forked child %d\n", pid ); break; @@ -285,8 +301,8 @@ char **argv; /* NOT REACHED */ } -static set_socket( port ) -int port; +static int +set_socket( int port ) { int s, one; struct sockaddr_in addr; @@ -297,30 +313,41 @@ int port; if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) { perror( "socket" ); - exit( 1 ); + exit( EXIT_FAILURE ); } +#ifdef SO_REUSEADDR /* set option so clients can't keep us from coming back up */ - one = 1; + one = 1; if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one) ) < 0 ) { perror( "setsockopt" ); - exit( 1 ); + exit( EXIT_FAILURE ); + } +#endif +#ifdef SO_KEEPALIVE + /* enable keep alives */ + one = 1; + if ( setsockopt( s, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, + sizeof(one) ) < 0 ) { + perror( "setsockopt" ); + exit( EXIT_FAILURE ); } +#endif /* bind to a name */ addr.sin_family = AF_INET; - addr.sin_addr.s_addr = INADDR_ANY; + addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons( port ); if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) { perror( "bind" ); - exit( 1 ); + exit( EXIT_FAILURE ); } /* listen for connections */ if ( listen( s, 5 ) == -1 ) { perror( "listen" ); - exit( 1 ); + exit( EXIT_FAILURE ); } if ( debug ) @@ -330,7 +357,7 @@ int port; } static RETSIGTYPE -wait4child() +wait4child( int sig ) { #ifndef HAVE_WAITPID WAITSTATUSTYPE status; @@ -339,18 +366,18 @@ wait4child() if ( debug ) printf( "parent: catching child status\n" ); #ifdef HAVE_WAITPID - while (waitpid ((pid_t) -1, NULL, WAIT_FLAGS) > 0) + while (waitpid ((pid_t) -1, (int *) NULL, WAIT_FLAGS) > 0) ; /* NULL */ #else - while (wait3( &status, WAIT_FLAGS, 0 ) > 0 ) + while (wait4( (pid_t) -1, &status, WAIT_FLAGS, 0 ) > 0 ) ; /* NULL */ #endif (void) SIGNAL( SIGCHLD, wait4child ); } -static do_queries( s ) -int s; +static void +do_queries( int s ) { char buf[1024], *query; int len; @@ -363,7 +390,7 @@ int s; if ( (fp = fdopen( s, "a+")) == NULL ) { perror( "fdopen" ); - exit( 1 ); + exit( EXIT_FAILURE ); } timeout.tv_sec = GO500GW_TIMEOUT; @@ -372,16 +399,16 @@ int s; FD_SET( fileno( fp ), &readfds ); if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 ) - exit( 1 ); + exit( EXIT_FAILURE ); if ( fgets( buf, sizeof(buf), fp ) == NULL ) - exit( 1 ); + exit( EXIT_FAILURE ); len = strlen( buf ); if ( debug ) { fprintf( stderr, "got %d bytes\n", len ); #ifdef LDAP_DEBUG - lber_bprint( buf, len ); + ber_bprint( buf, len ); #endif } @@ -396,7 +423,7 @@ int s; query = buf; /* strip off leading white space */ - while ( isspace( *query )) { + while ( isspace( (unsigned char) *query )) { ++query; --len; } @@ -421,17 +448,17 @@ int s; fprintf( fp, ".\r\n" ); rewind(fp); - exit( 0 ); + exit( EXIT_SUCCESS ); /* NOT REACHED */ } - if ( (ld = ldap_open( ldaphost, ldapport )) == NULL ) { - if ( debug ) perror( "ldap_open" ); + if ( (ld = ldap_init( ldaphost, ldapport )) == NULL ) { + if ( debug ) perror( "ldap_init" ); fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n", LDAP_SERVER_DOWN, myhost, myport ); fprintf( fp, ".\r\n" ); rewind(fp); - exit( 1 ); + exit( EXIT_FAILURE ); } deref = LDAP_DEREF_ALWAYS; @@ -440,14 +467,14 @@ int s; ldap_set_option(ld, LDAP_OPT_DEREF, &deref); - if ( (rc = ldap_simple_bind_s( ld, GO500GW_BINDDN, NULL )) + if ( (rc = ldap_simple_bind_s( ld, NULL, NULL )) != LDAP_SUCCESS ) { if ( debug ) ldap_perror( ld, "ldap_simple_bind_s" ); fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n", rc, myhost, myport ); fprintf( fp, ".\r\n" ); rewind(fp); - exit( 1 ); + exit( EXIT_FAILURE ); } switch ( *query++ ) { @@ -456,7 +483,7 @@ int s; break; case 'S': /* search */ - do_search( ld, fp, query, 1 ); + do_search( ld, fp, query ); break; case 'M': /* X.500 menu */ @@ -471,12 +498,12 @@ int s; fprintf( fp, ".\r\n" ); rewind(fp); - exit( 0 ); + exit( EXIT_SUCCESS ); /* NOT REACHED */ } -static char *pick_oc( oclist ) -char **oclist; +static char * +pick_oc( char **oclist ) { int i; @@ -493,10 +520,8 @@ char **oclist; return( "unknown" ); } -static isnonleaf( ld, oclist, dn ) -LDAP *ld; -char **oclist; -char *dn; +static int +isnonleaf( LDAP *ld, char **oclist, char *dn ) { int i, quipuobject = 0; @@ -547,14 +572,12 @@ char *dn; #endif } -static do_menu( ld, fp, dn ) -LDAP *ld; -FILE *fp; -char *dn; +static void +do_menu( LDAP *ld, FILE *fp, char *dn ) { char **s; char *rdn = NULL; - FriendlyMap *fm = NULL; + LDAPFriendlyMap *fm = NULL; if ( strcmp( dn, "" ) != 0 ) { s = ldap_explode_dn( dn, 1 ); @@ -580,15 +603,13 @@ char *dn; ldap_free_friendlymap( &fm ); } -static do_list( ld, fp, dn ) -LDAP *ld; -FILE *fp; -char *dn; +static void +do_list( LDAP *ld, FILE *fp, char *dn ) { int rc; LDAPMessage *e, *res; struct timeval timeout; - FriendlyMap *fm = NULL; + LDAPFriendlyMap *fm = NULL; static char *attrs[] = { "objectClass", 0 }; int deref = LDAP_DEREF_FINDING; @@ -658,9 +679,8 @@ char *dn; } } -static isoc( ocl, oc ) -char **ocl; -char *oc; +static int +isoc( char **ocl, char *oc ) { int i; @@ -672,9 +692,8 @@ char *oc; return( 0 ); } -static int make_scope( ld, dn ) -LDAP *ld; -char *dn; +static int +make_scope( LDAP *ld, char *dn ) { int scope; char **oc; @@ -705,10 +724,8 @@ char *dn; return( scope ); } -static do_search( ld, fp, query ) -LDAP *ld; -FILE *fp; -char *query; +static void +do_search( LDAP *ld, FILE *fp, char *query ) { int deref; int scope; @@ -723,7 +740,7 @@ char *query; if ( (filter = strchr( query, '\t' )) == NULL ) { fprintf( fp, "3Missing filter!\r\n" ); - exit( 1 ); + exit( EXIT_FAILURE ); } *filter++ = '\0'; base = query; @@ -751,7 +768,7 @@ char *query; #endif if ( (scope = make_scope( ld, base )) == -1 ) { fprintf( fp, "3Bad scope\r\n" ); - exit( 1 ); + exit( EXIT_FAILURE ); } filtertype = (scope == LDAP_SCOPE_ONELEVEL ? @@ -765,7 +782,7 @@ char *query; if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) { fprintf( stderr, "Cannot open filter file (%s)\n", filterfile ); - exit( 1 ); + exit( EXIT_FAILURE ); } count = 0; @@ -778,7 +795,7 @@ char *query; != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) { fprintf(fp, "0An error occurred (explanation)\tE%d\t%s\t%d\r\n", rc, myhost, myport ); - return( 1 ); + return; } if ( (count = ldap_count_entries( ld, res )) != 0 ) break; @@ -791,22 +808,21 @@ char *query; #endif if ( count == 0 ) { - return( 0 ); + return; } if ( count == 1 ) { - char *dn, **s, **oc; - int rc; + char *dn, **oc; e = ldap_first_entry( ld, res ); oc = ldap_get_values( ld, e, "objectClass" ); dn = ldap_get_dn( ld, e ); if ( isnonleaf( ld, oc, dn ) ) { - rc = do_menu( ld, fp, dn ); + do_menu( ld, fp, dn ); free( dn ); - return( rc ); + return; } free( dn ); @@ -846,15 +862,13 @@ char *query; static int -entry2textwrite( void *fp, char *buf, int len ) +entry2textwrite( void *fp, char *buf, ber_len_t len ) { return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len ); } -static do_read( ld, fp, dn ) -LDAP *ld; -FILE *fp; -char *dn; +static void +do_read( LDAP *ld, FILE *fp, char *dn ) { static struct ldap_disptmpl *tmpllist; @@ -867,8 +881,8 @@ char *dn; ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno); fprintf(fp, - "0An error occurred (explanation)\t@%d\t%s\t%d\r\n", - ld_errno, myhost, myport ); + "0An error occurred (explanation)\t@%s\t%s\t%d\r\n", + ldap_err2string( ld_errno ), myhost, myport ); } if ( tmpllist != NULL ) { @@ -876,8 +890,8 @@ char *dn; } } -static do_help( op ) -FILE *op; +static void +do_help( FILE *op ) { FILE *fp; char line[BUFSIZ]; @@ -896,9 +910,8 @@ FILE *op; fclose( fp ); } -static do_sizelimit( fp, type ) -FILE *fp; -char type; +static void +do_sizelimit( FILE *fp, char type ) { if ( type == 'S' ) { fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" ); @@ -914,9 +927,8 @@ char type; fprintf( fp, ".\r\n" ); } -static do_error( fp, s ) -FILE *fp; -char *s; +static void +do_error( FILE *fp, char *s ) { int code;