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