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