irc-server.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. if (!Req->prefix) {
  165. /* We definitely need a prefix here! */
  166. Log(LOG_ALERT, "Got SERVER command without prefix! (on connection %d)",
  167. Client_Conn(Client));
  168. Conn_Close(Client_Conn(Client), NULL,
  169. "SERVER command without prefix", true);
  170. return DISCONNECTED;
  171. }
  172. from = Client_Search( Req->prefix );
  173. if (! from) {
  174. /* Uh, Server, that introduced the new server is unknown?! */
  175. Log(LOG_ALERT,
  176. "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)",
  177. Req->prefix, Client_Conn(Client));
  178. Conn_Close(Client_Conn(Client), NULL,
  179. "Unknown ID in prefix of SERVER", true);
  180. return DISCONNECTED;
  181. }
  182. c = Client_NewRemoteServer(Client, Req->argv[0], from,
  183. atoi(Req->argv[1]), atoi(Req->argv[2]),
  184. Req->argv[3], true);
  185. if (!c) {
  186. Log(LOG_ALERT,
  187. "Can't create client structure for server! (on connection %d)",
  188. Client_Conn(Client));
  189. Conn_Close(Client_Conn(Client), NULL,
  190. "Can't allocate client structure for remote server",
  191. true);
  192. return DISCONNECTED;
  193. }
  194. if (Client_Hops(c) > 1 && Req->prefix[0])
  195. snprintf(str, sizeof(str), "connected to %s, ",
  196. Client_ID(from));
  197. else
  198. strcpy(str, "");
  199. Log(LOG_NOTICE|LOG_snotice,
  200. "Server \"%s\" registered (via %s, %s%d hop%s).",
  201. Client_ID(c), Client_ID(Client), str, Client_Hops(c),
  202. Client_Hops(c) > 1 ? "s": "" );
  203. /* notify other servers */
  204. IRC_WriteStrServersPrefix(Client, from, "SERVER %s %d %d :%s",
  205. Client_ID(c), Client_Hops(c) + 1,
  206. Client_MyToken(c), Client_Info(c));
  207. return CONNECTED;
  208. } else
  209. return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  210. Client_ID(Client), Req->command);
  211. } /* IRC_SERVER */
  212. /*
  213. * Handler for the IRC "NJOIN" command.
  214. *
  215. * @param Client The client from which this command has been received.
  216. * @param Req Request structure with prefix and all parameters.
  217. * @return CONNECTED or DISCONNECTED.
  218. */
  219. GLOBAL bool
  220. IRC_NJOIN( CLIENT *Client, REQUEST *Req )
  221. {
  222. char nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
  223. bool is_owner, is_chanadmin, is_op, is_halfop, is_voiced;
  224. CHANNEL *chan;
  225. CLIENT *c;
  226. assert(Client != NULL);
  227. assert(Req != NULL);
  228. strlcpy(nick_in, Req->argv[1], sizeof(nick_in));
  229. strcpy(nick_out, "");
  230. channame = Req->argv[0];
  231. ptr = strtok(nick_in, ",");
  232. while (ptr) {
  233. is_owner = is_chanadmin = is_op = is_halfop = is_voiced = false;
  234. /* cut off prefixes */
  235. while ((*ptr == '~') || (*ptr == '&') || (*ptr == '@') ||
  236. (*ptr == '%') || (*ptr == '+')) {
  237. if (*ptr == '~')
  238. is_owner = true;
  239. if (*ptr == '&')
  240. is_chanadmin = true;
  241. if (*ptr == '@')
  242. is_op = true;
  243. if (*ptr == '%')
  244. is_halfop = true;
  245. if (*ptr == '+')
  246. is_voiced = true;
  247. ptr++;
  248. }
  249. c = Client_Search(ptr);
  250. if (!c) {
  251. /* Client not found? */
  252. Log(LOG_ERR,
  253. "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!",
  254. ptr, channame);
  255. goto skip_njoin;
  256. }
  257. if (!Channel_Join(c, channame)) {
  258. /* Failed to join channel. Ooops!? */
  259. Log(LOG_ALERT,
  260. "Failed to join client \"%s\" to channel \"%s\" (NJOIN): killing it!",
  261. ptr, channame);
  262. IRC_KillClient(NULL, NULL, ptr, "Internal NJOIN error!");
  263. Log(LOG_DEBUG, "... done.");
  264. goto skip_njoin;
  265. }
  266. chan = Channel_Search(channame);
  267. assert(chan != NULL);
  268. if (is_owner)
  269. Channel_UserModeAdd(chan, c, 'q');
  270. if (is_chanadmin)
  271. Channel_UserModeAdd(chan, c, 'a');
  272. if (is_op)
  273. Channel_UserModeAdd(chan, c, 'o');
  274. if (is_halfop)
  275. Channel_UserModeAdd(chan, c, 'h');
  276. if (is_voiced)
  277. Channel_UserModeAdd(chan, c, 'v');
  278. /* Announce client to the channel */
  279. IRC_WriteStrChannelPrefix(Client, chan, c, false,
  280. "JOIN :%s", channame);
  281. /* Announce "channel user modes" to the channel, if any */
  282. strlcpy(modes, Channel_UserModes(chan, c), sizeof(modes));
  283. if (modes[0])
  284. IRC_WriteStrChannelPrefix(Client, chan, Client, false,
  285. "MODE %s +%s %s", channame,
  286. modes, Client_ID(c));
  287. /* Build nick list for forwarding command */
  288. if (nick_out[0] != '\0')
  289. strlcat(nick_out, ",", sizeof(nick_out));
  290. if (is_owner)
  291. strlcat(nick_out, "~", sizeof(nick_out));
  292. if (is_chanadmin)
  293. strlcat(nick_out, "&", sizeof(nick_out));
  294. if (is_op)
  295. strlcat(nick_out, "@", sizeof(nick_out));
  296. if (is_halfop)
  297. strlcat(nick_out, "%", sizeof(nick_out));
  298. if (is_voiced)
  299. strlcat(nick_out, "+", sizeof(nick_out));
  300. strlcat(nick_out, ptr, sizeof(nick_out));
  301. skip_njoin:
  302. /* Get next nick, if any ... */
  303. ptr = strtok(NULL, ",");
  304. }
  305. /* forward to other servers */
  306. if (nick_out[0] != '\0')
  307. IRC_WriteStrServersPrefix(Client, Client_ThisServer(),
  308. "NJOIN %s :%s", Req->argv[0], nick_out);
  309. return CONNECTED;
  310. } /* IRC_NJOIN */
  311. /**
  312. * Handler for the IRC "SQUIT" command.
  313. *
  314. * @param Client The client from which this command has been received.
  315. * @param Req Request structure with prefix and all parameters.
  316. * @return CONNECTED or DISCONNECTED.
  317. */
  318. GLOBAL bool
  319. IRC_SQUIT(CLIENT * Client, REQUEST * Req)
  320. {
  321. char msg[COMMAND_LEN], logmsg[COMMAND_LEN];
  322. CLIENT *from, *target;
  323. CONN_ID con, client_con;
  324. int loglevel;
  325. assert(Client != NULL);
  326. assert(Req != NULL);
  327. if (Client_Type(Client) != CLIENT_SERVER
  328. && !Client_HasMode(Client, 'o'))
  329. return Op_NoPrivileges(Client, Req);
  330. if (Client_Type(Client) == CLIENT_SERVER && Req->prefix) {
  331. from = Client_Search(Req->prefix);
  332. if (Client_Type(from) != CLIENT_SERVER
  333. && !Op_Check(Client, Req))
  334. return Op_NoPrivileges(Client, Req);
  335. } else
  336. from = Client;
  337. if (!from)
  338. return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
  339. Client_ID(Client), Req->prefix);
  340. if (Client_Type(Client) == CLIENT_USER)
  341. loglevel = LOG_NOTICE | LOG_snotice;
  342. else
  343. loglevel = LOG_DEBUG;
  344. Log(loglevel, "Got SQUIT from %s for \"%s\": \"%s\" ...",
  345. Client_ID(from), Req->argv[0], Req->argv[1]);
  346. target = Client_Search(Req->argv[0]);
  347. if (Client_Type(Client) != CLIENT_SERVER &&
  348. target == Client_ThisServer())
  349. return Op_NoPrivileges(Client, Req);
  350. if (!target) {
  351. /* The server is (already) unknown */
  352. Log(LOG_WARNING,
  353. "Got SQUIT from %s for unknown server \"%s\"!?",
  354. Client_ID(Client), Req->argv[0]);
  355. return CONNECTED;
  356. }
  357. client_con = Client_Conn(Client);
  358. con = Client_Conn(target);
  359. if (Req->argv[1][0])
  360. if (Client_NextHop(from) != Client || con > NONE)
  361. snprintf(msg, sizeof(msg), "\"%s\" (SQUIT from %s)",
  362. Req->argv[1], Client_ID(from));
  363. else
  364. strlcpy(msg, Req->argv[1], sizeof(msg));
  365. else
  366. snprintf(msg, sizeof(msg), "Got SQUIT from %s",
  367. Client_ID(from));
  368. if (con > NONE) {
  369. /* We are directly connected to the target server, so we
  370. * have to tear down the connection and to inform all the
  371. * other remaining servers in the network */
  372. IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
  373. "Received SQUIT %s from %s: %s",
  374. Req->argv[0], Client_ID(from),
  375. Req->argv[1][0] ? Req->argv[1] : "-");
  376. Conn_Close(con, NULL, msg, true);
  377. if (con == client_con)
  378. return DISCONNECTED;
  379. } else {
  380. /* This server is not directly connected, so the SQUIT must
  381. * be forwarded ... */
  382. if (Client_Type(from) != CLIENT_SERVER) {
  383. /* The origin is not an IRC server, so don't evaluate
  384. * this SQUIT but simply forward it */
  385. IRC_WriteStrClientPrefix(Client_NextHop(target),
  386. from, "SQUIT %s :%s", Req->argv[0], Req->argv[1]);
  387. } else {
  388. /* SQUIT has been generated by another server, so
  389. * remove the target server from the network! */
  390. logmsg[0] = '\0';
  391. if (!strchr(msg, '('))
  392. snprintf(logmsg, sizeof(logmsg),
  393. "\"%s\" (SQUIT from %s)", Req->argv[1],
  394. Client_ID(from));
  395. Client_Destroy(target, logmsg[0] ? logmsg : msg,
  396. msg, false);
  397. }
  398. }
  399. return CONNECTED;
  400. } /* IRC_SQUIT */
  401. /* -eof- */