summaryrefslogtreecommitdiff
path: root/lib/Vyatta/Zone.pm
blob: a4c9d6451aa49532dc415a06979eabd4d458b45a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# Module: Zone.pm
#
# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2009 Vyatta, Inc.
# All Rights Reserved.
#
# Author: Mohit Mehta
# Date: 2009
# Description: vyatta zone management
#
# **** End License ****
#

package Vyatta::Zone;

use Vyatta::Config;
use Vyatta::Misc;
use Vyatta::Interface;
use Vyatta::IpTables::Mgr;

use strict;
use warnings;

use base 'Exporter';

# mapping from config node to iptables command
our %cmd_hash = ( 'name'        => '/sbin/iptables',
                  'ipv6-name'   => '/sbin/ip6tables');

# mapping from config node to iptables/ip6tables table
our %table_hash = ( 'name'        => 'filter',
                    'ipv6-name'   => 'filter');

# mapping from zone default action to iptables jump target
our %policy_hash = ( 'drop'    => 'DROP',
                     'reject'  => 'REJECT',
                     'accept'  => 'RETURN');

our @EXPORT_OK = qw(%cmd_hash %table_hash %policy_hash);

my %get_zone_chain_hash = (
    get_zone_chain     => \&get_zone_chain,
    get_ips_zone_chain => \&get_ips_zone_chain,
);

my $debug="false";
my $syslog="false";
my $logger = 'sudo logger -t zone.pm -p local0.warn --';

my %script_to_feature_hash = (
        'vyatta-zone.pl'        => 'ZONE-FW',
        'vyatta-zone-ips.pl'    => 'ZONE-IPS');

sub run_cmd {
    my $cmd = shift;
    my $error = system("$cmd");

    if ($syslog eq "true") {
        my $func = (caller(1))[3];
        my $feature = zone_feature((caller(1))[1]);
        system("$logger [$feature] [$func] [$cmd] = [$error]");
    }
    if ($debug eq "true") {
        my $func = (caller(1))[3];
        my $feature = zone_feature((caller(1))[1]);
        print "[$feature] [$func] [$cmd] = [$error]\n";
    }
    return $error;
}

sub zone_feature {
  my ($script) = @_;
  $script =~ s/\/opt\/vyatta\/sbin\///;
  return $script_to_feature_hash{$script};
}

sub is_fwruleset_active {
    my ($value_func, $ruleset_type, $fw_ruleset) = @_;
    my $config = new Vyatta::Config;
    return $config->$value_func("firewall $ruleset_type $fw_ruleset");
}

sub get_all_zones {
    my $value_func = shift;
    my $config = new Vyatta::Config;
    return $config->$value_func("zone-policy zone");
}

sub get_zone_interfaces {
    my ($value_func, $zone_name) = @_;
    my $config = new Vyatta::Config;
    return $config->$value_func("zone-policy zone $zone_name interface");
}

sub get_from_zones {
    my ($value_func, $zone_name) = @_;
    my $config = new Vyatta::Config;
    return $config->$value_func("zone-policy zone $zone_name from");
}

sub get_firewall_ruleset {
    my ($value_func, $zone_name, $from_zone, $firewall_type) = @_;
    my $config = new Vyatta::Config;
    return $config->$value_func("zone-policy zone $zone_name from $from_zone
        firewall $firewall_type");
}

sub is_local_zone {
    my ($value_func, $zone_name) = @_;
    my $config = new Vyatta::Config;
    return $config->$value_func("zone-policy zone $zone_name local-zone");
}

sub is_ips_enabled {
    my ($value_func, $zone_name, $from_zone, $ips_type) = @_;
    $ips_type =~ s/name/enable/;
    my $config = new Vyatta::Config;
    return $config->$value_func("zone-policy zone $zone_name from $from_zone
	content-inspection $ips_type")
}

sub get_zone_default_policy {
    my ($value_func, $zone_name) = @_;
    my $config = new Vyatta::Config;
    return $config->$value_func("zone-policy zone $zone_name default-action");
}

sub get_ips_zone_default_policy {
    my ($value_func, $zone_name) = @_;
    return 'accept';
}

sub rule_exists {
    my ($command, $table, $chain_name, $target, $interface) = @_;
    my $cmd =
        "sudo $command -t $table -L " .
        "$chain_name -v 2>/dev/null | grep \" $target \" ";
    if (defined $interface) {
      $cmd .= "| grep \" $interface \" ";
    }
    $cmd .= "| wc -l";
    my $result = `$cmd`;
    return $result;
}

sub get_zone_chain {
    my ($value_func, $zone, $localout) = @_;
    my $chain_prefix = "VZONE_$zone"; # should be same length as ips_zone_chain
    return get_zone_chain_name($value_func, $zone, $localout, $chain_prefix);
}

sub get_ips_zone_chain {
    my ($value_func, $zone, $localout) = @_;
    my $chain_prefix = "VZIPS_$zone"; # should be same length as zone_chain
    return get_zone_chain_name($value_func, $zone, $localout, $chain_prefix);
}

sub get_zone_chain_name {
    my ($value_func, $zone, $localout, $chain_prefix) = @_;
    my $chain = $chain_prefix;
    if (defined(is_local_zone($value_func, $zone))) {
      # local zone
      if (defined $localout) {
        # local zone out chain
        $chain .= "_OUT";
      } else {
        # local zone in chain
        $chain .= "_IN";
      }
    }
    return $chain;
}

sub validity_checks {
    my @all_zones = get_all_zones("listNodes");
    my @all_interfaces = ();
    my $num_local_zones = 0;
    my $returnstring;
    foreach my $zone (@all_zones) {
      # get all from zones, see if they exist in config, if not display error
      my @from_zones = get_from_zones("listNodes", $zone);
      foreach my $from_zone (@from_zones) {
        if (scalar(grep(/^$from_zone$/, @all_zones)) == 0) {
          $returnstring = "$from_zone is a from zone under zone $zone\n" . 
		"It is either not defined or deleted from config";
          return ($returnstring, );
        }
      }
      my @zone_intfs = get_zone_interfaces("returnValues", $zone);
      if (scalar(@zone_intfs) == 0) {
        # no interfaces defined for this zone
        if (!defined(is_local_zone("exists", $zone))) {
          $returnstring = "Zone $zone has no interfaces defined " .  
				"and it's not a local-zone";
          return($returnstring, );
        }
        # zone defined as a local-zone
        my @zone_intfs_orig = get_zone_interfaces("returnOrigValues", $zone);
        if (scalar(@zone_intfs_orig) != 0) {
          # can't change change transit zone to local-zone on the fly
          $returnstring = "Zone $zone is a transit zone. " .
                "Cannot convert it to local-zone.\n" .
                "Please define another zone to create local-zone";
          return($returnstring, );
        }
        $num_local_zones++;
        # make sure only one zone is a local-zone
        if ($num_local_zones > 1) {
          return ("Only one zone can be defined as a local-zone", );
        }
      } else {
        # zone has interfaces, make sure it is not set as a local-zone
        if (defined(is_local_zone("exists", $zone))) {
          $returnstring = "local-zone cannot have interfaces defined";
          return($returnstring, );
        }
        # make sure you're not converting local-zone to transit zone either
        if (defined(is_local_zone("existsOrig", $zone))) {
          $returnstring = "Cannot convert local-zone $zone to transit zone" .  
				"\nPlease define another zone for it";
          return($returnstring, );
        }
        foreach my $interface (@zone_intfs) {
          # make sure zone features are not being used on zone interface
          my $intf = new Vyatta::Interface($interface);
          if ($intf) {
            my $config = new Vyatta::Config;
            $config->setLevel($intf->path());
            # make sure firewall is not applied to this interface
            if ($config->exists("firewall in name") ||
                $config->exists("firewall out name") ||
                $config->exists("firewall local name") ||
                $config->exists("firewall in ipv6-name") ||
                $config->exists("firewall out ipv6-name") ||
                $config->exists("firewall local ipv6-name")) {
              $returnstring =
                        "interface $interface has firewall rule-set " .
                        "configured, cannot be defined under a zone";
              return($returnstring, );
            }
            # make sure content-inspection is not applied to this interface
            if ($config->exists("content-inspection in enable") ||
                $config->exists("content-inspection out enable") ||
                $config->exists("content-inspection local enable") ||
                $config->exists("content-inspection in ipv6-enable") ||
                $config->exists("content-inspection out ipv6-enable") ||
                $config->exists("content-inspection local ipv6-enable")) {
              $returnstring =
                        "interface $interface has content-inspection " .
                        "configured, cannot be defined under a zone";
              return($returnstring, );
            }
          }
          # make sure an interface is not defined under two zones
          if (scalar(grep(/^$interface$/, @all_interfaces)) > 0) {
            return ("$interface defined under two zones", );
          } else {
            push(@all_interfaces, $interface);
          }
        }
      }
    }
    return;
}

sub create_zone_chain {
    my ($feature_func, $zone_name, $localoutchain) = @_;
    my ($cmd, $error);
    my $zone_chain=$get_zone_chain_hash{$feature_func}->("exists",
                        $zone_name, $localoutchain);

    # create zone chains in filter, ip6filter tables
    foreach my $tree (keys %cmd_hash) {
     $cmd = "sudo $cmd_hash{$tree} -t $table_hash{$tree} " .
                "-L $zone_chain >&/dev/null";
     $error = run_cmd($cmd);
     if ($error) {
       # chain does not exist, go ahead create it
       $cmd = "sudo $cmd_hash{$tree} -t $table_hash{$tree} -N $zone_chain";
       $error = run_cmd($cmd);
       return "create $zone_name chain with failed [$error]" if $error;
     }
    }

    return;
}

sub delete_zone_chain {
    my ($feature_func, $zone_name, $localoutchain) = @_;
    my ($cmd, $error);
    my $zone_chain=$get_zone_chain_hash{$feature_func}->("existsOrig",
                        $zone_name, $localoutchain);
    # delete zone chains from filter, ip6filter tables
    foreach my $tree (keys %cmd_hash) {
     # flush all rules from zone chain
     $cmd = "sudo $cmd_hash{$tree} -t $table_hash{$tree} -F $zone_chain";
     $error = run_cmd($cmd);
     return "flush all rules in $zone_name chain failed [$error]" if $error;

     # delete zone chain
     $cmd = "sudo $cmd_hash{$tree} -t $table_hash{$tree} -X $zone_chain";
     $error = run_cmd($cmd);
     return "delete $zone_name chain failed [$error]" if $error;
    }
    return;
}

sub add_intf_to_zonechain {
    my ($zone_chain_func, $zone_name, $interface, $feature_chain) = @_;
    my $zone_chain=
	$get_zone_chain_hash{$zone_chain_func}->("exists", $zone_name);
    my ($cmd, $error);
    foreach my $tree (keys %cmd_hash) {

     my $result = rule_exists ($cmd_hash{$tree}, $table_hash{$tree},
                                "$zone_chain", "RETURN", $interface);
     if ($result < 1) {
      # add rule to allow same zone to same zone traffic
      $cmd = "sudo $cmd_hash{$tree} -t $table_hash{$tree} -I $zone_chain " .
        "-i $interface -j RETURN";
      $error = run_cmd($cmd);
      return "call to add $interface to its zone-chain $zone_chain
failed [$error]" if $error;
     }

     # add jump rule to zone chain for this interface before last rule
     my $rule_cnt =
	Vyatta::IpTables::Mgr::count_iptables_rules($cmd_hash{$tree},
				$table_hash{$tree}, "$feature_chain");
     my $insert_at_rule_num=1;
     if ( $rule_cnt > 1 ) {
        $insert_at_rule_num=$rule_cnt;
     }
     $result = rule_exists ($cmd_hash{$tree}, $table_hash{$tree},
                "$feature_chain", "$zone_chain", $interface);
     if ($result < 1) {
      $cmd = "sudo $cmd_hash{$tree} -t $table_hash{$tree} -I " .
	"$feature_chain $insert_at_rule_num -o $interface -j $zone_chain";
      $error = run_cmd($cmd);
      return "call to add jump rule for outgoing interface $interface
to its $zone_chain chain failed [$error]" if $error;
     }
    }

    # success
    return;
}

sub delete_intf_from_zonechain {
    my ($zone_chain_func, $zone_name, $interface, $feature_chain) = @_;
    my $zone_chain=
	$get_zone_chain_hash{$zone_chain_func}->("existsOrig", $zone_name);
    my ($cmd, $error);

    foreach my $tree (keys %cmd_hash) {

     # delete rule to jump to zone chain for this interface
     $cmd = "sudo $cmd_hash{$tree} -t $table_hash{$tree} -D $feature_chain " .
        "-o $interface -j $zone_chain";
     $error = run_cmd($cmd);
     return "call to delete jump rule for outgoing interface $interface
to $zone_chain chain failed [$error]" if $error;

     # delete rule to allow same zone to same zone traffic
     $cmd = "sudo $cmd_hash{$tree} -t $table_hash{$tree} -D $zone_chain " .
	"-i $interface -j RETURN";
     $error = run_cmd($cmd);
     return "call to delete interface $interface from zone-chain
$zone_chain with failed [$error]" if $error;
    }

    # success
    return;
}

sub add_jump_to_localin_zonechain {
    my ($zone_chain_func, $zone_name, $feature_chain) = @_;
    my ($cmd, $error);
    my $zone_chain=
        $get_zone_chain_hash{$zone_chain_func}->("exists", $zone_name);

    foreach my $tree (keys %cmd_hash) {

      my $rule_cnt =
        Vyatta::IpTables::Mgr::count_iptables_rules($cmd_hash{$tree},
                                $table_hash{$tree}, $feature_chain);
      my $insert_at_rule_num=1;
      if ( $rule_cnt > 1 ) {
        $insert_at_rule_num=$rule_cnt;
      }
      my $result = rule_exists ($cmd_hash{$tree}, $table_hash{$tree},
                                        $feature_chain, $zone_chain);

      if ($result < 1) {
        # insert rule to filter local traffic from interface per ruleset
        $cmd = "sudo $cmd_hash{$tree} -t $table_hash{$tree} -I " .
                "$feature_chain $insert_at_rule_num -j $zone_chain";
        $error = run_cmd($cmd);
        return "call to add jump rule for local zone
$zone_chain chain failed [$error]" if $error;
      }
    }

    # success
    return;
}

sub remove_jump_to_localin_zonechain {
    my ($zone_chain_func, $zone_name, $feature_chain) = @_;
    my ($cmd, $error);
    my $zone_chain=
        $get_zone_chain_hash{$zone_chain_func}->("existsOrig", $zone_name);

    foreach my $tree (keys %cmd_hash) {

     # delete rule to filter traffic destined for system
     $cmd = "sudo $cmd_hash{$tree} -t $table_hash{$tree} -D $feature_chain " .
        "-j $zone_chain";
     $error = run_cmd($cmd);
     return "call to delete local zone
$zone_chain chain failed [$error]" if $error;

    }

    # success
    return;
}

sub add_jump_to_localout_zonechain {
    my ($zone_chain_func, $zone_name, $feature_chain) = @_;
    my ($cmd, $error);

    my $zone_chain=$get_zone_chain_hash{$zone_chain_func}->("exists",
                                $zone_name, 'localout');
    # add jump to local-zone-out chain
    foreach my $tree (keys %cmd_hash) {
      # if jump to localzoneout chain not inserted, then insert rule
      my $rule_cnt =
	Vyatta::IpTables::Mgr::count_iptables_rules($cmd_hash{$tree},
				$table_hash{$tree}, $feature_chain);
      my $insert_at_rule_num=1;
      if ( $rule_cnt > 1 ) {
        $insert_at_rule_num=$rule_cnt;
      }
      my $result = rule_exists ($cmd_hash{$tree}, $table_hash{$tree},
                                        $feature_chain, $zone_chain);
      if ($result < 1) {
        my $cmd = "sudo $cmd_hash{$tree} -t $table_hash{$tree} " .
           "-I $feature_chain $insert_at_rule_num -j $zone_chain";
        $error = run_cmd($cmd);
        return "call to add jump rule for local zone out
$zone_chain chain failed [$error]" if $error;
      }
    }

    # success
    return;
}

sub remove_jump_to_localout_zonechain {
    my ($zone_chain_func, $zone_name, $feature_chain) = @_;
    my ($cmd, $error);

    my $zone_chain=
	$get_zone_chain_hash{$zone_chain_func}->("existsOrig",
					$zone_name, 'localout');

    # if only two rules then delete jump from OUTPUT chain in both
    foreach my $tree (keys %cmd_hash) {
      my $rule_cnt =
	Vyatta::IpTables::Mgr::count_iptables_rules($cmd_hash{$tree},
					$table_hash{$tree}, $zone_chain);
      if ($rule_cnt > 2) {
        # atleast one of [ip or ip6]tables has local-zone as a from zone
        return;
      }
    }

    foreach my $tree (keys %cmd_hash) {
      $cmd = "sudo $cmd_hash{$tree} -t $table_hash{$tree} " .
            "-D $feature_chain -j $zone_chain";
      $error = run_cmd($cmd);
      return "call to delete jump rule for local zone out
$zone_chain chain failed [$error]" if $error;
     }

    # success
    return;
}

1;