2004-05-09 Michael Poole <mdpoole@troilus.org>
[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_log.h"
34 #include "ircd_reply.h"
35 #include "ircd_string.h"
36 #include "listener.h"
37 #include "list.h"
38 #include "match.h"
39 #include "motd.h"
40 #include "msg.h"
41 #include "msgq.h"
42 #include "numeric.h"
43 #include "numnicks.h"
44 #include "s_bsd.h"
45 #include "s_conf.h"
46 #include "s_debug.h"
47 #include "s_misc.h"
48 #include "s_serv.h"
49 #include "s_stats.h"
50 #include "s_user.h"
51 #include "send.h"
52 #include "struct.h"
53 #include "userload.h"
54
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <sys/time.h>
59
60
61 /*
62  * m_stats/s_stats
63  *
64  * Report configuration lines and other statistics from this
65  * server. 
66  *
67  * Note: The info is reported in the order the server uses
68  *       it--not reversed as in ircd.conf!
69  */
70
71 static unsigned int report_array[17][3] = {
72   {CONF_SERVER, RPL_STATSCLINE, 'C'},
73   {CONF_CLIENT, RPL_STATSILINE, 'I'},
74   {CONF_LEAF, RPL_STATSLLINE, 'L'},
75   {CONF_OPERATOR, RPL_STATSOLINE, 'O'},
76   {CONF_HUB, RPL_STATSHLINE, 'H'},
77   {CONF_LOCOP, RPL_STATSOLINE, 'o'},
78   {CONF_UWORLD, RPL_STATSULINE, 'U'},
79   {0, 0}
80 };
81
82 static void
83 stats_configured_links(struct Client *sptr, struct StatDesc* sd, int stat,
84                        char* param)
85 {
86   static char null[] = "<NULL>";
87   struct ConfItem *tmp;
88   int mask;
89   unsigned int *p;
90   unsigned short int port;
91   char c, *host, *pass, *name;
92   
93   mask = sd->sd_funcdata;
94
95   for (tmp = GlobalConfList; tmp; tmp = tmp->next) 
96   {
97     if ((tmp->status & mask))
98     {
99       for (p = &report_array[0][0]; *p; p += 3)
100         if (*p == tmp->status)
101           break;
102       if (!*p)
103         continue;
104       c = (char)*(p + 2);
105       host = BadPtr(tmp->host) ? null : tmp->host;
106       pass = BadPtr(tmp->passwd) ? null : tmp->passwd;
107       name = BadPtr(tmp->name) ? null : tmp->name;
108       port = tmp->port;
109       /*
110        * On K line the passwd contents can be
111        * displayed on STATS reply.    -Vesa
112        */
113       /* Special-case 'k' or 'K' lines as appropriate... -Kev */
114       if ((tmp->status & CONF_UWORLD))
115         send_reply(sptr, p[1], c, host, pass, name, port, get_conf_class(tmp));
116       else if ((tmp->status & (CONF_SERVER | CONF_HUB)))
117         send_reply(sptr, p[1], c, "*", name, port, get_conf_class(tmp));
118       else if ((tmp->status & CONF_CLIENT))
119       {
120         if(tmp->passwd && IsDigit(*tmp->passwd) && (!tmp->passwd[1] ||
121             (IsDigit(tmp->passwd[1]) && !tmp->passwd[2])))
122           send_reply(sptr, p[1], c, host, pass, name, port, get_conf_class(tmp));
123         else
124           send_reply(sptr, p[1], c, host, "*", name, port, get_conf_class(tmp));
125       }
126       else
127         send_reply(sptr, p[1], c, host, name, port, get_conf_class(tmp));
128     }
129   }
130 }
131
132 /*
133  * {CONF_CRULEALL, RPL_STATSDLINE, 'D'},
134  * {CONF_CRULEAUTO, RPL_STATSDLINE, 'd'},
135  */
136 static void
137 stats_crule_list(struct Client* to, struct StatDesc *sd, int stat,
138                  char *param)
139 {
140   int mask;
141   const struct CRuleConf* p = conf_get_crule_list();
142
143   mask = (stat == 'D') ? CRULE_ALL : CRULE_MASK;
144
145   for ( ; p; p = p->next)
146   {
147     if (p->type & mask)
148       send_reply(to, RPL_STATSDLINE, stat, p->hostmask, p->rule);
149   }
150 }
151
152 static void
153 stats_engine(struct Client *to, struct StatDesc *sd, int stat, char *param)
154 {
155   send_reply(to, RPL_STATSENGINE, engine_name());
156 }
157
158 static void
159 stats_access(struct Client *to, struct StatDesc *sd, int stat, char *param)
160 {
161   struct ConfItem *aconf;
162   int wilds = 0;
163   int count = 1000;
164
165   if (!param)
166   {
167     stats_configured_links(to, sd, stat, param);
168     return;
169   }
170
171   wilds = string_has_wildcards(param);
172
173   for (aconf = GlobalConfList; aconf; aconf = aconf->next)
174   {
175     if (aconf->status != CONF_CLIENT)
176       continue;
177     if ((!wilds && (!match(aconf->host, param) ||
178                     !match(aconf->name, param))) ||
179         (wilds && (!mmatch(param, aconf->host) ||
180                    !mmatch(param, aconf->name))))
181     {
182       send_reply(to, RPL_STATSILINE, 'I', aconf->host, aconf->name,
183                  aconf->port, get_conf_class(aconf));
184       if (--count == 0)
185         break;
186     }
187   }
188 }
189
190
191
192 /*
193  * {CONF_KILL, RPL_STATSKLINE, 'K'},
194  * {CONF_IPKILL, RPL_STATSKLINE, 'k'},
195  */
196 static void
197 report_deny_list(struct Client* to)
198 {
199   const struct DenyConf* p = conf_get_deny_list();
200   for ( ; p; p = p->next)
201     send_reply(to, RPL_STATSKLINE, (p->flags & DENY_FLAGS_IP) ? 'k' : 'K',
202                p->hostmask, p->message, p->usermask);
203 }
204
205 static void
206 stats_klines(struct Client *sptr, struct StatDesc *sd, int stat, char *mask)
207 {
208   int wilds = 0;
209   int count = 3;
210   int limit_query = 0;
211   char *user  = 0;
212   char *host;
213   const struct DenyConf* conf;
214  
215   if (!IsAnOper(sptr))
216     limit_query = 1;
217   
218   if (!mask)
219   {
220     if (limit_query)
221       need_more_params(sptr, "STATS K");
222     else
223       report_deny_list(sptr);
224     return;
225   }
226
227   if (!limit_query)
228   {
229     wilds = string_has_wildcards(mask);
230     count = 1000;
231   }
232   if ((host = strchr(mask, '@')))
233   {
234     user = mask;
235     *host++ = '\0';
236   }
237   else
238     host = mask;
239
240   for (conf = conf_get_deny_list(); conf; conf = conf->next)
241   {
242     if ((!wilds && ((user || conf->hostmask) &&
243                     !match(conf->hostmask, host) &&
244                     (!user || !match(conf->usermask, user)))) ||
245         (wilds && !mmatch(host, conf->hostmask) &&
246          (!user || !mmatch(user, conf->usermask))))
247     {
248       send_reply(sptr, RPL_STATSKLINE,
249                  (conf->flags & DENY_FLAGS_IP) ? 'k' : 'K',
250                  conf->hostmask, conf->message, conf->usermask);
251       if (--count == 0)
252         return;
253     }
254   }
255 }
256
257 static void
258 stats_links(struct Client* sptr, struct StatDesc* sd, int stat, char* name)
259 {
260   struct Client *acptr;
261   int i;
262   int wilds = 0;
263
264   if (name)
265     wilds = string_has_wildcards(name);
266
267   /*
268    * Send info about connections which match, or all if the
269    * mask matches me.name.  Only restrictions are on those who
270    * are invisible not being visible to 'foreigners' who use
271    * a wild card based search to list it.
272    */
273   send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
274              "SendM SendKBytes RcveM RcveKBytes :Open since");
275     for (i = 0; i <= HighestFd; i++)
276     {
277       if (!(acptr = LocalClientArray[i]))
278         continue;
279       /* Don't return clients when this is a request for `all' */
280       if (!name && IsUser(acptr))
281         continue;
282       /* Don't show invisible people to non opers unless they know the nick */
283       if (IsInvisible(acptr) && (!name || wilds) && !IsAnOper(acptr) &&
284           (acptr != sptr))
285         continue;
286       /* Only show the ones that match the given mask - if any */
287       if (name && wilds && match(name, cli_name(acptr)))
288         continue;
289       /* Skip all that do not match the specific query */
290       if (!(!name || wilds) && 0 != ircd_strcmp(name, cli_name(acptr)))
291         continue;
292       send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
293                  "%s %u %u %u %u %u :%Tu",
294                  (*(cli_name(acptr))) ? cli_name(acptr) : "<unregistered>",
295                  (int)MsgQLength(&(cli_sendQ(acptr))), (int)cli_sendM(acptr),
296                  (int)cli_sendK(acptr), (int)cli_receiveM(acptr),
297                  (int)cli_receiveK(acptr), CurrentTime - cli_firsttime(acptr));
298     }
299 }
300
301 static void
302 stats_commands(struct Client* to, struct StatDesc* sd, int stat, char* param)
303 {
304   struct Message *mptr;
305
306   for (mptr = msgtab; mptr->cmd; mptr++)
307     if (mptr->count)
308       send_reply(to, RPL_STATSCOMMANDS, mptr->cmd, mptr->count, mptr->bytes);
309 }
310
311 static void
312 stats_quarantine(struct Client* to, struct StatDesc* sd, int stat, char* param)
313 {
314   struct qline *qline;
315
316   for (qline = GlobalQuarantineList; qline; qline = qline->next)
317   {
318     if (param && match(param, qline->chname)) /* narrow search */
319       continue;
320     send_reply(to, RPL_STATSQLINE, qline->chname, qline->reason);
321   }
322 }
323  
324 static void
325 stats_mapping(struct Client *to, struct StatDesc* sd, int stat, char* param)
326 {
327   struct s_map *map;
328
329   send_reply(to, RPL_STATSRLINE, "Command", "Name", "Prepend", "Target");
330   for (map = GlobalServiceMapList; map; map = map->next) {
331     struct nick_host *nh;
332     for (nh = map->services; nh; nh = nh->next) {
333       send_reply(to, RPL_STATSRLINE, map->command, map->name,
334                  (map->prepend ? map->prepend : "*"), nh->nick);
335     }
336   }
337 }
338
339 static void
340 stats_uptime(struct Client* to, struct StatDesc* sd, int stat, char* param)
341 {
342   time_t nowr;
343
344   nowr = CurrentTime - cli_since(&me);
345   send_reply(to, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
346              (nowr / 60) % 60, nowr % 60);
347   send_reply(to, RPL_STATSCONN, max_connection_count, max_client_count);
348 }
349
350 static void
351 stats_servers_verbose(struct Client* sptr, struct StatDesc* sd, int stat,
352                       char* param)
353 {
354   struct Client *acptr;
355   
356   /*
357    * lowercase 'v' is for human-readable,
358    * uppercase 'V' is for machine-readable
359    */
360   if (stat == 'v')
361     send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE,
362                "%-20s %-20s Flags Hops Numeric   Lag  RTT   Up Down "
363                "Clients/Max Proto %-10s :Info", "Servername", "Uplink",
364                "LinkTS");
365
366   for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
367   {
368     if (!IsServer(acptr) && !IsMe(acptr))
369       continue;
370     /* narrow search */
371     if (param && match(param, cli_name(acptr)))
372       continue;
373     send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE, stat == 'v' ?
374                "%-20s %-20s %c%c%c%c  %4i %s %-4i %5i %4i %4i %4i %5i %5i "
375                "P%-2i   %Tu :%s" :
376                "%s %s %c%c%c%c %i %s %i %i %i %i %i %i %i P%i %Tu :%s",
377                cli_name(acptr),
378                cli_name(cli_serv(acptr)->up),
379                IsBurst(acptr) ? 'B' : '-',
380                IsBurstAck(acptr) ? 'A' : '-',
381                IsHub(acptr) ? 'H' : '-',
382                IsService(acptr) ? 'S' : '-',
383                cli_hopcount(acptr),
384                NumServ(acptr),
385                base64toint(cli_yxx(acptr)),
386                cli_serv(acptr)->lag,
387                cli_serv(acptr)->asll_rtt,
388                cli_serv(acptr)->asll_to,
389                cli_serv(acptr)->asll_from,
390                cli_serv(acptr)->clients,
391                cli_serv(acptr)->nn_mask,
392                cli_serv(acptr)->prot,
393                cli_serv(acptr)->timestamp,
394                cli_info(acptr));
395   }
396 }
397
398 #ifdef DEBUGMODE
399 static void
400 stats_meminfo(struct Client* to, struct StatDesc* sd, int stat, char* param)
401 {
402   class_send_meminfo(to);
403   send_listinfo(to, 0);
404 }
405 #endif
406
407 static void
408 stats_help(struct Client* to, struct StatDesc* sd, int stat, char* param)
409 {
410   struct StatDesc *asd;
411
412   /* only if it's my user */
413   if (MyUser(to))
414     for (asd = statsinfo; asd->sd_c; asd++)
415       if (asd->sd_c != sd->sd_c) /* don't send the help for us */
416         sendcmdto_one(&me, CMD_NOTICE, to, "%C :%c - %s", to, asd->sd_c,
417                       asd->sd_desc);
418 }
419
420 /* This array of structures contains information about all single-character
421  * stats.  Struct StatDesc is defined in s_stats.h.
422  */
423 struct StatDesc statsinfo[] = {
424   { 'c', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_c,
425     stats_configured_links, CONF_SERVER,
426     "Remote server connection lines." },
427   { 'd', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_d,
428     stats_crule_list, 0,
429     "Dynamic routing configuration." },
430   { 'e', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_e,
431     stats_engine, 0,
432     "Report server event loop engine." },
433   { 'f', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_f,
434     feature_report, 0,
435     "Feature settings." },
436   { 'g', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_g,
437     gline_stats, 0,
438     "Global bans (G-lines)." },
439   { 'h', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_h,
440     stats_configured_links, (CONF_HUB | CONF_LEAF),
441     "Hubs information." },
442   { 'i', (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_i,
443     stats_access, CONF_CLIENT,
444     "Connection authorization lines." },
445   { 'j', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_j,
446     msgq_histogram, 0,
447     "Message length histogram." },
448   { 'k', (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_k,
449     stats_klines, 0,
450     "Local bans (K-Lines)." },
451   { 'l', (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_l,
452     stats_links, 0,
453     "Current connections information." },
454 #if 0
455   { 'M', (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_M,
456     stats_memtotal, 0,
457     "Memory allocation & leak monitoring." },
458 #endif
459   { 'm', (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_m,
460     stats_commands, 0,
461     "Message usage information." },
462   { 'o', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_o,
463     stats_configured_links, CONF_OPS,
464     "Operator information." },
465   { 'p', (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_p,
466     show_ports, 0,
467     "Listening ports." },
468   { 'q', (STAT_FLAG_OPERONLY | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_q,
469     stats_quarantine, 0,
470     "Quarantined channels list." },
471   { 'R', (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_R,
472     stats_mapping, 0,
473     "Service mappings." },
474 #ifdef DEBUGMODE
475   { 'r', (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_r,
476     send_usage, 0,
477     "System resource usage (Debug only)." },
478 #endif
479   { 'T', (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_T,
480     motd_report, 0,
481     "Configured Message Of The Day files." },
482   { 't', (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_t,
483     tstats, 0,
484     "Local connection statistics (Total SND/RCV, etc)." },
485   { 'U', (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_U,
486     stats_configured_links, CONF_UWORLD,
487     "Service server & nick jupes information." },
488   { 'u', (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_u,
489     stats_uptime, 0,
490     "Current uptime & highest connection count." },
491   { 'v', (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_v,
492     stats_servers_verbose, 0,
493     "Verbose server information." },
494   { 'w', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_w,
495     calc_load, 0,
496     "Userload statistics." },
497 #ifdef DEBUGMODE
498   { 'x', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_x,
499     stats_meminfo, 0,
500     "List usage information (Debug only)." },
501 #endif
502   { 'y', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_y,
503     report_classes, 0,
504     "Connection classes." },
505   { 'z', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_z,
506     count_memory, 0,
507     "Memory/Structure allocation information." },
508   { '*', (STAT_FLAG_CASESENS | STAT_FLAG_VARPARAM), FEAT_LAST_F,
509     stats_help, 0,
510     "Send help for stats." },
511   { '\0', 0, FEAT_LAST_F, 0, 0, 0 }
512 };
513
514 /* This array is for mapping from characters to statistics descriptors */
515 struct StatDesc *statsmap[256];
516
517 /* Function to build the statsmap from the statsinfo array */
518 void
519 stats_init(void)
520 {
521   struct StatDesc *sd;
522   int i;
523
524   /* Make darn sure the statsmap array is initialized to all zeros */
525   for (i = 0; i < 256; i++)
526     statsmap[i] = 0;
527
528   /* Build the mapping */
529   for (sd = statsinfo; sd->sd_c; sd++)
530   {
531     if (sd->sd_flags & STAT_FLAG_CASESENS)
532       /* case sensitive character... */
533       statsmap[(int)sd->sd_c] = sd;
534     else
535     {
536       /* case insensitive--make sure to put in two entries */
537       statsmap[(int)ToLower((int)sd->sd_c)] = sd;
538       statsmap[(int)ToUpper((int)sd->sd_c)] = sd;
539     }
540   }
541 }