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"
42 #include "irc-login.h"
46 #include "irc-server.h"
47 #include "irc-write.h"
55 bool (*function) PARAMS(( CLIENT *Client, REQUEST *Request ));
59 static COMMAND My_Commands[] =
61 { "ADMIN", IRC_ADMIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
62 { "AWAY", IRC_AWAY, CLIENT_USER, 0, 0, 0 },
63 { "CAP", IRC_CAP, 0xFFFF, 0, 0, 0 },
64 { "CONNECT", IRC_CONNECT, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
65 { "DIE", IRC_DIE, CLIENT_USER, 0, 0, 0 },
66 { "DISCONNECT", IRC_DISCONNECT, CLIENT_USER, 0, 0, 0 },
67 { "ERROR", IRC_ERROR, 0xFFFF, 0, 0, 0 },
68 { "GLINE", IRC_xLINE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
69 { "HELP", IRC_HELP, CLIENT_USER, 0, 0, 0 },
70 { "INFO", IRC_INFO, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
71 { "INVITE", IRC_INVITE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
72 { "ISON", IRC_ISON, CLIENT_USER, 0, 0, 0 },
73 { "JOIN", IRC_JOIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
74 { "KICK", IRC_KICK, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
75 { "KILL", IRC_KILL, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
76 { "KLINE", IRC_xLINE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
77 { "LINKS", IRC_LINKS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
78 { "LIST", IRC_LIST, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
79 { "LUSERS", IRC_LUSERS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
80 { "MODE", IRC_MODE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
81 { "MOTD", IRC_MOTD, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
82 { "NAMES", IRC_NAMES, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
83 { "NICK", IRC_NICK, 0xFFFF, 0, 0, 0 },
84 { "NJOIN", IRC_NJOIN, CLIENT_SERVER, 0, 0, 0 },
85 { "NOTICE", IRC_NOTICE, 0xFFFF, 0, 0, 0 },
86 { "OPER", IRC_OPER, CLIENT_USER, 0, 0, 0 },
87 { "PART", IRC_PART, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
88 { "PASS", IRC_PASS, 0xFFFF, 0, 0, 0 },
89 { "PING", IRC_PING, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
90 { "PONG", IRC_PONG, 0xFFFF, 0, 0, 0 },
91 { "PRIVMSG", IRC_PRIVMSG, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
92 { "QUIT", IRC_QUIT, 0xFFFF, 0, 0, 0 },
93 { "REHASH", IRC_REHASH, CLIENT_USER, 0, 0, 0 },
94 { "RESTART", IRC_RESTART, CLIENT_USER, 0, 0, 0 },
95 { "SERVER", IRC_SERVER, 0xFFFF, 0, 0, 0 },
96 { "SERVICE", IRC_SERVICE, 0xFFFF, 0, 0, 0 },
97 { "SERVLIST", IRC_SERVLIST, CLIENT_USER, 0, 0, 0 },
98 { "SQUERY", IRC_SQUERY, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
99 { "SQUIT", IRC_SQUIT, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
100 { "STATS", IRC_STATS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
101 { "SUMMON", IRC_SUMMON, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
102 { "TIME", IRC_TIME, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
103 { "TOPIC", IRC_TOPIC, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
104 { "TRACE", IRC_TRACE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
105 { "USER", IRC_USER, 0xFFFF, 0, 0, 0 },
106 { "USERHOST", IRC_USERHOST, CLIENT_USER, 0, 0, 0 },
107 { "USERS", IRC_USERS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
108 { "VERSION", IRC_VERSION, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
109 { "WALLOPS", IRC_WALLOPS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
110 { "WEBIRC", IRC_WEBIRC, CLIENT_UNKNOWN, 0, 0, 0 },
111 { "WHO", IRC_WHO, CLIENT_USER, 0, 0, 0 },
112 { "WHOIS", IRC_WHOIS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
113 { "WHOWAS", IRC_WHOWAS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
116 { "CHANINFO", IRC_CHANINFO, CLIENT_SERVER, 0, 0, 0 },
120 { "GET", IRC_QUIT_HTTP, CLIENT_UNKNOWN, 0, 0, 0 },
121 { "POST", IRC_QUIT_HTTP, CLIENT_UNKNOWN, 0, 0, 0 },
123 { NULL, NULL, 0x0, 0, 0, 0 } /* Ende-Marke */
126 static void Init_Request PARAMS(( REQUEST *Req ));
128 static bool Validate_Prefix PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
129 static bool Validate_Command PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
130 static bool Validate_Args PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
132 static bool Handle_Request PARAMS(( CONN_ID Idx, REQUEST *Req ));
134 static bool ScrubCTCP PARAMS((char *Request));
137 * Return the pointer to the global "IRC command structure".
138 * This structure, an array of type "COMMAND" describes all the IRC commands
139 * implemented by ngIRCd and how to handle them.
140 * @return Pointer to the global command structure.
143 Parse_GetCommandStruct( void )
146 } /* Parse_GetCommandStruct */
150 * Parse a command ("request") received from a client.
152 * This function is called after the connection layer received a valid CR+LF
153 * terminated line of text: we asume that this is a valid IRC command and
154 * try to do something useful with it :-)
156 * All errors are reported to the client from which the command has been
157 * received, and if the error is fatal this connection is closed down.
159 * This function is able to parse the syntax as described in RFC 2812,
162 * @param Idx Index of the connection from which the command has been received.
163 * @param Request NULL terminated line of text (the "command").
164 * @return true on success (valid command or "regular" error), false if a
165 * fatal error occured and the connection has been shut down.
168 Parse_Request( CONN_ID Idx, char *Request )
175 assert( Request != NULL );
178 if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request );
181 Init_Request( &req );
183 /* remove leading & trailing whitespace */
184 ngt_TrimStr( Request );
186 if (Conf_ScrubCTCP && ScrubCTCP(Request))
189 if (Request[0] == ':') {
191 req.prefix = Request + 1;
192 ptr = strchr( Request, ' ' );
195 LogDebug("Connection %d: Parse error: prefix without command!?", Idx);
196 return Conn_WriteStr(Idx, "ERROR :Prefix without command");
200 /* ignore multiple spaces between prefix and command */
201 while( *(ptr + 1) == ' ' ) ptr++;
205 else start = Request;
207 ptr = strchr( start, ' ' );
212 /* ignore multiple spaces between parameters */
213 while( *(ptr + 1) == ' ' ) ptr++;
218 /* Arguments, Parameters */
224 if( start[0] == ':' )
226 req.argv[req.argc] = start + 1;
231 req.argv[req.argc] = start;
232 ptr = strchr( start, ' ' );
237 while( *(ptr + 1) == ' ' ) ptr++;
244 if( start[0] == ':' ) break;
245 if( req.argc > 14 ) break;
247 if( ptr ) start = ptr + 1;
252 if( ! Validate_Prefix( Idx, &req, &closed )) return ! closed;
253 if( ! Validate_Command( Idx, &req, &closed )) return ! closed;
254 if( ! Validate_Args( Idx, &req, &closed )) return ! closed;
256 return Handle_Request( Idx, &req );
257 } /* Parse_Request */
261 * Initialize request structure.
262 * @param Req Request structure to be initialized.
265 Init_Request( REQUEST *Req )
267 /* Neue Request-Struktur initialisieren */
271 assert( Req != NULL );
275 for( i = 0; i < 15; Req->argv[i++] = NULL );
281 Validate_Prefix( CONN_ID Idx, REQUEST *Req, bool *Closed )
286 assert( Req != NULL );
290 client = Conn_GetClient( Idx );
291 assert( client != NULL );
293 if (!Req->prefix && Client_Type(client) == CLIENT_SERVER
294 && !(Conn_Options(Idx) & CONN_RFC1459)
295 && strcasecmp(Req->command, "ERROR") != 0
296 && strcasecmp(Req->command, "PING") != 0)
299 "Received command without prefix (connection %d, command \"%s\")!?",
301 if (!Conn_WriteStr(Idx, "ERROR :Prefix missing"))
309 /* only validate if this connection is already registered */
310 if (Client_Type(client) != CLIENT_USER
311 && Client_Type(client) != CLIENT_SERVER
312 && Client_Type(client) != CLIENT_SERVICE) {
313 /* not registered, ignore prefix */
318 /* check if client in prefix is known */
319 c = Client_Search( Req->prefix );
322 "Invalid prefix \"%s\", client not known (connection %d, command \"%s\")!?",
323 Req->prefix, Idx, Req->command);
324 if (!Conn_WriteStr(Idx,
325 "ERROR :Invalid prefix \"%s\", client not known",
331 /* check if the client named in the prefix is expected
332 * to come from that direction */
333 if (Client_NextHop(c) != client) {
334 if (Client_Type(c) != CLIENT_SERVER) {
336 "Spoofed prefix \"%s\" from \"%s\" (connection %d, command \"%s\")!",
337 Req->prefix, Client_Mask(Conn_GetClient(Idx)), Idx,
339 Conn_Close(Idx, NULL, "Spoofed prefix", true);
343 "Ignoring spoofed prefix \"%s\" from \"%s\" (connection %d, command \"%s\").",
344 Req->prefix, Client_Mask(Conn_GetClient(Idx)), Idx,
352 } /* Validate_Prefix */
356 Validate_Command( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed )
359 assert( Req != NULL );
363 } /* Validate_Comman */
368 Validate_Args(CONN_ID Idx, REQUEST *Req, bool *Closed)
370 Validate_Args(UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed)
381 assert( Req != NULL );
383 /* CR and LF are never allowed in command parameters.
384 * But since we do accept lines terminated only with CR or LF in
385 * "non-RFC-compliant mode" (besides the correct CR+LF combination),
386 * this check can only trigger in "strict RFC" mode; therefore we
387 * optimize it away otherwise ... */
388 for (i = 0; i < Req->argc; i++) {
389 if (strchr(Req->argv[i], '\r') || strchr(Req->argv[i], '\n')) {
391 "Invalid character(s) in parameter (connection %d, command %s)!?",
393 if (!Conn_WriteStr(Idx,
394 "ERROR :Invalid character(s) in parameter!"))
402 } /* Validate_Args */
405 /* Command is a status code ("numeric") from another server */
407 Handle_Numeric(CLIENT *client, REQUEST *Req)
409 static const struct _NUMERIC Numerics[] = {
410 { 5, IRC_Num_ISUPPORT },
412 { 376, IRC_Num_ENDOFMOTD }
416 CLIENT *prefix, *target = NULL;
418 /* Determine target */
420 if (strcmp(Req->argv[0], "*") != 0)
421 target = Client_Search(Req->argv[0]);
423 target = Client_ThisServer();
427 /* Status code without target!? */
430 "Unknown target for status code %s: \"%s\"",
431 Req->command, Req->argv[0]);
434 "Unknown target for status code %s!",
438 if (target == Client_ThisServer()) {
439 /* This server is the target of the numeric */
440 num = atoi(Req->command);
442 for (i = 0; i < (int) C_ARRAY_SIZE(Numerics); i++) {
443 if (num == Numerics[i].numeric) {
444 if (!Numerics[i].function)
446 return Numerics[i].function(client, Req);
450 LogDebug("Ignored status code %s from \"%s\".",
451 Req->command, Client_ID(client));
455 /* Determine source */
456 if (! Req->prefix[0]) {
457 /* Oops, no prefix!? */
458 Log(LOG_WARNING, "Got status code %s from \"%s\" without prefix!?",
459 Req->command, Client_ID(client));
463 prefix = Client_Search(Req->prefix);
464 if (! prefix) { /* Oops, unknown prefix!? */
465 Log(LOG_WARNING, "Got status code %s from unknown source: \"%s\"", Req->command, Req->prefix);
469 /* Forward status code */
470 strlcpy(str, Req->command, sizeof(str));
471 for (i = 0; i < Req->argc; i++) {
472 if (i < Req->argc - 1)
473 strlcat(str, " ", sizeof(str));
475 strlcat(str, " :", sizeof(str));
476 strlcat(str, Req->argv[i], sizeof(str));
478 return IRC_WriteStrClientPrefix(target, prefix, "%s", str);
482 Handle_Request( CONN_ID Idx, REQUEST *Req )
490 assert( Req != NULL );
491 assert( Req->command != NULL );
493 client = Conn_GetClient( Idx );
494 assert( client != NULL );
497 client_type = Client_Type(client);
498 if ((client_type == CLIENT_SERVER ||
499 client_type == CLIENT_UNKNOWNSERVER)
500 && strlen(Req->command) == 3 && atoi(Req->command) > 1)
501 return Handle_Numeric(client, Req);
505 if (strcasecmp(Req->command, cmd->name) != 0) {
510 if (!(client_type & cmd->type))
511 return IRC_WriteStrClient(client, ERR_NOTREGISTERED_MSG, Client_ID(client));
513 /* Command is allowed for this client: call it and count produced bytes */
514 Conn_ResetWCounter();
515 result = (cmd->function)(client, Req);
516 cmd->bytes += Conn_WCounter();
518 /* Adjust counters */
519 if (client_type != CLIENT_SERVER)
526 if (client_type != CLIENT_USER &&
527 client_type != CLIENT_SERVER &&
528 client_type != CLIENT_SERVICE )
531 /* Unknown command and registered connection: generate error: */
532 LogDebug("Connection %d: Unknown command \"%s\", %d %s,%s prefix.",
533 Client_Conn( client ), Req->command, Req->argc,
534 Req->argc == 1 ? "parameter" : "parameters",
535 Req->prefix ? "" : " no" );
537 if (Client_Type(client) != CLIENT_SERVER) {
538 result = IRC_WriteStrClient(client, ERR_UNKNOWNCOMMAND_MSG,
539 Client_ID(client), Req->command);
540 Conn_SetPenalty(Idx, 1);
543 } /* Handle_Request */
547 * Check if incoming messages contains CTCP commands and should be dropped.
549 * @param Request NULL terminated incoming command.
550 * @returns true, when the message should be dropped.
553 ScrubCTCP(char *Request)
555 static const char me_cmd[] = "ACTION ";
556 static const char ctcp_char = 0x1;
557 bool dropCommand = false;
559 char *ptrEnd = strchr(Request, '\0');
561 if (Request[0] == ':' && ptrEnd > ptr)
564 while (ptr != ptrEnd && *ptr != ':')
567 if ((ptrEnd - ptr) > 1) {
569 if (*ptr == ctcp_char) {
572 /* allow /me commands */
573 if ((size_t)(ptrEnd - ptr) >= strlen(me_cmd)
574 && !strncmp(ptr, me_cmd, strlen(me_cmd)))