]> jfr.im git - solanum.git/blame - modules/m_stats.c
Merge branch 'elizafox-cleanups' of github.com:charybdis-ircd/charybdis into elizafox...
[solanum.git] / modules / m_stats.c
CommitLineData
212380e3
AC
1/*
2 * ircd-ratbox: an advanced Internet Relay Chat Daemon(ircd).
3 * m_stats.c: Sends the user statistics or config information.
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"
212380e3
AC
26#include "class.h" /* report_classes */
27#include "client.h" /* Client */
963c3faa 28#include "common.h"
4562c604 29#include "match.h"
212380e3
AC
30#include "ircd.h" /* me */
31#include "listener.h" /* show_ports */
212380e3
AC
32#include "msg.h" /* Message */
33#include "hostmask.h" /* report_mtrie_conf_links */
34#include "numeric.h" /* ERR_xxx */
35#include "scache.h" /* list_scache */
36#include "send.h" /* sendto_one */
212380e3
AC
37#include "s_conf.h" /* ConfItem */
38#include "s_serv.h" /* hunt_server */
47adde3d 39#include "s_stats.h"
212380e3 40#include "s_user.h" /* show_opers */
212380e3 41#include "blacklist.h" /* dnsbl stuff */
212380e3
AC
42#include "parse.h"
43#include "modules.h"
44#include "hook.h"
45#include "s_newconf.h"
46#include "hash.h"
47adde3d
VY
47#include "reject.h"
48#include "whowas.h"
a4bf26dd 49#include "rb_radixtree.h"
eb1b303d 50#include "sslproc.h"
212380e3 51
3abc337f
AW
52static const char stats_desc[] =
53 "Provides the STATS command to inspect various server/network information";
212380e3 54
3c7d6fcc 55static void m_stats (struct MsgBuf *, struct Client *, struct Client *, int, const char **);
eeabf33a 56
212380e3 57struct Message stats_msgtab = {
7baa37a9 58 "STATS", 0, 0, 0, 0,
212380e3
AC
59 {mg_unreg, {m_stats, 2}, {m_stats, 3}, mg_ignore, mg_ignore, {m_stats, 2}}
60};
61
62int doing_stats_hook;
63int doing_stats_p_hook;
64
65mapi_clist_av1 stats_clist[] = { &stats_msgtab, NULL };
66mapi_hlist_av1 stats_hlist[] = {
67 { "doing_stats", &doing_stats_hook },
68 { "doing_stats_p", &doing_stats_p_hook },
69 { NULL, NULL }
70};
71
3abc337f 72DECLARE_MODULE_AV2(stats, NULL, NULL, stats_clist, stats_hlist, NULL, NULL, NULL, stats_desc);
212380e3
AC
73
74const char *Lformat = "%s %u %u %u %u %u :%u %u %s";
75
3c7d6fcc
EM
76static void stats_l_list(struct Client *s, const char *, bool, bool, rb_dlink_list *, char,
77 bool (*check_fn)(struct Client *target_p));
212380e3
AC
78static void stats_l_client(struct Client *source_p, struct Client *target_p,
79 char statchar);
80
ea2d2700 81static int stats_spy(struct Client *, char, const char *);
212380e3
AC
82static void stats_p_spy(struct Client *);
83
84/* Heres our struct for the stats table */
963c3faa 85struct stats_cmd
212380e3 86{
23282da4 87 void (*handler) (struct Client *source_p);
212380e3
AC
88 int need_oper;
89 int need_admin;
90};
91
8a26cd19 92static void stats_dns_servers(struct Client *);
212380e3
AC
93static void stats_delay(struct Client *);
94static void stats_hash(struct Client *);
95static void stats_connect(struct Client *);
96static void stats_tdeny(struct Client *);
97static void stats_deny(struct Client *);
98static void stats_exempt(struct Client *);
99static void stats_events(struct Client *);
416d868e 100static void stats_prop_klines(struct Client *);
212380e3
AC
101static void stats_hubleaf(struct Client *);
102static void stats_auth(struct Client *);
103static void stats_tklines(struct Client *);
104static void stats_klines(struct Client *);
105static void stats_messages(struct Client *);
106static void stats_dnsbl(struct Client *);
107static void stats_oper(struct Client *);
3a177354 108static void stats_privset(struct Client *);
212380e3
AC
109static void stats_operedup(struct Client *);
110static void stats_ports(struct Client *);
111static void stats_tresv(struct Client *);
112static void stats_resv(struct Client *);
eb1b303d 113static void stats_ssld(struct Client *);
212380e3
AC
114static void stats_usage(struct Client *);
115static void stats_tstats(struct Client *);
116static void stats_uptime(struct Client *);
117static void stats_shared(struct Client *);
118static void stats_servers(struct Client *);
119static void stats_tgecos(struct Client *);
120static void stats_gecos(struct Client *);
121static void stats_class(struct Client *);
122static void stats_memory(struct Client *);
123static void stats_servlinks(struct Client *);
124static void stats_ltrace(struct Client *, int, const char **);
125static void stats_ziplinks(struct Client *);
a235e410 126static void stats_comm(struct Client *);
ac37f16a
AC
127static void stats_capability(struct Client *);
128
212380e3 129/* This table contains the possible stats items, in order:
963c3faa
EM
130 * stats letter, function to call, operonly? adminonly? --fl_
131 *
132 * Previously in this table letters were a column. I fixed it to use modern
133 * C initalisers so we don't have to iterate anymore.
134 * --Elizafox
212380e3 135 */
963c3faa 136static struct stats_cmd stats_cmd_table[255] = {
55799c6b 137 /* letter function need_oper need_admin */
963c3faa
EM
138 ['a'] = { stats_dns_servers, 1, 1, },
139 ['A'] = { stats_dns_servers, 1, 1, },
140 ['b'] = { stats_delay, 1, 1, },
141 ['B'] = { stats_hash, 1, 1, },
142 ['c'] = { stats_connect, 0, 0, },
143 ['C'] = { stats_capability, 1, 0, },
144 ['d'] = { stats_tdeny, 1, 0, },
145 ['D'] = { stats_deny, 1, 0, },
146 ['e'] = { stats_exempt, 1, 0, },
147 ['E'] = { stats_events, 1, 1, },
148 ['f'] = { stats_comm, 1, 1, },
149 ['F'] = { stats_comm, 1, 1, },
150 ['g'] = { stats_prop_klines, 1, 0, },
151 ['h'] = { stats_hubleaf, 0, 0, },
152 ['H'] = { stats_hubleaf, 0, 0, },
153 ['i'] = { stats_auth, 0, 0, },
154 ['I'] = { stats_auth, 0, 0, },
155 ['k'] = { stats_tklines, 0, 0, },
156 ['K'] = { stats_klines, 0, 0, },
157 ['l'] = { NULL /* special */, 0, 0, },
158 ['L'] = { NULL /* special */, 0, 0, },
159 ['m'] = { stats_messages, 0, 0, },
160 ['M'] = { stats_messages, 0, 0, },
161 ['n'] = { stats_dnsbl, 0, 0, },
162 ['o'] = { stats_oper, 0, 0, },
163 ['O'] = { stats_privset, 1, 0, },
164 ['p'] = { stats_operedup, 0, 0, },
165 ['P'] = { stats_ports, 0, 0, },
166 ['q'] = { stats_tresv, 1, 0, },
167 ['Q'] = { stats_resv, 1, 0, },
168 ['r'] = { stats_usage, 1, 0, },
169 ['R'] = { stats_usage, 1, 0, },
170 ['s'] = { stats_ssld, 1, 1, },
171 ['S'] = { stats_ssld, 1, 1, },
172 ['t'] = { stats_tstats, 1, 0, },
173 ['T'] = { stats_tstats, 1, 0, },
174 ['u'] = { stats_uptime, 0, 0, },
175 ['U'] = { stats_shared, 1, 0, },
176 ['v'] = { stats_servers, 0, 0, },
177 ['V'] = { stats_servers, 0, 0, },
178 ['x'] = { stats_tgecos, 1, 0, },
179 ['X'] = { stats_gecos, 1, 0, },
180 ['y'] = { stats_class, 0, 0, },
181 ['Y'] = { stats_class, 0, 0, },
182 ['z'] = { stats_memory, 1, 0, },
183 ['Z'] = { stats_ziplinks, 1, 0, },
184 ['?'] = { stats_servlinks, 0, 0, },
212380e3
AC
185};
186
187/*
188 * m_stats by fl_
963c3faa 189 * Modified heavily by Elizafox
212380e3
AC
190 * parv[1] = stat letter/command
191 * parv[2] = (if present) server/mask in stats L, or target
192 *
193 * This will search the tables for the appropriate stats letter,
55abcbb2 194 * if found execute it.
212380e3 195 */
3c7d6fcc 196static void
428ca87b 197m_stats(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3
AC
198{
199 static time_t last_used = 0;
200 int i;
963c3faa 201 struct stats_cmd *cmd;
212380e3 202 char statchar;
ea2d2700 203 int did_stats = 0;
212380e3
AC
204
205 statchar = parv[1][0];
206
207 if(MyClient(source_p) && !IsOper(source_p))
208 {
209 /* Check the user is actually allowed to do /stats, and isnt flooding */
e3354945 210 if((last_used + ConfigFileEntry.pace_wait) > rb_current_time())
212380e3
AC
211 {
212 /* safe enough to give this on a local connect only */
213 sendto_one(source_p, form_str(RPL_LOAD2HI),
214 me.name, source_p->name, "STATS");
55abcbb2 215 sendto_one_numeric(source_p, RPL_ENDOFSTATS,
212380e3 216 form_str(RPL_ENDOFSTATS), statchar);
3c7d6fcc 217 return;
212380e3
AC
218 }
219 else
e3354945 220 last_used = rb_current_time();
212380e3
AC
221 }
222
223 if(hunt_server (client_p, source_p, ":%s STATS %s :%s", 2, parc, parv) != HUNTED_ISME)
3c7d6fcc 224 return;
212380e3 225
8a26cd19 226 if((statchar != 'L') && (statchar != 'l'))
963c3faa 227 {
ea2d2700 228 did_stats = stats_spy(source_p, statchar, NULL);
963c3faa
EM
229 }
230 else
231 {
232 /* Blah, stats L needs the parameters, none of the others do.. */
233 stats_ltrace (source_p, parc, parv);
234 goto stats_out;
235 }
ea2d2700
AC
236
237 /* if did_stats is true, a module grabbed this STATS request */
963c3faa 238 if(did_stats)
ea2d2700 239 goto stats_out;
212380e3 240
963c3faa
EM
241 /* Look up */
242 cmd = &stats_cmd_table[statchar];
243 if(cmd->handler != NULL)
212380e3 244 {
963c3faa
EM
245 /* The stats table says what privs are needed, so check --fl_ */
246 /* Called for remote clients and for local opers, so check need_admin
247 * and need_oper
248 */
249 if(cmd->need_admin && !IsOperAdmin(source_p))
212380e3 250 {
963c3faa
EM
251 sendto_one(source_p, form_str(ERR_NOPRIVS),
252 me.name, source_p->name, "admin");
253 goto stats_out;
212380e3 254 }
963c3faa
EM
255 if(cmd->need_oper && !IsOper(source_p))
256 {
257 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
258 form_str (ERR_NOPRIVILEGES));
259 goto stats_out;
260 }
261
262 cmd->handler(source_p);
212380e3
AC
263 }
264
ea2d2700 265stats_out:
212380e3 266 /* Send the end of stats notice, and the stats_spy */
55abcbb2 267 sendto_one_numeric(source_p, RPL_ENDOFSTATS,
212380e3 268 form_str(RPL_ENDOFSTATS), statchar);
212380e3
AC
269}
270
271static void
8a26cd19 272stats_dns_servers (struct Client *source_p)
212380e3 273{
55799c6b
EM
274 rb_dlink_node *n;
275
276 RB_DLINK_FOREACH(n, nameservers.head)
277 {
278 sendto_one_numeric(source_p, RPL_STATSDEBUG, "A %s", (char *)n->data);
279 }
212380e3
AC
280}
281
282static void
283stats_delay(struct Client *source_p)
284{
285 struct nd_entry *nd;
b37021a4 286 struct DictionaryIter iter;
212380e3 287
b37021a4 288 DICTIONARY_FOREACH(nd, &iter, nd_dict)
212380e3 289 {
e4b9c8e1 290 sendto_one_notice(source_p, ":Delaying: %s for %ld",
212380e3
AC
291 nd->name, (long) nd->expire);
292 }
212380e3
AC
293}
294
21d5a11c
AC
295static void
296stats_hash_cb(const char *buf, void *client_p)
297{
298 sendto_one_numeric(client_p, RPL_STATSDEBUG, "B :%s", buf);
299}
300
212380e3
AC
301static void
302stats_hash(struct Client *source_p)
303{
8dacf9e9
AC
304 sendto_one_numeric(source_p, RPL_STATSDEBUG, "B :%-30s %-15s %-10s %-10s %-10s %-10s",
305 "NAME", "TYPE", "OBJECTS", "DEPTH SUM", "AVG DEPTH", "MAX DEPTH");
21d5a11c 306
a4bf26dd
EM
307 rb_dictionary_stats_walk(stats_hash_cb, source_p);
308 rb_radixtree_stats_walk(stats_hash_cb, source_p);
212380e3
AC
309}
310
311static void
312stats_connect(struct Client *source_p)
313{
314 static char buf[5];
315 struct server_conf *server_p;
316 char *s;
5b96d9a6 317 rb_dlink_node *ptr;
212380e3 318
55abcbb2 319 if((ConfigFileEntry.stats_c_oper_only ||
212380e3
AC
320 (ConfigServerHide.flatten_links && !IsExemptShide(source_p))) &&
321 !IsOper(source_p))
322 {
323 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
324 form_str(ERR_NOPRIVILEGES));
325 return;
326 }
327
5b96d9a6 328 RB_DLINK_FOREACH(ptr, server_conf_list.head)
212380e3
AC
329 {
330 server_p = ptr->data;
331
332 if(ServerConfIllegal(server_p))
333 continue;
334
212380e3
AC
335 s = buf;
336
337 if(IsOper(source_p))
338 {
339 if(ServerConfAutoconn(server_p))
340 *s++ = 'A';
8bd5767b 341 if(ServerConfSSL(server_p))
f53ed7f8 342 *s++ = 'S';
212380e3
AC
343 if(ServerConfTb(server_p))
344 *s++ = 'T';
345 if(ServerConfCompressed(server_p))
346 *s++ = 'Z';
347 }
348
f76ca178 349 if(s == buf)
212380e3
AC
350 *s++ = '*';
351
352 *s = '\0';
353
55abcbb2 354 sendto_one_numeric(source_p, RPL_STATSCLINE,
212380e3 355 form_str(RPL_STATSCLINE),
55abcbb2 356 "*@127.0.0.1",
212380e3
AC
357 buf, server_p->name,
358 server_p->port, server_p->class_name);
359 }
360}
361
362/* stats_tdeny()
363 *
364 * input - client to report to
365 * output - none
366 * side effects - client is given temp dline list.
367 */
368static void
369stats_tdeny (struct Client *source_p)
370{
371 char *host, *pass, *user, *oper_reason;
372 struct AddressRec *arec;
373 struct ConfItem *aconf;
374 int i;
375
376 for (i = 0; i < ATABLE_SIZE; i++)
377 {
378 for (arec = atable[i]; arec; arec = arec->next)
379 {
380 if(arec->type == CONF_DLINE)
381 {
382 aconf = arec->aconf;
383
384 if(!(aconf->flags & CONF_FLAGS_TEMPORARY))
385 continue;
386
387 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
388
55abcbb2 389 sendto_one_numeric(source_p, RPL_STATSDLINE,
212380e3
AC
390 form_str (RPL_STATSDLINE),
391 'd', host, pass,
392 oper_reason ? "|" : "",
393 oper_reason ? oper_reason : "");
394 }
395 }
396 }
397}
398
399/* stats_deny()
400 *
401 * input - client to report to
402 * output - none
403 * side effects - client is given dline list.
404 */
405static void
406stats_deny (struct Client *source_p)
407{
408 char *host, *pass, *user, *oper_reason;
409 struct AddressRec *arec;
410 struct ConfItem *aconf;
411 int i;
412
413 for (i = 0; i < ATABLE_SIZE; i++)
414 {
415 for (arec = atable[i]; arec; arec = arec->next)
416 {
417 if(arec->type == CONF_DLINE)
418 {
419 aconf = arec->aconf;
420
421 if(aconf->flags & CONF_FLAGS_TEMPORARY)
422 continue;
423
424 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
425
55abcbb2 426 sendto_one_numeric(source_p, RPL_STATSDLINE,
212380e3
AC
427 form_str (RPL_STATSDLINE),
428 'D', host, pass,
429 oper_reason ? "|" : "",
430 oper_reason ? oper_reason : "");
431 }
432 }
433 }
434}
435
436
437/* stats_exempt()
438 *
439 * input - client to report to
440 * output - none
441 * side effects - client is given list of exempt blocks
442 */
443static void
444stats_exempt(struct Client *source_p)
445{
29c92cf9
JB
446 char *name, *host, *user, *classname;
447 const char *pass;
212380e3
AC
448 struct AddressRec *arec;
449 struct ConfItem *aconf;
450 int i, port;
451
452 if(ConfigFileEntry.stats_e_disabled)
453 {
22f2f68a
JT
454 sendto_one_numeric(source_p, ERR_DISABLED,
455 form_str(ERR_DISABLED), "STATS e");
212380e3
AC
456 return;
457 }
458
459 for (i = 0; i < ATABLE_SIZE; i++)
460 {
461 for (arec = atable[i]; arec; arec = arec->next)
462 {
463 if(arec->type == CONF_EXEMPTDLINE)
464 {
465 aconf = arec->aconf;
466 get_printable_conf (aconf, &name, &host, &pass,
467 &user, &port, &classname);
468
55abcbb2 469 sendto_one_numeric(source_p, RPL_STATSDLINE,
212380e3
AC
470 form_str(RPL_STATSDLINE),
471 'e', host, pass, "", "");
472 }
473 }
474 }}
475
476
f237e31a
JT
477static void
478stats_events_cb(char *str, void *ptr)
479{
480 sendto_one_numeric(ptr, RPL_STATSDEBUG, "E :%s", str);
481}
482
483static void
484stats_events (struct Client *source_p)
485{
486 rb_dump_events(stats_events_cb, source_p);
212380e3
AC
487}
488
416d868e
JT
489static void
490stats_prop_klines(struct Client *source_p)
491{
492 struct ConfItem *aconf;
493 rb_dlink_node *ptr;
494 char *user, *host, *pass, *oper_reason;
495
496 RB_DLINK_FOREACH(ptr, prop_bans.head)
497 {
498 aconf = ptr->data;
499
500 /* Skip non-klines and deactivated klines. */
501 if(aconf->status != CONF_KILL)
502 continue;
503
55abcbb2 504 get_printable_kline(source_p, aconf, &host, &pass,
416d868e
JT
505 &user, &oper_reason);
506
507 sendto_one_numeric(source_p, RPL_STATSKLINE,
508 form_str(RPL_STATSKLINE),
509 'g', host, user, pass,
510 oper_reason ? "|" : "",
511 oper_reason ? oper_reason : "");
512 }
513}
514
212380e3
AC
515static void
516stats_hubleaf(struct Client *source_p)
517{
518 struct remote_conf *hub_p;
5b96d9a6 519 rb_dlink_node *ptr;
212380e3 520
55abcbb2 521 if((ConfigFileEntry.stats_h_oper_only ||
212380e3
AC
522 (ConfigServerHide.flatten_links && !IsExemptShide(source_p))) &&
523 !IsOper(source_p))
524 {
525 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
526 form_str (ERR_NOPRIVILEGES));
527 return;
528 }
529
5b96d9a6 530 RB_DLINK_FOREACH(ptr, hubleaf_conf_list.head)
212380e3
AC
531 {
532 hub_p = ptr->data;
533
534 if(hub_p->flags & CONF_HUB)
535 sendto_one_numeric(source_p, RPL_STATSHLINE,
536 form_str(RPL_STATSHLINE),
537 hub_p->host, hub_p->server);
538 else
539 sendto_one_numeric(source_p, RPL_STATSLLINE,
540 form_str(RPL_STATSLLINE),
541 hub_p->host, hub_p->server);
542 }
543}
544
545
546static void
547stats_auth (struct Client *source_p)
548{
549 /* Oper only, if unopered, return ERR_NOPRIVS */
550 if((ConfigFileEntry.stats_i_oper_only == 2) && !IsOper (source_p))
551 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
552 form_str (ERR_NOPRIVILEGES));
553
554 /* If unopered, Only return matching auth blocks */
555 else if((ConfigFileEntry.stats_i_oper_only == 1) && !IsOper (source_p))
556 {
557 struct ConfItem *aconf;
29c92cf9
JB
558 char *name, *host, *user, *classname;
559 const char *pass = "*";
212380e3
AC
560 int port;
561
562 if(MyConnect (source_p))
563 aconf = find_conf_by_address (source_p->host, source_p->sockhost, NULL,
564 (struct sockaddr *)&source_p->localClient->ip,
565 CONF_CLIENT,
566 source_p->localClient->ip.ss_family,
40c1fd47 567 source_p->username, NULL);
212380e3
AC
568 else
569 aconf = find_conf_by_address (source_p->host, NULL, NULL, NULL, CONF_CLIENT,
40c1fd47 570 0, source_p->username, NULL);
212380e3
AC
571
572 if(aconf == NULL)
573 return;
574
575 get_printable_conf (aconf, &name, &host, &pass, &user, &port, &classname);
40c1fd47
VY
576 if(!EmptyString(aconf->spasswd))
577 pass = aconf->spasswd;
212380e3
AC
578
579 sendto_one_numeric(source_p, RPL_STATSILINE, form_str(RPL_STATSILINE),
40c1fd47 580 name, pass, show_iline_prefix(source_p, aconf, user),
212380e3
AC
581 host, port, classname);
582 }
583
584 /* Theyre opered, or allowed to see all auth blocks */
585 else
586 report_auth (source_p);
587}
588
589
590static void
591stats_tklines(struct Client *source_p)
592{
593 /* Oper only, if unopered, return ERR_NOPRIVS */
594 if((ConfigFileEntry.stats_k_oper_only == 2) && !IsOper (source_p))
595 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
596 form_str (ERR_NOPRIVILEGES));
597
598 /* If unopered, Only return matching klines */
599 else if((ConfigFileEntry.stats_k_oper_only == 1) && !IsOper (source_p))
600 {
601 struct ConfItem *aconf;
602 char *host, *pass, *user, *oper_reason;
603
604 if(MyConnect (source_p))
605 aconf = find_conf_by_address (source_p->host, source_p->sockhost, NULL,
606 (struct sockaddr *)&source_p->localClient->ip,
607 CONF_KILL,
608 source_p->localClient->ip.ss_family,
40c1fd47 609 source_p->username, NULL);
212380e3
AC
610 else
611 aconf = find_conf_by_address (source_p->host, NULL, NULL, NULL, CONF_KILL,
40c1fd47 612 0, source_p->username, NULL);
212380e3
AC
613
614 if(aconf == NULL)
615 return;
616
617 /* dont report a permanent kline as a tkline */
618 if((aconf->flags & CONF_FLAGS_TEMPORARY) == 0)
619 return;
620
621 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
622
55abcbb2 623 sendto_one_numeric(source_p, RPL_STATSKLINE,
0a621c4b
JT
624 form_str(RPL_STATSKLINE), aconf->flags & CONF_FLAGS_TEMPORARY ? 'k' : 'K',
625 host, user, pass, oper_reason ? "|" : "",
212380e3
AC
626 oper_reason ? oper_reason : "");
627 }
628 /* Theyre opered, or allowed to see all klines */
629 else
630 {
631 struct ConfItem *aconf;
5b96d9a6 632 rb_dlink_node *ptr;
212380e3
AC
633 int i;
634 char *user, *host, *pass, *oper_reason;
635
636 for(i = 0; i < LAST_TEMP_TYPE; i++)
637 {
5b96d9a6 638 RB_DLINK_FOREACH(ptr, temp_klines[i].head)
212380e3
AC
639 {
640 aconf = ptr->data;
641
55abcbb2 642 get_printable_kline(source_p, aconf, &host, &pass,
212380e3
AC
643 &user, &oper_reason);
644
645 sendto_one_numeric(source_p, RPL_STATSKLINE,
646 form_str(RPL_STATSKLINE),
647 'k', host, user, pass,
648 oper_reason ? "|" : "",
649 oper_reason ? oper_reason : "");
650 }
651 }
652 }
653}
654
34c10ca8
EM
655/* report_Klines()
656 *
657 * inputs - Client to report to, mask
658 * outputs -
659 * side effects - Reports configured K-lines to client_p.
660 */
661static void
662report_Klines(struct Client *source_p)
663{
664 char *host, *pass, *user, *oper_reason;
665 struct AddressRec *arec;
666 struct ConfItem *aconf = NULL;
667 int i;
668
669 for (i = 0; i < ATABLE_SIZE; i++)
670 {
671 for (arec = atable[i]; arec; arec = arec->next)
672 {
673 if(arec->type == CONF_KILL)
674 {
675 aconf = arec->aconf;
676
677 /* its a tempkline, theyre reported elsewhere */
678 if(aconf->flags & CONF_FLAGS_TEMPORARY)
679 continue;
680
681 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
682 sendto_one_numeric(source_p, RPL_STATSKLINE,
683 form_str(RPL_STATSKLINE),
684 'K', host, user, pass,
685 oper_reason ? "|" : "",
686 oper_reason ? oper_reason : "");
687 }
688 }
689 }
690}
691
212380e3
AC
692static void
693stats_klines(struct Client *source_p)
694{
695 /* Oper only, if unopered, return ERR_NOPRIVS */
696 if((ConfigFileEntry.stats_k_oper_only == 2) && !IsOper (source_p))
697 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
698 form_str (ERR_NOPRIVILEGES));
699
700 /* If unopered, Only return matching klines */
701 else if((ConfigFileEntry.stats_k_oper_only == 1) && !IsOper (source_p))
702 {
703 struct ConfItem *aconf;
704 char *host, *pass, *user, *oper_reason;
705
706 /* search for a kline */
707 if(MyConnect (source_p))
708 aconf = find_conf_by_address (source_p->host, source_p->sockhost, NULL,
709 (struct sockaddr *)&source_p->localClient->ip,
710 CONF_KILL,
711 source_p->localClient->ip.ss_family,
40c1fd47 712 source_p->username, NULL);
212380e3
AC
713 else
714 aconf = find_conf_by_address (source_p->host, NULL, NULL, NULL, CONF_KILL,
40c1fd47 715 0, source_p->username, NULL);
212380e3
AC
716
717 if(aconf == NULL)
718 return;
719
212380e3
AC
720 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
721
722 sendto_one_numeric(source_p, RPL_STATSKLINE, form_str(RPL_STATSKLINE),
0a621c4b
JT
723 aconf->flags & CONF_FLAGS_TEMPORARY ? 'k' : 'K',
724 host, user, pass, oper_reason ? "|" : "",
212380e3
AC
725 oper_reason ? oper_reason : "");
726 }
727 /* Theyre opered, or allowed to see all klines */
728 else
729 report_Klines (source_p);
730}
731
732static void
733stats_messages(struct Client *source_p)
734{
e8f1c19e
EM
735 struct DictionaryIter iter;
736 struct Message *msg;
737 struct alias_entry *amsg;
738
739 DICTIONARY_FOREACH(msg, &iter, cmd_dict)
740 {
741 s_assert(msg->cmd != NULL);
742 sendto_one_numeric(source_p, RPL_STATSCOMMANDS,
743 form_str(RPL_STATSCOMMANDS),
744 msg->cmd, msg->count,
745 msg->bytes, msg->rcount);
746 }
747
748 DICTIONARY_FOREACH(amsg, &iter, alias_dict)
749 {
750 s_assert(amsg->name != NULL);
751 sendto_one_numeric(source_p, RPL_STATSCOMMANDS,
752 form_str(RPL_STATSCOMMANDS),
753 amsg->name, amsg->hits, 0L, 0);
754 }
212380e3
AC
755}
756
757static void
758stats_dnsbl(struct Client *source_p)
759{
5b96d9a6 760 rb_dlink_node *ptr;
212380e3
AC
761 struct Blacklist *blptr;
762
5b96d9a6 763 RB_DLINK_FOREACH(ptr, blacklist_list.head)
212380e3
AC
764 {
765 blptr = ptr->data;
766
767 /* use RPL_STATSDEBUG for now -- jilles */
768 sendto_one_numeric(source_p, RPL_STATSDEBUG, "n :%d %s %s (%d)",
769 blptr->hits,
770 blptr->host,
771 blptr->status & CONF_ILLEGAL ? "disabled" : "active",
772 blptr->refcount);
773 }
774}
775
776static void
777stats_oper(struct Client *source_p)
778{
779 struct oper_conf *oper_p;
5b96d9a6 780 rb_dlink_node *ptr;
212380e3
AC
781
782 if(!IsOper(source_p) && ConfigFileEntry.stats_o_oper_only)
783 {
784 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
785 form_str (ERR_NOPRIVILEGES));
786 return;
787 }
788
5b96d9a6 789 RB_DLINK_FOREACH(ptr, oper_conf_list.head)
212380e3
AC
790 {
791 oper_p = ptr->data;
55abcbb2
KB
792
793 sendto_one_numeric(source_p, RPL_STATSOLINE,
212380e3
AC
794 form_str(RPL_STATSOLINE),
795 oper_p->username, oper_p->host, oper_p->name,
880c94ad 796 IsOper(source_p) ? oper_p->privset->name : "0", "-1");
212380e3
AC
797 }
798}
799
ac37f16a
AC
800static void
801stats_capability_walk(const char *line, void *data)
802{
803 struct Client *client_p = data;
804
805 sendto_one_numeric(client_p, RPL_STATSDEBUG, "C :%s", line);
806}
807
808static void
809stats_capability(struct Client *client_p)
810{
811 capability_index_stats(stats_capability_walk, client_p);
812}
813
3a177354
JT
814static void
815stats_privset(struct Client *source_p)
816{
817 privilegeset_report(source_p);
818}
212380e3
AC
819
820/* stats_operedup()
821 *
822 * input - client pointer
823 * output - none
824 * side effects - client is shown a list of active opers
825 */
826static void
827stats_operedup (struct Client *source_p)
828{
829 struct Client *target_p;
5b96d9a6 830 rb_dlink_node *oper_ptr;
212380e3
AC
831 unsigned int count = 0;
832
5b96d9a6 833 RB_DLINK_FOREACH (oper_ptr, oper_list.head)
212380e3
AC
834 {
835 target_p = oper_ptr->data;
836
837 if(IsOperInvis(target_p) && !IsOper(source_p))
838 continue;
839
c127b45b 840 if(target_p->user->away)
212380e3
AC
841 continue;
842
843 count++;
844
845 sendto_one_numeric(source_p, RPL_STATSDEBUG,
846 "p :%s (%s@%s)",
55abcbb2 847 target_p->name, target_p->username,
212380e3
AC
848 target_p->host);
849 }
850
851 sendto_one_numeric(source_p, RPL_STATSDEBUG,
852 "p :%u staff members", count);
853
854 stats_p_spy (source_p);
855}
856
857static void
858stats_ports (struct Client *source_p)
859{
860 if(!IsOper (source_p) && ConfigFileEntry.stats_P_oper_only)
861 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
862 form_str (ERR_NOPRIVILEGES));
863 else
864 show_ports (source_p);
865}
866
867static void
868stats_tresv(struct Client *source_p)
869{
870 struct ConfItem *aconf;
a4bf26dd 871 struct rb_radixtree_iteration_state state;
5b96d9a6 872 rb_dlink_node *ptr;
212380e3
AC
873 int i;
874
5b96d9a6 875 RB_DLINK_FOREACH(ptr, resv_conf_list.head)
212380e3
AC
876 {
877 aconf = ptr->data;
878 if(aconf->hold)
55abcbb2 879 sendto_one_numeric(source_p, RPL_STATSQLINE,
212380e3 880 form_str(RPL_STATSQLINE),
70ea02eb 881 'q', aconf->port, aconf->host, aconf->passwd);
212380e3
AC
882 }
883
a4bf26dd 884 RB_RADIXTREE_FOREACH(aconf, &state, resv_tree)
212380e3 885 {
212380e3 886 if(aconf->hold)
55abcbb2 887 sendto_one_numeric(source_p, RPL_STATSQLINE,
212380e3 888 form_str(RPL_STATSQLINE),
70ea02eb 889 'q', aconf->port, aconf->host, aconf->passwd);
212380e3 890 }
212380e3
AC
891}
892
893
894static void
895stats_resv(struct Client *source_p)
896{
897 struct ConfItem *aconf;
a4bf26dd 898 struct rb_radixtree_iteration_state state;
5b96d9a6 899 rb_dlink_node *ptr;
212380e3
AC
900 int i;
901
5b96d9a6 902 RB_DLINK_FOREACH(ptr, resv_conf_list.head)
212380e3
AC
903 {
904 aconf = ptr->data;
905 if(!aconf->hold)
55abcbb2 906 sendto_one_numeric(source_p, RPL_STATSQLINE,
212380e3 907 form_str(RPL_STATSQLINE),
23959371 908 'Q', aconf->port, aconf->host, aconf->passwd);
212380e3
AC
909 }
910
a4bf26dd 911 RB_RADIXTREE_FOREACH(aconf, &state, resv_tree)
212380e3 912 {
212380e3 913 if(!aconf->hold)
55abcbb2 914 sendto_one_numeric(source_p, RPL_STATSQLINE,
212380e3 915 form_str(RPL_STATSQLINE),
23959371 916 'Q', aconf->port, aconf->host, aconf->passwd);
212380e3 917 }
212380e3
AC
918}
919
eb1b303d 920static void
e9ffc3c1 921stats_ssld_foreach(void *data, pid_t pid, int cli_count, enum ssld_status status, const char *version)
eb1b303d
SA
922{
923 struct Client *source_p = data;
924
925 sendto_one_numeric(source_p, RPL_STATSDEBUG,
e9ffc3c1 926 "S :%u %c %u :%s",
eb1b303d
SA
927 pid,
928 status == SSLD_DEAD ? 'D' : (status == SSLD_SHUTDOWN ? 'S' : 'A'),
e9ffc3c1
SA
929 cli_count,
930 version);
eb1b303d
SA
931}
932
933static void
934stats_ssld(struct Client *source_p)
935{
936 ssld_foreach_info(stats_ssld_foreach, source_p);
937}
938
212380e3
AC
939static void
940stats_usage (struct Client *source_p)
941{
942 struct rusage rus;
943 time_t secs;
944 time_t rup;
945#ifdef hz
946# define hzz hz
947#else
948# ifdef HZ
949# define hzz HZ
950# else
951 int hzz = 1;
952# endif
953#endif
954
955 if(getrusage(RUSAGE_SELF, &rus) == -1)
956 {
957 sendto_one_notice(source_p, ":Getruseage error: %s.",
958 strerror(errno));
959 return;
960 }
961 secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec;
962 if(0 == secs)
963 secs = 1;
964
e3354945 965 rup = (rb_current_time() - startup_time) * hzz;
212380e3
AC
966 if(0 == rup)
967 rup = 1;
55abcbb2 968
212380e3 969 sendto_one_numeric(source_p, RPL_STATSDEBUG,
e4ce3b54 970 "R :CPU Secs %d:%02d User %d:%02d System %d:%02d",
212380e3
AC
971 (int) (secs / 60), (int) (secs % 60),
972 (int) (rus.ru_utime.tv_sec / 60),
973 (int) (rus.ru_utime.tv_sec % 60),
55abcbb2 974 (int) (rus.ru_stime.tv_sec / 60),
212380e3
AC
975 (int) (rus.ru_stime.tv_sec % 60));
976 sendto_one_numeric(source_p, RPL_STATSDEBUG,
977 "R :RSS %ld ShMem %ld Data %ld Stack %ld",
55abcbb2 978 rus.ru_maxrss, (rus.ru_ixrss / rup),
212380e3
AC
979 (rus.ru_idrss / rup), (rus.ru_isrss / rup));
980 sendto_one_numeric(source_p, RPL_STATSDEBUG,
981 "R :Swaps %d Reclaims %d Faults %d",
982 (int) rus.ru_nswap, (int) rus.ru_minflt, (int) rus.ru_majflt);
983 sendto_one_numeric(source_p, RPL_STATSDEBUG,
984 "R :Block in %d out %d",
985 (int) rus.ru_inblock, (int) rus.ru_oublock);
986 sendto_one_numeric(source_p, RPL_STATSDEBUG,
987 "R :Msg Rcv %d Send %d",
988 (int) rus.ru_msgrcv, (int) rus.ru_msgsnd);
989 sendto_one_numeric(source_p, RPL_STATSDEBUG,
990 "R :Signals %d Context Vol. %d Invol %d",
55abcbb2 991 (int) rus.ru_nsignals, (int) rus.ru_nvcsw,
212380e3
AC
992 (int) rus.ru_nivcsw);
993}
994
54ac8b60
VY
995static void
996stats_tstats (struct Client *source_p)
997{
47adde3d
VY
998 struct Client *target_p;
999 struct ServerStatistics sp;
1000 rb_dlink_node *ptr;
1001
1002 memcpy(&sp, &ServerStats, sizeof(struct ServerStatistics));
1003
8bd5767b
JT
1004 RB_DLINK_FOREACH(ptr, serv_list.head)
1005 {
1006 target_p = ptr->data;
1007
1008 sp.is_sbs += target_p->localClient->sendB;
1009 sp.is_sbr += target_p->localClient->receiveB;
679ccbe5 1010 sp.is_sti += (unsigned long long)(rb_current_time() - target_p->localClient->firsttime);
8bd5767b
JT
1011 sp.is_sv++;
1012 }
1013
1014 RB_DLINK_FOREACH(ptr, lclient_list.head)
1015 {
1016 target_p = ptr->data;
1017
1018 sp.is_cbs += target_p->localClient->sendB;
1019 sp.is_cbr += target_p->localClient->receiveB;
679ccbe5 1020 sp.is_cti += (unsigned long long)(rb_current_time() - target_p->localClient->firsttime);
8bd5767b 1021 sp.is_cl++;
47adde3d
VY
1022 }
1023
1024 RB_DLINK_FOREACH(ptr, unknown_list.head)
1025 {
1026 sp.is_ni++;
1027 }
1028
1029 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1030 "T :accepts %u refused %u", sp.is_ac, sp.is_ref);
1031 sendto_one_numeric(source_p, RPL_STATSDEBUG,
55abcbb2 1032 "T :rejected %u delaying %lu",
43946961 1033 sp.is_rej, delay_exit_length());
ae09cb7d
JT
1034 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1035 "T :throttled refused %u throttle list size %lu", sp.is_thr, throttle_size());
47adde3d
VY
1036 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1037 "T :nicks being delayed %lu",
1038 get_nd_count());
1039 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1040 "T :unknown commands %u prefixes %u",
1041 sp.is_unco, sp.is_unpf);
1042 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1043 "T :nick collisions %u saves %u unknown closes %u",
1044 sp.is_kill, sp.is_save, sp.is_ni);
1045 sendto_one_numeric(source_p, RPL_STATSDEBUG,
55abcbb2 1046 "T :wrong direction %u empty %u",
47adde3d
VY
1047 sp.is_wrdi, sp.is_empt);
1048 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1049 "T :numerics seen %u", sp.is_num);
1050 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1051 "T :tgchange blocked msgs %u restricted addrs %lu",
1052 sp.is_tgch, rb_dlink_list_length(&tgchange_list));
e88a1f1b
KB
1053 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1054 "T :ratelimit blocked commands %u", sp.is_rl);
47adde3d
VY
1055 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1056 "T :auth successes %u fails %u",
1057 sp.is_asuc, sp.is_abad);
1058 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1059 "T :sasl successes %u fails %u",
1060 sp.is_ssuc, sp.is_sbad);
1061 sendto_one_numeric(source_p, RPL_STATSDEBUG, "T :Client Server");
1062 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1063 "T :connected %u %u", sp.is_cl, sp.is_sv);
8bd5767b
JT
1064 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1065 "T :bytes sent %lluK %lluK",
55abcbb2 1066 sp.is_cbs / 1024,
8bd5767b
JT
1067 sp.is_sbs / 1024);
1068 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1069 "T :bytes recv %lluK %lluK",
55abcbb2 1070 sp.is_cbr / 1024,
7685dd09 1071 sp.is_sbr / 1024);
8bd5767b 1072 sendto_one_numeric(source_p, RPL_STATSDEBUG,
679ccbe5
AS
1073 "T :time connected %llu %llu",
1074 sp.is_cti, sp.is_sti);
212380e3
AC
1075}
1076
1077static void
1078stats_uptime (struct Client *source_p)
1079{
1080 time_t now;
1081
e3354945 1082 now = rb_current_time() - startup_time;
55abcbb2 1083 sendto_one_numeric(source_p, RPL_STATSUPTIME,
212380e3 1084 form_str (RPL_STATSUPTIME),
77910830
JT
1085 (int)(now / 86400), (int)((now / 3600) % 24),
1086 (int)((now / 60) % 60), (int)(now % 60));
212380e3
AC
1087 sendto_one_numeric(source_p, RPL_STATSCONN,
1088 form_str (RPL_STATSCONN),
55abcbb2 1089 MaxConnectionCount, MaxClientCount,
212380e3
AC
1090 Count.totalrestartcount);
1091}
1092
1093struct shared_flags
1094{
1095 int flag;
1096 char letter;
1097};
1098static struct shared_flags shared_flagtable[] =
1099{
1100 { SHARED_PKLINE, 'K' },
1101 { SHARED_TKLINE, 'k' },
1102 { SHARED_UNKLINE, 'U' },
1103 { SHARED_PXLINE, 'X' },
1104 { SHARED_TXLINE, 'x' },
1105 { SHARED_UNXLINE, 'Y' },
1106 { SHARED_PRESV, 'Q' },
1107 { SHARED_TRESV, 'q' },
1108 { SHARED_UNRESV, 'R' },
1109 { SHARED_LOCOPS, 'L' },
1110 { SHARED_REHASH, 'H' },
7d91f0da
JT
1111 { SHARED_TDLINE, 'd' },
1112 { SHARED_PDLINE, 'D' },
1113 { SHARED_UNDLINE, 'E' },
fe749d37 1114 { SHARED_GRANT, 'G' },
cc7ae51c 1115 { SHARED_DIE, 'I' },
212380e3
AC
1116 { 0, '\0'}
1117};
1118
1119
1120static void
1121stats_shared (struct Client *source_p)
1122{
1123 struct remote_conf *shared_p;
5b96d9a6 1124 rb_dlink_node *ptr;
59134282 1125 char buf[sizeof(shared_flagtable)/sizeof(shared_flagtable[0])];
212380e3
AC
1126 char *p;
1127 int i;
1128
5b96d9a6 1129 RB_DLINK_FOREACH(ptr, shared_conf_list.head)
212380e3
AC
1130 {
1131 shared_p = ptr->data;
1132
1133 p = buf;
1134
1135 *p++ = 'c';
1136
1137 for(i = 0; shared_flagtable[i].flag != 0; i++)
1138 {
1139 if(shared_p->flags & shared_flagtable[i].flag)
1140 *p++ = shared_flagtable[i].letter;
1141 }
1142
1143 *p = '\0';
1144
55abcbb2 1145 sendto_one_numeric(source_p, RPL_STATSULINE,
212380e3
AC
1146 form_str(RPL_STATSULINE),
1147 shared_p->server, shared_p->username,
1148 shared_p->host, buf);
1149 }
1150
5b96d9a6 1151 RB_DLINK_FOREACH(ptr, cluster_conf_list.head)
212380e3
AC
1152 {
1153 shared_p = ptr->data;
1154
1155 p = buf;
1156
1157 *p++ = 'C';
1158
1159 for(i = 0; shared_flagtable[i].flag != 0; i++)
1160 {
1161 if(shared_p->flags & shared_flagtable[i].flag)
1162 *p++ = shared_flagtable[i].letter;
1163 }
1164
1165 *p = '\0';
1166
55abcbb2 1167 sendto_one_numeric(source_p, RPL_STATSULINE,
212380e3
AC
1168 form_str(RPL_STATSULINE),
1169 shared_p->server, "*", "*", buf);
1170 }
1171}
1172
1173/* stats_servers()
1174 *
1175 * input - client pointer
1176 * output - none
1177 * side effects - client is shown lists of who connected servers
1178 */
1179static void
1180stats_servers (struct Client *source_p)
1181{
1182 struct Client *target_p;
5b96d9a6 1183 rb_dlink_node *ptr;
212380e3
AC
1184 time_t seconds;
1185 int days, hours, minutes;
1186 int j = 0;
1187
1188 if(ConfigServerHide.flatten_links && !IsOper(source_p) &&
1189 !IsExemptShide(source_p))
1190 {
1191 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
1192 form_str (ERR_NOPRIVILEGES));
1193 return;
1194 }
1195
5b96d9a6 1196 RB_DLINK_FOREACH (ptr, serv_list.head)
212380e3
AC
1197 {
1198 target_p = ptr->data;
1199
1200 j++;
e3354945 1201 seconds = rb_current_time() - target_p->localClient->firsttime;
212380e3
AC
1202
1203 days = (int) (seconds / 86400);
1204 seconds %= 86400;
1205 hours = (int) (seconds / 3600);
1206 seconds %= 3600;
1207 minutes = (int) (seconds / 60);
1208 seconds %= 60;
1209
1210 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1211 "V :%s (%s!*@*) Idle: %d SendQ: %d "
1212 "Connected: %d day%s, %d:%02d:%02d",
1213 target_p->name,
1214 (target_p->serv->by[0] ? target_p->serv->by : "Remote."),
e3354945 1215 (int) (rb_current_time() - target_p->localClient->lasttime),
e8e79621 1216 (int) rb_linebuf_len (&target_p->localClient->buf_sendq),
55abcbb2 1217 days, (days == 1) ? "" : "s", hours, minutes,
212380e3
AC
1218 (int) seconds);
1219 }
1220
1221 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1222 "V :%d Server(s)", j);
1223}
1224
1225static void
1226stats_tgecos(struct Client *source_p)
1227{
1228 struct ConfItem *aconf;
5b96d9a6 1229 rb_dlink_node *ptr;
212380e3 1230
5b96d9a6 1231 RB_DLINK_FOREACH(ptr, xline_conf_list.head)
212380e3
AC
1232 {
1233 aconf = ptr->data;
1234
1235 if(aconf->hold)
1236 sendto_one_numeric(source_p, RPL_STATSXLINE,
1237 form_str(RPL_STATSXLINE),
23959371 1238 'x', aconf->port, aconf->host,
212380e3
AC
1239 aconf->passwd);
1240 }
1241}
1242
1243static void
1244stats_gecos(struct Client *source_p)
1245{
1246 struct ConfItem *aconf;
5b96d9a6 1247 rb_dlink_node *ptr;
212380e3 1248
5b96d9a6 1249 RB_DLINK_FOREACH(ptr, xline_conf_list.head)
212380e3
AC
1250 {
1251 aconf = ptr->data;
1252
1253 if(!aconf->hold)
1254 sendto_one_numeric(source_p, RPL_STATSXLINE,
1255 form_str(RPL_STATSXLINE),
55abcbb2 1256 'X', aconf->port, aconf->host,
212380e3
AC
1257 aconf->passwd);
1258 }
1259}
1260
1261static void
1262stats_class(struct Client *source_p)
1263{
1264 if(ConfigFileEntry.stats_y_oper_only && !IsOper(source_p))
1265 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
1266 form_str (ERR_NOPRIVILEGES));
1267 else
1268 report_classes(source_p);
1269}
1270
1271static void
1272stats_memory (struct Client *source_p)
1273{
47adde3d
VY
1274 struct Client *target_p;
1275 struct Channel *chptr;
47adde3d
VY
1276 rb_dlink_node *rb_dlink;
1277 rb_dlink_node *ptr;
1278 int channel_count = 0;
1279 int local_client_conf_count = 0; /* local client conf links */
1280 int users_counted = 0; /* user structs */
1281
1282 int channel_users = 0;
1283 int channel_invites = 0;
1284 int channel_bans = 0;
1285 int channel_except = 0;
1286 int channel_invex = 0;
1287 int channel_quiets = 0;
1288
1289 int class_count = 0; /* classes */
1290 int conf_count = 0; /* conf lines */
1291 int users_invited_count = 0; /* users invited */
1292 int user_channels = 0; /* users in channels */
c127b45b 1293 int aways_counted = 0;
47adde3d
VY
1294 size_t number_servers_cached; /* number of servers cached by scache */
1295
1296 size_t channel_memory = 0;
1297 size_t channel_ban_memory = 0;
1298 size_t channel_except_memory = 0;
1299 size_t channel_invex_memory = 0;
1300 size_t channel_quiet_memory = 0;
1301
c127b45b 1302 size_t away_memory = 0; /* memory used by aways */
47adde3d
VY
1303 size_t ww = 0; /* whowas array count */
1304 size_t wwm = 0; /* whowas array memory used */
1305 size_t conf_memory = 0; /* memory used by conf lines */
1306 size_t mem_servers_cached; /* memory used by scache */
1307
1308 size_t linebuf_count = 0;
1309 size_t linebuf_memory_used = 0;
1310
1311 size_t total_channel_memory = 0;
1312 size_t totww = 0;
1313
1314 size_t local_client_count = 0;
1315 size_t local_client_memory_used = 0;
1316
1317 size_t remote_client_count = 0;
1318 size_t remote_client_memory_used = 0;
1319
1320 size_t total_memory = 0;
1321
b47f8a4f 1322 whowas_memory_usage(&ww, &wwm);
47adde3d
VY
1323
1324 RB_DLINK_FOREACH(ptr, global_client_list.head)
1325 {
1326 target_p = ptr->data;
1327 if(MyConnect(target_p))
1328 {
1329 local_client_conf_count++;
1330 }
1331
1332 if(target_p->user)
1333 {
1334 users_counted++;
1335 users_invited_count += rb_dlink_list_length(&target_p->user->invited);
1336 user_channels += rb_dlink_list_length(&target_p->user->channel);
c127b45b
SB
1337 if(target_p->user->away)
1338 {
1339 aways_counted++;
1340 away_memory += (strlen(target_p->user->away) + 1);
1341 }
47adde3d
VY
1342 }
1343 }
1344
1345 /* Count up all channels, ban lists, except lists, Invex lists */
1346 RB_DLINK_FOREACH(ptr, global_channel_list.head)
1347 {
1348 chptr = ptr->data;
1349 channel_count++;
1350 channel_memory += (strlen(chptr->chname) + sizeof(struct Channel));
1351
1352 channel_users += rb_dlink_list_length(&chptr->members);
1353 channel_invites += rb_dlink_list_length(&chptr->invites);
1354
1355 RB_DLINK_FOREACH(rb_dlink, chptr->banlist.head)
1356 {
47adde3d
VY
1357 channel_bans++;
1358
1359 channel_ban_memory += sizeof(rb_dlink_node) + sizeof(struct Ban);
1360 }
1361
1362 RB_DLINK_FOREACH(rb_dlink, chptr->exceptlist.head)
1363 {
47adde3d
VY
1364 channel_except++;
1365
1366 channel_except_memory += (sizeof(rb_dlink_node) + sizeof(struct Ban));
1367 }
1368
1369 RB_DLINK_FOREACH(rb_dlink, chptr->invexlist.head)
1370 {
47adde3d
VY
1371 channel_invex++;
1372
1373 channel_invex_memory += (sizeof(rb_dlink_node) + sizeof(struct Ban));
1374 }
1375
1376 RB_DLINK_FOREACH(rb_dlink, chptr->quietlist.head)
1377 {
47adde3d
VY
1378 channel_quiets++;
1379
1380 channel_quiet_memory += (sizeof(rb_dlink_node) + sizeof(struct Ban));
1381 }
1382 }
1383
1384 /* count up all classes */
1385
1386 class_count = rb_dlink_list_length(&class_list) + 1;
1387
1388 rb_count_rb_linebuf_memory(&linebuf_count, &linebuf_memory_used);
1389
1390 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1391 "z :Users %u(%lu) Invites %u(%lu)",
1392 users_counted,
1393 (unsigned long) users_counted * sizeof(struct User),
55abcbb2 1394 users_invited_count,
47adde3d
VY
1395 (unsigned long) users_invited_count * sizeof(rb_dlink_node));
1396
1397 sendto_one_numeric(source_p, RPL_STATSDEBUG,
c127b45b 1398 "z :User channels %u(%lu) Aways %u(%d)",
47adde3d 1399 user_channels,
c127b45b
SB
1400 (unsigned long) user_channels * sizeof(rb_dlink_node),
1401 aways_counted, (int) away_memory);
47adde3d
VY
1402
1403 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1404 "z :Attached confs %u(%lu)",
1405 local_client_conf_count,
1406 (unsigned long) local_client_conf_count * sizeof(rb_dlink_node));
1407
1408 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1409 "z :Conflines %u(%d)", conf_count, (int) conf_memory);
1410
1411 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1412 "z :Classes %u(%lu)",
55abcbb2 1413 class_count,
47adde3d
VY
1414 (unsigned long) class_count * sizeof(struct Class));
1415
1416 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1417 "z :Channels %u(%d)",
1418 channel_count, (int) channel_memory);
1419
1420 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1421 "z :Bans %u(%d) Exceptions %u(%d) Invex %u(%d) Quiets %u(%d)",
1422 channel_bans, (int) channel_ban_memory,
1423 channel_except, (int) channel_except_memory,
1424 channel_invex, (int) channel_invex_memory,
1425 channel_quiets, (int) channel_quiet_memory);
1426
1427 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1428 "z :Channel members %u(%lu) invite %u(%lu)",
1429 channel_users,
1430 (unsigned long) channel_users * sizeof(rb_dlink_node),
55abcbb2 1431 channel_invites,
47adde3d
VY
1432 (unsigned long) channel_invites * sizeof(rb_dlink_node));
1433
1434 total_channel_memory = channel_memory +
1435 channel_ban_memory +
1436 channel_users * sizeof(rb_dlink_node) + channel_invites * sizeof(rb_dlink_node);
1437
1438 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1439 "z :Whowas array %ld(%ld)",
1440 (long)ww, (long)wwm);
1441
1442 totww = wwm;
1443
1444 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1445 "z :Hash: client %u(%ld) chan %u(%ld)",
55abcbb2 1446 U_MAX, (long)(U_MAX * sizeof(rb_dlink_list)),
47adde3d
VY
1447 CH_MAX, (long)(CH_MAX * sizeof(rb_dlink_list)));
1448
1449 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1450 "z :linebuf %ld(%ld)",
1451 (long)linebuf_count, (long)linebuf_memory_used);
1452
1453 count_scache(&number_servers_cached, &mem_servers_cached);
1454
1455 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1456 "z :scache %ld(%ld)",
1457 (long)number_servers_cached, (long)mem_servers_cached);
1458
1459 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1460 "z :hostname hash %d(%ld)",
1461 HOST_MAX, (long)HOST_MAX * sizeof(rb_dlink_list));
1462
1463 total_memory = totww + total_channel_memory + conf_memory +
1464 class_count * sizeof(struct Class);
1465
1466 total_memory += mem_servers_cached;
1467 sendto_one_numeric(source_p, RPL_STATSDEBUG,
55abcbb2
KB
1468 "z :Total: whowas %d channel %d conf %d",
1469 (int) totww, (int) total_channel_memory,
47adde3d
VY
1470 (int) conf_memory);
1471
1472 count_local_client_memory(&local_client_count, &local_client_memory_used);
1473 total_memory += local_client_memory_used;
1474
1475 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1476 "z :Local client Memory in use: %ld(%ld)",
1477 (long)local_client_count, (long)local_client_memory_used);
1478
1479
1480 count_remote_client_memory(&remote_client_count, &remote_client_memory_used);
1481 total_memory += remote_client_memory_used;
1482
1483 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1484 "z :Remote client Memory in use: %ld(%ld)",
1485 (long)remote_client_count,
1486 (long)remote_client_memory_used);
212380e3
AC
1487}
1488
1489static void
1490stats_ziplinks (struct Client *source_p)
1491{
5b96d9a6 1492 rb_dlink_node *ptr;
212380e3 1493 struct Client *target_p;
4c79bbd3 1494 struct ZipStats *zipstats;
212380e3 1495 int sent_data = 0;
70b72a07 1496 char buf[128], buf1[128];
5b96d9a6 1497 RB_DLINK_FOREACH (ptr, serv_list.head)
212380e3
AC
1498 {
1499 target_p = ptr->data;
1500 if(IsCapable (target_p, CAP_ZIP))
1501 {
55abcbb2 1502 zipstats = target_p->localClient->zipstats;
4c79bbd3
VY
1503 sprintf(buf, "%.2f%%", zipstats->out_ratio);
1504 sprintf(buf1, "%.2f%%", zipstats->in_ratio);
212380e3 1505 sendto_one_numeric(source_p, RPL_STATSDEBUG,
70b72a07 1506 "Z :ZipLinks stats for %s send[%s compression "
4c79bbd3
VY
1507 "(%llu kB data/%llu kB wire)] recv[%s compression "
1508 "(%llu kB data/%llu kB wire)]",
212380e3 1509 target_p->name,
55abcbb2
KB
1510 buf, zipstats->out >> 10,
1511 zipstats->out_wire >> 10, buf1,
4c79bbd3 1512 zipstats->in >> 10, zipstats->in_wire >> 10);
212380e3
AC
1513 sent_data++;
1514 }
1515 }
1516
1517 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1518 "Z :%u ziplink(s)", sent_data);
1519}
1520
1521static void
1522stats_servlinks (struct Client *source_p)
1523{
1524 static char Sformat[] = ":%s %d %s %s %u %u %u %u %u :%u %u %s";
1525 long uptime, sendK, receiveK;
1526 struct Client *target_p;
5b96d9a6 1527 rb_dlink_node *ptr;
212380e3 1528 int j = 0;
70b72a07 1529 char buf[128];
212380e3
AC
1530
1531 if(ConfigServerHide.flatten_links && !IsOper (source_p) &&
1532 !IsExemptShide(source_p))
1533 {
1534 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
1535 form_str (ERR_NOPRIVILEGES));
1536 return;
1537 }
1538
1539 sendK = receiveK = 0;
1540
5b96d9a6 1541 RB_DLINK_FOREACH (ptr, serv_list.head)
212380e3
AC
1542 {
1543 target_p = ptr->data;
1544
1545 j++;
1546 sendK += target_p->localClient->sendK;
1547 receiveK += target_p->localClient->receiveK;
1548
1549 sendto_one(source_p, Sformat,
1550 get_id(&me, source_p), RPL_STATSLINKINFO, get_id(source_p, source_p),
b3ebc7ab 1551 target_p->name,
d269d0b6 1552 (int) rb_linebuf_len (&target_p->localClient->buf_sendq),
212380e3
AC
1553 (int) target_p->localClient->sendM,
1554 (int) target_p->localClient->sendK,
1555 (int) target_p->localClient->receiveM,
1556 (int) target_p->localClient->receiveK,
e3354945 1557 rb_current_time() - target_p->localClient->firsttime,
55abcbb2 1558 (rb_current_time() > target_p->localClient->lasttime) ?
e3354945 1559 (rb_current_time() - target_p->localClient->lasttime) : 0,
212380e3
AC
1560 IsOper (source_p) ? show_capabilities (target_p) : "TS");
1561 }
1562
1563 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1564 "? :%u total server(s)", j);
1565
70b72a07 1566 snprintf(buf, sizeof buf, "%7.2f", _GMKv ((sendK)));
212380e3 1567 sendto_one_numeric(source_p, RPL_STATSDEBUG,
70b72a07
JT
1568 "? :Sent total : %s %s",
1569 buf, _GMKs (sendK));
1570 snprintf(buf, sizeof buf, "%7.2f", _GMKv ((receiveK)));
212380e3 1571 sendto_one_numeric(source_p, RPL_STATSDEBUG,
70b72a07
JT
1572 "? :Recv total : %s %s",
1573 buf, _GMKs (receiveK));
212380e3 1574
e3354945 1575 uptime = (rb_current_time() - startup_time);
70b72a07 1576 snprintf(buf, sizeof buf, "%7.2f %s (%4.1f K/s)",
55abcbb2 1577 _GMKv (me.localClient->sendK),
212380e3
AC
1578 _GMKs (me.localClient->sendK),
1579 (float) ((float) me.localClient->sendK / (float) uptime));
70b72a07
JT
1580 sendto_one_numeric(source_p, RPL_STATSDEBUG, "? :Server send: %s", buf);
1581 snprintf(buf, sizeof buf, "%7.2f %s (%4.1f K/s)",
212380e3
AC
1582 _GMKv (me.localClient->receiveK),
1583 _GMKs (me.localClient->receiveK),
1584 (float) ((float) me.localClient->receiveK / (float) uptime));
70b72a07 1585 sendto_one_numeric(source_p, RPL_STATSDEBUG, "? :Server recv: %s", buf);
212380e3
AC
1586}
1587
3c7d6fcc 1588static inline bool
4727c0f5
AC
1589stats_l_should_show_oper(struct Client *target_p)
1590{
3c7d6fcc 1591 return (!IsOperInvis(target_p));
4727c0f5
AC
1592}
1593
212380e3
AC
1594static void
1595stats_ltrace(struct Client *source_p, int parc, const char *parv[])
1596{
3c7d6fcc
EM
1597 bool doall = false;
1598 bool wilds = false;
212380e3
AC
1599 const char *name;
1600 char statchar = parv[1][0];
1601
1602 /* this is def targeted at us somehow.. */
1603 if(parc > 2 && !EmptyString(parv[2]))
1604 {
1605 /* directed at us generically? */
1606 if(match(parv[2], me.name) ||
1607 (!MyClient(source_p) && !irccmp(parv[2], me.id)))
1608 {
1609 name = me.name;
3c7d6fcc 1610 doall = true;
212380e3
AC
1611 }
1612 else
1613 {
1614 name = parv[2];
1615 wilds = strchr(name, '*') || strchr(name, '?');
1616 }
1617
1618 /* must be directed at a specific person thats not us */
1619 if(!doall && !wilds)
1620 {
1621 struct Client *target_p;
1622
1623 if(MyClient(source_p))
1624 target_p = find_named_person(name);
1625 else
1626 target_p = find_person(name);
1627
1628 if(target_p != NULL)
1629 {
1630 stats_spy(source_p, statchar, target_p->name);
1631 stats_l_client(source_p, target_p, statchar);
1632 }
1633 else
1634 sendto_one_numeric(source_p, ERR_NOSUCHSERVER,
1635 form_str(ERR_NOSUCHSERVER),
1636 name);
1637
1638 return;
1639 }
1640 }
1641 else
1642 {
1643 name = me.name;
3c7d6fcc 1644 doall = true;
212380e3
AC
1645 }
1646
1647 stats_spy(source_p, statchar, name);
1648
1649 if(doall)
1650 {
1651 /* local opers get everyone */
1652 if(MyOper(source_p))
1653 {
e82bda18
AC
1654 stats_l_list(source_p, name, doall, wilds, &unknown_list, statchar, NULL);
1655 stats_l_list(source_p, name, doall, wilds, &lclient_list, statchar, NULL);
212380e3
AC
1656 }
1657 else
1658 {
1659 /* they still need themselves if theyre local.. */
1660 if(MyClient(source_p))
1661 stats_l_client(source_p, source_p, statchar);
1662
4727c0f5 1663 stats_l_list(source_p, name, doall, wilds, &local_oper_list, statchar, stats_l_should_show_oper);
212380e3
AC
1664 }
1665
1666 if (!ConfigServerHide.flatten_links || IsOper(source_p) ||
1667 IsExemptShide(source_p))
e82bda18 1668 stats_l_list(source_p, name, doall, wilds, &serv_list, statchar, NULL);
212380e3
AC
1669
1670 return;
1671 }
1672
1673 /* ok, at this point theyre looking for a specific client whos on
1674 * our server.. but it contains a wildcard. --fl
1675 */
e82bda18 1676 stats_l_list(source_p, name, doall, wilds, &lclient_list, statchar, NULL);
212380e3
AC
1677
1678 return;
1679}
1680
212380e3 1681static void
3c7d6fcc
EM
1682stats_l_list(struct Client *source_p, const char *name, bool doall, bool wilds,
1683 rb_dlink_list * list, char statchar, bool (*check_fn)(struct Client *target_p))
212380e3 1684{
5b96d9a6 1685 rb_dlink_node *ptr;
212380e3
AC
1686 struct Client *target_p;
1687
1688 /* send information about connections which match. note, we
1689 * dont need tests for IsInvisible(), because non-opers will
1690 * never get here for normal clients --fl
1691 */
5b96d9a6 1692 RB_DLINK_FOREACH(ptr, list->head)
212380e3
AC
1693 {
1694 target_p = ptr->data;
1695
1696 if(!doall && wilds && !match(name, target_p->name))
1697 continue;
1698
e82bda18
AC
1699 if (check_fn == NULL || check_fn(target_p))
1700 stats_l_client(source_p, target_p, statchar);
212380e3
AC
1701 }
1702}
1703
1704void
1705stats_l_client(struct Client *source_p, struct Client *target_p,
1706 char statchar)
1707{
1708 if(IsAnyServer(target_p))
1709 {
1710 sendto_one_numeric(source_p, RPL_STATSLINKINFO, Lformat,
b3ebc7ab 1711 target_p->name,
d269d0b6 1712 (int) rb_linebuf_len(&target_p->localClient->buf_sendq),
212380e3
AC
1713 (int) target_p->localClient->sendM,
1714 (int) target_p->localClient->sendK,
1715 (int) target_p->localClient->receiveM,
1716 (int) target_p->localClient->receiveK,
e3354945 1717 rb_current_time() - target_p->localClient->firsttime,
55abcbb2 1718 (rb_current_time() > target_p->localClient->lasttime) ?
e3354945 1719 (rb_current_time() - target_p->localClient->lasttime) : 0,
212380e3
AC
1720 IsOper(source_p) ? show_capabilities(target_p) : "-");
1721 }
1722
1723 else
1724 {
1725 sendto_one_numeric(source_p, RPL_STATSLINKINFO, Lformat,
1726 show_ip(source_p, target_p) ?
1727 (IsUpper(statchar) ?
1728 get_client_name(target_p, SHOW_IP) :
1729 get_client_name(target_p, HIDE_IP)) :
1730 get_client_name(target_p, MASK_IP),
d269d0b6 1731 (int) rb_linebuf_len(&target_p->localClient->buf_sendq),
212380e3
AC
1732 (int) target_p->localClient->sendM,
1733 (int) target_p->localClient->sendK,
1734 (int) target_p->localClient->receiveM,
1735 (int) target_p->localClient->receiveK,
e3354945 1736 rb_current_time() - target_p->localClient->firsttime,
55abcbb2 1737 (rb_current_time() > target_p->localClient->lasttime) ?
e3354945 1738 (rb_current_time() - target_p->localClient->lasttime) : 0,
212380e3
AC
1739 "-");
1740 }
1741}
1742
f237e31a
JT
1743static void
1744rb_dump_fd_callback(int fd, const char *desc, void *data)
1745{
1746 struct Client *source_p = data;
1747 sendto_one_numeric(source_p, RPL_STATSDEBUG, "F :fd %-3d desc '%s'", fd, desc);
1748}
1749
1750static void
1751stats_comm(struct Client *source_p)
1752{
1753 rb_dump_fd(rb_dump_fd_callback, source_p);
a235e410
VY
1754}
1755
212380e3
AC
1756/*
1757 * stats_spy
1758 *
1759 * inputs - pointer to client doing the /stats
1760 * - char letter they are doing /stats on
1761 * output - none
1762 * side effects -
1763 * This little helper function reports to opers if configured.
212380e3 1764 */
ea2d2700 1765static int
212380e3
AC
1766stats_spy(struct Client *source_p, char statchar, const char *name)
1767{
1768 hook_data_int data;
1769
1770 data.client = source_p;
1771 data.arg1 = name;
1772 data.arg2 = (int) statchar;
ea2d2700 1773 data.result = 0;
212380e3
AC
1774
1775 call_hook(doing_stats_hook, &data);
ea2d2700
AC
1776
1777 return data.result;
212380e3
AC
1778}
1779
1780/* stats_p_spy()
1781 *
1782 * input - pointer to client doing stats
1783 * ouput -
1784 * side effects - call hook doing_stats_p
1785 */
1786static void
1787stats_p_spy (struct Client *source_p)
1788{
1789 hook_data data;
1790
1791 data.client = source_p;
1792 data.arg1 = data.arg2 = NULL;
1793
1794 call_hook(doing_stats_p_hook, &data);
1795}