Import from http://svn.freenode.net/ircd-seven/private/beu/seven (r196).
[seven-1.x.git] / modules / m_etrace.c
blob5465ae6d06804d3864d6f86b63a45029dc459ebc
1 /*
2 * ircd-ratbox: an advanced Internet Relay Chat Daemon(ircd).
3 * m_etrace.c: Gives local opers a trace output with added info.
5 * Copyright (C) 2002-2003 Lee Hardy <lee@leeh.co.uk>
6 * Copyright (C) 2002-2005 ircd-ratbox development team
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
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.
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.
32 * $Id: m_etrace.c 147 2006-11-13 12:35:45Z spb $
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"
42 #include "irc_string.h"
43 #include "ircd.h"
44 #include "numeric.h"
45 #include "commio.h"
46 #include "s_serv.h"
47 #include "s_conf.h"
48 #include "s_newconf.h"
49 #include "send.h"
50 #include "msg.h"
51 #include "parse.h"
52 #include "modules.h"
54 static int mo_etrace(struct Client *, struct Client *, int, const char **);
55 static int me_etrace(struct Client *, struct Client *, int, const char **);
56 static int mo_chantrace(struct Client *, struct Client *, int, const char **);
57 static int mo_masktrace(struct Client *, struct Client *, int, const char **);
59 struct Message etrace_msgtab = {
60 "ETRACE", 0, 0, 0, MFLG_SLOW,
61 {mg_ignore, mg_not_oper, mg_ignore, mg_ignore, {me_etrace, 0}, {mo_etrace, 0}}
63 struct Message chantrace_msgtab = {
64 "CHANTRACE", 0, 0, 0, MFLG_SLOW,
65 {mg_ignore, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_chantrace, 2}}
67 struct Message masktrace_msgtab = {
68 "MASKTRACE", 0, 0, 0, MFLG_SLOW,
69 {mg_ignore, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_masktrace, 2}}
72 mapi_clist_av1 etrace_clist[] = { &etrace_msgtab, &chantrace_msgtab, &masktrace_msgtab, NULL };
73 DECLARE_MODULE_AV1(etrace, NULL, NULL, etrace_clist, NULL, NULL, "$Revision: 147 $");
75 static void do_etrace(struct Client *source_p, int ipv4, int ipv6);
76 static void do_etrace_full(struct Client *source_p);
77 static void do_single_etrace(struct Client *source_p, struct Client *target_p);
79 static const char *empty_sockhost = "255.255.255.255";
80 static const char *spoofed_sockhost = "0";
83 * m_etrace
84 * parv[0] = sender prefix
85 * parv[1] = options [or target]
86 * parv[2] = [target]
88 static int
89 mo_etrace(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
91 if(!IsOperRouting(source_p))
93 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name,
94 "routing");
95 return 0;
98 if(parc > 1 && !EmptyString(parv[1]))
100 if(!irccmp(parv[1], "-full"))
101 do_etrace_full(source_p);
102 #ifdef IPV6
103 else if(!irccmp(parv[1], "-v6"))
104 do_etrace(source_p, 0, 1);
105 else if(!irccmp(parv[1], "-v4"))
106 do_etrace(source_p, 1, 0);
107 #endif
108 else
110 struct Client *target_p = find_named_person(parv[1]);
112 if(target_p)
114 if(!MyClient(target_p))
115 sendto_one(target_p, ":%s ENCAP %s ETRACE %s",
116 get_id(source_p, target_p),
117 target_p->user->server,
118 get_id(target_p, target_p));
119 else
120 do_single_etrace(source_p, target_p);
122 else
123 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
124 form_str(ERR_NOSUCHNICK), parv[1]);
127 else
128 do_etrace(source_p, 1, 1);
130 return 0;
133 static int
134 me_etrace(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
136 struct Client *target_p;
138 if(!IsOperRouting(source_p) || parc < 2 || EmptyString(parv[1]))
139 return 0;
141 /* we cant etrace remote clients.. we shouldnt even get sent them */
142 if((target_p = find_person(parv[1])) && MyClient(target_p))
143 do_single_etrace(source_p, target_p);
145 sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE),
146 target_p ? target_p->name : parv[1]);
148 return 0;
151 static void
152 do_etrace(struct Client *source_p, int ipv4, int ipv6)
154 struct Client *target_p;
155 dlink_node *ptr;
157 /* report all direct connections */
158 DLINK_FOREACH(ptr, lclient_list.head)
160 target_p = ptr->data;
162 #ifdef IPV6
163 if((!ipv4 && target_p->localClient->ip.ss_family == AF_INET) ||
164 (!ipv6 && target_p->localClient->ip.ss_family == AF_INET6))
165 continue;
166 #endif
168 sendto_one(source_p, form_str(RPL_ETRACE),
169 me.name, source_p->name,
170 IsOper(target_p) ? "Oper" : "User",
171 get_client_class(target_p),
172 target_p->name, target_p->username, target_p->host,
173 show_ip(source_p, target_p) ? target_p->sockhost : "255.255.255.255",
174 target_p->info);
177 sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE), me.name);
180 static void
181 do_etrace_full(struct Client *source_p)
183 dlink_node *ptr;
185 DLINK_FOREACH(ptr, lclient_list.head)
187 do_single_etrace(source_p, ptr->data);
190 sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE), me.name);
194 * do_single_etrace - searches local clients and displays those matching
195 * a pattern
196 * input - source client, target client
197 * output - etrace results
198 * side effects - etrace results are displayed
200 static void
201 do_single_etrace(struct Client *source_p, struct Client *target_p)
203 /* note, we hide fullcaps for spoofed users, as mirc can often
204 * advertise its internal ip address in the field --fl
206 if(!show_ip(source_p, target_p))
207 sendto_one(source_p, form_str(RPL_ETRACEFULL),
208 me.name, source_p->name,
209 IsOper(target_p) ? "Oper" : "User",
210 get_client_class(target_p),
211 target_p->name, target_p->username, target_p->host,
212 "255.255.255.255", "<hidden> <hidden>", target_p->info);
213 else
214 sendto_one(source_p, form_str(RPL_ETRACEFULL),
215 me.name, source_p->name,
216 IsOper(target_p) ? "Oper" : "User",
217 get_client_class(target_p),
218 target_p->name, target_p->username,
219 target_p->host, target_p->sockhost,
220 target_p->localClient->fullcaps, target_p->info);
223 static int
224 mo_chantrace(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
226 struct Client *target_p;
227 struct Channel *chptr;
228 struct membership *msptr;
229 const char *sockhost;
230 const char *name;
231 dlink_node *ptr;
232 int auspex = 0;
234 name = parv[1];
236 if(IsAuspex(source_p))
238 auspex = 1;
240 if(EmptyString(name))
242 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
243 me.name, source_p->name, "CHANTRACE");
244 return 0;
248 if((chptr = find_channel(name)) == NULL)
250 sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL, form_str(ERR_NOSUCHCHANNEL),
251 name);
252 return 0;
255 if(!auspex && !IsMember(client_p, chptr))
257 sendto_one_numeric(source_p, ERR_NOTONCHANNEL, form_str(ERR_NOTONCHANNEL),
258 chptr->chname);
259 return 0;
262 DLINK_FOREACH(ptr, chptr->members.head)
264 msptr = ptr->data;
265 target_p = msptr->client_p;
267 if(EmptyString(target_p->sockhost))
268 sockhost = empty_sockhost;
269 else if(!show_ip(source_p, target_p))
270 sockhost = spoofed_sockhost;
271 else
272 sockhost = target_p->sockhost;
274 sendto_one(source_p, form_str(RPL_ETRACE),
275 me.name, source_p->name,
276 IsOper(target_p) ? "Oper" : "User",
277 /* class field -- pretend its server.. */
278 target_p->servptr->name,
279 target_p->name, target_p->username, target_p->host,
280 sockhost, target_p->info);
283 sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE), me.name);
284 return 0;
287 static void
288 match_masktrace(struct Client *source_p, dlink_list *list,
289 const char *username, const char *hostname, const char *name,
290 const char *gecos)
292 struct Client *target_p;
293 dlink_node *ptr;
294 const char *sockhost;
295 char *mangle_gecos = NULL;
297 if(gecos != NULL)
299 if(strstr(gecos, "\\s"))
301 char *tmp = LOCAL_COPY(gecos);
302 char *orig = tmp;
303 char *new = tmp;
304 while(*orig)
306 if(*orig == '\\')
308 if(*(orig + 1) == 's')
310 *new++ = ' ';
311 orig += 2;
313 /* otherwise skip that and the escaped
314 * character after it, so we dont mistake
315 * \\s as \s --fl
317 else
319 *new++ = *orig++;
320 *new++ = *orig++;
323 else
324 *new++ = *orig++;
327 *new = '\0';
328 mangle_gecos = LOCAL_COPY(tmp);
330 else
331 mangle_gecos = LOCAL_COPY(gecos);
334 DLINK_FOREACH(ptr, list->head)
336 target_p = ptr->data;
337 if(!IsPerson(target_p))
338 continue;
340 if(EmptyString(target_p->sockhost))
341 sockhost = empty_sockhost;
342 else if(!show_ip(source_p, target_p))
343 sockhost = spoofed_sockhost;
344 else
345 sockhost = target_p->sockhost;
347 if(match(username, target_p->username) &&
348 (match(hostname, target_p->host) ||
349 match(hostname, target_p->orighost) ||
350 match(hostname, sockhost) || match_ips(hostname, sockhost)))
352 if(name != NULL && !match(name, target_p->name))
353 continue;
355 if(mangle_gecos != NULL && !match_esc(mangle_gecos, target_p->info))
356 continue;
358 sendto_one(source_p, form_str(RPL_ETRACE),
359 me.name, source_p->name,
360 IsOper(target_p) ? "Oper" : "User",
361 /* class field -- pretend its server.. */
362 target_p->servptr->name,
363 target_p->name, target_p->username, target_p->host,
364 sockhost, target_p->info);
369 static int
370 mo_masktrace(struct Client *client_p, struct Client *source_p, int parc,
371 const char *parv[])
373 char *name, *username, *hostname, *gecos;
374 const char *mask;
375 int auspex = 0;
377 mask = parv[1];
378 name = LOCAL_COPY(parv[1]);
379 collapse(name);
381 if(IsAuspex(source_p))
383 auspex = 1;
386 if(parc > 2 && !EmptyString(parv[2]))
388 gecos = LOCAL_COPY(parv[2]);
389 collapse_esc(gecos);
390 } else
391 gecos = NULL;
394 if((hostname = strchr(name, '@')) == NULL)
396 sendto_one(source_p, ":%s NOTICE %s :Invalid parameters", me.name, source_p->name);
397 return 0;
400 *hostname++ = '\0';
402 if((username = strchr(name, '!')) == NULL)
404 username = name;
405 name = NULL;
406 } else
407 *username++ = '\0';
409 if(EmptyString(username) || EmptyString(hostname))
411 sendto_one(source_p, ":%s NOTICE %s :Invalid parameters", me.name, source_p->name);
412 return 0;
415 if(auspex) {
416 match_masktrace(source_p, &global_client_list, username, hostname, name, gecos);
417 } else
418 match_masktrace(source_p, &lclient_list, username, hostname, name, gecos);
420 sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE), me.name);
421 return 0;