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