2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2022 Alexander Barton (alex@barton.de) and Contributors.
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.
16 * IRC commands for server links
25 #include "conn-func.h"
35 #include "irc-channel.h"
37 #include "irc-write.h"
40 #include "irc-server.h"
43 * Handler for the IRC "SERVER" command.
45 * @param Client The client from which this command has been received.
46 * @param Req Request structure with prefix and all parameters.
47 * @return CONNECTED or DISCONNECTED.
50 IRC_SERVER( CLIENT *Client, REQUEST *Req )
56 assert( Client != NULL );
57 assert( Req != NULL );
59 /* Return an error if this is not a local client */
60 if (Client_Conn(Client) <= NONE)
61 return IRC_WriteErrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
62 Client_ID(Client), Req->command);
64 if (Client_Type(Client) == CLIENT_GOTPASS ||
65 Client_Type(Client) == CLIENT_GOTPASS_2813) {
66 /* We got a PASS command from the peer, and now a SERVER
67 * command: the peer tries to register itself as a server. */
68 LogDebug("Connection %d: got SERVER command (new server link) ...",
71 if (Req->argc != 2 && Req->argc != 3)
72 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
76 /* Get configuration index of new remote server ... */
77 for (i = 0; i < MAX_SERVERS; i++)
78 if (strcasecmp(Req->argv[0], Conf_Server[i].name) == 0)
81 /* Make sure the remote server is configured here */
82 if (i >= MAX_SERVERS) {
84 "Connection %d: Server \"%s\" not configured here!",
85 Client_Conn(Client), Req->argv[0]);
86 Conn_Close(Client_Conn(Client), NULL,
87 "Server not configured here", true);
91 /* Check server password */
92 if (strcmp(Conn_Password(Client_Conn(Client)),
93 Conf_Server[i].pwd_in) != 0) {
95 "Connection %d: Got bad password from server \"%s\"!",
96 Client_Conn(Client), Req->argv[0]);
97 Conn_Close(Client_Conn(Client), NULL,
98 "Bad password", true);
102 /* Is there a registered server with this ID? */
103 if (!Client_CheckID(Client, Req->argv[0]))
106 /* Mark this connection as belonging to an configured server */
107 if (!Conf_SetServer(i, Client_Conn(Client)))
110 Client_SetID( Client, Req->argv[0] );
111 Client_SetHops( Client, 1 );
112 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
114 /* Is this server registering on our side, or are we connecting to
115 * a remote server? */
116 if (Client_Token(Client) != TOKEN_OUTBOUND) {
117 /* Incoming connection, send user/pass */
118 if (!IRC_WriteStrClient(Client, "PASS %s %s",
119 Conf_Server[i].pwd_out,
121 || !IRC_WriteStrClient(Client, "SERVER %s 1 :%s",
124 Conn_Close(Client_Conn(Client),
125 "Unexpected server behavior!",
129 Client_SetIntroducer(Client, Client);
130 Client_SetToken(Client, 1);
132 /* outgoing connect, we already sent a SERVER and PASS
133 * command to the peer */
134 Client_SetToken(Client, atoi(Req->argv[1]));
137 /* Check protocol level */
138 if (Client_Type(Client) == CLIENT_GOTPASS) {
139 /* We got a "simple" PASS command, so the peer is
140 * using the protocol as defined in RFC 1459. */
141 if (! (Conn_Options(Client_Conn(Client)) & CONN_RFC1459))
143 "Switching connection %d (\"%s\") to RFC 1459 compatibility mode.",
144 Client_Conn(Client), Client_ID(Client));
145 Conn_SetOption(Client_Conn(Client), CONN_RFC1459);
148 Client_SetType(Client, CLIENT_UNKNOWNSERVER);
151 if (Client_HasFlag(Client, 'Z')
152 && !Zip_InitConn(Client_Conn(Client))) {
153 Conn_Close(Client_Conn(Client),
154 "Can't initialize compression (zlib)!",
161 if (Client_HasFlag(Client, 'H')) {
162 LogDebug("Peer supports IRC+ extended server handshake ...");
163 if (!IRC_Send_ISUPPORT(Client))
165 return IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG,
169 if (Conf_MaxNickLength != CLIENT_NICK_LEN_DEFAULT)
171 "Attention: this server uses a non-standard nick length, but the peer doesn't support the IRC+ extended server handshake!");
176 return IRC_Num_ENDOFMOTD(Client, Req);
178 else if( Client_Type( Client ) == CLIENT_SERVER )
180 /* New server is being introduced to the network */
183 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
184 Client_ID(Client), Req->command);
186 /* check for existing server with same ID */
187 if (!Client_CheckID(Client, Req->argv[0]))
191 /* We definitely need a prefix here! */
192 Log(LOG_ALERT, "Got SERVER command without prefix! (on connection %d)",
193 Client_Conn(Client));
194 Conn_Close(Client_Conn(Client), NULL,
195 "SERVER command without prefix", true);
199 from = Client_Search( Req->prefix );
201 /* Uh, Server, that introduced the new server is unknown?! */
203 "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)",
204 Req->prefix, Client_Conn(Client));
205 Conn_Close(Client_Conn(Client), NULL,
206 "Unknown ID in prefix of SERVER", true);
210 c = Client_NewRemoteServer(Client, Req->argv[0], from,
211 atoi(Req->argv[1]), atoi(Req->argv[2]),
215 "Can't create client structure for server! (on connection %d)",
216 Client_Conn(Client));
217 Conn_Close(Client_Conn(Client), NULL,
218 "Can't allocate client structure for remote server",
223 if (Client_Hops(c) > 1 && Req->prefix[0])
224 snprintf(str, sizeof(str), "connected to %s, ",
228 Log(LOG_NOTICE|LOG_snotice,
229 "Server \"%s\" registered (via %s, %s%d hop%s).",
230 Client_ID(c), Client_ID(Client), str, Client_Hops(c),
231 Client_Hops(c) > 1 ? "s": "" );
233 /* notify other servers */
234 IRC_WriteStrServersPrefix(Client, from, "SERVER %s %d %d :%s",
235 Client_ID(c), Client_Hops(c) + 1,
236 Client_MyToken(c), Client_Info(c));
240 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
241 Client_ID(Client), Req->command);
245 * Handler for the IRC "NJOIN" command.
247 * @param Client The client from which this command has been received.
248 * @param Req Request structure with prefix and all parameters.
249 * @return CONNECTED or DISCONNECTED.
252 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
254 char nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
255 bool is_owner, is_chanadmin, is_op, is_halfop, is_voiced;
259 assert(Client != NULL);
262 strlcpy(nick_in, Req->argv[1], sizeof(nick_in));
263 strcpy(nick_out, "");
265 channame = Req->argv[0];
267 ptr = strtok(nick_in, ",");
269 is_owner = is_chanadmin = is_op = is_halfop = is_voiced = false;
271 /* cut off prefixes */
272 while ((*ptr == '~') || (*ptr == '&') || (*ptr == '@') ||
273 (*ptr == '%') || (*ptr == '+')) {
287 c = Client_Search(ptr);
289 /* Client not found? */
291 "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!",
296 if (!Channel_Join(c, channame)) {
297 /* Failed to join channel. Ooops!? */
299 "Failed to join client \"%s\" to channel \"%s\" (NJOIN): killing it!",
301 IRC_KillClient(NULL, NULL, ptr, "Internal NJOIN error!");
302 LogDebug("... done.");
306 chan = Channel_Search(channame);
307 assert(chan != NULL);
310 Channel_UserModeAdd(chan, c, 'q');
312 Channel_UserModeAdd(chan, c, 'a');
314 Channel_UserModeAdd(chan, c, 'o');
316 Channel_UserModeAdd(chan, c, 'h');
318 Channel_UserModeAdd(chan, c, 'v');
320 /* Announce client to the channel */
321 IRC_WriteStrChannelPrefix(Client, chan, c, false,
322 "JOIN :%s", channame);
324 /* If the client is connected to this server, it was remotely
325 * joined to the channel by another server/service: So send
326 * TOPIC and NAMES messages like on a regular JOIN command! */
327 if(Client_Conn(c) != NONE)
328 IRC_Send_Channel_Info(c, chan);
330 /* Announce "channel user modes" to the channel, if any */
331 strlcpy(modes, Channel_UserModes(chan, c), sizeof(modes));
333 IRC_WriteStrChannelPrefix(Client, chan, Client, false,
334 "MODE %s +%s %s", channame,
335 modes, Client_ID(c));
337 /* Build nick list for forwarding command */
338 if (nick_out[0] != '\0')
339 strlcat(nick_out, ",", sizeof(nick_out));
341 strlcat(nick_out, "~", sizeof(nick_out));
343 strlcat(nick_out, "&", sizeof(nick_out));
345 strlcat(nick_out, "@", sizeof(nick_out));
347 strlcat(nick_out, "%", sizeof(nick_out));
349 strlcat(nick_out, "+", sizeof(nick_out));
350 strlcat(nick_out, ptr, sizeof(nick_out));
353 /* Get next nick, if any ... */
354 ptr = strtok(NULL, ",");
357 /* forward to other servers */
358 if (nick_out[0] != '\0')
359 IRC_WriteStrServersPrefix(Client, Client_ThisServer(),
360 "NJOIN %s :%s", Req->argv[0], nick_out);
366 * Handler for the IRC "SQUIT" command.
368 * @param Client The client from which this command has been received.
369 * @param Req Request structure with prefix and all parameters.
370 * @return CONNECTED or DISCONNECTED.
373 IRC_SQUIT(CLIENT * Client, REQUEST * Req)
375 char msg[COMMAND_LEN], logmsg[COMMAND_LEN];
376 CLIENT *from, *target;
377 CONN_ID con, client_con;
380 assert(Client != NULL);
383 if (Client_Type(Client) != CLIENT_SERVER
384 && !Client_HasMode(Client, 'o'))
385 return Op_NoPrivileges(Client, Req);
387 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix) {
388 from = Client_Search(Req->prefix);
389 if (Client_Type(from) != CLIENT_SERVER
390 && !Op_Check(Client, Req))
391 return Op_NoPrivileges(Client, Req);
395 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
396 Client_ID(Client), Req->prefix);
398 if (Client_Type(Client) == CLIENT_USER)
399 loglevel = LOG_NOTICE | LOG_snotice;
401 loglevel = LOG_DEBUG;
402 Log(loglevel, "Got SQUIT from %s for \"%s\": \"%s\" ...",
403 Client_ID(from), Req->argv[0], Req->argv[1]);
405 target = Client_Search(Req->argv[0]);
406 if (Client_Type(Client) != CLIENT_SERVER &&
407 target == Client_ThisServer())
408 return Op_NoPrivileges(Client, Req);
410 /* The server is (already) unknown */
412 "Got SQUIT from %s for unknown server \"%s\"!?",
413 Client_ID(Client), Req->argv[0]);
417 client_con = Client_Conn(Client);
418 con = Client_Conn(target);
421 if (Client_NextHop(from) != Client || con > NONE)
422 snprintf(msg, sizeof(msg), "\"%s\" (SQUIT from %s)",
423 Req->argv[1], Client_ID(from));
425 strlcpy(msg, Req->argv[1], sizeof(msg));
427 snprintf(msg, sizeof(msg), "Got SQUIT from %s",
431 /* We are directly connected to the target server, so we
432 * have to tear down the connection and to inform all the
433 * other remaining servers in the network */
434 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
435 "Received SQUIT %s from %s: %s",
436 Req->argv[0], Client_ID(from),
437 Req->argv[1][0] ? Req->argv[1] : "-");
438 Conn_Close(con, NULL, msg, true);
439 if (con == client_con)
442 /* This server is not directly connected, so the SQUIT must
443 * be forwarded ... */
444 if (Client_Type(from) != CLIENT_SERVER) {
445 /* The origin is not an IRC server, so don't evaluate
446 * this SQUIT but simply forward it */
447 IRC_WriteStrClientPrefix(Client_NextHop(target),
448 from, "SQUIT %s :%s", Req->argv[0], Req->argv[1]);
450 /* SQUIT has been generated by another server, so
451 * remove the target server from the network! */
453 if (!strchr(msg, '('))
454 snprintf(logmsg, sizeof(logmsg),
455 "\"%s\" (SQUIT from %s)", Req->argv[1],
457 Client_Destroy(target, logmsg[0] ? logmsg : msg,