File: tcpdaccess.c

package info (click to toggle)
courier 0.60.0-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 52,288 kB
  • ctags: 12,677
  • sloc: ansic: 165,348; cpp: 24,820; sh: 16,410; perl: 6,839; makefile: 3,621; yacc: 289; sed: 16
file content (69 lines) | stat: -rw-r--r-- 1,020 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
** Copyright 1998 - 2000 Double Precision, Inc.
** See COPYING for distribution information.
*/

#if	HAVE_CONFIG_H
#include	"config.h"
#endif
#include	<stdio.h>
#include	<errno.h>
#include	<string.h>
#include	<stdlib.h>
#include	<ctype.h>
#include	"dbobj.h"

static const char rcsid[]="$Id: tcpdaccess.c,v 1.4 2005/05/18 17:30:37 mrsam Exp $";

static struct dbobj db;
static int db_isopen=0, db_isinit=0;

int openaccess(const char *filename)
{
	if (!db_isinit)
	{
		dbobj_init(&db);
		db_isinit=1;
	}
	if (db_isopen)
	{
		dbobj_close(&db);
		db_isopen=0;
	}

	if (dbobj_open(&db, filename, "R"))
		return (-1);
	db_isopen=1;
	return (0);
}

void closeaccess()
{
	if (!db_isopen)	return;
	dbobj_close(&db);
	db_isopen=0;
}

char *chkaccess(const char *ip)
{
size_t	l;
char	*p, *q;

	if (!db_isopen)	return (0);


	p=dbobj_fetch(&db, ip, strlen(ip), &l, "");

	if (!p)	return (0);
	q=(char *)malloc(l+1);
	if (!q)
	{
		perror("malloc");
		free(p);
		return (0);
	}
	memcpy(q, p, l);
	q[l]=0;
	free(p);
	return (q);
}