irc-server.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2014 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 for server links
  15. */
  16. #include <assert.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <strings.h>
  21. #include "conn-func.h"
  22. #include "conn-zip.h"
  23. #include "conf.h"
  24. #include "channel.h"
  25. #include "log.h"
  26. #include "messages.h"
  27. #include "parse.h"
  28. #include "numeric.h"
  29. #include "ngircd.h"
  30. #include "irc.h"
  31. #include "irc-info.h"
  32. #include "irc-write.h"
  33. #include "op.h"
  34. #include "irc-server.h"
  35. /**
  36. * Handler for the IRC "SERVER" command.
  37. *
  38. * @param Client The client from which this command has been received.
  39. * @param Req Request structure with prefix and all parameters.
  40. * @return CONNECTED or DISCONNECTED.
  41. */
  42. GLOBAL bool
  43. IRC_SERVER( CLIENT *Client, REQUEST *Req )
  44. {
  45. char str[100];
  46. CLIENT *from, *c;
  47. int i;
  48. assert( Client != NULL );
  49. assert( Req != NULL );
  50. /* Return an error if this is not a local client */
  51. if (Client_Conn(Client) <= NONE)
  52. return IRC_WriteErrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
  53. Client_ID(Client), Req->command);
  54. if (Client_Type(Client) == CLIENT_GOTPASS ||
  55. Client_Type(Client) == CLIENT_GOTPASS_2813) {
  56. /* We got a PASS command from the peer, and now a SERVER
  57. * command: the peer tries to register itself as a server. */
  58. LogDebug("Connection %d: got SERVER command (new server link) ...",
  59. Client_Conn(Client));
  60. if (Req->argc != 2 && Req->argc != 3)
  61. return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  62. Client_ID(Client),
  63. Req->command);
  64. /* Get configuration index of new remote server ... */
  65. for (i = 0; i < MAX_SERVERS; i++)
  66. if (strcasecmp(Req->argv[0], Conf_Server[i].name) == 0)
  67. break;
  68. /* Make sure the remote server is configured here */
  69. if (i >= MAX_SERVERS) {
  70. Log(LOG_ERR,
  71. "Connection %d: Server \"%s\" not configured here!",
  72. Client_Conn(Client), Req->argv[0]);
  73. Conn_Close(Client_Conn(Client), NULL,
  74. "Server not configured here", true);
  75. return DISCONNECTED;
  76. }
  77. /* Check server password */
  78. if (strcmp(Conn_Password(Client_Conn(Client)),
  79. Conf_Server[i].pwd_in) != 0) {
  80. Log(LOG_ERR,
  81. "Connection %d: Got bad password from server \"%s\"!",
  82. Client_Conn(Client), Req->argv[0]);
  83. Conn_Close(Client_Conn(Client), NULL,
  84. "Bad password", true);
  85. return DISCONNECTED;
  86. }
  87. /* Is there a registered server with this ID? */
  88. if (!Client_CheckID(Client, Req->argv[0]))
  89. return DISCONNECTED;
  90. /* Mark this connection as belonging to an configured server */
  91. if (!Conf_SetServer(i, Client_Conn(Client)))
  92. return DISCONNECTED;
  93. Client_SetID( Client, Req->argv[0] );
  94. Client_SetHops( Client, 1 );
  95. Client_SetInfo( Client, Req->argv[Req->argc - 1] );
  96. /* Is this server registering on our side, or are we connecting to
  97. * a remote server? */
  98. if (Client_Token(Client) != TOKEN_OUTBOUND) {
  99. /* Incoming connection, send user/pass */
  100. if (!IRC_WriteStrClient(Client, "PASS %s %s",
  101. Conf_Server[i].pwd_out,
  102. NGIRCd_ProtoID)
  103. || !IRC_WriteStrClient(Client, "SERVER %s 1 :%s",
  104. Conf_ServerName,
  105. Conf_ServerInfo)) {
  106. Conn_Close(Client_Conn(Client),
  107. "Unexpected server behavior!",
  108. NULL, false);
  109. return DISCONNECTED;
  110. }
  111. Client_SetIntroducer(Client, Client);
  112. Client_SetToken(Client, 1);
  113. } else {
  114. /* outgoing connect, we already sent a SERVER and PASS
  115. * command to the peer */
  116. Client_SetToken(Client, atoi(Req->argv[1]));
  117. }
  118. /* Check protocol level */
  119. if (Client_Type(Client) == CLIENT_GOTPASS) {
  120. /* We got a "simple" PASS command, so the peer is
  121. * using the protocol as defined in RFC 1459. */
  122. if (! (Conn_Options(Client_Conn(Client)) & CONN_RFC1459))
  123. Log(LOG_INFO,
  124. "Switching connection %d (\"%s\") to RFC 1459 compatibility mode.",
  125. Client_Conn(Client), Client_ID(Client));
  126. Conn_SetOption(Client_Conn(Client), CONN_RFC1459);
  127. }
  128. Client_SetType(Client, CLIENT_UNKNOWNSERVER);
  129. #ifdef ZLIB
  130. if (Client_HasFlag(Client, 'Z')
  131. && !Zip_InitConn(Client_Conn(Client))) {
  132. Conn_Close(Client_Conn(Client),
  133. "Can't initialize compression (zlib)!",
  134. NULL, false );
  135. return DISCONNECTED;
  136. }
  137. #endif
  138. #ifdef IRCPLUS
  139. if (Client_HasFlag(Client, 'H')) {
  140. LogDebug("Peer supports IRC+ extended server handshake ...");
  141. if (!IRC_Send_ISUPPORT(Client))
  142. return DISCONNECTED;
  143. return IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG,
  144. Client_ID(Client));
  145. } else {
  146. #endif
  147. if (Conf_MaxNickLength != CLIENT_NICK_LEN_DEFAULT)
  148. Log(LOG_CRIT,
  149. "Attention: this server uses a non-standard nick length, but the peer doesn't support the IRC+ extended server handshake!");
  150. #ifdef IRCPLUS
  151. }
  152. #endif
  153. return IRC_Num_ENDOFMOTD(Client, Req);
  154. }
  155. else if( Client_Type( Client ) == CLIENT_SERVER )
  156. {
  157. /* New server is being introduced to the network */
  158. if (Req->argc != 4)
  159. return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  160. Client_ID(Client), Req->command);
  161. /* check for existing server with same ID */
  162. if (!Client_CheckID(Client, Req->argv[0]))
  163. return DISCONNECTED;
  164. from = Client_Search( Req->prefix );
  165. if (! from) {
  166. /* Uh, Server, that introduced the new server is unknown?! */
  167. Log(LOG_ALERT,
  168. "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)",
  169. Req->prefix, Client_Conn(Client));
  170. Conn_Close(Client_Conn(Client), NULL,
  171. "Unknown ID in prefix of SERVER", true);
  172. return DISCONNECTED;
  173. }
  174. c = Client_NewRemoteServer(Client, Req->argv[0], from,
  175. atoi(Req->argv[1]), atoi(Req->argv[2]),
  176. Req->argv[3], true);
  177. if (!c) {
  178. Log(LOG_ALERT,
  179. "Can't create client structure for server! (on connection %d)",
  180. Client_Conn(Client));
  181. Conn_Close(Client_Conn(Client), NULL,
  182. "Can't allocate client structure for remote server",
  183. true);
  184. return DISCONNECTED;
  185. }
  186. if (Client_Hops(c) > 1 && Req->prefix[0])
  187. snprintf(str, sizeof(str), "connected to %s, ",
  188. Client_ID(from));
  189. else
  190. strcpy(str, "");
  191. Log(LOG_NOTICE|LOG_snotice,
  192. "Server \"%s\" registered (via %s, %s%d hop%s).",
  193. Client_ID(c), Client_ID(Client), str, Client_Hops(c),
  194. Client_Hops(c) > 1 ? "s": "" );
  195. /* notify other servers */
  196. IRC_WriteStrServersPrefix(Client, from, "SERVER %s %d %d :%s",
  197. Client_ID(c), Client_Hops(c) + 1,
  198. Client_MyToken(c), Client_Info(c));
  199. return CONNECTED;
  200. } else
  201. return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  202. Client_ID(Client), Req->command);
  203. } /* IRC_SERVER */
  204. /*
  205. * Handler for the IRC "NJOIN" command.
  206. *
  207. * @param Client The client from which this command has been received.
  208. * @param Req Request structure with prefix and all parameters.
  209. * @return CONNECTED or DISCONNECTED.
  210. */
  211. GLOBAL bool
  212. IRC_NJOIN( CLIENT *Client, REQUEST *Req )
  213. {
  214. char nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
  215. bool is_owner, is_chanadmin, is_op, is_halfop, is_voiced;
  216. CHANNEL *chan;
  217. CLIENT *c;
  218. assert(Client != NULL);
  219. assert(Req != NULL);
  220. strlcpy(nick_in, Req->argv[1], sizeof(nick_in));
  221. strcpy(nick_out, "");
  222. channame = Req->argv[0];
  223. ptr = strtok(nick_in, ",");
  224. while (ptr) {
  225. is_owner = is_chanadmin = is_op = is_halfop = is_voiced = false;
  226. /* cut off prefixes */
  227. while ((*ptr == '~') || (*ptr == '&') || (*ptr == '@') ||
  228. (*ptr == '%') || (*ptr == '+')) {
  229. if (*ptr == '~')
  230. is_owner = true;
  231. if (*ptr == '&')
  232. is_chanadmin = true;
  233. if (*ptr == '@')
  234. is_op = true;
  235. if (*ptr == '%')
  236. is_halfop = true;
  237. if (*ptr == '+')
  238. is_voiced = true;
  239. ptr++;
  240. }
  241. c = Client_Search(ptr);
  242. if (!c) {
  243. /* Client not found? */
  244. Log(LOG_ERR,
  245. "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!",
  246. ptr, channame);
  247. goto skip_njoin;
  248. }
  249. if (!Channel_Join(c, channame)) {
  250. /* Failed to join channel. Ooops!? */
  251. Log(LOG_ALERT,
  252. "Failed to join client \"%s\" to channel \"%s\" (NJOIN): killing it!",
  253. ptr, channame);
  254. IRC_KillClient(NULL, NULL, ptr, "Internal NJOIN error!");
  255. Log(LOG_DEBUG, "... done.");
  256. goto skip_njoin;
  257. }
  258. chan = Channel_Search(channame);
  259. assert(chan != NULL);
  260. if (is_owner)
  261. Channel_UserModeAdd(chan, c, 'q');
  262. if (is_chanadmin)
  263. Channel_UserModeAdd(chan, c, 'a');
  264. if (is_op)
  265. Channel_UserModeAdd(chan, c, 'o');
  266. if (is_halfop)
  267. Channel_UserModeAdd(chan, c, 'h');
  268. if (is_voiced)
  269. Channel_UserModeAdd(chan, c, 'v');
  270. /* Announce client to the channel */
  271. IRC_WriteStrChannelPrefix(Client, chan, c, false,
  272. "JOIN :%s", channame);
  273. /* Announce "channel user modes" to the channel, if any */
  274. strlcpy(modes, Channel_UserModes(chan, c), sizeof(modes));
  275. if (modes[0])
  276. IRC_WriteStrChannelPrefix(Client, chan, Client, false,
  277. "MODE %s +%s %s", channame,
  278. modes, Client_ID(c));
  279. /* Build nick list for forwarding command */
  280. if (nick_out[0] != '\0')
  281. strlcat(nick_out, ",", sizeof(nick_out));
  282. if (is_owner)
  283. strlcat(nick_out, "~", sizeof(nick_out));
  284. if (is_chanadmin)
  285. strlcat(nick_out, "&", sizeof(nick_out));
  286. if (is_op)
  287. strlcat(nick_out, "@", sizeof(nick_out));
  288. if (is_halfop)
  289. strlcat(nick_out, "%", sizeof(nick_out));
  290. if (is_voiced)
  291. strlcat(nick_out, "+", sizeof(nick_out));
  292. strlcat(nick_out, ptr, sizeof(nick_out));
  293. skip_njoin:
  294. /* Get next nick, if any ... */
  295. ptr = strtok(NULL, ",");
  296. }
  297. /* forward to other servers */
  298. if (nick_out[0] != '\0')
  299. IRC_WriteStrServersPrefix(Client, Client_ThisServer(),
  300. "NJOIN %s :%s", Req->argv[0], nick_out);
  301. return CONNECTED;
  302. } /* IRC_NJOIN */
  303. /**
  304. * Handler for the IRC "SQUIT" command.
  305. *
  306. * @param Client The client from which this command has been received.
  307. * @param Req Request structure with prefix and all parameters.
  308. * @return CONNECTED or DISCONNECTED.
  309. */
  310. GLOBAL bool
  311. IRC_SQUIT(CLIENT * Client, REQUEST * Req)
  312. {
  313. char msg[COMMAND_LEN], logmsg[COMMAND_LEN];
  314. CLIENT *from, *target;
  315. CONN_ID con;
  316. int loglevel;
  317. assert(Client != NULL);
  318. assert(Req != NULL);
  319. if (Client_Type(Client) != CLIENT_SERVER
  320. && !Client_HasMode(Client, 'o'))
  321. return Op_NoPrivileges(Client, Req);
  322. if (Client_Type(Client) == CLIENT_SERVER && Req->prefix) {
  323. from = Client_Search(Req->prefix);
  324. if (Client_Type(from) != CLIENT_SERVER
  325. && !Op_Check(Client, Req))
  326. return Op_NoPrivileges(Client, Req);
  327. } else
  328. from = Client;
  329. if (!from)
  330. return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
  331. Client_ID(Client), Req->prefix);
  332. if (Client_Type(Client) == CLIENT_USER)
  333. loglevel = LOG_NOTICE | LOG_snotice;
  334. else
  335. loglevel = LOG_DEBUG;
  336. Log(loglevel, "Got SQUIT from %s for \"%s\": \"%s\" ...",
  337. Client_ID(from), Req->argv[0], Req->argv[1]);
  338. target = Client_Search(Req->argv[0]);
  339. if (Client_Type(Client) != CLIENT_SERVER &&
  340. target == Client_ThisServer())
  341. return Op_NoPrivileges(Client, Req);
  342. if (!target) {
  343. /* The server is (already) unknown */
  344. Log(LOG_WARNING,
  345. "Got SQUIT from %s for unknown server \"%s\"!?",
  346. Client_ID(Client), Req->argv[0]);
  347. return CONNECTED;
  348. }
  349. con = Client_Conn(target);
  350. if (Req->argv[1][0])
  351. if (Client_NextHop(from) != Client || con > NONE)
  352. snprintf(msg, sizeof(msg), "\"%s\" (SQUIT from %s)",
  353. Req->argv[1], Client_ID(from));
  354. else
  355. strlcpy(msg, Req->argv[1], sizeof(msg));
  356. else
  357. snprintf(msg, sizeof(msg), "Got SQUIT from %s",
  358. Client_ID(from));
  359. if (con > NONE) {
  360. /* We are directly connected to the target server, so we
  361. * have to tear down the connection and to inform all the
  362. * other remaining servers in the network */
  363. IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
  364. "Received SQUIT %s from %s: %s",
  365. Req->argv[0], Client_ID(from),
  366. Req->argv[1][0] ? Req->argv[1] : "-");
  367. Conn_Close(con, NULL, msg, true);
  368. if (con == Client_Conn(Client))
  369. return DISCONNECTED;
  370. } else {
  371. /* This server is not directly connected, so the SQUIT must
  372. * be forwarded ... */
  373. if (Client_Type(from) != CLIENT_SERVER) {
  374. /* The origin is not an IRC server, so don't evaluate
  375. * this SQUIT but simply forward it */
  376. IRC_WriteStrClientPrefix(Client_NextHop(target),
  377. from, "SQUIT %s :%s", Req->argv[0], Req->argv[1]);
  378. } else {
  379. /* SQUIT has been generated by another server, so
  380. * remove the target server from the network! */
  381. logmsg[0] = '\0';
  382. if (!strchr(msg, '('))
  383. snprintf(logmsg, sizeof(logmsg),
  384. "\"%s\" (SQUIT from %s)", Req->argv[1],
  385. Client_ID(from));
  386. Client_Destroy(target, logmsg[0] ? logmsg : msg,
  387. msg, false);
  388. }
  389. }
  390. return CONNECTED;
  391. } /* IRC_SQUIT */
  392. /* -eof- */