730d54029aa255339075e090f0ff81594a907b36
[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     return need_more_params(sptr, "KILL");
136
137   user = parv[1];
138   path = parv[parc - 1];        /* Either defined or NULL (parc >= 3) */
139
140   if (!(victim = findNUser(parv[1]))) {
141     if (IsUser(sptr))
142       sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :KILL target disconnected "
143                     "before I got him :(", sptr);
144     return 0;
145   }
146
147   /*
148    * Notify all *local* opers about the KILL (this includes the one
149    * originating the kill, if from this server--the special numeric
150    * reply message is not generated anymore).
151    *
152    * Note: "victim->name" is used instead of "user" because we may
153    *       have changed the target because of the nickname change.
154    */
155   inpath = cli_name(cptr);
156
157   sendto_opmask_butone(0, IsServer(sptr) ? SNO_SERVKILL : SNO_OPERKILL,
158                        "Received KILL message for %s. From %s Path: %C!%s",
159                        get_client_name(victim,SHOW_IP), parv[0], cptr, path);
160
161   log_write_kill(victim, sptr, cli_name(cptr), path);
162   /*
163    * And pass on the message to other servers. Note, that if KILL
164    * was changed, the message has to be sent to all links, also
165    * back.
166    */
167   sendcmdto_serv_butone(sptr, CMD_KILL, cptr, "%C :%s!%s", victim, cli_name(cptr),
168                         path);
169   /*
170    * We *can* have crossed a NICK with this numeric... --Run
171    *
172    * Note the following situation:
173    *  KILL SAA -->       X
174    *  <-- S NICK ... SAA | <-- SAA QUIT <-- S NICK ... SAA <-- SQUIT S
175    * Where the KILL reaches point X before the QUIT does.
176    * This would then *still* cause an orphan because the KILL doesn't reach S
177    * (because of the SQUIT), the QUIT is ignored (because of the KILL)
178    * and the second NICK ... SAA causes an orphan on the server at the
179    * right (which then isn't removed when the SQUIT arrives).
180    * Therefore we still need to detect numeric nick collisions too.
181    *
182    * Bounce the kill back to the originator, if the client can't be found
183    * by the next hop (short lag) the bounce won't propagate further.
184    */
185   if (MyConnect(victim))
186     sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s!%s (Ghost 5 Numeric Collided)",
187                   victim, cli_name(cptr), path);
188   /*
189    * Set FLAGS_KILLED. This prevents exit_one_client from sending
190    * the unnecessary QUIT for this. (This flag should never be
191    * set in any other place)
192    */
193   cli_flags(victim) |= FLAGS_KILLED;
194
195   /*
196    * Tell the victim she/he has been zapped, but *only* if
197    * the victim is on current server--no sense in sending the
198    * notification chasing the above kill, it won't get far
199    * anyway (as this user don't exist there any more either)
200    */
201   if (MyConnect(victim))
202     sendcmdto_one(sptr, CMD_KILL, victim, "%C :%s!%s", victim, NumServ(cptr),
203                   path);
204   /*
205    * the first space in path will be at the end of the
206    * opers name:
207    * bla.bla.bla!host.net.dom!opername (comment)
208    */
209   if ((killer = strchr(path, ' '))) {
210     while (killer > path && '!' != *killer)
211       --killer;
212     if ('!' == *killer)
213       ++killer;
214   }
215   else
216     killer = path;
217   sprintf_irc(buf, "Killed (%s)", killer);
218
219   return exit_client(cptr, victim, sptr, buf);
220 }
221
222 /*
223  * mo_kill - oper message handler template
224  *
225  * NOTE: IsPrivileged(sptr), IsAnOper(sptr) == true
226  *       IsServer(cptr), IsServer(sptr) == false
227  *
228  * parv[0]      = sender prefix
229  * parv[1]      = kill victim
230  * parv[parc-1] = kill path
231  */
232 int mo_kill(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
233 {
234   struct Client* victim;
235   const char*    inpath;
236   char*          user;
237   char*          path;
238   char*          comment;
239   char           buf[BUFSIZE];
240
241   assert(0 != cptr);
242   assert(0 != sptr);
243   /*
244    * oper connection to this server, cptr is always sptr
245    */
246   assert(cptr == sptr);
247   assert(IsAnOper(sptr));
248
249   if (parc < 3 || EmptyString(parv[parc - 1]))
250     return need_more_params(sptr, "KILL");
251
252   user = parv[1];
253   if (!(victim = FindClient(user))) {
254     /*
255      * If the user has recently changed nick, we automaticly
256      * rewrite the KILL for this new nickname--this keeps
257      * servers in synch when nick change and kill collide
258      */
259     if (!(victim = get_history(user, (long)15)))
260       return send_reply(sptr, ERR_NOSUCHNICK, user);
261
262     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Changed KILL %s into %s", sptr,
263                   user, cli_name(victim));
264   }
265   if (!HasPriv(sptr, MyConnect(victim) ? PRIV_LOCAL_KILL : PRIV_KILL))
266     return send_reply(sptr, ERR_NOPRIVILEGES);
267
268   if (IsServer(victim) || IsMe(victim)) {
269     return send_reply(sptr, ERR_CANTKILLSERVER);
270   }
271   /*
272    * if the user is +k, prevent a kill from local user
273    */
274   if (IsChannelService(victim))
275     return send_reply(sptr, ERR_ISCHANSERVICE, "KILL", cli_name(victim));
276
277
278   if (!MyConnect(victim) && !HasPriv(sptr, PRIV_KILL)) {
279     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Nick %s isnt on your server",
280                   sptr, cli_name(victim));
281     return 0;
282   }
283
284   /*
285    * The kill originates from this server, initialize path.
286    * (In which case the 'path' may contain user suplied
287    * explanation ...or some nasty comment, sigh... >;-)
288    *
289    * ...!operhost!oper
290    * ...!operhost!oper (comment)
291    */
292
293   comment = parv[parc - 1];        /* Either defined or NULL (parc >= 3) */
294
295   if (strlen(comment) > TOPICLEN)
296     comment[TOPICLEN] = '\0';
297
298   inpath = cli_user(sptr)->host;
299
300   sprintf_irc(buf,
301               "%s%s (%s)", cli_name(cptr), IsOper(sptr) ? "" : "(L)", comment);
302   path = buf;
303
304   /*
305    * Notify all *local* opers about the KILL (this includes the one
306    * originating the kill, if from this server--the special numeric
307    * reply message is not generated anymore).
308    *
309    * Note: "victim->name" is used instead of "user" because we may
310    *       have changed the target because of the nickname change.
311    */
312   sendto_opmask_butone(0, SNO_OPERKILL,
313                        "Received KILL message for %s. From %s Path: %s!%s",
314                        get_client_name(victim,SHOW_IP), parv[0], inpath, path);
315
316   log_write_kill(victim, sptr, inpath, path);
317   /*
318    * And pass on the message to other servers. Note, that if KILL
319    * was changed, the message has to be sent to all links, also
320    * back.
321    * Suicide kills are NOT passed on --SRB
322    */
323   if (!MyConnect(victim)) {
324     sendcmdto_serv_butone(sptr, CMD_KILL, cptr, "%C :%s!%s", victim, inpath,
325                           path);
326
327    /*
328     * Set FLAGS_KILLED. This prevents exit_one_client from sending
329     * the unnecessary QUIT for this. (This flag should never be
330     * set in any other place)
331     */
332     cli_flags(victim) |= FLAGS_KILLED;
333
334     sprintf_irc(buf, "Killed by %s (%s)", cli_name(sptr), comment);
335   }
336   else {
337   /*
338    * Tell the victim she/he has been zapped, but *only* if
339    * the victim is on current server--no sense in sending the
340    * notification chasing the above kill, it won't get far
341    * anyway (as this user don't exist there any more either)
342    */
343     sendcmdto_one(sptr, CMD_KILL, victim, "%C :%s!%s", victim, inpath, path);
344     sprintf_irc(buf, "Local kill by %s (%s)", cli_name(sptr), comment);
345   }
346
347   return exit_client(cptr, victim, sptr, buf);
348 }
349
350 #if 0
351 /*
352  * m_kill
353  *
354  * parv[0] = sender prefix
355  * parv[1] = kill victim
356  * parv[parc-1] = kill path
357  */
358 int m_kill(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
359 {
360   struct Client* acptr;
361   const char*    inpath = get_client_name(cptr, HIDE_IP);
362   char*          user;
363   char*          path;
364   char*          killer;
365   int            chasing = 0;
366   char           buf[BUFSIZE];
367   char           buf2[BUFSIZE];
368
369   if (parc < 3 || *parv[1] == '\0')
370     return need_more_params(sptr, parv[0], "KILL");
371
372   user = parv[1];
373   path = parv[parc - 1];        /* Either defined or NULL (parc >= 3) */
374
375 #ifdef        OPER_KILL
376   if (!IsPrivileged(cptr))
377   {
378     sendto_one(sptr, err_str(ERR_NOPRIVILEGES), me.name, parv[0]); /* XXX DEAD */
379     return 0;
380   }
381 #else
382   if (!IsServer(cptr))
383   {
384     sendto_one(sptr, err_str(ERR_NOPRIVILEGES), me.name, parv[0]); /* XXX DEAD */
385     return 0;
386   }
387 #endif
388   if (IsAnOper(cptr))
389   {
390     if (EmptyString(path))
391       return need_more_params(sptr, parv[0], "KILL");
392
393     if (strlen(path) > TOPICLEN)
394       path[TOPICLEN] = '\0';
395   }
396
397   if (MyUser(sptr))
398   {
399     if (!(acptr = FindClient(user)))
400     {
401       /*
402        * If the user has recently changed nick, we automaticly
403        * rewrite the KILL for this new nickname--this keeps
404        * servers in synch when nick change and kill collide
405        */
406       if (!(acptr = get_history(user, (long)15)))
407       {
408         sendto_one(sptr, err_str(ERR_NOSUCHNICK), me.name, parv[0], user); /* XXX DEAD */
409         return 0;
410       }
411       sendto_one(sptr, ":%s NOTICE %s :Changed KILL %s into %s", /* XXX DEAD */
412           me.name, parv[0], user, acptr->name);
413       chasing = 1;
414     }
415   }
416   else if (!(acptr = findNUser(user)))
417   {
418     if (IsUser(sptr))
419       sendto_one(sptr, /* XXX DEAD */
420           "%s NOTICE %s%s :KILL target disconnected before I got him :(",
421           NumServ(&me), NumNick(sptr));
422     return 0;
423   }
424   if (!MyConnect(acptr) && IsLocOp(cptr))
425   {
426     sendto_one(sptr, err_str(ERR_NOPRIVILEGES), me.name, parv[0]); /* XXX DEAD */
427     return 0;
428   }
429   if (IsServer(acptr) || IsMe(acptr))
430   {
431     sendto_one(sptr, err_str(ERR_CANTKILLSERVER), me.name, parv[0]); /* XXX DEAD */
432     return 0;
433   }
434
435   /* if the user is +k, prevent a kill from local user */
436   if (IsChannelService(acptr) && MyUser(sptr))
437   {
438     sendto_one(sptr, err_str(ERR_ISCHANSERVICE), me.name, /* XXX DEAD */
439         parv[0], "KILL", acptr->name);
440     return 0;
441   }
442
443 #ifdef        LOCAL_KILL_ONLY
444   if (MyConnect(sptr) && !MyConnect(acptr))
445   {
446     sendto_one(sptr, ":%s NOTICE %s :Nick %s isnt on your server", /* XXX DEAD */
447         me.name, parv[0], acptr->name);
448     return 0;
449   }
450 #endif
451   if (!IsServer(cptr))
452   {
453     /*
454      * The kill originates from this server, initialize path.
455      * (In which case the 'path' may contain user suplied
456      * explanation ...or some nasty comment, sigh... >;-)
457      *
458      * ...!operhost!oper
459      * ...!operhost!oper (comment)
460      */
461     inpath = cptr->sockhost;
462
463     if (!EmptyString(path))
464     {
465       sprintf_irc(buf,
466           "%s%s (%s)", cptr->name, IsOper(sptr) ? "" : "(L)", path);
467       path = buf;
468     }
469     else
470       path = cptr->name;
471   }
472   else if (EmptyString(path))
473     path = "*no-path*";                /* Bogus server sending??? */
474   /*
475    * Notify all *local* opers about the KILL (this includes the one
476    * originating the kill, if from this server--the special numeric
477    * reply message is not generated anymore).
478    *
479    * Note: "acptr->name" is used instead of "user" because we may
480    *       have changed the target because of the nickname change.
481    */
482   if (IsLocOp(sptr) && !MyConnect(acptr))
483   {
484     sendto_one(sptr, err_str(ERR_NOPRIVILEGES), me.name, parv[0]); /* XXX DEAD */
485     return 0;
486   }
487   sendto_op_mask(IsServer(sptr) ? SNO_SERVKILL : SNO_OPERKILL, /* XXX DEAD */
488       "Received KILL message for %s. From %s Path: %s!%s",
489       acptr->name, parv[0], inpath, path);
490 #if defined(USE_SYSLOG) && defined(SYSLOG_KILL)
491   if (MyUser(acptr))
492   {                                /* get more infos when your local
493                                    clients are killed -- _dl */
494     if (IsServer(sptr))
495       ircd_log(L_TRACE, /* XXX DEAD */
496           "A local client %s!%s@%s KILLED from %s [%s] Path: %s!%s)",
497           acptr->name, acptr->user->username, acptr->user->host,
498           parv[0], sptr->name, inpath, path);
499     else
500       ircd_log(L_TRACE, /* XXX DEAD */
501           "A local client %s!%s@%s KILLED by %s [%s!%s@%s] (%s!%s)",
502           acptr->name, acptr->user->username, acptr->user->host,
503           parv[0], sptr->name, sptr->user->username, sptr->user->host,
504           inpath, path);
505   }
506   else if (IsOper(sptr))
507     ircd_log(L_TRACE, "KILL From %s For %s Path %s!%s", /* XXX DEAD */
508         parv[0], acptr->name, inpath, path);
509 #endif
510   /*
511    * And pass on the message to other servers. Note, that if KILL
512    * was changed, the message has to be sent to all links, also
513    * back.
514    * Suicide kills are NOT passed on --SRB
515    */
516   if (!MyConnect(acptr) || !MyConnect(sptr) || !IsAnOper(sptr))
517   {
518     sendto_highprot_butone(cptr, 10, ":%s " TOK_KILL " %s%s :%s!%s", /* XXX DEAD */
519         parv[0], NumNick(acptr), inpath, path);
520     /* We *can* have crossed a NICK with this numeric... --Run */
521     /* Note the following situation:
522      *  KILL SAA -->       X
523      *  <-- S NICK ... SAA | <-- SAA QUIT <-- S NICK ... SAA <-- SQUIT S
524      * Where the KILL reaches point X before the QUIT does.
525      * This would then *still* cause an orphan because the KILL doesn't reach S
526      * (because of the SQUIT), the QUIT is ignored (because of the KILL)
527      * and the second NICK ... SAA causes an orphan on the server at the
528      * right (which then isn't removed when the SQUIT arrives).
529      * Therefore we still need to detect numeric nick collisions too.
530      */
531     if (MyConnect(acptr) && IsServer(cptr))
532       sendto_one(cptr, "%s " TOK_KILL " %s%s :%s!%s (Ghost5)", /* XXX DEAD */
533           NumServ(&me), NumNick(acptr), inpath, path);
534     acptr->flags |= FLAGS_KILLED;
535   }
536
537   /*
538    * Tell the victim she/he has been zapped, but *only* if
539    * the victim is on current server--no sense in sending the
540    * notification chasing the above kill, it won't get far
541    * anyway (as this user don't exist there any more either)
542    */
543   if (MyConnect(acptr))
544     sendto_prefix_one(acptr, sptr, ":%s KILL %s :%s!%s", /* XXX DEAD */
545         parv[0], acptr->name, inpath, path);
546   /*
547    * Set FLAGS_KILLED. This prevents exit_one_client from sending
548    * the unnecessary QUIT for this. (This flag should never be
549    * set in any other place)
550    */
551   if (MyConnect(acptr) && MyConnect(sptr) && IsAnOper(sptr))
552     sprintf_irc(buf2, "Local kill by %s (%s)", sptr->name,
553         EmptyString(parv[parc - 1]) ? sptr->name : parv[parc - 1]);
554   else
555   {
556     if ((killer = strchr(path, ' ')))
557     {
558       while (*killer && *killer != '!')
559         killer--;
560       if (!*killer)
561         killer = path;
562       else
563         killer++;
564     }
565     else
566       killer = path;
567     sprintf_irc(buf2, "Killed (%s)", killer);
568   }
569   return exit_client(cptr, acptr, sptr, buf2);
570 }
571
572 #endif /* 0 */
573