Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / m_gline.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_gline.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 "gline.h"
86 #include "hash.h"
87 #include "ircd.h"
88 #include "ircd_features.h"
89 #include "ircd_log.h"
90 #include "ircd_reply.h"
91 #include "ircd_string.h"
92 #include "match.h"
93 #include "msg.h"
94 #include "numeric.h"
95 #include "numnicks.h"
96 #include "s_conf.h"
97 #include "s_debug.h"
98 #include "s_misc.h"
99 #include "send.h"
100
101 /* #include <assert.h> -- Now using assert in ircd_log.h */
102 #include <stdlib.h>
103 #include <string.h>
104
105 /*
106  * ms_gline - server message handler
107  *
108  * parv[0] = Sender prefix
109  * parv[1] = Target: server numeric
110  * parv[2] = (+|-)<G-line mask>
111  *
112  * For other parameters, see doc/readme.gline.
113  */
114 int
115 ms_gline(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
116 {
117   struct Client *acptr = 0;
118   struct Gline *agline = 0;
119   unsigned int flags = 0;
120   enum GlineAction action = GLINE_MODIFY;
121   time_t expire_off = 0, lastmod = 0, lifetime = 0;
122   char *mask = parv[2], *target = parv[1], *reason = "No reason", *tmp = 0;
123
124   if (parc < 3)
125     return need_more_params(sptr, "GLINE");
126
127   if (IsServer(sptr))
128     flags |= GLINE_FORCE;
129
130   if (*mask == '!') {
131     mask++;
132     flags |= GLINE_OPERFORCE; /* assume oper had WIDE_GLINE */
133   }
134
135   switch (*mask) { /* handle +, -, <, and > */
136   case '+': /* activate the G-line */
137     action = GLINE_ACTIVATE;
138     mask++;
139     break;
140
141   case '-': /* deactivate the G-line */
142     action = GLINE_DEACTIVATE;
143     mask++;
144     break;
145
146   case '>': /* locally activate the G-line */
147     action = GLINE_LOCAL_ACTIVATE;
148     mask++;
149     break;
150
151   case '<': /* locally deactivate the G-line */
152     action = GLINE_LOCAL_DEACTIVATE;
153     mask++;
154     break;
155   }
156
157   /* Now, let's figure out if it's a local or global G-line */
158   if (action == GLINE_LOCAL_ACTIVATE || action == GLINE_LOCAL_DEACTIVATE ||
159       (target[0] == '*' && target[1] == '\0'))
160     flags |= GLINE_GLOBAL;
161   else
162     flags |= GLINE_LOCAL;
163
164   /* now figure out if we need to resolve a server */
165   if ((action == GLINE_LOCAL_ACTIVATE || action == GLINE_LOCAL_DEACTIVATE ||
166        (flags & GLINE_LOCAL)) && !(acptr = FindNServer(target)))
167     return 0; /* no such server, jump out */
168
169   /* If it's a local activate/deactivate and server isn't me, propagate it */
170   if ((action == GLINE_LOCAL_ACTIVATE || action == GLINE_LOCAL_DEACTIVATE) &&
171       !IsMe(acptr)) {
172     Debug((DEBUG_DEBUG, "I am forwarding a local change to a global gline "
173            "to a remote server; target %s, mask %s, operforce %s, action %s",
174            target, mask, flags & GLINE_OPERFORCE ? "YES" : "NO",
175            action == GLINE_LOCAL_ACTIVATE ? ">" : "<"));
176
177     sendcmdto_one(sptr, CMD_GLINE, acptr, "%C %s%c%s", acptr,
178                   flags & GLINE_OPERFORCE ? "!" : "",
179                   action == GLINE_LOCAL_ACTIVATE ? ">" : "<", mask);
180
181     return 0; /* all done */
182   }
183
184   /* Next, try to find the G-line... */
185   if ((flags & GLINE_GLOBAL) || IsMe(acptr)) /* don't bother if it's not me! */
186     agline = gline_find(mask, flags | GLINE_ANY | GLINE_EXACT);
187
188   /* We now have all the pieces to tell us what we've got; let's put
189    * it all together and convert the rest of the arguments.
190    */
191
192   /* Handle the local G-lines first... */
193   if (flags & GLINE_LOCAL) {
194     assert(acptr);
195
196     /* normalize the action, first */
197     if (action == GLINE_LOCAL_ACTIVATE || action == GLINE_MODIFY)
198       action = GLINE_ACTIVATE;
199     else if (action == GLINE_LOCAL_DEACTIVATE)
200       action = GLINE_DEACTIVATE;
201
202     if (action == GLINE_ACTIVATE) { /* get expiration and reason */
203       if (parc < 5) /* check parameter count... */
204         return need_more_params(sptr, "GLINE");
205
206       expire_off = atoi(parv[3]); /* get expiration... */
207       reason = parv[parc - 1]; /* and reason */
208
209       if (IsMe(acptr)) {
210         if (agline) /* G-line already exists, so let's ignore it... */
211           return 0;
212
213         /* OK, create the local G-line */
214         Debug((DEBUG_DEBUG, "I am creating a local G-line here; target %s, "
215                "mask %s, operforce %s, action %s, expire %Tu, reason: %s",
216                target, mask, flags & GLINE_OPERFORCE ? "YES" : "NO",
217                action == GLINE_ACTIVATE ? "+" : "-", expire_off, reason));
218
219         return gline_add(cptr, sptr, mask, reason, expire_off, lastmod,
220                          lifetime, flags | GLINE_ACTIVE);
221       }
222     } else if (IsMe(acptr)) { /* destroying a local G-line */
223       if (!agline) /* G-line doesn't exist, so let's complain... */
224         return send_reply(sptr, ERR_NOSUCHGLINE, mask);
225
226       /* Let's now destroy the G-line */;
227       Debug((DEBUG_DEBUG, "I am destroying a local G-line here; target %s, "
228              "mask %s, operforce %s, action %s", target, mask,
229              flags & GLINE_OPERFORCE ? "YES" : "NO",
230              action == GLINE_ACTIVATE ? "+" : "-"));
231
232       return gline_destroy(cptr, sptr, agline);
233     }
234
235     /* OK, we've converted arguments; if it's not for us, forward */
236     /* UPDATE NOTE: Once all servers are updated to u2.10.12.11, the
237      * format string in this sendcmdto_one() may be updated to omit
238      * <lastmod> for GLINE_ACTIVATE and to omit <expire>, <lastmod>,
239      * and <reason> for GLINE_DEACTIVATE.
240      */
241     assert(!IsMe(acptr));
242
243     Debug((DEBUG_DEBUG, "I am forwarding a local G-line to a remote server; "
244            "target %s, mask %s, operforce %s, action %s, expire %Tu, "
245            "lastmod %Tu, reason: %s", target, mask,
246            flags & GLINE_OPERFORCE ? "YES" : "NO",
247            action == GLINE_ACTIVATE ? "+" :  "-", expire_off, CurrentTime,
248            reason));
249
250     sendcmdto_one(sptr, CMD_GLINE, acptr, "%C %s%c%s %Tu %Tu :%s",
251                   acptr, flags & GLINE_OPERFORCE ? "!" : "",
252                   action == GLINE_ACTIVATE ? '+' : '-', mask, expire_off,
253                   CurrentTime, reason);
254
255     return 0; /* all done */
256   }
257
258   /* can't modify a G-line that doesn't exist, so remap to activate */
259   if (!agline && action == GLINE_MODIFY)
260     action = GLINE_ACTIVATE;
261
262   /* OK, let's figure out what other parameters we may have... */
263   switch (action) {
264   case GLINE_LOCAL_ACTIVATE: /* locally activating a G-line */
265   case GLINE_LOCAL_DEACTIVATE: /* locally deactivating a G-line */
266     if (!agline) /* no G-line to locally activate or deactivate? */
267       return send_reply(sptr, ERR_NOSUCHGLINE, mask);
268     break; /* no additional parameters to manipulate */
269
270   case GLINE_ACTIVATE: /* activating a G-line */
271   case GLINE_DEACTIVATE: /* deactivating a G-line */
272     /* in either of these cases, we have at least a lastmod parameter */
273     if (parc < 4)
274       return need_more_params(sptr, "GLINE");
275     else if (parc == 4) /* lastmod only form... */
276       lastmod = atoi(parv[3]);
277     /*FALLTHROUGH*/
278   case GLINE_MODIFY: /* modifying a G-line */
279     /* convert expire and lastmod, look for lifetime and reason */
280     if (parc > 4) { /* protect against fall-through from 4-param form */
281       if (parc < 5)
282         return need_more_params(sptr, "GLINE");
283
284       expire_off = atoi(parv[3]); /* convert expiration and lastmod */
285       lastmod = atoi(parv[4]);
286
287       flags |= GLINE_EXPIRE; /* we have an expiration time update */
288
289       if (parc > 6) { /* no question, have a lifetime and reason */
290         lifetime = atoi(parv[5]);
291         reason = parv[parc - 1];
292
293         flags |= GLINE_LIFETIME | GLINE_REASON;
294       } else if (parc == 6) { /* either a lifetime or a reason */
295         if (!agline || /* gline creation, has to be the reason */
296             /* trial-convert as lifetime, and if it doesn't fully convert,
297              * it must be the reason */
298             ((lifetime = strtoul(parv[5], &tmp, 10)) && !*tmp)) {
299           lifetime = 0;
300           reason = parv[5];
301
302           flags |= GLINE_REASON; /* have a reason update */
303         } else if (lifetime)
304           flags |= GLINE_LIFETIME; /* have a lifetime update */
305       }
306     }
307   }
308
309   Debug((DEBUG_DEBUG, "I have a global G-line I am acting upon now; "
310          "target %s, mask %s, operforce %s, action %s, expire %Tu, "
311          "lastmod %Tu, lifetime %Tu, reason: %s; gline %s!  (fields "
312          "present: %s %s %s)", target, mask,
313          flags & GLINE_OPERFORCE ? "YES" : "NO",
314          action == GLINE_ACTIVATE ? "+" :
315          (action == GLINE_DEACTIVATE ? "-" :
316           (action == GLINE_LOCAL_ACTIVATE ? ">" :
317            (action == GLINE_LOCAL_DEACTIVATE ? "<" : "(MODIFY)"))),
318          expire_off, lastmod, lifetime, reason,
319          agline ? "EXISTS" : "does not exist",
320          flags & GLINE_EXPIRE ? "expire" : "",
321          flags & GLINE_LIFETIME ? "lifetime" : "",
322          flags & GLINE_REASON ? "reason" : ""));
323
324   /* OK, at this point, we have converted all available parameters.
325    * Let's actually do the action!
326    */
327   if (agline)
328     return gline_modify(cptr, sptr, agline, action, reason, expire_off,
329                         lastmod, lifetime, flags);
330
331   assert(action != GLINE_LOCAL_ACTIVATE);
332   assert(action != GLINE_LOCAL_DEACTIVATE);
333   assert(action != GLINE_MODIFY);
334
335   return gline_add(cptr, sptr, mask, reason, expire_off, lastmod, lifetime,
336                    flags | ((action == GLINE_ACTIVATE) ? GLINE_ACTIVE : 0));
337 }
338
339 /*
340  * mo_gline - oper message handler
341  *
342  * parv[0] = Sender prefix
343  * parv[1] = [[+|-]<G-line mask>]
344  *
345  * For other parameters, see doc/readme.gline.
346  */
347 int
348 mo_gline(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
349 {
350   struct Client *acptr = 0;
351   struct Gline *agline = 0;
352   unsigned int flags = 0;
353   enum GlineAction action = GLINE_MODIFY;
354   time_t expire_off = 0;
355   char *mask = parv[1], *target = 0, *reason = 0;
356
357   if (parc < 2)
358     return gline_list(sptr, 0);
359
360   if (*mask == '!') {
361     mask++;
362
363     if (HasPriv(sptr, PRIV_WIDE_GLINE))
364       flags |= GLINE_OPERFORCE;
365   }
366
367   switch (*mask) { /* handle +, -, <, and > */
368   case '+': /* activate the G-line */
369     action = GLINE_ACTIVATE;
370     mask++;
371     break;
372
373   case '-': /* deactivate the G-line */
374     action = GLINE_DEACTIVATE;
375     mask++;
376     break;
377
378   case '>': /* locally activate the G-line */
379     action = GLINE_LOCAL_ACTIVATE;
380     mask++;
381     break;
382
383   case '<': /* locally deactivate the G-line */
384     action = GLINE_LOCAL_DEACTIVATE;
385     mask++;
386     break;
387   }
388
389   /* OK, let's figure out the parameters... */
390   switch (action) {
391   case GLINE_MODIFY: /* no specific action on the G-line... */
392     if (parc == 2) /* user wants a listing of a specific G-line */
393       return gline_list(sptr, mask);
394     else if (parc < 4) /* must have target and expire, minimum */
395       return need_more_params(sptr, "GLINE");
396
397     target = parv[2]; /* get the target... */
398     expire_off = atoi(parv[3]); /* and the expiration */
399
400     flags |= GLINE_EXPIRE; /* remember that we got an expire time */
401
402     if (parc > 4) { /* also got a reason... */
403       reason = parv[4];
404       flags |= GLINE_REASON;
405     }
406
407     /* target is not global, interpolate action and require reason */
408     if (target[0] != '*' || target[1] != '\0') {
409       if (!reason) /* have to have a reason for this */
410         return need_more_params(sptr, "GLINE");
411
412       action = GLINE_ACTIVATE;
413     }
414     break;
415
416   case GLINE_LOCAL_ACTIVATE: /* locally activate a G-line */
417   case GLINE_LOCAL_DEACTIVATE: /* locally deactivate a G-line */
418     if (parc > 2) /* if target is available, pick it */
419       target = parv[2];
420     break;
421
422   case GLINE_ACTIVATE: /* activating/adding a G-line */
423   case GLINE_DEACTIVATE: /* deactivating/removing a G-line */
424     if (parc < 3)
425       return need_more_params(sptr, "GLINE");
426
427     if (parc > 3) {
428       /* get expiration and target */
429       expire_off = atoi(parv[parc - 2]);
430       reason = parv[parc - 1];
431
432       flags |= GLINE_EXPIRE | GLINE_REASON; /* remember that we got 'em */
433
434       if (parc > 4) /* also have a target! */
435         target = parv[2];
436     } else {
437       target = parv[2]; /* target has to be present, and has to be '*' */
438
439       if (target[0] != '*' || target[1] != '\0')
440         return need_more_params(sptr, "GLINE");
441     }
442     break;
443   }
444
445   /* Now let's figure out which is the target server */
446   if (!target) /* no target, has to be me... */
447     acptr = &me;
448   /* if it's not '*', look up the server */
449   else if ((target[0] != '*' || target[1] != '\0') &&
450            !(acptr = find_match_server(target)))
451     return send_reply(sptr, ERR_NOSUCHSERVER, target);
452
453   /* Now, is the G-line local or global? */
454   if (action == GLINE_LOCAL_ACTIVATE || action == GLINE_LOCAL_DEACTIVATE ||
455       !acptr)
456     flags |= GLINE_GLOBAL;
457   else /* it's some form of local G-line */
458     flags |= GLINE_LOCAL;
459
460   /* If it's a local activate/deactivate and server isn't me, propagate it */
461   if ((action == GLINE_LOCAL_ACTIVATE || action == GLINE_LOCAL_DEACTIVATE) &&
462       !IsMe(acptr)) {
463     Debug((DEBUG_DEBUG, "I am forwarding a local change to a global gline "
464            "to a remote server; target %s, mask %s, operforce %s, action %s",
465            cli_name(acptr), mask, flags & GLINE_OPERFORCE ? "YES" : "NO",
466            action == GLINE_LOCAL_ACTIVATE ? ">" : "<"));
467
468     sendcmdto_one(sptr, CMD_GLINE, acptr, "%C %s%c%s", acptr,
469                   flags & GLINE_OPERFORCE ? "!" : "",
470                   action == GLINE_LOCAL_ACTIVATE ? ">" : "<", mask);
471
472     return 0; /* all done */
473   }
474
475   /* Next, try to find the G-line... */
476   if ((flags & GLINE_GLOBAL) || IsMe(acptr)) /* don't bother if it's not me! */
477     agline = gline_find(mask, flags | GLINE_ANY | GLINE_EXACT);
478
479   /* We now have all the pieces to tell us what we've got; let's put
480    * it all together and convert the rest of the arguments.
481    */
482
483   /* Handle the local G-lines first... */
484   if (flags & GLINE_LOCAL) {
485     assert(acptr);
486
487     /* normalize the action, first */
488     if (action == GLINE_LOCAL_ACTIVATE || action == GLINE_MODIFY)
489       action = GLINE_ACTIVATE;
490     else if (action == GLINE_LOCAL_DEACTIVATE)
491       action = GLINE_DEACTIVATE;
492
493     /* If it's not for us, forward */
494     /* UPDATE NOTE: Once all servers are updated to u2.10.12.11, the
495      * format string in this sendcmdto_one() may be updated to omit
496      * <lastmod> for GLINE_ACTIVATE and to omit <expire>, <lastmod>,
497      * and <reason> for GLINE_DEACTIVATE.
498      */
499
500     if (!IsMe(acptr)) {
501       Debug((DEBUG_DEBUG, "I am forwarding a local G-line to a remote "
502              "server; target %s, mask %s, operforce %s, action %s, "
503              "expire %Tu, reason %s", target, mask,
504              flags & GLINE_OPERFORCE ? "YES" : "NO",
505              action == GLINE_ACTIVATE ? "+" : "-", expire_off, reason));
506
507       sendcmdto_one(sptr, CMD_GLINE, acptr, "%C %s%c%s %Tu %Tu :%s",
508                     acptr, flags & GLINE_OPERFORCE ? "!" : "",
509                     action == GLINE_ACTIVATE ? "+" : "-", mask, expire_off,
510                     CurrentTime, reason);
511
512       return 0; /* all done */
513     }
514
515     /* let's handle activation... */
516     if (action == GLINE_ACTIVATE) {
517       if (agline) /* G-line already exists, so let's ignore it... */
518         return 0;
519
520       /* OK, create the local G-line */
521       Debug((DEBUG_DEBUG, "I am creating a local G-line here; target %s, "
522              "mask %s, operforce %s, action  %s, expire %Tu, reason: %s",
523              target, mask, flags & GLINE_OPERFORCE ? "YES" : "NO",
524              action == GLINE_ACTIVATE ? "+" : "-", expire_off, reason));
525
526       return gline_add(cptr, sptr, mask, reason, expire_off, 0, 0,
527                        flags | GLINE_ACTIVE);
528     } else { /* OK, it's a deactivation/destruction */
529       if (!agline) /* G-line doesn't exist, so let's complain... */
530         return send_reply(sptr, ERR_NOSUCHGLINE, mask);
531
532       /* Let's now destroy the G-line */
533       Debug((DEBUG_DEBUG, "I am destroying a local G-line here; target %s, "
534              "mask %s, operforce %s, action %s", target, mask,
535              flags & GLINE_OPERFORCE ? "YES" : "NO",
536              action == GLINE_ACTIVATE ? "+" : "-"));
537
538       return gline_destroy(cptr, sptr, agline);
539     }
540   }
541
542   /* can't modify a G-line that doesn't exist... */
543   if (!agline &&
544       (action == GLINE_MODIFY || action == GLINE_LOCAL_ACTIVATE ||
545        action == GLINE_LOCAL_DEACTIVATE))
546     return send_reply(sptr, ERR_NOSUCHGLINE, mask);
547
548   Debug((DEBUG_DEBUG, "I have a global G-line I am acting upon now; "
549          "target %s, mask %s, operforce %s, action %s, expire %Tu, "
550          "reason: %s; gline %s!  (fields present: %s %s)", target, 
551          mask, flags & GLINE_OPERFORCE ? "YES" : "NO",
552          action == GLINE_ACTIVATE ? "+" :
553          (action == GLINE_DEACTIVATE ? "-" :
554           (action == GLINE_LOCAL_ACTIVATE ? ">" :
555            (action == GLINE_LOCAL_DEACTIVATE ? "<" : "(MODIFY)"))),
556          expire_off, reason, agline ? "EXISTS" : "does not exist",
557          flags & GLINE_EXPIRE ? "expire" : "",
558          flags & GLINE_REASON ? "reason" : ""));
559
560   if (agline) /* modifying an existing G-line */
561     return gline_modify(cptr, sptr, agline, action, reason, expire_off,
562                         CurrentTime, 0, flags);
563
564   assert(action != GLINE_LOCAL_ACTIVATE);
565   assert(action != GLINE_LOCAL_DEACTIVATE);
566   assert(action != GLINE_MODIFY);
567
568   /* create a new G-line */
569   return gline_add(cptr, sptr, mask, reason, expire_off, CurrentTime, 0,
570                    flags | ((action == GLINE_ACTIVATE) ? GLINE_ACTIVE : 0));
571 }
572
573 /*
574  * m_gline - user message handler
575  *
576  * parv[0] = Sender prefix
577  * parv[1] = [<server name>]
578  *
579  */
580 int
581 m_gline(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
582 {
583   if (parc < 2)
584     return send_reply(sptr, ERR_NOSUCHGLINE, "");
585
586   return gline_list(sptr, parv[1]);
587 }