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