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