]>
Commit | Line | Data |
---|---|---|
212380e3 | 1 | /* modules/m_testline.c |
55abcbb2 | 2 | * |
212380e3 AC |
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. | |
212380e3 AC |
29 | */ |
30 | #include "stdinc.h" | |
212380e3 AC |
31 | #include "send.h" |
32 | #include "client.h" | |
33 | #include "modules.h" | |
34 | #include "msg.h" | |
add9f99d | 35 | #include "hash.h" |
212380e3 AC |
36 | #include "hostmask.h" |
37 | #include "numeric.h" | |
38 | #include "s_conf.h" | |
39 | #include "s_newconf.h" | |
83235e9e | 40 | #include "reject.h" |
212380e3 | 41 | |
eeabf33a EM |
42 | static const char testline_desc[] = "Provides the ability to test I/K/D/X lines and RESVs"; |
43 | ||
3c7d6fcc | 44 | static void mo_testline(struct MsgBuf *, struct Client *, struct Client *, int, const char **); |
4dc6ff3d | 45 | static void mo_testkline(struct MsgBuf *, struct Client *, struct Client *, int, const char **); |
3c7d6fcc | 46 | static void mo_testgecos(struct MsgBuf *, struct Client *, struct Client *, int, const char **); |
212380e3 AC |
47 | |
48 | struct Message testline_msgtab = { | |
7baa37a9 | 49 | "TESTLINE", 0, 0, 0, 0, |
96bfafc1 | 50 | {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_testline, 2}} |
212380e3 | 51 | }; |
4dc6ff3d EK |
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 | }; | |
212380e3 | 56 | struct Message testgecos_msgtab = { |
7baa37a9 | 57 | "TESTGECOS", 0, 0, 0, 0, |
96bfafc1 | 58 | {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_testgecos, 2}} |
212380e3 AC |
59 | }; |
60 | ||
4dc6ff3d | 61 | mapi_clist_av1 testline_clist[] = { &testline_msgtab, &testkline_msgtab, &testgecos_msgtab, NULL }; |
eeabf33a | 62 | |
be9c3979 | 63 | DECLARE_MODULE_AV2(testline, NULL, NULL, testline_clist, NULL, NULL, NULL, NULL, testline_desc); |
212380e3 | 64 | |
3c7d6fcc | 65 | static void |
428ca87b | 66 | mo_testline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) |
212380e3 AC |
67 | { |
68 | struct ConfItem *aconf; | |
69 | struct ConfItem *resv_p; | |
e7046ee5 | 70 | struct rb_sockaddr_storage ip; |
212380e3 AC |
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; | |
83235e9e | 79 | int duration; |
e5b12a61 JT |
80 | char *puser, *phost, *reason, *operreason; |
81 | char reasonbuf[BUFSIZE]; | |
212380e3 | 82 | |
58a490f9 EK |
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 | ||
212380e3 AC |
90 | mask = LOCAL_COPY(parv[1]); |
91 | ||
add9f99d JT |
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', | |
e3354945 | 100 | resv_p->hold ? (long) ((resv_p->hold - rb_current_time()) / 60) : 0L, |
70ea02eb | 101 | resv_p->host, resv_p->passwd); |
add9f99d JT |
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]); | |
3c7d6fcc | 110 | return; |
add9f99d JT |
111 | } |
112 | ||
212380e3 AC |
113 | if((p = strchr(mask, '!'))) |
114 | { | |
115 | *p++ = '\0'; | |
116 | name = mask; | |
117 | mask = p; | |
118 | ||
119 | if(EmptyString(mask)) | |
3dcaa851 EK |
120 | { |
121 | sendto_one_notice(source_p, "Invalid syntax for TESTLINE"); | |
3c7d6fcc | 122 | return; |
3dcaa851 | 123 | } |
212380e3 AC |
124 | } |
125 | ||
126 | if((p = strchr(mask, '@'))) | |
127 | { | |
128 | *p++ = '\0'; | |
129 | username = mask; | |
130 | host = p; | |
131 | ||
132 | if(EmptyString(host)) | |
3dcaa851 EK |
133 | { |
134 | sendto_one_notice(source_p, "Invalid syntax for TESTLINE"); | |
3c7d6fcc | 135 | return; |
3dcaa851 | 136 | } |
212380e3 AC |
137 | } |
138 | else | |
139 | host = mask; | |
140 | ||
141 | /* parses as an IP, check for a dline */ | |
29c92cf9 | 142 | if((type = parse_netmask(host, &ip, &host_mask)) != HM_HOST) |
212380e3 | 143 | { |
54ac8b60 VY |
144 | if(type == HM_IPV6) |
145 | aconf = find_dline((struct sockaddr *)&ip, AF_INET6); | |
146 | else | |
54ac8b60 | 147 | aconf = find_dline((struct sockaddr *)&ip, AF_INET); |
212380e3 AC |
148 | |
149 | if(aconf && aconf->status & CONF_DLINE) | |
150 | { | |
e5b12a61 | 151 | get_printable_kline(source_p, aconf, &phost, &reason, &puser, &operreason); |
5203cba5 | 152 | snprintf(reasonbuf, sizeof(reasonbuf), "%s%s%s", reason, |
e5b12a61 | 153 | operreason ? "|" : "", operreason ? operreason : ""); |
212380e3 AC |
154 | sendto_one(source_p, form_str(RPL_TESTLINE), |
155 | me.name, source_p->name, | |
156 | (aconf->flags & CONF_FLAGS_TEMPORARY) ? 'd' : 'D', | |
55abcbb2 KB |
157 | (aconf->flags & CONF_FLAGS_TEMPORARY) ? |
158 | (long) ((aconf->hold - rb_current_time()) / 60) : 0L, | |
e5b12a61 | 159 | phost, reasonbuf); |
212380e3 | 160 | |
3c7d6fcc | 161 | return; |
212380e3 | 162 | } |
83235e9e JT |
163 | /* Otherwise, aconf is an exempt{} */ |
164 | if(aconf == NULL && | |
165 | (duration = is_reject_ip((struct sockaddr *)&ip))) | |
166 | sendto_one(source_p, form_str(RPL_TESTLINE), | |
167 | me.name, source_p->name, | |
168 | '!', | |
0cce01d3 | 169 | duration / 60L, |
83235e9e JT |
170 | host, "Reject cache"); |
171 | if(aconf == NULL && | |
172 | (duration = is_throttle_ip((struct sockaddr *)&ip))) | |
173 | sendto_one(source_p, form_str(RPL_TESTLINE), | |
174 | me.name, source_p->name, | |
175 | '!', | |
0cce01d3 | 176 | duration / 60L, |
83235e9e | 177 | host, "Throttled"); |
212380e3 AC |
178 | } |
179 | ||
180 | if (username != NULL) | |
181 | { | |
f427c8b0 VY |
182 | rb_strlcpy(user_trunc, username, sizeof user_trunc); |
183 | rb_strlcpy(notildeuser_trunc, *username == '~' ? username + 1 : username, sizeof notildeuser_trunc); | |
212380e3 AC |
184 | } |
185 | else | |
186 | { | |
f427c8b0 VY |
187 | rb_strlcpy(user_trunc, "dummy", sizeof user_trunc); |
188 | rb_strlcpy(notildeuser_trunc, "dummy", sizeof notildeuser_trunc); | |
212380e3 AC |
189 | } |
190 | /* now look for a matching I/K/G */ | |
191 | if((aconf = find_address_conf(host, NULL, user_trunc, notildeuser_trunc, | |
192 | (type != HM_HOST) ? (struct sockaddr *)&ip : NULL, | |
193 | (type != HM_HOST) ? ( | |
55abcbb2 | 194 | (type == HM_IPV6) ? AF_INET6 : |
40c1fd47 | 195 | AF_INET) : 0, NULL))) |
212380e3 AC |
196 | { |
197 | static char buf[HOSTLEN+USERLEN+2]; | |
198 | ||
199 | if(aconf->status & CONF_KILL) | |
200 | { | |
1e57e391 | 201 | get_printable_kline(source_p, aconf, &phost, &reason, &puser, &operreason); |
5203cba5 | 202 | snprintf(buf, sizeof(buf), "%s@%s", |
1e57e391 | 203 | puser, phost); |
5203cba5 | 204 | snprintf(reasonbuf, sizeof(reasonbuf), "%s%s%s", reason, |
cb2540a6 | 205 | operreason ? "|" : "", operreason ? operreason : ""); |
212380e3 AC |
206 | sendto_one(source_p, form_str(RPL_TESTLINE), |
207 | me.name, source_p->name, | |
208 | (aconf->flags & CONF_FLAGS_TEMPORARY) ? 'k' : 'K', | |
55abcbb2 | 209 | (aconf->flags & CONF_FLAGS_TEMPORARY) ? |
e3354945 | 210 | (long) ((aconf->hold - rb_current_time()) / 60) : 0L, |
cb2540a6 | 211 | buf, reasonbuf); |
3c7d6fcc | 212 | return; |
212380e3 | 213 | } |
212380e3 AC |
214 | } |
215 | ||
216 | /* they asked us to check a nick, so hunt for resvs.. */ | |
217 | if(name && (resv_p = find_nick_resv(name))) | |
218 | { | |
219 | sendto_one(source_p, form_str(RPL_TESTLINE), | |
220 | me.name, source_p->name, | |
221 | resv_p->hold ? 'q' : 'Q', | |
e3354945 | 222 | resv_p->hold ? (long) ((resv_p->hold - rb_current_time()) / 60) : 0L, |
70ea02eb | 223 | resv_p->host, resv_p->passwd); |
212380e3 AC |
224 | |
225 | /* this is a false positive, so make sure it isn't counted in stats q | |
226 | * --nenolod | |
227 | */ | |
228 | resv_p->port--; | |
3c7d6fcc | 229 | return; |
212380e3 AC |
230 | } |
231 | ||
232 | /* no matching resv, we can print the I: if it exists */ | |
233 | if(aconf && aconf->status & CONF_CLIENT) | |
234 | { | |
235 | sendto_one_numeric(source_p, RPL_STATSILINE, form_str(RPL_STATSILINE), | |
27f616dd | 236 | aconf->info.name, EmptyString(aconf->spasswd) ? "<NULL>" : aconf->spasswd, |
59c3d09a | 237 | show_iline_prefix(source_p, aconf, aconf->user), |
48a06ae3 EM |
238 | aconf->host, aconf->port, aconf->className, |
239 | CheckEmpty(aconf->desc)); | |
3c7d6fcc | 240 | return; |
212380e3 AC |
241 | } |
242 | ||
243 | /* nothing matches.. */ | |
244 | sendto_one(source_p, form_str(RPL_NOTESTLINE), | |
245 | me.name, source_p->name, parv[1]); | |
212380e3 AC |
246 | } |
247 | ||
4dc6ff3d EK |
248 | |
249 | static void | |
250 | mo_testkline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) | |
251 | { | |
252 | struct ConfItem *aconf; | |
253 | struct rb_sockaddr_storage ip; | |
254 | char user_trunc[USERLEN + 1]; | |
255 | const char *username = NULL; | |
256 | const char *host = NULL; | |
257 | char *mask; | |
258 | char *p; | |
259 | int host_mask; | |
260 | int type; | |
261 | int duration; | |
262 | char *puser, *phost, *reason, *operreason; | |
263 | char reasonbuf[BUFSIZE]; | |
264 | ||
265 | if (!HasPrivilege(source_p, "oper:testline")) | |
266 | { | |
267 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
268 | me.name, source_p->name, "testline"); | |
269 | return; | |
270 | } | |
271 | ||
272 | mask = LOCAL_COPY(parv[1]); | |
273 | ||
274 | if ((p = strchr(mask, '!'))) | |
275 | { | |
276 | mask = p + 1; | |
277 | ||
278 | if(EmptyString(mask)) | |
3dcaa851 EK |
279 | { |
280 | sendto_one_notice(source_p, "Invalid syntax for TESTKLINE"); | |
4dc6ff3d | 281 | return; |
3dcaa851 | 282 | } |
4dc6ff3d EK |
283 | } |
284 | ||
285 | if ((p = strchr(mask, '@'))) | |
286 | { | |
287 | *p++ = '\0'; | |
288 | username = mask; | |
289 | host = p; | |
290 | ||
291 | if(EmptyString(host)) | |
3dcaa851 EK |
292 | { |
293 | sendto_one_notice(source_p, "Invalid syntax for TESTKLINE"); | |
4dc6ff3d | 294 | return; |
3dcaa851 | 295 | } |
4dc6ff3d EK |
296 | } |
297 | else | |
298 | { | |
299 | host = mask; | |
300 | } | |
301 | ||
302 | /* parses as an IP, check for a dline */ | |
303 | if ((type = parse_netmask(host, &ip, &host_mask)) != HM_HOST) | |
304 | { | |
305 | if(type == HM_IPV6) | |
306 | aconf = find_dline((struct sockaddr *)&ip, AF_INET6); | |
307 | else | |
308 | aconf = find_dline((struct sockaddr *)&ip, AF_INET); | |
309 | ||
310 | if(aconf && aconf->status & CONF_DLINE) | |
311 | { | |
312 | get_printable_kline(source_p, aconf, &phost, &reason, &puser, &operreason); | |
313 | snprintf(reasonbuf, sizeof(reasonbuf), "%s%s%s", reason, | |
314 | operreason ? "|" : "", operreason ? operreason : ""); | |
315 | sendto_one(source_p, form_str(RPL_TESTLINE), | |
316 | me.name, source_p->name, | |
317 | (aconf->flags & CONF_FLAGS_TEMPORARY) ? 'd' : 'D', | |
318 | (aconf->flags & CONF_FLAGS_TEMPORARY) ? | |
319 | (long) ((aconf->hold - rb_current_time()) / 60) : 0L, | |
320 | phost, reasonbuf); | |
321 | ||
322 | return; | |
323 | } | |
324 | /* Otherwise, aconf is an exempt{} */ | |
325 | if (aconf == NULL && (duration = is_reject_ip((struct sockaddr *)&ip))) | |
326 | sendto_one(source_p, form_str(RPL_TESTLINE), | |
327 | me.name, source_p->name, | |
328 | '!', | |
329 | duration / 60L, | |
330 | host, "Reject cache"); | |
331 | if (aconf == NULL && (duration = is_throttle_ip((struct sockaddr *)&ip))) | |
332 | sendto_one(source_p, form_str(RPL_TESTLINE), | |
333 | me.name, source_p->name, | |
334 | '!', | |
335 | duration / 60L, | |
336 | host, "Throttled"); | |
337 | } | |
338 | ||
339 | if (username != NULL) | |
340 | rb_strlcpy(user_trunc, username, sizeof user_trunc); | |
341 | else | |
342 | rb_strlcpy(user_trunc, "dummy", sizeof user_trunc); | |
343 | ||
344 | aconf = find_conf_by_address(host, | |
345 | type != HM_HOST ? host : NULL, | |
346 | NULL, | |
347 | type != HM_HOST ? (struct sockaddr *)&ip : NULL, | |
348 | CONF_KILL, | |
349 | type == HM_IPV6 ? AF_INET6 : AF_INET, | |
350 | user_trunc, NULL); | |
351 | ||
352 | if (aconf != NULL && aconf->status & CONF_KILL) | |
353 | { | |
354 | static char buf[HOSTLEN+USERLEN+2]; | |
355 | ||
356 | get_printable_kline(source_p, aconf, &phost, &reason, &puser, &operreason); | |
357 | snprintf(buf, sizeof(buf), "%s@%s", | |
358 | puser, phost); | |
359 | snprintf(reasonbuf, sizeof(reasonbuf), "%s%s%s", reason, | |
360 | operreason ? "|" : "", operreason ? operreason : ""); | |
361 | sendto_one(source_p, form_str(RPL_TESTLINE), | |
362 | me.name, source_p->name, | |
363 | (aconf->flags & CONF_FLAGS_TEMPORARY) ? 'k' : 'K', | |
364 | (aconf->flags & CONF_FLAGS_TEMPORARY) ? | |
365 | (long) ((aconf->hold - rb_current_time()) / 60) : 0L, | |
366 | buf, reasonbuf); | |
367 | return; | |
368 | } | |
369 | ||
370 | sendto_one(source_p, form_str(RPL_NOTESTLINE), | |
371 | me.name, source_p->name, parv[1]); | |
372 | } | |
373 | ||
3c7d6fcc | 374 | static void |
428ca87b | 375 | mo_testgecos(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) |
212380e3 AC |
376 | { |
377 | struct ConfItem *aconf; | |
378 | ||
58a490f9 EK |
379 | if (!HasPrivilege(source_p, "oper:testline")) |
380 | { | |
381 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
382 | me.name, source_p->name, "testline"); | |
383 | return; | |
384 | } | |
385 | ||
212380e3 AC |
386 | if(!(aconf = find_xline(parv[1], 0))) |
387 | { | |
388 | sendto_one(source_p, form_str(RPL_NOTESTLINE), | |
389 | me.name, source_p->name, parv[1]); | |
3c7d6fcc | 390 | return; |
212380e3 AC |
391 | } |
392 | ||
393 | sendto_one(source_p, form_str(RPL_TESTLINE), | |
394 | me.name, source_p->name, | |
395 | aconf->hold ? 'x' : 'X', | |
e3354945 | 396 | aconf->hold ? (long) ((aconf->hold - rb_current_time()) / 60) : 0L, |
70ea02eb | 397 | aconf->host, aconf->passwd); |
212380e3 | 398 | } |