]> jfr.im git - solanum.git/blob - modules/core/m_ban.c
Add /stats g to show propagated (global) klines.
[solanum.git] / modules / core / m_ban.c
1 /*
2 * charybdis: An advanced ircd.
3 * m_ban.c: Propagates network bans across servers.
4 *
5 * Copyright (C) 2010 Jilles Tjoelker
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * 1.Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * 2.Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "stdinc.h"
31 #include "send.h"
32 #include "client.h"
33 #include "common.h"
34 #include "config.h"
35 #include "ircd.h"
36 #include "match.h"
37 #include "s_conf.h"
38 #include "msg.h"
39 #include "modules.h"
40 #include "hash.h"
41 #include "s_serv.h"
42 #include "operhash.h"
43 #include "reject.h"
44 #include "hostmask.h"
45
46 static int ms_ban(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
47
48 struct Message ban_msgtab = {
49 "BAN", 0, 0, 0, MFLG_SLOW,
50 {mg_unreg, mg_ignore, {ms_ban, 9}, {ms_ban, 9}, mg_ignore, mg_ignore}
51 };
52
53 mapi_clist_av1 ban_clist[] = { &ban_msgtab, NULL };
54 DECLARE_MODULE_AV1(ban, NULL, NULL, ban_clist, NULL, NULL, "$Revision: 1349 $");
55
56 /* ms_ban()
57 *
58 * parv[1] - type
59 * parv[2] - username mask or *
60 * parv[3] - hostname mask
61 * parv[4] - creation TS
62 * parv[5] - duration (relative to creation)
63 * parv[6] - lifetime (relative to creation)
64 * parv[7] - oper or *
65 * parv[8] - reason (possibly with |operreason)
66 */
67 static int
68 ms_ban(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
69 {
70 rb_dlink_node *ptr;
71 struct ConfItem *aconf;
72 unsigned int ntype;
73 const char *oper, *stype;
74 time_t created, hold, lifetime;
75 char *p;
76 int act;
77
78 if (strlen(parv[1]) != 1)
79 {
80 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
81 "Unknown BAN type %s from %s",
82 parv[1], source_p->name);
83 return 0;
84 }
85 switch (parv[1][0])
86 {
87 case 'K':
88 ntype = CONF_KILL;
89 stype = "K-Line";
90 break;
91 default:
92 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
93 "Unknown BAN type %s from %s",
94 parv[1], source_p->name);
95 return 0;
96 }
97 created = atol(parv[4]);
98 hold = created + atoi(parv[5]);
99 lifetime = created + atoi(parv[6]);
100 if (!strcmp(parv[7], "*"))
101 oper = IsServer(source_p) ? source_p->name : get_oper_name(source_p);
102 else
103 oper = parv[7];
104 ptr = find_prop_ban(ntype, parv[2], parv[3]);
105 if (ptr != NULL)
106 {
107 aconf = ptr->data;
108 if (aconf->created > created ||
109 (aconf->created == created &&
110 aconf->lifetime >= lifetime))
111 {
112 if (IsPerson(source_p))
113 sendto_one_notice(source_p,
114 ":Your %s [%s%s%s] has been superseded",
115 stype,
116 aconf->user ? aconf->user : "",
117 aconf->user ? "@" : "",
118 aconf->host);
119 return 0;
120 }
121 act = !(aconf->status & CONF_ILLEGAL) || (hold != created &&
122 hold > rb_current_time());
123 if (lifetime > aconf->lifetime)
124 aconf->lifetime = lifetime;
125 /* already expired, hmm */
126 if (aconf->lifetime <= rb_current_time())
127 return 0;
128 deactivate_conf(aconf, ptr);
129 rb_free(aconf->user);
130 aconf->user = NULL;
131 rb_free(aconf->host);
132 aconf->host = NULL;
133 operhash_delete(aconf->info.oper);
134 aconf->info.oper = NULL;
135 rb_free(aconf->passwd);
136 aconf->passwd = NULL;
137 rb_free(aconf->spasswd);
138 aconf->spasswd = NULL;
139 }
140 else
141 {
142 aconf = make_conf();
143 aconf->status = CONF_ILLEGAL | ntype;
144 aconf->lifetime = lifetime;
145 rb_dlinkAddAlloc(aconf, &prop_bans);
146 act = hold != created && hold > rb_current_time();
147 }
148 aconf->flags &= ~CONF_FLAGS_MYOPER;
149 aconf->flags |= CONF_FLAGS_TEMPORARY;
150 aconf->user = ntype == CONF_KILL ? rb_strdup(parv[2]) : NULL;
151 aconf->host = rb_strdup(parv[3]);
152 aconf->info.oper = operhash_add(oper);
153 aconf->created = created;
154 aconf->hold = hold;
155 p = strchr(parv[parc - 1], '|');
156 if (p == NULL)
157 aconf->passwd = rb_strdup(parv[parc - 1]);
158 else
159 {
160 aconf->passwd = rb_strndup(parv[parc - 1], p - parv[parc - 1] + 1);
161 aconf->spasswd = rb_strdup(p + 1);
162 }
163 if (act && hold != created)
164 {
165 /* Keep the notices in sync with modules/m_kline.c etc. */
166 sendto_realops_snomask(SNO_GENERAL, L_ALL,
167 "%s added global %d min. %s%s%s for [%s%s%s] [%s]",
168 IsServer(source_p) ? source_p->name : get_oper_name(source_p),
169 (hold - rb_current_time()) / 60,
170 stype,
171 strcmp(parv[7], "*") ? " from " : "",
172 strcmp(parv[7], "*") ? parv[7] : "",
173 aconf->user ? aconf->user : "",
174 aconf->user ? "@" : "",
175 aconf->host,
176 parv[parc - 1]);
177 ilog(L_KLINE, "%s %s %d %s %s %s", parv[1],
178 IsServer(source_p) ? source_p->name : get_oper_name(source_p),
179 (hold - rb_current_time()) / 60,
180 aconf->user, aconf->host,
181 parv[parc - 1]);
182 aconf->status &= ~CONF_ILLEGAL;
183 }
184 else if (act)
185 {
186 sendto_realops_snomask(SNO_GENERAL, L_ALL,
187 "%s has removed the global %s for: [%s%s%s]%s%s",
188 IsServer(source_p) ? source_p->name : get_oper_name(source_p),
189 stype,
190 aconf->user ? aconf->user : "",
191 aconf->user ? "@" : "",
192 aconf->host,
193 strcmp(parv[7], "*") ? " on behalf of " : "",
194 strcmp(parv[7], "*") ? parv[7] : "");
195 ilog(L_KLINE, "U%s %s %s %s", parv[1],
196 IsServer(source_p) ? source_p->name : get_oper_name(source_p),
197 aconf->user, aconf->host);
198 }
199 switch (ntype)
200 {
201 case CONF_KILL:
202 if (aconf->status & CONF_ILLEGAL)
203 remove_reject_mask(aconf->user, aconf->host);
204 else
205 {
206 add_conf_by_address(aconf->host, CONF_KILL, aconf->user, NULL, aconf);
207 if(ConfigFileEntry.kline_delay ||
208 (IsServer(source_p) &&
209 !HasSentEob(source_p)))
210 {
211 if(kline_queued == 0)
212 {
213 rb_event_addonce("check_klines", check_klines_event, NULL,
214 ConfigFileEntry.kline_delay);
215 kline_queued = 1;
216 }
217 }
218 else
219 check_klines();
220 }
221 break;
222 }
223 sendto_server(client_p, NULL, CAP_BAN|CAP_TS6, NOCAPS,
224 ":%s BAN %s %s %s %s %s %s %s :%s",
225 source_p->id,
226 parv[1],
227 parv[2],
228 parv[3],
229 parv[4],
230 parv[5],
231 parv[6],
232 parv[7],
233 parv[parc - 1]);
234 return 0;
235 }