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"
54 bool (*function) PARAMS(( CLIENT *Client, REQUEST *Request ));
58 static COMMAND My_Commands[] =
60 { "ADMIN", IRC_ADMIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
61 { "AWAY", IRC_AWAY, CLIENT_USER, 0, 0, 0 },
62 { "CONNECT", IRC_CONNECT, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
63 { "DIE", IRC_DIE, CLIENT_USER, 0, 0, 0 },
64 { "DISCONNECT", IRC_DISCONNECT, CLIENT_USER, 0, 0, 0 },
65 { "ERROR", IRC_ERROR, 0xFFFF, 0, 0, 0 },
66 { "HELP", IRC_HELP, CLIENT_USER, 0, 0, 0 },
67 { "INFO", IRC_INFO, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
68 { "INVITE", IRC_INVITE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
69 { "ISON", IRC_ISON, CLIENT_USER, 0, 0, 0 },
70 { "JOIN", IRC_JOIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
71 { "KICK", IRC_KICK, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
72 { "KILL", IRC_KILL, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
73 { "LINKS", IRC_LINKS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
74 { "LIST", IRC_LIST, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
75 { "LUSERS", IRC_LUSERS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
76 { "MODE", IRC_MODE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
77 { "MOTD", IRC_MOTD, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
78 { "NAMES", IRC_NAMES, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
79 { "NICK", IRC_NICK, 0xFFFF, 0, 0, 0 },
80 { "NJOIN", IRC_NJOIN, CLIENT_SERVER, 0, 0, 0 },
81 { "NOTICE", IRC_NOTICE, 0xFFFF, 0, 0, 0 },
82 { "OPER", IRC_OPER, CLIENT_USER, 0, 0, 0 },
83 { "PART", IRC_PART, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
84 { "PASS", IRC_PASS, 0xFFFF, 0, 0, 0 },
85 { "PING", IRC_PING, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
86 { "PONG", IRC_PONG, 0xFFFF, 0, 0, 0 },
87 { "PRIVMSG", IRC_PRIVMSG, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
88 { "QUIT", IRC_QUIT, 0xFFFF, 0, 0, 0 },
89 { "REHASH", IRC_REHASH, CLIENT_USER, 0, 0, 0 },
90 { "RESTART", IRC_RESTART, CLIENT_USER, 0, 0, 0 },
91 { "SERVER", IRC_SERVER, 0xFFFF, 0, 0, 0 },
92 { "SERVICE", IRC_SERVICE, 0xFFFF, 0, 0, 0 },
93 { "SERVLIST", IRC_SERVLIST, CLIENT_USER, 0, 0, 0 },
94 { "SQUERY", IRC_SQUERY, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
95 { "SQUIT", IRC_SQUIT, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
96 { "STATS", IRC_STATS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
97 { "SUMMON", IRC_SUMMON, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
98 { "TIME", IRC_TIME, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
99 { "TOPIC", IRC_TOPIC, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
100 { "TRACE", IRC_TRACE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
101 { "USER", IRC_USER, 0xFFFF, 0, 0, 0 },
102 { "USERHOST", IRC_USERHOST, CLIENT_USER, 0, 0, 0 },
103 { "USERS", IRC_USERS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
104 { "VERSION", IRC_VERSION, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
105 { "WALLOPS", IRC_WALLOPS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
106 { "WEBIRC", IRC_WEBIRC, CLIENT_UNKNOWN, 0, 0, 0 },
107 { "WHO", IRC_WHO, CLIENT_USER, 0, 0, 0 },
108 { "WHOIS", IRC_WHOIS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
109 { "WHOWAS", IRC_WHOWAS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
111 { "CHANINFO", IRC_CHANINFO, CLIENT_SERVER, 0, 0, 0 },
114 { "GET", IRC_QUIT_HTTP, CLIENT_UNKNOWN, 0, 0, 0 },
115 { "POST", IRC_QUIT_HTTP, CLIENT_UNKNOWN, 0, 0, 0 },
117 { NULL, NULL, 0x0, 0, 0, 0 } /* Ende-Marke */
120 static void Init_Request PARAMS(( REQUEST *Req ));
122 static bool Validate_Prefix PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
123 static bool Validate_Command PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
124 static bool Validate_Args PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
126 static bool Handle_Request PARAMS(( CONN_ID Idx, REQUEST *Req ));
128 static bool ScrubCTCP PARAMS((char *Request));
131 * Return the pointer to the global "IRC command structure".
132 * This structure, an array of type "COMMAND" describes all the IRC commands
133 * implemented by ngIRCd and how to handle them.
134 * @return Pointer to the global command structure.
137 Parse_GetCommandStruct( void )
140 } /* Parse_GetCommandStruct */
144 * Parse a command ("request") received from a client.
146 * This function is called after the connection layer received a valid CR+LF
147 * terminated line of text: we asume that this is a valid IRC command and
148 * try to do something useful with it :-)
150 * All errors are reported to the client from which the command has been
151 * received, and if the error is fatal this connection is closed down.
153 * This function is able to parse the syntax as described in RFC 2812,
156 * @param Idx Index of the connection from which the command has been received.
157 * @param Request NULL terminated line of text (the "command").
158 * @return true on success (valid command or "regular" error), false if a
159 * fatal error occured and the connection has been shut down.
162 Parse_Request( CONN_ID Idx, char *Request )
169 assert( Request != NULL );
172 if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request );
175 Init_Request( &req );
177 /* remove leading & trailing whitespace */
178 ngt_TrimStr( Request );
180 if (Conf_ScrubCTCP && ScrubCTCP(Request))
183 if (Request[0] == ':') {
185 req.prefix = Request + 1;
186 ptr = strchr( Request, ' ' );
189 LogDebug("Connection %d: Parse error: prefix without command!?", Idx);
190 return Conn_WriteStr(Idx, "ERROR :Prefix without command");
194 /* ignore multiple spaces between prefix and command */
195 while( *(ptr + 1) == ' ' ) ptr++;
199 else start = Request;
201 ptr = strchr( start, ' ' );
206 /* ignore multiple spaces between parameters */
207 while( *(ptr + 1) == ' ' ) ptr++;
212 /* Arguments, Parameters */
218 if( start[0] == ':' )
220 req.argv[req.argc] = start + 1;
225 req.argv[req.argc] = start;
226 ptr = strchr( start, ' ' );
231 while( *(ptr + 1) == ' ' ) ptr++;
238 if( start[0] == ':' ) break;
239 if( req.argc > 14 ) break;
241 if( ptr ) start = ptr + 1;
246 if( ! Validate_Prefix( Idx, &req, &closed )) return ! closed;
247 if( ! Validate_Command( Idx, &req, &closed )) return ! closed;
248 if( ! Validate_Args( Idx, &req, &closed )) return ! closed;
250 return Handle_Request( Idx, &req );
251 } /* Parse_Request */
255 * Initialize request structure.
256 * @param Req Request structure to be initialized.
259 Init_Request( REQUEST *Req )
261 /* Neue Request-Struktur initialisieren */
265 assert( Req != NULL );
269 for( i = 0; i < 15; Req->argv[i++] = NULL );
275 Validate_Prefix( CONN_ID Idx, REQUEST *Req, bool *Closed )
280 assert( Req != NULL );
284 client = Conn_GetClient( Idx );
285 assert( client != NULL );
287 if (!Req->prefix && Client_Type(client) == CLIENT_SERVER
288 && !(Conn_Options(Idx) & CONN_RFC1459)
289 && strcasecmp(Req->command, "ERROR") != 0
290 && strcasecmp(Req->command, "PING") != 0)
293 "Received command without prefix (connection %d, command \"%s\")!?",
295 if (!Conn_WriteStr(Idx, "ERROR :Prefix missing"))
303 /* only validate if this connection is already registered */
304 if (Client_Type(client) != CLIENT_USER
305 && Client_Type(client) != CLIENT_SERVER
306 && Client_Type(client) != CLIENT_SERVICE) {
307 /* not registered, ignore prefix */
312 /* check if client in prefix is known */
313 c = Client_Search( Req->prefix );
316 "Invalid prefix \"%s\", client not known (connection %d, command \"%s\")!?",
317 Req->prefix, Idx, Req->command);
318 if (!Conn_WriteStr(Idx,
319 "ERROR :Invalid prefix \"%s\", client not known",
325 /* check if the client named in the prefix is expected
326 * to come from that direction */
327 if (Client_NextHop(c) != client) {
328 if (Client_Type(c) != CLIENT_SERVER) {
330 "Spoofed prefix \"%s\" from \"%s\" (connection %d, command \"%s\")!",
331 Req->prefix, Client_Mask(Conn_GetClient(Idx)), Idx,
333 Conn_Close(Idx, NULL, "Spoofed prefix", true);
337 "Ignoring spoofed prefix \"%s\" from \"%s\" (connection %d, command \"%s\").",
338 Req->prefix, Client_Mask(Conn_GetClient(Idx)), Idx,
346 } /* Validate_Prefix */
350 Validate_Command( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed )
353 assert( Req != NULL );
357 } /* Validate_Comman */
362 Validate_Args(CONN_ID Idx, REQUEST *Req, bool *Closed)
364 Validate_Args(UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed)
375 assert( Req != NULL );
377 /* CR and LF are never allowed in command parameters.
378 * But since we do accept lines terminated only with CR or LF in
379 * "non-RFC-compliant mode" (besides the correct CR+LF combination),
380 * this check can only trigger in "strict RFC" mode; therefore we
381 * optimize it away otherwise ... */
382 for (i = 0; i < Req->argc; i++) {
383 if (strchr(Req->argv[i], '\r') || strchr(Req->argv[i], '\n')) {
385 "Invalid character(s) in parameter (connection %d, command %s)!?",
387 if (!Conn_WriteStr(Idx,
388 "ERROR :Invalid character(s) in parameter!"))
396 } /* Validate_Args */
399 /* Command is a status code ("numeric") from another server */
401 Handle_Numeric(CLIENT *client, REQUEST *Req)
403 static const struct _NUMERIC Numerics[] = {
404 { 5, IRC_Num_ISUPPORT },
406 { 376, IRC_Num_ENDOFMOTD }
410 CLIENT *prefix, *target = NULL;
412 /* Determine target */
414 if (strcmp(Req->argv[0], "*") != 0)
415 target = Client_Search(Req->argv[0]);
417 target = Client_ThisServer();
421 /* Status code without target!? */
424 "Unknown target for status code %s: \"%s\"",
425 Req->command, Req->argv[0]);
428 "Unknown target for status code %s!",
432 if (target == Client_ThisServer()) {
433 /* This server is the target of the numeric */
434 num = atoi(Req->command);
436 for (i = 0; i < (int) C_ARRAY_SIZE(Numerics); i++) {
437 if (num == Numerics[i].numeric) {
438 if (!Numerics[i].function)
440 return Numerics[i].function(client, Req);
444 LogDebug("Ignored status code %s from \"%s\".",
445 Req->command, Client_ID(client));
449 /* Determine source */
450 if (! Req->prefix[0]) {
451 /* Oops, no prefix!? */
452 Log(LOG_WARNING, "Got status code %s from \"%s\" without prefix!?",
453 Req->command, Client_ID(client));
457 prefix = Client_Search(Req->prefix);
458 if (! prefix) { /* Oops, unknown prefix!? */
459 Log(LOG_WARNING, "Got status code %s from unknown source: \"%s\"", Req->command, Req->prefix);
463 /* Forward status code */
464 strlcpy(str, Req->command, sizeof(str));
465 for (i = 0; i < Req->argc; i++) {
466 if (i < Req->argc - 1)
467 strlcat(str, " ", sizeof(str));
469 strlcat(str, " :", sizeof(str));
470 strlcat(str, Req->argv[i], sizeof(str));
472 return IRC_WriteStrClientPrefix(target, prefix, "%s", str);
476 Handle_Request( CONN_ID Idx, REQUEST *Req )
484 assert( Req != NULL );
485 assert( Req->command != NULL );
487 client = Conn_GetClient( Idx );
488 assert( client != NULL );
491 client_type = Client_Type(client);
492 if ((client_type == CLIENT_SERVER ||
493 client_type == CLIENT_UNKNOWNSERVER)
494 && strlen(Req->command) == 3 && atoi(Req->command) > 1)
495 return Handle_Numeric(client, Req);
499 if (strcasecmp(Req->command, cmd->name) != 0) {
504 if (!(client_type & cmd->type))
505 return IRC_WriteStrClient(client, ERR_NOTREGISTERED_MSG, Client_ID(client));
507 /* Command is allowed for this client: call it and count produced bytes */
508 Conn_ResetWCounter();
509 result = (cmd->function)(client, Req);
510 cmd->bytes += Conn_WCounter();
512 /* Adjust counters */
513 if (client_type != CLIENT_SERVER)
520 if (client_type != CLIENT_USER &&
521 client_type != CLIENT_SERVER &&
522 client_type != CLIENT_SERVICE )
525 /* Unknown command and registered connection: generate error: */
526 LogDebug("Connection %d: Unknown command \"%s\", %d %s,%s prefix.",
527 Client_Conn( client ), Req->command, Req->argc,
528 Req->argc == 1 ? "parameter" : "parameters",
529 Req->prefix ? "" : " no" );
531 if (Client_Type(client) != CLIENT_SERVER) {
532 result = IRC_WriteStrClient(client, ERR_UNKNOWNCOMMAND_MSG,
533 Client_ID(client), Req->command);
534 Conn_SetPenalty(Idx, 1);
537 } /* Handle_Request */
541 * Check if incoming messages contains CTCP commands and should be dropped.
543 * @param Request NULL terminated incoming command.
544 * @returns true, when the message should be dropped.
547 ScrubCTCP(char *Request)
549 static const char me_cmd[] = "ACTION ";
550 static const char ctcp_char = 0x1;
551 bool dropCommand = false;
553 char *ptrEnd = strchr(Request, '\0');
555 if (Request[0] == ':' && ptrEnd > ptr)
558 while (ptr != ptrEnd && *ptr != ':')
561 if ((ptrEnd - ptr) > 1) {
563 if (*ptr == ctcp_char) {
566 /* allow /me commands */
567 if ((size_t)(ptrEnd - ptr) >= strlen(me_cmd)
568 && !strncmp(ptr, me_cmd, strlen(me_cmd)))