- The big forward port. I probably broke lots of stuff, so please look over any
[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_uptime(struct Client* to, struct StatDesc* sd, int stat, char* param)
326 {
327   time_t nowr;
328
329   nowr = CurrentTime - cli_since(&me);
330   send_reply(to, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
331              (nowr / 60) % 60, nowr % 60);
332   send_reply(to, RPL_STATSCONN, max_connection_count, max_client_count);
333 }
334
335 static void
336 stats_servers_verbose(struct Client* sptr, struct StatDesc* sd, int stat,
337                       char* param)
338 {
339   struct Client *acptr;
340   
341   /*
342    * lowercase 'v' is for human-readable,
343    * uppercase 'V' is for machine-readable
344    */
345   if (stat == 'v')
346     send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE,
347                "%-20s %-20s Flags Hops Numeric   Lag  RTT   Up Down "
348                "Clients/Max Proto %-10s :Info", "Servername", "Uplink",
349                "LinkTS");
350
351   for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
352   {
353     if (!IsServer(acptr) && !IsMe(acptr))
354       continue;
355     /* narrow search */
356     if (param && match(param, cli_name(acptr)))
357       continue;
358     send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE, stat == 'v' ?
359                "%-20s %-20s %c%c%c%c  %4i %s %-4i %5i %4i %4i %4i %5i %5i "
360                "P%-2i   %Tu :%s" :
361                "%s %s %c%c%c%c %i %s %i %i %i %i %i %i %i P%i %Tu :%s",
362                cli_name(acptr),
363                cli_name(cli_serv(acptr)->up),
364                IsBurst(acptr) ? 'B' : '-',
365                IsBurstAck(acptr) ? 'A' : '-',
366                IsHub(acptr) ? 'H' : '-',
367                IsService(acptr) ? 'S' : '-',
368                cli_hopcount(acptr),
369                NumServ(acptr),
370                base64toint(cli_yxx(acptr)),
371                cli_serv(acptr)->lag,
372                cli_serv(acptr)->asll_rtt,
373                cli_serv(acptr)->asll_to,
374                cli_serv(acptr)->asll_from,
375                cli_serv(acptr)->clients,
376                cli_serv(acptr)->nn_mask,
377                cli_serv(acptr)->prot,
378                cli_serv(acptr)->timestamp,
379                cli_info(acptr));
380   }
381 }
382
383 #ifdef DEBUGMODE
384 static void
385 stats_meminfo(struct Client* to, struct StatDesc* sd, int stat, char* param)
386 {
387   class_send_meminfo(to);
388   send_listinfo(to, 0);
389 }
390 #endif
391
392 static void
393 stats_help(struct Client* to, struct StatDesc* sd, int stat, char* param)
394 {
395   struct StatDesc *asd;
396
397   /* only if it's my user */
398   if (MyUser(to))
399     for (asd = statsinfo; asd->sd_c; asd++)
400       if (asd->sd_c != sd->sd_c) /* don't send the help for us */
401         sendcmdto_one(&me, CMD_NOTICE, to, "%C :%c - %s", to, asd->sd_c,
402                       asd->sd_desc);
403 }
404
405 /* This array of structures contains information about all single-character
406  * stats.  Struct StatDesc is defined in s_stats.h.
407  */
408 struct StatDesc statsinfo[] = {
409   { 'c', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_c,
410     stats_configured_links, CONF_SERVER,
411     "Remote server connection lines." },
412   { 'd', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_d,
413     stats_crule_list, 0,
414     "Dynamic routing configuration." },
415   { 'e', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_e,
416     stats_engine, 0,
417     "Report server event loop engine." },
418   { 'f', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_f,
419     feature_report, 0,
420     "Feature settings." },
421   { 'g', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_g,
422     gline_stats, 0,
423     "Global bans (G-lines)." },
424   { 'h', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_h,
425     stats_configured_links, (CONF_HUB | CONF_LEAF),
426     "Hubs information." },
427   { 'i', (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_i,
428     stats_access, CONF_CLIENT,
429     "Connection authorization lines." },
430   { 'j', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_j,
431     msgq_histogram, 0,
432     "Message length histogram." },
433   { 'k', (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_k,
434     stats_klines, 0,
435     "Local bans (K-Lines)." },
436   { 'l', (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_l,
437     stats_links, 0,
438     "Current connections information." },
439 #if 0
440   { 'M', (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_M,
441     stats_memtotal, 0,
442     "Memory allocation & leak monitoring." },
443 #endif
444   { 'm', (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_m,
445     stats_commands, 0,
446     "Message usage information." },
447   { 'o', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_o,
448     stats_configured_links, CONF_OPS,
449     "Operator information." },
450   { 'p', (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_p,
451     show_ports, 0,
452     "Listening ports." },
453   { 'q', (STAT_FLAG_OPERONLY | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_q,
454     stats_quarantine, 0,
455     "Quarantined channels list." },
456 #ifdef DEBUGMODE
457   { 'r', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_r,
458     send_usage, 0,
459     "System resource usage (Debug only)." },
460 #endif
461   { 'T', (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_T,
462     motd_report, 0,
463     "Configured Message Of The Day files." },
464   { 't', (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_t,
465     tstats, 0,
466     "Local connection statistics (Total SND/RCV, etc)." },
467   { 'U', (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_U,
468     stats_configured_links, CONF_UWORLD,
469     "Service server & nick jupes information." },
470   { 'u', (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_u,
471     stats_uptime, 0,
472     "Current uptime & highest connection count." },
473   { 'v', (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_v,
474     stats_servers_verbose, 0,
475     "Verbose server information." },
476   { 'w', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_w,
477     calc_load, 0,
478     "Userload statistics." },
479 #ifdef DEBUGMODE
480   { 'x', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_x,
481     stats_meminfo, 0,
482     "List usage information (Debug only)." },
483 #endif
484   { 'y', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_y,
485     report_classes, 0,
486     "Connection classes." },
487   { 'z', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_z,
488     count_memory, 0,
489     "Memory/Structure allocation information." },
490   { '*', (STAT_FLAG_CASESENS | STAT_FLAG_VARPARAM), FEAT_LAST_F,
491     stats_help, 0,
492     "Send help for stats." },
493   { '\0', 0, FEAT_LAST_F, 0, 0, 0 }
494 };
495
496 /* This array is for mapping from characters to statistics descriptors */
497 struct StatDesc *statsmap[256];
498
499 /* Function to build the statsmap from the statsinfo array */
500 void
501 stats_init(void)
502 {
503   struct StatDesc *sd;
504   int i;
505
506   /* Make darn sure the statsmap array is initialized to all zeros */
507   for (i = 1; i < 256; i++)
508     statsmap[i] = 0;
509
510   /* Build the mapping */
511   for (sd = statsinfo; sd->sd_c; sd++)
512   {
513     if (sd->sd_flags & STAT_FLAG_CASESENS)
514       /* case sensitive character... */
515       statsmap[(int)sd->sd_c] = sd;
516     else
517     {
518       /* case insensitive--make sure to put in two entries */
519       statsmap[(int)ToLower((int)sd->sd_c)] = sd;
520       statsmap[(int)ToUpper((int)sd->sd_c)] = sd;
521     }
522   }
523 }