2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
15 static char UNUSED id[] = "$Id: parse.c,v 1.69.2.1 2008/02/05 13:11:20 fw Exp $";
19 * IRC command parser and validator.
31 #include "conn-func.h"
43 #include "irc-channel.h"
45 #include "irc-login.h"
49 #include "irc-server.h"
50 #include "irc-write.h"
56 COMMAND My_Commands[] =
58 { "ADMIN", IRC_ADMIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
59 { "AWAY", IRC_AWAY, CLIENT_USER, 0, 0, 0 },
60 { "CONNECT", IRC_CONNECT, CLIENT_USER, 0, 0, 0 },
61 { "DIE", IRC_DIE, CLIENT_USER, 0, 0, 0 },
62 { "DISCONNECT", IRC_DISCONNECT, CLIENT_USER, 0, 0, 0 },
63 { "ERROR", IRC_ERROR, 0xFFFF, 0, 0, 0 },
64 { "HELP", IRC_HELP, CLIENT_USER, 0, 0, 0 },
65 { "INVITE", IRC_INVITE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
66 { "ISON", IRC_ISON, CLIENT_USER, 0, 0, 0 },
67 { "JOIN", IRC_JOIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
68 { "KICK", IRC_KICK, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
69 { "KILL", IRC_KILL, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
70 { "LINKS", IRC_LINKS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
71 { "LIST", IRC_LIST, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
72 { "LUSERS", IRC_LUSERS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
73 { "MODE", IRC_MODE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
74 { "MOTD", IRC_MOTD, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
75 { "NAMES", IRC_NAMES, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
76 { "NICK", IRC_NICK, 0xFFFF, 0, 0, 0 },
77 { "NJOIN", IRC_NJOIN, CLIENT_SERVER, 0, 0, 0 },
78 { "NOTICE", IRC_NOTICE, 0xFFFF, 0, 0, 0 },
79 { "OPER", IRC_OPER, CLIENT_USER, 0, 0, 0 },
80 { "PART", IRC_PART, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
81 { "PASS", IRC_PASS, 0xFFFF, 0, 0, 0 },
82 { "PING", IRC_PING, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
83 { "PONG", IRC_PONG, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
84 { "PRIVMSG", IRC_PRIVMSG, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
85 { "QUIT", IRC_QUIT, 0xFFFF, 0, 0, 0 },
86 { "REHASH", IRC_REHASH, CLIENT_USER, 0, 0, 0 },
87 { "RESTART", IRC_RESTART, CLIENT_USER, 0, 0, 0 },
88 { "SERVER", IRC_SERVER, 0xFFFF, 0, 0, 0 },
89 { "SQUIT", IRC_SQUIT, CLIENT_SERVER, 0, 0, 0 },
90 { "STATS", IRC_STATS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
91 { "TIME", IRC_TIME, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
92 { "TOPIC", IRC_TOPIC, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
93 { "TRACE", IRC_TRACE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
94 { "USER", IRC_USER, 0xFFFF, 0, 0, 0 },
95 { "USERHOST", IRC_USERHOST, CLIENT_USER, 0, 0, 0 },
96 { "VERSION", IRC_VERSION, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
97 { "WALLOPS", IRC_WALLOPS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
98 { "WHO", IRC_WHO, CLIENT_USER, 0, 0, 0 },
99 { "WHOIS", IRC_WHOIS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
100 { "WHOWAS", IRC_WHOWAS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
102 { "CHANINFO", IRC_CHANINFO, CLIENT_SERVER, 0, 0, 0 },
104 { NULL, NULL, 0x0, 0, 0, 0 } /* Ende-Marke */
107 NUMERIC My_Numerics[] =
109 { 005, IRC_Num_ISUPPORT },
110 { 376, IRC_Num_ENDOFMOTD },
111 { 0, NULL } /* end marker */
115 static void Init_Request PARAMS(( REQUEST *Req ));
117 static bool Validate_Prefix PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
118 static bool Validate_Command PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
119 static bool Validate_Args PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
121 static bool Handle_Request PARAMS(( CONN_ID Idx, REQUEST *Req ));
125 * Return the pointer to the global "IRC command structure".
126 * This structure, an array of type "COMMAND" describes all the IRC commands
127 * implemented by ngIRCd and how to handle them.
128 * @return Pointer to the global command structure.
131 Parse_GetCommandStruct( void )
134 } /* Parse_GetCommandStruct */
138 * Parse a command ("request") received from a client.
140 * This function is called after the connection layer received a valid CR+LF
141 * terminated line of text: we asume that this is a valid IRC command and
142 * try to do something useful with it :-)
144 * All errors are reported to the client from which the command has been
145 * received, and if the error is fatal this connection is closed down.
147 * This function is able to parse the syntax as described in RFC 2812,
150 * @param Idx Index of the connection from which the command has been received.
151 * @param Request NULL terminated line of text (the "command").
152 * @return true on success (valid command or "regular" error), false if a
153 * fatal error occured and the connection has been shut down.
156 Parse_Request( CONN_ID Idx, char *Request )
163 assert( Request != NULL );
166 if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request );
169 Init_Request( &req );
171 /* Fuehrendes und folgendes "Geraffel" verwerfen */
172 ngt_TrimStr( Request );
174 /* gibt es ein Prefix? */
175 if( Request[0] == ':' )
177 /* Prefix vorhanden */
178 req.prefix = Request + 1;
179 ptr = strchr( Request, ' ' );
182 Log( LOG_DEBUG, "Connection %d: Parse error: prefix without command!?", Idx );
183 return Conn_WriteStr( Idx, "ERROR :Prefix without command!?" );
187 /* multiple Leerzeichen als Trenner zwischen
188 * Prefix und Befehl ignorieren */
189 while( *(ptr + 1) == ' ' ) ptr++;
193 else start = Request;
196 ptr = strchr( start, ' ' );
201 /* multiple Leerzeichen als Trenner vor
202 * Parametern ignorieren */
203 while( *(ptr + 1) == ' ' ) ptr++;
208 /* Argumente, Parameter */
211 /* Prinzipiell gibt es welche :-) */
215 /* Parameter-String "zerlegen" */
216 if( start[0] == ':' )
218 req.argv[req.argc] = start + 1;
223 req.argv[req.argc] = start;
224 ptr = strchr( start, ' ' );
229 /* multiple Leerzeichen als
230 * Parametertrenner ignorieren */
231 while( *(ptr + 1) == ' ' ) ptr++;
238 if( start[0] == ':' ) break;
239 if( req.argc > 14 ) break;
241 if( ptr ) start = ptr + 1;
246 /* Daten validieren */
247 if( ! Validate_Prefix( Idx, &req, &closed )) return ! closed;
248 if( ! Validate_Command( Idx, &req, &closed )) return ! closed;
249 if( ! Validate_Args( Idx, &req, &closed )) return ! closed;
251 return Handle_Request( Idx, &req );
252 } /* Parse_Request */
256 * Initialize request structure.
257 * @param Req Request structure to be initialized.
260 Init_Request( REQUEST *Req )
262 /* Neue Request-Struktur initialisieren */
266 assert( Req != NULL );
270 for( i = 0; i < 15; Req->argv[i++] = NULL );
276 Validate_Prefix( CONN_ID Idx, REQUEST *Req, bool *Closed )
281 assert( Req != NULL );
285 /* ist ueberhaupt ein Prefix vorhanden? */
286 if( ! Req->prefix ) return true;
288 /* Client-Struktur der Connection ermitteln */
289 client = Conn_GetClient( Idx );
290 assert( client != NULL );
292 /* nur validieren, wenn bereits registrierte Verbindung */
293 if(( Client_Type( client ) != CLIENT_USER ) && ( Client_Type( client ) != CLIENT_SERVER ) && ( Client_Type( client ) != CLIENT_SERVICE ))
295 /* noch nicht registrierte Verbindung.
296 * Das Prefix wird ignoriert. */
301 /* pruefen, ob der im Prefix angegebene Client bekannt ist */
302 c = Client_Search( Req->prefix );
305 /* im Prefix angegebener Client ist nicht bekannt */
306 Log( LOG_ERR, "Invalid prefix \"%s\", client not known (connection %d, command %s)!?", Req->prefix, Idx, Req->command );
307 if( ! Conn_WriteStr( Idx, "ERROR :Invalid prefix \"%s\", client not known!?", Req->prefix )) *Closed = true;
311 /* pruefen, ob der Client mit dem angegebenen Prefix in Richtung
312 * des Senders liegt, d.h. sicherstellen, dass das Prefix nicht
314 if( Client_NextHop( c ) != client )
316 /* das angegebene Prefix ist aus dieser Richtung, also
317 * aus der gegebenen Connection, ungueltig! */
318 Log( LOG_ERR, "Spoofed prefix \"%s\" from \"%s\" (connection %d, command %s)!", Req->prefix, Client_Mask( Conn_GetClient( Idx )), Idx, Req->command );
319 Conn_Close( Idx, NULL, "Spoofed prefix", true);
325 } /* Validate_Prefix */
329 Validate_Command( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed )
332 assert( Req != NULL );
336 } /* Validate_Comman */
340 Validate_Args( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed )
343 assert( Req != NULL );
347 } /* Validate_Args */
351 Handle_Request( CONN_ID Idx, REQUEST *Req )
353 /* Client-Request verarbeiten. Bei einem schwerwiegenden Fehler
354 * wird die Verbindung geschlossen und false geliefert. */
356 CLIENT *client, *target, *prefix;
364 assert( Req != NULL );
365 assert( Req->command != NULL );
367 client = Conn_GetClient( Idx );
368 assert( client != NULL );
371 if ((Client_Type(client) == CLIENT_SERVER ||
372 Client_Type(client) == CLIENT_UNKNOWNSERVER)
373 && strlen(Req->command) == 3 && atoi(Req->command) > 1) {
374 /* Command is a status code ("numeric") from an other server */
376 /* Determine target */
378 target = Client_Search( Req->argv[0] );
382 /* Status code without target!? */
385 "Unknown target for status code %s: \"%s\"",
386 Req->command, Req->argv[0]);
389 "Unknown target for status code %s!",
393 if (target == Client_ThisServer()) {
394 /* This server is the target of the numeric */
395 i = atoi(Req->command);
398 while (num->numeric > 0) {
399 if (i != num->numeric) {
403 result = (num->function)(client, Req);
407 LogDebug("Ignored status code %s from \"%s\".",
408 Req->command, Client_ID(client));
412 /* Determine source */
413 if( ! Req->prefix[0] )
415 /* Oops, no prefix!? */
416 Log( LOG_WARNING, "Got status code %s from \"%s\" without prefix!?", Req->command, Client_ID( client ));
419 else prefix = Client_Search( Req->prefix );
422 /* Oops, unknown prefix!? */
423 Log( LOG_WARNING, "Got status code %s from unknown source: \"%s\"", Req->command, Req->prefix );
427 /* Forward status code */
428 strlcpy( str, Req->command, sizeof( str ));
429 for( i = 0; i < Req->argc; i++ )
431 if( i < Req->argc - 1 ) strlcat( str, " ", sizeof( str ));
432 else strlcat( str, " :", sizeof( str ));
433 strlcat( str, Req->argv[i], sizeof( str ));
435 return IRC_WriteStrClientPrefix( target, prefix, "%s", str );
439 client_type = Client_Type(client);
443 if( strcasecmp( Req->command, cmd->name ) != 0 )
448 if( client_type & cmd->type )
450 /* Command is allowed for this client: call it and count produced bytes */
451 Conn_ResetWCounter( );
452 result = (cmd->function)( client, Req );
453 cmd->bytes += Conn_WCounter( );
455 /* Adjust counters */
456 if( client_type != CLIENT_SERVER ) cmd->lcount++;
463 /* Befehl ist fuer diesen Client-Typ nicht erlaubt! */
464 return IRC_WriteStrClient( client, ERR_NOTREGISTERED_MSG, Client_ID( client ));
468 if( Client_Type( client ) != CLIENT_USER &&
469 Client_Type( client ) != CLIENT_SERVER &&
470 Client_Type( client ) != CLIENT_SERVICE )
473 /* Unknown command and registered connection: generate error: */
474 Log( LOG_DEBUG, "Connection %d: Unknown command \"%s\", %d %s,%s prefix.",
475 Client_Conn( client ), Req->command, Req->argc,
476 Req->argc == 1 ? "parameter" : "parameters",
477 Req->prefix ? "" : " no" );
479 if (Client_Type(client) != CLIENT_SERVER) {
480 result = IRC_WriteStrClient(client, ERR_UNKNOWNCOMMAND_MSG,
481 Client_ID(client), Req->command);
482 Conn_SetPenalty(Idx, 1);
487 } /* Handle_Request */