1 |
|
/* |
2 |
< |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
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 by the Hybrid Development Team. |
5 |
> |
* Copyright (c) 2012-2015 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 |
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 |
19 |
> |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
20 |
|
* USA |
21 |
|
*/ |
22 |
|
|
25 |
|
* \version $Id$ |
26 |
|
*/ |
27 |
|
|
28 |
+ |
|
29 |
|
#include "stdinc.h" |
30 |
|
#include "conf_db.h" |
31 |
|
#include "memory.h" |
68 |
|
|
69 |
|
/*! \brief Write the current version number to the file. |
70 |
|
* \param f dbFile Struct Member |
71 |
+ |
* \param version Database version |
72 |
|
* \return 0 on error, 1 on success. |
73 |
|
*/ |
74 |
|
int |
85 |
|
} |
86 |
|
|
87 |
|
/*! \brief Open the database for reading |
86 |
– |
* \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 *filename) |
93 |
|
{ |
94 |
< |
struct dbFILE *f = MyMalloc(sizeof(*f)); |
94 |
> |
struct dbFILE *f = MyCalloc(sizeof(*f)); |
95 |
|
FILE *fp = NULL; |
96 |
|
|
97 |
|
strlcpy(f->filename, filename, sizeof(f->filename)); |
104 |
|
int errno_save = errno; |
105 |
|
|
106 |
|
if (errno != ENOENT) |
107 |
< |
ilog(LOG_TYPE_IRCD, "Can't read database file %s", f->filename); |
107 |
> |
ilog(LOG_TYPE_IRCD, "Cannot read database file %s", f->filename); |
108 |
|
|
109 |
|
MyFree(f); |
110 |
|
errno = errno_save; |
117 |
|
|
118 |
|
/*! \brief Open the database for writting |
119 |
|
* \param filename File to open as the database |
120 |
< |
* \param version Database Version |
120 |
> |
* \param version Database version |
121 |
|
* \return dbFile struct |
122 |
|
*/ |
123 |
|
static struct dbFILE * |
124 |
|
open_db_write(const char *filename, uint32_t version) |
125 |
|
{ |
126 |
< |
struct dbFILE *f = MyMalloc(sizeof(*f)); |
126 |
> |
struct dbFILE *f = MyCalloc(sizeof(*f)); |
127 |
|
int fd = 0; |
128 |
|
|
129 |
|
strlcpy(f->filename, filename, sizeof(f->filename)); |
156 |
|
|
157 |
|
if (!walloped++) |
158 |
|
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
159 |
< |
"Can't create temporary database file %s", |
159 |
> |
"Cannot create temporary database file %s", |
160 |
|
f->tempname); |
161 |
|
|
162 |
|
errno = errno_save; |
163 |
< |
ilog(LOG_TYPE_IRCD, "Can't create temporary database file %s", |
163 |
> |
ilog(LOG_TYPE_IRCD, "Cannot create temporary database file %s", |
164 |
|
f->tempname); |
165 |
|
|
166 |
|
if (f->fp) |
186 |
|
* |
187 |
|
* \param filename File to open as the database |
188 |
|
* \param mode Mode for writting or reading |
189 |
< |
* \param version Database Version |
189 |
> |
* \param version Database version |
190 |
|
* \return dbFile struct |
191 |
|
*/ |
192 |
|
struct dbFILE * |
232 |
|
* fails. |
233 |
|
* |
234 |
|
* \param dbFile struct |
235 |
+ |
* \return -1 on error, 0 on success. |
236 |
|
*/ |
237 |
|
int |
238 |
|
close_db(struct dbFILE *f) |
302 |
|
|
303 |
|
/*! \brief Write a 8bit integer |
304 |
|
* |
305 |
< |
* \param ret 8bit integer to write |
305 |
> |
* \param val 8bit integer to write |
306 |
|
* \param dbFile struct |
307 |
|
* \return -1 on error, 0 otherwise. |
308 |
|
*/ |
317 |
|
|
318 |
|
/*! \brief Read a unsigned 8bit integer |
319 |
|
* |
320 |
< |
* \param ret 8bit integer to read |
320 |
> |
* \param ret 16bit integer to read |
321 |
|
* \param dbFile struct |
322 |
|
* \return -1 on error, 0 otherwise. |
323 |
|
*/ |
336 |
|
|
337 |
|
/*! \brief Write a unsigned 16bit integer |
338 |
|
* |
339 |
< |
* \param ret 16bit integer to write |
339 |
> |
* \param val 16bit integer to write |
340 |
|
* \param dbFile struct |
341 |
|
* \return -1 on error, 0 otherwise. |
342 |
|
*/ |
374 |
|
|
375 |
|
/*! \brief Write a unsigned 32bit integer |
376 |
|
* |
377 |
< |
* \param ret unsigned 32bit integer to write |
377 |
> |
* \param val unsigned 32bit integer to write |
378 |
|
* \param dbFile struct |
379 |
|
* \return -1 on error, 0 otherwise. |
380 |
|
*/ |
422 |
|
|
423 |
|
/*! \brief Write a unsigned 64bit integer |
424 |
|
* |
425 |
< |
* \param ret unsigned 64bit integer to write |
425 |
> |
* \param val unsigned 64bit integer to write |
426 |
|
* \param dbFile struct |
427 |
|
* \return -1 on error, 0 otherwise. |
428 |
|
*/ |
469 |
|
|
470 |
|
/*! \brief Write Pointer |
471 |
|
* |
472 |
< |
* \param ret pointer to write |
472 |
> |
* \param ptr pointer to write |
473 |
|
* \param dbFile struct |
474 |
|
* \return -1 on error, 0 otherwise. |
475 |
|
*/ |
502 |
|
return 0; |
503 |
|
} |
504 |
|
|
505 |
< |
s = MyMalloc(len); |
505 |
> |
s = MyCalloc(len); |
506 |
|
|
507 |
|
if (len != fread(s, 1, len, f->fp)) |
508 |
|
{ |
516 |
|
|
517 |
|
/*! \brief Write String |
518 |
|
* |
519 |
< |
* \param ret string |
519 |
> |
* \param s string |
520 |
|
* \param dbFile struct |
521 |
|
* \return -1 on error, 0 otherwise. |
522 |
|
*/ |
733 |
|
} |
734 |
|
|
735 |
|
void |
734 |
– |
save_gline_database(void) |
735 |
– |
{ |
736 |
– |
uint32_t i = 0; |
737 |
– |
uint32_t records = 0; |
738 |
– |
struct dbFILE *f = NULL; |
739 |
– |
dlink_node *ptr = NULL; |
740 |
– |
|
741 |
– |
if (!(f = open_db(GPATH, "w", KLINE_DB_VERSION))) |
742 |
– |
return; |
743 |
– |
|
744 |
– |
for (i = 0; i < ATABLE_SIZE; ++i) |
745 |
– |
{ |
746 |
– |
DLINK_FOREACH(ptr, atable[i].head) |
747 |
– |
{ |
748 |
– |
struct AddressRec *arec = ptr->data; |
749 |
– |
|
750 |
– |
if (arec->type == CONF_GLINE && IsConfDatabase(arec->conf)) |
751 |
– |
++records; |
752 |
– |
} |
753 |
– |
} |
754 |
– |
|
755 |
– |
SAFE_WRITE(write_uint32(records, f), GPATH); |
756 |
– |
|
757 |
– |
for (i = 0; i < ATABLE_SIZE; ++i) |
758 |
– |
{ |
759 |
– |
DLINK_FOREACH(ptr, atable[i].head) |
760 |
– |
{ |
761 |
– |
struct AddressRec *arec = ptr->data; |
762 |
– |
|
763 |
– |
if (arec->type == CONF_GLINE && IsConfDatabase(arec->conf)) |
764 |
– |
{ |
765 |
– |
SAFE_WRITE(write_string(arec->conf->user, f), GPATH); |
766 |
– |
SAFE_WRITE(write_string(arec->conf->host, f), GPATH); |
767 |
– |
SAFE_WRITE(write_string(arec->conf->reason, f), GPATH); |
768 |
– |
SAFE_WRITE(write_uint64(arec->conf->setat, f), GPATH); |
769 |
– |
SAFE_WRITE(write_uint64(arec->conf->until, f), GPATH); |
770 |
– |
} |
771 |
– |
} |
772 |
– |
} |
773 |
– |
|
774 |
– |
close_db(f); |
775 |
– |
} |
776 |
– |
|
777 |
– |
void |
778 |
– |
load_gline_database(void) |
779 |
– |
{ |
780 |
– |
struct dbFILE *f = NULL; |
781 |
– |
struct MaskItem *conf = NULL; |
782 |
– |
char *field_1 = NULL; |
783 |
– |
char *field_2 = NULL; |
784 |
– |
char *field_3 = NULL; |
785 |
– |
uint32_t i = 0; |
786 |
– |
uint32_t records = 0; |
787 |
– |
uint64_t field_4 = 0; |
788 |
– |
uint64_t field_5 = 0; |
789 |
– |
|
790 |
– |
if (!(f = open_db(GPATH, "r", KLINE_DB_VERSION))) |
791 |
– |
return; |
792 |
– |
|
793 |
– |
if (get_file_version(f) < 1) |
794 |
– |
{ |
795 |
– |
close_db(f); |
796 |
– |
return; |
797 |
– |
} |
798 |
– |
|
799 |
– |
read_uint32(&records, f); |
800 |
– |
|
801 |
– |
for (i = 0; i < records; ++i) |
802 |
– |
{ |
803 |
– |
SAFE_READ(read_string(&field_1, f)); |
804 |
– |
SAFE_READ(read_string(&field_2, f)); |
805 |
– |
SAFE_READ(read_string(&field_3, f)); |
806 |
– |
SAFE_READ(read_uint64(&field_4, f)); |
807 |
– |
SAFE_READ(read_uint64(&field_5, f)); |
808 |
– |
|
809 |
– |
conf = conf_make(CONF_GLINE); |
810 |
– |
conf->user = field_1; |
811 |
– |
conf->host = field_2; |
812 |
– |
conf->reason = field_3; |
813 |
– |
conf->setat = field_4; |
814 |
– |
conf->until = field_5; |
815 |
– |
SetConfDatabase(conf); |
816 |
– |
|
817 |
– |
add_conf_by_address(CONF_GLINE, conf); |
818 |
– |
} |
819 |
– |
|
820 |
– |
close_db(f); |
821 |
– |
} |
822 |
– |
|
823 |
– |
void |
736 |
|
save_resv_database(void) |
737 |
|
{ |
738 |
|
uint32_t records = 0; |
743 |
|
if (!(f = open_db(RESVPATH, "w", KLINE_DB_VERSION))) |
744 |
|
return; |
745 |
|
|
746 |
< |
DLINK_FOREACH(ptr, resv_channel_list.head) |
746 |
> |
DLINK_FOREACH(ptr, cresv_items.head) |
747 |
|
{ |
748 |
|
conf = ptr->data; |
749 |
|
|
761 |
|
|
762 |
|
SAFE_WRITE(write_uint32(records, f), RESVPATH); |
763 |
|
|
764 |
< |
DLINK_FOREACH(ptr, resv_channel_list.head) |
764 |
> |
DLINK_FOREACH(ptr, cresv_items.head) |
765 |
|
{ |
766 |
|
conf = ptr->data; |
767 |
|
|
819 |
|
SAFE_READ(read_uint64(&tmp64_setat, f)); |
820 |
|
SAFE_READ(read_uint64(&tmp64_hold, f)); |
821 |
|
|
822 |
< |
if (IsChanPrefix(*name)) |
823 |
< |
{ |
912 |
< |
if ((conf = create_channel_resv(name, reason, 0)) == NULL) |
913 |
< |
continue; |
914 |
< |
|
915 |
< |
conf->setat = tmp64_setat; |
916 |
< |
conf->until = tmp64_hold; |
917 |
< |
SetConfDatabase(conf); |
918 |
< |
} |
919 |
< |
else |
920 |
< |
{ |
921 |
< |
if ((conf = create_nick_resv(name, reason, 0)) == NULL) |
922 |
< |
continue; |
822 |
> |
if ((conf = create_resv(name, reason, NULL)) == NULL) |
823 |
> |
continue; |
824 |
|
|
825 |
< |
conf->setat = tmp64_setat; |
826 |
< |
conf->until = tmp64_hold; |
827 |
< |
SetConfDatabase(conf); |
927 |
< |
} |
825 |
> |
conf->setat = tmp64_setat; |
826 |
> |
conf->until = tmp64_hold; |
827 |
> |
SetConfDatabase(conf); |
828 |
|
|
829 |
|
MyFree(name); |
830 |
|
MyFree(reason); |
917 |
|
{ |
918 |
|
save_kline_database(); |
919 |
|
save_dline_database(); |
1020 |
– |
save_gline_database(); |
920 |
|
save_xline_database(); |
921 |
|
save_resv_database(); |
922 |
|
} |