ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/conf_db.c
Revision: 1626
Committed: Thu Nov 1 17:21:59 2012 UTC (11 years, 5 months ago) by michael
Content type: text/x-csrc
File size: 24125 byte(s)
Log Message:
- minor cleanups to conf_db.c; Get rid of 16 bit limitation string lengths

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 *
4 * Copyright (C) 1996-2002 by Andrew Church <achurch@achurch.org>
5 * Copyright (C) 2012 by the 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: conf_db.c 1569 2012-10-16 18:46:53Z michael $
26 */
27
28 #include "stdinc.h"
29 #include "conf_db.h"
30 #include "memory.h"
31 #include "client.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 *service, 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, "Can't read %s database %s", service,
108 f->filename);
109
110 MyFree(f);
111 errno = errno_save;
112 return NULL;
113 }
114
115 f->fp = fp;
116 f->backupfp = NULL;
117
118 return f;
119 }
120
121 /*! \brief Open the database for writting
122 * \param service If error whom to return the error as
123 * \param filename File to open as the database
124 * \param version Database Version
125 * \return dbFile struct
126 */
127 static struct dbFILE *
128 open_db_write(const char *service, const char *filename,
129 uint32_t version)
130 {
131 struct dbFILE *f = MyMalloc(sizeof(*f));
132 int fd = 0;
133
134 strlcpy(f->filename, filename, sizeof(f->filename));
135
136 filename = f->filename;
137 f->mode = 'w';
138
139 snprintf(f->backupname, sizeof(f->backupname), "%s.save", filename);
140
141 if (!*f->backupname || !strcmp(f->backupname, filename))
142 {
143 int errno_save = errno;
144
145 ilog(LOG_TYPE_IRCD, "Opening %s database %s for write: Filename too long",
146 service, filename);
147 MyFree(f);
148 errno = errno_save;
149 return NULL;
150 }
151
152 unlink(filename);
153
154 f->backupfp = fopen(filename, "rb");
155
156 if (rename(filename, f->backupname) < 0 && errno != ENOENT)
157 {
158 int errno_save = errno;
159 static int walloped = 0;
160
161 if (!walloped++)
162 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
163 "Can't back up %s database %s",
164 service, filename);
165
166 errno = errno_save;
167 ilog(LOG_TYPE_IRCD, "Can't back up %s database %s", service, filename);
168
169 if (f->backupfp)
170 fclose(f->backupfp);
171
172 MyFree(f);
173 errno = errno_save;
174 return NULL;
175 }
176
177 unlink(filename);
178
179 /* Use open() to avoid people sneaking a new file in under us */
180 if ((fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666)) >= 0)
181 f->fp = fdopen(fd, "wb");
182
183 if (!f->fp || !write_file_version(f, version))
184 {
185 int errno_save = errno;
186 static int walloped = 0;
187
188 if (!walloped++)
189 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
190 "Can't write to %s database %s",
191 service, filename);
192
193 errno = errno_save;
194 ilog(LOG_TYPE_IRCD, "Can't write to %s database %s",
195 service, filename);
196
197 if (f->fp)
198 {
199 fclose(f->fp);
200 unlink(filename);
201 }
202
203 if (f->backupname[0] && rename(f->backupname, filename) < 0)
204 ilog(LOG_TYPE_IRCD, "Can't restore backup copy of %s",
205 filename);
206
207 MyFree(f);
208 errno = errno_save;
209 return NULL;
210 }
211
212 return f;
213 }
214
215 /*! \brief Open a database file for reading (*mode == 'r') or writing (*mode == 'w').
216 * Return the stream pointer, or NULL on error. When opening for write, it
217 * is an error for rename() to return an error (when backing up the original
218 * file) other than ENOENT, if NO_BACKUP_OKAY is not defined; it is an error
219 * if the version number cannot be written to the file; and it is a fatal
220 * error if opening the file for write fails and the backup was successfully
221 * made but cannot be restored.
222 *
223 * \param service If error whom to return the error as
224 * \param filename File to open as the database
225 * \param mode Mode for writting or reading
226 * \param version Database Version
227 * \return dbFile struct
228 */
229 struct dbFILE *
230 open_db(const char *service, const char *filename,
231 const char *mode, uint32_t version)
232 {
233 switch (*mode)
234 {
235 case 'r':
236 return open_db_read(service, filename);
237 break;
238 case 'w':
239 return open_db_write(service, filename, version);
240 break;
241 default:
242 errno = EINVAL;
243 return NULL;
244 }
245 }
246
247 /*! \brief Restore the database file to its condition before open_db(). This is
248 * identical to close_db() for files open for reading; however, for files
249 * open for writing, we first attempt to restore any backup file before
250 * closing files.
251 *
252 * \param dbFile struct
253 */
254 void
255 restore_db(struct dbFILE *f)
256 {
257 int errno_save = errno;
258
259 if (f->mode == 'w')
260 {
261 int ok = 0; /* Did we manage to restore the old file? */
262
263 errno = errno_save = 0;
264
265 if (f->backupname[0] && strcmp(f->backupname, f->filename))
266 if (rename(f->backupname, f->filename) == 0)
267 ok = 1;
268
269 if (!ok && f->backupfp)
270 {
271 char buf[1024];
272 size_t i;
273
274 ok = fseek(f->fp, 0, SEEK_SET) == 0;
275
276 while (ok && (i = fread(buf, 1, sizeof(buf), f->backupfp)) > 0)
277 if (fwrite(buf, 1, i, f->fp) != i)
278 ok = 0;
279
280 if (ok)
281 {
282 fflush(f->fp);
283 ftruncate(fileno(f->fp), ftell(f->fp));
284 }
285 }
286
287 if (!ok && errno > 0)
288 ilog(LOG_TYPE_IRCD, "Unable to restore backup of %s", f->filename);
289
290 errno_save = errno;
291
292 if (f->backupfp)
293 fclose(f->backupfp);
294 if (f->backupname[0])
295 unlink(f->backupname);
296 }
297
298 fclose(f->fp);
299
300 if (!errno_save)
301 errno_save = errno;
302
303 MyFree(f);
304 errno = errno_save;
305 }
306
307 /*! \brief Close a database file. If the file was opened for write, remove the
308 * backup we (may have) created earlier.
309 *
310 * \param dbFile struct
311 */
312 void
313 close_db(struct dbFILE *f)
314 {
315 if (f->mode == 'w' && f->backupname[0] &&
316 strcmp(f->backupname, f->filename))
317 {
318 if (f->backupfp)
319 fclose(f->backupfp);
320
321 unlink(f->backupname);
322 }
323
324 fclose(f->fp);
325 MyFree(f);
326 }
327
328 /*
329 * Read and write 2-, 4- and 8-byte quantities, pointers, and strings. All
330 * multibyte values are stored in big-endian order (most significant byte
331 * first). A pointer is stored as a byte, either 0 if NULL or 1 if not,
332 * and read pointers are returned as either (void *)0 or (void *)1. A
333 * string is stored with a 2-byte unsigned length (including the trailing
334 * \0) first; a length of 0 indicates that the string pointer is NULL.
335 * Written strings are truncated silently at 4294967294 bytes, and are always
336 * null-terminated.
337 */
338
339 /*! \brief Read a unsigned 8bit integer
340 *
341 * \param ret 8bit integer to read
342 * \param dbFile struct
343 * \return -1 on error, 0 otherwise.
344 */
345 int
346 read_uint8(unsigned char *ret, struct dbFILE *f)
347 {
348 int c = fgetc(f->fp);
349
350 if (c == EOF)
351 return -1;
352
353 *ret = c;
354 return 0;
355 }
356
357 /*! \brief Write a 8bit integer
358 *
359 * \param ret 8bit integer to write
360 * \param dbFile struct
361 * \return -1 on error, 0 otherwise.
362 */
363 int
364 write_uint8(unsigned char val, struct dbFILE *f)
365 {
366 if (fputc(val, f->fp) == EOF)
367 return -1;
368
369 return 0;
370 }
371
372 /*! \brief Read a unsigned 8bit integer
373 *
374 * \param ret 8bit integer to read
375 * \param dbFile struct
376 * \return -1 on error, 0 otherwise.
377 */
378 int
379 read_uint16(uint16_t *ret, struct dbFILE *f)
380 {
381 int c1 = fgetc(f->fp);
382 int c2 = fgetc(f->fp);
383
384 if (c1 == EOF || c2 == EOF)
385 return -1;
386
387 *ret = c1 << 8 | c2;
388 return 0;
389 }
390
391 /*! \brief Write a unsigned 16bit integer
392 *
393 * \param ret 16bit integer to write
394 * \param dbFile struct
395 * \return -1 on error, 0 otherwise.
396 */
397 int
398 write_uint16(uint16_t val, struct dbFILE *f)
399 {
400 if (fputc((val >> 8) & 0xFF, f->fp) == EOF ||
401 fputc(val & 0xFF, f->fp) == EOF)
402 return -1;
403
404 return 0;
405 }
406
407 /*! \brief Read a unsigned 32bit integer
408 *
409 * \param ret unsigned 32bit integer to read
410 * \param dbFile struct
411 * \return -1 on error, 0 otherwise.
412 */
413 int
414 read_uint32(uint32_t *ret, struct dbFILE *f)
415 {
416 int c1 = fgetc(f->fp);
417 int c2 = fgetc(f->fp);
418 int c3 = fgetc(f->fp);
419 int c4 = fgetc(f->fp);
420
421 if (c1 == EOF || c2 == EOF || c3 == EOF || c4 == EOF)
422 return -1;
423
424 *ret = c1 << 24 | c2 << 16 | c3 << 8 | c4;
425 return 0;
426 }
427
428
429 /*! \brief Write a unsigned 32bit integer
430 *
431 * \param ret unsigned 32bit integer to write
432 * \param dbFile struct
433 * \return -1 on error, 0 otherwise.
434 */
435 int
436 write_uint32(uint32_t val, struct dbFILE *f)
437 {
438 if (fputc((val >> 24) & 0xFF, f->fp) == EOF)
439 return -1;
440 if (fputc((val >> 16) & 0xFF, f->fp) == EOF)
441 return -1;
442 if (fputc((val >> 8) & 0xFF, f->fp) == EOF)
443 return -1;
444 if (fputc((val) & 0xFF, f->fp) == EOF)
445 return -1;
446 return 0;
447 }
448
449 /*! \brief Read a unsigned 64bit integer
450 *
451 * \param ret unsigned 64bit integer to read
452 * \param dbFile struct
453 * \return -1 on error, 0 otherwise.
454 */
455 int
456 read_uint64(uint64_t *ret, struct dbFILE *f)
457 {
458 int64_t c1 = fgetc(f->fp);
459 int64_t c2 = fgetc(f->fp);
460 int64_t c3 = fgetc(f->fp);
461 int64_t c4 = fgetc(f->fp);
462 int64_t c5 = fgetc(f->fp);
463 int64_t c6 = fgetc(f->fp);
464 int64_t c7 = fgetc(f->fp);
465 int64_t c8 = fgetc(f->fp);
466
467 if (c1 == EOF || c2 == EOF || c3 == EOF || c4 == EOF ||
468 c5 == EOF || c6 == EOF || c7 == EOF || c8 == EOF)
469 return -1;
470
471 *ret = c1 << 56 | c2 << 48 | c3 << 40 | c4 << 32 |
472 c5 << 24 | c6 << 16 | c7 << 8 | c8;
473 return 0;
474 }
475
476
477 /*! \brief Write a unsigned 64bit integer
478 *
479 * \param ret unsigned 64bit integer to write
480 * \param dbFile struct
481 * \return -1 on error, 0 otherwise.
482 */
483 int
484 write_uint64(uint64_t val, struct dbFILE *f)
485 {
486 if (fputc((val >> 56) & 0xFF, f->fp) == EOF)
487 return -1;
488 if (fputc((val >> 48) & 0xFF, f->fp) == EOF)
489 return -1;
490 if (fputc((val >> 40) & 0xFF, f->fp) == EOF)
491 return -1;
492 if (fputc((val >> 32) & 0xFF, f->fp) == EOF)
493 return -1;
494 if (fputc((val >> 24) & 0xFF, f->fp) == EOF)
495 return -1;
496 if (fputc((val >> 16) & 0xFF, f->fp) == EOF)
497 return -1;
498 if (fputc((val >> 8) & 0xFF, f->fp) == EOF)
499 return -1;
500 if (fputc((val) & 0xFF, f->fp) == EOF)
501 return -1;
502 return 0;
503 }
504
505 /*! \brief Read Pointer
506 *
507 * \param ret pointer to read
508 * \param dbFile struct
509 * \return -1 on error, 0 otherwise.
510 */
511 int
512 read_ptr(void **ret, struct dbFILE *f)
513 {
514 int c = fgetc(f->fp);
515
516 if (c == EOF)
517 return -1;
518
519 *ret = (c ? (void *)1 : (void *)0);
520 return 0;
521 }
522
523
524 /*! \brief Write Pointer
525 *
526 * \param ret pointer to write
527 * \param dbFile struct
528 * \return -1 on error, 0 otherwise.
529 */
530 int
531 write_ptr(const void *ptr, struct dbFILE *f)
532 {
533 if (fputc(ptr ? 1 : 0, f->fp) == EOF)
534 return -1;
535 return 0;
536 }
537
538 /*! \brief Read String
539 *
540 * \param ret string
541 * \param dbFile struct
542 * \return -1 on error, 0 otherwise.
543 */
544 int
545 read_string(char **ret, struct dbFILE *f)
546 {
547 char *s = NULL;
548 uint32_t len = 0;
549
550 if (read_uint32(&len, f) < 0)
551 return -1;
552
553 if (len == 0)
554 {
555 *ret = NULL;
556 return 0;
557 }
558
559 s = MyMalloc(len);
560
561 if (len != fread(s, 1, len, f->fp))
562 {
563 MyFree(s);
564 return -1;
565 }
566
567 *ret = s;
568 return 0;
569 }
570
571 /*! \brief Write String
572 *
573 * \param ret string
574 * \param dbFile struct
575 * \return -1 on error, 0 otherwise.
576 */
577 int
578 write_string(const char *s, struct dbFILE *f)
579 {
580 uint32_t len = 0;
581
582 if (!s)
583 return write_uint32(0, f);
584
585 len = strlen(s);
586
587 if (len > 4294967294)
588 len = 4294967294;
589 if (write_uint32(len + 1, f) < 0)
590 return -1;
591 if (len > 0 && fwrite(s, 1, len, f->fp) != len)
592 return -1;
593 if (fputc(0, f->fp) == EOF)
594 return -1;
595
596 return 0;
597 }
598
599 #define SAFE_READ(x) do { \
600 if ((x) < 0) { \
601 break; \
602 } \
603 } while (0)
604
605 #define SAFE_WRITE(x,db) do { \
606 if ((x) < 0) { \
607 restore_db(f); \
608 ilog(LOG_TYPE_IRCD, "Write error on %s", db); \
609 return; \
610 } \
611 } while (0)
612
613 void
614 save_kline_database(void)
615 {
616 unsigned int i = 0;
617 uint32_t cnt = 0, version = 0;
618 struct dbFILE *f = NULL;
619 dlink_node *ptr = NULL;
620
621 if (!(f = open_db("kline", KPATH, "w", KLINE_DB_VERSION)))
622 return;
623
624 for (i = 0; i < ATABLE_SIZE; ++i)
625 {
626 DLINK_FOREACH(ptr, atable[i].head)
627 {
628 struct AddressRec *arec = ptr->data;
629
630 if (arec->type == CONF_KLINE && !IsConfMain(arec->aconf))
631 ++cnt;
632 }
633 }
634
635 SAFE_WRITE(write_uint32(cnt, f), KPATH);
636
637 for (i = 0; i < ATABLE_SIZE; ++i)
638 {
639 DLINK_FOREACH(ptr, atable[i].head)
640 {
641 struct AddressRec *arec = ptr->data;
642
643 if (arec->type == CONF_KLINE && !IsConfMain(arec->aconf))
644 {
645 SAFE_WRITE(write_string(arec->aconf->user, f), KPATH);
646 SAFE_WRITE(write_string(arec->aconf->host, f), KPATH);
647 SAFE_WRITE(write_string(arec->aconf->reason, f), KPATH);
648 SAFE_WRITE(write_uint64(arec->aconf->setat, f), KPATH);
649 SAFE_WRITE(write_uint64(arec->aconf->hold, f), KPATH);
650 }
651 }
652 }
653
654 close_db(f);
655 }
656
657 void
658 load_kline_database(void)
659 {
660 unsigned int i = 0;
661 uint32_t cnt = 0, version = 0;
662 uint64_t tmp64 = 0;
663 struct dbFILE *f = NULL;
664 dlink_node *ptr = NULL, *ptr_next = NULL;
665
666 if (!(f = open_db("kline", KPATH, "r", KLINE_DB_VERSION)))
667 return;
668
669 if ((version = get_file_version(f) < 1))
670 {
671 close_db(f);
672 return;
673 }
674
675 read_uint32(&cnt, f);
676
677 for (i = 0; i < cnt; ++i)
678 {
679 struct AccessItem *aconf = map_to_conf(make_conf_item(KLINE_TYPE));
680
681 SAFE_READ(read_string(&aconf->user, f));
682 SAFE_READ(read_string(&aconf->host, f));
683 SAFE_READ(read_string(&aconf->reason, f));
684
685 SAFE_READ(read_uint64(&tmp64, f));
686 aconf->setat = tmp64;
687
688 SAFE_READ(read_uint64(&tmp64, f));
689 aconf->hold = tmp64;
690
691 if (aconf->hold)
692 SetConfTemporary(aconf);
693
694 add_conf_by_address(CONF_KLINE, aconf);
695 }
696
697 close_db(f);
698 }
699
700 void
701 save_dline_database(void)
702 {
703 unsigned int i = 0;
704 uint32_t cnt = 0, version = 0;
705 struct dbFILE *f = NULL;
706 dlink_node *ptr = NULL;
707
708 if (!(f = open_db("dline", DLPATH, "w", KLINE_DB_VERSION)))
709 return;
710
711 for (i = 0; i < ATABLE_SIZE; ++i)
712 {
713 DLINK_FOREACH(ptr, atable[i].head)
714 {
715 struct AddressRec *arec = ptr->data;
716
717 if (arec->type == CONF_DLINE && !IsConfMain(arec->aconf))
718 ++cnt;
719 }
720 }
721
722 SAFE_WRITE(write_uint32(cnt, f), DLPATH);
723
724 for (i = 0; i < ATABLE_SIZE; ++i)
725 {
726 DLINK_FOREACH(ptr, atable[i].head)
727 {
728 struct AddressRec *arec = ptr->data;
729
730 if (arec->type == CONF_DLINE && !IsConfMain(arec->aconf))
731 {
732 SAFE_WRITE(write_string(arec->aconf->host, f), DLPATH);
733 SAFE_WRITE(write_string(arec->aconf->reason, f), DLPATH);
734 SAFE_WRITE(write_uint64(arec->aconf->setat, f), DLPATH);
735 SAFE_WRITE(write_uint64(arec->aconf->hold, f), DLPATH);
736 }
737 }
738 }
739
740 close_db(f);
741 }
742
743 void
744 load_dline_database(void)
745 {
746 unsigned int i = 0;
747 uint32_t cnt = 0, version = 0;
748 uint64_t tmp64 = 0;
749 struct dbFILE *f = NULL;
750 dlink_node *ptr = NULL, *ptr_next = NULL;
751
752 if (!(f = open_db("dline", DLPATH, "r", KLINE_DB_VERSION)))
753 return;
754
755 if ((version = get_file_version(f) < 1))
756 {
757 close_db(f);
758 return;
759 }
760
761 read_uint32(&cnt, f);
762
763 for (i = 0; i < cnt; ++i)
764 {
765 struct AccessItem *aconf = map_to_conf(make_conf_item(DLINE_TYPE));
766
767 SAFE_READ(read_string(&aconf->host, f));
768 SAFE_READ(read_string(&aconf->reason, f));
769
770 SAFE_READ(read_uint64(&tmp64, f));
771 aconf->setat = tmp64;
772
773 SAFE_READ(read_uint64(&tmp64, f));
774 aconf->hold = tmp64;
775
776 if (aconf->hold)
777 SetConfTemporary(aconf);
778
779 add_conf_by_address(CONF_DLINE, aconf);
780 }
781
782 close_db(f);
783 }
784
785 void
786 save_gline_database(void)
787 {
788 unsigned int i = 0;
789 uint32_t cnt = 0, version = 0;
790 struct dbFILE *f = NULL;
791 dlink_node *ptr = NULL;
792
793 if (!(f = open_db("gline", GPATH, "w", KLINE_DB_VERSION)))
794 return;
795
796 for (i = 0; i < ATABLE_SIZE; ++i)
797 {
798 DLINK_FOREACH(ptr, atable[i].head)
799 {
800 struct AddressRec *arec = ptr->data;
801
802 if (arec->type == CONF_GLINE && !IsConfMain(arec->aconf))
803 ++cnt;
804 }
805 }
806
807 SAFE_WRITE(write_uint32(cnt, f), GPATH);
808
809 for (i = 0; i < ATABLE_SIZE; ++i)
810 {
811 DLINK_FOREACH(ptr, atable[i].head)
812 {
813 struct AddressRec *arec = ptr->data;
814
815 if (arec->type == CONF_GLINE && !IsConfMain(arec->aconf))
816 {
817 SAFE_WRITE(write_string(arec->aconf->user, f), GPATH);
818 SAFE_WRITE(write_string(arec->aconf->host, f), GPATH);
819 SAFE_WRITE(write_string(arec->aconf->reason, f), GPATH);
820 SAFE_WRITE(write_uint64(arec->aconf->setat, f), GPATH);
821 SAFE_WRITE(write_uint64(arec->aconf->hold, f), GPATH);
822 }
823 }
824 }
825
826 close_db(f);
827 }
828
829 void
830 load_gline_database(void)
831 {
832 unsigned int i = 0;
833 uint32_t cnt = 0, version = 0;
834 uint64_t tmp64 = 0;
835 struct dbFILE *f = NULL;
836 dlink_node *ptr = NULL, *ptr_next = NULL;
837
838 if (!(f = open_db("gline", GPATH, "r", KLINE_DB_VERSION)))
839 return;
840
841 if ((version = get_file_version(f) < 1))
842 {
843 close_db(f);
844 return;
845 }
846
847 read_uint32(&cnt, f);
848
849 for (i = 0; i < cnt; ++i)
850 {
851 struct AccessItem *aconf = map_to_conf(make_conf_item(GLINE_TYPE));
852
853 SAFE_READ(read_string(&aconf->user, f));
854 SAFE_READ(read_string(&aconf->host, f));
855 SAFE_READ(read_string(&aconf->reason, f));
856
857 SAFE_READ(read_uint64(&tmp64, f));
858 aconf->setat = tmp64;
859
860 SAFE_READ(read_uint64(&tmp64, f));
861 aconf->hold = tmp64;
862
863 if (aconf->hold)
864 SetConfTemporary(aconf);
865
866 add_conf_by_address(CONF_GLINE, aconf);
867 }
868
869 close_db(f);
870 }
871
872 void
873 save_resv_database(void)
874 {
875 unsigned int i = 0;
876 uint32_t cnt = 0, version = 0;
877 struct dbFILE *f = NULL;
878 dlink_node *ptr = NULL;
879 struct ConfItem *conf;
880 struct ResvChannel *resv_cp;
881 struct MatchItem *resv_np;
882
883 if (!(f = open_db("resv", RESVPATH, "w", KLINE_DB_VERSION)))
884 return;
885
886 DLINK_FOREACH(ptr, resv_channel_list.head)
887 {
888 resv_cp = ptr->data;
889
890 if (!resv_cp->conf)
891 ++cnt;
892 }
893
894 DLINK_FOREACH(ptr, nresv_items.head)
895 {
896 resv_np = map_to_conf(ptr->data);
897
898 if (!resv_np->action)
899 ++cnt;
900 }
901
902 SAFE_WRITE(write_uint32(cnt, f), RESVPATH);
903
904
905 DLINK_FOREACH(ptr, resv_channel_list.head)
906 {
907 resv_cp = ptr->data;
908
909 SAFE_WRITE(write_string(resv_cp->name, f), RESVPATH);
910 SAFE_WRITE(write_string(resv_cp->reason, f), RESVPATH);
911 SAFE_WRITE(write_uint64(resv_cp->setat, f), RESVPATH);
912 SAFE_WRITE(write_uint64(resv_cp->hold, f), RESVPATH);
913 }
914
915 DLINK_FOREACH(ptr, nresv_items.head)
916 {
917 conf = ptr->data;
918 resv_np = map_to_conf(conf);
919
920 SAFE_WRITE(write_string(conf->name, f), RESVPATH);
921 SAFE_WRITE(write_string(resv_np->reason, f), RESVPATH);
922 SAFE_WRITE(write_uint64(resv_np->setat, f), RESVPATH);
923 SAFE_WRITE(write_uint64(resv_np->hold, f), RESVPATH);
924 }
925
926 close_db(f);
927 }
928
929 void
930 load_resv_database(void)
931 {
932 unsigned int i = 0;
933 uint32_t cnt = 0, version = 0;
934 uint64_t tmp64_hold = 0, tmp64_setat = 0;
935 struct dbFILE *f = NULL;
936 dlink_node *ptr = NULL, *ptr_next = NULL;
937 char *name = NULL;
938 char *reason = NULL;
939 struct ConfItem *conf;
940 struct ResvChannel *resv_cp;
941 struct MatchItem *resv_np;
942
943 if (!(f = open_db("resv", RESVPATH, "r", KLINE_DB_VERSION)))
944 return;
945
946 if ((version = get_file_version(f) < 1))
947 {
948 close_db(f);
949 return;
950 }
951
952 read_uint32(&cnt, f);
953
954 for (i = 0; i < cnt; ++i)
955 {
956 SAFE_READ(read_string(&name, f));
957 SAFE_READ(read_string(&reason, f));
958 SAFE_READ(read_uint64(&tmp64_setat, f));
959 SAFE_READ(read_uint64(&tmp64_hold, f));
960
961 if (IsChanPrefix(*name))
962 {
963 if ((conf = create_channel_resv(name, reason, 0)) == NULL)
964 continue;
965
966 resv_cp = map_to_conf(conf);
967 resv_cp->setat = tmp64_setat;
968 resv_cp->hold = tmp64_hold;
969
970 if (resv_cp->hold)
971 add_temp_line(conf);
972 }
973 else
974 {
975 if ((conf = create_nick_resv(name, reason, 0)) == NULL)
976 continue;
977
978 resv_np = map_to_conf(conf);
979 resv_np->setat = tmp64_setat;
980 resv_np->hold = tmp64_hold;
981
982 if (resv_np->hold)
983 add_temp_line(conf);
984 }
985
986 MyFree(name);
987 MyFree(reason);
988 }
989
990 close_db(f);
991 }
992
993 void
994 save_xline_database(void)
995 {
996 unsigned int i = 0;
997 uint32_t cnt = 0, version = 0;
998 struct dbFILE *f = NULL;
999 dlink_node *ptr = NULL;
1000 struct ConfItem *conf = NULL;
1001 struct MatchItem *xconf = NULL;
1002
1003 if (!(f = open_db("xline", XPATH, "w", KLINE_DB_VERSION)))
1004 return;
1005
1006 DLINK_FOREACH(ptr, xconf_items.head)
1007 {
1008 conf = ptr->data;
1009
1010 if (!IsConfMain(conf))
1011 ++cnt;
1012 }
1013
1014 SAFE_WRITE(write_uint32(cnt, f), XPATH);
1015
1016
1017 DLINK_FOREACH(ptr, xconf_items.head)
1018 {
1019 conf = ptr->data;
1020 xconf = map_to_conf(conf);
1021
1022 SAFE_WRITE(write_string(conf->name, f), XPATH);
1023 SAFE_WRITE(write_string(xconf->reason, f), XPATH);
1024 SAFE_WRITE(write_uint64(xconf->setat, f), XPATH);
1025 SAFE_WRITE(write_uint64(xconf->hold, f), XPATH);
1026 }
1027
1028 close_db(f);
1029 }
1030
1031 void
1032 load_xline_database(void)
1033 {
1034 unsigned int i = 0;
1035 uint32_t cnt = 0, version = 0;
1036 uint64_t tmp64_hold = 0, tmp64_setat = 0;
1037 struct dbFILE *f = NULL;
1038 dlink_node *ptr = NULL, *ptr_next = NULL;
1039 char *name = NULL;
1040 char *reason = NULL;
1041 struct ConfItem *conf = NULL;
1042 struct MatchItem *xconf = NULL;
1043
1044 if (!(f = open_db("xline", XPATH, "r", KLINE_DB_VERSION)))
1045 return;
1046
1047 if ((version = get_file_version(f) < 1))
1048 {
1049 close_db(f);
1050 return;
1051 }
1052
1053 read_uint32(&cnt, f);
1054
1055 for (i = 0; i < cnt; ++i)
1056 {
1057 SAFE_READ(read_string(&name, f));
1058 SAFE_READ(read_string(&reason, f));
1059 SAFE_READ(read_uint64(&tmp64_setat, f));
1060 SAFE_READ(read_uint64(&tmp64_hold, f));
1061
1062 conf = make_conf_item(XLINE_TYPE);
1063 xconf = map_to_conf(conf);
1064
1065 conf->name = name;
1066 xconf->reason = reason;
1067 xconf->setat = tmp64_setat;
1068 xconf->hold = tmp64_hold;
1069
1070 if (xconf->hold)
1071 SetConfTemporary(conf);
1072 }
1073
1074 close_db(f);
1075 }
1076
1077 void
1078 save_all_databases(void *unused)
1079 {
1080 save_kline_database();
1081 save_dline_database();
1082 save_gline_database();
1083 save_xline_database();
1084 save_resv_database();
1085 }