4 BEGIN { push(@INC, ".."); };
10 # Returns a list of the details of configured tunnels, in the format used
11 # by the pptp-command script
15 opendir(DIR, $config{'peers_dir'});
16 while($f = readdir(DIR)) {
17 next if ($f =~ /^\./ || $f eq "__default");
18 local @opts = &parse_ppp_options("$config{'peers_dir'}/$f");
19 local ($pptp) = grep { $_->{'comment'} =~ /^PPTP/ } @opts;
21 # Is a tunnel config .. add it
22 push(@rv, { 'name' => $f,
23 'file' => "$config{'peers_dir'}/$f",
31 # parse_ppp_options(file)
40 # A comment, used to store meta-information
41 push(@rv, { 'comment' => $1,
44 'index' => scalar(@rv) });
46 elsif (/^([0-9\.]+):([0-9\.]+)/) {
47 # A local/remote IP specification
48 push(@rv, { 'local' => $1,
52 'index' => scalar(@rv) });
54 elsif (/^([^# ]*)\s*([^#]*)/) {
55 # A PPP options directive
56 push(@rv, { 'name' => $1,
60 'index' => scalar(@rv) });
71 local @rv = grep { lc($_->{'name'}) eq lc($_[0]) } @{$_[1]};
72 return wantarray ? @rv : $rv[0];
75 # save_ppp_option(&config, file, &old|name, &new)
78 local $ol = ref($_[2]) || !defined($_[2]) ? $_[2] : &find($_[2], $_[0]);
80 local $lref = &read_file_lines($_[1]);
84 $line = $nw->{'local'}.":".$nw->{'remote'};
86 elsif ($nw->{'comment'}) {
87 $line = "# ".$nw->{'comment'};
90 $line = $nw->{'name'};
91 $line .= " $nw->{'value'}" if ($nw->{'value'} ne "");
95 $lref->[$ol->{'line'}] = $line;
98 splice(@$lref, $ol->{'line'}, 1);
100 foreach $c (@{$_[0]}) {
101 $c->{'line'}-- if ($c->{'line'} > $ol->{'line'});
110 # Returns a list of the names of tunnels that appear to be active. May include
111 # other ppp calls as well
114 &foreign_require("proc", "proc-lib.pl");
116 foreach $p (&proc::list_processes()) {
117 if ($p->{'args'} =~ /pppd\s.*call\s+(.*\S+)/) {
118 push(@rv, [ $1, $p->{'pid'} ]);
119 if ($1 eq $config{'tunnel'}) {
120 $rv[$#rv]->[2] = $config{'iface'};
127 # parse_comments(&tunnel)
130 foreach $c (@{$_[0]->{'opts'}}) {
131 if ($c->{'comment'} =~ /Server IP: (\S+)/) {
132 $_[0]->{'server'} = $1;
133 $_[0]->{'server_c'} = $c;
135 elsif ($c->{'comment'} =~ /Route: (.*)/) {
136 push(@{$_[0]->{'routes'}}, $1);
137 push(@{$_[0]->{'routes_c'}}, $c);
142 @old_mppe = ( 'mppe-40', 'mppe-128', 'mppe-stateless' );
143 @new_mppe = ( [ 'mppe', 0 ], [ 'mppe-40', 1 ], [ 'mppe-128', 1 ],
144 [ 'mppe-stateful', 0 ] );
146 # mppe_options_form(&opts)
147 # Show a form for editing MPPE-related PPP options
148 sub mppe_options_form
150 # Get the PPPd version. Only those above 2.4.2 have built-in MPPE support
151 local $out = `pppd --help 2>&1`;
152 local $mppe_mode = &mppe_support();
153 print "<input type=hidden name=mppe_mode value='$mppe_mode'>\n";
157 # Show new MPPE options
159 foreach $o (@new_mppe) {
160 local $o0 = &find("require-".$o->[0], $opts);
161 local $o1 = &find("no".$o->[0], $opts);
162 local $mode = $o0 ? 2 : $o1 ? 0 : 1;
163 print "<tr> <td><b>",$text{'mppe_'.$o->[0]},"</b></td>\n";
164 print "<td colspan=3>\n";
165 printf "<input type=radio name=%s value=2 %s> %s\n",
166 $o->[0], $mode == 2 ? "checked" : "", $text{'mppe_m2'};
167 printf "<input type=radio name=%s value=1 %s> %s (%s)\n",
168 $o->[0], $mode == 1 ? "checked" : "", $text{'default'},
169 $o->[1] ? $text{'mppe_d1'} : $text{'mppe_d0'};
170 printf "<input type=radio name=%s value=0 %s> %s\n",
171 $o->[0], $mode == 0 ? "checked" : "", $text{'mppe_m0'};
172 print "</td> </tr>\n";
174 local @anyold = grep { &find($_, $opts) } @old_mppe;
176 print "<tr> <td colspan=4 align=center>",&text('mppe_old',
177 "<tt>".join(" ", @anyold)."</tt>"),"</td> </tr>\n";
181 # Show old MPPE options
183 foreach $o (@old_mppe) {
184 print "<tr>\n" if ($i%2 == 0);
185 local $v = &find($o, $opts);
186 print "<td><b>",$text{'mppe_'.$o},"</b></td> <td>\n";
187 printf "<input type=radio name=$o value=1 %s> %s\n",
188 $v ? "checked" : "", $text{'yes'};
189 printf "<input type=radio name=$o value=0 %s> %s</td>\n",
190 $v ? "" : "checked", $text{'no'};
191 print "</tr>\n" if ($i%2 == 1);
194 local @anynew = grep { &find($_, $opts) }
195 ( map { 'require-'.$_->[0] } @new_mppe ),
196 ( map { 'no'.$_->[0] } @new_mppe );
198 print "<tr> <td colspan=4 align=center>",&text('mppe_new',
199 "<tt>".join(" ", @anynew)."</tt>"),"</td> </tr>\n";
205 # parse_mppe_options(&config, file)
206 sub parse_mppe_options
209 if ($in{'mppe_mode'}) {
210 # Parse new-style options
211 foreach $o (map { $_->[0] } @new_mppe) {
213 &save_ppp_option($_[0], $_[1], "require-$o",
214 { 'name' => "require-$o" });
215 &save_ppp_option($_[0], $_[1], "no$o", undef);
217 elsif ($in{$o} == 1) {
218 &save_ppp_option($_[0], $_[1], "require-$o", undef);
219 &save_ppp_option($_[0], $_[1], "no$o", undef);
222 &save_ppp_option($_[0], $_[1], "require-$o", undef);
223 &save_ppp_option($_[0], $_[1], "no$o",
224 { 'name' => "no$o" });
229 # Parse old-style options
230 foreach $o (@old_mppe) {
231 &save_ppp_option($_[0], $_[1], $o,
232 $in{$o} ? { 'name' => $o } : undef);
238 # Returns 1 if the PPP daemon supports new-style MPPE options (version 2.4.2+,
239 # 0 if might only support the old options)
242 local $out = `pppd --help 2>&1`;
244 if ($out =~ /version\s+(\S+)/i) {
247 if ($vers =~ /^(\d+)/ && $1 > 2 ||
248 $vers =~ /^(\d+)\.(\d+)/ && $1 == 2 && $2 > 4 ||
249 $vers =~ /^(\d+)\.(\d+)\.(\d+)/ && $1 == 2 && $2 == 4 && $3 >= 2) {
255 # get_pppd_version(&out)
258 local $out = `pppd --help 2>&1`;
260 return $out =~ /version\s+(\S+)/i ? $1 : undef;
263 # connect_tunnel(&tunnel)
264 # Attempts to open some tunnel. Returns either :
265 # 1, iface-name, iface-address, iface-ptp
269 local $tunnel = $_[0];
270 &foreign_require("net", "net-lib.pl");
272 # Run the PPTP command, and wait for a new pppN interface to come up
273 local %sifaces = map { $_->{'fullname'}, $_->{'address'} } &get_ppp_ifaces();
274 local $start = time();
275 local $temp = &tempname();
276 &system_logged("modprobe ip_gre >/dev/null 2>&1");
277 &system_logged("$config{'pptp'} ".quotemeta($tunnel->{'server'})." call ".
278 quotemeta($tunnel->{'name'})." >$temp 2>&1 </dev/null &");
280 LOOP: while(time() - $start < $config{'timeout'}) {
282 local @nifaces = &get_ppp_ifaces();
284 foreach $i (@nifaces) {
285 if (!$sifaces{$i->{'fullname'}}) {
291 local $tempout = `cat $temp`;
294 # Find out if we were connected, or if it failed
296 # It worked! Add the routes
297 local (@rout, @rcmd);
298 if (@{$tunnel->{'routes'}}) {
299 local @routes = &net::list_routes();
300 local ($defroute) = grep { $_->{'dest'} eq '0.0.0.0' } @routes;
301 local $oldgw = $defroute->{'gateway'} if ($defroute);
302 foreach $r (@{$tunnel->{'routes'}}) {
304 $cmd =~ s/TUNNEL_DEV/$newiface->{'fullname'}/g;
305 $cmd =~ s/DEF_GW/$oldgw/g;
306 $cmd =~ s/GW/$newiface->{'ptp'}/g;
308 $out = &backquote_logged("$cmd 2>&1 </dev/null");
313 return (1, $newiface->{'fullname'}, $newiface->{'address'},
314 $newiface->{'ptp'}, \@rcmd, \@rout);
317 # Must have timed out due to a failure
318 &foreign_require("syslog", "syslog-lib.pl");
319 local $sysconf = &syslog::get_config();
322 foreach $c (@$sysconf) {
323 next if ($c->{'tag'} || !$c->{'file'} || !-f $c->{'file'});
324 local @st = stat($c->{'file'});
325 if ($st[9] > $start) {
326 # Was modified since start .. but by ppp or pptp?
327 local $tail = `tail -10 '$c->{'file'}'`;
328 if ($tail =~ /ppp|pptp/) {
334 return (0, $tempout.$logs || "No logged error messages found");
340 return grep { $_->{'fullname'} =~ /^ppp(\d+)$/ &&
341 $_->{'up'} && $_->{'address'} } &net::active_interfaces();