' if $DEBUG;
my $manager = $ui->get_permission_manager($r);
if ( $id ){
if ( $o = Ipblock->retrieve($id) ){
# Check if user can view this object
unless ( $manager && $manager->can($user, "view", $o) ){
$m->comp('/generic/error.mhtml', error=>"You don't have permission to view this object");
}
$title = $o->get_label;
}else{
$m->comp('/generic/error.mhtml', error => "Could not retrieve Ipblock id: $id");
}
# Block View has some limits
if ( $o->status->name eq 'Subnet' ){
$BLOCK_VIEW_MAX = Netdot->config->get('SUBNET_BLOCK_VIEW_MAX_PREFIX');
}elsif ( $o->status->name eq 'Container' ){
$BLOCK_VIEW_MAX = Netdot->config->get('CONTAINER_BLOCK_VIEW_MAX_PREFIX');
}
if ( $o->version == 4 ){
if ( $view_format eq 'block' ){
if ( $BLOCK_VIEW_MAX && ($o->prefix < $BLOCK_VIEW_MAX) ){
$view_format = 'list';
}
}
}elsif ( $o->version == 6 ){
$view_format = 'list' if $view_format eq 'block';
}
}else{
$title = "Search";
}
# By default, view object after any operation
# ( some exceptions will override )
my $view_object = 1;
unless ( $view_format =~ /^block|list|tree$/ ){
$m->comp('/generic/error.mhtml', error => "Invalid view format: $view_format. Perhaps wrongly configured in etc/Site.conf?");
}
%init>
%
<%perl>
#######################################################################################
# Show Root Blocks
#
#######################################################################################
if( $_action eq "SHOW_ROOTS" && !$id && $rootversion ){
unless ( $manager && $manager->can($user, "access_section", 'ip.html:show_roots') ){
$m->comp('/generic/error.mhtml', error => "You do not have permission to view this section");
}
my $get_roots = $ui->config->get('SHOW_ROOT_BLOCKS');
if ( $get_roots ){
@list = Ipblock->get_roots($rootversion);
my $max = $ui->config->get('ROOT_BLOCK_SHOW_MAX');
%perl>
Root Blocks: (<% scalar(@list) %>)
% if ( @list && scalar(@list) < $max ){
<& ipblock_list.mhtml, parent=>0, objects=>\@list, type=>"block", show_utilization=>$show_ch_util &>
% }else{
The number of root blocks exceeds the configured threshold (<% $max %>). Please use the search boxes.
% }
<%perl>
}
#######################################################################################
# Update
#
#######################################################################################
}elsif( $_action eq "UPDATE" ){
if ($submit eq "cancel") {
# Get rid of the new block
if ( $id != 0 ){
unless ( $o = Ipblock->retrieve($id) ){
$m->comp('/generic/error.mhtml', error => "Can't retrieve Ipblock id $id");
}
$o->delete;
}
# Show parent block
# reset all request variables to make this look like a view request
$view_object = 1;
$edit = 0;
$id = $parent;
if ( $id != 0 ){
unless ( $o = Ipblock->retrieve($id) ){
$m->comp('/generic/error.mhtml', error => "Can't retrieve Ipblock id $id");
}
}
} else {
my $new_digest = $o->get_digest();
eval {
Netdot::Model->do_transaction( sub{
if ( $state_digest && ($state_digest ne $new_digest) ){
$ui->throw_user("This IP changed while you were editing!");
}
$ui->form_to_db(%ARGS);
});
};
if ( my $e = $@ ){
$m->comp('/generic/error.mhtml', error=>$e);
}else{
# Do this to 'flush' the values associated with the object
# before redisplaying
$o = undef;
if ( $id ){
$o = Ipblock->retrieve($id);
}
}
}
#######################################################################################
# Update Children
#
#######################################################################################
}elsif( $_action eq "UPDATE_CHILDREN" ){
eval { $ui->form_to_db(%ARGS) };
if ( my $e = $@ ){
$m->comp('/generic/error.mhtml', error=>$e);
}else{
# Do this to 'flush' the values associated with the object
# before redisplaying
$o = undef;
if ( $id ){
$o = Ipblock->retrieve($id);
}
}
#######################################################################################
# ADD/MODIFY RANGE
#
#######################################################################################
}elsif ( $_action eq 'EDIT_RANGE'){
unless ( $manager && $manager->can($user, "edit", $o) ){
$m->comp('/generic/error.mhtml', error=>"You don't have permission to edit this object");
}
if ( $add_range_start && $add_range_end ){
if ( $range_status eq 'Delete' ){
eval {
Netdot::Model->do_transaction( sub{
$o->remove_range(start => $add_range_start,
end => $add_range_end);
});
};
}else{
eval {
Netdot::Model->do_transaction( sub{
$o->add_range(start => $add_range_start,
end => $add_range_end,
status => $range_status,
owner => $range_owner,
used_by => $range_used_by,
description => $range_description,
gen_dns => $range_gen_dns,
name_prefix => $name_prefix,
name_suffix => $name_suffix,
fzone => $range_fw_zone,
);
});
};
}
if ( my $e = $@ ){
$m->comp('/generic/error.mhtml', error=>$e);
}else{
$m->comp('.show_message', title=>"Action Message", msg=>"Address Range Modified Successfully");
$id = $o->id;
}
}else{
$m->comp('/generic/error.mhtml', error=>"You need to specify start and end addresses");
}
#######################################################################################
# ADD/MODIFY BLOCK
#
#######################################################################################
}elsif ( $_action eq "ADD_BLOCK" ){
# We need the parent to determine if there are sufficient rights to add
# a block, so if not passed, we require Admin
my $parent;
if ( $add_block_parent ){
$parent = Ipblock->retrieve($add_block_parent) ||
$m->comp('/generic/error.mhtml', error=>"Cannot retrieve IPblock id $add_block_parent");
unless ( $manager && $manager->can($user, 'edit', $parent) ){
$m->comp('/generic/error.mhtml', error=>"You don't have permission to add blocks here");
}
}else{
unless ( $manager && $manager->can($user, 'access_admin_section', 'ip.html:add_block') ){
$m->comp('/generic/error.mhtml', error=>"You don't have permission to do this");
}
}
unless ( $add_block_prefix ){
$m->comp('/generic/error.mhtml', error=>"You need to specify IP/prefix");
}
$add_block_prefix =~ s/\s+//g; # Remove spaces
if ( $submit && (($submit eq 'confirm') || ($submit eq 'Save')) ){
undef($submit);
my $nip = NetAddr::IP->new($add_block_prefix) ||
$m->comp('/generic/error.mhtml', error=>"Invalid block: $add_block_prefix");
my ($address, $prefix) = ($nip->addr, $nip->masklen);
if ( (($nip->version == 4 && $prefix != 32) ||
($nip->version == 6 && $prefix != 128)) &&
($block_status eq 'Dynamic' || $block_status eq 'Static') ){
$m->comp('/generic/error.mhtml', error=>"IP block cannot be 'Dynamic' or 'Static'. Specify IP Range instead.");
}
my $block;
# This has happened. Can't figure out why, but this should avoid errors
if ( defined $block_owner && ref($block_owner) eq 'ARRAY' ){
$block_owner = $block_owner->[0];
}
if ( defined $block_used_by && ref($block_used_by) eq 'ARRAY' ){
$block_used_by = $block_used_by->[0];
}
eval {
my %args = (status => $block_status,
owner => $block_owner,
used_by => $block_used_by,
description => $block_description,
);
if ( $block = Ipblock->search(address=>$address, prefix=>$prefix)->first ){
if ( $add_block_parent ) {
$ui->throw_user("This IP has already been set to '" . $block->status->name . "', possibly by someone else.");
}else{
$block->update(\%args);
}
}else{
$args{address} = $address;
$args{prefix} = $prefix;
$args{parent} = $add_block_parent if $add_block_parent;
$block = Ipblock->insert(\%args);
}
};
if ( my $e = $@ ){
$m->comp('/generic/error.mhtml', error=>$e);
}elsif ( $block ){
$o = $block;
$id = $block->id;
if( $o->is_address ) {
$added_ip = 1;
}else {
$added_block = 1;
}
if ( $o->version == 6 ){
$view_format = 'list';
}
}
}else{
$m->comp('/generic/confirm.html',
%ARGS,
target =>'../management/ip.html',
message =>'The IP address or block '.$add_block_prefix.' does not yet exist. '.
'Are you sure that you want to create it?',
);
}
#######################################################################################
# Add multiple blocks (from partitions in container.mhtml)
#
#######################################################################################
}elsif( $_action eq "ADD_BLOCKS" ){
unless ( $new_blocks_list && $new_blocks_status ){
$m->comp('/generic/error.mhtml', error=>"Missing list of blocks or desired status");
}
my @new_blocks = split ',', $new_blocks_list;
eval {
Netdot::Model->do_transaction( sub{
foreach my $b ( @new_blocks ){
my ($addr, $pref) = split '/', $b;
Ipblock->insert({address=>$addr, prefix=>$pref, status=>$new_blocks_status});
}
})
};
if ( my $e = $@ ){
$m->comp('/generic/error.mhtml', error=>"$e");
}else{
$m->comp('.show_message', title=>"Action Message", msg=>"IP blocks created successfully");
}
#######################################################################################
# ENABLE DHCP
#
#######################################################################################
}elsif ( $_action eq 'ENABLE_DHCP'){
unless ( $manager && $manager->can($user, 'access_admin_section', 'ip.html:enable_dhcp') ){
$m->comp('/generic/error.mhtml', error=>"You don't have permission to enable DHCP");
}
unless ( $ARGS{dhcp_global_scope} ){
$m->comp('/generic/error.mhtml', error=>"You need to specify at least the global scope");
}
my $container = delete $ARGS{dhcp_global_scope};
my %args = (container=>$container);
# Active flag
my $active = delete $ARGS{dhcp_active};
$args{active} = (defined $active && $active eq 'on')? 1 : 0;
# Grab all the dhcp options & declarations
foreach my $arg ( %ARGS ){
if ( $arg =~ /^dhcp_/ ){
my $value = $ARGS{$arg};
if ( $arg =~ /^dhcp_(.*)$/ ){
my $attr = $1;
$attr =~ s/^option_/option /;
$attr =~ s/_/-/g;
$args{attributes}{$attr} = $value;
};
}
}
eval {
$o->enable_dhcp(%args);
};
if ( my $e = $@ ){
$m->comp('/generic/error.mhtml', error=>$e);
}else{
$m->comp('.show_message', title=>"Action Message", msg=>"DHCP enabled Successfully");
}
#######################################################################################
# Delete
#
#######################################################################################
}elsif ( $_action eq "DELETE" ){
%perl>
% unless ( $manager && $manager->can($user, "delete", $o) ){
% $m->comp('/generic/error.mhtml', error=>"You don't have permission to delete this object");
% }
<%perl>
# Do not continue with object display
$view_object = 0;
#######################################################################################
# Confirm Delete
#
#######################################################################################
}elsif ( $_action eq "CONFIRM_DELETE" ){
unless ( $manager && $manager->can($user, "delete", $o) ){
$m->comp('/generic/error.mhtml', error=>"You don't have permission to delete this object");
}
my ($address, $prefix, $version) = ($o->address, $o->prefix, $o->version);
my $parentid = $o->parent->id if ( $o->parent );
# Remove from Database
#
my $rec = ($recursivedel)? 1 : 0;
eval { $o->delete( recursive => $rec ) };
if ( my $e = $@ ){
$m->comp('/generic/error.mhtml', error=>$e);
}
if ($rec){
$m->comp('.show_message', title=>"Action Message", msg=>"
Block $address/$prefix and all its children blocks have been deleted");
}else{
$m->comp('.show_message', title=>"Action Message", msg=>"
Block $address/$prefix has been deleted");
}
# Show parent block
# after block has been deleted
$id = $parentid;
if ($id != 0){
unless ( $o = Ipblock->retrieve($id) ){
$m->comp('/generic/error.mhtml', error => "Can't retrieve Ipblock id $id");
}
}
#######################################################################################
# LINK ZONES
#
#######################################################################################
}elsif ( $_action eq 'LINK_ZONES' ){
if ( scalar @link_zones && $link_zones[0] ne "" ){
foreach my $z ( @link_zones ){
eval {
SubnetZone->insert({subnet=>$o, zone=>$z});
};
if ( my $e = $@ ){
$m->comp('/generic/error.mhtml', error=>"Problem linking zones to this subnet: $e");
}else{
$m->comp('.show_message', title=>"Action Message", msg=>"
Zone(s) linked successfully");
}
}
}else{
$m->comp('/generic/error.mhtml', error=>"Please select one or more zones to link to this subnet");
}
#######################################################################################
# ADD_REV_ZONE
#
#######################################################################################
}elsif ( $_action eq 'ADD_REV_ZONE' ){
if ( @new_rev_zones && ($new_rev_zones[0] ne "") ){
eval {
Netdot::Model->do_transaction(sub{
foreach my $name ( @new_rev_zones ){
my %args = (name=>$name);
$args{template} = $template_zone_for_rev if ( $template_zone_for_rev );
my $z = Zone->insert(\%args);
}
});
};
if ( my $e = $@ ){
$m->comp('/generic/error.mhtml', error=>"Problem creating new reverse zones: $e");
}else{
$m->comp('.show_message', title=>"Action Message", msg=>"
Reverse zone(s) created successfully");
}
}else{
$m->comp('/generic/error.mhtml', error=>"Please provide a name for at least one reverse zone");
}
#######################################################################################
# ADD_A_RECORD
#
#######################################################################################
}elsif ( $_action eq 'ADD_A_RECORD' ){
unless ( $manager && $manager->can($user, "edit", $o) ){
$m->comp('/generic/error.mhtml', error=>"You don't have permission to edit this object");
}
unless ( $new_a_name && $new_a_zone ){
$m->comp('/generic/error.mhtml', error=>"Adding a DNS record requires both a name and a zone");
}
eval {
RR->insert({type=>"A", name=>$new_a_name, zone=>$new_a_zone, ipblock=>$o});
};
if ( my $e = $@ ){
$m->comp('/generic/error.mhtml', error=>$e);
}else{
$m->comp('.show_message', title=>"Action Message", msg=>"A or AAAA record added successfully");
}
#######################################################################################
# REBUILD_TREE
#
#######################################################################################
}elsif ( $_action eq 'REBUILD_TREE' ){
if ( $tree_version eq '4' || $tree_version eq 'all' ){
Ipblock->build_tree(4);
}elsif ( $tree_version eq '6' || $tree_version eq 'all' ){
Ipblock->build_tree(6);
}
$m->comp('.show_message', title=>"Action Message", msg=>"IP Tree Rebuilt Successfully");
}
#######################################################################################
# View
#
#######################################################################################
if ( $id && $view_object ){
if ( $edit ){
%perl>