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
24 * $Id: m_trace.c 3183 2007-02-01 01:07:42Z jilles $
39 #include "s_newconf.h"
45 static int m_trace(struct Client
*, struct Client
*, int, const char **);
47 static void trace_spy(struct Client
*, struct Client
*);
49 struct Message trace_msgtab
= {
50 "TRACE", 0, 0, 0, MFLG_SLOW
,
51 {mg_unreg
, {m_trace
, 0}, {m_trace
, 0}, mg_ignore
, mg_ignore
, {m_trace
, 0}}
56 mapi_clist_av1 trace_clist
[] = { &trace_msgtab
, NULL
};
57 mapi_hlist_av1 trace_hlist
[] = {
58 { "doing_trace", &doing_trace_hook
},
61 DECLARE_MODULE_AV1(trace
, NULL
, NULL
, trace_clist
, trace_hlist
, NULL
, "$Revision: 3183 $");
63 static void count_downlinks(struct Client
*server_p
, int *pservcount
, int *pusercount
);
64 static int report_this_status(struct Client
*source_p
, struct Client
*target_p
, int dow
);
68 * parv[0] = sender prefix
69 * parv[1] = servername
72 m_trace(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
74 struct Client
*target_p
= NULL
;
78 int cnt
= 0, wilds
, dow
;
87 if(hunt_server(client_p
, source_p
, ":%s TRACE %s :%s", 2, parc
, parv
) !=
95 /* if we have 3 parameters, then the command is directed at us. So
96 * we shouldnt be forwarding it anywhere.
100 switch (hunt_server(client_p
, source_p
, ":%s TRACE :%s", 1, parc
, parv
))
102 case HUNTED_PASS
: /* note: gets here only if parv[1] exists */
104 struct Client
*ac2ptr
;
106 if(MyClient(source_p
))
107 ac2ptr
= find_named_client(tname
);
109 ac2ptr
= find_client(tname
);
113 RB_DLINK_FOREACH(ptr
, global_client_list
.head
)
117 if(match(tname
, ac2ptr
->name
))
124 /* giving this out with flattened links defeats the
127 if(IsOper(source_p
) || IsExemptShide(source_p
) ||
128 !ConfigServerHide
.flatten_links
)
129 sendto_one_numeric(source_p
, RPL_TRACELINK
,
130 form_str(RPL_TRACELINK
),
132 ac2ptr
? ac2ptr
->name
: tname
,
133 ac2ptr
? ac2ptr
->from
->name
: "EEK!");
146 if(match(tname
, me
.name
))
150 /* if theyre tracing our SID, we need to move tname to our name so
151 * we dont give the sid in ENDOFTRACE
153 else if(!MyClient(source_p
) && !strcmp(tname
, me
.id
))
159 wilds
= strchr(tname
, '*') || strchr(tname
, '?');
160 dow
= wilds
|| doall
;
165 if(MyClient(source_p
) || parc
> 2)
166 target_p
= find_named_person(tname
);
168 target_p
= find_person(tname
);
170 /* tname could be pointing to an ID at this point, so reset
171 * it to target_p->name if we have a target --fl
175 report_this_status(source_p
, target_p
, 0);
176 tname
= target_p
->name
;
179 trace_spy(source_p
, target_p
);
181 sendto_one_numeric(source_p
, RPL_ENDOFTRACE
,
182 form_str(RPL_ENDOFTRACE
), tname
);
186 trace_spy(source_p
, NULL
);
188 /* give non-opers a limited trace output of themselves (if local),
189 * opers and servers (if no shide) --fl
191 if(!IsOper(source_p
))
193 if(MyClient(source_p
))
195 if(doall
|| (wilds
&& match(tname
, source_p
->name
)))
196 report_this_status(source_p
, source_p
, 0);
199 RB_DLINK_FOREACH(ptr
, local_oper_list
.head
)
201 target_p
= ptr
->data
;
203 if(!doall
&& wilds
&& (match(tname
, target_p
->name
) == 0))
206 report_this_status(source_p
, target_p
, 0);
209 if (IsExemptShide(source_p
) || !ConfigServerHide
.flatten_links
)
211 RB_DLINK_FOREACH(ptr
, serv_list
.head
)
213 target_p
= ptr
->data
;
215 if(!doall
&& wilds
&& !match(tname
, target_p
->name
))
218 report_this_status(source_p
, target_p
, 0);
222 sendto_one_numeric(source_p
, RPL_ENDOFTRACE
,
223 form_str(RPL_ENDOFTRACE
), tname
);
227 /* source_p is opered */
229 /* report all direct connections */
230 RB_DLINK_FOREACH(ptr
, lclient_list
.head
)
232 target_p
= ptr
->data
;
234 /* dont show invisible users to remote opers */
235 if(IsInvisible(target_p
) && dow
&& !MyConnect(source_p
) && !IsOper(target_p
))
238 if(!doall
&& wilds
&& !match(tname
, target_p
->name
))
241 cnt
= report_this_status(source_p
, target_p
, dow
);
244 RB_DLINK_FOREACH(ptr
, serv_list
.head
)
246 target_p
= ptr
->data
;
248 if(!doall
&& wilds
&& !match(tname
, target_p
->name
))
251 cnt
= report_this_status(source_p
, target_p
, dow
);
254 if(MyConnect(source_p
))
256 RB_DLINK_FOREACH(ptr
, unknown_list
.head
)
258 target_p
= ptr
->data
;
260 if(!doall
&& wilds
&& !match(tname
, target_p
->name
))
263 cnt
= report_this_status(source_p
, target_p
, dow
);
269 sendto_one_numeric(source_p
, ERR_NOSUCHSERVER
, form_str(ERR_NOSUCHSERVER
),
272 /* let the user have some idea that its at the end of the
275 sendto_one_numeric(source_p
, RPL_ENDOFTRACE
,
276 form_str(RPL_ENDOFTRACE
), tname
);
282 RB_DLINK_FOREACH(ptr
, class_list
.head
)
286 if(CurrUsers(cltmp
) > 0)
287 sendto_one_numeric(source_p
, RPL_TRACECLASS
,
288 form_str(RPL_TRACECLASS
),
289 ClassName(cltmp
), CurrUsers(cltmp
));
293 sendto_one_numeric(source_p
, RPL_ENDOFTRACE
, form_str(RPL_ENDOFTRACE
), tname
);
301 * inputs - pointer to server to count
302 * - pointers to server and user count
304 * side effects - server and user counts are added to given values
307 count_downlinks(struct Client
*server_p
, int *pservcount
, int *pusercount
)
312 *pusercount
+= rb_dlink_list_length(&server_p
->serv
->users
);
313 RB_DLINK_FOREACH(ptr
, server_p
->serv
->servers
.head
)
315 count_downlinks(ptr
->data
, pservcount
, pusercount
);
322 * inputs - pointer to client to report to
323 * - pointer to client to report about
324 * output - counter of number of hits
325 * side effects - NONE
328 report_this_status(struct Client
*source_p
, struct Client
*target_p
,
332 const char *class_name
;
336 /* sanity check - should never happen */
337 if(!MyConnect(target_p
))
340 rb_inet_ntop_sock((struct sockaddr
*)&target_p
->localClient
->ip
, ip
, sizeof(ip
));
341 class_name
= get_client_class(target_p
);
343 if(IsAnyServer(target_p
))
344 name
= get_server_name(target_p
, HIDE_IP
);
346 name
= get_client_name(target_p
, HIDE_IP
);
348 switch (target_p
->status
)
350 case STAT_CONNECTING
:
351 sendto_one_numeric(source_p
, RPL_TRACECONNECTING
,
352 form_str(RPL_TRACECONNECTING
),
358 sendto_one_numeric(source_p
, RPL_TRACEHANDSHAKE
,
359 form_str(RPL_TRACEHANDSHAKE
),
368 /* added time -Taner */
369 sendto_one_numeric(source_p
, RPL_TRACEUNKNOWN
,
370 form_str(RPL_TRACEUNKNOWN
),
371 class_name
, name
, ip
,
372 rb_current_time() - target_p
->localClient
->firsttime
);
377 /* Only opers see users if there is a wildcard
378 * but anyone can see all the opers.
380 if((IsOper(source_p
) &&
381 (MyClient(source_p
) || !(dow
&& IsInvisible(target_p
))))
382 || !dow
|| IsOper(target_p
) || (source_p
== target_p
))
385 sendto_one_numeric(source_p
, RPL_TRACEOPERATOR
,
386 form_str(RPL_TRACEOPERATOR
),
388 show_ip(source_p
, target_p
) ? ip
: "255.255.255.255",
389 rb_current_time() - target_p
->localClient
->lasttime
,
390 rb_current_time() - target_p
->localClient
->last
);
393 sendto_one_numeric(source_p
, RPL_TRACEUSER
,
394 form_str(RPL_TRACEUSER
),
396 show_ip(source_p
, target_p
) ? ip
: "255.255.255.255",
397 rb_current_time() - target_p
->localClient
->lasttime
,
398 rb_current_time() - target_p
->localClient
->last
);
408 count_downlinks(target_p
, &servcount
, &usercount
);
410 sendto_one_numeric(source_p
, RPL_TRACESERVER
, form_str(RPL_TRACESERVER
),
411 class_name
, servcount
, usercount
, name
,
412 *(target_p
->serv
->by
) ? target_p
->serv
->by
: "*", "*",
413 me
.name
, rb_current_time() - target_p
->localClient
->lasttime
);
419 default: /* ...we actually shouldn't come here... --msa */
420 sendto_one_numeric(source_p
, RPL_TRACENEWTYPE
,
421 form_str(RPL_TRACENEWTYPE
),
422 me
.name
, source_p
->name
, name
);
432 * input - pointer to client
434 * side effects - hook event doing_trace is called
437 trace_spy(struct Client
*source_p
, struct Client
*target_p
)
439 hook_data_client hdata
;
441 hdata
.client
= source_p
;
442 hdata
.target
= target_p
;
444 call_hook(doing_trace_hook
, &hdata
);