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"
47 #include "irc-server.h"
48 #include "irc-write.h"
56 bool (*function) PARAMS(( CLIENT *Client, REQUEST *Request ));
60 static COMMAND My_Commands[] =
62 { "ADMIN", IRC_ADMIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
63 { "AWAY", IRC_AWAY, CLIENT_USER, 0, 0, 0 },
64 { "CAP", IRC_CAP, 0xFFFF, 0, 0, 0 },
65 { "CONNECT", IRC_CONNECT, CLIENT_USER|CLIENT_SERVER, 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 { "GLINE", IRC_xLINE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
70 { "HELP", IRC_HELP, CLIENT_USER, 0, 0, 0 },
71 { "INFO", IRC_INFO, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
72 { "INVITE", IRC_INVITE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
73 { "ISON", IRC_ISON, CLIENT_USER, 0, 0, 0 },
74 { "JOIN", IRC_JOIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
75 { "KICK", IRC_KICK, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
76 { "KILL", IRC_KILL, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
77 { "KLINE", IRC_xLINE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
78 { "LINKS", IRC_LINKS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
79 { "LIST", IRC_LIST, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
80 { "LUSERS", IRC_LUSERS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
81 { "MODE", IRC_MODE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
82 { "MOTD", IRC_MOTD, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
83 { "NAMES", IRC_NAMES, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
84 { "NICK", IRC_NICK, 0xFFFF, 0, 0, 0 },
85 { "NJOIN", IRC_NJOIN, CLIENT_SERVER, 0, 0, 0 },
86 { "NOTICE", IRC_NOTICE, 0xFFFF, 0, 0, 0 },
87 { "OPER", IRC_OPER, CLIENT_USER, 0, 0, 0 },
88 { "PART", IRC_PART, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
89 { "PASS", IRC_PASS, 0xFFFF, 0, 0, 0 },
90 { "PING", IRC_PING, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
91 { "PONG", IRC_PONG, 0xFFFF, 0, 0, 0 },
92 { "PRIVMSG", IRC_PRIVMSG, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
93 { "QUIT", IRC_QUIT, 0xFFFF, 0, 0, 0 },
94 { "REHASH", IRC_REHASH, CLIENT_USER, 0, 0, 0 },
95 { "RESTART", IRC_RESTART, CLIENT_USER, 0, 0, 0 },
96 { "SERVER", IRC_SERVER, 0xFFFF, 0, 0, 0 },
97 { "SERVICE", IRC_SERVICE, 0xFFFF, 0, 0, 0 },
98 { "SERVLIST", IRC_SERVLIST, CLIENT_USER, 0, 0, 0 },
99 { "SQUERY", IRC_SQUERY, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
100 { "SQUIT", IRC_SQUIT, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
101 { "STATS", IRC_STATS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
102 { "SVSNICK", IRC_SVSNICK, CLIENT_SERVER, 0, 0, 0 },
103 { "SUMMON", IRC_SUMMON, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
104 { "TIME", IRC_TIME, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
105 { "TOPIC", IRC_TOPIC, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
106 { "TRACE", IRC_TRACE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
107 { "USER", IRC_USER, 0xFFFF, 0, 0, 0 },
108 { "USERHOST", IRC_USERHOST, CLIENT_USER, 0, 0, 0 },
109 { "USERS", IRC_USERS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
110 { "VERSION", IRC_VERSION, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
111 { "WALLOPS", IRC_WALLOPS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
112 { "WEBIRC", IRC_WEBIRC, CLIENT_UNKNOWN, 0, 0, 0 },
113 { "WHO", IRC_WHO, CLIENT_USER, 0, 0, 0 },
114 { "WHOIS", IRC_WHOIS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
115 { "WHOWAS", IRC_WHOWAS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
118 { "CHANINFO", IRC_CHANINFO, CLIENT_SERVER, 0, 0, 0 },
120 { "CHARCONV", IRC_CHARCONV, CLIENT_USER, 0, 0, 0 },
125 { "GET", IRC_QUIT_HTTP, CLIENT_UNKNOWN, 0, 0, 0 },
126 { "POST", IRC_QUIT_HTTP, CLIENT_UNKNOWN, 0, 0, 0 },
128 { NULL, NULL, 0x0, 0, 0, 0 } /* Ende-Marke */
131 static void Init_Request PARAMS(( REQUEST *Req ));
133 static bool Validate_Prefix PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
134 static bool Validate_Command PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
135 static bool Validate_Args PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
137 static bool Handle_Request PARAMS(( CONN_ID Idx, REQUEST *Req ));
139 static bool ScrubCTCP PARAMS((char *Request));
142 * Return the pointer to the global "IRC command structure".
143 * This structure, an array of type "COMMAND" describes all the IRC commands
144 * implemented by ngIRCd and how to handle them.
145 * @return Pointer to the global command structure.
148 Parse_GetCommandStruct( void )
151 } /* Parse_GetCommandStruct */
155 * Parse a command ("request") received from a client.
157 * This function is called after the connection layer received a valid CR+LF
158 * terminated line of text: we asume that this is a valid IRC command and
159 * try to do something useful with it :-)
161 * All errors are reported to the client from which the command has been
162 * received, and if the error is fatal this connection is closed down.
164 * This function is able to parse the syntax as described in RFC 2812,
167 * @param Idx Index of the connection from which the command has been received.
168 * @param Request NULL terminated line of text (the "command").
169 * @return true on success (valid command or "regular" error), false if a
170 * fatal error occured and the connection has been shut down.
173 Parse_Request( CONN_ID Idx, char *Request )
180 assert( Request != NULL );
183 if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request );
186 Init_Request( &req );
188 /* remove leading & trailing whitespace */
189 ngt_TrimStr( Request );
191 if (Conf_ScrubCTCP && ScrubCTCP(Request))
194 if (Request[0] == ':') {
196 req.prefix = Request + 1;
197 ptr = strchr( Request, ' ' );
200 LogDebug("Connection %d: Parse error: prefix without command!?", Idx);
201 return Conn_WriteStr(Idx, "ERROR :Prefix without command");
205 /* ignore multiple spaces between prefix and command */
206 while( *(ptr + 1) == ' ' ) ptr++;
210 else start = Request;
212 ptr = strchr( start, ' ' );
217 /* ignore multiple spaces between parameters */
218 while( *(ptr + 1) == ' ' ) ptr++;
223 /* Arguments, Parameters */
229 if( start[0] == ':' )
231 req.argv[req.argc] = start + 1;
236 req.argv[req.argc] = start;
237 ptr = strchr( start, ' ' );
242 while( *(ptr + 1) == ' ' ) ptr++;
249 if( start[0] == ':' ) break;
250 if( req.argc > 14 ) break;
252 if( ptr ) start = ptr + 1;
257 if( ! Validate_Prefix( Idx, &req, &closed )) return ! closed;
258 if( ! Validate_Command( Idx, &req, &closed )) return ! closed;
259 if( ! Validate_Args( Idx, &req, &closed )) return ! closed;
261 return Handle_Request( Idx, &req );
262 } /* Parse_Request */
266 * Initialize request structure.
267 * @param Req Request structure to be initialized.
270 Init_Request( REQUEST *Req )
272 /* Neue Request-Struktur initialisieren */
276 assert( Req != NULL );
280 for( i = 0; i < 15; Req->argv[i++] = NULL );
286 Validate_Prefix( CONN_ID Idx, REQUEST *Req, bool *Closed )
291 assert( Req != NULL );
295 client = Conn_GetClient( Idx );
296 assert( client != NULL );
298 if (!Req->prefix && Client_Type(client) == CLIENT_SERVER
299 && !(Conn_Options(Idx) & CONN_RFC1459)
300 && strcasecmp(Req->command, "ERROR") != 0
301 && strcasecmp(Req->command, "PING") != 0)
304 "Received command without prefix (connection %d, command \"%s\")!?",
306 if (!Conn_WriteStr(Idx, "ERROR :Prefix missing"))
314 /* only validate if this connection is already registered */
315 if (Client_Type(client) != CLIENT_USER
316 && Client_Type(client) != CLIENT_SERVER
317 && Client_Type(client) != CLIENT_SERVICE) {
318 /* not registered, ignore prefix */
323 /* check if client in prefix is known */
324 c = Client_Search( Req->prefix );
327 "Invalid prefix \"%s\", client not known (connection %d, command \"%s\")!?",
328 Req->prefix, Idx, Req->command);
329 if (!Conn_WriteStr(Idx,
330 "ERROR :Invalid prefix \"%s\", client not known",
336 /* check if the client named in the prefix is expected
337 * to come from that direction */
338 if (Client_NextHop(c) != client) {
339 if (Client_Type(c) != CLIENT_SERVER) {
341 "Spoofed prefix \"%s\" from \"%s\" (connection %d, command \"%s\")!",
342 Req->prefix, Client_Mask(Conn_GetClient(Idx)), Idx,
344 Conn_Close(Idx, NULL, "Spoofed prefix", true);
348 "Ignoring spoofed prefix \"%s\" from \"%s\" (connection %d, command \"%s\").",
349 Req->prefix, Client_Mask(Conn_GetClient(Idx)), Idx,
357 } /* Validate_Prefix */
361 Validate_Command( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed )
364 assert( Req != NULL );
368 } /* Validate_Comman */
373 Validate_Args(CONN_ID Idx, REQUEST *Req, bool *Closed)
375 Validate_Args(UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed)
386 assert( Req != NULL );
388 /* CR and LF are never allowed in command parameters.
389 * But since we do accept lines terminated only with CR or LF in
390 * "non-RFC-compliant mode" (besides the correct CR+LF combination),
391 * this check can only trigger in "strict RFC" mode; therefore we
392 * optimize it away otherwise ... */
393 for (i = 0; i < Req->argc; i++) {
394 if (strchr(Req->argv[i], '\r') || strchr(Req->argv[i], '\n')) {
396 "Invalid character(s) in parameter (connection %d, command %s)!?",
398 if (!Conn_WriteStr(Idx,
399 "ERROR :Invalid character(s) in parameter!"))
407 } /* Validate_Args */
410 /* Command is a status code ("numeric") from another server */
412 Handle_Numeric(CLIENT *client, REQUEST *Req)
414 static const struct _NUMERIC Numerics[] = {
415 { 5, IRC_Num_ISUPPORT },
417 { 376, IRC_Num_ENDOFMOTD }
421 CLIENT *prefix, *target = NULL;
423 /* Determine target */
425 if (strcmp(Req->argv[0], "*") != 0)
426 target = Client_Search(Req->argv[0]);
428 target = Client_ThisServer();
432 /* Status code without target!? */
435 "Unknown target for status code %s: \"%s\"",
436 Req->command, Req->argv[0]);
439 "Unknown target for status code %s!",
443 if (target == Client_ThisServer()) {
444 /* This server is the target of the numeric */
445 num = atoi(Req->command);
447 for (i = 0; i < (int) C_ARRAY_SIZE(Numerics); i++) {
448 if (num == Numerics[i].numeric) {
449 if (!Numerics[i].function)
451 return Numerics[i].function(client, Req);
455 LogDebug("Ignored status code %s from \"%s\".",
456 Req->command, Client_ID(client));
460 /* Determine source */
461 if (! Req->prefix[0]) {
462 /* Oops, no prefix!? */
463 Log(LOG_WARNING, "Got status code %s from \"%s\" without prefix!?",
464 Req->command, Client_ID(client));
468 prefix = Client_Search(Req->prefix);
469 if (! prefix) { /* Oops, unknown prefix!? */
470 Log(LOG_WARNING, "Got status code %s from unknown source: \"%s\"", Req->command, Req->prefix);
474 /* Forward status code */
475 strlcpy(str, Req->command, sizeof(str));
476 for (i = 0; i < Req->argc; i++) {
477 if (i < Req->argc - 1)
478 strlcat(str, " ", sizeof(str));
480 strlcat(str, " :", sizeof(str));
481 strlcat(str, Req->argv[i], sizeof(str));
483 return IRC_WriteStrClientPrefix(target, prefix, "%s", str);
487 Handle_Request( CONN_ID Idx, REQUEST *Req )
495 assert( Req != NULL );
496 assert( Req->command != NULL );
498 client = Conn_GetClient( Idx );
499 assert( client != NULL );
502 client_type = Client_Type(client);
503 if ((client_type == CLIENT_SERVER ||
504 client_type == CLIENT_UNKNOWNSERVER)
505 && strlen(Req->command) == 3 && atoi(Req->command) > 1)
506 return Handle_Numeric(client, Req);
510 if (strcasecmp(Req->command, cmd->name) != 0) {
515 if (!(client_type & cmd->type))
516 return IRC_WriteStrClient(client, ERR_NOTREGISTERED_MSG, Client_ID(client));
518 /* Command is allowed for this client: call it and count produced bytes */
519 Conn_ResetWCounter();
520 result = (cmd->function)(client, Req);
521 cmd->bytes += Conn_WCounter();
523 /* Adjust counters */
524 if (client_type != CLIENT_SERVER)
531 if (client_type != CLIENT_USER &&
532 client_type != CLIENT_SERVER &&
533 client_type != CLIENT_SERVICE )
536 /* Unknown command and registered connection: generate error: */
537 LogDebug("Connection %d: Unknown command \"%s\", %d %s,%s prefix.",
538 Client_Conn( client ), Req->command, Req->argc,
539 Req->argc == 1 ? "parameter" : "parameters",
540 Req->prefix ? "" : " no" );
542 if (Client_Type(client) != CLIENT_SERVER) {
543 result = IRC_WriteStrClient(client, ERR_UNKNOWNCOMMAND_MSG,
544 Client_ID(client), Req->command);
545 Conn_SetPenalty(Idx, 1);
548 } /* Handle_Request */
552 * Check if incoming messages contains CTCP commands and should be dropped.
554 * @param Request NULL terminated incoming command.
555 * @returns true, when the message should be dropped.
558 ScrubCTCP(char *Request)
560 static const char me_cmd[] = "ACTION ";
561 static const char ctcp_char = 0x1;
562 bool dropCommand = false;
564 char *ptrEnd = strchr(Request, '\0');
566 if (Request[0] == ':' && ptrEnd > ptr)
569 while (ptr != ptrEnd && *ptr != ':')
572 if ((ptrEnd - ptr) > 1) {
574 if (*ptr == ctcp_char) {
577 /* allow /me commands */
578 if ((size_t)(ptrEnd - ptr) >= strlen(me_cmd)
579 && !strncmp(ptr, me_cmd, strlen(me_cmd)))