Author: Ghostwolf <foxxe@wtfs.net>
[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 "s_stats.h"
27 #include "class.h"
28 #include "client.h"
29 #include "ircd.h"
30 #include "ircd_chattr.h"
31 #include "ircd_log.h"
32 #include "ircd_reply.h"
33 #include "ircd_string.h"
34 #include "listener.h"
35 #include "match.h"
36 #include "msg.h"
37 #include "numeric.h"
38 #include "numnicks.h"
39 #include "s_conf.h"
40 #include "s_user.h"
41 #include "send.h"
42 #include "struct.h"
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <sys/time.h>
47
48
49 /*
50  * m_stats/s_stats
51  *
52  * Report configuration lines and other statistics from this
53  * server. 
54  *
55  * Note: The info is reported in the order the server uses
56  *       it--not reversed as in ircd.conf!
57  */
58
59 /*
60  *  Help info displayed when user provides no stats parameter. --Gte
61  */
62 const char *statsinfo[] = {
63     "The following statistics are available:",
64     "U - Service server & nick jupes information.",
65     "u - Current uptime & highest connection count.",
66     "p - Listening ports.",
67     "i - Connection authorisation lines.",
68     "y - Connection classes.",
69     "c - Remote server connection lines.",
70     "h - Hubs information.",
71     "d - Dynamic routing configuration.", 
72     "l - Current connections information.",
73     "g - Global bans (G-lines).",
74     "k - Local bans (K-Lines).",
75     "o - Operator information.", 
76     "e - Report server event loop engine.",
77     "f - Feature settings.",
78     "m - Message usage information.",
79     "t - Local connection statistics (Total SND/RCV, etc).", 
80     "w - Userload statistics.",
81     "M - Memory allocation & leak monitoring.", 
82     "z - Memory/Structure allocation information.",
83     "r - System resource usage (Debug only).", 
84     "x - List usage information (Debug only).",
85     0,
86 };
87
88 static unsigned int report_array[17][3] = {
89   {CONF_SERVER, RPL_STATSCLINE, 'C'},
90   {CONF_CLIENT, RPL_STATSILINE, 'I'},
91   {CONF_LEAF, RPL_STATSLLINE, 'L'},
92   {CONF_OPERATOR, RPL_STATSOLINE, 'O'},
93   {CONF_HUB, RPL_STATSHLINE, 'H'},
94   {CONF_LOCOP, RPL_STATSOLINE, 'o'},
95   {CONF_UWORLD, RPL_STATSULINE, 'U'},
96   {0, 0}
97 };
98
99 void report_configured_links(struct Client *sptr, int mask)
100 {
101   static char null[] = "<NULL>";
102   struct ConfItem *tmp;
103   unsigned int *p;
104   unsigned short int port;
105   char c, *host, *pass, *name;
106   
107
108   for (tmp = GlobalConfList; tmp; tmp = tmp->next) 
109   {
110     if ((tmp->status & mask))
111     {
112       for (p = &report_array[0][0]; *p; p += 3)
113         if (*p == tmp->status)
114           break;
115       if (!*p)
116         continue;
117       c = (char)*(p + 2);
118       host = BadPtr(tmp->host) ? null : tmp->host;
119       pass = BadPtr(tmp->passwd) ? null : tmp->passwd;
120       name = BadPtr(tmp->name) ? null : tmp->name;
121       port = tmp->port;
122       /*
123        * On K line the passwd contents can be
124        * displayed on STATS reply.    -Vesa
125        */
126       /* Special-case 'k' or 'K' lines as appropriate... -Kev */
127       if ((tmp->status & CONF_UWORLD))
128         send_reply(sptr, p[1], c, host, pass, name, port, get_conf_class(tmp));
129       else if ((tmp->status & (CONF_SERVER | CONF_HUB)))
130         send_reply(sptr, p[1], c, "*", name, port, get_conf_class(tmp));
131       else if ((tmp->status & CONF_CLIENT))
132       {
133         if(tmp->passwd && IsDigit(*tmp->passwd) && (!tmp->passwd[1] ||
134             (IsDigit(tmp->passwd[1]) && !tmp->passwd[2])))
135           send_reply(sptr, p[1], c, host, pass, name, port, get_conf_class(tmp));
136         else
137           send_reply(sptr, p[1], c, host, "*", name, port, get_conf_class(tmp));
138       }
139       else
140         send_reply(sptr, p[1], c, host, name, port, get_conf_class(tmp));
141     }
142   }
143 }
144
145 /*
146  *  {CONF_TLINES, RPL_STATSTLINE, 'T'},
147  *
148  * see now motd_report() in motd.c
149  */
150 /*  void report_motd_list(struct Client* to) */
151 /*  { */
152 /*    const struct MotdConf* conf = conf_get_motd_list(); */
153 /*    for ( ; conf; conf = conf->next) */
154 /*      send_reply(to, RPL_STATSTLINE, 'T', conf->hostmask, conf->path); */
155 /*  } */
156
157 /*
158  * {CONF_CRULEALL, RPL_STATSDLINE, 'D'},
159  * {CONF_CRULEAUTO, RPL_STATSDLINE, 'd'},
160  */
161 void report_crule_list(struct Client* to, int mask)
162 {
163   const struct CRuleConf* p = conf_get_crule_list();
164   for ( ; p; p = p->next) {
165     if (0 != (p->type & mask))
166       send_reply(to, RPL_STATSDLINE, (CRULE_ALL == p->type) ? 'D' : 'd', p->hostmask, p->rule);
167   }
168 }
169
170 /*
171  * {CONF_KILL, RPL_STATSKLINE, 'K'},
172  * {CONF_IPKILL, RPL_STATSKLINE, 'k'},
173  */
174 void report_deny_list(struct Client* to)
175 {
176   const struct DenyConf* p = conf_get_deny_list();
177   for ( ; p; p = p->next)
178     send_reply(to, RPL_STATSKLINE, (p->flags & DENY_FLAGS_IP) ? 'k' : 'K',
179                p->hostmask, p->message, p->usermask);
180 }
181
182 /* m_stats is so obnoxiously full of special cases that the different
183  * hunt_server() possiblites were becoming very messy. It now uses a
184  * switch() so as to be easier to read and update as params change. 
185  * -Ghostwolf 
186  *
187  * 2.10.11: Don't check for the oper limitation if it's not our local server.
188  *          thusly once all the hubs have upgraded local opers will be able
189  *          to remote stats anywhere on the network.
190  */
191 int hunt_stats(struct Client* cptr, struct Client* sptr, int parc, char* parv[], char stat, int MustBeOper)
192 {
193   switch (stat)
194   {
195       /* open to all, standard # of params */
196     case 'U':
197     case 'u':
198     case 'F':
199     case 'f':
200       return hunt_server_cmd(sptr, CMD_STATS, cptr, MustBeOper, "%s :%C", 2,
201                              parc, parv);
202
203     /* open to all, varying # of params */
204     case 'k':
205     case 'K':
206     case 'i':
207     case 'I':
208     case 'p':
209     case 'P':
210     {
211       if (parc > 3)
212         return hunt_server_cmd(sptr, CMD_STATS, cptr, MustBeOper, "%s %C :%s",
213                                2, parc, parv);
214       else
215         return hunt_server_cmd(sptr, CMD_STATS, cptr, MustBeOper, "%s :%C", 2,
216                                parc, parv);
217     }
218
219       /* oper only, varying # of params */
220     case 'l':
221     case 'L':
222     case 'M':
223     {
224       if (parc == 4)
225         return hunt_server_cmd(sptr, CMD_STATS, cptr,
226                                MyUser(sptr) ? 1 : MustBeOper, "%s %C :%s", 2,
227                                parc, parv);
228       else if (parc > 4)
229         return hunt_server_cmd(sptr, CMD_STATS, cptr,
230                                MyUser(sptr) ? 1 : MustBeOper, "%s %C %s :%s",
231                                2, parc, parv);
232       else 
233         return hunt_server_cmd(sptr, CMD_STATS, cptr,
234                                MyUser(sptr) ? 1 : MustBeOper, "%s :%C", 2,
235                                parc, parv);
236     }
237
238       /* oper only, standard # of params */
239     default:
240       return hunt_server_cmd(sptr, CMD_STATS, cptr,
241                              MyUser(sptr) ? 1 : MustBeOper, "%s :%C", 2, parc,
242                              parv);
243   }
244 }
245