]> jfr.im git - solanum.git/blob - modules/m_testline.c
Document /testkline
[solanum.git] / modules / m_testline.c
1 /* modules/m_testline.c
2 *
3 * Copyright (C) 2004 Lee Hardy <lee@leeh.co.uk>
4 * Copyright (C) 2004-2005 ircd-ratbox development team
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * 1.Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * 2.Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3.The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30 #include "stdinc.h"
31 #include "send.h"
32 #include "client.h"
33 #include "modules.h"
34 #include "msg.h"
35 #include "hash.h"
36 #include "hostmask.h"
37 #include "numeric.h"
38 #include "s_conf.h"
39 #include "s_newconf.h"
40 #include "reject.h"
41
42 static const char testline_desc[] = "Provides the ability to test I/K/D/X lines and RESVs";
43
44 static void mo_testline(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
45 static void mo_testkline(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
46 static void mo_testgecos(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
47
48 struct Message testline_msgtab = {
49 "TESTLINE", 0, 0, 0, 0,
50 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_testline, 2}}
51 };
52 struct Message testkline_msgtab = {
53 "TESTKLINE", 0, 0, 0, 0,
54 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_testkline, 2}}
55 };
56 struct Message testgecos_msgtab = {
57 "TESTGECOS", 0, 0, 0, 0,
58 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_testgecos, 2}}
59 };
60
61 mapi_clist_av1 testline_clist[] = { &testline_msgtab, &testkline_msgtab, &testgecos_msgtab, NULL };
62
63 DECLARE_MODULE_AV2(testline, NULL, NULL, testline_clist, NULL, NULL, NULL, NULL, testline_desc);
64
65 static void
66 mo_testline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
67 {
68 struct ConfItem *aconf;
69 struct ConfItem *resv_p;
70 struct rb_sockaddr_storage ip;
71 char user_trunc[USERLEN + 1], notildeuser_trunc[USERLEN + 1];
72 const char *name = NULL;
73 const char *username = NULL;
74 const char *host = NULL;
75 char *mask;
76 char *p;
77 int host_mask;
78 int type;
79 int duration;
80 char *puser, *phost, *reason, *operreason;
81 char reasonbuf[BUFSIZE];
82
83 if (!HasPrivilege(source_p, "oper:testline"))
84 {
85 sendto_one(source_p, form_str(ERR_NOPRIVS),
86 me.name, source_p->name, "testline");
87 return;
88 }
89
90 mask = LOCAL_COPY(parv[1]);
91
92 if (IsChannelName(mask))
93 {
94 resv_p = hash_find_resv(mask);
95 if (resv_p != NULL)
96 {
97 sendto_one(source_p, form_str(RPL_TESTLINE),
98 me.name, source_p->name,
99 resv_p->hold ? 'q' : 'Q',
100 resv_p->hold ? (long) ((resv_p->hold - rb_current_time()) / 60) : 0L,
101 resv_p->host, resv_p->passwd);
102 /* this is a false positive, so make sure it isn't counted in stats q
103 * --nenolod
104 */
105 resv_p->port--;
106 }
107 else
108 sendto_one(source_p, form_str(RPL_NOTESTLINE),
109 me.name, source_p->name, parv[1]);
110 return;
111 }
112
113 if((p = strchr(mask, '!')))
114 {
115 *p++ = '\0';
116 name = mask;
117 mask = p;
118
119 if(EmptyString(mask))
120 return;
121 }
122
123 if((p = strchr(mask, '@')))
124 {
125 *p++ = '\0';
126 username = mask;
127 host = p;
128
129 if(EmptyString(host))
130 return;
131 }
132 else
133 host = mask;
134
135 /* parses as an IP, check for a dline */
136 if((type = parse_netmask(host, &ip, &host_mask)) != HM_HOST)
137 {
138 if(type == HM_IPV6)
139 aconf = find_dline((struct sockaddr *)&ip, AF_INET6);
140 else
141 aconf = find_dline((struct sockaddr *)&ip, AF_INET);
142
143 if(aconf && aconf->status & CONF_DLINE)
144 {
145 get_printable_kline(source_p, aconf, &phost, &reason, &puser, &operreason);
146 snprintf(reasonbuf, sizeof(reasonbuf), "%s%s%s", reason,
147 operreason ? "|" : "", operreason ? operreason : "");
148 sendto_one(source_p, form_str(RPL_TESTLINE),
149 me.name, source_p->name,
150 (aconf->flags & CONF_FLAGS_TEMPORARY) ? 'd' : 'D',
151 (aconf->flags & CONF_FLAGS_TEMPORARY) ?
152 (long) ((aconf->hold - rb_current_time()) / 60) : 0L,
153 phost, reasonbuf);
154
155 return;
156 }
157 /* Otherwise, aconf is an exempt{} */
158 if(aconf == NULL &&
159 (duration = is_reject_ip((struct sockaddr *)&ip)))
160 sendto_one(source_p, form_str(RPL_TESTLINE),
161 me.name, source_p->name,
162 '!',
163 duration / 60L,
164 host, "Reject cache");
165 if(aconf == NULL &&
166 (duration = is_throttle_ip((struct sockaddr *)&ip)))
167 sendto_one(source_p, form_str(RPL_TESTLINE),
168 me.name, source_p->name,
169 '!',
170 duration / 60L,
171 host, "Throttled");
172 }
173
174 if (username != NULL)
175 {
176 rb_strlcpy(user_trunc, username, sizeof user_trunc);
177 rb_strlcpy(notildeuser_trunc, *username == '~' ? username + 1 : username, sizeof notildeuser_trunc);
178 }
179 else
180 {
181 rb_strlcpy(user_trunc, "dummy", sizeof user_trunc);
182 rb_strlcpy(notildeuser_trunc, "dummy", sizeof notildeuser_trunc);
183 }
184 /* now look for a matching I/K/G */
185 if((aconf = find_address_conf(host, NULL, user_trunc, notildeuser_trunc,
186 (type != HM_HOST) ? (struct sockaddr *)&ip : NULL,
187 (type != HM_HOST) ? (
188 (type == HM_IPV6) ? AF_INET6 :
189 AF_INET) : 0, NULL)))
190 {
191 static char buf[HOSTLEN+USERLEN+2];
192
193 if(aconf->status & CONF_KILL)
194 {
195 get_printable_kline(source_p, aconf, &phost, &reason, &puser, &operreason);
196 snprintf(buf, sizeof(buf), "%s@%s",
197 puser, phost);
198 snprintf(reasonbuf, sizeof(reasonbuf), "%s%s%s", reason,
199 operreason ? "|" : "", operreason ? operreason : "");
200 sendto_one(source_p, form_str(RPL_TESTLINE),
201 me.name, source_p->name,
202 (aconf->flags & CONF_FLAGS_TEMPORARY) ? 'k' : 'K',
203 (aconf->flags & CONF_FLAGS_TEMPORARY) ?
204 (long) ((aconf->hold - rb_current_time()) / 60) : 0L,
205 buf, reasonbuf);
206 return;
207 }
208 }
209
210 /* they asked us to check a nick, so hunt for resvs.. */
211 if(name && (resv_p = find_nick_resv(name)))
212 {
213 sendto_one(source_p, form_str(RPL_TESTLINE),
214 me.name, source_p->name,
215 resv_p->hold ? 'q' : 'Q',
216 resv_p->hold ? (long) ((resv_p->hold - rb_current_time()) / 60) : 0L,
217 resv_p->host, resv_p->passwd);
218
219 /* this is a false positive, so make sure it isn't counted in stats q
220 * --nenolod
221 */
222 resv_p->port--;
223 return;
224 }
225
226 /* no matching resv, we can print the I: if it exists */
227 if(aconf && aconf->status & CONF_CLIENT)
228 {
229 sendto_one_numeric(source_p, RPL_STATSILINE, form_str(RPL_STATSILINE),
230 aconf->info.name, EmptyString(aconf->spasswd) ? "<NULL>" : aconf->spasswd,
231 show_iline_prefix(source_p, aconf, aconf->user),
232 aconf->host, aconf->port, aconf->className);
233 return;
234 }
235
236 /* nothing matches.. */
237 sendto_one(source_p, form_str(RPL_NOTESTLINE),
238 me.name, source_p->name, parv[1]);
239 }
240
241
242 static void
243 mo_testkline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
244 {
245 struct ConfItem *aconf;
246 struct rb_sockaddr_storage ip;
247 char user_trunc[USERLEN + 1];
248 const char *username = NULL;
249 const char *host = NULL;
250 char *mask;
251 char *p;
252 int host_mask;
253 int type;
254 int duration;
255 char *puser, *phost, *reason, *operreason;
256 char reasonbuf[BUFSIZE];
257
258 if (!HasPrivilege(source_p, "oper:testline"))
259 {
260 sendto_one(source_p, form_str(ERR_NOPRIVS),
261 me.name, source_p->name, "testline");
262 return;
263 }
264
265 mask = LOCAL_COPY(parv[1]);
266
267 if ((p = strchr(mask, '!')))
268 {
269 mask = p + 1;
270
271 if(EmptyString(mask))
272 return;
273 }
274
275 if ((p = strchr(mask, '@')))
276 {
277 *p++ = '\0';
278 username = mask;
279 host = p;
280
281 if(EmptyString(host))
282 return;
283 }
284 else
285 {
286 host = mask;
287 }
288
289 /* parses as an IP, check for a dline */
290 if ((type = parse_netmask(host, &ip, &host_mask)) != HM_HOST)
291 {
292 if(type == HM_IPV6)
293 aconf = find_dline((struct sockaddr *)&ip, AF_INET6);
294 else
295 aconf = find_dline((struct sockaddr *)&ip, AF_INET);
296
297 if(aconf && aconf->status & CONF_DLINE)
298 {
299 get_printable_kline(source_p, aconf, &phost, &reason, &puser, &operreason);
300 snprintf(reasonbuf, sizeof(reasonbuf), "%s%s%s", reason,
301 operreason ? "|" : "", operreason ? operreason : "");
302 sendto_one(source_p, form_str(RPL_TESTLINE),
303 me.name, source_p->name,
304 (aconf->flags & CONF_FLAGS_TEMPORARY) ? 'd' : 'D',
305 (aconf->flags & CONF_FLAGS_TEMPORARY) ?
306 (long) ((aconf->hold - rb_current_time()) / 60) : 0L,
307 phost, reasonbuf);
308
309 return;
310 }
311 /* Otherwise, aconf is an exempt{} */
312 if (aconf == NULL && (duration = is_reject_ip((struct sockaddr *)&ip)))
313 sendto_one(source_p, form_str(RPL_TESTLINE),
314 me.name, source_p->name,
315 '!',
316 duration / 60L,
317 host, "Reject cache");
318 if (aconf == NULL && (duration = is_throttle_ip((struct sockaddr *)&ip)))
319 sendto_one(source_p, form_str(RPL_TESTLINE),
320 me.name, source_p->name,
321 '!',
322 duration / 60L,
323 host, "Throttled");
324 }
325
326 if (username != NULL)
327 rb_strlcpy(user_trunc, username, sizeof user_trunc);
328 else
329 rb_strlcpy(user_trunc, "dummy", sizeof user_trunc);
330
331 aconf = find_conf_by_address(host,
332 type != HM_HOST ? host : NULL,
333 NULL,
334 type != HM_HOST ? (struct sockaddr *)&ip : NULL,
335 CONF_KILL,
336 type == HM_IPV6 ? AF_INET6 : AF_INET,
337 user_trunc, NULL);
338
339 if (aconf != NULL && aconf->status & CONF_KILL)
340 {
341 static char buf[HOSTLEN+USERLEN+2];
342
343 get_printable_kline(source_p, aconf, &phost, &reason, &puser, &operreason);
344 snprintf(buf, sizeof(buf), "%s@%s",
345 puser, phost);
346 snprintf(reasonbuf, sizeof(reasonbuf), "%s%s%s", reason,
347 operreason ? "|" : "", operreason ? operreason : "");
348 sendto_one(source_p, form_str(RPL_TESTLINE),
349 me.name, source_p->name,
350 (aconf->flags & CONF_FLAGS_TEMPORARY) ? 'k' : 'K',
351 (aconf->flags & CONF_FLAGS_TEMPORARY) ?
352 (long) ((aconf->hold - rb_current_time()) / 60) : 0L,
353 buf, reasonbuf);
354 return;
355 }
356
357 sendto_one(source_p, form_str(RPL_NOTESTLINE),
358 me.name, source_p->name, parv[1]);
359 }
360
361 static void
362 mo_testgecos(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
363 {
364 struct ConfItem *aconf;
365
366 if (!HasPrivilege(source_p, "oper:testline"))
367 {
368 sendto_one(source_p, form_str(ERR_NOPRIVS),
369 me.name, source_p->name, "testline");
370 return;
371 }
372
373 if(!(aconf = find_xline(parv[1], 0)))
374 {
375 sendto_one(source_p, form_str(RPL_NOTESTLINE),
376 me.name, source_p->name, parv[1]);
377 return;
378 }
379
380 sendto_one(source_p, form_str(RPL_TESTLINE),
381 me.name, source_p->name,
382 aconf->hold ? 'x' : 'X',
383 aconf->hold ? (long) ((aconf->hold - rb_current_time()) / 60) : 0L,
384 aconf->host, aconf->passwd);
385 }