1 # -*- coding: utf-8 -*- vim:encoding=utf-8:
2 # vim: tabstop=4:shiftwidth=4:softtabstop=4:expandtab
4 from django.db import models
5 from django.conf import settings
6 from django.contrib.auth.models import User
7 from django.utils.translation import ugettext_lazy as _
8 from utils import proxy as PR
12 from time import sleep
15 from flowspy.utils.randomizer import id_generator as id_gen
17 from flowspec.tasks import *
19 FORMAT = '%(asctime)s %(levelname)s: %(message)s'
20 logging.basicConfig(format=FORMAT)
21 logger = logging.getLogger(__name__)
22 logger.setLevel(logging.DEBUG)
26 ("dont-fragment", "Don't fragment"),
27 ("first-fragment", "First fragment"),
28 ("is-fragment", "Is fragment"),
29 ("last-fragment", "Last fragment"),
30 ("not-a-fragment", "Not a fragment")
35 ("discard", "Discard"),
36 ("community", "Community"),
37 ("next-term", "Next term"),
38 ("routing-instance", "Routing Instance"),
39 ("rate-limit", "Rate limit"),
63 ("EXPIRED", "EXPIRED"),
64 ("PENDING", "PENDING"),
65 ("OUTOFSYNC", "OUTOFSYNC"),
66 ("INACTIVE", "INACTIVE"),
67 ("ADMININACTIVE", "ADMININACTIVE"),
71 def days_offset(): return datetime.date.today() + datetime.timedelta(days = settings.EXPIRATION_DAYS_OFFSET)
73 class MatchPort(models.Model):
74 port = models.CharField(max_length=24, unique=True)
75 def __unicode__(self):
78 db_table = u'match_port'
80 class MatchDscp(models.Model):
81 dscp = models.CharField(max_length=24)
82 def __unicode__(self):
85 db_table = u'match_dscp'
87 class MatchProtocol(models.Model):
88 protocol = models.CharField(max_length=24, unique=True)
89 def __unicode__(self):
92 db_table = u'match_protocol'
95 class ThenAction(models.Model):
96 action = models.CharField(max_length=60, choices=THEN_CHOICES, verbose_name="Action")
97 action_value = models.CharField(max_length=255, blank=True, null=True, verbose_name="Action Value")
98 def __unicode__(self):
99 ret = "%s:%s" %(self.action, self.action_value)
100 return ret.rstrip(":")
102 db_table = u'then_action'
103 ordering = ['action', 'action_value']
104 unique_together = ("action", "action_value")
106 class Route(models.Model):
107 name = models.SlugField(max_length=128, verbose_name=_("Name"))
108 applier = models.ForeignKey(User, blank=True, null=True)
109 source = models.CharField(max_length=32, help_text=_("Network address. Use address/CIDR notation"), verbose_name=_("Source Address"))
110 sourceport = models.ManyToManyField(MatchPort, blank=True, null=True, related_name="matchSourcePort", verbose_name=_("Source Port"))
111 destination = models.CharField(max_length=32, help_text=_("Network address. Use address/CIDR notation"), verbose_name=_("Destination Address"))
112 destinationport = models.ManyToManyField(MatchPort, blank=True, null=True, related_name="matchDestinationPort", verbose_name=_("Destination Port"))
113 port = models.ManyToManyField(MatchPort, blank=True, null=True, related_name="matchPort", verbose_name=_("Port"))
114 dscp = models.ManyToManyField(MatchDscp, blank=True, null=True, verbose_name="DSCP")
115 fragmenttype = models.CharField(max_length=20, choices=FRAGMENT_CODES, blank=True, null=True, verbose_name="Fragment Type")
116 icmpcode = models.CharField(max_length=32, blank=True, null=True, verbose_name="ICMP Code")
117 icmptype = models.CharField(max_length=32, blank=True, null=True, verbose_name="ICMP Type")
118 packetlength = models.IntegerField(blank=True, null=True, verbose_name="Packet Length")
119 protocol = models.ManyToManyField(MatchProtocol, blank=True, null=True, verbose_name=_("Protocol"))
120 tcpflag = models.CharField(max_length=128, blank=True, null=True, verbose_name="TCP flag")
121 then = models.ManyToManyField(ThenAction, verbose_name=_("Then"))
122 filed = models.DateTimeField(auto_now_add=True)
123 last_updated = models.DateTimeField(auto_now=True)
124 status = models.CharField(max_length=20, choices=ROUTE_STATES, blank=True, null=True, verbose_name=_("Status"), default="PENDING")
125 # is_online = models.BooleanField(default=False)
126 # is_active = models.BooleanField(default=False)
127 expires = models.DateField(default=days_offset, verbose_name=_("Expires"))
128 response = models.CharField(max_length=512, blank=True, null=True, verbose_name=_("Response"))
129 comments = models.TextField(null=True, blank=True, verbose_name=_("Comments"))
132 def __unicode__(self):
137 verbose_name = "Rule"
138 verbose_name_plural = "Rules"
140 def save(self, *args, **kwargs):
143 self.name = "%s_%s" %(self.name, hash)
144 super(Route, self).save(*args, **kwargs) # Call the "real" save() method.
147 def clean(self, *args, **kwargs):
148 from django.core.exceptions import ValidationError
151 address = IPNetwork(self.destination)
152 self.destination = address.exploded
154 raise ValidationError(_('Invalid network address format at Destination Field'))
157 address = IPNetwork(self.source)
158 self.source = address.exploded
160 raise ValidationError(_('Invalid network address format at Source Field'))
162 def commit_add(self, *args, **kwargs):
163 peer = self.applier.get_profile().peer.domain_name
164 send_message("[%s] Adding rule %s. Please wait..." %(self.applier.username, self.name), peer)
165 response = add.delay(self)
166 logger.info("Got add job id: %s" %response)
168 def commit_edit(self, *args, **kwargs):
169 peer = self.applier.get_profile().peer.domain_name
170 send_message("[%s] Editing rule %s. Please wait..." %(self.applier.username, self.name), peer)
171 response = edit.delay(self)
172 logger.info("Got edit job id: %s" %response)
174 def commit_delete(self, *args, **kwargs):
177 if "reason" in kwargs:
178 reason = kwargs['reason']
179 reason_text = "Reason: %s. " %reason
180 peer = self.applier.get_profile().peer.domain_name
181 send_message("[%s] Suspending rule %s. %sPlease wait..." %(self.applier.username, self.name, reason_text), peer)
182 response = delete.delay(self, reason=reason)
183 logger.info("Got delete job id: %s" %response)
185 def has_expired(self):
186 today = datetime.date.today()
187 if today > self.expires:
191 def check_sync(self):
192 if not self.is_synced():
193 self.status = "OUTOFSYNC"
198 get_device = PR.Retriever()
199 device = get_device.fetch_device()
201 routes = device.routing_options[0].routes
202 except Exception as e:
203 self.status = "EXPIRED"
205 logger.error("No routing options on device. Exception: %s" %e)
208 if route.name == self.name:
210 logger.info('Found a matching rule name')
211 devicematch = route.match
213 assert(self.destination)
214 assert(devicematch['destination'][0])
215 if self.destination == devicematch['destination'][0]:
216 found = found and True
217 logger.info('Found a matching destination')
220 logger.info('Destination fields do not match')
225 assert(devicematch['source'][0])
226 if self.source == devicematch['source'][0]:
227 found = found and True
228 logger.info('Found a matching source')
231 logger.info('Source fields do not match')
235 assert(self.fragmenttype)
236 assert(devicematch['fragment'][0])
237 if self.fragmenttype == devicematch['fragment'][0]:
238 found = found and True
239 logger.info('Found a matching fragment type')
242 logger.info('Fragment type fields do not match')
246 assert(self.icmpcode)
247 assert(devicematch['icmp-code'][0])
248 if self.icmpcode == devicematch['icmp-code'][0]:
249 found = found and True
250 logger.info('Found a matching icmp code')
253 logger.info('Icmp code fields do not match')
257 assert(self.icmptype)
258 assert(devicematch['icmp-type'][0])
259 if self.icmptype == devicematch['icmp-type'][0]:
260 found = found and True
261 logger.info('Found a matching icmp type')
264 logger.info('Icmp type fields do not match')
267 if found and self.status != "ACTIVE":
268 logger.error('Rule is applied on device but appears as offline')
269 self.status = "ACTIVE"
272 if self.status == "ADMININACTIVE" or self.status == "INACTIVE" or self.status == "EXPIRED":
278 then_statements = self.then.all()
279 for statement in then_statements:
280 if statement.action_value:
281 ret = "%s %s:<strong>%s</strong><br/>" %(ret, statement.action, statement.action_value)
283 ret = "%s %s<br>" %(ret, statement.action)
284 return ret.rstrip(',')
286 get_then.short_description = 'Then statement'
287 get_then.allow_tags = True
292 ret = '%s Dst Addr:<strong>%s</strong> <br/>' %(ret, self.destination)
293 if self.fragmenttype:
294 ret = "%s Fragment Type:<strong>%s</strong><br/>" %(ret, self.fragmenttype)
296 ret = "%s ICMP code:<strong>%s</strong><br/>" %(ret, self.icmpcode)
298 ret = "%s ICMP Type:<strong>%s</strong><br/>" %(ret, self.icmptype)
299 if self.packetlength:
300 ret = "%s Packet Length:<strong>%s</strong><br/>" %(ret, self.packetlength)
302 ret = "%s Src Addr:<strong>%s</strong> <br/>" %(ret, self.source)
304 ret = "%s TCP flag:<strong>%s</strong><br/>" %(ret, self.tcpflag)
306 for port in self.port.all():
307 ret = ret + "Port:<strong>%s</strong> <br/>" %(port)
309 for protocol in self.protocol.all():
310 ret = ret + "Protocol:<strong>%s</strong> <br/>" %(protocol)
311 if self.destinationport:
312 for port in self.destinationport.all():
313 ret = ret + "Dst Port:<strong>%s</strong> <br/>" %(port)
315 for port in self.sourceport.all():
316 ret = ret +"Src Port:<strong>%s</strong> <br/>" %(port)
318 for dscp in self.dscp.all():
319 ret = ret + "%s Port:<strong>%s</strong> <br/>" %(ret, dscp)
320 return ret.rstrip('<br/>')
322 get_match.short_description = 'Match statement'
323 get_match.allow_tags = True
326 def applier_peer(self):
328 applier_peer = self.applier.get_profile().peer
334 def days_to_expire(self):
335 if self.status not in ['EXPIRED', 'ADMININACTIVE', 'ERROR', 'INACTIVE']:
336 expiration_days = (self.expires - datetime.date.today()).days
337 if expiration_days < settings.EXPIRATION_NOTIFY_DAYS:
338 return "%s" %expiration_days
344 def send_message(msg, user):
345 # username = user.username
347 b = beanstalkc.Connection()
348 b.use(settings.POLLS_TUBE)
349 tube_message = json.dumps({'message': str(msg), 'username':peer})