ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/modules/m_stats.c
Revision: 5543
Committed: Thu Feb 12 13:40:13 2015 UTC (9 years, 1 month ago) by michael
Content type: text/x-csrc
File size: 52479 byte(s)
Log Message:
- Renamed idle_time_get() to client_get_idle_time()

File Contents

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

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision