Clean.
[seven-1.x.git] / modules / m_trace.c
blob1c34e144bb39c90c695761f2eb556cecfbbfc266
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_trace.c: Traces a path to a client/server.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
24 * $Id: m_trace.c 147 2006-11-13 12:35:45Z spb $
27 #include "stdinc.h"
28 #include "class.h"
29 #include "hook.h"
30 #include "client.h"
31 #include "hash.h"
32 #include "common.h"
33 #include "hash.h"
34 #include "irc_string.h"
35 #include "ircd.h"
36 #include "numeric.h"
37 #include "commio.h"
38 #include "s_serv.h"
39 #include "s_conf.h"
40 #include "s_newconf.h"
41 #include "send.h"
42 #include "msg.h"
43 #include "parse.h"
44 #include "modules.h"
46 static int m_trace(struct Client *, struct Client *, int, const char **);
48 static void trace_spy(struct Client *, struct Client *);
50 struct Message trace_msgtab = {
51 "TRACE", 0, 0, 0, MFLG_SLOW,
52 {mg_unreg, {m_trace, 0}, {m_trace, 0}, mg_ignore, mg_ignore, {m_trace, 0}}
55 int doing_trace_hook;
57 mapi_clist_av1 trace_clist[] = { &trace_msgtab, NULL };
58 mapi_hlist_av1 trace_hlist[] = {
59 { "doing_trace", &doing_trace_hook },
60 { NULL, NULL }
62 DECLARE_MODULE_AV1(trace, NULL, NULL, trace_clist, trace_hlist, NULL, "$Revision: 147 $");
64 static void count_downlinks(struct Client *server_p, int *pservcount, int *pusercount);
65 static int report_this_status(struct Client *source_p, struct Client *target_p, int dow);
68 * m_trace
69 * parv[0] = sender prefix
70 * parv[1] = servername
72 static int
73 m_trace(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
75 struct Client *target_p = NULL;
76 struct Class *cltmp;
77 const char *tname;
78 int doall = 0;
79 int cnt = 0, wilds, dow;
80 dlink_node *ptr;
82 if(parc > 1)
84 tname = parv[1];
86 if(parc > 2)
88 if(hunt_server(client_p, source_p, ":%s TRACE %s :%s", 2, parc, parv) !=
89 HUNTED_ISME)
90 return 0;
93 else
94 tname = me.name;
96 /* if we have 3 parameters, then the command is directed at us. So
97 * we shouldnt be forwarding it anywhere.
99 if(parc < 3)
101 switch (hunt_server(client_p, source_p, ":%s TRACE :%s", 1, parc, parv))
103 case HUNTED_PASS: /* note: gets here only if parv[1] exists */
105 struct Client *ac2ptr;
107 if(MyClient(source_p))
108 ac2ptr = find_named_client(tname);
109 else
110 ac2ptr = find_client(tname);
112 if(ac2ptr == NULL)
114 DLINK_FOREACH(ptr, global_client_list.head)
116 ac2ptr = ptr->data;
118 if(match(tname, ac2ptr->name) || match(ac2ptr->name, tname))
119 break;
120 else
121 ac2ptr = NULL;
125 /* giving this out with flattened links defeats the
126 * object --fl
128 if(IsOperRouting(source_p) || IsExemptShide(source_p) ||
129 !ConfigServerHide.flatten_links)
130 sendto_one_numeric(source_p, RPL_TRACELINK,
131 form_str(RPL_TRACELINK),
132 ircd_version,
133 ac2ptr ? ac2ptr->name : tname,
134 ac2ptr ? ac2ptr->from->name : "EEK!");
136 return 0;
139 case HUNTED_ISME:
140 break;
142 default:
143 return 0;
147 if(match(tname, me.name))
149 doall = 1;
151 /* if theyre tracing our SID, we need to move tname to our name so
152 * we dont give the sid in ENDOFTRACE
154 else if(!MyClient(source_p) && !strcmp(tname, me.id))
156 doall = 1;
157 tname = me.name;
160 wilds = strchr(tname, '*') || strchr(tname, '?');
161 dow = wilds || doall;
163 /* specific trace */
164 if(dow == 0)
166 if(MyClient(source_p) || parc > 2)
167 target_p = find_named_person(tname);
168 else
169 target_p = find_person(tname);
171 /* tname could be pointing to an ID at this point, so reset
172 * it to target_p->name if we have a target --fl
174 if(target_p != NULL)
176 report_this_status(source_p, target_p, 0);
177 tname = target_p->name;
180 trace_spy(source_p, target_p);
182 sendto_one_numeric(source_p, RPL_ENDOFTRACE,
183 form_str(RPL_ENDOFTRACE), tname);
184 return 0;
187 trace_spy(source_p, NULL);
189 /* give non-opers a limited trace output of themselves (if local),
190 * opers and servers (if no shide) --fl
192 if(!IsOperRouting(source_p))
194 if(MyClient(source_p))
196 if(doall || (wilds && match(tname, source_p->name)))
197 report_this_status(source_p, source_p, 0);
200 DLINK_FOREACH(ptr, local_oper_list.head)
202 target_p = ptr->data;
204 if(!doall && wilds && (match(tname, target_p->name) == 0))
205 continue;
207 report_this_status(source_p, target_p, 0);
210 if (IsExemptShide(source_p) || !ConfigServerHide.flatten_links)
212 DLINK_FOREACH(ptr, serv_list.head)
214 target_p = ptr->data;
216 if(!doall && wilds && !match(tname, target_p->name))
217 continue;
219 report_this_status(source_p, target_p, 0);
223 sendto_one_numeric(source_p, RPL_ENDOFTRACE,
224 form_str(RPL_ENDOFTRACE), tname);
225 return 0;
228 /* source_p is opered */
230 /* report all direct connections */
231 DLINK_FOREACH(ptr, lclient_list.head)
233 target_p = ptr->data;
235 /* dont show invisible users to remote opers */
236 if(IsInvisible(target_p) && dow && !MyConnect(source_p) && !IsOper(target_p))
237 continue;
239 if(!doall && wilds && !match(tname, target_p->name))
240 continue;
242 cnt = report_this_status(source_p, target_p, dow);
245 DLINK_FOREACH(ptr, serv_list.head)
247 target_p = ptr->data;
249 if(!doall && wilds && !match(tname, target_p->name))
250 continue;
252 cnt = report_this_status(source_p, target_p, dow);
255 if(MyConnect(source_p))
257 DLINK_FOREACH(ptr, unknown_list.head)
259 target_p = ptr->data;
261 if(!doall && wilds && !match(tname, target_p->name))
262 continue;
264 cnt = report_this_status(source_p, target_p, dow);
268 if(!cnt)
270 sendto_one_numeric(source_p, ERR_NOSUCHSERVER, form_str(ERR_NOSUCHSERVER),
271 tname);
273 /* let the user have some idea that its at the end of the
274 * trace
276 sendto_one_numeric(source_p, RPL_ENDOFTRACE,
277 form_str(RPL_ENDOFTRACE), tname);
278 return 0;
281 if(doall)
283 DLINK_FOREACH(ptr, class_list.head)
285 cltmp = ptr->data;
287 if(CurrUsers(cltmp) > 0)
288 sendto_one_numeric(source_p, RPL_TRACECLASS,
289 form_str(RPL_TRACECLASS),
290 ClassName(cltmp), CurrUsers(cltmp));
294 sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE), tname);
296 return 0;
300 * count_downlinks
302 * inputs - pointer to server to count
303 * - pointers to server and user count
304 * output - NONE
305 * side effects - server and user counts are added to given values
307 static void
308 count_downlinks(struct Client *server_p, int *pservcount, int *pusercount)
310 dlink_node *ptr;
312 (*pservcount)++;
313 *pusercount += dlink_list_length(&server_p->serv->users);
314 DLINK_FOREACH(ptr, server_p->serv->servers.head)
316 count_downlinks(ptr->data, pservcount, pusercount);
321 * report_this_status
323 * inputs - pointer to client to report to
324 * - pointer to client to report about
325 * output - counter of number of hits
326 * side effects - NONE
328 static int
329 report_this_status(struct Client *source_p, struct Client *target_p,
330 int dow)
332 const char *name;
333 const char *class_name;
334 char ip[HOSTIPLEN];
335 int cnt = 0;
337 /* sanity check - should never happen */
338 if(!MyConnect(target_p))
339 return 0;
341 inetntop_sock((struct sockaddr *)&target_p->localClient->ip, ip, sizeof(ip));
342 class_name = get_client_class(target_p);
344 if(IsAnyServer(target_p))
345 name = get_server_name(target_p, HIDE_IP);
346 else
347 name = get_client_name(target_p, HIDE_IP);
349 switch (target_p->status)
351 case STAT_CONNECTING:
352 sendto_one_numeric(source_p, RPL_TRACECONNECTING,
353 form_str(RPL_TRACECONNECTING),
354 class_name, name);
355 cnt++;
356 break;
358 case STAT_HANDSHAKE:
359 sendto_one_numeric(source_p, RPL_TRACEHANDSHAKE,
360 form_str(RPL_TRACEHANDSHAKE),
361 class_name, name);
362 cnt++;
363 break;
365 case STAT_ME:
366 break;
368 case STAT_UNKNOWN:
369 /* added time -Taner */
370 sendto_one_numeric(source_p, RPL_TRACEUNKNOWN,
371 form_str(RPL_TRACEUNKNOWN),
372 class_name, name, ip,
373 CurrentTime - target_p->localClient->firsttime);
374 cnt++;
375 break;
377 case STAT_CLIENT:
378 /* Only opers see users if there is a wildcard
379 * but anyone can see all the opers.
381 if((IsOper(source_p) &&
382 (MyClient(source_p) || !(dow && IsInvisible(target_p))))
383 || !dow || IsOper(target_p) || (source_p == target_p))
385 if(IsOper(target_p))
386 sendto_one_numeric(source_p, RPL_TRACEOPERATOR,
387 form_str(RPL_TRACEOPERATOR),
388 class_name, name,
389 show_ip(source_p, target_p) ? ip : "255.255.255.255",
390 CurrentTime - target_p->localClient->lasttime,
391 CurrentTime - target_p->localClient->last);
393 else
394 sendto_one_numeric(source_p, RPL_TRACEUSER,
395 form_str(RPL_TRACEUSER),
396 class_name, name,
397 show_ip(source_p, target_p) ? ip : "255.255.255.255",
398 CurrentTime - target_p->localClient->lasttime,
399 CurrentTime - target_p->localClient->last);
400 cnt++;
402 break;
404 case STAT_SERVER:
406 int usercount = 0;
407 int servcount = 0;
409 count_downlinks(target_p, &servcount, &usercount);
411 sendto_one_numeric(source_p, RPL_TRACESERVER, form_str(RPL_TRACESERVER),
412 class_name, servcount, usercount, name,
413 *(target_p->serv->by) ? target_p->serv->by : "*", "*",
414 me.name, CurrentTime - target_p->localClient->lasttime);
415 cnt++;
418 break;
420 default: /* ...we actually shouldn't come here... --msa */
421 sendto_one_numeric(source_p, RPL_TRACENEWTYPE,
422 form_str(RPL_TRACENEWTYPE),
423 me.name, source_p->name, name);
424 cnt++;
425 break;
428 return (cnt);
431 /* trace_spy()
433 * input - pointer to client
434 * output - none
435 * side effects - hook event doing_trace is called
437 static void
438 trace_spy(struct Client *source_p, struct Client *target_p)
440 hook_data_client hdata;
442 hdata.client = source_p;
443 hdata.target = target_p;
445 call_hook(doing_trace_hook, &hdata);