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