]> jfr.im git - irc/rqf/shadowircd.git/blame - modules/m_trace.c
Do not log ERROR messages from unknowns.
[irc/rqf/shadowircd.git] / modules / m_trace.c
CommitLineData
212380e3 1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_trace.c: Traces a path to a client/server.
4 *
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
8 *
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.
13 *
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.
18 *
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
23 *
4d7a1ee5 24 * $Id: m_trace.c 3183 2007-02-01 01:07:42Z jilles $
212380e3 25 */
26
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"
13ae2f4b 34#include "match.h"
212380e3 35#include "ircd.h"
36#include "numeric.h"
212380e3 37#include "s_serv.h"
38#include "s_conf.h"
39#include "s_newconf.h"
40#include "send.h"
41#include "msg.h"
42#include "parse.h"
43#include "modules.h"
44
45static int m_trace(struct Client *, struct Client *, int, const char **);
46
47static void trace_spy(struct Client *, struct Client *);
48
49struct 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}}
52};
53
54int doing_trace_hook;
55
56mapi_clist_av1 trace_clist[] = { &trace_msgtab, NULL };
57mapi_hlist_av1 trace_hlist[] = {
58 { "doing_trace", &doing_trace_hook },
59 { NULL, NULL }
60};
4d7a1ee5 61DECLARE_MODULE_AV1(trace, NULL, NULL, trace_clist, trace_hlist, NULL, "$Revision: 3183 $");
212380e3 62
63static void count_downlinks(struct Client *server_p, int *pservcount, int *pusercount);
a70bad1d 64static int report_this_status(struct Client *source_p, struct Client *target_p);
212380e3 65
51b0223e
VY
66static const char *empty_sockhost = "255.255.255.255";
67
212380e3 68/*
69 * m_trace
70 * parv[0] = sender prefix
71 * parv[1] = servername
72 */
73static int
74m_trace(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
75{
76 struct Client *target_p = NULL;
77 struct Class *cltmp;
78 const char *tname;
79 int doall = 0;
80 int cnt = 0, wilds, dow;
08d11e34 81 rb_dlink_node *ptr;
212380e3 82
83 if(parc > 1)
84 {
85 tname = parv[1];
86
87 if(parc > 2)
88 {
89 if(hunt_server(client_p, source_p, ":%s TRACE %s :%s", 2, parc, parv) !=
90 HUNTED_ISME)
91 return 0;
92 }
93 }
94 else
95 tname = me.name;
96
97 /* if we have 3 parameters, then the command is directed at us. So
98 * we shouldnt be forwarding it anywhere.
99 */
100 if(parc < 3)
101 {
102 switch (hunt_server(client_p, source_p, ":%s TRACE :%s", 1, parc, parv))
103 {
104 case HUNTED_PASS: /* note: gets here only if parv[1] exists */
105 {
106 struct Client *ac2ptr;
107
108 if(MyClient(source_p))
109 ac2ptr = find_named_client(tname);
110 else
111 ac2ptr = find_client(tname);
112
113 if(ac2ptr == NULL)
114 {
08d11e34 115 RB_DLINK_FOREACH(ptr, global_client_list.head)
212380e3 116 {
117 ac2ptr = ptr->data;
118
4d7a1ee5 119 if(match(tname, ac2ptr->name))
212380e3 120 break;
121 else
122 ac2ptr = NULL;
123 }
124 }
125
126 /* giving this out with flattened links defeats the
127 * object --fl
128 */
129 if(IsOper(source_p) || IsExemptShide(source_p) ||
130 !ConfigServerHide.flatten_links)
131 sendto_one_numeric(source_p, RPL_TRACELINK,
132 form_str(RPL_TRACELINK),
133 ircd_version,
134 ac2ptr ? ac2ptr->name : tname,
135 ac2ptr ? ac2ptr->from->name : "EEK!");
136
137 return 0;
138 }
139
140 case HUNTED_ISME:
141 break;
142
143 default:
144 return 0;
145 }
146 }
147
148 if(match(tname, me.name))
149 {
150 doall = 1;
151 }
152 /* if theyre tracing our SID, we need to move tname to our name so
153 * we dont give the sid in ENDOFTRACE
154 */
155 else if(!MyClient(source_p) && !strcmp(tname, me.id))
156 {
157 doall = 1;
158 tname = me.name;
159 }
160
161 wilds = strchr(tname, '*') || strchr(tname, '?');
162 dow = wilds || doall;
163
164 /* specific trace */
165 if(dow == 0)
166 {
167 if(MyClient(source_p) || parc > 2)
168 target_p = find_named_person(tname);
169 else
170 target_p = find_person(tname);
171
172 /* tname could be pointing to an ID at this point, so reset
173 * it to target_p->name if we have a target --fl
174 */
175 if(target_p != NULL)
176 {
a70bad1d 177 report_this_status(source_p, target_p);
212380e3 178 tname = target_p->name;
179 }
180
181 trace_spy(source_p, target_p);
182
183 sendto_one_numeric(source_p, RPL_ENDOFTRACE,
184 form_str(RPL_ENDOFTRACE), tname);
185 return 0;
186 }
187
188 trace_spy(source_p, NULL);
189
190 /* give non-opers a limited trace output of themselves (if local),
191 * opers and servers (if no shide) --fl
192 */
193 if(!IsOper(source_p))
194 {
195 if(MyClient(source_p))
196 {
197 if(doall || (wilds && match(tname, source_p->name)))
a70bad1d 198 report_this_status(source_p, source_p);
212380e3 199 }
200
08d11e34 201 RB_DLINK_FOREACH(ptr, local_oper_list.head)
212380e3 202 {
203 target_p = ptr->data;
204
205 if(!doall && wilds && (match(tname, target_p->name) == 0))
206 continue;
207
a70bad1d 208 report_this_status(source_p, target_p);
212380e3 209 }
210
211 if (IsExemptShide(source_p) || !ConfigServerHide.flatten_links)
212 {
08d11e34 213 RB_DLINK_FOREACH(ptr, serv_list.head)
212380e3 214 {
215 target_p = ptr->data;
216
217 if(!doall && wilds && !match(tname, target_p->name))
218 continue;
219
a70bad1d 220 report_this_status(source_p, target_p);
212380e3 221 }
222 }
223
224 sendto_one_numeric(source_p, RPL_ENDOFTRACE,
225 form_str(RPL_ENDOFTRACE), tname);
226 return 0;
227 }
228
229 /* source_p is opered */
230
231 /* report all direct connections */
08d11e34 232 RB_DLINK_FOREACH(ptr, lclient_list.head)
212380e3 233 {
234 target_p = ptr->data;
235
236 /* dont show invisible users to remote opers */
237 if(IsInvisible(target_p) && dow && !MyConnect(source_p) && !IsOper(target_p))
238 continue;
239
240 if(!doall && wilds && !match(tname, target_p->name))
241 continue;
242
823d0e7a
JT
243 /* remote opers may not see invisible normal users */
244 if(dow && !MyConnect(source_p) && !IsOper(target_p) &&
245 IsInvisible(target_p))
246 continue;
247
a70bad1d 248 cnt = report_this_status(source_p, target_p);
212380e3 249 }
250
08d11e34 251 RB_DLINK_FOREACH(ptr, serv_list.head)
212380e3 252 {
253 target_p = ptr->data;
254
255 if(!doall && wilds && !match(tname, target_p->name))
256 continue;
257
a70bad1d 258 cnt = report_this_status(source_p, target_p);
212380e3 259 }
260
261 if(MyConnect(source_p))
262 {
08d11e34 263 RB_DLINK_FOREACH(ptr, unknown_list.head)
212380e3 264 {
265 target_p = ptr->data;
266
267 if(!doall && wilds && !match(tname, target_p->name))
268 continue;
269
a70bad1d 270 cnt = report_this_status(source_p, target_p);
212380e3 271 }
272 }
273
274 if(!cnt)
275 {
276 sendto_one_numeric(source_p, ERR_NOSUCHSERVER, form_str(ERR_NOSUCHSERVER),
277 tname);
278
279 /* let the user have some idea that its at the end of the
280 * trace
281 */
282 sendto_one_numeric(source_p, RPL_ENDOFTRACE,
283 form_str(RPL_ENDOFTRACE), tname);
284 return 0;
285 }
286
287 if(doall)
288 {
08d11e34 289 RB_DLINK_FOREACH(ptr, class_list.head)
212380e3 290 {
291 cltmp = ptr->data;
292
293 if(CurrUsers(cltmp) > 0)
294 sendto_one_numeric(source_p, RPL_TRACECLASS,
295 form_str(RPL_TRACECLASS),
296 ClassName(cltmp), CurrUsers(cltmp));
297 }
298 }
299
300 sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE), tname);
301
302 return 0;
303}
304
305/*
306 * count_downlinks
307 *
308 * inputs - pointer to server to count
309 * - pointers to server and user count
310 * output - NONE
311 * side effects - server and user counts are added to given values
312 */
313static void
314count_downlinks(struct Client *server_p, int *pservcount, int *pusercount)
315{
08d11e34 316 rb_dlink_node *ptr;
212380e3 317
318 (*pservcount)++;
08d11e34
WP
319 *pusercount += rb_dlink_list_length(&server_p->serv->users);
320 RB_DLINK_FOREACH(ptr, server_p->serv->servers.head)
212380e3 321 {
322 count_downlinks(ptr->data, pservcount, pusercount);
323 }
324}
325
326/*
327 * report_this_status
328 *
329 * inputs - pointer to client to report to
330 * - pointer to client to report about
331 * output - counter of number of hits
332 * side effects - NONE
333 */
334static int
a70bad1d 335report_this_status(struct Client *source_p, struct Client *target_p)
212380e3 336{
337 const char *name;
338 const char *class_name;
339 char ip[HOSTIPLEN];
340 int cnt = 0;
341
342 /* sanity check - should never happen */
343 if(!MyConnect(target_p))
344 return 0;
345
9879cd59 346 rb_inet_ntop_sock((struct sockaddr *)&target_p->localClient->ip, ip, sizeof(ip));
212380e3 347 class_name = get_client_class(target_p);
348
349 if(IsAnyServer(target_p))
715b28fe 350 name = target_p->name;
212380e3 351 else
352 name = get_client_name(target_p, HIDE_IP);
353
354 switch (target_p->status)
355 {
356 case STAT_CONNECTING:
357 sendto_one_numeric(source_p, RPL_TRACECONNECTING,
358 form_str(RPL_TRACECONNECTING),
359 class_name, name);
360 cnt++;
361 break;
362
363 case STAT_HANDSHAKE:
364 sendto_one_numeric(source_p, RPL_TRACEHANDSHAKE,
365 form_str(RPL_TRACEHANDSHAKE),
366 class_name, name);
367 cnt++;
368 break;
369
370 case STAT_ME:
371 break;
372
373 case STAT_UNKNOWN:
374 /* added time -Taner */
375 sendto_one_numeric(source_p, RPL_TRACEUNKNOWN,
376 form_str(RPL_TRACEUNKNOWN),
377 class_name, name, ip,
9f6bbe3c 378 rb_current_time() - target_p->localClient->firsttime);
212380e3 379 cnt++;
380 break;
381
382 case STAT_CLIENT:
346aca17
JT
383 {
384 int tnumeric;
212380e3 385
346aca17
JT
386 tnumeric = IsOper(target_p) ? RPL_TRACEOPERATOR : RPL_TRACEUSER;
387 sendto_one_numeric(source_p, tnumeric, form_str(tnumeric),
388 class_name, name,
389 show_ip(source_p, target_p) ? ip : empty_sockhost,
390 rb_current_time() - target_p->localClient->lasttime,
391 rb_current_time() - target_p->localClient->last);
392
393 cnt++;
394 }
a70bad1d 395 break;
212380e3 396
397 case STAT_SERVER:
398 {
399 int usercount = 0;
400 int servcount = 0;
401
402 count_downlinks(target_p, &servcount, &usercount);
403
404 sendto_one_numeric(source_p, RPL_TRACESERVER, form_str(RPL_TRACESERVER),
405 class_name, servcount, usercount, name,
406 *(target_p->serv->by) ? target_p->serv->by : "*", "*",
9f6bbe3c 407 me.name, rb_current_time() - target_p->localClient->lasttime);
212380e3 408 cnt++;
409
410 }
411 break;
412
413 default: /* ...we actually shouldn't come here... --msa */
414 sendto_one_numeric(source_p, RPL_TRACENEWTYPE,
415 form_str(RPL_TRACENEWTYPE),
416 me.name, source_p->name, name);
417 cnt++;
418 break;
419 }
420
421 return (cnt);
422}
423
424/* trace_spy()
425 *
426 * input - pointer to client
427 * output - none
428 * side effects - hook event doing_trace is called
429 */
430static void
431trace_spy(struct Client *source_p, struct Client *target_p)
432{
433 hook_data_client hdata;
434
435 hdata.client = source_p;
436 hdata.target = target_p;
437
438 call_hook(doing_trace_hook, &hdata);
439}