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