]>
Commit | Line | Data |
---|---|---|
832ed81a AC |
1 | /** |
2 | * ircd-ratbox: A slightly useful ircd. | |
3 | * bantool.c: The ircd-ratbox database managment tool. | |
4 | * | |
5 | * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center | |
6 | * Copyright (C) 1996-2002 Hybrid Development Team | |
7 | * Copyright (C) 2002-2008 ircd-ratbox development team | |
8 | * Copyright (C) 2008 Daniel J Reidy <dubkat@gmail.com> | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
23 | * USA | |
24 | * | |
25 | * $Id: bantool.c 26164 2008-10-26 19:52:43Z androsyn $ | |
26 | * | |
27 | * | |
28 | * The following server admins have either contributed various configs to test against, | |
29 | * or helped with debugging and feature requests. Many thanks to them. | |
30 | * stevoo / efnet.port80.se | |
31 | * AndroSyn / irc2.choopa.net, irc.igs.ca | |
32 | * Salvation / irc.blessed.net | |
33 | * JamesOff / efnet.demon.co.uk | |
34 | * | |
35 | * Thanks to AndroSyn for challenging me to learn C on the fly :) | |
36 | * BUGS Direct Question, Bug Reports, and Feature Requests to #ratbox on EFnet. | |
37 | * BUGS Complaints >/dev/null | |
38 | * | |
39 | */ | |
40 | ||
832ed81a AC |
41 | #include <stdio.h> |
42 | #include <stdlib.h> | |
43 | #include <time.h> | |
44 | ||
45 | #include "stdinc.h" | |
a18e9931 | 46 | #include "common.h" |
832ed81a AC |
47 | #include "rsdb.h" |
48 | ||
49 | #define EmptyString(x) ((x == NULL) || (*(x) == '\0')) | |
50 | #define CheckEmpty(x) EmptyString(x) ? "" : x | |
51 | ||
52 | #define BT_VERSION "0.4.1" | |
53 | ||
54 | typedef enum | |
55 | { | |
56 | BANDB_KLINE, | |
57 | BANDB_KLINE_PERM, | |
58 | BANDB_DLINE, | |
59 | BANDB_DLINE_PERM, | |
60 | BANDB_XLINE, | |
61 | BANDB_XLINE_PERM, | |
62 | BANDB_RESV, | |
63 | BANDB_RESV_PERM, | |
64 | LAST_BANDB_TYPE | |
65 | } bandb_type; | |
66 | ||
67 | ||
68 | static char bandb_letter[LAST_BANDB_TYPE] = { | |
69 | 'K', 'K', 'D', 'D', 'X', 'X', 'R', 'R' | |
70 | }; | |
71 | ||
72 | static const char *bandb_table[LAST_BANDB_TYPE] = { | |
73 | "kline", "kline", "dline", "dline", "xline", "xline", "resv", "resv" | |
74 | }; | |
75 | ||
76 | static const char *bandb_suffix[LAST_BANDB_TYPE] = { | |
77 | "", ".perm", | |
78 | "", ".perm", | |
79 | "", ".perm", | |
80 | "", ".perm" | |
81 | }; | |
82 | ||
83 | static char me[PATH_MAX]; | |
84 | ||
85 | /* *INDENT-OFF* */ | |
86 | /* report counters */ | |
87 | struct counter | |
88 | { | |
89 | unsigned int klines; | |
90 | unsigned int dlines; | |
91 | unsigned int xlines; | |
92 | unsigned int resvs; | |
93 | unsigned int error; | |
94 | } count = {0, 0, 0, 0, 0}; | |
95 | ||
96 | /* flags set by command line options */ | |
97 | struct flags | |
98 | { | |
99 | int none; | |
100 | int export; | |
101 | int import; | |
102 | int verify; | |
103 | int vacuum; | |
104 | int pretend; | |
105 | int verbose; | |
106 | int wipe; | |
107 | int dupes_ok; | |
108 | } flag = {YES, NO, NO, NO, NO, NO, NO, NO, NO}; | |
109 | /* *INDENT-ON* */ | |
110 | ||
111 | static int table_has_rows(const char *table); | |
112 | static int table_exists(const char *table); | |
113 | ||
114 | static const char *clean_gecos_field(const char *gecos); | |
115 | static char *bt_smalldate(const char *string); | |
116 | static char *getfield(char *newline); | |
117 | static char *strip_quotes(const char *string); | |
118 | static char *mangle_reason(const char *string); | |
119 | static char *escape_quotes(const char *string); | |
120 | ||
121 | static void db_error_cb(const char *errstr); | |
122 | static void db_reclaim_slack(void); | |
123 | static void export_config(const char *conf, int id); | |
124 | static void import_config(const char *conf, int id); | |
125 | static void check_schema(void); | |
126 | static void print_help(int i_exit); | |
127 | static void wipe_schema(void); | |
128 | static void drop_dupes(const char *user, const char *host, const char *t); | |
129 | ||
130 | /** | |
55abcbb2 | 131 | * swing your pants |
832ed81a AC |
132 | */ |
133 | int | |
134 | main(int argc, char *argv[]) | |
135 | { | |
136 | char etc[PATH_MAX]; | |
137 | char conf[PATH_MAX]; | |
138 | int opt; | |
139 | int i; | |
140 | ||
141 | rb_strlcpy(me, argv[0], sizeof(me)); | |
142 | ||
143 | while((opt = getopt(argc, argv, "hieuspvwd")) != -1) | |
144 | { | |
145 | switch (opt) | |
146 | { | |
147 | case 'h': | |
148 | print_help(EXIT_SUCCESS); | |
149 | break; | |
150 | case 'i': | |
151 | flag.none = NO; | |
152 | flag.import = YES; | |
153 | break; | |
154 | case 'e': | |
155 | flag.none = NO; | |
156 | flag.export = YES; | |
157 | break; | |
158 | case 'u': | |
159 | flag.none = NO; | |
160 | flag.verify = YES; | |
161 | break; | |
162 | case 's': | |
163 | flag.none = NO; | |
164 | flag.vacuum = YES; | |
165 | break; | |
166 | case 'p': | |
167 | flag.pretend = YES; | |
168 | break; | |
169 | case 'v': | |
170 | flag.verbose = YES; | |
171 | break; | |
172 | case 'w': | |
173 | flag.wipe = YES; | |
174 | break; | |
175 | case 'd': | |
176 | flag.dupes_ok = YES; | |
177 | break; | |
178 | default: /* '?' */ | |
179 | print_help(EXIT_FAILURE); | |
180 | } | |
181 | } | |
182 | ||
183 | /* they should really read the help. */ | |
184 | if(flag.none) | |
185 | print_help(EXIT_FAILURE); | |
186 | ||
187 | if((flag.import && flag.export) || (flag.export && flag.wipe) | |
188 | || (flag.verify && flag.pretend) || (flag.export && flag.pretend)) | |
189 | { | |
190 | fprintf(stderr, "* Error: Conflicting flags.\n"); | |
191 | if(flag.export && flag.pretend) | |
192 | fprintf(stderr, "* There is nothing to 'pretend' when exporting.\n"); | |
193 | ||
194 | fprintf(stderr, "* For an explination of commands, run: %s -h\n", me); | |
195 | exit(EXIT_FAILURE); | |
196 | } | |
197 | ||
198 | if(argv[optind] != NULL) | |
199 | rb_strlcpy(etc, argv[optind], sizeof(etc)); | |
200 | else | |
201 | rb_strlcpy(etc, ETCPATH, sizeof(ETCPATH)); | |
202 | ||
203 | fprintf(stdout, | |
204 | "* ircd-ratbox bantool v.%s ($Id: bantool.c 26164 2008-10-26 19:52:43Z androsyn $)\n", | |
205 | BT_VERSION); | |
206 | ||
207 | if(flag.pretend == NO) | |
208 | { | |
209 | if(rsdb_init(db_error_cb) == -1) | |
210 | { | |
211 | fprintf(stderr, "* Error: Unable to open database\n"); | |
212 | exit(EXIT_FAILURE); | |
213 | } | |
214 | check_schema(); | |
215 | ||
216 | if(flag.vacuum) | |
217 | db_reclaim_slack(); | |
218 | ||
219 | if(flag.import && flag.wipe) | |
220 | { | |
221 | flag.dupes_ok = YES; /* dont check for dupes if we are wiping the db clean */ | |
222 | for(i = 0; i < 3; i++) | |
223 | fprintf(stdout, | |
224 | "* WARNING: YOU ARE ABOUT TO WIPE YOUR DATABASE!\n"); | |
225 | ||
226 | fprintf(stdout, "* Press ^C to abort! "); | |
227 | fflush(stdout); | |
228 | rb_sleep(10, 0); | |
229 | fprintf(stdout, "Carrying on...\n"); | |
230 | wipe_schema(); | |
231 | } | |
232 | } | |
233 | if(flag.verbose && flag.dupes_ok == YES) | |
234 | fprintf(stdout, "* Allowing duplicate bans...\n"); | |
235 | ||
236 | /* checking for our files to import or export */ | |
237 | for(i = 0; i < LAST_BANDB_TYPE; i++) | |
238 | { | |
239 | rb_snprintf(conf, sizeof(conf), "%s/%s.conf%s", | |
240 | etc, bandb_table[i], bandb_suffix[i]); | |
241 | ||
242 | if(flag.import && flag.pretend == NO) | |
243 | rsdb_transaction(RSDB_TRANS_START); | |
244 | ||
245 | if(flag.import) | |
246 | import_config(conf, i); | |
247 | ||
248 | if(flag.export) | |
249 | export_config(conf, i); | |
250 | ||
251 | if(flag.import && flag.pretend == NO) | |
252 | rsdb_transaction(RSDB_TRANS_END); | |
253 | } | |
254 | ||
255 | if(flag.import) | |
256 | { | |
257 | if(count.error && flag.verbose) | |
29c92cf9 | 258 | fprintf(stderr, "* I was unable to locate %u config files to import.\n", |
832ed81a AC |
259 | count.error); |
260 | ||
29c92cf9 | 261 | fprintf(stdout, "* Import Stats: Klines: %u, Dlines: %u, Xlines: %u, Resvs: %u \n", |
832ed81a AC |
262 | count.klines, count.dlines, count.xlines, count.resvs); |
263 | ||
264 | fprintf(stdout, | |
265 | "*\n* If your IRC server is currently running, newly imported bans \n* will not take effect until you issue the command: /quote rehash bans\n"); | |
266 | ||
267 | if(flag.pretend) | |
268 | fprintf(stdout, | |
269 | "* Pretend mode engaged. Nothing was actually entered into the database.\n"); | |
270 | } | |
271 | ||
272 | return 0; | |
273 | } | |
274 | ||
275 | ||
276 | /** | |
277 | * export the database to old-style flat files | |
278 | */ | |
279 | static void | |
280 | export_config(const char *conf, int id) | |
281 | { | |
282 | struct rsdb_table table; | |
283 | static char sql[BUFSIZE * 2]; | |
284 | static char buf[512]; | |
285 | FILE *fd = NULL; | |
286 | int j; | |
287 | ||
288 | /* for sanity sake */ | |
289 | const int mask1 = 0; | |
290 | const int mask2 = 1; | |
291 | const int reason = 2; | |
292 | const int oper = 3; | |
293 | const int ts = 4; | |
294 | /* const int perm = 5; */ | |
295 | ||
296 | if(!table_has_rows(bandb_table[id])) | |
297 | return; | |
298 | ||
299 | if(strstr(conf, ".perm") != 0) | |
300 | rb_snprintf(sql, sizeof(sql), | |
301 | "SELECT DISTINCT mask1,mask2,reason,oper,time FROM %s WHERE perm = 1 ORDER BY time", | |
302 | bandb_table[id]); | |
303 | else | |
304 | rb_snprintf(sql, sizeof(sql), | |
305 | "SELECT DISTINCT mask1,mask2,reason,oper,time FROM %s WHERE perm = 0 ORDER BY time", | |
306 | bandb_table[id]); | |
307 | ||
308 | rsdb_exec_fetch(&table, sql); | |
309 | if(table.row_count <= 0) | |
310 | { | |
311 | rsdb_exec_fetch_end(&table); | |
312 | return; | |
313 | } | |
314 | ||
315 | if(flag.verbose) | |
316 | fprintf(stdout, "* checking for %s: ", conf); /* debug */ | |
317 | ||
318 | /* open config for reading, or skip to the next */ | |
319 | if(!(fd = fopen(conf, "w"))) | |
320 | { | |
321 | if(flag.verbose) | |
322 | fprintf(stdout, "\tmissing.\n"); | |
323 | count.error++; | |
324 | return; | |
325 | } | |
326 | ||
327 | for(j = 0; j < table.row_count; j++) | |
328 | { | |
329 | switch (id) | |
330 | { | |
331 | case BANDB_DLINE: | |
332 | case BANDB_DLINE_PERM: | |
333 | rb_snprintf(buf, sizeof(buf), | |
334 | "\"%s\",\"%s\",\"\",\"%s\",\"%s\",%s\n", | |
335 | table.row[j][mask1], | |
336 | mangle_reason(table.row[j][reason]), | |
337 | bt_smalldate(table.row[j][ts]), | |
338 | table.row[j][oper], table.row[j][ts]); | |
339 | break; | |
340 | ||
341 | case BANDB_XLINE: | |
342 | case BANDB_XLINE_PERM: | |
343 | rb_snprintf(buf, sizeof(buf), | |
344 | "\"%s\",\"0\",\"%s\",\"%s\",%s\n", | |
345 | escape_quotes(table.row[j][mask1]), | |
346 | mangle_reason(table.row[j][reason]), | |
347 | table.row[j][oper], table.row[j][ts]); | |
348 | break; | |
349 | ||
350 | case BANDB_RESV: | |
351 | case BANDB_RESV_PERM: | |
352 | rb_snprintf(buf, sizeof(buf), | |
353 | "\"%s\",\"%s\",\"%s\",%s\n", | |
354 | table.row[j][mask1], | |
355 | mangle_reason(table.row[j][reason]), | |
356 | table.row[j][oper], table.row[j][ts]); | |
357 | break; | |
358 | ||
359 | ||
360 | default: /* Klines */ | |
361 | rb_snprintf(buf, sizeof(buf), | |
362 | "\"%s\",\"%s\",\"%s\",\"\",\"%s\",\"%s\",%s\n", | |
363 | table.row[j][mask1], table.row[j][mask2], | |
364 | mangle_reason(table.row[j][reason]), | |
365 | bt_smalldate(table.row[j][ts]), table.row[j][oper], | |
366 | table.row[j][ts]); | |
367 | break; | |
368 | } | |
369 | ||
370 | fprintf(fd, "%s", buf); | |
371 | } | |
372 | ||
373 | rsdb_exec_fetch_end(&table); | |
374 | if(flag.verbose) | |
375 | fprintf(stdout, "\twritten.\n"); | |
376 | fclose(fd); | |
377 | } | |
378 | ||
379 | /** | |
380 | * attempt to condense the individual conf functions into one | |
381 | */ | |
382 | static void | |
383 | import_config(const char *conf, int id) | |
384 | { | |
385 | FILE *fd; | |
386 | ||
387 | char line[BUFSIZE]; | |
388 | char *p; | |
389 | int i = 0; | |
390 | ||
391 | char f_perm = 0; | |
f67e7283 JT |
392 | const char *f_mask1 = NULL; |
393 | const char *f_mask2 = NULL; | |
394 | const char *f_oper = NULL; | |
395 | const char *f_time = NULL; | |
396 | const char *f_reason = NULL; | |
397 | const char *f_oreason = NULL; | |
832ed81a AC |
398 | char newreason[REASONLEN]; |
399 | ||
400 | if(flag.verbose) | |
401 | fprintf(stdout, "* checking for %s: ", conf); /* debug */ | |
402 | ||
403 | /* open config for reading, or skip to the next */ | |
404 | if(!(fd = fopen(conf, "r"))) | |
405 | { | |
406 | if(flag.verbose) | |
407 | fprintf(stdout, "%*s", strlen(bandb_suffix[id]) > 0 ? 10 : 15, | |
408 | "missing.\n"); | |
409 | count.error++; | |
410 | return; | |
411 | } | |
412 | ||
413 | if(strstr(conf, ".perm") != 0) | |
414 | f_perm = 1; | |
415 | ||
416 | ||
417 | /* xline | |
418 | * "SYSTEM","0","banned","stevoo!stevoo@efnet.port80.se{stevoo}",1111080437 | |
419 | * resv | |
420 | * "OseK","banned nickname","stevoo!stevoo@efnet.port80.se{stevoo}",1111031619 | |
421 | * dline | |
422 | * "194.158.192.0/19","laptop scammers","","2005/3/17 05.33","stevoo!stevoo@efnet.port80.se{stevoo}",1111033988 | |
423 | */ | |
424 | while(fgets(line, sizeof(line), fd)) | |
425 | { | |
426 | if((p = strpbrk(line, "\r\n")) != NULL) | |
427 | *p = '\0'; | |
428 | ||
429 | if((*line == '\0') || (*line == '#')) | |
430 | continue; | |
431 | ||
432 | /* mask1 */ | |
433 | f_mask1 = getfield(line); | |
434 | ||
435 | if(EmptyString(f_mask1)) | |
436 | continue; | |
437 | ||
438 | /* mask2 */ | |
439 | switch (id) | |
440 | { | |
441 | case BANDB_XLINE: | |
442 | case BANDB_XLINE_PERM: | |
443 | f_mask1 = escape_quotes(clean_gecos_field(f_mask1)); | |
444 | getfield(NULL); /* empty field */ | |
445 | break; | |
446 | ||
447 | case BANDB_RESV: | |
448 | case BANDB_RESV_PERM: | |
449 | case BANDB_DLINE: | |
450 | case BANDB_DLINE_PERM: | |
451 | break; | |
452 | ||
453 | default: | |
454 | f_mask2 = getfield(NULL); | |
455 | if(EmptyString(f_mask2)) | |
456 | continue; | |
457 | break; | |
458 | } | |
459 | ||
460 | /* reason */ | |
461 | f_reason = getfield(NULL); | |
462 | if(EmptyString(f_reason)) | |
463 | continue; | |
464 | ||
465 | /* oper comment */ | |
466 | switch (id) | |
467 | { | |
468 | case BANDB_KLINE: | |
469 | case BANDB_KLINE_PERM: | |
470 | case BANDB_DLINE: | |
471 | case BANDB_DLINE_PERM: | |
472 | f_oreason = getfield(NULL); | |
473 | getfield(NULL); | |
474 | break; | |
475 | ||
476 | default: | |
477 | break; | |
478 | } | |
479 | ||
480 | f_oper = getfield(NULL); | |
481 | f_time = strip_quotes(f_oper + strlen(f_oper) + 2); | |
f67e7283 JT |
482 | if(EmptyString(f_oper)) |
483 | f_oper = "unknown"; | |
832ed81a AC |
484 | |
485 | /* meh */ | |
486 | if(id == BANDB_KLINE || id == BANDB_KLINE_PERM) | |
487 | { | |
488 | if(strstr(f_mask1, "!") != NULL) | |
489 | { | |
490 | fprintf(stderr, | |
491 | "* SKIPPING INVALID KLINE %s@%s set by %s\n", | |
492 | f_mask1, f_mask2, f_oper); | |
493 | fprintf(stderr, " You may wish to re-apply it correctly.\n"); | |
494 | continue; | |
495 | } | |
496 | } | |
497 | ||
498 | /* append operreason_field to reason_field */ | |
499 | if(!EmptyString(f_oreason)) | |
500 | rb_snprintf(newreason, sizeof(newreason), "%s | %s", f_reason, f_oreason); | |
501 | else | |
502 | rb_snprintf(newreason, sizeof(newreason), "%s", f_reason); | |
503 | ||
504 | if(flag.pretend == NO) | |
505 | { | |
506 | if(flag.dupes_ok == NO) | |
507 | drop_dupes(f_mask1, f_mask2, bandb_table[id]); | |
508 | ||
509 | rsdb_exec(NULL, | |
510 | "INSERT INTO %s (mask1, mask2, oper, time, perm, reason) VALUES('%Q','%Q','%Q','%Q','%d','%Q')", | |
511 | bandb_table[id], f_mask1, f_mask2, f_oper, f_time, f_perm, | |
512 | newreason); | |
513 | } | |
514 | ||
515 | if(flag.pretend && flag.verbose) | |
516 | fprintf(stdout, | |
517 | "%s: perm(%d) mask1(%s) mask2(%s) oper(%s) reason(%s) time(%s)\n", | |
518 | bandb_table[id], f_perm, f_mask1, f_mask2, f_oper, newreason, | |
519 | f_time); | |
520 | ||
521 | i++; | |
522 | } | |
523 | ||
524 | switch (bandb_letter[id]) | |
525 | { | |
526 | case 'K': | |
527 | count.klines += i; | |
528 | break; | |
529 | case 'D': | |
530 | count.dlines += i; | |
531 | break; | |
532 | case 'X': | |
533 | count.xlines += i; | |
534 | break; | |
535 | case 'R': | |
536 | count.resvs += i; | |
537 | break; | |
538 | default: | |
539 | break; | |
540 | } | |
541 | ||
542 | if(flag.verbose) | |
543 | fprintf(stdout, "%*s\n", strlen(bandb_suffix[id]) > 0 ? 10 : 15, "imported."); | |
544 | ||
e3a3eb92 JT |
545 | fclose(fd); |
546 | ||
832ed81a AC |
547 | return; |
548 | } | |
549 | ||
550 | /** | |
551 | * getfield | |
552 | * | |
553 | * inputs - input buffer | |
554 | * output - next field | |
555 | * side effects - field breakup for ircd.conf file. | |
556 | */ | |
557 | char * | |
558 | getfield(char *newline) | |
559 | { | |
560 | static char *line = NULL; | |
561 | char *end, *field; | |
562 | ||
563 | if(newline != NULL) | |
564 | line = newline; | |
565 | ||
566 | if(line == NULL) | |
567 | return (NULL); | |
568 | ||
569 | field = line; | |
570 | ||
571 | /* XXX make this skip to first " if present */ | |
572 | if(*field == '"') | |
573 | field++; | |
574 | else | |
575 | return (NULL); /* mal-formed field */ | |
576 | ||
577 | end = strchr(line, ','); | |
578 | ||
579 | while(1) | |
580 | { | |
581 | /* no trailing , - last field */ | |
582 | if(end == NULL) | |
583 | { | |
584 | end = line + strlen(line); | |
585 | line = NULL; | |
586 | ||
587 | if(*end == '"') | |
588 | { | |
589 | *end = '\0'; | |
590 | return field; | |
591 | } | |
592 | else | |
593 | return NULL; | |
594 | } | |
595 | else | |
596 | { | |
597 | /* look for a ", to mark the end of a field.. */ | |
598 | if(*(end - 1) == '"') | |
599 | { | |
600 | line = end + 1; | |
601 | end--; | |
602 | *end = '\0'; | |
603 | return field; | |
604 | } | |
605 | ||
606 | /* search for the next ',' */ | |
607 | end++; | |
608 | end = strchr(end, ','); | |
609 | } | |
610 | } | |
611 | ||
612 | return NULL; | |
613 | } | |
614 | ||
615 | /** | |
616 | * strip away "quotes" from around strings | |
617 | */ | |
618 | static char * | |
619 | strip_quotes(const char *string) | |
620 | { | |
621 | static char buf[14]; /* int(11) + 2 + \0 */ | |
622 | char *str = buf; | |
623 | ||
624 | if(string == NULL) | |
625 | return NULL; | |
626 | ||
627 | while(*string) | |
628 | { | |
629 | if(*string != '"') | |
630 | { | |
631 | *str++ = *string; | |
632 | } | |
633 | string++; | |
634 | } | |
635 | *str = '\0'; | |
636 | return buf; | |
637 | } | |
638 | ||
639 | /** | |
640 | * escape quotes in a string | |
641 | */ | |
642 | static char * | |
643 | escape_quotes(const char *string) | |
644 | { | |
645 | static char buf[BUFSIZE * 2]; | |
646 | char *str = buf; | |
647 | ||
648 | if(string == NULL) | |
649 | return NULL; | |
650 | ||
651 | while(*string) | |
652 | { | |
653 | if(*string == '"') | |
654 | { | |
655 | *str++ = '\\'; | |
656 | *str++ = '"'; | |
657 | } | |
658 | else | |
659 | { | |
660 | *str++ = *string; | |
661 | } | |
662 | string++; | |
663 | } | |
664 | *str = '\0'; | |
665 | return buf; | |
666 | } | |
667 | ||
668 | ||
669 | static char * | |
670 | mangle_reason(const char *string) | |
671 | { | |
672 | static char buf[BUFSIZE * 2]; | |
673 | char *str = buf; | |
674 | ||
675 | if(string == NULL) | |
676 | return NULL; | |
677 | ||
678 | while(*string) | |
679 | { | |
680 | switch (*string) | |
681 | { | |
682 | case '"': | |
683 | *str = '\''; | |
684 | break; | |
685 | case ':': | |
686 | *str = ' '; | |
687 | break; | |
688 | default: | |
689 | *str = *string; | |
690 | } | |
691 | string++; | |
692 | str++; | |
693 | ||
694 | } | |
695 | *str = '\0'; | |
696 | return buf; | |
697 | } | |
698 | ||
699 | ||
700 | /** | |
701 | * change spaces to \s in gecos field | |
702 | */ | |
703 | static const char * | |
704 | clean_gecos_field(const char *gecos) | |
705 | { | |
706 | static char buf[BUFSIZE * 2]; | |
707 | char *str = buf; | |
708 | ||
709 | if(gecos == NULL) | |
710 | return NULL; | |
711 | ||
712 | while(*gecos) | |
713 | { | |
714 | if(*gecos == ' ') | |
715 | { | |
716 | *str++ = '\\'; | |
717 | *str++ = 's'; | |
718 | } | |
719 | else | |
720 | *str++ = *gecos; | |
721 | gecos++; | |
722 | } | |
723 | *str = '\0'; | |
724 | return buf; | |
725 | } | |
726 | ||
727 | /** | |
728 | * verify the database integrity, and if necessary create apropriate tables | |
729 | */ | |
730 | static void | |
731 | check_schema(void) | |
732 | { | |
733 | int i, j; | |
734 | char type[8]; /* longest string is 'INTEGER\0' */ | |
735 | ||
736 | if(flag.verify || flag.verbose) | |
737 | fprintf(stdout, "* Verifying database.\n"); | |
738 | ||
739 | const char *columns[] = { | |
740 | "perm", | |
741 | "mask1", | |
742 | "mask2", | |
743 | "oper", | |
744 | "time", | |
745 | "reason", | |
746 | NULL | |
747 | }; | |
748 | ||
749 | for(i = 0; i < LAST_BANDB_TYPE; i++) | |
750 | { | |
751 | if(!table_exists(bandb_table[i])) | |
752 | { | |
753 | rsdb_exec(NULL, | |
754 | "CREATE TABLE %s (mask1 TEXT, mask2 TEXT, oper TEXT, time INTEGER, perm INTEGER, reason TEXT)", | |
755 | bandb_table[i]); | |
756 | } | |
757 | ||
758 | /* | |
759 | * i can't think of any better way to do this, other then attempt to | |
760 | * force the creation of column that may, or may not already exist. --dubkat | |
761 | */ | |
762 | else | |
763 | { | |
764 | for(j = 0; columns[j] != NULL; j++) | |
765 | { | |
766 | if(!strcmp(columns[j], "time") && !strcmp(columns[j], "perm")) | |
767 | rb_strlcpy(type, "INTEGER", sizeof(type)); | |
768 | else | |
769 | rb_strlcpy(type, "TEXT", sizeof(type)); | |
770 | ||
771 | /* attempt to add a column with extreme prejudice, errors are ignored */ | |
772 | rsdb_exec(NULL, "ALTER TABLE %s ADD COLUMN %s %s", bandb_table[i], | |
773 | columns[j], type); | |
774 | } | |
775 | } | |
776 | ||
777 | i++; /* skip over .perm */ | |
778 | } | |
779 | } | |
780 | ||
781 | static void | |
782 | db_reclaim_slack(void) | |
783 | { | |
784 | fprintf(stdout, "* Reclaiming free space.\n"); | |
785 | rsdb_exec(NULL, "VACUUM"); | |
786 | } | |
787 | ||
788 | ||
789 | /** | |
790 | * check that appropriate tables exist. | |
791 | */ | |
792 | static int | |
793 | table_exists(const char *dbtab) | |
794 | { | |
795 | struct rsdb_table table; | |
796 | rsdb_exec_fetch(&table, "SELECT name FROM sqlite_master WHERE type='table' AND name='%s'", | |
797 | dbtab); | |
798 | rsdb_exec_fetch_end(&table); | |
799 | return table.row_count; | |
800 | } | |
801 | ||
802 | /** | |
803 | * check that there are actual entries in a table | |
804 | */ | |
805 | static int | |
806 | table_has_rows(const char *dbtab) | |
807 | { | |
808 | struct rsdb_table table; | |
809 | rsdb_exec_fetch(&table, "SELECT * FROM %s", dbtab); | |
810 | rsdb_exec_fetch_end(&table); | |
811 | return table.row_count; | |
812 | } | |
813 | ||
814 | /** | |
815 | * completly wipes out an existing ban.db of all entries. | |
816 | */ | |
817 | static void | |
818 | wipe_schema(void) | |
819 | { | |
820 | int i; | |
821 | rsdb_transaction(RSDB_TRANS_START); | |
822 | for(i = 0; i < LAST_BANDB_TYPE; i++) | |
823 | { | |
824 | rsdb_exec(NULL, "DROP TABLE %s", bandb_table[i]); | |
825 | i++; /* double increment to skip over .perm */ | |
826 | } | |
827 | rsdb_transaction(RSDB_TRANS_END); | |
828 | ||
829 | check_schema(); | |
830 | } | |
831 | ||
832 | /** | |
833 | * remove pre-existing duplicate bans from the database. | |
834 | * we favor the new, imported ban over the one in the database | |
835 | */ | |
836 | void | |
837 | drop_dupes(const char *user, const char *host, const char *t) | |
838 | { | |
839 | rsdb_exec(NULL, "DELETE FROM %s WHERE mask1='%Q' AND mask2='%Q'", t, user, host); | |
840 | } | |
841 | ||
842 | static void | |
843 | db_error_cb(const char *errstr) | |
844 | { | |
845 | return; | |
846 | } | |
847 | ||
848 | ||
849 | /** | |
850 | * convert unix timestamp to human readable (small) date | |
851 | */ | |
852 | static char * | |
853 | bt_smalldate(const char *string) | |
854 | { | |
855 | static char buf[MAX_DATE_STRING]; | |
856 | struct tm *lt; | |
857 | time_t t; | |
858 | t = strtol(string, NULL, 10); | |
859 | lt = gmtime(&t); | |
860 | if(lt == NULL) | |
861 | return NULL; | |
862 | rb_snprintf(buf, sizeof(buf), "%d/%d/%d %02d.%02d", | |
863 | lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min); | |
864 | return buf; | |
865 | } | |
866 | ||
867 | /** | |
868 | * you are here ->. | |
869 | */ | |
870 | void | |
871 | print_help(int i_exit) | |
872 | { | |
873 | fprintf(stderr, "bantool v.%s - the ircd-ratbox database tool.\n", BT_VERSION); | |
874 | fprintf(stderr, "Copyright (C) 2008 Daniel J Reidy <dubkat@gmail.com>\n"); | |
875 | fprintf(stderr, "$Id: bantool.c 26164 2008-10-26 19:52:43Z androsyn $\n\n"); | |
876 | fprintf(stderr, "This program is distributed in the hope that it will be useful,\n" | |
877 | "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" | |
878 | "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" | |
879 | "GNU General Public License for more details.\n\n"); | |
880 | ||
881 | fprintf(stderr, "Usage: %s <-i|-e> [-p] [-v] [-h] [-d] [-w] [path]\n", me); | |
882 | fprintf(stderr, " -h : Display some slightly useful help.\n"); | |
883 | fprintf(stderr, " -i : Actually import configs into your database.\n"); | |
884 | fprintf(stderr, " -e : Export your database to old-style flat files.\n"); | |
885 | fprintf(stderr, | |
886 | " This is suitable for redistrubuting your banlists, or creating backups.\n"); | |
887 | fprintf(stderr, " -s : Reclaim empty slack space the database may be taking up.\n"); | |
888 | fprintf(stderr, " -u : Update the database tables to support any new features.\n"); | |
889 | fprintf(stderr, | |
890 | " This is automaticlly done if you are importing or exporting\n"); | |
891 | fprintf(stderr, " but should be run whenever you upgrade the ircd.\n"); | |
892 | fprintf(stderr, | |
893 | " -p : pretend, checks for the configs, and parses them, then tells you some data...\n"); | |
894 | fprintf(stderr, " but does not touch your database.\n"); | |
895 | fprintf(stderr, | |
896 | " -v : Be verbose... and it *is* very verbose! (intended for debugging)\n"); | |
897 | fprintf(stderr, " -d : Enable checking for redunant entries.\n"); | |
898 | fprintf(stderr, " -w : Completly wipe your database clean. May be used with -i \n"); | |
899 | fprintf(stderr, | |
900 | " path : An optional directory containing old ratbox configs for import, or export.\n"); | |
901 | fprintf(stderr, " If not specified, it looks in PREFIX/etc.\n"); | |
902 | exit(i_exit); | |
903 | } |