]>
jfr.im git - solanum.git/blob - bandb/bantool.c
2 * ircd-ratbox: A slightly useful ircd.
3 * bantool.c: The ircd-ratbox database managment tool.
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>
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.
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.
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
25 * $Id: bantool.c 26164 2008-10-26 19:52:43Z androsyn $
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
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
49 #define EmptyString(x) ((x == NULL) || (*(x) == '\0'))
50 #define CheckEmpty(x) EmptyString(x) ? "" : x
52 #define BT_VERSION "0.4.1"
68 static char bandb_letter
[LAST_BANDB_TYPE
] = {
69 'K', 'K', 'D', 'D', 'X', 'X', 'R', 'R'
72 static const char *bandb_table
[LAST_BANDB_TYPE
] = {
73 "kline", "kline", "dline", "dline", "xline", "xline", "resv", "resv"
76 static const char *bandb_suffix
[LAST_BANDB_TYPE
] = {
83 static char me
[PATH_MAX
];
94 } count
= {0, 0, 0, 0, 0};
96 /* flags set by command line options */
108 } flag
= {YES
, NO
, NO
, NO
, NO
, NO
, NO
, NO
, NO
};
111 static int table_has_rows(const char *table
);
112 static int table_exists(const char *table
);
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
);
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
);
134 main(int argc
, char *argv
[])
141 rb_strlcpy(me
, argv
[0], sizeof(me
));
143 while((opt
= getopt(argc
, argv
, "hieuspvwd")) != -1)
148 print_help(EXIT_SUCCESS
);
179 print_help(EXIT_FAILURE
);
183 /* they should really read the help. */
185 print_help(EXIT_FAILURE
);
187 if((flag
.import && flag
.export
) || (flag
.export
&& flag
.wipe
)
188 || (flag
.verify
&& flag
.pretend
) || (flag
.export
&& flag
.pretend
))
190 fprintf(stderr
, "* Error: Conflicting flags.\n");
191 if(flag
.export
&& flag
.pretend
)
192 fprintf(stderr
, "* There is nothing to 'pretend' when exporting.\n");
194 fprintf(stderr
, "* For an explination of commands, run: %s -h\n", me
);
198 if(argv
[optind
] != NULL
)
199 rb_strlcpy(etc
, argv
[optind
], sizeof(etc
));
201 rb_strlcpy(etc
, ETCPATH
, sizeof(ETCPATH
));
204 "* ircd-ratbox bantool v.%s ($Id: bantool.c 26164 2008-10-26 19:52:43Z androsyn $)\n",
207 if(flag
.pretend
== NO
)
209 if(rsdb_init(db_error_cb
) == -1)
211 fprintf(stderr
, "* Error: Unable to open database\n");
219 if(flag
.import && flag
.wipe
)
221 flag
.dupes_ok
= YES
; /* dont check for dupes if we are wiping the db clean */
222 for(i
= 0; i
< 3; i
++)
224 "* WARNING: YOU ARE ABOUT TO WIPE YOUR DATABASE!\n");
226 fprintf(stdout
, "* Press ^C to abort! ");
229 fprintf(stdout
, "Carrying on...\n");
233 if(flag
.verbose
&& flag
.dupes_ok
== YES
)
234 fprintf(stdout
, "* Allowing duplicate bans...\n");
236 /* checking for our files to import or export */
237 for(i
= 0; i
< LAST_BANDB_TYPE
; i
++)
239 rb_snprintf(conf
, sizeof(conf
), "%s/%s.conf%s",
240 etc
, bandb_table
[i
], bandb_suffix
[i
]);
242 if(flag
.import && flag
.pretend
== NO
)
243 rsdb_transaction(RSDB_TRANS_START
);
246 import_config(conf
, i
);
249 export_config(conf
, i
);
251 if(flag
.import && flag
.pretend
== NO
)
252 rsdb_transaction(RSDB_TRANS_END
);
257 if(count
.error
&& flag
.verbose
)
258 fprintf(stderr
, "* I was unable to locate %i config files to import.\n",
261 fprintf(stdout
, "* Import Stats: Klines: %i, Dlines: %i, Xlines: %i, Resvs: %i \n",
262 count
.klines
, count
.dlines
, count
.xlines
, count
.resvs
);
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");
269 "* Pretend mode engaged. Nothing was actually entered into the database.\n");
277 * export the database to old-style flat files
280 export_config(const char *conf
, int id
)
282 struct rsdb_table table
;
283 static char sql
[BUFSIZE
* 2];
284 static char buf
[512];
288 /* for sanity sake */
291 const int reason
= 2;
294 /* const int perm = 5; */
296 if(!table_has_rows(bandb_table
[id
]))
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",
304 rb_snprintf(sql
, sizeof(sql
),
305 "SELECT DISTINCT mask1,mask2,reason,oper,time FROM %s WHERE perm = 0 ORDER BY time",
308 rsdb_exec_fetch(&table
, sql
);
309 if(table
.row_count
<= 0)
311 rsdb_exec_fetch_end(&table
);
316 fprintf(stdout
, "* checking for %s: ", conf
); /* debug */
318 /* open config for reading, or skip to the next */
319 if(!(fd
= fopen(conf
, "w")))
322 fprintf(stdout
, "\tmissing.\n");
327 for(j
= 0; j
< table
.row_count
; j
++)
332 case BANDB_DLINE_PERM
:
333 rb_snprintf(buf
, sizeof(buf
),
334 "\"%s\",\"%s\",\"\",\"%s\",\"%s\",%s\n",
336 mangle_reason(table
.row
[j
][reason
]),
337 bt_smalldate(table
.row
[j
][ts
]),
338 table
.row
[j
][oper
], table
.row
[j
][ts
]);
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
]);
351 case BANDB_RESV_PERM
:
352 rb_snprintf(buf
, sizeof(buf
),
353 "\"%s\",\"%s\",\"%s\",%s\n",
355 mangle_reason(table
.row
[j
][reason
]),
356 table
.row
[j
][oper
], table
.row
[j
][ts
]);
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
],
370 fprintf(fd
, "%s", buf
);
373 rsdb_exec_fetch_end(&table
);
375 fprintf(stdout
, "\twritten.\n");
380 * attempt to condense the individual conf functions into one
383 import_config(const char *conf
, int id
)
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
;
398 char newreason
[REASONLEN
];
401 fprintf(stdout
, "* checking for %s: ", conf
); /* debug */
403 /* open config for reading, or skip to the next */
404 if(!(fd
= fopen(conf
, "r")))
407 fprintf(stdout
, "%*s", strlen(bandb_suffix
[id
]) > 0 ? 10 : 15,
413 if(strstr(conf
, ".perm") != 0)
418 * "SYSTEM","0","banned","stevoo!stevoo@efnet.port80.se{stevoo}",1111080437
420 * "OseK","banned nickname","stevoo!stevoo@efnet.port80.se{stevoo}",1111031619
422 * "194.158.192.0/19","laptop scammers","","2005/3/17 05.33","stevoo!stevoo@efnet.port80.se{stevoo}",1111033988
424 while(fgets(line
, sizeof(line
), fd
))
426 if((p
= strpbrk(line
, "\r\n")) != NULL
)
429 if((*line
== '\0') || (*line
== '#'))
433 f_mask1
= getfield(line
);
435 if(EmptyString(f_mask1
))
442 case BANDB_XLINE_PERM
:
443 f_mask1
= escape_quotes(clean_gecos_field(f_mask1
));
444 getfield(NULL
); /* empty field */
448 case BANDB_RESV_PERM
:
450 case BANDB_DLINE_PERM
:
454 f_mask2
= getfield(NULL
);
455 if(EmptyString(f_mask2
))
461 f_reason
= getfield(NULL
);
462 if(EmptyString(f_reason
))
469 case BANDB_KLINE_PERM
:
471 case BANDB_DLINE_PERM
:
472 f_oreason
= getfield(NULL
);
480 f_oper
= getfield(NULL
);
481 f_time
= strip_quotes(f_oper
+ strlen(f_oper
) + 2);
482 if(EmptyString(f_oper
))
486 if(id
== BANDB_KLINE
|| id
== BANDB_KLINE_PERM
)
488 if(strstr(f_mask1
, "!") != NULL
)
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");
498 /* append operreason_field to reason_field */
499 if(!EmptyString(f_oreason
))
500 rb_snprintf(newreason
, sizeof(newreason
), "%s | %s", f_reason
, f_oreason
);
502 rb_snprintf(newreason
, sizeof(newreason
), "%s", f_reason
);
504 if(flag
.pretend
== NO
)
506 if(flag
.dupes_ok
== NO
)
507 drop_dupes(f_mask1
, f_mask2
, bandb_table
[id
]);
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
,
515 if(flag
.pretend
&& flag
.verbose
)
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
,
524 switch (bandb_letter
[id
])
543 fprintf(stdout
, "%*s\n", strlen(bandb_suffix
[id
]) > 0 ? 10 : 15, "imported.");
553 * inputs - input buffer
554 * output - next field
555 * side effects - field breakup for ircd.conf file.
558 getfield(char *newline
)
560 static char *line
= NULL
;
571 /* XXX make this skip to first " if present */
575 return (NULL
); /* mal-formed field */
577 end
= strchr(line
, ',');
581 /* no trailing , - last field */
584 end
= line
+ strlen(line
);
597 /* look for a ", to mark the end of a field.. */
598 if(*(end
- 1) == '"')
606 /* search for the next ',' */
608 end
= strchr(end
, ',');
616 * strip away "quotes" from around strings
619 strip_quotes(const char *string
)
621 static char buf
[14]; /* int(11) + 2 + \0 */
640 * escape quotes in a string
643 escape_quotes(const char *string
)
645 static char buf
[BUFSIZE
* 2];
670 mangle_reason(const char *string
)
672 static char buf
[BUFSIZE
* 2];
701 * change spaces to \s in gecos field
704 clean_gecos_field(const char *gecos
)
706 static char buf
[BUFSIZE
* 2];
728 * verify the database integrity, and if necessary create apropriate tables
734 char type
[8]; /* longest string is 'INTEGER\0' */
736 if(flag
.verify
|| flag
.verbose
)
737 fprintf(stdout
, "* Verifying database.\n");
739 const char *columns
[] = {
749 for(i
= 0; i
< LAST_BANDB_TYPE
; i
++)
751 if(!table_exists(bandb_table
[i
]))
754 "CREATE TABLE %s (mask1 TEXT, mask2 TEXT, oper TEXT, time INTEGER, perm INTEGER, reason TEXT)",
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
764 for(j
= 0; columns
[j
] != NULL
; j
++)
766 if(!strcmp(columns
[j
], "time") && !strcmp(columns
[j
], "perm"))
767 rb_strlcpy(type
, "INTEGER", sizeof(type
));
769 rb_strlcpy(type
, "TEXT", sizeof(type
));
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
],
777 i
++; /* skip over .perm */
782 db_reclaim_slack(void)
784 fprintf(stdout
, "* Reclaiming free space.\n");
785 rsdb_exec(NULL
, "VACUUM");
790 * check that appropriate tables exist.
793 table_exists(const char *dbtab
)
795 struct rsdb_table table
;
796 rsdb_exec_fetch(&table
, "SELECT name FROM sqlite_master WHERE type='table' AND name='%s'",
798 rsdb_exec_fetch_end(&table
);
799 return table
.row_count
;
803 * check that there are actual entries in a table
806 table_has_rows(const char *dbtab
)
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
;
815 * completly wipes out an existing ban.db of all entries.
821 rsdb_transaction(RSDB_TRANS_START
);
822 for(i
= 0; i
< LAST_BANDB_TYPE
; i
++)
824 rsdb_exec(NULL
, "DROP TABLE %s", bandb_table
[i
]);
825 i
++; /* double increment to skip over .perm */
827 rsdb_transaction(RSDB_TRANS_END
);
833 * remove pre-existing duplicate bans from the database.
834 * we favor the new, imported ban over the one in the database
837 drop_dupes(const char *user
, const char *host
, const char *t
)
839 rsdb_exec(NULL
, "DELETE FROM %s WHERE mask1='%Q' AND mask2='%Q'", t
, user
, host
);
843 db_error_cb(const char *errstr
)
850 * convert unix timestamp to human readable (small) date
853 bt_smalldate(const char *string
)
855 static char buf
[MAX_DATE_STRING
];
858 t
= strtol(string
, NULL
, 10);
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
);
871 print_help(int i_exit
)
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");
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");
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");
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");
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");
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");
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");