Add banlist message handling. Bans can now be edited, added and removed via the banli...
[umurmur.git] / src / ban.c
1 /* Copyright (C) 2009-2012, Martin Johansson <martin@fatbob.nu>
2    Copyright (C) 2005-2012, Thorvald Natvig <thorvald@natvig.com>
3
4    All rights reserved.
5
6    Redistribution and use in source and binary forms, with or without
7    modification, are permitted provided that the following conditions
8    are met:
9
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.
18
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.
30 */
31
32 #include <stdlib.h>
33 #include <time.h>
34 #include "log.h"
35 #include "list.h"
36 #include "ban.h"
37 #include "conf.h"
38 #include "ssl.h"
39
40 declare_list(banlist);
41 static int bancount; /* = 0 */
42 static int ban_duration;
43
44 void Ban_init(void)
45 {
46         ban_duration = getIntConf(BAN_LENGTH);
47         /* Read ban file here */
48 }
49
50 void Ban_deinit(void)
51 {
52         /* Save banlist */
53 }
54 void Ban_UserBan(client_t *client, char *reason)
55 {
56         ban_t *ban;
57         char hexhash[41];
58
59         ban = malloc(sizeof(ban_t));
60         if (ban == NULL)
61                 Log_fatal("Out of memory");
62         memset(ban, 0, sizeof(ban_t));
63         
64         memcpy(ban->hash, client->hash, 20);
65         memcpy(&ban->address, &client->remote_tcp.sin_addr, sizeof(in_addr_t));
66         ban->mask = 128;
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);
73         bancount++;
74         
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)),
78                         ban->duration);
79 }
80
81
82 void Ban_pruneBanned()
83 {
84         struct dlist *itr;
85         ban_t *ban;
86         char hexhash[41];
87         uint64_t bantime_long;
88                 
89         list_iterate(itr, &banlist) {
90                 ban = list_get_entry(itr, ban_t, node);
91                 bantime_long = ban->duration * 1000000LL;
92 #ifdef DEBUG
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);
97 #endif
98                 /* Duration of 0 = forever */
99                 if (ban->duration != 0 && Timer_isElapsed(&ban->startTime, bantime_long)) {
100                         free(ban->name);
101                         free(ban->reason);
102                         list_del(&ban->node);
103                         free(ban);
104                         bancount--;
105                 }
106         }
107 }
108
109 bool_t Ban_isBanned(client_t *client)
110 {
111         struct dlist *itr;
112         ban_t *ban;
113         list_iterate(itr, &banlist) {
114                 ban = list_get_entry(itr, ban_t, node);
115                 if (memcmp(ban->hash, client->hash, 20) == 0) 
116                         return true;
117         }
118         return false;
119         
120 }
121
122 bool_t Ban_isBannedAddr(in_addr_t *addr)
123 {
124         struct dlist *itr;
125         ban_t *ban;
126         int mask;
127         in_addr_t tempaddr1, tempaddr2;
128         
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;
137                 }
138                 if (memcmp(&tempaddr1, &tempaddr2, sizeof(in_addr_t)) == 0) 
139                         return true;
140         }
141         return false;
142 }
143
144 int Ban_getBanCount(void)
145 {
146         return bancount;
147 }
148
149 message_t *Ban_getBanList(void)
150 {
151         int i = 0;
152         struct dlist *itr;
153         ban_t *ban;
154         message_t *msg;
155         struct tm timespec;
156         char timestr[32];
157         char hexhash[41];
158         uint8_t address[16];
159         
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, &timespec);
164                 strftime(timestr, 32, "%Y-%m-%dT%H:%M:%S", &timespec);
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);
172         }
173         return msg;
174 }
175
176 void Ban_clearBanList()
177 {
178         ban_t *ban;
179         struct dlist *itr, *save;
180         list_iterate_safe(itr, save, &banlist) {
181                 ban = list_get_entry(itr, ban_t, node);
182                 free(ban->name);
183                 free(ban->reason);
184                 list_del(&ban->node);
185                 free(ban);
186                 bancount--;
187         }
188 }
189
190 void Ban_putBanList(message_t *msg, int n_bans)
191 {
192         int i = 0;
193         struct tm timespec;
194         ban_t *ban;
195         char *hexhash, *name, *reason, *start;
196         uint32_t duration, mask;
197         uint8_t *address;
198         
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));
202                 if (ban == NULL)
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);
207                 ban->mask = mask;
208                 ban->reason = strdup(reason);
209                 ban->name = strdup(name);
210                 strptime(start, "%Y-%m-%dT%H:%M:%S", &timespec);
211                 ban->time = mktime(&timespec);
212                 Timer_init(&ban->startTime);
213                 ban->duration = duration;
214                 list_add_tail(&ban->node, &banlist);
215                 bancount++;
216         }
217 }