Make server-to-server protocol more robust

Now ngIRCd catches more errors on the server-to-server (S2S) protocol
that could crash the daemon before. This hasn't been a real problem
because the IRC S2S protocol is "trusted" by design, but the behavior
is much better now.

Thanks to wowaname on #ngircd for pointing this out!
This commit is contained in:
Alexander Barton 2015-09-03 16:22:36 +02:00
parent c2e4c304e3
commit eb9929e82c
6 changed files with 46 additions and 16 deletions

View File

@ -1,6 +1,6 @@
/* /*
* ngIRCd -- The Next Generation IRC Daemon * ngIRCd -- The Next Generation IRC Daemon
* Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors. * Copyright (c)2001-2015 Alexander Barton (alex@barton.de) and Contributors.
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -196,6 +196,7 @@ IRC_NICK( CLIENT *Client, REQUEST *Req )
/* Search "target" client */ /* Search "target" client */
if (Client_Type(Client) == CLIENT_SERVER) { if (Client_Type(Client) == CLIENT_SERVER) {
_IRC_REQUIRE_PREFIX_OR_RETURN_(Client, Req)
target = Client_Search(Req->prefix); target = Client_Search(Req->prefix);
if (!target) if (!target)
return IRC_WriteErrClient(Client, return IRC_WriteErrClient(Client,
@ -379,6 +380,8 @@ IRC_SVSNICK(CLIENT *Client, REQUEST *Req)
assert(Client != NULL); assert(Client != NULL);
assert(Req != NULL); assert(Req != NULL);
_IRC_REQUIRE_PREFIX_OR_RETURN_(Client, Req)
/* Search the originator */ /* Search the originator */
from = Client_Search(Req->prefix); from = Client_Search(Req->prefix);
if (!from) if (!from)
@ -481,6 +484,7 @@ IRC_USER(CLIENT * Client, REQUEST * Req)
Client_Type(Client) == CLIENT_SERVICE) { Client_Type(Client) == CLIENT_SERVICE) {
/* Server/service updating an user */ /* Server/service updating an user */
_IRC_ARGC_EQ_OR_RETURN_(Client, Req, 4) _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 4)
_IRC_REQUIRE_PREFIX_OR_RETURN_(Client, Req)
c = Client_Search(Req->prefix); c = Client_Search(Req->prefix);
if (!c) if (!c)
@ -653,6 +657,8 @@ IRC_QUIT( CLIENT *Client, REQUEST *Req )
if (Client_Type(Client) == CLIENT_SERVER) { if (Client_Type(Client) == CLIENT_SERVER) {
/* Server */ /* Server */
_IRC_REQUIRE_PREFIX_OR_RETURN_(Client, Req)
target = Client_Search(Req->prefix); target = Client_Search(Req->prefix);
if (!target) { if (!target) {
Log(LOG_WARNING, Log(LOG_WARNING,
@ -741,9 +747,10 @@ IRC_PING(CLIENT *Client, REQUEST *Req)
if (target != Client_ThisServer()) { if (target != Client_ThisServer()) {
/* Ok, we have to forward the PING */ /* Ok, we have to forward the PING */
if (Client_Type(Client) == CLIENT_SERVER) if (Client_Type(Client) == CLIENT_SERVER) {
_IRC_REQUIRE_PREFIX_OR_RETURN_(Client, Req)
from = Client_Search(Req->prefix); from = Client_Search(Req->prefix);
else } else
from = Client; from = Client;
if (!from) if (!from)
return IRC_WriteErrClient(Client, return IRC_WriteErrClient(Client,
@ -815,6 +822,8 @@ IRC_PONG(CLIENT *Client, REQUEST *Req)
/* Forward? */ /* Forward? */
if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) { if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
_IRC_REQUIRE_PREFIX_OR_RETURN_(Client, Req)
target = Client_Search(Req->argv[0]); target = Client_Search(Req->argv[0]);
if (!target) if (!target)
return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG, return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG,

View File

@ -1,6 +1,6 @@
/* /*
* ngIRCd -- The Next Generation IRC Daemon * ngIRCd -- The Next Generation IRC Daemon
* Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors. * Copyright (c)2001-2015 Alexander Barton (alex@barton.de) and Contributors.
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -65,6 +65,18 @@ if (Req->argc < Min || Req->argc > Max) { \
Client_ID(Client), Req->command); \ Client_ID(Client), Req->command); \
} }
/**
* Make sure that the command has a prefix.
*
* If there is no prefix, send an error to the client and return from
* the function.
*/
#define _IRC_REQUIRE_PREFIX_OR_RETURN_(Client, Req) \
if (!Req->prefix) { \
return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG, \
Client_ID(Client), Req->command); \
}
/** /**
* Get sender of an IRC command. * Get sender of an IRC command.
* *
@ -73,13 +85,17 @@ if (Req->argc < Min || Req->argc > Max) { \
* send an error to the client and return from the function. * send an error to the client and return from the function.
*/ */
#define _IRC_GET_SENDER_OR_RETURN_(Sender, Req, Client) \ #define _IRC_GET_SENDER_OR_RETURN_(Sender, Req, Client) \
if (Client_Type(Client) == CLIENT_SERVER) \ if (Client_Type(Client) == CLIENT_SERVER) { \
if (!Req->prefix) \
return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG, \
Client_ID(Client), Req->command); \
Sender = Client_Search(Req->prefix); \ Sender = Client_Search(Req->prefix); \
else \ } else \
Sender = Client; \ Sender = Client; \
if (!Sender) \ if (!Sender) \
return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG, \ return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG, \
Client_ID(Client), Req->prefix); Client_ID(Client), \
Req->prefix ? Req->prefix : "(none)");
/** /**
* Get target of an IRC command and make sure that it is a server. * Get target of an IRC command and make sure that it is a server.

View File

@ -1,6 +1,6 @@
/* /*
* ngIRCd -- The Next Generation IRC Daemon * ngIRCd -- The Next Generation IRC Daemon
* Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors. * Copyright (c)2001-2015 Alexander Barton (alex@barton.de) and Contributors.
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -24,6 +24,7 @@
#include "conn-func.h" #include "conn-func.h"
#include "channel.h" #include "channel.h"
#include "irc-macros.h"
#include "irc-write.h" #include "irc-write.h"
#include "log.h" #include "log.h"
#include "messages.h" #include "messages.h"
@ -47,6 +48,8 @@ IRC_METADATA(CLIENT *Client, REQUEST *Req)
assert(Client != NULL); assert(Client != NULL);
assert(Req != NULL); assert(Req != NULL);
_IRC_REQUIRE_PREFIX_OR_RETURN_(Client, Req)
prefix = Client_Search(Req->prefix); prefix = Client_Search(Req->prefix);
if (!prefix) if (!prefix)
return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG, return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,

View File

@ -1,6 +1,6 @@
/* /*
* ngIRCd -- The Next Generation IRC Daemon * ngIRCd -- The Next Generation IRC Daemon
* Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors. * Copyright (c)2001-2015 Alexander Barton (alex@barton.de) and Contributors.
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -30,6 +30,7 @@
#include "class.h" #include "class.h"
#include "parse.h" #include "parse.h"
#include "irc.h" #include "irc.h"
#include "irc-macros.h"
#include "irc-write.h" #include "irc-write.h"
#include "lists.h" #include "lists.h"
#include "log.h" #include "log.h"
@ -358,6 +359,7 @@ IRC_WALLOPS( CLIENT *Client, REQUEST *Req )
from = Client; from = Client;
break; break;
case CLIENT_SERVER: case CLIENT_SERVER:
_IRC_REQUIRE_PREFIX_OR_RETURN_(Client, Req)
from = Client_Search(Req->prefix); from = Client_Search(Req->prefix);
break; break;
default: default:

View File

@ -1,6 +1,6 @@
/* /*
* ngIRCd -- The Next Generation IRC Daemon * ngIRCd -- The Next Generation IRC Daemon
* Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors. * Copyright (c)2001-2015 Alexander Barton (alex@barton.de) and Contributors.
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -526,7 +526,7 @@ Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
Client_ID(Client), Req->command); Client_ID(Client), Req->command);
} }
if (Client_Type(Client) == CLIENT_SERVER) if (Client_Type(Client) == CLIENT_SERVER && Req->prefix)
from = Client_Search(Req->prefix); from = Client_Search(Req->prefix);
else else
from = Client; from = Client;

View File

@ -1,6 +1,6 @@
/* /*
* ngIRCd -- The Next Generation IRC Daemon * ngIRCd -- The Next Generation IRC Daemon
* Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors. * Copyright (c)2001-2015 Alexander Barton (alex@barton.de) and Contributors.
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -461,10 +461,10 @@ Handle_Numeric(CLIENT *client, REQUEST *Req)
} }
/* Determine source */ /* Determine source */
if (! Req->prefix[0]) { if (!Req->prefix) {
/* Oops, no prefix!? */ Log(LOG_WARNING,
Log(LOG_WARNING, "Got status code %s from \"%s\" without prefix!?", "Got status code %s from \"%s\" without prefix!?",
Req->command, Client_ID(client)); Req->command, Client_ID(client));
return true; return true;
} }