]> git.sur5r.net Git - openldap/blobdiff - clients/gopher/go500gw.c
Add OpenLDAP RCSid to *.[ch] in clients, libraries, and servers.
[openldap] / clients / gopher / go500gw.c
index ce37e796ecbab9840b178aec49640fb633c182a6..c1c5ab4a9ef47c556e3823f66d3ce195aedeb13f 100644 (file)
@@ -1,3 +1,4 @@
+/* $OpenLDAP$ */
 /*
  * Copyright (c) 1990 Regents of the University of Michigan.
  * All rights reserved.
@@ -13,7 +14,8 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <stdlib.h>
+
+#include <ac/stdlib.h>
 
 #include <ac/ctype.h>
 #include <ac/signal.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 "ldapconfig.h"
+#include "ldap_defaults.h"
 
 int    debug;
+int ldap_syslog;
+int ldap_syslog_level;
 int    dosyslog;
 int    inetd;
 int    dtblsize;
@@ -58,10 +64,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);
@@ -69,7 +75,7 @@ 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, int len );
+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);
@@ -82,7 +88,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
@@ -95,7 +101,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 */
@@ -110,12 +116,7 @@ main (int  argc, 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 */
@@ -181,7 +182,7 @@ main (int  argc, char **argv )
        if ( myhost[0] == '\0' && gethostname( myhost, sizeof(myhost) )
            == -1 ) {
                perror( "gethostname" );
-               exit( 1 );
+               exit( EXIT_FAILURE );
        }
 #endif
 
@@ -194,6 +195,15 @@ main (int  argc, 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 );
@@ -236,9 +246,9 @@ main (int  argc, char **argv )
 
                do_queries( 0 );
 
-               close( 0 );
+               tcp_close( 0 );
 
-               exit( 0 );
+               exit( EXIT_SUCCESS );
        }
 
        for ( ;; ) {
@@ -259,7 +269,7 @@ 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),
@@ -273,7 +283,7 @@ main (int  argc, char **argv )
 
                switch( pid = fork() ) {
                case 0:         /* child */
-                       close( s );
+                       tcp_close( s );
                        do_queries( ns );
                        break;
 
@@ -282,7 +292,7 @@ main (int  argc, char **argv )
                        break;
 
                default:        /* parent */
-                       close( ns );
+                       tcp_close( ns );
                        if ( debug )
                                fprintf( stderr, "forked child %d\n", pid );
                        break;
@@ -303,30 +313,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 )
@@ -345,10 +366,10 @@ wait4child( int sig )
        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
 
@@ -369,7 +390,7 @@ do_queries( int s )
 
        if ( (fp = fdopen( s, "a+")) == NULL ) {
                perror( "fdopen" );
-               exit( 1 );
+               exit( EXIT_FAILURE );
        }
 
        timeout.tv_sec = GO500GW_TIMEOUT;
@@ -378,16 +399,16 @@ 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 ) {
                fprintf( stderr, "got %d bytes\n", len );
 #ifdef LDAP_DEBUG
-               lber_bprint( buf, len );
+               ber_bprint( buf, len );
 #endif
        }
 
@@ -402,7 +423,7 @@ do_queries( int s )
        query = buf;
 
        /* strip off leading white space */
-       while ( isspace( *query )) {
+       while ( isspace( (unsigned char) *query )) {
                ++query;
                --len;
        }
@@ -427,17 +448,17 @@ do_queries( 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;
@@ -453,7 +474,7 @@ do_queries( int s )
                    rc, myhost, myport );
                fprintf( fp, ".\r\n" );
                rewind(fp);
-               exit( 1 );
+               exit( EXIT_FAILURE );
        }
 
        switch ( *query++ ) {
@@ -477,7 +498,7 @@ do_queries( int s )
        fprintf( fp, ".\r\n" );
        rewind(fp);
 
-       exit( 0 );
+       exit( EXIT_SUCCESS );
        /* NOT REACHED */
 }
 
@@ -556,7 +577,7 @@ 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 );
@@ -588,7 +609,7 @@ 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;
 
@@ -719,7 +740,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;
@@ -747,7 +768,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 ?
@@ -761,7 +782,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;
@@ -791,8 +812,7 @@ do_search( LDAP *ld, FILE *fp, char *query )
        }
 
        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" );
@@ -842,7 +862,7 @@ do_search( LDAP *ld, FILE *fp, 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 );
 }
@@ -861,8 +881,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 ) {