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