From: Florian Westphal Date: Fri, 29 Jul 2005 09:29:47 +0000 (+0000) Subject: internal changes needed for future ssl support X-Git-Tag: rel-0-10-0-pre1~131 X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=ngircd-alex.git;a=commitdiff_plain;h=51ccb5928ad1453b0593fedd934298384d09e619 internal changes needed for future ssl support --- diff --git a/src/ngircd/conf.c b/src/ngircd/conf.c index 0764d2c7..b4e56172 100644 --- a/src/ngircd/conf.c +++ b/src/ngircd/conf.c @@ -14,7 +14,7 @@ #include "portab.h" -static char UNUSED id[] = "$Id: conf.c,v 1.81 2005/07/28 16:23:55 fw Exp $"; +static char UNUSED id[] = "$Id: conf.c,v 1.82 2005/07/29 09:29:47 fw Exp $"; #include "imp.h" #include @@ -38,6 +38,7 @@ static char UNUSED id[] = "$Id: conf.c,v 1.81 2005/07/28 16:23:55 fw Exp $"; # include #endif +#include "array.h" #include "ngircd.h" #include "conn.h" #include "client.h" @@ -72,6 +73,54 @@ LOCAL void Config_Error_TooLong PARAMS(( const int LINE, const char *Value )); LOCAL void Init_Server_Struct PARAMS(( CONF_SERVER *Server )); +static void +ports_puts(array *a) +{ + unsigned int len; + UINT16 *ports; + len = array_length(a, sizeof(UINT16)); + if (len--) { + ports = (UINT16*) array_start(a); + printf("%u", (unsigned int) *ports); + while (len--) { + ports++; + printf(", %u", (unsigned int) *ports); + } + } + putc('\n', stdout); +} + + +static void +ports_parse(array *a, int Line, char *Arg) +{ + char *ptr; + int port; + UINT16 port16; + + array_trunc(a); + + /* Ports on that the server should listen. More port numbers + * must be separated by "," */ + ptr = strtok( Arg, "," ); + while (ptr) { + ngt_TrimStr( ptr ); + port = atol( ptr ); + if (port > 0 && port < 0xFFFF) { + port16 = (UINT16) port; + if (!array_catb(a, (char*)&port16, sizeof port16)) + Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s", + NGIRCd_ConfFile, Line, port, strerror(errno)); + } else { + Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!", + NGIRCd_ConfFile, Line, port ); + } + + ptr = strtok( NULL, "," ); + } +} + + GLOBAL void Conf_Init( void ) { @@ -126,13 +175,10 @@ Conf_Test( void ) printf( " MotdPhrase = %s\n", Conf_MotdPhrase ); printf( " ChrootDir = %s\n", Conf_Chroot ); printf( " PidFile = %s\n", Conf_PidFile); - printf( " Ports = " ); - for( i = 0; i < Conf_ListenPorts_Count; i++ ) - { - if( i != 0 ) printf( ", " ); - printf( "%u", (unsigned int) Conf_ListenPorts[i] ); - } - puts( "" ); + fputs(" Ports = ", stdout); + + ports_puts(&Conf_ListenPorts); + printf( " Listen = %s\n", Conf_ListenAddress ); pwd = getpwuid( Conf_UID ); if( pwd ) printf( " ServerUID = %s\n", pwd->pw_name ); @@ -188,6 +234,7 @@ Conf_Test( void ) puts( "[CHANNEL]" ); printf( " Name = %s\n", Conf_Channel[i].name ); printf( " Modes = %s\n", Conf_Channel[i].modes ); + topic = (char*)array_start(&Conf_Channel[i].topic); printf( " Topic = %s\n", topic ? topic : ""); puts( "" ); @@ -364,7 +411,6 @@ Set_Defaults( bool InitServers ) strlcpy( Conf_PidFile, PID_FILE, sizeof( Conf_PidFile )); - Conf_ListenPorts_Count = 0; strcpy( Conf_ListenAddress, "" ); Conf_UID = Conf_GID = 0; @@ -395,6 +441,7 @@ Read_Config( void ) /* Read configuration file. */ char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr; + const UINT16 defaultport = 6667; int line, i, n; FILE *fd; @@ -555,12 +602,13 @@ Read_Config( void ) assert( New_Server_Idx > NONE ); Conf_Server[New_Server_Idx] = New_Server; } - - /* If there are no ports configured use the default: 6667 */ - if( Conf_ListenPorts_Count < 1 ) - { - Conf_ListenPorts_Count = 1; - Conf_ListenPorts[0] = 6667; + + if (0 == array_length(&Conf_ListenPorts, sizeof(UINT16))) { + if (!array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport)) { + Config_Error( LOG_ALERT, "Could not add default listening Port %u: %s", + (unsigned int) defaultport, strerror(errno)); + exit( 1 ); + } } } /* Read_Config */ @@ -581,8 +629,6 @@ Handle_GLOBAL( int Line, char *Var, char *Arg ) { struct passwd *pwd; struct group *grp; - char *ptr; - long port; assert( Line > 0 ); assert( Var != NULL ); @@ -630,23 +676,9 @@ Handle_GLOBAL( int Line, char *Var, char *Arg ) if( strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail )) >= sizeof( Conf_ServerAdminMail )) Config_Error_TooLong( Line, Var ); return; } - if( strcasecmp( Var, "Ports" ) == 0 ) - { - /* Ports on that the server should listen. More port numbers - * must be separated by "," */ - ptr = strtok( Arg, "," ); - while( ptr ) - { - ngt_TrimStr( ptr ); - port = atol( ptr ); - if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Config_Error( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port ); - else - { - if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = (UINT16)port; - else Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!", NGIRCd_ConfFile, Line, port ); - } - ptr = strtok( NULL, "," ); - } + + if( strcasecmp( Var, "Ports" ) == 0 ) { + ports_parse(&Conf_ListenPorts, Line, Arg); return; } if( strcasecmp( Var, "MotdFile" ) == 0 ) diff --git a/src/ngircd/conf.h b/src/ngircd/conf.h index e312ccca..f9e30da2 100644 --- a/src/ngircd/conf.h +++ b/src/ngircd/conf.h @@ -8,7 +8,7 @@ * (at your option) any later version. * Please read the file COPYING, README and AUTHORS for more information. * - * $Id: conf.h,v 1.36 2005/07/28 16:23:55 fw Exp $ + * $Id: conf.h,v 1.37 2005/07/29 09:29:47 fw Exp $ * * Configuration management (header) */ @@ -78,8 +78,7 @@ GLOBAL char Conf_MotdFile[FNAME_LEN]; GLOBAL char Conf_MotdPhrase[LINE_LEN]; /* Ports the server should listen on */ -GLOBAL UINT16 Conf_ListenPorts[MAX_LISTEN_PORTS]; -GLOBAL unsigned int Conf_ListenPorts_Count; +GLOBAL array Conf_ListenPorts; /* Address to which the socket should be bound or empty (=all) */ GLOBAL char Conf_ListenAddress[16]; diff --git a/src/ngircd/conn.c b/src/ngircd/conn.c index 07c504cc..146cb8ef 100644 --- a/src/ngircd/conn.c +++ b/src/ngircd/conn.c @@ -17,7 +17,7 @@ #include "portab.h" #include "io.h" -static char UNUSED id[] = "$Id: conn.c,v 1.166 2005/07/28 16:13:09 fw Exp $"; +static char UNUSED id[] = "$Id: conn.c,v 1.167 2005/07/29 09:29:47 fw Exp $"; #include "imp.h" #include @@ -83,7 +83,7 @@ static char UNUSED id[] = "$Id: conn.c,v 1.166 2005/07/28 16:13:09 fw Exp $"; LOCAL bool Handle_Write PARAMS(( CONN_ID Idx )); -LOCAL void New_Connection PARAMS(( int Sock )); +LOCAL int New_Connection PARAMS(( int Sock )); LOCAL CONN_ID Socket2Index PARAMS(( int Sock )); LOCAL void Read_Request PARAMS(( CONN_ID Idx )); LOCAL bool Handle_Buffer PARAMS(( CONN_ID Idx )); @@ -103,6 +103,7 @@ int allow_severity = LOG_INFO; int deny_severity = LOG_ERR; #endif +static void server_login PARAMS((CONN_ID idx)); static void cb_clientserver PARAMS((int sock, short what)); @@ -158,7 +159,13 @@ cb_connserver(int sock, UNUSED short what) } Conn_OPTION_DEL( &My_Connections[idx], CONN_ISCONNECTING ); + server_login(idx); +} + +static void +server_login(CONN_ID idx) +{ Log( LOG_INFO, "Connection %d with \"%s:%d\" established. Now logging in ...", idx, My_Connections[idx].host, Conf_Server[Conf_GetServer( idx )].port ); @@ -275,34 +282,50 @@ Conn_Exit( void ) } /* Conn_Exit */ +static unsigned int +ports_initlisteners(array *a, void (*func)(int,short)) +{ + unsigned int created = 0, len; + int fd; + UINT16 *port; + + len = array_length(a, sizeof (UINT16)); + port = array_start(a); + while(len--) { + fd = NewListener( *port ); + if (fd < 0) { + port++; + continue; + } + if (!io_event_create( fd, IO_WANTREAD, func )) { + Log( LOG_ERR, "io_event_create(): Could not add listening fd %d (port %u): %s!", + fd, (unsigned int) *port, strerror(errno)); + close(fd); + port++; + continue; + } + created++; + port++; + } + + return created; +} + + GLOBAL int Conn_InitListeners( void ) { /* Initialize ports on which the server should accept connections */ - int created, fd; - unsigned int i; + unsigned int created; if (!io_library_init(CONNECTION_POOL)) { Log(LOG_EMERG, "Cannot initialize IO routines: %s", strerror(errno)); return -1; } - created = 0; - for( i = 0; i < Conf_ListenPorts_Count; i++ ) { - fd = NewListener( Conf_ListenPorts[i] ); - if (fd < 0) { - Log( LOG_ERR, "Can't listen on port %u!", (unsigned int) Conf_ListenPorts[i] ); - continue; - } - if (!io_event_create( fd, IO_WANTREAD, cb_listen )) { - Log( LOG_ERR, "io_event_create(): Could not add listening fd %d (port %u): %s!", - fd, (unsigned int) Conf_ListenPorts[i], strerror(errno)); - close(fd); - continue; - } - created++; - } + created = ports_initlisteners(&Conf_ListenPorts, cb_listen); + return created; } /* Conn_InitListeners */ @@ -836,7 +859,6 @@ Handle_Write( CONN_ID Idx ) } assert( My_Connections[Idx].sock > NONE ); - wdatalen = array_bytes(&My_Connections[Idx].wbuf ); #ifdef ZLIB if(( wdatalen == 0 ) && ( ! array_bytes(&My_Connections[Idx].zip.wbuf))) { @@ -872,7 +894,7 @@ Handle_Write( CONN_ID Idx ) } /* Handle_Write */ -LOCAL void +LOCAL int New_Connection( int Sock ) { /* Neue Client-Verbindung von Listen-Socket annehmen und @@ -896,7 +918,7 @@ New_Connection( int Sock ) if( new_sock < 0 ) { Log( LOG_CRIT, "Can't accept connection: %s!", strerror( errno )); - return; + return -1; } #ifdef TCPWRAP @@ -909,7 +931,7 @@ New_Connection( int Sock ) Log( deny_severity, "Refused connection from %s (by TCP Wrappers)!", inet_ntoa( new_addr.sin_addr )); Simple_Message( new_sock, "ERROR :Connection refused" ); close( new_sock ); - return; + return -1; } #endif @@ -924,7 +946,7 @@ New_Connection( int Sock ) Log( LOG_ERR, "Refused connection from %s: too may connections (%ld) from this IP address!", inet_ntoa( new_addr.sin_addr ), cnt); Simple_Message( new_sock, "ERROR :Connection refused, too many connections from your IP address!" ); close( new_sock ); - return; + return -1; } /* Freie Connection-Struktur suchen */ @@ -945,7 +967,7 @@ New_Connection( int Sock ) Log( LOG_ALERT, "Can't accept connection: limit (%d) reached!", Pool_Size ); Simple_Message( new_sock, "ERROR :Connection limit reached" ); close( new_sock ); - return; + return -1; } if( new_size > Conf_MaxConnections ) new_size = Conf_MaxConnections; } @@ -954,7 +976,7 @@ New_Connection( int Sock ) Log( LOG_ALERT, "Can't accept connection: limit (%d) reached -- overflow!", Pool_Size ); Simple_Message( new_sock, "ERROR :Connection limit reached" ); close( new_sock ); - return; + return -1; } ptr = (POINTER *)realloc( My_Connections, sizeof( CONNECTION ) * new_size ); @@ -962,7 +984,7 @@ New_Connection( int Sock ) Log( LOG_EMERG, "Can't allocate memory! [New_Connection]" ); Simple_Message( new_sock, "ERROR: Internal error" ); close( new_sock ); - return; + return -1; } #ifdef DEBUG @@ -986,7 +1008,7 @@ New_Connection( int Sock ) Log( LOG_ALERT, "Can't accept connection: can't create client structure!" ); Simple_Message( new_sock, "ERROR :Internal error" ); close( new_sock ); - return; + return -1; } /* Verbindung registrieren */ @@ -998,7 +1020,7 @@ New_Connection( int Sock ) if (!io_event_create( new_sock, IO_WANTREAD, cb_clientserver)) { Simple_Message( new_sock, "ERROR :Internal error" ); Conn_Close( idx, "io_event_create() failed", NULL, false ); - return; + return -1; } Log( LOG_INFO, "Accepted connection %d from %s:%d on socket %d.", idx, inet_ntoa( new_addr.sin_addr ), ntohs( new_addr.sin_port), Sock ); @@ -1016,6 +1038,7 @@ New_Connection( int Sock ) /* Penalty-Zeit setzen */ Conn_SetPenalty( idx, 4 ); + return new_sock; } /* New_Connection */ @@ -1525,8 +1548,7 @@ void Read_Resolver_Result( int r_fd ) /* Read result from pipe */ bytes_read = read( r_fd, readbuf, sizeof readbuf -1 ); - if( bytes_read < 0 ) - { + if( bytes_read < 0 ) { /* Error! */ Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror( errno )); FreeRes_stat( &My_Connections[i] ); @@ -1534,7 +1556,7 @@ void Read_Resolver_Result( int r_fd ) } len = (unsigned int) bytes_read; readbuf[len] = '\0'; - if (!array_catb(&s->buffer, readbuf, len)) { + if (!array_catb(&s->buffer, readbuf, len)) { Log( LOG_CRIT, "Resolver: Can't append result %s to buffer: %s", readbuf, strerror( errno )); FreeRes_stat(&My_Connections[i]); return; @@ -1614,7 +1636,7 @@ try_resolve: /* Search server ... */ n = Conf_GetServer( i ); assert( n > NONE ); - + bufptr = (char*) array_start(&s->buffer); strlcpy( Conf_Server[n].ip, bufptr, sizeof( Conf_Server[n].ip )); }