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