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"
35 #include "irc-write.h"
38 #include "irc-server.h"
41 * Handler for the IRC "SERVER" command.
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.
48 IRC_SERVER( CLIENT *Client, REQUEST *Req )
54 assert( Client != NULL );
55 assert( Req != NULL );
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);
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) ...",
69 if (Req->argc != 2 && Req->argc != 3)
70 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
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)
79 /* Make sure the remote server is configured here */
80 if (i >= MAX_SERVERS) {
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);
89 /* Check server password */
90 if (strcmp(Conn_Password(Client_Conn(Client)),
91 Conf_Server[i].pwd_in) != 0) {
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);
100 /* Is there a registered server with this ID? */
101 if (!Client_CheckID(Client, Req->argv[0]))
104 /* Mark this connection as belonging to an configured server */
105 if (!Conf_SetServer(i, Client_Conn(Client)))
108 Client_SetID( Client, Req->argv[0] );
109 Client_SetHops( Client, 1 );
110 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
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,
119 || !IRC_WriteStrClient(Client, "SERVER %s 1 :%s",
122 Conn_Close(Client_Conn(Client),
123 "Unexpected server behavior!",
127 Client_SetIntroducer(Client, Client);
128 Client_SetToken(Client, 1);
130 /* outgoing connect, we already sent a SERVER and PASS
131 * command to the peer */
132 Client_SetToken(Client, atoi(Req->argv[1]));
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))
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);
146 Client_SetType(Client, CLIENT_UNKNOWNSERVER);
149 if (Client_HasFlag(Client, 'Z')
150 && !Zip_InitConn(Client_Conn(Client))) {
151 Conn_Close(Client_Conn(Client),
152 "Can't initialize compression (zlib)!",
159 if (Client_HasFlag(Client, 'H')) {
160 LogDebug("Peer supports IRC+ extended server handshake ...");
161 if (!IRC_Send_ISUPPORT(Client))
163 return IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG,
167 if (Conf_MaxNickLength != CLIENT_NICK_LEN_DEFAULT)
169 "Attention: this server uses a non-standard nick length, but the peer doesn't support the IRC+ extended server handshake!");
174 return IRC_Num_ENDOFMOTD(Client, Req);
176 else if( Client_Type( Client ) == CLIENT_SERVER )
178 /* New server is being introduced to the network */
181 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
182 Client_ID(Client), Req->command);
184 /* check for existing server with same ID */
185 if (!Client_CheckID(Client, Req->argv[0]))
188 from = Client_Search( Req->prefix );
190 /* Uh, Server, that introduced the new server is unknown?! */
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);
199 c = Client_NewRemoteServer(Client, Req->argv[0], from,
200 atoi(Req->argv[1]), atoi(Req->argv[2]),
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",
212 if (Client_Hops(c) > 1 && Req->prefix[0])
213 snprintf(str, sizeof(str), "connected to %s, ",
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": "" );
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));
229 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
230 Client_ID(Client), Req->command);
234 * Handler for the IRC "NJOIN" command.
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.
241 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
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;
248 assert(Client != NULL);
251 strlcpy(nick_in, Req->argv[1], sizeof(nick_in));
252 strcpy(nick_out, "");
254 channame = Req->argv[0];
256 ptr = strtok(nick_in, ",");
258 is_owner = is_chanadmin = is_op = is_halfop = is_voiced = false;
260 /* cut off prefixes */
261 while ((*ptr == '~') || (*ptr == '&') || (*ptr == '@') ||
262 (*ptr == '%') || (*ptr == '+')) {
276 c = Client_Search(ptr);
278 /* Client not found? */
280 "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!",
285 Channel_Join(c, channame);
286 chan = Channel_Search(channame);
287 assert(chan != NULL);
290 Channel_UserModeAdd(chan, c, 'q');
292 Channel_UserModeAdd(chan, c, 'a');
294 Channel_UserModeAdd(chan, c, 'o');
296 Channel_UserModeAdd(chan, c, 'h');
298 Channel_UserModeAdd(chan, c, 'v');
300 /* Announce client to the channel */
301 IRC_WriteStrChannelPrefix(Client, chan, c, false,
302 "JOIN :%s", channame);
304 /* Announce "channel user modes" to the channel, if any */
305 strlcpy(modes, Channel_UserModes(chan, c), sizeof(modes));
307 IRC_WriteStrChannelPrefix(Client, chan, Client, false,
308 "MODE %s +%s %s", channame,
309 modes, Client_ID(c));
311 /* Build nick list for forwarding command */
312 if (nick_out[0] != '\0')
313 strlcat(nick_out, ",", sizeof(nick_out));
315 strlcat(nick_out, "~", sizeof(nick_out));
317 strlcat(nick_out, "&", sizeof(nick_out));
319 strlcat(nick_out, "@", sizeof(nick_out));
321 strlcat(nick_out, "%", sizeof(nick_out));
323 strlcat(nick_out, "+", sizeof(nick_out));
324 strlcat(nick_out, ptr, sizeof(nick_out));
327 /* Get next nick, if any ... */
328 ptr = strtok(NULL, ",");
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);
340 * Handler for the IRC "SQUIT" command.
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.
347 IRC_SQUIT(CLIENT * Client, REQUEST * Req)
349 char msg[COMMAND_LEN], logmsg[COMMAND_LEN];
350 CLIENT *from, *target;
354 assert(Client != NULL);
357 if (Client_Type(Client) != CLIENT_SERVER
358 && !Client_HasMode(Client, 'o'))
359 return Op_NoPrivileges(Client, Req);
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);
369 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
370 Client_ID(Client), Req->prefix);
372 if (Client_Type(Client) == CLIENT_USER)
373 loglevel = LOG_NOTICE | LOG_snotice;
375 loglevel = LOG_DEBUG;
376 Log(loglevel, "Got SQUIT from %s for \"%s\": \"%s\" ...",
377 Client_ID(from), Req->argv[0], Req->argv[1]);
379 target = Client_Search(Req->argv[0]);
380 if (Client_Type(Client) != CLIENT_SERVER &&
381 target == Client_ThisServer())
382 return Op_NoPrivileges(Client, Req);
384 /* The server is (already) unknown */
386 "Got SQUIT from %s for unknown server \"%s\"!?",
387 Client_ID(Client), Req->argv[0]);
391 con = Client_Conn(target);
394 if (Client_NextHop(from) != Client || con > NONE)
395 snprintf(msg, sizeof(msg), "\"%s\" (SQUIT from %s)",
396 Req->argv[1], Client_ID(from));
398 strlcpy(msg, Req->argv[1], sizeof(msg));
400 snprintf(msg, sizeof(msg), "Got SQUIT from %s",
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))
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]);
423 /* SQUIT has been generated by another server, so
424 * remove the target server from the network! */
426 if (!strchr(msg, '('))
427 snprintf(logmsg, sizeof(logmsg),
428 "\"%s\" (SQUIT from %s)", Req->argv[1],
430 Client_Destroy(target, logmsg[0] ? logmsg : msg,