Author: Isomer <isomer@coders.net>
[ircu2.10.12-pk.git] / ircd / m_kill.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_kill.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 #if 0
85 /*
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
89  */
90 #include "handlers.h"
91 #endif /* 0 */
92 #include "client.h"
93 #include "hash.h"
94 #include "ircd.h"
95 #include "ircd_log.h"
96 #include "ircd_reply.h"
97 #include "ircd_string.h"
98 #include "msg.h"
99 #include "numeric.h"
100 #include "numnicks.h"
101 #include "s_misc.h"
102 #include "send.h"
103 #include "whowas.h"
104
105 #include <assert.h>
106 #include <string.h>
107
108 /*
109  * ms_kill - server message handler template
110  *
111  * NOTE: IsServer(cptr) == true;
112  *
113  * parv[0]      = sender prefix
114  * parv[1]      = kill victim
115  * parv[parc-1] = kill path
116  */
117 int ms_kill(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
118 {
119   struct Client* victim;
120   const char*    inpath;
121   char*          user;
122   char*          path;
123   char*          killer;
124   char           buf[BUFSIZE];
125
126   assert(0 != cptr);
127   assert(0 != sptr);
128   assert(IsServer(cptr));
129
130   /*
131    * XXX - a server sending less than 3 params could really desync
132    * things
133    */
134   if (parc < 3) {
135     protocol_violation(sptr,"Too few arguments for KILL");
136     return need_more_params(sptr, "KILL");
137   }
138
139   user = parv[1];
140   path = parv[parc - 1];        /* Either defined or NULL (parc >= 3) */
141
142   if (!(victim = findNUser(parv[1]))) {
143     if (IsUser(sptr))
144       sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :KILL target disconnected "
145                     "before I got him :(", sptr);
146     return 0;
147   }
148
149   /*
150    * Notify all *local* opers about the KILL (this includes the one
151    * originating the kill, if from this server--the special numeric
152    * reply message is not generated anymore).
153    *
154    * Note: "victim->name" is used instead of "user" because we may
155    *       have changed the target because of the nickname change.
156    */
157   inpath = cli_name(cptr);
158
159   sendto_opmask_butone(0, IsServer(sptr) ? SNO_SERVKILL : SNO_OPERKILL,
160                        "Received KILL message for %s. From %s Path: %C!%s",
161                        get_client_name(victim,SHOW_IP), parv[0], cptr, path);
162
163   log_write_kill(victim, sptr, cli_name(cptr), path);
164   /*
165    * And pass on the message to other servers. Note, that if KILL
166    * was changed, the message has to be sent to all links, also
167    * back.
168    */
169   sendcmdto_serv_butone(sptr, CMD_KILL, cptr, "%C :%s!%s", victim, cli_name(cptr),
170                         path);
171   /*
172    * We *can* have crossed a NICK with this numeric... --Run
173    *
174    * Note the following situation:
175    *  KILL SAA -->       X
176    *  <-- S NICK ... SAA | <-- SAA QUIT <-- S NICK ... SAA <-- SQUIT S
177    * Where the KILL reaches point X before the QUIT does.
178    * This would then *still* cause an orphan because the KILL doesn't reach S
179    * (because of the SQUIT), the QUIT is ignored (because of the KILL)
180    * and the second NICK ... SAA causes an orphan on the server at the
181    * right (which then isn't removed when the SQUIT arrives).
182    * Therefore we still need to detect numeric nick collisions too.
183    *
184    * Bounce the kill back to the originator, if the client can't be found
185    * by the next hop (short lag) the bounce won't propagate further.
186    */
187   if (MyConnect(victim))
188     sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s!%s (Ghost 5 Numeric Collided)",
189                   victim, cli_name(cptr), path);
190   /*
191    * Set FLAGS_KILLED. This prevents exit_one_client from sending
192    * the unnecessary QUIT for this. (This flag should never be
193    * set in any other place)
194    */
195   cli_flags(victim) |= FLAGS_KILLED;
196
197   /*
198    * Tell the victim she/he has been zapped, but *only* if
199    * the victim is on current server--no sense in sending the
200    * notification chasing the above kill, it won't get far
201    * anyway (as this user don't exist there any more either)
202    */
203   if (MyConnect(victim))
204     sendcmdto_one(sptr, CMD_KILL, victim, "%C :%s!%s", victim, NumServ(cptr),
205                   path);
206   /*
207    * the first space in path will be at the end of the
208    * opers name:
209    * bla.bla.bla!host.net.dom!opername (comment)
210    */
211   if ((killer = strchr(path, ' '))) {
212     while (killer > path && '!' != *killer)
213       --killer;
214     if ('!' == *killer)
215       ++killer;
216   }
217   else
218     killer = path;
219   sprintf_irc(buf, "Killed (%s)", killer);
220
221   return exit_client(cptr, victim, sptr, buf);
222 }
223
224 /*
225  * mo_kill - oper message handler template
226  *
227  * NOTE: IsPrivileged(sptr), IsAnOper(sptr) == true
228  *       IsServer(cptr), IsServer(sptr) == false
229  *
230  * parv[0]      = sender prefix
231  * parv[1]      = kill victim
232  * parv[parc-1] = kill path
233  */
234 int mo_kill(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
235 {
236   struct Client* victim;
237   const char*    inpath;
238   char*          user;
239   char*          path;
240   char*          comment;
241   char           buf[BUFSIZE];
242
243   assert(0 != cptr);
244   assert(0 != sptr);
245   /*
246    * oper connection to this server, cptr is always sptr
247    */
248   assert(cptr == sptr);
249   assert(IsAnOper(sptr));
250
251   if (parc < 3 || EmptyString(parv[parc - 1]))
252     return need_more_params(sptr, "KILL");
253
254   user = parv[1];
255   if (!(victim = FindClient(user))) {
256     /*
257      * If the user has recently changed nick, we automaticly
258      * rewrite the KILL for this new nickname--this keeps
259      * servers in synch when nick change and kill collide
260      */
261     if (!(victim = get_history(user, (long)15)))
262       return send_reply(sptr, ERR_NOSUCHNICK, user);
263
264     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Changed KILL %s into %s", sptr,
265                   user, cli_name(victim));
266   }
267   if (!HasPriv(sptr, MyConnect(victim) ? PRIV_LOCAL_KILL : PRIV_KILL))
268     return send_reply(sptr, ERR_NOPRIVILEGES);
269
270   if (IsServer(victim) || IsMe(victim)) {
271     return send_reply(sptr, ERR_CANTKILLSERVER);
272   }
273   /*
274    * if the user is +k, prevent a kill from local user
275    */
276   if (IsChannelService(victim))
277     return send_reply(sptr, ERR_ISCHANSERVICE, "KILL", cli_name(victim));
278
279
280   if (!MyConnect(victim) && !HasPriv(sptr, PRIV_KILL)) {
281     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Nick %s isnt on your server",
282                   sptr, cli_name(victim));
283     return 0;
284   }
285
286   /*
287    * The kill originates from this server, initialize path.
288    * (In which case the 'path' may contain user suplied
289    * explanation ...or some nasty comment, sigh... >;-)
290    *
291    * ...!operhost!oper
292    * ...!operhost!oper (comment)
293    */
294
295   comment = parv[parc - 1];        /* Either defined or NULL (parc >= 3) */
296
297   if (strlen(comment) > TOPICLEN)
298     comment[TOPICLEN] = '\0';
299
300   inpath = cli_user(sptr)->host;
301
302   sprintf_irc(buf,
303               "%s%s (%s)", cli_name(cptr), IsOper(sptr) ? "" : "(L)", comment);
304   path = buf;
305
306   /*
307    * Notify all *local* opers about the KILL (this includes the one
308    * originating the kill, if from this server--the special numeric
309    * reply message is not generated anymore).
310    *
311    * Note: "victim->name" is used instead of "user" because we may
312    *       have changed the target because of the nickname change.
313    */
314   sendto_opmask_butone(0, SNO_OPERKILL,
315                        "Received KILL message for %s. From %s Path: %s!%s",
316                        get_client_name(victim,SHOW_IP), parv[0], inpath, path);
317
318   log_write_kill(victim, sptr, inpath, path);
319   /*
320    * And pass on the message to other servers. Note, that if KILL
321    * was changed, the message has to be sent to all links, also
322    * back.
323    * Suicide kills are NOT passed on --SRB
324    */
325   if (!MyConnect(victim)) {
326     sendcmdto_serv_butone(sptr, CMD_KILL, cptr, "%C :%s!%s", victim, inpath,
327                           path);
328
329    /*
330     * Set FLAGS_KILLED. This prevents exit_one_client from sending
331     * the unnecessary QUIT for this. (This flag should never be
332     * set in any other place)
333     */
334     cli_flags(victim) |= FLAGS_KILLED;
335
336     sprintf_irc(buf, "Killed by %s (%s)", cli_name(sptr), comment);
337   }
338   else {
339   /*
340    * Tell the victim she/he has been zapped, but *only* if
341    * the victim is on current server--no sense in sending the
342    * notification chasing the above kill, it won't get far
343    * anyway (as this user don't exist there any more either)
344    */
345     sendcmdto_one(sptr, CMD_KILL, victim, "%C :%s!%s", victim, inpath, path);
346     sprintf_irc(buf, "Local kill by %s (%s)", cli_name(sptr), comment);
347   }
348
349   return exit_client(cptr, victim, sptr, buf);
350 }