parse.c 15 KB

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