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