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