irc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2012 Alexander Barton (alex@barton.de) and Contributors.
  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 commands
  15. */
  16. #include "imp.h"
  17. #include <assert.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include "ngircd.h"
  21. #include "conn-func.h"
  22. #include "conf.h"
  23. #include "channel.h"
  24. #include "defines.h"
  25. #include "irc-write.h"
  26. #include "log.h"
  27. #include "match.h"
  28. #include "messages.h"
  29. #include "parse.h"
  30. #include "tool.h"
  31. #include "exp.h"
  32. #include "irc.h"
  33. static char *Option_String PARAMS((CONN_ID Idx));
  34. static bool Send_Message PARAMS((CLIENT *Client, REQUEST *Req, int ForceType,
  35. bool SendErrors));
  36. static bool Send_Message_Mask PARAMS((CLIENT *from, char *command,
  37. char *targetMask, char *message,
  38. bool SendErrors));
  39. /**
  40. * Check if a list limit is reached and inform client accordingly.
  41. *
  42. * @param From The client.
  43. * @param Count Reply item count.
  44. * @param Limit Reply limit.
  45. * @param Name Name of the list.
  46. * @return true if list limit has been reached; false otherwise.
  47. */
  48. GLOBAL bool
  49. IRC_CheckListTooBig(CLIENT *From, const int Count, const int Limit,
  50. const char *Name)
  51. {
  52. assert(From != NULL);
  53. assert(Count >= 0);
  54. assert(Limit > 0);
  55. assert(Name != NULL);
  56. if (Count < Limit)
  57. return false;
  58. (void)IRC_WriteStrClient(From,
  59. "NOTICE %s :%s list limit (%d) reached!",
  60. Client_ID(From), Name, Limit);
  61. IRC_SetPenalty(From, 2);
  62. return true;
  63. }
  64. GLOBAL bool
  65. IRC_ERROR( CLIENT *Client, REQUEST *Req )
  66. {
  67. assert( Client != NULL );
  68. assert( Req != NULL );
  69. if (Req->argc < 1)
  70. Log(LOG_NOTICE, "Got ERROR from \"%s\"!",
  71. Client_Mask(Client));
  72. else
  73. Log(LOG_NOTICE, "Got ERROR from \"%s\": \"%s\"!",
  74. Client_Mask(Client), Req->argv[0]);
  75. return CONNECTED;
  76. } /* IRC_ERROR */
  77. /**
  78. * Handler for the IRC "KILL" command.
  79. *
  80. * This function implements the IRC command "KILL" wich is used to selectively
  81. * disconnect clients. It can be used by IRC operators and servers, for example
  82. * to "solve" nick collisions after netsplits. See RFC 2812 section 3.7.1.
  83. *
  84. * Please note that this function is also called internally, without a real
  85. * KILL command being received over the network! Client is Client_ThisServer()
  86. * in this case, and the prefix in Req is NULL.
  87. *
  88. * @param Client The client from which this command has been received
  89. * or Client_ThisServer() when generated interanlly.
  90. * @param Req Request structure with prefix and all parameters.
  91. * @returns CONNECTED or DISCONNECTED.
  92. */
  93. GLOBAL bool
  94. IRC_KILL( CLIENT *Client, REQUEST *Req )
  95. {
  96. CLIENT *prefix, *c;
  97. char reason[COMMAND_LEN], *msg;
  98. CONN_ID my_conn, conn;
  99. assert (Client != NULL);
  100. assert (Req != NULL);
  101. if (Client_Type(Client) != CLIENT_SERVER && !Client_OperByMe(Client))
  102. return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG,
  103. Client_ID(Client));
  104. if (Req->argc != 2)
  105. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  106. Client_ID(Client), Req->command);
  107. /* Get prefix (origin); use the client if no prefix is given. */
  108. if (Req->prefix)
  109. prefix = Client_Search(Req->prefix);
  110. else
  111. prefix = Client;
  112. /* Log a warning message and use this server as origin when the
  113. * prefix (origin) is invalid. */
  114. if (!prefix) {
  115. Log(LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
  116. Req->prefix );
  117. prefix = Client_ThisServer();
  118. }
  119. if (Client != Client_ThisServer())
  120. Log(LOG_NOTICE|LOG_snotice,
  121. "Got KILL command from \"%s\" for \"%s\": %s",
  122. Client_Mask(prefix), Req->argv[0], Req->argv[1]);
  123. /* Build reason string: Prefix the "reason" if the originator is a
  124. * regular user, so users can't spoof KILLs of servers. */
  125. if (Client_Type(Client) == CLIENT_USER)
  126. snprintf(reason, sizeof(reason), "KILLed by %s: %s",
  127. Client_ID(Client), Req->argv[1]);
  128. else
  129. strlcpy(reason, Req->argv[1], sizeof(reason));
  130. /* Inform other servers */
  131. IRC_WriteStrServersPrefix(Client, prefix, "KILL %s :%s",
  132. Req->argv[0], reason);
  133. /* Save ID of this connection */
  134. my_conn = Client_Conn( Client );
  135. /* Do we host such a client? */
  136. c = Client_Search( Req->argv[0] );
  137. if( c )
  138. {
  139. if(( Client_Type( c ) != CLIENT_USER ) &&
  140. ( Client_Type( c ) != CLIENT_GOTNICK ))
  141. {
  142. /* Target of this KILL is not a regular user, this is
  143. * invalid! So we ignore this case if we received a
  144. * regular KILL from the network and try to kill the
  145. * client/connection anyway (but log an error!) if the
  146. * origin is the local server. */
  147. if( Client != Client_ThisServer( ))
  148. {
  149. /* Invalid KILL received from remote */
  150. if( Client_Type( c ) == CLIENT_SERVER )
  151. msg = ERR_CANTKILLSERVER_MSG;
  152. else
  153. msg = ERR_NOPRIVILEGES_MSG;
  154. return IRC_WriteStrClient( Client, msg,
  155. Client_ID( Client ));
  156. }
  157. Log( LOG_ERR, "Got KILL for invalid client type: %d, \"%s\"!",
  158. Client_Type( c ), Req->argv[0] );
  159. }
  160. /* Kill the client NOW:
  161. * - Close the local connection (if there is one),
  162. * - Destroy the CLIENT structure for remote clients.
  163. * Note: Conn_Close() removes the CLIENT structure as well. */
  164. conn = Client_Conn( c );
  165. if(conn > NONE)
  166. Conn_Close(conn, NULL, reason, true);
  167. else
  168. Client_Destroy(c, NULL, reason, false);
  169. }
  170. else
  171. Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
  172. /* Are we still connected or were we killed, too? */
  173. if(( my_conn > NONE ) && ( Conn_GetClient( my_conn )))
  174. return CONNECTED;
  175. else
  176. return DISCONNECTED;
  177. } /* IRC_KILL */
  178. /**
  179. * Handler for the IRC command NOTICE.
  180. */
  181. GLOBAL bool
  182. IRC_NOTICE(CLIENT *Client, REQUEST *Req)
  183. {
  184. return Send_Message(Client, Req, CLIENT_USER, false);
  185. } /* IRC_NOTICE */
  186. /**
  187. * Handler for the IRC command PRIVMSG.
  188. */
  189. GLOBAL bool
  190. IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
  191. {
  192. return Send_Message(Client, Req, CLIENT_USER, true);
  193. } /* IRC_PRIVMSG */
  194. /**
  195. * Handler for the IRC command SQUERY.
  196. */
  197. GLOBAL bool
  198. IRC_SQUERY(CLIENT *Client, REQUEST *Req)
  199. {
  200. return Send_Message(Client, Req, CLIENT_SERVICE, true);
  201. } /* IRC_SQUERY */
  202. GLOBAL bool
  203. IRC_TRACE( CLIENT *Client, REQUEST *Req )
  204. {
  205. CLIENT *from, *target, *c;
  206. CONN_ID idx, idx2;
  207. char user[CLIENT_USER_LEN];
  208. assert( Client != NULL );
  209. assert( Req != NULL );
  210. /* Bad number of arguments? */
  211. if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
  212. /* Search sender */
  213. if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
  214. else from = Client;
  215. if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  216. /* Search target */
  217. if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
  218. else target = Client_ThisServer( );
  219. /* Forward command to other server? */
  220. if( target != Client_ThisServer( ))
  221. {
  222. if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
  223. /* Send RPL_TRACELINK back to initiator */
  224. idx = Client_Conn( Client ); assert( idx > NONE );
  225. idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
  226. if( ! IRC_WriteStrClient( from, RPL_TRACELINK_MSG, Client_ID( from ), PACKAGE_NAME, PACKAGE_VERSION, Client_ID( target ), Client_ID( Client_NextHop( target )), Option_String( idx2 ), time( NULL ) - Conn_StartTime( idx2 ), Conn_SendQ( idx ), Conn_SendQ( idx2 ))) return DISCONNECTED;
  227. /* Forward command */
  228. IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
  229. return CONNECTED;
  230. }
  231. /* Infos about all connected servers */
  232. c = Client_First( );
  233. while( c )
  234. {
  235. if( Client_Conn( c ) > NONE )
  236. {
  237. /* Local client */
  238. if( Client_Type( c ) == CLIENT_SERVER )
  239. {
  240. /* Server link */
  241. strlcpy( user, Client_User( c ), sizeof( user ));
  242. if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
  243. if( ! IRC_WriteStrClient( from, RPL_TRACESERVER_MSG, Client_ID( from ), Client_ID( c ), user, Client_Hostname( c ), Client_Mask( Client_ThisServer( )), Option_String( Client_Conn( c )))) return DISCONNECTED;
  244. }
  245. if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
  246. {
  247. /* IRC Operator */
  248. if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
  249. }
  250. }
  251. c = Client_Next( c );
  252. }
  253. IRC_SetPenalty( Client, 3 );
  254. return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
  255. } /* IRC_TRACE */
  256. GLOBAL bool
  257. IRC_HELP( CLIENT *Client, REQUEST *Req )
  258. {
  259. COMMAND *cmd;
  260. assert( Client != NULL );
  261. assert( Req != NULL );
  262. /* Bad number of arguments? */
  263. if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
  264. cmd = Parse_GetCommandStruct( );
  265. while( cmd->name )
  266. {
  267. if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
  268. cmd++;
  269. }
  270. IRC_SetPenalty( Client, 2 );
  271. return CONNECTED;
  272. } /* IRC_HELP */
  273. static char *
  274. Option_String( CONN_ID Idx )
  275. {
  276. static char option_txt[8];
  277. UINT16 options;
  278. options = Conn_Options(Idx);
  279. strcpy(option_txt, "F"); /* No idea what this means, but the
  280. * original ircd sends it ... */
  281. #ifdef ZLIB
  282. if(options & CONN_ZIP) /* zlib compression supported. */
  283. strcat(option_txt, "z");
  284. #endif
  285. return option_txt;
  286. } /* Option_String */
  287. static bool
  288. Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
  289. {
  290. CLIENT *cl, *from;
  291. CL2CHAN *cl2chan;
  292. CHANNEL *chan;
  293. char *currentTarget = Req->argv[0];
  294. char *lastCurrentTarget = NULL;
  295. assert(Client != NULL);
  296. assert(Req != NULL);
  297. if (Req->argc == 0) {
  298. if (!SendErrors)
  299. return CONNECTED;
  300. return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
  301. Client_ID(Client), Req->command);
  302. }
  303. if (Req->argc == 1) {
  304. if (!SendErrors)
  305. return CONNECTED;
  306. return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
  307. Client_ID(Client));
  308. }
  309. if (Req->argc > 2) {
  310. if (!SendErrors)
  311. return CONNECTED;
  312. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  313. Client_ID(Client), Req->command);
  314. }
  315. if (Client_Type(Client) == CLIENT_SERVER)
  316. from = Client_Search(Req->prefix);
  317. else
  318. from = Client;
  319. if (!from)
  320. return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
  321. Client_ID(Client), Req->prefix);
  322. /* handle msgtarget = msgto *("," msgto) */
  323. currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
  324. ngt_UpperStr(Req->command);
  325. while (currentTarget) {
  326. /* Check for and handle valid <msgto> of form:
  327. * RFC 2812 2.3.1:
  328. * msgto = channel / ( user [ "%" host ] "@" servername )
  329. * msgto =/ ( user "%" host ) / targetmask
  330. * msgto =/ nickname / ( nickname "!" user "@" host )
  331. */
  332. if (strchr(currentTarget, '!') == NULL)
  333. /* nickname */
  334. cl = Client_Search(currentTarget);
  335. else
  336. cl = NULL;
  337. if (cl == NULL) {
  338. /* If currentTarget isn't a nickname check for:
  339. * user ["%" host] "@" servername
  340. * user "%" host
  341. * nickname "!" user "@" host
  342. */
  343. char target[COMMAND_LEN];
  344. char * nick = NULL;
  345. char * user = NULL;
  346. char * host = NULL;
  347. char * server = NULL;
  348. strlcpy(target, currentTarget, COMMAND_LEN);
  349. server = strchr(target, '@');
  350. if (server) {
  351. *server = '\0';
  352. server++;
  353. }
  354. host = strchr(target, '%');
  355. if (host) {
  356. *host = '\0';
  357. host++;
  358. }
  359. user = strchr(target, '!');
  360. if (user) {
  361. /* msgto form: nick!user@host */
  362. *user = '\0';
  363. user++;
  364. nick = target;
  365. host = server; /* not "@server" but "@host" */
  366. } else {
  367. user = target;
  368. }
  369. for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
  370. if (Client_Type(cl) != CLIENT_USER &&
  371. Client_Type(cl) != CLIENT_SERVICE)
  372. continue;
  373. if (nick != NULL && host != NULL) {
  374. if (strcasecmp(nick, Client_ID(cl)) == 0 &&
  375. strcasecmp(user, Client_User(cl)) == 0 &&
  376. strcasecmp(host, Client_HostnameCloaked(cl)) == 0)
  377. break;
  378. else
  379. continue;
  380. }
  381. if (strcasecmp(user, Client_User(cl)) != 0)
  382. continue;
  383. if (host != NULL && strcasecmp(host,
  384. Client_HostnameCloaked(cl)) != 0)
  385. continue;
  386. if (server != NULL && strcasecmp(server,
  387. Client_ID(Client_Introducer(cl))) != 0)
  388. continue;
  389. break;
  390. }
  391. }
  392. if (cl) {
  393. /* Target is a user, enforce type */
  394. #ifndef STRICT_RFC
  395. if (Client_Type(cl) != ForceType &&
  396. !(ForceType == CLIENT_USER &&
  397. (Client_Type(cl) == CLIENT_USER ||
  398. Client_Type(cl) == CLIENT_SERVICE))) {
  399. #else
  400. if (Client_Type(cl) != ForceType) {
  401. #endif
  402. if (SendErrors && !IRC_WriteStrClient(
  403. from, ERR_NOSUCHNICK_MSG,Client_ID(from),
  404. currentTarget))
  405. return DISCONNECTED;
  406. goto send_next_target;
  407. }
  408. #ifndef STRICT_RFC
  409. if (ForceType == CLIENT_SERVICE &&
  410. (Conn_Options(Client_Conn(Client_NextHop(cl)))
  411. & CONN_RFC1459)) {
  412. /* SQUERY command but RFC 1459 link: convert
  413. * request to PRIVMSG command */
  414. Req->command = "PRIVMSG";
  415. }
  416. #endif
  417. if (Client_HasMode(cl, 'C')) {
  418. cl2chan = Channel_FirstChannelOf(cl);
  419. while (cl2chan) {
  420. chan = Channel_GetChannel(cl2chan);
  421. if (Channel_IsMemberOf(chan, from))
  422. break;
  423. cl2chan = Channel_NextChannelOf(cl, cl2chan);
  424. }
  425. if (!cl2chan) {
  426. if (SendErrors && !IRC_WriteStrClient(
  427. from, ERR_NOTONSAMECHANNEL_MSG,
  428. Client_ID(from), Client_ID(cl)))
  429. return DISCONNECTED;
  430. goto send_next_target;
  431. }
  432. }
  433. if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
  434. && strchr(Client_Modes(cl), 'a')) {
  435. /* Target is away */
  436. if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
  437. Client_ID(from),
  438. Client_ID(cl),
  439. Client_Away(cl)))
  440. return DISCONNECTED;
  441. }
  442. if (Client_Conn(from) > NONE) {
  443. Conn_UpdateIdle(Client_Conn(from));
  444. }
  445. if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
  446. Req->command, Client_ID(cl),
  447. Req->argv[1]))
  448. return DISCONNECTED;
  449. } else if (ForceType != CLIENT_SERVICE
  450. && (chan = Channel_Search(currentTarget))) {
  451. if (!Channel_Write(chan, from, Client, Req->command,
  452. SendErrors, Req->argv[1]))
  453. return DISCONNECTED;
  454. } else if (ForceType != CLIENT_SERVICE
  455. /* $#: server/target mask, RFC 2812, sec. 3.3.1 */
  456. && strchr("$#", currentTarget[0])
  457. && strchr(currentTarget, '.')) {
  458. /* targetmask */
  459. if (!Send_Message_Mask(from, Req->command, currentTarget,
  460. Req->argv[1], SendErrors))
  461. return DISCONNECTED;
  462. } else {
  463. if (!SendErrors)
  464. return CONNECTED;
  465. if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
  466. Client_ID(from), currentTarget))
  467. return DISCONNECTED;
  468. }
  469. send_next_target:
  470. currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
  471. if (currentTarget)
  472. Conn_SetPenalty(Client_Conn(Client), 1);
  473. }
  474. return CONNECTED;
  475. } /* Send_Message */
  476. static bool
  477. Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
  478. char * message, bool SendErrors)
  479. {
  480. CLIENT *cl;
  481. bool client_match;
  482. char *mask = targetMask + 1;
  483. const char *check_wildcards;
  484. cl = NULL;
  485. if (strchr(Client_Modes(from), 'o') == NULL) {
  486. if (!SendErrors)
  487. return true;
  488. return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
  489. Client_ID(from));
  490. }
  491. /*
  492. * RFC 2812, sec. 3.3.1 requires that targetMask have at least one
  493. * dot (".") and no wildcards ("*", "?") following the last one.
  494. */
  495. check_wildcards = strrchr(targetMask, '.');
  496. assert(check_wildcards != NULL);
  497. if (check_wildcards &&
  498. check_wildcards[strcspn(check_wildcards, "*?")])
  499. {
  500. if (!SendErrors)
  501. return true;
  502. return IRC_WriteStrClient(from, ERR_WILDTOPLEVEL, targetMask);
  503. }
  504. /* #: hostmask, see RFC 2812, sec. 3.3.1 */
  505. if (targetMask[0] == '#') {
  506. for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
  507. if (Client_Type(cl) != CLIENT_USER)
  508. continue;
  509. client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
  510. if (client_match)
  511. if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
  512. command, Client_ID(cl), message))
  513. return false;
  514. }
  515. } else {
  516. assert(targetMask[0] == '$'); /* $: server mask, see RFC 2812, sec. 3.3.1 */
  517. for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
  518. if (Client_Type(cl) != CLIENT_USER)
  519. continue;
  520. client_match = MatchCaseInsensitive(mask,
  521. Client_ID(Client_Introducer(cl)));
  522. if (client_match)
  523. if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
  524. command, Client_ID(cl), message))
  525. return false;
  526. }
  527. }
  528. return CONNECTED;
  529. } /* Send_Message_Mask */
  530. /* -eof- */