Send RPL_TRACEEND at end of trace output.
[ircu2.10.12-pk.git] / ircd / m_trace.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_trace.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
6  * See file AUTHORS in IRC package for additional names of
7  * the programmers.
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 1, or (at your option)
12  * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * $Id$
24  */
25
26 /*
27  * m_functions execute protocol messages on this server:
28  *
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...).
34  *
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.
38  *
39  *            (!IsServer(cptr)) => (cptr == sptr), because
40  *            prefixes are taken *only* from servers...
41  *
42  *            (IsServer(cptr))
43  *                    (sptr == cptr) => the message didn't
44  *                    have the prefix.
45  *
46  *                    (sptr != cptr && IsServer(sptr) means
47  *                    the prefix specified servername. (?)
48  *
49  *                    (sptr != cptr && !IsServer(sptr) means
50  *                    that message originated from a remote
51  *                    user (not local).
52  *
53  *            combining
54  *
55  *            (!IsServer(sptr)) means that, sptr can safely
56  *            taken as defining the target structure of the
57  *            message in this server.
58  *
59  *    *Always* true (if 'parse' and others are working correct):
60  *
61  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
62  *
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 ]
67  *
68  *    parc    number of variable parameter strings (if zero,
69  *            parv is allowed to be NULL)
70  *
71  *    parv    a NULL terminated list of parameter pointers,
72  *
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*
78  *
79  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
80  *                    non-NULL pointers.
81  */
82 #include "config.h"
83
84 #include "class.h"
85 #include "client.h"
86 #include "hash.h"
87 #include "ircd.h"
88 #include "ircd_features.h"
89 #include "ircd_reply.h"
90 #include "ircd_string.h"
91 #include "match.h"
92 #include "msg.h"
93 #include "numeric.h"
94 #include "numnicks.h"
95 #include "s_bsd.h"
96 #include "s_conf.h"
97 #include "s_user.h"
98 #include "send.h"
99 #include "version.h"
100
101 #include <assert.h>
102 #include <string.h>
103
104 void do_trace(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
105 {
106   int i;
107   struct Client *acptr;
108   const struct ConnectionClass* cl;
109   char* tname;
110   int doall;
111   int link_s[MAXCONNECTIONS];
112   int link_u[MAXCONNECTIONS];
113   int cnt = 0;
114   int wilds;
115   int dow;
116
117   if (parc < 2 || BadPtr(parv[1]))
118   {
119     /* just "TRACE" without parameters. Must be from local client */
120     parc = 1;
121     acptr = &me;
122     tname = cli_name(&me);
123     i = HUNTED_ISME;
124   }
125   else if (parc < 3 || BadPtr(parv[2]))
126   {
127     /* No target specified. Make one before propagating. */
128     parc = 2;
129     tname = parv[1];
130     if ((acptr = find_match_server(parv[1])) ||
131         ((acptr = FindClient(parv[1])) && !MyUser(acptr)))
132     {
133       if (IsUser(acptr))
134         parv[2] = cli_name(cli_user(acptr)->server);
135       else
136         parv[2] = cli_name(acptr);
137       parc = 3;
138       parv[3] = 0;
139       if ((i = hunt_server_cmd(sptr, CMD_TRACE, cptr, IsServer(acptr),
140                                "%s :%C", 2, parc, parv)) == HUNTED_NOSUCH)
141         return;
142     }
143     else
144       i = HUNTED_ISME;
145   } else {
146     /* Got "TRACE <tname> :<target>" */
147     parc = 3;
148     if (MyUser(sptr) || Protocol(cptr) < 10)
149       acptr = find_match_server(parv[2]);
150     else
151       acptr = FindNServer(parv[2]);
152     if ((i = hunt_server_cmd(sptr, CMD_TRACE, cptr, 0, "%s :%C", 2, parc,
153                              parv)) == HUNTED_NOSUCH)
154       return;
155     tname = parv[1];
156   }
157
158   if (i == HUNTED_PASS) {
159     if (!acptr)
160       acptr = next_client(GlobalClientList, tname);
161     else
162       acptr = cli_from(acptr);
163     send_reply(sptr, RPL_TRACELINK,
164                version, debugmode, tname,
165                acptr ? cli_name(cli_from(acptr)) : "<No_match>");
166     return;
167   }
168
169   doall = (parv[1] && (parc > 1)) ? !match(tname, cli_name(&me)) : 1;
170   wilds = !parv[1] || strchr(tname, '*') || strchr(tname, '?');
171   dow = wilds || doall;
172
173   /* Don't give (long) remote listings to lusers */
174   if (dow && !MyConnect(sptr) && !IsAnOper(sptr)) {
175     send_reply(sptr, RPL_TRACEEND);
176     return;
177   }
178
179   for (i = 0; i < MAXCONNECTIONS; i++)
180     link_s[i] = 0, link_u[i] = 0;
181
182   if (doall) {
183     for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr)) {
184       if (IsUser(acptr))
185         link_u[cli_fd(cli_from(acptr))]++;
186       else if (IsServer(acptr))
187         link_s[cli_fd(cli_from(acptr))]++;
188     }
189   }
190
191   /* report all direct connections */
192
193   for (i = 0; i <= HighestFd; i++) {
194     const char *conClass;
195
196     if (!(acptr = LocalClientArray[i])) /* Local Connection? */
197       continue;
198     if (IsInvisible(acptr) && dow && !(MyConnect(sptr) && IsOper(sptr)) &&
199         !IsAnOper(acptr) && (acptr != sptr))
200       continue;
201     if (!doall && wilds && match(tname, cli_name(acptr)))
202       continue;
203     if (!dow && 0 != ircd_strcmp(tname, cli_name(acptr)))
204       continue;
205
206     conClass = get_client_class(acptr);
207
208     switch (cli_status(acptr)) {
209       case STAT_CONNECTING:
210         send_reply(sptr, RPL_TRACECONNECTING, conClass, cli_name(acptr));
211         cnt++;
212         break;
213       case STAT_HANDSHAKE:
214         send_reply(sptr, RPL_TRACEHANDSHAKE, conClass, cli_name(acptr));
215         cnt++;
216         break;
217       case STAT_ME:
218         break;
219       case STAT_UNKNOWN:
220       case STAT_UNKNOWN_USER:
221         send_reply(sptr, RPL_TRACEUNKNOWN, conClass,
222                    get_client_name(acptr, HIDE_IP));
223         cnt++;
224         break;
225       case STAT_UNKNOWN_SERVER:
226         send_reply(sptr, RPL_TRACEUNKNOWN, conClass, "Unknown Server");
227         cnt++;
228         break;
229       case STAT_USER:
230         /* Only opers see users if there is a wildcard
231            but anyone can see all the opers. */
232         if ((IsAnOper(sptr) && (MyUser(sptr) ||
233             !(dow && IsInvisible(acptr)))) || !dow || IsAnOper(acptr)) {
234           if (IsAnOper(acptr))
235             send_reply(sptr, RPL_TRACEOPERATOR, conClass,
236                        get_client_name(acptr, SHOW_IP),
237                        CurrentTime - cli_lasttime(acptr));
238           else
239             send_reply(sptr, RPL_TRACEUSER, conClass,
240                        get_client_name(acptr, SHOW_IP),
241                        CurrentTime - cli_lasttime(acptr));
242           cnt++;
243         }
244         break;
245         /*
246          * Connection is a server
247          *
248          * Serv <class> <nS> <nC> <name> <ConnBy> <last> <age>
249          *
250          * class        Class the server is in
251          * nS           Number of servers reached via this link
252          * nC           Number of clients reached via this link
253          * name         Name of the server linked
254          * ConnBy       Who established this link
255          * last         Seconds since we got something from this link
256          * age          Seconds this link has been alive
257          *
258          * Additional comments etc......        -Cym-<cym@acrux.net>
259          */
260
261       case STAT_SERVER:
262         if (cli_serv(acptr)->user)
263           send_reply(sptr, RPL_TRACESERVER, conClass, link_s[i],
264                      link_u[i], cli_name(acptr),
265                      (*(cli_serv(acptr))->by) ? cli_serv(acptr)->by : "*",
266                      cli_serv(acptr)->user->username, cli_serv(acptr)->user->host,
267                      CurrentTime - cli_lasttime(acptr),
268                      CurrentTime - cli_serv(acptr)->timestamp);
269         else
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_name(&me), CurrentTime - cli_lasttime(acptr),
274                      CurrentTime - cli_serv(acptr)->timestamp);
275         cnt++;
276         break;
277       default:                  /* We actually shouldn't come here, -msa */
278         send_reply(sptr, RPL_TRACENEWTYPE, get_client_name(acptr, HIDE_IP));
279         cnt++;
280         break;
281     }
282   }
283   /*
284    * Add these lines to summarize the above which can get rather long
285    * and messy when done remotely - Avalon
286    */
287   if (IsAnOper(sptr) && doall) {
288     for (cl = get_class_list(); cl; cl = cl->next) {
289       if (Links(cl) > 0)
290        send_reply(sptr, RPL_TRACECLASS, ConClass(cl), Links(cl));
291     }
292   }
293   send_reply(sptr, RPL_TRACEEND);
294 }
295
296 /*
297  * m_trace - generic message handler
298  *
299  * parv[0] = sender prefix
300  * parv[1] = nick or servername
301  * parv[2] = 'target' servername
302  */
303 int m_trace(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
304 {
305   if (feature_bool(FEAT_HIS_TRACE))
306     return send_reply(cptr, ERR_NOPRIVILEGES);
307   do_trace(cptr, sptr, parc, parv);
308   return 0;
309 }
310
311 /*
312  * ms_trace - server message handler
313  *
314  * parv[0] = sender prefix
315  * parv[1] = nick or servername
316  * parv[2] = 'target' servername
317  */
318 int ms_trace(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
319 {
320   do_trace(cptr, sptr, parc, parv);
321   return 0;
322 }
323
324 /*
325  * mo_trace - oper message handler
326  *
327  * parv[0] = sender prefix
328  * parv[1] = nick or servername
329  * parv[2] = 'target' servername
330  */
331 int mo_trace(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
332 {
333   if (feature_bool(FEAT_HIS_TRACE) && !IsAnOper(sptr))
334     return send_reply(cptr, ERR_NOPRIVILEGES);
335   do_trace(cptr, sptr, parc, parv);
336   return 0;
337 }