1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2015 ircd-hybrid development team |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file m_stats.c |
23 |
* \brief Includes required functions for processing the STATS command. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#include "list.h" |
29 |
#include "client.h" |
30 |
#include "irc_string.h" |
31 |
#include "ircd.h" |
32 |
#include "listener.h" |
33 |
#include "conf.h" |
34 |
#include "conf_class.h" |
35 |
#include "hostmask.h" |
36 |
#include "numeric.h" |
37 |
#include "send.h" |
38 |
#include "fdlist.h" |
39 |
#include "misc.h" |
40 |
#include "server.h" |
41 |
#include "user.h" |
42 |
#include "event.h" |
43 |
#include "dbuf.h" |
44 |
#include "parse.h" |
45 |
#include "modules.h" |
46 |
#include "resv.h" |
47 |
#include "whowas.h" |
48 |
#include "watch.h" |
49 |
#include "res.h" |
50 |
#include "motd.h" |
51 |
#include "ipcache.h" |
52 |
|
53 |
|
54 |
static const struct shared_flags |
55 |
{ |
56 |
const unsigned int type; |
57 |
const unsigned char letter; |
58 |
} flag_table[] = { |
59 |
{ SHARED_KLINE, 'K' }, |
60 |
{ SHARED_UNKLINE, 'U' }, |
61 |
{ SHARED_XLINE, 'X' }, |
62 |
{ SHARED_UNXLINE, 'Y' }, |
63 |
{ SHARED_RESV, 'Q' }, |
64 |
{ SHARED_UNRESV, 'R' }, |
65 |
{ SHARED_LOCOPS, 'L' }, |
66 |
{ SHARED_DLINE, 'D' }, |
67 |
{ SHARED_UNDLINE, 'E' }, |
68 |
{ 0, '\0' } |
69 |
}; |
70 |
|
71 |
/* |
72 |
* inputs - pointer to client requesting confitem report |
73 |
* - ConfType to report |
74 |
* output - none |
75 |
* side effects - |
76 |
*/ |
77 |
static void |
78 |
report_confitem_types(struct Client *source_p, enum maskitem_type type) |
79 |
{ |
80 |
const dlink_node *node = NULL; |
81 |
const struct MaskItem *conf = NULL; |
82 |
char buf[IRCD_BUFSIZE]; |
83 |
char *p = NULL; |
84 |
|
85 |
switch (type) |
86 |
{ |
87 |
case CONF_XLINE: |
88 |
DLINK_FOREACH(node, gecos_items.head) |
89 |
{ |
90 |
conf = node->data; |
91 |
sendto_one_numeric(source_p, &me, RPL_STATSXLINE, |
92 |
conf->until ? 'x': 'X', conf->count, |
93 |
conf->name, conf->reason); |
94 |
} |
95 |
|
96 |
break; |
97 |
|
98 |
case CONF_SHARED: |
99 |
DLINK_FOREACH(node, shared_items.head) |
100 |
{ |
101 |
conf = node->data; |
102 |
p = buf; |
103 |
|
104 |
*p++ = 'c'; |
105 |
|
106 |
for (const struct shared_flags *shared = flag_table; shared->type; ++shared) |
107 |
if (shared->type & conf->flags) |
108 |
*p++ = shared->letter; |
109 |
else |
110 |
*p++ = ToLower(shared->letter); |
111 |
|
112 |
*p = '\0'; |
113 |
|
114 |
sendto_one_numeric(source_p, &me, RPL_STATSULINE, conf->name, |
115 |
conf->user ? conf->user: "*", |
116 |
conf->host ? conf->host: "*", buf); |
117 |
} |
118 |
|
119 |
DLINK_FOREACH(node, cluster_items.head) |
120 |
{ |
121 |
conf = node->data; |
122 |
p = buf; |
123 |
|
124 |
*p++ = 'C'; |
125 |
|
126 |
for (const struct shared_flags *shared = flag_table; shared->type; ++shared) |
127 |
if (shared->type & conf->flags) |
128 |
*p++ = shared->letter; |
129 |
else |
130 |
*p++ = ToLower(shared->letter); |
131 |
|
132 |
*p = '\0'; |
133 |
|
134 |
sendto_one_numeric(source_p, &me, RPL_STATSULINE, conf->name, "*", "*", buf); |
135 |
} |
136 |
|
137 |
break; |
138 |
|
139 |
case CONF_OPER: |
140 |
DLINK_FOREACH(node, operator_items.head) |
141 |
{ |
142 |
conf = node->data; |
143 |
|
144 |
/* Don't allow non opers to see oper privs */ |
145 |
if (HasUMode(source_p, UMODE_OPER)) |
146 |
sendto_one_numeric(source_p, &me, RPL_STATSOLINE, 'O', conf->user, conf->host, |
147 |
conf->name, oper_privs_as_string(conf->port), |
148 |
conf->class->name); |
149 |
else |
150 |
sendto_one_numeric(source_p, &me, RPL_STATSOLINE, 'O', conf->user, conf->host, |
151 |
conf->name, "0", conf->class->name); |
152 |
} |
153 |
|
154 |
break; |
155 |
|
156 |
case CONF_SERVICE: |
157 |
DLINK_FOREACH(node, service_items.head) |
158 |
{ |
159 |
conf = node->data; |
160 |
sendto_one_numeric(source_p, &me, RPL_STATSSERVICE, 'S', "*", conf->name, 0, 0); |
161 |
} |
162 |
|
163 |
break; |
164 |
|
165 |
case CONF_SERVER: |
166 |
DLINK_FOREACH(node, server_items.head) |
167 |
{ |
168 |
p = buf; |
169 |
conf = node->data; |
170 |
|
171 |
buf[0] = '\0'; |
172 |
|
173 |
if (IsConfAllowAutoConn(conf)) |
174 |
*p++ = 'A'; |
175 |
if (IsConfSSL(conf)) |
176 |
*p++ = 'S'; |
177 |
if (buf[0] == '\0') |
178 |
*p++ = '*'; |
179 |
|
180 |
*p = '\0'; |
181 |
|
182 |
/* |
183 |
* Allow admins to see actual ips unless hide_server_ips is enabled |
184 |
*/ |
185 |
if (!ConfigServerHide.hide_server_ips && HasUMode(source_p, UMODE_ADMIN)) |
186 |
sendto_one_numeric(source_p, &me, RPL_STATSCLINE, 'C', conf->host, |
187 |
buf, conf->name, conf->port, |
188 |
conf->class->name); |
189 |
else |
190 |
sendto_one_numeric(source_p, &me, RPL_STATSCLINE, 'C', |
191 |
"*@127.0.0.1", buf, conf->name, conf->port, |
192 |
conf->class->name); |
193 |
} |
194 |
|
195 |
break; |
196 |
|
197 |
default: |
198 |
break; |
199 |
} |
200 |
} |
201 |
|
202 |
/* report_resv() |
203 |
* |
204 |
* inputs - pointer to client pointer to report to. |
205 |
* output - NONE |
206 |
* side effects - report all resvs to client. |
207 |
*/ |
208 |
static void |
209 |
report_resv(struct Client *source_p) |
210 |
{ |
211 |
const dlink_node *node = NULL; |
212 |
|
213 |
DLINK_FOREACH(node, cresv_items.head) |
214 |
{ |
215 |
const struct MaskItem *conf = node->data; |
216 |
|
217 |
sendto_one_numeric(source_p, &me, RPL_STATSQLINE, |
218 |
conf->until ? 'q' : 'Q', conf->count, |
219 |
conf->name, conf->reason); |
220 |
} |
221 |
|
222 |
DLINK_FOREACH(node, nresv_items.head) |
223 |
{ |
224 |
const struct MaskItem *conf = node->data; |
225 |
|
226 |
sendto_one_numeric(source_p, &me, RPL_STATSQLINE, |
227 |
conf->until ? 'q' : 'Q', conf->count, |
228 |
conf->name, conf->reason); |
229 |
} |
230 |
} |
231 |
|
232 |
/* |
233 |
* This is part of the STATS replies. There is no offical numeric for this |
234 |
* since this isn't an official command, in much the same way as HASH isn't. |
235 |
* It is also possible that some systems wont support this call or have |
236 |
* different field names for "struct rusage". |
237 |
* -avalon |
238 |
*/ |
239 |
static void |
240 |
stats_usage(struct Client *source_p, int parc, char *parv[]) |
241 |
{ |
242 |
struct rusage rus; |
243 |
time_t secs; |
244 |
time_t rup; |
245 |
#ifdef hz |
246 |
# define hzz hz |
247 |
#else |
248 |
# ifdef HZ |
249 |
# define hzz HZ |
250 |
# else |
251 |
int hzz = 1; |
252 |
# endif |
253 |
#endif |
254 |
|
255 |
if (getrusage(RUSAGE_SELF, &rus) == -1) |
256 |
{ |
257 |
sendto_one_notice(source_p, &me, ":Getruseage error: %s", |
258 |
strerror(errno)); |
259 |
return; |
260 |
} |
261 |
|
262 |
secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec; |
263 |
|
264 |
if (secs == 0) |
265 |
secs = 1; |
266 |
|
267 |
rup = (CurrentTime - me.connection->since) * hzz; |
268 |
|
269 |
if (rup == 0) |
270 |
rup = 1; |
271 |
|
272 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
273 |
"R :CPU Secs %d:%02d User %d:%02d System %d:%02d", |
274 |
(int)(secs/60), (int)(secs%60), |
275 |
(int)(rus.ru_utime.tv_sec/60), (int)(rus.ru_utime.tv_sec%60), |
276 |
(int)(rus.ru_stime.tv_sec/60), (int)(rus.ru_stime.tv_sec%60)); |
277 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
278 |
"R :RSS %ld ShMem %ld Data %ld Stack %ld", |
279 |
rus.ru_maxrss, |
280 |
(rus.ru_ixrss / rup), (rus.ru_idrss / rup), |
281 |
(rus.ru_isrss / rup)); |
282 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
283 |
"R :Swaps %d Reclaims %d Faults %d", |
284 |
(int)rus.ru_nswap, |
285 |
(int)rus.ru_minflt, (int)rus.ru_majflt); |
286 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
287 |
"R :Block in %d out %d", |
288 |
(int)rus.ru_inblock, |
289 |
(int)rus.ru_oublock); |
290 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
291 |
"R :Msg Rcv %d Send %d", |
292 |
(int)rus.ru_msgrcv, |
293 |
(int)rus.ru_msgsnd); |
294 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
295 |
"R :Signals %d Context Vol. %d Invol %d", |
296 |
(int)rus.ru_nsignals, |
297 |
(int)rus.ru_nvcsw, (int)rus.ru_nivcsw); |
298 |
} |
299 |
|
300 |
static void |
301 |
stats_memory(struct Client *source_p, int parc, char *parv[]) |
302 |
{ |
303 |
const dlink_node *gptr = NULL; |
304 |
const dlink_node *dlink = NULL; |
305 |
|
306 |
unsigned int local_client_conf_count = 0; /* local client conf links */ |
307 |
unsigned int users_counted = 0; /* user structs */ |
308 |
|
309 |
unsigned int channel_members = 0; |
310 |
unsigned int channel_invites = 0; |
311 |
unsigned int channel_bans = 0; |
312 |
unsigned int channel_except = 0; |
313 |
unsigned int channel_invex = 0; |
314 |
|
315 |
unsigned int wwu = 0; /* whowas users */ |
316 |
unsigned int class_count = 0; /* classes */ |
317 |
unsigned int aways_counted = 0; |
318 |
unsigned int number_ips_stored = 0; /* number of ip addresses hashed */ |
319 |
|
320 |
size_t channel_memory = 0; |
321 |
size_t channel_ban_memory = 0; |
322 |
size_t channel_except_memory = 0; |
323 |
size_t channel_invex_memory = 0; |
324 |
|
325 |
unsigned int safelist_count = 0; |
326 |
size_t safelist_memory = 0; |
327 |
|
328 |
size_t wwm = 0; /* whowas array memory used */ |
329 |
size_t conf_memory = 0; /* memory used by conf lines */ |
330 |
size_t mem_ips_stored = 0; /* memory used by ip address hash */ |
331 |
|
332 |
size_t total_channel_memory = 0; |
333 |
size_t totww = 0; |
334 |
|
335 |
unsigned int local_client_count = 0; |
336 |
unsigned int remote_client_count = 0; |
337 |
|
338 |
size_t local_client_memory_used = 0; |
339 |
size_t remote_client_memory_used = 0; |
340 |
|
341 |
size_t total_memory = 0; |
342 |
unsigned int topic_count = 0; |
343 |
|
344 |
unsigned int watch_list_headers = 0; /* watchlist headers */ |
345 |
unsigned int watch_list_entries = 0; /* watchlist entries */ |
346 |
size_t watch_list_memory = 0; /* watchlist memory used */ |
347 |
|
348 |
|
349 |
DLINK_FOREACH(gptr, global_client_list.head) |
350 |
{ |
351 |
const struct Client *target_p = gptr->data; |
352 |
|
353 |
if (MyConnect(target_p)) |
354 |
{ |
355 |
++local_client_count; |
356 |
local_client_conf_count += dlink_list_length(&target_p->connection->confs); |
357 |
watch_list_entries += dlink_list_length(&target_p->connection->watches); |
358 |
} |
359 |
else |
360 |
++remote_client_count; |
361 |
|
362 |
if (IsClient(target_p)) |
363 |
{ |
364 |
++users_counted; |
365 |
|
366 |
if (target_p->away[0]) |
367 |
++aways_counted; |
368 |
} |
369 |
} |
370 |
|
371 |
/* Count up all channels, ban lists, except lists, Invex lists */ |
372 |
channel_memory = dlink_list_length(&channel_list) * sizeof(struct Channel); |
373 |
|
374 |
DLINK_FOREACH(gptr, channel_list.head) |
375 |
{ |
376 |
const struct Channel *chptr = gptr->data; |
377 |
|
378 |
channel_members += dlink_list_length(&chptr->members); |
379 |
channel_invites += dlink_list_length(&chptr->invites); |
380 |
|
381 |
if (chptr->topic[0]) |
382 |
++topic_count; |
383 |
|
384 |
channel_bans += dlink_list_length(&chptr->banlist); |
385 |
channel_ban_memory += dlink_list_length(&chptr->banlist) * sizeof(struct Ban); |
386 |
|
387 |
channel_except += dlink_list_length(&chptr->exceptlist); |
388 |
channel_except_memory += dlink_list_length(&chptr->exceptlist) * sizeof(struct Ban); |
389 |
|
390 |
channel_invex += dlink_list_length(&chptr->invexlist); |
391 |
channel_invex_memory += dlink_list_length(&chptr->invexlist) * sizeof(struct Ban); |
392 |
} |
393 |
|
394 |
if ((safelist_count = dlink_list_length(&listing_client_list))) |
395 |
{ |
396 |
safelist_memory = safelist_count * sizeof(struct ListTask); |
397 |
DLINK_FOREACH(gptr, listing_client_list.head) |
398 |
{ |
399 |
const struct Client *acptr = gptr->data; |
400 |
|
401 |
DLINK_FOREACH(dlink, acptr->connection->list_task->show_mask.head) |
402 |
safelist_memory += strlen(dlink->data); |
403 |
|
404 |
DLINK_FOREACH(dlink, acptr->connection->list_task->hide_mask.head) |
405 |
safelist_memory += strlen(dlink->data); |
406 |
} |
407 |
} |
408 |
|
409 |
#if 0 |
410 |
/* XXX THIS has to be fixed !!!! -db */ |
411 |
/* count up all config items */ |
412 |
DLINK_FOREACH(dlink, ConfigItemList.head) |
413 |
{ |
414 |
aconf = dlink->data; |
415 |
conf_memory += aconf->host ? strlen(aconf->host)+1 : 0; |
416 |
conf_memory += aconf->passwd ? strlen(aconf->passwd)+1 : 0; |
417 |
conf_memory += aconf->name ? strlen(aconf->name)+1 : 0; |
418 |
conf_memory += sizeof(struct AccessItem); |
419 |
} |
420 |
#endif |
421 |
/* count up all classes */ |
422 |
class_count = dlink_list_length(class_get_list()); |
423 |
|
424 |
whowas_count_memory(&wwu, &wwm); |
425 |
watch_count_memory(&watch_list_headers, &watch_list_memory); |
426 |
|
427 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
428 |
"z :WATCH headers %u(%zu) entries %d(%u)", |
429 |
watch_list_headers, |
430 |
watch_list_memory, watch_list_entries, |
431 |
watch_list_entries * sizeof(dlink_node) * 2); |
432 |
|
433 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
434 |
"z :Clients %u(%u)", |
435 |
users_counted, |
436 |
(users_counted * sizeof(struct Client))); |
437 |
|
438 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
439 |
"z :User aways %u", aways_counted); |
440 |
|
441 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
442 |
"z :Attached confs %u(%zu)", |
443 |
local_client_conf_count, |
444 |
local_client_conf_count * sizeof(dlink_node)); |
445 |
|
446 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
447 |
"z :Resv channels %u(%zu) nicks %u(%zu)", |
448 |
dlink_list_length(&cresv_items), |
449 |
dlink_list_length(&cresv_items) * sizeof(struct MaskItem), |
450 |
dlink_list_length(&nresv_items), |
451 |
dlink_list_length(&nresv_items) * sizeof(struct MaskItem)); |
452 |
|
453 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
454 |
"z :Classes %u(%zu)", |
455 |
class_count, class_count * sizeof(struct ClassItem)); |
456 |
|
457 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
458 |
"z :Channels %u(%zu) Topics %u(%u)", |
459 |
dlink_list_length(&channel_list), |
460 |
channel_memory, topic_count, topic_count * |
461 |
(TOPICLEN + 1 + USERHOST_REPLYLEN)); |
462 |
|
463 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
464 |
"z :Bans %u(%zu)", |
465 |
channel_bans, channel_ban_memory); |
466 |
|
467 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
468 |
"z :Exceptions %u(%zu)", |
469 |
channel_except, channel_except_memory); |
470 |
|
471 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
472 |
"z :Invex %u(%zu)", |
473 |
channel_invex, channel_invex_memory); |
474 |
|
475 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
476 |
"z :Channel members %u(%zu) invites %u(%zu)", |
477 |
channel_members, |
478 |
channel_members * sizeof(struct Membership), |
479 |
channel_invites, |
480 |
channel_invites * sizeof(dlink_node) * 2); |
481 |
|
482 |
total_channel_memory = channel_memory + channel_ban_memory + |
483 |
channel_members * sizeof(struct Membership) + |
484 |
(channel_invites * sizeof(dlink_node) * 2); |
485 |
|
486 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
487 |
"z :Safelist %u(%zu)", |
488 |
safelist_count, safelist_memory); |
489 |
|
490 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
491 |
"z :Whowas users %u(%zu)", |
492 |
wwu, wwu * sizeof(struct Client)); |
493 |
|
494 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
495 |
"z :Whowas array %u(%zu)", |
496 |
NICKNAMEHISTORYLENGTH, wwm); |
497 |
|
498 |
totww = wwu * sizeof(struct Client) + wwm; |
499 |
|
500 |
motd_memory_count(source_p); |
501 |
ipcache_get_stats(&number_ips_stored, &mem_ips_stored); |
502 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
503 |
"z :iphash %u(%zu)", |
504 |
number_ips_stored, mem_ips_stored); |
505 |
|
506 |
total_memory = totww + total_channel_memory + conf_memory + class_count * |
507 |
sizeof(struct ClassItem); |
508 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
509 |
"z :Total: whowas %zu channel %zu conf %zu", |
510 |
totww, total_channel_memory, conf_memory); |
511 |
|
512 |
local_client_memory_used = local_client_count*(sizeof(struct Client) + sizeof(struct Connection)); |
513 |
total_memory += local_client_memory_used; |
514 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
515 |
"z :Local client Memory in use: %u(%zu)", |
516 |
local_client_count, local_client_memory_used); |
517 |
|
518 |
remote_client_memory_used = remote_client_count * sizeof(struct Client); |
519 |
total_memory += remote_client_memory_used; |
520 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
521 |
"z :Remote client Memory in use: %u(%zu)", |
522 |
remote_client_count, remote_client_memory_used); |
523 |
|
524 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
525 |
"z :TOTAL: %zu", |
526 |
me.name, RPL_STATSDEBUG, source_p->name, |
527 |
total_memory); |
528 |
} |
529 |
|
530 |
static void |
531 |
stats_dns_servers(struct Client *source_p, int parc, char *parv[]) |
532 |
{ |
533 |
report_dns_servers(source_p); |
534 |
} |
535 |
|
536 |
static void |
537 |
stats_connect(struct Client *source_p, int parc, char *parv[]) |
538 |
{ |
539 |
report_confitem_types(source_p, CONF_SERVER); |
540 |
} |
541 |
|
542 |
/* stats_deny() |
543 |
* |
544 |
* input - client to report to |
545 |
* output - none |
546 |
* side effects - client is given dline list. |
547 |
*/ |
548 |
static void |
549 |
stats_deny(struct Client *source_p, int parc, char *parv[]) |
550 |
{ |
551 |
const struct MaskItem *conf = NULL; |
552 |
const dlink_node *node = NULL; |
553 |
|
554 |
for (unsigned int i = 0; i < ATABLE_SIZE; ++i) |
555 |
{ |
556 |
DLINK_FOREACH(node, atable[i].head) |
557 |
{ |
558 |
const struct AddressRec *arec = node->data; |
559 |
|
560 |
if (arec->type != CONF_DLINE) |
561 |
continue; |
562 |
|
563 |
conf = arec->conf; |
564 |
|
565 |
/* Don't report a temporary dline as permanent dline */ |
566 |
if (conf->until) |
567 |
continue; |
568 |
|
569 |
sendto_one_numeric(source_p, &me, RPL_STATSDLINE, 'D', conf->host, conf->reason); |
570 |
} |
571 |
} |
572 |
} |
573 |
|
574 |
/* stats_tdeny() |
575 |
* |
576 |
* input - client to report to |
577 |
* output - none |
578 |
* side effects - client is given dline list. |
579 |
*/ |
580 |
static void |
581 |
stats_tdeny(struct Client *source_p, int parc, char *parv[]) |
582 |
{ |
583 |
const struct MaskItem *conf = NULL; |
584 |
const dlink_node *node = NULL; |
585 |
|
586 |
for (unsigned int i = 0; i < ATABLE_SIZE; ++i) |
587 |
{ |
588 |
DLINK_FOREACH(node, atable[i].head) |
589 |
{ |
590 |
const struct AddressRec *arec = node->data; |
591 |
|
592 |
if (arec->type != CONF_DLINE) |
593 |
continue; |
594 |
|
595 |
conf = arec->conf; |
596 |
|
597 |
/* Don't report a permanent dline as temporary dline */ |
598 |
if (!conf->until) |
599 |
continue; |
600 |
|
601 |
sendto_one_numeric(source_p, &me, RPL_STATSDLINE, 'd', conf->host, conf->reason); |
602 |
} |
603 |
} |
604 |
} |
605 |
|
606 |
/* stats_exempt() |
607 |
* |
608 |
* input - client to report to |
609 |
* output - none |
610 |
* side effects - client is given list of exempt blocks |
611 |
*/ |
612 |
static void |
613 |
stats_exempt(struct Client *source_p, int parc, char *parv[]) |
614 |
{ |
615 |
const struct MaskItem *conf = NULL; |
616 |
const dlink_node *node = NULL; |
617 |
|
618 |
if (ConfigGeneral.stats_e_disabled) |
619 |
{ |
620 |
sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES); |
621 |
return; |
622 |
} |
623 |
|
624 |
for (unsigned int i = 0; i < ATABLE_SIZE; ++i) |
625 |
{ |
626 |
DLINK_FOREACH(node, atable[i].head) |
627 |
{ |
628 |
const struct AddressRec *arec = node->data; |
629 |
|
630 |
if (arec->type != CONF_EXEMPT) |
631 |
continue; |
632 |
|
633 |
conf = arec->conf; |
634 |
sendto_one_numeric(source_p, &me, RPL_STATSDLINE, 'e', conf->host, ""); |
635 |
} |
636 |
} |
637 |
} |
638 |
|
639 |
static void |
640 |
stats_events(struct Client *source_p, int parc, char *parv[]) |
641 |
{ |
642 |
const dlink_node *node; |
643 |
|
644 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
645 |
"E :Operation Next Execution"); |
646 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
647 |
"E :---------------------------------------------"); |
648 |
|
649 |
DLINK_FOREACH(node, event_get_list()->head) |
650 |
{ |
651 |
const struct event *ev = node->data; |
652 |
|
653 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
654 |
"E :%-30s %-4d seconds", |
655 |
ev->name, |
656 |
(int)(ev->next - CurrentTime)); |
657 |
} |
658 |
} |
659 |
|
660 |
static void |
661 |
stats_hubleaf(struct Client *source_p, int parc, char *parv[]) |
662 |
{ |
663 |
const dlink_node *node = NULL, *dptr = NULL; |
664 |
|
665 |
DLINK_FOREACH(node, server_items.head) |
666 |
{ |
667 |
const struct MaskItem *conf = node->data; |
668 |
|
669 |
DLINK_FOREACH(dptr, conf->hub_list.head) |
670 |
sendto_one_numeric(source_p, &me, RPL_STATSHLINE, 'H', dptr->data, conf->name, 0, "*"); |
671 |
} |
672 |
|
673 |
DLINK_FOREACH(node, server_items.head) |
674 |
{ |
675 |
const struct MaskItem *conf = node->data; |
676 |
|
677 |
DLINK_FOREACH(dptr, conf->leaf_list.head) |
678 |
sendto_one_numeric(source_p, &me, RPL_STATSLLINE, 'L', dptr->data, conf->name, 0, "*"); |
679 |
} |
680 |
} |
681 |
|
682 |
/* |
683 |
* show_iline_prefix() |
684 |
* |
685 |
* inputs - pointer to struct Client requesting output |
686 |
* - pointer to struct MaskItem |
687 |
* - name to which iline prefix will be prefixed to |
688 |
* output - pointer to static string with prefixes listed in ascii form |
689 |
* side effects - NONE |
690 |
*/ |
691 |
static const char * |
692 |
show_iline_prefix(const struct Client *source_p, const struct MaskItem *conf) |
693 |
{ |
694 |
static char prefix_of_host[USERLEN + 16]; |
695 |
char *prefix_ptr = prefix_of_host; |
696 |
|
697 |
if (IsConfWebIRC(conf)) |
698 |
*prefix_ptr++ = '<'; |
699 |
if (IsNoTilde(conf)) |
700 |
*prefix_ptr++ = '-'; |
701 |
if (IsNeedIdentd(conf)) |
702 |
*prefix_ptr++ = '+'; |
703 |
if (!IsNeedPassword(conf)) |
704 |
*prefix_ptr++ = '&'; |
705 |
if (IsConfExemptResv(conf)) |
706 |
*prefix_ptr++ = '$'; |
707 |
if (IsConfDoSpoofIp(conf)) |
708 |
*prefix_ptr++ = '='; |
709 |
if (HasUMode(source_p, UMODE_OPER)) |
710 |
{ |
711 |
if (IsConfExemptKline(conf)) |
712 |
*prefix_ptr++ = '^'; |
713 |
if (IsConfExemptXline(conf)) |
714 |
*prefix_ptr++ = '!'; |
715 |
if (IsConfExemptLimits(conf)) |
716 |
*prefix_ptr++ = '>'; |
717 |
} |
718 |
|
719 |
if (IsConfCanFlood(conf)) |
720 |
*prefix_ptr++ = '|'; |
721 |
|
722 |
strlcpy(prefix_ptr, conf->user, USERLEN+1); |
723 |
|
724 |
return prefix_of_host; |
725 |
} |
726 |
|
727 |
static void |
728 |
report_auth(struct Client *source_p, int parc, char *parv[]) |
729 |
{ |
730 |
const struct MaskItem *conf = NULL; |
731 |
const dlink_node *node = NULL; |
732 |
|
733 |
for (unsigned int i = 0; i < ATABLE_SIZE; ++i) |
734 |
{ |
735 |
DLINK_FOREACH(node, atable[i].head) |
736 |
{ |
737 |
const struct AddressRec *arec = node->data; |
738 |
|
739 |
if (arec->type != CONF_CLIENT) |
740 |
continue; |
741 |
|
742 |
conf = arec->conf; |
743 |
|
744 |
if (!HasUMode(source_p, UMODE_OPER) && IsConfDoSpoofIp(conf)) |
745 |
continue; |
746 |
|
747 |
sendto_one_numeric(source_p, &me, RPL_STATSILINE, 'I', |
748 |
conf->name == NULL ? "*" : conf->name, |
749 |
show_iline_prefix(source_p, conf), |
750 |
conf->host, conf->port, |
751 |
conf->class->name); |
752 |
} |
753 |
} |
754 |
} |
755 |
|
756 |
static void |
757 |
stats_auth(struct Client *source_p, int parc, char *parv[]) |
758 |
{ |
759 |
/* Oper only, if unopered, return ERR_NOPRIVILEGES */ |
760 |
if (ConfigGeneral.stats_i_oper_only == 2 && !HasUMode(source_p, UMODE_OPER)) |
761 |
sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES); |
762 |
|
763 |
/* If unopered, only return matching auth blocks */ |
764 |
else if (ConfigGeneral.stats_i_oper_only == 1 && !HasUMode(source_p, UMODE_OPER)) |
765 |
{ |
766 |
const struct MaskItem *conf = NULL; |
767 |
|
768 |
if (MyConnect(source_p)) |
769 |
conf = find_conf_by_address(source_p->host, |
770 |
&source_p->connection->ip, CONF_CLIENT, |
771 |
source_p->connection->aftype, |
772 |
source_p->username, |
773 |
source_p->connection->password, 1); |
774 |
else |
775 |
conf = find_conf_by_address(source_p->host, NULL, CONF_CLIENT, 0, |
776 |
source_p->username, NULL, 1); |
777 |
|
778 |
if (conf == NULL) |
779 |
return; |
780 |
|
781 |
sendto_one_numeric(source_p, &me, RPL_STATSILINE, |
782 |
'I', "*", show_iline_prefix(source_p, conf), |
783 |
conf->host, conf->port, |
784 |
conf->class->name); |
785 |
} |
786 |
else /* They are opered, or allowed to see all auth blocks */ |
787 |
report_auth(source_p, 0, NULL); |
788 |
} |
789 |
|
790 |
/* report_Klines() |
791 |
* Inputs: Client to report to, |
792 |
* type(==0 for perm, !=0 for temporary) |
793 |
* mask |
794 |
* Output: None |
795 |
* Side effects: Reports configured K(or k)-lines to source_p. |
796 |
*/ |
797 |
static void |
798 |
report_Klines(struct Client *source_p, int tkline) |
799 |
{ |
800 |
const struct MaskItem *conf = NULL; |
801 |
const dlink_node *node = NULL; |
802 |
char c = '\0'; |
803 |
|
804 |
if (tkline) |
805 |
c = 'k'; |
806 |
else |
807 |
c = 'K'; |
808 |
|
809 |
for (unsigned int i = 0; i < ATABLE_SIZE; ++i) |
810 |
{ |
811 |
DLINK_FOREACH(node, atable[i].head) |
812 |
{ |
813 |
const struct AddressRec *arec = node->data; |
814 |
|
815 |
if (arec->type != CONF_KLINE) |
816 |
continue; |
817 |
|
818 |
conf = arec->conf; |
819 |
|
820 |
if ((!tkline && conf->until) || |
821 |
(tkline && !conf->until)) |
822 |
continue; |
823 |
|
824 |
sendto_one_numeric(source_p, &me, RPL_STATSKLINE, c, conf->host, conf->user, |
825 |
conf->reason); |
826 |
} |
827 |
} |
828 |
} |
829 |
|
830 |
static void |
831 |
stats_tklines(struct Client *source_p, int parc, char *parv[]) |
832 |
{ |
833 |
/* Oper only, if unopered, return ERR_NOPRIVILEGES */ |
834 |
if (ConfigGeneral.stats_k_oper_only == 2 && !HasUMode(source_p, UMODE_OPER)) |
835 |
sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES); |
836 |
|
837 |
/* If unopered, only return matching klines */ |
838 |
else if (ConfigGeneral.stats_k_oper_only == 1 && !HasUMode(source_p, UMODE_OPER)) |
839 |
{ |
840 |
const struct MaskItem *conf = NULL; |
841 |
|
842 |
if (MyConnect(source_p)) |
843 |
conf = find_conf_by_address(source_p->host, |
844 |
&source_p->connection->ip, CONF_KLINE, |
845 |
source_p->connection->aftype, |
846 |
source_p->username, NULL, 1); |
847 |
else |
848 |
conf = find_conf_by_address(source_p->host, NULL, CONF_KLINE, 0, |
849 |
source_p->username, NULL, 1); |
850 |
|
851 |
if (!conf) |
852 |
return; |
853 |
|
854 |
/* Don't report a permanent kline as temporary kline */ |
855 |
if (!conf->until) |
856 |
return; |
857 |
|
858 |
sendto_one_numeric(source_p, &me, RPL_STATSKLINE, 'k', |
859 |
conf->host, conf->user, conf->reason); |
860 |
} |
861 |
else /* They are opered, or allowed to see all klines */ |
862 |
report_Klines(source_p, 1); |
863 |
} |
864 |
|
865 |
static void |
866 |
stats_klines(struct Client *source_p, int parc, char *parv[]) |
867 |
{ |
868 |
/* Oper only, if unopered, return ERR_NOPRIVILEGES */ |
869 |
if (ConfigGeneral.stats_k_oper_only == 2 && !HasUMode(source_p, UMODE_OPER)) |
870 |
sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES); |
871 |
|
872 |
/* If unopered, only return matching klines */ |
873 |
else if (ConfigGeneral.stats_k_oper_only == 1 && !HasUMode(source_p, UMODE_OPER)) |
874 |
{ |
875 |
const struct MaskItem *conf = NULL; |
876 |
|
877 |
/* Search for a kline */ |
878 |
if (MyConnect(source_p)) |
879 |
conf = find_conf_by_address(source_p->host, |
880 |
&source_p->connection->ip, CONF_KLINE, |
881 |
source_p->connection->aftype, |
882 |
source_p->username, NULL, 0); |
883 |
else |
884 |
conf = find_conf_by_address(source_p->host, NULL, CONF_KLINE, 0, |
885 |
source_p->username, NULL, 0); |
886 |
|
887 |
if (!conf) |
888 |
return; |
889 |
|
890 |
/* Don't report a temporary kline as permanent kline */ |
891 |
if (conf->until) |
892 |
return; |
893 |
|
894 |
sendto_one_numeric(source_p, &me, RPL_STATSKLINE, 'K', |
895 |
conf->host, conf->user, conf->reason); |
896 |
} |
897 |
else /* They are opered, or allowed to see all klines */ |
898 |
report_Klines(source_p, 0); |
899 |
} |
900 |
|
901 |
static void |
902 |
stats_messages(struct Client *source_p, int parc, char *parv[]) |
903 |
{ |
904 |
if (!HasUMode(source_p, UMODE_OPER) && ConfigGeneral.stats_m_oper_only) |
905 |
sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES); |
906 |
else |
907 |
report_messages(source_p); |
908 |
} |
909 |
|
910 |
static void |
911 |
stats_oper(struct Client *source_p, int parc, char *parv[]) |
912 |
{ |
913 |
if (!HasUMode(source_p, UMODE_OPER) && ConfigGeneral.stats_o_oper_only) |
914 |
sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES); |
915 |
else |
916 |
report_confitem_types(source_p, CONF_OPER); |
917 |
} |
918 |
|
919 |
/* stats_operedup() |
920 |
* |
921 |
* input - client pointer |
922 |
* output - none |
923 |
* side effects - client is shown a list of active opers |
924 |
*/ |
925 |
static void |
926 |
stats_operedup(struct Client *source_p, int parc, char *parv[]) |
927 |
{ |
928 |
const dlink_node *node = NULL; |
929 |
unsigned int opercount = 0; |
930 |
char buf[IRCD_BUFSIZE] = ""; |
931 |
|
932 |
DLINK_FOREACH(node, oper_list.head) |
933 |
{ |
934 |
const struct Client *target_p = node->data; |
935 |
|
936 |
if (HasUMode(target_p, UMODE_HIDDEN) && !HasUMode(source_p, UMODE_OPER)) |
937 |
continue; |
938 |
|
939 |
if (HasUMode(source_p, UMODE_OPER) || !HasUMode(target_p, UMODE_HIDEIDLE)) |
940 |
snprintf(buf, sizeof(buf), "%u", client_get_idle_time(source_p, target_p)); |
941 |
else |
942 |
strlcpy(buf, "n/a", sizeof(buf)); |
943 |
|
944 |
if (MyClient(source_p) && HasUMode(source_p, UMODE_OPER)) |
945 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
946 |
"p :[%c][%s] %s (%s@%s) Idle: %s", |
947 |
HasUMode(target_p, UMODE_ADMIN) ? 'A' : 'O', |
948 |
oper_privs_as_string(target_p->connection->operflags), |
949 |
target_p->name, target_p->username, target_p->host, buf); |
950 |
else |
951 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
952 |
"p :[%c] %s (%s@%s) Idle: %s", |
953 |
HasUMode(target_p, UMODE_ADMIN) ? 'A' : 'O', |
954 |
target_p->name, target_p->username, target_p->host, buf); |
955 |
++opercount; |
956 |
} |
957 |
|
958 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
959 |
"p :%u OPER(s)", opercount); |
960 |
} |
961 |
|
962 |
/* show_ports() |
963 |
* |
964 |
* inputs - pointer to client to show ports to |
965 |
* output - none |
966 |
* side effects - send port listing to a client |
967 |
*/ |
968 |
static void |
969 |
show_ports(struct Client *source_p) |
970 |
{ |
971 |
char buf[IRCD_BUFSIZE] = ""; |
972 |
char *p = NULL; |
973 |
const dlink_node *node = NULL; |
974 |
|
975 |
DLINK_FOREACH(node, listener_get_list()->head) |
976 |
{ |
977 |
const struct Listener *listener = node->data; |
978 |
p = buf; |
979 |
|
980 |
if (listener->flags & LISTENER_HIDDEN) |
981 |
{ |
982 |
if (!HasUMode(source_p, UMODE_ADMIN)) |
983 |
continue; |
984 |
*p++ = 'H'; |
985 |
} |
986 |
|
987 |
if (listener->flags & LISTENER_SERVER) |
988 |
*p++ = 'S'; |
989 |
if (listener->flags & LISTENER_SSL) |
990 |
*p++ = 's'; |
991 |
*p = '\0'; |
992 |
|
993 |
|
994 |
if (HasUMode(source_p, UMODE_ADMIN) && |
995 |
(MyClient(source_p) || !ConfigServerHide.hide_server_ips)) |
996 |
sendto_one_numeric(source_p, &me, RPL_STATSPLINE, 'P', listener->port, |
997 |
listener->name, |
998 |
listener->ref_count, buf, |
999 |
listener->active ? "active" : "disabled"); |
1000 |
else |
1001 |
sendto_one_numeric(source_p, &me, RPL_STATSPLINE, 'P', listener->port, |
1002 |
me.name, listener->ref_count, buf, |
1003 |
listener->active ? "active" : "disabled"); |
1004 |
} |
1005 |
} |
1006 |
|
1007 |
static void |
1008 |
stats_ports(struct Client *source_p, int parc, char *parv[]) |
1009 |
{ |
1010 |
if (!HasUMode(source_p, UMODE_OPER) && ConfigGeneral.stats_P_oper_only) |
1011 |
sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES); |
1012 |
else |
1013 |
show_ports(source_p); |
1014 |
} |
1015 |
|
1016 |
static void |
1017 |
stats_resv(struct Client *source_p, int parc, char *parv[]) |
1018 |
{ |
1019 |
report_resv(source_p); |
1020 |
} |
1021 |
|
1022 |
static void |
1023 |
stats_service(struct Client *source_p, int parc, char *parv[]) |
1024 |
{ |
1025 |
report_confitem_types(source_p, CONF_SERVICE); |
1026 |
} |
1027 |
|
1028 |
static void |
1029 |
stats_tstats(struct Client *source_p, int parc, char *parv[]) |
1030 |
{ |
1031 |
const dlink_node *node = NULL; |
1032 |
struct ServerStatistics tmp; |
1033 |
struct ServerStatistics *sp = &tmp; |
1034 |
|
1035 |
memcpy(sp, &ServerStats, sizeof(struct ServerStatistics)); |
1036 |
|
1037 |
/* |
1038 |
* must use the += operator. is_sv is not the number of currently |
1039 |
* active server connections. Note the incrementation in |
1040 |
* s_bsd.c:close_connection. |
1041 |
*/ |
1042 |
sp->is_sv += dlink_list_length(&local_server_list); |
1043 |
|
1044 |
DLINK_FOREACH(node, local_server_list.head) |
1045 |
{ |
1046 |
const struct Client *target_p = node->data; |
1047 |
|
1048 |
sp->is_sbs += target_p->connection->send.bytes; |
1049 |
sp->is_sbr += target_p->connection->recv.bytes; |
1050 |
sp->is_sti += CurrentTime - target_p->connection->firsttime; |
1051 |
} |
1052 |
|
1053 |
sp->is_cl += dlink_list_length(&local_client_list); |
1054 |
|
1055 |
DLINK_FOREACH(node, local_client_list.head) |
1056 |
{ |
1057 |
const struct Client *target_p = node->data; |
1058 |
|
1059 |
sp->is_cbs += target_p->connection->send.bytes; |
1060 |
sp->is_cbr += target_p->connection->recv.bytes; |
1061 |
sp->is_cti += CurrentTime - target_p->connection->firsttime; |
1062 |
} |
1063 |
|
1064 |
sp->is_ni += dlink_list_length(&unknown_list); |
1065 |
|
1066 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
1067 |
"t :accepts %u refused %u", |
1068 |
sp->is_ac, sp->is_ref); |
1069 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
1070 |
"t :unknown commands %u prefixes %u", |
1071 |
sp->is_unco, sp->is_unpf); |
1072 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
1073 |
"t :nick collisions %u unknown closes %u", |
1074 |
sp->is_kill, sp->is_ni); |
1075 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
1076 |
"t :wrong direction %u empty %u", |
1077 |
sp->is_wrdi, sp->is_empt); |
1078 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
1079 |
"t :numerics seen %u", |
1080 |
sp->is_num); |
1081 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
1082 |
"t :auth successes %u fails %u", |
1083 |
sp->is_asuc, sp->is_abad); |
1084 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
1085 |
"t :Client Server"); |
1086 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
1087 |
"t :connected %u %u", |
1088 |
(unsigned int)sp->is_cl, |
1089 |
(unsigned int)sp->is_sv); |
1090 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
1091 |
"t :bytes sent %ju %ju", |
1092 |
sp->is_cbs, sp->is_sbs); |
1093 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
1094 |
"t :bytes recv %ju %ju", |
1095 |
sp->is_cbr, sp->is_sbr); |
1096 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
1097 |
"t :time connected %u %u", |
1098 |
(unsigned int)sp->is_cti, |
1099 |
(unsigned int)sp->is_sti); |
1100 |
} |
1101 |
|
1102 |
static void |
1103 |
stats_uptime(struct Client *source_p, int parc, char *parv[]) |
1104 |
{ |
1105 |
if (!HasUMode(source_p, UMODE_OPER) && ConfigGeneral.stats_u_oper_only) |
1106 |
sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES); |
1107 |
else |
1108 |
{ |
1109 |
sendto_one_numeric(source_p, &me, RPL_STATSUPTIME, |
1110 |
time_dissect(CurrentTime - me.connection->since)); |
1111 |
if (!ConfigServerHide.disable_remote_commands || HasUMode(source_p, UMODE_OPER)) |
1112 |
sendto_one_numeric(source_p, &me, RPL_STATSCONN, Count.max_loc_con, |
1113 |
Count.max_loc_cli, Count.totalrestartcount); |
1114 |
} |
1115 |
} |
1116 |
|
1117 |
static void |
1118 |
stats_shared(struct Client *source_p, int parc, char *parv[]) |
1119 |
{ |
1120 |
report_confitem_types(source_p, CONF_SHARED); |
1121 |
} |
1122 |
|
1123 |
/* stats_servers() |
1124 |
* |
1125 |
* input - client pointer |
1126 |
* output - none |
1127 |
* side effects - client is shown lists of who connected servers |
1128 |
*/ |
1129 |
static void |
1130 |
stats_servers(struct Client *source_p, int parc, char *parv[]) |
1131 |
{ |
1132 |
const dlink_node *node = NULL; |
1133 |
|
1134 |
DLINK_FOREACH(node, local_server_list.head) |
1135 |
{ |
1136 |
const struct Client *target_p = node->data; |
1137 |
|
1138 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
1139 |
"v :%s (%s!%s@%s) Idle: %d", |
1140 |
target_p->name, |
1141 |
(target_p->serv->by[0] ? target_p->serv->by : "Remote."), |
1142 |
"*", "*", (int)(CurrentTime - target_p->connection->lasttime)); |
1143 |
} |
1144 |
|
1145 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
1146 |
"v :%u Server(s)", |
1147 |
dlink_list_length(&local_server_list)); |
1148 |
} |
1149 |
|
1150 |
static void |
1151 |
stats_gecos(struct Client *source_p, int parc, char *parv[]) |
1152 |
{ |
1153 |
report_confitem_types(source_p, CONF_XLINE); |
1154 |
} |
1155 |
|
1156 |
static void |
1157 |
stats_class(struct Client *source_p, int parc, char *parv[]) |
1158 |
{ |
1159 |
const dlink_node *node = NULL; |
1160 |
|
1161 |
DLINK_FOREACH(node, class_get_list()->head) |
1162 |
{ |
1163 |
const struct ClassItem *class = node->data; |
1164 |
|
1165 |
sendto_one_numeric(source_p, &me, RPL_STATSYLINE, 'Y', |
1166 |
class->name, class->ping_freq, |
1167 |
class->con_freq, |
1168 |
class->max_total, class->max_sendq, |
1169 |
class->max_recvq, |
1170 |
class->ref_count, |
1171 |
class->number_per_cidr, class->cidr_bitlen_ipv4, |
1172 |
class->number_per_cidr, class->cidr_bitlen_ipv6, |
1173 |
class->active ? "active" : "disabled"); |
1174 |
} |
1175 |
} |
1176 |
|
1177 |
static void |
1178 |
stats_servlinks(struct Client *source_p, int parc, char *parv[]) |
1179 |
{ |
1180 |
uintmax_t sendB = 0, recvB = 0; |
1181 |
time_t uptime = 0; |
1182 |
dlink_node *node = NULL; |
1183 |
|
1184 |
if (ConfigServerHide.flatten_links && !HasUMode(source_p, UMODE_OPER)) |
1185 |
{ |
1186 |
sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES); |
1187 |
return; |
1188 |
} |
1189 |
|
1190 |
DLINK_FOREACH(node, local_server_list.head) |
1191 |
{ |
1192 |
struct Client *target_p = node->data; |
1193 |
|
1194 |
if (HasFlag(target_p, FLAGS_SERVICE) && ConfigServerHide.hide_services) |
1195 |
if (!HasUMode(source_p, UMODE_OPER)) |
1196 |
continue; |
1197 |
|
1198 |
sendB += target_p->connection->send.bytes; |
1199 |
recvB += target_p->connection->recv.bytes; |
1200 |
|
1201 |
/* ":%s 211 %s %s %u %u %ju %u %ju :%u %u %s" */ |
1202 |
sendto_one_numeric(source_p, &me, RPL_STATSLINKINFO, |
1203 |
get_client_name(target_p, HasUMode(source_p, UMODE_ADMIN) ? SHOW_IP : MASK_IP), |
1204 |
dbuf_length(&target_p->connection->buf_sendq), |
1205 |
target_p->connection->send.messages, |
1206 |
target_p->connection->send.bytes >> 10, |
1207 |
target_p->connection->recv.messages, |
1208 |
target_p->connection->recv.bytes >> 10, |
1209 |
(unsigned int)(CurrentTime - target_p->connection->firsttime), |
1210 |
(CurrentTime > target_p->connection->since) ? (unsigned int)(CurrentTime - target_p->connection->since) : 0, |
1211 |
HasUMode(source_p, UMODE_OPER) ? show_capabilities(target_p) : "TS"); |
1212 |
} |
1213 |
|
1214 |
sendB >>= 10; |
1215 |
recvB >>= 10; |
1216 |
|
1217 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, "? :%u total server(s)", |
1218 |
dlink_list_length(&local_server_list)); |
1219 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, "? :Sent total: %7.2f %s", |
1220 |
_GMKv(sendB), _GMKs(sendB)); |
1221 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, "? :Recv total: %7.2f %s", |
1222 |
_GMKv(recvB), _GMKs(recvB)); |
1223 |
|
1224 |
uptime = (CurrentTime - me.connection->since); |
1225 |
|
1226 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
1227 |
"? :Server send: %7.2f %s (%4.1f K/s)", |
1228 |
_GMKv((me.connection->send.bytes>>10)), |
1229 |
_GMKs((me.connection->send.bytes>>10)), |
1230 |
(float)((float)((me.connection->send.bytes) >> 10) / |
1231 |
(float)uptime)); |
1232 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, |
1233 |
"? :Server recv: %7.2f %s (%4.1f K/s)", |
1234 |
_GMKv((me.connection->recv.bytes>>10)), |
1235 |
_GMKs((me.connection->recv.bytes>>10)), |
1236 |
(float)((float)((me.connection->recv.bytes) >> 10) / |
1237 |
(float)uptime)); |
1238 |
} |
1239 |
|
1240 |
/* parse_stats_args() |
1241 |
* |
1242 |
* inputs - arg count |
1243 |
* - args |
1244 |
* - doall flag |
1245 |
* - wild card or not |
1246 |
* output - pointer to name to use |
1247 |
* side effects - |
1248 |
* common parse routine for m_stats args |
1249 |
* |
1250 |
*/ |
1251 |
static const char * |
1252 |
parse_stats_args(struct Client *source_p, int parc, char *parv[], int *doall, int *wilds) |
1253 |
{ |
1254 |
if (parc > 2) |
1255 |
{ |
1256 |
const char *name = parv[2]; |
1257 |
|
1258 |
if (!irccmp(name, ID_or_name(&me, source_p))) |
1259 |
*doall = 2; |
1260 |
else if (!match(name, ID_or_name(&me, source_p))) |
1261 |
*doall = 1; |
1262 |
|
1263 |
*wilds = has_wildcards(name); |
1264 |
|
1265 |
return name; |
1266 |
} |
1267 |
|
1268 |
return NULL; |
1269 |
} |
1270 |
|
1271 |
static void |
1272 |
stats_L_list(struct Client *source_p, const char *name, int doall, int wilds, |
1273 |
dlink_list *list, const char statchar) |
1274 |
{ |
1275 |
dlink_node *node = NULL; |
1276 |
|
1277 |
/* |
1278 |
* Send info about connections which match, or all if the |
1279 |
* mask matches from. Only restrictions are on those who |
1280 |
* are invisible not being visible to 'foreigners' who use |
1281 |
* a wild card based search to list it. |
1282 |
*/ |
1283 |
DLINK_FOREACH(node, list->head) |
1284 |
{ |
1285 |
struct Client *target_p = node->data; |
1286 |
|
1287 |
if (HasUMode(target_p, UMODE_INVISIBLE) && (doall || wilds) && |
1288 |
!(MyConnect(source_p) && HasUMode(source_p, UMODE_OPER)) && |
1289 |
!HasUMode(target_p, UMODE_OPER) && (target_p != source_p)) |
1290 |
continue; |
1291 |
|
1292 |
if (!doall && wilds && match(name, target_p->name)) |
1293 |
continue; |
1294 |
|
1295 |
if (!(doall || wilds) && irccmp(name, target_p->name)) |
1296 |
continue; |
1297 |
|
1298 |
/* |
1299 |
* This basically shows ips for our opers if it's not a server/admin, or |
1300 |
* it's one of our admins. |
1301 |
*/ |
1302 |
if (MyClient(source_p) && HasUMode(source_p, UMODE_OPER) && |
1303 |
(HasUMode(source_p, UMODE_ADMIN) || |
1304 |
(!IsServer(target_p) && !HasUMode(target_p, UMODE_ADMIN) && |
1305 |
!IsHandshake(target_p) && !IsConnecting(target_p)))) |
1306 |
{ |
1307 |
sendto_one_numeric(source_p, &me, RPL_STATSLINKINFO, |
1308 |
(IsUpper(statchar)) ? |
1309 |
get_client_name(target_p, SHOW_IP) : |
1310 |
get_client_name(target_p, HIDE_IP), |
1311 |
dbuf_length(&target_p->connection->buf_sendq), |
1312 |
target_p->connection->send.messages, |
1313 |
target_p->connection->send.bytes>>10, |
1314 |
target_p->connection->recv.messages, |
1315 |
target_p->connection->recv.bytes>>10, |
1316 |
(unsigned int)(CurrentTime - target_p->connection->firsttime), |
1317 |
(CurrentTime > target_p->connection->since) ? (unsigned int)(CurrentTime - target_p->connection->since) : 0, |
1318 |
IsServer(target_p) ? show_capabilities(target_p) : "-"); |
1319 |
} |
1320 |
else |
1321 |
{ |
1322 |
/* If it's a server, mask the real IP */ |
1323 |
if (IsServer(target_p) || IsHandshake(target_p) || IsConnecting(target_p)) |
1324 |
sendto_one_numeric(source_p, &me, RPL_STATSLINKINFO, |
1325 |
get_client_name(target_p, MASK_IP), |
1326 |
dbuf_length(&target_p->connection->buf_sendq), |
1327 |
target_p->connection->send.messages, |
1328 |
target_p->connection->send.bytes>>10, |
1329 |
target_p->connection->recv.messages, |
1330 |
target_p->connection->recv.bytes>>10, |
1331 |
(unsigned int)(CurrentTime - target_p->connection->firsttime), |
1332 |
(CurrentTime > target_p->connection->since) ? (unsigned int)(CurrentTime - target_p->connection->since):0, |
1333 |
IsServer(target_p) ? show_capabilities(target_p) : "-"); |
1334 |
else /* show the real IP */ |
1335 |
sendto_one_numeric(source_p, &me, RPL_STATSLINKINFO, |
1336 |
(IsUpper(statchar)) ? |
1337 |
get_client_name(target_p, SHOW_IP) : |
1338 |
get_client_name(target_p, HIDE_IP), |
1339 |
dbuf_length(&target_p->connection->buf_sendq), |
1340 |
target_p->connection->send.messages, |
1341 |
target_p->connection->send.bytes>>10, |
1342 |
target_p->connection->recv.messages, |
1343 |
target_p->connection->recv.bytes>>10, |
1344 |
(unsigned int)(CurrentTime - target_p->connection->firsttime), |
1345 |
(CurrentTime > target_p->connection->since) ? (unsigned int)(CurrentTime - target_p->connection->since):0, |
1346 |
IsServer(target_p) ? show_capabilities(target_p) : "-"); |
1347 |
} |
1348 |
} |
1349 |
} |
1350 |
|
1351 |
/* |
1352 |
* stats_L |
1353 |
* |
1354 |
* inputs - pointer to client to report to |
1355 |
* - doall flag |
1356 |
* - wild card or not |
1357 |
* output - NONE |
1358 |
* side effects - |
1359 |
*/ |
1360 |
static void |
1361 |
stats_L(struct Client *source_p, const char *name, int doall, |
1362 |
int wilds, const char statchar) |
1363 |
{ |
1364 |
stats_L_list(source_p, name, doall, wilds, &unknown_list, statchar); |
1365 |
stats_L_list(source_p, name, doall, wilds, &local_client_list, statchar); |
1366 |
stats_L_list(source_p, name, doall, wilds, &local_server_list, statchar); |
1367 |
} |
1368 |
|
1369 |
static void |
1370 |
stats_ltrace(struct Client *source_p, int parc, char *parv[]) |
1371 |
{ |
1372 |
int doall = 0; |
1373 |
int wilds = 0; |
1374 |
const char *name = NULL; |
1375 |
|
1376 |
if ((name = parse_stats_args(source_p, parc, parv, &doall, &wilds))) |
1377 |
{ |
1378 |
const char statchar = *parv[1]; |
1379 |
stats_L(source_p, name, doall, wilds, statchar); |
1380 |
} |
1381 |
else |
1382 |
sendto_one_numeric(source_p, &me, ERR_NEEDMOREPARAMS, "STATS"); |
1383 |
} |
1384 |
|
1385 |
struct StatsStruct |
1386 |
{ |
1387 |
const unsigned char letter; |
1388 |
void (*handler)(struct Client *, int, char *[]); |
1389 |
const unsigned int required_modes; |
1390 |
}; |
1391 |
|
1392 |
static const struct StatsStruct *stats_map[256]; |
1393 |
static const struct StatsStruct stats_tab[] = |
1394 |
{ |
1395 |
{ 'a', stats_dns_servers, UMODE_ADMIN }, |
1396 |
{ 'A', stats_dns_servers, UMODE_ADMIN }, |
1397 |
{ 'c', stats_connect, UMODE_OPER }, |
1398 |
{ 'C', stats_connect, UMODE_OPER }, |
1399 |
{ 'd', stats_tdeny, UMODE_OPER }, |
1400 |
{ 'D', stats_deny, UMODE_OPER }, |
1401 |
{ 'e', stats_exempt, UMODE_OPER }, |
1402 |
{ 'E', stats_events, UMODE_ADMIN }, |
1403 |
{ 'f', fd_dump, UMODE_ADMIN }, |
1404 |
{ 'F', fd_dump, UMODE_ADMIN }, |
1405 |
{ 'h', stats_hubleaf, UMODE_OPER }, |
1406 |
{ 'H', stats_hubleaf, UMODE_OPER }, |
1407 |
{ 'i', stats_auth, 0 }, |
1408 |
{ 'I', stats_auth, 0 }, |
1409 |
{ 'k', stats_tklines, 0 }, |
1410 |
{ 'K', stats_klines, 0 }, |
1411 |
{ 'l', stats_ltrace, UMODE_OPER }, |
1412 |
{ 'L', stats_ltrace, UMODE_OPER }, |
1413 |
{ 'm', stats_messages, 0 }, |
1414 |
{ 'M', stats_messages, 0 }, |
1415 |
{ 'o', stats_oper, 0 }, |
1416 |
{ 'O', stats_oper, 0 }, |
1417 |
{ 'p', stats_operedup, 0 }, |
1418 |
{ 'P', stats_ports, 0 }, |
1419 |
{ 'q', stats_resv, UMODE_OPER }, |
1420 |
{ 'Q', stats_resv, UMODE_OPER }, |
1421 |
{ 'r', stats_usage, UMODE_OPER }, |
1422 |
{ 'R', stats_usage, UMODE_OPER }, |
1423 |
{ 's', stats_service, UMODE_OPER }, |
1424 |
{ 'S', stats_service, UMODE_OPER }, |
1425 |
{ 't', stats_tstats, UMODE_OPER }, |
1426 |
{ 'T', motd_report, UMODE_OPER }, |
1427 |
{ 'u', stats_uptime, 0 }, |
1428 |
{ 'U', stats_shared, UMODE_OPER }, |
1429 |
{ 'v', stats_servers, UMODE_OPER }, |
1430 |
{ 'x', stats_gecos, UMODE_OPER }, |
1431 |
{ 'X', stats_gecos, UMODE_OPER }, |
1432 |
{ 'y', stats_class, UMODE_OPER }, |
1433 |
{ 'Y', stats_class, UMODE_OPER }, |
1434 |
{ 'z', stats_memory, UMODE_OPER }, |
1435 |
{ '?', stats_servlinks, 0 }, |
1436 |
{ '\0', NULL, 0 } |
1437 |
}; |
1438 |
|
1439 |
static void |
1440 |
do_stats(struct Client *source_p, int parc, char *parv[]) |
1441 |
{ |
1442 |
const unsigned char statchar = *parv[1]; |
1443 |
const struct StatsStruct *tab; |
1444 |
|
1445 |
if (statchar == '\0') |
1446 |
{ |
1447 |
sendto_one_numeric(source_p, &me, RPL_ENDOFSTATS, '*'); |
1448 |
return; |
1449 |
} |
1450 |
|
1451 |
if ((tab = stats_map[statchar])) |
1452 |
{ |
1453 |
if (!tab->required_modes || HasUMode(source_p, tab->required_modes)) |
1454 |
tab->handler(source_p, parc, parv); |
1455 |
else |
1456 |
sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES); |
1457 |
|
1458 |
sendto_realops_flags(UMODE_SPY, L_ALL, SEND_NOTICE, |
1459 |
"STATS %c requested by %s (%s@%s) [%s]", |
1460 |
statchar, source_p->name, source_p->username, |
1461 |
source_p->host, source_p->servptr->name); |
1462 |
} |
1463 |
|
1464 |
sendto_one_numeric(source_p, &me, RPL_ENDOFSTATS, statchar); |
1465 |
} |
1466 |
|
1467 |
/* |
1468 |
* m_stats() |
1469 |
* parv[0] = command |
1470 |
* parv[1] = stat letter/command |
1471 |
* parv[2] = (if present) server/mask in stats L |
1472 |
*/ |
1473 |
static int |
1474 |
m_stats(struct Client *source_p, int parc, char *parv[]) |
1475 |
{ |
1476 |
static time_t last_used = 0; |
1477 |
|
1478 |
/* Check the user is actually allowed to do /stats, and isn't flooding */ |
1479 |
if ((last_used + ConfigGeneral.pace_wait) > CurrentTime) |
1480 |
{ |
1481 |
sendto_one_numeric(source_p, &me, RPL_LOAD2HI, "STATS"); |
1482 |
return 0; |
1483 |
} |
1484 |
|
1485 |
last_used = CurrentTime; |
1486 |
|
1487 |
/* Is the stats meant for us? */ |
1488 |
if (!ConfigServerHide.disable_remote_commands) |
1489 |
if (hunt_server(source_p, ":%s STATS %s :%s", 2, parc, parv) != HUNTED_ISME) |
1490 |
return 0; |
1491 |
|
1492 |
do_stats(source_p, parc, parv); |
1493 |
return 0; |
1494 |
} |
1495 |
|
1496 |
/* |
1497 |
* ms_stats() |
1498 |
* parv[0] = command |
1499 |
* parv[1] = stat letter/command |
1500 |
* parv[2] = (if present) server/mask in stats L, or target |
1501 |
*/ |
1502 |
static int |
1503 |
ms_stats(struct Client *source_p, int parc, char *parv[]) |
1504 |
{ |
1505 |
if (hunt_server(source_p, ":%s STATS %s :%s", 2, parc, parv) != HUNTED_ISME) |
1506 |
return 0; |
1507 |
|
1508 |
do_stats(source_p, parc, parv); |
1509 |
return 0; |
1510 |
} |
1511 |
|
1512 |
static void |
1513 |
stats_init(void) |
1514 |
{ |
1515 |
for (const struct StatsStruct *tab = stats_tab; tab->letter; ++tab) |
1516 |
stats_map[tab->letter] = tab; |
1517 |
} |
1518 |
|
1519 |
static struct Message stats_msgtab = |
1520 |
{ |
1521 |
.cmd = "STATS", |
1522 |
.args_min = 2, |
1523 |
.args_max = MAXPARA, |
1524 |
.handlers[UNREGISTERED_HANDLER] = m_unregistered, |
1525 |
.handlers[CLIENT_HANDLER] = m_stats, |
1526 |
.handlers[SERVER_HANDLER] = ms_stats, |
1527 |
.handlers[ENCAP_HANDLER] = m_ignore, |
1528 |
.handlers[OPER_HANDLER] = ms_stats |
1529 |
}; |
1530 |
|
1531 |
static void |
1532 |
module_init(void) |
1533 |
{ |
1534 |
stats_init(); |
1535 |
mod_add_cmd(&stats_msgtab); |
1536 |
} |
1537 |
|
1538 |
static void |
1539 |
module_exit(void) |
1540 |
{ |
1541 |
mod_del_cmd(&stats_msgtab); |
1542 |
} |
1543 |
|
1544 |
struct module module_entry = |
1545 |
{ |
1546 |
.version = "$Revision$", |
1547 |
.modinit = module_init, |
1548 |
.modexit = module_exit, |
1549 |
}; |