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