parse.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2008 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 "client.h"
  26. #include "channel.h"
  27. #include "log.h"
  28. #include "messages.h"
  29. #include "tool.h"
  30. #include "exp.h"
  31. #include "parse.h"
  32. #include "imp.h"
  33. #include "irc.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. 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, CLIENT_USER|CLIENT_SERVER, 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. { "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. #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
  110. /**
  111. * Return the pointer to the global "IRC command structure".
  112. * This structure, an array of type "COMMAND" describes all the IRC commands
  113. * implemented by ngIRCd and how to handle them.
  114. * @return Pointer to the global command structure.
  115. */
  116. GLOBAL COMMAND *
  117. Parse_GetCommandStruct( void )
  118. {
  119. return My_Commands;
  120. } /* Parse_GetCommandStruct */
  121. /**
  122. * Parse a command ("request") received from a client.
  123. *
  124. * This function is called after the connection layer received a valid CR+LF
  125. * terminated line of text: we asume that this is a valid IRC command and
  126. * try to do something useful with it :-)
  127. *
  128. * All errors are reported to the client from which the command has been
  129. * received, and if the error is fatal this connection is closed down.
  130. *
  131. * This function is able to parse the syntax as described in RFC 2812,
  132. * section 2.3.
  133. *
  134. * @param Idx Index of the connection from which the command has been received.
  135. * @param Request NULL terminated line of text (the "command").
  136. * @return true on success (valid command or "regular" error), false if a
  137. * fatal error occured and the connection has been shut down.
  138. */
  139. GLOBAL bool
  140. Parse_Request( CONN_ID Idx, char *Request )
  141. {
  142. REQUEST req;
  143. char *start, *ptr;
  144. bool closed;
  145. assert( Idx >= 0 );
  146. assert( Request != NULL );
  147. #ifdef SNIFFER
  148. if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request );
  149. #endif
  150. Init_Request( &req );
  151. /* remove leading & trailing whitespace */
  152. ngt_TrimStr( Request );
  153. if( Request[0] == ':' )
  154. {
  155. /* Prefix */
  156. req.prefix = Request + 1;
  157. ptr = strchr( Request, ' ' );
  158. if( ! ptr )
  159. {
  160. LogDebug("Connection %d: Parse error: prefix without command!?", Idx);
  161. return Conn_WriteStr( Idx, "ERROR :Prefix without command!?" );
  162. }
  163. *ptr = '\0';
  164. #ifndef STRICT_RFC
  165. /* ignore multiple spaces between prefix and command */
  166. while( *(ptr + 1) == ' ' ) ptr++;
  167. #endif
  168. start = ptr + 1;
  169. }
  170. else start = Request;
  171. ptr = strchr( start, ' ' );
  172. if( ptr )
  173. {
  174. *ptr = '\0';
  175. #ifndef STRICT_RFC
  176. /* ignore multiple spaces between parameters */
  177. while( *(ptr + 1) == ' ' ) ptr++;
  178. #endif
  179. }
  180. req.command = start;
  181. /* Arguments, Parameters */
  182. if( ptr )
  183. {
  184. start = ptr + 1;
  185. while( start )
  186. {
  187. if( start[0] == ':' )
  188. {
  189. req.argv[req.argc] = start + 1;
  190. ptr = NULL;
  191. }
  192. else
  193. {
  194. req.argv[req.argc] = start;
  195. ptr = strchr( start, ' ' );
  196. if( ptr )
  197. {
  198. *ptr = '\0';
  199. #ifndef STRICT_RFC
  200. while( *(ptr + 1) == ' ' ) ptr++;
  201. #endif
  202. }
  203. }
  204. req.argc++;
  205. if( start[0] == ':' ) break;
  206. if( req.argc > 14 ) break;
  207. if( ptr ) start = ptr + 1;
  208. else start = NULL;
  209. }
  210. }
  211. if( ! Validate_Prefix( Idx, &req, &closed )) return ! closed;
  212. if( ! Validate_Command( Idx, &req, &closed )) return ! closed;
  213. if( ! Validate_Args( Idx, &req, &closed )) return ! closed;
  214. return Handle_Request( Idx, &req );
  215. } /* Parse_Request */
  216. /**
  217. * Initialize request structure.
  218. * @param Req Request structure to be initialized.
  219. */
  220. static void
  221. Init_Request( REQUEST *Req )
  222. {
  223. /* Neue Request-Struktur initialisieren */
  224. int i;
  225. assert( Req != NULL );
  226. Req->prefix = NULL;
  227. Req->command = NULL;
  228. for( i = 0; i < 15; Req->argv[i++] = NULL );
  229. Req->argc = 0;
  230. } /* Init_Request */
  231. static bool
  232. Validate_Prefix( CONN_ID Idx, REQUEST *Req, bool *Closed )
  233. {
  234. CLIENT *client, *c;
  235. assert( Idx >= 0 );
  236. assert( Req != NULL );
  237. *Closed = false;
  238. if( ! Req->prefix ) return true;
  239. client = Conn_GetClient( Idx );
  240. assert( client != NULL );
  241. /* only validate if this connection is already registered */
  242. if(( Client_Type( client ) != CLIENT_USER ) && ( Client_Type( client ) != CLIENT_SERVER ) && ( Client_Type( client ) != CLIENT_SERVICE ))
  243. {
  244. /* not registered, ignore prefix */
  245. Req->prefix = NULL;
  246. return true;
  247. }
  248. /* check if client in prefix is known */
  249. c = Client_Search( Req->prefix );
  250. if( ! c )
  251. {
  252. Log( LOG_ERR, "Invalid prefix \"%s\", client not known (connection %d, command %s)!?", Req->prefix, Idx, Req->command );
  253. if( ! Conn_WriteStr( Idx, "ERROR :Invalid prefix \"%s\", client not known!?", Req->prefix )) *Closed = true;
  254. return false;
  255. }
  256. /* check if the client named in the prefix is expected
  257. * to come from that direction */
  258. if( Client_NextHop( c ) != client )
  259. {
  260. Log( LOG_ERR, "Spoofed prefix \"%s\" from \"%s\" (connection %d, command %s)!", Req->prefix, Client_Mask( Conn_GetClient( Idx )), Idx, Req->command );
  261. Conn_Close( Idx, NULL, "Spoofed prefix", true);
  262. *Closed = true;
  263. return false;
  264. }
  265. return true;
  266. } /* Validate_Prefix */
  267. static bool
  268. Validate_Command( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed )
  269. {
  270. assert( Idx >= 0 );
  271. assert( Req != NULL );
  272. *Closed = false;
  273. return true;
  274. } /* Validate_Comman */
  275. static bool
  276. #ifdef STRICT_RFC
  277. Validate_Args(CONN_ID Idx, REQUEST *Req, bool *Closed)
  278. #else
  279. Validate_Args(UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed)
  280. #endif
  281. {
  282. #ifdef STRICT_RFC
  283. int i;
  284. #endif
  285. *Closed = false;
  286. #ifdef STRICT_RFC
  287. assert( Idx >= 0 );
  288. assert( Req != NULL );
  289. /* CR and LF are never allowed in command parameters.
  290. * But since we do accept lines terminated only with CR or LF in
  291. * "non-RFC-compliant mode" (besides the correct CR+LF combination),
  292. * this check can only trigger in "strict RFC" mode; therefore we
  293. * optimize it away otherwise ... */
  294. for (i = 0; i < Req->argc; i++) {
  295. if (strchr(Req->argv[i], '\r') || strchr(Req->argv[i], '\n')) {
  296. Log(LOG_ERR,
  297. "Invalid character(s) in parameter (connection %d, command %s)!?",
  298. Idx, Req->command);
  299. if (!Conn_WriteStr(Idx,
  300. "ERROR :Invalid character(s) in parameter!"))
  301. *Closed = true;
  302. return false;
  303. }
  304. }
  305. #endif
  306. return true;
  307. } /* Validate_Args */
  308. /* Command is a status code ("numeric") from another server */
  309. static bool
  310. Handle_Numeric(CLIENT *client, REQUEST *Req)
  311. {
  312. static const struct _NUMERIC Numerics[] = {
  313. { 5, IRC_Num_ISUPPORT },
  314. { 20, NULL },
  315. { 376, IRC_Num_ENDOFMOTD }
  316. };
  317. int i, num;
  318. char str[LINE_LEN];
  319. CLIENT *prefix, *target = NULL;
  320. /* Determine target */
  321. if (Req->argc > 0) {
  322. if (strcmp(Req->argv[0], "*") != 0)
  323. target = Client_Search(Req->argv[0]);
  324. else
  325. target = Client_ThisServer();
  326. }
  327. if (!target) {
  328. /* Status code without target!? */
  329. if (Req->argc > 0)
  330. Log(LOG_WARNING,
  331. "Unknown target for status code %s: \"%s\"",
  332. Req->command, Req->argv[0]);
  333. else
  334. Log(LOG_WARNING,
  335. "Unknown target for status code %s!",
  336. Req->command);
  337. return true;
  338. }
  339. if (target == Client_ThisServer()) {
  340. /* This server is the target of the numeric */
  341. num = atoi(Req->command);
  342. for (i = 0; i < (int) ARRAY_SIZE(Numerics); i++) {
  343. if (num == Numerics[i].numeric) {
  344. if (!Numerics[i].function)
  345. return CONNECTED;
  346. return Numerics[i].function(client, Req);
  347. }
  348. }
  349. LogDebug("Ignored status code %s from \"%s\".",
  350. Req->command, Client_ID(client));
  351. return true;
  352. }
  353. /* Determine source */
  354. if (! Req->prefix[0]) {
  355. /* Oops, no prefix!? */
  356. Log(LOG_WARNING, "Got status code %s from \"%s\" without prefix!?",
  357. Req->command, Client_ID(client));
  358. return true;
  359. }
  360. prefix = Client_Search(Req->prefix);
  361. if (! prefix) { /* Oops, unknown prefix!? */
  362. Log(LOG_WARNING, "Got status code %s from unknown source: \"%s\"", Req->command, Req->prefix);
  363. return true;
  364. }
  365. /* Forward status code */
  366. strlcpy(str, Req->command, sizeof(str));
  367. for (i = 0; i < Req->argc; i++) {
  368. if (i < Req->argc - 1)
  369. strlcat(str, " ", sizeof(str));
  370. else
  371. strlcat(str, " :", sizeof(str));
  372. strlcat(str, Req->argv[i], sizeof(str));
  373. }
  374. return IRC_WriteStrClientPrefix(target, prefix, "%s", str);
  375. }
  376. static bool
  377. Handle_Request( CONN_ID Idx, REQUEST *Req )
  378. {
  379. CLIENT *client;
  380. bool result = true;
  381. int client_type;
  382. COMMAND *cmd;
  383. assert( Idx >= 0 );
  384. assert( Req != NULL );
  385. assert( Req->command != NULL );
  386. client = Conn_GetClient( Idx );
  387. assert( client != NULL );
  388. /* Numeric? */
  389. client_type = Client_Type(client);
  390. if ((client_type == CLIENT_SERVER ||
  391. client_type == CLIENT_UNKNOWNSERVER)
  392. && strlen(Req->command) == 3 && atoi(Req->command) > 1)
  393. return Handle_Numeric(client, Req);
  394. cmd = My_Commands;
  395. while (cmd->name) {
  396. if (strcasecmp(Req->command, cmd->name) != 0) {
  397. cmd++;
  398. continue;
  399. }
  400. if (!(client_type & cmd->type))
  401. return IRC_WriteStrClient(client, ERR_NOTREGISTERED_MSG, Client_ID(client));
  402. /* Command is allowed for this client: call it and count produced bytes */
  403. Conn_ResetWCounter();
  404. result = (cmd->function)(client, Req);
  405. cmd->bytes += Conn_WCounter();
  406. /* Adjust counters */
  407. if (client_type != CLIENT_SERVER)
  408. cmd->lcount++;
  409. else
  410. cmd->rcount++;
  411. return result;
  412. }
  413. if (client_type != CLIENT_USER &&
  414. client_type != CLIENT_SERVER &&
  415. client_type != CLIENT_SERVICE )
  416. return true;
  417. /* Unknown command and registered connection: generate error: */
  418. LogDebug("Connection %d: Unknown command \"%s\", %d %s,%s prefix.",
  419. Client_Conn( client ), Req->command, Req->argc,
  420. Req->argc == 1 ? "parameter" : "parameters",
  421. Req->prefix ? "" : " no" );
  422. if (Client_Type(client) != CLIENT_SERVER) {
  423. result = IRC_WriteStrClient(client, ERR_UNKNOWNCOMMAND_MSG,
  424. Client_ID(client), Req->command);
  425. Conn_SetPenalty(Idx, 1);
  426. }
  427. return result;
  428. } /* Handle_Request */
  429. /* -eof- */