2 2ee05c9a 2002-03-03 alex * ngIRCd -- The Next Generation IRC Daemon
3 53b98fd7 2007-11-20 alex * Copyright (c)2001-2007 Alexander Barton (alex@barton.de)
5 490f28ff 2002-12-12 alex * This program is free software; you can redistribute it and/or modify
6 490f28ff 2002-12-12 alex * it under the terms of the GNU General Public License as published by
7 490f28ff 2002-12-12 alex * the Free Software Foundation; either version 2 of the License, or
8 490f28ff 2002-12-12 alex * (at your option) any later version.
9 490f28ff 2002-12-12 alex * Please read the file COPYING, README and AUTHORS for more information.
11 490f28ff 2002-12-12 alex * IRC commands for server links
15 ca33cbda 2002-03-12 alex #include "portab.h"
17 ca33cbda 2002-03-12 alex #include "imp.h"
18 2ee05c9a 2002-03-03 alex #include <assert.h>
19 2ee05c9a 2002-03-03 alex #include <stdio.h>
20 2ee05c9a 2002-03-03 alex #include <stdlib.h>
21 2ee05c9a 2002-03-03 alex #include <string.h>
22 57c7e236 2004-01-17 alex #include <strings.h>
24 3012c232 2004-05-11 alex #include "defines.h"
25 c2f60abe 2002-05-27 alex #include "resolve.h"
26 c2f60abe 2002-05-27 alex #include "conn.h"
27 a60465be 2008-09-23 alex #include "conn-func.h"
28 437f2c33 2003-12-30 alex #include "conn-zip.h"
29 a2544e49 2003-12-30 alex #include "conf.h"
30 c2f60abe 2002-05-27 alex #include "client.h"
31 c2f60abe 2002-05-27 alex #include "channel.h"
32 2ee05c9a 2002-03-03 alex #include "irc-write.h"
33 dd598200 2004-04-25 alex #include "lists.h"
34 2ee05c9a 2002-03-03 alex #include "log.h"
35 2ee05c9a 2002-03-03 alex #include "messages.h"
36 c2f60abe 2002-05-27 alex #include "parse.h"
37 47ca178a 2007-11-21 alex #include "numeric.h"
38 d58431a0 2002-09-02 alex #include "ngircd.h"
39 47ca178a 2007-11-21 alex #include "irc-info.h"
41 ca33cbda 2002-03-12 alex #include "exp.h"
42 2ee05c9a 2002-03-03 alex #include "irc-server.h"
46 27d947fb 2006-10-01 alex * Handler for the IRC command "SERVER".
47 27d947fb 2006-10-01 alex * See RFC 2813 section 4.1.2.
50 c2f60abe 2002-05-27 alex IRC_SERVER( CLIENT *Client, REQUEST *Req )
52 47ca178a 2007-11-21 alex char str[LINE_LEN], *ptr;
53 47ca178a 2007-11-21 alex CLIENT *from, *c;
56 65f3adca 2002-11-26 alex CONN_ID con;
58 2ee05c9a 2002-03-03 alex assert( Client != NULL );
59 2ee05c9a 2002-03-03 alex assert( Req != NULL );
61 27d947fb 2006-10-01 alex /* Return an error if this is not a local client */
62 27d947fb 2006-10-01 alex if (Client_Conn(Client) <= NONE)
63 27d947fb 2006-10-01 alex return IRC_WriteStrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
64 27d947fb 2006-10-01 alex Client_ID(Client), Req->command);
66 a60465be 2008-09-23 alex if (Client_Type(Client) == CLIENT_GOTPASS ||
67 a60465be 2008-09-23 alex Client_Type(Client) == CLIENT_GOTPASS_2813) {
68 27d947fb 2006-10-01 alex /* We got a PASS command from the peer, and now a SERVER
69 27d947fb 2006-10-01 alex * command: the peer tries to register itself as a server. */
70 37602d15 2006-12-02 fw LogDebug("Connection %d: got SERVER command (new server link) ...",
71 37602d15 2006-12-02 fw Client_Conn(Client));
73 2ee05c9a 2002-03-03 alex /* Falsche Anzahl Parameter? */
74 2ee05c9a 2002-03-03 alex if(( Req->argc != 2 ) && ( Req->argc != 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
76 2ee05c9a 2002-03-03 alex /* Ist dieser Server bei uns konfiguriert? */
77 a2544e49 2003-12-30 alex for( i = 0; i < MAX_SERVERS; i++ ) if( strcasecmp( Req->argv[0], Conf_Server[i].name ) == 0 ) break;
78 a2544e49 2003-12-30 alex if( i >= MAX_SERVERS )
80 2ee05c9a 2002-03-03 alex /* Server ist nicht konfiguriert! */
81 2ee05c9a 2002-03-03 alex Log( LOG_ERR, "Connection %d: Server \"%s\" not configured here!", Client_Conn( Client ), Req->argv[0] );
82 8adff592 2005-03-19 fw Conn_Close( Client_Conn( Client ), NULL, "Server not configured here", true);
83 2ee05c9a 2002-03-03 alex return DISCONNECTED;
85 b2615bcc 2002-11-19 alex if( strcmp( Client_Password( Client ), Conf_Server[i].pwd_in ) != 0 )
87 2ee05c9a 2002-03-03 alex /* Falsches Passwort */
88 b2615bcc 2002-11-19 alex Log( LOG_ERR, "Connection %d: Got bad password from server \"%s\"!", Client_Conn( Client ), Req->argv[0] );
89 8adff592 2005-03-19 fw Conn_Close( Client_Conn( Client ), NULL, "Bad password", true);
90 2ee05c9a 2002-03-03 alex return DISCONNECTED;
93 2ee05c9a 2002-03-03 alex /* Ist ein Server mit dieser ID bereits registriert? */
94 2ee05c9a 2002-03-03 alex if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
96 2ee05c9a 2002-03-03 alex /* Server-Strukturen fuellen ;-) */
97 2ee05c9a 2002-03-03 alex Client_SetID( Client, Req->argv[0] );
98 2ee05c9a 2002-03-03 alex Client_SetHops( Client, 1 );
99 2ee05c9a 2002-03-03 alex Client_SetInfo( Client, Req->argv[Req->argc - 1] );
101 2310ac2c 2002-04-08 alex /* Meldet sich der Server bei uns an (d.h., bauen nicht wir
102 2310ac2c 2002-04-08 alex * selber die Verbindung zu einem anderen Server auf)? */
103 65f3adca 2002-11-26 alex con = Client_Conn( Client );
104 2310ac2c 2002-04-08 alex if( Client_Token( Client ) != TOKEN_OUTBOUND )
106 2310ac2c 2002-04-08 alex /* Eingehende Verbindung: Unseren SERVER- und PASS-Befehl senden */
108 8adff592 2005-03-19 fw if( ! IRC_WriteStrClient( Client, "PASS %s %s", Conf_Server[i].pwd_out, NGIRCd_ProtoID )) ok = false;
109 2ee05c9a 2002-03-03 alex else ok = IRC_WriteStrClient( Client, "SERVER %s 1 :%s", Conf_ServerName, Conf_ServerInfo );
112 8adff592 2005-03-19 fw Conn_Close( con, "Unexpected server behavior!", NULL, false );
113 2ee05c9a 2002-03-03 alex return DISCONNECTED;
115 2ee05c9a 2002-03-03 alex Client_SetIntroducer( Client, Client );
116 2ee05c9a 2002-03-03 alex Client_SetToken( Client, 1 );
120 2310ac2c 2002-04-08 alex /* Ausgehende verbindung, SERVER und PASS wurden von uns bereits
121 2310ac2c 2002-04-08 alex * an die Gegenseite uerbermittelt */
122 2310ac2c 2002-04-08 alex Client_SetToken( Client, atoi( Req->argv[1] ));
125 47ca178a 2007-11-21 alex /* Mark this connection as belonging to an configured server */
126 47ca178a 2007-11-21 alex Conf_SetServer(i, con);
128 a60465be 2008-09-23 alex /* Check protocol level */
129 a60465be 2008-09-23 alex if (Client_Type(Client) == CLIENT_GOTPASS) {
130 a60465be 2008-09-23 alex /* We got a "simple" PASS command, so the peer is
131 a60465be 2008-09-23 alex * using the protocol as defined in RFC 1459. */
132 a60465be 2008-09-23 alex if (! (Conn_Options(con) & CONN_RFC1459))
133 a60465be 2008-09-23 alex Log(LOG_INFO,
134 a60465be 2008-09-23 alex "Switching connection %d (\"%s\") to RFC 1459 compatibility mode.",
135 a60465be 2008-09-23 alex con, Client_ID(Client));
136 a60465be 2008-09-23 alex Conn_SetOption(con, CONN_RFC1459);
139 47ca178a 2007-11-21 alex Client_SetType(Client, CLIENT_UNKNOWNSERVER);
141 c40592d2 2003-12-26 alex #ifdef ZLIB
142 65f3adca 2002-11-26 alex /* Kompression initialisieren, wenn erforderlich */
143 65f3adca 2002-11-26 alex if( strchr( Client_Flags( Client ), 'Z' ))
145 437f2c33 2003-12-30 alex if( ! Zip_InitConn( con ))
147 65f3adca 2002-11-26 alex /* Fehler! */
148 8adff592 2005-03-19 fw Conn_Close( con, "Can't inizialize compression (zlib)!", NULL, false );
149 65f3adca 2002-11-26 alex return DISCONNECTED;
154 3dc16212 2002-09-07 alex #ifdef IRCPLUS
155 47ca178a 2007-11-21 alex if (strchr(Client_Flags(Client), 'H')) {
156 47ca178a 2007-11-21 alex LogDebug("Peer supports IRC+ extended server handshake ...");
157 47ca178a 2007-11-21 alex if (!IRC_Send_ISUPPORT(Client))
158 47ca178a 2007-11-21 alex return DISCONNECTED;
159 47ca178a 2007-11-21 alex return IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG,
160 47ca178a 2007-11-21 alex Client_ID(Client));
163 47ca178a 2007-11-21 alex if (Conf_MaxNickLength != CLIENT_NICK_LEN_DEFAULT)
164 47ca178a 2007-11-21 alex Log(LOG_CRIT,
165 47ca178a 2007-11-21 alex "Attention: this server uses a non-standard nick length, but the peer doesn't support the IRC+ extended server handshake!");
166 dd598200 2004-04-25 alex #ifdef IRCPLUS
170 47ca178a 2007-11-21 alex return IRC_Num_ENDOFMOTD(Client, Req);
172 2ee05c9a 2002-03-03 alex else if( Client_Type( Client ) == CLIENT_SERVER )
174 2ee05c9a 2002-03-03 alex /* Neuer Server wird im Netz angekuendigt */
176 2ee05c9a 2002-03-03 alex /* Falsche Anzahl Parameter? */
177 2ee05c9a 2002-03-03 alex if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
179 2ee05c9a 2002-03-03 alex /* Ist ein Server mit dieser ID bereits registriert? */
180 2ee05c9a 2002-03-03 alex if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
182 2ee05c9a 2002-03-03 alex /* Ueberfluessige Hostnamen aus Info-Text entfernen */
183 2ee05c9a 2002-03-03 alex ptr = strchr( Req->argv[3] + 2, '[' );
184 2ee05c9a 2002-03-03 alex if( ! ptr ) ptr = Req->argv[3];
186 bc4ed226 2002-03-25 alex from = Client_Search( Req->prefix );
187 2ee05c9a 2002-03-03 alex if( ! from )
189 2ee05c9a 2002-03-03 alex /* Hm, Server, der diesen einfuehrt, ist nicht bekannt!? */
190 2ee05c9a 2002-03-03 alex Log( LOG_ALERT, "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)", Req->prefix, Client_Conn( Client ));
191 8adff592 2005-03-19 fw Conn_Close( Client_Conn( Client ), NULL, "Unknown ID in prefix of SERVER", true);
192 2ee05c9a 2002-03-03 alex return DISCONNECTED;
195 2ee05c9a 2002-03-03 alex /* Neue Client-Struktur anlegen */
196 8adff592 2005-03-19 fw c = Client_NewRemoteServer( Client, Req->argv[0], from, atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr, true);
199 2ee05c9a 2002-03-03 alex /* Neue Client-Struktur konnte nicht angelegt werden */
200 2ee05c9a 2002-03-03 alex Log( LOG_ALERT, "Can't create client structure for server! (on connection %d)", Client_Conn( Client ));
201 8adff592 2005-03-19 fw Conn_Close( Client_Conn( Client ), NULL, "Can't allocate client structure for remote server", true);
202 2ee05c9a 2002-03-03 alex return DISCONNECTED;
205 2ee05c9a 2002-03-03 alex /* Log-Meldung zusammenbauen und ausgeben */
206 b316c380 2002-12-26 alex if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) snprintf( str, sizeof( str ), "connected to %s, ", Client_ID( from ));
207 2ee05c9a 2002-03-03 alex else strcpy( str, "" );
208 6b58ab84 2002-03-27 alex 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": "" );
210 2ee05c9a 2002-03-03 alex /* Andere Server informieren */
211 2ee05c9a 2002-03-03 alex IRC_WriteStrServersPrefix( Client, from, "SERVER %s %d %d :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_MyToken( c ), Client_Info( c ));
213 2ee05c9a 2002-03-03 alex return CONNECTED;
215 47ca178a 2007-11-21 alex return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
216 47ca178a 2007-11-21 alex Client_ID(Client), Req->command);
217 2ee05c9a 2002-03-03 alex } /* IRC_SERVER */
221 c2f60abe 2002-05-27 alex IRC_NJOIN( CLIENT *Client, REQUEST *Req )
223 8adff592 2005-03-19 fw char nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
224 8adff592 2005-03-19 fw bool is_op, is_voiced;
225 2ee05c9a 2002-03-03 alex CHANNEL *chan;
228 2ee05c9a 2002-03-03 alex assert( Client != NULL );
229 2ee05c9a 2002-03-03 alex assert( Req != NULL );
231 2ee05c9a 2002-03-03 alex /* Falsche Anzahl Parameter? */
232 2ee05c9a 2002-03-03 alex if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
234 feafb6cb 2003-04-20 alex strlcpy( nick_in, Req->argv[1], sizeof( nick_in ));
235 feafb6cb 2003-04-20 alex strcpy( nick_out, "" );
237 2ee05c9a 2002-03-03 alex channame = Req->argv[0];
238 feafb6cb 2003-04-20 alex ptr = strtok( nick_in, "," );
239 2ee05c9a 2002-03-03 alex while( ptr )
241 8adff592 2005-03-19 fw is_op = is_voiced = false;
243 2ee05c9a 2002-03-03 alex /* Prefixe abschneiden */
244 2ee05c9a 2002-03-03 alex while(( *ptr == '@' ) || ( *ptr == '+' ))
246 8adff592 2005-03-19 fw if( *ptr == '@' ) is_op = true;
247 8adff592 2005-03-19 fw if( *ptr == '+' ) is_voiced = true;
251 bc4ed226 2002-03-25 alex c = Client_Search( ptr );
254 2ee05c9a 2002-03-03 alex Channel_Join( c, channame );
255 2ee05c9a 2002-03-03 alex chan = Channel_Search( channame );
256 2ee05c9a 2002-03-03 alex assert( chan != NULL );
258 2ee05c9a 2002-03-03 alex if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
259 2ee05c9a 2002-03-03 alex if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
261 2ee05c9a 2002-03-03 alex /* im Channel bekannt machen */
262 8adff592 2005-03-19 fw IRC_WriteStrChannelPrefix( Client, chan, c, false, "JOIN :%s", channame );
264 2ee05c9a 2002-03-03 alex /* Channel-User-Modes setzen */
265 695631b2 2002-12-26 alex strlcpy( modes, Channel_UserModes( chan, c ), sizeof( modes ));
266 2ee05c9a 2002-03-03 alex if( modes[0] )
268 2ee05c9a 2002-03-03 alex /* Modes im Channel bekannt machen */
269 8adff592 2005-03-19 fw IRC_WriteStrChannelPrefix( Client, chan, Client, false, "MODE %s +%s %s", channame, modes, Client_ID( c ));
272 feafb6cb 2003-04-20 alex if( nick_out[0] != '\0' ) strlcat( nick_out, ",", sizeof( nick_out ));
273 6647da58 2003-07-09 alex if( is_op ) strlcat( nick_out, "@", sizeof( nick_out ));
274 6647da58 2003-07-09 alex if( is_voiced ) strlcat( nick_out, "+", sizeof( nick_out ));
275 feafb6cb 2003-04-20 alex strlcat( nick_out, ptr, sizeof( nick_out ));
277 7d30c8ce 2002-08-26 alex else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
279 2ee05c9a 2002-03-03 alex /* naechsten Nick suchen */
280 2ee05c9a 2002-03-03 alex ptr = strtok( NULL, "," );
283 2ee05c9a 2002-03-03 alex /* an andere Server weiterleiten */
284 feafb6cb 2003-04-20 alex if( nick_out[0] != '\0' ) IRC_WriteStrServersPrefix( Client, Client_ThisServer( ), "NJOIN %s :%s", Req->argv[0], nick_out );
286 2ee05c9a 2002-03-03 alex return CONNECTED;
287 2ee05c9a 2002-03-03 alex } /* IRC_NJOIN */
291 c2f60abe 2002-05-27 alex IRC_SQUIT( CLIENT *Client, REQUEST *Req )
293 2ee05c9a 2002-03-03 alex CLIENT *target;
294 8adff592 2005-03-19 fw char msg[LINE_LEN + 64];
296 2ee05c9a 2002-03-03 alex assert( Client != NULL );
297 2ee05c9a 2002-03-03 alex assert( Req != NULL );
299 2ee05c9a 2002-03-03 alex /* Falsche Anzahl Parameter? */
300 2ee05c9a 2002-03-03 alex if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
302 2ee05c9a 2002-03-03 alex Log( LOG_DEBUG, "Got SQUIT from %s for \"%s\": \"%s\" ...", Client_ID( Client ), Req->argv[0], Req->argv[1] );
304 bc4ed226 2002-03-25 alex target = Client_Search( Req->argv[0] );
305 2ee05c9a 2002-03-03 alex if( ! target )
307 9fc7e66f 2002-03-11 alex /* Den Server kennen wir nicht (mehr), also nichts zu tun. */
308 f7a0ff1f 2002-03-11 alex Log( LOG_WARNING, "Got SQUIT from %s for unknown server \"%s\"!?", Client_ID( Client ), Req->argv[0] );
309 2ee05c9a 2002-03-03 alex return CONNECTED;
312 2ee05c9a 2002-03-03 alex if( Req->argv[1][0] )
314 2ee05c9a 2002-03-03 alex if( strlen( Req->argv[1] ) > LINE_LEN ) Req->argv[1][LINE_LEN] = '\0';
315 b316c380 2002-12-26 alex snprintf( msg, sizeof( msg ), "%s (SQUIT from %s).", Req->argv[1], Client_ID( Client ));
317 b316c380 2002-12-26 alex else snprintf( msg, sizeof( msg ), "Got SQUIT from %s.", Client_ID( Client ));
319 2ee05c9a 2002-03-03 alex if( Client_Conn( target ) > NONE )
321 2ee05c9a 2002-03-03 alex /* dieser Server hat die Connection */
322 8adff592 2005-03-19 fw if( Req->argv[1][0] ) Conn_Close( Client_Conn( target ), msg, Req->argv[1], true);
323 8adff592 2005-03-19 fw else Conn_Close( Client_Conn( target ), msg, NULL, true);
324 2ee05c9a 2002-03-03 alex return DISCONNECTED;
328 2ee05c9a 2002-03-03 alex /* Verbindung hielt anderer Server */
329 8adff592 2005-03-19 fw Client_Destroy( target, msg, Req->argv[1], false );
330 2ee05c9a 2002-03-03 alex return CONNECTED;
332 2ee05c9a 2002-03-03 alex } /* IRC_SQUIT */
335 2ee05c9a 2002-03-03 alex /* -eof- */