summaryrefslogtreecommitdiff
path: root/scripts/firewall/vyatta-ipset.pl
blob: ef964f3d125886f03fda85581b3374f04f42cff3 (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
#!/usr/bin/perl
#
# Module: vyatta-ipset.pl
# 
# **** 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-2010 Vyatta, Inc.
# All Rights Reserved.
# 
# Author: Stig Thormodsrud
# Date: January 2009
# Description: Script to configure ipset to support firewall groups
# 
# **** End License ****
#

use Getopt::Long;
use POSIX;

use lib "/opt/vyatta/share/perl5";
use Vyatta::Config;
use Vyatta::TypeChecker;
use Vyatta::Misc;
use Vyatta::IpTables::IpSet;
use Sort::Versions;

use warnings;
use strict;


sub ipset_create {
    my ($set_name, $set_type) = @_;

    my $group = new Vyatta::IpTables::IpSet($set_name, $set_type);

    return $group->create();
}

sub ipset_delete {
    my $set_name = shift;

    my $group = new Vyatta::IpTables::IpSet($set_name);
    return $group->delete();
}

sub ipset_check_member {
    my ($set_name, $set_type, $member) = @_;

    die "undefined type or member" if ! defined $set_type or ! defined $member;

    my $group = new Vyatta::IpTables::IpSet($set_name, $set_type);
    return $group->check_member($member);
}

sub ipset_add_member {
    my ($set_name, $member, $alias, $set_type) = @_;
    my $hyphenated_port = 'false'; 
    if (($set_type eq 'port') and ($member =~ /^\D\w+-\w*/)){
      $member = "\[$member]";
      $hyphenated_port = 'true';
    }

    die "Error: undefined member" if ! defined $member; 
    my $group = new Vyatta::IpTables::IpSet($set_name);
    return $group->add_member($member, $alias, $hyphenated_port);
}

sub ipset_delete_member {
    my ($set_name, $member, $set_type) = @_;

    my $hyphenated_port = 'false'; 
    if (($set_type eq 'port') and ($member =~ /^\D\w+-\w*/)){
      $member = "\[$member]";
      $hyphenated_port = 'true';
    }

    die "Error: undefined member" if ! defined $member; 
    my $group = new Vyatta::IpTables::IpSet($set_name);
    return $group->delete_member($member, $hyphenated_port);
}

sub ipset_check_set_type {
   my ($set_name, $set_type) = @_;

   die "Error: undefined set_name\n" if ! defined $set_name; 
   die "Error: undefined set_type\n" if ! defined $set_type; 

   my $group = new Vyatta::IpTables::IpSet($set_name);
   return "Group [$set_name] has not been defined\n" if ! $group->exists();
   my $type = $group->get_type();
   $type = 'undefined' if ! defined $type;
   if ($type ne $set_type) {
       return "Error: group [$set_name] is of type [$type] not [$set_type]\n";
   }
   return;
}

sub ipset_show_members {
    my ($set_name) = @_;

    die "Error: undefined set_name\n" if ! defined $set_name; 
    my $group = new Vyatta::IpTables::IpSet($set_name);
    return "Group [$set_name] has not been defined\n" if ! $group->exists();
    my $type    = $group->get_type();
    my @members = $group->get_members();
    my $desc    = $group->get_description();
    my @fw_refs = $group->get_firewall_references();
    push @fw_refs, 'none' if scalar(@fw_refs) == 0;

    my $padding = ' ' x 13;
    print "Name       : $set_name\n";
    print "Type       : $type\n";
    print "Description: $desc\n" if defined $desc;
    print "References : ", join(', ', @fw_refs), "\n";
    print "Members    :\n";
    print $padding, join($padding, @members);
    return;
}

sub ipset_is_set_empty {
    my ($set_name) = @_;
    
    die "Error: undefined set_name\n" if ! defined $set_name; 
    my $group = new Vyatta::IpTables::IpSet($set_name);
    return "Group [$set_name] has not been defined\n" if ! $group->exists();
    my @members = $group->get_members();
    exit 0 if scalar(@members) > 0;
    exit 1;
}

sub ipset_show_sets {
    my @lines = `ipset -L`;
    my @sets = ();
    foreach my $line (@lines) {
	if ($line =~ /^Name:\s+(\S+)$/ ) {
            push @sets, $1;
	}
    }
    foreach my $set (sort { versioncmp($b, $a) } (@sets)) {
        ipset_show_members($set);
        print "\n";
    }
    return;
}

sub ipset_copy_set {
    my ($set_name, $set_type, $set_copy) = @_;

    die "Error: undefined set_name\n" if ! defined $set_name; 
    die "Error: undefined set_type\n" if ! defined $set_type; 
    die "Error: undefined set_copy\n" if ! defined $set_copy; 

    my $group = new Vyatta::IpTables::IpSet($set_name);
    my $copy  = new Vyatta::IpTables::IpSet($set_copy, $set_type);

    if ($copy->exists()) {
        return "Error: copy already exists [$set_copy]\n";
    }

    if ($group->exists()) {
        my $type = $group->get_type();
        if ($type ne $set_type) {
            return "Error: type mismatch [$type] [$set_type]\n";
        }
        # copy members to new group
        my $tmpfile = "/tmp/set.$$";
        system("ipset -S $set_name > $tmpfile");
        system("sed -i \'s/ $set_name / $set_copy /g\' $tmpfile");
        system("ipset -R < $tmpfile");
        unlink $tmpfile;
        my $copy  = new Vyatta::IpTables::IpSet($set_copy, $set_type);
        return if $copy->exists();
        return "Error: problem copying group\n";
    } else {
        my $rc = $group->create();
        return $rc;
    }
}

sub ipset_is_group_deleted {
    my ($set_name, $set_type) = @_;

    die "Error: undefined set_name\n" if ! defined $set_name; 
    die "Error: undefined set_type\n" if ! defined $set_type; 

    my $config = new Vyatta::Config;
    $config->setLevel("firewall group $set_type-group");
    my %nodes = $config->listNodeStatus();

    if ($nodes{$set_name} eq 'deleted') {
        exit 0;
    } else {
        exit 1;
    }
}

sub ipset_is_group_used {
    my ($set_name, $set_type) = @_;

    die "Error: undefined set_name\n" if ! defined $set_name; 
    die "Error: undefined set_type\n" if ! defined $set_type; 

    my $group = new Vyatta::IpTables::IpSet($set_name);
    my $refs = $group->references();
    exit 0 if $refs > 0;
    exit 1;
}

sub update_set {
  my ($set_name, $set_type) = @_;
  my $cfg = new Vyatta::Config;
  my ($rc, $newset);
  my $cpath = "firewall group $set_type-group $set_name";
  if ($cfg->existsOrig($cpath)) {
    if (!$cfg->exists($cpath)) {
      # deleted
      return $rc if (($rc = ipset_delete($set_name)));
      return;
    }
  } else {
    if ($cfg->exists($cpath)) {
      # added
      return $rc if (($rc = ipset_create($set_name, $set_type)));
      $newset = 1;
    } else {
      # doesn't exist! should not happen
      return "Updating non-existent group [$set_name]";
    }

  }
  # added or potentially changed => iterate members
  # to ensure that vyatta config and ipset stay in-sync, do the following:
  # 1. copy orig set to tmp set
  my $tmpset = "$set_name-$$";
  if (($rc = ipset_copy_set($set_name, $set_type, $tmpset))) {
    # copy failed
    if ($newset) {
      # destroy newly-created set since we're failing
      system('ipset', '--destroy', $set_name);
    }
    return $rc;
  }

  # 2. add/delete members to/from tmp set according to changes
  my @ovals = $cfg->returnOrigValues("$cpath $set_type");
  my @nvals = $cfg->returnValues("$cpath $set_type");
  my %vals = $cfg->compareValueLists(\@ovals, \@nvals);
  while (1) {
    for my $d (@{$vals{deleted}}) {
      last if (($rc = ipset_delete_member($tmpset, $d, $set_type)));
    }
    last if ($rc);
    for my $a (@{$vals{added}}) {
      last if (($rc = ipset_add_member($tmpset, $a, $set_name, $set_type)));
    }
    last;
  }

  # 3. "commit" changes and/or clean up
  if (!$rc) {
    # no error
    system('ipset', '--swap', $tmpset, $set_name);
  } elsif ($newset) {
    # destroy newly-created set since we're failing
    system('ipset', '--destroy', $set_name);
  }
  system('ipset', '--destroy', $tmpset);
  return $rc;
}

sub prune_deleted_sets {
  my $cfg = new Vyatta::Config;
  my @set_types = keys(%Vyatta::IpTables::IpSet::grouptype_hash);
  foreach my $set_type (@set_types) {
    $cfg->setLevel("firewall group $set_type-group");
    my %groups = $cfg->listNodeStatus();
    next if (scalar(keys(%groups)) < 1);
    foreach my $g (keys(%groups)) {
      next if ($groups{$g} ne 'deleted');
      next if ($cfg->isEffective($g)); # don't prune if delete failed
      my $rc;
      return $rc if (($rc = ipset_delete($g)));
    }
  }
  exit 0;
}

#
# main
#
my ($action, $set_name, $set_type, $member, $set_copy, $alias);

GetOptions("action=s"   => \$action,
           "set-name=s" => \$set_name,
           "set-type=s" => \$set_type,
           "member=s"   => \$member,
           "alias=s"    => \$alias,
           "set-copy=s" => \$set_copy,
);

die "undefined action" if ! defined $action;

my $rc;
$rc = ipset_create($set_name, $set_type) if $action eq 'create-set';

$rc = ipset_delete($set_name) if $action eq 'delete-set';

$rc = ipset_check_member($set_name, $set_type, $member) 
    if $action eq 'check-member';

$rc = ipset_add_member($set_name, $member, $alias, $set_type) if $action eq 'add-member';

$rc = ipset_delete_member($set_name, $member) if $action eq 'delete-member';

$rc = ipset_check_set_type($set_name, $set_type) if $action eq 'check-set-type';

$rc = ipset_show_members($set_name) if $action eq 'show-set-members';

$rc = ipset_show_sets() if $action eq 'show-sets';

$rc = ipset_is_set_empty($set_name) if $action eq 'is-set-empty'; 

$rc = ipset_copy_set($set_name, $set_type, $set_copy) if $action eq 'copy-set';

$rc = ipset_is_group_deleted($set_name, $set_type) 
    if $action eq 'is-group-deleted';

$rc = ipset_is_group_used($set_name, $set_type) if $action eq 'is-group-used';

$rc = update_set($set_name, $set_type) if $action eq 'update-set';
$rc = prune_deleted_sets() if $action eq 'prune-deleted-sets';

if (defined $rc) {
    print $rc;
    exit 1;
}
exit 0;

# end of file