ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/modules/m_stats.c
Revision: 5881
Committed: Sun May 3 16:04:15 2015 UTC (8 years, 10 months ago) by michael
Content type: text/x-csrc
File size: 47662 byte(s)
Log Message:
- Use C99-style initializers in all struct Message items
- Removed MFLG_SLOW
- Removed DUMMY_HANDLER

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

Properties

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