|
@@ -1606,8 +1606,6 @@ def firewall_zones(request):
|
|
}
|
|
}
|
|
return HttpResponse(template.render(context, request))
|
|
return HttpResponse(template.render(context, request))
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
@is_authenticated
|
|
@is_authenticated
|
|
def firewall_zones_add(request):
|
|
def firewall_zones_add(request):
|
|
msg = vmsg.msg()
|
|
msg = vmsg.msg()
|
|
@@ -1652,7 +1650,7 @@ def firewall_zones_add(request):
|
|
valid = True
|
|
valid = True
|
|
msg.add_success("Local-zone defined")
|
|
msg.add_success("Local-zone defined")
|
|
else:
|
|
else:
|
|
- msg.add_success("Local-zone failed to set")
|
|
|
|
|
|
+ msg.add_error("Local-zone failed to set")
|
|
else:
|
|
else:
|
|
# add all interfaces
|
|
# add all interfaces
|
|
interfaces_form = []
|
|
interfaces_form = []
|
|
@@ -1668,7 +1666,7 @@ def firewall_zones_add(request):
|
|
valid = True
|
|
valid = True
|
|
msg.add_success("Interface added to zone: " + iface_form)
|
|
msg.add_success("Interface added to zone: " + iface_form)
|
|
else:
|
|
else:
|
|
- msg.add_success("Interface not added to zone: " + iface_form + " - " + v.reason)
|
|
|
|
|
|
+ msg.add_error("Interface not added to zone: " + iface_form + " - " + v.reason)
|
|
|
|
|
|
if valid == True:
|
|
if valid == True:
|
|
# if editing remove localzone if set
|
|
# if editing remove localzone if set
|
|
@@ -1685,18 +1683,18 @@ def firewall_zones_add(request):
|
|
valid = True
|
|
valid = True
|
|
msg.add_success("Description defined")
|
|
msg.add_success("Description defined")
|
|
else:
|
|
else:
|
|
- msg.add_success("Description failed to set")
|
|
|
|
|
|
+ msg.add_error("Description failed to set")
|
|
|
|
|
|
if request.POST.get('action', None) != None:
|
|
if request.POST.get('action', None) != None:
|
|
zoneaction = request.POST.get('action')
|
|
zoneaction = request.POST.get('action')
|
|
- zoneaction = zonedescription.strip()
|
|
|
|
|
|
+ zoneaction = zoneaction.strip()
|
|
if zoneaction in ['drop', 'reject']:
|
|
if zoneaction in ['drop', 'reject']:
|
|
v = vapi.set_firewall_zone_defaultaction(hostname_default, zonename, zoneaction)
|
|
v = vapi.set_firewall_zone_defaultaction(hostname_default, zonename, zoneaction)
|
|
if v.success:
|
|
if v.success:
|
|
valid = True
|
|
valid = True
|
|
msg.add_success("Default action defined")
|
|
msg.add_success("Default action defined")
|
|
else:
|
|
else:
|
|
- msg.add_success("Default action failed to set")
|
|
|
|
|
|
+ msg.add_error("Default action failed to set")
|
|
|
|
|
|
|
|
|
|
msg.add_success("Zone added")
|
|
msg.add_success("Zone added")
|
|
@@ -1721,16 +1719,15 @@ def firewall_zones_add(request):
|
|
}
|
|
}
|
|
return HttpResponse(template.render(context, request))
|
|
return HttpResponse(template.render(context, request))
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
@is_authenticated
|
|
@is_authenticated
|
|
def firewall_zones_edit(request, zonename):
|
|
def firewall_zones_edit(request, zonename):
|
|
- msg = vmsg.msg()
|
|
|
|
|
|
+ # validation
|
|
|
|
+ zonename = zonename.strip()
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|
|
|
|
+ msg = vmsg.msg()
|
|
|
|
+
|
|
# basic methods all views should call
|
|
# basic methods all views should call
|
|
all_instances = vyos.instance_getall()
|
|
all_instances = vyos.instance_getall()
|
|
hostname_default = vyos.get_hostname_prefered(request)
|
|
hostname_default = vyos.get_hostname_prefered(request)
|
|
@@ -1743,6 +1740,30 @@ def firewall_zones_edit(request, zonename):
|
|
get_firewall_zone = vapi.get_firewall_zone(hostname_default, zonename)
|
|
get_firewall_zone = vapi.get_firewall_zone(hostname_default, zonename)
|
|
zoneinfo = get_firewall_zone.data
|
|
zoneinfo = get_firewall_zone.data
|
|
|
|
|
|
|
|
+ form_changed = False
|
|
|
|
+ if request.POST.get('form_changed', None) == "1":
|
|
|
|
+ form_changed = True
|
|
|
|
+
|
|
|
|
+ # set interface_alias in format eth0 if has not vif and eth0.vlan if has vlan
|
|
|
|
+ for iname in interfaces_all_names:
|
|
|
|
+ if 'vif' in iname:
|
|
|
|
+ iname['interface_alias'] = "{interface_name}.{vif}".format(interface_name=iname['interface_name'], vif=iname['vif'])
|
|
|
|
+ else:
|
|
|
|
+ iname['interface_alias'] = iname['interface_name']
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ # create a dict
|
|
|
|
+ interfaces_all_names_dict = {}
|
|
|
|
+ for iname in interfaces_all_names:
|
|
|
|
+ if 'vif' in iname:
|
|
|
|
+ ialias = "{interface_name}.{vif}".format(interface_name=iname['interface_name'], vif=iname['vif'])
|
|
|
|
+ else:
|
|
|
|
+ ialias = iname['interface_name']
|
|
|
|
+
|
|
|
|
+ interfaces_all_names_dict[ialias] = iname
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
if zoneinfo == None:
|
|
if zoneinfo == None:
|
|
msg.add_error("Zone not exists")
|
|
msg.add_error("Zone not exists")
|
|
template = loader.get_template('firewall/zones-edit.html')
|
|
template = loader.get_template('firewall/zones-edit.html')
|
|
@@ -1757,112 +1778,167 @@ def firewall_zones_edit(request, zonename):
|
|
'interfaces_all_names': interfaces_all_names,
|
|
'interfaces_all_names': interfaces_all_names,
|
|
'msg' : msg.get_all(),
|
|
'msg' : msg.get_all(),
|
|
"zoneinfo": zoneinfo,
|
|
"zoneinfo": zoneinfo,
|
|
|
|
+ "zonename": zonename,
|
|
"exists": False
|
|
"exists": False
|
|
}
|
|
}
|
|
return HttpResponse(template.render(context, request))
|
|
return HttpResponse(template.render(context, request))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- interfaces_defined = []
|
|
|
|
- interfaces_defined_form = []
|
|
|
|
- interfaces_zone = {}
|
|
|
|
- allzones = []
|
|
|
|
|
|
+ interfaces_defined = []
|
|
|
|
+ interfaces_defined_form = []
|
|
|
|
+ allzones = []
|
|
|
|
+
|
|
|
|
+ interfaces_zone_alias = []
|
|
|
|
+ interfaces_zone_alias_other = []
|
|
|
|
+ interfaces_zone = []
|
|
|
|
+ interfaces_zone_other = []
|
|
|
|
|
|
if get_firewall_zones.success:
|
|
if get_firewall_zones.success:
|
|
allzones = get_firewall_zones.data
|
|
allzones = get_firewall_zones.data
|
|
if 'zone' in allzones:
|
|
if 'zone' in allzones:
|
|
for zone in allzones['zone']:
|
|
for zone in allzones['zone']:
|
|
if 'interface' in allzones['zone'][zone]:
|
|
if 'interface' in allzones['zone'][zone]:
|
|
- for zoneinterface in allzones['zone'][zone]['interface']:
|
|
|
|
|
|
+ if isinstance(allzones['zone'][zone]['interface'], list):
|
|
|
|
+ for zoneinterface in allzones['zone'][zone]['interface']:
|
|
|
|
+ if zone == zonename:
|
|
|
|
+ #print("@@@", zone, zoneinterface)
|
|
|
|
+ interfaces_zone_alias.append("interface_" + zoneinterface)
|
|
|
|
+ interfaces_zone.append(zoneinterface)
|
|
|
|
+ else:
|
|
|
|
+ interfaces_zone_alias_other.append("interface_" + zoneinterface)
|
|
|
|
+ interfaces_zone_other.append(zoneinterface)
|
|
|
|
+
|
|
|
|
+ interfaces_defined.append(zoneinterface)
|
|
|
|
+ interfaces_defined_form.append("interface_" + zoneinterface)
|
|
|
|
+ else:
|
|
|
|
+ zoneinterface = allzones['zone'][zone]['interface']
|
|
|
|
+ if zone == zonename:
|
|
|
|
+ # print("@@@", zone, zoneinterface)
|
|
|
|
+ interfaces_zone_alias.append("interface_" + zoneinterface)
|
|
|
|
+ interfaces_zone.append(zoneinterface)
|
|
|
|
+ else:
|
|
|
|
+ interfaces_zone_alias_other.append("interface_" + zoneinterface)
|
|
|
|
+ interfaces_zone_other.append(zoneinterface)
|
|
|
|
+
|
|
|
|
+
|
|
interfaces_defined.append(zoneinterface)
|
|
interfaces_defined.append(zoneinterface)
|
|
interfaces_defined_form.append("interface_" + zoneinterface)
|
|
interfaces_defined_form.append("interface_" + zoneinterface)
|
|
- interfaces_zone[zoneinterface] = zone
|
|
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|
|
# local control vars
|
|
# local control vars
|
|
valid = False
|
|
valid = False
|
|
localzone = False
|
|
localzone = False
|
|
|
|
|
|
- if request.POST.get('name', None) != None or len(zonename) > 0:
|
|
|
|
- if len(zonename) == 0:
|
|
|
|
- zonename = request.POST.get('name')
|
|
|
|
- zonename = zonename.strip()
|
|
|
|
-
|
|
|
|
- if request.POST.get('localzone', None) != None:
|
|
|
|
- # set local-zone
|
|
|
|
- v = vapi.set_firewall_zone_localzone(hostname_default, zonename)
|
|
|
|
- if v.success:
|
|
|
|
- valid = True
|
|
|
|
- msg.add_success("Local-zone defined")
|
|
|
|
- else:
|
|
|
|
- msg.add_success("Local-zone failed to set")
|
|
|
|
- else:
|
|
|
|
- # add all interfaces
|
|
|
|
- interfaces_form = []
|
|
|
|
- for rv in request.POST:
|
|
|
|
- iface_form = None
|
|
|
|
- if rv.startswith("interface_"):
|
|
|
|
- rvprefixlen = len("interface_")
|
|
|
|
- iface_form = rv[rvprefixlen:]
|
|
|
|
- interfaces_form.append(iface_form)
|
|
|
|
|
|
|
|
- v = vapi.set_firewall_zone_interface(hostname_default, zonename, iface_form)
|
|
|
|
|
|
+ # add all interfaces
|
|
|
|
+ interfaces_form = []
|
|
|
|
+ for rv in request.POST:
|
|
|
|
+ iface_form = None
|
|
|
|
+ if rv.startswith("interface_"):
|
|
|
|
+ rvprefixlen = len("interface_")
|
|
|
|
+ iface_form = rv[rvprefixlen:]
|
|
|
|
+ interfaces_form.append(iface_form)
|
|
|
|
+
|
|
|
|
+ # each interface unset on form we need to delete from zone
|
|
|
|
+ if form_changed:
|
|
|
|
+ for iface in interfaces_all_names:
|
|
|
|
+ # interface belongs to zone currently
|
|
|
|
+ if iface["interface_alias"] in interfaces_zone:
|
|
|
|
+ # interface not marked on form
|
|
|
|
+ if iface["interface_alias"] not in interfaces_form:
|
|
|
|
+ v = vapi.delete_firewall_zone_interface(hostname_default, zonename, iface["interface_alias"])
|
|
if v.success:
|
|
if v.success:
|
|
valid = True
|
|
valid = True
|
|
- msg.add_success("Interface added to zone: " + iface_form)
|
|
|
|
|
|
+ msg.add_success("Interface {iface} removed from zone.".format(iface=iface["interface_alias"]))
|
|
|
|
+ zalias = "interface_" + iface["interface_alias"]
|
|
|
|
+ if zalias in interfaces_zone_alias:
|
|
|
|
+ interfaces_zone_alias.remove(zalias)
|
|
else:
|
|
else:
|
|
- msg.add_success("Interface not added to zone: " + iface_form + " - " + v.reason)
|
|
|
|
-
|
|
|
|
- if valid == True:
|
|
|
|
- # if editing remove localzone if set
|
|
|
|
- pass
|
|
|
|
|
|
+ msg.add_error("Interface {iface} not removed from zone: {error}".format(iface=iface["interface_alias"], error=v.reason))
|
|
|
|
|
|
|
|
|
|
- if valid == True:
|
|
|
|
- if request.POST.get('description', None) != None:
|
|
|
|
- zonedescription = request.POST.get('description')
|
|
|
|
- zonedescription = zonedescription.strip()
|
|
|
|
- if len(zonedescription) > 0:
|
|
|
|
- v = vapi.set_firewall_zone_description(hostname_default, zonename, zonedescription)
|
|
|
|
- if v.success:
|
|
|
|
- valid = True
|
|
|
|
- msg.add_success("Description defined")
|
|
|
|
- else:
|
|
|
|
- msg.add_success("Description failed to set")
|
|
|
|
|
|
+ # each interface set on form we need to add to zone
|
|
|
|
+ for iface in interfaces_form:
|
|
|
|
+ if iface in interfaces_zone:
|
|
|
|
+ msg.add_info("Zone add interface {iface} not added since already addded.".format(iface=iface))
|
|
|
|
+ elif iface in interfaces_zone_other:
|
|
|
|
+ msg.add_alert("Zone add interface {iface} not added since belongs to other zone.".format(iface=iface))
|
|
|
|
+ else:
|
|
|
|
+ v = vapi.set_firewall_zone_interface(hostname_default, zonename, iface)
|
|
|
|
+ if v.success:
|
|
|
|
+ valid = True
|
|
|
|
+ msg.add_success("Zone add interface {iface} added.".format(iface=iface))
|
|
|
|
+ zalias = "interface_" + iface
|
|
|
|
+ interfaces_zone_alias.append(zalias)
|
|
|
|
+ else:
|
|
|
|
+ msg.add_success("Zone add interface {iface} not added: {error}.".format(iface=iface, error=v.reason))
|
|
|
|
+
|
|
|
|
+ if request.POST.get('description', None) != None:
|
|
|
|
+ zonedescription = request.POST.get('description')
|
|
|
|
+ zonedescription = zonedescription.strip()
|
|
|
|
+ if zoneinfo['description'] != zonedescription:
|
|
|
|
+ if len(zonedescription) > 0:
|
|
|
|
+ v = vapi.set_firewall_zone_description(hostname_default, zonename, zonedescription)
|
|
|
|
+ if v.success:
|
|
|
|
+ valid = True
|
|
|
|
+ msg.add_success("Description defined")
|
|
|
|
+ zoneinfo['description'] = zonedescription
|
|
|
|
+ else:
|
|
|
|
+ msg.add_success("Description failed to set")
|
|
|
|
+
|
|
|
|
+ if request.POST.get('action', None) != None:
|
|
|
|
+ zoneaction = request.POST.get('action')
|
|
|
|
+ zoneaction = zoneaction.strip()
|
|
|
|
+ if zoneaction in ['drop', 'reject']:
|
|
|
|
+ if 'default-action' not in zoneinfo or zoneinfo['default-action'] != zoneaction:
|
|
|
|
+ v = vapi.set_firewall_zone_defaultaction(hostname_default, zonename, zoneaction)
|
|
|
|
+ if v.success:
|
|
|
|
+ valid = True
|
|
|
|
+ msg.add_success("Default action defined")
|
|
|
|
+ zoneinfo['default-action'] = zoneaction
|
|
|
|
+ else:
|
|
|
|
+ msg.add_success("Default action failed to set")
|
|
|
|
|
|
- if request.POST.get('action', None) != None:
|
|
|
|
- zoneaction = request.POST.get('action')
|
|
|
|
- zoneaction = zonedescription.strip()
|
|
|
|
- if zoneaction in ['drop', 'reject']:
|
|
|
|
- v = vapi.set_firewall_zone_defaultaction(hostname_default, zonename, zoneaction)
|
|
|
|
- if v.success:
|
|
|
|
- valid = True
|
|
|
|
- msg.add_success("Default action defined")
|
|
|
|
- else:
|
|
|
|
- msg.add_success("Default action failed to set")
|
|
|
|
|
|
|
|
|
|
+ """if request.POST.get('localzone', None) != None:
|
|
|
|
+ # set local-zone
|
|
|
|
+ v = vapi.set_firewall_zone_localzone(hostname_default, zonename)
|
|
|
|
+ if v.success:
|
|
|
|
+ valid = True
|
|
|
|
+ msg.add_success("Local-zone defined")
|
|
|
|
+ else:
|
|
|
|
+ msg.add_success("Local-zone failed to set")"""
|
|
|
|
|
|
- msg.add_success("Zone added")
|
|
|
|
|
|
+ zoneaction = None
|
|
|
|
+ if 'default-action' in zoneinfo:
|
|
|
|
+ zoneaction = zoneinfo['default-action']
|
|
|
|
|
|
template = loader.get_template('firewall/zones-edit.html')
|
|
template = loader.get_template('firewall/zones-edit.html')
|
|
context = {
|
|
context = {
|
|
#'interfaces': interfaces,
|
|
#'interfaces': interfaces,
|
|
- 'instances': all_instances,
|
|
|
|
- 'hostname_default': hostname_default,
|
|
|
|
- 'username': request.user,
|
|
|
|
- 'is_superuser': is_superuser,
|
|
|
|
- 'interfaces': interfaces,
|
|
|
|
- 'interfaces_pretty': pprint.pformat(interfaces, indent=4, width=120),
|
|
|
|
- 'interfaces_all_names_pretty': pprint.pformat(interfaces_all_names, indent=4, width=120),
|
|
|
|
- 'interfaces_all_names': interfaces_all_names,
|
|
|
|
- 'msg' : msg.get_all(),
|
|
|
|
- 'allzones': allzones,
|
|
|
|
- 'interfaces_defined': interfaces_defined,
|
|
|
|
- 'interfaces_defined_form': interfaces_defined_form,
|
|
|
|
- 'interfaces_zone': interfaces_zone,
|
|
|
|
- 'form_added': valid,
|
|
|
|
- "zoneinfo": zoneinfo,
|
|
|
|
- "zonename": zonename,
|
|
|
|
- "exists": True,
|
|
|
|
|
|
+ 'instances': all_instances,
|
|
|
|
+ 'hostname_default': hostname_default,
|
|
|
|
+ 'username': request.user,
|
|
|
|
+ 'is_superuser': is_superuser,
|
|
|
|
+ 'interfaces': interfaces,
|
|
|
|
+ 'interfaces_pretty': pprint.pformat(interfaces, indent=4, width=120),
|
|
|
|
+ 'interfaces_all_names_pretty': pprint.pformat(interfaces_all_names, indent=4, width=120),
|
|
|
|
+ 'interfaces_all_names': interfaces_all_names,
|
|
|
|
+ 'msg' : msg.get_all(),
|
|
|
|
+ 'allzones': allzones,
|
|
|
|
+ 'interfaces_defined': interfaces_defined,
|
|
|
|
+ 'interfaces_defined_form': interfaces_defined_form,
|
|
|
|
+ 'interfaces_zone_alias': interfaces_zone_alias,
|
|
|
|
+ 'interfaces_zone_alias_other': interfaces_zone_alias_other,
|
|
|
|
+ "zoneinfo": zoneinfo,
|
|
|
|
+ "allzones_pretty": pprint.pformat(allzones, indent=4, width=120),
|
|
|
|
+ "zonename": zonename,
|
|
|
|
+ "exists": True,
|
|
|
|
+ 'interfaces_all_names_dict': interfaces_all_names_dict,
|
|
|
|
+ 'interfaces_all_names_dict_pretty': pprint.pformat(interfaces_all_names_dict, indent=4, width=120),
|
|
|
|
+ 'zoneaction': zoneaction,
|
|
|
|
|
|
}
|
|
}
|
|
return HttpResponse(template.render(context, request))
|
|
return HttpResponse(template.render(context, request))
|