Move check_if_ipmask() from support.* to match.*.
[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_reply.h"
90 #include "ircd_string.h"
91 #include "match.h"
92 #include "msg.h"
93 #include "numeric.h"
94 #include "numnicks.h"
95 #include "s_conf.h"
96 #include "s_misc.h"
97 #include "send.h"
98
99 #include <assert.h>
100 #include <stdlib.h>
101 #include <string.h>
102
103 /*
104  * ms_gline - server message handler
105  *
106  * parv[0] = Sender prefix
107  * parv[1] = Target: server numeric
108  * parv[2] = (+|-)<G-line mask>
109  * parv[3] = G-line lifetime
110  *
111  * From Uworld:
112  *
113  * parv[4] = Comment
114  *
115  * From somewhere else:
116  *
117  * parv[4] = Last modification time
118  * parv[5] = Comment
119  *
120  */
121 int
122 ms_gline(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
123 {
124   struct Client *acptr = 0;
125   struct Gline *agline;
126   unsigned int flags = 0;
127   time_t expire_off, lastmod = 0;
128   char *mask = parv[2], *target = parv[1], *reason = "No reason";
129
130   if (*mask == '!')
131   {
132     mask++;
133     flags |= GLINE_OPERFORCE; /* assume oper had WIDE_GLINE */
134   }
135
136   if ((parc == 3 && *mask == '-') || parc == 5)
137   {
138     if (!find_conf_byhost(cli_confs(cptr), cli_name(sptr), CONF_UWORLD))
139       return need_more_params(sptr, "GLINE");
140
141     if (parc > 4)
142       reason = parv[4];
143     flags |= GLINE_FORCE;
144   }
145   else if (parc > 5)
146   {
147     lastmod = atoi(parv[4]);
148     reason = parv[5];
149   }
150   else
151     return need_more_params(sptr, "GLINE");
152
153   if (IsServer(sptr))
154     flags |= GLINE_FORCE;
155
156   if (!(target[0] == '*' && target[1] == '\0')) {
157     if (!(acptr = FindNServer(target)))
158       return 0; /* no such server */
159
160     if (!IsMe(acptr)) { /* manually propagate */
161       if (!lastmod)
162         sendcmdto_one(sptr, CMD_GLINE, acptr,
163                       (parc == 3) ? "%C %s" : "%C %s %s :%s", acptr, mask,
164                       parv[3], reason);
165       else
166         sendcmdto_one(sptr, CMD_GLINE, acptr, "%C %s%s %s %s :%s", acptr,
167                       flags & GLINE_OPERFORCE ? "!" : "", mask, parv[3],
168                       parv[4], reason);
169
170       return 0;
171     }
172
173     flags |= GLINE_LOCAL;
174   }
175
176   if (*mask == '-')
177     mask++;
178   else if (*mask == '+') {
179     flags |= GLINE_ACTIVE;
180     mask++;
181   } else
182     flags |= GLINE_ACTIVE;
183
184   expire_off = parc < 5 ? 0 : atoi(parv[3]);
185
186   agline = gline_find(mask, GLINE_ANY | GLINE_EXACT);
187
188   if (agline) {
189     if (GlineIsLocal(agline) && !(flags & GLINE_LOCAL)) /* global over local */
190       gline_free(agline);
191     else if (!lastmod && ((flags & GLINE_ACTIVE) == GlineIsRemActive(agline)))
192       return gline_propagate(cptr, sptr, agline);
193     else if (!lastmod || GlineLastMod(agline) < lastmod) { /* new mod */
194       if (flags & GLINE_ACTIVE)
195         return gline_activate(cptr, sptr, agline, lastmod, flags);
196       else
197         return gline_deactivate(cptr, sptr, agline, lastmod, flags);
198     } else if (GlineLastMod(agline) == lastmod || IsBurstOrBurstAck(cptr))
199       return 0;
200     else
201       return gline_resend(cptr, agline); /* other server desynched WRT gline */
202   } else if (parc == 3 && !(flags & GLINE_ACTIVE)) {
203     /* U-lined server removing a G-line we don't have; propagate the removal
204      * anyway.
205      */
206     if (!(flags & GLINE_LOCAL))
207       sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* -%s", mask);
208     return 0;
209   } else if (parc < 5)
210     return need_more_params(sptr, "GLINE");
211
212   return gline_add(cptr, sptr, mask, reason, expire_off, lastmod, flags);
213 }
214
215 /*
216  * mo_gline - oper message handler
217  *
218  * parv[0] = Sender prefix
219  * parv[1] = [[+|-]<G-line mask>]
220  *
221  * Local (to me) style:
222  *
223  * parv[2] = [Expiration offset]
224  * parv[3] = [Comment]
225  *
226  * Global (or remote local) style:
227  *
228  * parv[2] = [target]
229  * parv[3] = [Expiration offset]
230  * parv[4] = [Comment]
231  *
232  */
233 int
234 mo_gline(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
235 {
236   struct Client *acptr = 0;
237   struct Gline *agline;
238   unsigned int flags = 0;
239   time_t expire_off;
240   char *mask = parv[1], *target = 0, *reason;
241
242   if (parc < 2)
243     return gline_list(sptr, 0);
244
245   if (*mask == '!') {
246     mask++;
247
248     if (HasPriv(sptr, PRIV_WIDE_GLINE))
249       flags |= GLINE_OPERFORCE;
250   }
251
252   if (*mask == '+') {
253     flags |= GLINE_ACTIVE;
254     mask++;
255
256   } else if (*mask == '-')
257     mask++;
258   else
259     return gline_list(sptr, mask);
260
261   if (parc == 4) {
262     expire_off = atoi(parv[2]);
263     reason = parv[3];
264     flags |= GLINE_LOCAL;
265   } else if (parc > 4) {
266     target = parv[2];
267     expire_off = atoi(parv[3]);
268     reason = parv[4];
269   } else
270     return need_more_params(sptr, "GLINE");
271
272   if (target)
273   {
274     if (!(target[0] == '*' && target[1] == '\0'))
275     {
276       if (!(acptr = find_match_server(target)))
277         return send_reply(sptr, ERR_NOSUCHSERVER, target);
278
279       /* manually propagate, since we don't set it */
280       if (!IsMe(acptr))
281       {
282         if (!feature_bool(FEAT_CONFIG_OPERCMDS))
283           return send_reply(sptr, ERR_DISABLED, "GLINE");
284
285         if (!HasPriv(sptr, PRIV_GLINE))
286           return send_reply(sptr, ERR_NOPRIVILEGES);
287
288         sendcmdto_one(sptr, CMD_GLINE, acptr, "%C %s%c%s %s %Tu :%s", acptr,
289                       flags & GLINE_OPERFORCE ? "!" : "",
290                       flags & GLINE_ACTIVE ? '+' : '-', mask, parv[3],
291                       TStime(), reason);
292         return 0;
293       }
294       flags |= GLINE_LOCAL;
295     }
296   }
297
298   if (!(flags & GLINE_LOCAL) && !feature_bool(FEAT_CONFIG_OPERCMDS))
299     return send_reply(sptr, ERR_DISABLED, "GLINE");
300
301   if (!HasPriv(sptr, (flags & GLINE_LOCAL ? PRIV_LOCAL_GLINE : PRIV_GLINE)))
302     return send_reply(sptr, ERR_NOPRIVILEGES);
303
304   agline = gline_find(mask, GLINE_ANY | GLINE_EXACT);
305
306   if (agline) {
307     if (GlineIsLocal(agline) && !(flags & GLINE_LOCAL)) /* global over local */
308       gline_free(agline);
309     else {
310       if (!GlineLastMod(agline)) /* force mods to Uworld-set G-lines local */
311         flags |= GLINE_LOCAL;
312
313       if (flags & GLINE_ACTIVE)
314         return gline_activate(cptr, sptr, agline,
315                               GlineLastMod(agline) ? TStime() : 0, flags);
316       else
317         return gline_deactivate(cptr, sptr, agline,
318                                 GlineLastMod(agline) ? TStime() : 0, flags);
319     }
320   }
321
322   return gline_add(cptr, sptr, mask, reason, expire_off, TStime(), flags);
323 }
324
325 /*
326  * m_gline - user message handler
327  *
328  * parv[0] = Sender prefix
329  * parv[1] = [<server name>]
330  *
331  */
332 int
333 m_gline(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
334 {
335   if (parc < 2)
336     return send_reply(sptr, ERR_NOSUCHGLINE, "");
337
338   return gline_list(sptr, parv[1]);
339 }