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