ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/modules/m_stats.c
Revision: 7234
Committed: Wed Feb 3 16:30:10 2016 UTC (8 years, 1 month ago) by michael
Content type: text/x-csrc
File size: 50020 byte(s)
Log Message:
- Move resv.* to conf_resv.*

File Contents

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

Properties

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