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
36 #include "s_newconf.h"
42 static const char trace_desc
[] =
43 "Provides the TRACE command to trace the route to a client or server";
45 static void m_trace(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
47 static void trace_spy(struct Client
*, struct Client
*);
49 struct Message trace_msgtab
= {
51 {mg_unreg
, {m_trace
, 0}, {m_trace
, 0}, mg_ignore
, mg_ignore
, {m_trace
, 0}}
55 int doing_trace_show_idle_hook
;
57 mapi_clist_av1 trace_clist
[] = { &trace_msgtab
, NULL
};
58 mapi_hlist_av1 trace_hlist
[] = {
59 { "doing_trace", &doing_trace_hook
},
60 { "doing_trace_show_idle", &doing_trace_show_idle_hook
},
63 DECLARE_MODULE_AV2(trace
, NULL
, NULL
, trace_clist
, trace_hlist
, NULL
, NULL
, NULL
, trace_desc
);
65 static void count_downlinks(struct Client
*server_p
, int *pservcount
, int *pusercount
);
66 static int report_this_status(struct Client
*source_p
, struct Client
*target_p
);
68 static const char *empty_sockhost
= "255.255.255.255";
72 * parv[1] = servername
75 m_trace(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
77 struct Client
*target_p
= NULL
;
80 bool doall
= false, wilds
, dow
;
90 if(hunt_server(client_p
, source_p
, ":%s TRACE %s :%s", 2, parc
, parv
) !=
98 /* if we have 3 parameters, then the command is directed at us. So
99 * we shouldnt be forwarding it anywhere.
103 switch (hunt_server(client_p
, source_p
, ":%s TRACE :%s", 1, parc
, parv
))
105 case HUNTED_PASS
: /* note: gets here only if parv[1] exists */
107 struct Client
*ac2ptr
;
109 if(MyClient(source_p
))
110 ac2ptr
= find_named_client(tname
);
112 ac2ptr
= find_client(tname
);
116 RB_DLINK_FOREACH(ptr
, global_serv_list
.head
)
120 if(match(tname
, ac2ptr
->name
))
127 /* giving this out with flattened links defeats the
130 if(IsOperGeneral(source_p
) || IsExemptShide(source_p
) ||
131 !ConfigServerHide
.flatten_links
)
132 sendto_one_numeric(source_p
, RPL_TRACELINK
,
133 form_str(RPL_TRACELINK
),
135 ac2ptr
? ac2ptr
->name
: tname
,
136 ac2ptr
? ac2ptr
->from
->name
: "EEK!");
149 if(match(tname
, me
.name
))
153 /* if theyre tracing our SID, we need to move tname to our name so
154 * we dont give the sid in ENDOFTRACE
156 else if(!MyClient(source_p
) && !strcmp(tname
, me
.id
))
162 wilds
= strchr(tname
, '*') || strchr(tname
, '?');
163 dow
= wilds
|| doall
;
168 if(MyClient(source_p
) || parc
> 2)
169 target_p
= find_named_person(tname
);
171 target_p
= find_person(tname
);
173 /* tname could be pointing to an ID at this point, so reset
174 * it to target_p->name if we have a target --fl
178 report_this_status(source_p
, target_p
);
179 tname
= target_p
->name
;
182 trace_spy(source_p
, target_p
);
184 sendto_one_numeric(source_p
, RPL_ENDOFTRACE
,
185 form_str(RPL_ENDOFTRACE
), tname
);
189 trace_spy(source_p
, NULL
);
191 /* give non-opers a limited trace output of themselves (if local),
192 * opers and servers (if no shide) --fl
194 if(!IsOper(source_p
))
196 if(MyClient(source_p
))
198 if(doall
|| (wilds
&& match(tname
, source_p
->name
)))
199 report_this_status(source_p
, source_p
);
202 RB_DLINK_FOREACH(ptr
, local_oper_list
.head
)
204 target_p
= ptr
->data
;
206 if(!doall
&& wilds
&& (match(tname
, target_p
->name
) == 0))
209 if(!SeesOper(target_p
, source_p
))
212 report_this_status(source_p
, target_p
);
215 if (IsExemptShide(source_p
) || !ConfigServerHide
.flatten_links
)
217 RB_DLINK_FOREACH(ptr
, serv_list
.head
)
219 target_p
= ptr
->data
;
221 if(!doall
&& wilds
&& !match(tname
, target_p
->name
))
224 report_this_status(source_p
, target_p
);
228 sendto_one_numeric(source_p
, RPL_ENDOFTRACE
,
229 form_str(RPL_ENDOFTRACE
), tname
);
233 /* source_p is opered */
235 /* report all direct connections */
236 RB_DLINK_FOREACH(ptr
, lclient_list
.head
)
238 target_p
= ptr
->data
;
240 /* dont show invisible users to remote opers */
241 if(IsInvisible(target_p
) && dow
&& !MyConnect(source_p
) && !SeesOper(target_p
, source_p
))
244 if(!doall
&& wilds
&& !match(tname
, target_p
->name
))
247 /* remote opers may not see invisible normal users */
248 if(dow
&& !MyConnect(source_p
) && !SeesOper(target_p
, source_p
) &&
249 IsInvisible(target_p
))
252 cnt
= report_this_status(source_p
, target_p
);
255 RB_DLINK_FOREACH(ptr
, serv_list
.head
)
257 target_p
= ptr
->data
;
259 if(!doall
&& wilds
&& !match(tname
, target_p
->name
))
262 cnt
= report_this_status(source_p
, target_p
);
265 if(MyConnect(source_p
))
267 RB_DLINK_FOREACH(ptr
, unknown_list
.head
)
269 target_p
= ptr
->data
;
271 if(!doall
&& wilds
&& !match(tname
, target_p
->name
))
274 cnt
= report_this_status(source_p
, target_p
);
280 sendto_one_numeric(source_p
, ERR_NOSUCHSERVER
, form_str(ERR_NOSUCHSERVER
),
283 /* let the user have some idea that its at the end of the
286 sendto_one_numeric(source_p
, RPL_ENDOFTRACE
,
287 form_str(RPL_ENDOFTRACE
), tname
);
293 RB_DLINK_FOREACH(ptr
, class_list
.head
)
297 if(CurrUsers(cltmp
) > 0)
298 sendto_one_numeric(source_p
, RPL_TRACECLASS
,
299 form_str(RPL_TRACECLASS
),
300 ClassName(cltmp
), CurrUsers(cltmp
));
304 sendto_one_numeric(source_p
, RPL_ENDOFTRACE
, form_str(RPL_ENDOFTRACE
), tname
);
310 * inputs - pointer to server to count
311 * - pointers to server and user count
313 * side effects - server and user counts are added to given values
316 count_downlinks(struct Client
*server_p
, int *pservcount
, int *pusercount
)
321 *pusercount
+= rb_dlink_list_length(&server_p
->serv
->users
);
322 RB_DLINK_FOREACH(ptr
, server_p
->serv
->servers
.head
)
324 count_downlinks(ptr
->data
, pservcount
, pusercount
);
331 * inputs - pointer to client to report to
332 * - pointer to client to report about
333 * output - counter of number of hits
334 * side effects - NONE
337 report_this_status(struct Client
*source_p
, struct Client
*target_p
)
340 const char *class_name
;
344 /* sanity check - should never happen */
345 if(!MyConnect(target_p
))
348 rb_inet_ntop_sock((struct sockaddr
*)&target_p
->localClient
->ip
, ip
, sizeof(ip
));
349 class_name
= get_client_class(target_p
);
351 if(IsAnyServer(target_p
))
352 name
= target_p
->name
;
354 name
= get_client_name(target_p
, HIDE_IP
);
356 switch (target_p
->status
)
358 case STAT_CONNECTING
:
359 sendto_one_numeric(source_p
, RPL_TRACECONNECTING
,
360 form_str(RPL_TRACECONNECTING
),
366 sendto_one_numeric(source_p
, RPL_TRACEHANDSHAKE
,
367 form_str(RPL_TRACEHANDSHAKE
),
376 /* added time -Taner */
377 sendto_one_numeric(source_p
, RPL_TRACEUNKNOWN
,
378 form_str(RPL_TRACEUNKNOWN
),
379 class_name
, name
, ip
,
380 (unsigned long)(rb_current_time() - target_p
->localClient
->firsttime
));
386 /* fire the doing_trace_show_idle hook to allow modules to tell us whether to show the idle time */
387 hook_data_client_approval hdata_showidle
;
389 hdata_showidle
.client
= source_p
;
390 hdata_showidle
.target
= target_p
;
391 hdata_showidle
.approved
= WHOIS_IDLE_SHOW
;
393 call_hook(doing_trace_show_idle_hook
, &hdata_showidle
);
395 sendto_one_numeric(source_p
,
396 SeesOper(target_p
, source_p
) ? RPL_TRACEOPERATOR
: RPL_TRACEUSER
,
397 SeesOper(target_p
, source_p
) ? form_str(RPL_TRACEOPERATOR
) : form_str(RPL_TRACEUSER
),
399 show_ip(source_p
, target_p
) ? ip
: empty_sockhost
,
400 hdata_showidle
.approved
? (unsigned long)(rb_current_time() - target_p
->localClient
->lasttime
) : 0,
401 hdata_showidle
.approved
? (unsigned long)(rb_current_time() - target_p
->localClient
->last
) : 0);
412 count_downlinks(target_p
, &servcount
, &usercount
);
414 sendto_one_numeric(source_p
, RPL_TRACESERVER
, form_str(RPL_TRACESERVER
),
415 class_name
, servcount
, usercount
, name
,
416 *(target_p
->serv
->by
) ? target_p
->serv
->by
: "*", "*",
418 (unsigned long)(rb_current_time() - target_p
->localClient
->lasttime
));
424 default: /* ...we actually shouldn't come here... --msa */
425 sendto_one_numeric(source_p
, RPL_TRACENEWTYPE
,
426 form_str(RPL_TRACENEWTYPE
), name
);
436 * input - pointer to client
438 * side effects - hook event doing_trace is called
441 trace_spy(struct Client
*source_p
, struct Client
*target_p
)
443 hook_data_client hdata
;
445 hdata
.client
= source_p
;
446 hdata
.target
= target_p
;
448 call_hook(doing_trace_hook
, &hdata
);