]>
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 * 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
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
46 #define EmptyString(x) ((x == NULL) || (*(x) == '\0'))
47 #define CheckEmpty(x) EmptyString(x) ? "" : x
49 #define BT_VERSION "0.4.1"
65 static char bandb_letter
[LAST_BANDB_TYPE
] = {
66 'K', 'K', 'D', 'D', 'X', 'X', 'R', 'R'
69 static const char *bandb_table
[LAST_BANDB_TYPE
] = {
70 "kline", "kline", "dline", "dline", "xline", "xline", "resv", "resv"
73 static const char *bandb_suffix
[LAST_BANDB_TYPE
] = {
80 static char me
[PATH_MAX
];
91 } count
= {0, 0, 0, 0, 0};
93 /* flags set by command line options */
105 } flag
= {YES
, NO
, NO
, NO
, NO
, NO
, NO
, NO
, NO
};
108 static int table_has_rows(const char *table
);
109 static int table_exists(const char *table
);
111 static const char *clean_gecos_field(const char *gecos
);
112 static char *bt_smalldate(const char *string
);
113 static char *getfield(char *newline
);
114 static char *strip_quotes(const char *string
);
115 static char *mangle_reason(const char *string
);
116 static char *escape_quotes(const char *string
);
118 static void db_error_cb(const char *errstr
);
119 static void db_reclaim_slack(void);
120 static void export_config(const char *conf
, int id
);
121 static void import_config(const char *conf
, int id
);
122 static void check_schema(void);
123 static void print_help(int i_exit
);
124 static void wipe_schema(void);
125 static void drop_dupes(const char *user
, const char *host
, const char *t
);
131 main(int argc
, char *argv
[])
138 rb_strlcpy(me
, argv
[0], sizeof(me
));
140 while((opt
= getopt(argc
, argv
, "hieuspvwd")) != -1)
145 print_help(EXIT_SUCCESS
);
176 print_help(EXIT_FAILURE
);
180 /* they should really read the help. */
182 print_help(EXIT_FAILURE
);
184 if((flag
.import && flag
.export
) || (flag
.export
&& flag
.wipe
)
185 || (flag
.verify
&& flag
.pretend
) || (flag
.export
&& flag
.pretend
))
187 fprintf(stderr
, "* Error: Conflicting flags.\n");
188 if(flag
.export
&& flag
.pretend
)
189 fprintf(stderr
, "* There is nothing to 'pretend' when exporting.\n");
191 fprintf(stderr
, "* For an explination of commands, run: %s -h\n", me
);
195 if(argv
[optind
] != NULL
)
196 rb_strlcpy(etc
, argv
[optind
], sizeof(etc
));
198 rb_strlcpy(etc
, ETCPATH
, sizeof(ETCPATH
));
201 "* charybdis bantool v.%s\n", BT_VERSION
);
203 if(flag
.pretend
== NO
)
205 if(rsdb_init(db_error_cb
) == -1)
207 fprintf(stderr
, "* Error: Unable to open database\n");
215 if(flag
.import && flag
.wipe
)
217 flag
.dupes_ok
= YES
; /* dont check for dupes if we are wiping the db clean */
218 for(i
= 0; i
< 3; i
++)
220 "* WARNING: YOU ARE ABOUT TO WIPE YOUR DATABASE!\n");
222 fprintf(stdout
, "* Press ^C to abort! ");
225 fprintf(stdout
, "Carrying on...\n");
229 if(flag
.verbose
&& flag
.dupes_ok
== YES
)
230 fprintf(stdout
, "* Allowing duplicate bans...\n");
232 /* checking for our files to import or export */
233 for(i
= 0; i
< LAST_BANDB_TYPE
; i
++)
235 snprintf(conf
, sizeof(conf
), "%s/%s.conf%s",
236 etc
, bandb_table
[i
], bandb_suffix
[i
]);
238 if(flag
.import && flag
.pretend
== NO
)
239 rsdb_transaction(RSDB_TRANS_START
);
242 import_config(conf
, i
);
245 export_config(conf
, i
);
247 if(flag
.import && flag
.pretend
== NO
)
248 rsdb_transaction(RSDB_TRANS_END
);
253 if(count
.error
&& flag
.verbose
)
254 fprintf(stderr
, "* I was unable to locate %u config files to import.\n",
257 fprintf(stdout
, "* Import Stats: Klines: %u, Dlines: %u, Xlines: %u, Resvs: %u \n",
258 count
.klines
, count
.dlines
, count
.xlines
, count
.resvs
);
261 "*\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");
265 "* Pretend mode engaged. Nothing was actually entered into the database.\n");
273 * export the database to old-style flat files
276 export_config(const char *conf
, int id
)
278 struct rsdb_table table
;
279 static char sql
[BUFSIZE
* 2];
280 static char buf
[512];
284 /* for sanity sake */
287 const int reason
= 2;
290 /* const int perm = 5; */
292 if(!table_has_rows(bandb_table
[id
]))
295 if(strstr(conf
, ".perm") != 0)
296 snprintf(sql
, sizeof(sql
),
297 "SELECT DISTINCT mask1,mask2,reason,oper,time FROM %s WHERE perm = 1 ORDER BY time",
300 snprintf(sql
, sizeof(sql
),
301 "SELECT DISTINCT mask1,mask2,reason,oper,time FROM %s WHERE perm = 0 ORDER BY time",
304 rsdb_exec_fetch(&table
, sql
);
305 if(table
.row_count
<= 0)
307 rsdb_exec_fetch_end(&table
);
312 fprintf(stdout
, "* checking for %s: ", conf
); /* debug */
314 /* open config for reading, or skip to the next */
315 if(!(fd
= fopen(conf
, "w")))
318 fprintf(stdout
, "\tmissing.\n");
323 for(j
= 0; j
< table
.row_count
; j
++)
328 case BANDB_DLINE_PERM
:
329 snprintf(buf
, sizeof(buf
),
330 "\"%s\",\"%s\",\"\",\"%s\",\"%s\",%s\n",
332 mangle_reason(table
.row
[j
][reason
]),
333 bt_smalldate(table
.row
[j
][ts
]),
334 table
.row
[j
][oper
], table
.row
[j
][ts
]);
338 case BANDB_XLINE_PERM
:
339 snprintf(buf
, sizeof(buf
),
340 "\"%s\",\"0\",\"%s\",\"%s\",%s\n",
341 escape_quotes(table
.row
[j
][mask1
]),
342 mangle_reason(table
.row
[j
][reason
]),
343 table
.row
[j
][oper
], table
.row
[j
][ts
]);
347 case BANDB_RESV_PERM
:
348 snprintf(buf
, sizeof(buf
),
349 "\"%s\",\"%s\",\"%s\",%s\n",
351 mangle_reason(table
.row
[j
][reason
]),
352 table
.row
[j
][oper
], table
.row
[j
][ts
]);
356 default: /* Klines */
357 snprintf(buf
, sizeof(buf
),
358 "\"%s\",\"%s\",\"%s\",\"\",\"%s\",\"%s\",%s\n",
359 table
.row
[j
][mask1
], table
.row
[j
][mask2
],
360 mangle_reason(table
.row
[j
][reason
]),
361 bt_smalldate(table
.row
[j
][ts
]), table
.row
[j
][oper
],
366 fprintf(fd
, "%s", buf
);
369 rsdb_exec_fetch_end(&table
);
371 fprintf(stdout
, "\twritten.\n");
376 * attempt to condense the individual conf functions into one
379 import_config(const char *conf
, int id
)
388 const char *f_mask1
= NULL
;
389 const char *f_mask2
= NULL
;
390 const char *f_oper
= NULL
;
391 const char *f_time
= NULL
;
392 const char *f_reason
= NULL
;
393 const char *f_oreason
= NULL
;
394 char newreason
[REASONLEN
];
397 fprintf(stdout
, "* checking for %s: ", conf
); /* debug */
399 /* open config for reading, or skip to the next */
400 if(!(fd
= fopen(conf
, "r")))
403 fprintf(stdout
, "%*s", strlen(bandb_suffix
[id
]) > 0 ? 10 : 15,
409 if(strstr(conf
, ".perm") != 0)
414 * "SYSTEM","0","banned","stevoo!stevoo@efnet.port80.se{stevoo}",1111080437
416 * "OseK","banned nickname","stevoo!stevoo@efnet.port80.se{stevoo}",1111031619
418 * "194.158.192.0/19","laptop scammers","","2005/3/17 05.33","stevoo!stevoo@efnet.port80.se{stevoo}",1111033988
420 while(fgets(line
, sizeof(line
), fd
))
422 if((p
= strpbrk(line
, "\r\n")) != NULL
)
425 if((*line
== '\0') || (*line
== '#'))
429 f_mask1
= getfield(line
);
431 if(EmptyString(f_mask1
))
438 case BANDB_XLINE_PERM
:
439 f_mask1
= escape_quotes(clean_gecos_field(f_mask1
));
440 getfield(NULL
); /* empty field */
444 case BANDB_RESV_PERM
:
446 case BANDB_DLINE_PERM
:
450 f_mask2
= getfield(NULL
);
451 if(EmptyString(f_mask2
))
457 f_reason
= getfield(NULL
);
458 if(EmptyString(f_reason
))
465 case BANDB_KLINE_PERM
:
467 case BANDB_DLINE_PERM
:
468 f_oreason
= getfield(NULL
);
476 f_oper
= getfield(NULL
);
477 f_time
= strip_quotes(f_oper
+ strlen(f_oper
) + 2);
478 if(EmptyString(f_oper
))
482 if(id
== BANDB_KLINE
|| id
== BANDB_KLINE_PERM
)
484 if(strstr(f_mask1
, "!") != NULL
)
487 "* SKIPPING INVALID KLINE %s@%s set by %s\n",
488 f_mask1
, f_mask2
, f_oper
);
489 fprintf(stderr
, " You may wish to re-apply it correctly.\n");
494 /* append operreason_field to reason_field */
495 if(!EmptyString(f_oreason
))
496 snprintf(newreason
, sizeof(newreason
), "%s | %s", f_reason
, f_oreason
);
498 snprintf(newreason
, sizeof(newreason
), "%s", f_reason
);
500 if(flag
.pretend
== NO
)
502 if(flag
.dupes_ok
== NO
)
503 drop_dupes(f_mask1
, f_mask2
, bandb_table
[id
]);
506 "INSERT INTO %s (mask1, mask2, oper, time, perm, reason) VALUES('%Q','%Q','%Q','%Q','%d','%Q')",
507 bandb_table
[id
], f_mask1
, f_mask2
, f_oper
, f_time
, f_perm
,
511 if(flag
.pretend
&& flag
.verbose
)
513 "%s: perm(%d) mask1(%s) mask2(%s) oper(%s) reason(%s) time(%s)\n",
514 bandb_table
[id
], f_perm
, f_mask1
, f_mask2
, f_oper
, newreason
,
520 switch (bandb_letter
[id
])
539 fprintf(stdout
, "%*s\n", strlen(bandb_suffix
[id
]) > 0 ? 10 : 15, "imported.");
549 * inputs - input buffer
550 * output - next field
551 * side effects - field breakup for ircd.conf file.
554 getfield(char *newline
)
556 static char *line
= NULL
;
567 /* XXX make this skip to first " if present */
571 return (NULL
); /* mal-formed field */
573 end
= strchr(line
, ',');
577 /* no trailing , - last field */
580 end
= line
+ strlen(line
);
593 /* look for a ", to mark the end of a field.. */
594 if(*(end
- 1) == '"')
602 /* search for the next ',' */
604 end
= strchr(end
, ',');
612 * strip away "quotes" from around strings
615 strip_quotes(const char *string
)
617 static char buf
[14]; /* int(11) + 2 + \0 */
636 * escape quotes in a string
639 escape_quotes(const char *string
)
641 static char buf
[BUFSIZE
* 2];
666 mangle_reason(const char *string
)
668 static char buf
[BUFSIZE
* 2];
697 * change spaces to \s in gecos field
700 clean_gecos_field(const char *gecos
)
702 static char buf
[BUFSIZE
* 2];
724 * verify the database integrity, and if necessary create apropriate tables
730 char type
[8]; /* longest string is 'INTEGER\0' */
732 if(flag
.verify
|| flag
.verbose
)
733 fprintf(stdout
, "* Verifying database.\n");
735 const char *columns
[] = {
745 for(i
= 0; i
< LAST_BANDB_TYPE
; i
++)
747 if(!table_exists(bandb_table
[i
]))
750 "CREATE TABLE %s (mask1 TEXT, mask2 TEXT, oper TEXT, time INTEGER, perm INTEGER, reason TEXT)",
755 * i can't think of any better way to do this, other then attempt to
756 * force the creation of column that may, or may not already exist. --dubkat
760 for(j
= 0; columns
[j
] != NULL
; j
++)
762 if(!strcmp(columns
[j
], "time") && !strcmp(columns
[j
], "perm"))
763 rb_strlcpy(type
, "INTEGER", sizeof(type
));
765 rb_strlcpy(type
, "TEXT", sizeof(type
));
767 /* attempt to add a column with extreme prejudice, errors are ignored */
768 rsdb_exec(NULL
, "ALTER TABLE %s ADD COLUMN %s %s", bandb_table
[i
],
773 i
++; /* skip over .perm */
778 db_reclaim_slack(void)
780 fprintf(stdout
, "* Reclaiming free space.\n");
781 rsdb_exec(NULL
, "VACUUM");
786 * check that appropriate tables exist.
789 table_exists(const char *dbtab
)
791 struct rsdb_table table
;
792 rsdb_exec_fetch(&table
, "SELECT name FROM sqlite_master WHERE type='table' AND name='%s'",
794 rsdb_exec_fetch_end(&table
);
795 return table
.row_count
;
799 * check that there are actual entries in a table
802 table_has_rows(const char *dbtab
)
804 struct rsdb_table table
;
805 rsdb_exec_fetch(&table
, "SELECT * FROM %s", dbtab
);
806 rsdb_exec_fetch_end(&table
);
807 return table
.row_count
;
811 * completly wipes out an existing ban.db of all entries.
817 rsdb_transaction(RSDB_TRANS_START
);
818 for(i
= 0; i
< LAST_BANDB_TYPE
; i
++)
820 rsdb_exec(NULL
, "DROP TABLE %s", bandb_table
[i
]);
821 i
++; /* double increment to skip over .perm */
823 rsdb_transaction(RSDB_TRANS_END
);
829 * remove pre-existing duplicate bans from the database.
830 * we favor the new, imported ban over the one in the database
833 drop_dupes(const char *user
, const char *host
, const char *t
)
835 rsdb_exec(NULL
, "DELETE FROM %s WHERE mask1='%Q' AND mask2='%Q'", t
, user
, host
);
839 db_error_cb(const char *errstr
)
846 * convert unix timestamp to human readable (small) date
849 bt_smalldate(const char *string
)
851 static char buf
[MAX_DATE_STRING
];
854 t
= strtol(string
, NULL
, 10);
858 snprintf(buf
, sizeof(buf
), "%d/%d/%d %02d.%02d",
859 lt
->tm_year
+ 1900, lt
->tm_mon
+ 1, lt
->tm_mday
, lt
->tm_hour
, lt
->tm_min
);
867 print_help(int i_exit
)
869 fprintf(stderr
, "bantool v.%s - the charybdis database tool.\n", BT_VERSION
);
870 fprintf(stderr
, "Copyright (C) 2008 Daniel J Reidy <dubkat@gmail.com>\n");
871 fprintf(stderr
, "This program is distributed in the hope that it will be useful,\n"
872 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
873 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
874 "GNU General Public License for more details.\n\n");
876 fprintf(stderr
, "Usage: %s <-i|-e> [-p] [-v] [-h] [-d] [-w] [path]\n", me
);
877 fprintf(stderr
, " -h : Display some slightly useful help.\n");
878 fprintf(stderr
, " -i : Actually import configs into your database.\n");
879 fprintf(stderr
, " -e : Export your database to old-style flat files.\n");
881 " This is suitable for redistrubuting your banlists, or creating backups.\n");
882 fprintf(stderr
, " -s : Reclaim empty slack space the database may be taking up.\n");
883 fprintf(stderr
, " -u : Update the database tables to support any new features.\n");
885 " This is automaticlly done if you are importing or exporting\n");
886 fprintf(stderr
, " but should be run whenever you upgrade the ircd.\n");
888 " -p : pretend, checks for the configs, and parses them, then tells you some data...\n");
889 fprintf(stderr
, " but does not touch your database.\n");
891 " -v : Be verbose... and it *is* very verbose! (intended for debugging)\n");
892 fprintf(stderr
, " -d : Enable checking for redunant entries.\n");
893 fprintf(stderr
, " -w : Completly wipe your database clean. May be used with -i \n");
895 " path : An optional directory containing old ratbox configs for import, or export.\n");
896 fprintf(stderr
, " If not specified, it looks in PREFIX/etc.\n");