2 * IRC - Internet Relay Chat, ircd/m_trace.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * See file AUTHORS in IRC package for additional names of
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 1, or (at your option)
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * m_functions execute protocol messages on this server:
29 * cptr is always NON-NULL, pointing to a *LOCAL* client
30 * structure (with an open socket connected!). This
31 * identifies the physical socket where the message
32 * originated (or which caused the m_function to be
33 * executed--some m_functions may call others...).
35 * sptr is the source of the message, defined by the
36 * prefix part of the message if present. If not
37 * or prefix not found, then sptr==cptr.
39 * (!IsServer(cptr)) => (cptr == sptr), because
40 * prefixes are taken *only* from servers...
43 * (sptr == cptr) => the message didn't
46 * (sptr != cptr && IsServer(sptr) means
47 * the prefix specified servername. (?)
49 * (sptr != cptr && !IsServer(sptr) means
50 * that message originated from a remote
55 * (!IsServer(sptr)) means that, sptr can safely
56 * taken as defining the target structure of the
57 * message in this server.
59 * *Always* true (if 'parse' and others are working correct):
61 * 1) sptr->from == cptr (note: cptr->from == cptr)
63 * 2) MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64 * *cannot* be a local connection, unless it's
65 * actually cptr!). [MyConnect(x) should probably
66 * be defined as (x == x->from) --msa ]
68 * parc number of variable parameter strings (if zero,
69 * parv is allowed to be NULL)
71 * parv a NULL terminated list of parameter pointers,
73 * parv[0], sender (prefix string), if not present
74 * this points to an empty string.
75 * parv[1]...parv[parc-1]
76 * pointers to additional parameters
77 * parv[parc] == NULL, *always*
79 * note: it is guaranteed that parv[0]..parv[parc-1] are all
86 * No need to include handlers.h here the signatures must match
87 * and we don't need to force a rebuild of all the handlers everytime
88 * we add a new one to the list. --Bleep
96 #include "ircd_reply.h"
97 #include "ircd_string.h"
101 #include "numnicks.h"
112 * m_trace - generic message handler
114 * parv[0] = sender prefix
115 * parv[1] = nick or servername
116 * parv[2] = 'target' servername
118 int m_trace(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
121 struct Client *acptr;
122 const struct ConnectionClass* cl;
125 int link_s[MAXCONNECTIONS];
126 int link_u[MAXCONNECTIONS];
131 if (parc < 2 || BadPtr(parv[1])) {
132 /* just "TRACE" without parameters. Must be from local client */
135 tname = cli_name(&me);
137 } else if (parc < 3 || BadPtr(parv[2])) {
138 /* No target specified. Make one before propagating. */
141 if ((acptr = find_match_server(parv[1])) ||
142 ((acptr = FindClient(parv[1])) && !MyUser(acptr))) {
144 parv[2] = cli_name(cli_user(acptr)->server);
146 parv[2] = cli_name(acptr);
149 if ((i = hunt_server_cmd(sptr, CMD_TRACE, cptr, IsServer(acptr),
150 "%s :%C", 2, parc, parv)) == HUNTED_NOSUCH)
155 /* Got "TRACE <tname> :<target>" */
157 if (MyUser(sptr) || Protocol(cptr) < 10)
158 acptr = find_match_server(parv[2]);
160 acptr = FindNServer(parv[2]);
161 if ((i = hunt_server_cmd(sptr, CMD_TRACE, cptr, 0, "%s :%C", 2, parc,
162 parv)) == HUNTED_NOSUCH)
167 if (i == HUNTED_PASS) {
169 acptr = next_client(GlobalClientList, tname);
171 acptr = cli_from(acptr);
172 send_reply(sptr, RPL_TRACELINK,
173 version, debugmode, tname,
174 acptr ? cli_name(cli_from(acptr)) : "<No_match>");
178 doall = (parv[1] && (parc > 1)) ? !match(tname, cli_name(&me)) : 1;
179 wilds = !parv[1] || strchr(tname, '*') || strchr(tname, '?');
180 dow = wilds || doall;
182 /* Don't give (long) remote listings to lusers */
183 if (dow && !MyConnect(sptr) && !IsAnOper(sptr))
186 for (i = 0; i < MAXCONNECTIONS; i++)
187 link_s[i] = 0, link_u[i] = 0;
190 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr)) {
192 link_u[cli_fd(cli_from(acptr))]++;
193 else if (IsServer(acptr))
194 link_s[cli_fd(cli_from(acptr))]++;
198 /* report all direct connections */
200 for (i = 0; i <= HighestFd; i++) {
201 unsigned int conClass;
203 if (!(acptr = LocalClientArray[i])) /* Local Connection? */
205 if (IsInvisible(acptr) && dow && !(MyConnect(sptr) && IsOper(sptr)) &&
206 !IsAnOper(acptr) && (acptr != sptr))
208 if (!doall && wilds && match(tname, cli_name(acptr)))
210 if (!dow && 0 != ircd_strcmp(tname, cli_name(acptr)))
213 conClass = get_client_class(acptr);
215 switch (cli_status(acptr)) {
216 case STAT_CONNECTING:
217 send_reply(sptr, RPL_TRACECONNECTING, conClass, cli_name(acptr));
221 send_reply(sptr, RPL_TRACEHANDSHAKE, conClass, cli_name(acptr));
227 case STAT_UNKNOWN_USER:
228 send_reply(sptr, RPL_TRACEUNKNOWN, conClass,
229 get_client_name(acptr, HIDE_IP));
232 case STAT_UNKNOWN_SERVER:
233 send_reply(sptr, RPL_TRACEUNKNOWN, conClass, "Unknown Server");
237 /* Only opers see users if there is a wildcard
238 but anyone can see all the opers. */
239 if ((IsAnOper(sptr) && (MyUser(sptr) ||
240 !(dow && IsInvisible(acptr)))) || !dow || IsAnOper(acptr)) {
242 send_reply(sptr, RPL_TRACEOPERATOR, conClass,
243 get_client_name(acptr, SHOW_IP),
244 CurrentTime - cli_lasttime(acptr));
246 send_reply(sptr, RPL_TRACEUSER, conClass,
247 get_client_name(acptr, SHOW_IP),
248 CurrentTime - cli_lasttime(acptr));
253 * Connection is a server
255 * Serv <class> <nS> <nC> <name> <ConnBy> <last> <age>
257 * class Class the server is in
258 * nS Number of servers reached via this link
259 * nC Number of clients reached via this link
260 * name Name of the server linked
261 * ConnBy Who established this link
262 * last Seconds since we got something from this link
263 * age Seconds this link has been alive
265 * Additional comments etc...... -Cym-<cym@acrux.net>
269 if (cli_serv(acptr)->user)
270 send_reply(sptr, RPL_TRACESERVER, conClass, link_s[i],
271 link_u[i], cli_name(acptr),
272 (*(cli_serv(acptr))->by) ? cli_serv(acptr)->by : "*",
273 cli_serv(acptr)->user->username, cli_serv(acptr)->user->host,
274 CurrentTime - cli_lasttime(acptr),
275 CurrentTime - cli_serv(acptr)->timestamp);
277 send_reply(sptr, RPL_TRACESERVER, conClass, link_s[i],
278 link_u[i], cli_name(acptr),
279 (*(cli_serv(acptr))->by) ? cli_serv(acptr)->by : "*", "*",
280 cli_name(&me), CurrentTime - cli_lasttime(acptr),
281 CurrentTime - cli_serv(acptr)->timestamp);
284 default: /* We actually shouldn't come here, -msa */
285 send_reply(sptr, RPL_TRACENEWTYPE, get_client_name(acptr, HIDE_IP));
291 * Add these lines to summarize the above which can get rather long
292 * and messy when done remotely - Avalon
294 if (!IsAnOper(sptr) || !cnt) {
296 /* let the user have some idea that its at the end of the trace */
297 send_reply(sptr, RPL_TRACESERVER, 0, link_s[cli_fd(&me)],
298 link_u[cli_fd(&me)], "<No_match>", *(cli_serv(&me)->by) ?
299 cli_serv(&me)->by : "*", "*", cli_name(&me), 0, 0);
303 for (cl = get_class_list(); cl; cl = cl->next) {
305 send_reply(sptr, RPL_TRACECLASS, ConClass(cl), Links(cl));
312 * ms_trace - server message handler
314 * parv[0] = sender prefix
315 * parv[1] = nick or servername
316 * parv[2] = 'target' servername
318 int ms_trace(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
321 struct Client *acptr;
322 const struct ConnectionClass* cl;
325 int link_s[MAXCONNECTIONS];
326 int link_u[MAXCONNECTIONS];
331 if (parc < 2 || BadPtr(parv[1])) {
332 /* just "TRACE" without parameters. Must be from local client */
335 tname = cli_name(&me);
337 } else if (parc < 3 || BadPtr(parv[2])) {
338 /* No target specified. Make one before propagating. */
341 if ((acptr = find_match_server(parv[1])) ||
342 ((acptr = FindClient(parv[1])) && !MyUser(acptr))) {
344 parv[2] = cli_name(cli_user(acptr)->server);
346 parv[2] = cli_name(acptr);
350 if ((i = hunt_server_cmd(sptr, CMD_TRACE, cptr, IsServer(acptr),
351 "%s :%C", 2, parc, parv)) == HUNTED_NOSUCH)
356 /* Got "TRACE <tname> :<target>" */
358 if (MyUser(sptr) || Protocol(cptr) < 10)
359 acptr = find_match_server(parv[2]);
361 acptr = FindNServer(parv[2]);
362 if ((i = hunt_server_cmd(sptr, CMD_TRACE, cptr, 0, "%s :%C", 2, parc,
363 parv)) == HUNTED_NOSUCH)
368 if (i == HUNTED_PASS) {
370 acptr = next_client(GlobalClientList, tname);
372 acptr = cli_from(acptr);
373 send_reply(sptr, RPL_TRACELINK,
374 version, debugmode, tname,
375 acptr ? cli_name(cli_from(acptr)) : "<No_match>");
379 doall = (parv[1] && (parc > 1)) ? !match(tname, cli_name(&me)) : 1;
380 wilds = !parv[1] || strchr(tname, '*') || strchr(tname, '?');
381 dow = wilds || doall;
383 /* Don't give (long) remote listings to lusers */
384 if (dow && !MyConnect(sptr) && !IsAnOper(sptr))
387 for (i = 0; i < MAXCONNECTIONS; i++)
388 link_s[i] = 0, link_u[i] = 0;
391 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr)) {
393 link_u[cli_fd(cli_from(acptr))]++;
394 else if (IsServer(acptr))
395 link_s[cli_fd(cli_from(acptr))]++;
399 /* report all direct connections */
401 for (i = 0; i <= HighestFd; i++) {
402 unsigned int conClass;
404 if (!(acptr = LocalClientArray[i])) /* Local Connection? */
406 if (IsInvisible(acptr) && dow && !(MyConnect(sptr) && IsOper(sptr)) &&
407 !IsAnOper(acptr) && (acptr != sptr))
409 if (!doall && wilds && match(tname, cli_name(acptr)))
411 if (!dow && 0 != ircd_strcmp(tname, cli_name(acptr)))
413 conClass = get_client_class(acptr);
415 switch (cli_status(acptr)) {
416 case STAT_CONNECTING:
417 send_reply(sptr, RPL_TRACECONNECTING, conClass, cli_name(acptr));
421 send_reply(sptr, RPL_TRACEHANDSHAKE, conClass, cli_name(acptr));
427 case STAT_UNKNOWN_USER:
428 send_reply(sptr, RPL_TRACEUNKNOWN, conClass,
429 get_client_name(acptr, HIDE_IP));
432 case STAT_UNKNOWN_SERVER:
433 send_reply(sptr, RPL_TRACEUNKNOWN, conClass, "Unknown Server");
437 /* Only opers see users if there is a wildcard
438 but anyone can see all the opers. */
439 if ((IsAnOper(sptr) && (MyUser(sptr) ||
440 !(dow && IsInvisible(acptr)))) || !dow || IsAnOper(acptr)) {
442 send_reply(sptr, RPL_TRACEOPERATOR, conClass,
443 get_client_name(acptr, SHOW_IP),
444 CurrentTime - cli_lasttime(acptr));
446 send_reply(sptr, RPL_TRACEUSER, conClass,
447 get_client_name(acptr, SHOW_IP),
448 CurrentTime - cli_lasttime(acptr));
453 * Connection is a server
455 * Serv <class> <nS> <nC> <name> <ConnBy> <last> <age>
457 * class Class the server is in
458 * nS Number of servers reached via this link
459 * nC Number of clients reached via this link
460 * name Name of the server linked
461 * ConnBy Who established this link
462 * last Seconds since we got something from this link
463 * age Seconds this link has been alive
465 * Additional comments etc...... -Cym-<cym@acrux.net>
469 if (cli_serv(acptr)->user)
470 send_reply(sptr, RPL_TRACESERVER, conClass, link_s[i],
471 link_u[i], cli_name(acptr),
472 (*(cli_serv(acptr))->by) ? cli_serv(acptr)->by : "*",
473 cli_serv(acptr)->user->username, cli_serv(acptr)->user->host,
474 CurrentTime - cli_lasttime(acptr),
475 CurrentTime - cli_serv(acptr)->timestamp);
477 send_reply(sptr, RPL_TRACESERVER, conClass, link_s[i],
478 link_u[i], cli_name(acptr),
479 (*(cli_serv(acptr))->by) ? cli_serv(acptr)->by : "*", "*",
480 cli_name(&me), CurrentTime - cli_lasttime(acptr),
481 CurrentTime - cli_serv(acptr)->timestamp);
484 default: /* We actually shouldn't come here, -msa */
485 send_reply(sptr, RPL_TRACENEWTYPE, get_client_name(acptr, HIDE_IP));
491 * Add these lines to summarize the above which can get rather long
492 * and messy when done remotely - Avalon
494 if (!IsAnOper(sptr) || !cnt) {
496 /* let the user have some idea that its at the end of the trace */
497 send_reply(sptr, RPL_TRACESERVER, 0, link_s[cli_fd(&me)],
498 link_u[cli_fd(&me)], "<No_match>", *(cli_serv(&me)->by) ?
499 cli_serv(&me)->by : "*", "*", cli_name(&me), 0, 0);
503 for (cl = get_class_list(); cl; cl = cl->next) {
505 send_reply(sptr, RPL_TRACECLASS, ConClass(cl), Links(cl));
512 * mo_trace - oper message handler
514 * parv[0] = sender prefix
515 * parv[1] = nick or servername
516 * parv[2] = 'target' servername
518 int mo_trace(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
521 struct Client* acptr;
522 const struct ConnectionClass* cl;
525 int link_s[MAXCONNECTIONS];
526 int link_u[MAXCONNECTIONS];
531 if (parc < 2 || BadPtr(parv[1])) {
532 /* just "TRACE" without parameters. Must be from local client */
535 tname = cli_name(&me);
537 } else if (parc < 3 || BadPtr(parv[2])) {
538 /* No target specified. Make one before propagating. */
541 if ((acptr = find_match_server(parv[1])) ||
542 ((acptr = FindClient(parv[1])) && !MyUser(acptr))) {
544 parv[2] = cli_name(cli_user(acptr)->server);
546 parv[2] = cli_name(acptr);
549 if ((i = hunt_server_cmd(sptr, CMD_TRACE, cptr, IsServer(acptr),
550 "%s :%C", 2, parc, parv)) == HUNTED_NOSUCH)
555 /* Got "TRACE <tname> :<target>" */
557 if (MyUser(sptr) || Protocol(cptr) < 10)
558 acptr = find_match_server(parv[2]);
560 acptr = FindNServer(parv[2]);
561 if ((i = hunt_server_cmd(sptr, CMD_TRACE, cptr, 0, "%s :%C", 2, parc,
562 parv)) == HUNTED_NOSUCH)
567 if (i == HUNTED_PASS) {
569 acptr = next_client(GlobalClientList, tname);
571 acptr = cli_from(acptr);
572 send_reply(sptr, RPL_TRACELINK,
573 version, debugmode, tname,
574 acptr ? cli_name(cli_from(acptr)) : "<No_match>");
578 doall = (parv[1] && (parc > 1)) ? !match(tname, cli_name(&me)) : 1;
579 wilds = !parv[1] || strchr(tname, '*') || strchr(tname, '?');
580 dow = wilds || doall;
582 /* Don't give (long) remote listings to lusers */
583 if (dow && !MyConnect(sptr) && !IsAnOper(sptr))
586 for (i = 0; i < MAXCONNECTIONS; i++)
587 link_s[i] = 0, link_u[i] = 0;
590 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr)) {
592 link_u[cli_fd(cli_from(acptr))]++;
593 else if (IsServer(acptr))
594 link_s[cli_fd(cli_from(acptr))]++;
598 /* report all direct connections */
600 for (i = 0; i <= HighestFd; i++) {
601 unsigned int conClass;
603 if (!(acptr = LocalClientArray[i])) /* Local Connection? */
605 if (IsInvisible(acptr) && dow && !(MyConnect(sptr) && IsOper(sptr)) &&
606 !IsAnOper(acptr) && (acptr != sptr))
608 if (!doall && wilds && match(tname, cli_name(acptr)))
610 if (!dow && 0 != ircd_strcmp(tname, cli_name(acptr)))
612 conClass = get_client_class(acptr);
614 switch (cli_status(acptr)) {
615 case STAT_CONNECTING:
616 send_reply(sptr, RPL_TRACECONNECTING, conClass, cli_name(acptr));
620 send_reply(sptr, RPL_TRACEHANDSHAKE, conClass, cli_name(acptr));
626 case STAT_UNKNOWN_USER:
627 send_reply(sptr, RPL_TRACEUNKNOWN, conClass,
628 get_client_name(acptr, HIDE_IP));
631 case STAT_UNKNOWN_SERVER:
632 send_reply(sptr, RPL_TRACEUNKNOWN, conClass, "Unknown Server");
636 /* Only opers see users if there is a wildcard
637 but anyone can see all the opers. */
638 if ((IsAnOper(sptr) && (MyUser(sptr) ||
639 !(dow && IsInvisible(acptr)))) || !dow || IsAnOper(acptr)) {
641 send_reply(sptr, RPL_TRACEOPERATOR, conClass,
642 get_client_name(acptr, SHOW_IP),
643 CurrentTime - cli_lasttime(acptr));
645 send_reply(sptr, RPL_TRACEUSER, conClass,
646 get_client_name(acptr, SHOW_IP),
647 CurrentTime - cli_lasttime(acptr));
652 * Connection is a server
654 * Serv <class> <nS> <nC> <name> <ConnBy> <last> <age>
656 * class Class the server is in
657 * nS Number of servers reached via this link
658 * nC Number of clients reached via this link
659 * name Name of the server linked
660 * ConnBy Who established this link
661 * last Seconds since we got something from this link
662 * age Seconds this link has been alive
664 * Additional comments etc...... -Cym-<cym@acrux.net>
668 if (cli_serv(acptr)->user)
669 send_reply(sptr, RPL_TRACESERVER, conClass, link_s[i],
670 link_u[i], cli_name(acptr),
671 (*(cli_serv(acptr))->by) ? cli_serv(acptr)->by : "*",
672 cli_serv(acptr)->user->username, cli_serv(acptr)->user->host,
673 CurrentTime - cli_lasttime(acptr),
674 CurrentTime - cli_serv(acptr)->timestamp);
676 send_reply(sptr, RPL_TRACESERVER, conClass, link_s[i],
677 link_u[i], cli_name(acptr),
678 (*(cli_serv(acptr))->by) ? cli_serv(acptr)->by : "*", "*",
679 cli_name(&me), CurrentTime - cli_lasttime(acptr),
680 CurrentTime - cli_serv(acptr)->timestamp);
683 default: /* We actually shouldn't come here, -msa */
684 send_reply(sptr, RPL_TRACENEWTYPE, get_client_name(acptr, HIDE_IP));
690 * Add these lines to summarize the above which can get rather long
691 * and messy when done remotely - Avalon
693 if (!IsAnOper(sptr) || !cnt) {
695 /* let the user have some idea that its at the end of the trace */
696 send_reply(sptr, RPL_TRACESERVER, 0, link_s[cli_fd(&me)],
697 link_u[cli_fd(&me)], "<No_match>", *(cli_serv(&me)->by) ?
698 cli_serv(&me)->by : "*", "*", cli_name(&me), 0, 0);
702 for (cl = get_class_list(); cl; cl = cl->next) {
704 send_reply(sptr, RPL_TRACECLASS, ConClass(cl), Links(cl));