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
88 #include "ircd_features.h"
89 #include "ircd_reply.h"
90 #include "ircd_string.h"
105 * m_trace - generic message handler
107 * parv[0] = sender prefix
108 * parv[1] = nick or servername
109 * parv[2] = 'target' servername
111 int m_trace(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
114 struct Client *acptr;
115 const struct ConnectionClass* cl;
118 int link_s[MAXCONNECTIONS];
119 int link_u[MAXCONNECTIONS];
124 if (feature_bool(FEAT_HIS_TRACE))
125 return send_reply(cptr, ERR_NOPRIVILEGES);
127 if (parc < 2 || BadPtr(parv[1]))
129 /* just "TRACE" without parameters. Must be from local client */
132 tname = cli_name(&me);
135 else if (parc < 3 || BadPtr(parv[2]))
137 /* No target specified. Make one before propagating. */
140 if ((acptr = find_match_server(parv[1])) ||
141 ((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)
156 /* Got "TRACE <tname> :<target>" */
158 if (MyUser(sptr) || Protocol(cptr) < 10)
159 acptr = find_match_server(parv[2]);
161 acptr = FindNServer(parv[2]);
162 if ((i = hunt_server_cmd(sptr, CMD_TRACE, cptr, 0, "%s :%C", 2, parc,
163 parv)) == HUNTED_NOSUCH)
168 if (i == HUNTED_PASS) {
170 acptr = next_client(GlobalClientList, tname);
172 acptr = cli_from(acptr);
173 send_reply(sptr, RPL_TRACELINK,
174 version, debugmode, tname,
175 acptr ? cli_name(cli_from(acptr)) : "<No_match>");
179 doall = (parv[1] && (parc > 1)) ? !match(tname, cli_name(&me)) : 1;
180 wilds = !parv[1] || strchr(tname, '*') || strchr(tname, '?');
181 dow = wilds || doall;
183 /* Don't give (long) remote listings to lusers */
184 if (dow && !MyConnect(sptr) && !IsAnOper(sptr))
187 for (i = 0; i < MAXCONNECTIONS; i++)
188 link_s[i] = 0, link_u[i] = 0;
191 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr)) {
193 link_u[cli_fd(cli_from(acptr))]++;
194 else if (IsServer(acptr))
195 link_s[cli_fd(cli_from(acptr))]++;
199 /* report all direct connections */
201 for (i = 0; i <= HighestFd; i++) {
202 const char *conClass;
204 if (!(acptr = LocalClientArray[i])) /* Local Connection? */
206 if (IsInvisible(acptr) && dow && !(MyConnect(sptr) && IsOper(sptr)) &&
207 !IsAnOper(acptr) && (acptr != sptr))
209 if (!doall && wilds && match(tname, cli_name(acptr)))
211 if (!dow && 0 != ircd_strcmp(tname, cli_name(acptr)))
214 conClass = get_client_class(acptr);
216 switch (cli_status(acptr)) {
217 case STAT_CONNECTING:
218 send_reply(sptr, RPL_TRACECONNECTING, conClass, cli_name(acptr));
222 send_reply(sptr, RPL_TRACEHANDSHAKE, conClass, cli_name(acptr));
228 case STAT_UNKNOWN_USER:
229 send_reply(sptr, RPL_TRACEUNKNOWN, conClass,
230 get_client_name(acptr, HIDE_IP));
233 case STAT_UNKNOWN_SERVER:
234 send_reply(sptr, RPL_TRACEUNKNOWN, conClass, "Unknown Server");
238 /* Only opers see users if there is a wildcard
239 but anyone can see all the opers. */
240 if ((IsAnOper(sptr) && (MyUser(sptr) ||
241 !(dow && IsInvisible(acptr)))) || !dow || IsAnOper(acptr)) {
243 send_reply(sptr, RPL_TRACEOPERATOR, conClass,
244 get_client_name(acptr, SHOW_IP),
245 CurrentTime - cli_lasttime(acptr));
247 send_reply(sptr, RPL_TRACEUSER, conClass,
248 get_client_name(acptr, SHOW_IP),
249 CurrentTime - cli_lasttime(acptr));
254 * Connection is a server
256 * Serv <class> <nS> <nC> <name> <ConnBy> <last> <age>
258 * class Class the server is in
259 * nS Number of servers reached via this link
260 * nC Number of clients reached via this link
261 * name Name of the server linked
262 * ConnBy Who established this link
263 * last Seconds since we got something from this link
264 * age Seconds this link has been alive
266 * Additional comments etc...... -Cym-<cym@acrux.net>
270 if (cli_serv(acptr)->user)
271 send_reply(sptr, RPL_TRACESERVER, conClass, link_s[i],
272 link_u[i], cli_name(acptr),
273 (*(cli_serv(acptr))->by) ? cli_serv(acptr)->by : "*",
274 cli_serv(acptr)->user->username, cli_serv(acptr)->user->host,
275 CurrentTime - cli_lasttime(acptr),
276 CurrentTime - cli_serv(acptr)->timestamp);
278 send_reply(sptr, RPL_TRACESERVER, conClass, link_s[i],
279 link_u[i], cli_name(acptr),
280 (*(cli_serv(acptr))->by) ? cli_serv(acptr)->by : "*", "*",
281 cli_name(&me), CurrentTime - cli_lasttime(acptr),
282 CurrentTime - cli_serv(acptr)->timestamp);
285 default: /* We actually shouldn't come here, -msa */
286 send_reply(sptr, RPL_TRACENEWTYPE, get_client_name(acptr, HIDE_IP));
292 * Add these lines to summarize the above which can get rather long
293 * and messy when done remotely - Avalon
295 if (!IsAnOper(sptr) || !cnt) {
297 /* let the user have some idea that its at the end of the trace */
298 send_reply(sptr, RPL_TRACESERVER, 0, link_s[cli_fd(&me)],
299 link_u[cli_fd(&me)], "<No_match>", *(cli_serv(&me)->by) ?
300 cli_serv(&me)->by : "*", "*", cli_name(&me), 0, 0);
304 for (cl = get_class_list(); cl; cl = cl->next) {
306 send_reply(sptr, RPL_TRACECLASS, ConClass(cl), Links(cl));
313 * ms_trace - server message handler
315 * parv[0] = sender prefix
316 * parv[1] = nick or servername
317 * parv[2] = 'target' servername
319 int ms_trace(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
322 struct Client *acptr;
323 const struct ConnectionClass* cl;
326 int link_s[MAXCONNECTIONS];
327 int link_u[MAXCONNECTIONS];
332 if (parc < 2 || BadPtr(parv[1])) {
333 /* just "TRACE" without parameters. Must be from local client */
336 tname = cli_name(&me);
338 } else if (parc < 3 || BadPtr(parv[2])) {
339 /* No target specified. Make one before propagating. */
342 if ((acptr = find_match_server(parv[1])) ||
343 ((acptr = FindClient(parv[1])) && !MyUser(acptr))) {
345 parv[2] = cli_name(cli_user(acptr)->server);
347 parv[2] = cli_name(acptr);
351 if ((i = hunt_server_cmd(sptr, CMD_TRACE, cptr, IsServer(acptr),
352 "%s :%C", 2, parc, parv)) == HUNTED_NOSUCH)
357 /* Got "TRACE <tname> :<target>" */
359 if (MyUser(sptr) || Protocol(cptr) < 10)
360 acptr = find_match_server(parv[2]);
362 acptr = FindNServer(parv[2]);
363 if ((i = hunt_server_cmd(sptr, CMD_TRACE, cptr, 0, "%s :%C", 2, parc,
364 parv)) == HUNTED_NOSUCH)
369 if (i == HUNTED_PASS) {
371 acptr = next_client(GlobalClientList, tname);
373 acptr = cli_from(acptr);
374 send_reply(sptr, RPL_TRACELINK,
375 version, debugmode, tname,
376 acptr ? cli_name(cli_from(acptr)) : "<No_match>");
380 doall = (parv[1] && (parc > 1)) ? !match(tname, cli_name(&me)) : 1;
381 wilds = !parv[1] || strchr(tname, '*') || strchr(tname, '?');
382 dow = wilds || doall;
384 /* Don't give (long) remote listings to lusers */
385 if (dow && !MyConnect(sptr) && !IsAnOper(sptr))
388 for (i = 0; i < MAXCONNECTIONS; i++)
389 link_s[i] = 0, link_u[i] = 0;
392 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr)) {
394 link_u[cli_fd(cli_from(acptr))]++;
395 else if (IsServer(acptr))
396 link_s[cli_fd(cli_from(acptr))]++;
400 /* report all direct connections */
402 for (i = 0; i <= HighestFd; i++) {
403 const char *conClass;
405 if (!(acptr = LocalClientArray[i])) /* Local Connection? */
407 if (IsInvisible(acptr) && dow && !(MyConnect(sptr) && IsOper(sptr)) &&
408 !IsAnOper(acptr) && (acptr != sptr))
410 if (!doall && wilds && match(tname, cli_name(acptr)))
412 if (!dow && 0 != ircd_strcmp(tname, cli_name(acptr)))
414 conClass = get_client_class(acptr);
416 switch (cli_status(acptr)) {
417 case STAT_CONNECTING:
418 send_reply(sptr, RPL_TRACECONNECTING, conClass, cli_name(acptr));
422 send_reply(sptr, RPL_TRACEHANDSHAKE, conClass, cli_name(acptr));
428 case STAT_UNKNOWN_USER:
429 send_reply(sptr, RPL_TRACEUNKNOWN, conClass,
430 get_client_name(acptr, HIDE_IP));
433 case STAT_UNKNOWN_SERVER:
434 send_reply(sptr, RPL_TRACEUNKNOWN, conClass, "Unknown Server");
438 /* Only opers see users if there is a wildcard
439 but anyone can see all the opers. */
440 if ((IsAnOper(sptr) && (MyUser(sptr) ||
441 !(dow && IsInvisible(acptr)))) || !dow || IsAnOper(acptr)) {
443 send_reply(sptr, RPL_TRACEOPERATOR, conClass,
444 get_client_name(acptr, SHOW_IP),
445 CurrentTime - cli_lasttime(acptr));
447 send_reply(sptr, RPL_TRACEUSER, conClass,
448 get_client_name(acptr, SHOW_IP),
449 CurrentTime - cli_lasttime(acptr));
454 * Connection is a server
456 * Serv <class> <nS> <nC> <name> <ConnBy> <last> <age>
458 * class Class the server is in
459 * nS Number of servers reached via this link
460 * nC Number of clients reached via this link
461 * name Name of the server linked
462 * ConnBy Who established this link
463 * last Seconds since we got something from this link
464 * age Seconds this link has been alive
466 * Additional comments etc...... -Cym-<cym@acrux.net>
470 if (cli_serv(acptr)->user)
471 send_reply(sptr, RPL_TRACESERVER, conClass, link_s[i],
472 link_u[i], cli_name(acptr),
473 (*(cli_serv(acptr))->by) ? cli_serv(acptr)->by : "*",
474 cli_serv(acptr)->user->username, cli_serv(acptr)->user->host,
475 CurrentTime - cli_lasttime(acptr),
476 CurrentTime - cli_serv(acptr)->timestamp);
478 send_reply(sptr, RPL_TRACESERVER, conClass, link_s[i],
479 link_u[i], cli_name(acptr),
480 (*(cli_serv(acptr))->by) ? cli_serv(acptr)->by : "*", "*",
481 cli_name(&me), CurrentTime - cli_lasttime(acptr),
482 CurrentTime - cli_serv(acptr)->timestamp);
485 default: /* We actually shouldn't come here, -msa */
486 send_reply(sptr, RPL_TRACENEWTYPE, get_client_name(acptr, HIDE_IP));
492 * Add these lines to summarize the above which can get rather long
493 * and messy when done remotely - Avalon
495 if (!IsAnOper(sptr) || !cnt) {
497 /* let the user have some idea that its at the end of the trace */
498 send_reply(sptr, RPL_TRACESERVER, 0, link_s[cli_fd(&me)],
499 link_u[cli_fd(&me)], "<No_match>", *(cli_serv(&me)->by) ?
500 cli_serv(&me)->by : "*", "*", cli_name(&me), 0, 0);
504 for (cl = get_class_list(); cl; cl = cl->next) {
506 send_reply(sptr, RPL_TRACECLASS, ConClass(cl), Links(cl));
513 * mo_trace - oper message handler
515 * parv[0] = sender prefix
516 * parv[1] = nick or servername
517 * parv[2] = 'target' servername
519 int mo_trace(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
522 struct Client* acptr;
523 const struct ConnectionClass* cl;
526 int link_s[MAXCONNECTIONS];
527 int link_u[MAXCONNECTIONS];
532 if (parc < 2 || BadPtr(parv[1])) {
533 /* just "TRACE" without parameters. Must be from local client */
536 tname = cli_name(&me);
538 } else if (parc < 3 || BadPtr(parv[2])) {
539 /* No target specified. Make one before propagating. */
542 if ((acptr = find_match_server(parv[1])) ||
543 ((acptr = FindClient(parv[1])) && !MyUser(acptr))) {
545 parv[2] = cli_name(cli_user(acptr)->server);
547 parv[2] = cli_name(acptr);
550 if ((i = hunt_server_cmd(sptr, CMD_TRACE, cptr, IsServer(acptr),
551 "%s :%C", 2, parc, parv)) == HUNTED_NOSUCH)
556 /* Got "TRACE <tname> :<target>" */
558 if (MyUser(sptr) || Protocol(cptr) < 10)
559 acptr = find_match_server(parv[2]);
561 acptr = FindNServer(parv[2]);
562 if ((i = hunt_server_cmd(sptr, CMD_TRACE, cptr, 0, "%s :%C", 2, parc,
563 parv)) == HUNTED_NOSUCH)
568 if (i == HUNTED_PASS) {
570 acptr = next_client(GlobalClientList, tname);
572 acptr = cli_from(acptr);
573 send_reply(sptr, RPL_TRACELINK,
574 version, debugmode, tname,
575 acptr ? cli_name(cli_from(acptr)) : "<No_match>");
579 doall = (parv[1] && (parc > 1)) ? !match(tname, cli_name(&me)) : 1;
580 wilds = !parv[1] || strchr(tname, '*') || strchr(tname, '?');
581 dow = wilds || doall;
583 /* Don't give (long) remote listings to lusers */
584 if (dow && !MyConnect(sptr) && !IsAnOper(sptr))
587 for (i = 0; i < MAXCONNECTIONS; i++)
588 link_s[i] = 0, link_u[i] = 0;
591 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr)) {
593 link_u[cli_fd(cli_from(acptr))]++;
594 else if (IsServer(acptr))
595 link_s[cli_fd(cli_from(acptr))]++;
599 /* report all direct connections */
601 for (i = 0; i <= HighestFd; i++) {
602 const char *conClass;
604 if (!(acptr = LocalClientArray[i])) /* Local Connection? */
606 if (IsInvisible(acptr) && dow && !(MyConnect(sptr) && IsOper(sptr)) &&
607 !IsAnOper(acptr) && (acptr != sptr))
609 if (!doall && wilds && match(tname, cli_name(acptr)))
611 if (!dow && 0 != ircd_strcmp(tname, cli_name(acptr)))
613 conClass = get_client_class(acptr);
615 switch (cli_status(acptr)) {
616 case STAT_CONNECTING:
617 send_reply(sptr, RPL_TRACECONNECTING, conClass, cli_name(acptr));
621 send_reply(sptr, RPL_TRACEHANDSHAKE, conClass, cli_name(acptr));
627 case STAT_UNKNOWN_USER:
628 send_reply(sptr, RPL_TRACEUNKNOWN, conClass,
629 get_client_name(acptr, HIDE_IP));
632 case STAT_UNKNOWN_SERVER:
633 send_reply(sptr, RPL_TRACEUNKNOWN, conClass, "Unknown Server");
637 /* Only opers see users if there is a wildcard
638 but anyone can see all the opers. */
639 if ((IsAnOper(sptr) && (MyUser(sptr) ||
640 !(dow && IsInvisible(acptr)))) || !dow || IsAnOper(acptr)) {
642 send_reply(sptr, RPL_TRACEOPERATOR, conClass,
643 get_client_name(acptr, SHOW_IP),
644 CurrentTime - cli_lasttime(acptr));
646 send_reply(sptr, RPL_TRACEUSER, conClass,
647 get_client_name(acptr, SHOW_IP),
648 CurrentTime - cli_lasttime(acptr));
653 * Connection is a server
655 * Serv <class> <nS> <nC> <name> <ConnBy> <last> <age>
657 * class Class the server is in
658 * nS Number of servers reached via this link
659 * nC Number of clients reached via this link
660 * name Name of the server linked
661 * ConnBy Who established this link
662 * last Seconds since we got something from this link
663 * age Seconds this link has been alive
665 * Additional comments etc...... -Cym-<cym@acrux.net>
669 if (cli_serv(acptr)->user)
670 send_reply(sptr, RPL_TRACESERVER, conClass, link_s[i],
671 link_u[i], cli_name(acptr),
672 (*(cli_serv(acptr))->by) ? cli_serv(acptr)->by : "*",
673 cli_serv(acptr)->user->username, cli_serv(acptr)->user->host,
674 CurrentTime - cli_lasttime(acptr),
675 CurrentTime - cli_serv(acptr)->timestamp);
677 send_reply(sptr, RPL_TRACESERVER, conClass, link_s[i],
678 link_u[i], cli_name(acptr),
679 (*(cli_serv(acptr))->by) ? cli_serv(acptr)->by : "*", "*",
680 cli_name(&me), CurrentTime - cli_lasttime(acptr),
681 CurrentTime - cli_serv(acptr)->timestamp);
684 default: /* We actually shouldn't come here, -msa */
685 send_reply(sptr, RPL_TRACENEWTYPE, get_client_name(acptr, HIDE_IP));
691 * Add these lines to summarize the above which can get rather long
692 * and messy when done remotely - Avalon
694 if (!IsAnOper(sptr) || !cnt) {
696 /* let the user have some idea that its at the end of the trace */
697 send_reply(sptr, RPL_TRACESERVER, 0, link_s[cli_fd(&me)],
698 link_u[cli_fd(&me)], "<No_match>", *(cli_serv(&me)->by) ?
699 cli_serv(&me)->by : "*", "*", cli_name(&me), 0, 0);
703 for (cl = get_class_list(); cl; cl = cl->next) {
705 send_reply(sptr, RPL_TRACECLASS, ConClass(cl), Links(cl));