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