Author: Kev <klmitch@mit.edu>
[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_log.h"
90 #include "ircd_reply.h"
91 #include "ircd_string.h"
92 #include "match.h"
93 #include "msg.h"
94 #include "numeric.h"
95 #include "numnicks.h"
96 #include "s_bsd.h"
97 #include "s_conf.h"
98 #include "s_user.h"
99 #include "send.h"
100 #include "version.h"
101
102 /* #include <assert.h> -- Now using assert in ircd_log.h */
103 #include <string.h>
104
105 void do_trace(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
106 {
107   int i;
108   struct Client *acptr;
109   const struct ConnectionClass* cl;
110   char* tname;
111   int doall;
112   int link_s[MAXCONNECTIONS];
113   int link_u[MAXCONNECTIONS];
114   int cnt = 0;
115   int wilds;
116   int dow;
117
118   if (parc < 2 || BadPtr(parv[1]))
119   {
120     /* just "TRACE" without parameters. Must be from local client */
121     parc = 1;
122     acptr = &me;
123     tname = cli_name(&me);
124     i = HUNTED_ISME;
125   }
126   else if (parc < 3 || BadPtr(parv[2]))
127   {
128     /* No target specified. Make one before propagating. */
129     parc = 2;
130     tname = parv[1];
131     if ((acptr = find_match_server(parv[1])) ||
132         ((acptr = FindClient(parv[1])) && !MyUser(acptr)))
133     {
134       if (IsUser(acptr))
135         parv[2] = cli_name(cli_user(acptr)->server);
136       else
137         parv[2] = cli_name(acptr);
138       parc = 3;
139       parv[3] = 0;
140       if ((i = hunt_server_cmd(sptr, CMD_TRACE, cptr, IsServer(acptr),
141                                "%s :%C", 2, parc, parv)) == HUNTED_NOSUCH)
142         return;
143     }
144     else
145       i = HUNTED_ISME;
146   } else {
147     /* Got "TRACE <tname> :<target>" */
148     parc = 3;
149     if (MyUser(sptr) || Protocol(cptr) < 10)
150       acptr = find_match_server(parv[2]);
151     else
152       acptr = FindNServer(parv[2]);
153     if ((i = hunt_server_cmd(sptr, CMD_TRACE, cptr, 0, "%s :%C", 2, parc,
154                              parv)) == HUNTED_NOSUCH)
155       return;
156     tname = parv[1];
157   }
158
159   if (i == HUNTED_PASS) {
160     if (!acptr)
161       acptr = next_client(GlobalClientList, tname);
162     else
163       acptr = cli_from(acptr);
164     send_reply(sptr, RPL_TRACELINK,
165                version, debugmode, tname,
166                acptr ? cli_name(cli_from(acptr)) : "<No_match>");
167     return;
168   }
169
170   doall = (parv[1] && (parc > 1)) ? !match(tname, cli_name(&me)) : 1;
171   wilds = !parv[1] || strchr(tname, '*') || strchr(tname, '?');
172   dow = wilds || doall;
173
174   /* Don't give (long) remote listings to lusers */
175   if (dow && !MyConnect(sptr) && !IsAnOper(sptr)) {
176     send_reply(sptr, RPL_TRACEEND);
177     return;
178   }
179
180   for (i = 0; i < MAXCONNECTIONS; i++)
181     link_s[i] = 0, link_u[i] = 0;
182
183   if (doall) {
184     for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr)) {
185       if (IsUser(acptr))
186         link_u[cli_fd(cli_from(acptr))]++;
187       else if (IsServer(acptr))
188         link_s[cli_fd(cli_from(acptr))]++;
189     }
190   }
191
192   /* report all direct connections */
193
194   for (i = 0; i <= HighestFd; i++) {
195     const char *conClass;
196
197     if (!(acptr = LocalClientArray[i])) /* Local Connection? */
198       continue;
199     if (IsInvisible(acptr) && dow && !(MyConnect(sptr) && IsOper(sptr)) &&
200         !IsAnOper(acptr) && (acptr != sptr))
201       continue;
202     if (!doall && wilds && match(tname, cli_name(acptr)))
203       continue;
204     if (!dow && 0 != ircd_strcmp(tname, cli_name(acptr)))
205       continue;
206
207     conClass = get_client_class(acptr);
208
209     switch (cli_status(acptr)) {
210       case STAT_CONNECTING:
211         send_reply(sptr, RPL_TRACECONNECTING, conClass, cli_name(acptr));
212         cnt++;
213         break;
214       case STAT_HANDSHAKE:
215         send_reply(sptr, RPL_TRACEHANDSHAKE, conClass, cli_name(acptr));
216         cnt++;
217         break;
218       case STAT_ME:
219         break;
220       case STAT_UNKNOWN:
221       case STAT_UNKNOWN_USER:
222         send_reply(sptr, RPL_TRACEUNKNOWN, conClass,
223                    get_client_name(acptr, HIDE_IP));
224         cnt++;
225         break;
226       case STAT_UNKNOWN_SERVER:
227         send_reply(sptr, RPL_TRACEUNKNOWN, conClass, "Unknown Server");
228         cnt++;
229         break;
230       case STAT_USER:
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)) {
235           if (IsAnOper(acptr))
236             send_reply(sptr, RPL_TRACEOPERATOR, conClass,
237                        get_client_name(acptr, SHOW_IP),
238                        CurrentTime - cli_lasttime(acptr));
239           else
240             send_reply(sptr, RPL_TRACEUSER, conClass,
241                        get_client_name(acptr, SHOW_IP),
242                        CurrentTime - cli_lasttime(acptr));
243           cnt++;
244         }
245         break;
246         /*
247          * Connection is a server
248          *
249          * Serv <class> <nS> <nC> <name> <ConnBy> <last> <age>
250          *
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
258          *
259          * Additional comments etc......        -Cym-<cym@acrux.net>
260          */
261
262       case STAT_SERVER:
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);
270         else
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);
276         cnt++;
277         break;
278       default:                  /* We actually shouldn't come here, -msa */
279         send_reply(sptr, RPL_TRACENEWTYPE, get_client_name(acptr, HIDE_IP));
280         cnt++;
281         break;
282     }
283   }
284   /*
285    * Add these lines to summarize the above which can get rather long
286    * and messy when done remotely - Avalon
287    */
288   if (IsAnOper(sptr) && doall) {
289     for (cl = get_class_list(); cl; cl = cl->next) {
290       if (Links(cl) > 0)
291        send_reply(sptr, RPL_TRACECLASS, ConClass(cl), Links(cl));
292     }
293   }
294   send_reply(sptr, RPL_TRACEEND);
295 }
296
297 /*
298  * m_trace - generic message handler
299  *
300  * parv[0] = sender prefix
301  * parv[1] = nick or servername
302  * parv[2] = 'target' servername
303  */
304 int m_trace(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
305 {
306   if (feature_bool(FEAT_HIS_TRACE))
307     return send_reply(cptr, ERR_NOPRIVILEGES);
308   do_trace(cptr, sptr, parc, parv);
309   return 0;
310 }
311
312 /*
313  * ms_trace - server message handler
314  *
315  * parv[0] = sender prefix
316  * parv[1] = nick or servername
317  * parv[2] = 'target' servername
318  */
319 int ms_trace(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
320 {
321   do_trace(cptr, sptr, parc, parv);
322   return 0;
323 }
324
325 /*
326  * mo_trace - oper message handler
327  *
328  * parv[0] = sender prefix
329  * parv[1] = nick or servername
330  * parv[2] = 'target' servername
331  */
332 int mo_trace(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
333 {
334   if (feature_bool(FEAT_HIS_TRACE) && !IsAnOper(sptr))
335     return send_reply(cptr, ERR_NOPRIVILEGES);
336   do_trace(cptr, sptr, parc, parv);
337   return 0;
338 }