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