]>
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); | |
abee738b | 64 | static int report_this_status(struct Client *source_p, struct Client *target_p); |
212380e3 AC |
65 | |
66 | /* | |
67 | * m_trace | |
68 | * parv[0] = sender prefix | |
69 | * parv[1] = servername | |
70 | */ | |
71 | static int | |
72 | m_trace(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) | |
73 | { | |
74 | struct Client *target_p = NULL; | |
75 | struct Class *cltmp; | |
76 | const char *tname; | |
77 | int doall = 0; | |
78 | int cnt = 0, wilds, dow; | |
5b96d9a6 | 79 | rb_dlink_node *ptr; |
212380e3 AC |
80 | |
81 | if(parc > 1) | |
82 | { | |
83 | tname = parv[1]; | |
84 | ||
85 | if(parc > 2) | |
86 | { | |
87 | if(hunt_server(client_p, source_p, ":%s TRACE %s :%s", 2, parc, parv) != | |
88 | HUNTED_ISME) | |
89 | return 0; | |
90 | } | |
91 | } | |
92 | else | |
93 | tname = me.name; | |
94 | ||
95 | /* if we have 3 parameters, then the command is directed at us. So | |
96 | * we shouldnt be forwarding it anywhere. | |
97 | */ | |
98 | if(parc < 3) | |
99 | { | |
100 | switch (hunt_server(client_p, source_p, ":%s TRACE :%s", 1, parc, parv)) | |
101 | { | |
102 | case HUNTED_PASS: /* note: gets here only if parv[1] exists */ | |
103 | { | |
104 | struct Client *ac2ptr; | |
105 | ||
106 | if(MyClient(source_p)) | |
107 | ac2ptr = find_named_client(tname); | |
108 | else | |
109 | ac2ptr = find_client(tname); | |
110 | ||
111 | if(ac2ptr == NULL) | |
112 | { | |
5b96d9a6 | 113 | RB_DLINK_FOREACH(ptr, global_client_list.head) |
212380e3 AC |
114 | { |
115 | ac2ptr = ptr->data; | |
116 | ||
4d7a1ee5 | 117 | if(match(tname, ac2ptr->name)) |
212380e3 AC |
118 | break; |
119 | else | |
120 | ac2ptr = NULL; | |
121 | } | |
122 | } | |
123 | ||
124 | /* giving this out with flattened links defeats the | |
125 | * object --fl | |
126 | */ | |
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), | |
131 | ircd_version, | |
132 | ac2ptr ? ac2ptr->name : tname, | |
133 | ac2ptr ? ac2ptr->from->name : "EEK!"); | |
134 | ||
135 | return 0; | |
136 | } | |
137 | ||
138 | case HUNTED_ISME: | |
139 | break; | |
140 | ||
141 | default: | |
142 | return 0; | |
143 | } | |
144 | } | |
145 | ||
146 | if(match(tname, me.name)) | |
147 | { | |
148 | doall = 1; | |
149 | } | |
150 | /* if theyre tracing our SID, we need to move tname to our name so | |
151 | * we dont give the sid in ENDOFTRACE | |
152 | */ | |
153 | else if(!MyClient(source_p) && !strcmp(tname, me.id)) | |
154 | { | |
155 | doall = 1; | |
156 | tname = me.name; | |
157 | } | |
158 | ||
159 | wilds = strchr(tname, '*') || strchr(tname, '?'); | |
160 | dow = wilds || doall; | |
161 | ||
162 | /* specific trace */ | |
163 | if(dow == 0) | |
164 | { | |
165 | if(MyClient(source_p) || parc > 2) | |
166 | target_p = find_named_person(tname); | |
167 | else | |
168 | target_p = find_person(tname); | |
169 | ||
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 | |
172 | */ | |
173 | if(target_p != NULL) | |
174 | { | |
abee738b | 175 | report_this_status(source_p, target_p); |
212380e3 AC |
176 | tname = target_p->name; |
177 | } | |
178 | ||
179 | trace_spy(source_p, target_p); | |
180 | ||
181 | sendto_one_numeric(source_p, RPL_ENDOFTRACE, | |
182 | form_str(RPL_ENDOFTRACE), tname); | |
183 | return 0; | |
184 | } | |
185 | ||
186 | trace_spy(source_p, NULL); | |
187 | ||
188 | /* give non-opers a limited trace output of themselves (if local), | |
189 | * opers and servers (if no shide) --fl | |
190 | */ | |
191 | if(!IsOper(source_p)) | |
192 | { | |
193 | if(MyClient(source_p)) | |
194 | { | |
195 | if(doall || (wilds && match(tname, source_p->name))) | |
abee738b | 196 | report_this_status(source_p, source_p); |
212380e3 AC |
197 | } |
198 | ||
5b96d9a6 | 199 | RB_DLINK_FOREACH(ptr, local_oper_list.head) |
212380e3 AC |
200 | { |
201 | target_p = ptr->data; | |
202 | ||
203 | if(!doall && wilds && (match(tname, target_p->name) == 0)) | |
204 | continue; | |
205 | ||
abee738b | 206 | report_this_status(source_p, target_p); |
212380e3 AC |
207 | } |
208 | ||
209 | if (IsExemptShide(source_p) || !ConfigServerHide.flatten_links) | |
210 | { | |
5b96d9a6 | 211 | RB_DLINK_FOREACH(ptr, serv_list.head) |
212380e3 AC |
212 | { |
213 | target_p = ptr->data; | |
214 | ||
215 | if(!doall && wilds && !match(tname, target_p->name)) | |
216 | continue; | |
217 | ||
abee738b | 218 | report_this_status(source_p, target_p); |
212380e3 AC |
219 | } |
220 | } | |
221 | ||
222 | sendto_one_numeric(source_p, RPL_ENDOFTRACE, | |
223 | form_str(RPL_ENDOFTRACE), tname); | |
224 | return 0; | |
225 | } | |
226 | ||
227 | /* source_p is opered */ | |
228 | ||
229 | /* report all direct connections */ | |
5b96d9a6 | 230 | RB_DLINK_FOREACH(ptr, lclient_list.head) |
212380e3 AC |
231 | { |
232 | target_p = ptr->data; | |
233 | ||
234 | /* dont show invisible users to remote opers */ | |
235 | if(IsInvisible(target_p) && dow && !MyConnect(source_p) && !IsOper(target_p)) | |
236 | continue; | |
237 | ||
238 | if(!doall && wilds && !match(tname, target_p->name)) | |
239 | continue; | |
240 | ||
abee738b JT |
241 | /* remote opers may not see invisible normal users */ |
242 | if(dow && !MyConnect(source_p) && !IsOper(target_p) && | |
243 | IsInvisible(target_p)) | |
244 | continue; | |
245 | ||
246 | cnt = report_this_status(source_p, target_p); | |
212380e3 AC |
247 | } |
248 | ||
5b96d9a6 | 249 | RB_DLINK_FOREACH(ptr, serv_list.head) |
212380e3 AC |
250 | { |
251 | target_p = ptr->data; | |
252 | ||
253 | if(!doall && wilds && !match(tname, target_p->name)) | |
254 | continue; | |
255 | ||
abee738b | 256 | cnt = report_this_status(source_p, target_p); |
212380e3 AC |
257 | } |
258 | ||
259 | if(MyConnect(source_p)) | |
260 | { | |
5b96d9a6 | 261 | RB_DLINK_FOREACH(ptr, unknown_list.head) |
212380e3 AC |
262 | { |
263 | target_p = ptr->data; | |
264 | ||
265 | if(!doall && wilds && !match(tname, target_p->name)) | |
266 | continue; | |
267 | ||
abee738b | 268 | cnt = report_this_status(source_p, target_p); |
212380e3 AC |
269 | } |
270 | } | |
271 | ||
272 | if(!cnt) | |
273 | { | |
274 | sendto_one_numeric(source_p, ERR_NOSUCHSERVER, form_str(ERR_NOSUCHSERVER), | |
275 | tname); | |
276 | ||
277 | /* let the user have some idea that its at the end of the | |
278 | * trace | |
279 | */ | |
280 | sendto_one_numeric(source_p, RPL_ENDOFTRACE, | |
281 | form_str(RPL_ENDOFTRACE), tname); | |
282 | return 0; | |
283 | } | |
284 | ||
285 | if(doall) | |
286 | { | |
5b96d9a6 | 287 | RB_DLINK_FOREACH(ptr, class_list.head) |
212380e3 AC |
288 | { |
289 | cltmp = ptr->data; | |
290 | ||
291 | if(CurrUsers(cltmp) > 0) | |
292 | sendto_one_numeric(source_p, RPL_TRACECLASS, | |
293 | form_str(RPL_TRACECLASS), | |
294 | ClassName(cltmp), CurrUsers(cltmp)); | |
295 | } | |
296 | } | |
297 | ||
298 | sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE), tname); | |
299 | ||
300 | return 0; | |
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 | |
abee738b | 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)) | |
348 | name = get_server_name(target_p, HIDE_IP); | |
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, | |
e3354945 | 376 | rb_current_time() - target_p->localClient->firsttime); |
212380e3 AC |
377 | cnt++; |
378 | break; | |
379 | ||
380 | case STAT_CLIENT: | |
85111986 JT |
381 | if(IsOper(target_p)) |
382 | sendto_one_numeric(source_p, RPL_TRACEOPERATOR, | |
383 | form_str(RPL_TRACEOPERATOR), | |
384 | class_name, name, | |
385 | show_ip(source_p, target_p) ? ip : "255.255.255.255", | |
386 | rb_current_time() - target_p->localClient->lasttime, | |
387 | rb_current_time() - target_p->localClient->last); | |
212380e3 | 388 | |
85111986 JT |
389 | else |
390 | sendto_one_numeric(source_p, RPL_TRACEUSER, | |
391 | form_str(RPL_TRACEUSER), | |
392 | class_name, name, | |
393 | show_ip(source_p, target_p) ? ip : "255.255.255.255", | |
394 | rb_current_time() - target_p->localClient->lasttime, | |
395 | rb_current_time() - target_p->localClient->last); | |
396 | cnt++; | |
212380e3 AC |
397 | break; |
398 | ||
399 | case STAT_SERVER: | |
400 | { | |
401 | int usercount = 0; | |
402 | int servcount = 0; | |
403 | ||
404 | count_downlinks(target_p, &servcount, &usercount); | |
405 | ||
406 | sendto_one_numeric(source_p, RPL_TRACESERVER, form_str(RPL_TRACESERVER), | |
407 | class_name, servcount, usercount, name, | |
408 | *(target_p->serv->by) ? target_p->serv->by : "*", "*", | |
e3354945 | 409 | me.name, rb_current_time() - target_p->localClient->lasttime); |
212380e3 AC |
410 | cnt++; |
411 | ||
412 | } | |
413 | break; | |
414 | ||
415 | default: /* ...we actually shouldn't come here... --msa */ | |
416 | sendto_one_numeric(source_p, RPL_TRACENEWTYPE, | |
417 | form_str(RPL_TRACENEWTYPE), | |
418 | me.name, source_p->name, name); | |
419 | cnt++; | |
420 | break; | |
421 | } | |
422 | ||
423 | return (cnt); | |
424 | } | |
425 | ||
426 | /* trace_spy() | |
427 | * | |
428 | * input - pointer to client | |
429 | * output - none | |
430 | * side effects - hook event doing_trace is called | |
431 | */ | |
432 | static void | |
433 | trace_spy(struct Client *source_p, struct Client *target_p) | |
434 | { | |
435 | hook_data_client hdata; | |
436 | ||
437 | hdata.client = source_p; | |
438 | hdata.target = target_p; | |
439 | ||
440 | call_hook(doing_trace_hook, &hdata); | |
441 | } |