irc-server.c 12 KB

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