]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-server.c
Introduce option to configure the maximum nick name lenth in ngircd.conf
[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.46 2007/11/21 12:16:36 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 "numeric.h"
39 #include "ngircd.h"
40 #include "irc-info.h"
41
42 #include "exp.h"
43 #include "irc-server.h"
44
45
46 /**
47  * Handler for the IRC command "SERVER".
48  * See RFC 2813 section 4.1.2.
49  */
50 GLOBAL bool
51 IRC_SERVER( CLIENT *Client, REQUEST *Req )
52 {
53         char str[LINE_LEN], *ptr;
54         CLIENT *from, *c;
55         bool ok;
56         int i;
57         CONN_ID con;
58         
59         assert( Client != NULL );
60         assert( Req != NULL );
61
62         /* Return an error if this is not a local client */
63         if (Client_Conn(Client) <= NONE)
64                 return IRC_WriteStrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
65                                           Client_ID(Client), Req->command);
66
67         if (Client_Type(Client) == CLIENT_GOTPASS) {
68                 /* We got a PASS command from the peer, and now a SERVER
69                  * command: the peer tries to register itself as a server. */
70                 LogDebug("Connection %d: got SERVER command (new server link) ...",
71                         Client_Conn(Client));
72
73                 /* Falsche Anzahl Parameter? */
74                 if(( Req->argc != 2 ) && ( Req->argc != 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
75
76                 /* Ist dieser Server bei uns konfiguriert? */
77                 for( i = 0; i < MAX_SERVERS; i++ ) if( strcasecmp( Req->argv[0], Conf_Server[i].name ) == 0 ) break;
78                 if( i >= MAX_SERVERS )
79                 {
80                         /* Server ist nicht konfiguriert! */
81                         Log( LOG_ERR, "Connection %d: Server \"%s\" not configured here!", Client_Conn( Client ), Req->argv[0] );
82                         Conn_Close( Client_Conn( Client ), NULL, "Server not configured here", true);
83                         return DISCONNECTED;
84                 }
85                 if( strcmp( Client_Password( Client ), Conf_Server[i].pwd_in ) != 0 )
86                 {
87                         /* Falsches Passwort */
88                         Log( LOG_ERR, "Connection %d: Got bad password from server \"%s\"!", Client_Conn( Client ), Req->argv[0] );
89                         Conn_Close( Client_Conn( Client ), NULL, "Bad password", true);
90                         return DISCONNECTED;
91                 }
92                 
93                 /* Ist ein Server mit dieser ID bereits registriert? */
94                 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
95
96                 /* Server-Strukturen fuellen ;-) */
97                 Client_SetID( Client, Req->argv[0] );
98                 Client_SetHops( Client, 1 );
99                 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
100
101                 /* Meldet sich der Server bei uns an (d.h., bauen nicht wir
102                  * selber die Verbindung zu einem anderen Server auf)? */
103                 con = Client_Conn( Client );
104                 if( Client_Token( Client ) != TOKEN_OUTBOUND )
105                 {
106                         /* Eingehende Verbindung: Unseren SERVER- und PASS-Befehl senden */
107                         ok = true;
108                         if( ! IRC_WriteStrClient( Client, "PASS %s %s", Conf_Server[i].pwd_out, NGIRCd_ProtoID )) ok = false;
109                         else ok = IRC_WriteStrClient( Client, "SERVER %s 1 :%s", Conf_ServerName, Conf_ServerInfo );
110                         if( ! ok )
111                         {
112                                 Conn_Close( con, "Unexpected server behavior!", NULL, false );
113                                 return DISCONNECTED;
114                         }
115                         Client_SetIntroducer( Client, Client );
116                         Client_SetToken( Client, 1 );
117                 }
118                 else
119                 {
120                         /* Ausgehende verbindung, SERVER und PASS wurden von uns bereits
121                          * an die Gegenseite uerbermittelt */
122                         Client_SetToken( Client, atoi( Req->argv[1] ));
123                 }
124
125                 /* Mark this connection as belonging to an configured server */
126                 Conf_SetServer(i, con);
127                 
128                 Client_SetType(Client, CLIENT_UNKNOWNSERVER);
129
130 #ifdef ZLIB
131                 /* Kompression initialisieren, wenn erforderlich */
132                 if( strchr( Client_Flags( Client ), 'Z' ))
133                 {
134                         if( ! Zip_InitConn( con ))
135                         {
136                                 /* Fehler! */
137                                 Conn_Close( con, "Can't inizialize compression (zlib)!", NULL, false );
138                                 return DISCONNECTED;
139                         }
140                 }
141 #endif
142
143 #ifdef IRCPLUS
144                 if (strchr(Client_Flags(Client), 'H')) {
145                         LogDebug("Peer supports IRC+ extended server handshake ...");
146                         if (!IRC_Send_ISUPPORT(Client))
147                                 return DISCONNECTED;
148                         return IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG,
149                                                   Client_ID(Client));
150                 } else {
151 #endif
152                         if (Conf_MaxNickLength != CLIENT_NICK_LEN_DEFAULT)
153                                 Log(LOG_CRIT,
154                                     "Attention: this server uses a non-standard nick length, but the peer doesn't support the IRC+ extended server handshake!");
155 #ifdef IRCPLUS
156                 }
157 #endif
158
159                 return IRC_Num_ENDOFMOTD(Client, Req);
160         }
161         else if( Client_Type( Client ) == CLIENT_SERVER )
162         {
163                 /* Neuer Server wird im Netz angekuendigt */
164
165                 /* Falsche Anzahl Parameter? */
166                 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
167
168                 /* Ist ein Server mit dieser ID bereits registriert? */
169                 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
170
171                 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
172                 ptr = strchr( Req->argv[3] + 2, '[' );
173                 if( ! ptr ) ptr = Req->argv[3];
174
175                 from = Client_Search( Req->prefix );
176                 if( ! from )
177                 {
178                         /* Hm, Server, der diesen einfuehrt, ist nicht bekannt!? */
179                         Log( LOG_ALERT, "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)", Req->prefix, Client_Conn( Client ));
180                         Conn_Close( Client_Conn( Client ), NULL, "Unknown ID in prefix of SERVER", true);
181                         return DISCONNECTED;
182                 }
183
184                 /* Neue Client-Struktur anlegen */
185                 c = Client_NewRemoteServer( Client, Req->argv[0], from, atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr, true);
186                 if( ! c )
187                 {
188                         /* Neue Client-Struktur konnte nicht angelegt werden */
189                         Log( LOG_ALERT, "Can't create client structure for server! (on connection %d)", Client_Conn( Client ));
190                         Conn_Close( Client_Conn( Client ), NULL, "Can't allocate client structure for remote server", true);
191                         return DISCONNECTED;
192                 }
193
194                 /* Log-Meldung zusammenbauen und ausgeben */
195                 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) snprintf( str, sizeof( str ), "connected to %s, ", Client_ID( from ));
196                 else strcpy( str, "" );
197                 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": "" );
198
199                 /* Andere Server informieren */
200                 IRC_WriteStrServersPrefix( Client, from, "SERVER %s %d %d :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_MyToken( c ), Client_Info( c ));
201
202                 return CONNECTED;
203         } else
204                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
205                                           Client_ID(Client), Req->command);
206 } /* IRC_SERVER */
207
208
209 GLOBAL bool
210 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
211 {
212         char nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
213         bool is_op, is_voiced;
214         CHANNEL *chan;
215         CLIENT *c;
216         
217         assert( Client != NULL );
218         assert( Req != NULL );
219
220         /* Falsche Anzahl Parameter? */
221         if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
222
223         strlcpy( nick_in, Req->argv[1], sizeof( nick_in ));
224         strcpy( nick_out, "" );
225
226         channame = Req->argv[0];
227         ptr = strtok( nick_in, "," );
228         while( ptr )
229         {
230                 is_op = is_voiced = false;
231                 
232                 /* Prefixe abschneiden */
233                 while(( *ptr == '@' ) || ( *ptr == '+' ))
234                 {
235                         if( *ptr == '@' ) is_op = true;
236                         if( *ptr == '+' ) is_voiced = true;
237                         ptr++;
238                 }
239
240                 c = Client_Search( ptr );
241                 if( c )
242                 {
243                         Channel_Join( c, channame );
244                         chan = Channel_Search( channame );
245                         assert( chan != NULL );
246                         
247                         if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
248                         if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
249
250                         /* im Channel bekannt machen */
251                         IRC_WriteStrChannelPrefix( Client, chan, c, false, "JOIN :%s", channame );
252
253                         /* Channel-User-Modes setzen */
254                         strlcpy( modes, Channel_UserModes( chan, c ), sizeof( modes ));
255                         if( modes[0] )
256                         {
257                                 /* Modes im Channel bekannt machen */
258                                 IRC_WriteStrChannelPrefix( Client, chan, Client, false, "MODE %s +%s %s", channame, modes, Client_ID( c ));
259                         }
260
261                         if( nick_out[0] != '\0' ) strlcat( nick_out, ",", sizeof( nick_out ));
262                         if( is_op ) strlcat( nick_out, "@", sizeof( nick_out ));
263                         if( is_voiced ) strlcat( nick_out, "+", sizeof( nick_out ));
264                         strlcat( nick_out, ptr, sizeof( nick_out ));
265                 }
266                 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
267                 
268                 /* naechsten Nick suchen */
269                 ptr = strtok( NULL, "," );
270         }
271
272         /* an andere Server weiterleiten */
273         if( nick_out[0] != '\0' ) IRC_WriteStrServersPrefix( Client, Client_ThisServer( ), "NJOIN %s :%s", Req->argv[0], nick_out );
274
275         return CONNECTED;
276 } /* IRC_NJOIN */
277
278
279 GLOBAL bool
280 IRC_SQUIT( CLIENT *Client, REQUEST *Req )
281 {
282         CLIENT *target;
283         char msg[LINE_LEN + 64];
284
285         assert( Client != NULL );
286         assert( Req != NULL );
287
288         /* Falsche Anzahl Parameter? */
289         if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
290
291         Log( LOG_DEBUG, "Got SQUIT from %s for \"%s\": \"%s\" ...", Client_ID( Client ), Req->argv[0], Req->argv[1] );
292
293         target = Client_Search( Req->argv[0] );
294         if( ! target )
295         {
296                 /* Den Server kennen wir nicht (mehr), also nichts zu tun. */
297                 Log( LOG_WARNING, "Got SQUIT from %s for unknown server \"%s\"!?", Client_ID( Client ), Req->argv[0] );
298                 return CONNECTED;
299         }
300
301         if( Req->argv[1][0] )
302         {
303                 if( strlen( Req->argv[1] ) > LINE_LEN ) Req->argv[1][LINE_LEN] = '\0';
304                 snprintf( msg, sizeof( msg ), "%s (SQUIT from %s).", Req->argv[1], Client_ID( Client ));
305         }
306         else snprintf( msg, sizeof( msg ), "Got SQUIT from %s.", Client_ID( Client ));
307
308         if( Client_Conn( target ) > NONE )
309         {
310                 /* dieser Server hat die Connection */
311                 if( Req->argv[1][0] ) Conn_Close( Client_Conn( target ), msg, Req->argv[1], true);
312                 else Conn_Close( Client_Conn( target ), msg, NULL, true);
313                 return DISCONNECTED;
314         }
315         else
316         {
317                 /* Verbindung hielt anderer Server */
318                 Client_Destroy( target, msg, Req->argv[1], false );
319                 return CONNECTED;
320         }
321 } /* IRC_SQUIT */
322
323
324 /* -eof- */