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"
90 #include "ircd_reply.h"
91 #include "ircd_string.h"
102 /* #include <assert.h> -- Now using assert in ircd_log.h */
105 void do_trace(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
108 struct Client *acptr;
109 const struct ConnectionClass* cl;
112 int link_s[MAXCONNECTIONS];
113 int link_u[MAXCONNECTIONS];
118 if (parc < 2 || BadPtr(parv[1]))
120 /* just "TRACE" without parameters. Must be from local client */
123 tname = cli_name(&me);
126 else if (parc < 3 || BadPtr(parv[2]))
128 /* No target specified. Make one before propagating. */
131 if ((acptr = find_match_server(parv[1])) ||
132 ((acptr = FindClient(parv[1])) && !MyUser(acptr)))
135 parv[2] = cli_name(cli_user(acptr)->server);
137 parv[2] = cli_name(acptr);
140 if ((i = hunt_server_cmd(sptr, CMD_TRACE, cptr, IsServer(acptr),
141 "%s :%C", 2, parc, parv)) == HUNTED_NOSUCH)
147 /* Got "TRACE <tname> :<target>" */
149 if (MyUser(sptr) || Protocol(cptr) < 10)
150 acptr = find_match_server(parv[2]);
152 acptr = FindNServer(parv[2]);
153 if ((i = hunt_server_cmd(sptr, CMD_TRACE, cptr, 0, "%s :%C", 2, parc,
154 parv)) == HUNTED_NOSUCH)
159 if (i == HUNTED_PASS) {
161 acptr = next_client(GlobalClientList, tname);
163 acptr = cli_from(acptr);
164 send_reply(sptr, RPL_TRACELINK,
165 version, debugmode, tname,
166 acptr ? cli_name(cli_from(acptr)) : "<No_match>");
170 doall = (parv[1] && (parc > 1)) ? !match(tname, cli_name(&me)) : 1;
171 wilds = !parv[1] || strchr(tname, '*') || strchr(tname, '?');
172 dow = wilds || doall;
174 /* Don't give (long) remote listings to lusers */
175 if (dow && !MyConnect(sptr) && !IsAnOper(sptr)) {
176 send_reply(sptr, RPL_TRACEEND);
180 for (i = 0; i < MAXCONNECTIONS; i++)
181 link_s[i] = 0, link_u[i] = 0;
184 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr)) {
186 link_u[cli_fd(cli_from(acptr))]++;
187 else if (IsServer(acptr))
188 link_s[cli_fd(cli_from(acptr))]++;
192 /* report all direct connections */
194 for (i = 0; i <= HighestFd; i++) {
195 const char *conClass;
197 if (!(acptr = LocalClientArray[i])) /* Local Connection? */
199 if (IsInvisible(acptr) && dow && !(MyConnect(sptr) && IsOper(sptr)) &&
200 !IsAnOper(acptr) && (acptr != sptr))
202 if (!doall && wilds && match(tname, cli_name(acptr)))
204 if (!dow && 0 != ircd_strcmp(tname, cli_name(acptr)))
207 conClass = get_client_class(acptr);
209 switch (cli_status(acptr)) {
210 case STAT_CONNECTING:
211 send_reply(sptr, RPL_TRACECONNECTING, conClass, cli_name(acptr));
215 send_reply(sptr, RPL_TRACEHANDSHAKE, conClass, cli_name(acptr));
221 case STAT_UNKNOWN_USER:
222 send_reply(sptr, RPL_TRACEUNKNOWN, conClass,
223 get_client_name(acptr, HIDE_IP));
226 case STAT_UNKNOWN_SERVER:
227 send_reply(sptr, RPL_TRACEUNKNOWN, conClass, "Unknown Server");
231 /* Only opers see users if there is a wildcard
232 but anyone can see all the opers. */
233 if ((IsAnOper(sptr) && (MyUser(sptr) ||
234 !(dow && IsInvisible(acptr)))) || !dow || IsAnOper(acptr)) {
236 send_reply(sptr, RPL_TRACEOPERATOR, conClass,
237 get_client_name(acptr, SHOW_IP),
238 CurrentTime - cli_lasttime(acptr));
240 send_reply(sptr, RPL_TRACEUSER, conClass,
241 get_client_name(acptr, SHOW_IP),
242 CurrentTime - cli_lasttime(acptr));
247 * Connection is a server
249 * Serv <class> <nS> <nC> <name> <ConnBy> <last> <age>
251 * class Class the server is in
252 * nS Number of servers reached via this link
253 * nC Number of clients reached via this link
254 * name Name of the server linked
255 * ConnBy Who established this link
256 * last Seconds since we got something from this link
257 * age Seconds this link has been alive
259 * Additional comments etc...... -Cym-<cym@acrux.net>
263 if (cli_serv(acptr)->user)
264 send_reply(sptr, RPL_TRACESERVER, conClass, link_s[i],
265 link_u[i], cli_name(acptr),
266 (*(cli_serv(acptr))->by) ? cli_serv(acptr)->by : "*",
267 cli_serv(acptr)->user->username, cli_serv(acptr)->user->host,
268 CurrentTime - cli_lasttime(acptr),
269 CurrentTime - cli_serv(acptr)->timestamp);
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_name(&me), CurrentTime - cli_lasttime(acptr),
275 CurrentTime - cli_serv(acptr)->timestamp);
278 default: /* We actually shouldn't come here, -msa */
279 send_reply(sptr, RPL_TRACENEWTYPE, get_client_name(acptr, HIDE_IP));
285 * Add these lines to summarize the above which can get rather long
286 * and messy when done remotely - Avalon
288 if (IsAnOper(sptr) && doall) {
289 for (cl = get_class_list(); cl; cl = cl->next) {
291 send_reply(sptr, RPL_TRACECLASS, ConClass(cl), Links(cl));
294 send_reply(sptr, RPL_TRACEEND);
298 * m_trace - generic message handler
300 * parv[0] = sender prefix
301 * parv[1] = nick or servername
302 * parv[2] = 'target' servername
304 int m_trace(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
306 if (feature_bool(FEAT_HIS_TRACE))
307 return send_reply(cptr, ERR_NOPRIVILEGES);
308 do_trace(cptr, sptr, parc, parv);
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[])
321 do_trace(cptr, sptr, parc, parv);
326 * mo_trace - oper message handler
328 * parv[0] = sender prefix
329 * parv[1] = nick or servername
330 * parv[2] = 'target' servername
332 int mo_trace(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
334 if (feature_bool(FEAT_HIS_TRACE) && !IsAnOper(sptr))
335 return send_reply(cptr, ERR_NOPRIVILEGES);
336 do_trace(cptr, sptr, parc, parv);