ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/conf_db.c
Revision: 5805
Committed: Tue Apr 21 21:49:58 2015 UTC (11 years, 5 months ago) by michael
Content type: text/x-csrc
File size: 20104 byte(s)
Log Message:
- Removed glines
- Added kline_min_cidr, kline_min_cidr6, dline_min_cidr and
  dline_min_cidr6 configuration options

File Contents

# User Rev Content
1 michael 1620 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 michael 1620 *
4 michael 1668 * Copyright (C) 1996-2009 by Andrew Church <achurch@achurch.org>
5 michael 5347 * Copyright (c) 2012-2015 ircd-hybrid development team
6 michael 1620 *
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 michael 4565 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20 michael 1620 * USA
21     */
22    
23     /*! \file conf_db.c
24     * \brief Includes file utilities for database handling
25 michael 1635 * \version $Id$
26 michael 1620 */
27    
28 michael 2916
29 michael 1620 #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 michael 1622 #include "conf.h"
36     #include "hostmask.h"
37     #include "resv.h"
38 michael 1620
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 michael 1621 uint32_t
48 michael 1620 get_file_version(struct dbFILE *f)
49     {
50 michael 1621 uint32_t version = 0;
51 michael 1620
52 michael 1621 if (read_uint32(&version, f) == -1)
53 michael 1620 {
54 michael 1621 ilog(LOG_TYPE_IRCD, "Error reading version number on %s: %s",
55     f->filename, strerror(errno));
56 michael 1620 return 0;
57     }
58 michael 1621
59     if (version < 1)
60 michael 1620 {
61 michael 1621 ilog(LOG_TYPE_IRCD, "Invalid version number (%u) on %s",
62 michael 1620 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 michael 3286 * \param version Database version
72 michael 1620 * \return 0 on error, 1 on success.
73     */
74     int
75     write_file_version(struct dbFILE *f, uint32_t version)
76     {
77     if (write_uint32(version, f) == -1)
78     {
79     ilog(LOG_TYPE_IRCD, "Error writing version number on %s",
80     f->filename);
81     return 0;
82     }
83    
84     return 1;
85     }
86    
87     /*! \brief Open the database for reading
88     * \param filename File to open as the database
89     * \return dbFile struct
90     */
91     static struct dbFILE *
92 michael 1668 open_db_read(const char *filename)
93 michael 1620 {
94 michael 3504 struct dbFILE *f = MyCalloc(sizeof(*f));
95 michael 1620 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 michael 3067 ilog(LOG_TYPE_IRCD, "Cannot read database file %s", f->filename);
108 michael 1620
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 michael 3286 * \param version Database version
121 michael 1620 * \return dbFile struct
122     */
123     static struct dbFILE *
124 michael 1668 open_db_write(const char *filename, uint32_t version)
125 michael 1620 {
126 michael 3504 struct dbFILE *f = MyCalloc(sizeof(*f));
127 michael 1620 int fd = 0;
128    
129     strlcpy(f->filename, filename, sizeof(f->filename));
130    
131     filename = f->filename;
132     f->mode = 'w';
133    
134 michael 1668 snprintf(f->tempname, sizeof(f->tempname), "%s.new", filename);
135 michael 1620
136 michael 1668 if (f->tempname[0] == '\0' || !strcmp(f->tempname, filename))
137 michael 1620 {
138 michael 1668 ilog(LOG_TYPE_IRCD, "Opening database file %s for write: Filename too long",
139     filename);
140 michael 1620 MyFree(f);
141 michael 1668 errno = ENAMETOOLONG;
142 michael 1620 return NULL;
143     }
144    
145 michael 1668 remove(f->tempname);
146 michael 1620
147     /* Use open() to avoid people sneaking a new file in under us */
148 michael 1668 fd = open(f->tempname, O_WRONLY | O_CREAT | O_EXCL, 0666);
149     if (fd >= 0)
150 michael 1626 f->fp = fdopen(fd, "wb");
151 michael 1620
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 michael 3067 "Cannot create temporary database file %s",
160 michael 1668 f->tempname);
161 michael 1620
162     errno = errno_save;
163 michael 3067 ilog(LOG_TYPE_IRCD, "Cannot create temporary database file %s",
164 michael 1668 f->tempname);
165 michael 1620
166     if (f->fp)
167     fclose(f->fp);
168    
169 michael 1668 remove(f->tempname);
170     MyFree(f);
171 michael 1620
172 michael 1668 errno = errno_save;
173     return NULL;
174 michael 1620 }
175    
176     return f;
177     }
178    
179     /*! \brief Open a database file for reading (*mode == 'r') or writing (*mode == 'w').
180 michael 1668 * 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 michael 1620 *
184 michael 1668 * `version' is only used when opening a file for writing, and indicates the
185     * version number to write to the file.
186     *
187 michael 1620 * \param filename File to open as the database
188     * \param mode Mode for writting or reading
189 michael 3286 * \param version Database version
190 michael 1620 * \return dbFile struct
191     */
192     struct dbFILE *
193 michael 1668 open_db(const char *filename, const char *mode, uint32_t version)
194 michael 1620 {
195     switch (*mode)
196     {
197     case 'r':
198 michael 1668 return open_db_read(filename);
199 michael 1620 break;
200     case 'w':
201 michael 1668 return open_db_write(filename, version);
202 michael 1620 break;
203     default:
204     errno = EINVAL;
205     return NULL;
206     }
207     }
208    
209 michael 1668 /*! \brief Restore the database file to its condition before open_db(). This is
210 michael 1620 * identical to close_db() for files open for reading; however, for files
211 michael 1668 * 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 michael 1620 *
214     * \param dbFile struct
215     */
216     void
217     restore_db(struct dbFILE *f)
218     {
219     int errno_save = errno;
220    
221 michael 1668 if (f->fp)
222     fclose(f->fp);
223     if (f->mode == 'w' && f->tempname[0])
224     remove(f->tempname);
225 michael 1620
226     MyFree(f);
227     errno = errno_save;
228     }
229    
230 michael 1668 /*! \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 michael 1620 *
234     * \param dbFile struct
235 michael 3286 * \return -1 on error, 0 on success.
236 michael 1620 */
237 michael 1668 int
238 michael 1620 close_db(struct dbFILE *f)
239     {
240 michael 1668 int res;
241    
242     if (!f->fp)
243 michael 1620 {
244 michael 1668 errno = EINVAL;
245     return -1;
246     }
247 michael 1620
248 michael 1668 res = fclose(f->fp);
249     f->fp = NULL;
250    
251     if (res != 0)
252     return -1;
253    
254     if (f->mode == 'w' && f->tempname[0] && strcmp(f->tempname, f->filename))
255     {
256     if (rename(f->tempname, f->filename) < 0)
257     {
258     int errno_save = errno;
259    
260     sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, "Unable to move new "
261     "data to database file %s; new data NOT saved.",
262     f->filename);
263     errno = errno_save;
264     ilog(LOG_TYPE_IRCD, "Unable to move new data to database file %s; new "
265     "data NOT saved.", f->filename);
266     remove(f->tempname);
267     }
268 michael 1620 }
269    
270     MyFree(f);
271 michael 1668 return 0;
272 michael 1620 }
273    
274     /*
275     * Read and write 2-, 4- and 8-byte quantities, pointers, and strings. All
276     * multibyte values are stored in big-endian order (most significant byte
277     * first). A pointer is stored as a byte, either 0 if NULL or 1 if not,
278     * and read pointers are returned as either (void *)0 or (void *)1. A
279     * string is stored with a 2-byte unsigned length (including the trailing
280     * \0) first; a length of 0 indicates that the string pointer is NULL.
281 michael 1626 * Written strings are truncated silently at 4294967294 bytes, and are always
282 michael 1620 * null-terminated.
283     */
284    
285     /*! \brief Read a unsigned 8bit integer
286     *
287     * \param ret 8bit integer to read
288     * \param dbFile struct
289     * \return -1 on error, 0 otherwise.
290     */
291     int
292 michael 1627 read_uint8(uint8_t*ret, struct dbFILE *f)
293 michael 1620 {
294     int c = fgetc(f->fp);
295    
296     if (c == EOF)
297     return -1;
298    
299     *ret = c;
300     return 0;
301     }
302    
303     /*! \brief Write a 8bit integer
304     *
305 michael 3286 * \param val 8bit integer to write
306 michael 1620 * \param dbFile struct
307     * \return -1 on error, 0 otherwise.
308     */
309     int
310 michael 1627 write_uint8(uint8_t val, struct dbFILE *f)
311 michael 1620 {
312     if (fputc(val, f->fp) == EOF)
313     return -1;
314    
315     return 0;
316     }
317    
318     /*! \brief Read a unsigned 8bit integer
319     *
320 michael 3286 * \param ret 16bit integer to read
321 michael 1620 * \param dbFile struct
322     * \return -1 on error, 0 otherwise.
323     */
324     int
325     read_uint16(uint16_t *ret, struct dbFILE *f)
326     {
327     int c1 = fgetc(f->fp);
328     int c2 = fgetc(f->fp);
329    
330     if (c1 == EOF || c2 == EOF)
331     return -1;
332    
333     *ret = c1 << 8 | c2;
334     return 0;
335     }
336    
337     /*! \brief Write a unsigned 16bit integer
338     *
339 michael 3286 * \param val 16bit integer to write
340 michael 1620 * \param dbFile struct
341     * \return -1 on error, 0 otherwise.
342     */
343     int
344     write_uint16(uint16_t val, struct dbFILE *f)
345     {
346     if (fputc((val >> 8) & 0xFF, f->fp) == EOF ||
347     fputc(val & 0xFF, f->fp) == EOF)
348     return -1;
349    
350     return 0;
351     }
352    
353     /*! \brief Read a unsigned 32bit integer
354     *
355     * \param ret unsigned 32bit integer to read
356     * \param dbFile struct
357     * \return -1 on error, 0 otherwise.
358     */
359     int
360     read_uint32(uint32_t *ret, struct dbFILE *f)
361     {
362     int c1 = fgetc(f->fp);
363     int c2 = fgetc(f->fp);
364     int c3 = fgetc(f->fp);
365     int c4 = fgetc(f->fp);
366    
367     if (c1 == EOF || c2 == EOF || c3 == EOF || c4 == EOF)
368     return -1;
369    
370     *ret = c1 << 24 | c2 << 16 | c3 << 8 | c4;
371     return 0;
372     }
373    
374    
375     /*! \brief Write a unsigned 32bit integer
376     *
377 michael 3286 * \param val unsigned 32bit integer to write
378 michael 1620 * \param dbFile struct
379     * \return -1 on error, 0 otherwise.
380     */
381     int
382     write_uint32(uint32_t val, struct dbFILE *f)
383     {
384     if (fputc((val >> 24) & 0xFF, f->fp) == EOF)
385     return -1;
386     if (fputc((val >> 16) & 0xFF, f->fp) == EOF)
387     return -1;
388     if (fputc((val >> 8) & 0xFF, f->fp) == EOF)
389     return -1;
390     if (fputc((val) & 0xFF, f->fp) == EOF)
391     return -1;
392     return 0;
393     }
394    
395     /*! \brief Read a unsigned 64bit integer
396     *
397     * \param ret unsigned 64bit integer to read
398     * \param dbFile struct
399     * \return -1 on error, 0 otherwise.
400     */
401     int
402     read_uint64(uint64_t *ret, struct dbFILE *f)
403     {
404     int64_t c1 = fgetc(f->fp);
405     int64_t c2 = fgetc(f->fp);
406     int64_t c3 = fgetc(f->fp);
407     int64_t c4 = fgetc(f->fp);
408     int64_t c5 = fgetc(f->fp);
409     int64_t c6 = fgetc(f->fp);
410     int64_t c7 = fgetc(f->fp);
411     int64_t c8 = fgetc(f->fp);
412    
413     if (c1 == EOF || c2 == EOF || c3 == EOF || c4 == EOF ||
414     c5 == EOF || c6 == EOF || c7 == EOF || c8 == EOF)
415     return -1;
416    
417     *ret = c1 << 56 | c2 << 48 | c3 << 40 | c4 << 32 |
418     c5 << 24 | c6 << 16 | c7 << 8 | c8;
419     return 0;
420     }
421    
422    
423     /*! \brief Write a unsigned 64bit integer
424     *
425 michael 3286 * \param val unsigned 64bit integer to write
426 michael 1620 * \param dbFile struct
427     * \return -1 on error, 0 otherwise.
428     */
429     int
430     write_uint64(uint64_t val, struct dbFILE *f)
431     {
432     if (fputc((val >> 56) & 0xFF, f->fp) == EOF)
433     return -1;
434     if (fputc((val >> 48) & 0xFF, f->fp) == EOF)
435     return -1;
436     if (fputc((val >> 40) & 0xFF, f->fp) == EOF)
437     return -1;
438     if (fputc((val >> 32) & 0xFF, f->fp) == EOF)
439     return -1;
440     if (fputc((val >> 24) & 0xFF, f->fp) == EOF)
441     return -1;
442     if (fputc((val >> 16) & 0xFF, f->fp) == EOF)
443     return -1;
444     if (fputc((val >> 8) & 0xFF, f->fp) == EOF)
445     return -1;
446     if (fputc((val) & 0xFF, f->fp) == EOF)
447     return -1;
448     return 0;
449     }
450    
451     /*! \brief Read Pointer
452     *
453     * \param ret pointer to read
454     * \param dbFile struct
455     * \return -1 on error, 0 otherwise.
456     */
457     int
458     read_ptr(void **ret, struct dbFILE *f)
459     {
460     int c = fgetc(f->fp);
461    
462     if (c == EOF)
463     return -1;
464    
465     *ret = (c ? (void *)1 : (void *)0);
466     return 0;
467     }
468    
469    
470     /*! \brief Write Pointer
471     *
472 michael 3286 * \param ptr pointer to write
473 michael 1620 * \param dbFile struct
474     * \return -1 on error, 0 otherwise.
475     */
476     int
477     write_ptr(const void *ptr, struct dbFILE *f)
478     {
479     if (fputc(ptr ? 1 : 0, f->fp) == EOF)
480     return -1;
481     return 0;
482     }
483    
484     /*! \brief Read String
485     *
486     * \param ret string
487     * \param dbFile struct
488     * \return -1 on error, 0 otherwise.
489     */
490     int
491     read_string(char **ret, struct dbFILE *f)
492     {
493     char *s = NULL;
494 michael 1626 uint32_t len = 0;
495 michael 1620
496 michael 1626 if (read_uint32(&len, f) < 0)
497 michael 1620 return -1;
498    
499     if (len == 0)
500     {
501     *ret = NULL;
502     return 0;
503     }
504    
505 michael 3504 s = MyCalloc(len);
506 michael 1620
507     if (len != fread(s, 1, len, f->fp))
508     {
509     MyFree(s);
510     return -1;
511     }
512    
513     *ret = s;
514     return 0;
515     }
516    
517     /*! \brief Write String
518     *
519 michael 3286 * \param s string
520 michael 1620 * \param dbFile struct
521     * \return -1 on error, 0 otherwise.
522     */
523     int
524     write_string(const char *s, struct dbFILE *f)
525     {
526     uint32_t len = 0;
527    
528     if (!s)
529 michael 1626 return write_uint32(0, f);
530 michael 1620
531     len = strlen(s);
532    
533 michael 1626 if (len > 4294967294)
534     len = 4294967294;
535     if (write_uint32(len + 1, f) < 0)
536 michael 1620 return -1;
537     if (len > 0 && fwrite(s, 1, len, f->fp) != len)
538     return -1;
539     if (fputc(0, f->fp) == EOF)
540     return -1;
541    
542     return 0;
543     }
544 michael 1622
545     #define SAFE_READ(x) do { \
546     if ((x) < 0) { \
547     break; \
548     } \
549     } while (0)
550    
551     #define SAFE_WRITE(x,db) do { \
552     if ((x) < 0) { \
553     restore_db(f); \
554     ilog(LOG_TYPE_IRCD, "Write error on %s", db); \
555     return; \
556     } \
557     } while (0)
558    
559     void
560     save_kline_database(void)
561     {
562 michael 1628 uint32_t i = 0;
563     uint32_t records = 0;
564 michael 1622 struct dbFILE *f = NULL;
565     dlink_node *ptr = NULL;
566    
567 michael 1668 if (!(f = open_db(KPATH, "w", KLINE_DB_VERSION)))
568 michael 1622 return;
569    
570     for (i = 0; i < ATABLE_SIZE; ++i)
571     {
572     DLINK_FOREACH(ptr, atable[i].head)
573     {
574     struct AddressRec *arec = ptr->data;
575    
576 michael 1632 if (arec->type == CONF_KLINE && IsConfDatabase(arec->conf))
577 michael 1628 ++records;
578 michael 1622 }
579     }
580    
581 michael 1628 SAFE_WRITE(write_uint32(records, f), KPATH);
582 michael 1622
583     for (i = 0; i < ATABLE_SIZE; ++i)
584     {
585     DLINK_FOREACH(ptr, atable[i].head)
586     {
587     struct AddressRec *arec = ptr->data;
588    
589 michael 1632 if (arec->type == CONF_KLINE && IsConfDatabase(arec->conf))
590 michael 1622 {
591 michael 1632 SAFE_WRITE(write_string(arec->conf->user, f), KPATH);
592     SAFE_WRITE(write_string(arec->conf->host, f), KPATH);
593     SAFE_WRITE(write_string(arec->conf->reason, f), KPATH);
594     SAFE_WRITE(write_uint64(arec->conf->setat, f), KPATH);
595 michael 1649 SAFE_WRITE(write_uint64(arec->conf->until, f), KPATH);
596 michael 1622 }
597     }
598     }
599    
600     close_db(f);
601     }
602    
603     void
604     load_kline_database(void)
605     {
606     struct dbFILE *f = NULL;
607 michael 1632 struct MaskItem *conf = NULL;
608 michael 1628 char *field_1 = NULL;
609     char *field_2 = NULL;
610     char *field_3 = NULL;
611     uint32_t i = 0;
612     uint32_t records = 0;
613     uint64_t field_4 = 0;
614     uint64_t field_5 = 0;
615 michael 1622
616 michael 1668 if (!(f = open_db(KPATH, "r", KLINE_DB_VERSION)))
617 michael 1622 return;
618    
619 michael 1628 if (get_file_version(f) < 1)
620 michael 1622 {
621     close_db(f);
622     return;
623     }
624    
625 michael 1628 read_uint32(&records, f);
626 michael 1622
627 michael 1628 for (i = 0; i < records; ++i)
628 michael 1622 {
629 michael 1628 SAFE_READ(read_string(&field_1, f));
630     SAFE_READ(read_string(&field_2, f));
631     SAFE_READ(read_string(&field_3, f));
632     SAFE_READ(read_uint64(&field_4, f));
633     SAFE_READ(read_uint64(&field_5, f));
634 michael 1622
635 michael 1632 conf = conf_make(CONF_KLINE);
636     conf->user = field_1;
637     conf->host = field_2;
638     conf->reason = field_3;
639     conf->setat = field_4;
640 michael 1649 conf->until = field_5;
641 michael 1632 SetConfDatabase(conf);
642 michael 1622
643 michael 1632 add_conf_by_address(CONF_KLINE, conf);
644 michael 1622 }
645    
646     close_db(f);
647     }
648    
649     void
650     save_dline_database(void)
651     {
652 michael 1628 uint32_t i = 0;
653     uint32_t records = 0;
654 michael 1622 struct dbFILE *f = NULL;
655     dlink_node *ptr = NULL;
656    
657 michael 1668 if (!(f = open_db(DLPATH, "w", KLINE_DB_VERSION)))
658 michael 1622 return;
659    
660     for (i = 0; i < ATABLE_SIZE; ++i)
661     {
662     DLINK_FOREACH(ptr, atable[i].head)
663     {
664     struct AddressRec *arec = ptr->data;
665    
666 michael 1632 if (arec->type == CONF_DLINE && IsConfDatabase(arec->conf))
667 michael 1628 ++records;
668 michael 1622 }
669     }
670    
671 michael 1628 SAFE_WRITE(write_uint32(records, f), DLPATH);
672 michael 1622
673     for (i = 0; i < ATABLE_SIZE; ++i)
674     {
675     DLINK_FOREACH(ptr, atable[i].head)
676     {
677     struct AddressRec *arec = ptr->data;
678    
679 michael 1632 if (arec->type == CONF_DLINE && IsConfDatabase(arec->conf))
680 michael 1622 {
681 michael 1632 SAFE_WRITE(write_string(arec->conf->host, f), DLPATH);
682     SAFE_WRITE(write_string(arec->conf->reason, f), DLPATH);
683     SAFE_WRITE(write_uint64(arec->conf->setat, f), DLPATH);
684 michael 1649 SAFE_WRITE(write_uint64(arec->conf->until, f), DLPATH);
685 michael 1622 }
686     }
687     }
688    
689     close_db(f);
690     }
691    
692     void
693     load_dline_database(void)
694     {
695     struct dbFILE *f = NULL;
696 michael 1632 struct MaskItem *conf = NULL;
697 michael 1628 char *field_1 = NULL;
698     char *field_2 = NULL;
699     uint32_t i = 0;
700     uint32_t records = 0;
701     uint64_t field_3 = 0;
702     uint64_t field_4 = 0;
703 michael 1622
704 michael 1668 if (!(f = open_db(DLPATH, "r", KLINE_DB_VERSION)))
705 michael 1622 return;
706    
707 michael 1628 if (get_file_version(f) < 1)
708 michael 1622 {
709     close_db(f);
710     return;
711     }
712    
713 michael 1628 read_uint32(&records, f);
714 michael 1622
715 michael 1628 for (i = 0; i < records; ++i)
716 michael 1622 {
717 michael 1628 SAFE_READ(read_string(&field_1, f));
718     SAFE_READ(read_string(&field_2, f));
719     SAFE_READ(read_uint64(&field_3, f));
720     SAFE_READ(read_uint64(&field_4, f));
721 michael 1622
722 michael 1632 conf = conf_make(CONF_DLINE);
723     conf->host = field_1;
724     conf->reason = field_2;
725     conf->setat = field_3;
726 michael 1649 conf->until = field_4;
727 michael 1632 SetConfDatabase(conf);
728 michael 1622
729 michael 1632 add_conf_by_address(CONF_DLINE, conf);
730 michael 1622 }
731    
732     close_db(f);
733     }
734    
735     void
736     save_resv_database(void)
737     {
738 michael 1628 uint32_t records = 0;
739 michael 1622 struct dbFILE *f = NULL;
740     dlink_node *ptr = NULL;
741 michael 1632 struct MaskItem *conf = NULL;
742 michael 1622
743 michael 1668 if (!(f = open_db(RESVPATH, "w", KLINE_DB_VERSION)))
744 michael 1622 return;
745    
746 michael 2174 DLINK_FOREACH(ptr, cresv_items.head)
747 michael 1622 {
748 michael 1632 conf = ptr->data;
749 michael 1622
750 michael 1632 if (IsConfDatabase(conf))
751 michael 1628 ++records;
752 michael 1622 }
753    
754     DLINK_FOREACH(ptr, nresv_items.head)
755     {
756 michael 1632 conf = ptr->data;
757 michael 1622
758 michael 1632 if (IsConfDatabase(conf))
759 michael 1628 ++records;
760 michael 1622 }
761    
762 michael 1628 SAFE_WRITE(write_uint32(records, f), RESVPATH);
763 michael 1622
764 michael 2174 DLINK_FOREACH(ptr, cresv_items.head)
765 michael 1622 {
766 michael 1632 conf = ptr->data;
767 michael 1622
768 michael 1632 if (!IsConfDatabase(conf))
769 michael 1628 continue;
770    
771 michael 1632 SAFE_WRITE(write_string(conf->name, f), RESVPATH);
772     SAFE_WRITE(write_string(conf->reason, f), RESVPATH);
773     SAFE_WRITE(write_uint64(conf->setat, f), RESVPATH);
774 michael 1649 SAFE_WRITE(write_uint64(conf->until, f), RESVPATH);
775 michael 1622 }
776    
777     DLINK_FOREACH(ptr, nresv_items.head)
778     {
779     conf = ptr->data;
780    
781 michael 1632 if (!IsConfDatabase(conf))
782 michael 1628 continue;
783    
784 michael 1622 SAFE_WRITE(write_string(conf->name, f), RESVPATH);
785 michael 1632 SAFE_WRITE(write_string(conf->reason, f), RESVPATH);
786     SAFE_WRITE(write_uint64(conf->setat, f), RESVPATH);
787 michael 1649 SAFE_WRITE(write_uint64(conf->until, f), RESVPATH);
788 michael 1622 }
789    
790     close_db(f);
791     }
792    
793     void
794     load_resv_database(void)
795     {
796 michael 1628 uint32_t i = 0;
797     uint32_t records = 0;
798 michael 1622 uint64_t tmp64_hold = 0, tmp64_setat = 0;
799     struct dbFILE *f = NULL;
800     char *name = NULL;
801     char *reason = NULL;
802 michael 1632 struct MaskItem *conf = NULL;
803 michael 1622
804 michael 1668 if (!(f = open_db(RESVPATH, "r", KLINE_DB_VERSION)))
805 michael 1622 return;
806    
807 michael 1628 if (get_file_version(f) < 1)
808 michael 1622 {
809     close_db(f);
810     return;
811     }
812    
813 michael 1628 read_uint32(&records, f);
814 michael 1622
815 michael 1628 for (i = 0; i < records; ++i)
816 michael 1622 {
817     SAFE_READ(read_string(&name, f));
818     SAFE_READ(read_string(&reason, f));
819     SAFE_READ(read_uint64(&tmp64_setat, f));
820     SAFE_READ(read_uint64(&tmp64_hold, f));
821    
822 michael 1858 if ((conf = create_resv(name, reason, NULL)) == NULL)
823     continue;
824 michael 1622
825 michael 1858 conf->setat = tmp64_setat;
826     conf->until = tmp64_hold;
827     SetConfDatabase(conf);
828 michael 1622
829     MyFree(name);
830     MyFree(reason);
831     }
832    
833     close_db(f);
834     }
835    
836     void
837     save_xline_database(void)
838     {
839 michael 1628 uint32_t records = 0;
840 michael 1622 struct dbFILE *f = NULL;
841     dlink_node *ptr = NULL;
842 michael 1632 struct MaskItem *conf = NULL;
843 michael 1622
844 michael 1668 if (!(f = open_db(XPATH, "w", KLINE_DB_VERSION)))
845 michael 1622 return;
846    
847     DLINK_FOREACH(ptr, xconf_items.head)
848     {
849     conf = ptr->data;
850    
851 michael 1632 if (IsConfDatabase(conf))
852 michael 1628 ++records;
853 michael 1622 }
854    
855 michael 1628 SAFE_WRITE(write_uint32(records, f), XPATH);
856 michael 1622
857     DLINK_FOREACH(ptr, xconf_items.head)
858     {
859     conf = ptr->data;
860    
861 michael 1632 if (!IsConfDatabase(conf))
862 michael 1628 continue;
863    
864 michael 1622 SAFE_WRITE(write_string(conf->name, f), XPATH);
865 michael 1632 SAFE_WRITE(write_string(conf->reason, f), XPATH);
866     SAFE_WRITE(write_uint64(conf->setat, f), XPATH);
867 michael 1649 SAFE_WRITE(write_uint64(conf->until, f), XPATH);
868 michael 1622 }
869    
870     close_db(f);
871     }
872    
873     void
874     load_xline_database(void)
875     {
876 michael 1628 uint32_t i = 0;
877     uint32_t records = 0;
878 michael 1622 uint64_t tmp64_hold = 0, tmp64_setat = 0;
879     struct dbFILE *f = NULL;
880     char *name = NULL;
881     char *reason = NULL;
882 michael 1632 struct MaskItem *conf = NULL;
883 michael 1622
884 michael 1668 if (!(f = open_db(XPATH, "r", KLINE_DB_VERSION)))
885 michael 1622 return;
886    
887 michael 1628 if (get_file_version(f) < 1)
888 michael 1622 {
889     close_db(f);
890     return;
891     }
892    
893 michael 1628 read_uint32(&records, f);
894 michael 1622
895 michael 1628 for (i = 0; i < records; ++i)
896 michael 1622 {
897     SAFE_READ(read_string(&name, f));
898     SAFE_READ(read_string(&reason, f));
899     SAFE_READ(read_uint64(&tmp64_setat, f));
900     SAFE_READ(read_uint64(&tmp64_hold, f));
901    
902 michael 1632 conf = conf_make(CONF_XLINE);
903 michael 1622
904 michael 1632 SetConfDatabase(conf);
905 michael 1628
906 michael 1622 conf->name = name;
907 michael 1632 conf->reason = reason;
908     conf->setat = tmp64_setat;
909 michael 1649 conf->until = tmp64_hold;
910 michael 1622 }
911    
912     close_db(f);
913     }
914 michael 1625
915     void
916     save_all_databases(void *unused)
917     {
918     save_kline_database();
919     save_dline_database();
920     save_xline_database();
921     save_resv_database();
922     }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision