irc-server.c 11 KB

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