/*
 * Copyright (c) 2001-2007, OpenFWTK Development Group
 * All rights reserved. See LICENSE.
 */

/*
 * OpenFWTK project list management functions
 *
 * (C) Copyright 2001,2002 ArkanoiD <ark@eltex.ru>
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include "firewall.h"
#include "firewall2.h"
#include "fwfunc.h"

static char* moduleId ATTR_UNUSED = "$Id: list.c,v 1.3 2007/09/10 02:49:13 arkenoi Exp $";

void
addlist(entry,list)
char	*entry;
char	***list;
{
	int	entries = 0;

	if (!*list) 
		*list = (char **) xmalloc (sizeof(char *) *2);
	else {
		for (entries=0; (*list)[entries]; entries++)
			;
		*list = (char **) xrealloc (*list,(entries+2)*sizeof(char*));
	}
	if (!*list)
		return;
	(*list)[entries] = xstrdup(entry);
	(*list)[entries+1] = NULL;
}

void
freelist(list)
char	***list;
{
	char	**xp;

	if (!*list)
		return;

	for (xp = *list; *xp; xp++) 
		free(*xp);

	free(*list);
	*list = NULL;
}

int
searchlist(entry,list)
char	*entry;
char	**list;
{

	char	**xp;

/*
 *	Empty list means no restrictions and thus always matches
 */

	if (!list)
		return(0);

	for (xp = list; *xp; xp++) {
		if (**xp == '!' && !strcmp(*xp + 1,entry)) 
			return(1);
		if (!strcmp(*xp,entry) || !strcmp(*xp,"*"))
			return(0);
	}
	return(1);
}

int
searchlistnc(entry,list)
char	*entry;
char	**list;
{

	char	**xp;

/*
 *	Empty list means no restrictions and thus always matches
 */

	if (!list)
		return(0);

	for (xp = list; *xp; xp++) {
		if (**xp == '!' && !strcasecmp(*xp + 1,entry)) 
			return(1);
		if(!strcasecmp(*xp,entry) || !strcasecmp(*xp,"*"))
			return(0);
	}
	return(1);
}

int
searchlisth(entry,list)
char	*entry;
char	**list;
{

	char	**xp;

/*
 *	Empty list means no restrictions and thus always matches
 */

	if (!list)
		return(0);

	for (xp = list; *xp; xp++) {
		if (**xp == '!' && hostmatch(*xp + 1,entry)) 
			return(1);
		if(hostmatch(*xp,entry))
			return(0);
	}
	return(1);
}


