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"
53 bool (*function) PARAMS(( CLIENT *Client, REQUEST *Request ));
57 static COMMAND My_Commands[] =
59 { "ADMIN", IRC_ADMIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
60 { "AWAY", IRC_AWAY, CLIENT_USER, 0, 0, 0 },
61 { "CONNECT", IRC_CONNECT, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
62 { "DIE", IRC_DIE, CLIENT_USER, 0, 0, 0 },
63 { "DISCONNECT", IRC_DISCONNECT, CLIENT_USER, 0, 0, 0 },
64 { "ERROR", IRC_ERROR, 0xFFFF, 0, 0, 0 },
65 { "HELP", IRC_HELP, CLIENT_USER, 0, 0, 0 },
66 { "INFO", IRC_INFO, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
67 { "INVITE", IRC_INVITE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
68 { "ISON", IRC_ISON, CLIENT_USER, 0, 0, 0 },
69 { "JOIN", IRC_JOIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
70 { "KICK", IRC_KICK, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
71 { "KILL", IRC_KILL, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
72 { "LINKS", IRC_LINKS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
73 { "LIST", IRC_LIST, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
74 { "LUSERS", IRC_LUSERS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
75 { "MODE", IRC_MODE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
76 { "MOTD", IRC_MOTD, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
77 { "NAMES", IRC_NAMES, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
78 { "NICK", IRC_NICK, 0xFFFF, 0, 0, 0 },
79 { "NJOIN", IRC_NJOIN, CLIENT_SERVER, 0, 0, 0 },
80 { "NOTICE", IRC_NOTICE, 0xFFFF, 0, 0, 0 },
81 { "OPER", IRC_OPER, CLIENT_USER, 0, 0, 0 },
82 { "PART", IRC_PART, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
83 { "PASS", IRC_PASS, 0xFFFF, 0, 0, 0 },
84 { "PING", IRC_PING, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
85 { "PONG", IRC_PONG, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
86 { "PRIVMSG", IRC_PRIVMSG, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
87 { "QUIT", IRC_QUIT, 0xFFFF, 0, 0, 0 },
88 { "REHASH", IRC_REHASH, CLIENT_USER, 0, 0, 0 },
89 { "RESTART", IRC_RESTART, CLIENT_USER, 0, 0, 0 },
90 { "SERVER", IRC_SERVER, 0xFFFF, 0, 0, 0 },
91 { "SERVICE", IRC_SERVICE, 0xFFFF, 0, 0, 0 },
92 { "SERVLIST", IRC_SERVLIST, CLIENT_USER, 0, 0, 0 },
93 { "SQUERY", IRC_SQUERY, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
94 { "SQUIT", IRC_SQUIT, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
95 { "STATS", IRC_STATS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
96 { "SUMMON", IRC_SUMMON, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
97 { "TIME", IRC_TIME, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
98 { "TOPIC", IRC_TOPIC, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
99 { "TRACE", IRC_TRACE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
100 { "USER", IRC_USER, 0xFFFF, 0, 0, 0 },
101 { "USERHOST", IRC_USERHOST, CLIENT_USER, 0, 0, 0 },
102 { "USERS", IRC_USERS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
103 { "VERSION", IRC_VERSION, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
104 { "WALLOPS", IRC_WALLOPS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
105 { "WEBIRC", IRC_WEBIRC, CLIENT_UNKNOWN, 0, 0, 0 },
106 { "WHO", IRC_WHO, CLIENT_USER, 0, 0, 0 },
107 { "WHOIS", IRC_WHOIS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
108 { "WHOWAS", IRC_WHOWAS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
110 { "CHANINFO", IRC_CHANINFO, CLIENT_SERVER, 0, 0, 0 },
113 { "GET", IRC_QUIT_HTTP, CLIENT_UNKNOWN, 0, 0, 0 },
114 { "POST", IRC_QUIT_HTTP, CLIENT_UNKNOWN, 0, 0, 0 },
116 { NULL, NULL, 0x0, 0, 0, 0 } /* Ende-Marke */
119 static void Init_Request PARAMS(( REQUEST *Req ));
121 static bool Validate_Prefix PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
122 static bool Validate_Command PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
123 static bool Validate_Args PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
125 static bool Handle_Request PARAMS(( CONN_ID Idx, REQUEST *Req ));
128 * Return the pointer to the global "IRC command structure".
129 * This structure, an array of type "COMMAND" describes all the IRC commands
130 * implemented by ngIRCd and how to handle them.
131 * @return Pointer to the global command structure.
134 Parse_GetCommandStruct( void )
137 } /* Parse_GetCommandStruct */
141 * Parse a command ("request") received from a client.
143 * This function is called after the connection layer received a valid CR+LF
144 * terminated line of text: we asume that this is a valid IRC command and
145 * try to do something useful with it :-)
147 * All errors are reported to the client from which the command has been
148 * received, and if the error is fatal this connection is closed down.
150 * This function is able to parse the syntax as described in RFC 2812,
153 * @param Idx Index of the connection from which the command has been received.
154 * @param Request NULL terminated line of text (the "command").
155 * @return true on success (valid command or "regular" error), false if a
156 * fatal error occured and the connection has been shut down.
159 Parse_Request( CONN_ID Idx, char *Request )
166 assert( Request != NULL );
169 if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request );
172 Init_Request( &req );
174 /* remove leading & trailing whitespace */
175 ngt_TrimStr( Request );
177 if( Request[0] == ':' )
180 req.prefix = Request + 1;
181 ptr = strchr( Request, ' ' );
184 LogDebug("Connection %d: Parse error: prefix without command!?", Idx);
185 return Conn_WriteStr(Idx, "ERROR :Prefix without command");
189 /* ignore multiple spaces between prefix and command */
190 while( *(ptr + 1) == ' ' ) ptr++;
194 else start = Request;
196 ptr = strchr( start, ' ' );
201 /* ignore multiple spaces between parameters */
202 while( *(ptr + 1) == ' ' ) ptr++;
207 /* Arguments, Parameters */
213 if( start[0] == ':' )
215 req.argv[req.argc] = start + 1;
220 req.argv[req.argc] = start;
221 ptr = strchr( start, ' ' );
226 while( *(ptr + 1) == ' ' ) ptr++;
233 if( start[0] == ':' ) break;
234 if( req.argc > 14 ) break;
236 if( ptr ) start = ptr + 1;
241 if( ! Validate_Prefix( Idx, &req, &closed )) return ! closed;
242 if( ! Validate_Command( Idx, &req, &closed )) return ! closed;
243 if( ! Validate_Args( Idx, &req, &closed )) return ! closed;
245 return Handle_Request( Idx, &req );
246 } /* Parse_Request */
250 * Initialize request structure.
251 * @param Req Request structure to be initialized.
254 Init_Request( REQUEST *Req )
256 /* Neue Request-Struktur initialisieren */
260 assert( Req != NULL );
264 for( i = 0; i < 15; Req->argv[i++] = NULL );
270 Validate_Prefix( CONN_ID Idx, REQUEST *Req, bool *Closed )
275 assert( Req != NULL );
279 if( ! Req->prefix ) return true;
281 client = Conn_GetClient( Idx );
282 assert( client != NULL );
284 /* only validate if this connection is already registered */
285 if (Client_Type(client) != CLIENT_USER
286 && Client_Type(client) != CLIENT_SERVER
287 && Client_Type(client) != CLIENT_SERVICE) {
288 /* not registered, ignore prefix */
293 /* check if client in prefix is known */
294 c = Client_Search( Req->prefix );
297 "Invalid prefix \"%s\", client not known (connection %d, command \"%s\")!?",
298 Req->prefix, Idx, Req->command);
299 if (!Conn_WriteStr(Idx,
300 "ERROR :Invalid prefix \"%s\", client not known",
306 /* check if the client named in the prefix is expected
307 * to come from that direction */
308 if (Client_NextHop(c) != client) {
310 "Spoofed prefix \"%s\" from \"%s\" (connection %d, command \"%s\")!",
311 Req->prefix, Client_Mask(Conn_GetClient(Idx)), Idx,
313 Conn_Close(Idx, NULL, "Spoofed prefix", true);
319 } /* Validate_Prefix */
323 Validate_Command( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed )
326 assert( Req != NULL );
330 } /* Validate_Comman */
335 Validate_Args(CONN_ID Idx, REQUEST *Req, bool *Closed)
337 Validate_Args(UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed)
348 assert( Req != NULL );
350 /* CR and LF are never allowed in command parameters.
351 * But since we do accept lines terminated only with CR or LF in
352 * "non-RFC-compliant mode" (besides the correct CR+LF combination),
353 * this check can only trigger in "strict RFC" mode; therefore we
354 * optimize it away otherwise ... */
355 for (i = 0; i < Req->argc; i++) {
356 if (strchr(Req->argv[i], '\r') || strchr(Req->argv[i], '\n')) {
358 "Invalid character(s) in parameter (connection %d, command %s)!?",
360 if (!Conn_WriteStr(Idx,
361 "ERROR :Invalid character(s) in parameter!"))
369 } /* Validate_Args */
372 /* Command is a status code ("numeric") from another server */
374 Handle_Numeric(CLIENT *client, REQUEST *Req)
376 static const struct _NUMERIC Numerics[] = {
377 { 5, IRC_Num_ISUPPORT },
379 { 376, IRC_Num_ENDOFMOTD }
383 CLIENT *prefix, *target = NULL;
385 /* Determine target */
387 if (strcmp(Req->argv[0], "*") != 0)
388 target = Client_Search(Req->argv[0]);
390 target = Client_ThisServer();
394 /* Status code without target!? */
397 "Unknown target for status code %s: \"%s\"",
398 Req->command, Req->argv[0]);
401 "Unknown target for status code %s!",
405 if (target == Client_ThisServer()) {
406 /* This server is the target of the numeric */
407 num = atoi(Req->command);
409 for (i = 0; i < (int) C_ARRAY_SIZE(Numerics); i++) {
410 if (num == Numerics[i].numeric) {
411 if (!Numerics[i].function)
413 return Numerics[i].function(client, Req);
417 LogDebug("Ignored status code %s from \"%s\".",
418 Req->command, Client_ID(client));
422 /* Determine source */
423 if (! Req->prefix[0]) {
424 /* Oops, no prefix!? */
425 Log(LOG_WARNING, "Got status code %s from \"%s\" without prefix!?",
426 Req->command, Client_ID(client));
430 prefix = Client_Search(Req->prefix);
431 if (! prefix) { /* Oops, unknown prefix!? */
432 Log(LOG_WARNING, "Got status code %s from unknown source: \"%s\"", Req->command, Req->prefix);
436 /* Forward status code */
437 strlcpy(str, Req->command, sizeof(str));
438 for (i = 0; i < Req->argc; i++) {
439 if (i < Req->argc - 1)
440 strlcat(str, " ", sizeof(str));
442 strlcat(str, " :", sizeof(str));
443 strlcat(str, Req->argv[i], sizeof(str));
445 return IRC_WriteStrClientPrefix(target, prefix, "%s", str);
450 Handle_Request( CONN_ID Idx, REQUEST *Req )
458 assert( Req != NULL );
459 assert( Req->command != NULL );
461 client = Conn_GetClient( Idx );
462 assert( client != NULL );
465 client_type = Client_Type(client);
466 if ((client_type == CLIENT_SERVER ||
467 client_type == CLIENT_UNKNOWNSERVER)
468 && strlen(Req->command) == 3 && atoi(Req->command) > 1)
469 return Handle_Numeric(client, Req);
473 if (strcasecmp(Req->command, cmd->name) != 0) {
478 if (!(client_type & cmd->type))
479 return IRC_WriteStrClient(client, ERR_NOTREGISTERED_MSG, Client_ID(client));
481 /* Command is allowed for this client: call it and count produced bytes */
482 Conn_ResetWCounter();
483 result = (cmd->function)(client, Req);
484 cmd->bytes += Conn_WCounter();
486 /* Adjust counters */
487 if (client_type != CLIENT_SERVER)
494 if (client_type != CLIENT_USER &&
495 client_type != CLIENT_SERVER &&
496 client_type != CLIENT_SERVICE )
499 /* Unknown command and registered connection: generate error: */
500 LogDebug("Connection %d: Unknown command \"%s\", %d %s,%s prefix.",
501 Client_Conn( client ), Req->command, Req->argc,
502 Req->argc == 1 ? "parameter" : "parameters",
503 Req->prefix ? "" : " no" );
505 if (Client_Type(client) != CLIENT_SERVER) {
506 result = IRC_WriteStrClient(client, ERR_UNKNOWNCOMMAND_MSG,
507 Client_ID(client), Req->command);
508 Conn_SetPenalty(Idx, 1);
511 } /* Handle_Request */