irc-server.c 12 KB

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