3fc0e2dc1e84f2032e1ad0aa4f1c70cc82b14e93
[ngircd-alex.git] / src / ngircd / irc-server.c
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
14
15 #include "portab.h"
16
17 static char UNUSED id[] = "$Id: irc-server.c,v 1.45 2007/11/20 20:02:41 alex Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <strings.h>
25
26 #include "defines.h"
27 #include "resolve.h"
28 #include "conn.h"
29 #include "conn-zip.h"
30 #include "conf.h"
31 #include "client.h"
32 #include "channel.h"
33 #include "irc-write.h"
34 #include "lists.h"
35 #include "log.h"
36 #include "messages.h"
37 #include "parse.h"
38 #include "ngircd.h"
39
40 #include "exp.h"
41 #include "irc-server.h"
42
43
44 #ifdef IRCPLUS
45 static bool
46 Synchronize_Lists( CLIENT *Client )
47 {
48         CHANNEL *c;
49         struct list_head *head;
50         struct list_elem *elem;
51
52         assert( Client != NULL );
53
54         c = Channel_First();
55
56         while (c) {
57                 head = Channel_GetListBans(c);
58
59                 elem = Lists_GetFirst(head);
60                 while (elem) {
61                         if( ! IRC_WriteStrClient( Client, "MODE %s +b %s",
62                                         Channel_Name(c), Lists_GetMask(elem)))
63                         {
64                                 return false;
65                         }
66                         elem = Lists_GetNext(elem);
67                 }
68
69                 head = Channel_GetListInvites(c);
70                 elem = Lists_GetFirst(head);
71                 while (elem) {
72                         if( ! IRC_WriteStrClient( Client, "MODE %s +I %s",
73                                         Channel_Name( c ), Lists_GetMask(elem)))
74                         {
75                                 return false;
76                         }
77                         elem = Lists_GetNext(elem);
78                 }
79                 c = Channel_Next(c);
80         }
81         return true;
82 }
83 #endif
84
85
86 /**
87  * Handler for the IRC command "SERVER".
88  * See RFC 2813 section 4.1.2.
89  */
90 GLOBAL bool
91 IRC_SERVER( CLIENT *Client, REQUEST *Req )
92 {
93         char str[LINE_LEN], *ptr, *modes, *topic;
94         CLIENT *from, *c, *cl;
95         CL2CHAN *cl2chan;
96         int max_hops, i;
97         CHANNEL *chan;
98         bool ok;
99         CONN_ID con;
100         
101         assert( Client != NULL );
102         assert( Req != NULL );
103
104         /* Return an error if this is not a local client */
105         if (Client_Conn(Client) <= NONE)
106                 return IRC_WriteStrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
107                                           Client_ID(Client), Req->command);
108
109         if (Client_Type(Client) == CLIENT_GOTPASS) {
110                 /* We got a PASS command from the peer, and now a SERVER
111                  * command: the peer tries to register itself as a server. */
112                 LogDebug("Connection %d: got SERVER command (new server link) ...",
113                         Client_Conn(Client));
114
115                 /* Falsche Anzahl Parameter? */
116                 if(( Req->argc != 2 ) && ( Req->argc != 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
117
118                 /* Ist dieser Server bei uns konfiguriert? */
119                 for( i = 0; i < MAX_SERVERS; i++ ) if( strcasecmp( Req->argv[0], Conf_Server[i].name ) == 0 ) break;
120                 if( i >= MAX_SERVERS )
121                 {
122                         /* Server ist nicht konfiguriert! */
123                         Log( LOG_ERR, "Connection %d: Server \"%s\" not configured here!", Client_Conn( Client ), Req->argv[0] );
124                         Conn_Close( Client_Conn( Client ), NULL, "Server not configured here", true);
125                         return DISCONNECTED;
126                 }
127                 if( strcmp( Client_Password( Client ), Conf_Server[i].pwd_in ) != 0 )
128                 {
129                         /* Falsches Passwort */
130                         Log( LOG_ERR, "Connection %d: Got bad password from server \"%s\"!", Client_Conn( Client ), Req->argv[0] );
131                         Conn_Close( Client_Conn( Client ), NULL, "Bad password", true);
132                         return DISCONNECTED;
133                 }
134                 
135                 /* Ist ein Server mit dieser ID bereits registriert? */
136                 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
137
138                 /* Server-Strukturen fuellen ;-) */
139                 Client_SetID( Client, Req->argv[0] );
140                 Client_SetHops( Client, 1 );
141                 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
142
143                 /* Meldet sich der Server bei uns an (d.h., bauen nicht wir
144                  * selber die Verbindung zu einem anderen Server auf)? */
145                 con = Client_Conn( Client );
146                 if( Client_Token( Client ) != TOKEN_OUTBOUND )
147                 {
148                         /* Eingehende Verbindung: Unseren SERVER- und PASS-Befehl senden */
149                         ok = true;
150                         if( ! IRC_WriteStrClient( Client, "PASS %s %s", Conf_Server[i].pwd_out, NGIRCd_ProtoID )) ok = false;
151                         else ok = IRC_WriteStrClient( Client, "SERVER %s 1 :%s", Conf_ServerName, Conf_ServerInfo );
152                         if( ! ok )
153                         {
154                                 Conn_Close( con, "Unexpected server behavior!", NULL, false );
155                                 return DISCONNECTED;
156                         }
157                         Client_SetIntroducer( Client, Client );
158                         Client_SetToken( Client, 1 );
159                 }
160                 else
161                 {
162                         /* Ausgehende verbindung, SERVER und PASS wurden von uns bereits
163                          * an die Gegenseite uerbermittelt */
164                         Client_SetToken( Client, atoi( Req->argv[1] ));
165                 }
166
167                 Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" registered (connection %d, 1 hop - direct link).", Client_ID( Client ), con );
168
169                 Client_SetType( Client, CLIENT_SERVER );
170                 Conf_SetServer( i, con );
171
172 #ifdef ZLIB
173                 /* Kompression initialisieren, wenn erforderlich */
174                 if( strchr( Client_Flags( Client ), 'Z' ))
175                 {
176                         if( ! Zip_InitConn( con ))
177                         {
178                                 /* Fehler! */
179                                 Conn_Close( con, "Can't inizialize compression (zlib)!", NULL, false );
180                                 return DISCONNECTED;
181                         }
182                 }
183 #endif
184
185                 /* maximalen Hop Count ermitteln */
186                 max_hops = 0;
187                 c = Client_First( );
188                 while( c )
189                 {
190                         if( Client_Hops( c ) > max_hops ) max_hops = Client_Hops( c );
191                         c = Client_Next( c );
192                 }
193                 
194                 /* Alle bisherigen Server dem neuen Server bekannt machen,
195                  * die bisherigen Server ueber den neuen informierenn */
196                 for( i = 0; i < ( max_hops + 1 ); i++ )
197                 {
198                         c = Client_First( );
199                         while( c )
200                         {
201                                 if(( Client_Type( c ) == CLIENT_SERVER ) && ( c != Client ) && ( c != Client_ThisServer( )) && ( Client_Hops( c ) == i ))
202                                 {
203                                         if( Client_Conn( c ) > NONE )
204                                         {
205                                                 /* Dem gefundenen Server gleich den neuen
206                                                  * Server bekannt machen */
207                                                 if( ! IRC_WriteStrClient( c, "SERVER %s %d %d :%s", Client_ID( Client ), Client_Hops( Client ) + 1, Client_MyToken( Client ), Client_Info( Client ))) return DISCONNECTED;
208                                         }
209                                         
210                                         /* Inform the new server about this one */
211                                         if (! IRC_WriteStrClientPrefix(Client,
212                                                         Client_Hops(c) == 1 ? Client_ThisServer() : Client_TopServer(c),
213                                                         "SERVER %s %d %d :%s",
214                                                         Client_ID(c), Client_Hops(c) + 1,
215                                                         Client_MyToken(c), Client_Info(c)))
216                                                 return DISCONNECTED;
217                                 }
218                                 c = Client_Next( c );
219                         }
220                 }
221
222                 /* alle User dem neuen Server bekannt machen */
223                 c = Client_First( );
224                 while( c )
225                 {
226                         if( Client_Type( c ) == CLIENT_USER )
227                         {
228                                 /* User an neuen Server melden */
229                                 if( ! IRC_WriteStrClient( Client, "NICK %s %d %s %s %d +%s :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_User( c ), Client_Hostname( c ), Client_MyToken( Client_Introducer( c )), Client_Modes( c ), Client_Info( c ))) return DISCONNECTED;
230                         }
231                         c = Client_Next( c );
232                 }
233
234                 /* Channels dem neuen Server bekannt machen */
235                 chan = Channel_First( );
236                 while( chan )
237                 {
238 #ifdef IRCPLUS
239                         /* Send CHANINFO if the peer supports it */
240                         if( strchr( Client_Flags( Client ), 'C' ))
241                         {
242 #ifdef DEBUG
243                                 Log( LOG_DEBUG, "Sending CHANINFO commands ..." );
244 #endif
245                                 modes = Channel_Modes( chan );
246                                 topic = Channel_Topic( chan );
247
248                                 if( *modes || *topic )
249                                 {
250                                         /* send CHANINFO */
251                                         if(( ! strchr( Channel_Modes( chan ), 'k' )) && ( ! strchr( Channel_Modes( chan ), 'l' )) && ( ! *topic ))
252                                         {
253                                                 /* "CHANINFO <chan> +<modes>" */
254                                                 if( ! IRC_WriteStrClient( Client, "CHANINFO %s +%s", Channel_Name( chan ), modes )) return DISCONNECTED;
255                                         }
256                                         else if(( ! strchr( Channel_Modes( chan ), 'k' )) && ( ! strchr( Channel_Modes( chan ), 'l' )))
257                                         {
258                                                 /* "CHANINFO <chan> +<modes> :<topic>" */
259                                                 if( ! IRC_WriteStrClient( Client, "CHANINFO %s +%s :%s", Channel_Name( chan ), modes, topic )) return DISCONNECTED;
260                                         }
261                                         else
262                                         {
263                                                 /* "CHANINFO <chan> +<modes> <key> <limit> :<topic>" */
264                                                 if( ! IRC_WriteStrClient( Client, "CHANINFO %s +%s %s %lu :%s",
265                                                         Channel_Name( chan ), modes,
266                                                         strchr( Channel_Modes( chan ), 'k' ) ? Channel_Key( chan ) : "*",
267                                                         strchr( Channel_Modes( chan ), 'l' ) ? Channel_MaxUsers( chan ) : 0, topic ))
268                                                 {
269                                                         return DISCONNECTED;
270                                                 }
271                                         }
272                                 }
273                         }
274 #endif
275
276                         /* alle Member suchen */
277                         cl2chan = Channel_FirstMember( chan );
278                         snprintf( str, sizeof( str ), "NJOIN %s :", Channel_Name( chan ));
279                         while( cl2chan )
280                         {
281                                 cl = Channel_GetClient( cl2chan );
282                                 assert( cl != NULL );
283
284                                 /* Nick, ggf. mit Modes, anhaengen */
285                                 if( str[strlen( str ) - 1] != ':' ) strlcat( str, ",", sizeof( str ));
286                                 if( strchr( Channel_UserModes( chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
287                                 if( strchr( Channel_UserModes( chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
288                                 strlcat( str, Client_ID( cl ), sizeof( str ));
289
290                                 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 8 ))
291                                 {
292                                         /* Zeile senden */
293                                         if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
294                                         snprintf( str, sizeof( str ), "NJOIN %s :", Channel_Name( chan ));
295                                 }
296                                 
297                                 cl2chan = Channel_NextMember( chan, cl2chan );
298                         }
299
300                         /* noch Daten da? */
301                         if( str[strlen( str ) - 1] != ':')
302                         {
303                                 /* Ja; Also senden ... */
304                                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
305                         }
306
307                         /* Get next channel ... */
308                         chan = Channel_Next(chan);
309                 }
310
311 #ifdef IRCPLUS
312                 if (strchr(Client_Flags(Client), 'L')) {
313 #ifdef DEBUG
314                         Log(LOG_DEBUG,
315                             "Synchronizing INVITE- and BAN-lists ...");
316 #endif
317                         /* Synchronize INVITE- and BAN-lists */
318                         if (!Synchronize_Lists(Client))
319                                 return DISCONNECTED;
320                 }
321 #endif
322
323                 return CONNECTED;
324         }
325         else if( Client_Type( Client ) == CLIENT_SERVER )
326         {
327                 /* Neuer Server wird im Netz angekuendigt */
328
329                 /* Falsche Anzahl Parameter? */
330                 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
331
332                 /* Ist ein Server mit dieser ID bereits registriert? */
333                 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
334
335                 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
336                 ptr = strchr( Req->argv[3] + 2, '[' );
337                 if( ! ptr ) ptr = Req->argv[3];
338
339                 from = Client_Search( Req->prefix );
340                 if( ! from )
341                 {
342                         /* Hm, Server, der diesen einfuehrt, ist nicht bekannt!? */
343                         Log( LOG_ALERT, "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)", Req->prefix, Client_Conn( Client ));
344                         Conn_Close( Client_Conn( Client ), NULL, "Unknown ID in prefix of SERVER", true);
345                         return DISCONNECTED;
346                 }
347
348                 /* Neue Client-Struktur anlegen */
349                 c = Client_NewRemoteServer( Client, Req->argv[0], from, atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr, true);
350                 if( ! c )
351                 {
352                         /* Neue Client-Struktur konnte nicht angelegt werden */
353                         Log( LOG_ALERT, "Can't create client structure for server! (on connection %d)", Client_Conn( Client ));
354                         Conn_Close( Client_Conn( Client ), NULL, "Can't allocate client structure for remote server", true);
355                         return DISCONNECTED;
356                 }
357
358                 /* Log-Meldung zusammenbauen und ausgeben */
359                 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) snprintf( str, sizeof( str ), "connected to %s, ", Client_ID( from ));
360                 else strcpy( str, "" );
361                 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": "" );
362
363                 /* Andere Server informieren */
364                 IRC_WriteStrServersPrefix( Client, from, "SERVER %s %d %d :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_MyToken( c ), Client_Info( c ));
365
366                 return CONNECTED;
367         }
368         else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
369 } /* IRC_SERVER */
370
371
372 GLOBAL bool
373 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
374 {
375         char nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
376         bool is_op, is_voiced;
377         CHANNEL *chan;
378         CLIENT *c;
379         
380         assert( Client != NULL );
381         assert( Req != NULL );
382
383         /* Falsche Anzahl Parameter? */
384         if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
385
386         strlcpy( nick_in, Req->argv[1], sizeof( nick_in ));
387         strcpy( nick_out, "" );
388
389         channame = Req->argv[0];
390         ptr = strtok( nick_in, "," );
391         while( ptr )
392         {
393                 is_op = is_voiced = false;
394                 
395                 /* Prefixe abschneiden */
396                 while(( *ptr == '@' ) || ( *ptr == '+' ))
397                 {
398                         if( *ptr == '@' ) is_op = true;
399                         if( *ptr == '+' ) is_voiced = true;
400                         ptr++;
401                 }
402
403                 c = Client_Search( ptr );
404                 if( c )
405                 {
406                         Channel_Join( c, channame );
407                         chan = Channel_Search( channame );
408                         assert( chan != NULL );
409                         
410                         if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
411                         if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
412
413                         /* im Channel bekannt machen */
414                         IRC_WriteStrChannelPrefix( Client, chan, c, false, "JOIN :%s", channame );
415
416                         /* Channel-User-Modes setzen */
417                         strlcpy( modes, Channel_UserModes( chan, c ), sizeof( modes ));
418                         if( modes[0] )
419                         {
420                                 /* Modes im Channel bekannt machen */
421                                 IRC_WriteStrChannelPrefix( Client, chan, Client, false, "MODE %s +%s %s", channame, modes, Client_ID( c ));
422                         }
423
424                         if( nick_out[0] != '\0' ) strlcat( nick_out, ",", sizeof( nick_out ));
425                         if( is_op ) strlcat( nick_out, "@", sizeof( nick_out ));
426                         if( is_voiced ) strlcat( nick_out, "+", sizeof( nick_out ));
427                         strlcat( nick_out, ptr, sizeof( nick_out ));
428                 }
429                 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
430                 
431                 /* naechsten Nick suchen */
432                 ptr = strtok( NULL, "," );
433         }
434
435         /* an andere Server weiterleiten */
436         if( nick_out[0] != '\0' ) IRC_WriteStrServersPrefix( Client, Client_ThisServer( ), "NJOIN %s :%s", Req->argv[0], nick_out );
437
438         return CONNECTED;
439 } /* IRC_NJOIN */
440
441
442 GLOBAL bool
443 IRC_SQUIT( CLIENT *Client, REQUEST *Req )
444 {
445         CLIENT *target;
446         char msg[LINE_LEN + 64];
447
448         assert( Client != NULL );
449         assert( Req != NULL );
450
451         /* Falsche Anzahl Parameter? */
452         if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
453
454         Log( LOG_DEBUG, "Got SQUIT from %s for \"%s\": \"%s\" ...", Client_ID( Client ), Req->argv[0], Req->argv[1] );
455
456         target = Client_Search( Req->argv[0] );
457         if( ! target )
458         {
459                 /* Den Server kennen wir nicht (mehr), also nichts zu tun. */
460                 Log( LOG_WARNING, "Got SQUIT from %s for unknown server \"%s\"!?", Client_ID( Client ), Req->argv[0] );
461                 return CONNECTED;
462         }
463
464         if( Req->argv[1][0] )
465         {
466                 if( strlen( Req->argv[1] ) > LINE_LEN ) Req->argv[1][LINE_LEN] = '\0';
467                 snprintf( msg, sizeof( msg ), "%s (SQUIT from %s).", Req->argv[1], Client_ID( Client ));
468         }
469         else snprintf( msg, sizeof( msg ), "Got SQUIT from %s.", Client_ID( Client ));
470
471         if( Client_Conn( target ) > NONE )
472         {
473                 /* dieser Server hat die Connection */
474                 if( Req->argv[1][0] ) Conn_Close( Client_Conn( target ), msg, Req->argv[1], true);
475                 else Conn_Close( Client_Conn( target ), msg, NULL, true);
476                 return DISCONNECTED;
477         }
478         else
479         {
480                 /* Verbindung hielt anderer Server */
481                 Client_Destroy( target, msg, Req->argv[1], false );
482                 return CONNECTED;
483         }
484 } /* IRC_SQUIT */
485
486
487 /* -eof- */