parse.c 15 KB

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