ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/conf_db.c
Revision: 7304
Committed: Wed Feb 10 10:45:46 2016 UTC (10 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 19317 byte(s)
Log Message:
- Cleanup/separate XLINE / gecos{} conf implementation

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 7006 * Copyright (c) 2012-2016 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 michael 7234 #include "conf.h"
31 michael 1620 #include "conf_db.h"
32 michael 7304 #include "conf_gecos.h"
33 michael 7234 #include "conf_resv.h"
34 michael 1620 #include "memory.h"
35     #include "log.h"
36     #include "send.h"
37     #include "irc_string.h"
38 michael 1622 #include "hostmask.h"
39 michael 1620
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 michael 5899 static uint32_t
49 michael 1620 get_file_version(struct dbFILE *f)
50     {
51 michael 1621 uint32_t version = 0;
52 michael 1620
53 michael 1621 if (read_uint32(&version, f) == -1)
54 michael 1620 {
55 michael 1621 ilog(LOG_TYPE_IRCD, "Error reading version number on %s: %s",
56     f->filename, strerror(errno));
57 michael 1620 return 0;
58     }
59 michael 1621
60     if (version < 1)
61 michael 1620 {
62 michael 1621 ilog(LOG_TYPE_IRCD, "Invalid version number (%u) on %s",
63 michael 1620 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 michael 3286 * \param version Database version
73 michael 1620 * \return 0 on error, 1 on success.
74     */
75 michael 5899 static int
76 michael 1620 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 michael 1668 open_db_read(const char *filename)
94 michael 1620 {
95 michael 7032 struct dbFILE *f = xcalloc(sizeof(*f));
96 michael 1620 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 michael 3067 ilog(LOG_TYPE_IRCD, "Cannot read database file %s", f->filename);
109 michael 1620
110 michael 7032 xfree(f);
111 michael 1620 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 michael 3286 * \param version Database version
122 michael 1620 * \return dbFile struct
123     */
124     static struct dbFILE *
125 michael 1668 open_db_write(const char *filename, uint32_t version)
126 michael 1620 {
127 michael 7032 struct dbFILE *f = xcalloc(sizeof(*f));
128 michael 1620 int fd = 0;
129    
130     strlcpy(f->filename, filename, sizeof(f->filename));
131    
132     filename = f->filename;
133     f->mode = 'w';
134    
135 michael 1668 snprintf(f->tempname, sizeof(f->tempname), "%s.new", filename);
136 michael 1620
137 michael 1668 if (f->tempname[0] == '\0' || !strcmp(f->tempname, filename))
138 michael 1620 {
139 michael 1668 ilog(LOG_TYPE_IRCD, "Opening database file %s for write: Filename too long",
140     filename);
141 michael 7032 xfree(f);
142 michael 1668 errno = ENAMETOOLONG;
143 michael 1620 return NULL;
144     }
145    
146 michael 1668 remove(f->tempname);
147 michael 1620
148     /* Use open() to avoid people sneaking a new file in under us */
149 michael 1668 fd = open(f->tempname, O_WRONLY | O_CREAT | O_EXCL, 0666);
150     if (fd >= 0)
151 michael 1626 f->fp = fdopen(fd, "wb");
152 michael 1620
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 michael 6318 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
160 michael 3067 "Cannot create temporary database file %s",
161 michael 1668 f->tempname);
162 michael 1620
163     errno = errno_save;
164 michael 3067 ilog(LOG_TYPE_IRCD, "Cannot create temporary database file %s",
165 michael 1668 f->tempname);
166 michael 1620
167     if (f->fp)
168     fclose(f->fp);
169    
170 michael 1668 remove(f->tempname);
171 michael 7032 xfree(f);
172 michael 1620
173 michael 1668 errno = errno_save;
174     return NULL;
175 michael 1620 }
176    
177     return f;
178     }
179    
180     /*! \brief Open a database file for reading (*mode == 'r') or writing (*mode == 'w').
181 michael 1668 * 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 michael 1620 *
185 michael 1668 * `version' is only used when opening a file for writing, and indicates the
186     * version number to write to the file.
187     *
188 michael 1620 * \param filename File to open as the database
189     * \param mode Mode for writting or reading
190 michael 3286 * \param version Database version
191 michael 1620 * \return dbFile struct
192     */
193 michael 5899 static struct dbFILE *
194 michael 1668 open_db(const char *filename, const char *mode, uint32_t version)
195 michael 1620 {
196     switch (*mode)
197     {
198     case 'r':
199 michael 1668 return open_db_read(filename);
200 michael 1620 break;
201     case 'w':
202 michael 1668 return open_db_write(filename, version);
203 michael 1620 break;
204     default:
205     errno = EINVAL;
206     return NULL;
207     }
208     }
209    
210 michael 1668 /*! \brief Restore the database file to its condition before open_db(). This is
211 michael 1620 * identical to close_db() for files open for reading; however, for files
212 michael 1668 * 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 michael 1620 *
215     * \param dbFile struct
216     */
217 michael 5899 static void
218 michael 1620 restore_db(struct dbFILE *f)
219     {
220     int errno_save = errno;
221    
222 michael 1668 if (f->fp)
223     fclose(f->fp);
224     if (f->mode == 'w' && f->tempname[0])
225     remove(f->tempname);
226 michael 1620
227 michael 7032 xfree(f);
228 michael 1620 errno = errno_save;
229     }
230    
231 michael 1668 /*! \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 michael 1620 *
235     * \param dbFile struct
236 michael 3286 * \return -1 on error, 0 on success.
237 michael 1620 */
238 michael 5899 static int
239 michael 1620 close_db(struct dbFILE *f)
240     {
241 michael 1668 int res;
242    
243     if (!f->fp)
244 michael 1620 {
245 michael 1668 errno = EINVAL;
246     return -1;
247     }
248 michael 1620
249 michael 1668 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 michael 6318 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, "Unable to move new "
262 michael 1668 "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 michael 1620 }
270    
271 michael 7032 xfree(f);
272 michael 1668 return 0;
273 michael 1620 }
274    
275     /*
276 michael 5899 * 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 michael 1620 * \0) first; a length of 0 indicates that the string pointer is NULL.
280 michael 1626 * Written strings are truncated silently at 4294967294 bytes, and are always
281 michael 1620 * null-terminated.
282     */
283    
284     /*! \brief Read a unsigned 8bit integer
285     *
286 michael 3286 * \param ret 16bit integer to read
287 michael 1620 * \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 michael 3286 * \param val 16bit integer to write
306 michael 1620 * \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 michael 3286 * \param val unsigned 32bit integer to write
344 michael 1620 * \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 michael 3286 * \param val unsigned 64bit integer to write
392 michael 1620 * \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 michael 1626 uint32_t len = 0;
428 michael 1620
429 michael 1626 if (read_uint32(&len, f) < 0)
430 michael 1620 return -1;
431    
432     if (len == 0)
433     {
434     *ret = NULL;
435     return 0;
436     }
437    
438 michael 7032 s = xcalloc(len);
439 michael 1620
440     if (len != fread(s, 1, len, f->fp))
441     {
442 michael 7032 xfree(s);
443 michael 1620 return -1;
444     }
445    
446     *ret = s;
447     return 0;
448     }
449    
450     /*! \brief Write String
451     *
452 michael 3286 * \param s string
453 michael 1620 * \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 michael 1626 return write_uint32(0, f);
463 michael 1620
464     len = strlen(s);
465    
466 michael 1626 if (len > 4294967294)
467     len = 4294967294;
468     if (write_uint32(len + 1, f) < 0)
469 michael 1620 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 michael 1622
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 michael 6928 save_kline_database(const char *filename)
494 michael 1622 {
495 michael 1628 uint32_t i = 0;
496     uint32_t records = 0;
497 michael 1622 struct dbFILE *f = NULL;
498     dlink_node *ptr = NULL;
499    
500 michael 6928 if (!(f = open_db(filename, "w", KLINE_DB_VERSION)))
501 michael 1622 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 michael 1632 if (arec->type == CONF_KLINE && IsConfDatabase(arec->conf))
510 michael 1628 ++records;
511 michael 1622 }
512     }
513    
514 michael 6928 SAFE_WRITE(write_uint32(records, f), filename);
515 michael 1622
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 michael 1632 if (arec->type == CONF_KLINE && IsConfDatabase(arec->conf))
523 michael 1622 {
524 michael 6928 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 michael 1622 }
530     }
531     }
532    
533     close_db(f);
534     }
535    
536     void
537 michael 6928 load_kline_database(const char *filename)
538 michael 1622 {
539     struct dbFILE *f = NULL;
540 michael 1632 struct MaskItem *conf = NULL;
541 michael 1628 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 michael 1622
549 michael 6928 if (!(f = open_db(filename, "r", KLINE_DB_VERSION)))
550 michael 1622 return;
551    
552 michael 1628 if (get_file_version(f) < 1)
553 michael 1622 {
554     close_db(f);
555     return;
556     }
557    
558 michael 1628 read_uint32(&records, f);
559 michael 1622
560 michael 1628 for (i = 0; i < records; ++i)
561 michael 1622 {
562 michael 1628 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 michael 1622
568 michael 1632 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 michael 1649 conf->until = field_5;
574 michael 1632 SetConfDatabase(conf);
575 michael 1622
576 michael 1632 add_conf_by_address(CONF_KLINE, conf);
577 michael 1622 }
578    
579     close_db(f);
580     }
581    
582     void
583 michael 6928 save_dline_database(const char *filename)
584 michael 1622 {
585 michael 1628 uint32_t i = 0;
586     uint32_t records = 0;
587 michael 1622 struct dbFILE *f = NULL;
588     dlink_node *ptr = NULL;
589    
590 michael 6928 if (!(f = open_db(filename, "w", KLINE_DB_VERSION)))
591 michael 1622 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 michael 1632 if (arec->type == CONF_DLINE && IsConfDatabase(arec->conf))
600 michael 1628 ++records;
601 michael 1622 }
602     }
603    
604 michael 6928 SAFE_WRITE(write_uint32(records, f), filename);
605 michael 1622
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 michael 1632 if (arec->type == CONF_DLINE && IsConfDatabase(arec->conf))
613 michael 1622 {
614 michael 6928 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 michael 1622 }
619     }
620     }
621    
622     close_db(f);
623     }
624    
625     void
626 michael 6928 load_dline_database(const char *filename)
627 michael 1622 {
628     struct dbFILE *f = NULL;
629 michael 1632 struct MaskItem *conf = NULL;
630 michael 1628 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 michael 1622
637 michael 6928 if (!(f = open_db(filename, "r", KLINE_DB_VERSION)))
638 michael 1622 return;
639    
640 michael 1628 if (get_file_version(f) < 1)
641 michael 1622 {
642     close_db(f);
643     return;
644     }
645    
646 michael 1628 read_uint32(&records, f);
647 michael 1622
648 michael 1628 for (i = 0; i < records; ++i)
649 michael 1622 {
650 michael 1628 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 michael 1622
655 michael 1632 conf = conf_make(CONF_DLINE);
656     conf->host = field_1;
657     conf->reason = field_2;
658     conf->setat = field_3;
659 michael 1649 conf->until = field_4;
660 michael 1632 SetConfDatabase(conf);
661 michael 1622
662 michael 1632 add_conf_by_address(CONF_DLINE, conf);
663 michael 1622 }
664    
665     close_db(f);
666     }
667    
668     void
669 michael 6928 save_resv_database(const char *filename)
670 michael 1622 {
671 michael 1628 uint32_t records = 0;
672 michael 1622 struct dbFILE *f = NULL;
673 michael 7282 dlink_node *node = NULL;
674     const struct ResvItem *resv = NULL;
675 michael 1622
676 michael 6928 if (!(f = open_db(filename, "w", KLINE_DB_VERSION)))
677 michael 1622 return;
678    
679 michael 7282 DLINK_FOREACH(node, resv_chan_get_list()->head)
680 michael 1622 {
681 michael 7282 resv = node->data;
682 michael 1622
683 michael 7282 if (resv->in_database)
684 michael 1628 ++records;
685 michael 1622 }
686    
687 michael 7282 DLINK_FOREACH(node, resv_nick_get_list()->head)
688 michael 1622 {
689 michael 7282 resv = node->data;
690 michael 1622
691 michael 7282 if (resv->in_database)
692 michael 1628 ++records;
693 michael 1622 }
694    
695 michael 6928 SAFE_WRITE(write_uint32(records, f), filename);
696 michael 1622
697 michael 7282 DLINK_FOREACH(node, resv_chan_get_list()->head)
698 michael 1622 {
699 michael 7282 resv = node->data;
700 michael 1622
701 michael 7282 if (!resv->in_database)
702 michael 1628 continue;
703    
704 michael 7282 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 michael 1622 }
709    
710 michael 7282 DLINK_FOREACH(node, resv_nick_get_list()->head)
711 michael 1622 {
712 michael 7282 resv = node->data;
713 michael 1622
714 michael 7282 if (!resv->in_database)
715 michael 1628 continue;
716    
717 michael 7282 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 michael 1622 }
722    
723     close_db(f);
724     }
725    
726     void
727 michael 6928 load_resv_database(const char *filename)
728 michael 1622 {
729 michael 1628 uint32_t i = 0;
730     uint32_t records = 0;
731 michael 1622 uint64_t tmp64_hold = 0, tmp64_setat = 0;
732     struct dbFILE *f = NULL;
733     char *name = NULL;
734     char *reason = NULL;
735 michael 7282 struct ResvItem *resv = NULL;
736 michael 1622
737 michael 6928 if (!(f = open_db(filename, "r", KLINE_DB_VERSION)))
738 michael 1622 return;
739    
740 michael 1628 if (get_file_version(f) < 1)
741 michael 1622 {
742     close_db(f);
743     return;
744     }
745    
746 michael 1628 read_uint32(&records, f);
747 michael 1622
748 michael 1628 for (i = 0; i < records; ++i)
749 michael 1622 {
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 michael 7282 if ((resv = resv_make(name, reason, NULL)) == NULL)
756 michael 1858 continue;
757 michael 1622
758 michael 7282 resv->setat = tmp64_setat;
759     resv->expire = tmp64_hold;
760     resv->in_database = 1;
761 michael 1622
762 michael 7032 xfree(name);
763     xfree(reason);
764 michael 1622 }
765    
766     close_db(f);
767     }
768    
769     void
770 michael 6928 save_xline_database(const char *filename)
771 michael 1622 {
772 michael 1628 uint32_t records = 0;
773 michael 1622 struct dbFILE *f = NULL;
774     dlink_node *ptr = NULL;
775 michael 7304 struct GecosItem *gecos = NULL;
776 michael 1622
777 michael 6928 if (!(f = open_db(filename, "w", KLINE_DB_VERSION)))
778 michael 1622 return;
779    
780 michael 7304 DLINK_FOREACH(ptr, gecos_get_list()->head)
781 michael 1622 {
782 michael 7304 gecos = ptr->data;
783 michael 1622
784 michael 7304 if (gecos->in_database)
785 michael 1628 ++records;
786 michael 1622 }
787    
788 michael 6928 SAFE_WRITE(write_uint32(records, f), filename);
789 michael 1622
790 michael 7304 DLINK_FOREACH(ptr, gecos_get_list()->head)
791 michael 1622 {
792 michael 7304 gecos = ptr->data;
793 michael 1622
794 michael 7304 if (!gecos->in_database)
795 michael 1628 continue;
796    
797 michael 7304 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 michael 1622 }
802    
803     close_db(f);
804     }
805    
806     void
807 michael 6928 load_xline_database(const char *filename)
808 michael 1622 {
809 michael 1628 uint32_t i = 0;
810     uint32_t records = 0;
811 michael 1622 uint64_t tmp64_hold = 0, tmp64_setat = 0;
812     struct dbFILE *f = NULL;
813     char *name = NULL;
814     char *reason = NULL;
815 michael 7304 struct GecosItem *gecos = NULL;
816 michael 1622
817 michael 6928 if (!(f = open_db(filename, "r", KLINE_DB_VERSION)))
818 michael 1622 return;
819    
820 michael 1628 if (get_file_version(f) < 1)
821 michael 1622 {
822     close_db(f);
823     return;
824     }
825    
826 michael 1628 read_uint32(&records, f);
827 michael 1622
828 michael 1628 for (i = 0; i < records; ++i)
829 michael 1622 {
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 michael 7304 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 michael 1622 }
842    
843     close_db(f);
844     }
845 michael 1625
846     void
847     save_all_databases(void *unused)
848     {
849 michael 6928 save_kline_database(ConfigGeneral.klinefile);
850     save_dline_database(ConfigGeneral.dlinefile);
851     save_xline_database(ConfigGeneral.xlinefile);
852     save_resv_database(ConfigGeneral.resvfile);
853 michael 1625 }

Properties

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