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