irc-server.c 11 KB

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