]> jfr.im git - irc/rqf/shadowircd.git/blame - modules/m_stats.c
Add PRIVS extension, a command that shows oper and some auth{} privileges.
[irc/rqf/shadowircd.git] / modules / m_stats.c
CommitLineData
212380e3 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 "tools.h" /* dlink_node/dlink_list */
29#include "class.h" /* report_classes */
30#include "client.h" /* Client */
31#include "common.h" /* TRUE/FALSE */
32#include "irc_string.h"
33#include "ircd.h" /* me */
34#include "listener.h" /* show_ports */
35#include "s_gline.h"
36#include "msg.h" /* Message */
37#include "hostmask.h" /* report_mtrie_conf_links */
38#include "numeric.h" /* ERR_xxx */
39#include "scache.h" /* list_scache */
40#include "send.h" /* sendto_one */
41#include "commio.h" /* highest_fd */
42#include "s_conf.h" /* ConfItem */
43#include "s_serv.h" /* hunt_server */
44#include "s_stats.h" /* tstats */
45#include "s_user.h" /* show_opers */
46#include "event.h" /* events */
47#include "blacklist.h" /* dnsbl stuff */
48#include "linebuf.h"
49#include "parse.h"
50#include "modules.h"
51#include "hook.h"
52#include "s_newconf.h"
53#include "hash.h"
54
55static int m_stats (struct Client *, struct Client *, int, const char **);
56
57struct Message stats_msgtab = {
58 "STATS", 0, 0, 0, MFLG_SLOW,
59 {mg_unreg, {m_stats, 2}, {m_stats, 3}, mg_ignore, mg_ignore, {m_stats, 2}}
60};
61
62int doing_stats_hook;
63int doing_stats_p_hook;
64
65mapi_clist_av1 stats_clist[] = { &stats_msgtab, NULL };
66mapi_hlist_av1 stats_hlist[] = {
67 { "doing_stats", &doing_stats_hook },
68 { "doing_stats_p", &doing_stats_p_hook },
69 { NULL, NULL }
70};
71
72DECLARE_MODULE_AV1(stats, NULL, NULL, stats_clist, stats_hlist, NULL, "$Revision: 1608 $");
73
74const char *Lformat = "%s %u %u %u %u %u :%u %u %s";
75
76static void stats_l_list(struct Client *s, const char *, int, int, dlink_list *, char);
77static void stats_l_client(struct Client *source_p, struct Client *target_p,
78 char statchar);
79
80static void stats_spy(struct Client *, char, const char *);
81static void stats_p_spy(struct Client *);
82
83/* Heres our struct for the stats table */
84struct StatsStruct
85{
86 char letter;
87 void (*handler) ();
88 int need_oper;
89 int need_admin;
90};
91
92static void stats_dns_servers (struct Client *);
93static void stats_delay(struct Client *);
94static void stats_hash(struct Client *);
95static void stats_connect(struct Client *);
96static void stats_tdeny(struct Client *);
97static void stats_deny(struct Client *);
98static void stats_exempt(struct Client *);
99static void stats_events(struct Client *);
100static void stats_glines(struct Client *);
101static void stats_pending_glines(struct Client *);
102static void stats_hubleaf(struct Client *);
103static void stats_auth(struct Client *);
104static void stats_tklines(struct Client *);
105static void stats_klines(struct Client *);
106static void stats_messages(struct Client *);
107static void stats_dnsbl(struct Client *);
108static void stats_oper(struct Client *);
109static void stats_operedup(struct Client *);
110static void stats_ports(struct Client *);
111static void stats_tresv(struct Client *);
112static void stats_resv(struct Client *);
113static void stats_usage(struct Client *);
114static void stats_tstats(struct Client *);
115static void stats_uptime(struct Client *);
116static void stats_shared(struct Client *);
117static void stats_servers(struct Client *);
118static void stats_tgecos(struct Client *);
119static void stats_gecos(struct Client *);
120static void stats_class(struct Client *);
121static void stats_memory(struct Client *);
122static void stats_servlinks(struct Client *);
123static void stats_ltrace(struct Client *, int, const char **);
124static void stats_ziplinks(struct Client *);
125
126/* This table contains the possible stats items, in order:
127 * stats letter, function to call, operonly? adminonly?
128 * case only matters in the stats letter column.. -- fl_
129 */
130static struct StatsStruct stats_cmd_table[] = {
131 /* letter function need_oper need_admin */
132 {'a', stats_dns_servers, 1, 1, },
133 {'A', stats_dns_servers, 1, 1, },
134 {'b', stats_delay, 1, 1, },
135 {'B', stats_hash, 1, 1, },
136 {'c', stats_connect, 0, 0, },
137 {'C', stats_connect, 0, 0, },
138 {'d', stats_tdeny, 1, 0, },
139 {'D', stats_deny, 1, 0, },
140 {'e', stats_exempt, 1, 0, },
141 {'E', stats_events, 1, 1, },
142 {'f', comm_dump, 1, 1, },
143 {'F', comm_dump, 1, 1, },
144 {'g', stats_pending_glines, 1, 0, },
145 {'G', stats_glines, 1, 0, },
146 {'h', stats_hubleaf, 0, 0, },
147 {'H', stats_hubleaf, 0, 0, },
148 {'i', stats_auth, 0, 0, },
149 {'I', stats_auth, 0, 0, },
150 {'k', stats_tklines, 0, 0, },
151 {'K', stats_klines, 0, 0, },
152 {'l', stats_ltrace, 0, 0, },
153 {'L', stats_ltrace, 0, 0, },
154 {'m', stats_messages, 0, 0, },
155 {'M', stats_messages, 0, 0, },
156 {'n', stats_dnsbl, 0, 0, },
157 {'o', stats_oper, 0, 0, },
158 {'O', stats_oper, 0, 0, },
159 {'p', stats_operedup, 0, 0, },
160 {'P', stats_ports, 0, 0, },
161 {'q', stats_tresv, 1, 0, },
162 {'Q', stats_resv, 1, 0, },
163 {'r', stats_usage, 1, 0, },
164 {'R', stats_usage, 1, 0, },
165 {'t', stats_tstats, 1, 0, },
166 {'T', stats_tstats, 1, 0, },
167 {'u', stats_uptime, 0, 0, },
168 {'U', stats_shared, 1, 0, },
169 {'v', stats_servers, 0, 0, },
170 {'V', stats_servers, 0, 0, },
171 {'x', stats_tgecos, 1, 0, },
172 {'X', stats_gecos, 1, 0, },
173 {'y', stats_class, 0, 0, },
174 {'Y', stats_class, 0, 0, },
175 {'z', stats_memory, 1, 0, },
176 {'Z', stats_ziplinks, 1, 0, },
177 {'?', stats_servlinks, 0, 0, },
178 {(char) 0, (void (*)()) 0, 0, 0, }
179};
180
181/*
182 * m_stats by fl_
183 * parv[0] = sender prefix
184 * parv[1] = stat letter/command
185 * parv[2] = (if present) server/mask in stats L, or target
186 *
187 * This will search the tables for the appropriate stats letter,
188 * if found execute it.
189 */
190static int
191m_stats(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
192{
193 static time_t last_used = 0;
194 int i;
195 char statchar;
196
197 statchar = parv[1][0];
198
199 if(MyClient(source_p) && !IsOper(source_p))
200 {
201 /* Check the user is actually allowed to do /stats, and isnt flooding */
202 if((last_used + ConfigFileEntry.pace_wait) > CurrentTime)
203 {
204 /* safe enough to give this on a local connect only */
205 sendto_one(source_p, form_str(RPL_LOAD2HI),
206 me.name, source_p->name, "STATS");
207 sendto_one_numeric(source_p, RPL_ENDOFSTATS,
208 form_str(RPL_ENDOFSTATS), statchar);
209 return 0;
210 }
211 else
212 last_used = CurrentTime;
213 }
214
215 if(hunt_server (client_p, source_p, ":%s STATS %s :%s", 2, parc, parv) != HUNTED_ISME)
216 return 0;
217
218 if((statchar != 'L') && (statchar != 'l'))
219 stats_spy(source_p, statchar, NULL);
220
221 for (i = 0; stats_cmd_table[i].handler; i++)
222 {
223 if(stats_cmd_table[i].letter == statchar)
224 {
225 /* The stats table says what privs are needed, so check --fl_ */
226 /* Called for remote clients and for local opers, so check need_admin
227 * and need_oper
228 */
229 if((stats_cmd_table[i].need_admin && !IsOperAdmin (source_p)) ||
230 (stats_cmd_table[i].need_oper && !IsOper (source_p)))
231 {
232 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
233 form_str (ERR_NOPRIVILEGES));
234 break;
235 }
236
237 /* Blah, stats L needs the parameters, none of the others do.. */
238 if(statchar == 'L' || statchar == 'l')
239 stats_cmd_table[i].handler (source_p, parc, parv);
240 else
241 stats_cmd_table[i].handler (source_p);
242 }
243 }
244
245 /* Send the end of stats notice, and the stats_spy */
246 sendto_one_numeric(source_p, RPL_ENDOFSTATS,
247 form_str(RPL_ENDOFSTATS), statchar);
248
249 return 0;
250}
251
252static void
253stats_dns_servers (struct Client *source_p)
254{
255 report_dns_servers (source_p);
256}
257
258static void
259stats_delay(struct Client *source_p)
260{
261 struct nd_entry *nd;
b37021a4 262 struct DictionaryIter iter;
212380e3 263
b37021a4 264 DICTIONARY_FOREACH(nd, &iter, nd_dict)
212380e3 265 {
212380e3 266 sendto_one_notice(source_p, "Delaying: %s for %ld",
267 nd->name, (long) nd->expire);
268 }
212380e3 269}
270
271static void
272stats_hash(struct Client *source_p)
273{
274 hash_stats(source_p);
275}
276
277static void
278stats_connect(struct Client *source_p)
279{
280 static char buf[5];
281 struct server_conf *server_p;
282 char *s;
283 dlink_node *ptr;
284
285 if((ConfigFileEntry.stats_c_oper_only ||
286 (ConfigServerHide.flatten_links && !IsExemptShide(source_p))) &&
287 !IsOper(source_p))
288 {
289 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
290 form_str(ERR_NOPRIVILEGES));
291 return;
292 }
293
294 DLINK_FOREACH(ptr, server_conf_list.head)
295 {
296 server_p = ptr->data;
297
298 if(ServerConfIllegal(server_p))
299 continue;
300
301 buf[0] = '\0';
302 s = buf;
303
304 if(IsOper(source_p))
305 {
306 if(ServerConfAutoconn(server_p))
307 *s++ = 'A';
308 if(ServerConfTb(server_p))
309 *s++ = 'T';
310 if(ServerConfCompressed(server_p))
311 *s++ = 'Z';
312 }
313
314 if(!buf[0])
315 *s++ = '*';
316
317 *s = '\0';
318
319 sendto_one_numeric(source_p, RPL_STATSCLINE,
320 form_str(RPL_STATSCLINE),
321#ifndef HIDE_SERVERS_IPS
322 server_p->host,
323#else
324 "*@127.0.0.1",
325#endif
326 buf, server_p->name,
327 server_p->port, server_p->class_name);
328 }
329}
330
331/* stats_tdeny()
332 *
333 * input - client to report to
334 * output - none
335 * side effects - client is given temp dline list.
336 */
337static void
338stats_tdeny (struct Client *source_p)
339{
340 char *host, *pass, *user, *oper_reason;
341 struct AddressRec *arec;
342 struct ConfItem *aconf;
343 int i;
344
345 for (i = 0; i < ATABLE_SIZE; i++)
346 {
347 for (arec = atable[i]; arec; arec = arec->next)
348 {
349 if(arec->type == CONF_DLINE)
350 {
351 aconf = arec->aconf;
352
353 if(!(aconf->flags & CONF_FLAGS_TEMPORARY))
354 continue;
355
356 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
357
358 sendto_one_numeric(source_p, RPL_STATSDLINE,
359 form_str (RPL_STATSDLINE),
360 'd', host, pass,
361 oper_reason ? "|" : "",
362 oper_reason ? oper_reason : "");
363 }
364 }
365 }
366}
367
368/* stats_deny()
369 *
370 * input - client to report to
371 * output - none
372 * side effects - client is given dline list.
373 */
374static void
375stats_deny (struct Client *source_p)
376{
377 char *host, *pass, *user, *oper_reason;
378 struct AddressRec *arec;
379 struct ConfItem *aconf;
380 int i;
381
382 for (i = 0; i < ATABLE_SIZE; i++)
383 {
384 for (arec = atable[i]; arec; arec = arec->next)
385 {
386 if(arec->type == CONF_DLINE)
387 {
388 aconf = arec->aconf;
389
390 if(aconf->flags & CONF_FLAGS_TEMPORARY)
391 continue;
392
393 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
394
395 sendto_one_numeric(source_p, RPL_STATSDLINE,
396 form_str (RPL_STATSDLINE),
397 'D', host, pass,
398 oper_reason ? "|" : "",
399 oper_reason ? oper_reason : "");
400 }
401 }
402 }
403}
404
405
406/* stats_exempt()
407 *
408 * input - client to report to
409 * output - none
410 * side effects - client is given list of exempt blocks
411 */
412static void
413stats_exempt(struct Client *source_p)
414{
415 char *name, *host, *pass, *user, *classname;
416 struct AddressRec *arec;
417 struct ConfItem *aconf;
418 int i, port;
419
420 if(ConfigFileEntry.stats_e_disabled)
421 {
422 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
423 form_str (ERR_NOPRIVILEGES));
424 return;
425 }
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_EXEMPTDLINE)
432 {
433 aconf = arec->aconf;
434 get_printable_conf (aconf, &name, &host, &pass,
435 &user, &port, &classname);
436
437 sendto_one_numeric(source_p, RPL_STATSDLINE,
438 form_str(RPL_STATSDLINE),
439 'e', host, pass, "", "");
440 }
441 }
442 }}
443
444
445static void
446stats_events (struct Client *source_p)
447{
448 show_events (source_p);
449}
450
451/* stats_pending_glines()
452 *
453 * input - client pointer
454 * output - none
455 * side effects - client is shown list of pending glines
456 */
457static void
458stats_pending_glines (struct Client *source_p)
459{
460 if(ConfigFileEntry.glines)
461 {
462 dlink_node *pending_node;
463 struct gline_pending *glp_ptr;
464 char timebuffer[MAX_DATE_STRING];
465 struct tm *tmptr;
466
467 DLINK_FOREACH (pending_node, pending_glines.head)
468 {
469 glp_ptr = pending_node->data;
470
471 tmptr = localtime (&glp_ptr->time_request1);
472 strftime (timebuffer, MAX_DATE_STRING, "%Y/%m/%d %H:%M:%S", tmptr);
473
474 sendto_one_notice(source_p,
475 ":1) %s!%s@%s on %s requested gline at %s for %s@%s [%s]",
476 glp_ptr->oper_nick1,
477 glp_ptr->oper_user1, glp_ptr->oper_host1,
478 glp_ptr->oper_server1, timebuffer,
479 glp_ptr->user, glp_ptr->host, glp_ptr->reason1);
480
481 if(glp_ptr->oper_nick2[0])
482 {
483 tmptr = localtime (&glp_ptr->time_request2);
484 strftime (timebuffer, MAX_DATE_STRING, "%Y/%m/%d %H:%M:%S", tmptr);
485 sendto_one_notice(source_p,
486 ":2) %s!%s@%s on %s requested gline at %s for %s@%s [%s]",
487 glp_ptr->oper_nick2,
488 glp_ptr->oper_user2, glp_ptr->oper_host2,
489 glp_ptr->oper_server2, timebuffer,
490 glp_ptr->user, glp_ptr->host, glp_ptr->reason2);
491 }
492 }
493
494 if(dlink_list_length (&pending_glines) > 0)
495 sendto_one_notice(source_p, ":End of Pending G-lines");
496 }
497 else
498 sendto_one_notice(source_p, ":This server does not support G-Lines");
499
500}
501
502/* stats_glines()
503 *
504 * input - client pointer
505 * output - none
506 * side effects - client is shown list of glines
507 */
508static void
509stats_glines (struct Client *source_p)
510{
511 if(ConfigFileEntry.glines)
512 {
513 dlink_node *gline_node;
514 struct ConfItem *kill_ptr;
515
516 DLINK_FOREACH_PREV (gline_node, glines.tail)
517 {
518 kill_ptr = gline_node->data;
519
520 sendto_one_numeric(source_p, RPL_STATSKLINE,
521 form_str(RPL_STATSKLINE), 'G',
522 kill_ptr->host ? kill_ptr->host : "*",
523 kill_ptr->user ? kill_ptr->user : "*",
524 kill_ptr->passwd ? kill_ptr->passwd : "No Reason",
525 kill_ptr->spasswd ? "|" : "",
526 kill_ptr->spasswd ? kill_ptr->spasswd : "");
527 }
528 }
529 else
530 sendto_one_notice(source_p, ":This server does not support G-Lines");
531}
532
533
534static void
535stats_hubleaf(struct Client *source_p)
536{
537 struct remote_conf *hub_p;
538 dlink_node *ptr;
539
540 if((ConfigFileEntry.stats_h_oper_only ||
541 (ConfigServerHide.flatten_links && !IsExemptShide(source_p))) &&
542 !IsOper(source_p))
543 {
544 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
545 form_str (ERR_NOPRIVILEGES));
546 return;
547 }
548
549 DLINK_FOREACH(ptr, hubleaf_conf_list.head)
550 {
551 hub_p = ptr->data;
552
553 if(hub_p->flags & CONF_HUB)
554 sendto_one_numeric(source_p, RPL_STATSHLINE,
555 form_str(RPL_STATSHLINE),
556 hub_p->host, hub_p->server);
557 else
558 sendto_one_numeric(source_p, RPL_STATSLLINE,
559 form_str(RPL_STATSLLINE),
560 hub_p->host, hub_p->server);
561 }
562}
563
564
565static void
566stats_auth (struct Client *source_p)
567{
568 /* Oper only, if unopered, return ERR_NOPRIVS */
569 if((ConfigFileEntry.stats_i_oper_only == 2) && !IsOper (source_p))
570 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
571 form_str (ERR_NOPRIVILEGES));
572
573 /* If unopered, Only return matching auth blocks */
574 else if((ConfigFileEntry.stats_i_oper_only == 1) && !IsOper (source_p))
575 {
576 struct ConfItem *aconf;
577 char *name, *host, *pass, *user, *classname;
578 int port;
579
580 if(MyConnect (source_p))
581 aconf = find_conf_by_address (source_p->host, source_p->sockhost, NULL,
582 (struct sockaddr *)&source_p->localClient->ip,
583 CONF_CLIENT,
584 source_p->localClient->ip.ss_family,
585 source_p->username);
586 else
587 aconf = find_conf_by_address (source_p->host, NULL, NULL, NULL, CONF_CLIENT,
588 0, source_p->username);
589
590 if(aconf == NULL)
591 return;
592
593 get_printable_conf (aconf, &name, &host, &pass, &user, &port, &classname);
594
595 sendto_one_numeric(source_p, RPL_STATSILINE, form_str(RPL_STATSILINE),
596 name, show_iline_prefix(source_p, aconf, user),
597 host, port, classname);
598 }
599
600 /* Theyre opered, or allowed to see all auth blocks */
601 else
602 report_auth (source_p);
603}
604
605
606static void
607stats_tklines(struct Client *source_p)
608{
609 /* Oper only, if unopered, return ERR_NOPRIVS */
610 if((ConfigFileEntry.stats_k_oper_only == 2) && !IsOper (source_p))
611 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
612 form_str (ERR_NOPRIVILEGES));
613
614 /* If unopered, Only return matching klines */
615 else if((ConfigFileEntry.stats_k_oper_only == 1) && !IsOper (source_p))
616 {
617 struct ConfItem *aconf;
618 char *host, *pass, *user, *oper_reason;
619
620 if(MyConnect (source_p))
621 aconf = find_conf_by_address (source_p->host, source_p->sockhost, NULL,
622 (struct sockaddr *)&source_p->localClient->ip,
623 CONF_KILL,
624 source_p->localClient->ip.ss_family,
625 source_p->username);
626 else
627 aconf = find_conf_by_address (source_p->host, NULL, NULL, NULL, CONF_KILL,
628 0, source_p->username);
629
630 if(aconf == NULL)
631 return;
632
633 /* dont report a permanent kline as a tkline */
634 if((aconf->flags & CONF_FLAGS_TEMPORARY) == 0)
635 return;
636
637 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
638
639 sendto_one_numeric(source_p, RPL_STATSKLINE,
8eee9ee1
JT
640 form_str(RPL_STATSKLINE), aconf->flags & CONF_FLAGS_TEMPORARY ? 'k' : 'K',
641 host, user, pass, oper_reason ? "|" : "",
212380e3 642 oper_reason ? oper_reason : "");
643 }
644 /* Theyre opered, or allowed to see all klines */
645 else
646 {
647 struct ConfItem *aconf;
648 dlink_node *ptr;
649 int i;
650 char *user, *host, *pass, *oper_reason;
651
652 for(i = 0; i < LAST_TEMP_TYPE; i++)
653 {
654 DLINK_FOREACH(ptr, temp_klines[i].head)
655 {
656 aconf = ptr->data;
657
658 get_printable_kline(source_p, aconf, &host, &pass,
659 &user, &oper_reason);
660
661 sendto_one_numeric(source_p, RPL_STATSKLINE,
662 form_str(RPL_STATSKLINE),
663 'k', host, user, pass,
664 oper_reason ? "|" : "",
665 oper_reason ? oper_reason : "");
666 }
667 }
668 }
669}
670
671static void
672stats_klines(struct Client *source_p)
673{
674 /* Oper only, if unopered, return ERR_NOPRIVS */
675 if((ConfigFileEntry.stats_k_oper_only == 2) && !IsOper (source_p))
676 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
677 form_str (ERR_NOPRIVILEGES));
678
679 /* If unopered, Only return matching klines */
680 else if((ConfigFileEntry.stats_k_oper_only == 1) && !IsOper (source_p))
681 {
682 struct ConfItem *aconf;
683 char *host, *pass, *user, *oper_reason;
684
685 /* search for a kline */
686 if(MyConnect (source_p))
687 aconf = find_conf_by_address (source_p->host, source_p->sockhost, NULL,
688 (struct sockaddr *)&source_p->localClient->ip,
689 CONF_KILL,
690 source_p->localClient->ip.ss_family,
691 source_p->username);
692 else
693 aconf = find_conf_by_address (source_p->host, NULL, NULL, NULL, CONF_KILL,
694 0, source_p->username);
695
696 if(aconf == NULL)
697 return;
698
212380e3 699 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
700
701 sendto_one_numeric(source_p, RPL_STATSKLINE, form_str(RPL_STATSKLINE),
8eee9ee1
JT
702 aconf->flags & CONF_FLAGS_TEMPORARY ? 'k' : 'K',
703 host, user, pass, oper_reason ? "|" : "",
212380e3 704 oper_reason ? oper_reason : "");
705 }
706 /* Theyre opered, or allowed to see all klines */
707 else
708 report_Klines (source_p);
709}
710
711static void
712stats_messages(struct Client *source_p)
713{
714 report_messages(source_p);
715}
716
717static void
718stats_dnsbl(struct Client *source_p)
719{
720 dlink_node *ptr;
721 struct Blacklist *blptr;
722
723 DLINK_FOREACH(ptr, blacklist_list.head)
724 {
725 blptr = ptr->data;
726
727 /* use RPL_STATSDEBUG for now -- jilles */
728 sendto_one_numeric(source_p, RPL_STATSDEBUG, "n :%d %s %s (%d)",
729 blptr->hits,
730 blptr->host,
731 blptr->status & CONF_ILLEGAL ? "disabled" : "active",
732 blptr->refcount);
733 }
734}
735
736static void
737stats_oper(struct Client *source_p)
738{
739 struct oper_conf *oper_p;
740 dlink_node *ptr;
741
742 if(!IsOper(source_p) && ConfigFileEntry.stats_o_oper_only)
743 {
744 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
745 form_str (ERR_NOPRIVILEGES));
746 return;
747 }
748
749 DLINK_FOREACH(ptr, oper_conf_list.head)
750 {
751 oper_p = ptr->data;
752
753 sendto_one_numeric(source_p, RPL_STATSOLINE,
754 form_str(RPL_STATSOLINE),
755 oper_p->username, oper_p->host, oper_p->name,
756 IsOper(source_p) ? get_oper_privs(oper_p->flags) : "0", "-1");
757 }
758}
759
760
761/* stats_operedup()
762 *
763 * input - client pointer
764 * output - none
765 * side effects - client is shown a list of active opers
766 */
767static void
768stats_operedup (struct Client *source_p)
769{
770 struct Client *target_p;
771 dlink_node *oper_ptr;
772 unsigned int count = 0;
773
774 DLINK_FOREACH (oper_ptr, oper_list.head)
775 {
776 target_p = oper_ptr->data;
777
778 if(IsOperInvis(target_p) && !IsOper(source_p))
779 continue;
780
781 if(target_p->user->away)
782 continue;
783
784 count++;
785
786 sendto_one_numeric(source_p, RPL_STATSDEBUG,
787 "p :%s (%s@%s)",
788 target_p->name, target_p->username,
789 target_p->host);
790 }
791
792 sendto_one_numeric(source_p, RPL_STATSDEBUG,
793 "p :%u staff members", count);
794
795 stats_p_spy (source_p);
796}
797
798static void
799stats_ports (struct Client *source_p)
800{
801 if(!IsOper (source_p) && ConfigFileEntry.stats_P_oper_only)
802 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
803 form_str (ERR_NOPRIVILEGES));
804 else
805 show_ports (source_p);
806}
807
808static void
809stats_tresv(struct Client *source_p)
810{
811 struct ConfItem *aconf;
812 dlink_node *ptr;
813 int i;
814
815 DLINK_FOREACH(ptr, resv_conf_list.head)
816 {
817 aconf = ptr->data;
818 if(aconf->hold)
819 sendto_one_numeric(source_p, RPL_STATSQLINE,
820 form_str(RPL_STATSQLINE),
821 'q', aconf->port, aconf->name, aconf->passwd);
822 }
823
824 HASH_WALK(i, R_MAX, ptr, resvTable)
825 {
826 aconf = ptr->data;
827 if(aconf->hold)
828 sendto_one_numeric(source_p, RPL_STATSQLINE,
829 form_str(RPL_STATSQLINE),
830 'q', aconf->port, aconf->name, aconf->passwd);
831 }
832 HASH_WALK_END
833}
834
835
836static void
837stats_resv(struct Client *source_p)
838{
839 struct ConfItem *aconf;
840 dlink_node *ptr;
841 int i;
842
843 DLINK_FOREACH(ptr, resv_conf_list.head)
844 {
845 aconf = ptr->data;
846 if(!aconf->hold)
847 sendto_one_numeric(source_p, RPL_STATSQLINE,
848 form_str(RPL_STATSQLINE),
849 'Q', aconf->port, aconf->name, aconf->passwd);
850 }
851
852 HASH_WALK(i, R_MAX, ptr, resvTable)
853 {
854 aconf = ptr->data;
855 if(!aconf->hold)
856 sendto_one_numeric(source_p, RPL_STATSQLINE,
857 form_str(RPL_STATSQLINE),
858 'Q', aconf->port, aconf->name, aconf->passwd);
859 }
860 HASH_WALK_END
861}
862
863static void
864stats_usage (struct Client *source_p)
865{
866 struct rusage rus;
867 time_t secs;
868 time_t rup;
869#ifdef hz
870# define hzz hz
871#else
872# ifdef HZ
873# define hzz HZ
874# else
875 int hzz = 1;
876# endif
877#endif
878
879 if(getrusage(RUSAGE_SELF, &rus) == -1)
880 {
881 sendto_one_notice(source_p, ":Getruseage error: %s.",
882 strerror(errno));
883 return;
884 }
885 secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec;
886 if(0 == secs)
887 secs = 1;
888
889 rup = (CurrentTime - startup_time) * hzz;
890 if(0 == rup)
891 rup = 1;
892
893 sendto_one_numeric(source_p, RPL_STATSDEBUG,
894 "R :CPU Secs %d:%d User %d:%d System %d:%d",
895 (int) (secs / 60), (int) (secs % 60),
896 (int) (rus.ru_utime.tv_sec / 60),
897 (int) (rus.ru_utime.tv_sec % 60),
898 (int) (rus.ru_stime.tv_sec / 60),
899 (int) (rus.ru_stime.tv_sec % 60));
900 sendto_one_numeric(source_p, RPL_STATSDEBUG,
901 "R :RSS %ld ShMem %ld Data %ld Stack %ld",
902 rus.ru_maxrss, (rus.ru_ixrss / rup),
903 (rus.ru_idrss / rup), (rus.ru_isrss / rup));
904 sendto_one_numeric(source_p, RPL_STATSDEBUG,
905 "R :Swaps %d Reclaims %d Faults %d",
906 (int) rus.ru_nswap, (int) rus.ru_minflt, (int) rus.ru_majflt);
907 sendto_one_numeric(source_p, RPL_STATSDEBUG,
908 "R :Block in %d out %d",
909 (int) rus.ru_inblock, (int) rus.ru_oublock);
910 sendto_one_numeric(source_p, RPL_STATSDEBUG,
911 "R :Msg Rcv %d Send %d",
912 (int) rus.ru_msgrcv, (int) rus.ru_msgsnd);
913 sendto_one_numeric(source_p, RPL_STATSDEBUG,
914 "R :Signals %d Context Vol. %d Invol %d",
915 (int) rus.ru_nsignals, (int) rus.ru_nvcsw,
916 (int) rus.ru_nivcsw);
917}
918
919static void
920stats_tstats (struct Client *source_p)
921{
922 tstats (source_p);
923}
924
925static void
926stats_uptime (struct Client *source_p)
927{
928 time_t now;
929
930 now = CurrentTime - startup_time;
931 sendto_one_numeric(source_p, RPL_STATSUPTIME,
932 form_str (RPL_STATSUPTIME),
933 now / 86400, (now / 3600) % 24,
934 (now / 60) % 60, now % 60);
935 sendto_one_numeric(source_p, RPL_STATSCONN,
936 form_str (RPL_STATSCONN),
937 MaxConnectionCount, MaxClientCount,
938 Count.totalrestartcount);
939}
940
941struct shared_flags
942{
943 int flag;
944 char letter;
945};
946static struct shared_flags shared_flagtable[] =
947{
948 { SHARED_PKLINE, 'K' },
949 { SHARED_TKLINE, 'k' },
950 { SHARED_UNKLINE, 'U' },
951 { SHARED_PXLINE, 'X' },
952 { SHARED_TXLINE, 'x' },
953 { SHARED_UNXLINE, 'Y' },
954 { SHARED_PRESV, 'Q' },
955 { SHARED_TRESV, 'q' },
956 { SHARED_UNRESV, 'R' },
957 { SHARED_LOCOPS, 'L' },
958 { SHARED_REHASH, 'H' },
959 { 0, '\0'}
960};
961
962
963static void
964stats_shared (struct Client *source_p)
965{
966 struct remote_conf *shared_p;
967 dlink_node *ptr;
968 char buf[15];
969 char *p;
970 int i;
971
972 DLINK_FOREACH(ptr, shared_conf_list.head)
973 {
974 shared_p = ptr->data;
975
976 p = buf;
977
978 *p++ = 'c';
979
980 for(i = 0; shared_flagtable[i].flag != 0; i++)
981 {
982 if(shared_p->flags & shared_flagtable[i].flag)
983 *p++ = shared_flagtable[i].letter;
984 }
985
986 *p = '\0';
987
988 sendto_one_numeric(source_p, RPL_STATSULINE,
989 form_str(RPL_STATSULINE),
990 shared_p->server, shared_p->username,
991 shared_p->host, buf);
992 }
993
994 DLINK_FOREACH(ptr, cluster_conf_list.head)
995 {
996 shared_p = ptr->data;
997
998 p = buf;
999
1000 *p++ = 'C';
1001
1002 for(i = 0; shared_flagtable[i].flag != 0; i++)
1003 {
1004 if(shared_p->flags & shared_flagtable[i].flag)
1005 *p++ = shared_flagtable[i].letter;
1006 }
1007
1008 *p = '\0';
1009
1010 sendto_one_numeric(source_p, RPL_STATSULINE,
1011 form_str(RPL_STATSULINE),
1012 shared_p->server, "*", "*", buf);
1013 }
1014}
1015
1016/* stats_servers()
1017 *
1018 * input - client pointer
1019 * output - none
1020 * side effects - client is shown lists of who connected servers
1021 */
1022static void
1023stats_servers (struct Client *source_p)
1024{
1025 struct Client *target_p;
1026 dlink_node *ptr;
1027 time_t seconds;
1028 int days, hours, minutes;
1029 int j = 0;
1030
1031 if(ConfigServerHide.flatten_links && !IsOper(source_p) &&
1032 !IsExemptShide(source_p))
1033 {
1034 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
1035 form_str (ERR_NOPRIVILEGES));
1036 return;
1037 }
1038
1039 DLINK_FOREACH (ptr, serv_list.head)
1040 {
1041 target_p = ptr->data;
1042
1043 j++;
1044 seconds = CurrentTime - target_p->localClient->firsttime;
1045
1046 days = (int) (seconds / 86400);
1047 seconds %= 86400;
1048 hours = (int) (seconds / 3600);
1049 seconds %= 3600;
1050 minutes = (int) (seconds / 60);
1051 seconds %= 60;
1052
1053 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1054 "V :%s (%s!*@*) Idle: %d SendQ: %d "
1055 "Connected: %d day%s, %d:%02d:%02d",
1056 target_p->name,
1057 (target_p->serv->by[0] ? target_p->serv->by : "Remote."),
1058 (int) (CurrentTime - target_p->localClient->lasttime),
1059 (int) linebuf_len (&target_p->localClient->buf_sendq),
1060 days, (days == 1) ? "" : "s", hours, minutes,
1061 (int) seconds);
1062 }
1063
1064 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1065 "V :%d Server(s)", j);
1066}
1067
1068static void
1069stats_tgecos(struct Client *source_p)
1070{
1071 struct ConfItem *aconf;
1072 dlink_node *ptr;
1073
1074 DLINK_FOREACH(ptr, xline_conf_list.head)
1075 {
1076 aconf = ptr->data;
1077
1078 if(aconf->hold)
1079 sendto_one_numeric(source_p, RPL_STATSXLINE,
1080 form_str(RPL_STATSXLINE),
1081 'x', aconf->port, aconf->name,
1082 aconf->passwd);
1083 }
1084}
1085
1086static void
1087stats_gecos(struct Client *source_p)
1088{
1089 struct ConfItem *aconf;
1090 dlink_node *ptr;
1091
1092 DLINK_FOREACH(ptr, xline_conf_list.head)
1093 {
1094 aconf = ptr->data;
1095
1096 if(!aconf->hold)
1097 sendto_one_numeric(source_p, RPL_STATSXLINE,
1098 form_str(RPL_STATSXLINE),
1099 'X', aconf->port, aconf->name,
1100 aconf->passwd);
1101 }
1102}
1103
1104static void
1105stats_class(struct Client *source_p)
1106{
1107 if(ConfigFileEntry.stats_y_oper_only && !IsOper(source_p))
1108 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
1109 form_str (ERR_NOPRIVILEGES));
1110 else
1111 report_classes(source_p);
1112}
1113
1114static void
1115stats_memory (struct Client *source_p)
1116{
1117 count_memory (source_p);
1118}
1119
1120static void
1121stats_ziplinks (struct Client *source_p)
1122{
1123 dlink_node *ptr;
1124 struct Client *target_p;
1125 int sent_data = 0;
1126
1127 DLINK_FOREACH (ptr, serv_list.head)
1128 {
1129 target_p = ptr->data;
1130 if(IsCapable (target_p, CAP_ZIP))
1131 {
1132 /* we use memcpy(3) and a local copy of the structure to
1133 * work around a register use bug on GCC on the SPARC.
1134 * -jmallett, 04/27/2002
1135 */
1136 struct ZipStats zipstats;
1137 memcpy (&zipstats, &target_p->localClient->zipstats,
1138 sizeof (struct ZipStats));
1139 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1140 "Z :ZipLinks stats for %s send[%.2f%% compression "
1141 "(%lu kB data/%lu kB wire)] recv[%.2f%% compression "
1142 "(%lu kB data/%lu kB wire)]",
1143 target_p->name,
1144 zipstats.out_ratio, zipstats.outK, zipstats.outK_wire,
1145 zipstats.in_ratio, zipstats.inK, zipstats.inK_wire);
1146 sent_data++;
1147 }
1148 }
1149
1150 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1151 "Z :%u ziplink(s)", sent_data);
1152}
1153
1154static void
1155stats_servlinks (struct Client *source_p)
1156{
1157 static char Sformat[] = ":%s %d %s %s %u %u %u %u %u :%u %u %s";
1158 long uptime, sendK, receiveK;
1159 struct Client *target_p;
1160 dlink_node *ptr;
1161 int j = 0;
1162
1163 if(ConfigServerHide.flatten_links && !IsOper (source_p) &&
1164 !IsExemptShide(source_p))
1165 {
1166 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
1167 form_str (ERR_NOPRIVILEGES));
1168 return;
1169 }
1170
1171 sendK = receiveK = 0;
1172
1173 DLINK_FOREACH (ptr, serv_list.head)
1174 {
1175 target_p = ptr->data;
1176
1177 j++;
1178 sendK += target_p->localClient->sendK;
1179 receiveK += target_p->localClient->receiveK;
1180
1181 sendto_one(source_p, Sformat,
1182 get_id(&me, source_p), RPL_STATSLINKINFO, get_id(source_p, source_p),
1183 get_server_name(target_p, SHOW_IP),
1184 (int) linebuf_len (&target_p->localClient->buf_sendq),
1185 (int) target_p->localClient->sendM,
1186 (int) target_p->localClient->sendK,
1187 (int) target_p->localClient->receiveM,
1188 (int) target_p->localClient->receiveK,
1189 CurrentTime - target_p->localClient->firsttime,
1190 (CurrentTime > target_p->localClient->lasttime) ?
1191 (CurrentTime - target_p->localClient->lasttime) : 0,
1192 IsOper (source_p) ? show_capabilities (target_p) : "TS");
1193 }
1194
1195 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1196 "? :%u total server(s)", j);
1197
1198 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1199 "? :Sent total : %7.2f %s",
1200 _GMKv (sendK), _GMKs (sendK));
1201 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1202 "? :Recv total : %7.2f %s",
1203 _GMKv (receiveK), _GMKs (receiveK));
1204
1205 uptime = (CurrentTime - startup_time);
1206
1207 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1208 "? :Server send: %7.2f %s (%4.1f K/s)",
1209 _GMKv (me.localClient->sendK),
1210 _GMKs (me.localClient->sendK),
1211 (float) ((float) me.localClient->sendK / (float) uptime));
1212 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1213 "? :Server recv: %7.2f %s (%4.1f K/s)",
1214 _GMKv (me.localClient->receiveK),
1215 _GMKs (me.localClient->receiveK),
1216 (float) ((float) me.localClient->receiveK / (float) uptime));
1217}
1218
1219static void
1220stats_ltrace(struct Client *source_p, int parc, const char *parv[])
1221{
1222 int doall = 0;
1223 int wilds = 0;
1224 const char *name;
1225 char statchar = parv[1][0];
1226
1227 /* this is def targeted at us somehow.. */
1228 if(parc > 2 && !EmptyString(parv[2]))
1229 {
1230 /* directed at us generically? */
1231 if(match(parv[2], me.name) ||
1232 (!MyClient(source_p) && !irccmp(parv[2], me.id)))
1233 {
1234 name = me.name;
1235 doall = 1;
1236 }
1237 else
1238 {
1239 name = parv[2];
1240 wilds = strchr(name, '*') || strchr(name, '?');
1241 }
1242
1243 /* must be directed at a specific person thats not us */
1244 if(!doall && !wilds)
1245 {
1246 struct Client *target_p;
1247
1248 if(MyClient(source_p))
1249 target_p = find_named_person(name);
1250 else
1251 target_p = find_person(name);
1252
1253 if(target_p != NULL)
1254 {
1255 stats_spy(source_p, statchar, target_p->name);
1256 stats_l_client(source_p, target_p, statchar);
1257 }
1258 else
1259 sendto_one_numeric(source_p, ERR_NOSUCHSERVER,
1260 form_str(ERR_NOSUCHSERVER),
1261 name);
1262
1263 return;
1264 }
1265 }
1266 else
1267 {
1268 name = me.name;
1269 doall = 1;
1270 }
1271
1272 stats_spy(source_p, statchar, name);
1273
1274 if(doall)
1275 {
1276 /* local opers get everyone */
1277 if(MyOper(source_p))
1278 {
1279 stats_l_list(source_p, name, doall, wilds, &unknown_list, statchar);
1280 stats_l_list(source_p, name, doall, wilds, &lclient_list, statchar);
1281 }
1282 else
1283 {
1284 /* they still need themselves if theyre local.. */
1285 if(MyClient(source_p))
1286 stats_l_client(source_p, source_p, statchar);
1287
1288 stats_l_list(source_p, name, doall, wilds, &local_oper_list, statchar);
1289 }
1290
1291 if (!ConfigServerHide.flatten_links || IsOper(source_p) ||
1292 IsExemptShide(source_p))
1293 stats_l_list(source_p, name, doall, wilds, &serv_list, statchar);
1294
1295 return;
1296 }
1297
1298 /* ok, at this point theyre looking for a specific client whos on
1299 * our server.. but it contains a wildcard. --fl
1300 */
1301 stats_l_list(source_p, name, doall, wilds, &lclient_list, statchar);
1302
1303 return;
1304}
1305
1306
1307static void
1308stats_l_list(struct Client *source_p, const char *name, int doall, int wilds,
1309 dlink_list * list, char statchar)
1310{
1311 dlink_node *ptr;
1312 struct Client *target_p;
1313
1314 /* send information about connections which match. note, we
1315 * dont need tests for IsInvisible(), because non-opers will
1316 * never get here for normal clients --fl
1317 */
1318 DLINK_FOREACH(ptr, list->head)
1319 {
1320 target_p = ptr->data;
1321
1322 if(!doall && wilds && !match(name, target_p->name))
1323 continue;
1324
1325 stats_l_client(source_p, target_p, statchar);
1326 }
1327}
1328
1329void
1330stats_l_client(struct Client *source_p, struct Client *target_p,
1331 char statchar)
1332{
1333 if(IsAnyServer(target_p))
1334 {
1335 sendto_one_numeric(source_p, RPL_STATSLINKINFO, Lformat,
1336 get_server_name(target_p, SHOW_IP),
1337 (int) linebuf_len(&target_p->localClient->buf_sendq),
1338 (int) target_p->localClient->sendM,
1339 (int) target_p->localClient->sendK,
1340 (int) target_p->localClient->receiveM,
1341 (int) target_p->localClient->receiveK,
1342 CurrentTime - target_p->localClient->firsttime,
1343 (CurrentTime > target_p->localClient->lasttime) ?
1344 (CurrentTime - target_p->localClient->lasttime) : 0,
1345 IsOper(source_p) ? show_capabilities(target_p) : "-");
1346 }
1347
1348 else
1349 {
1350 sendto_one_numeric(source_p, RPL_STATSLINKINFO, Lformat,
1351 show_ip(source_p, target_p) ?
1352 (IsUpper(statchar) ?
1353 get_client_name(target_p, SHOW_IP) :
1354 get_client_name(target_p, HIDE_IP)) :
1355 get_client_name(target_p, MASK_IP),
1356 (int) linebuf_len(&target_p->localClient->buf_sendq),
1357 (int) target_p->localClient->sendM,
1358 (int) target_p->localClient->sendK,
1359 (int) target_p->localClient->receiveM,
1360 (int) target_p->localClient->receiveK,
1361 CurrentTime - target_p->localClient->firsttime,
1362 (CurrentTime > target_p->localClient->lasttime) ?
1363 (CurrentTime - target_p->localClient->lasttime) : 0,
1364 "-");
1365 }
1366}
1367
1368/*
1369 * stats_spy
1370 *
1371 * inputs - pointer to client doing the /stats
1372 * - char letter they are doing /stats on
1373 * output - none
1374 * side effects -
1375 * This little helper function reports to opers if configured.
1376 * personally, I don't see why opers need to see stats requests
1377 * at all. They are just "noise" to an oper, and users can't do
1378 * any damage with stats requests now anyway. So, why show them?
1379 * -Dianora
1380 */
1381static void
1382stats_spy(struct Client *source_p, char statchar, const char *name)
1383{
1384 hook_data_int data;
1385
1386 data.client = source_p;
1387 data.arg1 = name;
1388 data.arg2 = (int) statchar;
1389
1390 call_hook(doing_stats_hook, &data);
1391}
1392
1393/* stats_p_spy()
1394 *
1395 * input - pointer to client doing stats
1396 * ouput -
1397 * side effects - call hook doing_stats_p
1398 */
1399static void
1400stats_p_spy (struct Client *source_p)
1401{
1402 hook_data data;
1403
1404 data.client = source_p;
1405 data.arg1 = data.arg2 = NULL;
1406
1407 call_hook(doing_stats_p_hook, &data);
1408}
1409