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