2 * IRC - Internet Relay Chat, ircd/m_stats.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * See file AUTHORS in IRC package for additional names of
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 1, or (at your option)
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * m_functions execute protocol messages on this server:
29 * cptr is always NON-NULL, pointing to a *LOCAL* client
30 * structure (with an open socket connected!). This
31 * identifies the physical socket where the message
32 * originated (or which caused the m_function to be
33 * executed--some m_functions may call others...).
35 * sptr is the source of the message, defined by the
36 * prefix part of the message if present. If not
37 * or prefix not found, then sptr==cptr.
39 * (!IsServer(cptr)) => (cptr == sptr), because
40 * prefixes are taken *only* from servers...
43 * (sptr == cptr) => the message didn't
46 * (sptr != cptr && IsServer(sptr) means
47 * the prefix specified servername. (?)
49 * (sptr != cptr && !IsServer(sptr) means
50 * that message originated from a remote
55 * (!IsServer(sptr)) means that, sptr can safely
56 * taken as defining the target structure of the
57 * message in this server.
59 * *Always* true (if 'parse' and others are working correct):
61 * 1) sptr->from == cptr (note: cptr->from == cptr)
63 * 2) MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64 * *cannot* be a local connection, unless it's
65 * actually cptr!). [MyConnect(x) should probably
66 * be defined as (x == x->from) --msa ]
68 * parc number of variable parameter strings (if zero,
69 * parv is allowed to be NULL)
71 * parv a NULL terminated list of parameter pointers,
73 * parv[0], sender (prefix string), if not present
74 * this points to an empty string.
75 * parv[1]...parv[parc-1]
76 * pointers to additional parameters
77 * parv[parc] == NULL, *always*
79 * note: it is guaranteed that parv[0]..parv[parc-1] are all
95 #include "ircd_alloc.h"
96 #include "ircd_chattr.h"
97 #include "ircd_events.h"
98 #include "ircd_features.h"
99 #include "ircd_policy.h"
100 #include "ircd_reply.h"
101 #include "ircd_string.h"
103 #include "listener.h"
108 #include "numnicks.h"
109 #include "opercmds.h"
118 #include "userload.h"
125 int report_klines(struct Client* sptr, char* mask, int limit_query)
131 const struct DenyConf* conf;
133 if (EmptyString(mask)) {
135 return need_more_params(sptr, "STATS K");
137 report_deny_list(sptr);
142 wilds = string_has_wildcards(mask);
146 if ((host = strchr(mask, '@'))) {
154 for (conf = conf_get_deny_list(); conf; conf = conf->next) {
155 if ((!wilds && ((user || conf->hostmask) &&
156 !match(conf->hostmask, host) &&
157 (!user || !match(conf->usermask, user)))) ||
158 (wilds && !mmatch(host, conf->hostmask) &&
159 (!user || !mmatch(user, conf->usermask))))
161 send_reply(sptr, RPL_STATSKLINE,
162 (conf->flags & DENY_FLAGS_IP) ? 'k' : 'K',
163 conf->hostmask, conf->message, conf->usermask);
168 /* send_reply(sptr, RPL_ENDOFSTATS, stat); */
174 * m_stats - generic message handler
176 * parv[0] = sender prefix
177 * parv[1] = statistics selector (defaults to Message frequency)
178 * parv[2] = target server (current server defaulted, if omitted)
179 * And 'stats l' and 'stats' L:
180 * parv[3] = server mask ("*" defaulted, if omitted)
182 * parv[3] = port mask (returns p-lines when its port is matched by this)
183 * Or for stats k,K,i and I:
184 * parv[3] = [user@]host.name (returns which K/I-lines match this)
185 * or [user@]host.mask (returns which K/I-lines are mmatched by this)
186 * (defaults to old reply if ommitted, when local or Oper)
187 * A remote mask (something containing wildcards) is only
188 * allowed for IRC Operators.
190 * parv[3] = time param
191 * parv[4] = time param
192 * (see report_memleak_stats() in runmalloc.c for details)
194 * This function is getting really ugly. -Ghostwolf
196 int m_stats(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
198 char stat = parc > 1 ? parv[1][0] : '\0';
199 const char **infotext = statsinfo;
201 if (hunt_stats(cptr, sptr, parc, parv, stat, HEAD_IN_SAND_REMOTE)
209 #ifdef HEAD_IN_SAND_STATS_L
210 return m_not_oper(sptr,cptr,parc,parv);
213 struct Client *acptr;
219 if (parc > 3 && !EmptyString(parv[3])) {
221 wilds = string_has_wildcards(name);
226 * Send info about connections which match, or all if the
227 * mask matches me.name. Only restrictions are on those who
228 * are invisible not being visible to 'foreigners' who use
229 * a wild card based search to list it.
231 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
232 "SendM SendKBytes RcveM RcveKBytes :Open since");
233 for (i = 0; i <= HighestFd; i++)
235 if (!(acptr = LocalClientArray[i]))
237 /* Don't return clients when this is a request for `all' */
238 if (doall && IsUser(acptr))
240 /* Don't show invisible people to non opers unless they know the nick */
241 if (IsInvisible(acptr) && (doall || wilds) && !IsAnOper(acptr) && (acptr != sptr))
243 /* Only show the ones that match the given mask - if any */
244 if (!doall && wilds && match(name, cli_name(acptr)))
246 /* Skip all that do not match the specific query */
247 if (!(doall || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
249 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
250 "%s %u %u %u %u %u :%Tu", (*(cli_name(acptr))) ? cli_name(acptr) : "<unregistered>",
251 (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
252 (int)cli_sendK(acptr), (int)cli_receiveM(acptr),
253 (int)cli_receiveK(acptr), CurrentTime - cli_firsttime(acptr));
261 #ifdef HEAD_IN_SAND_STATS_C
262 return m_not_oper(sptr,cptr,parc,parv);
264 report_configured_links(sptr, CONF_SERVER);
269 case 'e': /* report engine name */
270 #ifdef HEAD_IN_SAND_STATS_E
271 return m_not_oper(sptr,cptr,parc,parv);
273 send_reply(sptr, RPL_STATSENGINE, engine_name());
278 case 'g': /* send glines */
279 #ifdef HEAD_IN_SAND_STATS_G
280 return m_not_oper(sptr,cptr,parc,parv);
288 #ifdef HEAD_IN_SAND_STATS_H
289 return m_not_oper(sptr,cptr,parc,parv);
291 report_configured_links(sptr, CONF_HUB | CONF_LEAF);
296 case 'k': /* display CONF_IPKILL as well as CONF_KILL -Kev */
297 #ifdef HEAD_IN_SAND_STATS_K
298 /* Simple version - if you want to fix it - send in a patch */
299 return m_not_oper(sptr,cptr,parc,parv);
301 if (0 == report_klines(sptr, (parc == 4) ? parv[3] : 0, 0))
307 #ifdef HEAD_IN_SAND_STATS_F
308 return m_not_oper(sptr,cptr,parc,parv);
310 feature_report(sptr);
316 #ifdef HEAD_IN_SAND_STATS_I
317 /* Simple version - if you want to fix it - send in a patch */
318 return m_not_oper(sptr,cptr,parc,parv);
321 struct ConfItem *aconf;
327 report_configured_links(sptr, CONF_CLIENT);
330 if (EmptyString(parv[3]))
331 return need_more_params(sptr, "STATS I");
334 wilds = string_has_wildcards(host);
336 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
337 if (CONF_CLIENT == aconf->status) {
338 if ((!wilds && (!match(aconf->host, host) ||
339 !match(aconf->name, host))) ||
340 (wilds && (!mmatch(host, aconf->host) ||
341 !mmatch(host, aconf->name))))
343 if (acptr->passwd && IsDigit(*aconf->passwd) &&
344 (!aconf->passwd[1] ||
345 (IsDigit(aconf->passwd[1]) && !aconf->passwd[2])
346 send_reply(sptr, RPL_STATSILINE, 'I', aconf->passwd, aconf->host,
347 aconf->name, aconf->port, get_conf_class(aconf));
350 send_reply(sptr, RPL_STATSILINE, 'I', "*", aconf->host,
351 aconf->name, aconf->port, get_conf_class(aconf));
362 #ifdef HEAD_IN_SAND_STATS_M
363 return m_not_oper(sptr,cptr,parc,parv);
366 send_reply(sptr, RPL_STATMEMTOT, fda_get_byte_count(),
367 fda_get_block_count());
373 #ifdef HEAD_IN_SAND_STATS_m
374 return m_not_oper(sptr,cptr,parc,parv);
377 struct Message *mptr;
379 for (mptr = msgtab; mptr->cmd; mptr++)
381 send_reply(sptr, RPL_STATSCOMMANDS, mptr->cmd, mptr->count,
389 #ifdef HEAD_IN_SAND_STATS_O
390 return m_not_oper(sptr,cptr,parc,parv);
392 report_configured_links(sptr, CONF_OPS);
399 * show listener ports
400 * show hidden ports to opers, if there are more than 3 parameters,
401 * interpret the fourth parameter as the port number.
403 #ifdef HEAD_IN_SAND_STATS_P
404 return m_not_oper(sptr,cptr,parc,parv);
406 show_ports(sptr, 0, (parc > 3) ? atoi(parv[3]) : 0, 100);
412 #ifdef HEAD_IN_SAND_STATS_R
413 return m_not_oper(sptr,cptr,parc,parv);
416 send_usage(sptr, parv[0]);
422 #ifdef HEAD_IN_SAND_STATS_D
423 return m_not_oper(sptr,cptr,parc,parv);
425 report_crule_list(sptr, CRULE_ALL);
430 #ifdef HEAD_IN_SAND_STATS_d
431 return m_not_oper(sptr,cptr,parc,parv);
433 report_crule_list(sptr, CRULE_MASK);
438 #ifdef HEAD_IN_SAND_STATS_t
439 return m_not_oper(sptr,cptr,parc,parv);
441 tstats(sptr, parv[0]);
446 #ifdef HEAD_IN_SAND_STATS_T
447 return m_not_oper(sptr,cptr,parc,parv);
454 #ifdef HEAD_IN_SAND_STATS_U
455 return m_not_oper(sptr,cptr,parc,parv);
457 report_configured_links(sptr, CONF_UWORLD);
462 #ifdef HEAD_IN_SAND_STATS_u
463 return m_not_oper(sptr,cptr,parc,parv);
468 nowr = CurrentTime - cli_since(&me);
469 send_reply(sptr, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
470 (nowr / 60) % 60, nowr % 60);
471 send_reply(sptr, RPL_STATSCONN, max_connection_count, max_client_count);
478 #ifdef HEAD_IN_SAND_STATS_W
479 return m_not_oper(sptr,cptr,parc,parv);
487 #ifdef HEAD_IN_SAND_STATS_X
488 return m_not_oper(sptr,cptr,parc,parv);
491 class_send_meminfo(sptr);
492 send_listinfo(sptr, parv[0]);
499 #ifdef HEAD_IN_SAND_STATS_Y
500 return m_not_oper(sptr,cptr,parc,parv);
502 report_classes(sptr);
508 #ifdef HEAD_IN_SAND_STATS_Z
509 return m_not_oper(sptr,cptr,parc,parv);
511 count_memory(sptr, parv[0]);
518 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :%s", sptr, *infotext++);
521 send_reply(sptr, RPL_ENDOFSTATS, stat);
526 * ms_stats - server message handler
528 * parv[0] = sender prefix
529 * parv[1] = statistics selector (defaults to Message frequency)
530 * parv[2] = target server (current server defaulted, if omitted)
531 * And 'stats l' and 'stats' L:
532 * parv[3] = server mask ("*" defaulted, if omitted)
534 * parv[3] = port mask (returns p-lines when its port is matched by this)
535 * Or for stats k,K,i and I:
536 * parv[3] = [user@]host.name (returns which K/I-lines match this)
537 * or [user@]host.mask (returns which K/I-lines are mmatched by this)
538 * (defaults to old reply if ommitted, when local or Oper)
539 * A remote mask (something containing wildcards) is only
540 * allowed for IRC Operators.
542 * parv[3] = time param
543 * parv[4] = time param
544 * (see report_memleak_stats() in runmalloc.c for details)
546 * This function is getting really ugly. -Ghostwolf
548 int ms_stats(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
550 struct Message *mptr;
551 struct Client *acptr;
552 struct ConfItem *aconf;
553 char stat = parc > 1 ? parv[1][0] : '\0';
556 if (hunt_stats(cptr, sptr, parc, parv, stat, 0) != HUNTED_ISME)
568 if (parc > 3 && !EmptyString(parv[3])) {
570 wilds = string_has_wildcards(name);
575 * Send info about connections which match, or all if the
576 * mask matches me.name. Only restrictions are on those who
577 * are invisible not being visible to 'foreigners' who use
578 * a wild card based search to list it.
580 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
581 "SendM SendKBytes RcveM RcveKBytes :Open since");
582 for (i = 0; i <= HighestFd; i++)
584 if (!(acptr = LocalClientArray[i]))
586 /* Don't return clients when this is a request for `all' */
587 if (doall && IsUser(acptr))
589 /* Don't show invisible people to unauthorized people when using
590 * wildcards -- Is this still needed now /stats is oper only ?
591 * Not here, because ms_stats is specifically a remote command,
592 * thus the check was removed. -Ghostwolf */
593 /* Only show the ones that match the given mask - if any */
594 if (!doall && wilds && match(name, cli_name(acptr)))
596 /* Skip all that do not match the specific query */
597 if (!(doall || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
599 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
600 "%s %u %u %u %u %u :%Tu", cli_name(acptr),
601 (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
602 (int)cli_sendK(acptr), (int)cli_receiveM(acptr),
603 (int)cli_receiveK(acptr), CurrentTime - cli_firsttime(acptr));
609 report_configured_links(sptr, CONF_SERVER);
612 case 'e': /* report engine name */
613 send_reply(sptr, RPL_STATSENGINE, engine_name());
616 case 'g': /* send glines */
621 report_configured_links(sptr, CONF_HUB | CONF_LEAF);
624 case 'k': /* display CONF_IPKILL as well as CONF_KILL -Kev */
625 if (0 == report_klines(sptr, (parc > 3) ? parv[3] : 0, !IsOper(sptr)))
630 feature_report(sptr);
639 if (parc < 4 && IsOper(sptr)) {
640 report_configured_links(sptr, CONF_CLIENT);
643 if (parc < 4 || EmptyString(parv[3]))
644 return need_more_params(sptr, "STATS I");
647 wilds = string_has_wildcards(parv[3]);
653 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
654 if (CONF_CLIENT == aconf->status) {
655 if ((!wilds && (!match(aconf->host, host) ||
656 !match(aconf->name, host))) ||
657 (wilds && (!mmatch(host, aconf->host) ||
658 !mmatch(host, aconf->name))))
660 send_reply(sptr, RPL_STATSILINE, 'I', aconf->host, aconf->name,
661 aconf->port, get_conf_class(aconf));
671 send_reply(sptr, RPL_STATMEMTOT, fda_get_byte_count(),
672 fda_get_block_count());
676 for (mptr = msgtab; mptr->cmd; mptr++)
678 send_reply(sptr, RPL_STATSCOMMANDS, mptr->cmd, mptr->count,
683 report_configured_links(sptr, CONF_OPS);
688 * show listener ports
689 * show hidden ports to opers, if there are more than 3 parameters,
690 * interpret the fourth parameter as the port number, limit non-local
691 * or non-oper results to 8 ports.
693 show_ports(sptr, IsOper(sptr), (parc > 3) ? atoi(parv[3]) : 0, IsOper(sptr) ? 100 : 8);
698 send_usage(sptr, parv[0]);
702 report_crule_list(sptr, CRULE_ALL);
705 report_crule_list(sptr, CRULE_MASK);
708 tstats(sptr, parv[0]);
714 report_configured_links(sptr, CONF_UWORLD);
720 nowr = CurrentTime - cli_since(&me);
721 send_reply(sptr, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
722 (nowr / 60) % 60, nowr % 60);
723 send_reply(sptr, RPL_STATSCONN, max_connection_count, max_client_count);
733 class_send_meminfo(sptr);
734 send_listinfo(sptr, parv[0]);
739 report_classes(sptr);
743 count_memory(sptr, parv[0]);
749 send_reply(sptr, RPL_ENDOFSTATS, stat);
754 * mo_stats - oper message handler
756 * parv[0] = sender prefix
757 * parv[1] = statistics selector (defaults to Message frequency)
758 * parv[2] = target server (current server defaulted, if omitted)
759 * And 'stats l' and 'stats' L:
760 * parv[3] = server mask ("*" defaulted, if omitted)
762 * parv[3] = port mask (returns p-lines when its port is matched by this)
763 * Or for stats k,K,i and I:
764 * parv[3] = [user@]host.name (returns which K/I-lines match this)
765 * or [user@]host.mask (returns which K/I-lines are mmatched by this)
766 * (defaults to old reply if ommitted, when local or Oper)
767 * A remote mask (something containing wildcards) is only
768 * allowed for IRC Operators.
770 * parv[3] = time param
771 * parv[4] = time param
772 * (see report_memleak_stats() in runmalloc.c for details)
774 * This function is getting really ugly. -Ghostwolf
776 int mo_stats(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
778 struct Message* mptr;
779 struct Client* acptr;
780 struct ConfItem* aconf;
781 char stat = parc > 1 ? parv[1][0] : '\0';
782 const char** infotext = statsinfo;
785 if (hunt_stats(cptr, sptr, parc, parv, stat, HEAD_IN_SAND_REMOTE)
794 int doall = 0, wilds = 0;
796 if (parc > 3 && !EmptyString(parv[3])) {
798 wilds = string_has_wildcards(name);
803 * Send info about connections which match, or all if the
804 * mask matches me.name. Only restrictions are on those who
805 * are invisible not being visible to 'foreigners' who use
806 * a wild card based search to list it.
808 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
809 "SendM SendKBytes RcveM RcveKBytes :Open since");
810 for (i = 0; i <= HighestFd; i++)
812 if (!(acptr = LocalClientArray[i]))
814 /* Don't return clients when this is a request for `all' */
815 if (doall && IsUser(acptr))
817 /* Only show the ones that match the given mask - if any */
818 if (!doall && wilds && match(name, cli_name(acptr)))
820 /* Skip all that do not match the specific query */
821 if (!(doall || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
823 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
824 "%s %u %u %u %u %u :%Tu", cli_name(acptr),
825 (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
826 (int)cli_sendK(acptr), (int)cli_receiveM(acptr),
827 (int)cli_receiveK(acptr), CurrentTime - cli_firsttime(acptr));
833 report_configured_links(sptr, CONF_SERVER);
836 case 'e': /* report engine name */
837 send_reply(sptr, RPL_STATSENGINE, engine_name());
840 case 'g': /* send glines */
845 report_configured_links(sptr, CONF_HUB | CONF_LEAF);
848 case 'k': /* display CONF_IPKILL as well as CONF_KILL -Kev */
849 if (0 == report_klines(sptr, (parc > 3) ? parv[3] : 0, 0))
854 feature_report(sptr);
864 report_configured_links(sptr, CONF_CLIENT);
867 if (EmptyString(parv[3]))
868 return need_more_params(sptr, "STATS I");
871 wilds = string_has_wildcards(host);
873 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
874 if (CONF_CLIENT == aconf->status) {
875 if ((!wilds && (!match(aconf->host, host) ||
876 !match(aconf->name, host))) ||
877 (wilds && (!mmatch(host, aconf->host) ||
878 !mmatch(host, aconf->name))))
880 send_reply(sptr, RPL_STATSILINE, 'I', aconf->host, aconf->name,
881 aconf->port, get_conf_class(aconf));
891 send_reply(sptr, RPL_STATMEMTOT, fda_get_byte_count(),
892 fda_get_block_count());
896 for (mptr = msgtab; mptr->cmd; mptr++)
898 send_reply(sptr, RPL_STATSCOMMANDS, mptr->cmd, mptr->count,
903 report_configured_links(sptr, CONF_OPS);
908 * show listener ports
909 * show hidden ports to opers, if there are more than 3 parameters,
910 * interpret the fourth parameter as the port number, limit non-local
911 * or non-oper results to 8 ports.
913 show_ports(sptr, 1, (parc > 3) ? atoi(parv[3]) : 0, 100);
918 send_usage(sptr, parv[0]);
922 report_crule_list(sptr, CRULE_ALL);
925 report_crule_list(sptr, CRULE_MASK);
928 tstats(sptr, parv[0]);
934 report_configured_links(sptr, CONF_UWORLD);
940 nowr = CurrentTime - cli_since(&me);
941 send_reply(sptr, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
942 (nowr / 60) % 60, nowr % 60);
943 send_reply(sptr, RPL_STATSCONN, max_connection_count, max_client_count);
953 class_send_meminfo(sptr);
954 send_listinfo(sptr, parv[0]);
959 report_classes(sptr);
963 count_memory(sptr, parv[0]);
968 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :%s", sptr, *infotext++);
971 send_reply(sptr, RPL_ENDOFSTATS, stat);