irc.c 15 KB

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