Allow /stats to accept long names for the displayed statistics.
[ircu2.10.12-pk.git] / ircd / s_stats.c
1 /*
2  * IRC - Internet Relay Chat, ircd/s_stats.c
3  * Copyright (C) 2000 Joseph Bongaarts
4  *
5  * See file AUTHORS in IRC package for additional names of
6  * the programmers.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 1, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * $Id$
23  */
24 #include "config.h"
25
26 #include "class.h"
27 #include "client.h"
28 #include "gline.h"
29 #include "ircd.h"
30 #include "ircd_chattr.h"
31 #include "ircd_events.h"
32 #include "ircd_features.h"
33 #include "ircd_crypt.h"
34 #include "ircd_log.h"
35 #include "ircd_reply.h"
36 #include "ircd_string.h"
37 #include "listener.h"
38 #include "list.h"
39 #include "match.h"
40 #include "motd.h"
41 #include "msg.h"
42 #include "msgq.h"
43 #include "numeric.h"
44 #include "numnicks.h"
45 #include "res.h"
46 #include "s_bsd.h"
47 #include "s_conf.h"
48 #include "s_debug.h"
49 #include "s_misc.h"
50 #include "s_serv.h"
51 #include "s_stats.h"
52 #include "s_user.h"
53 #include "send.h"
54 #include "struct.h"
55 #include "userload.h"
56
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <sys/time.h>
61
62
63 /*
64  * m_stats/s_stats
65  *
66  * Report configuration lines and other statistics from this
67  * server.
68  *
69  * Note: The info is reported in the order the server uses
70  *       it--not reversed as in ircd.conf!
71  */
72
73 static unsigned int report_array[17][3] = {
74   {CONF_SERVER, RPL_STATSCLINE, 'C'},
75   {CONF_CLIENT, RPL_STATSILINE, 'I'},
76   {CONF_LEAF, RPL_STATSLLINE, 'L'},
77   {CONF_OPERATOR, RPL_STATSOLINE, 'O'},
78   {CONF_HUB, RPL_STATSHLINE, 'H'},
79   {CONF_UWORLD, RPL_STATSULINE, 'U'},
80   {0, 0}
81 };
82
83 /* The statsinfo array should only be used in this file, but just TRY
84  * telling the compiler that you want to forward declare a static array,
85  * and see how it responds.  So we forward declare it "extern".
86  */
87 extern struct StatDesc statsinfo[];
88
89 static void
90 stats_configured_links(struct Client *sptr, const struct StatDesc* sd,
91                        char* param)
92 {
93   static char null[] = "<NULL>";
94   struct ConfItem *tmp;
95   int mask;
96   unsigned int *p;
97   unsigned short int port;
98   char c, *host, *pass, *name;
99
100   mask = sd->sd_funcdata;
101
102   for (tmp = GlobalConfList; tmp; tmp = tmp->next) 
103   {
104     if ((tmp->status & mask))
105     {
106       for (p = &report_array[0][0]; *p; p += 3)
107         if (*p == tmp->status)
108           break;
109       if (!*p)
110         continue;
111       c = (char)*(p + 2);
112       host = BadPtr(tmp->host) ? null : tmp->host;
113       pass = BadPtr(tmp->passwd) ? null : tmp->passwd;
114       name = BadPtr(tmp->name) ? null : tmp->name;
115       port = tmp->address.port;
116       /*
117        * On K line the passwd contents can be
118        * displayed on STATS reply.    -Vesa
119        */
120       /* Special-case 'k' or 'K' lines as appropriate... -Kev */
121       if ((tmp->status & CONF_UWORLD))
122         send_reply(sptr, p[1], c, host, pass, name, port, get_conf_class(tmp));
123       else if ((tmp->status & (CONF_SERVER | CONF_HUB)))
124         send_reply(sptr, p[1], c, "*", name, port, get_conf_class(tmp));
125       else if ((tmp->status & CONF_CLIENT))
126       {
127         if(tmp->passwd && IsDigit(*tmp->passwd) && (!tmp->passwd[1] ||
128             (IsDigit(tmp->passwd[1]) && !tmp->passwd[2])))
129           send_reply(sptr, p[1], c, host, pass, name, port, get_conf_class(tmp));
130         else
131           send_reply(sptr, p[1], c, host, "*", name, port, get_conf_class(tmp));
132       }
133       else
134         send_reply(sptr, p[1], c, host, name, port, get_conf_class(tmp));
135     }
136   }
137 }
138
139 /*
140  * {CONF_CRULEALL, RPL_STATSDLINE, 'D'},
141  * {CONF_CRULEAUTO, RPL_STATSDLINE, 'd'},
142  */
143 static void
144 stats_crule_list(struct Client* to, const struct StatDesc *sd,
145                  char *param)
146 {
147   const struct CRuleConf* p = conf_get_crule_list();
148
149   for ( ; p; p = p->next)
150   {
151     if (p->type & sd->sd_funcdata)
152       send_reply(to, RPL_STATSDLINE, sd->sd_c, p->hostmask, p->rule);
153   }
154 }
155
156 static void
157 stats_engine(struct Client *to, const struct StatDesc *sd, char *param)
158 {
159   send_reply(to, RPL_STATSENGINE, engine_name());
160 }
161
162 static void
163 stats_access(struct Client *to, const struct StatDesc *sd, char *param)
164 {
165   struct ConfItem *aconf;
166   int wilds = 0;
167   int count = 1000;
168
169   if (!param)
170   {
171     stats_configured_links(to, sd, param);
172     return;
173   }
174
175   wilds = string_has_wildcards(param);
176
177   for (aconf = GlobalConfList; aconf; aconf = aconf->next)
178   {
179     if (aconf->status != CONF_CLIENT)
180       continue;
181     if ((!wilds && (!match(aconf->host, param) ||
182                     !match(aconf->name, param))) ||
183         (wilds && (!mmatch(param, aconf->host) ||
184                    !mmatch(param, aconf->name))))
185     {
186       send_reply(to, RPL_STATSILINE, 'I', aconf->host, aconf->name,
187                  aconf->address.port, get_conf_class(aconf));
188       if (--count == 0)
189         break;
190     }
191   }
192 }
193
194
195
196 /*
197  * {CONF_KILL, RPL_STATSKLINE, 'K'},
198  * {CONF_IPKILL, RPL_STATSKLINE, 'k'},
199  */
200 static void
201 report_deny_list(struct Client* to)
202 {
203   const struct DenyConf* p = conf_get_deny_list();
204   for ( ; p; p = p->next)
205     send_reply(to, RPL_STATSKLINE, (p->flags & DENY_FLAGS_IP) ? 'k' : 'K',
206                p->hostmask, p->message, p->usermask);
207 }
208
209 static void
210 stats_klines(struct Client *sptr, const struct StatDesc *sd, char *mask)
211 {
212   int wilds = 0;
213   int count = 3;
214   int limit_query = 0;
215   char *user  = 0;
216   char *host;
217   const struct DenyConf* conf;
218
219   if (!IsAnOper(sptr))
220     limit_query = 1;
221
222   if (!mask)
223   {
224     if (limit_query)
225       need_more_params(sptr, "STATS K");
226     else
227       report_deny_list(sptr);
228     return;
229   }
230
231   if (!limit_query)
232   {
233     wilds = string_has_wildcards(mask);
234     count = 1000;
235   }
236   if ((host = strchr(mask, '@')))
237   {
238     user = mask;
239     *host++ = '\0';
240   }
241   else
242     host = mask;
243
244   for (conf = conf_get_deny_list(); conf; conf = conf->next)
245   {
246     if ((!wilds && ((user || conf->hostmask) &&
247                     !match(conf->hostmask, host) &&
248                     (!user || !match(conf->usermask, user)))) ||
249         (wilds && !mmatch(host, conf->hostmask) &&
250          (!user || !mmatch(user, conf->usermask))))
251     {
252       send_reply(sptr, RPL_STATSKLINE,
253                  (conf->flags & DENY_FLAGS_IP) ? 'k' : 'K',
254                  conf->hostmask, conf->message, conf->usermask);
255       if (--count == 0)
256         return;
257     }
258   }
259 }
260
261 static void
262 stats_links(struct Client* sptr, const struct StatDesc* sd, char* name)
263 {
264   struct Client *acptr;
265   int i;
266   int wilds = 0;
267
268   if (name)
269     wilds = string_has_wildcards(name);
270
271   /*
272    * Send info about connections which match, or all if the
273    * mask matches me.name.  Only restrictions are on those who
274    * are invisible not being visible to 'foreigners' who use
275    * a wild card based search to list it.
276    */
277   send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
278              "SendM SendKBytes RcveM RcveKBytes :Open since");
279     for (i = 0; i <= HighestFd; i++)
280     {
281       if (!(acptr = LocalClientArray[i]))
282         continue;
283       /* Don't return clients when this is a request for `all' */
284       if (!name && IsUser(acptr))
285         continue;
286       /* Don't show invisible people to non opers unless they know the nick */
287       if (IsInvisible(acptr) && (!name || wilds) && !IsAnOper(acptr) &&
288           (acptr != sptr))
289         continue;
290       /* Only show the ones that match the given mask - if any */
291       if (name && wilds && match(name, cli_name(acptr)))
292         continue;
293       /* Skip all that do not match the specific query */
294       if (!(!name || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
295         continue;
296       send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
297                  "%s %u %u %u %u %u :%Tu",
298                  (*(cli_name(acptr))) ? cli_name(acptr) : "<unregistered>",
299                  (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
300                  (int)cli_sendK(acptr), (int)cli_receiveM(acptr),
301                  (int)cli_receiveK(acptr), CurrentTime - cli_firsttime(acptr));
302     }
303 }
304
305 /* hopefuly this will be where we'll spit out info about loaded modules */
306 static void
307 stats_modules(struct Client* to, const struct StatDesc* sd, char* param)
308 {
309 crypt_mechs_t* mechs;
310
311   send_reply(to, SND_EXPLICIT | RPL_STATSLLINE, 
312    "Module  Description      Entry Point");
313
314  /* atm the only "modules" we have are the crypto mechanisms,
315     eventualy they'll be part of a global dl module list, for now
316     i'll just output data about them -- hikari */
317
318  if(crypt_mechs_root == NULL)
319   return;
320
321  mechs = crypt_mechs_root->next;
322
323  for(;;)
324  {
325   if(mechs == NULL)
326    return;
327
328   send_reply(to, SND_EXPLICIT | RPL_STATSLLINE, 
329    "%s  %s     0x%X", 
330    mechs->mech->shortname, mechs->mech->description, 
331    mechs->mech->crypt_function);
332
333   mechs = mechs->next;
334  }
335
336 }
337
338 static void
339 stats_commands(struct Client* to, const struct StatDesc* sd, char* param)
340 {
341   struct Message *mptr;
342
343   for (mptr = msgtab; mptr->cmd; mptr++)
344     if (mptr->count)
345       send_reply(to, RPL_STATSCOMMANDS, mptr->cmd, mptr->count, mptr->bytes);
346 }
347
348 static void
349 stats_quarantine(struct Client* to, const struct StatDesc* sd, char* param)
350 {
351   struct qline *qline;
352
353   for (qline = GlobalQuarantineList; qline; qline = qline->next)
354   {
355     if (param && match(param, qline->chname)) /* narrow search */
356       continue;
357     send_reply(to, RPL_STATSQLINE, qline->chname, qline->reason);
358   }
359 }
360
361 static void
362 stats_mapping(struct Client *to, const struct StatDesc* sd, char* param)
363 {
364   struct s_map *map;
365
366   send_reply(to, RPL_STATSRLINE, "Command", "Name", "Prepend", "Target");
367   for (map = GlobalServiceMapList; map; map = map->next) {
368     struct nick_host *nh;
369     for (nh = map->services; nh; nh = nh->next) {
370       send_reply(to, RPL_STATSRLINE, map->command, map->name,
371                  (map->prepend ? map->prepend : "*"), nh->nick);
372     }
373   }
374 }
375
376 static void
377 stats_uptime(struct Client* to, const struct StatDesc* sd, char* param)
378 {
379   time_t nowr;
380
381   nowr = CurrentTime - cli_since(&me);
382   send_reply(to, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
383              (nowr / 60) % 60, nowr % 60);
384   send_reply(to, RPL_STATSCONN, max_connection_count, max_client_count);
385 }
386
387 static void
388 stats_servers_verbose(struct Client* sptr, const struct StatDesc* sd,
389                       char* param)
390 {
391   struct Client *acptr;
392   const char *fmt;
393
394   /*
395    * lowercase 'v' is for human-readable,
396    * uppercase 'V' is for machine-readable
397    */
398   if (sd->sd_funcdata) {
399     send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE,
400                "%-20s %-20s Flags Hops Numeric   Lag  RTT   Up Down "
401                "Clients/Max Proto %-10s :Info", "Servername", "Uplink",
402                "LinkTS");
403     fmt = "%-20s %-20s %c%c%c%c  %4i %s %-4i %5i %4i %4i %4i %5i %5i P%-2i   %Tu :%s";
404   } else {
405     fmt = "%s %s %c%c%c%c %i %s %i %i %i %i %i %i %i P%i %Tu :%s";
406   }
407
408   for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
409   {
410     if (!IsServer(acptr) && !IsMe(acptr))
411       continue;
412     /* narrow search */
413     if (param && match(param, cli_name(acptr)))
414       continue;
415     send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE, fmt,
416                cli_name(acptr),
417                cli_name(cli_serv(acptr)->up),
418                IsBurst(acptr) ? 'B' : '-',
419                IsBurstAck(acptr) ? 'A' : '-',
420                IsHub(acptr) ? 'H' : '-',
421                IsService(acptr) ? 'S' : '-',
422                cli_hopcount(acptr),
423                NumServ(acptr),
424                base64toint(cli_yxx(acptr)),
425                cli_serv(acptr)->lag,
426                cli_serv(acptr)->asll_rtt,
427                cli_serv(acptr)->asll_to,
428                cli_serv(acptr)->asll_from,
429                cli_serv(acptr)->clients,
430                cli_serv(acptr)->nn_mask,
431                cli_serv(acptr)->prot,
432                cli_serv(acptr)->timestamp,
433                cli_info(acptr));
434   }
435 }
436
437 #ifdef DEBUGMODE
438 static void
439 stats_meminfo(struct Client* to, const struct StatDesc* sd, char* param)
440 {
441   class_send_meminfo(to);
442   send_listinfo(to, 0);
443 }
444 #endif
445
446 static void
447 stats_help(struct Client* to, const struct StatDesc* sd, char* param)
448 {
449   struct StatDesc *asd;
450
451   /* only if it's my user */
452   if (MyUser(to))
453     for (asd = statsinfo; asd->sd_name; asd++)
454       if (asd != sd) /* don't send the help for us */
455         sendcmdto_one(&me, CMD_NOTICE, to, "%C :%c (%s) - %s", to, asd->sd_c,
456                       asd->sd_name, asd->sd_desc);
457 }
458
459 /* This array of structures contains information about all single-character
460  * stats.  Struct StatDesc is defined in s_stats.h.
461  */
462 struct StatDesc statsinfo[] = {
463   { 'a', "nameservers", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_a,
464     report_dns_servers, 0,
465     "DNS servers." },
466   { 'c', "connect", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_c,
467     stats_configured_links, CONF_SERVER,
468     "Remote server connection lines." },
469   { 'd', "maskrules", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_d,
470     stats_crule_list, CRULE_MASK,
471     "Dynamic routing configuration." },
472   { 'D', "crules", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_d,
473     stats_crule_list, CRULE_ALL,
474     "Dynamic routing configuration." },
475   { 'e', "engine", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_e,
476     stats_engine, 0,
477     "Report server event loop engine." },
478   { 'f', "features", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_f,
479     feature_report, 0,
480     "Feature settings." },
481   { 'g', "glines", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_g,
482     gline_stats, 0,
483     "Global bans (G-lines)." },
484   { 'h', "hubs", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_h,
485     stats_configured_links, (CONF_HUB | CONF_LEAF),
486     "Hubs information." },
487   { 'i', "access", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_i,
488     stats_access, CONF_CLIENT,
489     "Connection authorization lines." },
490   { 'j', "histogram", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_j,
491     msgq_histogram, 0,
492     "Message length histogram." },
493   { 'k', "klines", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_k,
494     stats_klines, 0,
495     "Local bans (K-Lines)." },
496   { 'l', "links", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS),
497     FEAT_HIS_STATS_l,
498     stats_links, 0,
499     "Current connections information." },
500   { 'L', "modules", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS),
501     FEAT_HIS_STATS_L,
502     stats_modules, 0,
503     "Dynamically loaded modules." },
504   { 'm', "commands", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_m,
505     stats_commands, 0,
506     "Message usage information." },
507   { 'o', "operators", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_o,
508     stats_configured_links, CONF_OPERATOR,
509     "Operator information." },
510   { 'p', "ports", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_p,
511     show_ports, 0,
512     "Listening ports." },
513   { 'q', "quarantines", (STAT_FLAG_OPERONLY | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_q,
514     stats_quarantine, 0,
515     "Quarantined channels list." },
516   { 'R', "mappings", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_R,
517     stats_mapping, 0,
518     "Service mappings." },
519 #ifdef DEBUGMODE
520   { 'r', "usage", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_r,
521     send_usage, 0,
522     "System resource usage (Debug only)." },
523 #endif
524   { 'T', "motds", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_T,
525     motd_report, 0,
526     "Configured Message Of The Day files." },
527   { 't', "locals", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_t,
528     tstats, 0,
529     "Local connection statistics (Total SND/RCV, etc)." },
530   { 'U', "uworld", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_U,
531     stats_configured_links, CONF_UWORLD,
532     "Service server & nick jupes information." },
533   { 'u', "uptime", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_u,
534     stats_uptime, 0,
535     "Current uptime & highest connection count." },
536   { 'v', "vservers", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS), FEAT_HIS_STATS_v,
537     stats_servers_verbose, 1,
538     "Verbose server information." },
539   { 'V', "vserversmach", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS), FEAT_HIS_STATS_v,
540     stats_servers_verbose, 0,
541     "Verbose server information." },
542   { 'w', "userload", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_w,
543     calc_load, 0,
544     "Userload statistics." },
545 #ifdef DEBUGMODE
546   { 'x', "memusage", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_x,
547     stats_meminfo, 0,
548     "List usage information (Debug only)." },
549 #endif
550   { 'y', "classes", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_y,
551     report_classes, 0,
552     "Connection classes." },
553   { 'z', "memory", STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_z,
554     count_memory, 0,
555     "Memory/Structure allocation information." },
556   { '*', "help", (STAT_FLAG_CASESENS | STAT_FLAG_VARPARAM), FEAT_LAST_F,
557     stats_help, 0,
558     "Send help for stats." },
559   { '\0', 0, FEAT_LAST_F, 0, 0, 0 }
560 };
561
562 /* This array is for mapping from characters to statistics descriptors */
563 static struct StatDesc *statsmap[256];
564 static int statscount;
565
566 static int
567 stats_cmp(const void *a_, const void *b_)
568 {
569   const struct StatDesc *a = a_;
570   const struct StatDesc *b = b_;
571   return ircd_strcmp(a->sd_name, b->sd_name);
572 }
573
574 static int
575 stats_search(const void *key, const void *sd_)
576 {
577   const struct StatDesc *sd = sd_;
578   return ircd_strcmp(key, sd->sd_name);
579 }
580
581 /* Look up a stats handler.  If name_or_char is just one character
582  * long, use that as a character index; otherwise, look it up by
583  * name in statsinfo.
584  */
585 const struct StatDesc *
586 stats_find(const char *name_or_char)
587 {
588   if (!name_or_char[1])
589     return statsmap[(int)name_or_char[0]];
590   else
591     return bsearch(name_or_char, statsinfo, statscount, sizeof(statsinfo[0]), stats_search);
592 }
593
594 /* Function to build the statsmap from the statsinfo array */
595 void
596 stats_init(void)
597 {
598   struct StatDesc *sd;
599   int i;
600
601   /* Make darn sure the statsmap array is initialized to all zeros */
602   for (i = 0; i < 256; i++)
603     statsmap[i] = 0;
604
605   /* Count number of stats entries and sort them. */
606   for (statscount = 0, sd = statsinfo; sd->sd_name; sd++, statscount++) {}
607   qsort(statsinfo, statscount, sizeof(statsinfo[0]), stats_cmp);
608
609   /* Build the mapping */
610   for (sd = statsinfo; sd->sd_name; sd++)
611   {
612     if (!sd->sd_c)
613       continue;
614     else if (sd->sd_flags & STAT_FLAG_CASESENS)
615       /* case sensitive character... */
616       statsmap[(int)sd->sd_c] = sd;
617     else
618     {
619       /* case insensitive--make sure to put in two entries */
620       statsmap[(int)ToLower((int)sd->sd_c)] = sd;
621       statsmap[(int)ToUpper((int)sd->sd_c)] = sd;
622     }
623   }
624 }