]> git.sur5r.net Git - openldap/blobdiff - servers/ldapd/main.c
Fix memory leak if duplicate attr name in at_insert
[openldap] / servers / ldapd / main.c
index f5f421e512b35f1d77bfc803e1386f8283fa4710..764f4e9c094e8267595f4baebe4541d97d77dc92 100644 (file)
@@ -1,3 +1,4 @@
+/* $OpenLDAP$ */
 /*
  * Copyright (c) 1990-1996 Regents of the University of Michigan.
  * All rights reserved.
 #include <ac/unistd.h>
 #include <ac/wait.h>
 
+#ifdef LDAP_PROCTITLE
+#include <ac/setproctitle.h>
+#endif
+
 #include <quipu/commonarg.h>
 #include <quipu/ds_error.h>
 
 #include "lber.h"
+#include "../../libraries/liblber/lber-int.h"  /* get struct sockbuf */
 #include "ldap.h"
 #include "common.h"
+#include "lutil.h"             /* Get lutil_detach() */
 
 #ifdef HAVE_TCPD
 #include <tcpd.h>
@@ -43,12 +50,11 @@ int allow_severity = LOG_INFO;
 int deny_severity = LOG_NOTICE;
 #endif /* TCP_WRAPPERS */
 
-void log_and_exit();
-static set_socket();
-static do_queries();
-static RETSIGTYPE wait4child();
+static int     set_socket( int port, int udp );
+static void    do_queries( int clientsock, int udp );
+static RETSIGTYPE wait4child( int sig );
 #ifdef LDAP_CONNECTIONLESS
-static udp_init();
+static int     udp_init( int port, int createsocket );
 #endif
 
 #ifdef LDAP_DEBUG
@@ -77,10 +83,8 @@ char *kerberos_keyfile;
 int    dtblsize;
 int    RunFromInetd = 0;
 
-extern char Versionstr[];
-
-static usage( name )
-char   *name;
+static void
+usage( char *name )
 {
        fprintf( stderr, "usage: %s [-d debuglvl] [-p port] [-l] [-c dsa] [-r referraltimeout]", name );
 #ifdef LDAP_CONNECTIONLESS
@@ -95,31 +99,26 @@ char        *name;
        fprintf( stderr, "\n" );
 }
 
-main (argc, argv)
-int    argc;
-char   **argv;
+int
+main( int argc, char **argv )
 {
        int                     tcps, ns;
 #ifdef LDAP_CONNECTIONLESS
        int                     udps;
 #endif
        int                     myport = LDAP_PORT;
+       int                     no_detach = 0;
        int                     i, pid, socktype;
        char                    *myname;
        fd_set                  readfds;
        struct hostent          *hp;
        struct sockaddr_in      from;
-       int                     len;
+       socklen_t               len;
        int                     dsapargc;
        char                    **dsapargv;
-       RETSIGTYPE                      wait4child();
-#ifndef NOSETPROCTITLE
+#ifdef LDAP_PROCTITLE
        char                    title[80];
-       extern char             **Argv;
-       extern int              Argc;
 #endif
-       extern char             *optarg;
-       extern int              optind;
 
 #ifdef VMS
        /* Pick up socket from inetd-type server on VMS */
@@ -153,13 +152,15 @@ char      **argv;
                        dsapargc = 3;
                        break;
 
-               case 'd':       /* turn on debugging */
+               case 'd':       /* set debug level and 'do not detach' flag */
+                       no_detach = 1;
 #ifdef LDAP_DEBUG
                        ldap_debug = atoi( optarg );
                        if ( ldap_debug & LDAP_DEBUG_PACKETS )
-                               lber_debug = ldap_debug;
+                               ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
 #else
-                       fprintf( stderr, "Not compiled with -DLDAP_DEBUG!\n" );
+                       if ( atoi( optarg ) != 0 )
+                               fputs( "Not compiled with -DLDAP_DEBUG!\n", stderr );
 #endif
                        break;
 
@@ -243,7 +244,7 @@ char        **argv;
        }
 #endif /* FD_SETSIZE */
 
-#ifndef NOSETPROCTITLE
+#if defined(LDAP_PROCTITLE) && !defined( HAVE_SETPROCTITLE )
        /* for setproctitle */
        Argv = argv;
        Argc = argc;
@@ -260,14 +261,14 @@ char      **argv;
         * that have exited
         */
        if (!RunFromInetd) {
-#ifndef NOSETPROCTITLE
+#ifdef LDAP_PROCTITLE
                setproctitle( "initializing" );
 #endif
 #ifndef VMS
-               (void) detach();
+               lutil_detach( no_detach, 1 );
 #endif
-               (void) SIGNAL( SIGCHLD, (void *) wait4child );
-               (void) SIGNAL( SIGINT, (void *) log_and_exit );
+               (void) SIGNAL( SIGCHLD, wait4child );
+               (void) SIGNAL( SIGINT, log_and_exit );
        }
 
        /* 
@@ -299,7 +300,7 @@ char        **argv;
        (void) get_syntaxes();
        if (RunFromInetd) {
                len = sizeof( socktype );
-               getsockopt( ns, SOL_SOCKET, SO_TYPE, &socktype, &len );
+               getsockopt( ns, SOL_SOCKET, SO_TYPE, (char *)&socktype, &len );
                if ( socktype == SOCK_DGRAM ) {
 #ifdef LDAP_CONNECTIONLESS
                        Debug( LDAP_DEBUG_ARGS,
@@ -330,7 +331,7 @@ char        **argv;
                                    inet_ntoa( from.sin_addr ) );
                        }
 
-#ifndef NOSETPROCTITLE
+#ifdef LDAP_PROCTITLE
                        sprintf( title, "%s %d\n", hp == NULL ?
                            inet_ntoa( from.sin_addr ) : hp->h_name, myport );
                        setproctitle( title );
@@ -355,7 +356,7 @@ char        **argv;
         * if we are doing CLDAP as well, handle those requests on the fly
         */
 
-#ifndef NOSETPROCTITLE
+#ifdef LDAP_PROCTITLE
 #ifdef LDAP_CONNECTIONLESS
         sprintf( title, "listening %s/%s %d", do_tcp ? "tcp" : "",
             do_udp ? "udp" : "", myport );
@@ -405,7 +406,7 @@ char        **argv;
 
 #ifdef HAVE_TCPD
                if ( !hosts_ctl("ldapd", (hp == NULL) ? "unknown" : hp->h_name,
-                       inet_ntoa( from.sin_addr ), STRING_UNKNOWN ) {
+                       inet_ntoa( from.sin_addr ), STRING_UNKNOWN) ) {
 
                        Debug( LDAP_DEBUG_ARGS, "connection from %s (%s) denied.\n",
                                (hp == NULL) ? "unknown" : hp->h_name,
@@ -417,7 +418,7 @@ char        **argv;
                                    inet_ntoa( from.sin_addr ) );
                        }
 
-                       close(ns);
+                       tcp_close(ns);
                        continue;
                }
 #endif /* TCP_WRAPPERS */
@@ -435,13 +436,13 @@ char      **argv;
 
 #ifdef VMS
                /* This is for debug on terminal on VMS */
-               close( tcps );
-#ifndef NOSETPROCTITLE
+               tcp_close( tcps );
+#ifdef LDAP_PROCTITLE
                setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
                    hp->h_name );
 #endif
                gettimeofday( &conn_start_tv, (struct timezone *) NULL );
-               (void) SIGNAL( SIGPIPE, (void *) log_and_exit );
+               (void) SIGNAL( SIGPIPE, log_and_exit );
 
                do_queries( ns, 0 );
                /* NOT REACHED */
@@ -449,15 +450,15 @@ char      **argv;
 
                switch( pid = fork() ) {
                case 0:         /* child */
-                       close( tcps );
-#ifndef NOSETPROCTITLE
+                       tcp_close( tcps );
+#ifdef LDAP_PROCTITLE
                         sprintf( title, "%s (%d)\n", hp == NULL ?
                                inet_ntoa( from.sin_addr ) : hp->h_name,
                                myport );
                        setproctitle( title );
 #endif
                        gettimeofday( &conn_start_tv, (struct timezone *) NULL );
-                       (void) SIGNAL( SIGPIPE, (void *) log_and_exit );
+                       (void) SIGNAL( SIGPIPE, log_and_exit );
 
                        do_queries( ns, 0 );
                        break;
@@ -466,14 +467,14 @@ char      **argv;
 #ifdef LDAP_DEBUG
                        if ( ldap_debug ) perror( "fork" );
 #endif
-                       close( ns );
+                       tcp_close( ns );
                        syslog( LOG_ERR, "fork failed %m" );
                        /* let things cool off */
                        sleep( 15 );
                        break;
 
                default:        /* parent */
-                       close( ns );
+                       tcp_close( ns );
                        Debug( LDAP_DEBUG_TRACE, "forked child %d\n", pid, 0,
                            0 );
                        break;
@@ -482,14 +483,14 @@ char      **argv;
        /* NOT REACHED */
 }
 
-static
+static void
 do_queries(
     int        clientsock,
     int        udp             /* is this a UDP (CLDAP) request? */
 )
 {
        fd_set          readfds;
-       int             rc, i;
+       int             rc;
        struct timeval  timeout;
        Sockbuf         sb;
 #ifdef LDAP_CONNECTIONLESS
@@ -514,23 +515,14 @@ do_queries(
                conn_init();
        }
 
-       (void) memset( (void *) &sb, '\0', sizeof( sb ) );
-       sb.sb_sd = clientsock;
-       sb.sb_naddr = ( udp ) ? 1 : 0;
-#ifdef LDAP_CONNECTIONLESS
-       sb.sb_addrs = (void **)saddrlist;
-       sb.sb_fromaddr = &faddr;
-       sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
-#endif
-       sb.sb_ber.ber_buf = NULL;
-       sb.sb_ber.ber_ptr = NULL;
-       sb.sb_ber.ber_end = NULL;
-
+       ber_pvt_sb_init( &sb );
+       ber_pvt_sb_set_desc( &sb, clientsock );
+       ber_pvt_sb_set_io( &sb, (udp) ? &ber_pvt_sb_io_udp :
+                                       &ber_pvt_sb_io_tcp, NULL );
        timeout.tv_sec = idletime;
        timeout.tv_usec = 0;
        for ( ;; ) {
                struct conn             *dsaconn;
-               extern struct conn      *conns;
 
                FD_ZERO( &readfds );
                FD_SET( clientsock, &readfds );
@@ -538,6 +530,7 @@ do_queries(
 
 #ifdef LDAP_DEBUG
                if ( ldap_debug & LDAP_DEBUG_CONNS ) {
+                       int i;
                        Debug( LDAP_DEBUG_CONNS, "FDLIST:", 0, 0, 0 );
                        for ( i = 0; i < dtblsize; i++ ) {
                                if ( FD_ISSET( i, &readfds ) ) {
@@ -554,7 +547,7 @@ do_queries(
                 * already waiting for us on the client sock.
                 */
 
-               if ( sb.sb_ber.ber_ptr >= sb.sb_ber.ber_end ) {
+               if ( ! ber_pvt_sb_data_ready( &sb ) ) {
                        if ( (rc = select( dtblsize, &readfds, 0, 0,
                            udp ? 0 : &timeout )) < 1 ) {
 #ifdef LDAP_DEBUG
@@ -580,7 +573,7 @@ do_queries(
                        }
                }
 
-               if ( sb.sb_ber.ber_ptr < sb.sb_ber.ber_end ||
+               if ( ber_pvt_sb_data_ready( &sb ) ||
                    FD_ISSET( clientsock, &readfds ) ) {
                        client_request( &sb, conns, udp );
                } else {
@@ -596,7 +589,8 @@ do_queries(
        /* NOT REACHED */
 }
 
-static set_socket(
+static int
+set_socket(
     int        port,
     int        udp     /* UDP port? */
 )
@@ -609,18 +603,31 @@ static set_socket(
                 exit( 1 );
         }
 
+#ifdef SO_REUSEADDR
         /* set option so clients can't keep us from coming back up */
-       i = 1;
+               i = 1;
         if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (void *) &i, sizeof(i) )
-           < 0 ) {
+                   < 0 )
+               {
+                perror( "setsockopt" );
+                exit( 1 );
+        }
+#endif
+#ifdef SO_KEEPALIVE
+        /* enable keep alives */
+               i = 1;
+        if ( setsockopt( s, SOL_SOCKET, SO_KEEPALIVE, (void *) &i, sizeof(i) )
+                   < 0 )
+               {
                 perror( "setsockopt" );
                 exit( 1 );
         }
+#endif
 
         /* bind to a name */
        (void)memset( (void *)&addr, '\0', sizeof( addr ));
         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" );
@@ -641,7 +648,8 @@ static set_socket(
        return( s );
 }
 
-static RETSIGTYPE wait4child()
+static RETSIGTYPE
+wait4child( int sig )
 {
 #ifndef HAVE_WAITPID
        WAITSTATUSTYPE     status;
@@ -650,18 +658,18 @@ static RETSIGTYPE wait4child()
        Debug( LDAP_DEBUG_TRACE, "parent: catching child status\n", 0, 0, 0 );
 
 #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, (void *) wait4child );
+       (void) SIGNAL( SIGCHLD, wait4child );
 }
 
 
-void
+RETSIGTYPE
 log_and_exit( int exitcode )
 {
        struct timeval  tv;
@@ -689,9 +697,6 @@ udp_init(
 {
        int     s, bound;
        char    *matched;
-       extern char             *dsa_address;
-       extern struct PSAPaddr  *psap_cpy();
-       extern struct conn      *conns;
 
        if ( createsocket )
                s = set_socket( port, 1 );