Skip to content

Commit

Permalink
Rewrite/cleanup huge portion of TKL handling (16 files updated, but
Browse files Browse the repository at this point in the history
src/modules/tkl.c is the main one).
Also move DB writing/reading functions to src/misc.c so they can be
removed out of channeldb and tkldb.

Important note to current tkldb users:
Unfortunately due to the major cleanup I had to remove upgrading
for previously saved tkl db files. That seemed not worth the effort
for maybe <15 current users or so. It also makes the tkldb code
a lot more cleaner. Otherwise it would be a huge mess.

Currently a FIXME item: spamfilter support in RMTKL.
  • Loading branch information
syzop committed Aug 31, 2019
1 parent 0116c4f commit 8a6c848
Show file tree
Hide file tree
Showing 16 changed files with 1,359 additions and 1,168 deletions.
24 changes: 22 additions & 2 deletions include/h.h
Original file line number Diff line number Diff line change
Expand Up @@ -645,8 +645,20 @@ extern MODVAR int (*tkl_hash)(unsigned int c);
extern MODVAR char (*tkl_typetochar)(int type);
extern MODVAR int (*tkl_chartotype)(char c);
extern MODVAR char *(*tkl_type_string)(aTKline *tk);
extern MODVAR aTKline *(*tkl_add_line)(int type, char *usermask, char *hostmask, char *reason, char *setby,
time_t expire_at, time_t set_at, time_t spamf_tkl_duration, char *spamf_tkl_reason, MatchType spamf_match_type, int soft, int flags);
extern MODVAR aTKline *(*tkl_add_serverban)(int type, char *usermask, char *hostmask, char *reason, char *setby,
time_t expire_at, time_t set_at, int soft, int flags);
extern MODVAR aTKline *(*tkl_add_nameban)(int type, char *name, int hold, char *reason, char *setby,
time_t expire_at, time_t set_at, int flags);
extern MODVAR aTKline *(*tkl_add_spamfilter)(int type, unsigned short target, unsigned short action, aMatch *match, char *setby,
time_t expire_at, time_t set_at,
time_t spamf_tkl_duration, char *spamf_tkl_reason,
int flags);
extern MODVAR aTKline *(*find_tkl_serverban)(int type, char *usermask, char *hostmask, int softban);
extern MODVAR aTKline *(*find_tkl_nameban)(int type, char *name, int hold);
extern MODVAR aTKline *(*find_tkl_spamfilter)(int type, char *match_string, unsigned short action, unsigned short target);
extern MODVAR void (*sendnotice_tkl_del)(char *removed_by, aTKline *tkl);
extern MODVAR void (*sendnotice_tkl_add)(aTKline *tkl);
extern MODVAR void (*free_tkl)(aTKline *tkl);
extern MODVAR aTKline *(*tkl_del_line)(aTKline *tkl);
extern MODVAR void (*tkl_check_local_remove_shun)(aTKline *tmp);
extern MODVAR int (*find_tkline_match)(aClient *cptr, int skip_soft);
Expand Down Expand Up @@ -861,3 +873,11 @@ extern int history_request(aClient *acptr, char *object, HistoryFilter *filter);
extern int history_del(char *object, int max_lines, long max_time);
extern int history_destroy(char *object);
extern void special_delayed_unloading(void);
extern int write_int64(FILE *fd, uint64_t t);
extern int write_int32(FILE *fd, uint32_t t);
extern int read_int64(FILE *fd, uint64_t *t);
extern int read_int32(FILE *fd, uint32_t *t);
extern int read_data(FILE *fd, void *buf, size_t len);
extern int write_data(FILE *fd, const void *buf, size_t len);
extern int write_str(FILE *fd, char *x);
extern int read_str(FILE *fd, char **x);
10 changes: 9 additions & 1 deletion include/modules.h
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,7 @@ _UNREAL_ERROR(_hook_error_incompatible, "Incompatible hook function. Check argum
#define EFUNC_REGISTER_USER 7
#define EFUNC_TKL_HASH 8
#define EFUNC_TKL_TYPETOCHAR 9
#define EFUNC_TKL_ADD_LINE 10
#define EFUNC_TKL_ADD_SERVERBAN 10
#define EFUNC_TKL_DEL_LINE 11
#define EFUNC_TKL_CHECK_LOCAL_REMOVE_SHUN 12
#define EFUNC_TKL_EXPIRE 13
Expand Down Expand Up @@ -1227,6 +1227,14 @@ _UNREAL_ERROR(_hook_error_incompatible, "Incompatible hook function. Check argum
#define EFUNC_BROADCAST_MD_GLOBALVAR_CMD 67
#define EFUNC_TKL_IP_HASH 68
#define EFUNC_TKL_IP_HASH_TYPE 69
#define EFUNC_TKL_ADD_NAMEBAN 70
#define EFUNC_TKL_ADD_SPAMFILTER 71
#define EFUNC_SENDNOTICE_TKL_ADD 72
#define EFUNC_SENDNOTICE_TKL_DEL 73
#define EFUNC_FREE_TKL 74
#define EFUNC_FIND_TKL_SERVERBAN 75
#define EFUNC_FIND_TKL_NAMEBAN 76
#define EFUNC_FIND_TKL_SPAMFILTER 77

/* Module flags */
#define MODFLAG_NONE 0x0000
Expand Down
47 changes: 34 additions & 13 deletions include/struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ typedef struct aloopStruct LoopStruct;
typedef struct ConfItem aConfItem;
typedef struct t_kline aTKline;
typedef struct _spamfilter Spamfilter;
typedef struct _serverban ServerBan;
typedef struct _nameban NameBan;
typedef struct _spamexcept SpamExcept;
/* New Config Stuff */
typedef struct _conditionalconfig ConditionalConfig;
typedef struct _configentry ConfigEntry;
typedef struct _configfile ConfigFile;
Expand Down Expand Up @@ -711,11 +712,17 @@ struct Server {
#define TKL_ZAP 0x0002
#define TKL_GLOBAL 0x0004
#define TKL_SHUN 0x0008
#define TKL_QUIET 0x0010
#define TKL_SPAMF 0x0020
#define TKL_NICK 0x0040
#define TKL_NAME 0x0040
#define TKL_EXCEPT 0x0080

#define TKLIsServerBan(tkl) ((tkl)->type & (TKL_KILL|TKL_ZAP|TKL_SHUN))
#define TKLIsServerBanType(tpe) ((tpe) & (TKL_KILL|TKL_ZAP|TKL_SHUN))
#define TKLIsSpamfilter(tkl) ((tkl)->type & TKL_SPAMF)
#define TKLIsSpamfilterType(tpe) ((tpe) & TKL_SPAMF)
#define TKLIsNameBan(tkl) ((tkl)->type & TKL_NAME)
#define TKLIsNameBanType(tpe) ((tpe) & TKL_NAME)

#define SPAMF_CHANMSG 0x0001 /* c */
#define SPAMF_USERMSG 0x0002 /* p */
#define SPAMF_USERNOTICE 0x0004 /* n */
Expand All @@ -730,14 +737,30 @@ struct Server {
/* Other flags only for function calls: */
#define SPAMFLAG_NOWARN 0x0001

/** Spamfilter sub-struct of TKL entry */
/** Spamfilter sub-struct of TKL entry (Spamfilter) */
struct _spamfilter {
unsigned short target;
unsigned short action; /**< Ban action, see BAN_ACT* */
aMatch *expr; /**< Spamfilter matcher */
aMatch *match; /**< Spamfilter matcher */
char *tkl_reason; /**< Reason to use for bans placed by this spamfilter, escaped by unreal_encodespace(). */
time_t tkl_duration; /**< Duration of bans placed by this spamfilter */
};

/** Server ban sub-struct of TKL entry (KLINE/GLINE/ZLINE/GZLINE/SHUN) */
struct _serverban {
char *usermask; /**< User mask */
char *hostmask; /**< Host mask */
unsigned short subtype; /**< See TKL_SUBTYPE_* */
char *reason; /**< Reason */
};

/* Name ban sub-struct of TKL entry (QLINE) */
struct _nameban {
char hold; /**< nickname hold is used by services */
char *name; /**< the nick or channel that is banned */
char *reason; /**< Reason */
};

#define TKL_SUBTYPE_NONE 0x0000
#define TKL_SUBTYPE_SOFT 0x0001 /* (require SASL) */

Expand All @@ -747,17 +770,15 @@ struct _spamfilter {
struct t_kline {
aTKline *prev, *next;
unsigned int type; /**< TKL type. One of TKL_*, such as TKL_KILL|TKL_GLOBAL for gline */
unsigned short subtype; /* TKL subtype. For spamfilter one of SPAMF_*, otherwise TKL_SUBTYPE_* */
unsigned short flags; /**< One of TKL_FLAG_*, such as TKL_FLAG_CONFIG */
char *set_by; /**< By who was this entry added */
time_t set_at; /**< When this entry was added */
time_t expire_at; /**< When this entry will expire */
union {
Spamfilter *spamf;
Spamfilter *spamfilter;
ServerBan *serverban;
NameBan *nameban;
} ptr;
char usermask[USERLEN + 3]; /**< User mask [different for spamfilter] */
char *hostmask; /**< Host mask [different for spamfilter] */
char *reason; /**< Reason [different for spamfilter] */
char *setby; /**< By who was this entry added */
time_t expire_at; /**< When this entry will expire */
time_t set_at; /**< When this entry was added */
};

/** A spamfilter except entry */
Expand Down
28 changes: 14 additions & 14 deletions src/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ static NameValue _LogFlags[] = {
static NameValue ExceptTklFlags[] = {
{ 0, "all" },
{ TKL_GLOBAL|TKL_KILL, "gline" },
{ TKL_GLOBAL|TKL_NICK, "gqline" },
{ TKL_GLOBAL|TKL_NAME, "gqline" },
{ TKL_GLOBAL|TKL_ZAP, "gzline" },
{ TKL_NICK, "qline" },
{ TKL_NAME, "qline" },
{ TKL_GLOBAL|TKL_SHUN, "shun" }
};

Expand Down Expand Up @@ -1644,34 +1644,34 @@ void postconf_defaults(void)
// which is more meaningful.
for (tk = tklines[tkl_hash('q')]; tk; tk = tk->next)
{
if (tk->type != TKL_NICK)
if (tk->type != TKL_NAME)
continue;
if (!tk->setby)
if (!tk->set_by)
{
if (me.name[0] != '\0')
tk->setby = strdup(me.name);
tk->set_by = strdup(me.name);
else
tk->setby = strdup(conf_me->name ? conf_me->name : "~server~");
tk->set_by = strdup(conf_me->name ? conf_me->name : "~server~");
}
}

for (tk = tklines[tkl_hash('f')]; tk; tk = tk->next)
{
if (tk->type != TKL_SPAMF)
continue; /* global entry or something else.. */
if (!strcmp(tk->ptr.spamf->tkl_reason, "<internally added by ircd>"))
if (!strcmp(tk->ptr.spamfilter->tkl_reason, "<internally added by ircd>"))
{
MyFree(tk->ptr.spamf->tkl_reason);
tk->ptr.spamf->tkl_reason = strdup(encoded);
tk->ptr.spamf->tkl_duration = SPAMFILTER_BAN_TIME;
MyFree(tk->ptr.spamfilter->tkl_reason);
tk->ptr.spamfilter->tkl_reason = strdup(encoded);
tk->ptr.spamfilter->tkl_duration = SPAMFILTER_BAN_TIME;
}
/* This one is even more ugly, but our config crap is VERY confusing :[ */
if (!tk->setby)
if (!tk->set_by)
{
if (me.name[0] != '\0')
tk->setby = strdup(me.name);
tk->set_by = strdup(me.name);
else
tk->setby = strdup(conf_me->name ? conf_me->name : "~server~");
tk->set_by = strdup(conf_me->name ? conf_me->name : "~server~");
}
}

Expand Down Expand Up @@ -6882,7 +6882,7 @@ int _conf_require(ConfigFile *conf, ConfigEntry *ce)
if (!reason)
reason = strdup("-");

tkl_add_line(TKL_KILL, usermask, hostmask, reason, "-config-", 0, TStime(), 0, NULL, 0, 1, TKL_FLAG_CONFIG);
tkl_add_serverban(TKL_KILL, usermask, hostmask, reason, "-config-", 0, TStime(), 1, TKL_FLAG_CONFIG);
safefree(usermask);
safefree(hostmask);
safefree(reason);
Expand Down
159 changes: 159 additions & 0 deletions src/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1418,3 +1418,162 @@ time_t server_time_to_unix_time(const char *tbuf)
ret = my_timegm(&tm);
return ret;
}

/** Write a 64 bit integer.
* @param fd File descriptor
* @param t The value to write
* @example write_int64(fd, 1234);
* @returns 1 on success, 0 on failure.
*/
int write_int64(FILE *fd, uint64_t t)
{
if (fwrite(&t, 1, sizeof(t), fd) < sizeof(t))
return 0;
return 1;
}

/** Write a 32 bit integer.
* @param fd File descriptor
* @param t The value to write
* @example write_int32(fd, 1234);
* @returns 1 on success, 0 on failure.
*/
int write_int32(FILE *fd, uint32_t t)
{
if (fwrite(&t, 1, sizeof(t), fd) < sizeof(t))
return 0;
return 1;
}

/** Read a 64 bit integer.
* @param fd File descriptor
* @param t The value to write
* @example read_int64(fd, &var);
* @returns 1 on success, 0 on failure.
*/
int read_int64(FILE *fd, uint64_t *t)
{
if (fread(t, 1, sizeof(uint64_t), fd) < sizeof(uint64_t))
return 0;
return 1;
}

/** Read a 64 bit integer.
* @param fd File descriptor
* @param t The value to write
* @example read_int64(fd, &var);
* @returns 1 on success, 0 on failure.
*/
int read_int32(FILE *fd, uint32_t *t)
{
if (fread(t, 1, sizeof(uint32_t), fd) < sizeof(uint32_t))
return 0;
return 1;
}

/** Read binary data.
* @param fd File descriptor
* @param buf Pointer to buffer
* @param len Size of buffer
* @example read_data(fd, buf, sizeof(buf));
* @notes This function is not used much, in most cases
* you should use read_str(), read_int32() or
* read_int64() instead.
* @returns 1 on success, 0 on failure.
*/
int read_data(FILE *fd, void *buf, size_t len)
{
if (fread(buf, 1, len, fd) < len)
return 0;
return 1;
}

/** Write binary data.
* @param fd File descriptor
* @param buf Pointer to buffer
* @param len Size of buffer
* @example write_data(fd, buf, sizeof(buf));
* @notes This function is not used much, in most cases
* you should use write_str(), write_int32() or
* write_int64() instead.
* @returns 1 on success, 0 on failure.
*/
int write_data(FILE *fd, const void *buf, size_t len)
{
if (fwrite(buf, 1, len, fd) < len)
return 0;
return 1;
}

/** Write a string.
* @param fd File descriptor
* @param x Pointer to string
* @example write_str(fd, "hello there!");
* @notes This function can write a string up to 65534
* characters, which should be plenty for usage
* in UnrealIRCd.
* Note that 'x' can safely be NULL.
* @returns 1 on success, 0 on failure.
*/
int write_str(FILE *fd, char *x)
{
uint16_t len;

len = x ? strlen(x) : 0xffff;
if (!write_data(fd, &len, sizeof(len)))
return 0;
if ((len > 0) && (len < 0xffff))
{
if (!write_data(fd, x, len))
return 0;
}
return 1;
}

/** Read a string.
* @param fd File descriptor
* @param x Pointer to string pointer
* @example write_str(fd, &str);
* @notes This function will allocate memory for the data
* and set the string pointer to this value.
* If a NULL pointer was written via write_str()
* then read_str() may also return a NULL pointer.
* @returns 1 on success, 0 on failure.
*/
int read_str(FILE *fd, char **x)
{
uint16_t len;
size_t size;

*x = NULL;

if (!read_data(fd, &len, sizeof(len)))
return 0;

if (len == 0xffff)
{
/* Magic value meaning NULL */
*x = NULL;
return 1;
}

if (len == 0)
{
/* 0 means empty string */
*x = strdup("");
return 1;
}

if (len > 10000)
return 0;

size = len;
*x = MyMallocEx(size + 1);
if (!read_data(fd, *x, size))
{
safefree(*x);
return 0;
}
(*x)[len] = 0;
return 1;
}
Loading

0 comments on commit 8a6c848

Please sign in to comment.