2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2010 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.
16 * IRC command parser and validator.
28 #include "conn-func.h"
39 #include "irc-channel.h"
41 #include "irc-login.h"
45 #include "irc-server.h"
46 #include "irc-write.h"
53 bool (*function) PARAMS(( CLIENT *Client, REQUEST *Request ));
57 static COMMAND My_Commands[] =
59 { "ADMIN", IRC_ADMIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
60 { "AWAY", IRC_AWAY, CLIENT_USER, 0, 0, 0 },
61 { "CONNECT", IRC_CONNECT, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
62 { "DIE", IRC_DIE, CLIENT_USER, 0, 0, 0 },
63 { "DISCONNECT", IRC_DISCONNECT, CLIENT_USER, 0, 0, 0 },
64 { "ERROR", IRC_ERROR, 0xFFFF, 0, 0, 0 },
65 { "HELP", IRC_HELP, CLIENT_USER, 0, 0, 0 },
66 { "INFO", IRC_INFO, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
67 { "INVITE", IRC_INVITE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
68 { "ISON", IRC_ISON, CLIENT_USER, 0, 0, 0 },
69 { "JOIN", IRC_JOIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
70 { "KICK", IRC_KICK, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
71 { "KILL", IRC_KILL, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
72 { "LINKS", IRC_LINKS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
73 { "LIST", IRC_LIST, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
74 { "LUSERS", IRC_LUSERS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
75 { "MODE", IRC_MODE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
76 { "MOTD", IRC_MOTD, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
77 { "NAMES", IRC_NAMES, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
78 { "NICK", IRC_NICK, 0xFFFF, 0, 0, 0 },
79 { "NJOIN", IRC_NJOIN, CLIENT_SERVER, 0, 0, 0 },
80 { "NOTICE", IRC_NOTICE, 0xFFFF, 0, 0, 0 },
81 { "OPER", IRC_OPER, CLIENT_USER, 0, 0, 0 },
82 { "PART", IRC_PART, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
83 { "PASS", IRC_PASS, 0xFFFF, 0, 0, 0 },
84 { "PING", IRC_PING, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
85 { "PONG", IRC_PONG, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
86 { "PRIVMSG", IRC_PRIVMSG, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
87 { "QUIT", IRC_QUIT, 0xFFFF, 0, 0, 0 },
88 { "REHASH", IRC_REHASH, CLIENT_USER, 0, 0, 0 },
89 { "RESTART", IRC_RESTART, CLIENT_USER, 0, 0, 0 },
90 { "SERVER", IRC_SERVER, 0xFFFF, 0, 0, 0 },
91 { "SERVICE", IRC_SERVICE, 0xFFFF, 0, 0, 0 },
92 { "SERVLIST", IRC_SERVLIST, CLIENT_USER, 0, 0, 0 },
93 { "SQUERY", IRC_SQUERY, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
94 { "SQUIT", IRC_SQUIT, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
95 { "STATS", IRC_STATS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
96 { "SUMMON", IRC_SUMMON, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
97 { "TIME", IRC_TIME, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
98 { "TOPIC", IRC_TOPIC, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
99 { "TRACE", IRC_TRACE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
100 { "USER", IRC_USER, 0xFFFF, 0, 0, 0 },
101 { "USERHOST", IRC_USERHOST, CLIENT_USER, 0, 0, 0 },
102 { "USERS", IRC_USERS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
103 { "VERSION", IRC_VERSION, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
104 { "WALLOPS", IRC_WALLOPS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
105 { "WEBIRC", IRC_WEBIRC, CLIENT_UNKNOWN, 0, 0, 0 },
106 { "WHO", IRC_WHO, CLIENT_USER, 0, 0, 0 },
107 { "WHOIS", IRC_WHOIS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
108 { "WHOWAS", IRC_WHOWAS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
110 { "CHANINFO", IRC_CHANINFO, CLIENT_SERVER, 0, 0, 0 },
112 { NULL, NULL, 0x0, 0, 0, 0 } /* Ende-Marke */
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 ));
124 * Return the pointer to the global "IRC command structure".
125 * This structure, an array of type "COMMAND" describes all the IRC commands
126 * implemented by ngIRCd and how to handle them.
127 * @return Pointer to the global command structure.
130 Parse_GetCommandStruct( void )
133 } /* Parse_GetCommandStruct */
137 * Parse a command ("request") received from a client.
139 * This function is called after the connection layer received a valid CR+LF
140 * terminated line of text: we asume that this is a valid IRC command and
141 * try to do something useful with it :-)
143 * All errors are reported to the client from which the command has been
144 * received, and if the error is fatal this connection is closed down.
146 * This function is able to parse the syntax as described in RFC 2812,
149 * @param Idx Index of the connection from which the command has been received.
150 * @param Request NULL terminated line of text (the "command").
151 * @return true on success (valid command or "regular" error), false if a
152 * fatal error occured and the connection has been shut down.
155 Parse_Request( CONN_ID Idx, char *Request )
162 assert( Request != NULL );
165 if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request );
168 Init_Request( &req );
170 /* remove leading & trailing whitespace */
171 ngt_TrimStr( Request );
173 if( Request[0] == ':' )
176 req.prefix = Request + 1;
177 ptr = strchr( Request, ' ' );
180 LogDebug("Connection %d: Parse error: prefix without command!?", Idx);
181 return Conn_WriteStr(Idx, "ERROR :Prefix without command");
185 /* ignore multiple spaces between prefix and command */
186 while( *(ptr + 1) == ' ' ) ptr++;
190 else start = Request;
192 ptr = strchr( start, ' ' );
197 /* ignore multiple spaces between parameters */
198 while( *(ptr + 1) == ' ' ) ptr++;
203 /* Arguments, Parameters */
209 if( start[0] == ':' )
211 req.argv[req.argc] = start + 1;
216 req.argv[req.argc] = start;
217 ptr = strchr( start, ' ' );
222 while( *(ptr + 1) == ' ' ) ptr++;
229 if( start[0] == ':' ) break;
230 if( req.argc > 14 ) break;
232 if( ptr ) start = ptr + 1;
237 if( ! Validate_Prefix( Idx, &req, &closed )) return ! closed;
238 if( ! Validate_Command( Idx, &req, &closed )) return ! closed;
239 if( ! Validate_Args( Idx, &req, &closed )) return ! closed;
241 return Handle_Request( Idx, &req );
242 } /* Parse_Request */
246 * Initialize request structure.
247 * @param Req Request structure to be initialized.
250 Init_Request( REQUEST *Req )
252 /* Neue Request-Struktur initialisieren */
256 assert( Req != NULL );
260 for( i = 0; i < 15; Req->argv[i++] = NULL );
266 Validate_Prefix( CONN_ID Idx, REQUEST *Req, bool *Closed )
271 assert( Req != NULL );
275 if( ! Req->prefix ) return true;
277 client = Conn_GetClient( Idx );
278 assert( client != NULL );
280 /* only validate if this connection is already registered */
281 if (Client_Type(client) != CLIENT_USER
282 && Client_Type(client) != CLIENT_SERVER
283 && Client_Type(client) != CLIENT_SERVICE) {
284 /* not registered, ignore prefix */
289 /* check if client in prefix is known */
290 c = Client_Search( Req->prefix );
293 "Invalid prefix \"%s\", client not known (connection %d, command \"%s\")!?",
294 Req->prefix, Idx, Req->command);
295 if (!Conn_WriteStr(Idx,
296 "ERROR :Invalid prefix \"%s\", client not known",
302 /* check if the client named in the prefix is expected
303 * to come from that direction */
304 if (Client_NextHop(c) != client) {
306 "Spoofed prefix \"%s\" from \"%s\" (connection %d, command \"%s\")!",
307 Req->prefix, Client_Mask(Conn_GetClient(Idx)), Idx,
309 Conn_Close(Idx, NULL, "Spoofed prefix", true);
315 } /* Validate_Prefix */
319 Validate_Command( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed )
322 assert( Req != NULL );
326 } /* Validate_Comman */
331 Validate_Args(CONN_ID Idx, REQUEST *Req, bool *Closed)
333 Validate_Args(UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed)
344 assert( Req != NULL );
346 /* CR and LF are never allowed in command parameters.
347 * But since we do accept lines terminated only with CR or LF in
348 * "non-RFC-compliant mode" (besides the correct CR+LF combination),
349 * this check can only trigger in "strict RFC" mode; therefore we
350 * optimize it away otherwise ... */
351 for (i = 0; i < Req->argc; i++) {
352 if (strchr(Req->argv[i], '\r') || strchr(Req->argv[i], '\n')) {
354 "Invalid character(s) in parameter (connection %d, command %s)!?",
356 if (!Conn_WriteStr(Idx,
357 "ERROR :Invalid character(s) in parameter!"))
365 } /* Validate_Args */
368 /* Command is a status code ("numeric") from another server */
370 Handle_Numeric(CLIENT *client, REQUEST *Req)
372 static const struct _NUMERIC Numerics[] = {
373 { 5, IRC_Num_ISUPPORT },
375 { 376, IRC_Num_ENDOFMOTD }
379 CLIENT *prefix, *target = NULL;
381 /* Determine target */
383 if (strcmp(Req->argv[0], "*") != 0)
384 target = Client_Search(Req->argv[0]);
386 target = Client_ThisServer();
390 /* Status code without target!? */
393 "Unknown target for status code %s: \"%s\"",
394 Req->command, Req->argv[0]);
397 "Unknown target for status code %s!",
401 if (target == Client_ThisServer()) {
402 /* This server is the target of the numeric */
403 num = atoi(Req->command);
405 for (i = 0; i < (int) C_ARRAY_SIZE(Numerics); i++) {
406 if (num == Numerics[i].numeric) {
407 if (!Numerics[i].function)
409 return Numerics[i].function(client, Req);
413 LogDebug("Ignored status code %s from \"%s\".",
414 Req->command, Client_ID(client));
418 /* Determine source */
419 if (! Req->prefix[0]) {
420 /* Oops, no prefix!? */
421 Log(LOG_WARNING, "Got status code %s from \"%s\" without prefix!?",
422 Req->command, Client_ID(client));
426 prefix = Client_Search(Req->prefix);
427 if (! prefix) { /* Oops, unknown prefix!? */
428 Log(LOG_WARNING, "Got status code %s from unknown source: \"%s\"", Req->command, Req->prefix);
432 /* Forward status code */
433 strlcpy(str, Req->command, sizeof(str));
434 for (i = 0; i < Req->argc; i++) {
435 if (i < Req->argc - 1)
436 strlcat(str, " ", sizeof(str));
438 strlcat(str, " :", sizeof(str));
439 strlcat(str, Req->argv[i], sizeof(str));
441 return IRC_WriteStrClientPrefix(target, prefix, "%s", str);
446 Handle_Request( CONN_ID Idx, REQUEST *Req )
454 assert( Req != NULL );
455 assert( Req->command != NULL );
457 client = Conn_GetClient( Idx );
458 assert( client != NULL );
461 client_type = Client_Type(client);
462 if ((client_type == CLIENT_SERVER ||
463 client_type == CLIENT_UNKNOWNSERVER)
464 && strlen(Req->command) == 3 && atoi(Req->command) > 1)
465 return Handle_Numeric(client, Req);
469 if (strcasecmp(Req->command, cmd->name) != 0) {
474 if (!(client_type & cmd->type))
475 return IRC_WriteStrClient(client, ERR_NOTREGISTERED_MSG, Client_ID(client));
477 /* Command is allowed for this client: call it and count produced bytes */
478 Conn_ResetWCounter();
479 result = (cmd->function)(client, Req);
480 cmd->bytes += Conn_WCounter();
482 /* Adjust counters */
483 if (client_type != CLIENT_SERVER)
490 if (client_type != CLIENT_USER &&
491 client_type != CLIENT_SERVER &&
492 client_type != CLIENT_SERVICE )
495 /* Unknown command and registered connection: generate error: */
496 LogDebug("Connection %d: Unknown command \"%s\", %d %s,%s prefix.",
497 Client_Conn( client ), Req->command, Req->argc,
498 Req->argc == 1 ? "parameter" : "parameters",
499 Req->prefix ? "" : " no" );
501 if (Client_Type(client) != CLIENT_SERVER) {
502 result = IRC_WriteStrClient(client, ERR_UNKNOWNCOMMAND_MSG,
503 Client_ID(client), Req->command);
504 Conn_SetPenalty(Idx, 1);
507 } /* Handle_Request */