2 * Copyright (c) 2017, De Rais <derais@cock.li>
4 * Permission to use, copy, modify, and/or distribute this software for
5 * any purpose with or without fee is hereby granted, provided that the
6 * above copyright notice and this permission notice appear in all
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
33 const char *program_name
= "rb79-ban-ip";
35 /* Show usage message, do not exit */
38 size_t len
= strlen(program_name
);
40 printf("Usage: %s [ -i individual-ip ] # -i, -f mutually exclusive\n",
42 printf(" %*s [ -f first-ip -l last-ip ]\n", (int) len
, "");
43 printf(" %*s [ -m ban-message ]\n", (int) len
, "");
44 printf(" %*s [ -b board-name ] # default: global ban\n",
46 printf(" %*s [ -u banned-until ] # default: 7 days from now\n",
51 int main(int argc
, char **argv
)
54 struct configuration conf
= { 0 };
56 const char *b_arg
= 0;
58 uint_fast8_t global_ban
= 0;
59 const char *first_ip
= 0;
60 char *normalized_first_ip
= 0;
61 const char *last_ip
= 0;
62 char *normalized_last_ip
= 0;
63 const char *ban_message
= 0;
64 const char *raw_until
= 0;
67 setlocale(LC_ALL
, "");
70 while ((opt
= getopt(argc
, argv
, "i:f:l:m:u:b:")) != -1) {
75 ERROR_MESSAGE("-b already specified");
84 ERROR_MESSAGE("Only one IP address or"
85 " range may be specified");
95 ERROR_MESSAGE("Only one IP address or"
96 " range may be specified");
105 ERROR_MESSAGE("Only one IP address or"
106 " range may be specified");
115 ERROR_MESSAGE("Only one ban message"
116 " may be specified");
120 ban_message
= optarg
;
125 ERROR_MESSAGE("Only one expiry time"
126 " may be specified");
140 ERROR_MESSAGE("An IP address or range must be specified");
150 until
= (time_t) strtoull(raw_until
, 0, 0);
152 until
= time(0) + (7 * 24 * 60 * 60);
155 conf
= (struct configuration
) {
157 .static_www_folder
= static_www_folder
, /* */
158 .work_path
= work_path
, /* */
159 .trip_salt
= trip_salt
, /* */
160 .trip_salt_len
= strlen(trip_salt
), /* */
161 .boards
= boards
, /* */
162 .boards_num
= NUM_OF(boards
), /* */
163 .max_form_data_size
= max_form_data_size
, /* */
164 .max_file_size
= max_file_size
, /* */
165 .max_text_len
= max_text_len
, /* */
166 .filetypes
= filetypes
, /* */
167 .filetypes_num
= NUM_OF(filetypes
), /* */
168 .file_description_prog
= file_description_prog
, /* */
169 .headers
= headers
, /* */
170 .headers_num
= NUM_OF(headers
), /* */
171 .challenges
= challenges
, /* */
172 .challenges_num
= NUM_OF(challenges
), /* */
173 .wordfilter_inputs
= wordfilter_inputs
, /* */
174 .wordfilter_inputs_num
= NUM_OF(wordfilter_inputs
), /* */
177 /* Interpret board */
181 board_idx
= (size_t) -1;
183 for (size_t j
= 0; j
< conf
.boards_num
; ++j
) {
184 if (!strcmp(conf
.boards
[j
].name
, b_arg
)) {
189 if (board_idx
== (size_t) -1) {
190 ERROR_MESSAGE("No board \"%s\" known", b_arg
);
195 /* Interpret IP addresses */
196 if (util_normalize_ip(first_ip
, &normalized_first_ip
) < 0) {
200 if (util_normalize_ip(last_ip
, &normalized_last_ip
) < 0) {
204 /* Set up a minimal part of the system */
205 if (setup_locks(&conf
) < 0) {
209 if (setup_dbs(&conf
) < 0) {
213 if (setup_write_thread(&conf
) < 0) {
217 ret
= db_insert_ban(global_ban
, board_idx
, normalized_first_ip
,
218 normalized_last_ip
, ban_message
, time(0), until
);
222 clean_write_thread();
223 free(normalized_first_ip
);
224 free(normalized_last_ip
);