irc-server.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. static char UNUSED id[] = "$Id: irc-server.c,v 1.46 2007/11/21 12:16:36 alex Exp $";
  15. #include "imp.h"
  16. #include <assert.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <strings.h>
  21. #include "defines.h"
  22. #include "resolve.h"
  23. #include "conn.h"
  24. #include "conn-zip.h"
  25. #include "conf.h"
  26. #include "client.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 "exp.h"
  37. #include "irc-server.h"
  38. /**
  39. * Handler for the IRC command "SERVER".
  40. * See RFC 2813 section 4.1.2.
  41. */
  42. GLOBAL bool
  43. IRC_SERVER( CLIENT *Client, REQUEST *Req )
  44. {
  45. char str[LINE_LEN], *ptr;
  46. CLIENT *from, *c;
  47. bool ok;
  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. /* 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. /* Falsche Anzahl Parameter? */
  62. if(( Req->argc != 2 ) && ( Req->argc != 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  63. /* Ist dieser Server bei uns konfiguriert? */
  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. /* Server ist nicht konfiguriert! */
  68. Log( LOG_ERR, "Connection %d: Server \"%s\" not configured here!", Client_Conn( Client ), Req->argv[0] );
  69. Conn_Close( Client_Conn( Client ), NULL, "Server not configured here", true);
  70. return DISCONNECTED;
  71. }
  72. if( strcmp( Client_Password( Client ), Conf_Server[i].pwd_in ) != 0 )
  73. {
  74. /* Falsches Passwort */
  75. Log( LOG_ERR, "Connection %d: Got bad password from server \"%s\"!", Client_Conn( Client ), Req->argv[0] );
  76. Conn_Close( Client_Conn( Client ), NULL, "Bad password", true);
  77. return DISCONNECTED;
  78. }
  79. /* Ist ein Server mit dieser ID bereits registriert? */
  80. if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
  81. /* Server-Strukturen fuellen ;-) */
  82. Client_SetID( Client, Req->argv[0] );
  83. Client_SetHops( Client, 1 );
  84. Client_SetInfo( Client, Req->argv[Req->argc - 1] );
  85. /* Meldet sich der Server bei uns an (d.h., bauen nicht wir
  86. * selber die Verbindung zu einem anderen Server auf)? */
  87. con = Client_Conn( Client );
  88. if( Client_Token( Client ) != TOKEN_OUTBOUND )
  89. {
  90. /* Eingehende Verbindung: Unseren SERVER- und PASS-Befehl senden */
  91. ok = true;
  92. if( ! IRC_WriteStrClient( Client, "PASS %s %s", Conf_Server[i].pwd_out, NGIRCd_ProtoID )) ok = false;
  93. else ok = IRC_WriteStrClient( Client, "SERVER %s 1 :%s", Conf_ServerName, Conf_ServerInfo );
  94. if( ! ok )
  95. {
  96. Conn_Close( con, "Unexpected server behavior!", NULL, false );
  97. return DISCONNECTED;
  98. }
  99. Client_SetIntroducer( Client, Client );
  100. Client_SetToken( Client, 1 );
  101. }
  102. else
  103. {
  104. /* Ausgehende verbindung, SERVER und PASS wurden von uns bereits
  105. * an die Gegenseite uerbermittelt */
  106. Client_SetToken( Client, atoi( Req->argv[1] ));
  107. }
  108. /* Mark this connection as belonging to an configured server */
  109. Conf_SetServer(i, con);
  110. Client_SetType(Client, CLIENT_UNKNOWNSERVER);
  111. #ifdef ZLIB
  112. /* Kompression initialisieren, wenn erforderlich */
  113. if( strchr( Client_Flags( Client ), 'Z' ))
  114. {
  115. if( ! Zip_InitConn( con ))
  116. {
  117. /* Fehler! */
  118. Conn_Close( con, "Can't inizialize compression (zlib)!", NULL, false );
  119. return DISCONNECTED;
  120. }
  121. }
  122. #endif
  123. #ifdef IRCPLUS
  124. if (strchr(Client_Flags(Client), 'H')) {
  125. LogDebug("Peer supports IRC+ extended server handshake ...");
  126. if (!IRC_Send_ISUPPORT(Client))
  127. return DISCONNECTED;
  128. return IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG,
  129. Client_ID(Client));
  130. } else {
  131. #endif
  132. if (Conf_MaxNickLength != CLIENT_NICK_LEN_DEFAULT)
  133. Log(LOG_CRIT,
  134. "Attention: this server uses a non-standard nick length, but the peer doesn't support the IRC+ extended server handshake!");
  135. #ifdef IRCPLUS
  136. }
  137. #endif
  138. return IRC_Num_ENDOFMOTD(Client, Req);
  139. }
  140. else if( Client_Type( Client ) == CLIENT_SERVER )
  141. {
  142. /* Neuer Server wird im Netz angekuendigt */
  143. /* Falsche Anzahl Parameter? */
  144. if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  145. /* Ist ein Server mit dieser ID bereits registriert? */
  146. if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
  147. /* Ueberfluessige Hostnamen aus Info-Text entfernen */
  148. ptr = strchr( Req->argv[3] + 2, '[' );
  149. if( ! ptr ) ptr = Req->argv[3];
  150. from = Client_Search( Req->prefix );
  151. if( ! from )
  152. {
  153. /* Hm, Server, der diesen einfuehrt, ist nicht bekannt!? */
  154. Log( LOG_ALERT, "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)", Req->prefix, Client_Conn( Client ));
  155. Conn_Close( Client_Conn( Client ), NULL, "Unknown ID in prefix of SERVER", true);
  156. return DISCONNECTED;
  157. }
  158. /* Neue Client-Struktur anlegen */
  159. c = Client_NewRemoteServer( Client, Req->argv[0], from, atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr, true);
  160. if( ! c )
  161. {
  162. /* Neue Client-Struktur konnte nicht angelegt werden */
  163. Log( LOG_ALERT, "Can't create client structure for server! (on connection %d)", Client_Conn( Client ));
  164. Conn_Close( Client_Conn( Client ), NULL, "Can't allocate client structure for remote server", true);
  165. return DISCONNECTED;
  166. }
  167. /* Log-Meldung zusammenbauen und ausgeben */
  168. if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) snprintf( str, sizeof( str ), "connected to %s, ", Client_ID( from ));
  169. else strcpy( str, "" );
  170. 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": "" );
  171. /* Andere Server informieren */
  172. IRC_WriteStrServersPrefix( Client, from, "SERVER %s %d %d :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_MyToken( c ), Client_Info( c ));
  173. return CONNECTED;
  174. } else
  175. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  176. Client_ID(Client), Req->command);
  177. } /* IRC_SERVER */
  178. GLOBAL bool
  179. IRC_NJOIN( CLIENT *Client, REQUEST *Req )
  180. {
  181. char nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
  182. bool is_op, is_voiced;
  183. CHANNEL *chan;
  184. CLIENT *c;
  185. assert( Client != NULL );
  186. assert( Req != NULL );
  187. /* Falsche Anzahl Parameter? */
  188. if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  189. strlcpy( nick_in, Req->argv[1], sizeof( nick_in ));
  190. strcpy( nick_out, "" );
  191. channame = Req->argv[0];
  192. ptr = strtok( nick_in, "," );
  193. while( ptr )
  194. {
  195. is_op = is_voiced = false;
  196. /* Prefixe abschneiden */
  197. while(( *ptr == '@' ) || ( *ptr == '+' ))
  198. {
  199. if( *ptr == '@' ) is_op = true;
  200. if( *ptr == '+' ) is_voiced = true;
  201. ptr++;
  202. }
  203. c = Client_Search( ptr );
  204. if( c )
  205. {
  206. Channel_Join( c, channame );
  207. chan = Channel_Search( channame );
  208. assert( chan != NULL );
  209. if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
  210. if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
  211. /* im Channel bekannt machen */
  212. IRC_WriteStrChannelPrefix( Client, chan, c, false, "JOIN :%s", channame );
  213. /* Channel-User-Modes setzen */
  214. strlcpy( modes, Channel_UserModes( chan, c ), sizeof( modes ));
  215. if( modes[0] )
  216. {
  217. /* Modes im Channel bekannt machen */
  218. IRC_WriteStrChannelPrefix( Client, chan, Client, false, "MODE %s +%s %s", channame, modes, Client_ID( c ));
  219. }
  220. if( nick_out[0] != '\0' ) strlcat( nick_out, ",", sizeof( nick_out ));
  221. if( is_op ) strlcat( nick_out, "@", sizeof( nick_out ));
  222. if( is_voiced ) strlcat( nick_out, "+", sizeof( nick_out ));
  223. strlcat( nick_out, ptr, sizeof( nick_out ));
  224. }
  225. else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
  226. /* naechsten Nick suchen */
  227. ptr = strtok( NULL, "," );
  228. }
  229. /* an andere Server weiterleiten */
  230. if( nick_out[0] != '\0' ) IRC_WriteStrServersPrefix( Client, Client_ThisServer( ), "NJOIN %s :%s", Req->argv[0], nick_out );
  231. return CONNECTED;
  232. } /* IRC_NJOIN */
  233. GLOBAL bool
  234. IRC_SQUIT( CLIENT *Client, REQUEST *Req )
  235. {
  236. CLIENT *target;
  237. char msg[LINE_LEN + 64];
  238. assert( Client != NULL );
  239. assert( Req != NULL );
  240. /* Falsche Anzahl Parameter? */
  241. if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  242. Log( LOG_DEBUG, "Got SQUIT from %s for \"%s\": \"%s\" ...", Client_ID( Client ), Req->argv[0], Req->argv[1] );
  243. target = Client_Search( Req->argv[0] );
  244. if( ! target )
  245. {
  246. /* Den Server kennen wir nicht (mehr), also nichts zu tun. */
  247. Log( LOG_WARNING, "Got SQUIT from %s for unknown server \"%s\"!?", Client_ID( Client ), Req->argv[0] );
  248. return CONNECTED;
  249. }
  250. if( Req->argv[1][0] )
  251. {
  252. if( strlen( Req->argv[1] ) > LINE_LEN ) Req->argv[1][LINE_LEN] = '\0';
  253. snprintf( msg, sizeof( msg ), "%s (SQUIT from %s).", Req->argv[1], Client_ID( Client ));
  254. }
  255. else snprintf( msg, sizeof( msg ), "Got SQUIT from %s.", Client_ID( Client ));
  256. if( Client_Conn( target ) > NONE )
  257. {
  258. /* dieser Server hat die Connection */
  259. if( Req->argv[1][0] ) Conn_Close( Client_Conn( target ), msg, Req->argv[1], true);
  260. else Conn_Close( Client_Conn( target ), msg, NULL, true);
  261. return DISCONNECTED;
  262. }
  263. else
  264. {
  265. /* Verbindung hielt anderer Server */
  266. Client_Destroy( target, msg, Req->argv[1], false );
  267. return CONNECTED;
  268. }
  269. } /* IRC_SQUIT */
  270. /* -eof- */