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