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);
102 * This check is only done if RequireClientCert is disabled, and this Servers [SERVER] section has
103 * "SSLVerify" enabled.
104 * (if RequireClientCert is set, certificate validation is done during SSL/TLS handshake)
106 CONN_ID con = Client_Conn (Client);
107 if (Conf_Server[i].SSLVerify && !(Conn_Options(con) & CONN_SSL_PEERCERT_OK)) {
108 Log(LOG_ERR, "Connection %d: SSLVerify is set, and server \"%s\" did not present a valid certificate",
109 Client_Conn(Client), Req->argv[0]);
110 Conn_Close(Client_Conn(Client), NULL, "No valid SSL certificate", true);
115 /* Is there a registered server with this ID? */
116 if (!Client_CheckID(Client, Req->argv[0]))
119 /* Mark this connection as belonging to an configured server */
120 if (!Conf_SetServer(i, Client_Conn(Client)))
123 Client_SetID( Client, Req->argv[0] );
124 Client_SetHops( Client, 1 );
125 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
127 /* Is this server registering on our side, or are we connecting to
128 * a remote server? */
129 if (Client_Token(Client) != TOKEN_OUTBOUND) {
130 /* Incoming connection, send user/pass */
131 if (!IRC_WriteStrClient(Client, "PASS %s %s",
132 Conf_Server[i].pwd_out,
134 || !IRC_WriteStrClient(Client, "SERVER %s 1 :%s",
137 Conn_Close(Client_Conn(Client),
138 "Unexpected server behavior!",
142 Client_SetIntroducer(Client, Client);
143 Client_SetToken(Client, 1);
145 /* outgoing connect, we already sent a SERVER and PASS
146 * command to the peer */
147 Client_SetToken(Client, atoi(Req->argv[1]));
150 /* Check protocol level */
151 if (Client_Type(Client) == CLIENT_GOTPASS) {
152 /* We got a "simple" PASS command, so the peer is
153 * using the protocol as defined in RFC 1459. */
154 if (! (Conn_Options(Client_Conn(Client)) & CONN_RFC1459))
156 "Switching connection %d (\"%s\") to RFC 1459 compatibility mode.",
157 Client_Conn(Client), Client_ID(Client));
158 Conn_SetOption(Client_Conn(Client), CONN_RFC1459);
161 Client_SetType(Client, CLIENT_UNKNOWNSERVER);
164 if (Client_HasFlag(Client, 'Z')
165 && !Zip_InitConn(Client_Conn(Client))) {
166 Conn_Close(Client_Conn(Client),
167 "Can't initialize compression (zlib)!",
174 if (Client_HasFlag(Client, 'H')) {
175 LogDebug("Peer supports IRC+ extended server handshake ...");
176 if (!IRC_Send_ISUPPORT(Client))
178 return IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG,
182 if (Conf_MaxNickLength != CLIENT_NICK_LEN_DEFAULT)
184 "Attention: this server uses a non-standard nick length, but the peer doesn't support the IRC+ extended server handshake!");
189 return IRC_Num_ENDOFMOTD(Client, Req);
191 else if( Client_Type( Client ) == CLIENT_SERVER )
193 /* New server is being introduced to the network */
196 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
197 Client_ID(Client), Req->command);
199 /* check for existing server with same ID */
200 if (!Client_CheckID(Client, Req->argv[0]))
203 from = Client_Search( Req->prefix );
205 /* Uh, Server, that introduced the new server is unknown?! */
207 "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)",
208 Req->prefix, Client_Conn(Client));
209 Conn_Close(Client_Conn(Client), NULL,
210 "Unknown ID in prefix of SERVER", true);
214 c = Client_NewRemoteServer(Client, Req->argv[0], from,
215 atoi(Req->argv[1]), atoi(Req->argv[2]),
219 "Can't create client structure for server! (on connection %d)",
220 Client_Conn(Client));
221 Conn_Close(Client_Conn(Client), NULL,
222 "Can't allocate client structure for remote server",
227 if (Client_Hops(c) > 1 && Req->prefix[0])
228 snprintf(str, sizeof(str), "connected to %s, ",
232 Log(LOG_NOTICE|LOG_snotice,
233 "Server \"%s\" registered (via %s, %s%d hop%s).",
234 Client_ID(c), Client_ID(Client), str, Client_Hops(c),
235 Client_Hops(c) > 1 ? "s": "" );
237 /* notify other servers */
238 IRC_WriteStrServersPrefix(Client, from, "SERVER %s %d %d :%s",
239 Client_ID(c), Client_Hops(c) + 1,
240 Client_MyToken(c), Client_Info(c));
244 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
245 Client_ID(Client), Req->command);
249 * Handler for the IRC "NJOIN" command.
251 * @param Client The client from which this command has been received.
252 * @param Req Request structure with prefix and all parameters.
253 * @return CONNECTED or DISCONNECTED.
256 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
258 char nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
259 bool is_owner, is_chanadmin, is_op, is_halfop, is_voiced;
263 assert( Client != NULL );
264 assert( Req != NULL );
266 strlcpy( nick_in, Req->argv[1], sizeof( nick_in ));
267 strcpy( nick_out, "" );
269 channame = Req->argv[0];
270 ptr = strtok( nick_in, "," );
273 is_owner = is_chanadmin = is_op = is_halfop = is_voiced = false;
275 /* cut off prefixes */
276 while(( *ptr == '~') || ( *ptr == '&' ) || ( *ptr == '@' ) ||
277 ( *ptr == '%') || ( *ptr == '+' ))
279 if( *ptr == '~' ) is_owner = true;
280 if( *ptr == '&' ) is_chanadmin = true;
281 if( *ptr == '@' ) is_op = true;
282 if( *ptr == 'h' ) is_halfop = true;
283 if( *ptr == '+' ) is_voiced = true;
287 c = Client_Search( ptr );
290 Channel_Join( c, channame );
291 chan = Channel_Search( channame );
292 assert( chan != NULL );
294 if( is_owner ) Channel_UserModeAdd( chan, c, 'q' );
295 if( is_chanadmin ) Channel_UserModeAdd( chan, c, 'a' );
296 if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
297 if( is_halfop ) Channel_UserModeAdd( chan, c, 'h' );
298 if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
300 /* announce to channel... */
301 IRC_WriteStrChannelPrefix( Client, chan, c, false, "JOIN :%s", channame );
303 /* set Channel-User-Modes */
304 strlcpy( modes, Channel_UserModes( chan, c ), sizeof( modes ));
307 /* send modes to channel */
308 IRC_WriteStrChannelPrefix( Client, chan, Client, false, "MODE %s +%s %s", channame, modes, Client_ID( c ));
311 if( nick_out[0] != '\0' ) strlcat( nick_out, ",", sizeof( nick_out ));
312 if( is_owner ) strlcat( nick_out, "~", sizeof( nick_out ));
313 if( is_chanadmin ) strlcat( nick_out, "&", sizeof( nick_out ));
314 if( is_op ) strlcat( nick_out, "@", sizeof( nick_out ));
315 if( is_halfop ) strlcat( nick_out, "%", sizeof( nick_out ));
316 if( is_voiced ) strlcat( nick_out, "+", sizeof( nick_out ));
317 strlcat( nick_out, ptr, sizeof( nick_out ));
319 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
321 /* search for next Nick */
322 ptr = strtok( NULL, "," );
325 /* forward to other servers */
326 if (nick_out[0] != '\0')
327 IRC_WriteStrServersPrefix(Client, Client_ThisServer(),
328 "NJOIN %s :%s", Req->argv[0], nick_out);
334 * Handler for the IRC "SQUIT" command.
336 * @param Client The client from which this command has been received.
337 * @param Req Request structure with prefix and all parameters.
338 * @return CONNECTED or DISCONNECTED.
341 IRC_SQUIT(CLIENT * Client, REQUEST * Req)
343 char msg[COMMAND_LEN], logmsg[COMMAND_LEN];
344 CLIENT *from, *target;
348 assert(Client != NULL);
351 if (Client_Type(Client) != CLIENT_SERVER
352 && !Client_HasMode(Client, 'o'))
353 return Op_NoPrivileges(Client, Req);
355 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix) {
356 from = Client_Search(Req->prefix);
357 if (Client_Type(from) != CLIENT_SERVER
358 && !Op_Check(Client, Req))
359 return Op_NoPrivileges(Client, Req);
363 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
364 Client_ID(Client), Req->prefix);
366 if (Client_Type(Client) == CLIENT_USER)
367 loglevel = LOG_NOTICE | LOG_snotice;
369 loglevel = LOG_DEBUG;
370 Log(loglevel, "Got SQUIT from %s for \"%s\": \"%s\" ...",
371 Client_ID(from), Req->argv[0], Req->argv[1]);
373 target = Client_Search(Req->argv[0]);
374 if (Client_Type(Client) != CLIENT_SERVER &&
375 target == Client_ThisServer())
376 return Op_NoPrivileges(Client, Req);
378 /* The server is (already) unknown */
380 "Got SQUIT from %s for unknown server \"%s\"!?",
381 Client_ID(Client), Req->argv[0]);
385 con = Client_Conn(target);
388 if (Client_NextHop(from) != Client || con > NONE)
389 snprintf(msg, sizeof(msg), "\"%s\" (SQUIT from %s)",
390 Req->argv[1], Client_ID(from));
392 strlcpy(msg, Req->argv[1], sizeof(msg));
394 snprintf(msg, sizeof(msg), "Got SQUIT from %s",
398 /* We are directly connected to the target server, so we
399 * have to tear down the connection and to inform all the
400 * other remaining servers in the network */
401 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
402 "Received SQUIT %s from %s: %s",
403 Req->argv[0], Client_ID(from),
404 Req->argv[1][0] ? Req->argv[1] : "-");
405 Conn_Close(con, NULL, msg, true);
406 if (con == Client_Conn(Client))
409 /* This server is not directly connected, so the SQUIT must
410 * be forwarded ... */
411 if (Client_Type(from) != CLIENT_SERVER) {
412 /* The origin is not an IRC server, so don't evaluate
413 * this SQUIT but simply forward it */
414 IRC_WriteStrClientPrefix(Client_NextHop(target),
415 from, "SQUIT %s :%s", Req->argv[0], Req->argv[1]);
417 /* SQUIT has been generated by another server, so
418 * remove the target server from the network! */
420 if (!strchr(msg, '('))
421 snprintf(logmsg, sizeof(logmsg),
422 "\"%s\" (SQUIT from %s)", Req->argv[1],
424 Client_Destroy(target, logmsg[0] ? logmsg : msg,