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