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