Some cleanup.
[umurmur.git] / src / ban.c
1 /* Copyright (C) 2009-2011, Martin Johansson <martin@fatbob.nu>
2    Copyright (C) 2005-2011, 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 "log.h"
34 #include "list.h"
35 #include "ban.h"
36 #include "conf.h"
37 #include "ssl.h"
38
39 declare_list(banlist);
40 static int bancount; /* = 0 */
41
42 void Ban_UserBan(client_t *client, char *reason)
43 {
44         ban_t *ban;
45         char hexhash[41];
46
47         ban = malloc(sizeof(ban_t));
48         memcpy(ban->hash, client->hash, 20);
49         memcpy(&ban->address, &client->remote_tcp.sin_addr, sizeof(in_addr_t));
50         ban->reason = strdup(reason);
51         ban->name = strdup(client->username);
52         Timer_init(&ban->startTime);
53         list_add_tail(&ban->node, &banlist);
54         
55         SSLi_hash2hex(ban->hash, hexhash);
56         Log_info("User %s kickbanned. Reason: '%s' Hash: %s IP: %s Banned for: %d seconds",
57                  ban->name, ban->reason, hexhash, inet_ntoa(*((struct in_addr *)&ban->address)),
58                  getIntConf(BAN_LENGTH));
59 }
60
61
62 void Ban_pruneBanned()
63 {
64         struct dlist *itr;
65         static int64_t bantime = 0;
66         ban_t *ban;
67         char hexhash[41];
68         
69         if (bantime == 0) {
70                 bantime = getIntConf(BAN_LENGTH) * 1000000LL;
71         }
72         
73         list_iterate(itr, &banlist) {
74                 ban = list_get_entry(itr, ban_t, node);
75 #ifdef DEBUG
76                 SSLi_hash2hex(ban->hash, hexhash);
77                 Log_debug("BL: User %s Reason: '%s' Hash: %s IP: %s Time left: %d",
78                           ban->name, ban->reason, hexhash, inet_ntoa(*((struct in_addr *)&ban->address)),
79                           bantime / 1000000LL - Timer_elapsed(&ban->startTime) / 1000000LL);
80 #endif
81                 if (Timer_isElapsed(&ban->startTime, bantime)) {
82                         free(ban->name);
83                         free(ban->reason);
84                         list_del(&ban->node);
85                         free(ban);
86                 }
87         }
88 }
89
90 bool_t Ban_isBanned(client_t *client)
91 {
92         struct dlist *itr;
93         ban_t *ban;
94         list_iterate(itr, &banlist) {
95                 ban = list_get_entry(itr, ban_t, node);
96                 if (memcmp(ban->hash, client->hash, 20) == 0) 
97                         return true;
98         }
99         return false;
100         
101 }
102
103 bool_t Ban_isBannedAddr(in_addr_t *addr)
104 {
105         struct dlist *itr;
106         ban_t *ban;
107         list_iterate(itr, &banlist) {
108                 ban = list_get_entry(itr, ban_t, node);
109                 if (memcmp(&ban->address, addr, sizeof(in_addr_t)) == 0) 
110                         return true;
111         }
112         return false;
113 }
114