]> jfr.im git - solanum.git/blame - modules/m_etrace.c
ircd: handle some EXIT_FAILURE cases differently on win32
[solanum.git] / modules / m_etrace.c
CommitLineData
212380e3
AC
1/*
2 * ircd-ratbox: an advanced Internet Relay Chat Daemon(ircd).
3 * m_etrace.c: Gives local opers a trace output with added info.
4 *
5 * Copyright (C) 2002-2003 Lee Hardy <lee@leeh.co.uk>
6 * Copyright (C) 2002-2005 ircd-ratbox development team
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * 1.Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 * 2.Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3.The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
212380e3
AC
31 */
32
33#include "stdinc.h"
34#include "class.h"
35#include "hook.h"
36#include "client.h"
37#include "hash.h"
38#include "common.h"
39#include "hash.h"
4562c604 40#include "match.h"
212380e3
AC
41#include "ircd.h"
42#include "numeric.h"
212380e3
AC
43#include "s_serv.h"
44#include "s_conf.h"
45#include "s_newconf.h"
46#include "send.h"
47#include "msg.h"
48#include "parse.h"
49#include "modules.h"
77d3d2db 50#include "logger.h"
0b904d91 51#include "supported.h"
212380e3 52
eeabf33a
EM
53static const char etrace_desc[] =
54 "Provides enhanced tracing facilities to opers (ETRACE, CHANTRACE, and MASKTRACE)";
55
3c7d6fcc
EM
56static void mo_etrace(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
57static void me_etrace(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
58static void m_chantrace(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
59static void mo_masktrace(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
212380e3
AC
60
61struct Message etrace_msgtab = {
7baa37a9 62 "ETRACE", 0, 0, 0, 0,
212380e3
AC
63 {mg_ignore, mg_not_oper, mg_ignore, mg_ignore, {me_etrace, 0}, {mo_etrace, 0}}
64};
65struct Message chantrace_msgtab = {
7baa37a9 66 "CHANTRACE", 0, 0, 0, 0,
5807e0be 67 {mg_ignore, {m_chantrace, 2}, mg_ignore, mg_ignore, mg_ignore, {m_chantrace, 2}}
212380e3
AC
68};
69struct Message masktrace_msgtab = {
7baa37a9 70 "MASKTRACE", 0, 0, 0, 0,
212380e3
AC
71 {mg_ignore, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_masktrace, 2}}
72};
73
0b904d91
AC
74static int
75_modinit(void)
76{
77 add_isupport("ETRACE", isupport_string, "");
78
79 return 0;
80}
81
82static void
83_moddeinit(void)
84{
85 delete_isupport("ETRACE");
86}
87
212380e3 88mapi_clist_av1 etrace_clist[] = { &etrace_msgtab, &chantrace_msgtab, &masktrace_msgtab, NULL };
9fd8e7cb 89
9fd8e7cb 90DECLARE_MODULE_AV2(etrace, _modinit, _moddeinit, etrace_clist, NULL, NULL, NULL, NULL, etrace_desc);
212380e3
AC
91
92static void do_etrace(struct Client *source_p, int ipv4, int ipv6);
93static void do_etrace_full(struct Client *source_p);
94static void do_single_etrace(struct Client *source_p, struct Client *target_p);
95
96static const char *empty_sockhost = "255.255.255.255";
97static const char *spoofed_sockhost = "0";
98
99/*
100 * m_etrace
212380e3
AC
101 * parv[1] = options [or target]
102 * parv[2] = [target]
103 */
3c7d6fcc 104static void
428ca87b 105mo_etrace(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3
AC
106{
107 if(parc > 1 && !EmptyString(parv[1]))
108 {
109 if(!irccmp(parv[1], "-full"))
110 do_etrace_full(source_p);
ccda6e3f 111#ifdef RB_IPV6
212380e3
AC
112 else if(!irccmp(parv[1], "-v6"))
113 do_etrace(source_p, 0, 1);
114 else if(!irccmp(parv[1], "-v4"))
115 do_etrace(source_p, 1, 0);
116#endif
117 else
118 {
119 struct Client *target_p = find_named_person(parv[1]);
120
121 if(target_p)
122 {
123 if(!MyClient(target_p))
124 sendto_one(target_p, ":%s ENCAP %s ETRACE %s",
125 get_id(source_p, target_p),
c88cdb00 126 target_p->servptr->name,
212380e3
AC
127 get_id(target_p, target_p));
128 else
129 do_single_etrace(source_p, target_p);
130 }
131 else
132 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
133 form_str(ERR_NOSUCHNICK), parv[1]);
134 }
135 }
136 else
137 do_etrace(source_p, 1, 1);
212380e3
AC
138}
139
3c7d6fcc 140static void
428ca87b 141me_etrace(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3
AC
142{
143 struct Client *target_p;
144
145 if(!IsOper(source_p) || parc < 2 || EmptyString(parv[1]))
3c7d6fcc 146 return;
212380e3
AC
147
148 /* we cant etrace remote clients.. we shouldnt even get sent them */
149 if((target_p = find_person(parv[1])) && MyClient(target_p))
150 do_single_etrace(source_p, target_p);
151
55abcbb2 152 sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE),
212380e3 153 target_p ? target_p->name : parv[1]);
212380e3
AC
154}
155
156static void
157do_etrace(struct Client *source_p, int ipv4, int ipv6)
158{
159 struct Client *target_p;
5b96d9a6 160 rb_dlink_node *ptr;
212380e3
AC
161
162 /* report all direct connections */
5b96d9a6 163 RB_DLINK_FOREACH(ptr, lclient_list.head)
212380e3
AC
164 {
165 target_p = ptr->data;
166
ccda6e3f 167#ifdef RB_IPV6
212380e3
AC
168 if((!ipv4 && target_p->localClient->ip.ss_family == AF_INET) ||
169 (!ipv6 && target_p->localClient->ip.ss_family == AF_INET6))
170 continue;
171#endif
172
173 sendto_one(source_p, form_str(RPL_ETRACE),
55abcbb2
KB
174 me.name, source_p->name,
175 IsOper(target_p) ? "Oper" : "User",
212380e3
AC
176 get_client_class(target_p),
177 target_p->name, target_p->username, target_p->host,
178 show_ip(source_p, target_p) ? target_p->sockhost : "255.255.255.255",
179 target_p->info);
180 }
181
182 sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE), me.name);
183}
184
185static void
186do_etrace_full(struct Client *source_p)
187{
5b96d9a6 188 rb_dlink_node *ptr;
212380e3 189
5b96d9a6 190 RB_DLINK_FOREACH(ptr, lclient_list.head)
212380e3
AC
191 {
192 do_single_etrace(source_p, ptr->data);
193 }
194
195 sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE), me.name);
196}
197
198/*
199 * do_single_etrace - searches local clients and displays those matching
200 * a pattern
201 * input - source client, target client
202 * output - etrace results
203 * side effects - etrace results are displayed
204 */
205static void
206do_single_etrace(struct Client *source_p, struct Client *target_p)
207{
208 /* note, we hide fullcaps for spoofed users, as mirc can often
209 * advertise its internal ip address in the field --fl
210 */
211 if(!show_ip(source_p, target_p))
212 sendto_one(source_p, form_str(RPL_ETRACEFULL),
55abcbb2 213 me.name, source_p->name,
212380e3
AC
214 IsOper(target_p) ? "Oper" : "User",
215 get_client_class(target_p),
55abcbb2 216 target_p->name, target_p->username, target_p->host,
212380e3
AC
217 "255.255.255.255", "<hidden> <hidden>", target_p->info);
218 else
219 sendto_one(source_p, form_str(RPL_ETRACEFULL),
55abcbb2 220 me.name, source_p->name,
212380e3
AC
221 IsOper(target_p) ? "Oper" : "User",
222 get_client_class(target_p),
55abcbb2 223 target_p->name, target_p->username,
212380e3
AC
224 target_p->host, target_p->sockhost,
225 target_p->localClient->fullcaps, target_p->info);
226}
227
3c7d6fcc 228static void
428ca87b 229m_chantrace(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3
AC
230{
231 struct Client *target_p;
232 struct Channel *chptr;
233 struct membership *msptr;
234 const char *sockhost;
235 const char *name;
5b96d9a6 236 rb_dlink_node *ptr;
212380e3
AC
237 int operspy = 0;
238
239 name = parv[1];
240
241 if(IsOperSpy(source_p) && parv[1][0] == '!')
242 {
243 name++;
244 operspy = 1;
245
246 if(EmptyString(name))
247 {
248 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
249 me.name, source_p->name, "CHANTRACE");
3c7d6fcc 250 return;
212380e3
AC
251 }
252 }
253
254 if((chptr = find_channel(name)) == NULL)
255 {
256 sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL, form_str(ERR_NOSUCHCHANNEL),
257 name);
3c7d6fcc 258 return;
212380e3
AC
259 }
260
261 /* dont report operspys for nonexistant channels. */
262 if(operspy)
263 report_operspy(source_p, "CHANTRACE", chptr->chname);
264
265 if(!operspy && !IsMember(client_p, chptr))
266 {
267 sendto_one_numeric(source_p, ERR_NOTONCHANNEL, form_str(ERR_NOTONCHANNEL),
268 chptr->chname);
3c7d6fcc 269 return;
212380e3
AC
270 }
271
5b96d9a6 272 RB_DLINK_FOREACH(ptr, chptr->members.head)
212380e3
AC
273 {
274 msptr = ptr->data;
275 target_p = msptr->client_p;
276
277 if(EmptyString(target_p->sockhost))
278 sockhost = empty_sockhost;
279 else if(!show_ip(source_p, target_p))
280 sockhost = spoofed_sockhost;
281 else
282 sockhost = target_p->sockhost;
283
284 sendto_one(source_p, form_str(RPL_ETRACE),
55abcbb2 285 me.name, source_p->name,
212380e3
AC
286 IsOper(target_p) ? "Oper" : "User",
287 /* class field -- pretend its server.. */
288 target_p->servptr->name,
289 target_p->name, target_p->username, target_p->host,
290 sockhost, target_p->info);
291 }
292
293 sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE), me.name);
212380e3
AC
294}
295
296static void
5b96d9a6 297match_masktrace(struct Client *source_p, rb_dlink_list *list,
212380e3
AC
298 const char *username, const char *hostname, const char *name,
299 const char *gecos)
300{
301 struct Client *target_p;
5b96d9a6 302 rb_dlink_node *ptr;
55abcbb2
KB
303 const char *sockhost;
304
5b96d9a6 305 RB_DLINK_FOREACH(ptr, list->head)
212380e3
AC
306 {
307 target_p = ptr->data;
308 if(!IsPerson(target_p))
309 continue;
55abcbb2 310
212380e3
AC
311 if(EmptyString(target_p->sockhost))
312 sockhost = empty_sockhost;
313 else if(!show_ip(source_p, target_p))
314 sockhost = spoofed_sockhost;
315 else
316 sockhost = target_p->sockhost;
317
318 if(match(username, target_p->username) &&
319 (match(hostname, target_p->host) ||
320 match(hostname, target_p->orighost) ||
321 match(hostname, sockhost) || match_ips(hostname, sockhost)))
322 {
323 if(name != NULL && !match(name, target_p->name))
324 continue;
325
39b23540 326 if(gecos != NULL && !match_esc(gecos, target_p->info))
212380e3 327 continue;
55abcbb2 328
212380e3 329 sendto_one(source_p, form_str(RPL_ETRACE),
55abcbb2 330 me.name, source_p->name,
212380e3
AC
331 IsOper(target_p) ? "Oper" : "User",
332 /* class field -- pretend its server.. */
333 target_p->servptr->name,
334 target_p->name, target_p->username, target_p->host,
335 sockhost, target_p->info);
336 }
337 }
338}
339
3c7d6fcc 340static void
428ca87b 341mo_masktrace(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc,
212380e3
AC
342 const char *parv[])
343{
344 char *name, *username, *hostname, *gecos;
345 const char *mask;
346 int operspy = 0;
347
55abcbb2
KB
348 mask = parv[1];
349 name = LOCAL_COPY(parv[1]);
212380e3
AC
350 collapse(name);
351
352 if(IsOperSpy(source_p) && parv[1][0] == '!')
353 {
354 name++;
355 mask++;
356 operspy = 1;
55abcbb2
KB
357 }
358
212380e3
AC
359 if(parc > 2 && !EmptyString(parv[2]))
360 {
361 gecos = LOCAL_COPY(parv[2]);
362 collapse_esc(gecos);
363 } else
364 gecos = NULL;
55abcbb2 365
212380e3
AC
366
367 if((hostname = strchr(name, '@')) == NULL)
368 {
5366977b 369 sendto_one_notice(source_p, ":Invalid parameters");
3c7d6fcc 370 return;
212380e3
AC
371 }
372
373 *hostname++ = '\0';
55abcbb2 374
212380e3
AC
375 if((username = strchr(name, '!')) == NULL)
376 {
377 username = name;
378 name = NULL;
379 } else
380 *username++ = '\0';
381
382 if(EmptyString(username) || EmptyString(hostname))
383 {
5366977b 384 sendto_one_notice(source_p, ":Invalid parameters");
3c7d6fcc 385 return;
212380e3 386 }
55abcbb2 387
212380e3
AC
388 if(operspy) {
389 if (!ConfigFileEntry.operspy_dont_care_user_info)
390 {
391 char buf[512];
f427c8b0 392 rb_strlcpy(buf, mask, sizeof(buf));
212380e3 393 if(!EmptyString(gecos)) {
1f9de103
VY
394 rb_strlcat(buf, " ", sizeof(buf));
395 rb_strlcat(buf, gecos, sizeof(buf));
55abcbb2 396 }
212380e3 397
55abcbb2 398 report_operspy(source_p, "MASKTRACE", buf);
212380e3 399 }
55abcbb2 400 match_masktrace(source_p, &global_client_list, username, hostname, name, gecos);
212380e3
AC
401 } else
402 match_masktrace(source_p, &lclient_list, username, hostname, name, gecos);
403
55abcbb2 404 sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE), me.name);
212380e3 405}