-
Notifications
You must be signed in to change notification settings - Fork 6
/
security.cpp
261 lines (209 loc) · 7.76 KB
/
security.cpp
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/***************************************************************************
security.cpp - antispam&SSL and other secur.
-------------------
begin : Tue Apr 3 2001
copyright : (C) 2001 by Alexander Bilichenko
email : [email protected]
***************************************************************************/
#include "basetypes.h"
#include "security.h"
#include "error.h"
#include "messages.h"
/* find out is IP and Msg in buf
* also find latest date of record in list
* if ip found it returns index of it in buf, otherwise -1 returned
*/
int CheckForIpAndMsgInQueue(DWORD ip, DWORD Msg, SReadQueue *buf, int n, int &Alatest)
{
Alatest = 0; // Max time
for(int i = 0; i < n; i++)
{
// also find latest date
if(buf[Alatest].Date > buf[i].Date) Alatest = i;
if(buf[i].IP == ip && buf[i].MsgIndex == Msg)
{
return i;
}
}
return -1;
}
/* */
int CheckReadValidity(DWORD IP, DWORD MsgIndex)
{
WCFILE *f;
SReadQueue buf[READQUEUE_LENGTH + 1];
time_t t = time(NULL);
int x, Alatest;
if((f = wcfopen(F_ANTISPAM, FILE_ACCESS_MODES_RW)) == NULL)
{
if((f = wcfopen(F_ANTISPAM, FILE_ACCESS_MODES_CW)) != NULL)
{
// create and save post list
lock_file(f);
// TODO: replace this sequence with a single truncate() call
SSpamQueue *mbuf = (SSpamQueue*)calloc(SPAMQUEUE_LENGTH, sizeof(SSpamQueue));
if (!mbuf) {
unlock_file(f);
wcfclose(f);
printhtmlerror();
}
if(!fCheckedWrite(mbuf, SPAMQUEUE_LENGTH*sizeof(SSpamQueue), f)) {
unlock_file(f);
wcfclose(f);
printhtmlerror();
}
free(mbuf);
// create and save read list
memset(buf, 0, sizeof(SReadQueue)*READQUEUE_LENGTH);
if(!fCheckedWrite(buf, READQUEUE_LENGTH*sizeof(SReadQueue), f)) {
unlock_file(f);
wcfclose(f);
printhtmlerror();
}
unlock_file(f);
wcfclose(f);
if((f = wcfopen(F_ANTISPAM, FILE_ACCESS_MODES_RW)) == NULL)
printhtmlerror();
}
else printhtmlerror();
}
lock_file(f);
if(wcfseek(f, READQUEUE_PREFIX, SEEK_SET) != 0) {
unlock_file(f);
wcfclose(f);
printhtmlerror();
}
if(!fCheckedRead(buf, READQUEUE_LENGTH*sizeof(SReadQueue), f)) {
unlock_file(f);
wcfclose(f);
printhtmlerror();
}
if((x = CheckForIpAndMsgInQueue(IP, MsgIndex, buf, READQUEUE_LENGTH, Alatest)) != -1)
{
unlock_file(f);
wcfclose(f);
// return status = READ INVALID
return 0;
}
// update: delete entry with most early date
// and set our client information
buf[Alatest].IP = IP;
buf[Alatest].Date = t;
buf[Alatest].MsgIndex = MsgIndex;
if(wcfseek(f, READQUEUE_PREFIX, SEEK_SET) != 0) {
unlock_file(f);
wcfclose(f);
printhtmlerror();
}
if(!fCheckedWrite(buf, READQUEUE_LENGTH*sizeof(SReadQueue), f)) {
unlock_file(f);
wcfclose(f);
printhtmlerror();
}
unlock_file(f);
wcfclose(f);
// return status - READ VALID
return 1;
}
/* find out is IP in buf
* if ip found it returns index of it in buf, otherwise -1 returned
*/
int CheckForIpInQueue(DWORD ip, SSpamQueue *buf, int n, int &Alatest)
{
Alatest = 0; // Max time
for(int i = 0; i < n; i++)
{
// also find latest date
if(buf[Alatest].Date > buf[i].Date) Alatest = i;
if(buf[i].IP == ip)
{
return i;
}
}
return -1;
}
int MarkPostfromIPInvalid(DWORD IP)
{
int Alatest;
WCFILE *f;
time_t t = time(NULL);
SSpamQueue buf[SPAMQUEUE_LENGTH + 1];
if((f = wcfopen(F_ANTISPAM, FILE_ACCESS_MODES_RW)) == NULL)
printhtmlerror();
lock_file(f);
if(!fCheckedRead(buf, SPAMQUEUE_LENGTH*sizeof(SSpamQueue), f))
printhtmlerror();
Alatest = 0; // Max time
for(int i = 0; i < SPAMQUEUE_LENGTH; i++)
{
// find earliest date
if(buf[Alatest].Date > buf[i].Date) Alatest = i;
}
// update: delete entry with most early date
buf[Alatest].IP = IP;
buf[Alatest].Date = t;
if(wcfseek(f, 0, SEEK_SET) != 0)
printhtmlerror();
if(!fCheckedWrite(buf, SPAMQUEUE_LENGTH*sizeof(SSpamQueue), f))
printhtmlerror();
unlock_file(f);
wcfclose(f);
return 0;
}
/* check if it valid enter of user with IP
* return 1 if valid, 0 otherwise
*/
int CheckPostfromIPValidity(DWORD IP, int TimeInterval)
{
WCFILE *f;
SSpamQueue buf[SPAMQUEUE_LENGTH + 1];
time_t t = time(NULL);
int x, Alatest;
if((f = wcfopen(F_ANTISPAM, FILE_ACCESS_MODES_RW)) == NULL)
{
if((f = wcfopen(F_ANTISPAM, FILE_ACCESS_MODES_CW)) != NULL)
{
// create and save post list
lock_file(f);
memset(buf, 0, sizeof(SSpamQueue)*SPAMQUEUE_LENGTH);
if(!fCheckedWrite(buf, SPAMQUEUE_LENGTH*sizeof(SSpamQueue), f)) {
unlock_file(f);
wcfclose(f);
printhtmlerror();
}
// create and save read list
// TODO: replace this sequence with a single truncate() call
SReadQueue *mbuf = (SReadQueue*)calloc(READQUEUE_LENGTH, sizeof(SReadQueue));
if (!mbuf) {
unlock_file(f);
wcfclose(f);
printhtmlerror();
}
if(!fCheckedWrite(mbuf, READQUEUE_LENGTH*sizeof(SReadQueue), f)) {
unlock_file(f);
wcfclose(f);
printhtmlerror();
}
free(mbuf);
unlock_file(f);
wcfclose(f);
return 1;
}
else printhtmlerror();
}
lock_file(f);
if(!fCheckedRead(buf, SPAMQUEUE_LENGTH*sizeof(SSpamQueue), f))
printhtmlerror();
unlock_file(f);
wcfclose(f);
if((x = CheckForIpInQueue(IP, buf, SPAMQUEUE_LENGTH, Alatest)) != -1)
{
if(buf[x].Date + TimeInterval > t && buf[x].Date < t )
{
// return status - POST INVALID
return 0;
}
}
// return status - POST VALID
return 1;
}