| 1 |
#!/usr/bin/perl
|
| 2 |
#
|
| 3 |
# $Id: pptp_fe.pl,v 1.1 2001/11/29 05:19:10 quozl Exp $
|
| 4 |
#
|
| 5 |
# pptp_fe.pl, privileged portion of xpptp_fe.pl
|
| 6 |
# Copyright (C) 2001 Smoot Carl-Mitchell (smoot@tic.com)
|
| 7 |
#
|
| 8 |
# This program is free software; you can redistribute it and/or modify
|
| 9 |
# it under the terms of the GNU General Public License as published by
|
| 10 |
# the Free Software Foundation; either version 2 of the License, or
|
| 11 |
# (at your option) any later version.
|
| 12 |
#
|
| 13 |
# This program is distributed in the hope that it will be useful,
|
| 14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 16 |
# GNU General Public License for more details.
|
| 17 |
#
|
| 18 |
# You should have received a copy of the GNU General Public License
|
| 19 |
# along with this program; if not, write to the Free Software
|
| 20 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
| 21 |
#
|
| 22 |
|
| 23 |
use strict;
|
| 24 |
use Getopt::Std;
|
| 25 |
use Time::localtime;
|
| 26 |
use IO::Handle;
|
| 27 |
|
| 28 |
my $Usage = "usage: pptp_fe [-c config_file] [-d] [-h] [-k] [-n network]
|
| 29 |
[-p] [-r routes] [-t timeout] [host]
|
| 30 |
where:
|
| 31 |
-c - configuration file (default is ~/.pptp_fe.conf)
|
| 32 |
-d - pppd debug flag
|
| 33 |
-h - this help message
|
| 34 |
-k - kill pppd daemon with route to network
|
| 35 |
-n - network number of remote private network in x.x.x.x/n notation
|
| 36 |
-r - routes to add to routing table separated by commas
|
| 37 |
-p - suppress prompting
|
| 38 |
-t - connection timeout retry interval in seconds (default 60 seconds)
|
| 39 |
host - remote PPTP server name
|
| 40 |
";
|
| 41 |
|
| 42 |
my %Opt;
|
| 43 |
getopts("c:dhkn:pr:t:", \%Opt);
|
| 44 |
|
| 45 |
my $Config_File = $Opt{'c'};
|
| 46 |
$Config_File = "$ENV{'HOME'}/.pptp_fe.conf" unless $Opt{'c'};
|
| 47 |
my $Config;
|
| 48 |
my $Debug = $Opt{'d'};
|
| 49 |
$Debug = 0 unless $Debug;
|
| 50 |
my $Debug_Flag = "debug" if $Debug;
|
| 51 |
my $Help = $Opt{'h'};
|
| 52 |
my $Kill = $Opt{'k'};
|
| 53 |
my $Net = $Opt{'n'};
|
| 54 |
my $No_Prompt = $Opt{'p'};
|
| 55 |
my $Route = $Opt{'r'};
|
| 56 |
my $Timeout = $Opt{'t'}; $Timeout = 60 unless $Timeout;
|
| 57 |
|
| 58 |
print($Usage), exit(1) if $Help;
|
| 59 |
|
| 60 |
my $Server = $ARGV[0];
|
| 61 |
|
| 62 |
my $State = "disconnected";
|
| 63 |
|
| 64 |
system("modprobe ppp-compress-18");
|
| 65 |
|
| 66 |
$Config = cmd_read_config_file($Config_File);
|
| 67 |
for my $cmd (@$Config) {
|
| 68 |
cmd_set($cmd, 1);
|
| 69 |
}
|
| 70 |
|
| 71 |
print "($State) > " unless $No_Prompt;
|
| 72 |
STDOUT->flush;
|
| 73 |
for (;;) {
|
| 74 |
my $rin = '';
|
| 75 |
my $rout = '';
|
| 76 |
vec($rin, fileno(STDIN), 1) = 1;
|
| 77 |
command() if select($rout=$rin, undef, undef, 5);
|
| 78 |
|
| 79 |
my $interface = "";
|
| 80 |
if ($State eq "connected" && ! ($interface = net_interface_up($Net))) {
|
| 81 |
print "\n";
|
| 82 |
print "interface $interface for $Net not up - restarting\n";
|
| 83 |
cmd_connect();
|
| 84 |
print "($State) > " unless $No_Prompt;;
|
| 85 |
}
|
| 86 |
}
|
| 87 |
|
| 88 |
sub command {
|
| 89 |
|
| 90 |
my $input;
|
| 91 |
sysread(STDIN, $input, 1024);
|
| 92 |
|
| 93 |
for my $line1 (split("\n", $input)) {
|
| 94 |
my $line = $line1;
|
| 95 |
$line =~ s/\s*$//;
|
| 96 |
$line =~ s/^\s*//;
|
| 97 |
my ($command, $arguments) = split(" ", $line, 2);
|
| 98 |
|
| 99 |
if ($command eq "c") {
|
| 100 |
cmd_connect();
|
| 101 |
}
|
| 102 |
elsif ($command eq "d") {
|
| 103 |
cmd_disconnect();
|
| 104 |
}
|
| 105 |
elsif ($command eq "h") {
|
| 106 |
cmd_help();
|
| 107 |
}
|
| 108 |
elsif ($command eq "l") {
|
| 109 |
cmd_list();
|
| 110 |
}
|
| 111 |
elsif ($command eq "q") {
|
| 112 |
cmd_disconnect();
|
| 113 |
exit 0;
|
| 114 |
}
|
| 115 |
elsif ($command eq "r") {
|
| 116 |
$Config = cmd_read_config_file($arguments);
|
| 117 |
}
|
| 118 |
elsif ($command eq "s") {
|
| 119 |
cmd_set($arguments, 0);
|
| 120 |
}
|
| 121 |
elsif ($command eq "w") {
|
| 122 |
cmd_write_config_file($arguments);
|
| 123 |
}
|
| 124 |
elsif ($command ne "") {
|
| 125 |
print "unknown command\n";
|
| 126 |
}
|
| 127 |
}
|
| 128 |
print "($State) > " unless $No_Prompt;
|
| 129 |
STDOUT->flush;
|
| 130 |
}
|
| 131 |
|
| 132 |
sub cmd_connect {
|
| 133 |
|
| 134 |
cmd_disconnect() if $State eq "connected";
|
| 135 |
|
| 136 |
my $start_time = time();
|
| 137 |
my $date_string = ctime($start_time);
|
| 138 |
print "$date_string Running pptp $Server $Debug_Flag";
|
| 139 |
system("pptp $Server $Debug_Flag");
|
| 140 |
|
| 141 |
my $interface = "";
|
| 142 |
|
| 143 |
do {
|
| 144 |
sleep 1;
|
| 145 |
$interface = net_interface_up($Net);
|
| 146 |
print ".";
|
| 147 |
} until ($interface || time() > $start_time + $Timeout);
|
| 148 |
|
| 149 |
if (time() > $start_time + $Timeout) {
|
| 150 |
print "timed out after $Timeout sec\n";
|
| 151 |
$State = "disconnected";
|
| 152 |
return 0;
|
| 153 |
}
|
| 154 |
|
| 155 |
print "\n";
|
| 156 |
|
| 157 |
my $ifcfg = `ifconfig $interface`;
|
| 158 |
$ifcfg =~ /P-t-P:(.*) Mask/;
|
| 159 |
my $ip = $1;
|
| 160 |
print "setting route to network $Net to interface $interface\n";
|
| 161 |
system("route add -net $Net dev $interface metric 2");
|
| 162 |
|
| 163 |
# Routes are separated by commas
|
| 164 |
my @route = split(/,/, $Route);
|
| 165 |
for my $route (@route) {
|
| 166 |
my $net_flag = "";
|
| 167 |
$net_flag = "-net" if $route =~ /\//;
|
| 168 |
|
| 169 |
print "setting route to $route to interface $interface\n";
|
| 170 |
system("route add $net_flag $route dev $interface");
|
| 171 |
}
|
| 172 |
|
| 173 |
$State = "connected";
|
| 174 |
print "connected\n";
|
| 175 |
return 1;
|
| 176 |
}
|
| 177 |
|
| 178 |
sub cmd_disconnect {
|
| 179 |
|
| 180 |
return 1 if $State eq "disconnected";
|
| 181 |
|
| 182 |
my $interface = net_interface_up($Net);
|
| 183 |
my $pid_file = "/var/run/$interface.pid";
|
| 184 |
|
| 185 |
# delete the named pipes - XXX this is a bit crude
|
| 186 |
system("rm -f /var/run/pptp/*");
|
| 187 |
|
| 188 |
$State = "disconnected", return 1 unless $interface && -f $pid_file;
|
| 189 |
|
| 190 |
my $pid = `cat $pid_file`;
|
| 191 |
chomp $pid;
|
| 192 |
print "killing pppd($pid)\n";
|
| 193 |
kill("HUP", $pid);
|
| 194 |
print "waiting for pppd to die";
|
| 195 |
do {
|
| 196 |
sleep 1;
|
| 197 |
print ".";
|
| 198 |
}
|
| 199 |
until (kill(0, $pid));
|
| 200 |
|
| 201 |
print "\n";
|
| 202 |
$State = "disconnected";
|
| 203 |
print "disconnected\n";
|
| 204 |
return 1;
|
| 205 |
}
|
| 206 |
|
| 207 |
sub cmd_list {
|
| 208 |
|
| 209 |
print "Server = $Server\n";
|
| 210 |
print "Network = $Net\n";
|
| 211 |
print "Routes = $Route\n";
|
| 212 |
print "Debug = $Debug_Flag\n";
|
| 213 |
print "No_Prompt = $No_Prompt\n";
|
| 214 |
print "Timeout = $Timeout\n";
|
| 215 |
print "\n";
|
| 216 |
}
|
| 217 |
|
| 218 |
sub cmd_help {
|
| 219 |
|
| 220 |
print "Commands are:\n";
|
| 221 |
print "c - initiate PPTP connection\n";
|
| 222 |
print "d - disconnect PPTP\n";
|
| 223 |
print "h - this help message\n";
|
| 224 |
print "l - list current configuration\n";
|
| 225 |
print "q - quite the program\n";
|
| 226 |
print "r - read configuration file\n";
|
| 227 |
print "s - set configuration variable (l for a list)\n";
|
| 228 |
print "w - write the configuration file\n";
|
| 229 |
|
| 230 |
}
|
| 231 |
|
| 232 |
sub cmd_set {
|
| 233 |
my $input = shift;
|
| 234 |
my $no_replace = shift;
|
| 235 |
|
| 236 |
my ($variable, $value) = split(/\s*=\s*/, $input);
|
| 237 |
|
| 238 |
$variable = "\L$variable";
|
| 239 |
if (! $variable) {
|
| 240 |
print "syntax: s variable = value\n";
|
| 241 |
return 0;
|
| 242 |
}
|
| 243 |
|
| 244 |
if ($variable eq "server") {
|
| 245 |
$Server = $value unless $no_replace && $Server;
|
| 246 |
}
|
| 247 |
elsif ($variable eq "network") {
|
| 248 |
$Net = $value unless $no_replace && $Net;
|
| 249 |
}
|
| 250 |
elsif ($variable eq "routes") {
|
| 251 |
$Route = $value unless $no_replace && $Route;
|
| 252 |
}
|
| 253 |
elsif ($variable eq "debug") {
|
| 254 |
$Debug_Flag = $value unless $no_replace && $Debug_Flag;
|
| 255 |
}
|
| 256 |
elsif ($variable eq "no_prompt") {
|
| 257 |
$No_Prompt = $value unless $no_replace && $No_Prompt;
|
| 258 |
}
|
| 259 |
elsif ($variable eq "timeout") {
|
| 260 |
$Timeout = $value unless $no_replace && $Timeout;
|
| 261 |
}
|
| 262 |
elsif ($variable eq "config_file") {
|
| 263 |
$Config_File = $value unless $no_replace && $Config_File;
|
| 264 |
}
|
| 265 |
else {
|
| 266 |
print "unknown variable\n";
|
| 267 |
}
|
| 268 |
}
|
| 269 |
|
| 270 |
sub cmd_read_config_file {
|
| 271 |
my $file = shift;
|
| 272 |
|
| 273 |
my $config = [];
|
| 274 |
$file = $Config_File unless $file;
|
| 275 |
local *IN;
|
| 276 |
if (!open(IN, $file)) {
|
| 277 |
print "cannot open $file\n";
|
| 278 |
return $config;
|
| 279 |
}
|
| 280 |
|
| 281 |
my @config_file = <IN>;
|
| 282 |
close IN;
|
| 283 |
push @config_file, "\n";
|
| 284 |
chomp @config_file;
|
| 285 |
|
| 286 |
for my $line (@config_file) {
|
| 287 |
next if /\s*#/;
|
| 288 |
|
| 289 |
if ($line =~ /\S/) {
|
| 290 |
$line =~ s/^\s*//;
|
| 291 |
$line =~ s/\s*$//;
|
| 292 |
push @$config, $line;
|
| 293 |
next;
|
| 294 |
}
|
| 295 |
}
|
| 296 |
return $config;
|
| 297 |
}
|
| 298 |
|
| 299 |
sub cmd_write_config_file {
|
| 300 |
my $file = shift;
|
| 301 |
|
| 302 |
$file = $Config_File unless $file;
|
| 303 |
local *OUT;
|
| 304 |
if (!open(OUT, ">$file")) {
|
| 305 |
print "cannot open $file\n";
|
| 306 |
return 0;
|
| 307 |
}
|
| 308 |
|
| 309 |
my $oldfh = select OUT;
|
| 310 |
cmd_list();
|
| 311 |
close OUT;
|
| 312 |
select $oldfh;
|
| 313 |
|
| 314 |
return 1;
|
| 315 |
}
|
| 316 |
|
| 317 |
sub net_interface_up {
|
| 318 |
my $cidr = shift;
|
| 319 |
|
| 320 |
# cidr is net/bits
|
| 321 |
my($net, $nbits) = split(/\//, $cidr);
|
| 322 |
|
| 323 |
# compute the network number
|
| 324 |
my $netnum = netnum($net, $nbits);
|
| 325 |
local(*INTERFACE);
|
| 326 |
open(INTERFACE, "ifconfig|") || die "cannot run ifconfig - $!\n";
|
| 327 |
|
| 328 |
my $interface = "";
|
| 329 |
my @interface = <INTERFACE>;
|
| 330 |
close INTERFACE;
|
| 331 |
for (@interface) {
|
| 332 |
chomp;
|
| 333 |
|
| 334 |
# new interface
|
| 335 |
if (/^[a-zA-Z]/) {
|
| 336 |
if ($interface =~ /(.*) Link.*P-t-P:(.*) Mask/) {
|
| 337 |
my $interface_name = $1;
|
| 338 |
my $ip = $2;
|
| 339 |
return $interface_name
|
| 340 |
if netnum($ip, $nbits) == $netnum;
|
| 341 |
}
|
| 342 |
$interface = "";
|
| 343 |
}
|
| 344 |
$interface .= $_;
|
| 345 |
}
|
| 346 |
return "";
|
| 347 |
}
|
| 348 |
|
| 349 |
sub netnum {
|
| 350 |
my $net = shift;
|
| 351 |
my $bits = shift;
|
| 352 |
|
| 353 |
my @octets = split(/\./, $net);
|
| 354 |
my $netnum = 0;
|
| 355 |
for my $octet (@octets) {
|
| 356 |
$netnum <<= 8;
|
| 357 |
$netnum |= $octet;
|
| 358 |
}
|
| 359 |
|
| 360 |
my $mask = 0;
|
| 361 |
for (1..$bits) {
|
| 362 |
$mask <<= 1;
|
| 363 |
$mask |= 1;
|
| 364 |
}
|
| 365 |
$mask = $mask << (32-$bits);
|
| 366 |
|
| 367 |
$netnum &= $mask;
|
| 368 |
|
| 369 |
return $netnum;
|
| 370 |
}
|