]>
Commit | Line | Data |
---|---|---|
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" | |
30 | #include "common.h" | |
31 | #include "hash.h" | |
4562c604 | 32 | #include "match.h" |
212380e3 AC |
33 | #include "ircd.h" |
34 | #include "numeric.h" | |
212380e3 AC |
35 | #include "s_serv.h" |
36 | #include "s_conf.h" | |
37 | #include "s_newconf.h" | |
38 | #include "send.h" | |
39 | #include "msg.h" | |
40 | #include "parse.h" | |
41 | #include "modules.h" | |
42 | ||
be9c3979 AW |
43 | static const char trace_desc[] = |
44 | "Provides the TRACE command to trace the route to a client or server"; | |
212380e3 | 45 | |
3c7d6fcc | 46 | static void m_trace(struct MsgBuf *, struct Client *, struct Client *, int, const char **); |
eeabf33a | 47 | |
212380e3 AC |
48 | static void trace_spy(struct Client *, struct Client *); |
49 | ||
50 | struct Message trace_msgtab = { | |
7baa37a9 | 51 | "TRACE", 0, 0, 0, 0, |
212380e3 AC |
52 | {mg_unreg, {m_trace, 0}, {m_trace, 0}, mg_ignore, mg_ignore, {m_trace, 0}} |
53 | }; | |
54 | ||
55 | int doing_trace_hook; | |
56 | ||
57 | mapi_clist_av1 trace_clist[] = { &trace_msgtab, NULL }; | |
58 | mapi_hlist_av1 trace_hlist[] = { | |
59 | { "doing_trace", &doing_trace_hook }, | |
60 | { NULL, NULL } | |
61 | }; | |
be9c3979 | 62 | DECLARE_MODULE_AV2(trace, NULL, NULL, trace_clist, trace_hlist, NULL, NULL, NULL, trace_desc); |
212380e3 AC |
63 | |
64 | static void count_downlinks(struct Client *server_p, int *pservcount, int *pusercount); | |
a4da8e48 | 65 | static int report_this_status(struct Client *source_p, struct Client *target_p); |
212380e3 | 66 | |
a3afc27a VY |
67 | static const char *empty_sockhost = "255.255.255.255"; |
68 | ||
212380e3 AC |
69 | /* |
70 | * m_trace | |
212380e3 AC |
71 | * parv[1] = servername |
72 | */ | |
3c7d6fcc | 73 | static void |
428ca87b | 74 | m_trace(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) |
212380e3 AC |
75 | { |
76 | struct Client *target_p = NULL; | |
77 | struct Class *cltmp; | |
78 | const char *tname; | |
3c7d6fcc EM |
79 | bool doall = false, wilds, dow; |
80 | int cnt = 0; | |
5b96d9a6 | 81 | rb_dlink_node *ptr; |
212380e3 AC |
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) | |
3c7d6fcc | 91 | return; |
212380e3 AC |
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 | { | |
2fb07961 | 115 | RB_DLINK_FOREACH(ptr, global_serv_list.head) |
212380e3 AC |
116 | { |
117 | ac2ptr = ptr->data; | |
118 | ||
4d7a1ee5 | 119 | if(match(tname, ac2ptr->name)) |
212380e3 AC |
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) | |
55abcbb2 | 131 | sendto_one_numeric(source_p, RPL_TRACELINK, |
212380e3 | 132 | form_str(RPL_TRACELINK), |
55abcbb2 | 133 | ircd_version, |
212380e3 AC |
134 | ac2ptr ? ac2ptr->name : tname, |
135 | ac2ptr ? ac2ptr->from->name : "EEK!"); | |
136 | ||
3c7d6fcc | 137 | return; |
212380e3 AC |
138 | } |
139 | ||
140 | case HUNTED_ISME: | |
141 | break; | |
142 | ||
143 | default: | |
3c7d6fcc | 144 | return; |
212380e3 AC |
145 | } |
146 | } | |
147 | ||
148 | if(match(tname, me.name)) | |
149 | { | |
3c7d6fcc | 150 | doall = true; |
212380e3 AC |
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 | { | |
3c7d6fcc | 157 | doall = true; |
212380e3 AC |
158 | tname = me.name; |
159 | } | |
160 | ||
161 | wilds = strchr(tname, '*') || strchr(tname, '?'); | |
162 | dow = wilds || doall; | |
163 | ||
164 | /* specific trace */ | |
3c7d6fcc | 165 | if(!dow) |
212380e3 AC |
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 | { | |
a4da8e48 | 177 | report_this_status(source_p, target_p); |
212380e3 AC |
178 | tname = target_p->name; |
179 | } | |
180 | ||
181 | trace_spy(source_p, target_p); | |
182 | ||
55abcbb2 | 183 | sendto_one_numeric(source_p, RPL_ENDOFTRACE, |
212380e3 | 184 | form_str(RPL_ENDOFTRACE), tname); |
3c7d6fcc | 185 | return; |
212380e3 AC |
186 | } |
187 | ||
188 | trace_spy(source_p, NULL); | |
189 | ||
55abcbb2 | 190 | /* give non-opers a limited trace output of themselves (if local), |
212380e3 AC |
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))) | |
a4da8e48 | 198 | report_this_status(source_p, source_p); |
212380e3 AC |
199 | } |
200 | ||
5b96d9a6 | 201 | RB_DLINK_FOREACH(ptr, local_oper_list.head) |
212380e3 AC |
202 | { |
203 | target_p = ptr->data; | |
204 | ||
205 | if(!doall && wilds && (match(tname, target_p->name) == 0)) | |
206 | continue; | |
207 | ||
a4da8e48 | 208 | report_this_status(source_p, target_p); |
212380e3 AC |
209 | } |
210 | ||
211 | if (IsExemptShide(source_p) || !ConfigServerHide.flatten_links) | |
212 | { | |
5b96d9a6 | 213 | RB_DLINK_FOREACH(ptr, serv_list.head) |
212380e3 AC |
214 | { |
215 | target_p = ptr->data; | |
216 | ||
217 | if(!doall && wilds && !match(tname, target_p->name)) | |
218 | continue; | |
219 | ||
a4da8e48 | 220 | report_this_status(source_p, target_p); |
212380e3 AC |
221 | } |
222 | } | |
223 | ||
55abcbb2 | 224 | sendto_one_numeric(source_p, RPL_ENDOFTRACE, |
212380e3 | 225 | form_str(RPL_ENDOFTRACE), tname); |
3c7d6fcc | 226 | return; |
212380e3 AC |
227 | } |
228 | ||
229 | /* source_p is opered */ | |
230 | ||
231 | /* report all direct connections */ | |
5b96d9a6 | 232 | RB_DLINK_FOREACH(ptr, lclient_list.head) |
212380e3 AC |
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 | ||
abee738b 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 | ||
a4da8e48 | 248 | cnt = report_this_status(source_p, target_p); |
212380e3 AC |
249 | } |
250 | ||
5b96d9a6 | 251 | RB_DLINK_FOREACH(ptr, serv_list.head) |
212380e3 AC |
252 | { |
253 | target_p = ptr->data; | |
254 | ||
255 | if(!doall && wilds && !match(tname, target_p->name)) | |
256 | continue; | |
257 | ||
a4da8e48 | 258 | cnt = report_this_status(source_p, target_p); |
212380e3 AC |
259 | } |
260 | ||
261 | if(MyConnect(source_p)) | |
262 | { | |
5b96d9a6 | 263 | RB_DLINK_FOREACH(ptr, unknown_list.head) |
212380e3 AC |
264 | { |
265 | target_p = ptr->data; | |
266 | ||
267 | if(!doall && wilds && !match(tname, target_p->name)) | |
268 | continue; | |
269 | ||
a4da8e48 | 270 | cnt = report_this_status(source_p, target_p); |
212380e3 AC |
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 | */ | |
55abcbb2 | 282 | sendto_one_numeric(source_p, RPL_ENDOFTRACE, |
212380e3 | 283 | form_str(RPL_ENDOFTRACE), tname); |
3c7d6fcc | 284 | return; |
212380e3 AC |
285 | } |
286 | ||
287 | if(doall) | |
288 | { | |
5b96d9a6 | 289 | RB_DLINK_FOREACH(ptr, class_list.head) |
212380e3 AC |
290 | { |
291 | cltmp = ptr->data; | |
292 | ||
293 | if(CurrUsers(cltmp) > 0) | |
294 | sendto_one_numeric(source_p, RPL_TRACECLASS, | |
55abcbb2 | 295 | form_str(RPL_TRACECLASS), |
212380e3 AC |
296 | ClassName(cltmp), CurrUsers(cltmp)); |
297 | } | |
298 | } | |
299 | ||
300 | sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE), tname); | |
212380e3 AC |
301 | } |
302 | ||
303 | /* | |
304 | * count_downlinks | |
305 | * | |
306 | * inputs - pointer to server to count | |
307 | * - pointers to server and user count | |
308 | * output - NONE | |
309 | * side effects - server and user counts are added to given values | |
310 | */ | |
311 | static void | |
312 | count_downlinks(struct Client *server_p, int *pservcount, int *pusercount) | |
313 | { | |
5b96d9a6 | 314 | rb_dlink_node *ptr; |
212380e3 AC |
315 | |
316 | (*pservcount)++; | |
5b96d9a6 AC |
317 | *pusercount += rb_dlink_list_length(&server_p->serv->users); |
318 | RB_DLINK_FOREACH(ptr, server_p->serv->servers.head) | |
212380e3 AC |
319 | { |
320 | count_downlinks(ptr->data, pservcount, pusercount); | |
321 | } | |
322 | } | |
323 | ||
324 | /* | |
325 | * report_this_status | |
326 | * | |
327 | * inputs - pointer to client to report to | |
328 | * - pointer to client to report about | |
329 | * output - counter of number of hits | |
330 | * side effects - NONE | |
331 | */ | |
332 | static int | |
a4da8e48 | 333 | report_this_status(struct Client *source_p, struct Client *target_p) |
212380e3 AC |
334 | { |
335 | const char *name; | |
336 | const char *class_name; | |
337 | char ip[HOSTIPLEN]; | |
338 | int cnt = 0; | |
339 | ||
340 | /* sanity check - should never happen */ | |
341 | if(!MyConnect(target_p)) | |
342 | return 0; | |
343 | ||
caa4d9d2 | 344 | rb_inet_ntop_sock((struct sockaddr *)&target_p->localClient->ip, ip, sizeof(ip)); |
212380e3 AC |
345 | class_name = get_client_class(target_p); |
346 | ||
347 | if(IsAnyServer(target_p)) | |
b3ebc7ab | 348 | name = target_p->name; |
212380e3 AC |
349 | else |
350 | name = get_client_name(target_p, HIDE_IP); | |
351 | ||
352 | switch (target_p->status) | |
353 | { | |
354 | case STAT_CONNECTING: | |
355 | sendto_one_numeric(source_p, RPL_TRACECONNECTING, | |
356 | form_str(RPL_TRACECONNECTING), | |
357 | class_name, name); | |
358 | cnt++; | |
359 | break; | |
360 | ||
361 | case STAT_HANDSHAKE: | |
362 | sendto_one_numeric(source_p, RPL_TRACEHANDSHAKE, | |
363 | form_str(RPL_TRACEHANDSHAKE), | |
364 | class_name, name); | |
365 | cnt++; | |
366 | break; | |
367 | ||
368 | case STAT_ME: | |
369 | break; | |
370 | ||
371 | case STAT_UNKNOWN: | |
372 | /* added time -Taner */ | |
373 | sendto_one_numeric(source_p, RPL_TRACEUNKNOWN, | |
374 | form_str(RPL_TRACEUNKNOWN), | |
375 | class_name, name, ip, | |
77910830 | 376 | (unsigned long)(rb_current_time() - target_p->localClient->firsttime)); |
212380e3 AC |
377 | cnt++; |
378 | break; | |
379 | ||
380 | case STAT_CLIENT: | |
a672fbb7 | 381 | { |
6f7b36d5 AC |
382 | sendto_one_numeric(source_p, |
383 | IsOper(target_p) ? RPL_TRACEOPERATOR : RPL_TRACEUSER, | |
384 | IsOper(target_p) ? form_str(RPL_TRACEOPERATOR) : form_str(RPL_TRACEUSER), | |
a672fbb7 JT |
385 | class_name, name, |
386 | show_ip(source_p, target_p) ? ip : empty_sockhost, | |
0cce01d3 JT |
387 | (unsigned long)(rb_current_time() - target_p->localClient->lasttime), |
388 | (unsigned long)(rb_current_time() - target_p->localClient->last)); | |
a672fbb7 JT |
389 | |
390 | cnt++; | |
391 | } | |
a4da8e48 | 392 | break; |
212380e3 AC |
393 | |
394 | case STAT_SERVER: | |
395 | { | |
396 | int usercount = 0; | |
397 | int servcount = 0; | |
398 | ||
399 | count_downlinks(target_p, &servcount, &usercount); | |
400 | ||
401 | sendto_one_numeric(source_p, RPL_TRACESERVER, form_str(RPL_TRACESERVER), | |
402 | class_name, servcount, usercount, name, | |
403 | *(target_p->serv->by) ? target_p->serv->by : "*", "*", | |
0cce01d3 JT |
404 | me.name, |
405 | (unsigned long)(rb_current_time() - target_p->localClient->lasttime)); | |
212380e3 AC |
406 | cnt++; |
407 | ||
408 | } | |
409 | break; | |
410 | ||
411 | default: /* ...we actually shouldn't come here... --msa */ | |
55abcbb2 | 412 | sendto_one_numeric(source_p, RPL_TRACENEWTYPE, |
0cce01d3 | 413 | form_str(RPL_TRACENEWTYPE), name); |
212380e3 AC |
414 | cnt++; |
415 | break; | |
416 | } | |
417 | ||
418 | return (cnt); | |
419 | } | |
420 | ||
421 | /* trace_spy() | |
422 | * | |
423 | * input - pointer to client | |
424 | * output - none | |
425 | * side effects - hook event doing_trace is called | |
426 | */ | |
427 | static void | |
428 | trace_spy(struct Client *source_p, struct Client *target_p) | |
429 | { | |
430 | hook_data_client hdata; | |
431 | ||
432 | hdata.client = source_p; | |
433 | hdata.target = target_p; | |
434 | ||
435 | call_hook(doing_trace_hook, &hdata); | |
436 | } |