From 3550bc48c0209f8ab2fc6f9b1e423c03d54028df Mon Sep 17 00:00:00 2001 From: Kurt Zeilenga Date: Wed, 17 Mar 1999 03:56:25 +0000 Subject: [PATCH] Add slapd -a address support. Allows you to bind to a specific address. Useful for running multiple servers in a virtual hosting environment. Modified test001-ldif2ldbm to verify this functionality. Assumes localhost is 127.0.0.1. --- doc/man/man8/slapd.8 | 10 ++++++++-- servers/slapd/daemon.c | 10 +++------- servers/slapd/main.c | 34 +++++++++++++++++++++++---------- tests/scripts/defines.sh | 1 + tests/scripts/test001-ldif2ldbm | 2 +- tests/scripts/test001-slapadd | 2 +- 6 files changed, 38 insertions(+), 21 deletions(-) diff --git a/doc/man/man8/slapd.8 b/doc/man/man8/slapd.8 index bc6cd54680..36bd63c588 100644 --- a/doc/man/man8/slapd.8 +++ b/doc/man/man8/slapd.8 @@ -3,7 +3,7 @@ slapd \- Stand-alone LDAP Daemon .SH SYNOPSIS .B LIBEXECDIR/slapd [\-d debug\-level] -.B [\-f slapd\-config\-file] [\-p port\-number] +.B [\-f slapd\-config\-file] [\-a address] [\-p port\-number] .B [\-s syslog\-level] [\-l syslog\-local\-user] [\-i] .B .SH DESCRIPTION @@ -87,10 +87,16 @@ facility. Specifies the slapd configuration file. The default is .BR ETCDIR/slapd.conf . .TP +.BI \-a " address" +.B slapd +will listen on all addresses (INADDR_ANY) unless this option +is given to override the default. The address is expected in +Internet standard '.' format. +.TP .BI \-p " port\-number" .B slapd will listen on the default LDAP port (389) unless this option is given -to override the default. +to override the default. A numeric port number is expected. .TP .B \-i This option tells diff --git a/servers/slapd/daemon.c b/servers/slapd/daemon.c index 0b0cfe6b4d..b395a72ee2 100644 --- a/servers/slapd/daemon.c +++ b/servers/slapd/daemon.c @@ -50,12 +50,12 @@ extern char *slapd_args_file; void * slapd_daemon( - void *port + void *ptr ) { + struct sockaddr_in *addr = ptr; int i; int tcps, ns; - struct sockaddr_in addr; fd_set readfds; fd_set writefds; FILE *fp; @@ -104,11 +104,7 @@ slapd_daemon( "unknown", 0 ); } - (void) memset( (void *) &addr, '\0', sizeof(addr) ); - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = INADDR_ANY; - addr.sin_port = htons( (int)port ); - if ( bind( tcps, (struct sockaddr *) &addr, sizeof(addr) ) == -1 ) { + if ( bind( tcps, (struct sockaddr *) addr, sizeof(*addr) ) == -1 ) { Debug( LDAP_DEBUG_ANY, "bind() failed errno %d (%s)\n", errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] : "unknown", 0 ); diff --git a/servers/slapd/main.c b/servers/slapd/main.c index 10e14d7fb5..f7b4469390 100644 --- a/servers/slapd/main.c +++ b/servers/slapd/main.c @@ -62,7 +62,7 @@ main( int argc, char **argv ) int i; int inetd = 0; int rc = 0; - int port; + struct sockaddr_in bind_addr; int udp; #ifdef LOG_LOCAL4 int syslogUser = DEFAULT_SYSLOG_USER; @@ -72,16 +72,27 @@ main( int argc, char **argv ) int serverMode = SLAP_SERVER_MODE; configfile = SLAPD_DEFAULT_CONFIGFILE; - port = LDAP_PORT; + + (void) memset( (void*) &bind_addr, '\0', sizeof(bind_addr)); + bind_addr.sin_family = AF_INET; + bind_addr.sin_addr.s_addr = htonl(INADDR_ANY); + bind_addr.sin_port = htons(LDAP_PORT); + g_argc = argc; g_argv = argv; #ifdef SLAPD_BDB2 - while ( (i = getopt( argc, argv, "d:f:ip:s:ut" )) != EOF ) { + while ( (i = getopt( argc, argv, "d:f:ia:p:s:ut" )) != EOF ) { #else - while ( (i = getopt( argc, argv, "d:f:ip:s:u" )) != EOF ) { + while ( (i = getopt( argc, argv, "d:f:ia:p:s:u" )) != EOF ) { #endif switch ( i ) { + case 'a': /* bind address */ + if(!inet_aton(optarg, &bind_addr.sin_addr)) { + fprintf(stderr, "invalid address (%s) for -a option", optarg); + } + break; + #ifdef LDAP_DEBUG case 'd': /* turn on debugging */ if ( optarg[0] == '?' ) { @@ -132,21 +143,24 @@ main( int argc, char **argv ) inetd = 1; break; - case 'p': /* port on which to listen */ - port = atoi( optarg ); - break; + case 'p': { /* port on which to listen */ + int port = atoi( optarg ); + if(! port ) { + fprintf(stderr, "-p %s must be numeric\n", optarg); + } else { + bind_addr.sin_port = htons(port); + } + } break; case 's': /* set syslog level */ ldap_syslog = atoi( optarg ); break; #ifdef LOG_LOCAL4 - case 'l': /* set syslog local user */ syslogUser = cnvt_str2int( optarg, syslog_types, DEFAULT_SYSLOG_USER ); break; - #endif case 'u': /* do udp */ @@ -221,7 +235,7 @@ main( int argc, char **argv ) time( &starttime ); status = ldap_pvt_thread_create( &listener_tid, 0, - slapd_daemon, (void *) port ); + slapd_daemon, &bind_addr ); if ( status != 0 ) { Debug( LDAP_DEBUG_ANY, diff --git a/tests/scripts/defines.sh b/tests/scripts/defines.sh index d9005382a0..8d84c1f726 100755 --- a/tests/scripts/defines.sh +++ b/tests/scripts/defines.sh @@ -35,6 +35,7 @@ LDAPADD=../clients/tools/ldapadd LDAPMODRDN=../clients/tools/ldapmodrdn SLAPDTESTER=$PROGDIR/slapd-tester LVL=5 +ADDR=127.0.0.1 PORT=9009 SLAVEPORT=9010 DBDIR=./test-db diff --git a/tests/scripts/test001-ldif2ldbm b/tests/scripts/test001-ldif2ldbm index 6ab1a5c52b..547aa830d3 100755 --- a/tests/scripts/test001-ldif2ldbm +++ b/tests/scripts/test001-ldif2ldbm @@ -28,7 +28,7 @@ if [ $RC != 0 ]; then fi echo "Starting slapd on TCP/IP port $PORT..." -$SLAPD -f $CONF -p $PORT -d $LVL $TIMING > $MASTERLOG 2>&1 & +$SLAPD -f $CONF -p $PORT -a $ADDR -d $LVL $TIMING > $MASTERLOG 2>&1 & PID=$! echo "Using ldapsearch to retrieve all the entries..." diff --git a/tests/scripts/test001-slapadd b/tests/scripts/test001-slapadd index 6ab1a5c52b..547aa830d3 100755 --- a/tests/scripts/test001-slapadd +++ b/tests/scripts/test001-slapadd @@ -28,7 +28,7 @@ if [ $RC != 0 ]; then fi echo "Starting slapd on TCP/IP port $PORT..." -$SLAPD -f $CONF -p $PORT -d $LVL $TIMING > $MASTERLOG 2>&1 & +$SLAPD -f $CONF -p $PORT -a $ADDR -d $LVL $TIMING > $MASTERLOG 2>&1 & PID=$! echo "Using ldapsearch to retrieve all the entries..." -- 2.39.5