2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2013 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
28 #include "conn-func.h"
39 #include "irc-macros.h"
40 #include "irc-write.h"
44 #include "irc-server.h"
47 * Handler for the IRC "SERVER" command.
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.
54 IRC_SERVER( CLIENT *Client, REQUEST *Req )
60 assert( Client != NULL );
61 assert( Req != NULL );
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);
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) ...",
75 if (Req->argc != 2 && Req->argc != 3) {
76 IRC_SetPenalty(Client, 2);
77 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
82 /* Get configuration index of new remote server ... */
83 for (i = 0; i < MAX_SERVERS; i++)
84 if (strcasecmp(Req->argv[0], Conf_Server[i].name) == 0)
87 /* Make sure the remote server is configured here */
88 if (i >= MAX_SERVERS) {
90 "Connection %d: Server \"%s\" not configured here!",
91 Client_Conn(Client), Req->argv[0]);
92 Conn_Close(Client_Conn(Client), NULL,
93 "Server not configured here", true);
97 /* Check server password */
98 if (strcmp(Conn_Password(Client_Conn(Client)),
99 Conf_Server[i].pwd_in) != 0) {
101 "Connection %d: Got bad password from server \"%s\"!",
102 Client_Conn(Client), Req->argv[0]);
103 Conn_Close(Client_Conn(Client), NULL,
104 "Bad password", true);
108 /* Is there a registered server with this ID? */
109 if (!Client_CheckID(Client, Req->argv[0]))
112 /* Mark this connection as belonging to an configured server */
113 if (!Conf_SetServer(i, Client_Conn(Client)))
116 Client_SetID( Client, Req->argv[0] );
117 Client_SetHops( Client, 1 );
118 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
120 /* Is this server registering on our side, or are we connecting to
121 * a remote server? */
122 if (Client_Token(Client) != TOKEN_OUTBOUND) {
123 /* Incoming connection, send user/pass */
124 if (!IRC_WriteStrClient(Client, "PASS %s %s",
125 Conf_Server[i].pwd_out,
127 || !IRC_WriteStrClient(Client, "SERVER %s 1 :%s",
130 Conn_Close(Client_Conn(Client),
131 "Unexpected server behavior!",
135 Client_SetIntroducer(Client, Client);
136 Client_SetToken(Client, 1);
138 /* outgoing connect, we already sent a SERVER and PASS
139 * command to the peer */
140 Client_SetToken(Client, atoi(Req->argv[1]));
143 /* Check protocol level */
144 if (Client_Type(Client) == CLIENT_GOTPASS) {
145 /* We got a "simple" PASS command, so the peer is
146 * using the protocol as defined in RFC 1459. */
147 if (! (Conn_Options(Client_Conn(Client)) & CONN_RFC1459))
149 "Switching connection %d (\"%s\") to RFC 1459 compatibility mode.",
150 Client_Conn(Client), Client_ID(Client));
151 Conn_SetOption(Client_Conn(Client), CONN_RFC1459);
154 Client_SetType(Client, CLIENT_UNKNOWNSERVER);
157 if (Client_HasFlag(Client, 'Z')
158 && !Zip_InitConn(Client_Conn(Client))) {
159 Conn_Close(Client_Conn(Client),
160 "Can't initialize compression (zlib)!",
167 if (Client_HasFlag(Client, 'H')) {
168 LogDebug("Peer supports IRC+ extended server handshake ...");
169 if (!IRC_Send_ISUPPORT(Client))
171 return IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG,
175 if (Conf_MaxNickLength != CLIENT_NICK_LEN_DEFAULT)
177 "Attention: this server uses a non-standard nick length, but the peer doesn't support the IRC+ extended server handshake!");
182 return IRC_Num_ENDOFMOTD(Client, Req);
184 else if( Client_Type( Client ) == CLIENT_SERVER )
186 /* New server is being introduced to the network */
188 if (Req->argc != 4) {
189 IRC_SetPenalty(Client, 2);
190 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
191 Client_ID(Client), Req->command);
194 /* check for existing server with same ID */
195 if (!Client_CheckID(Client, Req->argv[0]))
198 from = Client_Search( Req->prefix );
200 /* Uh, Server, that introduced the new server is unknown?! */
202 "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)",
203 Req->prefix, Client_Conn(Client));
204 Conn_Close(Client_Conn(Client), NULL,
205 "Unknown ID in prefix of SERVER", true);
209 c = Client_NewRemoteServer(Client, Req->argv[0], from,
210 atoi(Req->argv[1]), atoi(Req->argv[2]),
214 "Can't create client structure for server! (on connection %d)",
215 Client_Conn(Client));
216 Conn_Close(Client_Conn(Client), NULL,
217 "Can't allocate client structure for remote server",
222 if (Client_Hops(c) > 1 && Req->prefix[0])
223 snprintf(str, sizeof(str), "connected to %s, ",
227 Log(LOG_NOTICE|LOG_snotice,
228 "Server \"%s\" registered (via %s, %s%d hop%s).",
229 Client_ID(c), Client_ID(Client), str, Client_Hops(c),
230 Client_Hops(c) > 1 ? "s": "" );
232 /* notify other servers */
233 IRC_WriteStrServersPrefix(Client, from, "SERVER %s %d %d :%s",
234 Client_ID(c), Client_Hops(c) + 1,
235 Client_MyToken(c), Client_Info(c));
239 IRC_SetPenalty(Client, 2);
240 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
241 Client_ID(Client), Req->command);
246 * Handler for the IRC "NJOIN" command.
248 * @param Client The client from which this command has been received.
249 * @param Req Request structure with prefix and all parameters.
250 * @return CONNECTED or DISCONNECTED.
253 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
255 char nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
256 bool is_owner, is_chanadmin, is_op, is_halfop, is_voiced;
260 assert( Client != NULL );
261 assert( Req != NULL );
263 strlcpy( nick_in, Req->argv[1], sizeof( nick_in ));
264 strcpy( nick_out, "" );
266 channame = Req->argv[0];
267 ptr = strtok( nick_in, "," );
270 is_owner = is_chanadmin = is_op = is_halfop = is_voiced = false;
272 /* cut off prefixes */
273 while(( *ptr == '~') || ( *ptr == '&' ) || ( *ptr == '@' ) ||
274 ( *ptr == '%') || ( *ptr == '+' ))
276 if( *ptr == '~' ) is_owner = true;
277 if( *ptr == '&' ) is_chanadmin = true;
278 if( *ptr == '@' ) is_op = true;
279 if( *ptr == 'h' ) is_halfop = true;
280 if( *ptr == '+' ) is_voiced = true;
284 c = Client_Search( ptr );
287 Channel_Join( c, channame );
288 chan = Channel_Search( channame );
289 assert( chan != NULL );
291 if( is_owner ) Channel_UserModeAdd( chan, c, 'q' );
292 if( is_chanadmin ) Channel_UserModeAdd( chan, c, 'a' );
293 if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
294 if( is_halfop ) Channel_UserModeAdd( chan, c, 'h' );
295 if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
297 /* announce to channel... */
298 IRC_WriteStrChannelPrefix( Client, chan, c, false, "JOIN :%s", channame );
300 /* set Channel-User-Modes */
301 strlcpy( modes, Channel_UserModes( chan, c ), sizeof( modes ));
304 /* send modes to channel */
305 IRC_WriteStrChannelPrefix( Client, chan, Client, false, "MODE %s +%s %s", channame, modes, Client_ID( c ));
308 if( nick_out[0] != '\0' ) strlcat( nick_out, ",", sizeof( nick_out ));
309 if( is_owner ) strlcat( nick_out, "~", sizeof( nick_out ));
310 if( is_chanadmin ) strlcat( nick_out, "&", sizeof( nick_out ));
311 if( is_op ) strlcat( nick_out, "@", sizeof( nick_out ));
312 if( is_halfop ) strlcat( nick_out, "%", sizeof( nick_out ));
313 if( is_voiced ) strlcat( nick_out, "+", sizeof( nick_out ));
314 strlcat( nick_out, ptr, sizeof( nick_out ));
316 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
318 /* search for next Nick */
319 ptr = strtok( NULL, "," );
322 /* forward to other servers */
323 if (nick_out[0] != '\0')
324 IRC_WriteStrServersPrefix(Client, Client_ThisServer(),
325 "NJOIN %s :%s", Req->argv[0], nick_out);
331 * Handler for the IRC "SQUIT" command.
333 * @param Client The client from which this command has been received.
334 * @param Req Request structure with prefix and all parameters.
335 * @return CONNECTED or DISCONNECTED.
338 IRC_SQUIT(CLIENT * Client, REQUEST * Req)
340 char msg[COMMAND_LEN], logmsg[COMMAND_LEN];
341 CLIENT *from, *target;
345 assert(Client != NULL);
348 if (Client_Type(Client) != CLIENT_SERVER
349 && !Client_HasMode(Client, 'o'))
350 return Op_NoPrivileges(Client, Req);
352 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix) {
353 from = Client_Search(Req->prefix);
354 if (Client_Type(from) != CLIENT_SERVER
355 && !Op_Check(Client, Req))
356 return Op_NoPrivileges(Client, Req);
360 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
361 Client_ID(Client), Req->prefix);
363 if (Client_Type(Client) == CLIENT_USER)
364 loglevel = LOG_NOTICE | LOG_snotice;
366 loglevel = LOG_DEBUG;
367 Log(loglevel, "Got SQUIT from %s for \"%s\": \"%s\" ...",
368 Client_ID(from), Req->argv[0], Req->argv[1]);
370 target = Client_Search(Req->argv[0]);
371 if (Client_Type(Client) != CLIENT_SERVER &&
372 target == Client_ThisServer())
373 return Op_NoPrivileges(Client, Req);
375 /* The server is (already) unknown */
377 "Got SQUIT from %s for unknown server \"%s\"!?",
378 Client_ID(Client), Req->argv[0]);
382 con = Client_Conn(target);
385 if (Client_NextHop(from) != Client || con > NONE)
386 snprintf(msg, sizeof(msg), "\"%s\" (SQUIT from %s)",
387 Req->argv[1], Client_ID(from));
389 strlcpy(msg, Req->argv[1], sizeof(msg));
391 snprintf(msg, sizeof(msg), "Got SQUIT from %s",
395 /* We are directly connected to the target server, so we
396 * have to tear down the connection and to inform all the
397 * other remaining servers in the network */
398 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
399 "Received SQUIT %s from %s: %s",
400 Req->argv[0], Client_ID(from),
401 Req->argv[1][0] ? Req->argv[1] : "-");
402 Conn_Close(con, NULL, msg, true);
403 if (con == Client_Conn(Client))
406 /* This server is not directly connected, so the SQUIT must
407 * be forwarded ... */
408 if (Client_Type(from) != CLIENT_SERVER) {
409 /* The origin is not an IRC server, so don't evaluate
410 * this SQUIT but simply forward it */
411 IRC_WriteStrClientPrefix(Client_NextHop(target),
412 from, "SQUIT %s :%s", Req->argv[0], Req->argv[1]);
414 /* SQUIT has been generated by another server, so
415 * remove the target server from the network! */
417 if (!strchr(msg, '('))
418 snprintf(logmsg, sizeof(logmsg),
419 "\"%s\" (SQUIT from %s)", Req->argv[1],
421 Client_Destroy(target, logmsg[0] ? logmsg : msg,