]> jfr.im git - solanum.git/blob - modules/m_etrace.c
send: implement partial support for outbound tags (enough for account-tag as a testcase)
[solanum.git] / modules / m_etrace.c
1 /*
2 * ircd-ratbox: an advanced Internet Relay Chat Daemon(ircd).
3 * m_etrace.c: Gives local opers a trace output with added info.
4 *
5 * Copyright (C) 2002-2003 Lee Hardy <lee@leeh.co.uk>
6 * Copyright (C) 2002-2005 ircd-ratbox development team
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * 1.Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 * 2.Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3.The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 * $Id: m_etrace.c 3161 2007-01-25 07:23:01Z nenolod $
33 */
34
35 #include "stdinc.h"
36 #include "class.h"
37 #include "hook.h"
38 #include "client.h"
39 #include "hash.h"
40 #include "common.h"
41 #include "hash.h"
42 #include "match.h"
43 #include "ircd.h"
44 #include "numeric.h"
45 #include "s_serv.h"
46 #include "s_conf.h"
47 #include "s_newconf.h"
48 #include "send.h"
49 #include "msg.h"
50 #include "parse.h"
51 #include "modules.h"
52 #include "logger.h"
53 #include "supported.h"
54
55 static int mo_etrace(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
56 static int me_etrace(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
57 static int m_chantrace(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
58 static int mo_masktrace(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
59
60 struct Message etrace_msgtab = {
61 "ETRACE", 0, 0, 0, 0,
62 {mg_ignore, mg_not_oper, mg_ignore, mg_ignore, {me_etrace, 0}, {mo_etrace, 0}}
63 };
64 struct Message chantrace_msgtab = {
65 "CHANTRACE", 0, 0, 0, 0,
66 {mg_ignore, {m_chantrace, 2}, mg_ignore, mg_ignore, mg_ignore, {m_chantrace, 2}}
67 };
68 struct Message masktrace_msgtab = {
69 "MASKTRACE", 0, 0, 0, 0,
70 {mg_ignore, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_masktrace, 2}}
71 };
72
73 static int
74 _modinit(void)
75 {
76 add_isupport("ETRACE", isupport_string, "");
77
78 return 0;
79 }
80
81 static void
82 _moddeinit(void)
83 {
84 delete_isupport("ETRACE");
85 }
86
87 mapi_clist_av1 etrace_clist[] = { &etrace_msgtab, &chantrace_msgtab, &masktrace_msgtab, NULL };
88 DECLARE_MODULE_AV1(etrace, _modinit, _moddeinit, etrace_clist, NULL, NULL, "$Revision: 3161 $");
89
90 static void do_etrace(struct Client *source_p, int ipv4, int ipv6);
91 static void do_etrace_full(struct Client *source_p);
92 static void do_single_etrace(struct Client *source_p, struct Client *target_p);
93
94 static const char *empty_sockhost = "255.255.255.255";
95 static const char *spoofed_sockhost = "0";
96
97 /*
98 * m_etrace
99 * parv[1] = options [or target]
100 * parv[2] = [target]
101 */
102 static int
103 mo_etrace(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
104 {
105 if(parc > 1 && !EmptyString(parv[1]))
106 {
107 if(!irccmp(parv[1], "-full"))
108 do_etrace_full(source_p);
109 #ifdef RB_IPV6
110 else if(!irccmp(parv[1], "-v6"))
111 do_etrace(source_p, 0, 1);
112 else if(!irccmp(parv[1], "-v4"))
113 do_etrace(source_p, 1, 0);
114 #endif
115 else
116 {
117 struct Client *target_p = find_named_person(parv[1]);
118
119 if(target_p)
120 {
121 if(!MyClient(target_p))
122 sendto_one(target_p, ":%s ENCAP %s ETRACE %s",
123 get_id(source_p, target_p),
124 target_p->servptr->name,
125 get_id(target_p, target_p));
126 else
127 do_single_etrace(source_p, target_p);
128 }
129 else
130 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
131 form_str(ERR_NOSUCHNICK), parv[1]);
132 }
133 }
134 else
135 do_etrace(source_p, 1, 1);
136
137 return 0;
138 }
139
140 static int
141 me_etrace(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
142 {
143 struct Client *target_p;
144
145 if(!IsOper(source_p) || parc < 2 || EmptyString(parv[1]))
146 return 0;
147
148 /* we cant etrace remote clients.. we shouldnt even get sent them */
149 if((target_p = find_person(parv[1])) && MyClient(target_p))
150 do_single_etrace(source_p, target_p);
151
152 sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE),
153 target_p ? target_p->name : parv[1]);
154
155 return 0;
156 }
157
158 static void
159 do_etrace(struct Client *source_p, int ipv4, int ipv6)
160 {
161 struct Client *target_p;
162 rb_dlink_node *ptr;
163
164 /* report all direct connections */
165 RB_DLINK_FOREACH(ptr, lclient_list.head)
166 {
167 target_p = ptr->data;
168
169 #ifdef RB_IPV6
170 if((!ipv4 && target_p->localClient->ip.ss_family == AF_INET) ||
171 (!ipv6 && target_p->localClient->ip.ss_family == AF_INET6))
172 continue;
173 #endif
174
175 sendto_one(source_p, form_str(RPL_ETRACE),
176 me.name, source_p->name,
177 IsOper(target_p) ? "Oper" : "User",
178 get_client_class(target_p),
179 target_p->name, target_p->username, target_p->host,
180 show_ip(source_p, target_p) ? target_p->sockhost : "255.255.255.255",
181 target_p->info);
182 }
183
184 sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE), me.name);
185 }
186
187 static void
188 do_etrace_full(struct Client *source_p)
189 {
190 rb_dlink_node *ptr;
191
192 RB_DLINK_FOREACH(ptr, lclient_list.head)
193 {
194 do_single_etrace(source_p, ptr->data);
195 }
196
197 sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE), me.name);
198 }
199
200 /*
201 * do_single_etrace - searches local clients and displays those matching
202 * a pattern
203 * input - source client, target client
204 * output - etrace results
205 * side effects - etrace results are displayed
206 */
207 static void
208 do_single_etrace(struct Client *source_p, struct Client *target_p)
209 {
210 /* note, we hide fullcaps for spoofed users, as mirc can often
211 * advertise its internal ip address in the field --fl
212 */
213 if(!show_ip(source_p, target_p))
214 sendto_one(source_p, form_str(RPL_ETRACEFULL),
215 me.name, source_p->name,
216 IsOper(target_p) ? "Oper" : "User",
217 get_client_class(target_p),
218 target_p->name, target_p->username, target_p->host,
219 "255.255.255.255", "<hidden> <hidden>", target_p->info);
220 else
221 sendto_one(source_p, form_str(RPL_ETRACEFULL),
222 me.name, source_p->name,
223 IsOper(target_p) ? "Oper" : "User",
224 get_client_class(target_p),
225 target_p->name, target_p->username,
226 target_p->host, target_p->sockhost,
227 target_p->localClient->fullcaps, target_p->info);
228 }
229
230 static int
231 m_chantrace(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
232 {
233 struct Client *target_p;
234 struct Channel *chptr;
235 struct membership *msptr;
236 const char *sockhost;
237 const char *name;
238 rb_dlink_node *ptr;
239 int operspy = 0;
240
241 name = parv[1];
242
243 if(IsOperSpy(source_p) && parv[1][0] == '!')
244 {
245 name++;
246 operspy = 1;
247
248 if(EmptyString(name))
249 {
250 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
251 me.name, source_p->name, "CHANTRACE");
252 return 0;
253 }
254 }
255
256 if((chptr = find_channel(name)) == NULL)
257 {
258 sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL, form_str(ERR_NOSUCHCHANNEL),
259 name);
260 return 0;
261 }
262
263 /* dont report operspys for nonexistant channels. */
264 if(operspy)
265 report_operspy(source_p, "CHANTRACE", chptr->chname);
266
267 if(!operspy && !IsMember(client_p, chptr))
268 {
269 sendto_one_numeric(source_p, ERR_NOTONCHANNEL, form_str(ERR_NOTONCHANNEL),
270 chptr->chname);
271 return 0;
272 }
273
274 RB_DLINK_FOREACH(ptr, chptr->members.head)
275 {
276 msptr = ptr->data;
277 target_p = msptr->client_p;
278
279 if(EmptyString(target_p->sockhost))
280 sockhost = empty_sockhost;
281 else if(!show_ip(source_p, target_p))
282 sockhost = spoofed_sockhost;
283 else
284 sockhost = target_p->sockhost;
285
286 sendto_one(source_p, form_str(RPL_ETRACE),
287 me.name, source_p->name,
288 IsOper(target_p) ? "Oper" : "User",
289 /* class field -- pretend its server.. */
290 target_p->servptr->name,
291 target_p->name, target_p->username, target_p->host,
292 sockhost, target_p->info);
293 }
294
295 sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE), me.name);
296 return 0;
297 }
298
299 static void
300 match_masktrace(struct Client *source_p, rb_dlink_list *list,
301 const char *username, const char *hostname, const char *name,
302 const char *gecos)
303 {
304 struct Client *target_p;
305 rb_dlink_node *ptr;
306 const char *sockhost;
307
308 RB_DLINK_FOREACH(ptr, list->head)
309 {
310 target_p = ptr->data;
311 if(!IsPerson(target_p))
312 continue;
313
314 if(EmptyString(target_p->sockhost))
315 sockhost = empty_sockhost;
316 else if(!show_ip(source_p, target_p))
317 sockhost = spoofed_sockhost;
318 else
319 sockhost = target_p->sockhost;
320
321 if(match(username, target_p->username) &&
322 (match(hostname, target_p->host) ||
323 match(hostname, target_p->orighost) ||
324 match(hostname, sockhost) || match_ips(hostname, sockhost)))
325 {
326 if(name != NULL && !match(name, target_p->name))
327 continue;
328
329 if(gecos != NULL && !match_esc(gecos, target_p->info))
330 continue;
331
332 sendto_one(source_p, form_str(RPL_ETRACE),
333 me.name, source_p->name,
334 IsOper(target_p) ? "Oper" : "User",
335 /* class field -- pretend its server.. */
336 target_p->servptr->name,
337 target_p->name, target_p->username, target_p->host,
338 sockhost, target_p->info);
339 }
340 }
341 }
342
343 static int
344 mo_masktrace(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc,
345 const char *parv[])
346 {
347 char *name, *username, *hostname, *gecos;
348 const char *mask;
349 int operspy = 0;
350
351 mask = parv[1];
352 name = LOCAL_COPY(parv[1]);
353 collapse(name);
354
355 if(IsOperSpy(source_p) && parv[1][0] == '!')
356 {
357 name++;
358 mask++;
359 operspy = 1;
360 }
361
362 if(parc > 2 && !EmptyString(parv[2]))
363 {
364 gecos = LOCAL_COPY(parv[2]);
365 collapse_esc(gecos);
366 } else
367 gecos = NULL;
368
369
370 if((hostname = strchr(name, '@')) == NULL)
371 {
372 sendto_one_notice(source_p, ":Invalid parameters");
373 return 0;
374 }
375
376 *hostname++ = '\0';
377
378 if((username = strchr(name, '!')) == NULL)
379 {
380 username = name;
381 name = NULL;
382 } else
383 *username++ = '\0';
384
385 if(EmptyString(username) || EmptyString(hostname))
386 {
387 sendto_one_notice(source_p, ":Invalid parameters");
388 return 0;
389 }
390
391 if(operspy) {
392 if (!ConfigFileEntry.operspy_dont_care_user_info)
393 {
394 char buf[512];
395 rb_strlcpy(buf, mask, sizeof(buf));
396 if(!EmptyString(gecos)) {
397 rb_strlcat(buf, " ", sizeof(buf));
398 rb_strlcat(buf, gecos, sizeof(buf));
399 }
400
401 report_operspy(source_p, "MASKTRACE", buf);
402 }
403 match_masktrace(source_p, &global_client_list, username, hostname, name, gecos);
404 } else
405 match_masktrace(source_p, &lclient_list, username, hostname, name, gecos);
406
407 sendto_one_numeric(source_p, RPL_ENDOFTRACE, form_str(RPL_ENDOFTRACE), me.name);
408 return 0;
409 }