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