parse.c 15 KB

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