]> git.sur5r.net Git - openldap/blobdiff - clients/gopher/go500.c
s/<stdlib.h>/<ac/stdlib.h>/
[openldap] / clients / gopher / go500.c
index 6a8a70c5281ace4afcf7036b437e666d76073958..7b5141bbed4888792882f4de23fdc3e0f063c3e5 100644 (file)
@@ -14,6 +14,8 @@
 
 #include <stdio.h>
 
+#include <ac/stdlib.h>
+
 #include <ac/ctype.h>
 #include <ac/signal.h>
 #include <ac/socket.h>
 #include <ac/unistd.h>
 #include <ac/wait.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 "ldapconfig.h"
 #include "lber.h"
 #include "ldap.h"
+
+#define ldap_debug debug
+#include "ldap_log.h"
+
+#include "lutil.h"
+
 #include "disptmpl.h"
 
 int    debug;
 int    dosyslog;
+int ldap_syslog;
+int ldap_syslog_level;
 int    inetd;
 int    dtblsize;
 
-char   *ldaphost = LDAPHOST;
-char   *base = GO500_BASE;
+char   *ldaphost = NULL;
+char   *base = NULL;
 int    rdncount = GO500_RDNCOUNT;
 char   *filterfile = FILTERFILE;
 char   *templatefile = TEMPLATEFILE;
@@ -48,24 +62,23 @@ char        *templatefile = TEMPLATEFILE;
 char   myhost[MAXHOSTNAMELEN];
 int    myport;
 
-static set_socket();
-static RETSIGTYPE wait4child();
-static do_queries();
-static do_error();
-static do_search();
-static do_read();
-extern int strcasecmp();
+static void usage      ( char *name );
+static int  set_socket (int port);
+static RETSIGTYPE wait4child(int sig);
+static void do_queries (int s);
+static void do_error   (FILE *fp, char *s);
+static void do_search  (LDAP *ld, FILE *fp, char *buf);
+static void do_read    (LDAP *ld, FILE *fp, char *dn);
 
-static usage( name )
-char   *name;
+static void
+usage( char *name )
 {
        fprintf( stderr, "usage: %s [-d debuglevel] [-f filterfile] [-t templatefile]\r\n\t[-a] [-l] [-p port] [-x ldaphost] [-b searchbase] [-c rdncount]\r\n", name );
        exit( 1 );
 }
 
-main (argc, argv)
-int    argc;
-char   **argv;
+int
+main( int argc, char **argv )
 {
        int                     s, ns, rc;
        int                     port = -1;
@@ -75,14 +88,12 @@ char        **argv;
        struct hostent          *hp;
        struct sockaddr_in      from;
        int                     fromlen;
-       RETSIGTYPE                      wait4child();
-       extern char             *optarg;
-       extern char             **Argv;
-       extern int              Argc;
 
+#if defined( LDAP_PROCTITLE ) && !defined( HAVE_SETPROCTITLE )
        /* for setproctitle */
-        Argv = argv;
-        Argc = argc;
+       Argv = argv;
+       Argc = argc;
+#endif
 
        while ( (i = getopt( argc, argv, "b:d:f:lp:c:t:x:I" )) != EOF ) {
                switch( i ) {
@@ -91,7 +102,7 @@ char **argv;
                        break;
 
                case 'd':       /* debug level */
-                       debug = atoi( optarg );
+                       debug |= atoi( optarg );
                        break;
 
                case 'f':       /* ldap filter file */
@@ -151,16 +162,24 @@ char      **argv;
        }
 #endif /* FD_SETSIZE*/
 
-
        /* detach if stderr is redirected or no debugging */
        if ( inetd == 0 )
-               (void) detach( debug );
+               lutil_detach( debug && !isatty( 1 ), 1 );
 
        if ( (myname = strrchr( argv[0], '/' )) == NULL )
                myname = strdup( argv[0] );
        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 );
@@ -195,8 +214,10 @@ char       **argv;
                                    inet_ntoa( from.sin_addr ) );
                        }
 
+#ifdef LDAP_PROCTITLE
                        setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
                            hp->h_name );
+#endif
                }
 
                do_queries( 0 );
@@ -236,7 +257,7 @@ char        **argv;
 
                switch( pid = fork() ) {
                case 0:         /* child */
-                       close( s );
+                       tcp_close( s );
                        do_queries( ns );
                        break;
 
@@ -245,7 +266,7 @@ char        **argv;
                        break;
 
                default:        /* parent */
-                       close( ns );
+                       tcp_close( ns );
                        if ( debug )
                                fprintf( stderr, "forked child %d\n", pid );
                        break;
@@ -254,9 +275,8 @@ char        **argv;
        /* NOT REACHED */
 }
 
-static
-set_socket( port )
-int    port;
+static int
+set_socket( int port )
 {
        int                     s, one;
        struct sockaddr_in      addr;
@@ -299,7 +319,7 @@ int port;
 }
 
 static RETSIGTYPE
-wait4child()
+wait4child( int sig )
 {
 #ifndef HAVE_WAITPID
         WAITSTATUSTYPE     status;
@@ -308,19 +328,18 @@ wait4child()
         if ( debug ) printf( "parent: catching child status\n" );
 
 #ifdef HAVE_WAITPID
-       while (waitpid ((pid_t) -1, 0, 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;
@@ -349,7 +368,7 @@ int s;
        if ( debug ) {
                fprintf( stderr, "got %d bytes\n", len );
 #ifdef LDAP_DEBUG
-               lber_bprint( buf, len );
+               ber_bprint( buf, len );
 #endif
        }
 
@@ -364,26 +383,28 @@ int       s;
        query = buf;
 
        /* strip off leading white space */
-       while ( isspace( *query )) {
+       while ( isspace( (unsigned char) *query )) {
                ++query;
                --len;
        }
 
        rewind(fp);
 
-       if ( *query != '~' && *query != '@' ) {
-               if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
-                       fprintf(fp,
-                           "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
-                           LDAP_SERVER_DOWN, myhost, myport );
-                       fprintf( fp, ".\r\n" );
-                       rewind(fp);
-                       exit( 1 );
-               }
+       if ( *query == '~' || *query == '@' ) {
+               ld = NULL;
+       } else if ( (ld = ldap_open( ldaphost, 0 )) == NULL ) {
+               fprintf(fp,
+                       "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
+                       LDAP_SERVER_DOWN, myhost, myport );
+               fprintf( fp, ".\r\n" );
+               rewind(fp);
+               exit( 1 );
+       } else {
+               int deref = GO500_DEREF;
+               ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
 
-               ld->ld_deref = GO500_DEREF;
-               if ( (rc = ldap_simple_bind_s( ld, GO500_BINDDN, GO500_BIND_CRED ))
-                   != LDAP_SUCCESS ) {
+               rc = ldap_simple_bind_s( ld, NULL, NULL );
+               if ( rc != LDAP_SUCCESS ) {
                        fprintf(fp,
                            "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
                            rc, myhost, myport );
@@ -417,16 +438,17 @@ int       s;
 
        fprintf( fp, ".\r\n" );
        rewind(fp);
-       ldap_unbind( ld );
+
+       if ( ld != NULL) {
+               ldap_unbind( ld );
+       }
 
        exit( 1 );
        /* NOT REACHED */
 }
 
-static
-do_error( fp, s )
-FILE   *fp;
-char   *s;
+static void
+do_error( FILE *fp, char *s )
 {
        int     code;
 
@@ -438,11 +460,8 @@ char       *s;
        fprintf( fp, ".\r\n" );
 }
 
-static
-do_search( ld, fp, buf )
-LDAP   *ld;
-FILE   *fp;
-char   *buf;
+static void
+do_search( LDAP *ld, FILE *fp, char *buf )
 {
        char            *dn, *rdn;
        char            **title;
@@ -552,14 +571,11 @@ char      *buf;
 static int
 entry2textwrite( void *fp, char *buf, int len )
 {
-        return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : 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;