]>
Commit | Line | Data |
---|---|---|
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 | { | |
121 | sendto_one_notice(source_p, "Invalid syntax for TESTLINE"); | |
122 | return; | |
123 | } | |
124 | } | |
125 | ||
126 | if((p = strchr(mask, '@'))) | |
127 | { | |
128 | *p++ = '\0'; | |
129 | username = mask; | |
130 | host = p; | |
131 | ||
132 | if(EmptyString(host)) | |
133 | { | |
134 | sendto_one_notice(source_p, "Invalid syntax for TESTLINE"); | |
135 | return; | |
136 | } | |
137 | } | |
138 | else | |
139 | host = mask; | |
140 | ||
141 | /* parses as an IP, check for a dline */ | |
142 | if((type = parse_netmask(host, &ip, &host_mask)) != HM_HOST) | |
143 | { | |
144 | if(type == HM_IPV6) | |
145 | aconf = find_dline((struct sockaddr *)&ip, AF_INET6); | |
146 | else | |
147 | aconf = find_dline((struct sockaddr *)&ip, AF_INET); | |
148 | ||
149 | if(aconf && aconf->status & CONF_DLINE) | |
150 | { | |
151 | get_printable_kline(source_p, aconf, &phost, &reason, &puser, &operreason); | |
152 | snprintf(reasonbuf, sizeof(reasonbuf), "%s%s%s", reason, | |
153 | operreason ? "|" : "", operreason ? operreason : ""); | |
154 | sendto_one(source_p, form_str(RPL_TESTLINE), | |
155 | me.name, source_p->name, | |
156 | (aconf->flags & CONF_FLAGS_TEMPORARY) ? 'd' : 'D', | |
157 | (aconf->flags & CONF_FLAGS_TEMPORARY) ? | |
158 | (long) ((aconf->hold - rb_current_time()) / 60) : 0L, | |
159 | phost, reasonbuf); | |
160 | ||
161 | return; | |
162 | } | |
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 | '!', | |
169 | duration / 60L, | |
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 | '!', | |
176 | duration / 60L, | |
177 | host, "Throttled"); | |
178 | } | |
179 | ||
180 | if (username != NULL) | |
181 | { | |
182 | rb_strlcpy(user_trunc, username, sizeof user_trunc); | |
183 | rb_strlcpy(notildeuser_trunc, *username == '~' ? username + 1 : username, sizeof notildeuser_trunc); | |
184 | } | |
185 | else | |
186 | { | |
187 | rb_strlcpy(user_trunc, "dummy", sizeof user_trunc); | |
188 | rb_strlcpy(notildeuser_trunc, "dummy", sizeof notildeuser_trunc); | |
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) ? ( | |
194 | (type == HM_IPV6) ? AF_INET6 : | |
195 | AF_INET) : 0, NULL))) | |
196 | { | |
197 | static char buf[HOSTLEN+USERLEN+2]; | |
198 | ||
199 | if(aconf->status & CONF_KILL) | |
200 | { | |
201 | get_printable_kline(source_p, aconf, &phost, &reason, &puser, &operreason); | |
202 | snprintf(buf, sizeof(buf), "%s@%s", | |
203 | puser, phost); | |
204 | snprintf(reasonbuf, sizeof(reasonbuf), "%s%s%s", reason, | |
205 | operreason ? "|" : "", operreason ? operreason : ""); | |
206 | sendto_one(source_p, form_str(RPL_TESTLINE), | |
207 | me.name, source_p->name, | |
208 | (aconf->flags & CONF_FLAGS_TEMPORARY) ? 'k' : 'K', | |
209 | (aconf->flags & CONF_FLAGS_TEMPORARY) ? | |
210 | (long) ((aconf->hold - rb_current_time()) / 60) : 0L, | |
211 | buf, reasonbuf); | |
212 | return; | |
213 | } | |
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', | |
222 | resv_p->hold ? (long) ((resv_p->hold - rb_current_time()) / 60) : 0L, | |
223 | resv_p->host, resv_p->passwd); | |
224 | ||
225 | /* this is a false positive, so make sure it isn't counted in stats q | |
226 | * --nenolod | |
227 | */ | |
228 | resv_p->port--; | |
229 | return; | |
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), | |
236 | aconf->info.name, EmptyString(aconf->spasswd) ? "<NULL>" : aconf->spasswd, | |
237 | show_iline_prefix(source_p, aconf, aconf->user), | |
238 | aconf->host, aconf->port, aconf->className); | |
239 | return; | |
240 | } | |
241 | ||
242 | /* nothing matches.. */ | |
243 | sendto_one(source_p, form_str(RPL_NOTESTLINE), | |
244 | me.name, source_p->name, parv[1]); | |
245 | } | |
246 | ||
247 | ||
248 | static void | |
249 | mo_testkline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) | |
250 | { | |
251 | struct ConfItem *aconf; | |
252 | struct rb_sockaddr_storage ip; | |
253 | char user_trunc[USERLEN + 1]; | |
254 | const char *username = NULL; | |
255 | const char *host = NULL; | |
256 | char *mask; | |
257 | char *p; | |
258 | int host_mask; | |
259 | int type; | |
260 | int duration; | |
261 | char *puser, *phost, *reason, *operreason; | |
262 | char reasonbuf[BUFSIZE]; | |
263 | ||
264 | if (!HasPrivilege(source_p, "oper:testline")) | |
265 | { | |
266 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
267 | me.name, source_p->name, "testline"); | |
268 | return; | |
269 | } | |
270 | ||
271 | mask = LOCAL_COPY(parv[1]); | |
272 | ||
273 | if ((p = strchr(mask, '!'))) | |
274 | { | |
275 | mask = p + 1; | |
276 | ||
277 | if(EmptyString(mask)) | |
278 | { | |
279 | sendto_one_notice(source_p, "Invalid syntax for TESTKLINE"); | |
280 | return; | |
281 | } | |
282 | } | |
283 | ||
284 | if ((p = strchr(mask, '@'))) | |
285 | { | |
286 | *p++ = '\0'; | |
287 | username = mask; | |
288 | host = p; | |
289 | ||
290 | if(EmptyString(host)) | |
291 | { | |
292 | sendto_one_notice(source_p, "Invalid syntax for TESTKLINE"); | |
293 | return; | |
294 | } | |
295 | } | |
296 | else | |
297 | { | |
298 | host = mask; | |
299 | } | |
300 | ||
301 | /* parses as an IP, check for a dline */ | |
302 | if ((type = parse_netmask(host, &ip, &host_mask)) != HM_HOST) | |
303 | { | |
304 | if(type == HM_IPV6) | |
305 | aconf = find_dline((struct sockaddr *)&ip, AF_INET6); | |
306 | else | |
307 | aconf = find_dline((struct sockaddr *)&ip, AF_INET); | |
308 | ||
309 | if(aconf && aconf->status & CONF_DLINE) | |
310 | { | |
311 | get_printable_kline(source_p, aconf, &phost, &reason, &puser, &operreason); | |
312 | snprintf(reasonbuf, sizeof(reasonbuf), "%s%s%s", reason, | |
313 | operreason ? "|" : "", operreason ? operreason : ""); | |
314 | sendto_one(source_p, form_str(RPL_TESTLINE), | |
315 | me.name, source_p->name, | |
316 | (aconf->flags & CONF_FLAGS_TEMPORARY) ? 'd' : 'D', | |
317 | (aconf->flags & CONF_FLAGS_TEMPORARY) ? | |
318 | (long) ((aconf->hold - rb_current_time()) / 60) : 0L, | |
319 | phost, reasonbuf); | |
320 | ||
321 | return; | |
322 | } | |
323 | /* Otherwise, aconf is an exempt{} */ | |
324 | if (aconf == NULL && (duration = is_reject_ip((struct sockaddr *)&ip))) | |
325 | sendto_one(source_p, form_str(RPL_TESTLINE), | |
326 | me.name, source_p->name, | |
327 | '!', | |
328 | duration / 60L, | |
329 | host, "Reject cache"); | |
330 | if (aconf == NULL && (duration = is_throttle_ip((struct sockaddr *)&ip))) | |
331 | sendto_one(source_p, form_str(RPL_TESTLINE), | |
332 | me.name, source_p->name, | |
333 | '!', | |
334 | duration / 60L, | |
335 | host, "Throttled"); | |
336 | } | |
337 | ||
338 | if (username != NULL) | |
339 | rb_strlcpy(user_trunc, username, sizeof user_trunc); | |
340 | else | |
341 | rb_strlcpy(user_trunc, "dummy", sizeof user_trunc); | |
342 | ||
343 | aconf = find_conf_by_address(host, | |
344 | type != HM_HOST ? host : NULL, | |
345 | NULL, | |
346 | type != HM_HOST ? (struct sockaddr *)&ip : NULL, | |
347 | CONF_KILL, | |
348 | type == HM_IPV6 ? AF_INET6 : AF_INET, | |
349 | user_trunc, NULL); | |
350 | ||
351 | if (aconf != NULL && aconf->status & CONF_KILL) | |
352 | { | |
353 | static char buf[HOSTLEN+USERLEN+2]; | |
354 | ||
355 | get_printable_kline(source_p, aconf, &phost, &reason, &puser, &operreason); | |
356 | snprintf(buf, sizeof(buf), "%s@%s", | |
357 | puser, phost); | |
358 | snprintf(reasonbuf, sizeof(reasonbuf), "%s%s%s", reason, | |
359 | operreason ? "|" : "", operreason ? operreason : ""); | |
360 | sendto_one(source_p, form_str(RPL_TESTLINE), | |
361 | me.name, source_p->name, | |
362 | (aconf->flags & CONF_FLAGS_TEMPORARY) ? 'k' : 'K', | |
363 | (aconf->flags & CONF_FLAGS_TEMPORARY) ? | |
364 | (long) ((aconf->hold - rb_current_time()) / 60) : 0L, | |
365 | buf, reasonbuf); | |
366 | return; | |
367 | } | |
368 | ||
369 | sendto_one(source_p, form_str(RPL_NOTESTLINE), | |
370 | me.name, source_p->name, parv[1]); | |
371 | } | |
372 | ||
373 | static void | |
374 | mo_testgecos(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) | |
375 | { | |
376 | struct ConfItem *aconf; | |
377 | ||
378 | if (!HasPrivilege(source_p, "oper:testline")) | |
379 | { | |
380 | sendto_one(source_p, form_str(ERR_NOPRIVS), | |
381 | me.name, source_p->name, "testline"); | |
382 | return; | |
383 | } | |
384 | ||
385 | if(!(aconf = find_xline(parv[1], 0))) | |
386 | { | |
387 | sendto_one(source_p, form_str(RPL_NOTESTLINE), | |
388 | me.name, source_p->name, parv[1]); | |
389 | return; | |
390 | } | |
391 | ||
392 | sendto_one(source_p, form_str(RPL_TESTLINE), | |
393 | me.name, source_p->name, | |
394 | aconf->hold ? 'x' : 'X', | |
395 | aconf->hold ? (long) ((aconf->hold - rb_current_time()) / 60) : 0L, | |
396 | aconf->host, aconf->passwd); | |
397 | } |