]> jfr.im git - solanum.git/blame - modules/m_trace.c
make VERSION not include sid (#118)
[solanum.git] / modules / m_trace.c
CommitLineData
212380e3
AC
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
212380e3
AC
23 */
24
25#include "stdinc.h"
26#include "class.h"
27#include "hook.h"
28#include "client.h"
29#include "hash.h"
212380e3 30#include "hash.h"
4562c604 31#include "match.h"
212380e3
AC
32#include "ircd.h"
33#include "numeric.h"
212380e3
AC
34#include "s_serv.h"
35#include "s_conf.h"
36#include "s_newconf.h"
37#include "send.h"
38#include "msg.h"
39#include "parse.h"
40#include "modules.h"
41
be9c3979
AW
42static const char trace_desc[] =
43 "Provides the TRACE command to trace the route to a client or server";
212380e3 44
3c7d6fcc 45static void m_trace(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
eeabf33a 46
212380e3
AC
47static void trace_spy(struct Client *, struct Client *);
48
49struct Message trace_msgtab = {
7baa37a9 50 "TRACE", 0, 0, 0, 0,
212380e3
AC
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};
be9c3979 61DECLARE_MODULE_AV2(trace, NULL, NULL, trace_clist, trace_hlist, NULL, NULL, NULL, trace_desc);
212380e3
AC
62
63static void count_downlinks(struct Client *server_p, int *pservcount, int *pusercount);
a4da8e48 64static int report_this_status(struct Client *source_p, struct Client *target_p);
212380e3 65
a3afc27a
VY
66static const char *empty_sockhost = "255.255.255.255";
67
212380e3
AC
68/*
69 * m_trace
212380e3
AC
70 * parv[1] = servername
71 */
3c7d6fcc 72static void
428ca87b 73m_trace(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3
AC
74{
75 struct Client *target_p = NULL;
76 struct Class *cltmp;
77 const char *tname;
3c7d6fcc
EM
78 bool doall = false, wilds, dow;
79 int cnt = 0;
5b96d9a6 80 rb_dlink_node *ptr;
212380e3
AC
81
82 if(parc > 1)
83 {
84 tname = parv[1];
85
86 if(parc > 2)
87 {
88 if(hunt_server(client_p, source_p, ":%s TRACE %s :%s", 2, parc, parv) !=
89 HUNTED_ISME)
3c7d6fcc 90 return;
212380e3
AC
91 }
92 }
93 else
94 tname = me.name;
95
96 /* if we have 3 parameters, then the command is directed at us. So
97 * we shouldnt be forwarding it anywhere.
98 */
99 if(parc < 3)
100 {
101 switch (hunt_server(client_p, source_p, ":%s TRACE :%s", 1, parc, parv))
102 {
103 case HUNTED_PASS: /* note: gets here only if parv[1] exists */
104 {
105 struct Client *ac2ptr;
106
107 if(MyClient(source_p))
108 ac2ptr = find_named_client(tname);
109 else
110 ac2ptr = find_client(tname);
111
112 if(ac2ptr == NULL)
113 {
2fb07961 114 RB_DLINK_FOREACH(ptr, global_serv_list.head)
212380e3
AC
115 {
116 ac2ptr = ptr->data;
117
4d7a1ee5 118 if(match(tname, ac2ptr->name))
212380e3
AC
119 break;
120 else
121 ac2ptr = NULL;
122 }
123 }
124
125 /* giving this out with flattened links defeats the
126 * object --fl
127 */
d4f7eb4c 128 if(IsOperGeneral(source_p) || IsExemptShide(source_p) ||
212380e3 129 !ConfigServerHide.flatten_links)
55abcbb2 130 sendto_one_numeric(source_p, RPL_TRACELINK,
212380e3 131 form_str(RPL_TRACELINK),
55abcbb2 132 ircd_version,
212380e3
AC
133 ac2ptr ? ac2ptr->name : tname,
134 ac2ptr ? ac2ptr->from->name : "EEK!");
135
3c7d6fcc 136 return;
212380e3
AC
137 }
138
139 case HUNTED_ISME:
140 break;
141
142 default:
3c7d6fcc 143 return;
212380e3
AC
144 }
145 }
146
147 if(match(tname, me.name))
148 {
3c7d6fcc 149 doall = true;
212380e3
AC
150 }
151 /* if theyre tracing our SID, we need to move tname to our name so
152 * we dont give the sid in ENDOFTRACE
153 */
154 else if(!MyClient(source_p) && !strcmp(tname, me.id))
155 {
3c7d6fcc 156 doall = true;
212380e3
AC
157 tname = me.name;
158 }
159
160 wilds = strchr(tname, '*') || strchr(tname, '?');
161 dow = wilds || doall;
162
163 /* specific trace */
3c7d6fcc 164 if(!dow)
212380e3
AC
165 {
166 if(MyClient(source_p) || parc > 2)
167 target_p = find_named_person(tname);
168 else
169 target_p = find_person(tname);
170
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
173 */
174 if(target_p != NULL)
175 {
a4da8e48 176 report_this_status(source_p, target_p);
212380e3
AC
177 tname = target_p->name;
178 }
179
180 trace_spy(source_p, target_p);
181
55abcbb2 182 sendto_one_numeric(source_p, RPL_ENDOFTRACE,
212380e3 183 form_str(RPL_ENDOFTRACE), tname);
3c7d6fcc 184 return;
212380e3
AC
185 }
186
187 trace_spy(source_p, NULL);
188
55abcbb2 189 /* give non-opers a limited trace output of themselves (if local),
212380e3
AC
190 * opers and servers (if no shide) --fl
191 */
192 if(!IsOper(source_p))
193 {
194 if(MyClient(source_p))
195 {
196 if(doall || (wilds && match(tname, source_p->name)))
a4da8e48 197 report_this_status(source_p, source_p);
212380e3
AC
198 }
199
5b96d9a6 200 RB_DLINK_FOREACH(ptr, local_oper_list.head)
212380e3
AC
201 {
202 target_p = ptr->data;
203
204 if(!doall && wilds && (match(tname, target_p->name) == 0))
205 continue;
206
1123eefc
EK
207 if(!SeesOper(target_p, source_p))
208 continue;
209
a4da8e48 210 report_this_status(source_p, target_p);
212380e3
AC
211 }
212
213 if (IsExemptShide(source_p) || !ConfigServerHide.flatten_links)
214 {
5b96d9a6 215 RB_DLINK_FOREACH(ptr, serv_list.head)
212380e3
AC
216 {
217 target_p = ptr->data;
218
219 if(!doall && wilds && !match(tname, target_p->name))
220 continue;
221
a4da8e48 222 report_this_status(source_p, target_p);
212380e3
AC
223 }
224 }
225
55abcbb2 226 sendto_one_numeric(source_p, RPL_ENDOFTRACE,
212380e3 227 form_str(RPL_ENDOFTRACE), tname);
3c7d6fcc 228 return;
212380e3
AC
229 }
230
231 /* source_p is opered */
232
233 /* report all direct connections */
5b96d9a6 234 RB_DLINK_FOREACH(ptr, lclient_list.head)
212380e3
AC
235 {
236 target_p = ptr->data;
237
238 /* dont show invisible users to remote opers */
1123eefc 239 if(IsInvisible(target_p) && dow && !MyConnect(source_p) && !SeesOper(target_p, source_p))
212380e3
AC
240 continue;
241
242 if(!doall && wilds && !match(tname, target_p->name))
243 continue;
244
abee738b 245 /* remote opers may not see invisible normal users */
1123eefc 246 if(dow && !MyConnect(source_p) && !SeesOper(target_p, source_p) &&
abee738b
JT
247 IsInvisible(target_p))
248 continue;
249
a4da8e48 250 cnt = report_this_status(source_p, target_p);
212380e3
AC
251 }
252
5b96d9a6 253 RB_DLINK_FOREACH(ptr, serv_list.head)
212380e3
AC
254 {
255 target_p = ptr->data;
256
257 if(!doall && wilds && !match(tname, target_p->name))
258 continue;
259
a4da8e48 260 cnt = report_this_status(source_p, target_p);
212380e3
AC
261 }
262
263 if(MyConnect(source_p))
264 {
5b96d9a6 265 RB_DLINK_FOREACH(ptr, unknown_list.head)
212380e3
AC
266 {
267 target_p = ptr->data;
268
269 if(!doall && wilds && !match(tname, target_p->name))
270 continue;
271
a4da8e48 272 cnt = report_this_status(source_p, target_p);
212380e3
AC
273 }
274 }
275
276 if(!cnt)
277 {
278 sendto_one_numeric(source_p, ERR_NOSUCHSERVER, form_str(ERR_NOSUCHSERVER),
279 tname);
280
281 /* let the user have some idea that its at the end of the
282 * trace
283 */
55abcbb2 284 sendto_one_numeric(source_p, RPL_ENDOFTRACE,
212380e3 285 form_str(RPL_ENDOFTRACE), tname);
3c7d6fcc 286 return;
212380e3
AC
287 }
288
289 if(doall)
290 {
5b96d9a6 291 RB_DLINK_FOREACH(ptr, class_list.head)
212380e3
AC
292 {
293 cltmp = ptr->data;
294
295 if(CurrUsers(cltmp) > 0)
296 sendto_one_numeric(source_p, RPL_TRACECLASS,
55abcbb2 297 form_str(RPL_TRACECLASS),
212380e3
AC
298 ClassName(cltmp), CurrUsers(cltmp));
299 }
300 }
301
302 sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE), tname);
212380e3
AC
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{
5b96d9a6 316 rb_dlink_node *ptr;
212380e3
AC
317
318 (*pservcount)++;
5b96d9a6
AC
319 *pusercount += rb_dlink_list_length(&server_p->serv->users);
320 RB_DLINK_FOREACH(ptr, server_p->serv->servers.head)
212380e3
AC
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
a4da8e48 335report_this_status(struct Client *source_p, struct Client *target_p)
212380e3
AC
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
caa4d9d2 346 rb_inet_ntop_sock((struct sockaddr *)&target_p->localClient->ip, ip, sizeof(ip));
212380e3
AC
347 class_name = get_client_class(target_p);
348
349 if(IsAnyServer(target_p))
b3ebc7ab 350 name = target_p->name;
212380e3
AC
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,
77910830 378 (unsigned long)(rb_current_time() - target_p->localClient->firsttime));
212380e3
AC
379 cnt++;
380 break;
381
382 case STAT_CLIENT:
a672fbb7 383 {
6f7b36d5 384 sendto_one_numeric(source_p,
1123eefc
EK
385 SeesOper(target_p, source_p) ? RPL_TRACEOPERATOR : RPL_TRACEUSER,
386 SeesOper(target_p, source_p) ? form_str(RPL_TRACEOPERATOR) : form_str(RPL_TRACEUSER),
a672fbb7
JT
387 class_name, name,
388 show_ip(source_p, target_p) ? ip : empty_sockhost,
0cce01d3
JT
389 (unsigned long)(rb_current_time() - target_p->localClient->lasttime),
390 (unsigned long)(rb_current_time() - target_p->localClient->last));
a672fbb7
JT
391
392 cnt++;
393 }
a4da8e48 394 break;
212380e3
AC
395
396 case STAT_SERVER:
397 {
398 int usercount = 0;
399 int servcount = 0;
400
401 count_downlinks(target_p, &servcount, &usercount);
402
403 sendto_one_numeric(source_p, RPL_TRACESERVER, form_str(RPL_TRACESERVER),
404 class_name, servcount, usercount, name,
405 *(target_p->serv->by) ? target_p->serv->by : "*", "*",
0cce01d3
JT
406 me.name,
407 (unsigned long)(rb_current_time() - target_p->localClient->lasttime));
212380e3
AC
408 cnt++;
409
410 }
411 break;
412
413 default: /* ...we actually shouldn't come here... --msa */
55abcbb2 414 sendto_one_numeric(source_p, RPL_TRACENEWTYPE,
0cce01d3 415 form_str(RPL_TRACENEWTYPE), name);
212380e3
AC
416 cnt++;
417 break;
418 }
419
420 return (cnt);
421}
422
423/* trace_spy()
424 *
425 * input - pointer to client
426 * output - none
427 * side effects - hook event doing_trace is called
428 */
429static void
430trace_spy(struct Client *source_p, struct Client *target_p)
431{
432 hook_data_client hdata;
433
434 hdata.client = source_p;
435 hdata.target = target_p;
436
437 call_hook(doing_trace_hook, &hdata);
438}