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