]> jfr.im git - irc/rqf/shadowircd.git/blob - modules/m_stats.c
Add resv oper priv, enabled by default for compatibility.
[irc/rqf/shadowircd.git] / modules / m_stats.c
1 /*
2 * ircd-ratbox: an advanced Internet Relay Chat Daemon(ircd).
3 * m_stats.c: Sends the user statistics or config information.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
24 * $Id: m_stats.c 1608 2006-06-04 02:11:40Z jilles $
25 */
26
27 #include "stdinc.h"
28 #include "tools.h" /* dlink_node/dlink_list */
29 #include "class.h" /* report_classes */
30 #include "client.h" /* Client */
31 #include "common.h" /* TRUE/FALSE */
32 #include "irc_string.h"
33 #include "ircd.h" /* me */
34 #include "listener.h" /* show_ports */
35 #include "s_gline.h"
36 #include "msg.h" /* Message */
37 #include "hostmask.h" /* report_mtrie_conf_links */
38 #include "numeric.h" /* ERR_xxx */
39 #include "scache.h" /* list_scache */
40 #include "send.h" /* sendto_one */
41 #include "commio.h" /* highest_fd */
42 #include "s_conf.h" /* ConfItem */
43 #include "s_serv.h" /* hunt_server */
44 #include "s_stats.h" /* tstats */
45 #include "s_user.h" /* show_opers */
46 #include "event.h" /* events */
47 #include "blacklist.h" /* dnsbl stuff */
48 #include "linebuf.h"
49 #include "parse.h"
50 #include "modules.h"
51 #include "hook.h"
52 #include "s_newconf.h"
53 #include "hash.h"
54
55 static int m_stats (struct Client *, struct Client *, int, const char **);
56
57 struct Message stats_msgtab = {
58 "STATS", 0, 0, 0, MFLG_SLOW,
59 {mg_unreg, {m_stats, 2}, {m_stats, 3}, mg_ignore, mg_ignore, {m_stats, 2}}
60 };
61
62 int doing_stats_hook;
63 int doing_stats_p_hook;
64
65 mapi_clist_av1 stats_clist[] = { &stats_msgtab, NULL };
66 mapi_hlist_av1 stats_hlist[] = {
67 { "doing_stats", &doing_stats_hook },
68 { "doing_stats_p", &doing_stats_p_hook },
69 { NULL, NULL }
70 };
71
72 DECLARE_MODULE_AV1(stats, NULL, NULL, stats_clist, stats_hlist, NULL, "$Revision: 1608 $");
73
74 const char *Lformat = "%s %u %u %u %u %u :%u %u %s";
75
76 static void stats_l_list(struct Client *s, const char *, int, int, dlink_list *, char);
77 static void stats_l_client(struct Client *source_p, struct Client *target_p,
78 char statchar);
79
80 static void stats_spy(struct Client *, char, const char *);
81 static void stats_p_spy(struct Client *);
82
83 /* Heres our struct for the stats table */
84 struct StatsStruct
85 {
86 char letter;
87 void (*handler) ();
88 int need_oper;
89 int need_admin;
90 };
91
92 static void stats_dns_servers (struct Client *);
93 static void stats_delay(struct Client *);
94 static void stats_hash(struct Client *);
95 static void stats_connect(struct Client *);
96 static void stats_tdeny(struct Client *);
97 static void stats_deny(struct Client *);
98 static void stats_exempt(struct Client *);
99 static void stats_events(struct Client *);
100 static void stats_glines(struct Client *);
101 static void stats_pending_glines(struct Client *);
102 static void stats_hubleaf(struct Client *);
103 static void stats_auth(struct Client *);
104 static void stats_tklines(struct Client *);
105 static void stats_klines(struct Client *);
106 static void stats_messages(struct Client *);
107 static void stats_dnsbl(struct Client *);
108 static void stats_oper(struct Client *);
109 static void stats_operedup(struct Client *);
110 static void stats_ports(struct Client *);
111 static void stats_tresv(struct Client *);
112 static void stats_resv(struct Client *);
113 static void stats_usage(struct Client *);
114 static void stats_tstats(struct Client *);
115 static void stats_uptime(struct Client *);
116 static void stats_shared(struct Client *);
117 static void stats_servers(struct Client *);
118 static void stats_tgecos(struct Client *);
119 static void stats_gecos(struct Client *);
120 static void stats_class(struct Client *);
121 static void stats_memory(struct Client *);
122 static void stats_servlinks(struct Client *);
123 static void stats_ltrace(struct Client *, int, const char **);
124 static void stats_ziplinks(struct Client *);
125
126 /* This table contains the possible stats items, in order:
127 * stats letter, function to call, operonly? adminonly?
128 * case only matters in the stats letter column.. -- fl_
129 */
130 static struct StatsStruct stats_cmd_table[] = {
131 /* letter function need_oper need_admin */
132 {'a', stats_dns_servers, 1, 1, },
133 {'A', stats_dns_servers, 1, 1, },
134 {'b', stats_delay, 1, 1, },
135 {'B', stats_hash, 1, 1, },
136 {'c', stats_connect, 0, 0, },
137 {'C', stats_connect, 0, 0, },
138 {'d', stats_tdeny, 1, 0, },
139 {'D', stats_deny, 1, 0, },
140 {'e', stats_exempt, 1, 0, },
141 {'E', stats_events, 1, 1, },
142 {'f', comm_dump, 1, 1, },
143 {'F', comm_dump, 1, 1, },
144 {'g', stats_pending_glines, 1, 0, },
145 {'G', stats_glines, 1, 0, },
146 {'h', stats_hubleaf, 0, 0, },
147 {'H', stats_hubleaf, 0, 0, },
148 {'i', stats_auth, 0, 0, },
149 {'I', stats_auth, 0, 0, },
150 {'k', stats_tklines, 0, 0, },
151 {'K', stats_klines, 0, 0, },
152 {'l', stats_ltrace, 0, 0, },
153 {'L', stats_ltrace, 0, 0, },
154 {'m', stats_messages, 0, 0, },
155 {'M', stats_messages, 0, 0, },
156 {'n', stats_dnsbl, 0, 0, },
157 {'o', stats_oper, 0, 0, },
158 {'O', stats_oper, 0, 0, },
159 {'p', stats_operedup, 0, 0, },
160 {'P', stats_ports, 0, 0, },
161 {'q', stats_tresv, 1, 0, },
162 {'Q', stats_resv, 1, 0, },
163 {'r', stats_usage, 1, 0, },
164 {'R', stats_usage, 1, 0, },
165 {'t', stats_tstats, 1, 0, },
166 {'T', stats_tstats, 1, 0, },
167 {'u', stats_uptime, 0, 0, },
168 {'U', stats_shared, 1, 0, },
169 {'v', stats_servers, 0, 0, },
170 {'V', stats_servers, 0, 0, },
171 {'x', stats_tgecos, 1, 0, },
172 {'X', stats_gecos, 1, 0, },
173 {'y', stats_class, 0, 0, },
174 {'Y', stats_class, 0, 0, },
175 {'z', stats_memory, 1, 0, },
176 {'Z', stats_ziplinks, 1, 0, },
177 {'?', stats_servlinks, 0, 0, },
178 {(char) 0, (void (*)()) 0, 0, 0, }
179 };
180
181 /*
182 * m_stats by fl_
183 * parv[0] = sender prefix
184 * parv[1] = stat letter/command
185 * parv[2] = (if present) server/mask in stats L, or target
186 *
187 * This will search the tables for the appropriate stats letter,
188 * if found execute it.
189 */
190 static int
191 m_stats(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
192 {
193 static time_t last_used = 0;
194 int i;
195 char statchar;
196
197 statchar = parv[1][0];
198
199 if(MyClient(source_p) && !IsOper(source_p))
200 {
201 /* Check the user is actually allowed to do /stats, and isnt flooding */
202 if((last_used + ConfigFileEntry.pace_wait) > CurrentTime)
203 {
204 /* safe enough to give this on a local connect only */
205 sendto_one(source_p, form_str(RPL_LOAD2HI),
206 me.name, source_p->name, "STATS");
207 sendto_one_numeric(source_p, RPL_ENDOFSTATS,
208 form_str(RPL_ENDOFSTATS), statchar);
209 return 0;
210 }
211 else
212 last_used = CurrentTime;
213 }
214
215 if(hunt_server (client_p, source_p, ":%s STATS %s :%s", 2, parc, parv) != HUNTED_ISME)
216 return 0;
217
218 if((statchar != 'L') && (statchar != 'l'))
219 stats_spy(source_p, statchar, NULL);
220
221 for (i = 0; stats_cmd_table[i].handler; i++)
222 {
223 if(stats_cmd_table[i].letter == statchar)
224 {
225 /* The stats table says what privs are needed, so check --fl_ */
226 /* Called for remote clients and for local opers, so check need_admin
227 * and need_oper
228 */
229 if((stats_cmd_table[i].need_admin && !IsOperAdmin (source_p)) ||
230 (stats_cmd_table[i].need_oper && !IsOper (source_p)))
231 {
232 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
233 form_str (ERR_NOPRIVILEGES));
234 break;
235 }
236
237 /* Blah, stats L needs the parameters, none of the others do.. */
238 if(statchar == 'L' || statchar == 'l')
239 stats_cmd_table[i].handler (source_p, parc, parv);
240 else
241 stats_cmd_table[i].handler (source_p);
242 }
243 }
244
245 /* Send the end of stats notice, and the stats_spy */
246 sendto_one_numeric(source_p, RPL_ENDOFSTATS,
247 form_str(RPL_ENDOFSTATS), statchar);
248
249 return 0;
250 }
251
252 static void
253 stats_dns_servers (struct Client *source_p)
254 {
255 report_dns_servers (source_p);
256 }
257
258 static void
259 stats_delay(struct Client *source_p)
260 {
261 struct nd_entry *nd;
262 struct DictionaryIter iter;
263
264 DICTIONARY_FOREACH(nd, &iter, nd_dict)
265 {
266 sendto_one_notice(source_p, "Delaying: %s for %ld",
267 nd->name, (long) nd->expire);
268 }
269 }
270
271 static void
272 stats_hash(struct Client *source_p)
273 {
274 hash_stats(source_p);
275 }
276
277 static void
278 stats_connect(struct Client *source_p)
279 {
280 static char buf[5];
281 struct server_conf *server_p;
282 char *s;
283 dlink_node *ptr;
284
285 if((ConfigFileEntry.stats_c_oper_only ||
286 (ConfigServerHide.flatten_links && !IsExemptShide(source_p))) &&
287 !IsOper(source_p))
288 {
289 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
290 form_str(ERR_NOPRIVILEGES));
291 return;
292 }
293
294 DLINK_FOREACH(ptr, server_conf_list.head)
295 {
296 server_p = ptr->data;
297
298 if(ServerConfIllegal(server_p))
299 continue;
300
301 buf[0] = '\0';
302 s = buf;
303
304 if(IsOper(source_p))
305 {
306 if(ServerConfAutoconn(server_p))
307 *s++ = 'A';
308 if(ServerConfTb(server_p))
309 *s++ = 'T';
310 if(ServerConfCompressed(server_p))
311 *s++ = 'Z';
312 }
313
314 if(!buf[0])
315 *s++ = '*';
316
317 *s = '\0';
318
319 sendto_one_numeric(source_p, RPL_STATSCLINE,
320 form_str(RPL_STATSCLINE),
321 #ifndef HIDE_SERVERS_IPS
322 server_p->host,
323 #else
324 "*@127.0.0.1",
325 #endif
326 buf, server_p->name,
327 server_p->port, server_p->class_name);
328 }
329 }
330
331 /* stats_tdeny()
332 *
333 * input - client to report to
334 * output - none
335 * side effects - client is given temp dline list.
336 */
337 static void
338 stats_tdeny (struct Client *source_p)
339 {
340 char *host, *pass, *user, *oper_reason;
341 struct AddressRec *arec;
342 struct ConfItem *aconf;
343 int i;
344
345 for (i = 0; i < ATABLE_SIZE; i++)
346 {
347 for (arec = atable[i]; arec; arec = arec->next)
348 {
349 if(arec->type == CONF_DLINE)
350 {
351 aconf = arec->aconf;
352
353 if(!(aconf->flags & CONF_FLAGS_TEMPORARY))
354 continue;
355
356 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
357
358 sendto_one_numeric(source_p, RPL_STATSDLINE,
359 form_str (RPL_STATSDLINE),
360 'd', host, pass,
361 oper_reason ? "|" : "",
362 oper_reason ? oper_reason : "");
363 }
364 }
365 }
366 }
367
368 /* stats_deny()
369 *
370 * input - client to report to
371 * output - none
372 * side effects - client is given dline list.
373 */
374 static void
375 stats_deny (struct Client *source_p)
376 {
377 char *host, *pass, *user, *oper_reason;
378 struct AddressRec *arec;
379 struct ConfItem *aconf;
380 int i;
381
382 for (i = 0; i < ATABLE_SIZE; i++)
383 {
384 for (arec = atable[i]; arec; arec = arec->next)
385 {
386 if(arec->type == CONF_DLINE)
387 {
388 aconf = arec->aconf;
389
390 if(aconf->flags & CONF_FLAGS_TEMPORARY)
391 continue;
392
393 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
394
395 sendto_one_numeric(source_p, RPL_STATSDLINE,
396 form_str (RPL_STATSDLINE),
397 'D', host, pass,
398 oper_reason ? "|" : "",
399 oper_reason ? oper_reason : "");
400 }
401 }
402 }
403 }
404
405
406 /* stats_exempt()
407 *
408 * input - client to report to
409 * output - none
410 * side effects - client is given list of exempt blocks
411 */
412 static void
413 stats_exempt(struct Client *source_p)
414 {
415 char *name, *host, *pass, *user, *classname;
416 struct AddressRec *arec;
417 struct ConfItem *aconf;
418 int i, port;
419
420 if(ConfigFileEntry.stats_e_disabled)
421 {
422 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
423 form_str (ERR_NOPRIVILEGES));
424 return;
425 }
426
427 for (i = 0; i < ATABLE_SIZE; i++)
428 {
429 for (arec = atable[i]; arec; arec = arec->next)
430 {
431 if(arec->type == CONF_EXEMPTDLINE)
432 {
433 aconf = arec->aconf;
434 get_printable_conf (aconf, &name, &host, &pass,
435 &user, &port, &classname);
436
437 sendto_one_numeric(source_p, RPL_STATSDLINE,
438 form_str(RPL_STATSDLINE),
439 'e', host, pass, "", "");
440 }
441 }
442 }}
443
444
445 static void
446 stats_events (struct Client *source_p)
447 {
448 show_events (source_p);
449 }
450
451 /* stats_pending_glines()
452 *
453 * input - client pointer
454 * output - none
455 * side effects - client is shown list of pending glines
456 */
457 static void
458 stats_pending_glines (struct Client *source_p)
459 {
460 if(ConfigFileEntry.glines)
461 {
462 dlink_node *pending_node;
463 struct gline_pending *glp_ptr;
464 char timebuffer[MAX_DATE_STRING];
465 struct tm *tmptr;
466
467 DLINK_FOREACH (pending_node, pending_glines.head)
468 {
469 glp_ptr = pending_node->data;
470
471 tmptr = localtime (&glp_ptr->time_request1);
472 strftime (timebuffer, MAX_DATE_STRING, "%Y/%m/%d %H:%M:%S", tmptr);
473
474 sendto_one_notice(source_p,
475 ":1) %s!%s@%s on %s requested gline at %s for %s@%s [%s]",
476 glp_ptr->oper_nick1,
477 glp_ptr->oper_user1, glp_ptr->oper_host1,
478 glp_ptr->oper_server1, timebuffer,
479 glp_ptr->user, glp_ptr->host, glp_ptr->reason1);
480
481 if(glp_ptr->oper_nick2[0])
482 {
483 tmptr = localtime (&glp_ptr->time_request2);
484 strftime (timebuffer, MAX_DATE_STRING, "%Y/%m/%d %H:%M:%S", tmptr);
485 sendto_one_notice(source_p,
486 ":2) %s!%s@%s on %s requested gline at %s for %s@%s [%s]",
487 glp_ptr->oper_nick2,
488 glp_ptr->oper_user2, glp_ptr->oper_host2,
489 glp_ptr->oper_server2, timebuffer,
490 glp_ptr->user, glp_ptr->host, glp_ptr->reason2);
491 }
492 }
493
494 if(dlink_list_length (&pending_glines) > 0)
495 sendto_one_notice(source_p, ":End of Pending G-lines");
496 }
497 else
498 sendto_one_notice(source_p, ":This server does not support G-Lines");
499
500 }
501
502 /* stats_glines()
503 *
504 * input - client pointer
505 * output - none
506 * side effects - client is shown list of glines
507 */
508 static void
509 stats_glines (struct Client *source_p)
510 {
511 if(ConfigFileEntry.glines)
512 {
513 dlink_node *gline_node;
514 struct ConfItem *kill_ptr;
515
516 DLINK_FOREACH_PREV (gline_node, glines.tail)
517 {
518 kill_ptr = gline_node->data;
519
520 sendto_one_numeric(source_p, RPL_STATSKLINE,
521 form_str(RPL_STATSKLINE), 'G',
522 kill_ptr->host ? kill_ptr->host : "*",
523 kill_ptr->user ? kill_ptr->user : "*",
524 kill_ptr->passwd ? kill_ptr->passwd : "No Reason",
525 kill_ptr->spasswd ? "|" : "",
526 kill_ptr->spasswd ? kill_ptr->spasswd : "");
527 }
528 }
529 else
530 sendto_one_notice(source_p, ":This server does not support G-Lines");
531 }
532
533
534 static void
535 stats_hubleaf(struct Client *source_p)
536 {
537 struct remote_conf *hub_p;
538 dlink_node *ptr;
539
540 if((ConfigFileEntry.stats_h_oper_only ||
541 (ConfigServerHide.flatten_links && !IsExemptShide(source_p))) &&
542 !IsOper(source_p))
543 {
544 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
545 form_str (ERR_NOPRIVILEGES));
546 return;
547 }
548
549 DLINK_FOREACH(ptr, hubleaf_conf_list.head)
550 {
551 hub_p = ptr->data;
552
553 if(hub_p->flags & CONF_HUB)
554 sendto_one_numeric(source_p, RPL_STATSHLINE,
555 form_str(RPL_STATSHLINE),
556 hub_p->host, hub_p->server);
557 else
558 sendto_one_numeric(source_p, RPL_STATSLLINE,
559 form_str(RPL_STATSLLINE),
560 hub_p->host, hub_p->server);
561 }
562 }
563
564
565 static void
566 stats_auth (struct Client *source_p)
567 {
568 /* Oper only, if unopered, return ERR_NOPRIVS */
569 if((ConfigFileEntry.stats_i_oper_only == 2) && !IsOper (source_p))
570 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
571 form_str (ERR_NOPRIVILEGES));
572
573 /* If unopered, Only return matching auth blocks */
574 else if((ConfigFileEntry.stats_i_oper_only == 1) && !IsOper (source_p))
575 {
576 struct ConfItem *aconf;
577 char *name, *host, *pass, *user, *classname;
578 int port;
579
580 if(MyConnect (source_p))
581 aconf = find_conf_by_address (source_p->host, source_p->sockhost, NULL,
582 (struct sockaddr *)&source_p->localClient->ip,
583 CONF_CLIENT,
584 source_p->localClient->ip.ss_family,
585 source_p->username);
586 else
587 aconf = find_conf_by_address (source_p->host, NULL, NULL, NULL, CONF_CLIENT,
588 0, source_p->username);
589
590 if(aconf == NULL)
591 return;
592
593 get_printable_conf (aconf, &name, &host, &pass, &user, &port, &classname);
594
595 sendto_one_numeric(source_p, RPL_STATSILINE, form_str(RPL_STATSILINE),
596 name, show_iline_prefix(source_p, aconf, user),
597 host, port, classname);
598 }
599
600 /* Theyre opered, or allowed to see all auth blocks */
601 else
602 report_auth (source_p);
603 }
604
605
606 static void
607 stats_tklines(struct Client *source_p)
608 {
609 /* Oper only, if unopered, return ERR_NOPRIVS */
610 if((ConfigFileEntry.stats_k_oper_only == 2) && !IsOper (source_p))
611 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
612 form_str (ERR_NOPRIVILEGES));
613
614 /* If unopered, Only return matching klines */
615 else if((ConfigFileEntry.stats_k_oper_only == 1) && !IsOper (source_p))
616 {
617 struct ConfItem *aconf;
618 char *host, *pass, *user, *oper_reason;
619
620 if(MyConnect (source_p))
621 aconf = find_conf_by_address (source_p->host, source_p->sockhost, NULL,
622 (struct sockaddr *)&source_p->localClient->ip,
623 CONF_KILL,
624 source_p->localClient->ip.ss_family,
625 source_p->username);
626 else
627 aconf = find_conf_by_address (source_p->host, NULL, NULL, NULL, CONF_KILL,
628 0, source_p->username);
629
630 if(aconf == NULL)
631 return;
632
633 /* dont report a permanent kline as a tkline */
634 if((aconf->flags & CONF_FLAGS_TEMPORARY) == 0)
635 return;
636
637 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
638
639 sendto_one_numeric(source_p, RPL_STATSKLINE,
640 form_str(RPL_STATSKLINE), 'k',
641 user, pass, oper_reason ? "|" : "",
642 oper_reason ? oper_reason : "");
643 }
644 /* Theyre opered, or allowed to see all klines */
645 else
646 {
647 struct ConfItem *aconf;
648 dlink_node *ptr;
649 int i;
650 char *user, *host, *pass, *oper_reason;
651
652 for(i = 0; i < LAST_TEMP_TYPE; i++)
653 {
654 DLINK_FOREACH(ptr, temp_klines[i].head)
655 {
656 aconf = ptr->data;
657
658 get_printable_kline(source_p, aconf, &host, &pass,
659 &user, &oper_reason);
660
661 sendto_one_numeric(source_p, RPL_STATSKLINE,
662 form_str(RPL_STATSKLINE),
663 'k', host, user, pass,
664 oper_reason ? "|" : "",
665 oper_reason ? oper_reason : "");
666 }
667 }
668 }
669 }
670
671 static void
672 stats_klines(struct Client *source_p)
673 {
674 /* Oper only, if unopered, return ERR_NOPRIVS */
675 if((ConfigFileEntry.stats_k_oper_only == 2) && !IsOper (source_p))
676 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
677 form_str (ERR_NOPRIVILEGES));
678
679 /* If unopered, Only return matching klines */
680 else if((ConfigFileEntry.stats_k_oper_only == 1) && !IsOper (source_p))
681 {
682 struct ConfItem *aconf;
683 char *host, *pass, *user, *oper_reason;
684
685 /* search for a kline */
686 if(MyConnect (source_p))
687 aconf = find_conf_by_address (source_p->host, source_p->sockhost, NULL,
688 (struct sockaddr *)&source_p->localClient->ip,
689 CONF_KILL,
690 source_p->localClient->ip.ss_family,
691 source_p->username);
692 else
693 aconf = find_conf_by_address (source_p->host, NULL, NULL, NULL, CONF_KILL,
694 0, source_p->username);
695
696 if(aconf == NULL)
697 return;
698
699 /* dont report a tkline as a kline */
700 if(aconf->flags & CONF_FLAGS_TEMPORARY)
701 return;
702
703 get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason);
704
705 sendto_one_numeric(source_p, RPL_STATSKLINE, form_str(RPL_STATSKLINE),
706 'K', host, user, pass, oper_reason ? "|" : "",
707 oper_reason ? oper_reason : "");
708 }
709 /* Theyre opered, or allowed to see all klines */
710 else
711 report_Klines (source_p);
712 }
713
714 static void
715 stats_messages(struct Client *source_p)
716 {
717 report_messages(source_p);
718 }
719
720 static void
721 stats_dnsbl(struct Client *source_p)
722 {
723 dlink_node *ptr;
724 struct Blacklist *blptr;
725
726 DLINK_FOREACH(ptr, blacklist_list.head)
727 {
728 blptr = ptr->data;
729
730 /* use RPL_STATSDEBUG for now -- jilles */
731 sendto_one_numeric(source_p, RPL_STATSDEBUG, "n :%d %s %s (%d)",
732 blptr->hits,
733 blptr->host,
734 blptr->status & CONF_ILLEGAL ? "disabled" : "active",
735 blptr->refcount);
736 }
737 }
738
739 static void
740 stats_oper(struct Client *source_p)
741 {
742 struct oper_conf *oper_p;
743 dlink_node *ptr;
744
745 if(!IsOper(source_p) && ConfigFileEntry.stats_o_oper_only)
746 {
747 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
748 form_str (ERR_NOPRIVILEGES));
749 return;
750 }
751
752 DLINK_FOREACH(ptr, oper_conf_list.head)
753 {
754 oper_p = ptr->data;
755
756 sendto_one_numeric(source_p, RPL_STATSOLINE,
757 form_str(RPL_STATSOLINE),
758 oper_p->username, oper_p->host, oper_p->name,
759 IsOper(source_p) ? get_oper_privs(oper_p->flags) : "0", "-1");
760 }
761 }
762
763
764 /* stats_operedup()
765 *
766 * input - client pointer
767 * output - none
768 * side effects - client is shown a list of active opers
769 */
770 static void
771 stats_operedup (struct Client *source_p)
772 {
773 struct Client *target_p;
774 dlink_node *oper_ptr;
775 unsigned int count = 0;
776
777 DLINK_FOREACH (oper_ptr, oper_list.head)
778 {
779 target_p = oper_ptr->data;
780
781 if(IsOperInvis(target_p) && !IsOper(source_p))
782 continue;
783
784 if(target_p->user->away)
785 continue;
786
787 count++;
788
789 sendto_one_numeric(source_p, RPL_STATSDEBUG,
790 "p :%s (%s@%s)",
791 target_p->name, target_p->username,
792 target_p->host);
793 }
794
795 sendto_one_numeric(source_p, RPL_STATSDEBUG,
796 "p :%u staff members", count);
797
798 stats_p_spy (source_p);
799 }
800
801 static void
802 stats_ports (struct Client *source_p)
803 {
804 if(!IsOper (source_p) && ConfigFileEntry.stats_P_oper_only)
805 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
806 form_str (ERR_NOPRIVILEGES));
807 else
808 show_ports (source_p);
809 }
810
811 static void
812 stats_tresv(struct Client *source_p)
813 {
814 struct ConfItem *aconf;
815 dlink_node *ptr;
816 int i;
817
818 DLINK_FOREACH(ptr, resv_conf_list.head)
819 {
820 aconf = ptr->data;
821 if(aconf->hold)
822 sendto_one_numeric(source_p, RPL_STATSQLINE,
823 form_str(RPL_STATSQLINE),
824 'q', aconf->port, aconf->name, aconf->passwd);
825 }
826
827 HASH_WALK(i, R_MAX, ptr, resvTable)
828 {
829 aconf = ptr->data;
830 if(aconf->hold)
831 sendto_one_numeric(source_p, RPL_STATSQLINE,
832 form_str(RPL_STATSQLINE),
833 'q', aconf->port, aconf->name, aconf->passwd);
834 }
835 HASH_WALK_END
836 }
837
838
839 static void
840 stats_resv(struct Client *source_p)
841 {
842 struct ConfItem *aconf;
843 dlink_node *ptr;
844 int i;
845
846 DLINK_FOREACH(ptr, resv_conf_list.head)
847 {
848 aconf = ptr->data;
849 if(!aconf->hold)
850 sendto_one_numeric(source_p, RPL_STATSQLINE,
851 form_str(RPL_STATSQLINE),
852 'Q', aconf->port, aconf->name, aconf->passwd);
853 }
854
855 HASH_WALK(i, R_MAX, ptr, resvTable)
856 {
857 aconf = ptr->data;
858 if(!aconf->hold)
859 sendto_one_numeric(source_p, RPL_STATSQLINE,
860 form_str(RPL_STATSQLINE),
861 'Q', aconf->port, aconf->name, aconf->passwd);
862 }
863 HASH_WALK_END
864 }
865
866 static void
867 stats_usage (struct Client *source_p)
868 {
869 struct rusage rus;
870 time_t secs;
871 time_t rup;
872 #ifdef hz
873 # define hzz hz
874 #else
875 # ifdef HZ
876 # define hzz HZ
877 # else
878 int hzz = 1;
879 # endif
880 #endif
881
882 if(getrusage(RUSAGE_SELF, &rus) == -1)
883 {
884 sendto_one_notice(source_p, ":Getruseage error: %s.",
885 strerror(errno));
886 return;
887 }
888 secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec;
889 if(0 == secs)
890 secs = 1;
891
892 rup = (CurrentTime - startup_time) * hzz;
893 if(0 == rup)
894 rup = 1;
895
896 sendto_one_numeric(source_p, RPL_STATSDEBUG,
897 "R :CPU Secs %d:%d User %d:%d System %d:%d",
898 (int) (secs / 60), (int) (secs % 60),
899 (int) (rus.ru_utime.tv_sec / 60),
900 (int) (rus.ru_utime.tv_sec % 60),
901 (int) (rus.ru_stime.tv_sec / 60),
902 (int) (rus.ru_stime.tv_sec % 60));
903 sendto_one_numeric(source_p, RPL_STATSDEBUG,
904 "R :RSS %ld ShMem %ld Data %ld Stack %ld",
905 rus.ru_maxrss, (rus.ru_ixrss / rup),
906 (rus.ru_idrss / rup), (rus.ru_isrss / rup));
907 sendto_one_numeric(source_p, RPL_STATSDEBUG,
908 "R :Swaps %d Reclaims %d Faults %d",
909 (int) rus.ru_nswap, (int) rus.ru_minflt, (int) rus.ru_majflt);
910 sendto_one_numeric(source_p, RPL_STATSDEBUG,
911 "R :Block in %d out %d",
912 (int) rus.ru_inblock, (int) rus.ru_oublock);
913 sendto_one_numeric(source_p, RPL_STATSDEBUG,
914 "R :Msg Rcv %d Send %d",
915 (int) rus.ru_msgrcv, (int) rus.ru_msgsnd);
916 sendto_one_numeric(source_p, RPL_STATSDEBUG,
917 "R :Signals %d Context Vol. %d Invol %d",
918 (int) rus.ru_nsignals, (int) rus.ru_nvcsw,
919 (int) rus.ru_nivcsw);
920 }
921
922 static void
923 stats_tstats (struct Client *source_p)
924 {
925 tstats (source_p);
926 }
927
928 static void
929 stats_uptime (struct Client *source_p)
930 {
931 time_t now;
932
933 now = CurrentTime - startup_time;
934 sendto_one_numeric(source_p, RPL_STATSUPTIME,
935 form_str (RPL_STATSUPTIME),
936 now / 86400, (now / 3600) % 24,
937 (now / 60) % 60, now % 60);
938 sendto_one_numeric(source_p, RPL_STATSCONN,
939 form_str (RPL_STATSCONN),
940 MaxConnectionCount, MaxClientCount,
941 Count.totalrestartcount);
942 }
943
944 struct shared_flags
945 {
946 int flag;
947 char letter;
948 };
949 static struct shared_flags shared_flagtable[] =
950 {
951 { SHARED_PKLINE, 'K' },
952 { SHARED_TKLINE, 'k' },
953 { SHARED_UNKLINE, 'U' },
954 { SHARED_PXLINE, 'X' },
955 { SHARED_TXLINE, 'x' },
956 { SHARED_UNXLINE, 'Y' },
957 { SHARED_PRESV, 'Q' },
958 { SHARED_TRESV, 'q' },
959 { SHARED_UNRESV, 'R' },
960 { SHARED_LOCOPS, 'L' },
961 { SHARED_REHASH, 'H' },
962 { 0, '\0'}
963 };
964
965
966 static void
967 stats_shared (struct Client *source_p)
968 {
969 struct remote_conf *shared_p;
970 dlink_node *ptr;
971 char buf[15];
972 char *p;
973 int i;
974
975 DLINK_FOREACH(ptr, shared_conf_list.head)
976 {
977 shared_p = ptr->data;
978
979 p = buf;
980
981 *p++ = 'c';
982
983 for(i = 0; shared_flagtable[i].flag != 0; i++)
984 {
985 if(shared_p->flags & shared_flagtable[i].flag)
986 *p++ = shared_flagtable[i].letter;
987 }
988
989 *p = '\0';
990
991 sendto_one_numeric(source_p, RPL_STATSULINE,
992 form_str(RPL_STATSULINE),
993 shared_p->server, shared_p->username,
994 shared_p->host, buf);
995 }
996
997 DLINK_FOREACH(ptr, cluster_conf_list.head)
998 {
999 shared_p = ptr->data;
1000
1001 p = buf;
1002
1003 *p++ = 'C';
1004
1005 for(i = 0; shared_flagtable[i].flag != 0; i++)
1006 {
1007 if(shared_p->flags & shared_flagtable[i].flag)
1008 *p++ = shared_flagtable[i].letter;
1009 }
1010
1011 *p = '\0';
1012
1013 sendto_one_numeric(source_p, RPL_STATSULINE,
1014 form_str(RPL_STATSULINE),
1015 shared_p->server, "*", "*", buf);
1016 }
1017 }
1018
1019 /* stats_servers()
1020 *
1021 * input - client pointer
1022 * output - none
1023 * side effects - client is shown lists of who connected servers
1024 */
1025 static void
1026 stats_servers (struct Client *source_p)
1027 {
1028 struct Client *target_p;
1029 dlink_node *ptr;
1030 time_t seconds;
1031 int days, hours, minutes;
1032 int j = 0;
1033
1034 if(ConfigServerHide.flatten_links && !IsOper(source_p) &&
1035 !IsExemptShide(source_p))
1036 {
1037 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
1038 form_str (ERR_NOPRIVILEGES));
1039 return;
1040 }
1041
1042 DLINK_FOREACH (ptr, serv_list.head)
1043 {
1044 target_p = ptr->data;
1045
1046 j++;
1047 seconds = CurrentTime - target_p->localClient->firsttime;
1048
1049 days = (int) (seconds / 86400);
1050 seconds %= 86400;
1051 hours = (int) (seconds / 3600);
1052 seconds %= 3600;
1053 minutes = (int) (seconds / 60);
1054 seconds %= 60;
1055
1056 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1057 "V :%s (%s!*@*) Idle: %d SendQ: %d "
1058 "Connected: %d day%s, %d:%02d:%02d",
1059 target_p->name,
1060 (target_p->serv->by[0] ? target_p->serv->by : "Remote."),
1061 (int) (CurrentTime - target_p->localClient->lasttime),
1062 (int) linebuf_len (&target_p->localClient->buf_sendq),
1063 days, (days == 1) ? "" : "s", hours, minutes,
1064 (int) seconds);
1065 }
1066
1067 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1068 "V :%d Server(s)", j);
1069 }
1070
1071 static void
1072 stats_tgecos(struct Client *source_p)
1073 {
1074 struct ConfItem *aconf;
1075 dlink_node *ptr;
1076
1077 DLINK_FOREACH(ptr, xline_conf_list.head)
1078 {
1079 aconf = ptr->data;
1080
1081 if(aconf->hold)
1082 sendto_one_numeric(source_p, RPL_STATSXLINE,
1083 form_str(RPL_STATSXLINE),
1084 'x', aconf->port, aconf->name,
1085 aconf->passwd);
1086 }
1087 }
1088
1089 static void
1090 stats_gecos(struct Client *source_p)
1091 {
1092 struct ConfItem *aconf;
1093 dlink_node *ptr;
1094
1095 DLINK_FOREACH(ptr, xline_conf_list.head)
1096 {
1097 aconf = ptr->data;
1098
1099 if(!aconf->hold)
1100 sendto_one_numeric(source_p, RPL_STATSXLINE,
1101 form_str(RPL_STATSXLINE),
1102 'X', aconf->port, aconf->name,
1103 aconf->passwd);
1104 }
1105 }
1106
1107 static void
1108 stats_class(struct Client *source_p)
1109 {
1110 if(ConfigFileEntry.stats_y_oper_only && !IsOper(source_p))
1111 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
1112 form_str (ERR_NOPRIVILEGES));
1113 else
1114 report_classes(source_p);
1115 }
1116
1117 static void
1118 stats_memory (struct Client *source_p)
1119 {
1120 count_memory (source_p);
1121 }
1122
1123 static void
1124 stats_ziplinks (struct Client *source_p)
1125 {
1126 dlink_node *ptr;
1127 struct Client *target_p;
1128 int sent_data = 0;
1129
1130 DLINK_FOREACH (ptr, serv_list.head)
1131 {
1132 target_p = ptr->data;
1133 if(IsCapable (target_p, CAP_ZIP))
1134 {
1135 /* we use memcpy(3) and a local copy of the structure to
1136 * work around a register use bug on GCC on the SPARC.
1137 * -jmallett, 04/27/2002
1138 */
1139 struct ZipStats zipstats;
1140 memcpy (&zipstats, &target_p->localClient->zipstats,
1141 sizeof (struct ZipStats));
1142 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1143 "Z :ZipLinks stats for %s send[%.2f%% compression "
1144 "(%lu kB data/%lu kB wire)] recv[%.2f%% compression "
1145 "(%lu kB data/%lu kB wire)]",
1146 target_p->name,
1147 zipstats.out_ratio, zipstats.outK, zipstats.outK_wire,
1148 zipstats.in_ratio, zipstats.inK, zipstats.inK_wire);
1149 sent_data++;
1150 }
1151 }
1152
1153 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1154 "Z :%u ziplink(s)", sent_data);
1155 }
1156
1157 static void
1158 stats_servlinks (struct Client *source_p)
1159 {
1160 static char Sformat[] = ":%s %d %s %s %u %u %u %u %u :%u %u %s";
1161 long uptime, sendK, receiveK;
1162 struct Client *target_p;
1163 dlink_node *ptr;
1164 int j = 0;
1165
1166 if(ConfigServerHide.flatten_links && !IsOper (source_p) &&
1167 !IsExemptShide(source_p))
1168 {
1169 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
1170 form_str (ERR_NOPRIVILEGES));
1171 return;
1172 }
1173
1174 sendK = receiveK = 0;
1175
1176 DLINK_FOREACH (ptr, serv_list.head)
1177 {
1178 target_p = ptr->data;
1179
1180 j++;
1181 sendK += target_p->localClient->sendK;
1182 receiveK += target_p->localClient->receiveK;
1183
1184 sendto_one(source_p, Sformat,
1185 get_id(&me, source_p), RPL_STATSLINKINFO, get_id(source_p, source_p),
1186 get_server_name(target_p, SHOW_IP),
1187 (int) linebuf_len (&target_p->localClient->buf_sendq),
1188 (int) target_p->localClient->sendM,
1189 (int) target_p->localClient->sendK,
1190 (int) target_p->localClient->receiveM,
1191 (int) target_p->localClient->receiveK,
1192 CurrentTime - target_p->localClient->firsttime,
1193 (CurrentTime > target_p->localClient->lasttime) ?
1194 (CurrentTime - target_p->localClient->lasttime) : 0,
1195 IsOper (source_p) ? show_capabilities (target_p) : "TS");
1196 }
1197
1198 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1199 "? :%u total server(s)", j);
1200
1201 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1202 "? :Sent total : %7.2f %s",
1203 _GMKv (sendK), _GMKs (sendK));
1204 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1205 "? :Recv total : %7.2f %s",
1206 _GMKv (receiveK), _GMKs (receiveK));
1207
1208 uptime = (CurrentTime - startup_time);
1209
1210 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1211 "? :Server send: %7.2f %s (%4.1f K/s)",
1212 _GMKv (me.localClient->sendK),
1213 _GMKs (me.localClient->sendK),
1214 (float) ((float) me.localClient->sendK / (float) uptime));
1215 sendto_one_numeric(source_p, RPL_STATSDEBUG,
1216 "? :Server recv: %7.2f %s (%4.1f K/s)",
1217 _GMKv (me.localClient->receiveK),
1218 _GMKs (me.localClient->receiveK),
1219 (float) ((float) me.localClient->receiveK / (float) uptime));
1220 }
1221
1222 static void
1223 stats_ltrace(struct Client *source_p, int parc, const char *parv[])
1224 {
1225 int doall = 0;
1226 int wilds = 0;
1227 const char *name;
1228 char statchar = parv[1][0];
1229
1230 /* this is def targeted at us somehow.. */
1231 if(parc > 2 && !EmptyString(parv[2]))
1232 {
1233 /* directed at us generically? */
1234 if(match(parv[2], me.name) ||
1235 (!MyClient(source_p) && !irccmp(parv[2], me.id)))
1236 {
1237 name = me.name;
1238 doall = 1;
1239 }
1240 else
1241 {
1242 name = parv[2];
1243 wilds = strchr(name, '*') || strchr(name, '?');
1244 }
1245
1246 /* must be directed at a specific person thats not us */
1247 if(!doall && !wilds)
1248 {
1249 struct Client *target_p;
1250
1251 if(MyClient(source_p))
1252 target_p = find_named_person(name);
1253 else
1254 target_p = find_person(name);
1255
1256 if(target_p != NULL)
1257 {
1258 stats_spy(source_p, statchar, target_p->name);
1259 stats_l_client(source_p, target_p, statchar);
1260 }
1261 else
1262 sendto_one_numeric(source_p, ERR_NOSUCHSERVER,
1263 form_str(ERR_NOSUCHSERVER),
1264 name);
1265
1266 return;
1267 }
1268 }
1269 else
1270 {
1271 name = me.name;
1272 doall = 1;
1273 }
1274
1275 stats_spy(source_p, statchar, name);
1276
1277 if(doall)
1278 {
1279 /* local opers get everyone */
1280 if(MyOper(source_p))
1281 {
1282 stats_l_list(source_p, name, doall, wilds, &unknown_list, statchar);
1283 stats_l_list(source_p, name, doall, wilds, &lclient_list, statchar);
1284 }
1285 else
1286 {
1287 /* they still need themselves if theyre local.. */
1288 if(MyClient(source_p))
1289 stats_l_client(source_p, source_p, statchar);
1290
1291 stats_l_list(source_p, name, doall, wilds, &local_oper_list, statchar);
1292 }
1293
1294 if (!ConfigServerHide.flatten_links || IsOper(source_p) ||
1295 IsExemptShide(source_p))
1296 stats_l_list(source_p, name, doall, wilds, &serv_list, statchar);
1297
1298 return;
1299 }
1300
1301 /* ok, at this point theyre looking for a specific client whos on
1302 * our server.. but it contains a wildcard. --fl
1303 */
1304 stats_l_list(source_p, name, doall, wilds, &lclient_list, statchar);
1305
1306 return;
1307 }
1308
1309
1310 static void
1311 stats_l_list(struct Client *source_p, const char *name, int doall, int wilds,
1312 dlink_list * list, char statchar)
1313 {
1314 dlink_node *ptr;
1315 struct Client *target_p;
1316
1317 /* send information about connections which match. note, we
1318 * dont need tests for IsInvisible(), because non-opers will
1319 * never get here for normal clients --fl
1320 */
1321 DLINK_FOREACH(ptr, list->head)
1322 {
1323 target_p = ptr->data;
1324
1325 if(!doall && wilds && !match(name, target_p->name))
1326 continue;
1327
1328 stats_l_client(source_p, target_p, statchar);
1329 }
1330 }
1331
1332 void
1333 stats_l_client(struct Client *source_p, struct Client *target_p,
1334 char statchar)
1335 {
1336 if(IsAnyServer(target_p))
1337 {
1338 sendto_one_numeric(source_p, RPL_STATSLINKINFO, Lformat,
1339 get_server_name(target_p, SHOW_IP),
1340 (int) linebuf_len(&target_p->localClient->buf_sendq),
1341 (int) target_p->localClient->sendM,
1342 (int) target_p->localClient->sendK,
1343 (int) target_p->localClient->receiveM,
1344 (int) target_p->localClient->receiveK,
1345 CurrentTime - target_p->localClient->firsttime,
1346 (CurrentTime > target_p->localClient->lasttime) ?
1347 (CurrentTime - target_p->localClient->lasttime) : 0,
1348 IsOper(source_p) ? show_capabilities(target_p) : "-");
1349 }
1350
1351 else
1352 {
1353 sendto_one_numeric(source_p, RPL_STATSLINKINFO, Lformat,
1354 show_ip(source_p, target_p) ?
1355 (IsUpper(statchar) ?
1356 get_client_name(target_p, SHOW_IP) :
1357 get_client_name(target_p, HIDE_IP)) :
1358 get_client_name(target_p, MASK_IP),
1359 (int) linebuf_len(&target_p->localClient->buf_sendq),
1360 (int) target_p->localClient->sendM,
1361 (int) target_p->localClient->sendK,
1362 (int) target_p->localClient->receiveM,
1363 (int) target_p->localClient->receiveK,
1364 CurrentTime - target_p->localClient->firsttime,
1365 (CurrentTime > target_p->localClient->lasttime) ?
1366 (CurrentTime - target_p->localClient->lasttime) : 0,
1367 "-");
1368 }
1369 }
1370
1371 /*
1372 * stats_spy
1373 *
1374 * inputs - pointer to client doing the /stats
1375 * - char letter they are doing /stats on
1376 * output - none
1377 * side effects -
1378 * This little helper function reports to opers if configured.
1379 * personally, I don't see why opers need to see stats requests
1380 * at all. They are just "noise" to an oper, and users can't do
1381 * any damage with stats requests now anyway. So, why show them?
1382 * -Dianora
1383 */
1384 static void
1385 stats_spy(struct Client *source_p, char statchar, const char *name)
1386 {
1387 hook_data_int data;
1388
1389 data.client = source_p;
1390 data.arg1 = name;
1391 data.arg2 = (int) statchar;
1392
1393 call_hook(doing_stats_hook, &data);
1394 }
1395
1396 /* stats_p_spy()
1397 *
1398 * input - pointer to client doing stats
1399 * ouput -
1400 * side effects - call hook doing_stats_p
1401 */
1402 static void
1403 stats_p_spy (struct Client *source_p)
1404 {
1405 hook_data data;
1406
1407 data.client = source_p;
1408 data.arg1 = data.arg2 = NULL;
1409
1410 call_hook(doing_stats_p_hook, &data);
1411 }
1412