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