parse.c 13 KB

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