0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-02 10:08:56 +02:00

ircd: Remove dns, packet/parse, reject.

This commit is contained in:
Jason Volk 2016-09-11 21:06:05 -07:00
parent 922337386e
commit dd1ef4fecf
9 changed files with 0 additions and 1762 deletions

View file

@ -1,49 +0,0 @@
/*
* charybdis
* dns.h: A header with the DNS functions.
*
* Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
* Copyright (C) 1996-2002 Hybrid Development Team
* Copyright (C) 2002-2012 ircd-ratbox development team
* Copyright (C) 2016 William Pitcock <nenolod@dereferenced.org>
*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
#pragma once
#define HAVE_IRCD_DNS_H
#ifdef __cplusplus
namespace ircd {
extern rb_dlink_list nameservers;
typedef void (*DNSCB)(const char *res, int status, int aftype, void *data);
typedef void (*DNSLISTCB)(int resc, const char *resv[], int status, void *data);
uint32_t lookup_hostname(const char *hostname, int aftype, DNSCB callback, void *data);
uint32_t lookup_ip(const char *hostname, int aftype, DNSCB callback, void *data);
void cancel_lookup(uint32_t xid);
void cancel_dns_stats(uint32_t xid);
void dns_results_callback(const char *callid, const char *status, const char *aftype, const char *results);
void dns_stats_results_callback(const char *callid, const char *status, int resc, const char *resv[]);
void init_dns(void);
void reload_nameservers(void);
} // namespace ircd
#endif // __cplusplus

View file

@ -1,36 +0,0 @@
/*
* ircd-ratbox: A slightly useful ircd.
* packet.h: A header for the packet functions.
*
* Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
* Copyright (C) 1996-2002 Hybrid Development Team
* Copyright (C) 2002-2004 ircd-ratbox development team
*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#pragma once
#define HAVE_IRCD_PACKET_H
#ifdef __cplusplus
namespace ircd {
extern PF read_packet;
extern EVH flood_recalc;
extern void flood_endgrace(client::client *);
} // namespace ircd
#endif // __cplusplus

View file

@ -1,45 +0,0 @@
/*
* ircd-ratbox: A slightly useful ircd.
* parse.h: A header for the message parser.
*
* Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
* Copyright (C) 1996-2002 Hybrid Development Team
* Copyright (C) 2002-2004 ircd-ratbox development team
*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#pragma once
#define HAVE_IRCD_PARSE_H
#ifdef __cplusplus
namespace ircd {
struct Message;
struct MsgBuf;
struct alias_entry;
extern void parse(client::client *, char *, char *);
extern void handle_encap(struct MsgBuf *, client::client *, client::client *,
const char *, int, const char *parv[]);
extern void mod_add_cmd(struct Message *msg);
extern void mod_del_cmd(struct Message *msg);
extern std::map<std::string, std::shared_ptr<alias_entry>, case_insensitive_less> alias_dict;
extern std::map<std::string, Message *, case_insensitive_less> cmd_dict;
} // namespace ircd
#endif // __cplusplus

View file

@ -1,49 +0,0 @@
/*
* ircd-ratbox: A slightly useful ircd
* reject.h: header to a file which rejects users with prejudice
*
* Copyright (C) 2003 Aaron Sethman <androsyn@ratbox.org>
* Copyright (C) 2003-2005 ircd-ratbox development team
*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*
*/
#pragma once
#define HAVE_IRCD_REJECT_H
#ifdef __cplusplus
namespace ircd {
/* amount of time to delay a rejected clients exit */
#define DELAYED_EXIT_TIME 10
void init_reject(void);
int check_reject(rb_fde_t *F, struct sockaddr *addr);
void add_reject(client::client *, const char *mask1, const char *mask2);
int is_reject_ip(struct sockaddr *addr);
void flush_reject(void);
int remove_reject_ip(const char *ip);
int remove_reject_mask(const char *mask1, const char *mask2);
unsigned long delay_exit_length(void);
int throttle_add(struct sockaddr *addr);
int is_throttle_ip(struct sockaddr *addr);
unsigned long throttle_size(void);
void flush_throttle(void);
} // namespace ircd
#endif // __cplusplus

View file

@ -118,12 +118,10 @@ namespace ircd
#include "capability.h"
#include "certfp.h"
#include "class.h"
#include "dns.h"
#include "hash.h"
#include "hook.h"
#include "monitor.h"
#include "operhash.h"
#include "packet.h"
#include "privilege.h"
#include "ratelimit.h"
#include "reject.h"

View file

@ -1,327 +0,0 @@
/*
* dns.c: An interface to the resolver module in authd
* (based somewhat on ircd-ratbox dns.c)
*
* Copyright (C) 2005 Aaron Sethman <androsyn@ratbox.org>
* Copyright (C) 2005-2012 ircd-ratbox development team
* Copyright (C) 2016 William Pitcock <nenolod@dereferenced.org>
*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
namespace ircd {
#define DNS_HOST_IPV4 ((char)'4')
#define DNS_HOST_IPV6 ((char)'6')
#define DNS_REVERSE_IPV4 ((char)'R')
#define DNS_REVERSE_IPV6 ((char)'S')
static void submit_dns(uint32_t uid, char type, const char *addr);
static void submit_dns_stat(uint32_t uid);
struct dnsreq
{
DNSCB callback;
void *data;
};
struct dnsstatreq
{
DNSLISTCB callback;
void *data;
};
/* These serve as a form of sparse array */
static rb_dictionary *query_dict;
static rb_dictionary *stat_dict;
rb_dlink_list nameservers;
static uint32_t query_id = 0;
static uint32_t stat_id = 0;
static inline uint32_t
assign_id(uint32_t *id)
{
if(++(*id) == 0)
*id = 1;
return *id;
}
static void
handle_dns_failure(uint32_t xid)
{
struct dnsreq *req = (dnsreq *)rb_dictionary_retrieve(query_dict, RB_UINT_TO_POINTER(xid));
s_assert(req);
if(req->callback == NULL)
return;
req->callback("FAILED", 0, 0, req->data);
req->callback = NULL;
req->data = NULL;
}
static void
handle_dns_stat_failure(uint32_t xid)
{
struct dnsstatreq *req = (dnsstatreq *)rb_dictionary_retrieve(stat_dict, RB_UINT_TO_POINTER(xid));
s_assert(req);
if(req->callback == NULL)
return;
req->callback(1, NULL, 2, req->data);
req->callback = NULL;
req->data = NULL;
}
void
cancel_lookup(uint32_t xid)
{
struct dnsreq *req = (dnsreq *)rb_dictionary_retrieve(query_dict, RB_UINT_TO_POINTER(xid));
s_assert(req);
req->callback = NULL;
req->data = NULL;
}
void
cancel_dns_stats(uint32_t xid)
{
struct dnsstatreq *req = (dnsstatreq *)rb_dictionary_retrieve(stat_dict, RB_UINT_TO_POINTER(xid));
s_assert(req);
req->callback = NULL;
req->data = NULL;
}
uint32_t
lookup_hostname(const char *hostname, int aftype, DNSCB callback, void *data)
{
struct dnsreq *req = (dnsreq *)rb_malloc(sizeof(struct dnsreq));
int aft;
uint32_t rid = assign_id(&query_id);
check_authd();
rb_dictionary_add(query_dict, RB_UINT_TO_POINTER(rid), req);
req->callback = callback;
req->data = data;
#ifdef RB_IPV6
if(aftype == AF_INET6)
aft = 6;
else
#endif
aft = 4;
submit_dns(rid, aft == 4 ? DNS_HOST_IPV4 : DNS_HOST_IPV6, hostname);
return (rid);
}
uint32_t
lookup_ip(const char *addr, int aftype, DNSCB callback, void *data)
{
struct dnsreq *req = (dnsreq *)rb_malloc(sizeof(struct dnsreq));
int aft;
uint32_t rid = assign_id(&query_id);
check_authd();
rb_dictionary_add(query_dict, RB_UINT_TO_POINTER(rid), req);
req->callback = callback;
req->data = data;
#ifdef RB_IPV6
if(aftype == AF_INET6)
aft = 6;
else
#endif
aft = 4;
submit_dns(rid, aft == 4 ? DNS_REVERSE_IPV4 : DNS_REVERSE_IPV6, addr);
return (rid);
}
static uint32_t
get_nameservers(DNSLISTCB callback, void *data)
{
struct dnsstatreq *req = (dnsstatreq *)rb_malloc(sizeof(struct dnsstatreq));
uint32_t qid = assign_id(&stat_id);
check_authd();
rb_dictionary_add(stat_dict, RB_UINT_TO_POINTER(qid), req);
req->callback = callback;
req->data = data;
submit_dns_stat(qid);
return (qid);
}
void
dns_results_callback(const char *callid, const char *status, const char *type, const char *results)
{
struct dnsreq *req;
uint32_t rid;
int st;
int aft;
long lrid = strtol(callid, NULL, 16);
if(lrid > UINT32_MAX)
return;
rid = (uint32_t)lrid;
req = (dnsreq *)rb_dictionary_retrieve(query_dict, RB_UINT_TO_POINTER(rid));
if(req == NULL)
return;
st = (*status == 'O');
aft = *type == '6' || *type == 'S' ? 6 : 4;
if(req->callback == NULL)
{
/* got cancelled..oh well */
req->data = NULL;
return;
}
#ifdef RB_IPV6
if(aft == 6)
aft = AF_INET6;
else
#endif
aft = AF_INET;
req->callback(results, st, aft, req->data);
rb_free(req);
rb_dictionary_delete(query_dict, RB_UINT_TO_POINTER(rid));
}
void
dns_stats_results_callback(const char *callid, const char *status, int resc, const char *resv[])
{
struct dnsstatreq *req;
uint32_t qid;
int st;
long lqid = strtol(callid, NULL, 16);
if(lqid > UINT32_MAX)
return;
qid = (uint32_t)lqid;
req = (dnsstatreq *)rb_dictionary_retrieve(stat_dict, RB_UINT_TO_POINTER(qid));
s_assert(req);
if(req->callback == NULL)
{
req->data = NULL;
return;
}
switch(*status)
{
case 'Y':
st = 0;
break;
case 'X':
/* Error */
st = 1;
break;
default:
/* Shouldn't happen... */
return;
}
/* Query complete */
req->callback(resc, resv, st, req->data);
rb_free(req);
rb_dictionary_delete(stat_dict, RB_UINT_TO_POINTER(qid));
}
static void
stats_results_callback(int resc, const char *resv[], int status, void *data)
{
if(status == 0)
{
rb_dlink_node *n, *tn;
RB_DLINK_FOREACH_SAFE(n, tn, nameservers.head)
{
/* Clean up old nameservers */
rb_free(n->data);
rb_dlinkDestroy(n, &nameservers);
}
for(int i = 0; i < resc; i++)
rb_dlinkAddAlloc(rb_strdup(resv[i]), &nameservers);
}
else
{
const char *error = resc ? resv[resc] : "Unknown error";
iwarn("Error getting DNS servers: %s", error);
}
}
void
init_dns(void)
{
query_dict = rb_dictionary_create("dns queries", rb_uint32cmp);
stat_dict = rb_dictionary_create("dns stat queries", rb_uint32cmp);
(void)get_nameservers(stats_results_callback, NULL);
}
void
reload_nameservers(void)
{
check_authd();
rb_helper_write(authd_helper, "R D");
(void)get_nameservers(stats_results_callback, NULL);
}
static void
submit_dns(uint32_t nid, char type, const char *addr)
{
if(authd_helper == NULL)
{
handle_dns_failure(nid);
return;
}
rb_helper_write(authd_helper, "D %x %c %s", nid, type, addr);
}
static void
submit_dns_stat(uint32_t nid)
{
if(authd_helper == NULL)
{
handle_dns_stat_failure(nid);
return;
}
rb_helper_write(authd_helper, "S %x D", nid);
}
} // namespace ircd

View file

@ -1,349 +0,0 @@
/*
* ircd-ratbox: A slightly useful ircd.
* packet.c: Packet handlers.
*
* Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
* Copyright (C) 1996-2002 Hybrid Development Team
* Copyright (C) 2002-2005 ircd-ratbox development team
*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
namespace ircd {
static char readBuf[READBUF_SIZE];
static void client_dopacket(client::client *client_p, char *buffer, size_t length);
/*
* parse_client_queued - parse client queued messages
*/
static void
parse_client_queued(client::client *client_p)
{
int dolen = 0;
int allow_read;
if(is_any_dead(*client_p))
return;
if(is_unknown(*client_p))
{
allow_read = ConfigFileEntry.client_flood_burst_max;
for (;;)
{
if(client_p->localClient->sent_parsed >= allow_read)
break;
dolen = rb_linebuf_get(&client_p->localClient->
buf_recvq, readBuf, READBUF_SIZE,
LINEBUF_COMPLETE, LINEBUF_PARSED);
if(dolen <= 0 || is_dead(*client_p))
break;
client_dopacket(client_p, readBuf, dolen);
client_p->localClient->sent_parsed++;
/* He's dead cap'n */
if(is_any_dead(*client_p))
return;
/* if theyve dropped out of the unknown state, break and move
* to the parsing for their appropriate status. --fl
*/
if(!is_unknown(*client_p))
{
/* reset their flood limits, they're now
* graced to flood
*/
client_p->localClient->sent_parsed = 0;
break;
}
}
/* If sent_parsed is impossibly high, drop it down.
* This is useful if the configuration is changed.
*/
if(client_p->localClient->sent_parsed > allow_read)
client_p->localClient->sent_parsed = allow_read;
}
if(is_any_server(*client_p) || is_exempt_flood(*client_p))
{
while (!is_any_dead(*client_p) && (dolen = rb_linebuf_get(&client_p->localClient->buf_recvq,
readBuf, READBUF_SIZE, LINEBUF_COMPLETE,
LINEBUF_PARSED)) > 0)
{
client_dopacket(client_p, readBuf, dolen);
}
}
else if(is_client(*client_p))
{
if(is_flood_done(*client_p))
allow_read = ConfigFileEntry.client_flood_burst_max;
else
allow_read = ConfigFileEntry.client_flood_burst_rate;
allow_read *= ConfigFileEntry.client_flood_message_time;
/* allow opers 4 times the amount of messages as users. why 4?
* why not. :) --fl_
*/
if(is(*client_p, umode::OPER) && ConfigFileEntry.no_oper_flood)
allow_read *= 4;
/*
* Handle flood protection here - if we exceed our flood limit on
* messages in this loop, we simply drop out of the loop prematurely.
* -- adrian
*/
for (;;)
{
/* This flood protection works as follows:
*
* A client is given allow_read lines to send to the server. Every
* time a line is parsed, sent_parsed is increased. sent_parsed
* is decreased by 1 every time flood_recalc is called.
*
* Thus a client can 'burst' allow_read lines to the server, any
* excess lines will be parsed one per flood_recalc() call.
*
* Therefore a client will be penalised more if they keep flooding,
* as sent_parsed will always hover around the allow_read limit
* and no 'bursts' will be permitted.
*/
if(client_p->localClient->sent_parsed >= allow_read)
break;
dolen = rb_linebuf_get(&client_p->localClient->
buf_recvq, readBuf, READBUF_SIZE,
LINEBUF_COMPLETE, LINEBUF_PARSED);
if(!dolen)
break;
client_dopacket(client_p, readBuf, dolen);
if(is_any_dead(*client_p))
return;
client_p->localClient->sent_parsed += ConfigFileEntry.client_flood_message_time;
}
/* If sent_parsed is impossibly high, drop it down.
* This is useful if the configuration is changed.
*/
if(client_p->localClient->sent_parsed > allow_read +
ConfigFileEntry.client_flood_message_time - 1)
client_p->localClient->sent_parsed = allow_read +
ConfigFileEntry.client_flood_message_time - 1;
}
}
/* flood_endgrace()
*
* marks the end of the clients grace period
*/
void
flood_endgrace(client::client *client_p)
{
set_flood_done(*client_p);
/* sent_parsed could be way over client_flood_burst_max but under
* client_flood_burst_rate so reset it.
*/
client_p->localClient->sent_parsed = 0;
}
/*
* flood_recalc
*
* recalculate the number of allowed flood lines. this should be called
* once a second on any given client. We then attempt to flush some data.
*/
void
flood_recalc(void *unused)
{
rb_dlink_node *ptr, *next;
client::client *client_p;
RB_DLINK_FOREACH_SAFE(ptr, next, lclient_list.head)
{
client_p = (client::client *)ptr->data;
if(rb_unlikely(is_me(*client_p)))
continue;
if(rb_unlikely(client_p->localClient == NULL))
continue;
if(is_flood_done(*client_p))
client_p->localClient->sent_parsed -= ConfigFileEntry.client_flood_message_num;
else
client_p->localClient->sent_parsed = 0;
if(client_p->localClient->sent_parsed < 0)
client_p->localClient->sent_parsed = 0;
parse_client_queued(client_p);
if(rb_unlikely(is_any_dead(*client_p)))
continue;
}
RB_DLINK_FOREACH_SAFE(ptr, next, unknown_list.head)
{
client_p = (client::client *)ptr->data;
if(client_p->localClient == NULL)
continue;
client_p->localClient->sent_parsed--;
if(client_p->localClient->sent_parsed < 0)
client_p->localClient->sent_parsed = 0;
parse_client_queued(client_p);
}
}
/*
* read_packet - Read a 'packet' of data from a connection and process it.
*/
void
read_packet(rb_fde_t * F, void *data)
{
client::client *client_p = (client::client *)data;
int length = 0;
int binary = 0;
while(1)
{
if(is_any_dead(*client_p))
return;
/*
* Read some data. We *used to* do anti-flood protection here, but
* I personally think it makes the code too hairy to make sane.
* -- adrian
*/
length = rb_read(client_p->localClient->F, readBuf, READBUF_SIZE);
if(length < 0)
{
if(rb_ignore_errno(errno))
rb_setselect(client_p->localClient->F,
RB_SELECT_READ, read_packet, client_p);
else
error_exit_client(client_p, length);
return;
}
else if(length == 0)
{
error_exit_client(client_p, length);
return;
}
if(client_p->localClient->lasttime < rb_current_time())
client_p->localClient->lasttime = rb_current_time();
client_p->flags &= ~client::flags::PINGSENT;
/*
* Before we even think of parsing what we just read, stick
* it on the end of the receive queue and do it when its
* turn comes around.
*/
if(is_handshake(*client_p) || is_unknown(*client_p))
binary = 1;
(void) rb_linebuf_parse(&client_p->localClient->buf_recvq, readBuf, length, binary);
if(is_any_dead(*client_p))
return;
/* Attempt to parse what we have */
parse_client_queued(client_p);
if(is_any_dead(*client_p))
return;
/* Check to make sure we're not flooding */
if(!is_any_server(*client_p) &&
(rb_linebuf_alloclen(&client_p->localClient->buf_recvq) > ConfigFileEntry.client_flood_max_lines))
{
if(!(ConfigFileEntry.no_oper_flood && is(*client_p, umode::OPER)))
{
exit_client(client_p, client_p, client_p, "Excess Flood");
return;
}
}
/* bail if short read */
if(length < READBUF_SIZE)
{
rb_setselect(client_p->localClient->F, RB_SELECT_READ, read_packet, client_p);
return;
}
}
}
/*
* client_dopacket - copy packet to client buf and parse it
* client_p - pointer to client structure for which the buffer data
* applies.
* buffer - pointr to the buffer containing the newly read data
* length - number of valid bytes of data in the buffer
*
* Note:
* It is implicitly assumed that dopacket is called only
* with client_p of "local" variation, which contains all the
* necessary fields (buffer etc..)
*/
void
client_dopacket(client::client *client_p, char *buffer, size_t length)
{
s_assert(client_p != NULL);
s_assert(buffer != NULL);
if(client_p == NULL || buffer == NULL)
return;
if(is_any_dead(*client_p))
return;
/*
* Update messages received
*/
++me.localClient->receiveM;
++client_p->localClient->receiveM;
/*
* Update bytes received
*/
client_p->localClient->receiveB += length;
if(client_p->localClient->receiveB > 1023)
{
client_p->localClient->receiveK += (client_p->localClient->receiveB >> 10);
client_p->localClient->receiveB &= 0x03ff; /* 2^10 = 1024, 3ff = 1023 */
}
me.localClient->receiveB += length;
if(me.localClient->receiveB > 1023)
{
me.localClient->receiveK += (me.localClient->receiveB >> 10);
me.localClient->receiveB &= 0x03ff;
}
//parse(client_p, buffer, buffer + length);
}
} // namespace ircd

View file

@ -1,519 +0,0 @@
/*
* charybdis: an advanced ircd.
* parse.c: The message parser.
*
* Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
* Copyright (C) 1996-2002 Hybrid Development Team
* Copyright (C) 2002-2005 ircd-ratbox development team
* Copyright (C) 2007-2016 William Pitcock
*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
/*
make_index<Message>(),
{
[](mod &mod, const std::string &name)
{
auto &tab(get<Message>(mod, name));
mod_add_cmd(&tab);
},
[](mod &mod, const std::string &name)
{
auto &tab(get<Message>(mod, name));
mod_del_cmd(&tab);
}
}
*/
namespace ircd {
std::map<std::string, std::shared_ptr<alias_entry>, case_insensitive_less> alias_dict;
std::map<std::string, Message *, case_insensitive_less> cmd_dict;
static void cancel_clients(client::client *, client::client *);
static void remove_unknown(client::client *, const char *, char *);
static void do_numeric(int, client::client *, client::client *, int, const char **);
static int handle_command(struct Message *, struct MsgBuf *, client::client *, client::client *);
static char buffer[1024];
/* parse()
*
* given a raw buffer, parses it and generates parv and parc
*/
void
parse(client::client *client_p, char *pbuffer, char *bufend)
{
client::client *from = client_p;
char *end;
int res;
int numeric = 0;
struct Message *mptr;
struct MsgBuf msgbuf;
s_assert(my_connect(*client_p));
s_assert(client_p->localClient->F != NULL);
if(is_any_dead(*client_p))
return;
end = bufend - 1;
/* XXX this should be done before parse() is called */
if(*end == '\n')
*end-- = '\0';
if(*end == '\r')
*end = '\0';
res = msgbuf_parse(&msgbuf, pbuffer);
if (res)
{
ServerStats.is_empt++;
return;
}
if (msgbuf.origin != NULL && is_server(*client_p))
{
from = find_client(msgbuf.origin);
/* didnt find any matching client, issue a kill */
if(from == NULL)
{
ServerStats.is_unpf++;
remove_unknown(client_p, msgbuf.origin, pbuffer);
return;
}
/* fake direction, hmm. */
if(from->from != client_p)
{
ServerStats.is_wrdi++;
cancel_clients(client_p, from);
return;
}
}
if(rfc1459::is_digit(*msgbuf.cmd) && rfc1459::is_digit(*(msgbuf.cmd + 1)) && rfc1459::is_digit(*(msgbuf.cmd + 2)))
{
mptr = NULL;
numeric = atoi(msgbuf.cmd);
ServerStats.is_num++;
}
else
{
mptr = cmd_dict[msgbuf.cmd];
/* no command or its encap only, error */
if(!mptr || !mptr->cmd)
{
if(is_person(*from))
{
sendto_one(from, form_str(ERR_UNKNOWNCOMMAND),
me.name, from->name, msgbuf.cmd);
}
ServerStats.is_unco++;
return;
}
mptr->bytes += msgbuf.parselen;
}
if(mptr == NULL)
{
do_numeric(numeric, client_p, from, msgbuf.n_para, msgbuf.para);
return;
}
if(handle_command(mptr, &msgbuf, client_p, from) < -1)
{
char *p;
for (p = pbuffer; p <= end; p += 8)
{
/* HACK HACK */
/* Its expected this nasty code can be removed
* or rewritten later if still needed.
*/
if((p + 8) > end)
{
for (; p <= end; p++)
{
ilog(L_MAIN, "%02x |%c", p[0], p[0]);
}
}
else
ilog(L_MAIN,
"%02x %02x %02x %02x %02x %02x %02x %02x |%c%c%c%c%c%c%c%c",
p[0], p[1], p[2], p[3], p[4], p[5],
p[6], p[7], p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
}
}
}
/*
* handle_command
*
* inputs - pointer to message block
* - pointer to message buffer
* - pointer to client
* - pointer to client message is from
* output - -1 if error from server
* side effects -
*/
static int
handle_command(struct Message *mptr, struct MsgBuf *msgbuf_p, client::client *client_p, client::client *from)
{
struct MessageEntry ehandler;
MessageHandler handler = 0;
char squitreason[80];
if(is_any_dead(*client_p))
return -1;
if(is_server(*client_p))
mptr->rcount++;
mptr->count++;
ehandler = mptr->handlers[from->handler];
handler = ehandler.handler;
/* check right amount of params is passed... --is */
if(msgbuf_p->n_para < ehandler.min_para ||
(ehandler.min_para && EmptyString(msgbuf_p->para[ehandler.min_para - 1])))
{
if(!is_server(*client_p))
{
sendto_one(client_p, form_str(ERR_NEEDMOREPARAMS),
me.name,
EmptyString(client_p->name) ? "*" : client_p->name,
mptr->cmd);
if(my(*client_p))
return (1);
else
return (-1);
}
sendto_realops_snomask(sno::GENERAL, L_ALL,
"Dropping server %s due to (invalid) command '%s'"
" with only %zu arguments (expecting %zu).",
client_p->name, mptr->cmd, msgbuf_p->n_para, ehandler.min_para);
ilog(L_SERVER,
"Insufficient parameters (%zu < %zu) for command '%s' from %s.",
msgbuf_p->n_para, ehandler.min_para, mptr->cmd, client_p->name);
snprintf(squitreason, sizeof squitreason,
"Insufficient parameters (%zu < %zu) for command '%s'",
msgbuf_p->n_para, ehandler.min_para, mptr->cmd);
exit_client(client_p, client_p, client_p, squitreason);
return (-1);
}
handler(msgbuf_p, *client_p, *from, msgbuf_p->n_para, msgbuf_p->para);
return (1);
}
void
handle_encap(struct MsgBuf *msgbuf_p, client::client *client_p, client::client *source_p,
const char *command, int parc, const char *parv[])
{
struct Message *mptr;
struct MessageEntry ehandler;
MessageHandler handler = 0;
mptr = cmd_dict[command];
if(mptr == NULL || mptr->cmd == NULL)
return;
ehandler = mptr->handlers[ENCAP_HANDLER];
handler = ehandler.handler;
if((size_t)parc < ehandler.min_para ||
(ehandler.min_para && EmptyString(parv[ehandler.min_para - 1])))
return;
handler(msgbuf_p, *client_p, *source_p, parc, parv);
}
/* mod_add_cmd
*
* inputs - command name
* - pointer to struct Message
* output - none
* side effects - load this one command name
* msg->count msg->bytes is modified in place, in
* modules address space. Might not want to do that...
*/
void
mod_add_cmd(struct Message *msg)
{
s_assert(msg != NULL);
if(msg == NULL)
return;
if (cmd_dict[msg->cmd] != NULL)
{
s_assert(0);
return;
}
msg->count = 0;
msg->rcount = 0;
msg->bytes = 0;
cmd_dict[msg->cmd] = msg;
}
/* mod_del_cmd
*
* inputs - command name
* output - none
* side effects - unload this one command name
*/
void
mod_del_cmd(struct Message *msg)
{
s_assert(msg != NULL);
if(msg == NULL)
return;
cmd_dict.erase(msg->cmd);
}
/* cancel_clients()
*
* inputs - client who sent us the message, client with fake
* direction
* outputs - a given warning about the fake direction
* side effects -
*/
static void
cancel_clients(client::client *client_p, client::client *source_p)
{
/* ok, fake prefix happens naturally during a burst on a nick
* collision with TS5, we cant kill them because one client has to
* survive, so we just send an error.
*/
if(is_server(*source_p) || is_me(*source_p))
{
sendto_realops_snomask(sno::DEBUG, L_ALL,
"Message for %s[%s] from %s",
source_p->name, source_p->from->name,
client_p->name);
}
else
{
sendto_realops_snomask(sno::DEBUG, L_ALL,
"Message for %s[%s@%s!%s] from %s (TS, ignored)",
source_p->name,
source_p->username,
source_p->host,
source_p->from->name,
client_p->name);
}
}
/* remove_unknown()
*
* inputs - client who gave us message, supposed sender, buffer
* output -
* side effects - kills issued for clients, squits for servers
*/
static void
remove_unknown(client::client *client_p, const char *lsender, char *lbuffer)
{
int slen = strlen(lsender);
char sid[4];
client::client *server;
/* meepfoo is a nickname (ignore)
* #XXXXXXXX is a UID (KILL)
* #XX is a SID (SQUIT)
* meep.foo is a server (SQUIT)
*/
if((rfc1459::is_digit(lsender[0]) && slen == 3) ||
(strchr(lsender, '.') != NULL))
{
sendto_realops_snomask(sno::DEBUG, L_ALL,
"Unknown prefix (%s) from %s, Squitting %s",
lbuffer, client_p->name, lsender);
sendto_one(client_p,
":%s SQUIT %s :(Unknown prefix (%s) from %s)",
get_id(&me, client_p), lsender,
lbuffer, client_p->name);
}
else if(!rfc1459::is_digit(lsender[0]))
;
else if(slen != 9)
sendto_realops_snomask(sno::DEBUG, L_ALL,
"Invalid prefix (%s) from %s",
lbuffer, client_p->name);
else
{
memcpy(sid, lsender, 3);
sid[3] = '\0';
server = find_server(NULL, sid);
if (server != NULL && server->from == client_p)
sendto_one(client_p, ":%s KILL %s :%s (Unknown Client)",
get_id(&me, client_p), lsender, me.name);
}
}
/*
*
* parc number of arguments ('sender' counted as one!)
* parv[1]..parv[parc-1]
* pointers to additional parameters, this is a NULL
* terminated list (parv[parc] == NULL).
*
* *WARNING*
* Numerics are mostly error reports. If there is something
* wrong with the message, just *DROP* it! Don't even think of
* sending back a neat error message -- big danger of creating
* a ping pong error message...
*/
static void
do_numeric(int numeric, client::client *client_p, client::client *source_p, int parc, const char *parv[])
{
client::client *target_p;
chan::chan *chptr;
if(parc < 2 || !is_server(*source_p))
return;
/* Remap low number numerics. */
if(numeric < 100)
numeric += 100;
/*
* Prepare the parameter portion of the message into 'buffer'.
* (Because the buffer is twice as large as the message buffer
* for the socket, no overflow can occur here... ...on current
* assumptions--bets are off, if these are changed --msa)
* Note: if buffer is non-empty, it will begin with SPACE.
*/
if(parc > 1)
{
char *t = buffer; /* Current position within the buffer */
int i;
int tl; /* current length of presently being built string in t */
for (i = 2; i < (parc - 1); i++)
{
tl = sprintf(t, " %s", parv[i]);
t += tl;
}
sprintf(t, " :%s", parv[parc - 1]);
}
if((target_p = find_client(parv[1])) != NULL)
{
if(is_me(*target_p))
{
/*
* We shouldn't get numerics sent to us,
* any numerics we do get indicate a bug somewhere..
*/
/* ugh. this is here because of nick collisions. when two servers
* relink, they burst each other their nicks, then perform collides.
* if there is a nick collision, BOTH servers will kill their own
* nicks, and BOTH will kill the other servers nick, which wont exist,
* because it will have been already killed by the local server.
*
* unfortunately, as we cant guarantee other servers will do the
* "right thing" on a nick collision, we have to keep both kills.
* ergo we need to ignore ERR_NOSUCHNICK. --fl_
*/
/* quick comment. This _was_ tried. i.e. assume the other servers
* will do the "right thing" and kill a nick that is colliding.
* unfortunately, it did not work. --Dianora
*/
/* note, now we send PING on server connect, we can
* also get ERR_NOSUCHSERVER..
*/
if(numeric != ERR_NOSUCHNICK &&
numeric != ERR_NOSUCHSERVER)
sendto_realops_snomask(sno::GENERAL, L_ADMIN,
"*** %s(via %s) sent a %03d numeric to me: %s",
source_p->name,
client_p->name, numeric, buffer);
return;
}
else if(target_p->from == client_p)
{
/* This message changed direction (nick collision?)
* ignore it.
*/
return;
}
/* csircd will send out unknown umode flag for +a (admin), drop it here. */
if(numeric == ERR_UMODEUNKNOWNFLAG && my(*target_p))
return;
/* Fake it for server hiding, if its our client */
sendto_one(target_p, ":%s %03d %s%s",
get_id(source_p, target_p), numeric,
get_id(target_p, target_p), buffer);
return;
}
else if((chptr = chan::get(parv[1], std::nothrow)) != NULL)
sendto_channel_flags(client_p, chan::ALL_MEMBERS, source_p, chptr,
"%03d %s%s",
numeric, chptr->name.c_str(), buffer);
}
void
m_not_oper(struct MsgBuf *msgbuf_p, client::client &client, client::client &source, int parc, const char *parv[])
{
sendto_one_numeric(&source, ERR_NOPRIVILEGES, form_str(ERR_NOPRIVILEGES));
}
void
m_unregistered(struct MsgBuf *msgbuf_p, client::client &client, client::client &source, int parc, const char *parv[])
{
if(is_any_server(client))
return;
/* bit of a hack.
* I don't =really= want to waste a bit in a flag
* number_of_nick_changes is only really valid after the client
* is fully registered..
*/
if(client.localClient->number_of_nick_changes == 0)
{
sendto_one(&client, form_str(ERR_NOTREGISTERED), me.name);
client.localClient->number_of_nick_changes++;
}
}
void
m_registered(struct MsgBuf *msgbuf_p, client::client &client, client::client &source, int parc, const char *parv[])
{
sendto_one(&client, form_str(ERR_ALREADYREGISTRED), me.name, source.name);
}
void
m_ignore(struct MsgBuf *msgbuf_p, client::client &client, client::client &source, int parc, const char *parv[])
{
/* Does nothing */
}
} // namespace ircd

View file

@ -1,386 +0,0 @@
/*
* ircd-ratbox: A slightly useful ircd
* reject.c: reject users with prejudice
*
* Copyright (C) 2003 Aaron Sethman <androsyn@ratbox.org>
* Copyright (C) 2003-2005 ircd-ratbox development team
*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
namespace ircd {
static rb_patricia_tree_t *reject_tree;
static rb_dlink_list delay_exit;
static rb_dlink_list reject_list;
static rb_dlink_list throttle_list;
static rb_patricia_tree_t *throttle_tree;
static void throttle_expires(void *unused);
typedef struct _reject_data
{
rb_dlink_node rnode;
time_t time;
unsigned int count;
uint32_t mask_hashv;
} reject_t;
typedef struct _delay_data
{
rb_dlink_node node;
rb_fde_t *F;
} delay_t;
typedef struct _throttle
{
rb_dlink_node node;
time_t last;
int count;
} throttle_t;
unsigned long
delay_exit_length(void)
{
return rb_dlink_list_length(&delay_exit);
}
static void
reject_exit(void *unused)
{
rb_dlink_node *ptr, *ptr_next;
delay_t *ddata;
static const char *errbuf = "ERROR :Closing Link: (*** Banned (cache))\r\n";
RB_DLINK_FOREACH_SAFE(ptr, ptr_next, delay_exit.head)
{
ddata = (delay_t *)ptr->data;
rb_write(ddata->F, errbuf, strlen(errbuf));
rb_close(ddata->F);
rb_free(ddata);
}
delay_exit.head = delay_exit.tail = NULL;
delay_exit.length = 0;
}
static void
reject_expires(void *unused)
{
rb_dlink_node *ptr, *next;
rb_patricia_node_t *pnode;
reject_t *rdata;
RB_DLINK_FOREACH_SAFE(ptr, next, reject_list.head)
{
pnode = (rb_patricia_node_t *)ptr->data;
rdata = (reject_t *)pnode->data;
if(rdata->time + ConfigFileEntry.reject_duration > rb_current_time())
continue;
rb_dlinkDelete(ptr, &reject_list);
rb_free(rdata);
rb_patricia_remove(reject_tree, pnode);
}
}
void
init_reject(void)
{
reject_tree = rb_new_patricia(PATRICIA_BITS);
throttle_tree = rb_new_patricia(PATRICIA_BITS);
rb_event_add("reject_exit", reject_exit, NULL, DELAYED_EXIT_TIME);
rb_event_add("reject_expires", reject_expires, NULL, 60);
rb_event_add("throttle_expires", throttle_expires, NULL, 10);
}
unsigned long
throttle_size(void)
{
unsigned long count;
rb_dlink_node *ptr;
rb_patricia_node_t *pnode;
throttle_t *t;
count = 0;
RB_DLINK_FOREACH(ptr, throttle_list.head)
{
pnode = (rb_patricia_node_t *)ptr->data;
t = (throttle_t *)pnode->data;
if (t->count > ConfigFileEntry.throttle_count)
count++;
}
return count;
}
void
add_reject(client::client *client_p, const char *mask1, const char *mask2)
{
rb_patricia_node_t *pnode;
reject_t *rdata;
uint32_t hashv;
/* Reject is disabled */
if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_duration == 0)
return;
hashv = 0;
if (mask1 != NULL)
hashv ^= fnv_hash_upper((const unsigned char *)mask1, 32);
if (mask2 != NULL)
hashv ^= fnv_hash_upper((const unsigned char *)mask2, 32);
if((pnode = rb_match_ip(reject_tree, (struct sockaddr *)&client_p->localClient->ip)) != NULL)
{
rdata = (reject_t *)pnode->data;
rdata->time = rb_current_time();
rdata->count++;
}
else
{
int bitlen = 32;
#ifdef RB_IPV6
if(GET_SS_FAMILY(&client_p->localClient->ip) == AF_INET6)
bitlen = 128;
#endif
pnode = make_and_lookup_ip(reject_tree, (struct sockaddr *)&client_p->localClient->ip, bitlen);
pnode->data = rdata = (reject_t *)rb_malloc(sizeof(reject_t));
rb_dlinkAddTail(pnode, &rdata->rnode, &reject_list);
rdata->time = rb_current_time();
rdata->count = 1;
}
rdata->mask_hashv = hashv;
}
int
check_reject(rb_fde_t *F, struct sockaddr *addr)
{
rb_patricia_node_t *pnode;
reject_t *rdata;
delay_t *ddata;
/* Reject is disabled */
if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_duration == 0)
return 0;
pnode = rb_match_ip(reject_tree, addr);
if(pnode != NULL)
{
rdata = (reject_t *)pnode->data;
rdata->time = rb_current_time();
if(rdata->count > (unsigned long)ConfigFileEntry.reject_after_count)
{
ddata = (delay_t *)rb_malloc(sizeof(delay_t));
ServerStats.is_rej++;
rb_setselect(F, RB_SELECT_WRITE | RB_SELECT_READ, NULL, NULL);
ddata->F = F;
rb_dlinkAdd(ddata, &ddata->node, &delay_exit);
return 1;
}
}
/* Caller does what it wants */
return 0;
}
int
is_reject_ip(struct sockaddr *addr)
{
rb_patricia_node_t *pnode;
reject_t *rdata;
int duration;
/* Reject is disabled */
if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_duration == 0)
return 0;
pnode = rb_match_ip(reject_tree, addr);
if(pnode != NULL)
{
rdata = (reject_t *)pnode->data;
if(rdata->count > (unsigned long)ConfigFileEntry.reject_after_count)
{
duration = rdata->time + ConfigFileEntry.reject_duration - rb_current_time();
return duration > 0 ? duration : 1;
}
}
return 0;
}
void
flush_reject(void)
{
rb_dlink_node *ptr, *next;
rb_patricia_node_t *pnode;
reject_t *rdata;
RB_DLINK_FOREACH_SAFE(ptr, next, reject_list.head)
{
pnode = (rb_patricia_node_t *)ptr->data;
rdata = (reject_t *)pnode->data;
rb_dlinkDelete(ptr, &reject_list);
rb_free(rdata);
rb_patricia_remove(reject_tree, pnode);
}
}
int
remove_reject_ip(const char *ip)
{
rb_patricia_node_t *pnode;
/* Reject is disabled */
if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_duration == 0)
return -1;
if((pnode = rb_match_string(reject_tree, ip)) != NULL)
{
reject_t *rdata = (reject_t *)pnode->data;
rb_dlinkDelete(&rdata->rnode, &reject_list);
rb_free(rdata);
rb_patricia_remove(reject_tree, pnode);
return 1;
}
return 0;
}
int
remove_reject_mask(const char *mask1, const char *mask2)
{
rb_dlink_node *ptr, *next;
rb_patricia_node_t *pnode;
reject_t *rdata;
uint32_t hashv;
int n = 0;
hashv = 0;
if (mask1 != NULL)
hashv ^= fnv_hash_upper((const unsigned char *)mask1, 32);
if (mask2 != NULL)
hashv ^= fnv_hash_upper((const unsigned char *)mask2, 32);
RB_DLINK_FOREACH_SAFE(ptr, next, reject_list.head)
{
pnode = (rb_patricia_node_t *)ptr->data;
rdata = (reject_t *)pnode->data;
if (rdata->mask_hashv == hashv)
{
rb_dlinkDelete(ptr, &reject_list);
rb_free(rdata);
rb_patricia_remove(reject_tree, pnode);
n++;
}
}
return n;
}
int
throttle_add(struct sockaddr *addr)
{
throttle_t *t;
rb_patricia_node_t *pnode;
if((pnode = rb_match_ip(throttle_tree, addr)) != NULL)
{
t = (throttle_t *)pnode->data;
if(t->count > ConfigFileEntry.throttle_count)
{
ServerStats.is_thr++;
return 1;
}
/* Stop penalizing them after they've been throttled */
t->last = rb_current_time();
t->count++;
} else {
int bitlen = 32;
#ifdef RB_IPV6
if(GET_SS_FAMILY(addr) == AF_INET6)
bitlen = 128;
#endif
t = (throttle_t *)rb_malloc(sizeof(throttle_t));
t->last = rb_current_time();
t->count = 1;
pnode = make_and_lookup_ip(throttle_tree, addr, bitlen);
pnode->data = t;
rb_dlinkAdd(pnode, &t->node, &throttle_list);
}
return 0;
}
int
is_throttle_ip(struct sockaddr *addr)
{
throttle_t *t;
rb_patricia_node_t *pnode;
int duration;
if((pnode = rb_match_ip(throttle_tree, addr)) != NULL)
{
t = (throttle_t *)pnode->data;
if(t->count > ConfigFileEntry.throttle_count)
{
duration = t->last + ConfigFileEntry.throttle_duration - rb_current_time();
return duration > 0 ? duration : 1;
}
}
return 0;
}
void
flush_throttle(void)
{
rb_dlink_node *ptr, *next;
rb_patricia_node_t *pnode;
throttle_t *t;
RB_DLINK_FOREACH_SAFE(ptr, next, throttle_list.head)
{
pnode = (rb_patricia_node_t *)ptr->data;
t = (throttle_t *)pnode->data;
rb_dlinkDelete(ptr, &throttle_list);
rb_free(t);
rb_patricia_remove(throttle_tree, pnode);
}
}
static void
throttle_expires(void *unused)
{
rb_dlink_node *ptr, *next;
rb_patricia_node_t *pnode;
throttle_t *t;
RB_DLINK_FOREACH_SAFE(ptr, next, throttle_list.head)
{
pnode = (rb_patricia_node_t *)ptr->data;
t = (throttle_t *)pnode->data;
if(t->last + ConfigFileEntry.throttle_duration > rb_current_time())
continue;
rb_dlinkDelete(ptr, &throttle_list);
rb_free(t);
rb_patricia_remove(throttle_tree, pnode);
}
}
} // namespace ircd