1 /* Copyright (C) 2009-2012, Martin Johansson <martin@fatbob.nu>
2 Copyright (C) 2005-2012, Thorvald Natvig <thorvald@natvig.com>
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
10 - Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 - Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15 - Neither the name of the Developers nor the names of its contributors may
16 be used to endorse or promote products derived from this software without
17 specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 declare_list(banlist);
41 static int bancount; /* = 0 */
42 static int ban_duration;
46 ban_duration = getIntConf(BAN_LENGTH);
47 /* Read ban file here */
54 void Ban_UserBan(client_t *client, char *reason)
59 ban = malloc(sizeof(ban_t));
61 Log_fatal("Out of memory");
62 memset(ban, 0, sizeof(ban_t));
64 memcpy(ban->hash, client->hash, 20);
65 memcpy(&ban->address, &client->remote_tcp.sin_addr, sizeof(in_addr_t));
67 ban->reason = strdup(reason);
68 ban->name = strdup(client->username);
69 ban->time = time(NULL);
70 ban->duration = ban_duration;
71 Timer_init(&ban->startTime);
72 list_add_tail(&ban->node, &banlist);
75 SSLi_hash2hex(ban->hash, hexhash);
76 Log_info_client(client, "User kickbanned. Reason: '%s' Hash: %s IP: %s Banned for: %d seconds",
77 ban->name, ban->reason, hexhash, inet_ntoa(*((struct in_addr *)&ban->address)),
82 void Ban_pruneBanned()
87 uint64_t bantime_long;
89 list_iterate(itr, &banlist) {
90 ban = list_get_entry(itr, ban_t, node);
91 bantime_long = ban->duration * 1000000LL;
93 SSLi_hash2hex(ban->hash, hexhash);
94 Log_debug("BL: User %s Reason: '%s' Hash: %s IP: %s Time left: %d",
95 ban->name, ban->reason, hexhash, inet_ntoa(*((struct in_addr *)&ban->address)),
96 bantime_long / 1000000LL - Timer_elapsed(&ban->startTime) / 1000000LL);
98 /* Duration of 0 = forever */
99 if (ban->duration != 0 && Timer_isElapsed(&ban->startTime, bantime_long)) {
102 list_del(&ban->node);
109 bool_t Ban_isBanned(client_t *client)
113 list_iterate(itr, &banlist) {
114 ban = list_get_entry(itr, ban_t, node);
115 if (memcmp(ban->hash, client->hash, 20) == 0)
122 bool_t Ban_isBannedAddr(in_addr_t *addr)
127 in_addr_t tempaddr1, tempaddr2;
129 list_iterate(itr, &banlist) {
130 ban = list_get_entry(itr, ban_t, node);
131 mask = ban->mask - 96;
132 if (mask < 32) { /* XXX - only ipv4 support */
133 memcpy(&tempaddr1, addr, sizeof(in_addr_t));
134 memcpy(&tempaddr2, &ban->address, sizeof(in_addr_t));
135 tempaddr1 &= (2 ^ mask) - 1;
136 tempaddr2 &= (2 ^ mask) - 1;
138 if (memcmp(&tempaddr1, &tempaddr2, sizeof(in_addr_t)) == 0)
144 int Ban_getBanCount(void)
149 message_t *Ban_getBanList(void)
160 msg = Msg_banList_create(bancount);
161 list_iterate(itr, &banlist) {
162 ban = list_get_entry(itr, ban_t, node);
163 gmtime_r(&ban->time, ×pec);
164 strftime(timestr, 32, "%Y-%m-%dT%H:%M:%S", ×pec);
165 SSLi_hash2hex(ban->hash, hexhash);
166 /* ipv4 representation as ipv6 address. */
167 memset(address, 0, 16);
168 memcpy(&address[12], &ban->address, 4);
169 memset(&address[10], 0xff, 2); /* IPv4 */
170 Msg_banList_addEntry(msg, i++, address, ban->mask, ban->name,
171 hexhash, ban->reason, timestr, ban->duration);
176 void Ban_clearBanList()
179 struct dlist *itr, *save;
180 list_iterate_safe(itr, save, &banlist) {
181 ban = list_get_entry(itr, ban_t, node);
184 list_del(&ban->node);
190 void Ban_putBanList(message_t *msg, int n_bans)
195 char *hexhash, *name, *reason, *start;
196 uint32_t duration, mask;
199 for (i = 0; i < n_bans; i++) {
200 Msg_banList_getEntry(msg, i, &address, &mask, &name, &hexhash, &reason, &start, &duration);
201 ban = malloc(sizeof(ban_t));
203 Log_fatal("Out of memory");
204 memset(ban, 0, sizeof(ban_t));
205 SSLi_hex2hash(hexhash, ban->hash);
206 memcpy(&ban->address, &address[12], 4);
208 ban->reason = strdup(reason);
209 ban->name = strdup(name);
210 strptime(start, "%Y-%m-%dT%H:%M:%S", ×pec);
211 ban->time = mktime(×pec);
212 Timer_init(&ban->startTime);
213 ban->duration = duration;
214 list_add_tail(&ban->node, &banlist);