2002-05-27 13:22:39 +02:00
|
|
|
/*
|
|
|
|
* ngIRCd -- The Next Generation IRC Daemon
|
2011-12-24 13:34:25 +01:00
|
|
|
* Copyright (c)2001-2011 Alexander Barton (alex@barton.de) and Contributors.
|
2002-05-27 13:22:39 +02:00
|
|
|
*
|
2002-12-12 13:24:18 +01:00
|
|
|
* 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.
|
|
|
|
* Please read the file COPYING, README and AUTHORS for more information.
|
2002-05-27 13:22:39 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "portab.h"
|
|
|
|
|
2010-12-27 17:14:14 +01:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Management of IRC lists: ban, invite, etc.
|
|
|
|
*/
|
|
|
|
|
2002-05-27 13:22:39 +02:00
|
|
|
#include "imp.h"
|
|
|
|
#include <assert.h>
|
|
|
|
|
2002-06-09 15:18:23 +02:00
|
|
|
#include "defines.h"
|
2002-05-27 13:22:39 +02:00
|
|
|
#include "conn.h"
|
|
|
|
#include "channel.h"
|
2002-06-09 15:18:23 +02:00
|
|
|
#include "log.h"
|
2002-09-08 02:55:45 +02:00
|
|
|
#include "match.h"
|
|
|
|
#include "messages.h"
|
|
|
|
#include "irc-write.h"
|
2002-06-09 15:18:23 +02:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2004-01-17 04:17:49 +01:00
|
|
|
#include <strings.h>
|
2002-05-27 13:22:39 +02:00
|
|
|
|
|
|
|
#include "exp.h"
|
|
|
|
#include "lists.h"
|
|
|
|
|
2006-08-12 13:56:24 +02:00
|
|
|
#define MASK_LEN (2*CLIENT_HOST_LEN)
|
2002-06-09 15:18:23 +02:00
|
|
|
|
2006-12-07 18:57:20 +01:00
|
|
|
struct list_elem {
|
|
|
|
struct list_elem *next;
|
2005-03-19 19:43:48 +01:00
|
|
|
char mask[MASK_LEN];
|
2011-12-25 14:19:45 +01:00
|
|
|
char *reason;
|
2011-12-24 13:34:25 +01:00
|
|
|
time_t valid_until; /** 0: unlimited; 1: once; t(>1): until t */
|
2006-12-07 18:57:20 +01:00
|
|
|
};
|
2002-09-08 02:55:45 +02:00
|
|
|
|
|
|
|
|
2006-12-07 18:57:20 +01:00
|
|
|
GLOBAL const char *
|
|
|
|
Lists_GetMask(const struct list_elem *e)
|
2002-05-27 13:22:39 +02:00
|
|
|
{
|
2006-12-07 18:57:20 +01:00
|
|
|
return e->mask;
|
|
|
|
}
|
2002-05-27 13:22:39 +02:00
|
|
|
|
2011-12-25 14:19:45 +01:00
|
|
|
GLOBAL const char *
|
|
|
|
Lists_GetReason(const struct list_elem *e)
|
|
|
|
{
|
|
|
|
assert(e != NULL);
|
|
|
|
return e->reason;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLOBAL time_t
|
|
|
|
Lists_GetValidity(const struct list_elem *e)
|
|
|
|
{
|
|
|
|
assert(e != NULL);
|
|
|
|
return e->valid_until;
|
|
|
|
}
|
2002-05-27 13:22:39 +02:00
|
|
|
|
2006-12-07 18:57:20 +01:00
|
|
|
GLOBAL struct list_elem*
|
|
|
|
Lists_GetFirst(const struct list_head *h)
|
2002-05-27 13:22:39 +02:00
|
|
|
{
|
2006-12-07 18:57:20 +01:00
|
|
|
return h->first;
|
|
|
|
}
|
2002-06-09 15:18:23 +02:00
|
|
|
|
|
|
|
|
2006-12-07 18:57:20 +01:00
|
|
|
GLOBAL struct list_elem*
|
|
|
|
Lists_GetNext(const struct list_elem *e)
|
2004-04-25 17:40:19 +02:00
|
|
|
{
|
2006-12-07 18:57:20 +01:00
|
|
|
return e->next;
|
|
|
|
}
|
2004-04-25 17:40:19 +02:00
|
|
|
|
2011-12-25 14:19:45 +01:00
|
|
|
/**
|
|
|
|
* Add a new mask to a list.
|
|
|
|
*
|
|
|
|
* @param header List head.
|
|
|
|
* @param Mask The IRC mask to add to the list.
|
|
|
|
* @param ValidUntil 0: unlimited, 1: only once, t>1: until given time_t.
|
|
|
|
* @param Reason Reason string or NULL, if no reason should be saved.
|
|
|
|
* @return true on success, false otherwise.
|
|
|
|
*/
|
2006-12-07 18:57:20 +01:00
|
|
|
bool
|
2011-12-25 14:19:45 +01:00
|
|
|
Lists_Add(struct list_head *header, const char *Mask, time_t ValidUntil,
|
|
|
|
const char *Reason)
|
2002-09-08 02:55:45 +02:00
|
|
|
{
|
2006-12-07 18:57:20 +01:00
|
|
|
struct list_elem *e, *newelem;
|
2002-09-08 02:55:45 +02:00
|
|
|
|
2006-12-07 18:57:20 +01:00
|
|
|
assert( header != NULL );
|
2002-09-08 02:55:45 +02:00
|
|
|
assert( Mask != NULL );
|
|
|
|
|
2006-12-07 18:57:20 +01:00
|
|
|
if (Lists_CheckDupeMask(header, Mask )) return true;
|
2004-04-25 17:40:19 +02:00
|
|
|
|
2006-12-07 18:57:20 +01:00
|
|
|
e = Lists_GetFirst(header);
|
2004-04-25 17:40:19 +02:00
|
|
|
|
2006-12-07 18:57:20 +01:00
|
|
|
newelem = malloc(sizeof(struct list_elem));
|
|
|
|
if( ! newelem ) {
|
|
|
|
Log( LOG_EMERG, "Can't allocate memory for new Ban/Invite entry!" );
|
2007-01-29 22:13:26 +01:00
|
|
|
return false;
|
2004-04-25 17:40:19 +02:00
|
|
|
}
|
|
|
|
|
2011-12-25 14:19:45 +01:00
|
|
|
strlcpy(newelem->mask, Mask, sizeof(newelem->mask));
|
|
|
|
if (Reason) {
|
|
|
|
newelem->reason = malloc(strlen(Reason) + 1);
|
|
|
|
if (newelem->reason)
|
|
|
|
strlcpy(newelem->reason, Reason, strlen(Reason) + 1);
|
|
|
|
else
|
|
|
|
Log(LOG_EMERG,
|
|
|
|
"Can't allocate memory for new list reason text!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
newelem->reason = NULL;
|
2011-12-24 13:34:25 +01:00
|
|
|
newelem->valid_until = ValidUntil;
|
2006-12-07 18:57:20 +01:00
|
|
|
newelem->next = e;
|
|
|
|
header->first = newelem;
|
2004-04-25 17:40:19 +02:00
|
|
|
|
2006-12-07 18:57:20 +01:00
|
|
|
return true;
|
|
|
|
}
|
2002-09-08 02:55:45 +02:00
|
|
|
|
|
|
|
|
2006-12-07 18:57:20 +01:00
|
|
|
static void
|
|
|
|
Lists_Unlink(struct list_head *header, struct list_elem *p, struct list_elem *victim)
|
2004-04-25 17:40:19 +02:00
|
|
|
{
|
2006-12-07 18:57:20 +01:00
|
|
|
assert(victim != NULL);
|
|
|
|
assert(header != NULL);
|
2004-04-25 17:40:19 +02:00
|
|
|
|
2006-12-07 18:57:20 +01:00
|
|
|
if (p) p->next = victim->next;
|
|
|
|
else header->first = victim->next;
|
2004-04-25 17:40:19 +02:00
|
|
|
|
2011-12-25 14:19:45 +01:00
|
|
|
if (victim->reason)
|
|
|
|
free(victim->reason);
|
2006-12-07 18:57:20 +01:00
|
|
|
free(victim);
|
|
|
|
}
|
2002-09-08 02:55:45 +02:00
|
|
|
|
|
|
|
|
2005-03-19 19:43:48 +01:00
|
|
|
GLOBAL void
|
2006-12-07 18:57:20 +01:00
|
|
|
Lists_Del(struct list_head *header, const char *Mask)
|
2002-09-08 02:55:45 +02:00
|
|
|
{
|
2006-12-07 18:57:20 +01:00
|
|
|
struct list_elem *e, *last, *victim;
|
2002-09-08 02:55:45 +02:00
|
|
|
|
2006-12-07 18:57:20 +01:00
|
|
|
assert( header != NULL );
|
2002-09-08 02:55:45 +02:00
|
|
|
assert( Mask != NULL );
|
|
|
|
|
|
|
|
last = NULL;
|
2006-12-07 18:57:20 +01:00
|
|
|
e = Lists_GetFirst(header);
|
|
|
|
while( e ) {
|
|
|
|
if(strcasecmp( e->mask, Mask ) == 0 ) {
|
|
|
|
LogDebug("Deleted \"%s\" from list", e->mask);
|
|
|
|
victim = e;
|
|
|
|
e = victim->next;
|
|
|
|
Lists_Unlink(header, last, victim);
|
|
|
|
continue;
|
2002-09-08 02:55:45 +02:00
|
|
|
}
|
2006-12-07 18:57:20 +01:00
|
|
|
last = e;
|
|
|
|
e = e->next;
|
2002-09-08 02:55:45 +02:00
|
|
|
}
|
2006-12-07 18:57:20 +01:00
|
|
|
}
|
2002-06-02 17:23:16 +02:00
|
|
|
|
|
|
|
|
2006-12-07 18:57:20 +01:00
|
|
|
GLOBAL void
|
|
|
|
Lists_Free(struct list_head *head)
|
2002-05-27 13:22:39 +02:00
|
|
|
{
|
2006-12-07 18:57:20 +01:00
|
|
|
struct list_elem *e, *victim;
|
2002-09-08 02:55:45 +02:00
|
|
|
|
2006-12-07 18:57:20 +01:00
|
|
|
assert(head != NULL);
|
2002-05-27 13:22:39 +02:00
|
|
|
|
2006-12-07 18:57:20 +01:00
|
|
|
e = head->first;
|
|
|
|
head->first = NULL;
|
|
|
|
while (e) {
|
|
|
|
LogDebug("Deleted \"%s\" from invite list" , e->mask);
|
|
|
|
victim = e;
|
|
|
|
e = e->next;
|
2011-12-25 14:19:45 +01:00
|
|
|
if (victim->reason)
|
|
|
|
free(victim->reason);
|
|
|
|
free(victim);
|
2002-09-08 02:55:45 +02:00
|
|
|
}
|
2006-12-07 18:57:20 +01:00
|
|
|
}
|
2002-05-27 13:22:39 +02:00
|
|
|
|
|
|
|
|
2006-12-07 18:57:20 +01:00
|
|
|
GLOBAL bool
|
|
|
|
Lists_CheckDupeMask(const struct list_head *h, const char *Mask )
|
2002-06-09 15:18:23 +02:00
|
|
|
{
|
2006-12-07 18:57:20 +01:00
|
|
|
struct list_elem *e;
|
|
|
|
e = h->first;
|
|
|
|
while (e) {
|
|
|
|
if (strcasecmp( e->mask, Mask ) == 0 )
|
|
|
|
return true;
|
|
|
|
e = e->next;
|
2002-06-09 15:18:23 +02:00
|
|
|
}
|
2006-12-07 18:57:20 +01:00
|
|
|
return false;
|
|
|
|
}
|
2002-06-09 15:18:23 +02:00
|
|
|
|
|
|
|
|
2008-04-19 14:11:25 +02:00
|
|
|
GLOBAL const char *
|
|
|
|
Lists_MakeMask(const char *Pattern)
|
2002-09-08 02:55:45 +02:00
|
|
|
{
|
2005-01-26 14:23:24 +01:00
|
|
|
/* This function generats a valid IRC mask of "any" string. This
|
|
|
|
* mask is only valid until the next call to Lists_MakeMask(),
|
|
|
|
* because a single global buffer is used. You have to copy the
|
|
|
|
* generated mask to some sane location yourself! */
|
2002-09-08 02:55:45 +02:00
|
|
|
|
2005-03-19 19:43:48 +01:00
|
|
|
static char TheMask[MASK_LEN];
|
|
|
|
char *excl, *at;
|
2002-09-08 02:55:45 +02:00
|
|
|
|
2002-10-03 23:49:59 +02:00
|
|
|
assert( Pattern != NULL );
|
2002-09-08 02:55:45 +02:00
|
|
|
|
|
|
|
excl = strchr( Pattern, '!' );
|
|
|
|
at = strchr( Pattern, '@' );
|
|
|
|
|
|
|
|
if(( at ) && ( at < excl )) excl = NULL;
|
|
|
|
|
|
|
|
if(( ! at ) && ( ! excl ))
|
|
|
|
{
|
2005-01-26 14:23:24 +01:00
|
|
|
/* Neither "!" nor "@" found: use string as nick name */
|
2002-12-26 17:25:43 +01:00
|
|
|
strlcpy( TheMask, Pattern, sizeof( TheMask ) - 5 );
|
|
|
|
strlcat( TheMask, "!*@*", sizeof( TheMask ));
|
2002-09-08 02:55:45 +02:00
|
|
|
return TheMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(( ! at ) && ( excl ))
|
|
|
|
{
|
2005-01-26 14:23:24 +01:00
|
|
|
/* Domain part is missing */
|
2002-12-26 17:25:43 +01:00
|
|
|
strlcpy( TheMask, Pattern, sizeof( TheMask ) - 3 );
|
|
|
|
strlcat( TheMask, "@*", sizeof( TheMask ));
|
2002-09-08 02:55:45 +02:00
|
|
|
return TheMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(( at ) && ( ! excl ))
|
|
|
|
{
|
2005-01-26 14:23:24 +01:00
|
|
|
/* User name is missing */
|
2002-09-08 02:55:45 +02:00
|
|
|
*at = '\0'; at++;
|
2005-01-26 14:23:24 +01:00
|
|
|
strlcpy( TheMask, Pattern, sizeof( TheMask ) - 5 );
|
2002-12-26 17:25:43 +01:00
|
|
|
strlcat( TheMask, "!*@", sizeof( TheMask ));
|
|
|
|
strlcat( TheMask, at, sizeof( TheMask ));
|
2002-09-08 02:55:45 +02:00
|
|
|
return TheMask;
|
|
|
|
}
|
|
|
|
|
2005-01-26 14:23:24 +01:00
|
|
|
/* All parts (nick, user and domain name) are given */
|
2002-12-26 17:25:43 +01:00
|
|
|
strlcpy( TheMask, Pattern, sizeof( TheMask ));
|
2002-09-08 02:55:45 +02:00
|
|
|
return TheMask;
|
|
|
|
} /* Lists_MakeMask */
|
|
|
|
|
|
|
|
|
2006-12-07 18:57:20 +01:00
|
|
|
bool
|
|
|
|
Lists_Check( struct list_head *header, CLIENT *Client)
|
2002-09-08 02:55:45 +02:00
|
|
|
{
|
2011-12-24 13:34:25 +01:00
|
|
|
struct list_elem *e, *last, *next;
|
2002-09-08 02:55:45 +02:00
|
|
|
|
2006-12-07 18:57:20 +01:00
|
|
|
assert( header != NULL );
|
2002-09-08 02:55:45 +02:00
|
|
|
|
2006-12-07 18:57:20 +01:00
|
|
|
e = header->first;
|
2002-09-08 02:55:45 +02:00
|
|
|
last = NULL;
|
|
|
|
|
2011-12-24 13:34:25 +01:00
|
|
|
while (e) {
|
|
|
|
next = e->next;
|
|
|
|
if (e->valid_until > 1 && e->valid_until < time(NULL)) {
|
|
|
|
/* Entry is expired, delete it */
|
|
|
|
LogDebug("Deleted \"%s\" from list (expired).",
|
|
|
|
e->mask);
|
|
|
|
Lists_Unlink(header, last, e);
|
|
|
|
e = next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Match(e->mask, Client_Mask(Client))) {
|
|
|
|
if (e->valid_until == 1 ) {
|
|
|
|
/* Entry is valid only once, delete it */
|
|
|
|
LogDebug("Deleted \"%s\" from list (used).",
|
|
|
|
e->mask);
|
2006-12-07 18:57:20 +01:00
|
|
|
Lists_Unlink(header, last, e);
|
2002-09-08 02:55:45 +02:00
|
|
|
}
|
2006-12-07 18:57:20 +01:00
|
|
|
return true;
|
2002-09-08 02:55:45 +02:00
|
|
|
}
|
2006-12-07 18:57:20 +01:00
|
|
|
last = e;
|
2011-12-24 13:34:25 +01:00
|
|
|
e = next;
|
2002-09-08 02:55:45 +02:00
|
|
|
}
|
|
|
|
|
2005-03-19 19:43:48 +01:00
|
|
|
return false;
|
2006-12-07 18:57:20 +01:00
|
|
|
}
|
2002-09-08 19:06:07 +02:00
|
|
|
|
2002-05-27 13:22:39 +02:00
|
|
|
/* -eof- */
|