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