parse.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001,2002 by 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. static char UNUSED id[] = "$Id: parse.c,v 1.67 2006/04/23 10:37:27 fw Exp $";
  13. /**
  14. * @file
  15. * IRC command parser and validator.
  16. */
  17. #include "imp.h"
  18. #include <assert.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <strings.h>
  23. #include "ngircd.h"
  24. #include "defines.h"
  25. #include "conn-func.h"
  26. #include "client.h"
  27. #include "channel.h"
  28. #include "log.h"
  29. #include "messages.h"
  30. #include "tool.h"
  31. #include "exp.h"
  32. #include "parse.h"
  33. #include "imp.h"
  34. #include "irc.h"
  35. #include "irc-channel.h"
  36. #include "irc-info.h"
  37. #include "irc-login.h"
  38. #include "irc-mode.h"
  39. #include "irc-op.h"
  40. #include "irc-oper.h"
  41. #include "irc-server.h"
  42. #include "irc-write.h"
  43. #include "exp.h"
  44. COMMAND My_Commands[] =
  45. {
  46. { "ADMIN", IRC_ADMIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  47. { "AWAY", IRC_AWAY, CLIENT_USER, 0, 0, 0 },
  48. { "CONNECT", IRC_CONNECT, CLIENT_USER, 0, 0, 0 },
  49. { "DIE", IRC_DIE, CLIENT_USER, 0, 0, 0 },
  50. { "DISCONNECT", IRC_DISCONNECT, CLIENT_USER, 0, 0, 0 },
  51. { "ERROR", IRC_ERROR, 0xFFFF, 0, 0, 0 },
  52. { "HELP", IRC_HELP, CLIENT_USER, 0, 0, 0 },
  53. { "INVITE", IRC_INVITE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  54. { "ISON", IRC_ISON, CLIENT_USER, 0, 0, 0 },
  55. { "JOIN", IRC_JOIN, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  56. { "KICK", IRC_KICK, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  57. { "KILL", IRC_KILL, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  58. { "LINKS", IRC_LINKS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  59. { "LIST", IRC_LIST, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  60. { "LUSERS", IRC_LUSERS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  61. { "MODE", IRC_MODE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  62. { "MOTD", IRC_MOTD, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  63. { "NAMES", IRC_NAMES, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  64. { "NICK", IRC_NICK, 0xFFFF, 0, 0, 0 },
  65. { "NJOIN", IRC_NJOIN, CLIENT_SERVER, 0, 0, 0 },
  66. { "NOTICE", IRC_NOTICE, 0xFFFF, 0, 0, 0 },
  67. { "OPER", IRC_OPER, CLIENT_USER, 0, 0, 0 },
  68. { "PART", IRC_PART, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  69. { "PASS", IRC_PASS, 0xFFFF, 0, 0, 0 },
  70. { "PING", IRC_PING, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  71. { "PONG", IRC_PONG, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  72. { "PRIVMSG", IRC_PRIVMSG, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  73. { "QUIT", IRC_QUIT, 0xFFFF, 0, 0, 0 },
  74. { "REHASH", IRC_REHASH, CLIENT_USER, 0, 0, 0 },
  75. { "RESTART", IRC_RESTART, CLIENT_USER, 0, 0, 0 },
  76. { "SERVER", IRC_SERVER, 0xFFFF, 0, 0, 0 },
  77. { "SQUIT", IRC_SQUIT, CLIENT_SERVER, 0, 0, 0 },
  78. { "STATS", IRC_STATS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  79. { "TIME", IRC_TIME, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  80. { "TOPIC", IRC_TOPIC, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  81. { "TRACE", IRC_TRACE, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  82. { "USER", IRC_USER, 0xFFFF, 0, 0, 0 },
  83. { "USERHOST", IRC_USERHOST, CLIENT_USER, 0, 0, 0 },
  84. { "VERSION", IRC_VERSION, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  85. { "WHO", IRC_WHO, CLIENT_USER, 0, 0, 0 },
  86. { "WHOIS", IRC_WHOIS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  87. { "WHOWAS", IRC_WHOWAS, CLIENT_USER|CLIENT_SERVER, 0, 0, 0 },
  88. #ifdef IRCPLUS
  89. { "CHANINFO", IRC_CHANINFO, CLIENT_SERVER, 0, 0, 0 },
  90. #endif
  91. { NULL, NULL, 0x0, 0, 0, 0 } /* Ende-Marke */
  92. };
  93. static void Init_Request PARAMS(( REQUEST *Req ));
  94. static bool Validate_Prefix PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
  95. static bool Validate_Command PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
  96. static bool Validate_Args PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
  97. static bool Handle_Request PARAMS(( CONN_ID Idx, REQUEST *Req ));
  98. /**
  99. * Return the pointer to the global "IRC command structure".
  100. * This structure, an array of type "COMMAND" describes all the IRC commands
  101. * implemented by ngIRCd and how to handle them.
  102. * @return Pointer to the global command structure.
  103. */
  104. GLOBAL COMMAND *
  105. Parse_GetCommandStruct( void )
  106. {
  107. return My_Commands;
  108. } /* Parse_GetCommandStruct */
  109. /**
  110. * Parse a command ("request") received from a client.
  111. *
  112. * This function is called after the connection layer received a valid CR+LF
  113. * terminated line of text: we asume that this is a valid IRC command and
  114. * try to do something useful with it :-)
  115. *
  116. * All errors are reported to the client from which the command has been
  117. * received, and if the error is fatal this connection is closed down.
  118. *
  119. * This function is able to parse the syntax as described in RFC 2812,
  120. * section 2.3.
  121. *
  122. * @param Idx Index of the connection from which the command has been received.
  123. * @param Request NULL terminated line of text (the "command").
  124. * @return true on success (valid command or "regular" error), false if a
  125. * fatal error occured and the connection has been shut down.
  126. */
  127. GLOBAL bool
  128. Parse_Request( CONN_ID Idx, char *Request )
  129. {
  130. REQUEST req;
  131. char *start, *ptr;
  132. bool closed;
  133. assert( Idx >= 0 );
  134. assert( Request != NULL );
  135. #ifdef SNIFFER
  136. if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request );
  137. #endif
  138. Init_Request( &req );
  139. /* Fuehrendes und folgendes "Geraffel" verwerfen */
  140. ngt_TrimStr( Request );
  141. /* gibt es ein Prefix? */
  142. if( Request[0] == ':' )
  143. {
  144. /* Prefix vorhanden */
  145. req.prefix = Request + 1;
  146. ptr = strchr( Request, ' ' );
  147. if( ! ptr )
  148. {
  149. Log( LOG_DEBUG, "Connection %d: Parse error: prefix without command!?", Idx );
  150. return Conn_WriteStr( Idx, "ERROR :Prefix without command!?" );
  151. }
  152. *ptr = '\0';
  153. #ifndef STRICT_RFC
  154. /* multiple Leerzeichen als Trenner zwischen
  155. * Prefix und Befehl ignorieren */
  156. while( *(ptr + 1) == ' ' ) ptr++;
  157. #endif
  158. start = ptr + 1;
  159. }
  160. else start = Request;
  161. /* Befehl */
  162. ptr = strchr( start, ' ' );
  163. if( ptr )
  164. {
  165. *ptr = '\0';
  166. #ifndef STRICT_RFC
  167. /* multiple Leerzeichen als Trenner vor
  168. * Parametern ignorieren */
  169. while( *(ptr + 1) == ' ' ) ptr++;
  170. #endif
  171. }
  172. req.command = start;
  173. /* Argumente, Parameter */
  174. if( ptr )
  175. {
  176. /* Prinzipiell gibt es welche :-) */
  177. start = ptr + 1;
  178. while( start )
  179. {
  180. /* Parameter-String "zerlegen" */
  181. if( start[0] == ':' )
  182. {
  183. req.argv[req.argc] = start + 1;
  184. ptr = NULL;
  185. }
  186. else
  187. {
  188. req.argv[req.argc] = start;
  189. ptr = strchr( start, ' ' );
  190. if( ptr )
  191. {
  192. *ptr = '\0';
  193. #ifndef STRICT_RFC
  194. /* multiple Leerzeichen als
  195. * Parametertrenner ignorieren */
  196. while( *(ptr + 1) == ' ' ) ptr++;
  197. #endif
  198. }
  199. }
  200. req.argc++;
  201. if( start[0] == ':' ) break;
  202. if( req.argc > 14 ) break;
  203. if( ptr ) start = ptr + 1;
  204. else start = NULL;
  205. }
  206. }
  207. /* Daten validieren */
  208. if( ! Validate_Prefix( Idx, &req, &closed )) return ! closed;
  209. if( ! Validate_Command( Idx, &req, &closed )) return ! closed;
  210. if( ! Validate_Args( Idx, &req, &closed )) return ! closed;
  211. return Handle_Request( Idx, &req );
  212. } /* Parse_Request */
  213. /**
  214. * Initialize request structure.
  215. * @param Req Request structure to be initialized.
  216. */
  217. static void
  218. Init_Request( REQUEST *Req )
  219. {
  220. /* Neue Request-Struktur initialisieren */
  221. int i;
  222. assert( Req != NULL );
  223. Req->prefix = NULL;
  224. Req->command = NULL;
  225. for( i = 0; i < 15; Req->argv[i++] = NULL );
  226. Req->argc = 0;
  227. } /* Init_Request */
  228. static bool
  229. Validate_Prefix( CONN_ID Idx, REQUEST *Req, bool *Closed )
  230. {
  231. CLIENT *client, *c;
  232. assert( Idx >= 0 );
  233. assert( Req != NULL );
  234. *Closed = false;
  235. /* ist ueberhaupt ein Prefix vorhanden? */
  236. if( ! Req->prefix ) return true;
  237. /* Client-Struktur der Connection ermitteln */
  238. client = Conn_GetClient( Idx );
  239. assert( client != NULL );
  240. /* nur validieren, wenn bereits registrierte Verbindung */
  241. if(( Client_Type( client ) != CLIENT_USER ) && ( Client_Type( client ) != CLIENT_SERVER ) && ( Client_Type( client ) != CLIENT_SERVICE ))
  242. {
  243. /* noch nicht registrierte Verbindung.
  244. * Das Prefix wird ignoriert. */
  245. Req->prefix = NULL;
  246. return true;
  247. }
  248. /* pruefen, ob der im Prefix angegebene Client bekannt ist */
  249. c = Client_Search( Req->prefix );
  250. if( ! c )
  251. {
  252. /* im Prefix angegebener Client ist nicht bekannt */
  253. Log( LOG_ERR, "Invalid prefix \"%s\", client not known (connection %d, command %s)!?", Req->prefix, Idx, Req->command );
  254. if( ! Conn_WriteStr( Idx, "ERROR :Invalid prefix \"%s\", client not known!?", Req->prefix )) *Closed = true;
  255. return false;
  256. }
  257. /* pruefen, ob der Client mit dem angegebenen Prefix in Richtung
  258. * des Senders liegt, d.h. sicherstellen, dass das Prefix nicht
  259. * gefaelscht ist */
  260. if( Client_NextHop( c ) != client )
  261. {
  262. /* das angegebene Prefix ist aus dieser Richtung, also
  263. * aus der gegebenen Connection, ungueltig! */
  264. Log( LOG_ERR, "Spoofed prefix \"%s\" from \"%s\" (connection %d, command %s)!", Req->prefix, Client_Mask( Conn_GetClient( Idx )), Idx, Req->command );
  265. Conn_Close( Idx, NULL, "Spoofed prefix", true);
  266. *Closed = true;
  267. return false;
  268. }
  269. return true;
  270. } /* Validate_Prefix */
  271. static bool
  272. Validate_Command( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed )
  273. {
  274. assert( Idx >= 0 );
  275. assert( Req != NULL );
  276. *Closed = false;
  277. return true;
  278. } /* Validate_Comman */
  279. static bool
  280. Validate_Args( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed )
  281. {
  282. assert( Idx >= 0 );
  283. assert( Req != NULL );
  284. *Closed = false;
  285. return true;
  286. } /* Validate_Args */
  287. static bool
  288. Handle_Request( CONN_ID Idx, REQUEST *Req )
  289. {
  290. /* Client-Request verarbeiten. Bei einem schwerwiegenden Fehler
  291. * wird die Verbindung geschlossen und false geliefert. */
  292. CLIENT *client, *target, *prefix;
  293. char str[LINE_LEN];
  294. bool result;
  295. COMMAND *cmd;
  296. int i;
  297. assert( Idx >= 0 );
  298. assert( Req != NULL );
  299. assert( Req->command != NULL );
  300. client = Conn_GetClient( Idx );
  301. assert( client != NULL );
  302. /* Statuscode? */
  303. if(( Client_Type( client ) == CLIENT_SERVER ) && ( strlen( Req->command ) == 3 ) && ( atoi( Req->command ) > 100 ))
  304. {
  305. /* Command is a status code from an other server */
  306. /* Determine target */
  307. if( Req->argc > 0 ) target = Client_Search( Req->argv[0] );
  308. else target = NULL;
  309. if( ! target )
  310. {
  311. /* Status code without target!? */
  312. if( Req->argc > 0 ) Log( LOG_WARNING, "Unknown target for status code %s: \"%s\"", Req->command, Req->argv[0] );
  313. else Log( LOG_WARNING, "Unknown target for status code %s!", Req->command );
  314. return true;
  315. }
  316. if( target == Client_ThisServer( ))
  317. {
  318. /* This server is the target, ignore it */
  319. Log( LOG_DEBUG, "Ignored status code %s from \"%s\".", Req->command, Client_ID( client ));
  320. return true;
  321. }
  322. /* Determine source */
  323. if( ! Req->prefix[0] )
  324. {
  325. /* Oops, no prefix!? */
  326. Log( LOG_WARNING, "Got status code %s from \"%s\" without prefix!?", Req->command, Client_ID( client ));
  327. return true;
  328. }
  329. else prefix = Client_Search( Req->prefix );
  330. if( ! prefix )
  331. {
  332. /* Oops, unknown prefix!? */
  333. Log( LOG_WARNING, "Got status code %s from unknown source: \"%s\"", Req->command, Req->prefix );
  334. return true;
  335. }
  336. /* Forward status code */
  337. strlcpy( str, Req->command, sizeof( str ));
  338. for( i = 0; i < Req->argc; i++ )
  339. {
  340. if( i < Req->argc - 1 ) strlcat( str, " ", sizeof( str ));
  341. else strlcat( str, " :", sizeof( str ));
  342. strlcat( str, Req->argv[i], sizeof( str ));
  343. }
  344. return IRC_WriteStrClientPrefix( target, prefix, "%s", str );
  345. }
  346. cmd = My_Commands;
  347. while( cmd->name )
  348. {
  349. /* Befehl suchen */
  350. if( strcasecmp( Req->command, cmd->name ) != 0 )
  351. {
  352. cmd++; continue;
  353. }
  354. if( Client_Type( client ) & cmd->type )
  355. {
  356. /* Command is allowed for this client: call it and count produced bytes */
  357. Conn_ResetWCounter( );
  358. result = (cmd->function)( client, Req );
  359. cmd->bytes += Conn_WCounter( );
  360. /* Adjust counters */
  361. if( Client_Type( client ) != CLIENT_SERVER ) cmd->lcount++;
  362. else cmd->rcount++;
  363. return result;
  364. }
  365. else
  366. {
  367. /* Befehl ist fuer diesen Client-Typ nicht erlaubt! */
  368. return IRC_WriteStrClient( client, ERR_NOTREGISTERED_MSG, Client_ID( client ));
  369. }
  370. }
  371. if( Client_Type( client ) != CLIENT_USER &&
  372. Client_Type( client ) != CLIENT_SERVER &&
  373. Client_Type( client ) != CLIENT_SERVICE )
  374. return true;
  375. /* Unknown command and registered connection: generate error: */
  376. Log( LOG_DEBUG, "Connection %d: Unknown command \"%s\", %d %s,%s prefix.",
  377. Client_Conn( client ), Req->command, Req->argc,
  378. Req->argc == 1 ? "parameter" : "parameters",
  379. Req->prefix ? "" : " no" );
  380. if (Client_Type(client) != CLIENT_SERVER) {
  381. result = IRC_WriteStrClient(client, ERR_UNKNOWNCOMMAND_MSG,
  382. Client_ID(client), Req->command);
  383. Conn_SetPenalty(Idx, 1);
  384. return result;
  385. }
  386. return true;
  387. } /* Handle_Request */
  388. /* -eof- */