/
badip.c
198 lines (141 loc) · 3.68 KB
/
badip.c
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* $Id: badip.c,v 1.4 2008/04/11 10:52:18 devel Exp $ (c) 2006 D.Brockhaus
*
* $Log: badip.c,v $
* Revision 1.4 2008/04/11 10:52:18 devel
* added spam protection
*
* Revision 1.3 2007/07/09 11:19:46 devel
* table merc -> table merc0
*
* Revision 1.2 2007/02/24 14:09:54 devel
* NWO first checkin, feb 24
*
* Revision 1.1 2006/09/22 09:55:46 devel
* Initial revision
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <errno.h>
#include <math.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <mysql/mysql.h>
#include <mysql/mysqld_error.h>
#include "badip.h"
static int get_count(MYSQL *mysql,unsigned int ip,int timeout)
{
MYSQL_RES *result;
MYSQL_ROW row;
char buf[256];
int cnt;
sprintf(buf,"select count(*) from merc0.badip where IP=%u and t>=%d",ip,(int)(time(NULL)-timeout));
if (mysql_query(mysql,buf)) {
printf("select: Error: %s (%d)",mysql_error(mysql),mysql_errno(mysql));
return 42;
}
if (!(result=mysql_store_result(mysql))) {
printf("store: Error: %s (%d)",mysql_error(mysql),mysql_errno(mysql));
return 42;
}
if (!(row=mysql_fetch_row(result))) return 42;
cnt=atoi(row[0]);
mysql_free_result(result);
return cnt;
}
int is_badpass_ip(MYSQL *mysql,unsigned int ip)
{
int cnt;
cnt=get_count(mysql,ip,60);
if (cnt>3) return 1;
cnt=get_count(mysql,ip,60*60);
if (cnt>8) return 1;
cnt=get_count(mysql,ip,60*60*24);
if (cnt>25) return 1;
return 0;
}
void add_badpass_ip(MYSQL *mysql,unsigned int ip)
{
char buf[256];
sprintf(buf,"insert merc0.badip values(%u,%d)",ip,(int)time(NULL));
if (mysql_query(mysql,buf)) {
printf("select: Error: %s (%d)",mysql_error(mysql),mysql_errno(mysql));
return;
}
}
void clean_badpass_ips(MYSQL *mysql)
{
char buf[256];
sprintf(buf,"delete from merc0.badip where t<%d",(int)(time(NULL)-60*60*24*7));
if (mysql_query(mysql,buf)) {
printf("select: Error: %s (%d)",mysql_error(mysql),mysql_errno(mysql));
return;
}
}
/*
create table badip (IP int unsigned not null, t int not null, key lookup(IP,t), key(t));
*/
static int get_count2(MYSQL *mysql,unsigned int ip,int timeout)
{
MYSQL_RES *result;
MYSQL_ROW row;
char buf[256];
int cnt;
sprintf(buf,"select count(*) from merc0.createip where IP=%u and t>=%d",ip,(int)(time(NULL)-timeout));
if (mysql_query(mysql,buf)) {
printf("select: Error: %s (%d)",mysql_error(mysql),mysql_errno(mysql));
return 42;
}
if (!(result=mysql_store_result(mysql))) {
printf("store: Error: %s (%d)",mysql_error(mysql),mysql_errno(mysql));
return 42;
}
if (!(row=mysql_fetch_row(result))) return 42;
cnt=atoi(row[0]);
mysql_free_result(result);
return cnt;
}
int is_create_ip(MYSQL *mysql,unsigned int ip)
{
int cnt;
cnt=get_count2(mysql,ip,60);
if (cnt>3) return 1;
cnt=get_count2(mysql,ip,60*60);
if (cnt>8) return 1;
cnt=get_count2(mysql,ip,60*60*24*7);
if (cnt>25) return 1;
return 0;
}
void add_create_ip(MYSQL *mysql,unsigned int ip)
{
char buf[256];
sprintf(buf,"insert merc0.createip values(%u,%d)",ip,(int)time(NULL));
if (mysql_query(mysql,buf)) {
printf("select: Error: %s (%d)",mysql_error(mysql),mysql_errno(mysql));
return;
}
}
void clean_createpass_ips(MYSQL *mysql)
{
char buf[256];
sprintf(buf,"delete from merc0.createip where t<%d",(int)(time(NULL)-60*60*24*7));
if (mysql_query(mysql,buf)) {
printf("select: Error: %s (%d)",mysql_error(mysql),mysql_errno(mysql));
return;
}
}
/*
use merc0;
create table createip (IP int unsigned not null, t int not null, key lookup(IP,t), key(t));
*/