parse.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
  4. *
  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.
  10. */
  11. #include "portab.h"
  12. /**
  13. * @file
  14. * IRC command parser and validator.
  15. */
  16. #include "imp.h"
  17. #include <assert.h>
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <strings.h>
  22. #include "ngircd.h"
  23. #include "defines.h"
  24. #include "conn-func.h"
  25. #include "channel.h"
  26. #include "log.h"
  27. #include "messages.h"
  28. #include "tool.h"
  29. #include "exp.h"
  30. #include "parse.h"
  31. #include "imp.h"
  32. #include "irc.h"
  33. #include "irc-cap.h"
  34. #include "irc-channel.h"
  35. #include "irc-encoding.h"
  36. #include "irc-info.h"
  37. #include "irc-login.h"
  38. #include "irc-metadata.h"
  39. #include "irc-mode.h"
  40. #include "irc-op.h"
  41. #include "irc-oper.h"
  42. #include "irc-server.h"
  43. #include "irc-write.h"
  44. #include "numeric.h"
  45. #include "exp.h"
  46. #include "conf.h"
  47. struct _NUMERIC {
  48. int numeric;
  49. bool (*function) PARAMS(( CLIENT *Client, REQUEST *Request ));
  50. };
  51. static COMMAND My_Commands[] =
  52. {
  53. { "ADMIN", IRC_ADMIN, CLIENT_USER|CLIENT_SERVER, 0, 1, 0, 0, 0 },
  54. { "AWAY", IRC_AWAY, CLIENT_USER, 0, 1, 0, 0, 0 },
  55. { "CAP", IRC_CAP, CLIENT_ANY, 1, 2, 0, 0, 0 },
  56. { "CONNECT", IRC_CONNECT, CLIENT_USER|CLIENT_SERVER, 0, -1, 0, 0, 0 },
  57. #ifdef STRICT_RFC
  58. { "DIE", IRC_DIE, CLIENT_USER, 0, 0, 0, 0, 0 },
  59. #else
  60. { "DIE", IRC_DIE, CLIENT_USER, 0, 1, 0, 0, 0 },
  61. #endif
  62. { "DISCONNECT", IRC_DISCONNECT, CLIENT_USER, 1, 1, 0, 0, 0 },
  63. { "ERROR", IRC_ERROR, CLIENT_ANY, 0, -1, 0, 0, 0 },
  64. { "GLINE", IRC_xLINE, CLIENT_USER|CLIENT_SERVER, 0, -1, 0, 0, 0 },
  65. { "HELP", IRC_HELP, CLIENT_USER, 0, 1, 0, 0, 0 },
  66. { "INFO", IRC_INFO, CLIENT_USER|CLIENT_SERVER, 0, 1, 0, 0, 0 },
  67. { "INVITE", IRC_INVITE, CLIENT_USER|CLIENT_SERVER, 2, 2, 0, 0, 0 },
  68. { "ISON", IRC_ISON, CLIENT_USER, 1, -1, 0, 0, 0 },
  69. { "JOIN", IRC_JOIN, CLIENT_USER|CLIENT_SERVER, 1, 2, 0, 0, 0 },
  70. { "KICK", IRC_KICK, CLIENT_USER|CLIENT_SERVER, 2, 3, 0, 0, 0 },
  71. { "KILL", IRC_KILL, CLIENT_USER|CLIENT_SERVER, 2, 2, 0, 0, 0 },
  72. { "KLINE", IRC_xLINE, CLIENT_USER|CLIENT_SERVER, 0, -1, 0, 0, 0 },
  73. { "LINKS", IRC_LINKS, CLIENT_USER|CLIENT_SERVER, 0, 2, 0, 0, 0 },
  74. { "LIST", IRC_LIST, CLIENT_USER|CLIENT_SERVER, 0, 2, 0, 0, 0 },
  75. { "LUSERS", IRC_LUSERS, CLIENT_USER|CLIENT_SERVER, 0, 2, 0, 0, 0 },
  76. { "METADATA", IRC_METADATA, CLIENT_SERVER, 3, 3, 0, 0, 0 },
  77. { "MODE", IRC_MODE, CLIENT_USER|CLIENT_SERVER, 1, -1, 0, 0, 0 },
  78. { "MOTD", IRC_MOTD, CLIENT_USER|CLIENT_SERVER, 0, 1, 0, 0, 0 },
  79. { "NAMES", IRC_NAMES, CLIENT_USER|CLIENT_SERVER, 0, 2, 0, 0, 0 },
  80. { "NICK", IRC_NICK, CLIENT_ANY, 0, -1, 0, 0, 0 },
  81. { "NJOIN", IRC_NJOIN, CLIENT_SERVER, 2, 2, 0, 0, 0 },
  82. { "NOTICE", IRC_NOTICE, CLIENT_ANY, 0, -1, 0, 0, 0 },
  83. { "OPER", IRC_OPER, CLIENT_USER, 2, 2, 0, 0, 0 },
  84. { "PART", IRC_PART, CLIENT_USER|CLIENT_SERVER, 1, 2, 0, 0, 0 },
  85. { "PASS", IRC_PASS, CLIENT_ANY, 0, -1, 0, 0, 0 },
  86. { "PING", IRC_PING, CLIENT_USER|CLIENT_SERVER, 0, -1, 0, 0, 0 },
  87. { "PONG", IRC_PONG, CLIENT_ANY, 0, -1, 0, 0, 0 },
  88. { "PRIVMSG", IRC_PRIVMSG, CLIENT_USER|CLIENT_SERVER, 0, 2, 0, 0, 0 },
  89. { "QUIT", IRC_QUIT, CLIENT_ANY, 0, 1, 0, 0, 0 },
  90. { "REHASH", IRC_REHASH, CLIENT_USER, 0, 0, 0, 0, 0 },
  91. { "RESTART", IRC_RESTART, CLIENT_USER, 0, 0, 0, 0, 0 },
  92. { "SERVER", IRC_SERVER, CLIENT_ANY, 0, -1, 0, 0, 0 },
  93. { "SERVICE", IRC_SERVICE, CLIENT_ANY, 6, 6, 0, 0, 0 },
  94. { "SERVLIST", IRC_SERVLIST, CLIENT_USER, 0, 2, 0, 0, 0 },
  95. { "SQUERY", IRC_SQUERY, CLIENT_USER|CLIENT_SERVER, 0, 2, 0, 0, 0 },
  96. { "SQUIT", IRC_SQUIT, CLIENT_USER|CLIENT_SERVER, 2, 2, 0, 0, 0 },
  97. { "STATS", IRC_STATS, CLIENT_USER|CLIENT_SERVER, 0, 2, 0, 0, 0 },
  98. { "SVSNICK", IRC_SVSNICK, CLIENT_SERVER, 2, 2, 0, 0, 0 },
  99. { "SUMMON", IRC_SUMMON, CLIENT_USER|CLIENT_SERVER, 0, -1, 0, 0, 0 },
  100. { "TIME", IRC_TIME, CLIENT_USER|CLIENT_SERVER, 0, 1, 0, 0, 0 },
  101. { "TOPIC", IRC_TOPIC, CLIENT_USER|CLIENT_SERVER, 1, 2, 0, 0, 0 },
  102. { "TRACE", IRC_TRACE, CLIENT_USER|CLIENT_SERVER, 0, 1, 0, 0, 0 },
  103. { "USER", IRC_USER, CLIENT_ANY, 0, -1, 0, 0, 0 },
  104. { "USERHOST", IRC_USERHOST, CLIENT_USER, 1, -1, 0, 0, 0 },
  105. { "USERS", IRC_USERS, CLIENT_USER|CLIENT_SERVER, 0, -1, 0, 0, 0 },
  106. { "VERSION", IRC_VERSION, CLIENT_USER|CLIENT_SERVER, 0, 1, 0, 0, 0 },
  107. { "WALLOPS", IRC_WALLOPS, CLIENT_USER|CLIENT_SERVER, 1, 1, 0, 0, 0 },
  108. { "WEBIRC", IRC_WEBIRC, CLIENT_UNKNOWN, 4, 4, 0, 0, 0 },
  109. { "WHO", IRC_WHO, CLIENT_USER, 0, 2, 0, 0, 0 },
  110. { "WHOIS", IRC_WHOIS, CLIENT_USER|CLIENT_SERVER, 0, -1, 0, 0, 0 },
  111. { "WHOWAS", IRC_WHOWAS, CLIENT_USER|CLIENT_SERVER, 0, -1, 0, 0, 0 },
  112. #ifdef IRCPLUS
  113. { "CHANINFO", IRC_CHANINFO, CLIENT_SERVER, 0, -1, 0, 0, 0 },
  114. # ifdef ICONV
  115. { "CHARCONV", IRC_CHARCONV, CLIENT_USER, 1, 1, 0, 0, 0 },
  116. # endif
  117. #endif
  118. #ifndef STRICT_RFC
  119. { "GET", IRC_QUIT_HTTP, CLIENT_UNKNOWN, 0, -1, 0, 0, 0 },
  120. { "POST", IRC_QUIT_HTTP, CLIENT_UNKNOWN, 0, -1, 0, 0, 0 },
  121. #endif
  122. { NULL, NULL, 0x0, 0, 0, 0, 0, 0 } /* End-Mark */
  123. };
  124. static void Init_Request PARAMS(( REQUEST *Req ));
  125. static bool Validate_Prefix PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
  126. static bool Validate_Command PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
  127. static bool Validate_Args PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
  128. static bool Handle_Request PARAMS(( CONN_ID Idx, REQUEST *Req ));
  129. static bool ScrubCTCP PARAMS((char *Request));
  130. /**
  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.
  135. */
  136. GLOBAL COMMAND *
  137. Parse_GetCommandStruct( void )
  138. {
  139. return My_Commands;
  140. } /* Parse_GetCommandStruct */
  141. /**
  142. * Parse a command ("request") received from a client.
  143. *
  144. * This function is called after the connection layer received a valid CR+LF
  145. * terminated line of text: we assume that this is a valid IRC command and
  146. * try to do something useful with it :-)
  147. *
  148. * All errors are reported to the client from which the command has been
  149. * received, and if the error is fatal this connection is closed down.
  150. *
  151. * This function is able to parse the syntax as described in RFC 2812,
  152. * section 2.3.
  153. *
  154. * @param Idx Index of the connection from which the command has been received.
  155. * @param Request NULL terminated line of text (the "command").
  156. * @return true on success (valid command or "regular" error), false if a
  157. * fatal error occurred and the connection has been shut down.
  158. */
  159. GLOBAL bool
  160. Parse_Request( CONN_ID Idx, char *Request )
  161. {
  162. REQUEST req;
  163. char *start, *ptr;
  164. bool closed;
  165. assert( Idx >= 0 );
  166. assert( Request != NULL );
  167. #ifdef SNIFFER
  168. if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request );
  169. #endif
  170. Init_Request( &req );
  171. /* remove leading & trailing whitespace */
  172. ngt_TrimStr( Request );
  173. if (Conf_ScrubCTCP && ScrubCTCP(Request))
  174. return true;
  175. if (Request[0] == ':') {
  176. /* Prefix */
  177. req.prefix = Request + 1;
  178. ptr = strchr( Request, ' ' );
  179. if( ! ptr )
  180. {
  181. LogDebug("Connection %d: Parse error: prefix without command!?", Idx);
  182. return Conn_WriteStr(Idx, "ERROR :Prefix without command");
  183. }
  184. *ptr = '\0';
  185. #ifndef STRICT_RFC
  186. /* ignore multiple spaces between prefix and command */
  187. while( *(ptr + 1) == ' ' ) ptr++;
  188. #endif
  189. start = ptr + 1;
  190. }
  191. else start = Request;
  192. ptr = strchr( start, ' ' );
  193. if( ptr )
  194. {
  195. *ptr = '\0';
  196. #ifndef STRICT_RFC
  197. /* ignore multiple spaces between parameters */
  198. while( *(ptr + 1) == ' ' ) ptr++;
  199. #endif
  200. }
  201. req.command = start;
  202. /* Arguments, Parameters */
  203. if( ptr )
  204. {
  205. start = ptr + 1;
  206. while( start )
  207. {
  208. if( start[0] == ':' )
  209. {
  210. req.argv[req.argc] = start + 1;
  211. ptr = NULL;
  212. }
  213. else
  214. {
  215. req.argv[req.argc] = start;
  216. ptr = strchr( start, ' ' );
  217. if( ptr )
  218. {
  219. *ptr = '\0';
  220. #ifndef STRICT_RFC
  221. while( *(ptr + 1) == ' ' ) ptr++;
  222. #endif
  223. }
  224. }
  225. req.argc++;
  226. if( start[0] == ':' ) break;
  227. if( req.argc > 14 ) break;
  228. if( ptr ) start = ptr + 1;
  229. else start = NULL;
  230. }
  231. }
  232. if( ! Validate_Prefix( Idx, &req, &closed )) return ! closed;
  233. if( ! Validate_Command( Idx, &req, &closed )) return ! closed;
  234. if( ! Validate_Args( Idx, &req, &closed )) return ! closed;
  235. return Handle_Request( Idx, &req );
  236. } /* Parse_Request */
  237. /**
  238. * Initialize request structure.
  239. * @param Req Request structure to be initialized.
  240. */
  241. static void
  242. Init_Request( REQUEST *Req )
  243. {
  244. int i;
  245. assert( Req != NULL );
  246. Req->prefix = NULL;
  247. Req->command = NULL;
  248. for( i = 0; i < 15; Req->argv[i++] = NULL );
  249. Req->argc = 0;
  250. } /* Init_Request */
  251. static bool
  252. Validate_Prefix( CONN_ID Idx, REQUEST *Req, bool *Closed )
  253. {
  254. CLIENT *client, *c;
  255. assert( Idx >= 0 );
  256. assert( Req != NULL );
  257. *Closed = false;
  258. client = Conn_GetClient( Idx );
  259. assert( client != NULL );
  260. if (!Req->prefix && Client_Type(client) == CLIENT_SERVER
  261. && !(Conn_Options(Idx) & CONN_RFC1459)
  262. && strcasecmp(Req->command, "ERROR") != 0
  263. && strcasecmp(Req->command, "PING") != 0)
  264. {
  265. Log(LOG_ERR,
  266. "Received command without prefix (connection %d, command \"%s\")!?",
  267. Idx, Req->command);
  268. if (!Conn_WriteStr(Idx, "ERROR :Prefix missing"))
  269. *Closed = true;
  270. return false;
  271. }
  272. if (!Req->prefix)
  273. return true;
  274. /* only validate if this connection is already registered */
  275. if (Client_Type(client) != CLIENT_USER
  276. && Client_Type(client) != CLIENT_SERVER
  277. && Client_Type(client) != CLIENT_SERVICE) {
  278. /* not registered, ignore prefix */
  279. Req->prefix = NULL;
  280. return true;
  281. }
  282. /* check if client in prefix is known */
  283. c = Client_Search(Req->prefix);
  284. if (!c) {
  285. if (Client_Type(client) != CLIENT_SERVER) {
  286. Log(LOG_ERR,
  287. "Ignoring command with invalid prefix \"%s\" from \"%s\" (connection %d, command \"%s\")!",
  288. Req->prefix, Client_ID(client), Idx, Req->command);
  289. if (!Conn_WriteStr(Idx,
  290. "ERROR :Invalid prefix \"%s\"",
  291. Req->prefix))
  292. *Closed = true;
  293. IRC_SetPenalty(client, 2);
  294. } else
  295. LogDebug("Ignoring command with invalid prefix \"%s\" from \"%s\" (connection %d, command \"%s\")!",
  296. Req->prefix, Client_ID(client), Idx, Req->command);
  297. return false;
  298. }
  299. /* check if the client named in the prefix is expected
  300. * to come from that direction */
  301. if (Client_NextHop(c) != client) {
  302. if (Client_Type(client) != CLIENT_SERVER) {
  303. Log(LOG_ERR,
  304. "Spoofed prefix \"%s\" from \"%s\" (connection %d, command \"%s\"), closing connection!",
  305. Req->prefix, Client_ID(client), Idx, Req->command);
  306. Conn_Close(Idx, NULL, "Spoofed prefix", true);
  307. *Closed = true;
  308. } else {
  309. Log(LOG_WARNING,
  310. "Ignoring command with spoofed prefix \"%s\" from \"%s\" (connection %d, command \"%s\")!",
  311. Req->prefix, Client_ID(client), Idx, Req->command);
  312. }
  313. return false;
  314. }
  315. return true;
  316. } /* Validate_Prefix */
  317. static bool
  318. Validate_Command( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed )
  319. {
  320. assert( Idx >= 0 );
  321. assert( Req != NULL );
  322. *Closed = false;
  323. return true;
  324. } /* Validate_Command */
  325. static bool
  326. #ifdef STRICT_RFC
  327. Validate_Args(CONN_ID Idx, REQUEST *Req, bool *Closed)
  328. #else
  329. Validate_Args(UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed)
  330. #endif
  331. {
  332. #ifdef STRICT_RFC
  333. int i;
  334. #endif
  335. *Closed = false;
  336. #ifdef STRICT_RFC
  337. assert( Idx >= 0 );
  338. assert( Req != NULL );
  339. /* CR and LF are never allowed in command parameters.
  340. * But since we do accept lines terminated only with CR or LF in
  341. * "non-RFC-compliant mode" (besides the correct CR+LF combination),
  342. * this check can only trigger in "strict RFC" mode; therefore we
  343. * optimize it away otherwise ... */
  344. for (i = 0; i < Req->argc; i++) {
  345. if (strchr(Req->argv[i], '\r') || strchr(Req->argv[i], '\n')) {
  346. Log(LOG_ERR,
  347. "Invalid character(s) in parameter (connection %d, command %s)!?",
  348. Idx, Req->command);
  349. if (!Conn_WriteStr(Idx,
  350. "ERROR :Invalid character(s) in parameter!"))
  351. *Closed = true;
  352. return false;
  353. }
  354. }
  355. #endif
  356. return true;
  357. } /* Validate_Args */
  358. /* Command is a status code ("numeric") from another server */
  359. static bool
  360. Handle_Numeric(CLIENT *client, REQUEST *Req)
  361. {
  362. static const struct _NUMERIC Numerics[] = {
  363. { 5, IRC_Num_ISUPPORT },
  364. { 20, NULL },
  365. { 376, IRC_Num_ENDOFMOTD }
  366. };
  367. int i, num;
  368. char str[COMMAND_LEN];
  369. CLIENT *prefix, *target = NULL;
  370. /* Determine target */
  371. if (Req->argc > 0) {
  372. if (strcmp(Req->argv[0], "*") != 0)
  373. target = Client_Search(Req->argv[0]);
  374. else
  375. target = Client_ThisServer();
  376. }
  377. if (!target) {
  378. /* Status code without target!? */
  379. if (Req->argc > 0)
  380. Log(LOG_WARNING,
  381. "Unknown target for status code %s: \"%s\"",
  382. Req->command, Req->argv[0]);
  383. else
  384. Log(LOG_WARNING,
  385. "Unknown target for status code %s!",
  386. Req->command);
  387. return true;
  388. }
  389. if (target == Client_ThisServer()) {
  390. /* This server is the target of the numeric */
  391. num = atoi(Req->command);
  392. for (i = 0; i < (int) C_ARRAY_SIZE(Numerics); i++) {
  393. if (num == Numerics[i].numeric) {
  394. if (!Numerics[i].function)
  395. return CONNECTED;
  396. return Numerics[i].function(client, Req);
  397. }
  398. }
  399. LogDebug("Ignored status code %s from \"%s\".",
  400. Req->command, Client_ID(client));
  401. return true;
  402. }
  403. /* Determine source */
  404. if (! Req->prefix[0]) {
  405. /* Oops, no prefix!? */
  406. Log(LOG_WARNING, "Got status code %s from \"%s\" without prefix!?",
  407. Req->command, Client_ID(client));
  408. return true;
  409. }
  410. prefix = Client_Search(Req->prefix);
  411. if (! prefix) { /* Oops, unknown prefix!? */
  412. Log(LOG_WARNING, "Got status code %s from unknown source: \"%s\"", Req->command, Req->prefix);
  413. return true;
  414. }
  415. /* Forward status code */
  416. strlcpy(str, Req->command, sizeof(str));
  417. for (i = 0; i < Req->argc; i++) {
  418. if (i < Req->argc - 1)
  419. strlcat(str, " ", sizeof(str));
  420. else
  421. strlcat(str, " :", sizeof(str));
  422. strlcat(str, Req->argv[i], sizeof(str));
  423. }
  424. return IRC_WriteStrClientPrefix(target, prefix, "%s", str);
  425. }
  426. static bool
  427. Handle_Request( CONN_ID Idx, REQUEST *Req )
  428. {
  429. CLIENT *client;
  430. bool result = true;
  431. int client_type;
  432. COMMAND *cmd;
  433. assert( Idx >= 0 );
  434. assert( Req != NULL );
  435. assert( Req->command != NULL );
  436. client = Conn_GetClient( Idx );
  437. assert( client != NULL );
  438. /* Numeric? */
  439. client_type = Client_Type(client);
  440. if ((client_type == CLIENT_SERVER ||
  441. client_type == CLIENT_UNKNOWNSERVER)
  442. && strlen(Req->command) == 3 && atoi(Req->command) > 1)
  443. return Handle_Numeric(client, Req);
  444. cmd = My_Commands;
  445. while (cmd->name) {
  446. if (strcasecmp(Req->command, cmd->name) != 0) {
  447. cmd++;
  448. continue;
  449. }
  450. if (!(client_type & cmd->type)) {
  451. if (client_type == CLIENT_USER
  452. && cmd->type & CLIENT_SERVER)
  453. return IRC_WriteErrClient(client,
  454. ERR_NOTREGISTEREDSERVER_MSG,
  455. Client_ID(client));
  456. else
  457. return IRC_WriteErrClient(client,
  458. ERR_NOTREGISTERED_MSG,
  459. Client_ID(client));
  460. }
  461. if (Req->argc < cmd->min_argc ||
  462. (cmd->max_argc != -1 && Req->argc > cmd->max_argc)) {
  463. IRC_SetPenalty(client, 2);
  464. return IRC_WriteStrClient(client, ERR_NEEDMOREPARAMS_MSG,
  465. Client_ID(client), Req->command);
  466. }
  467. /* Command is allowed for this client: call it and count
  468. * generated bytes in output */
  469. Conn_ResetWCounter();
  470. result = (cmd->function)(client, Req);
  471. cmd->bytes += Conn_WCounter();
  472. /* Adjust counters */
  473. if (client_type != CLIENT_SERVER)
  474. cmd->lcount++;
  475. else
  476. cmd->rcount++;
  477. return result;
  478. }
  479. if (client_type != CLIENT_USER &&
  480. client_type != CLIENT_SERVER &&
  481. client_type != CLIENT_SERVICE )
  482. return true;
  483. /* Unknown command and registered connection: generate error: */
  484. LogDebug("Connection %d: Unknown command \"%s\", %d %s,%s prefix.",
  485. Client_Conn( client ), Req->command, Req->argc,
  486. Req->argc == 1 ? "parameter" : "parameters",
  487. Req->prefix ? "" : " no" );
  488. if (Client_Type(client) != CLIENT_SERVER)
  489. result = IRC_WriteErrClient(client, ERR_UNKNOWNCOMMAND_MSG,
  490. Client_ID(client), Req->command);
  491. return result;
  492. } /* Handle_Request */
  493. /**
  494. * Check if incoming messages contains CTCP commands and should be dropped.
  495. *
  496. * @param Request NULL terminated incoming command.
  497. * @returns true, when the message should be dropped.
  498. */
  499. static bool
  500. ScrubCTCP(char *Request)
  501. {
  502. static const char me_cmd[] = "ACTION ";
  503. static const char ctcp_char = 0x1;
  504. bool dropCommand = false;
  505. char *ptr = Request;
  506. char *ptrEnd = strchr(Request, '\0');
  507. if (Request[0] == ':' && ptrEnd > ptr)
  508. ptr++;
  509. while (ptr != ptrEnd && *ptr != ':')
  510. ptr++;
  511. if ((ptrEnd - ptr) > 1) {
  512. ptr++;
  513. if (*ptr == ctcp_char) {
  514. dropCommand = true;
  515. ptr++;
  516. /* allow /me commands */
  517. if ((size_t)(ptrEnd - ptr) >= strlen(me_cmd)
  518. && !strncmp(ptr, me_cmd, strlen(me_cmd)))
  519. dropCommand = false;
  520. }
  521. }
  522. return dropCommand;
  523. }
  524. /* -eof- */