Apply hikari's pluggable crypt mechanism patch
[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 "s_bsd.h"
46 #include "s_conf.h"
47 #include "s_debug.h"
48 #include "s_misc.h"
49 #include "s_serv.h"
50 #include "s_stats.h"
51 #include "s_user.h"
52 #include "send.h"
53 #include "struct.h"
54 #include "userload.h"
55
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <sys/time.h>
60
61
62 /*
63  * m_stats/s_stats
64  *
65  * Report configuration lines and other statistics from this
66  * server. 
67  *
68  * Note: The info is reported in the order the server uses
69  *       it--not reversed as in ircd.conf!
70  */
71
72 static unsigned int report_array[17][3] = {
73   {CONF_SERVER, RPL_STATSCLINE, 'C'},
74   {CONF_CLIENT, RPL_STATSILINE, 'I'},
75   {CONF_LEAF, RPL_STATSLLINE, 'L'},
76   {CONF_OPERATOR, RPL_STATSOLINE, 'O'},
77   {CONF_HUB, RPL_STATSHLINE, 'H'},
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 /* hopefuly this will be where we'll spit out info about loaded modules */
302 static void
303 stats_modules(struct Client* to, struct StatDesc* sd, int stat, char* param)
304 {
305 crypt_mechs_t* mechs;
306
307   send_reply(to, SND_EXPLICIT | RPL_STATSLLINE, 
308    "Module  Description      Entry Point");
309
310  /* atm the only "modules" we have are the crypto mechanisms,
311     eventualy they'll be part of a global dl module list, for now
312     i'll just output data about them -- hikari */
313
314  if(crypt_mechs_root == NULL)
315   return;
316
317  mechs = crypt_mechs_root->next;
318
319  for(;;)
320  {
321   if(mechs == NULL)
322    return;
323
324   send_reply(to, SND_EXPLICIT | RPL_STATSLLINE, 
325    "%s  %s     0x%X", 
326    mechs->mech->shortname, mechs->mech->description, 
327    mechs->mech->crypt_function);
328
329   mechs = mechs->next;
330  }
331
332 }
333
334 static void
335 stats_commands(struct Client* to, struct StatDesc* sd, int stat, char* param)
336 {
337   struct Message *mptr;
338
339   for (mptr = msgtab; mptr->cmd; mptr++)
340     if (mptr->count)
341       send_reply(to, RPL_STATSCOMMANDS, mptr->cmd, mptr->count, mptr->bytes);
342 }
343
344 static void
345 stats_quarantine(struct Client* to, struct StatDesc* sd, int stat, char* param)
346 {
347   struct qline *qline;
348
349   for (qline = GlobalQuarantineList; qline; qline = qline->next)
350   {
351     if (param && match(param, qline->chname)) /* narrow search */
352       continue;
353     send_reply(to, RPL_STATSQLINE, qline->chname, qline->reason);
354   }
355 }
356  
357 static void
358 stats_mapping(struct Client *to, struct StatDesc* sd, int stat, char* param)
359 {
360   struct s_map *map;
361
362   send_reply(to, RPL_STATSRLINE, "Command", "Name", "Prepend", "Target");
363   for (map = GlobalServiceMapList; map; map = map->next) {
364     struct nick_host *nh;
365     for (nh = map->services; nh; nh = nh->next) {
366       send_reply(to, RPL_STATSRLINE, map->command, map->name,
367                  (map->prepend ? map->prepend : "*"), nh->nick);
368     }
369   }
370 }
371
372 static void
373 stats_uptime(struct Client* to, struct StatDesc* sd, int stat, char* param)
374 {
375   time_t nowr;
376
377   nowr = CurrentTime - cli_since(&me);
378   send_reply(to, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
379              (nowr / 60) % 60, nowr % 60);
380   send_reply(to, RPL_STATSCONN, max_connection_count, max_client_count);
381 }
382
383 static void
384 stats_servers_verbose(struct Client* sptr, struct StatDesc* sd, int stat,
385                       char* param)
386 {
387   struct Client *acptr;
388   
389   /*
390    * lowercase 'v' is for human-readable,
391    * uppercase 'V' is for machine-readable
392    */
393   if (stat == 'v')
394     send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE,
395                "%-20s %-20s Flags Hops Numeric   Lag  RTT   Up Down "
396                "Clients/Max Proto %-10s :Info", "Servername", "Uplink",
397                "LinkTS");
398
399   for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr))
400   {
401     if (!IsServer(acptr) && !IsMe(acptr))
402       continue;
403     /* narrow search */
404     if (param && match(param, cli_name(acptr)))
405       continue;
406     send_reply(sptr, SND_EXPLICIT | RPL_STATSVERBOSE, stat == 'v' ?
407                "%-20s %-20s %c%c%c%c  %4i %s %-4i %5i %4i %4i %4i %5i %5i "
408                "P%-2i   %Tu :%s" :
409                "%s %s %c%c%c%c %i %s %i %i %i %i %i %i %i P%i %Tu :%s",
410                cli_name(acptr),
411                cli_name(cli_serv(acptr)->up),
412                IsBurst(acptr) ? 'B' : '-',
413                IsBurstAck(acptr) ? 'A' : '-',
414                IsHub(acptr) ? 'H' : '-',
415                IsService(acptr) ? 'S' : '-',
416                cli_hopcount(acptr),
417                NumServ(acptr),
418                base64toint(cli_yxx(acptr)),
419                cli_serv(acptr)->lag,
420                cli_serv(acptr)->asll_rtt,
421                cli_serv(acptr)->asll_to,
422                cli_serv(acptr)->asll_from,
423                cli_serv(acptr)->clients,
424                cli_serv(acptr)->nn_mask,
425                cli_serv(acptr)->prot,
426                cli_serv(acptr)->timestamp,
427                cli_info(acptr));
428   }
429 }
430
431 #ifdef DEBUGMODE
432 static void
433 stats_meminfo(struct Client* to, struct StatDesc* sd, int stat, char* param)
434 {
435   class_send_meminfo(to);
436   send_listinfo(to, 0);
437 }
438 #endif
439
440 static void
441 stats_help(struct Client* to, struct StatDesc* sd, int stat, char* param)
442 {
443   struct StatDesc *asd;
444
445   /* only if it's my user */
446   if (MyUser(to))
447     for (asd = statsinfo; asd->sd_c; asd++)
448       if (asd->sd_c != sd->sd_c) /* don't send the help for us */
449         sendcmdto_one(&me, CMD_NOTICE, to, "%C :%c - %s", to, asd->sd_c,
450                       asd->sd_desc);
451 }
452
453 /* This array of structures contains information about all single-character
454  * stats.  Struct StatDesc is defined in s_stats.h.
455  */
456 struct StatDesc statsinfo[] = {
457   { 'c', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_c,
458     stats_configured_links, CONF_SERVER,
459     "Remote server connection lines." },
460   { 'd', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_d,
461     stats_crule_list, 0,
462     "Dynamic routing configuration." },
463   { 'e', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_e,
464     stats_engine, 0,
465     "Report server event loop engine." },
466   { 'f', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_f,
467     feature_report, 0,
468     "Feature settings." },
469   { 'g', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_g,
470     gline_stats, 0,
471     "Global bans (G-lines)." },
472   { 'h', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_h,
473     stats_configured_links, (CONF_HUB | CONF_LEAF),
474     "Hubs information." },
475   { 'i', (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_i,
476     stats_access, CONF_CLIENT,
477     "Connection authorization lines." },
478   { 'j', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_j,
479     msgq_histogram, 0,
480     "Message length histogram." },
481   { 'k', (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_k,
482     stats_klines, 0,
483     "Local bans (K-Lines)." },
484   { 'l', (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS), 
485     FEAT_HIS_STATS_l,
486     stats_links, 0,
487     "Current connections information." },
488   { 'L', (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS), 
489     FEAT_HIS_STATS_L,
490     stats_modules, 0,
491     "Dynamicly loaded modules." },
492 #if 0
493   { 'M', (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_M,
494     stats_memtotal, 0,
495     "Memory allocation & leak monitoring." },
496 #endif
497   { 'm', (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_m,
498     stats_commands, 0,
499     "Message usage information." },
500   { 'o', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_o,
501     stats_configured_links, CONF_OPERATOR,
502     "Operator information." },
503   { 'p', (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_p,
504     show_ports, 0,
505     "Listening ports." },
506   { 'q', (STAT_FLAG_OPERONLY | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_q,
507     stats_quarantine, 0,
508     "Quarantined channels list." },
509   { 'R', (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_R,
510     stats_mapping, 0,
511     "Service mappings." },
512 #ifdef DEBUGMODE
513   { 'r', (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_r,
514     send_usage, 0,
515     "System resource usage (Debug only)." },
516 #endif
517   { 'T', (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_T,
518     motd_report, 0,
519     "Configured Message Of The Day files." },
520   { 't', (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_t,
521     tstats, 0,
522     "Local connection statistics (Total SND/RCV, etc)." },
523   { 'U', (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_U,
524     stats_configured_links, CONF_UWORLD,
525     "Service server & nick jupes information." },
526   { 'u', (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_u,
527     stats_uptime, 0,
528     "Current uptime & highest connection count." },
529   { 'v', (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_v,
530     stats_servers_verbose, 0,
531     "Verbose server information." },
532   { 'w', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_w,
533     calc_load, 0,
534     "Userload statistics." },
535 #ifdef DEBUGMODE
536   { 'x', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_x,
537     stats_meminfo, 0,
538     "List usage information (Debug only)." },
539 #endif
540   { 'y', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_y,
541     report_classes, 0,
542     "Connection classes." },
543   { 'z', STAT_FLAG_OPERFEAT, FEAT_HIS_STATS_z,
544     count_memory, 0,
545     "Memory/Structure allocation information." },
546   { '*', (STAT_FLAG_CASESENS | STAT_FLAG_VARPARAM), FEAT_LAST_F,
547     stats_help, 0,
548     "Send help for stats." },
549   { '\0', 0, FEAT_LAST_F, 0, 0, 0 }
550 };
551
552 /* This array is for mapping from characters to statistics descriptors */
553 struct StatDesc *statsmap[256];
554
555 /* Function to build the statsmap from the statsinfo array */
556 void
557 stats_init(void)
558 {
559   struct StatDesc *sd;
560   int i;
561
562   /* Make darn sure the statsmap array is initialized to all zeros */
563   for (i = 0; i < 256; i++)
564     statsmap[i] = 0;
565
566   /* Build the mapping */
567   for (sd = statsinfo; sd->sd_c; sd++)
568   {
569     if (sd->sd_flags & STAT_FLAG_CASESENS)
570       /* case sensitive character... */
571       statsmap[(int)sd->sd_c] = sd;
572     else
573     {
574       /* case insensitive--make sure to put in two entries */
575       statsmap[(int)ToLower((int)sd->sd_c)] = sd;
576       statsmap[(int)ToUpper((int)sd->sd_c)] = sd;
577     }
578   }
579 }