1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (C) 1996-2009 by Andrew Church <achurch@achurch.org> |
5 |
* Copyright (c) 2012-2014 ircd-hybrid development team |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
20 |
* USA |
21 |
*/ |
22 |
|
23 |
/*! \file conf_db.c |
24 |
* \brief Includes file utilities for database handling |
25 |
* \version $Id$ |
26 |
*/ |
27 |
|
28 |
|
29 |
#include "stdinc.h" |
30 |
#include "conf_db.h" |
31 |
#include "memory.h" |
32 |
#include "log.h" |
33 |
#include "send.h" |
34 |
#include "irc_string.h" |
35 |
#include "conf.h" |
36 |
#include "hostmask.h" |
37 |
#include "resv.h" |
38 |
|
39 |
|
40 |
/*! \brief Return the version number on the file. Return 0 if there is no version |
41 |
* number or the number doesn't make sense (i.e. less than 1 or greater |
42 |
* than FILE_VERSION). |
43 |
* |
44 |
* \param f dbFile Struct Member |
45 |
* \return int 0 if failure, 1 > is the version number |
46 |
*/ |
47 |
uint32_t |
48 |
get_file_version(struct dbFILE *f) |
49 |
{ |
50 |
uint32_t version = 0; |
51 |
|
52 |
if (read_uint32(&version, f) == -1) |
53 |
{ |
54 |
ilog(LOG_TYPE_IRCD, "Error reading version number on %s: %s", |
55 |
f->filename, strerror(errno)); |
56 |
return 0; |
57 |
} |
58 |
|
59 |
if (version < 1) |
60 |
{ |
61 |
ilog(LOG_TYPE_IRCD, "Invalid version number (%u) on %s", |
62 |
version, f->filename); |
63 |
return 0; |
64 |
} |
65 |
|
66 |
return version; |
67 |
} |
68 |
|
69 |
/*! \brief Write the current version number to the file. |
70 |
* \param f dbFile Struct Member |
71 |
* \return 0 on error, 1 on success. |
72 |
*/ |
73 |
int |
74 |
write_file_version(struct dbFILE *f, uint32_t version) |
75 |
{ |
76 |
if (write_uint32(version, f) == -1) |
77 |
{ |
78 |
ilog(LOG_TYPE_IRCD, "Error writing version number on %s", |
79 |
f->filename); |
80 |
return 0; |
81 |
} |
82 |
|
83 |
return 1; |
84 |
} |
85 |
|
86 |
/*! \brief Open the database for reading |
87 |
* \param service If error whom to return the error as |
88 |
* \param filename File to open as the database |
89 |
* \return dbFile struct |
90 |
*/ |
91 |
static struct dbFILE * |
92 |
open_db_read(const char *filename) |
93 |
{ |
94 |
struct dbFILE *f = MyMalloc(sizeof(*f)); |
95 |
FILE *fp = NULL; |
96 |
|
97 |
strlcpy(f->filename, filename, sizeof(f->filename)); |
98 |
|
99 |
f->mode = 'r'; |
100 |
fp = fopen(f->filename, "rb"); |
101 |
|
102 |
if (!fp) |
103 |
{ |
104 |
int errno_save = errno; |
105 |
|
106 |
if (errno != ENOENT) |
107 |
ilog(LOG_TYPE_IRCD, "Cannot read database file %s", f->filename); |
108 |
|
109 |
MyFree(f); |
110 |
errno = errno_save; |
111 |
return NULL; |
112 |
} |
113 |
|
114 |
f->fp = fp; |
115 |
return f; |
116 |
} |
117 |
|
118 |
/*! \brief Open the database for writting |
119 |
* \param filename File to open as the database |
120 |
* \param version Database Version |
121 |
* \return dbFile struct |
122 |
*/ |
123 |
static struct dbFILE * |
124 |
open_db_write(const char *filename, uint32_t version) |
125 |
{ |
126 |
struct dbFILE *f = MyMalloc(sizeof(*f)); |
127 |
int fd = 0; |
128 |
|
129 |
strlcpy(f->filename, filename, sizeof(f->filename)); |
130 |
|
131 |
filename = f->filename; |
132 |
f->mode = 'w'; |
133 |
|
134 |
snprintf(f->tempname, sizeof(f->tempname), "%s.new", filename); |
135 |
|
136 |
if (f->tempname[0] == '\0' || !strcmp(f->tempname, filename)) |
137 |
{ |
138 |
ilog(LOG_TYPE_IRCD, "Opening database file %s for write: Filename too long", |
139 |
filename); |
140 |
MyFree(f); |
141 |
errno = ENAMETOOLONG; |
142 |
return NULL; |
143 |
} |
144 |
|
145 |
remove(f->tempname); |
146 |
|
147 |
/* Use open() to avoid people sneaking a new file in under us */ |
148 |
fd = open(f->tempname, O_WRONLY | O_CREAT | O_EXCL, 0666); |
149 |
if (fd >= 0) |
150 |
f->fp = fdopen(fd, "wb"); |
151 |
|
152 |
if (!f->fp || !write_file_version(f, version)) |
153 |
{ |
154 |
int errno_save = errno; |
155 |
static int walloped = 0; |
156 |
|
157 |
if (!walloped++) |
158 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
159 |
"Cannot create temporary database file %s", |
160 |
f->tempname); |
161 |
|
162 |
errno = errno_save; |
163 |
ilog(LOG_TYPE_IRCD, "Cannot create temporary database file %s", |
164 |
f->tempname); |
165 |
|
166 |
if (f->fp) |
167 |
fclose(f->fp); |
168 |
|
169 |
remove(f->tempname); |
170 |
MyFree(f); |
171 |
|
172 |
errno = errno_save; |
173 |
return NULL; |
174 |
} |
175 |
|
176 |
return f; |
177 |
} |
178 |
|
179 |
/*! \brief Open a database file for reading (*mode == 'r') or writing (*mode == 'w'). |
180 |
* Return the stream pointer, or NULL on error. When opening for write, the |
181 |
* file actually opened is a temporary file, which will be renamed to the |
182 |
* original file on close. |
183 |
* |
184 |
* `version' is only used when opening a file for writing, and indicates the |
185 |
* version number to write to the file. |
186 |
* |
187 |
* \param filename File to open as the database |
188 |
* \param mode Mode for writting or reading |
189 |
* \param version Database Version |
190 |
* \return dbFile struct |
191 |
*/ |
192 |
struct dbFILE * |
193 |
open_db(const char *filename, const char *mode, uint32_t version) |
194 |
{ |
195 |
switch (*mode) |
196 |
{ |
197 |
case 'r': |
198 |
return open_db_read(filename); |
199 |
break; |
200 |
case 'w': |
201 |
return open_db_write(filename, version); |
202 |
break; |
203 |
default: |
204 |
errno = EINVAL; |
205 |
return NULL; |
206 |
} |
207 |
} |
208 |
|
209 |
/*! \brief Restore the database file to its condition before open_db(). This is |
210 |
* identical to close_db() for files open for reading; however, for files |
211 |
* open for writing, we discard the new temporary file instead of renaming |
212 |
* it over the old file. The value of errno is preserved. |
213 |
* |
214 |
* \param dbFile struct |
215 |
*/ |
216 |
void |
217 |
restore_db(struct dbFILE *f) |
218 |
{ |
219 |
int errno_save = errno; |
220 |
|
221 |
if (f->fp) |
222 |
fclose(f->fp); |
223 |
if (f->mode == 'w' && f->tempname[0]) |
224 |
remove(f->tempname); |
225 |
|
226 |
MyFree(f); |
227 |
errno = errno_save; |
228 |
} |
229 |
|
230 |
/*! \brief Close a database file. If the file was opened for write, moves the new |
231 |
* file over the old one, and logs/wallops an error message if the rename() |
232 |
* fails. |
233 |
* |
234 |
* \param dbFile struct |
235 |
*/ |
236 |
int |
237 |
close_db(struct dbFILE *f) |
238 |
{ |
239 |
int res; |
240 |
|
241 |
if (!f->fp) |
242 |
{ |
243 |
errno = EINVAL; |
244 |
return -1; |
245 |
} |
246 |
|
247 |
res = fclose(f->fp); |
248 |
f->fp = NULL; |
249 |
|
250 |
if (res != 0) |
251 |
return -1; |
252 |
|
253 |
if (f->mode == 'w' && f->tempname[0] && strcmp(f->tempname, f->filename)) |
254 |
{ |
255 |
if (rename(f->tempname, f->filename) < 0) |
256 |
{ |
257 |
int errno_save = errno; |
258 |
|
259 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, "Unable to move new " |
260 |
"data to database file %s; new data NOT saved.", |
261 |
f->filename); |
262 |
errno = errno_save; |
263 |
ilog(LOG_TYPE_IRCD, "Unable to move new data to database file %s; new " |
264 |
"data NOT saved.", f->filename); |
265 |
remove(f->tempname); |
266 |
} |
267 |
} |
268 |
|
269 |
MyFree(f); |
270 |
return 0; |
271 |
} |
272 |
|
273 |
/* |
274 |
* Read and write 2-, 4- and 8-byte quantities, pointers, and strings. All |
275 |
* multibyte values are stored in big-endian order (most significant byte |
276 |
* first). A pointer is stored as a byte, either 0 if NULL or 1 if not, |
277 |
* and read pointers are returned as either (void *)0 or (void *)1. A |
278 |
* string is stored with a 2-byte unsigned length (including the trailing |
279 |
* \0) first; a length of 0 indicates that the string pointer is NULL. |
280 |
* Written strings are truncated silently at 4294967294 bytes, and are always |
281 |
* null-terminated. |
282 |
*/ |
283 |
|
284 |
/*! \brief Read a unsigned 8bit integer |
285 |
* |
286 |
* \param ret 8bit integer to read |
287 |
* \param dbFile struct |
288 |
* \return -1 on error, 0 otherwise. |
289 |
*/ |
290 |
int |
291 |
read_uint8(uint8_t*ret, struct dbFILE *f) |
292 |
{ |
293 |
int c = fgetc(f->fp); |
294 |
|
295 |
if (c == EOF) |
296 |
return -1; |
297 |
|
298 |
*ret = c; |
299 |
return 0; |
300 |
} |
301 |
|
302 |
/*! \brief Write a 8bit integer |
303 |
* |
304 |
* \param ret 8bit integer to write |
305 |
* \param dbFile struct |
306 |
* \return -1 on error, 0 otherwise. |
307 |
*/ |
308 |
int |
309 |
write_uint8(uint8_t val, struct dbFILE *f) |
310 |
{ |
311 |
if (fputc(val, f->fp) == EOF) |
312 |
return -1; |
313 |
|
314 |
return 0; |
315 |
} |
316 |
|
317 |
/*! \brief Read a unsigned 8bit integer |
318 |
* |
319 |
* \param ret 8bit integer to read |
320 |
* \param dbFile struct |
321 |
* \return -1 on error, 0 otherwise. |
322 |
*/ |
323 |
int |
324 |
read_uint16(uint16_t *ret, struct dbFILE *f) |
325 |
{ |
326 |
int c1 = fgetc(f->fp); |
327 |
int c2 = fgetc(f->fp); |
328 |
|
329 |
if (c1 == EOF || c2 == EOF) |
330 |
return -1; |
331 |
|
332 |
*ret = c1 << 8 | c2; |
333 |
return 0; |
334 |
} |
335 |
|
336 |
/*! \brief Write a unsigned 16bit integer |
337 |
* |
338 |
* \param ret 16bit integer to write |
339 |
* \param dbFile struct |
340 |
* \return -1 on error, 0 otherwise. |
341 |
*/ |
342 |
int |
343 |
write_uint16(uint16_t val, struct dbFILE *f) |
344 |
{ |
345 |
if (fputc((val >> 8) & 0xFF, f->fp) == EOF || |
346 |
fputc(val & 0xFF, f->fp) == EOF) |
347 |
return -1; |
348 |
|
349 |
return 0; |
350 |
} |
351 |
|
352 |
/*! \brief Read a unsigned 32bit integer |
353 |
* |
354 |
* \param ret unsigned 32bit integer to read |
355 |
* \param dbFile struct |
356 |
* \return -1 on error, 0 otherwise. |
357 |
*/ |
358 |
int |
359 |
read_uint32(uint32_t *ret, struct dbFILE *f) |
360 |
{ |
361 |
int c1 = fgetc(f->fp); |
362 |
int c2 = fgetc(f->fp); |
363 |
int c3 = fgetc(f->fp); |
364 |
int c4 = fgetc(f->fp); |
365 |
|
366 |
if (c1 == EOF || c2 == EOF || c3 == EOF || c4 == EOF) |
367 |
return -1; |
368 |
|
369 |
*ret = c1 << 24 | c2 << 16 | c3 << 8 | c4; |
370 |
return 0; |
371 |
} |
372 |
|
373 |
|
374 |
/*! \brief Write a unsigned 32bit integer |
375 |
* |
376 |
* \param ret unsigned 32bit integer to write |
377 |
* \param dbFile struct |
378 |
* \return -1 on error, 0 otherwise. |
379 |
*/ |
380 |
int |
381 |
write_uint32(uint32_t val, struct dbFILE *f) |
382 |
{ |
383 |
if (fputc((val >> 24) & 0xFF, f->fp) == EOF) |
384 |
return -1; |
385 |
if (fputc((val >> 16) & 0xFF, f->fp) == EOF) |
386 |
return -1; |
387 |
if (fputc((val >> 8) & 0xFF, f->fp) == EOF) |
388 |
return -1; |
389 |
if (fputc((val) & 0xFF, f->fp) == EOF) |
390 |
return -1; |
391 |
return 0; |
392 |
} |
393 |
|
394 |
/*! \brief Read a unsigned 64bit integer |
395 |
* |
396 |
* \param ret unsigned 64bit integer to read |
397 |
* \param dbFile struct |
398 |
* \return -1 on error, 0 otherwise. |
399 |
*/ |
400 |
int |
401 |
read_uint64(uint64_t *ret, struct dbFILE *f) |
402 |
{ |
403 |
int64_t c1 = fgetc(f->fp); |
404 |
int64_t c2 = fgetc(f->fp); |
405 |
int64_t c3 = fgetc(f->fp); |
406 |
int64_t c4 = fgetc(f->fp); |
407 |
int64_t c5 = fgetc(f->fp); |
408 |
int64_t c6 = fgetc(f->fp); |
409 |
int64_t c7 = fgetc(f->fp); |
410 |
int64_t c8 = fgetc(f->fp); |
411 |
|
412 |
if (c1 == EOF || c2 == EOF || c3 == EOF || c4 == EOF || |
413 |
c5 == EOF || c6 == EOF || c7 == EOF || c8 == EOF) |
414 |
return -1; |
415 |
|
416 |
*ret = c1 << 56 | c2 << 48 | c3 << 40 | c4 << 32 | |
417 |
c5 << 24 | c6 << 16 | c7 << 8 | c8; |
418 |
return 0; |
419 |
} |
420 |
|
421 |
|
422 |
/*! \brief Write a unsigned 64bit integer |
423 |
* |
424 |
* \param ret unsigned 64bit integer to write |
425 |
* \param dbFile struct |
426 |
* \return -1 on error, 0 otherwise. |
427 |
*/ |
428 |
int |
429 |
write_uint64(uint64_t val, struct dbFILE *f) |
430 |
{ |
431 |
if (fputc((val >> 56) & 0xFF, f->fp) == EOF) |
432 |
return -1; |
433 |
if (fputc((val >> 48) & 0xFF, f->fp) == EOF) |
434 |
return -1; |
435 |
if (fputc((val >> 40) & 0xFF, f->fp) == EOF) |
436 |
return -1; |
437 |
if (fputc((val >> 32) & 0xFF, f->fp) == EOF) |
438 |
return -1; |
439 |
if (fputc((val >> 24) & 0xFF, f->fp) == EOF) |
440 |
return -1; |
441 |
if (fputc((val >> 16) & 0xFF, f->fp) == EOF) |
442 |
return -1; |
443 |
if (fputc((val >> 8) & 0xFF, f->fp) == EOF) |
444 |
return -1; |
445 |
if (fputc((val) & 0xFF, f->fp) == EOF) |
446 |
return -1; |
447 |
return 0; |
448 |
} |
449 |
|
450 |
/*! \brief Read Pointer |
451 |
* |
452 |
* \param ret pointer to read |
453 |
* \param dbFile struct |
454 |
* \return -1 on error, 0 otherwise. |
455 |
*/ |
456 |
int |
457 |
read_ptr(void **ret, struct dbFILE *f) |
458 |
{ |
459 |
int c = fgetc(f->fp); |
460 |
|
461 |
if (c == EOF) |
462 |
return -1; |
463 |
|
464 |
*ret = (c ? (void *)1 : (void *)0); |
465 |
return 0; |
466 |
} |
467 |
|
468 |
|
469 |
/*! \brief Write Pointer |
470 |
* |
471 |
* \param ret pointer to write |
472 |
* \param dbFile struct |
473 |
* \return -1 on error, 0 otherwise. |
474 |
*/ |
475 |
int |
476 |
write_ptr(const void *ptr, struct dbFILE *f) |
477 |
{ |
478 |
if (fputc(ptr ? 1 : 0, f->fp) == EOF) |
479 |
return -1; |
480 |
return 0; |
481 |
} |
482 |
|
483 |
/*! \brief Read String |
484 |
* |
485 |
* \param ret string |
486 |
* \param dbFile struct |
487 |
* \return -1 on error, 0 otherwise. |
488 |
*/ |
489 |
int |
490 |
read_string(char **ret, struct dbFILE *f) |
491 |
{ |
492 |
char *s = NULL; |
493 |
uint32_t len = 0; |
494 |
|
495 |
if (read_uint32(&len, f) < 0) |
496 |
return -1; |
497 |
|
498 |
if (len == 0) |
499 |
{ |
500 |
*ret = NULL; |
501 |
return 0; |
502 |
} |
503 |
|
504 |
s = MyMalloc(len); |
505 |
|
506 |
if (len != fread(s, 1, len, f->fp)) |
507 |
{ |
508 |
MyFree(s); |
509 |
return -1; |
510 |
} |
511 |
|
512 |
*ret = s; |
513 |
return 0; |
514 |
} |
515 |
|
516 |
/*! \brief Write String |
517 |
* |
518 |
* \param ret string |
519 |
* \param dbFile struct |
520 |
* \return -1 on error, 0 otherwise. |
521 |
*/ |
522 |
int |
523 |
write_string(const char *s, struct dbFILE *f) |
524 |
{ |
525 |
uint32_t len = 0; |
526 |
|
527 |
if (!s) |
528 |
return write_uint32(0, f); |
529 |
|
530 |
len = strlen(s); |
531 |
|
532 |
if (len > 4294967294) |
533 |
len = 4294967294; |
534 |
if (write_uint32(len + 1, f) < 0) |
535 |
return -1; |
536 |
if (len > 0 && fwrite(s, 1, len, f->fp) != len) |
537 |
return -1; |
538 |
if (fputc(0, f->fp) == EOF) |
539 |
return -1; |
540 |
|
541 |
return 0; |
542 |
} |
543 |
|
544 |
#define SAFE_READ(x) do { \ |
545 |
if ((x) < 0) { \ |
546 |
break; \ |
547 |
} \ |
548 |
} while (0) |
549 |
|
550 |
#define SAFE_WRITE(x,db) do { \ |
551 |
if ((x) < 0) { \ |
552 |
restore_db(f); \ |
553 |
ilog(LOG_TYPE_IRCD, "Write error on %s", db); \ |
554 |
return; \ |
555 |
} \ |
556 |
} while (0) |
557 |
|
558 |
void |
559 |
save_kline_database(void) |
560 |
{ |
561 |
uint32_t i = 0; |
562 |
uint32_t records = 0; |
563 |
struct dbFILE *f = NULL; |
564 |
dlink_node *ptr = NULL; |
565 |
|
566 |
if (!(f = open_db(KPATH, "w", KLINE_DB_VERSION))) |
567 |
return; |
568 |
|
569 |
for (i = 0; i < ATABLE_SIZE; ++i) |
570 |
{ |
571 |
DLINK_FOREACH(ptr, atable[i].head) |
572 |
{ |
573 |
struct AddressRec *arec = ptr->data; |
574 |
|
575 |
if (arec->type == CONF_KLINE && IsConfDatabase(arec->conf)) |
576 |
++records; |
577 |
} |
578 |
} |
579 |
|
580 |
SAFE_WRITE(write_uint32(records, f), KPATH); |
581 |
|
582 |
for (i = 0; i < ATABLE_SIZE; ++i) |
583 |
{ |
584 |
DLINK_FOREACH(ptr, atable[i].head) |
585 |
{ |
586 |
struct AddressRec *arec = ptr->data; |
587 |
|
588 |
if (arec->type == CONF_KLINE && IsConfDatabase(arec->conf)) |
589 |
{ |
590 |
SAFE_WRITE(write_string(arec->conf->user, f), KPATH); |
591 |
SAFE_WRITE(write_string(arec->conf->host, f), KPATH); |
592 |
SAFE_WRITE(write_string(arec->conf->reason, f), KPATH); |
593 |
SAFE_WRITE(write_uint64(arec->conf->setat, f), KPATH); |
594 |
SAFE_WRITE(write_uint64(arec->conf->until, f), KPATH); |
595 |
} |
596 |
} |
597 |
} |
598 |
|
599 |
close_db(f); |
600 |
} |
601 |
|
602 |
void |
603 |
load_kline_database(void) |
604 |
{ |
605 |
struct dbFILE *f = NULL; |
606 |
struct MaskItem *conf = NULL; |
607 |
char *field_1 = NULL; |
608 |
char *field_2 = NULL; |
609 |
char *field_3 = NULL; |
610 |
uint32_t i = 0; |
611 |
uint32_t records = 0; |
612 |
uint64_t field_4 = 0; |
613 |
uint64_t field_5 = 0; |
614 |
|
615 |
if (!(f = open_db(KPATH, "r", KLINE_DB_VERSION))) |
616 |
return; |
617 |
|
618 |
if (get_file_version(f) < 1) |
619 |
{ |
620 |
close_db(f); |
621 |
return; |
622 |
} |
623 |
|
624 |
read_uint32(&records, f); |
625 |
|
626 |
for (i = 0; i < records; ++i) |
627 |
{ |
628 |
SAFE_READ(read_string(&field_1, f)); |
629 |
SAFE_READ(read_string(&field_2, f)); |
630 |
SAFE_READ(read_string(&field_3, f)); |
631 |
SAFE_READ(read_uint64(&field_4, f)); |
632 |
SAFE_READ(read_uint64(&field_5, f)); |
633 |
|
634 |
conf = conf_make(CONF_KLINE); |
635 |
conf->user = field_1; |
636 |
conf->host = field_2; |
637 |
conf->reason = field_3; |
638 |
conf->setat = field_4; |
639 |
conf->until = field_5; |
640 |
SetConfDatabase(conf); |
641 |
|
642 |
add_conf_by_address(CONF_KLINE, conf); |
643 |
} |
644 |
|
645 |
close_db(f); |
646 |
} |
647 |
|
648 |
void |
649 |
save_dline_database(void) |
650 |
{ |
651 |
uint32_t i = 0; |
652 |
uint32_t records = 0; |
653 |
struct dbFILE *f = NULL; |
654 |
dlink_node *ptr = NULL; |
655 |
|
656 |
if (!(f = open_db(DLPATH, "w", KLINE_DB_VERSION))) |
657 |
return; |
658 |
|
659 |
for (i = 0; i < ATABLE_SIZE; ++i) |
660 |
{ |
661 |
DLINK_FOREACH(ptr, atable[i].head) |
662 |
{ |
663 |
struct AddressRec *arec = ptr->data; |
664 |
|
665 |
if (arec->type == CONF_DLINE && IsConfDatabase(arec->conf)) |
666 |
++records; |
667 |
} |
668 |
} |
669 |
|
670 |
SAFE_WRITE(write_uint32(records, f), DLPATH); |
671 |
|
672 |
for (i = 0; i < ATABLE_SIZE; ++i) |
673 |
{ |
674 |
DLINK_FOREACH(ptr, atable[i].head) |
675 |
{ |
676 |
struct AddressRec *arec = ptr->data; |
677 |
|
678 |
if (arec->type == CONF_DLINE && IsConfDatabase(arec->conf)) |
679 |
{ |
680 |
SAFE_WRITE(write_string(arec->conf->host, f), DLPATH); |
681 |
SAFE_WRITE(write_string(arec->conf->reason, f), DLPATH); |
682 |
SAFE_WRITE(write_uint64(arec->conf->setat, f), DLPATH); |
683 |
SAFE_WRITE(write_uint64(arec->conf->until, f), DLPATH); |
684 |
} |
685 |
} |
686 |
} |
687 |
|
688 |
close_db(f); |
689 |
} |
690 |
|
691 |
void |
692 |
load_dline_database(void) |
693 |
{ |
694 |
struct dbFILE *f = NULL; |
695 |
struct MaskItem *conf = NULL; |
696 |
char *field_1 = NULL; |
697 |
char *field_2 = NULL; |
698 |
uint32_t i = 0; |
699 |
uint32_t records = 0; |
700 |
uint64_t field_3 = 0; |
701 |
uint64_t field_4 = 0; |
702 |
|
703 |
if (!(f = open_db(DLPATH, "r", KLINE_DB_VERSION))) |
704 |
return; |
705 |
|
706 |
if (get_file_version(f) < 1) |
707 |
{ |
708 |
close_db(f); |
709 |
return; |
710 |
} |
711 |
|
712 |
read_uint32(&records, f); |
713 |
|
714 |
for (i = 0; i < records; ++i) |
715 |
{ |
716 |
SAFE_READ(read_string(&field_1, f)); |
717 |
SAFE_READ(read_string(&field_2, f)); |
718 |
SAFE_READ(read_uint64(&field_3, f)); |
719 |
SAFE_READ(read_uint64(&field_4, f)); |
720 |
|
721 |
conf = conf_make(CONF_DLINE); |
722 |
conf->host = field_1; |
723 |
conf->reason = field_2; |
724 |
conf->setat = field_3; |
725 |
conf->until = field_4; |
726 |
SetConfDatabase(conf); |
727 |
|
728 |
add_conf_by_address(CONF_DLINE, conf); |
729 |
} |
730 |
|
731 |
close_db(f); |
732 |
} |
733 |
|
734 |
void |
735 |
save_gline_database(void) |
736 |
{ |
737 |
uint32_t i = 0; |
738 |
uint32_t records = 0; |
739 |
struct dbFILE *f = NULL; |
740 |
dlink_node *ptr = NULL; |
741 |
|
742 |
if (!(f = open_db(GPATH, "w", KLINE_DB_VERSION))) |
743 |
return; |
744 |
|
745 |
for (i = 0; i < ATABLE_SIZE; ++i) |
746 |
{ |
747 |
DLINK_FOREACH(ptr, atable[i].head) |
748 |
{ |
749 |
struct AddressRec *arec = ptr->data; |
750 |
|
751 |
if (arec->type == CONF_GLINE && IsConfDatabase(arec->conf)) |
752 |
++records; |
753 |
} |
754 |
} |
755 |
|
756 |
SAFE_WRITE(write_uint32(records, f), GPATH); |
757 |
|
758 |
for (i = 0; i < ATABLE_SIZE; ++i) |
759 |
{ |
760 |
DLINK_FOREACH(ptr, atable[i].head) |
761 |
{ |
762 |
struct AddressRec *arec = ptr->data; |
763 |
|
764 |
if (arec->type == CONF_GLINE && IsConfDatabase(arec->conf)) |
765 |
{ |
766 |
SAFE_WRITE(write_string(arec->conf->user, f), GPATH); |
767 |
SAFE_WRITE(write_string(arec->conf->host, f), GPATH); |
768 |
SAFE_WRITE(write_string(arec->conf->reason, f), GPATH); |
769 |
SAFE_WRITE(write_uint64(arec->conf->setat, f), GPATH); |
770 |
SAFE_WRITE(write_uint64(arec->conf->until, f), GPATH); |
771 |
} |
772 |
} |
773 |
} |
774 |
|
775 |
close_db(f); |
776 |
} |
777 |
|
778 |
void |
779 |
load_gline_database(void) |
780 |
{ |
781 |
struct dbFILE *f = NULL; |
782 |
struct MaskItem *conf = NULL; |
783 |
char *field_1 = NULL; |
784 |
char *field_2 = NULL; |
785 |
char *field_3 = NULL; |
786 |
uint32_t i = 0; |
787 |
uint32_t records = 0; |
788 |
uint64_t field_4 = 0; |
789 |
uint64_t field_5 = 0; |
790 |
|
791 |
if (!(f = open_db(GPATH, "r", KLINE_DB_VERSION))) |
792 |
return; |
793 |
|
794 |
if (get_file_version(f) < 1) |
795 |
{ |
796 |
close_db(f); |
797 |
return; |
798 |
} |
799 |
|
800 |
read_uint32(&records, f); |
801 |
|
802 |
for (i = 0; i < records; ++i) |
803 |
{ |
804 |
SAFE_READ(read_string(&field_1, f)); |
805 |
SAFE_READ(read_string(&field_2, f)); |
806 |
SAFE_READ(read_string(&field_3, f)); |
807 |
SAFE_READ(read_uint64(&field_4, f)); |
808 |
SAFE_READ(read_uint64(&field_5, f)); |
809 |
|
810 |
conf = conf_make(CONF_GLINE); |
811 |
conf->user = field_1; |
812 |
conf->host = field_2; |
813 |
conf->reason = field_3; |
814 |
conf->setat = field_4; |
815 |
conf->until = field_5; |
816 |
SetConfDatabase(conf); |
817 |
|
818 |
add_conf_by_address(CONF_GLINE, conf); |
819 |
} |
820 |
|
821 |
close_db(f); |
822 |
} |
823 |
|
824 |
void |
825 |
save_resv_database(void) |
826 |
{ |
827 |
uint32_t records = 0; |
828 |
struct dbFILE *f = NULL; |
829 |
dlink_node *ptr = NULL; |
830 |
struct MaskItem *conf = NULL; |
831 |
|
832 |
if (!(f = open_db(RESVPATH, "w", KLINE_DB_VERSION))) |
833 |
return; |
834 |
|
835 |
DLINK_FOREACH(ptr, cresv_items.head) |
836 |
{ |
837 |
conf = ptr->data; |
838 |
|
839 |
if (IsConfDatabase(conf)) |
840 |
++records; |
841 |
} |
842 |
|
843 |
DLINK_FOREACH(ptr, nresv_items.head) |
844 |
{ |
845 |
conf = ptr->data; |
846 |
|
847 |
if (IsConfDatabase(conf)) |
848 |
++records; |
849 |
} |
850 |
|
851 |
SAFE_WRITE(write_uint32(records, f), RESVPATH); |
852 |
|
853 |
DLINK_FOREACH(ptr, cresv_items.head) |
854 |
{ |
855 |
conf = ptr->data; |
856 |
|
857 |
if (!IsConfDatabase(conf)) |
858 |
continue; |
859 |
|
860 |
SAFE_WRITE(write_string(conf->name, f), RESVPATH); |
861 |
SAFE_WRITE(write_string(conf->reason, f), RESVPATH); |
862 |
SAFE_WRITE(write_uint64(conf->setat, f), RESVPATH); |
863 |
SAFE_WRITE(write_uint64(conf->until, f), RESVPATH); |
864 |
} |
865 |
|
866 |
DLINK_FOREACH(ptr, nresv_items.head) |
867 |
{ |
868 |
conf = ptr->data; |
869 |
|
870 |
if (!IsConfDatabase(conf)) |
871 |
continue; |
872 |
|
873 |
SAFE_WRITE(write_string(conf->name, f), RESVPATH); |
874 |
SAFE_WRITE(write_string(conf->reason, f), RESVPATH); |
875 |
SAFE_WRITE(write_uint64(conf->setat, f), RESVPATH); |
876 |
SAFE_WRITE(write_uint64(conf->until, f), RESVPATH); |
877 |
} |
878 |
|
879 |
close_db(f); |
880 |
} |
881 |
|
882 |
void |
883 |
load_resv_database(void) |
884 |
{ |
885 |
uint32_t i = 0; |
886 |
uint32_t records = 0; |
887 |
uint64_t tmp64_hold = 0, tmp64_setat = 0; |
888 |
struct dbFILE *f = NULL; |
889 |
char *name = NULL; |
890 |
char *reason = NULL; |
891 |
struct MaskItem *conf = NULL; |
892 |
|
893 |
if (!(f = open_db(RESVPATH, "r", KLINE_DB_VERSION))) |
894 |
return; |
895 |
|
896 |
if (get_file_version(f) < 1) |
897 |
{ |
898 |
close_db(f); |
899 |
return; |
900 |
} |
901 |
|
902 |
read_uint32(&records, f); |
903 |
|
904 |
for (i = 0; i < records; ++i) |
905 |
{ |
906 |
SAFE_READ(read_string(&name, f)); |
907 |
SAFE_READ(read_string(&reason, f)); |
908 |
SAFE_READ(read_uint64(&tmp64_setat, f)); |
909 |
SAFE_READ(read_uint64(&tmp64_hold, f)); |
910 |
|
911 |
if ((conf = create_resv(name, reason, NULL)) == NULL) |
912 |
continue; |
913 |
|
914 |
conf->setat = tmp64_setat; |
915 |
conf->until = tmp64_hold; |
916 |
SetConfDatabase(conf); |
917 |
|
918 |
MyFree(name); |
919 |
MyFree(reason); |
920 |
} |
921 |
|
922 |
close_db(f); |
923 |
} |
924 |
|
925 |
void |
926 |
save_xline_database(void) |
927 |
{ |
928 |
uint32_t records = 0; |
929 |
struct dbFILE *f = NULL; |
930 |
dlink_node *ptr = NULL; |
931 |
struct MaskItem *conf = NULL; |
932 |
|
933 |
if (!(f = open_db(XPATH, "w", KLINE_DB_VERSION))) |
934 |
return; |
935 |
|
936 |
DLINK_FOREACH(ptr, xconf_items.head) |
937 |
{ |
938 |
conf = ptr->data; |
939 |
|
940 |
if (IsConfDatabase(conf)) |
941 |
++records; |
942 |
} |
943 |
|
944 |
SAFE_WRITE(write_uint32(records, f), XPATH); |
945 |
|
946 |
DLINK_FOREACH(ptr, xconf_items.head) |
947 |
{ |
948 |
conf = ptr->data; |
949 |
|
950 |
if (!IsConfDatabase(conf)) |
951 |
continue; |
952 |
|
953 |
SAFE_WRITE(write_string(conf->name, f), XPATH); |
954 |
SAFE_WRITE(write_string(conf->reason, f), XPATH); |
955 |
SAFE_WRITE(write_uint64(conf->setat, f), XPATH); |
956 |
SAFE_WRITE(write_uint64(conf->until, f), XPATH); |
957 |
} |
958 |
|
959 |
close_db(f); |
960 |
} |
961 |
|
962 |
void |
963 |
load_xline_database(void) |
964 |
{ |
965 |
uint32_t i = 0; |
966 |
uint32_t records = 0; |
967 |
uint64_t tmp64_hold = 0, tmp64_setat = 0; |
968 |
struct dbFILE *f = NULL; |
969 |
char *name = NULL; |
970 |
char *reason = NULL; |
971 |
struct MaskItem *conf = NULL; |
972 |
|
973 |
if (!(f = open_db(XPATH, "r", KLINE_DB_VERSION))) |
974 |
return; |
975 |
|
976 |
if (get_file_version(f) < 1) |
977 |
{ |
978 |
close_db(f); |
979 |
return; |
980 |
} |
981 |
|
982 |
read_uint32(&records, f); |
983 |
|
984 |
for (i = 0; i < records; ++i) |
985 |
{ |
986 |
SAFE_READ(read_string(&name, f)); |
987 |
SAFE_READ(read_string(&reason, f)); |
988 |
SAFE_READ(read_uint64(&tmp64_setat, f)); |
989 |
SAFE_READ(read_uint64(&tmp64_hold, f)); |
990 |
|
991 |
conf = conf_make(CONF_XLINE); |
992 |
|
993 |
SetConfDatabase(conf); |
994 |
|
995 |
conf->name = name; |
996 |
conf->reason = reason; |
997 |
conf->setat = tmp64_setat; |
998 |
conf->until = tmp64_hold; |
999 |
} |
1000 |
|
1001 |
close_db(f); |
1002 |
} |
1003 |
|
1004 |
void |
1005 |
save_all_databases(void *unused) |
1006 |
{ |
1007 |
save_kline_database(); |
1008 |
save_dline_database(); |
1009 |
save_gline_database(); |
1010 |
save_xline_database(); |
1011 |
save_resv_database(); |
1012 |
} |