irc-server.c 13 KB

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