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-2018 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
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.h" |
31 |
#include "conf_db.h" |
32 |
#include "conf_gecos.h" |
33 |
#include "conf_resv.h" |
34 |
#include "memory.h" |
35 |
#include "log.h" |
36 |
#include "send.h" |
37 |
#include "irc_string.h" |
38 |
#include "hostmask.h" |
39 |
|
40 |
|
41 |
/*! \brief Return the version number on the file. Return 0 if there is no version |
42 |
* number or the number doesn't make sense (i.e. less than 1 or greater |
43 |
* than FILE_VERSION). |
44 |
* |
45 |
* \param f dbFile Struct Member |
46 |
* \return int 0 if failure, 1 > is the version number |
47 |
*/ |
48 |
static uint32_t |
49 |
get_file_version(struct dbFILE *f) |
50 |
{ |
51 |
uint32_t version = 0; |
52 |
|
53 |
if (read_uint32(&version, f) == -1) |
54 |
{ |
55 |
ilog(LOG_TYPE_IRCD, "Error reading version number on %s: %s", |
56 |
f->filename, strerror(errno)); |
57 |
return 0; |
58 |
} |
59 |
|
60 |
if (version < 1) |
61 |
{ |
62 |
ilog(LOG_TYPE_IRCD, "Invalid version number (%u) on %s", |
63 |
version, f->filename); |
64 |
return 0; |
65 |
} |
66 |
|
67 |
return version; |
68 |
} |
69 |
|
70 |
/*! \brief Write the current version number to the file. |
71 |
* \param f dbFile Struct Member |
72 |
* \param version Database version |
73 |
* \return 0 on error, 1 on success. |
74 |
*/ |
75 |
static int |
76 |
write_file_version(struct dbFILE *f, uint32_t version) |
77 |
{ |
78 |
if (write_uint32(version, f) == -1) |
79 |
{ |
80 |
ilog(LOG_TYPE_IRCD, "Error writing version number on %s", |
81 |
f->filename); |
82 |
return 0; |
83 |
} |
84 |
|
85 |
return 1; |
86 |
} |
87 |
|
88 |
/*! \brief Open the database for reading |
89 |
* \param filename File to open as the database |
90 |
* \return dbFile struct |
91 |
*/ |
92 |
static struct dbFILE * |
93 |
open_db_read(const char *filename) |
94 |
{ |
95 |
struct dbFILE *f = xcalloc(sizeof(*f)); |
96 |
|
97 |
strlcpy(f->filename, filename, sizeof(f->filename)); |
98 |
|
99 |
f->mode = 'r'; |
100 |
f->fp = fopen(f->filename, "rb"); |
101 |
|
102 |
if (f->fp == NULL) |
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 |
xfree(f); |
110 |
errno = errno_save; |
111 |
return NULL; |
112 |
} |
113 |
|
114 |
return f; |
115 |
} |
116 |
|
117 |
/*! \brief Open the database for writting |
118 |
* \param filename File to open as the database |
119 |
* \param version Database version |
120 |
* \return dbFile struct |
121 |
*/ |
122 |
static struct dbFILE * |
123 |
open_db_write(const char *filename, uint32_t version) |
124 |
{ |
125 |
struct dbFILE *f = xcalloc(sizeof(*f)); |
126 |
int fd = 0; |
127 |
|
128 |
strlcpy(f->filename, filename, sizeof(f->filename)); |
129 |
|
130 |
filename = f->filename; |
131 |
f->mode = 'w'; |
132 |
|
133 |
snprintf(f->tempname, sizeof(f->tempname), "%s.new", filename); |
134 |
|
135 |
if (f->tempname[0] == '\0' || !strcmp(f->tempname, filename)) |
136 |
{ |
137 |
ilog(LOG_TYPE_IRCD, "Opening database file %s for write: Filename too long", |
138 |
filename); |
139 |
xfree(f); |
140 |
errno = ENAMETOOLONG; |
141 |
return NULL; |
142 |
} |
143 |
|
144 |
remove(f->tempname); |
145 |
|
146 |
/* Use open() to avoid people sneaking a new file in under us */ |
147 |
fd = open(f->tempname, O_WRONLY | O_CREAT | O_EXCL, 0666); |
148 |
if (fd >= 0) |
149 |
f->fp = fdopen(fd, "wb"); |
150 |
|
151 |
if (!f->fp || !write_file_version(f, version)) |
152 |
{ |
153 |
int errno_save = errno; |
154 |
static int walloped = 0; |
155 |
|
156 |
if (!walloped++) |
157 |
sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, |
158 |
"Cannot create temporary database file %s", |
159 |
f->tempname); |
160 |
|
161 |
errno = errno_save; |
162 |
ilog(LOG_TYPE_IRCD, "Cannot create temporary database file %s", |
163 |
f->tempname); |
164 |
|
165 |
if (f->fp) |
166 |
fclose(f->fp); |
167 |
|
168 |
remove(f->tempname); |
169 |
xfree(f); |
170 |
|
171 |
errno = errno_save; |
172 |
return NULL; |
173 |
} |
174 |
|
175 |
return f; |
176 |
} |
177 |
|
178 |
/*! \brief Open a database file for reading (*mode == 'r') or writing (*mode == 'w'). |
179 |
* Return the stream pointer, or NULL on error. When opening for write, the |
180 |
* file actually opened is a temporary file, which will be renamed to the |
181 |
* original file on close. |
182 |
* |
183 |
* `version' is only used when opening a file for writing, and indicates the |
184 |
* version number to write to the file. |
185 |
* |
186 |
* \param filename File to open as the database |
187 |
* \param mode Mode for writting or reading |
188 |
* \param version Database version |
189 |
* \return dbFile struct |
190 |
*/ |
191 |
static struct dbFILE * |
192 |
open_db(const char *filename, const char *mode, uint32_t version) |
193 |
{ |
194 |
switch (*mode) |
195 |
{ |
196 |
case 'r': |
197 |
return open_db_read(filename); |
198 |
break; |
199 |
case 'w': |
200 |
return open_db_write(filename, version); |
201 |
break; |
202 |
default: |
203 |
errno = EINVAL; |
204 |
return NULL; |
205 |
} |
206 |
} |
207 |
|
208 |
/*! \brief Restore the database file to its condition before open_db(). This is |
209 |
* identical to close_db() for files open for reading; however, for files |
210 |
* open for writing, we discard the new temporary file instead of renaming |
211 |
* it over the old file. The value of errno is preserved. |
212 |
* |
213 |
* \param dbFile struct |
214 |
*/ |
215 |
static void |
216 |
restore_db(struct dbFILE *f) |
217 |
{ |
218 |
int errno_save = errno; |
219 |
|
220 |
if (f->fp) |
221 |
fclose(f->fp); |
222 |
if (f->mode == 'w' && f->tempname[0]) |
223 |
remove(f->tempname); |
224 |
|
225 |
xfree(f); |
226 |
errno = errno_save; |
227 |
} |
228 |
|
229 |
/*! \brief Close a database file. If the file was opened for write, moves the new |
230 |
* file over the old one, and logs/wallops an error message if the rename() |
231 |
* fails. |
232 |
* |
233 |
* \param dbFile struct |
234 |
* \return -1 on error, 0 on success. |
235 |
*/ |
236 |
static 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_SERVNOTICE, 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 |
xfree(f); |
270 |
return 0; |
271 |
} |
272 |
|
273 |
/* |
274 |
* Read and write 2-, 4- and 8-byte quantities, and strings. All multibyte |
275 |
* values are stored in big-endian order (most significant byte first). |
276 |
* A string is stored with a 2-byte unsigned length (including the trailing |
277 |
* \0) first; a length of 0 indicates that the string pointer is NULL. |
278 |
* Written strings are truncated silently at 4294967294 bytes, and are always |
279 |
* null-terminated. |
280 |
*/ |
281 |
|
282 |
/*! \brief Read a unsigned 8bit integer |
283 |
* |
284 |
* \param ret 16bit integer to read |
285 |
* \param dbFile struct |
286 |
* \return -1 on error, 0 otherwise. |
287 |
*/ |
288 |
int |
289 |
read_uint16(uint16_t *ret, struct dbFILE *f) |
290 |
{ |
291 |
int c1 = fgetc(f->fp); |
292 |
int c2 = fgetc(f->fp); |
293 |
|
294 |
if (c1 == EOF || c2 == EOF) |
295 |
return -1; |
296 |
|
297 |
*ret = c1 << 8 | c2; |
298 |
return 0; |
299 |
} |
300 |
|
301 |
/*! \brief Write a unsigned 16bit integer |
302 |
* |
303 |
* \param val 16bit integer to write |
304 |
* \param dbFile struct |
305 |
* \return -1 on error, 0 otherwise. |
306 |
*/ |
307 |
int |
308 |
write_uint16(uint16_t val, struct dbFILE *f) |
309 |
{ |
310 |
if (fputc((val >> 8) & 0xFF, f->fp) == EOF || |
311 |
fputc(val & 0xFF, f->fp) == EOF) |
312 |
return -1; |
313 |
|
314 |
return 0; |
315 |
} |
316 |
|
317 |
/*! \brief Read a unsigned 32bit integer |
318 |
* |
319 |
* \param ret unsigned 32bit integer to read |
320 |
* \param dbFile struct |
321 |
* \return -1 on error, 0 otherwise. |
322 |
*/ |
323 |
int |
324 |
read_uint32(uint32_t *ret, struct dbFILE *f) |
325 |
{ |
326 |
int c1 = fgetc(f->fp); |
327 |
int c2 = fgetc(f->fp); |
328 |
int c3 = fgetc(f->fp); |
329 |
int c4 = fgetc(f->fp); |
330 |
|
331 |
if (c1 == EOF || c2 == EOF || c3 == EOF || c4 == EOF) |
332 |
return -1; |
333 |
|
334 |
*ret = c1 << 24 | c2 << 16 | c3 << 8 | c4; |
335 |
return 0; |
336 |
} |
337 |
|
338 |
|
339 |
/*! \brief Write a unsigned 32bit integer |
340 |
* |
341 |
* \param val unsigned 32bit integer to write |
342 |
* \param dbFile struct |
343 |
* \return -1 on error, 0 otherwise. |
344 |
*/ |
345 |
int |
346 |
write_uint32(uint32_t val, struct dbFILE *f) |
347 |
{ |
348 |
if (fputc((val >> 24) & 0xFF, f->fp) == EOF) |
349 |
return -1; |
350 |
if (fputc((val >> 16) & 0xFF, f->fp) == EOF) |
351 |
return -1; |
352 |
if (fputc((val >> 8) & 0xFF, f->fp) == EOF) |
353 |
return -1; |
354 |
if (fputc((val) & 0xFF, f->fp) == EOF) |
355 |
return -1; |
356 |
return 0; |
357 |
} |
358 |
|
359 |
/*! \brief Read a unsigned 64bit integer |
360 |
* |
361 |
* \param ret unsigned 64bit integer to read |
362 |
* \param dbFile struct |
363 |
* \return -1 on error, 0 otherwise. |
364 |
*/ |
365 |
int |
366 |
read_uint64(uint64_t *ret, struct dbFILE *f) |
367 |
{ |
368 |
int64_t c1 = fgetc(f->fp); |
369 |
int64_t c2 = fgetc(f->fp); |
370 |
int64_t c3 = fgetc(f->fp); |
371 |
int64_t c4 = fgetc(f->fp); |
372 |
int64_t c5 = fgetc(f->fp); |
373 |
int64_t c6 = fgetc(f->fp); |
374 |
int64_t c7 = fgetc(f->fp); |
375 |
int64_t c8 = fgetc(f->fp); |
376 |
|
377 |
if (c1 == EOF || c2 == EOF || c3 == EOF || c4 == EOF || |
378 |
c5 == EOF || c6 == EOF || c7 == EOF || c8 == EOF) |
379 |
return -1; |
380 |
|
381 |
*ret = c1 << 56 | c2 << 48 | c3 << 40 | c4 << 32 | |
382 |
c5 << 24 | c6 << 16 | c7 << 8 | c8; |
383 |
return 0; |
384 |
} |
385 |
|
386 |
|
387 |
/*! \brief Write a unsigned 64bit integer |
388 |
* |
389 |
* \param val unsigned 64bit integer to write |
390 |
* \param dbFile struct |
391 |
* \return -1 on error, 0 otherwise. |
392 |
*/ |
393 |
int |
394 |
write_uint64(uint64_t val, struct dbFILE *f) |
395 |
{ |
396 |
if (fputc((val >> 56) & 0xFF, f->fp) == EOF) |
397 |
return -1; |
398 |
if (fputc((val >> 48) & 0xFF, f->fp) == EOF) |
399 |
return -1; |
400 |
if (fputc((val >> 40) & 0xFF, f->fp) == EOF) |
401 |
return -1; |
402 |
if (fputc((val >> 32) & 0xFF, f->fp) == EOF) |
403 |
return -1; |
404 |
if (fputc((val >> 24) & 0xFF, f->fp) == EOF) |
405 |
return -1; |
406 |
if (fputc((val >> 16) & 0xFF, f->fp) == EOF) |
407 |
return -1; |
408 |
if (fputc((val >> 8) & 0xFF, f->fp) == EOF) |
409 |
return -1; |
410 |
if (fputc((val) & 0xFF, f->fp) == EOF) |
411 |
return -1; |
412 |
return 0; |
413 |
} |
414 |
|
415 |
/*! \brief Read String |
416 |
* |
417 |
* \param ret string |
418 |
* \param dbFile struct |
419 |
* \return -1 on error, 0 otherwise. |
420 |
*/ |
421 |
int |
422 |
read_string(char **ret, struct dbFILE *f) |
423 |
{ |
424 |
char *s = NULL; |
425 |
uint32_t len = 0; |
426 |
|
427 |
if (read_uint32(&len, f) < 0) |
428 |
return -1; |
429 |
|
430 |
if (len == 0) |
431 |
{ |
432 |
*ret = NULL; |
433 |
return 0; |
434 |
} |
435 |
|
436 |
s = xcalloc(len); |
437 |
|
438 |
if (len != fread(s, 1, len, f->fp)) |
439 |
{ |
440 |
xfree(s); |
441 |
return -1; |
442 |
} |
443 |
|
444 |
*ret = s; |
445 |
return 0; |
446 |
} |
447 |
|
448 |
/*! \brief Write String |
449 |
* |
450 |
* \param s string |
451 |
* \param dbFile struct |
452 |
* \return -1 on error, 0 otherwise. |
453 |
*/ |
454 |
int |
455 |
write_string(const char *s, struct dbFILE *f) |
456 |
{ |
457 |
uint32_t len = 0; |
458 |
|
459 |
if (!s) |
460 |
return write_uint32(0, f); |
461 |
|
462 |
len = strlen(s); |
463 |
|
464 |
if (len > 4294967294) |
465 |
len = 4294967294; |
466 |
if (write_uint32(len + 1, f) < 0) |
467 |
return -1; |
468 |
if (len > 0 && fwrite(s, 1, len, f->fp) != len) |
469 |
return -1; |
470 |
if (fputc(0, f->fp) == EOF) |
471 |
return -1; |
472 |
|
473 |
return 0; |
474 |
} |
475 |
|
476 |
#define SAFE_READ(x) do { \ |
477 |
if ((x) < 0) { \ |
478 |
break; \ |
479 |
} \ |
480 |
} while (0) |
481 |
|
482 |
#define SAFE_WRITE(x,db) do { \ |
483 |
if ((x) < 0) { \ |
484 |
restore_db(f); \ |
485 |
ilog(LOG_TYPE_IRCD, "Write error on %s", db); \ |
486 |
return; \ |
487 |
} \ |
488 |
} while (0) |
489 |
|
490 |
void |
491 |
save_kline_database(const char *filename) |
492 |
{ |
493 |
uint32_t i = 0; |
494 |
uint32_t records = 0; |
495 |
struct dbFILE *f = NULL; |
496 |
dlink_node *ptr = NULL; |
497 |
|
498 |
if (!(f = open_db(filename, "w", KLINE_DB_VERSION))) |
499 |
return; |
500 |
|
501 |
for (i = 0; i < ATABLE_SIZE; ++i) |
502 |
{ |
503 |
DLINK_FOREACH(ptr, atable[i].head) |
504 |
{ |
505 |
struct AddressRec *arec = ptr->data; |
506 |
|
507 |
if (arec->type == CONF_KLINE && IsConfDatabase(arec->conf)) |
508 |
++records; |
509 |
} |
510 |
} |
511 |
|
512 |
SAFE_WRITE(write_uint32(records, f), filename); |
513 |
|
514 |
for (i = 0; i < ATABLE_SIZE; ++i) |
515 |
{ |
516 |
DLINK_FOREACH(ptr, atable[i].head) |
517 |
{ |
518 |
struct AddressRec *arec = ptr->data; |
519 |
|
520 |
if (arec->type == CONF_KLINE && IsConfDatabase(arec->conf)) |
521 |
{ |
522 |
SAFE_WRITE(write_string(arec->conf->user, f), filename); |
523 |
SAFE_WRITE(write_string(arec->conf->host, f), filename); |
524 |
SAFE_WRITE(write_string(arec->conf->reason, f), filename); |
525 |
SAFE_WRITE(write_uint64(arec->conf->setat, f), filename); |
526 |
SAFE_WRITE(write_uint64(arec->conf->until, f), filename); |
527 |
} |
528 |
} |
529 |
} |
530 |
|
531 |
close_db(f); |
532 |
} |
533 |
|
534 |
void |
535 |
load_kline_database(const char *filename) |
536 |
{ |
537 |
struct dbFILE *f = NULL; |
538 |
struct MaskItem *conf = NULL; |
539 |
char *field_1 = NULL; |
540 |
char *field_2 = NULL; |
541 |
char *field_3 = NULL; |
542 |
uint32_t i = 0; |
543 |
uint32_t records = 0; |
544 |
uint64_t field_4 = 0; |
545 |
uint64_t field_5 = 0; |
546 |
|
547 |
if (!(f = open_db(filename, "r", KLINE_DB_VERSION))) |
548 |
return; |
549 |
|
550 |
if (get_file_version(f) < 1) |
551 |
{ |
552 |
close_db(f); |
553 |
return; |
554 |
} |
555 |
|
556 |
read_uint32(&records, f); |
557 |
|
558 |
for (i = 0; i < records; ++i) |
559 |
{ |
560 |
SAFE_READ(read_string(&field_1, f)); |
561 |
SAFE_READ(read_string(&field_2, f)); |
562 |
SAFE_READ(read_string(&field_3, f)); |
563 |
SAFE_READ(read_uint64(&field_4, f)); |
564 |
SAFE_READ(read_uint64(&field_5, f)); |
565 |
|
566 |
conf = conf_make(CONF_KLINE); |
567 |
conf->user = field_1; |
568 |
conf->host = field_2; |
569 |
conf->reason = field_3; |
570 |
conf->setat = field_4; |
571 |
conf->until = field_5; |
572 |
SetConfDatabase(conf); |
573 |
|
574 |
add_conf_by_address(CONF_KLINE, conf); |
575 |
} |
576 |
|
577 |
close_db(f); |
578 |
} |
579 |
|
580 |
void |
581 |
save_dline_database(const char *filename) |
582 |
{ |
583 |
uint32_t i = 0; |
584 |
uint32_t records = 0; |
585 |
struct dbFILE *f = NULL; |
586 |
dlink_node *ptr = NULL; |
587 |
|
588 |
if (!(f = open_db(filename, "w", KLINE_DB_VERSION))) |
589 |
return; |
590 |
|
591 |
for (i = 0; i < ATABLE_SIZE; ++i) |
592 |
{ |
593 |
DLINK_FOREACH(ptr, atable[i].head) |
594 |
{ |
595 |
struct AddressRec *arec = ptr->data; |
596 |
|
597 |
if (arec->type == CONF_DLINE && IsConfDatabase(arec->conf)) |
598 |
++records; |
599 |
} |
600 |
} |
601 |
|
602 |
SAFE_WRITE(write_uint32(records, f), filename); |
603 |
|
604 |
for (i = 0; i < ATABLE_SIZE; ++i) |
605 |
{ |
606 |
DLINK_FOREACH(ptr, atable[i].head) |
607 |
{ |
608 |
struct AddressRec *arec = ptr->data; |
609 |
|
610 |
if (arec->type == CONF_DLINE && IsConfDatabase(arec->conf)) |
611 |
{ |
612 |
SAFE_WRITE(write_string(arec->conf->host, f), filename); |
613 |
SAFE_WRITE(write_string(arec->conf->reason, f), filename); |
614 |
SAFE_WRITE(write_uint64(arec->conf->setat, f), filename); |
615 |
SAFE_WRITE(write_uint64(arec->conf->until, f), filename); |
616 |
} |
617 |
} |
618 |
} |
619 |
|
620 |
close_db(f); |
621 |
} |
622 |
|
623 |
void |
624 |
load_dline_database(const char *filename) |
625 |
{ |
626 |
struct dbFILE *f = NULL; |
627 |
struct MaskItem *conf = NULL; |
628 |
char *field_1 = NULL; |
629 |
char *field_2 = NULL; |
630 |
uint32_t i = 0; |
631 |
uint32_t records = 0; |
632 |
uint64_t field_3 = 0; |
633 |
uint64_t field_4 = 0; |
634 |
|
635 |
if (!(f = open_db(filename, "r", KLINE_DB_VERSION))) |
636 |
return; |
637 |
|
638 |
if (get_file_version(f) < 1) |
639 |
{ |
640 |
close_db(f); |
641 |
return; |
642 |
} |
643 |
|
644 |
read_uint32(&records, f); |
645 |
|
646 |
for (i = 0; i < records; ++i) |
647 |
{ |
648 |
SAFE_READ(read_string(&field_1, f)); |
649 |
SAFE_READ(read_string(&field_2, f)); |
650 |
SAFE_READ(read_uint64(&field_3, f)); |
651 |
SAFE_READ(read_uint64(&field_4, f)); |
652 |
|
653 |
conf = conf_make(CONF_DLINE); |
654 |
conf->host = field_1; |
655 |
conf->reason = field_2; |
656 |
conf->setat = field_3; |
657 |
conf->until = field_4; |
658 |
SetConfDatabase(conf); |
659 |
|
660 |
add_conf_by_address(CONF_DLINE, conf); |
661 |
} |
662 |
|
663 |
close_db(f); |
664 |
} |
665 |
|
666 |
void |
667 |
save_resv_database(const char *filename) |
668 |
{ |
669 |
uint32_t records = 0; |
670 |
struct dbFILE *f = NULL; |
671 |
dlink_node *node = NULL; |
672 |
const struct ResvItem *resv = NULL; |
673 |
|
674 |
if (!(f = open_db(filename, "w", KLINE_DB_VERSION))) |
675 |
return; |
676 |
|
677 |
DLINK_FOREACH(node, resv_chan_get_list()->head) |
678 |
{ |
679 |
resv = node->data; |
680 |
|
681 |
if (resv->in_database) |
682 |
++records; |
683 |
} |
684 |
|
685 |
DLINK_FOREACH(node, resv_nick_get_list()->head) |
686 |
{ |
687 |
resv = node->data; |
688 |
|
689 |
if (resv->in_database) |
690 |
++records; |
691 |
} |
692 |
|
693 |
SAFE_WRITE(write_uint32(records, f), filename); |
694 |
|
695 |
DLINK_FOREACH(node, resv_chan_get_list()->head) |
696 |
{ |
697 |
resv = node->data; |
698 |
|
699 |
if (!resv->in_database) |
700 |
continue; |
701 |
|
702 |
SAFE_WRITE(write_string(resv->mask, f), filename); |
703 |
SAFE_WRITE(write_string(resv->reason, f), filename); |
704 |
SAFE_WRITE(write_uint64(resv->setat, f), filename); |
705 |
SAFE_WRITE(write_uint64(resv->expire, f), filename); |
706 |
} |
707 |
|
708 |
DLINK_FOREACH(node, resv_nick_get_list()->head) |
709 |
{ |
710 |
resv = node->data; |
711 |
|
712 |
if (!resv->in_database) |
713 |
continue; |
714 |
|
715 |
SAFE_WRITE(write_string(resv->mask, f), filename); |
716 |
SAFE_WRITE(write_string(resv->reason, f), filename); |
717 |
SAFE_WRITE(write_uint64(resv->setat, f), filename); |
718 |
SAFE_WRITE(write_uint64(resv->expire, f), filename); |
719 |
} |
720 |
|
721 |
close_db(f); |
722 |
} |
723 |
|
724 |
void |
725 |
load_resv_database(const char *filename) |
726 |
{ |
727 |
uint32_t i = 0; |
728 |
uint32_t records = 0; |
729 |
uint64_t tmp64_hold = 0, tmp64_setat = 0; |
730 |
struct dbFILE *f = NULL; |
731 |
char *name = NULL; |
732 |
char *reason = NULL; |
733 |
struct ResvItem *resv = NULL; |
734 |
|
735 |
if (!(f = open_db(filename, "r", KLINE_DB_VERSION))) |
736 |
return; |
737 |
|
738 |
if (get_file_version(f) < 1) |
739 |
{ |
740 |
close_db(f); |
741 |
return; |
742 |
} |
743 |
|
744 |
read_uint32(&records, f); |
745 |
|
746 |
for (i = 0; i < records; ++i) |
747 |
{ |
748 |
SAFE_READ(read_string(&name, f)); |
749 |
SAFE_READ(read_string(&reason, f)); |
750 |
SAFE_READ(read_uint64(&tmp64_setat, f)); |
751 |
SAFE_READ(read_uint64(&tmp64_hold, f)); |
752 |
|
753 |
if ((resv = resv_make(name, reason, NULL)) == NULL) |
754 |
continue; |
755 |
|
756 |
resv->setat = tmp64_setat; |
757 |
resv->expire = tmp64_hold; |
758 |
resv->in_database = 1; |
759 |
|
760 |
xfree(name); |
761 |
xfree(reason); |
762 |
} |
763 |
|
764 |
close_db(f); |
765 |
} |
766 |
|
767 |
void |
768 |
save_xline_database(const char *filename) |
769 |
{ |
770 |
uint32_t records = 0; |
771 |
struct dbFILE *f = NULL; |
772 |
dlink_node *ptr = NULL; |
773 |
struct GecosItem *gecos = NULL; |
774 |
|
775 |
if (!(f = open_db(filename, "w", KLINE_DB_VERSION))) |
776 |
return; |
777 |
|
778 |
DLINK_FOREACH(ptr, gecos_get_list()->head) |
779 |
{ |
780 |
gecos = ptr->data; |
781 |
|
782 |
if (gecos->in_database) |
783 |
++records; |
784 |
} |
785 |
|
786 |
SAFE_WRITE(write_uint32(records, f), filename); |
787 |
|
788 |
DLINK_FOREACH(ptr, gecos_get_list()->head) |
789 |
{ |
790 |
gecos = ptr->data; |
791 |
|
792 |
if (!gecos->in_database) |
793 |
continue; |
794 |
|
795 |
SAFE_WRITE(write_string(gecos->mask, f), filename); |
796 |
SAFE_WRITE(write_string(gecos->reason, f), filename); |
797 |
SAFE_WRITE(write_uint64(gecos->setat, f), filename); |
798 |
SAFE_WRITE(write_uint64(gecos->expire, f), filename); |
799 |
} |
800 |
|
801 |
close_db(f); |
802 |
} |
803 |
|
804 |
void |
805 |
load_xline_database(const char *filename) |
806 |
{ |
807 |
uint32_t i = 0; |
808 |
uint32_t records = 0; |
809 |
uint64_t tmp64_hold = 0, tmp64_setat = 0; |
810 |
struct dbFILE *f = NULL; |
811 |
char *name = NULL; |
812 |
char *reason = NULL; |
813 |
struct GecosItem *gecos = NULL; |
814 |
|
815 |
if (!(f = open_db(filename, "r", KLINE_DB_VERSION))) |
816 |
return; |
817 |
|
818 |
if (get_file_version(f) < 1) |
819 |
{ |
820 |
close_db(f); |
821 |
return; |
822 |
} |
823 |
|
824 |
read_uint32(&records, f); |
825 |
|
826 |
for (i = 0; i < records; ++i) |
827 |
{ |
828 |
SAFE_READ(read_string(&name, f)); |
829 |
SAFE_READ(read_string(&reason, f)); |
830 |
SAFE_READ(read_uint64(&tmp64_setat, f)); |
831 |
SAFE_READ(read_uint64(&tmp64_hold, f)); |
832 |
|
833 |
gecos = gecos_make(); |
834 |
gecos->in_database = 1; |
835 |
gecos->mask = name; |
836 |
gecos->reason = reason; |
837 |
gecos->setat = tmp64_setat; |
838 |
gecos->expire = tmp64_hold; |
839 |
} |
840 |
|
841 |
close_db(f); |
842 |
} |
843 |
|
844 |
void |
845 |
save_all_databases(void *unused) |
846 |
{ |
847 |
save_kline_database(ConfigGeneral.klinefile); |
848 |
save_dline_database(ConfigGeneral.dlinefile); |
849 |
save_xline_database(ConfigGeneral.xlinefile); |
850 |
save_resv_database(ConfigGeneral.resvfile); |
851 |
} |