]> git.sur5r.net Git - openldap/blobdiff - clients/gopher/go500gw.c
initialize Sockbuf * to NULL
[openldap] / clients / gopher / go500gw.c
index 9f8935fcd57f30a0a47888a24023b02559ea4fcc..efe5054d811d97c2bcb67c7e32d0f6f0c11fe69b 100644 (file)
@@ -1,3 +1,4 @@
+/* $OpenLDAP$ */
 /*
  * Copyright (c) 1990 Regents of the University of Michigan.
  * All rights reserved.
 #include <ac/unistd.h>
 #include <ac/wait.h>
 
+#include <ac/param.h>
 #include <ac/setproctitle.h>
 
-#ifdef HAVE_SYS_PARAM_H
-#include <sys/param.h>
-#endif
-
 #ifdef HAVE_SYS_RESOURCE_H
 #include <sys/resource.h>
 #endif
 
 
-#include "lber.h"
-#include "ldap.h"
+#include <ldap.h>
+#include <disptmpl.h>
 
 #define ldap_debug debug
 #include "ldap_log.h"
 
 #include "lutil.h"
 
-#include "disptmpl.h"
 
 #include "ldap_defaults.h"
 
@@ -64,10 +61,10 @@ char                *templatefile = TEMPLATEFILE;
 char           *friendlyfile = FRIENDLYFILE;
 int            rdncount = GO500GW_RDNCOUNT;
 
-static void usage      ( char *name );
+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);
+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);
@@ -88,7 +85,7 @@ 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 );
 }
 
 int
@@ -101,7 +98,7 @@ main (int  argc, char **argv )
        fd_set                  readfds;
        struct hostent          *hp;
        struct sockaddr_in      from;
-       int                     fromlen;
+       socklen_t               fromlen;
 
 #if defined( LDAP_PROCTITLE ) && !defined( HAVE_SETPROCTITLE )
        /* for setproctitle */
@@ -179,11 +176,12 @@ main (int  argc, char **argv )
 #ifdef GO500GW_HOSTNAME
        strcpy( myhost, GO500GW_HOSTNAME );
 #else
-       if ( myhost[0] == '\0' && gethostname( myhost, sizeof(myhost) )
+       if ( myhost[0] == '\0' && gethostname( myhost, sizeof(myhost)-1 )
            == -1 ) {
                perror( "gethostname" );
-               exit( 1 );
+               exit( EXIT_FAILURE );
        }
+       myhost[sizeof(myhost)-1] = '\0';
 #endif
 
        /* detach if stderr is redirected or no debugging */
@@ -207,7 +205,7 @@ main (int  argc, char **argv )
        if ( dosyslog ) {
 #ifdef LOG_LOCAL3
                openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL3 );
-#else
+#elif LOG_DEBUG
                openlog( myname, OPENLOG_OPTIONS );
 #endif
        }
@@ -226,8 +224,8 @@ main (int  argc, char **argv )
                fromlen = sizeof(from);
                if ( getpeername( 0, (struct sockaddr *) &from, &fromlen )
                    == 0 ) {
-                       hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
-                           sizeof(from.sin_addr.s_addr), AF_INET );
+                       hp = gethostbyaddr( (char *) &(from.sin_addr),
+                           sizeof(from.sin_addr), AF_INET );
                        Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
                            (hp == NULL) ? "unknown" : hp->h_name,
                            inet_ntoa( from.sin_addr ), 0 );
@@ -239,7 +237,7 @@ main (int  argc, char **argv )
                        }
 
 #ifdef LDAP_PROCTITLE
-                       setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
+                       setproctitle( "%s", hp == NULL ? inet_ntoa( from.sin_addr ) :
                            hp->h_name );
 #endif
                }
@@ -248,7 +246,7 @@ main (int  argc, char **argv )
 
                tcp_close( 0 );
 
-               exit( 0 );
+               exit( EXIT_SUCCESS );
        }
 
        for ( ;; ) {
@@ -269,11 +267,11 @@ main (int  argc, 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),
-                   sizeof(from.sin_addr.s_addr), AF_INET );
+               hp = gethostbyaddr( (char *) &(from.sin_addr),
+                   sizeof(from.sin_addr), AF_INET );
 
                if ( dosyslog ) {
                        syslog( LOG_INFO, "TCP connection from %s (%s)",
@@ -313,30 +311,41 @@ set_socket( 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 )
@@ -362,7 +371,7 @@ wait4child( int sig )
                ;       /* NULL */
 #endif
 
-       (void) SIGNAL( SIGCHLD, wait4child );
+       (void) SIGNAL_REINSTALL ( SIGCHLD, wait4child );
 }
 
 static void
@@ -379,7 +388,7 @@ do_queries( int s )
 
        if ( (fp = fdopen( s, "a+")) == NULL ) {
                perror( "fdopen" );
-               exit( 1 );
+               exit( EXIT_FAILURE );
        }
 
        timeout.tv_sec = GO500GW_TIMEOUT;
@@ -388,10 +397,10 @@ do_queries( 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 ) {
@@ -437,7 +446,7 @@ do_queries( int s )
                fprintf( fp, ".\r\n" );
                rewind(fp);
 
-               exit( 0 );
+               exit( EXIT_SUCCESS );
                /* NOT REACHED */
        }
 
@@ -447,7 +456,7 @@ do_queries( int s )
                    LDAP_SERVER_DOWN, myhost, myport );
                fprintf( fp, ".\r\n" );
                rewind(fp);
-               exit( 1 );
+               exit( EXIT_FAILURE );
        }
 
        deref = LDAP_DEREF_ALWAYS;
@@ -463,7 +472,7 @@ do_queries( int s )
                    rc, myhost, myport );
                fprintf( fp, ".\r\n" );
                rewind(fp);
-               exit( 1 );
+               exit( EXIT_FAILURE );
        }
 
        switch ( *query++ ) {
@@ -487,7 +496,7 @@ do_queries( int s )
        fprintf( fp, ".\r\n" );
        rewind(fp);
 
-       exit( 0 );
+       exit( EXIT_SUCCESS );
        /* NOT REACHED */
 }
 
@@ -545,7 +554,7 @@ isnonleaf( LDAP *ld, char **oclist, char *dn )
                timeout.tv_usec = 0;
                ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
                if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_ONELEVEL,
-                   "(objectClass=*)", attrs, 0, &timeout, &res ))
+                   NULL, attrs, 0, &timeout, &res ))
                    == LDAP_SUCCESS || rc == LDAP_SIZELIMIT_EXCEEDED ) {
                        sizelimit = LDAP_NO_LIMIT;
                        ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
@@ -695,7 +704,7 @@ make_scope( LDAP *ld, char *dn )
 
        timeout.tv_sec = GO500GW_TIMEOUT;
        timeout.tv_usec = 0;
-       if ( ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
+       if ( ldap_search_st( ld, dn, LDAP_SCOPE_BASE, NULL,
            attrs, 0, &timeout, &res ) != LDAP_SUCCESS ) {
                return( -1 );
        }
@@ -729,7 +738,7 @@ do_search( LDAP *ld, FILE *fp, char *query )
 
        if ( (filter = strchr( query, '\t' )) == NULL ) {
                fprintf( fp, "3Missing filter!\r\n" );
-               exit( 1 );
+               exit( EXIT_FAILURE );
        }
        *filter++ = '\0';
        base = query;
@@ -757,7 +766,7 @@ do_search( LDAP *ld, FILE *fp, 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 ?
@@ -771,7 +780,7 @@ do_search( LDAP *ld, FILE *fp, 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;
@@ -870,8 +879,8 @@ do_read( LDAP *ld, FILE *fp, 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 ) {