summaryrefslogtreecommitdiff
path: root/scripts/policy/vyatta-policy.pl
blob: 928c69f6af106926fca5336698d21f2b380118b2 (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
#!/usr/bin/perl
use lib "/opt/vyatta/share/perl5/";
use VyattaConfig;
use VyattaMisc;
use Getopt::Long;
$VTYSH='/usr/bin/vtysh';

GetOptions("update-access-list=s"    => \$accesslist,
           "update-aspath-list=s"    => \$aspathlist,
           "update-community-list=s" => \$communitylist,
           "check-peer-syntax=s"     => \$peer,
);

if (defined $accesslist)    { update_access_list($accesslist); }
if (defined $aspathlist)    { update_as_path($aspathlist); }
if (defined $communitylist) { update_community_list($communitylist); }
if (defined $peer)          { check_peer_syntax($peer); }

exit 0;

sub numerically { $a <=> $b; }

sub check_peer_syntax() {
  my $peer = shift;
  
  $_ = $peer;
  if (/^local$/) { exit 0; }
  if (! isIpAddress("$peer")) { exit 0; }
  exit 1;
}

sub update_community_list() {
  my $num = shift;
  my $config = new VyattaConfig;
  my @rules = ();
  my $rule;

  # remove the old rule
  system ("$VTYSH -c \"configure terminal\" -c \"no ip community-list $num\" ");

  $config->setLevel("policy community-list $num rule");
  @rules = $config->listNodes();

  foreach $rule (sort numerically @rules) {
    my $action, $regex = '';

    # set the action
    $action = $config->returnValue("$rule action");
    if (! defined $action) {
      print "You must specify an action for as-path-list $list rule $rule\n";
      exit 1;
    }

    # grab the regex
    if (defined $config->returnValue("$rule regex")) {
      $regex = $config->returnValue("$rule regex");
    }
    else {
      print "You must specify a regex for community-list $list rule $rule\n";
      exit 1;
    }

   system ("$VTYSH -c \"configure terminal\" -c \"ip community-list $num $action $regex\" ");
  }

  exit 0;
}

sub update_as_path() {
  my $word = shift;
  my $config = new VyattaConfig;
  my @rules = ();
  my $rule;

  # remove the old rule
  system ("$VTYSH -c \"configure terminal\" -c \"no ip as-path access-list $word\" ");

  $config->setLevel("policy as-path-list $word rule");
  @rules = $config->listNodes();

  foreach $rule (sort numerically @rules) {
    my $action, $regex = '';

    # set the action
    $action = $config->returnValue("$rule action");
    if (! defined $action) {
      print "You must specify an action for as-path-list $list rule $rule\n";
      exit 1;
    }

    # grab the regex
    if (defined $config->returnValue("$rule regex")) {
      $regex = $config->returnValue("$rule regex");
    }
    else {
      print "You must specify a regex for as-path-list $list rule $rule\n";
      exit 1;
    }

   system ("$VTYSH -c \"configure terminal\" -c \"ip as-path access-list $word $action $regex\" ");
  }

  exit 0;
}

sub update_access_list() {
  my $list = shift;
  my $config = new VyattaConfig;
  my @rules = ();
  my $rule;

  # remove the old rule
  system ("$VTYSH -c \"configure terminal\" -c \"no access-list $list\" ");

  $config->setLevel("policy access-list $list rule");
  @rules = $config->listNodes();

  foreach $rule (sort numerically @rules) {
    my $ip, $action, $src, $dst, $srcmsk, $dstmsk = '';

    # set the action
    $action = $config->returnValue("$rule action");
    if (! defined $action) { 
      print "You must specify an action for access-list $list rule $rule\n";
      exit 1;
    }

    # TODO: ask someone why config->exists() is returning !0?
    # set the source filter
    if (defined $config->returnValue("$rule source host")) { 
      $src = $config->returnValue("$rule source host"); 
      $src = "host " . $src;
    }
    elsif (defined $config->returnValue("$rule source network")) { 
      $src  = $config->returnValue("$rule source network"); 
      $srcmsk = $config->returnValue("$rule source inverse-mask"); 
    }
    else {
      $src = $config->returnValue("$rule source any");
      if ("$src" eq "true") { $src = "any"; }
      else {
        print "error in source section of access-list $list rule $rule\n";
        exit 1;
      }
    }

    # set the destination filter if extended list
    if ((($list >= 100) && ($list <= 199)) || (($list >= 2000) && ($list <= 2699))) {
      $ip = 'ip ';
      # TODO: ask someone why config->exists() is returning !0?
      if (defined $config->returnValue("$rule destination host")) { 
        $dst = $config->returnValue("$rule destination host"); 
        $dst = "host " . $dst;
      }
      elsif (defined $config->returnValue("$rule destination network")) {
        $dst  = $config->returnValue("$rule destination network");
        $dstmsk = $config->returnValue("$rule destination inverse-mask");
      }
      else {
        $dst = $config->returnValue("$rule destination any");
        if ("$dst" eq "true") { $dst = "any"; }
        else {
          print "error in destination section of access-list $list rule $rule\n";
          exit 1;
        }
      }
    }

   system ("$VTYSH -c \"configure terminal\" -c \"access-list $list $action $ip $src $srcmsk $dst $dstmsk\" "); 
  }

  exit 0; 
}