2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2018 Alexander Barton (alex@barton.de) and Contributors.
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.
25 #include "conn-func.h"
35 #include "irc-channel.h"
37 # include "irc-encoding.h"
40 #include "irc-login.h"
41 #include "irc-metadata.h"
45 #include "irc-server.h"
46 #include "irc-write.h"
51 bool (*function) PARAMS(( CLIENT *Client, REQUEST *Request ));
55 static COMMAND My_Commands[] =
57 #define _CMD(name, func, type, min, max, penalty) \
58 { (name), (func), (type), (min), (max), (penalty), 0, 0, 0 }
59 _CMD("ADMIN", IRC_ADMIN, CLIENT_USER|CLIENT_SERVER, 0, 1, 1),
60 _CMD("AWAY", IRC_AWAY, CLIENT_USER, 0, 1, 0),
61 _CMD("CAP", IRC_CAP, CLIENT_ANY, 1, 2, 0),
62 _CMD("CONNECT", IRC_CONNECT, CLIENT_USER|CLIENT_SERVER, 0, -1, 0),
64 _CMD("DIE", IRC_DIE, CLIENT_USER, 0, 0, 0),
66 _CMD("DIE", IRC_DIE, CLIENT_USER, 0, 1, 0),
68 _CMD("DISCONNECT", IRC_DISCONNECT, CLIENT_USER, 1, 1, 0),
69 _CMD("ERROR", IRC_ERROR, CLIENT_ANY, 0, -1, 0),
70 _CMD("GLINE", IRC_xLINE, CLIENT_USER|CLIENT_SERVER, 0, -1, 0),
71 _CMD("HELP", IRC_HELP, CLIENT_USER, 0, 1, 2),
72 _CMD("INFO", IRC_INFO, CLIENT_USER|CLIENT_SERVER, 0, 1, 2),
73 _CMD("INVITE", IRC_INVITE, CLIENT_USER|CLIENT_SERVER, 2, 2, 1),
74 _CMD("ISON", IRC_ISON, CLIENT_USER, 1, -1, 0),
75 _CMD("JOIN", IRC_JOIN, CLIENT_USER|CLIENT_SERVER, 1, 2, 0),
76 _CMD("KICK", IRC_KICK, CLIENT_USER|CLIENT_SERVER, 2, 3, 0),
77 _CMD("KILL", IRC_KILL, CLIENT_USER|CLIENT_SERVER, 2, 2, 0),
78 _CMD("KLINE", IRC_xLINE, CLIENT_USER|CLIENT_SERVER, 0, -1, 0),
79 _CMD("LINKS", IRC_LINKS, CLIENT_USER|CLIENT_SERVER, 0, 2, 1),
80 _CMD("LIST", IRC_LIST, CLIENT_USER|CLIENT_SERVER, 0, 2, 2),
81 _CMD("LUSERS", IRC_LUSERS, CLIENT_USER|CLIENT_SERVER, 0, 2, 1),
82 _CMD("METADATA", IRC_METADATA, CLIENT_SERVER, 3, 3, 0),
83 _CMD("MODE", IRC_MODE, CLIENT_USER|CLIENT_SERVER, 1, -1, 1),
84 _CMD("MOTD", IRC_MOTD, CLIENT_USER|CLIENT_SERVER, 0, 1, 3),
85 _CMD("NAMES", IRC_NAMES, CLIENT_USER|CLIENT_SERVER, 0, 2, 1),
86 _CMD("NICK", IRC_NICK, CLIENT_ANY, 0, -1, 0),
87 _CMD("NJOIN", IRC_NJOIN, CLIENT_SERVER, 2, 2, 0),
88 _CMD("NOTICE", IRC_NOTICE, CLIENT_ANY, 0, -1, 0),
89 _CMD("OPER", IRC_OPER, CLIENT_USER, 2, 2, 0),
90 _CMD("PART", IRC_PART, CLIENT_USER|CLIENT_SERVER, 1, 2, 0),
91 _CMD("PASS", IRC_PASS, CLIENT_ANY, 0, -1, 0),
92 _CMD("PING", IRC_PING, CLIENT_USER|CLIENT_SERVER, 0, -1, 0),
93 _CMD("PONG", IRC_PONG, CLIENT_ANY, 0, -1, 0),
94 _CMD("PRIVMSG", IRC_PRIVMSG, CLIENT_USER|CLIENT_SERVER, 0, 2, 0),
95 _CMD("QUIT", IRC_QUIT, CLIENT_ANY, 0, 1, 0),
96 _CMD("REHASH", IRC_REHASH, CLIENT_USER, 0, 0, 0),
97 _CMD("RESTART", IRC_RESTART, CLIENT_USER, 0, 0, 0),
98 _CMD("SERVER", IRC_SERVER, CLIENT_ANY, 0, -1, 0),
99 _CMD("SERVICE", IRC_SERVICE, CLIENT_ANY, 6, 6, 0),
100 _CMD("SERVLIST", IRC_SERVLIST, CLIENT_USER, 0, 2, 1),
101 _CMD("SQUERY", IRC_SQUERY, CLIENT_USER|CLIENT_SERVER, 0, 2, 0),
102 _CMD("SQUIT", IRC_SQUIT, CLIENT_USER|CLIENT_SERVER, 2, 2, 0),
103 _CMD("STATS", IRC_STATS, CLIENT_USER|CLIENT_SERVER, 0, 2, 2),
104 _CMD("SVSNICK", IRC_SVSNICK, CLIENT_SERVER, 2, 2, 0),
105 _CMD("SUMMON", IRC_SUMMON, CLIENT_USER|CLIENT_SERVER, 0, -1, 0),
106 _CMD("TIME", IRC_TIME, CLIENT_USER|CLIENT_SERVER, 0, 1, 1),
107 _CMD("TOPIC", IRC_TOPIC, CLIENT_USER|CLIENT_SERVER, 1, 2, 1),
108 _CMD("TRACE", IRC_TRACE, CLIENT_USER|CLIENT_SERVER, 0, 1, 3),
109 _CMD("USER", IRC_USER, CLIENT_ANY, 0, -1, 0),
110 _CMD("USERHOST", IRC_USERHOST, CLIENT_USER, 1, -1, 1),
111 _CMD("USERS", IRC_USERS, CLIENT_USER|CLIENT_SERVER, 0, -1, 0),
112 _CMD("VERSION", IRC_VERSION, CLIENT_USER|CLIENT_SERVER, 0, 1, 1),
113 _CMD("WALLOPS", IRC_WALLOPS, CLIENT_USER|CLIENT_SERVER, 1, 1, 0),
114 _CMD("WEBIRC", IRC_WEBIRC, CLIENT_UNKNOWN, 4, 5, 0),
115 _CMD("WHO", IRC_WHO, CLIENT_USER, 0, 2, 1),
116 _CMD("WHOIS", IRC_WHOIS, CLIENT_USER|CLIENT_SERVER, 0, -1, 1),
117 _CMD("WHOWAS", IRC_WHOWAS, CLIENT_USER|CLIENT_SERVER, 0, -1, 0),
120 _CMD("CHANINFO", IRC_CHANINFO, CLIENT_SERVER, 0, -1, 0),
122 _CMD("CHARCONV", IRC_CHARCONV, CLIENT_USER, 1, 1, 0),
127 _CMD("GET", IRC_QUIT_HTTP, CLIENT_UNKNOWN, 0, -1, 0),
128 _CMD("POST", IRC_QUIT_HTTP, CLIENT_UNKNOWN, 0, -1, 0),
130 _CMD(NULL, NULL, 0, 0, 0, 0) /* End-Mark */
134 static void Init_Request PARAMS(( REQUEST *Req ));
136 static bool Validate_Prefix PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
137 static bool Validate_Command PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
138 static bool Validate_Args PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
140 static bool Handle_Request PARAMS(( CONN_ID Idx, REQUEST *Req ));
142 static bool ScrubCTCP PARAMS((char *Request));
145 * Return the pointer to the global "IRC command structure".
146 * This structure, an array of type "COMMAND" describes all the IRC commands
147 * implemented by ngIRCd and how to handle them.
148 * @return Pointer to the global command structure.
151 Parse_GetCommandStruct( void )
154 } /* Parse_GetCommandStruct */
158 * Parse a command ("request") received from a client.
160 * This function is called after the connection layer received a valid CR+LF
161 * terminated line of text: we assume that this is a valid IRC command and
162 * try to do something useful with it :-)
164 * All errors are reported to the client from which the command has been
165 * received, and if the error is fatal this connection is closed down.
167 * This function is able to parse the syntax as described in RFC 2812,
170 * @param Idx Index of the connection from which the command has been received.
171 * @param Request NULL terminated line of text (the "command").
172 * @return CONNECTED on success (valid command or "regular" error), DISCONNECTED
173 * if a fatal error occurred and the connection has been shut down.
176 Parse_Request( CONN_ID Idx, char *Request )
183 assert( Request != NULL );
186 if( NGIRCd_Sniffer ) LogDebug( " <- connection %d: '%s'.", Idx, Request );
189 Init_Request( &req );
191 /* remove leading & trailing whitespace */
192 ngt_TrimStr( Request );
194 if (Conf_ScrubCTCP && ScrubCTCP(Request))
197 if (Request[0] == ':') {
199 req.prefix = Request + 1;
200 ptr = strchr( Request, ' ' );
203 LogDebug("Connection %d: Parse error: prefix without command!?", Idx);
204 return Conn_WriteStr(Idx, "ERROR :Prefix without command");
208 /* ignore multiple spaces between prefix and command */
209 while( *(ptr + 1) == ' ' ) ptr++;
213 else start = Request;
215 ptr = strchr( start, ' ' );
220 /* ignore multiple spaces between parameters */
221 while( *(ptr + 1) == ' ' ) ptr++;
226 /* Arguments, Parameters */
232 if( start[0] == ':' )
234 req.argv[req.argc] = start + 1;
239 req.argv[req.argc] = start;
240 ptr = strchr( start, ' ' );
245 while( *(ptr + 1) == ' ' ) ptr++;
252 if( start[0] == ':' ) break;
253 if( req.argc > 14 ) break;
255 if( ptr ) start = ptr + 1;
260 if(!Validate_Prefix(Idx, &req, &closed))
262 if(!Validate_Command(Idx, &req, &closed))
264 if(!Validate_Args(Idx, &req, &closed))
267 return Handle_Request(Idx, &req);
268 } /* Parse_Request */
272 * Initialize request structure.
273 * @param Req Request structure to be initialized.
276 Init_Request( REQUEST *Req )
280 assert( Req != NULL );
284 for( i = 0; i < 15; Req->argv[i++] = NULL );
290 Validate_Prefix( CONN_ID Idx, REQUEST *Req, bool *Closed )
295 assert( Req != NULL );
299 client = Conn_GetClient( Idx );
300 assert( client != NULL );
302 if (!Req->prefix && Client_Type(client) == CLIENT_SERVER
303 && !(Conn_Options(Idx) & CONN_RFC1459)
304 && strcasecmp(Req->command, "ERROR") != 0
305 && strcasecmp(Req->command, "PING") != 0)
308 "Received command without prefix (connection %d, command \"%s\")!?",
310 if (!Conn_WriteStr(Idx, "ERROR :Prefix missing"))
318 /* only validate if this connection is already registered */
319 if (Client_Type(client) != CLIENT_USER
320 && Client_Type(client) != CLIENT_SERVER
321 && Client_Type(client) != CLIENT_SERVICE) {
322 /* not registered, ignore prefix */
327 /* check if client in prefix is known */
328 c = Client_Search(Req->prefix);
330 if (Client_Type(client) != CLIENT_SERVER) {
332 "Ignoring command with invalid prefix \"%s\" from \"%s\" (connection %d, command \"%s\")!",
333 Req->prefix, Client_ID(client), Idx, Req->command);
334 if (!Conn_WriteStr(Idx,
335 "ERROR :Invalid prefix \"%s\"",
338 IRC_SetPenalty(client, 2);
340 LogDebug("Ignoring command with invalid prefix \"%s\" from \"%s\" (connection %d, command \"%s\")!",
341 Req->prefix, Client_ID(client), Idx, Req->command);
345 /* check if the client named in the prefix is expected
346 * to come from that direction */
347 if (Client_NextHop(c) != client) {
348 if (Client_Type(client) != CLIENT_SERVER) {
350 "Spoofed prefix \"%s\" from \"%s\" (connection %d, command \"%s\"), closing connection!",
351 Req->prefix, Client_ID(client), Idx, Req->command);
352 Conn_Close(Idx, NULL, "Spoofed prefix", true);
356 "Ignoring command with spoofed prefix \"%s\" from \"%s\" (connection %d, command \"%s\")!",
357 Req->prefix, Client_ID(client), Idx, Req->command);
363 } /* Validate_Prefix */
367 Validate_Command( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed )
370 assert( Req != NULL );
374 } /* Validate_Command */
379 Validate_Args(CONN_ID Idx, REQUEST *Req, bool *Closed)
381 Validate_Args(UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed)
392 assert( Req != NULL );
394 /* CR and LF are never allowed in command parameters.
395 * But since we do accept lines terminated only with CR or LF in
396 * "non-RFC-compliant mode" (besides the correct CR+LF combination),
397 * this check can only trigger in "strict RFC" mode; therefore we
398 * optimize it away otherwise ... */
399 for (i = 0; i < Req->argc; i++) {
400 if (strchr(Req->argv[i], '\r') || strchr(Req->argv[i], '\n')) {
402 "Invalid character(s) in parameter (connection %d, command %s)!?",
404 if (!Conn_WriteStr(Idx,
405 "ERROR :Invalid character(s) in parameter!"))
413 } /* Validate_Args */
416 /* Command is a status code ("numeric") from another server */
418 Handle_Numeric(CLIENT *client, REQUEST *Req)
420 static const struct _NUMERIC Numerics[] = {
421 { 5, IRC_Num_ISUPPORT },
423 { 376, IRC_Num_ENDOFMOTD }
426 char str[COMMAND_LEN];
427 CLIENT *prefix, *target = NULL;
429 /* Determine target */
431 if (strcmp(Req->argv[0], "*") != 0)
432 target = Client_Search(Req->argv[0]);
434 target = Client_ThisServer();
438 /* Status code without target!? */
441 "Unknown target for status code %s: \"%s\"",
442 Req->command, Req->argv[0]);
445 "Unknown target for status code %s!",
449 if (target == Client_ThisServer()) {
450 /* This server is the target of the numeric */
451 num = atoi(Req->command);
453 for (i = 0; i < (int) C_ARRAY_SIZE(Numerics); i++) {
454 if (num == Numerics[i].numeric) {
455 if (!Numerics[i].function)
457 return Numerics[i].function(client, Req);
461 LogDebug("Ignored status code %s from \"%s\".",
462 Req->command, Client_ID(client));
466 /* Determine source */
469 "Got status code %s from \"%s\" without prefix!?",
470 Req->command, Client_ID(client));
474 prefix = Client_Search(Req->prefix);
475 if (! prefix) { /* Oops, unknown prefix!? */
476 Log(LOG_WARNING, "Got status code %s from unknown source: \"%s\"", Req->command, Req->prefix);
480 /* Forward status code */
481 strlcpy(str, Req->command, sizeof(str));
482 for (i = 0; i < Req->argc; i++) {
483 if (i < Req->argc - 1)
484 strlcat(str, " ", sizeof(str));
486 strlcat(str, " :", sizeof(str));
487 strlcat(str, Req->argv[i], sizeof(str));
489 return IRC_WriteStrClientPrefix(target, prefix, "%s", str);
493 Handle_Request( CONN_ID Idx, REQUEST *Req )
496 bool result = CONNECTED;
501 assert( Req != NULL );
502 assert( Req->command != NULL );
504 client = Conn_GetClient( Idx );
505 assert( client != NULL );
508 client_type = Client_Type(client);
509 if ((client_type == CLIENT_SERVER ||
510 client_type == CLIENT_UNKNOWNSERVER)
511 && strlen(Req->command) == 3 && atoi(Req->command) > 1)
512 return Handle_Numeric(client, Req);
516 if (strcasecmp(Req->command, cmd->name) != 0) {
521 if (!(client_type & cmd->type)) {
522 if (client_type == CLIENT_USER
523 && cmd->type & CLIENT_SERVER)
524 return IRC_WriteErrClient(client,
525 ERR_NOTREGISTEREDSERVER_MSG,
528 return IRC_WriteErrClient(client,
529 ERR_NOTREGISTERED_MSG,
534 IRC_SetPenalty(client, cmd->penalty);
536 if (Req->argc < cmd->min_argc ||
537 (cmd->max_argc != -1 && Req->argc > cmd->max_argc))
538 return IRC_WriteErrClient(client, ERR_NEEDMOREPARAMS_MSG,
539 Client_ID(client), Req->command);
541 /* Command is allowed for this client: call it and count
542 * generated bytes in output */
543 Conn_ResetWCounter();
544 result = (cmd->function)(client, Req);
545 cmd->bytes += Conn_WCounter();
547 /* Adjust counters */
548 if (client_type != CLIENT_SERVER)
553 /* Return result of command (CONNECTED/DISCONNECTED). */
557 if (client_type != CLIENT_USER &&
558 client_type != CLIENT_SERVER &&
559 client_type != CLIENT_SERVICE )
562 LogDebug("Connection %d: Unknown command \"%s\", %d %s,%s prefix.",
563 Client_Conn( client ), Req->command, Req->argc,
564 Req->argc == 1 ? "parameter" : "parameters",
565 Req->prefix ? "" : " no" );
567 /* Unknown command and registered connection: generate error: */
568 if (client_type != CLIENT_SERVER)
569 result = IRC_WriteErrClient(client, ERR_UNKNOWNCOMMAND_MSG,
570 Client_ID(client), Req->command);
573 } /* Handle_Request */
577 * Check if incoming messages contains CTCP commands and should be dropped.
579 * @param Request NULL terminated incoming command.
580 * @returns true, when the message should be dropped.
583 ScrubCTCP(char *Request)
585 static const char me_cmd[] = "ACTION ";
586 static const char ctcp_char = 0x1;
587 bool dropCommand = false;
589 char *ptrEnd = strchr(Request, '\0');
591 if (Request[0] == ':' && ptrEnd > ptr)
594 while (ptr != ptrEnd && *ptr != ':')
597 if ((ptrEnd - ptr) > 1) {
599 if (*ptr == ctcp_char) {
602 /* allow /me commands */
603 if ((size_t)(ptrEnd - ptr) >= strlen(me_cmd)
604 && !strncmp(ptr, me_cmd, strlen(me_cmd)))