summaryrefslogtreecommitdiff
path: root/scripts/vyos-update-nhrp.pl
blob: 5741eeb2641df1df1bf51c79fcc6c781ae9fc124 (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
#!/usr/bin/perl
#
# Module: vyos-update-nhrp.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 Vyos maintainers.
# All Rights Reserved.
#
# Author: Kim Hagen
# Date: August 2014
# Description: Script to configure nhrp
#
# **** End License ****
#

use Getopt::Long;
use POSIX;
use File::Basename;
use File::Compare;
use NetAddr::IP;

use lib "/opt/vyatta/share/perl5/";
use Vyatta::Config;
use Vyatta::Interface;

use strict;
use warnings;

my $conffile = '/etc/opennhrp/opennhrp.conf';
my $ipsecfile = '/etc/opennhrp/opennhrp.ipsec';

sub usage {
	print <<EOF;
Usage:
	$0 --set_nhrp --set_ipsec --get_esp_gr_names --get_ike_gr_names
EOF
	exit 1;
}

sub get_esp_groups {
	my $group_names = "";
	my $esp_groups = new Vyatta::Config;

	$esp_groups->setLevel("vpn ipsec esp-group");

	my @esp_groups = $esp_groups->listNodes();

	foreach my $group(@esp_groups) {
		$group_names = $group_names . " " . $group;
	}
	return $group_names;
}

sub get_ike_groups {
	my $group_names = "";
	my $ike_groups = new Vyatta::Config;

	$ike_groups->setLevel("vpn ipsec ike-group");

	my @ike_groups = $ike_groups->listNodes();

	foreach my $group(@ike_groups) {
		$group_names = $group_names . " " . $group;
	}
	return $group_names;
}

sub configure_nhrp_tunnels {
	my $cmd;
	my $config_tun = new Vyatta::Config;
	my $config_nhrp_tun = new Vyatta::Config;

	$config_tun->setLevel("interfaces tunnel");
	$config_nhrp_tun->setLevel("protocols nhrp tunnel");

	my @tunnels = $config_tun->listNodes();
	my @nhrp_tunnels = $config_nhrp_tun->listNodes();
	my $notun = 0;

	open (my $fh,">$conffile");
	print $fh "";
	close $fh;

	if (@nhrp_tunnels) {
		foreach my $nhrp_tunnel(@nhrp_tunnels) {
			if ($nhrp_tunnel ~~ @tunnels) {
				if ($config_tun->returnValue("$nhrp_tunnel encapsulation") eq "gre-multipoint") {
					my @conf_file = tunnel_config($nhrp_tunnel);
					open (my $fh,">>$conffile");
					foreach (@conf_file) {
		       				print $fh "$_";
	       				}
					close $fh;
					$notun = 1;
				}
				else {
					print ("Tunnel $nhrp_tunnel is not configured for 'gre-multipoint'\n");
					exit 1;
				}
			}
			else {
				print ("Tunnel $nhrp_tunnel does not exist.\n");
				exit 1;
			}
		}
	}
	if ("$notun" eq "1" ) {
		$cmd = "/etc/init.d/opennhrp.init restart";
		system ($cmd);
	}
	else {
		$cmd = "/etc/init.d/opennhrp.init stop";
		system ($cmd);
	}
}

sub configure_nhrp_ipsec {
	my $cmd;
	my $config_nhrp_tun = new Vyatta::Config;
	my $config_ipsec = new Vyatta::Config;

	$config_nhrp_tun->setLevel("protocols nhrp tunnel");
	$config_ipsec->setLevel("vpn ipsec profile");

	my @nhrp_tunnels = $config_nhrp_tun->listNodes();
	my @ipsec_profiles = $config_ipsec->listNodes();
	my $isipsec = 0;
	
	open(my $fh,">$ipsecfile");
	print $fh "";
	close $fh;

	if (@ipsec_profiles) {
		foreach my $ipsec_profile(@ipsec_profiles) {
			my $config_ipsec_profile_tun = new Vyatta::Config;

			$config_ipsec_profile_tun->setLevel("vpn ipsec profile $ipsec_profile bind tunnel");

			my @ipsec_profile_tunnels = $config_ipsec_profile_tun->listNodes();

			foreach my $ipsec_profile_tunnel(@ipsec_profile_tunnels) {
				if ($ipsec_profile_tunnel ~~ @nhrp_tunnels) {
					my @conf_file = ipsec_config($ipsec_profile, $ipsec_profile_tunnel);
					open (my $fh,">>$ipsecfile");
					foreach (@conf_file) {
						print $fh "$_";
					}
					close $fh;
					$isipsec = 1;
				}
			}
		}
	}
	if ("$isipsec" eq "1" ) {
		$cmd = "/etc/init.d/opennhrp.init restart";
		system ($cmd);
	}
}

sub tunnel_config {
	my $tunnel_Config = new Vyatta::Config;
	my $tunnel_ID = $_[0];

	$tunnel_Config->setLevel("protocols nhrp tunnel");

	my @conf_file = ();
	my $type = "#hub";

	push(@conf_file, "interface $tunnel_ID $type\n");
	if ( $tunnel_Config->exists("$tunnel_ID map")) {
		$type = "#spoke";
		my @maps = $tunnel_Config->listNodes("$tunnel_ID map");
		my $map = $maps[0];
		push(@conf_file, " map", " $map");
		push(@conf_file, " ", $tunnel_Config->returnValue("$tunnel_ID map $map nbma-address"));
		shift(@conf_file);
		unshift(@conf_file, "interface $tunnel_ID $type\n");

		if ($tunnel_Config->exists("$tunnel_ID map $map register")) {
			push(@conf_file, " register");
		}
		if ($tunnel_Config->exists("$tunnel_ID map $map cisco")) {
			push(@conf_file, " cisco");
		}
		push(@conf_file, "\n");
	}
	if ($tunnel_Config->returnValue("$tunnel_ID cisco-authentication") ne "") {
		push(@conf_file, " cisco-authentication ", $tunnel_Config->returnValue("$tunnel_ID cisco-authentication") , "\n"); 
	}
	push(@conf_file, " holding-time", " ", $tunnel_Config->returnValue("$tunnel_ID holding-time") , "\n");
	if ($tunnel_Config->exists("$tunnel_ID shortcut")) {
		push(@conf_file, " shortcut\n");
	}
	if ($tunnel_Config->exists("$tunnel_ID non-caching")) {
		push(@conf_file, " non-caching\n");
	}
	if ($tunnel_Config->exists("$tunnel_ID multicast")) {
		push(@conf_file, " multicast ", $tunnel_Config->returnValue("$tunnel_ID multicast") ,"\n");
	}	
	if ($tunnel_Config->exists("$tunnel_ID redirect")) {
		push(@conf_file, " redirect\n");
	}		
	push(@conf_file, "\n");

	return @conf_file;
}

sub ipsec_config {
	my $new_rule = "";
	my ($ipsec_profile, $ipsec_tun) = @_;
	my $config_prot = new Vyatta::Config;
	my $config_ipsec = new Vyatta::Config;

	$config_ipsec->setLevel("vpn ipsec profile $ipsec_profile");

	my $config_tun = new Vyatta::Config;	

	$config_tun->setLevel("interfaces tunnel $ipsec_tun");

	my @conf_file = ();
	my ($esp_group, $ike_group, @tun_ip) = undef;

	$esp_group = $config_ipsec->returnValue("esp-group");
	$ike_group = $config_ipsec->returnValue("ike-group");	
	@tun_ip = $config_tun->returnValues('address');
	
	if (@tun_ip) {
		for my $ip (@tun_ip) {
			push(@conf_file, "interface $ipsec_tun\n");
			push(@conf_file, "$ip\n");
			if ($esp_group) {
				$config_prot->setLevel("vpn ipsec esp-group $esp_group");

				my @proposals = $config_prot->listNodes("proposal");
				my %proposals = @proposals;
				my $x = scalar(keys %proposals);
				my $y = 0;
				push(@conf_file, " --esp", " ");
				foreach my $proposal (@proposals) {
					if ($y != 0 && $y <= $x ) {
						push(@conf_file, ",");
					}
					if ($config_prot->exists("proposal $proposal encryption")) {
						push(@conf_file, $config_prot->returnValue("proposal $proposal encryption"));
					}
					else {
						push(@conf_file, "aes128");
					}
					if ($config_prot->exists("proposal $proposal hash")) {
						push(@conf_file, "-", $config_prot->returnValue("proposal $proposal hash"));
					}
					else {
						push(@conf_file, "-sha1");
					}
					if ($config_prot->exists("pfs")) {
						my $pfs = $config_prot->returnValue("pfs");
						if ($pfs eq 'dh-group2') {
							push(@conf_file, "-modp1024");
						}
						elsif ($pfs eq 'dh-group5') {
							push(@conf_file, "-modp1536");
						}
						elsif ($pfs eq 'dh-group14') {
							push(@conf_file, "-modp2048");
						}
						elsif ($pfs eq 'dh-group15') {
							push(@conf_file, "-modp3072");
						}
						elsif ($pfs eq 'dh-group16') {
							push(@conf_file, "-modp4096");
						}
						elsif ($pfs eq 'dh-group17') {
							push(@conf_file, "-modp6144");
						}
						elsif ($pfs eq 'dh-group18') {
							push(@conf_file, "-modp8192");
						}
						elsif ($pfs eq 'dh-group19') {
							push(@conf_file, "-ecp256");
						}
						elsif ($pfs eq 'dh-group20') {
							push(@conf_file, "-ecp384");
						}
						elsif ($pfs eq 'dh-group21') {
							push(@conf_file, "-ecp521");
						}
						elsif ($pfs eq 'dh-group22') {
							push(@conf_file, "-modp1024s160");
						}
						elsif ($pfs eq 'dh-group23') {
							push(@conf_file, "-modp2048s224");
						}
						elsif ($pfs eq 'dh-group24') {
							push(@conf_file, "-modp2048s256");
						}
						elsif ($pfs eq 'dh-group25') {
							push(@conf_file, "-ecp192");
						}
						elsif ($pfs eq 'dh-group26') {
							push(@conf_file, "-ecp224");
						}
					}
					++$y;
				}
				push(@conf_file, "\n");
			}
		
			if ($ike_group) {
				$config_prot->setLevel("vpn ipsec ike-group $ike_group");
				my @proposals = $config_prot->listNodes("proposal");
				my %proposals = @proposals;
				my $x = scalar(keys %proposals);
				my $y = 0;
				push(@conf_file, " --ike", " ");
				foreach my $proposal (@proposals) {
					if ($y != 0 && $y <= $x ) {
						push(@conf_file, ",");
					}
					if ($config_prot->exists("proposal $proposal encryption")) {
						push(@conf_file, $config_prot->returnValue("proposal $proposal encryption"));
					}
					else {
						push(@conf_file, "aes128");
					}
					if ($config_prot->exists("proposal $proposal hash")) {
						push(@conf_file, "-", $config_prot->returnValue("proposal $proposal hash"));
					}
					else {
						push(@conf_file, "-sha1");
					}
					if ($config_prot->exists("proposal $proposal dh-group")) {
						my $pfs = $config_prot->returnValue("proposal $proposal dh-group");
						if ($pfs eq '2') {
							push(@conf_file, "-modp1024");
						}
						elsif ($pfs eq '5') {
							push(@conf_file, "-modp1536");
						}
						elsif ($pfs eq '14') {
							push(@conf_file, "-modp2048");
						}
						elsif ($pfs eq '15') {
							push(@conf_file, "-modp3072");
						}
						elsif ($pfs eq '16') {
							push(@conf_file, "-modp4096");
						}
						elsif ($pfs eq '17') {
							push(@conf_file, "-modp6144");
						}
						elsif ($pfs eq '18') {
							push(@conf_file, "-modp8192");
						}
						elsif ($pfs eq '19') {
							push(@conf_file, "-ecp256");
						}
						elsif ($pfs eq '20') {
							push(@conf_file, "-ecp384");
						}
						elsif ($pfs eq '21') {
							push(@conf_file, "-ecp521");
						}
						elsif ($pfs eq '22') {
							push(@conf_file, "-modp1024s160");
						}
						elsif ($pfs eq '23') {
							push(@conf_file, "-modp2048s224");
						}
						elsif ($pfs eq '24') {
							push(@conf_file, "-modp2048s256");
						}
						elsif ($pfs eq '25') {
							push(@conf_file, "-ecp192");
						}
						elsif ($pfs eq '26') {
							push(@conf_file, "-ecp224");
						}
					}
					++$y;
				}
				push(@conf_file, "\n");
			}
       	}
   	}
	push(@conf_file, "\n");

	return @conf_file;
}

#
# main
#

my ($set_nhrp, $set_ipsec, $get_esp_gr_names, $get_ike_gr_names);

GetOptions (
	"set_ipsec"			=> \$set_ipsec,
	"set_nhrp"			=> \$set_nhrp,
	"get_esp_gr_names"	=> \$get_esp_gr_names,
	"get_ike_gr_names"	=> \$get_ike_gr_names,
) or usage ();

my $rc = 1;
$rc = print get_esp_groups() if $get_esp_gr_names;
$rc = print get_ike_groups() if $get_ike_gr_names;
$rc = configure_nhrp_ipsec() if $set_ipsec;
$rc = configure_nhrp_tunnels() if $set_nhrp;

exit $rc;

# end of file