X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=ngircd-alex.git;a=blobdiff_plain;f=src%2Fngircd%2Fparse.c;h=5109169b72a068b8ff4655adda8bcb6637c093b1;hp=ac24dbf985f2c5b5f215e321099ceca158e66d01;hb=47ca178a219d682c589b27e64ee1a4e936cc7bdc;hpb=17f7c6d3edf26e644b98e2b11fd3b1959ac8c0e6 diff --git a/src/ngircd/parse.c b/src/ngircd/parse.c index ac24dbf9..5109169b 100644 --- a/src/ngircd/parse.c +++ b/src/ngircd/parse.c @@ -7,24 +7,28 @@ * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * Please read the file COPYING, README and AUTHORS for more information. - * - * IRC command parser and validator */ #include "portab.h" -static char UNUSED id[] = "$Id: parse.c,v 1.52 2002/12/18 13:53:20 alex Exp $"; +static char UNUSED id[] = "$Id: parse.c,v 1.69 2007/11/21 12:16:36 alex Exp $"; + +/** + * @file + * IRC command parser and validator. + */ #include "imp.h" #include #include #include #include +#include #include "ngircd.h" #include "defines.h" -#include "conn.h" +#include "conn-func.h" #include "client.h" #include "channel.h" #include "log.h" @@ -44,6 +48,7 @@ static char UNUSED id[] = "$Id: parse.c,v 1.52 2002/12/18 13:53:20 alex Exp $"; #include "irc-oper.h" #include "irc-server.h" #include "irc-write.h" +#include "numeric.h" #include "exp.h" @@ -54,7 +59,9 @@ COMMAND My_Commands[] = { "AWAY", IRC_AWAY, CLIENT_USER, 0, 0, 0 }, { "CONNECT", IRC_CONNECT, CLIENT_USER, 0, 0, 0 }, { "DIE", IRC_DIE, CLIENT_USER, 0, 0, 0 }, + { "DISCONNECT", IRC_DISCONNECT, CLIENT_USER, 0, 0, 0 }, { "ERROR", IRC_ERROR, 0xFFFF, 0, 0, 0 }, + { "HELP", IRC_HELP, CLIENT_USER, 0, 0, 0 }, { "INVITE", IRC_INVITE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 }, { "ISON", IRC_ISON, CLIENT_USER, 0, 0, 0 }, { "JOIN", IRC_JOIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 }, @@ -83,9 +90,11 @@ COMMAND My_Commands[] = { "STATS", IRC_STATS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 }, { "TIME", IRC_TIME, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 }, { "TOPIC", IRC_TOPIC, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 }, + { "TRACE", IRC_TRACE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 }, { "USER", IRC_USER, 0xFFFF, 0, 0, 0 }, { "USERHOST", IRC_USERHOST, CLIENT_USER, 0, 0, 0 }, { "VERSION", IRC_VERSION, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 }, + { "WALLOPS", IRC_WALLOPS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 }, { "WHO", IRC_WHO, CLIENT_USER, 0, 0, 0 }, { "WHOIS", IRC_WHOIS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 }, { "WHOWAS", IRC_WHOWAS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 }, @@ -95,33 +104,60 @@ COMMAND My_Commands[] = { NULL, NULL, 0x0, 0, 0, 0 } /* Ende-Marke */ }; +NUMERIC My_Numerics[] = +{ + { 005, IRC_Num_ISUPPORT }, + { 376, IRC_Num_ENDOFMOTD }, + { 0, NULL } /* end marker */ +}; + -LOCAL VOID Init_Request PARAMS(( REQUEST *Req )); +static void Init_Request PARAMS(( REQUEST *Req )); -LOCAL BOOLEAN Validate_Prefix PARAMS(( CONN_ID Idx, REQUEST *Req, BOOLEAN *Closed )); -LOCAL BOOLEAN Validate_Command PARAMS(( CONN_ID Idx, REQUEST *Req, BOOLEAN *Closed )); -LOCAL BOOLEAN Validate_Args PARAMS(( CONN_ID Idx, REQUEST *Req, BOOLEAN *Closed )); +static bool Validate_Prefix PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed )); +static bool Validate_Command PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed )); +static bool Validate_Args PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed )); -LOCAL BOOLEAN Handle_Request PARAMS(( CONN_ID Idx, REQUEST *Req )); +static bool Handle_Request PARAMS(( CONN_ID Idx, REQUEST *Req )); +/** + * Return the pointer to the global "IRC command structure". + * This structure, an array of type "COMMAND" describes all the IRC commands + * implemented by ngIRCd and how to handle them. + * @return Pointer to the global command structure. + */ GLOBAL COMMAND * -Parse_GetCommandStruct( VOID ) +Parse_GetCommandStruct( void ) { return My_Commands; } /* Parse_GetCommandStruct */ -GLOBAL BOOLEAN -Parse_Request( CONN_ID Idx, CHAR *Request ) +/** + * Parse a command ("request") received from a client. + * + * This function is called after the connection layer received a valid CR+LF + * terminated line of text: we asume that this is a valid IRC command and + * try to do something useful with it :-) + * + * All errors are reported to the client from which the command has been + * received, and if the error is fatal this connection is closed down. + * + * This function is able to parse the syntax as described in RFC 2812, + * section 2.3. + * + * @param Idx Index of the connection from which the command has been received. + * @param Request NULL terminated line of text (the "command"). + * @return true on success (valid command or "regular" error), false if a + * fatal error occured and the connection has been shut down. + */ +GLOBAL bool +Parse_Request( CONN_ID Idx, char *Request ) { - /* Client-Request parsen. Bei einem schwerwiegenden Fehler wird - * die Verbindung geschlossen und FALSE geliefert. - * Der Aufbau gueltiger Requests ist in RFC 2812, 2.3 definiert. */ - REQUEST req; - CHAR *start, *ptr; - BOOLEAN closed; + char *start, *ptr; + bool closed; assert( Idx >= 0 ); assert( Request != NULL ); @@ -163,7 +199,7 @@ Parse_Request( CONN_ID Idx, CHAR *Request ) *ptr = '\0'; #ifndef STRICT_RFC /* multiple Leerzeichen als Trenner vor - *Parametertrennern ignorieren */ + * Parametern ignorieren */ while( *(ptr + 1) == ' ' ) ptr++; #endif } @@ -216,12 +252,16 @@ Parse_Request( CONN_ID Idx, CHAR *Request ) } /* Parse_Request */ -LOCAL VOID +/** + * Initialize request structure. + * @param Req Request structure to be initialized. + */ +static void Init_Request( REQUEST *Req ) { /* Neue Request-Struktur initialisieren */ - INT i; + int i; assert( Req != NULL ); @@ -232,21 +272,21 @@ Init_Request( REQUEST *Req ) } /* Init_Request */ -LOCAL BOOLEAN -Validate_Prefix( CONN_ID Idx, REQUEST *Req, BOOLEAN *Closed ) +static bool +Validate_Prefix( CONN_ID Idx, REQUEST *Req, bool *Closed ) { CLIENT *client, *c; assert( Idx >= 0 ); assert( Req != NULL ); - *Closed = FALSE; + *Closed = false; /* ist ueberhaupt ein Prefix vorhanden? */ - if( ! Req->prefix ) return TRUE; + if( ! Req->prefix ) return true; /* Client-Struktur der Connection ermitteln */ - client = Client_GetFromConn( Idx ); + client = Conn_GetClient( Idx ); assert( client != NULL ); /* nur validieren, wenn bereits registrierte Verbindung */ @@ -255,7 +295,7 @@ Validate_Prefix( CONN_ID Idx, REQUEST *Req, BOOLEAN *Closed ) /* noch nicht registrierte Verbindung. * Das Prefix wird ignoriert. */ Req->prefix = NULL; - return TRUE; + return true; } /* pruefen, ob der im Prefix angegebene Client bekannt ist */ @@ -264,8 +304,8 @@ Validate_Prefix( CONN_ID Idx, REQUEST *Req, BOOLEAN *Closed ) { /* im Prefix angegebener Client ist nicht bekannt */ Log( LOG_ERR, "Invalid prefix \"%s\", client not known (connection %d, command %s)!?", Req->prefix, Idx, Req->command ); - if( ! Conn_WriteStr( Idx, "ERROR :Invalid prefix \"%s\", client not known!?", Req->prefix )) *Closed = TRUE; - return FALSE; + if( ! Conn_WriteStr( Idx, "ERROR :Invalid prefix \"%s\", client not known!?", Req->prefix )) *Closed = true; + return false; } /* pruefen, ob der Client mit dem angegebenen Prefix in Richtung @@ -275,97 +315,122 @@ Validate_Prefix( CONN_ID Idx, REQUEST *Req, BOOLEAN *Closed ) { /* das angegebene Prefix ist aus dieser Richtung, also * aus der gegebenen Connection, ungueltig! */ - Log( LOG_ERR, "Spoofed prefix \"%s\" from \"%s\" (connection %d, command %s)!", Req->prefix, Client_Mask( Client_GetFromConn( Idx )), Idx, Req->command ); - Conn_Close( Idx, NULL, "Spoofed prefix", TRUE ); - *Closed = TRUE; - return FALSE; + Log( LOG_ERR, "Spoofed prefix \"%s\" from \"%s\" (connection %d, command %s)!", Req->prefix, Client_Mask( Conn_GetClient( Idx )), Idx, Req->command ); + Conn_Close( Idx, NULL, "Spoofed prefix", true); + *Closed = true; + return false; } - return TRUE; + return true; } /* Validate_Prefix */ -LOCAL BOOLEAN -Validate_Command( CONN_ID Idx, REQUEST *Req, BOOLEAN *Closed ) +static bool +Validate_Command( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed ) { assert( Idx >= 0 ); assert( Req != NULL ); - *Closed = FALSE; + *Closed = false; - return TRUE; + return true; } /* Validate_Comman */ -LOCAL BOOLEAN -Validate_Args( CONN_ID Idx, REQUEST *Req, BOOLEAN *Closed ) +static bool +Validate_Args( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed ) { assert( Idx >= 0 ); assert( Req != NULL ); - *Closed = FALSE; + *Closed = false; - return TRUE; + return true; } /* Validate_Args */ -LOCAL BOOLEAN +static bool Handle_Request( CONN_ID Idx, REQUEST *Req ) { /* Client-Request verarbeiten. Bei einem schwerwiegenden Fehler - * wird die Verbindung geschlossen und FALSE geliefert. */ + * wird die Verbindung geschlossen und false geliefert. */ CLIENT *client, *target, *prefix; - CHAR str[LINE_LEN]; - BOOLEAN result; + char str[LINE_LEN]; + bool result; COMMAND *cmd; - INT i; + NUMERIC *num; + int i; assert( Idx >= 0 ); assert( Req != NULL ); assert( Req->command != NULL ); - client = Client_GetFromConn( Idx ); + client = Conn_GetClient( Idx ); assert( client != NULL ); - /* Statuscode, der geforwarded werden muss? */ - if(( strlen( Req->command ) == 3 ) && ( atoi( Req->command ) > 100 )) - { - /* Befehl ist ein Statuscode */ + /* Numeric? */ + if ((Client_Type(client) == CLIENT_SERVER || + Client_Type(client) == CLIENT_UNKNOWNSERVER) + && strlen(Req->command) == 3 && atoi(Req->command) > 1) { + /* Command is a status code ("numeric") from an other server */ - /* Zielserver ermitteln */ - if(( Client_Type( client ) == CLIENT_SERVER ) && ( Req->argc > 0 )) target = Client_Search( Req->argv[0] ); - else target = NULL; - if( ! target ) - { - if( Req->argc > 0 ) Log( LOG_WARNING, "Unknown target for status code %s: \"%s\"", Req->command, Req->argv[0] ); - else Log( LOG_WARNING, "Unknown target for status code %s!", Req->command ); - return TRUE; + /* Determine target */ + if (Req->argc > 0) + target = Client_Search( Req->argv[0] ); + else + target = NULL; + if (!target) { + /* Status code without target!? */ + if (Req->argc > 0) + Log(LOG_WARNING, + "Unknown target for status code %s: \"%s\"", + Req->command, Req->argv[0]); + else + Log(LOG_WARNING, + "Unknown target for status code %s!", + Req->command); + return true; } - if( target == Client_ThisServer( )) - { - Log( LOG_DEBUG, "Ignored status code %s from \"%s\".", Req->command, Client_ID( client )); - return TRUE; + if (target == Client_ThisServer()) { + /* This server is the target of the numeric */ + i = atoi(Req->command); + + num = My_Numerics; + while (num->numeric > 0) { + if (i != num->numeric) { + num++; + continue; + } + result = (num->function)(client, Req); + return result; + } + + LogDebug("Ignored status code %s from \"%s\".", + Req->command, Client_ID(client)); + return true; } - /* Quell-Client ermitteln */ + /* Determine source */ if( ! Req->prefix[0] ) { - Log( LOG_WARNING, "Got status code without prefix!?" ); - return TRUE; + /* Oops, no prefix!? */ + Log( LOG_WARNING, "Got status code %s from \"%s\" without prefix!?", Req->command, Client_ID( client )); + return true; } else prefix = Client_Search( Req->prefix ); if( ! prefix ) { - Log( LOG_WARNING, "Got status code from unknown source: \"%s\"", Req->prefix ); - return TRUE; + /* Oops, unknown prefix!? */ + Log( LOG_WARNING, "Got status code %s from unknown source: \"%s\"", Req->command, Req->prefix ); + return true; } - /* Statuscode weiterleiten */ - strcpy( str, Req->command ); + /* Forward status code */ + strlcpy( str, Req->command, sizeof( str )); for( i = 0; i < Req->argc; i++ ) { - if( i < Req->argc - 1 ) strcat( str, " " ); - else strcat( str, " :" ); - strcat( str, Req->argv[i] ); + if( i < Req->argc - 1 ) strlcat( str, " ", sizeof( str )); + else strlcat( str, " :", sizeof( str )); + strlcat( str, Req->argv[i], sizeof( str )); } return IRC_WriteStrClientPrefix( target, prefix, "%s", str ); } @@ -398,11 +463,26 @@ Handle_Request( CONN_ID Idx, REQUEST *Req ) return IRC_WriteStrClient( client, ERR_NOTREGISTERED_MSG, Client_ID( client )); } } + + if( Client_Type( client ) != CLIENT_USER && + Client_Type( client ) != CLIENT_SERVER && + Client_Type( client ) != CLIENT_SERVICE ) + return true; - /* Unbekannter Befehl */ - Log( LOG_DEBUG, "Connection %d: Unknown command \"%s\", %d %s,%s prefix.", Client_Conn( client ), Req->command, Req->argc, Req->argc == 1 ? "parameter" : "parameters", Req->prefix ? "" : " no" ); - if( Client_Type( client ) != CLIENT_SERVER ) return IRC_WriteStrClient( client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( client ), Req->command ); - else return TRUE; + /* Unknown command and registered connection: generate error: */ + Log( LOG_DEBUG, "Connection %d: Unknown command \"%s\", %d %s,%s prefix.", + Client_Conn( client ), Req->command, Req->argc, + Req->argc == 1 ? "parameter" : "parameters", + Req->prefix ? "" : " no" ); + + if (Client_Type(client) != CLIENT_SERVER) { + result = IRC_WriteStrClient(client, ERR_UNKNOWNCOMMAND_MSG, + Client_ID(client), Req->command); + Conn_SetPenalty(Idx, 1); + return result; + } + + return true; } /* Handle_Request */