X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=ngircd-alex.git;a=blobdiff_plain;f=src%2Fngircd%2Fparse.c;h=453275377175f0e1b86809fa8007c42bd0f3fc55;hp=5329e3c331a2c7b9b8e8bb376f42b056d10d1185;hb=fa3dbfd8d685a96031c5813128fa1453095ef413;hpb=1c8c92af424db1760111d96e5f4f9d18d3d39d53 diff --git a/src/ngircd/parse.c b/src/ngircd/parse.c index 5329e3c3..45327537 100644 --- a/src/ngircd/parse.c +++ b/src/ngircd/parse.c @@ -9,112 +9,56 @@ * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS. * - * $Id: parse.c,v 1.16 2002/01/05 23:23:20 alex Exp $ + * $Id: parse.c,v 1.45 2002/11/22 23:33:00 alex Exp $ * * parse.c: Parsen der Client-Anfragen - * - * $Log: parse.c,v $ - * Revision 1.16 2002/01/05 23:23:20 alex - * - generisches Forwarding von Zahlen-Statuscodes implementiert. - * - * Revision 1.15 2002/01/05 01:42:08 alex - * - an Server werden keine ERRORS mehr wegen unbekannter Befehle geschickt. - * - * Revision 1.14 2002/01/04 17:56:45 alex - * - neuer Befehl SQUIT. - * - * Revision 1.13 2002/01/04 01:20:02 alex - * - Client-Strukruren werden nur noch ueber Funktionen angesprochen. - * - * Revision 1.12 2002/01/03 02:24:49 alex - * - neue Befehle NJOIN und SERVER begonnen. - * - * Revision 1.11 2002/01/02 02:43:22 alex - * - Copyright-Texte aktualisiert. - * - neuer Befehl ERROR. - * - * Revision 1.10 2001/12/31 15:33:13 alex - * - neuer Befehl NAMES, kleinere Bugfixes. - * - Bug bei PING behoben: war zu restriktiv implementiert :-) - * - * Revision 1.9 2001/12/31 02:18:51 alex - * - viele neue Befehle (WHOIS, ISON, OPER, DIE, RESTART), - * - neuen Header "defines.h" mit (fast) allen Konstanten. - * - Code Cleanups und viele "kleine" Aenderungen & Bugfixes. - * - * Revision 1.8 2001/12/29 03:08:19 alex - * - Fuehrende und folgende Leerzeichen etc. in Requests werden geloescht. - * - Logmeldungen (mal wieder) ein wenig angepasst. - * - * Revision 1.7 2001/12/27 19:13:21 alex - * - neue Befehle NOTICE und PRIVMSG. - * - Debug-Logging ein wenig reduziert. - * - * Revision 1.6 2001/12/26 14:45:37 alex - * - "Code Cleanups". - * - * Revision 1.5 2001/12/26 03:23:03 alex - * - PING/PONG-Befehle implementiert. - * - * Revision 1.4 2001/12/25 22:04:26 alex - * - Aenderungen an den Debug- und Logging-Funktionen. - * - * Revision 1.3 2001/12/25 19:18:36 alex - * - Gross- und Kleinschreibung der IRC-Befehle wird ignoriert. - * - bessere Debug-Ausgaben. - * - * Revision 1.2 2001/12/23 21:56:47 alex - * - bessere Debug-Ausgaben, - * - Bug im Parameter-Parser behoben (bei "langem" Parameter) - * - erste IRC-Befehle werden erkannt :-) - * - * Revision 1.1 2001/12/21 23:53:16 alex - * - Modul zum Parsen von Client-Requests begonnen. */ -#include -#include "global.h" +#include "portab.h" -#include +#include "imp.h" #include #include #include #include -#include "client.h" +#include "ngircd.h" +#include "defines.h" #include "conn.h" -#include "irc.h" +#include "client.h" +#include "channel.h" #include "log.h" #include "messages.h" #include "tool.h" -#include +#include "exp.h" #include "parse.h" +#include "imp.h" +#include "irc.h" +#include "irc-channel.h" +#include "irc-login.h" +#include "irc-mode.h" +#include "irc-op.h" +#include "irc-oper.h" +#include "irc-server.h" +#include "irc-write.h" -LOCAL VOID Init_Request( REQUEST *Req ); - -LOCAL BOOLEAN Parse_Error( CONN_ID Idx, CHAR *Error ); - -LOCAL BOOLEAN Validate_Prefix( REQUEST *Req ); -LOCAL BOOLEAN Validate_Command( REQUEST *Req ); -LOCAL BOOLEAN Validate_Args( REQUEST *Req ); - -LOCAL BOOLEAN Handle_Request( CONN_ID Idx, REQUEST *Req ); +#include "exp.h" -GLOBAL VOID Parse_Init( VOID ) -{ -} /* Parse_Init */ +LOCAL 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 )); -GLOBAL VOID Parse_Exit( VOID ) -{ -} /* Parse_Exit */ +LOCAL BOOLEAN Handle_Request PARAMS(( CONN_ID Idx, REQUEST *Req )); -GLOBAL BOOLEAN Parse_Request( CONN_ID Idx, CHAR *Request ) +GLOBAL BOOLEAN +Parse_Request( CONN_ID Idx, CHAR *Request ) { /* Client-Request parsen. Bei einem schwerwiegenden Fehler wird * die Verbindung geschlossen und FALSE geliefert. @@ -122,14 +66,15 @@ GLOBAL BOOLEAN Parse_Request( CONN_ID Idx, CHAR *Request ) REQUEST req; CHAR *start, *ptr; + BOOLEAN closed; assert( Idx >= 0 ); assert( Request != NULL ); #ifdef SNIFFER - Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request ); + if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request ); #endif - + Init_Request( &req ); /* Fuehrendes und folgendes "Geraffel" verwerfen */ @@ -141,20 +86,37 @@ GLOBAL BOOLEAN Parse_Request( CONN_ID Idx, CHAR *Request ) /* Prefix vorhanden */ req.prefix = Request + 1; ptr = strchr( Request, ' ' ); - if( ! ptr ) return Parse_Error( Idx, "Prefix without command!?" ); + if( ! ptr ) + { + Log( LOG_DEBUG, "Connection %d: Parse error: prefix without command!?", Idx ); + return Conn_WriteStr( Idx, "ERROR :Prefix without command!?" ); + } *ptr = '\0'; +#ifndef STRICT_RFC + /* multiple Leerzeichen als Trenner zwischen + * Prefix und Befehl ignorieren */ + while( *(ptr + 1) == ' ' ) ptr++; +#endif start = ptr + 1; } else start = Request; - if( ! Validate_Prefix( &req )) return Parse_Error( Idx, "Invalid prefix"); + if( ! Validate_Prefix( Idx, &req, &closed )) return ! closed; /* Befehl */ ptr = strchr( start, ' ' ); - if( ptr ) *ptr = '\0'; + if( ptr ) + { + *ptr = '\0'; +#ifndef STRICT_RFC + /* multiple Leerzeichen als Trenner vor + *Parametertrennern ignorieren */ + while( *(ptr + 1) == ' ' ) ptr++; +#endif + } req.command = start; - if( ! Validate_Command( &req )) return Parse_Error( Idx, "Invalid command" ); + if( ! Validate_Command( Idx, &req, &closed )) return ! closed; /* Argumente, Parameter */ if( ptr ) @@ -173,31 +135,40 @@ GLOBAL BOOLEAN Parse_Request( CONN_ID Idx, CHAR *Request ) { req.argv[req.argc] = start; ptr = strchr( start, ' ' ); - if( ptr ) *ptr = '\0'; + if( ptr ) + { + *ptr = '\0'; +#ifndef STRICT_RFC + /* multiple Leerzeichen als + * Parametertrenner ignorieren */ + while( *(ptr + 1) == ' ' ) ptr++; +#endif + } } - + req.argc++; if( start[0] == ':' ) break; if( req.argc > 14 ) break; - + if( ptr ) start = ptr + 1; else start = NULL; } } - - if( ! Validate_Args( &req )) return Parse_Error( Idx, "Invalid argument(s)" ); + + if( ! Validate_Args( Idx, &req, &closed )) return ! closed; return Handle_Request( Idx, &req ); } /* Parse_Request */ -LOCAL VOID Init_Request( REQUEST *Req ) +LOCAL VOID +Init_Request( REQUEST *Req ) { /* Neue Request-Struktur initialisieren */ INT i; - + assert( Req != NULL ); Req->prefix = NULL; @@ -207,42 +178,83 @@ LOCAL VOID Init_Request( REQUEST *Req ) } /* Init_Request */ -LOCAL BOOLEAN Parse_Error( CONN_ID Idx, CHAR *Error ) +LOCAL BOOLEAN +Validate_Prefix( CONN_ID Idx, REQUEST *Req, BOOLEAN *Closed ) { - /* Fehler beim Parsen. Fehlermeldung an den Client schicken. - * TRUE: Connection wurde durch diese Funktion nicht geschlossen, - * FALSE: Connection wurde terminiert. */ - + CLIENT *client, *c; + assert( Idx >= 0 ); - assert( Error != NULL ); + assert( Req != NULL ); - Log( LOG_DEBUG, "Connection %d: Parse error: %s", Idx, Error ); - return Conn_WriteStr( Idx, "ERROR :Parse error: %s", Error ); -} /* Parse_Error */ + *Closed = FALSE; + /* ist ueberhaupt ein Prefix vorhanden? */ + if( ! Req->prefix ) return TRUE; + + /* Client-Struktur der Connection ermitteln */ + client = Client_GetFromConn( Idx ); + assert( client != NULL ); + + /* nur validieren, wenn bereits registrierte Verbindung */ + if(( Client_Type( client ) != CLIENT_USER ) && ( Client_Type( client ) != CLIENT_SERVER ) && ( Client_Type( client ) != CLIENT_SERVICE )) + { + /* noch nicht registrierte Verbindung. + * Das Prefix wird ignoriert. */ + Req->prefix = NULL; + return TRUE; + } + + /* pruefen, ob der im Prefix angegebene Client bekannt ist */ + c = Client_Search( Req->prefix ); + if( ! c ) + { + /* im Prefix angegebener Client ist nicht bekannt */ + Log( LOG_ERR, "Invalid prefix \"%s\", client not known (connection %d)!?", Req->prefix, Idx ); + 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 + * des Senders liegt, d.h. sicherstellen, dass das Prefix nicht + * gefaelscht ist */ + if( Client_NextHop( c ) != client ) + { + /* das angegebene Prefix ist aus dieser Richtung, also + * aus der gegebenen Connection, ungueltig! */ + Log( LOG_ERR, "Spoofed prefix \"%s\" from \"%s\" (connection %d)!", Req->prefix, Client_Mask( Client_GetFromConn( Idx )), Idx ); + Conn_Close( Idx, NULL, "Spoofed prefix", TRUE ); + *Closed = TRUE; + return FALSE; + } -LOCAL BOOLEAN Validate_Prefix( REQUEST *Req ) -{ - assert( Req != NULL ); return TRUE; } /* Validate_Prefix */ -LOCAL BOOLEAN Validate_Command( REQUEST *Req ) +LOCAL BOOLEAN +Validate_Command( CONN_ID Idx, REQUEST *Req, BOOLEAN *Closed ) { + assert( Idx >= 0 ); assert( Req != NULL ); + *Closed = FALSE; + return TRUE; } /* Validate_Comman */ -LOCAL BOOLEAN Validate_Args( REQUEST *Req ) +LOCAL BOOLEAN +Validate_Args( CONN_ID Idx, REQUEST *Req, BOOLEAN *Closed ) { + assert( Idx >= 0 ); assert( Req != NULL ); + *Closed = FALSE; + return TRUE; } /* Validate_Args */ -LOCAL BOOLEAN Handle_Request( CONN_ID Idx, REQUEST *Req ) +LOCAL BOOLEAN +Handle_Request( CONN_ID Idx, REQUEST *Req ) { /* Client-Request verarbeiten. Bei einem schwerwiegenden Fehler * wird die Verbindung geschlossen und FALSE geliefert. */ @@ -264,14 +276,32 @@ LOCAL BOOLEAN Handle_Request( CONN_ID Idx, REQUEST *Req ) /* Befehl ist ein Statuscode */ /* Zielserver ermitteln */ - if(( Client_Type( client ) == CLIENT_SERVER ) && ( Req->argc > 0 )) target = Client_GetFromID( Req->argv[0] ); + if(( Client_Type( client ) == CLIENT_SERVER ) && ( Req->argc > 0 )) target = Client_Search( Req->argv[0] ); else target = NULL; - if( ! target ) return TRUE; + 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; + } + if( target == Client_ThisServer( )) + { + Log( LOG_DEBUG, "Ignored status code %s from \"%s\".", Req->command, Client_ID( client )); + return TRUE; + } /* Quell-Client ermitteln */ - if( ! Req->prefix[0] ) return TRUE; - else prefix = Client_GetFromID( Req->prefix ); - if( ! prefix ) return TRUE; + if( ! Req->prefix[0] ) + { + Log( LOG_WARNING, "Got status code without prefix!?" ); + return TRUE; + } + else prefix = Client_Search( Req->prefix ); + if( ! prefix ) + { + Log( LOG_WARNING, "Got status code from unknown source: \"%s\"", Req->prefix ); + return TRUE; + } /* Statuscode weiterleiten */ strcpy( str, Req->command ); @@ -281,33 +311,52 @@ LOCAL BOOLEAN Handle_Request( CONN_ID Idx, REQUEST *Req ) else strcat( str, " :" ); strcat( str, Req->argv[i] ); } - return IRC_WriteStrClientPrefix( target, prefix, str ); + return IRC_WriteStrClientPrefix( target, prefix, "%s", str ); } - if( strcasecmp( Req->command, "PASS" ) == 0 ) return IRC_PASS( client, Req ); + if( strcasecmp( Req->command, "ADMIN" ) == 0 ) return IRC_ADMIN( client, Req ); + else if( strcasecmp( Req->command, "AWAY" ) == 0 ) return IRC_AWAY( client, Req ); + else if( strcasecmp( Req->command, "CONNECT" ) == 0 ) return IRC_CONNECT( client, Req ); + else if( strcasecmp( Req->command, "DIE" ) == 0 ) return IRC_DIE( client, Req ); + else if( strcasecmp( Req->command, "ERROR" ) == 0 ) return IRC_ERROR( client, Req ); + else if( strcasecmp( Req->command, "INVITE" ) == 0 ) return IRC_INVITE( client, Req ); + else if( strcasecmp( Req->command, "ISON" ) == 0 ) return IRC_ISON( client, Req ); + else if( strcasecmp( Req->command, "JOIN" ) == 0 ) return IRC_JOIN( client, Req ); + else if( strcasecmp( Req->command, "KICK" ) == 0 ) return IRC_KICK( client, Req ); + else if( strcasecmp( Req->command, "KILL" ) == 0 ) return IRC_KILL( client, Req ); + else if( strcasecmp( Req->command, "LINKS" ) == 0 ) return IRC_LINKS( client, Req ); + else if( strcasecmp( Req->command, "LIST" ) == 0 ) return IRC_LIST( client, Req ); + else if( strcasecmp( Req->command, "LUSERS" ) == 0 ) return IRC_LUSERS( client, Req ); + else if( strcasecmp( Req->command, "MODE" ) == 0 ) return IRC_MODE( client, Req ); + else if( strcasecmp( Req->command, "MOTD" ) == 0 ) return IRC_MOTD( client, Req ); + else if( strcasecmp( Req->command, "NAMES" ) == 0 ) return IRC_NAMES( client, Req ); else if( strcasecmp( Req->command, "NICK" ) == 0 ) return IRC_NICK( client, Req ); - else if( strcasecmp( Req->command, "USER" ) == 0 ) return IRC_USER( client, Req ); - else if( strcasecmp( Req->command, "SERVER" ) == 0 ) return IRC_SERVER( client, Req ); else if( strcasecmp( Req->command, "NJOIN" ) == 0 ) return IRC_NJOIN( client, Req ); - else if( strcasecmp( Req->command, "QUIT" ) == 0 ) return IRC_QUIT( client, Req ); - else if( strcasecmp( Req->command, "SQUIT" ) == 0 ) return IRC_SQUIT( client, Req ); + else if( strcasecmp( Req->command, "NOTICE" ) == 0 ) return IRC_NOTICE( client, Req ); + else if( strcasecmp( Req->command, "OPER" ) == 0 ) return IRC_OPER( client, Req ); + else if( strcasecmp( Req->command, "PART" ) == 0 ) return IRC_PART( client, Req ); + else if( strcasecmp( Req->command, "PASS" ) == 0 ) return IRC_PASS( client, Req ); else if( strcasecmp( Req->command, "PING" ) == 0 ) return IRC_PING( client, Req ); else if( strcasecmp( Req->command, "PONG" ) == 0 ) return IRC_PONG( client, Req ); - else if( strcasecmp( Req->command, "MOTD" ) == 0 ) return IRC_MOTD( client, Req ); else if( strcasecmp( Req->command, "PRIVMSG" ) == 0 ) return IRC_PRIVMSG( client, Req ); - else if( strcasecmp( Req->command, "NOTICE" ) == 0 ) return IRC_NOTICE( client, Req ); - else if( strcasecmp( Req->command, "MODE" ) == 0 ) return IRC_MODE( client, Req ); - else if( strcasecmp( Req->command, "NAMES" ) == 0 ) return IRC_NAMES( client, Req ); - else if( strcasecmp( Req->command, "ISON" ) == 0 ) return IRC_ISON( client, Req ); - else if( strcasecmp( Req->command, "WHOIS" ) == 0 ) return IRC_WHOIS( client, Req ); - else if( strcasecmp( Req->command, "USERHOST" ) == 0 ) return IRC_USERHOST( client, Req ); - else if( strcasecmp( Req->command, "OPER" ) == 0 ) return IRC_OPER( client, Req ); - else if( strcasecmp( Req->command, "DIE" ) == 0 ) return IRC_DIE( client, Req ); + else if( strcasecmp( Req->command, "QUIT" ) == 0 ) return IRC_QUIT( client, Req ); + else if( strcasecmp( Req->command, "REHASH" ) == 0 ) return IRC_REHASH( client, Req ); else if( strcasecmp( Req->command, "RESTART" ) == 0 ) return IRC_RESTART( client, Req ); - else if( strcasecmp( Req->command, "ERROR" ) == 0 ) return IRC_ERROR( client, Req ); + else if( strcasecmp( Req->command, "SERVER" ) == 0 ) return IRC_SERVER( client, Req ); + else if( strcasecmp( Req->command, "SQUIT" ) == 0 ) return IRC_SQUIT( client, Req ); + else if( strcasecmp( Req->command, "TOPIC" ) == 0 ) return IRC_TOPIC( client, Req ); + else if( strcasecmp( Req->command, "USER" ) == 0 ) return IRC_USER( client, Req ); + else if( strcasecmp( Req->command, "USERHOST" ) == 0 ) return IRC_USERHOST( client, Req ); + else if( strcasecmp( Req->command, "VERSION" ) == 0 ) return IRC_VERSION( client, Req ); + else if( strcasecmp( Req->command, "WHO" ) == 0 ) return IRC_WHO( client, Req ); + else if( strcasecmp( Req->command, "WHOIS" ) == 0 ) return IRC_WHOIS( client, Req ); + else if( strcasecmp( Req->command, "WHOWAS" ) == 0 ) return IRC_WHOWAS( client, Req ); +#ifdef IRCPLUS + else if( strcasecmp( Req->command, "CHANINFO" ) == 0 ) return IRC_CHANINFO( client, Req ); +#endif /* Unbekannter Befehl */ - if( Client_Type( client ) != CLIENT_SERVER ) IRC_WriteStrClient( client, Client_ThisServer( ), ERR_UNKNOWNCOMMAND_MSG, Client_ID( client ), Req->command ); + if( Client_Type( client ) != CLIENT_SERVER ) IRC_WriteStrClient( client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( client ), Req->command ); 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" ); return TRUE;