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"
40 #include "irc-channel.h"
41 #include "irc-encoding.h"
43 #include "irc-login.h"
44 #include "irc-metadata.h"
48 #include "irc-server.h"
49 #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 { "CAP", IRC_CAP, 0xFFFF, 0, 0, 0 },
66 { "CONNECT", IRC_CONNECT, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
67 { "DIE", IRC_DIE, CLIENT_USER, 0, 0, 0 },
68 { "DISCONNECT", IRC_DISCONNECT, CLIENT_USER, 0, 0, 0 },
69 { "ERROR", IRC_ERROR, 0xFFFF, 0, 0, 0 },
70 { "GLINE", IRC_xLINE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
71 { "HELP", IRC_HELP, CLIENT_USER, 0, 0, 0 },
72 { "INFO", IRC_INFO, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
73 { "INVITE", IRC_INVITE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
74 { "ISON", IRC_ISON, CLIENT_USER, 0, 0, 0 },
75 { "JOIN", IRC_JOIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
76 { "KICK", IRC_KICK, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
77 { "KILL", IRC_KILL, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
78 { "KLINE", IRC_xLINE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
79 { "LINKS", IRC_LINKS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
80 { "LIST", IRC_LIST, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
81 { "LUSERS", IRC_LUSERS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
82 { "METADATA", IRC_METADATA, CLIENT_SERVER, 0, 0, 0 },
83 { "MODE", IRC_MODE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
84 { "MOTD", IRC_MOTD, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
85 { "NAMES", IRC_NAMES, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
86 { "NICK", IRC_NICK, 0xFFFF, 0, 0, 0 },
87 { "NJOIN", IRC_NJOIN, CLIENT_SERVER, 0, 0, 0 },
88 { "NOTICE", IRC_NOTICE, 0xFFFF, 0, 0, 0 },
89 { "OPER", IRC_OPER, CLIENT_USER, 0, 0, 0 },
90 { "PART", IRC_PART, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
91 { "PASS", IRC_PASS, 0xFFFF, 0, 0, 0 },
92 { "PING", IRC_PING, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
93 { "PONG", IRC_PONG, 0xFFFF, 0, 0, 0 },
94 { "PRIVMSG", IRC_PRIVMSG, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
95 { "QUIT", IRC_QUIT, 0xFFFF, 0, 0, 0 },
96 { "REHASH", IRC_REHASH, CLIENT_USER, 0, 0, 0 },
97 { "RESTART", IRC_RESTART, CLIENT_USER, 0, 0, 0 },
98 { "SERVER", IRC_SERVER, 0xFFFF, 0, 0, 0 },
99 { "SERVICE", IRC_SERVICE, 0xFFFF, 0, 0, 0 },
100 { "SERVLIST", IRC_SERVLIST, CLIENT_USER, 0, 0, 0 },
101 { "SQUERY", IRC_SQUERY, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
102 { "SQUIT", IRC_SQUIT, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
103 { "STATS", IRC_STATS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
104 { "SVSNICK", IRC_SVSNICK, CLIENT_SERVER, 0, 0, 0 },
105 { "SUMMON", IRC_SUMMON, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
106 { "TIME", IRC_TIME, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
107 { "TOPIC", IRC_TOPIC, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
108 { "TRACE", IRC_TRACE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
109 { "USER", IRC_USER, 0xFFFF, 0, 0, 0 },
110 { "USERHOST", IRC_USERHOST, CLIENT_USER, 0, 0, 0 },
111 { "USERS", IRC_USERS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
112 { "VERSION", IRC_VERSION, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
113 { "WALLOPS", IRC_WALLOPS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
114 { "WEBIRC", IRC_WEBIRC, CLIENT_UNKNOWN, 0, 0, 0 },
115 { "WHO", IRC_WHO, CLIENT_USER, 0, 0, 0 },
116 { "WHOIS", IRC_WHOIS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
117 { "WHOWAS", IRC_WHOWAS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
120 { "CHANINFO", IRC_CHANINFO, CLIENT_SERVER, 0, 0, 0 },
122 { "CHARCONV", IRC_CHARCONV, CLIENT_USER, 0, 0, 0 },
127 { "GET", IRC_QUIT_HTTP, CLIENT_UNKNOWN, 0, 0, 0 },
128 { "POST", IRC_QUIT_HTTP, CLIENT_UNKNOWN, 0, 0, 0 },
130 { NULL, NULL, 0x0, 0, 0, 0 } /* Ende-Marke */
133 static void Init_Request PARAMS(( REQUEST *Req ));
135 static bool Validate_Prefix PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
136 static bool Validate_Command PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
137 static bool Validate_Args PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
139 static bool Handle_Request PARAMS(( CONN_ID Idx, REQUEST *Req ));
141 static bool ScrubCTCP PARAMS((char *Request));
144 * Return the pointer to the global "IRC command structure".
145 * This structure, an array of type "COMMAND" describes all the IRC commands
146 * implemented by ngIRCd and how to handle them.
147 * @return Pointer to the global command structure.
150 Parse_GetCommandStruct( void )
153 } /* Parse_GetCommandStruct */
157 * Parse a command ("request") received from a client.
159 * This function is called after the connection layer received a valid CR+LF
160 * terminated line of text: we asume that this is a valid IRC command and
161 * try to do something useful with it :-)
163 * All errors are reported to the client from which the command has been
164 * received, and if the error is fatal this connection is closed down.
166 * This function is able to parse the syntax as described in RFC 2812,
169 * @param Idx Index of the connection from which the command has been received.
170 * @param Request NULL terminated line of text (the "command").
171 * @return true on success (valid command or "regular" error), false if a
172 * fatal error occured and the connection has been shut down.
175 Parse_Request( CONN_ID Idx, char *Request )
182 assert( Request != NULL );
185 if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request );
188 Init_Request( &req );
190 /* remove leading & trailing whitespace */
191 ngt_TrimStr( Request );
193 if (Conf_ScrubCTCP && ScrubCTCP(Request))
196 if (Request[0] == ':') {
198 req.prefix = Request + 1;
199 ptr = strchr( Request, ' ' );
202 LogDebug("Connection %d: Parse error: prefix without command!?", Idx);
203 return Conn_WriteStr(Idx, "ERROR :Prefix without command");
207 /* ignore multiple spaces between prefix and command */
208 while( *(ptr + 1) == ' ' ) ptr++;
212 else start = Request;
214 ptr = strchr( start, ' ' );
219 /* ignore multiple spaces between parameters */
220 while( *(ptr + 1) == ' ' ) ptr++;
225 /* Arguments, Parameters */
231 if( start[0] == ':' )
233 req.argv[req.argc] = start + 1;
238 req.argv[req.argc] = start;
239 ptr = strchr( start, ' ' );
244 while( *(ptr + 1) == ' ' ) ptr++;
251 if( start[0] == ':' ) break;
252 if( req.argc > 14 ) break;
254 if( ptr ) start = ptr + 1;
259 if( ! Validate_Prefix( Idx, &req, &closed )) return ! closed;
260 if( ! Validate_Command( Idx, &req, &closed )) return ! closed;
261 if( ! Validate_Args( Idx, &req, &closed )) return ! closed;
263 return Handle_Request( Idx, &req );
264 } /* Parse_Request */
268 * Initialize request structure.
269 * @param Req Request structure to be initialized.
272 Init_Request( REQUEST *Req )
274 /* Neue Request-Struktur initialisieren */
278 assert( Req != NULL );
282 for( i = 0; i < 15; Req->argv[i++] = NULL );
288 Validate_Prefix( CONN_ID Idx, REQUEST *Req, bool *Closed )
293 assert( Req != NULL );
297 client = Conn_GetClient( Idx );
298 assert( client != NULL );
300 if (!Req->prefix && Client_Type(client) == CLIENT_SERVER
301 && !(Conn_Options(Idx) & CONN_RFC1459)
302 && strcasecmp(Req->command, "ERROR") != 0
303 && strcasecmp(Req->command, "PING") != 0)
306 "Received command without prefix (connection %d, command \"%s\")!?",
308 if (!Conn_WriteStr(Idx, "ERROR :Prefix missing"))
316 /* only validate if this connection is already registered */
317 if (Client_Type(client) != CLIENT_USER
318 && Client_Type(client) != CLIENT_SERVER
319 && Client_Type(client) != CLIENT_SERVICE) {
320 /* not registered, ignore prefix */
325 /* check if client in prefix is known */
326 c = Client_Search( Req->prefix );
329 "Invalid prefix \"%s\", client not known (connection %d, command \"%s\")!?",
330 Req->prefix, Idx, Req->command);
331 if (!Conn_WriteStr(Idx,
332 "ERROR :Invalid prefix \"%s\", client not known",
338 /* check if the client named in the prefix is expected
339 * to come from that direction */
340 if (Client_NextHop(c) != client) {
341 if (Client_Type(c) != CLIENT_SERVER) {
343 "Spoofed prefix \"%s\" from \"%s\" (connection %d, command \"%s\")!",
344 Req->prefix, Client_Mask(Conn_GetClient(Idx)), Idx,
346 Conn_Close(Idx, NULL, "Spoofed prefix", true);
350 "Ignoring spoofed prefix \"%s\" from \"%s\" (connection %d, command \"%s\").",
351 Req->prefix, Client_Mask(Conn_GetClient(Idx)), Idx,
359 } /* Validate_Prefix */
363 Validate_Command( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed )
366 assert( Req != NULL );
370 } /* Validate_Comman */
375 Validate_Args(CONN_ID Idx, REQUEST *Req, bool *Closed)
377 Validate_Args(UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed)
388 assert( Req != NULL );
390 /* CR and LF are never allowed in command parameters.
391 * But since we do accept lines terminated only with CR or LF in
392 * "non-RFC-compliant mode" (besides the correct CR+LF combination),
393 * this check can only trigger in "strict RFC" mode; therefore we
394 * optimize it away otherwise ... */
395 for (i = 0; i < Req->argc; i++) {
396 if (strchr(Req->argv[i], '\r') || strchr(Req->argv[i], '\n')) {
398 "Invalid character(s) in parameter (connection %d, command %s)!?",
400 if (!Conn_WriteStr(Idx,
401 "ERROR :Invalid character(s) in parameter!"))
409 } /* Validate_Args */
412 /* Command is a status code ("numeric") from another server */
414 Handle_Numeric(CLIENT *client, REQUEST *Req)
416 static const struct _NUMERIC Numerics[] = {
417 { 5, IRC_Num_ISUPPORT },
419 { 376, IRC_Num_ENDOFMOTD }
423 CLIENT *prefix, *target = NULL;
425 /* Determine target */
427 if (strcmp(Req->argv[0], "*") != 0)
428 target = Client_Search(Req->argv[0]);
430 target = Client_ThisServer();
434 /* Status code without target!? */
437 "Unknown target for status code %s: \"%s\"",
438 Req->command, Req->argv[0]);
441 "Unknown target for status code %s!",
445 if (target == Client_ThisServer()) {
446 /* This server is the target of the numeric */
447 num = atoi(Req->command);
449 for (i = 0; i < (int) C_ARRAY_SIZE(Numerics); i++) {
450 if (num == Numerics[i].numeric) {
451 if (!Numerics[i].function)
453 return Numerics[i].function(client, Req);
457 LogDebug("Ignored status code %s from \"%s\".",
458 Req->command, Client_ID(client));
462 /* Determine source */
463 if (! Req->prefix[0]) {
464 /* Oops, no prefix!? */
465 Log(LOG_WARNING, "Got status code %s from \"%s\" without prefix!?",
466 Req->command, Client_ID(client));
470 prefix = Client_Search(Req->prefix);
471 if (! prefix) { /* Oops, unknown prefix!? */
472 Log(LOG_WARNING, "Got status code %s from unknown source: \"%s\"", Req->command, Req->prefix);
476 /* Forward status code */
477 strlcpy(str, Req->command, sizeof(str));
478 for (i = 0; i < Req->argc; i++) {
479 if (i < Req->argc - 1)
480 strlcat(str, " ", sizeof(str));
482 strlcat(str, " :", sizeof(str));
483 strlcat(str, Req->argv[i], sizeof(str));
485 return IRC_WriteStrClientPrefix(target, prefix, "%s", str);
489 Handle_Request( CONN_ID Idx, REQUEST *Req )
497 assert( Req != NULL );
498 assert( Req->command != NULL );
500 client = Conn_GetClient( Idx );
501 assert( client != NULL );
504 client_type = Client_Type(client);
505 if ((client_type == CLIENT_SERVER ||
506 client_type == CLIENT_UNKNOWNSERVER)
507 && strlen(Req->command) == 3 && atoi(Req->command) > 1)
508 return Handle_Numeric(client, Req);
512 if (strcasecmp(Req->command, cmd->name) != 0) {
517 if (!(client_type & cmd->type)) {
518 if (client_type == CLIENT_USER
519 && cmd->type & CLIENT_SERVER)
520 return IRC_WriteStrClient(client,
521 ERR_NOTREGISTEREDSERVER_MSG,
524 return IRC_WriteStrClient(client,
525 ERR_NOTREGISTERED_MSG,
529 /* Command is allowed for this client: call it and count
530 * generated bytes in output */
531 Conn_ResetWCounter();
532 result = (cmd->function)(client, Req);
533 cmd->bytes += Conn_WCounter();
535 /* Adjust counters */
536 if (client_type != CLIENT_SERVER)
543 if (client_type != CLIENT_USER &&
544 client_type != CLIENT_SERVER &&
545 client_type != CLIENT_SERVICE )
548 /* Unknown command and registered connection: generate error: */
549 LogDebug("Connection %d: Unknown command \"%s\", %d %s,%s prefix.",
550 Client_Conn( client ), Req->command, Req->argc,
551 Req->argc == 1 ? "parameter" : "parameters",
552 Req->prefix ? "" : " no" );
554 if (Client_Type(client) != CLIENT_SERVER) {
555 result = IRC_WriteStrClient(client, ERR_UNKNOWNCOMMAND_MSG,
556 Client_ID(client), Req->command);
557 Conn_SetPenalty(Idx, 1);
560 } /* Handle_Request */
564 * Check if incoming messages contains CTCP commands and should be dropped.
566 * @param Request NULL terminated incoming command.
567 * @returns true, when the message should be dropped.
570 ScrubCTCP(char *Request)
572 static const char me_cmd[] = "ACTION ";
573 static const char ctcp_char = 0x1;
574 bool dropCommand = false;
576 char *ptrEnd = strchr(Request, '\0');
578 if (Request[0] == ':' && ptrEnd > ptr)
581 while (ptr != ptrEnd && *ptr != ':')
584 if ((ptrEnd - ptr) > 1) {
586 if (*ptr == ctcp_char) {
589 /* allow /me commands */
590 if ((size_t)(ptrEnd - ptr) >= strlen(me_cmd)
591 && !strncmp(ptr, me_cmd, strlen(me_cmd)))