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 { "CONNECT", IRC_CONNECT, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
64 { "DIE", IRC_DIE, CLIENT_USER, 0, 0, 0 },
65 { "DISCONNECT", IRC_DISCONNECT, CLIENT_USER, 0, 0, 0 },
66 { "ERROR", IRC_ERROR, 0xFFFF, 0, 0, 0 },
67 { "GLINE", IRC_xLINE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
68 { "HELP", IRC_HELP, CLIENT_USER, 0, 0, 0 },
69 { "INFO", IRC_INFO, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
70 { "INVITE", IRC_INVITE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
71 { "ISON", IRC_ISON, CLIENT_USER, 0, 0, 0 },
72 { "JOIN", IRC_JOIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
73 { "KICK", IRC_KICK, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
74 { "KILL", IRC_KILL, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
75 { "KLINE", IRC_xLINE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
76 { "LINKS", IRC_LINKS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
77 { "LIST", IRC_LIST, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
78 { "LUSERS", IRC_LUSERS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
79 { "MODE", IRC_MODE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
80 { "MOTD", IRC_MOTD, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
81 { "NAMES", IRC_NAMES, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
82 { "NICK", IRC_NICK, 0xFFFF, 0, 0, 0 },
83 { "NJOIN", IRC_NJOIN, CLIENT_SERVER, 0, 0, 0 },
84 { "NOTICE", IRC_NOTICE, 0xFFFF, 0, 0, 0 },
85 { "OPER", IRC_OPER, CLIENT_USER, 0, 0, 0 },
86 { "PART", IRC_PART, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
87 { "PASS", IRC_PASS, 0xFFFF, 0, 0, 0 },
88 { "PING", IRC_PING, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
89 { "PONG", IRC_PONG, 0xFFFF, 0, 0, 0 },
90 { "PRIVMSG", IRC_PRIVMSG, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
91 { "QUIT", IRC_QUIT, 0xFFFF, 0, 0, 0 },
92 { "REHASH", IRC_REHASH, CLIENT_USER, 0, 0, 0 },
93 { "RESTART", IRC_RESTART, CLIENT_USER, 0, 0, 0 },
94 { "SERVER", IRC_SERVER, 0xFFFF, 0, 0, 0 },
95 { "SERVICE", IRC_SERVICE, 0xFFFF, 0, 0, 0 },
96 { "SERVLIST", IRC_SERVLIST, CLIENT_USER, 0, 0, 0 },
97 { "SQUERY", IRC_SQUERY, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
98 { "SQUIT", IRC_SQUIT, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
99 { "STATS", IRC_STATS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
100 { "SUMMON", IRC_SUMMON, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
101 { "TIME", IRC_TIME, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
102 { "TOPIC", IRC_TOPIC, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
103 { "TRACE", IRC_TRACE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
104 { "USER", IRC_USER, 0xFFFF, 0, 0, 0 },
105 { "USERHOST", IRC_USERHOST, CLIENT_USER, 0, 0, 0 },
106 { "USERS", IRC_USERS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
107 { "VERSION", IRC_VERSION, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
108 { "WALLOPS", IRC_WALLOPS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
109 { "WEBIRC", IRC_WEBIRC, CLIENT_UNKNOWN, 0, 0, 0 },
110 { "WHO", IRC_WHO, CLIENT_USER, 0, 0, 0 },
111 { "WHOIS", IRC_WHOIS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
112 { "WHOWAS", IRC_WHOWAS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
114 { "CHANINFO", IRC_CHANINFO, CLIENT_SERVER, 0, 0, 0 },
117 { "CAP", IRC_CAP, 0xFFFF, 0, 0, 0 },
118 { "GET", IRC_QUIT_HTTP, CLIENT_UNKNOWN, 0, 0, 0 },
119 { "POST", IRC_QUIT_HTTP, CLIENT_UNKNOWN, 0, 0, 0 },
121 { NULL, NULL, 0x0, 0, 0, 0 } /* Ende-Marke */
124 static void Init_Request PARAMS(( REQUEST *Req ));
126 static bool Validate_Prefix PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
127 static bool Validate_Command PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
128 static bool Validate_Args PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
130 static bool Handle_Request PARAMS(( CONN_ID Idx, REQUEST *Req ));
132 static bool ScrubCTCP PARAMS((char *Request));
135 * Return the pointer to the global "IRC command structure".
136 * This structure, an array of type "COMMAND" describes all the IRC commands
137 * implemented by ngIRCd and how to handle them.
138 * @return Pointer to the global command structure.
141 Parse_GetCommandStruct( void )
144 } /* Parse_GetCommandStruct */
148 * Parse a command ("request") received from a client.
150 * This function is called after the connection layer received a valid CR+LF
151 * terminated line of text: we asume that this is a valid IRC command and
152 * try to do something useful with it :-)
154 * All errors are reported to the client from which the command has been
155 * received, and if the error is fatal this connection is closed down.
157 * This function is able to parse the syntax as described in RFC 2812,
160 * @param Idx Index of the connection from which the command has been received.
161 * @param Request NULL terminated line of text (the "command").
162 * @return true on success (valid command or "regular" error), false if a
163 * fatal error occured and the connection has been shut down.
166 Parse_Request( CONN_ID Idx, char *Request )
173 assert( Request != NULL );
176 if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request );
179 Init_Request( &req );
181 /* remove leading & trailing whitespace */
182 ngt_TrimStr( Request );
184 if (Conf_ScrubCTCP && ScrubCTCP(Request))
187 if (Request[0] == ':') {
189 req.prefix = Request + 1;
190 ptr = strchr( Request, ' ' );
193 LogDebug("Connection %d: Parse error: prefix without command!?", Idx);
194 return Conn_WriteStr(Idx, "ERROR :Prefix without command");
198 /* ignore multiple spaces between prefix and command */
199 while( *(ptr + 1) == ' ' ) ptr++;
203 else start = Request;
205 ptr = strchr( start, ' ' );
210 /* ignore multiple spaces between parameters */
211 while( *(ptr + 1) == ' ' ) ptr++;
216 /* Arguments, Parameters */
222 if( start[0] == ':' )
224 req.argv[req.argc] = start + 1;
229 req.argv[req.argc] = start;
230 ptr = strchr( start, ' ' );
235 while( *(ptr + 1) == ' ' ) ptr++;
242 if( start[0] == ':' ) break;
243 if( req.argc > 14 ) break;
245 if( ptr ) start = ptr + 1;
250 if( ! Validate_Prefix( Idx, &req, &closed )) return ! closed;
251 if( ! Validate_Command( Idx, &req, &closed )) return ! closed;
252 if( ! Validate_Args( Idx, &req, &closed )) return ! closed;
254 return Handle_Request( Idx, &req );
255 } /* Parse_Request */
259 * Initialize request structure.
260 * @param Req Request structure to be initialized.
263 Init_Request( REQUEST *Req )
265 /* Neue Request-Struktur initialisieren */
269 assert( Req != NULL );
273 for( i = 0; i < 15; Req->argv[i++] = NULL );
279 Validate_Prefix( CONN_ID Idx, REQUEST *Req, bool *Closed )
284 assert( Req != NULL );
288 client = Conn_GetClient( Idx );
289 assert( client != NULL );
291 if (!Req->prefix && Client_Type(client) == CLIENT_SERVER
292 && !(Conn_Options(Idx) & CONN_RFC1459)
293 && strcasecmp(Req->command, "ERROR") != 0
294 && strcasecmp(Req->command, "PING") != 0)
297 "Received command without prefix (connection %d, command \"%s\")!?",
299 if (!Conn_WriteStr(Idx, "ERROR :Prefix missing"))
307 /* only validate if this connection is already registered */
308 if (Client_Type(client) != CLIENT_USER
309 && Client_Type(client) != CLIENT_SERVER
310 && Client_Type(client) != CLIENT_SERVICE) {
311 /* not registered, ignore prefix */
316 /* check if client in prefix is known */
317 c = Client_Search( Req->prefix );
320 "Invalid prefix \"%s\", client not known (connection %d, command \"%s\")!?",
321 Req->prefix, Idx, Req->command);
322 if (!Conn_WriteStr(Idx,
323 "ERROR :Invalid prefix \"%s\", client not known",
329 /* check if the client named in the prefix is expected
330 * to come from that direction */
331 if (Client_NextHop(c) != client) {
332 if (Client_Type(c) != CLIENT_SERVER) {
334 "Spoofed prefix \"%s\" from \"%s\" (connection %d, command \"%s\")!",
335 Req->prefix, Client_Mask(Conn_GetClient(Idx)), Idx,
337 Conn_Close(Idx, NULL, "Spoofed prefix", true);
341 "Ignoring spoofed prefix \"%s\" from \"%s\" (connection %d, command \"%s\").",
342 Req->prefix, Client_Mask(Conn_GetClient(Idx)), Idx,
350 } /* Validate_Prefix */
354 Validate_Command( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed )
357 assert( Req != NULL );
361 } /* Validate_Comman */
366 Validate_Args(CONN_ID Idx, REQUEST *Req, bool *Closed)
368 Validate_Args(UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed)
379 assert( Req != NULL );
381 /* CR and LF are never allowed in command parameters.
382 * But since we do accept lines terminated only with CR or LF in
383 * "non-RFC-compliant mode" (besides the correct CR+LF combination),
384 * this check can only trigger in "strict RFC" mode; therefore we
385 * optimize it away otherwise ... */
386 for (i = 0; i < Req->argc; i++) {
387 if (strchr(Req->argv[i], '\r') || strchr(Req->argv[i], '\n')) {
389 "Invalid character(s) in parameter (connection %d, command %s)!?",
391 if (!Conn_WriteStr(Idx,
392 "ERROR :Invalid character(s) in parameter!"))
400 } /* Validate_Args */
403 /* Command is a status code ("numeric") from another server */
405 Handle_Numeric(CLIENT *client, REQUEST *Req)
407 static const struct _NUMERIC Numerics[] = {
408 { 5, IRC_Num_ISUPPORT },
410 { 376, IRC_Num_ENDOFMOTD }
414 CLIENT *prefix, *target = NULL;
416 /* Determine target */
418 if (strcmp(Req->argv[0], "*") != 0)
419 target = Client_Search(Req->argv[0]);
421 target = Client_ThisServer();
425 /* Status code without target!? */
428 "Unknown target for status code %s: \"%s\"",
429 Req->command, Req->argv[0]);
432 "Unknown target for status code %s!",
436 if (target == Client_ThisServer()) {
437 /* This server is the target of the numeric */
438 num = atoi(Req->command);
440 for (i = 0; i < (int) C_ARRAY_SIZE(Numerics); i++) {
441 if (num == Numerics[i].numeric) {
442 if (!Numerics[i].function)
444 return Numerics[i].function(client, Req);
448 LogDebug("Ignored status code %s from \"%s\".",
449 Req->command, Client_ID(client));
453 /* Determine source */
454 if (! Req->prefix[0]) {
455 /* Oops, no prefix!? */
456 Log(LOG_WARNING, "Got status code %s from \"%s\" without prefix!?",
457 Req->command, Client_ID(client));
461 prefix = Client_Search(Req->prefix);
462 if (! prefix) { /* Oops, unknown prefix!? */
463 Log(LOG_WARNING, "Got status code %s from unknown source: \"%s\"", Req->command, Req->prefix);
467 /* Forward status code */
468 strlcpy(str, Req->command, sizeof(str));
469 for (i = 0; i < Req->argc; i++) {
470 if (i < Req->argc - 1)
471 strlcat(str, " ", sizeof(str));
473 strlcat(str, " :", sizeof(str));
474 strlcat(str, Req->argv[i], sizeof(str));
476 return IRC_WriteStrClientPrefix(target, prefix, "%s", str);
480 Handle_Request( CONN_ID Idx, REQUEST *Req )
488 assert( Req != NULL );
489 assert( Req->command != NULL );
491 client = Conn_GetClient( Idx );
492 assert( client != NULL );
495 client_type = Client_Type(client);
496 if ((client_type == CLIENT_SERVER ||
497 client_type == CLIENT_UNKNOWNSERVER)
498 && strlen(Req->command) == 3 && atoi(Req->command) > 1)
499 return Handle_Numeric(client, Req);
503 if (strcasecmp(Req->command, cmd->name) != 0) {
508 if (!(client_type & cmd->type))
509 return IRC_WriteStrClient(client, ERR_NOTREGISTERED_MSG, Client_ID(client));
511 /* Command is allowed for this client: call it and count produced bytes */
512 Conn_ResetWCounter();
513 result = (cmd->function)(client, Req);
514 cmd->bytes += Conn_WCounter();
516 /* Adjust counters */
517 if (client_type != CLIENT_SERVER)
524 if (client_type != CLIENT_USER &&
525 client_type != CLIENT_SERVER &&
526 client_type != CLIENT_SERVICE )
529 /* Unknown command and registered connection: generate error: */
530 LogDebug("Connection %d: Unknown command \"%s\", %d %s,%s prefix.",
531 Client_Conn( client ), Req->command, Req->argc,
532 Req->argc == 1 ? "parameter" : "parameters",
533 Req->prefix ? "" : " no" );
535 if (Client_Type(client) != CLIENT_SERVER) {
536 result = IRC_WriteStrClient(client, ERR_UNKNOWNCOMMAND_MSG,
537 Client_ID(client), Req->command);
538 Conn_SetPenalty(Idx, 1);
541 } /* Handle_Request */
545 * Check if incoming messages contains CTCP commands and should be dropped.
547 * @param Request NULL terminated incoming command.
548 * @returns true, when the message should be dropped.
551 ScrubCTCP(char *Request)
553 static const char me_cmd[] = "ACTION ";
554 static const char ctcp_char = 0x1;
555 bool dropCommand = false;
557 char *ptrEnd = strchr(Request, '\0');
559 if (Request[0] == ':' && ptrEnd > ptr)
562 while (ptr != ptrEnd && *ptr != ':')
565 if ((ptrEnd - ptr) > 1) {
567 if (*ptr == ctcp_char) {
570 /* allow /me commands */
571 if ((size_t)(ptrEnd - ptr) >= strlen(me_cmd)
572 && !strncmp(ptr, me_cmd, strlen(me_cmd)))