]> jfr.im git - irc/rqf/shadowircd.git/blame - modules/m_etrace.c
Update TODO
[irc/rqf/shadowircd.git] / modules / m_etrace.c
CommitLineData
212380e3 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.
31 *
5366977b 32 * $Id: m_etrace.c 3161 2007-01-25 07:23:01Z nenolod $
212380e3 33 */
34
35#include "stdinc.h"
36#include "class.h"
37#include "hook.h"
38#include "client.h"
39#include "hash.h"
40#include "common.h"
41#include "hash.h"
13ae2f4b 42#include "match.h"
212380e3 43#include "ircd.h"
44#include "numeric.h"
212380e3 45#include "s_serv.h"
46#include "s_conf.h"
47#include "s_newconf.h"
48#include "send.h"
49#include "msg.h"
50#include "parse.h"
51#include "modules.h"
52
53static int mo_etrace(struct Client *, struct Client *, int, const char **);
54static int me_etrace(struct Client *, struct Client *, int, const char **);
28823cd3 55static int m_chantrace(struct Client *, struct Client *, int, const char **);
212380e3 56static int mo_masktrace(struct Client *, struct Client *, int, const char **);
57
58struct Message etrace_msgtab = {
59 "ETRACE", 0, 0, 0, MFLG_SLOW,
60 {mg_ignore, mg_not_oper, mg_ignore, mg_ignore, {me_etrace, 0}, {mo_etrace, 0}}
61};
62struct Message chantrace_msgtab = {
63 "CHANTRACE", 0, 0, 0, MFLG_SLOW,
28823cd3 64 {mg_ignore, {m_chantrace, 2}, mg_ignore, mg_ignore, mg_ignore, {m_chantrace, 2}}
212380e3 65};
66struct Message masktrace_msgtab = {
67 "MASKTRACE", 0, 0, 0, MFLG_SLOW,
68 {mg_ignore, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_masktrace, 2}}
69};
70
71mapi_clist_av1 etrace_clist[] = { &etrace_msgtab, &chantrace_msgtab, &masktrace_msgtab, NULL };
5366977b 72DECLARE_MODULE_AV1(etrace, NULL, NULL, etrace_clist, NULL, NULL, "$Revision: 3161 $");
212380e3 73
74static void do_etrace(struct Client *source_p, int ipv4, int ipv6);
75static void do_etrace_full(struct Client *source_p);
76static void do_single_etrace(struct Client *source_p, struct Client *target_p);
77
78static const char *empty_sockhost = "255.255.255.255";
79static const char *spoofed_sockhost = "0";
80
81/*
82 * m_etrace
83 * parv[0] = sender prefix
84 * parv[1] = options [or target]
85 * parv[2] = [target]
86 */
87static int
88mo_etrace(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
89{
90 if(parc > 1 && !EmptyString(parv[1]))
91 {
92 if(!irccmp(parv[1], "-full"))
93 do_etrace_full(source_p);
2c2e0aa9 94#ifdef RB_IPV6
212380e3 95 else if(!irccmp(parv[1], "-v6"))
96 do_etrace(source_p, 0, 1);
97 else if(!irccmp(parv[1], "-v4"))
98 do_etrace(source_p, 1, 0);
99#endif
100 else
101 {
102 struct Client *target_p = find_named_person(parv[1]);
103
104 if(target_p)
105 {
106 if(!MyClient(target_p))
107 sendto_one(target_p, ":%s ENCAP %s ETRACE %s",
108 get_id(source_p, target_p),
c88cdb00 109 target_p->servptr->name,
212380e3 110 get_id(target_p, target_p));
111 else
112 do_single_etrace(source_p, target_p);
113 }
114 else
115 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
116 form_str(ERR_NOSUCHNICK), parv[1]);
117 }
118 }
119 else
120 do_etrace(source_p, 1, 1);
121
122 return 0;
123}
124
125static int
126me_etrace(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
127{
128 struct Client *target_p;
129
130 if(!IsOper(source_p) || parc < 2 || EmptyString(parv[1]))
131 return 0;
132
133 /* we cant etrace remote clients.. we shouldnt even get sent them */
134 if((target_p = find_person(parv[1])) && MyClient(target_p))
135 do_single_etrace(source_p, target_p);
136
137 sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE),
138 target_p ? target_p->name : parv[1]);
139
140 return 0;
141}
142
143static void
144do_etrace(struct Client *source_p, int ipv4, int ipv6)
145{
146 struct Client *target_p;
08d11e34 147 rb_dlink_node *ptr;
212380e3 148
149 /* report all direct connections */
08d11e34 150 RB_DLINK_FOREACH(ptr, lclient_list.head)
212380e3 151 {
152 target_p = ptr->data;
153
2c2e0aa9 154#ifdef RB_IPV6
212380e3 155 if((!ipv4 && target_p->localClient->ip.ss_family == AF_INET) ||
156 (!ipv6 && target_p->localClient->ip.ss_family == AF_INET6))
157 continue;
158#endif
159
160 sendto_one(source_p, form_str(RPL_ETRACE),
161 me.name, source_p->name,
162 IsOper(target_p) ? "Oper" : "User",
163 get_client_class(target_p),
164 target_p->name, target_p->username, target_p->host,
165 show_ip(source_p, target_p) ? target_p->sockhost : "255.255.255.255",
166 target_p->info);
167 }
168
169 sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE), me.name);
170}
171
172static void
173do_etrace_full(struct Client *source_p)
174{
08d11e34 175 rb_dlink_node *ptr;
212380e3 176
08d11e34 177 RB_DLINK_FOREACH(ptr, lclient_list.head)
212380e3 178 {
179 do_single_etrace(source_p, ptr->data);
180 }
181
182 sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE), me.name);
183}
184
185/*
186 * do_single_etrace - searches local clients and displays those matching
187 * a pattern
188 * input - source client, target client
189 * output - etrace results
190 * side effects - etrace results are displayed
191 */
192static void
193do_single_etrace(struct Client *source_p, struct Client *target_p)
194{
195 /* note, we hide fullcaps for spoofed users, as mirc can often
196 * advertise its internal ip address in the field --fl
197 */
198 if(!show_ip(source_p, target_p))
199 sendto_one(source_p, form_str(RPL_ETRACEFULL),
200 me.name, source_p->name,
201 IsOper(target_p) ? "Oper" : "User",
202 get_client_class(target_p),
203 target_p->name, target_p->username, target_p->host,
204 "255.255.255.255", "<hidden> <hidden>", target_p->info);
205 else
206 sendto_one(source_p, form_str(RPL_ETRACEFULL),
207 me.name, source_p->name,
208 IsOper(target_p) ? "Oper" : "User",
209 get_client_class(target_p),
210 target_p->name, target_p->username,
211 target_p->host, target_p->sockhost,
212 target_p->localClient->fullcaps, target_p->info);
213}
214
215static int
28823cd3 216m_chantrace(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3 217{
218 struct Client *target_p;
219 struct Channel *chptr;
220 struct membership *msptr;
221 const char *sockhost;
222 const char *name;
08d11e34 223 rb_dlink_node *ptr;
212380e3 224 int operspy = 0;
225
226 name = parv[1];
227
228 if(IsOperSpy(source_p) && parv[1][0] == '!')
229 {
230 name++;
231 operspy = 1;
232
233 if(EmptyString(name))
234 {
235 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
236 me.name, source_p->name, "CHANTRACE");
237 return 0;
238 }
239 }
240
241 if((chptr = find_channel(name)) == NULL)
242 {
243 sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL, form_str(ERR_NOSUCHCHANNEL),
244 name);
245 return 0;
246 }
247
248 /* dont report operspys for nonexistant channels. */
249 if(operspy)
250 report_operspy(source_p, "CHANTRACE", chptr->chname);
251
252 if(!operspy && !IsMember(client_p, chptr))
253 {
254 sendto_one_numeric(source_p, ERR_NOTONCHANNEL, form_str(ERR_NOTONCHANNEL),
255 chptr->chname);
256 return 0;
257 }
258
08d11e34 259 RB_DLINK_FOREACH(ptr, chptr->members.head)
212380e3 260 {
261 msptr = ptr->data;
262 target_p = msptr->client_p;
263
264 if(EmptyString(target_p->sockhost))
265 sockhost = empty_sockhost;
266 else if(!show_ip(source_p, target_p))
267 sockhost = spoofed_sockhost;
268 else
269 sockhost = target_p->sockhost;
270
271 sendto_one(source_p, form_str(RPL_ETRACE),
272 me.name, source_p->name,
273 IsOper(target_p) ? "Oper" : "User",
274 /* class field -- pretend its server.. */
275 target_p->servptr->name,
276 target_p->name, target_p->username, target_p->host,
277 sockhost, target_p->info);
278 }
279
280 sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE), me.name);
281 return 0;
282}
283
284static void
08d11e34 285match_masktrace(struct Client *source_p, rb_dlink_list *list,
212380e3 286 const char *username, const char *hostname, const char *name,
287 const char *gecos)
288{
289 struct Client *target_p;
08d11e34 290 rb_dlink_node *ptr;
212380e3 291 const char *sockhost;
292 char *mangle_gecos = NULL;
293
294 if(gecos != NULL)
295 {
296 if(strstr(gecos, "\\s"))
297 {
298 char *tmp = LOCAL_COPY(gecos);
299 char *orig = tmp;
300 char *new = tmp;
301 while(*orig)
302 {
303 if(*orig == '\\' && *(orig + 1) != '\0')
304 {
305 if(*(orig + 1) == 's')
306 {
307 *new++ = ' ';
308 orig += 2;
309 }
310 /* otherwise skip that and the escaped
311 * character after it, so we dont mistake
312 * \\s as \s --fl
313 */
314 else
315 {
316 *new++ = *orig++;
317 *new++ = *orig++;
318 }
319 }
320 else
321 *new++ = *orig++;
322 }
323
324 *new = '\0';
325 mangle_gecos = LOCAL_COPY(tmp);
326 }
327 else
328 mangle_gecos = LOCAL_COPY(gecos);
329 }
330
08d11e34 331 RB_DLINK_FOREACH(ptr, list->head)
212380e3 332 {
333 target_p = ptr->data;
334 if(!IsPerson(target_p))
335 continue;
336
337 if(EmptyString(target_p->sockhost))
338 sockhost = empty_sockhost;
339 else if(!show_ip(source_p, target_p))
340 sockhost = spoofed_sockhost;
341 else
342 sockhost = target_p->sockhost;
343
344 if(match(username, target_p->username) &&
345 (match(hostname, target_p->host) ||
346 match(hostname, target_p->orighost) ||
347 match(hostname, sockhost) || match_ips(hostname, sockhost)))
348 {
349 if(name != NULL && !match(name, target_p->name))
350 continue;
351
352 if(mangle_gecos != NULL && !match_esc(mangle_gecos, target_p->info))
353 continue;
354
355 sendto_one(source_p, form_str(RPL_ETRACE),
356 me.name, source_p->name,
357 IsOper(target_p) ? "Oper" : "User",
358 /* class field -- pretend its server.. */
359 target_p->servptr->name,
360 target_p->name, target_p->username, target_p->host,
361 sockhost, target_p->info);
362 }
363 }
364}
365
366static int
367mo_masktrace(struct Client *client_p, struct Client *source_p, int parc,
368 const char *parv[])
369{
370 char *name, *username, *hostname, *gecos;
371 const char *mask;
372 int operspy = 0;
373
374 mask = parv[1];
375 name = LOCAL_COPY(parv[1]);
376 collapse(name);
377
378 if(IsOperSpy(source_p) && parv[1][0] == '!')
379 {
380 name++;
381 mask++;
382 operspy = 1;
383 }
384
385 if(parc > 2 && !EmptyString(parv[2]))
386 {
387 gecos = LOCAL_COPY(parv[2]);
388 collapse_esc(gecos);
389 } else
390 gecos = NULL;
391
392
393 if((hostname = strchr(name, '@')) == NULL)
394 {
5366977b 395 sendto_one_notice(source_p, ":Invalid parameters");
212380e3 396 return 0;
397 }
398
399 *hostname++ = '\0';
400
401 if((username = strchr(name, '!')) == NULL)
402 {
403 username = name;
404 name = NULL;
405 } else
406 *username++ = '\0';
407
408 if(EmptyString(username) || EmptyString(hostname))
409 {
5366977b 410 sendto_one_notice(source_p, ":Invalid parameters");
212380e3 411 return 0;
412 }
413
414 if(operspy) {
415 if (!ConfigFileEntry.operspy_dont_care_user_info)
416 {
417 char buf[512];
907468c4 418 rb_strlcpy(buf, mask, sizeof(buf));
212380e3 419 if(!EmptyString(gecos)) {
a64c5173
VY
420 rb_strlcat(buf, " ", sizeof(buf));
421 rb_strlcat(buf, gecos, sizeof(buf));
212380e3 422 }
423
424 report_operspy(source_p, "MASKTRACE", buf);
425 }
426 match_masktrace(source_p, &global_client_list, username, hostname, name, gecos);
427 } else
428 match_masktrace(source_p, &lclient_list, username, hostname, name, gecos);
429
430 sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE), me.name);
431 return 0;
432}