parse.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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, 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. { "SQUIT", IRC_SQUIT, CLIENT_SERVER, 0, 0, 0 },
  83. { "STATS", IRC_STATS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  84. { "SUMMON", IRC_SUMMON, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  85. { "TIME", IRC_TIME, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  86. { "TOPIC", IRC_TOPIC, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  87. { "TRACE", IRC_TRACE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  88. { "USER", IRC_USER, 0xFFFF, 0, 0, 0 },
  89. { "USERHOST", IRC_USERHOST, CLIENT_USER, 0, 0, 0 },
  90. { "USERS", IRC_USERS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  91. { "VERSION", IRC_VERSION, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  92. { "WALLOPS", IRC_WALLOPS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  93. { "WHO", IRC_WHO, CLIENT_USER, 0, 0, 0 },
  94. { "WHOIS", IRC_WHOIS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  95. { "WHOWAS", IRC_WHOWAS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  96. #ifdef IRCPLUS
  97. { "CHANINFO", IRC_CHANINFO, CLIENT_SERVER, 0, 0, 0 },
  98. #endif
  99. { NULL, NULL, 0x0, 0, 0, 0 } /* Ende-Marke */
  100. };
  101. static void Init_Request PARAMS(( REQUEST *Req ));
  102. static bool Validate_Prefix PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
  103. static bool Validate_Command PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
  104. static bool Validate_Args PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
  105. static bool Handle_Request PARAMS(( CONN_ID Idx, REQUEST *Req ));
  106. #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
  107. /**
  108. * Return the pointer to the global "IRC command structure".
  109. * This structure, an array of type "COMMAND" describes all the IRC commands
  110. * implemented by ngIRCd and how to handle them.
  111. * @return Pointer to the global command structure.
  112. */
  113. GLOBAL COMMAND *
  114. Parse_GetCommandStruct( void )
  115. {
  116. return My_Commands;
  117. } /* Parse_GetCommandStruct */
  118. /**
  119. * Parse a command ("request") received from a client.
  120. *
  121. * This function is called after the connection layer received a valid CR+LF
  122. * terminated line of text: we asume that this is a valid IRC command and
  123. * try to do something useful with it :-)
  124. *
  125. * All errors are reported to the client from which the command has been
  126. * received, and if the error is fatal this connection is closed down.
  127. *
  128. * This function is able to parse the syntax as described in RFC 2812,
  129. * section 2.3.
  130. *
  131. * @param Idx Index of the connection from which the command has been received.
  132. * @param Request NULL terminated line of text (the "command").
  133. * @return true on success (valid command or "regular" error), false if a
  134. * fatal error occured and the connection has been shut down.
  135. */
  136. GLOBAL bool
  137. Parse_Request( CONN_ID Idx, char *Request )
  138. {
  139. REQUEST req;
  140. char *start, *ptr;
  141. bool closed;
  142. assert( Idx >= 0 );
  143. assert( Request != NULL );
  144. #ifdef SNIFFER
  145. if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request );
  146. #endif
  147. Init_Request( &req );
  148. /* Fuehrendes und folgendes "Geraffel" verwerfen */
  149. ngt_TrimStr( Request );
  150. /* gibt es ein Prefix? */
  151. if( Request[0] == ':' )
  152. {
  153. /* Prefix vorhanden */
  154. req.prefix = Request + 1;
  155. ptr = strchr( Request, ' ' );
  156. if( ! ptr )
  157. {
  158. Log( LOG_DEBUG, "Connection %d: Parse error: prefix without command!?", Idx );
  159. return Conn_WriteStr( Idx, "ERROR :Prefix without command!?" );
  160. }
  161. *ptr = '\0';
  162. #ifndef STRICT_RFC
  163. /* multiple Leerzeichen als Trenner zwischen
  164. * Prefix und Befehl ignorieren */
  165. while( *(ptr + 1) == ' ' ) ptr++;
  166. #endif
  167. start = ptr + 1;
  168. }
  169. else start = Request;
  170. /* Befehl */
  171. ptr = strchr( start, ' ' );
  172. if( ptr )
  173. {
  174. *ptr = '\0';
  175. #ifndef STRICT_RFC
  176. /* multiple Leerzeichen als Trenner vor
  177. * Parametern ignorieren */
  178. while( *(ptr + 1) == ' ' ) ptr++;
  179. #endif
  180. }
  181. req.command = start;
  182. /* Argumente, Parameter */
  183. if( ptr )
  184. {
  185. /* Prinzipiell gibt es welche :-) */
  186. start = ptr + 1;
  187. while( start )
  188. {
  189. /* Parameter-String "zerlegen" */
  190. if( start[0] == ':' )
  191. {
  192. req.argv[req.argc] = start + 1;
  193. ptr = NULL;
  194. }
  195. else
  196. {
  197. req.argv[req.argc] = start;
  198. ptr = strchr( start, ' ' );
  199. if( ptr )
  200. {
  201. *ptr = '\0';
  202. #ifndef STRICT_RFC
  203. /* multiple Leerzeichen als
  204. * Parametertrenner ignorieren */
  205. while( *(ptr + 1) == ' ' ) ptr++;
  206. #endif
  207. }
  208. }
  209. req.argc++;
  210. if( start[0] == ':' ) break;
  211. if( req.argc > 14 ) break;
  212. if( ptr ) start = ptr + 1;
  213. else start = NULL;
  214. }
  215. }
  216. /* Daten validieren */
  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. /* ist ueberhaupt ein Prefix vorhanden? */
  245. if( ! Req->prefix ) return true;
  246. /* Client-Struktur der Connection ermitteln */
  247. client = Conn_GetClient( Idx );
  248. assert( client != NULL );
  249. /* nur validieren, wenn bereits registrierte Verbindung */
  250. if(( Client_Type( client ) != CLIENT_USER ) && ( Client_Type( client ) != CLIENT_SERVER ) && ( Client_Type( client ) != CLIENT_SERVICE ))
  251. {
  252. /* noch nicht registrierte Verbindung.
  253. * Das Prefix wird ignoriert. */
  254. Req->prefix = NULL;
  255. return true;
  256. }
  257. /* pruefen, ob der im Prefix angegebene Client bekannt ist */
  258. c = Client_Search( Req->prefix );
  259. if( ! c )
  260. {
  261. /* im Prefix angegebener Client ist nicht bekannt */
  262. Log( LOG_ERR, "Invalid prefix \"%s\", client not known (connection %d, command %s)!?", Req->prefix, Idx, Req->command );
  263. if( ! Conn_WriteStr( Idx, "ERROR :Invalid prefix \"%s\", client not known!?", Req->prefix )) *Closed = true;
  264. return false;
  265. }
  266. /* pruefen, ob der Client mit dem angegebenen Prefix in Richtung
  267. * des Senders liegt, d.h. sicherstellen, dass das Prefix nicht
  268. * gefaelscht ist */
  269. if( Client_NextHop( c ) != client )
  270. {
  271. /* das angegebene Prefix ist aus dieser Richtung, also
  272. * aus der gegebenen Connection, ungueltig! */
  273. Log( LOG_ERR, "Spoofed prefix \"%s\" from \"%s\" (connection %d, command %s)!", Req->prefix, Client_Mask( Conn_GetClient( Idx )), Idx, Req->command );
  274. Conn_Close( Idx, NULL, "Spoofed prefix", true);
  275. *Closed = true;
  276. return false;
  277. }
  278. return true;
  279. } /* Validate_Prefix */
  280. static bool
  281. Validate_Command( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed )
  282. {
  283. assert( Idx >= 0 );
  284. assert( Req != NULL );
  285. *Closed = false;
  286. return true;
  287. } /* Validate_Comman */
  288. static bool
  289. Validate_Args(CONN_ID Idx, REQUEST *Req, bool *Closed)
  290. {
  291. #ifdef STRICT_RFC
  292. int i;
  293. #endif
  294. assert( Idx >= 0 );
  295. assert( Req != NULL );
  296. *Closed = false;
  297. #ifdef STRICT_RFC
  298. /* CR and LF are never allowed in command parameters.
  299. * But since we do accept lines terminated only with CR or LF in
  300. * "non-RFC-compliant mode" (besides the correct CR+LF combination),
  301. * this check can only trigger in "strict RFC" mode; therefore we
  302. * optimize it away otherwise ... */
  303. for (i = 0; i < Req->argc; i++) {
  304. if (strchr(Req->argv[i], '\r') || strchr(Req->argv[i], '\n')) {
  305. Log(LOG_ERR,
  306. "Invalid character(s) in parameter (connection %d, command %s)!?",
  307. Idx, Req->command);
  308. if (!Conn_WriteStr(Idx,
  309. "ERROR :Invalid character(s) in parameter!"))
  310. *Closed = true;
  311. return false;
  312. }
  313. }
  314. #endif
  315. return true;
  316. } /* Validate_Args */
  317. /* Command is a status code ("numeric") from another server */
  318. static bool
  319. Handle_Numeric(CLIENT *client, REQUEST *Req)
  320. {
  321. static const struct _NUMERIC Numerics[] = {
  322. { 005, IRC_Num_ISUPPORT },
  323. { 376, IRC_Num_ENDOFMOTD }
  324. };
  325. int i, num;
  326. char str[LINE_LEN];
  327. CLIENT *prefix, *target = NULL;
  328. /* Determine target */
  329. if (Req->argc > 0)
  330. target = Client_Search(Req->argv[0]);
  331. if (!target) {
  332. /* Status code without target!? */
  333. if (Req->argc > 0)
  334. Log(LOG_WARNING,
  335. "Unknown target for status code %s: \"%s\"",
  336. Req->command, Req->argv[0]);
  337. else
  338. Log(LOG_WARNING,
  339. "Unknown target for status code %s!",
  340. Req->command);
  341. return true;
  342. }
  343. if (target == Client_ThisServer()) {
  344. /* This server is the target of the numeric */
  345. num = atoi(Req->command);
  346. for (i = 0; i < (int) ARRAY_SIZE(Numerics); i++) {
  347. if (num == Numerics[i].numeric)
  348. return Numerics[i].function(client, Req);
  349. }
  350. LogDebug("Ignored status code %s from \"%s\".",
  351. Req->command, Client_ID(client));
  352. return true;
  353. }
  354. /* Determine source */
  355. if (! Req->prefix[0]) {
  356. /* Oops, no prefix!? */
  357. Log(LOG_WARNING, "Got status code %s from \"%s\" without prefix!?",
  358. Req->command, Client_ID(client));
  359. return true;
  360. }
  361. prefix = Client_Search(Req->prefix);
  362. if (! prefix) { /* Oops, unknown prefix!? */
  363. Log(LOG_WARNING, "Got status code %s from unknown source: \"%s\"", Req->command, Req->prefix);
  364. return true;
  365. }
  366. /* Forward status code */
  367. strlcpy(str, Req->command, sizeof(str));
  368. for (i = 0; i < Req->argc; i++) {
  369. if (i < Req->argc - 1)
  370. strlcat(str, " ", sizeof(str));
  371. else
  372. strlcat(str, " :", sizeof(str));
  373. strlcat(str, Req->argv[i], sizeof(str));
  374. }
  375. return IRC_WriteStrClientPrefix(target, prefix, "%s", str);
  376. }
  377. static bool
  378. Handle_Request( CONN_ID Idx, REQUEST *Req )
  379. {
  380. /* Client-Request verarbeiten. Bei einem schwerwiegenden Fehler
  381. * wird die Verbindung geschlossen und false geliefert. */
  382. CLIENT *client;
  383. bool result = true;
  384. int client_type;
  385. COMMAND *cmd;
  386. assert( Idx >= 0 );
  387. assert( Req != NULL );
  388. assert( Req->command != NULL );
  389. client = Conn_GetClient( Idx );
  390. assert( client != NULL );
  391. /* Numeric? */
  392. client_type = Client_Type(client);
  393. if ((client_type == CLIENT_SERVER ||
  394. client_type == CLIENT_UNKNOWNSERVER)
  395. && strlen(Req->command) == 3 && atoi(Req->command) > 1)
  396. return Handle_Numeric(client, Req);
  397. cmd = My_Commands;
  398. while (cmd->name) {
  399. /* Befehl suchen */
  400. if (strcasecmp(Req->command, cmd->name) != 0) {
  401. cmd++;
  402. continue;
  403. }
  404. if (!(client_type & cmd->type))
  405. return IRC_WriteStrClient(client, ERR_NOTREGISTERED_MSG, Client_ID(client));
  406. /* Command is allowed for this client: call it and count produced bytes */
  407. Conn_ResetWCounter();
  408. result = (cmd->function)(client, Req);
  409. cmd->bytes += Conn_WCounter();
  410. /* Adjust counters */
  411. if (client_type != CLIENT_SERVER)
  412. cmd->lcount++;
  413. else
  414. cmd->rcount++;
  415. return result;
  416. }
  417. if (client_type != CLIENT_USER &&
  418. client_type != CLIENT_SERVER &&
  419. client_type != CLIENT_SERVICE )
  420. return true;
  421. /* Unknown command and registered connection: generate error: */
  422. LogDebug("Connection %d: Unknown command \"%s\", %d %s,%s prefix.",
  423. Client_Conn( client ), Req->command, Req->argc,
  424. Req->argc == 1 ? "parameter" : "parameters",
  425. Req->prefix ? "" : " no" );
  426. if (Client_Type(client) != CLIENT_SERVER) {
  427. result = IRC_WriteStrClient(client, ERR_UNKNOWNCOMMAND_MSG,
  428. Client_ID(client), Req->command);
  429. Conn_SetPenalty(Idx, 1);
  430. }
  431. return result;
  432. } /* Handle_Request */
  433. /* -eof- */