2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2014 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"
36 #include "irc-write.h"
39 #include "irc-server.h"
42 * Handler for the IRC "SERVER" command.
44 * @param Client The client from which this command has been received.
45 * @param Req Request structure with prefix and all parameters.
46 * @return CONNECTED or DISCONNECTED.
49 IRC_SERVER( CLIENT *Client, REQUEST *Req )
55 assert( Client != NULL );
56 assert( Req != NULL );
58 /* Return an error if this is not a local client */
59 if (Client_Conn(Client) <= NONE)
60 return IRC_WriteErrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
61 Client_ID(Client), Req->command);
63 if (Client_Type(Client) == CLIENT_GOTPASS ||
64 Client_Type(Client) == CLIENT_GOTPASS_2813) {
65 /* We got a PASS command from the peer, and now a SERVER
66 * command: the peer tries to register itself as a server. */
67 LogDebug("Connection %d: got SERVER command (new server link) ...",
70 if (Req->argc != 2 && Req->argc != 3)
71 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
75 /* Get configuration index of new remote server ... */
76 for (i = 0; i < MAX_SERVERS; i++)
77 if (strcasecmp(Req->argv[0], Conf_Server[i].name) == 0)
80 /* Make sure the remote server is configured here */
81 if (i >= MAX_SERVERS) {
83 "Connection %d: Server \"%s\" not configured here!",
84 Client_Conn(Client), Req->argv[0]);
85 Conn_Close(Client_Conn(Client), NULL,
86 "Server not configured here", true);
90 /* Check server password */
91 if (strcmp(Conn_Password(Client_Conn(Client)),
92 Conf_Server[i].pwd_in) != 0) {
94 "Connection %d: Got bad password from server \"%s\"!",
95 Client_Conn(Client), Req->argv[0]);
96 Conn_Close(Client_Conn(Client), NULL,
97 "Bad password", true);
101 /* Is there a registered server with this ID? */
102 if (!Client_CheckID(Client, Req->argv[0]))
105 /* Mark this connection as belonging to an configured server */
106 if (!Conf_SetServer(i, Client_Conn(Client)))
109 Client_SetID( Client, Req->argv[0] );
110 Client_SetHops( Client, 1 );
111 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
113 /* Is this server registering on our side, or are we connecting to
114 * a remote server? */
115 if (Client_Token(Client) != TOKEN_OUTBOUND) {
116 /* Incoming connection, send user/pass */
117 if (!IRC_WriteStrClient(Client, "PASS %s %s",
118 Conf_Server[i].pwd_out,
120 || !IRC_WriteStrClient(Client, "SERVER %s 1 :%s",
123 Conn_Close(Client_Conn(Client),
124 "Unexpected server behavior!",
128 Client_SetIntroducer(Client, Client);
129 Client_SetToken(Client, 1);
131 /* outgoing connect, we already sent a SERVER and PASS
132 * command to the peer */
133 Client_SetToken(Client, atoi(Req->argv[1]));
136 /* Check protocol level */
137 if (Client_Type(Client) == CLIENT_GOTPASS) {
138 /* We got a "simple" PASS command, so the peer is
139 * using the protocol as defined in RFC 1459. */
140 if (! (Conn_Options(Client_Conn(Client)) & CONN_RFC1459))
142 "Switching connection %d (\"%s\") to RFC 1459 compatibility mode.",
143 Client_Conn(Client), Client_ID(Client));
144 Conn_SetOption(Client_Conn(Client), CONN_RFC1459);
147 Client_SetType(Client, CLIENT_UNKNOWNSERVER);
150 if (Client_HasFlag(Client, 'Z')
151 && !Zip_InitConn(Client_Conn(Client))) {
152 Conn_Close(Client_Conn(Client),
153 "Can't initialize compression (zlib)!",
160 if (Client_HasFlag(Client, 'H')) {
161 LogDebug("Peer supports IRC+ extended server handshake ...");
162 if (!IRC_Send_ISUPPORT(Client))
164 return IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG,
168 if (Conf_MaxNickLength != CLIENT_NICK_LEN_DEFAULT)
170 "Attention: this server uses a non-standard nick length, but the peer doesn't support the IRC+ extended server handshake!");
175 return IRC_Num_ENDOFMOTD(Client, Req);
177 else if( Client_Type( Client ) == CLIENT_SERVER )
179 /* New server is being introduced to the network */
182 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
183 Client_ID(Client), Req->command);
185 /* check for existing server with same ID */
186 if (!Client_CheckID(Client, Req->argv[0]))
189 from = Client_Search( Req->prefix );
191 /* Uh, Server, that introduced the new server is unknown?! */
193 "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)",
194 Req->prefix, Client_Conn(Client));
195 Conn_Close(Client_Conn(Client), NULL,
196 "Unknown ID in prefix of SERVER", true);
200 c = Client_NewRemoteServer(Client, Req->argv[0], from,
201 atoi(Req->argv[1]), atoi(Req->argv[2]),
205 "Can't create client structure for server! (on connection %d)",
206 Client_Conn(Client));
207 Conn_Close(Client_Conn(Client), NULL,
208 "Can't allocate client structure for remote server",
213 if (Client_Hops(c) > 1 && Req->prefix[0])
214 snprintf(str, sizeof(str), "connected to %s, ",
218 Log(LOG_NOTICE|LOG_snotice,
219 "Server \"%s\" registered (via %s, %s%d hop%s).",
220 Client_ID(c), Client_ID(Client), str, Client_Hops(c),
221 Client_Hops(c) > 1 ? "s": "" );
223 /* notify other servers */
224 IRC_WriteStrServersPrefix(Client, from, "SERVER %s %d %d :%s",
225 Client_ID(c), Client_Hops(c) + 1,
226 Client_MyToken(c), Client_Info(c));
230 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
231 Client_ID(Client), Req->command);
235 * Handler for the IRC "NJOIN" command.
237 * @param Client The client from which this command has been received.
238 * @param Req Request structure with prefix and all parameters.
239 * @return CONNECTED or DISCONNECTED.
242 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
244 char nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
245 bool is_owner, is_chanadmin, is_op, is_halfop, is_voiced;
249 assert(Client != NULL);
252 strlcpy(nick_in, Req->argv[1], sizeof(nick_in));
253 strcpy(nick_out, "");
255 channame = Req->argv[0];
257 ptr = strtok(nick_in, ",");
259 is_owner = is_chanadmin = is_op = is_halfop = is_voiced = false;
261 /* cut off prefixes */
262 while ((*ptr == '~') || (*ptr == '&') || (*ptr == '@') ||
263 (*ptr == '%') || (*ptr == '+')) {
277 c = Client_Search(ptr);
279 /* Client not found? */
281 "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!",
286 if (Channel_Join(c, channame)) {
287 /* Failed to join channel. Ooops!? */
289 "Failed to join client \"%s\" to channel \"%s\" (NJOIN): killing it!",
291 IRC_KillClient(NULL, NULL, ptr, "Internal NJOIN error!");
292 Log(LOG_DEBUG, "... done.");
296 chan = Channel_Search(channame);
297 assert(chan != NULL);
300 Channel_UserModeAdd(chan, c, 'q');
302 Channel_UserModeAdd(chan, c, 'a');
304 Channel_UserModeAdd(chan, c, 'o');
306 Channel_UserModeAdd(chan, c, 'h');
308 Channel_UserModeAdd(chan, c, 'v');
310 /* Announce client to the channel */
311 IRC_WriteStrChannelPrefix(Client, chan, c, false,
312 "JOIN :%s", channame);
314 /* Announce "channel user modes" to the channel, if any */
315 strlcpy(modes, Channel_UserModes(chan, c), sizeof(modes));
317 IRC_WriteStrChannelPrefix(Client, chan, Client, false,
318 "MODE %s +%s %s", channame,
319 modes, Client_ID(c));
321 /* Build nick list for forwarding command */
322 if (nick_out[0] != '\0')
323 strlcat(nick_out, ",", sizeof(nick_out));
325 strlcat(nick_out, "~", sizeof(nick_out));
327 strlcat(nick_out, "&", sizeof(nick_out));
329 strlcat(nick_out, "@", sizeof(nick_out));
331 strlcat(nick_out, "%", sizeof(nick_out));
333 strlcat(nick_out, "+", sizeof(nick_out));
334 strlcat(nick_out, ptr, sizeof(nick_out));
337 /* Get next nick, if any ... */
338 ptr = strtok(NULL, ",");
341 /* forward to other servers */
342 if (nick_out[0] != '\0')
343 IRC_WriteStrServersPrefix(Client, Client_ThisServer(),
344 "NJOIN %s :%s", Req->argv[0], nick_out);
350 * Handler for the IRC "SQUIT" command.
352 * @param Client The client from which this command has been received.
353 * @param Req Request structure with prefix and all parameters.
354 * @return CONNECTED or DISCONNECTED.
357 IRC_SQUIT(CLIENT * Client, REQUEST * Req)
359 char msg[COMMAND_LEN], logmsg[COMMAND_LEN];
360 CLIENT *from, *target;
364 assert(Client != NULL);
367 if (Client_Type(Client) != CLIENT_SERVER
368 && !Client_HasMode(Client, 'o'))
369 return Op_NoPrivileges(Client, Req);
371 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix) {
372 from = Client_Search(Req->prefix);
373 if (Client_Type(from) != CLIENT_SERVER
374 && !Op_Check(Client, Req))
375 return Op_NoPrivileges(Client, Req);
379 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
380 Client_ID(Client), Req->prefix);
382 if (Client_Type(Client) == CLIENT_USER)
383 loglevel = LOG_NOTICE | LOG_snotice;
385 loglevel = LOG_DEBUG;
386 Log(loglevel, "Got SQUIT from %s for \"%s\": \"%s\" ...",
387 Client_ID(from), Req->argv[0], Req->argv[1]);
389 target = Client_Search(Req->argv[0]);
390 if (Client_Type(Client) != CLIENT_SERVER &&
391 target == Client_ThisServer())
392 return Op_NoPrivileges(Client, Req);
394 /* The server is (already) unknown */
396 "Got SQUIT from %s for unknown server \"%s\"!?",
397 Client_ID(Client), Req->argv[0]);
401 con = Client_Conn(target);
404 if (Client_NextHop(from) != Client || con > NONE)
405 snprintf(msg, sizeof(msg), "\"%s\" (SQUIT from %s)",
406 Req->argv[1], Client_ID(from));
408 strlcpy(msg, Req->argv[1], sizeof(msg));
410 snprintf(msg, sizeof(msg), "Got SQUIT from %s",
414 /* We are directly connected to the target server, so we
415 * have to tear down the connection and to inform all the
416 * other remaining servers in the network */
417 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
418 "Received SQUIT %s from %s: %s",
419 Req->argv[0], Client_ID(from),
420 Req->argv[1][0] ? Req->argv[1] : "-");
421 Conn_Close(con, NULL, msg, true);
422 if (con == Client_Conn(Client))
425 /* This server is not directly connected, so the SQUIT must
426 * be forwarded ... */
427 if (Client_Type(from) != CLIENT_SERVER) {
428 /* The origin is not an IRC server, so don't evaluate
429 * this SQUIT but simply forward it */
430 IRC_WriteStrClientPrefix(Client_NextHop(target),
431 from, "SQUIT %s :%s", Req->argv[0], Req->argv[1]);
433 /* SQUIT has been generated by another server, so
434 * remove the target server from the network! */
436 if (!strchr(msg, '('))
437 snprintf(logmsg, sizeof(logmsg),
438 "\"%s\" (SQUIT from %s)", Req->argv[1],
440 Client_Destroy(target, logmsg[0] ? logmsg : msg,