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.70 2008/01/13 16:12:49 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"
57 bool (*function) PARAMS(( CLIENT *Client, REQUEST *Request ));
61 static COMMAND My_Commands[] =
63 { "ADMIN", IRC_ADMIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
64 { "AWAY", IRC_AWAY, CLIENT_USER, 0, 0, 0 },
65 { "CONNECT", IRC_CONNECT, CLIENT_USER, 0, 0, 0 },
66 { "DIE", IRC_DIE, CLIENT_USER, 0, 0, 0 },
67 { "DISCONNECT", IRC_DISCONNECT, CLIENT_USER, 0, 0, 0 },
68 { "ERROR", IRC_ERROR, 0xFFFF, 0, 0, 0 },
69 { "HELP", IRC_HELP, CLIENT_USER, 0, 0, 0 },
70 { "INVITE", IRC_INVITE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
71 { "ISON", IRC_ISON, CLIENT_USER, 0, 0, 0 },
72 { "JOIN", IRC_JOIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
73 { "KICK", IRC_KICK, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
74 { "KILL", IRC_KILL, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
75 { "LINKS", IRC_LINKS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
76 { "LIST", IRC_LIST, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
77 { "LUSERS", IRC_LUSERS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
78 { "MODE", IRC_MODE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
79 { "MOTD", IRC_MOTD, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
80 { "NAMES", IRC_NAMES, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
81 { "NICK", IRC_NICK, 0xFFFF, 0, 0, 0 },
82 { "NJOIN", IRC_NJOIN, CLIENT_SERVER, 0, 0, 0 },
83 { "NOTICE", IRC_NOTICE, 0xFFFF, 0, 0, 0 },
84 { "OPER", IRC_OPER, CLIENT_USER, 0, 0, 0 },
85 { "PART", IRC_PART, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
86 { "PASS", IRC_PASS, 0xFFFF, 0, 0, 0 },
87 { "PING", IRC_PING, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
88 { "PONG", IRC_PONG, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
89 { "PRIVMSG", IRC_PRIVMSG, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
90 { "QUIT", IRC_QUIT, 0xFFFF, 0, 0, 0 },
91 { "REHASH", IRC_REHASH, CLIENT_USER, 0, 0, 0 },
92 { "RESTART", IRC_RESTART, CLIENT_USER, 0, 0, 0 },
93 { "SERVER", IRC_SERVER, 0xFFFF, 0, 0, 0 },
94 { "SQUIT", IRC_SQUIT, CLIENT_SERVER, 0, 0, 0 },
95 { "STATS", IRC_STATS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
96 { "TIME", IRC_TIME, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
97 { "TOPIC", IRC_TOPIC, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
98 { "TRACE", IRC_TRACE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
99 { "USER", IRC_USER, 0xFFFF, 0, 0, 0 },
100 { "USERHOST", IRC_USERHOST, CLIENT_USER, 0, 0, 0 },
101 { "VERSION", IRC_VERSION, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
102 { "WALLOPS", IRC_WALLOPS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
103 { "WHO", IRC_WHO, CLIENT_USER, 0, 0, 0 },
104 { "WHOIS", IRC_WHOIS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
105 { "WHOWAS", IRC_WHOWAS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
107 { "CHANINFO", IRC_CHANINFO, CLIENT_SERVER, 0, 0, 0 },
109 { NULL, NULL, 0x0, 0, 0, 0 } /* Ende-Marke */
112 static void Init_Request PARAMS(( REQUEST *Req ));
114 static bool Validate_Prefix PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
115 static bool Validate_Command PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
116 static bool Validate_Args PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
118 static bool Handle_Request PARAMS(( CONN_ID Idx, REQUEST *Req ));
120 #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
123 * Return the pointer to the global "IRC command structure".
124 * This structure, an array of type "COMMAND" describes all the IRC commands
125 * implemented by ngIRCd and how to handle them.
126 * @return Pointer to the global command structure.
129 Parse_GetCommandStruct( void )
132 } /* Parse_GetCommandStruct */
136 * Parse a command ("request") received from a client.
138 * This function is called after the connection layer received a valid CR+LF
139 * terminated line of text: we asume that this is a valid IRC command and
140 * try to do something useful with it :-)
142 * All errors are reported to the client from which the command has been
143 * received, and if the error is fatal this connection is closed down.
145 * This function is able to parse the syntax as described in RFC 2812,
148 * @param Idx Index of the connection from which the command has been received.
149 * @param Request NULL terminated line of text (the "command").
150 * @return true on success (valid command or "regular" error), false if a
151 * fatal error occured and the connection has been shut down.
154 Parse_Request( CONN_ID Idx, char *Request )
161 assert( Request != NULL );
164 if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request );
167 Init_Request( &req );
169 /* Fuehrendes und folgendes "Geraffel" verwerfen */
170 ngt_TrimStr( Request );
172 /* gibt es ein Prefix? */
173 if( Request[0] == ':' )
175 /* Prefix vorhanden */
176 req.prefix = Request + 1;
177 ptr = strchr( Request, ' ' );
180 Log( LOG_DEBUG, "Connection %d: Parse error: prefix without command!?", Idx );
181 return Conn_WriteStr( Idx, "ERROR :Prefix without command!?" );
185 /* multiple Leerzeichen als Trenner zwischen
186 * Prefix und Befehl ignorieren */
187 while( *(ptr + 1) == ' ' ) ptr++;
191 else start = Request;
194 ptr = strchr( start, ' ' );
199 /* multiple Leerzeichen als Trenner vor
200 * Parametern ignorieren */
201 while( *(ptr + 1) == ' ' ) ptr++;
206 /* Argumente, Parameter */
209 /* Prinzipiell gibt es welche :-) */
213 /* Parameter-String "zerlegen" */
214 if( start[0] == ':' )
216 req.argv[req.argc] = start + 1;
221 req.argv[req.argc] = start;
222 ptr = strchr( start, ' ' );
227 /* multiple Leerzeichen als
228 * Parametertrenner ignorieren */
229 while( *(ptr + 1) == ' ' ) ptr++;
236 if( start[0] == ':' ) break;
237 if( req.argc > 14 ) break;
239 if( ptr ) start = ptr + 1;
244 /* Daten validieren */
245 if( ! Validate_Prefix( Idx, &req, &closed )) return ! closed;
246 if( ! Validate_Command( Idx, &req, &closed )) return ! closed;
247 if( ! Validate_Args( Idx, &req, &closed )) return ! closed;
249 return Handle_Request( Idx, &req );
250 } /* Parse_Request */
254 * Initialize request structure.
255 * @param Req Request structure to be initialized.
258 Init_Request( REQUEST *Req )
260 /* Neue Request-Struktur initialisieren */
264 assert( Req != NULL );
268 for( i = 0; i < 15; Req->argv[i++] = NULL );
274 Validate_Prefix( CONN_ID Idx, REQUEST *Req, bool *Closed )
279 assert( Req != NULL );
283 /* ist ueberhaupt ein Prefix vorhanden? */
284 if( ! Req->prefix ) return true;
286 /* Client-Struktur der Connection ermitteln */
287 client = Conn_GetClient( Idx );
288 assert( client != NULL );
290 /* nur validieren, wenn bereits registrierte Verbindung */
291 if(( Client_Type( client ) != CLIENT_USER ) && ( Client_Type( client ) != CLIENT_SERVER ) && ( Client_Type( client ) != CLIENT_SERVICE ))
293 /* noch nicht registrierte Verbindung.
294 * Das Prefix wird ignoriert. */
299 /* pruefen, ob der im Prefix angegebene Client bekannt ist */
300 c = Client_Search( Req->prefix );
303 /* im Prefix angegebener Client ist nicht bekannt */
304 Log( LOG_ERR, "Invalid prefix \"%s\", client not known (connection %d, command %s)!?", Req->prefix, Idx, Req->command );
305 if( ! Conn_WriteStr( Idx, "ERROR :Invalid prefix \"%s\", client not known!?", Req->prefix )) *Closed = true;
309 /* pruefen, ob der Client mit dem angegebenen Prefix in Richtung
310 * des Senders liegt, d.h. sicherstellen, dass das Prefix nicht
312 if( Client_NextHop( c ) != client )
314 /* das angegebene Prefix ist aus dieser Richtung, also
315 * aus der gegebenen Connection, ungueltig! */
316 Log( LOG_ERR, "Spoofed prefix \"%s\" from \"%s\" (connection %d, command %s)!", Req->prefix, Client_Mask( Conn_GetClient( Idx )), Idx, Req->command );
317 Conn_Close( Idx, NULL, "Spoofed prefix", true);
323 } /* Validate_Prefix */
327 Validate_Command( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed )
330 assert( Req != NULL );
334 } /* Validate_Comman */
338 Validate_Args( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed )
341 assert( Req != NULL );
345 } /* Validate_Args */
348 /* Command is a status code ("numeric") from another server */
350 Handle_Numeric(CLIENT *client, REQUEST *Req)
352 static const struct _NUMERIC Numerics[] = {
353 { 005, IRC_Num_ISUPPORT },
354 { 376, IRC_Num_ENDOFMOTD }
358 CLIENT *prefix, *target = NULL;
360 /* Determine target */
362 target = Client_Search(Req->argv[0]);
365 /* Status code without target!? */
368 "Unknown target for status code %s: \"%s\"",
369 Req->command, Req->argv[0]);
372 "Unknown target for status code %s!",
376 if (target == Client_ThisServer()) {
377 /* This server is the target of the numeric */
378 num = atoi(Req->command);
380 for (i = 0; i < (int) ARRAY_SIZE(Numerics); i++) {
381 if (num == Numerics[i].numeric)
382 return Numerics[i].function(client, Req);
385 LogDebug("Ignored status code %s from \"%s\".",
386 Req->command, Client_ID(client));
390 /* Determine source */
391 if (! Req->prefix[0]) {
392 /* Oops, no prefix!? */
393 Log(LOG_WARNING, "Got status code %s from \"%s\" without prefix!?",
394 Req->command, Client_ID(client));
398 prefix = Client_Search(Req->prefix);
399 if (! prefix) { /* Oops, unknown prefix!? */
400 Log(LOG_WARNING, "Got status code %s from unknown source: \"%s\"", Req->command, Req->prefix);
404 /* Forward status code */
405 strlcpy(str, Req->command, sizeof(str));
406 for (i = 0; i < Req->argc; i++) {
407 if (i < Req->argc - 1)
408 strlcat(str, " ", sizeof(str));
410 strlcat(str, " :", sizeof(str));
411 strlcat(str, Req->argv[i], sizeof(str));
413 return IRC_WriteStrClientPrefix(target, prefix, "%s", str);
418 Handle_Request( CONN_ID Idx, REQUEST *Req )
420 /* Client-Request verarbeiten. Bei einem schwerwiegenden Fehler
421 * wird die Verbindung geschlossen und false geliefert. */
427 assert( Req != NULL );
428 assert( Req->command != NULL );
430 client = Conn_GetClient( Idx );
431 assert( client != NULL );
434 if ((Client_Type(client) == CLIENT_SERVER ||
435 Client_Type(client) == CLIENT_UNKNOWNSERVER)
436 && strlen(Req->command) == 3 && atoi(Req->command) > 1)
437 return Handle_Numeric(client, Req);
442 if (strcasecmp(Req->command, cmd->name) != 0) {
447 if (!(Client_Type(client) & cmd->type))
448 return IRC_WriteStrClient(client, ERR_NOTREGISTERED_MSG, Client_ID(client));
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) != CLIENT_SERVER)
463 if (Client_Type( client ) != CLIENT_USER &&
464 Client_Type( client ) != CLIENT_SERVER &&
465 Client_Type( client ) != CLIENT_SERVICE )
468 /* Unknown command and registered connection: generate error: */
469 LogDebug("Connection %d: Unknown command \"%s\", %d %s,%s prefix.",
470 Client_Conn( client ), Req->command, Req->argc,
471 Req->argc == 1 ? "parameter" : "parameters",
472 Req->prefix ? "" : " no" );
474 if (Client_Type(client) != CLIENT_SERVER) {
475 result = IRC_WriteStrClient(client, ERR_UNKNOWNCOMMAND_MSG,
476 Client_ID(client), Req->command);
477 Conn_SetPenalty(Idx, 1);
480 } /* Handle_Request */