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