]>
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 | |
23 | * | |
4d7a1ee5 | 24 | * $Id: m_trace.c 3183 2007-02-01 01:07:42Z jilles $ |
212380e3 AC |
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" | |
4562c604 | 34 | #include "match.h" |
212380e3 AC |
35 | #include "ircd.h" |
36 | #include "numeric.h" | |
212380e3 AC |
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 | ||
45 | static int m_trace(struct Client *, struct Client *, int, const char **); | |
46 | ||
47 | static void trace_spy(struct Client *, struct Client *); | |
48 | ||
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}} | |
52 | }; | |
53 | ||
54 | int doing_trace_hook; | |
55 | ||
56 | mapi_clist_av1 trace_clist[] = { &trace_msgtab, NULL }; | |
57 | mapi_hlist_av1 trace_hlist[] = { | |
58 | { "doing_trace", &doing_trace_hook }, | |
59 | { NULL, NULL } | |
60 | }; | |
4d7a1ee5 | 61 | DECLARE_MODULE_AV1(trace, NULL, NULL, trace_clist, trace_hlist, NULL, "$Revision: 3183 $"); |
212380e3 AC |
62 | |
63 | static void count_downlinks(struct Client *server_p, int *pservcount, int *pusercount); | |
a4da8e48 | 64 | static int report_this_status(struct Client *source_p, struct Client *target_p); |
212380e3 | 65 | |
a3afc27a VY |
66 | static const char *empty_sockhost = "255.255.255.255"; |
67 | ||
212380e3 AC |
68 | /* |
69 | * m_trace | |
212380e3 AC |
70 | * parv[1] = servername |
71 | */ | |
72 | static int | |
73 | m_trace(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) | |
74 | { | |
75 | struct Client *target_p = NULL; | |
76 | struct Class *cltmp; | |
77 | const char *tname; | |
78 | int doall = 0; | |
79 | int cnt = 0, wilds, dow; | |
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) | |
90 | return 0; | |
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 | */ | |
128 | if(IsOper(source_p) || IsExemptShide(source_p) || | |
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 | ||
136 | return 0; | |
137 | } | |
138 | ||
139 | case HUNTED_ISME: | |
140 | break; | |
141 | ||
142 | default: | |
143 | return 0; | |
144 | } | |
145 | } | |
146 | ||
147 | if(match(tname, me.name)) | |
148 | { | |
149 | doall = 1; | |
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 | { | |
156 | doall = 1; | |
157 | tname = me.name; | |
158 | } | |
159 | ||
160 | wilds = strchr(tname, '*') || strchr(tname, '?'); | |
161 | dow = wilds || doall; | |
162 | ||
163 | /* specific trace */ | |
164 | if(dow == 0) | |
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 AC |
183 | form_str(RPL_ENDOFTRACE), tname); |
184 | return 0; | |
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 | ||
a4da8e48 | 207 | report_this_status(source_p, target_p); |
212380e3 AC |
208 | } |
209 | ||
210 | if (IsExemptShide(source_p) || !ConfigServerHide.flatten_links) | |
211 | { | |
5b96d9a6 | 212 | RB_DLINK_FOREACH(ptr, serv_list.head) |
212380e3 AC |
213 | { |
214 | target_p = ptr->data; | |
215 | ||
216 | if(!doall && wilds && !match(tname, target_p->name)) | |
217 | continue; | |
218 | ||
a4da8e48 | 219 | report_this_status(source_p, target_p); |
212380e3 AC |
220 | } |
221 | } | |
222 | ||
55abcbb2 | 223 | sendto_one_numeric(source_p, RPL_ENDOFTRACE, |
212380e3 AC |
224 | form_str(RPL_ENDOFTRACE), tname); |
225 | return 0; | |
226 | } | |
227 | ||
228 | /* source_p is opered */ | |
229 | ||
230 | /* report all direct connections */ | |
5b96d9a6 | 231 | RB_DLINK_FOREACH(ptr, lclient_list.head) |
212380e3 AC |
232 | { |
233 | target_p = ptr->data; | |
234 | ||
235 | /* dont show invisible users to remote opers */ | |
236 | if(IsInvisible(target_p) && dow && !MyConnect(source_p) && !IsOper(target_p)) | |
237 | continue; | |
238 | ||
239 | if(!doall && wilds && !match(tname, target_p->name)) | |
240 | continue; | |
241 | ||
abee738b JT |
242 | /* remote opers may not see invisible normal users */ |
243 | if(dow && !MyConnect(source_p) && !IsOper(target_p) && | |
244 | IsInvisible(target_p)) | |
245 | continue; | |
246 | ||
a4da8e48 | 247 | cnt = report_this_status(source_p, target_p); |
212380e3 AC |
248 | } |
249 | ||
5b96d9a6 | 250 | RB_DLINK_FOREACH(ptr, serv_list.head) |
212380e3 AC |
251 | { |
252 | target_p = ptr->data; | |
253 | ||
254 | if(!doall && wilds && !match(tname, target_p->name)) | |
255 | continue; | |
256 | ||
a4da8e48 | 257 | cnt = report_this_status(source_p, target_p); |
212380e3 AC |
258 | } |
259 | ||
260 | if(MyConnect(source_p)) | |
261 | { | |
5b96d9a6 | 262 | RB_DLINK_FOREACH(ptr, unknown_list.head) |
212380e3 AC |
263 | { |
264 | target_p = ptr->data; | |
265 | ||
266 | if(!doall && wilds && !match(tname, target_p->name)) | |
267 | continue; | |
268 | ||
a4da8e48 | 269 | cnt = report_this_status(source_p, target_p); |
212380e3 AC |
270 | } |
271 | } | |
272 | ||
273 | if(!cnt) | |
274 | { | |
275 | sendto_one_numeric(source_p, ERR_NOSUCHSERVER, form_str(ERR_NOSUCHSERVER), | |
276 | tname); | |
277 | ||
278 | /* let the user have some idea that its at the end of the | |
279 | * trace | |
280 | */ | |
55abcbb2 | 281 | sendto_one_numeric(source_p, RPL_ENDOFTRACE, |
212380e3 AC |
282 | form_str(RPL_ENDOFTRACE), tname); |
283 | return 0; | |
284 | } | |
285 | ||
286 | if(doall) | |
287 | { | |
5b96d9a6 | 288 | RB_DLINK_FOREACH(ptr, class_list.head) |
212380e3 AC |
289 | { |
290 | cltmp = ptr->data; | |
291 | ||
292 | if(CurrUsers(cltmp) > 0) | |
293 | sendto_one_numeric(source_p, RPL_TRACECLASS, | |
55abcbb2 | 294 | form_str(RPL_TRACECLASS), |
212380e3 AC |
295 | ClassName(cltmp), CurrUsers(cltmp)); |
296 | } | |
297 | } | |
298 | ||
299 | sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE), tname); | |
300 | ||
301 | return 0; | |
302 | } | |
303 | ||
304 | /* | |
305 | * count_downlinks | |
306 | * | |
307 | * inputs - pointer to server to count | |
308 | * - pointers to server and user count | |
309 | * output - NONE | |
310 | * side effects - server and user counts are added to given values | |
311 | */ | |
312 | static void | |
313 | count_downlinks(struct Client *server_p, int *pservcount, int *pusercount) | |
314 | { | |
5b96d9a6 | 315 | rb_dlink_node *ptr; |
212380e3 AC |
316 | |
317 | (*pservcount)++; | |
5b96d9a6 AC |
318 | *pusercount += rb_dlink_list_length(&server_p->serv->users); |
319 | RB_DLINK_FOREACH(ptr, server_p->serv->servers.head) | |
212380e3 AC |
320 | { |
321 | count_downlinks(ptr->data, pservcount, pusercount); | |
322 | } | |
323 | } | |
324 | ||
325 | /* | |
326 | * report_this_status | |
327 | * | |
328 | * inputs - pointer to client to report to | |
329 | * - pointer to client to report about | |
330 | * output - counter of number of hits | |
331 | * side effects - NONE | |
332 | */ | |
333 | static int | |
a4da8e48 | 334 | report_this_status(struct Client *source_p, struct Client *target_p) |
212380e3 AC |
335 | { |
336 | const char *name; | |
337 | const char *class_name; | |
338 | char ip[HOSTIPLEN]; | |
339 | int cnt = 0; | |
340 | ||
341 | /* sanity check - should never happen */ | |
342 | if(!MyConnect(target_p)) | |
343 | return 0; | |
344 | ||
caa4d9d2 | 345 | rb_inet_ntop_sock((struct sockaddr *)&target_p->localClient->ip, ip, sizeof(ip)); |
212380e3 AC |
346 | class_name = get_client_class(target_p); |
347 | ||
348 | if(IsAnyServer(target_p)) | |
b3ebc7ab | 349 | name = target_p->name; |
212380e3 AC |
350 | else |
351 | name = get_client_name(target_p, HIDE_IP); | |
352 | ||
353 | switch (target_p->status) | |
354 | { | |
355 | case STAT_CONNECTING: | |
356 | sendto_one_numeric(source_p, RPL_TRACECONNECTING, | |
357 | form_str(RPL_TRACECONNECTING), | |
358 | class_name, name); | |
359 | cnt++; | |
360 | break; | |
361 | ||
362 | case STAT_HANDSHAKE: | |
363 | sendto_one_numeric(source_p, RPL_TRACEHANDSHAKE, | |
364 | form_str(RPL_TRACEHANDSHAKE), | |
365 | class_name, name); | |
366 | cnt++; | |
367 | break; | |
368 | ||
369 | case STAT_ME: | |
370 | break; | |
371 | ||
372 | case STAT_UNKNOWN: | |
373 | /* added time -Taner */ | |
374 | sendto_one_numeric(source_p, RPL_TRACEUNKNOWN, | |
375 | form_str(RPL_TRACEUNKNOWN), | |
376 | class_name, name, ip, | |
77910830 | 377 | (unsigned long)(rb_current_time() - target_p->localClient->firsttime)); |
212380e3 AC |
378 | cnt++; |
379 | break; | |
380 | ||
381 | case STAT_CLIENT: | |
a672fbb7 | 382 | { |
6f7b36d5 AC |
383 | sendto_one_numeric(source_p, |
384 | IsOper(target_p) ? RPL_TRACEOPERATOR : RPL_TRACEUSER, | |
385 | IsOper(target_p) ? form_str(RPL_TRACEOPERATOR) : form_str(RPL_TRACEUSER), | |
a672fbb7 JT |
386 | class_name, name, |
387 | show_ip(source_p, target_p) ? ip : empty_sockhost, | |
0cce01d3 JT |
388 | (unsigned long)(rb_current_time() - target_p->localClient->lasttime), |
389 | (unsigned long)(rb_current_time() - target_p->localClient->last)); | |
a672fbb7 JT |
390 | |
391 | cnt++; | |
392 | } | |
a4da8e48 | 393 | break; |
212380e3 AC |
394 | |
395 | case STAT_SERVER: | |
396 | { | |
397 | int usercount = 0; | |
398 | int servcount = 0; | |
399 | ||
400 | count_downlinks(target_p, &servcount, &usercount); | |
401 | ||
402 | sendto_one_numeric(source_p, RPL_TRACESERVER, form_str(RPL_TRACESERVER), | |
403 | class_name, servcount, usercount, name, | |
404 | *(target_p->serv->by) ? target_p->serv->by : "*", "*", | |
0cce01d3 JT |
405 | me.name, |
406 | (unsigned long)(rb_current_time() - target_p->localClient->lasttime)); | |
212380e3 AC |
407 | cnt++; |
408 | ||
409 | } | |
410 | break; | |
411 | ||
412 | default: /* ...we actually shouldn't come here... --msa */ | |
55abcbb2 | 413 | sendto_one_numeric(source_p, RPL_TRACENEWTYPE, |
0cce01d3 | 414 | form_str(RPL_TRACENEWTYPE), name); |
212380e3 AC |
415 | cnt++; |
416 | break; | |
417 | } | |
418 | ||
419 | return (cnt); | |
420 | } | |
421 | ||
422 | /* trace_spy() | |
423 | * | |
424 | * input - pointer to client | |
425 | * output - none | |
426 | * side effects - hook event doing_trace is called | |
427 | */ | |
428 | static void | |
429 | trace_spy(struct Client *source_p, struct Client *target_p) | |
430 | { | |
431 | hook_data_client hdata; | |
432 | ||
433 | hdata.client = source_p; | |
434 | hdata.target = target_p; | |
435 | ||
436 | call_hook(doing_trace_hook, &hdata); | |
437 | } |