summaryrefslogtreecommitdiff
blob: 83899d9a2c91bcffb1900e46a55e77785cd03055 (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
#!/usr/bin/perl
#
#   $Id: pptp_fe.pl,v 1.1 2001/11/29 05:19:10 quozl Exp $
#
#   pptp_fe.pl, privileged portion of xpptp_fe.pl
#   Copyright (C) 2001  Smoot Carl-Mitchell (smoot@tic.com)
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   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.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

use strict;
use Getopt::Std;
use Time::localtime;
use IO::Handle;

my $Usage = "usage: pptp_fe [-c config_file] [-d] [-h] [-k] [-n network]
		[-p] [-r routes] [-t timeout] [host]
	where:
	-c - configuration file (default is ~/.pptp_fe.conf)
	-d - pppd debug flag
	-h - this help message
	-k - kill pppd daemon with route to network
	-n - network number of remote private network in x.x.x.x/n notation
	-r - routes to add to routing table separated by commas
	-p - suppress prompting
	-t - connection timeout retry interval in seconds (default 60 seconds)
	host - remote PPTP server name
";

my %Opt;
getopts("c:dhkn:pr:t:", \%Opt);

my $Config_File = $Opt{'c'};
$Config_File = "$ENV{'HOME'}/.pptp_fe.conf" unless $Opt{'c'};
my $Config;
my $Debug = $Opt{'d'};
$Debug = 0 unless $Debug;
my $Debug_Flag = "debug" if $Debug;
my $Help = $Opt{'h'};
my $Kill = $Opt{'k'};
my $Net = $Opt{'n'};
my $No_Prompt = $Opt{'p'};
my $Route = $Opt{'r'};
my $Timeout = $Opt{'t'}; $Timeout = 60 unless $Timeout;

print($Usage), exit(1) if $Help; 

my $Server = $ARGV[0];

my $State = "disconnected";

system("modprobe ppp-compress-18");

$Config = cmd_read_config_file($Config_File);
for my $cmd (@$Config) {
	cmd_set($cmd, 1);
}

print "($State) > " unless $No_Prompt;
STDOUT->flush;
for (;;) {
	my $rin = '';
	my $rout = '';
	vec($rin, fileno(STDIN), 1) = 1;
	command() if select($rout=$rin,  undef, undef, 5);

	my $interface = "";
	if ($State eq "connected" && ! ($interface = net_interface_up($Net))) {
		print "\n";
		print "interface $interface for $Net not up - restarting\n";
		cmd_connect();
		print "($State) > " unless $No_Prompt;;
	}
}

sub command {

	my $input;
	sysread(STDIN, $input, 1024);

	for my $line1 (split("\n", $input)) {
		my $line = $line1;
		$line =~ s/\s*$//;
		$line =~ s/^\s*//;
		my ($command, $arguments) = split(" ", $line, 2);

		if ($command eq "c") {
			cmd_connect();
		}
		elsif ($command eq "d") {
			cmd_disconnect();
		}
		elsif ($command eq "h") {
			cmd_help();
		}
		elsif ($command eq "l") {
			cmd_list();
		}
		elsif ($command eq "q") {
			cmd_disconnect();
			exit 0;
		}
		elsif ($command eq "r") {
			$Config = cmd_read_config_file($arguments);
		}
		elsif ($command eq "s") {
			cmd_set($arguments, 0);
		}
		elsif ($command eq "w") {
			cmd_write_config_file($arguments);
		}
		elsif ($command ne "") {
			print "unknown command\n";
		}
	}
	print "($State) > " unless $No_Prompt;
	STDOUT->flush;
}

sub cmd_connect {

	cmd_disconnect() if $State eq "connected";

	my $start_time = time();
	my $date_string = ctime($start_time);
	print "$date_string Running pptp $Server $Debug_Flag";
	system("pptp $Server $Debug_Flag");
	
	my $interface = "";
	
	do {
		sleep 1;
		$interface = net_interface_up($Net);
		print ".";
	} until ($interface || time() > $start_time + $Timeout);
	
	if (time() > $start_time + $Timeout) {
		print "timed out after $Timeout sec\n";
		$State = "disconnected";
		return 0;
	}

	print "\n";
	
	my $ifcfg = `ifconfig $interface`;
	$ifcfg =~ /P-t-P:(.*)  Mask/;
	my $ip = $1;
	print "setting route to network $Net to interface $interface\n";
	system("route add -net $Net dev $interface metric 2");
	
	# Routes are separated by commas
	my @route = split(/,/, $Route);
	for my $route (@route) {
		my $net_flag = "";
		$net_flag = "-net" if $route =~ /\//;
	
		print "setting route to $route to interface $interface\n";
		system("route add $net_flag $route dev $interface");
	}

	$State = "connected";
	print "connected\n";
	return 1;
}

sub cmd_disconnect {

	return 1 if $State eq "disconnected";

	my $interface = net_interface_up($Net);
	my $pid_file = "/var/run/$interface.pid";

	# delete the named pipes - XXX this is a bit crude
	system("rm -f /var/run/pptp/*");
	
	$State = "disconnected", return 1 unless $interface && -f $pid_file;

	my $pid = `cat $pid_file`; 
	chomp $pid;
	print "killing pppd($pid)\n";
	kill("HUP", $pid);
	print "waiting for pppd to die";
	do {
		sleep 1;
		print ".";
	}
	until (kill(0, $pid));

	print "\n";
	$State = "disconnected";
	print "disconnected\n";
	return 1;
}

sub cmd_list {

	print "Server = $Server\n";
	print "Network = $Net\n";
	print "Routes = $Route\n";
	print "Debug = $Debug_Flag\n";
	print "No_Prompt = $No_Prompt\n";
	print "Timeout = $Timeout\n";
	print "\n";
}

sub cmd_help {

	print "Commands are:\n";
	print "c - initiate PPTP connection\n";
	print "d - disconnect PPTP\n";
	print "h - this help message\n";
	print "l - list current configuration\n";
	print "q - quite the program\n";
	print "r - read configuration file\n";
	print "s - set configuration variable (l for a list)\n";
	print "w - write the configuration file\n";

}

sub cmd_set {
	my $input = shift;
	my $no_replace = shift;

	my ($variable, $value) = split(/\s*=\s*/, $input);

	$variable = "\L$variable";
	if (! $variable) {
		print "syntax: s variable = value\n";
		return 0;
	}

	if ($variable eq "server") {
		$Server = $value unless $no_replace && $Server;
	}
	elsif ($variable eq "network") {
		$Net = $value unless $no_replace && $Net;
	}
	elsif ($variable eq "routes") {
		$Route = $value unless $no_replace && $Route;
	}
	elsif ($variable eq "debug") {
		$Debug_Flag = $value unless $no_replace && $Debug_Flag;
	}
	elsif ($variable eq "no_prompt") {
		$No_Prompt = $value unless $no_replace && $No_Prompt;
	}
	elsif ($variable eq "timeout") {
		$Timeout = $value unless $no_replace && $Timeout;
	}
	elsif ($variable eq "config_file") {
		$Config_File = $value unless $no_replace && $Config_File;
	}
	else {
		print "unknown variable\n";
	}
}

sub cmd_read_config_file {
	my $file = shift;

	my $config = [];
	$file = $Config_File unless $file; 
	local *IN;
	if (!open(IN, $file)) {
		print "cannot open $file\n";
		return $config;
	}

	my @config_file = <IN>;
	close IN;
	push @config_file, "\n";
	chomp @config_file;

	for my $line (@config_file) {
		next if /\s*#/;

		if ($line =~ /\S/) {
			$line =~ s/^\s*//;
			$line =~ s/\s*$//;
			push @$config, $line;
			next;
		}
	}
	return $config;
}

sub cmd_write_config_file {
	my $file = shift;

	$file = $Config_File unless $file; 
	local *OUT;
	if (!open(OUT, ">$file")) {
		print "cannot open $file\n";
		return 0;
	}

	my $oldfh = select OUT;
	cmd_list();
	close OUT;
	select $oldfh;

	return 1;
}

sub net_interface_up {
	my $cidr = shift;

	# cidr is net/bits
	my($net, $nbits) = split(/\//, $cidr);

	# compute the network number
	my $netnum = netnum($net, $nbits);
	local(*INTERFACE);
	open(INTERFACE, "ifconfig|") || die "cannot run ifconfig - $!\n";

	my $interface = "";
	my @interface = <INTERFACE>;
	close INTERFACE;
	for  (@interface) {
		chomp;

		# new interface
		if (/^[a-zA-Z]/) {
			if ($interface =~ /(.*)      Link.*P-t-P:(.*)  Mask/) {
				my $interface_name = $1;
				my $ip = $2;
				return $interface_name
					if netnum($ip, $nbits) == $netnum;
			}
			$interface = "";
		}
		$interface .= $_;
	}
	return "";
}

sub netnum {
	my $net = shift;
	my $bits = shift;

	my @octets = split(/\./, $net);
	my $netnum = 0;
	for my $octet (@octets) {
		$netnum <<= 8;
		$netnum |= $octet;
	}

	my $mask = 0;
	for (1..$bits) {
		$mask <<= 1;
		$mask |= 1;
	}
	$mask = $mask << (32-$bits);

	$netnum &= $mask;

	return $netnum;
}