Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / gline.c
1 /*
2  * IRC - Internet Relay Chat, ircd/gline.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Finland
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 1, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  * $Id$
21  */
22 #include "gline.h"
23 #include "client.h"
24 #include "ircd.h"
25 #include "ircd_alloc.h"
26 #include "ircd_reply.h"
27 #include "ircd_string.h"
28 #include "match.h"
29 #include "numeric.h"
30 #include "s_bsd.h"
31 #include "s_misc.h"
32 #include "send.h"
33 #include "struct.h"
34 #include "support.h"
35 #include "msg.h"
36 #include "numnicks.h"
37 #include "numeric.h"
38 #include "sys.h"    /* FALSE bleah */
39
40 #include <assert.h>
41 #include <string.h>
42
43 struct Gline* GlobalGlineList  = 0;
44 struct Gline* BadChanGlineList = 0;
45
46 static void
47 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
48 {
49   char *tmp;
50
51   if (!(tmp = strchr(userhost, '@'))) {
52     *user_p = def_user;
53     *host_p = userhost;
54   } else {
55     *user_p = userhost;
56     *(tmp++) = '\0';
57     *host_p = tmp;
58   }
59 }
60
61 static struct Gline *
62 make_gline(char *userhost, char *reason, time_t expire, time_t lastmod,
63            unsigned int flags)
64 {
65   struct Gline *gline, *sgline, *after = 0;
66   char *user, *host;
67
68   if (!(flags & GLINE_BADCHAN)) { /* search for overlapping glines first */
69     canon_userhost(userhost, &user, &host, "*"); /* find user and host */
70
71     for (gline = GlobalGlineList; gline; gline = sgline) {
72       sgline = gline->gl_next;
73
74       if (gline->gl_expire <= CurrentTime)
75         gline_free(gline);
76       else if ((gline->gl_flags & GLINE_LOCAL) != (flags & GLINE_LOCAL))
77         continue;
78       else if (!mmatch(gline->gl_user, user) && /* gline contains new mask */
79                !mmatch(gline->gl_host, host)) {
80         if (expire <= gline->gl_expire) /* will expire before wider gline */
81           return 0;
82         else
83           after = gline; /* stick new gline after this one */
84       } else if (!mmatch(user, gline->gl_user) && /* new mask contains gline */
85                  !mmatch(host, gline->gl_host) &&
86                  gline->gl_expire <= expire) /* gline expires before new one */
87         gline_free(gline); /* save some memory */
88     }
89   }
90
91   gline = (struct Gline *)MyMalloc(sizeof(struct Gline)); /* alloc memory */
92   assert(0 != gline);
93
94   DupString(gline->gl_reason, reason); /* initialize gline... */
95   gline->gl_expire = expire;
96   gline->gl_lastmod = lastmod;
97   gline->gl_flags = flags & GLINE_MASK;
98
99   if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
100     DupString(gline->gl_user, userhost); /* first, remember channel */
101     gline->gl_host = 0;
102
103     gline->gl_next = BadChanGlineList; /* then link it into list */
104     gline->gl_prev_p = &BadChanGlineList;
105     if (BadChanGlineList)
106       BadChanGlineList->gl_prev_p = &gline->gl_next;
107     BadChanGlineList = gline;
108   } else {
109     DupString(gline->gl_user, user); /* remember them... */
110     DupString(gline->gl_host, host);
111
112     if (check_if_ipmask(host)) /* mark if it's an IP mask */
113       gline->gl_flags |= GLINE_IPMASK;
114
115     if (after) {
116       gline->gl_next = after->gl_next;
117       gline->gl_prev_p = &after->gl_next;
118       if (after->gl_next)
119         after->gl_next->gl_prev_p = &gline->gl_next;
120       after->gl_next = gline;
121     } else {
122       gline->gl_next = GlobalGlineList; /* then link it into list */
123       gline->gl_prev_p = &GlobalGlineList;
124       if (GlobalGlineList)
125         GlobalGlineList->gl_prev_p = &gline->gl_next;
126       GlobalGlineList = gline;
127     }
128   }
129
130   return gline;
131 }
132
133 static int
134 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
135 {
136   struct Client *acptr;
137   int fd, retval = 0, tval;
138
139   if (!GlineIsActive(gline)) /* no action taken on inactive glines */
140     return 0;
141
142   for (fd = HighestFd; fd >= 0; --fd) {
143     /*
144      * get the users!
145      */
146     if ((acptr = LocalClientArray[fd])) {
147       if (!acptr->user)
148         continue;
149
150       if ((GlineIsIpMask(gline) ? match(gline->gl_host, acptr->sock_ip) :
151            match(gline->gl_host, acptr->sockhost)) == 0 &&
152           (!acptr->user->username ||
153            match(gline->gl_user, acptr->user->username) == 0)) {
154         /* ok, here's one that got G-lined */
155         send_reply(acptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s",
156                    gline->gl_reason);
157
158         /* let the ops know about it */
159         sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
160                              get_client_name(acptr, FALSE));
161
162         /* and get rid of him */
163         if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
164                                     gline->gl_reason)))
165           retval = tval; /* retain killed status */
166       }
167     }
168   }
169
170   return retval;
171 }
172
173 static void
174 propagate_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
175 {
176   if (GlineIsLocal(gline) || (IsUser(sptr) && !gline->gl_lastmod))
177     return;
178
179   if (gline->gl_lastmod)
180     sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
181                           GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
182                           GlineIsBadChan(gline) ? "" : "@",
183                           GlineIsBadChan(gline) ? "" : gline->gl_host,
184                           gline->gl_expire - CurrentTime, gline->gl_lastmod,
185                           gline->gl_reason);
186   else
187     sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu :%s",
188                           GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
189                           GlineIsBadChan(gline) ? "" : "@",
190                           GlineIsBadChan(gline) ? "" : gline->gl_host,
191                           gline->gl_expire - CurrentTime, gline->gl_reason);
192 }
193
194 int 
195 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
196           char *reason, time_t expire, time_t lastmod, unsigned int flags)
197 {
198   struct Gline *agline;
199
200   assert(0 != userhost);
201   assert(0 != reason);
202
203   /*
204    * You cannot set a negative (or zero) expire time, nor can you set an
205    * expiration time for greater than GLINE_MAX_EXPIRE.
206    */
207   if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
208     if (!IsServer(sptr) && MyConnect(sptr))
209       send_reply(sptr, ERR_BADEXPIRE, expire);
210     return 0;
211   }
212
213   expire += CurrentTime; /* convert from lifetime to timestamp */
214
215   /* NO_OLD_GLINE allows *@#channel to work correctly */
216   if (*userhost == '#' || *userhost == '&' || *userhost == '+'
217 # ifndef NO_OLD_GLINE
218       || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
219 # endif /* OLD_GLINE */
220       ) {
221 # ifndef LOCAL_BADCHAN
222     if (flags & GLINE_LOCAL)
223       return 0;
224 # endif
225     flags |= GLINE_BADCHAN;
226   }
227
228   /* Inform ops... */
229   sendto_opmask_butone(0, SNO_GLINE, "%s adding %s %s for %s, expiring at "
230                        "%Tu: %s",
231                        IsServer(sptr) ? sptr->name : sptr->user->server->name,
232                        flags & GLINE_LOCAL ? "local" : "global",
233                        flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
234                        expire + TSoffset, reason);
235
236 #ifdef GPATH
237   /* and log it */
238   write_log(GPATH, "# %Tu %C adding %s %s for %s, expiring at %Tu: %s\n",
239             TStime(), sptr, flags & GLINE_LOCAL ? "local" : "global",
240             flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
241             expire + TSoffset, reason);
242 #endif /* GPATH */
243
244   /* make the gline */
245   agline = make_gline(userhost, reason, expire, lastmod, flags);
246
247   if (!agline) /* if it overlapped, silently return */
248     return 0;
249
250   propagate_gline(cptr, sptr, agline);
251
252   if (GlineIsBadChan(agline))
253     return 0;
254
255 #ifdef GPATH
256   /* this can be inserted into the conf */
257   write_log(GPATH, "%c:%s:%s:%s\n", GlineIsIpMask(agline) ? 'k' : 'K',
258             GlineHost(agline), GlineReason(agline), GlineUser(agline));
259 #endif /* GPATH */
260
261   return do_gline(cptr, sptr, agline); /* knock off users if necessary */
262 }
263
264 int
265 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
266                time_t lastmod, unsigned int flags)
267 {
268   unsigned int saveflags = 0;
269
270   assert(0 != gline);
271   assert(!GlineIsLocal(gline));
272
273   saveflags = gline->gl_flags;
274
275   if (flags & GLINE_LOCAL)
276     gline->gl_flags &= ~GLINE_LDEACT;
277   else {
278     gline->gl_flags |= GLINE_ACTIVE;
279
280     if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
281       gline->gl_lastmod++;
282     else
283       gline->gl_lastmod = lastmod;
284   }
285
286   if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
287     return 0; /* was active to begin with */
288
289   /* Inform ops and log it */
290   sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
291                        "expiring at %Tu: %s",
292                        IsServer(sptr) ? sptr->name : sptr->user->server->name,
293                        GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
294                        gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
295                        GlineIsBadChan(gline) ? "" : gline->gl_host,
296                        gline->gl_expire + TSoffset, gline->gl_reason);
297
298 #ifdef GPATH
299   write_log(GPATH, "# %Tu %C activating global %s for %s%s%s, expiring at "
300             "%Tu: %s\n", TStime(), sptr,
301             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
302             gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
303             GlineIsBadChan(gline) ? "" : gline->gl_host,
304             gline->gl_expire + TSoffset, gline->gl_reason);
305 #endif /* GPATH */
306
307   if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
308     propagate_gline(cptr, sptr, gline);
309
310   return GlineIsBadChan(gline) ? 0 : do_gline(cptr, sptr, gline);
311 }
312
313 int
314 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
315                  time_t lastmod, unsigned int flags)
316 {
317   unsigned int saveflags = 0;
318   char *msg;
319
320   assert(0 != gline);
321
322   saveflags = gline->gl_flags;
323
324   if (GlineIsLocal(gline))
325     msg = "removing local";
326   else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL))
327     msg = "removing global";
328   else {
329     msg = "deactivating global";
330
331     if (flags & GLINE_LOCAL)
332       gline->gl_flags |= GLINE_LDEACT;
333     else {
334       gline->gl_flags &= ~GLINE_ACTIVE;
335
336       if (gline->gl_lastmod >= lastmod)
337         gline->gl_lastmod++;
338       else
339         gline->gl_lastmod = lastmod;
340     }
341
342     if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
343       return 0; /* was inactive to begin with */
344   }
345
346   /* Inform ops and log it */
347   sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
348                        "%s",
349                        IsServer(sptr) ? sptr->name : sptr->user->server->name,
350                        msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
351                        gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
352                        GlineIsBadChan(gline) ? "" : gline->gl_host,
353                        gline->gl_expire + TSoffset, gline->gl_reason);
354
355 #ifdef GPATH
356   write_log(GPATH, "# %Tu %C %s %s for %s%s%s, expiring at %Tu: %s\n",
357             TStime(), sptr, msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
358             gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
359             GlineIsBadChan(gline) ? "" : gline->gl_host,
360             gline->gl_expire + TSoffset, gline->gl_reason);
361 #endif /* GPATH */
362
363   if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
364     gline_free(gline);
365   else if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
366     propagate_gline(cptr, sptr, gline);
367
368   return 0;
369 }
370
371 struct Gline *
372 gline_find(char *userhost, unsigned int flags)
373 {
374   struct Gline *gline;
375   struct Gline *sgline;
376   char *user, *host, *t_uh;
377
378   if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
379     for (gline = BadChanGlineList; gline; gline = sgline) {
380       sgline = gline->gl_next;
381
382       if (gline->gl_expire <= CurrentTime)
383         gline_free(gline);
384       else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
385                 match(gline->gl_user, userhost)) == 0)
386         return gline;
387     }
388   }
389
390   if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
391       *userhost == '#' || *userhost == '&' || *userhost == '+'
392 #ifndef NO_OLD_GLINE
393       || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
394 #endif /* NO_OLD_GLINE */
395       )
396     return 0;
397
398   DupString(t_uh, userhost);
399   canon_userhost(t_uh, &user, &host, 0);
400
401   for (gline = GlobalGlineList; gline; gline = sgline) {
402     sgline = gline->gl_next;
403
404     if (gline->gl_expire <= CurrentTime)
405       gline_free(gline);
406     else if (flags & GLINE_EXACT) {
407       if (ircd_strcmp(gline->gl_host, host) == 0 &&
408           ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
409            ircd_strcmp(gline->gl_user, user) == 0))
410         break;
411     } else {
412       if (match(gline->gl_host, host) == 0 &&
413           ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
414            match(gline->gl_user, user) == 0))
415       break;
416     }
417   }
418
419   MyFree(t_uh);
420
421   return gline;
422 }
423
424 struct Gline *
425 gline_lookup(struct Client *cptr)
426 {
427   struct Gline *gline;
428   struct Gline *sgline;
429
430   for (gline = GlobalGlineList; gline; gline = sgline) {
431     sgline = gline->gl_next;
432
433     if (gline->gl_expire <= CurrentTime)
434       gline_free(gline);
435     else if ((GlineIsIpMask(gline) ?
436               match(gline->gl_host, ircd_ntoa((const char *)&cptr->ip)) :
437               match(gline->gl_host, cptr->user->host)) == 0 &&
438              match(gline->gl_user, cptr->user->username) == 0)
439       return gline;
440   }
441
442   return 0;
443 }
444
445 void
446 gline_free(struct Gline *gline)
447 {
448   assert(0 != gline);
449
450   *gline->gl_prev_p = gline->gl_next; /* squeeze this gline out */
451   if (gline->gl_next)
452     gline->gl_next->gl_prev_p = gline->gl_prev_p;
453
454   MyFree(gline->gl_user); /* free up the memory */
455   if (gline->gl_host)
456     MyFree(gline->gl_host);
457   MyFree(gline->gl_reason);
458   MyFree(gline);
459 }
460
461 void
462 gline_burst(struct Client *cptr)
463 {
464   struct Gline *gline;
465   struct Gline *sgline;
466
467   for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
468     sgline = gline->gl_next;
469
470     if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
471       gline_free(gline);
472     else if (!GlineIsLocal(gline) && gline->gl_lastmod)
473       sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s@%s %Tu %Tu :%s",
474                     GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
475                     gline->gl_host, gline->gl_expire - CurrentTime,
476                     gline->gl_lastmod, gline->gl_reason);
477   }
478
479   for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
480     sgline = gline->gl_next;
481
482     if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
483       gline_free(gline);
484     else if (!GlineIsLocal(gline) && gline->gl_lastmod)
485       sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu :%s",
486                     GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
487                     gline->gl_expire - CurrentTime, gline->gl_lastmod,
488                     gline->gl_reason);
489   }
490 }
491
492 int
493 gline_resend(struct Client *cptr, struct Gline *gline)
494 {
495   if (GlineIsLocal(gline) || !gline->gl_lastmod)
496     return 0;
497
498   sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
499                 GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
500                 GlineIsBadChan(gline) ? "" : "@",
501                 GlineIsBadChan(gline) ? "" : gline->gl_host,
502                 gline->gl_expire - CurrentTime, gline->gl_lastmod,
503                 gline->gl_reason);
504
505   return 0;
506 }
507
508 int
509 gline_list(struct Client *sptr, char *userhost)
510 {
511   struct Gline *gline;
512   struct Gline *sgline;
513
514   if (userhost) {
515     if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
516       return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
517
518     /* send gline information along */
519     send_reply(sptr, RPL_GLIST, gline->gl_user,
520                GlineIsBadChan(gline) ? "" : "@",
521                GlineIsBadChan(gline) ? "" : gline->gl_host,
522                gline->gl_expire + TSoffset,
523                GlineIsLocal(gline) ? me.name : "*",
524                GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
525   } else {
526     for (gline = GlobalGlineList; gline; gline = sgline) {
527       sgline = gline->gl_next;
528
529       if (gline->gl_expire <= CurrentTime)
530         gline_free(gline);
531       else
532         send_reply(sptr, RPL_GLIST, gline->gl_user, "@", gline->gl_host,
533                    gline->gl_expire + TSoffset,
534                    GlineIsLocal(gline) ? me.name : "*",
535                    GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
536     }
537
538     for (gline = BadChanGlineList; gline; gline = sgline) {
539       sgline = gline->gl_next;
540
541       if (gline->gl_expire <= CurrentTime)
542         gline_free(gline);
543       else
544         send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
545                    gline->gl_expire + TSoffset,
546                    GlineIsLocal(gline) ? me.name : "*",
547                    GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
548     }
549   }
550
551   /* end of gline information */
552   return send_reply(sptr, RPL_ENDOFGLIST);
553 }
554
555 void
556 gline_stats(struct Client *sptr)
557 {
558   struct Gline *gline;
559   struct Gline *sgline;
560
561   for (gline = GlobalGlineList; gline; gline = sgline) {
562     sgline = gline->gl_next;
563
564     if (gline->gl_expire <= CurrentTime)
565       gline_free(gline);
566     else
567       send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user, gline->gl_host,
568                  gline->gl_expire + TSoffset, gline->gl_reason);
569   }
570 }