2 # Common functions for configuring WV-Dial
3 # XXX what about redhat connect process?
4 # XXX what about SuSE connect process?
6 BEGIN { push(@INC, ".."); };
9 $details_file = "$module_config_directory/connect";
10 $resolv_conf = "/etc/resolv.conf";
11 $ppp_resolv_conf = "/etc/ppp/resolv.conf";
12 $save_resolv_conf = $resolv_conf.".save";
15 # Returns a list of all configuration settings
20 open(FILE, $config{'file'});
26 $sect = { 'name' => $1,
27 'index' => scalar(@rv),
34 elsif (/^\s*([^=]+\S)\s*=\s*(.*)/ && $sect) {
35 # A directive within a section
36 $sect->{'values'}->{lc($1)} = $2;
37 $sect->{'onames'}->{lc($1)} = $1;
38 $sect->{'eline'} = $lnum;
46 # create_dialer(&dialer)
47 # Add a dialer to the configuration
50 local $lref = &read_file_lines($config{'file'});
51 push(@$lref, "", &dialer_lines($_[0]));
55 # update_dialer(&dialer)
58 local $lref = &read_file_lines($config{'file'});
59 splice(@$lref, $_[0]->{'line'}, $_[0]->{'eline'} - $_[0]->{'line'} + 1,
60 &dialer_lines($_[0]));
64 # delete_dialer(&dialer)
67 local $lref = &read_file_lines($config{'file'});
68 splice(@$lref, $_[0]->{'line'}, $_[0]->{'eline'} - $_[0]->{'line'} + 1);
69 if ($_[0]->{'line'} && $lref->[$_[0]->{'line'}-1] eq "") {
70 splice(@$lref, $_[0]->{'line'}-1, 1);
75 # dialer_lines(&dialer)
78 local @rv = "[$_[0]->{'name'}]";
80 foreach $k (keys %{$_[0]->{'values'}}) {
81 local $pk = $_[0]->{'onames'}->{$k} || $k;
82 push(@rv, $pk." = ".$_[0]->{'values'}->{$k});
88 # Returns a human-readable modem device name
91 if ($_[0] =~ /^\/dev\/ttyS(\d+)$/) {
92 return &text('device_serial', $1+1);
95 return "<tt>$_[0]</tt>";
99 # save_connect_details(ip, pid, section)
100 sub save_connect_details
102 &open_tempfile(DETAILS, ">$details_file");
103 &print_tempfile(DETAILS, $_[0],"\n",$_[1],"\n",$_[2],"\n");
104 &close_tempfile(DETAILS);
107 # get_connect_details()
108 sub get_connect_details
110 local ($ip, $pid, $sect);
111 open(DETAILS, $details_file) || return ();
112 chop($ip = <DETAILS>);
113 chop($pid = <DETAILS>);
114 chop($sect = <DETAILS>);
116 return ($ip, $pid, $sect);
120 # Returns the PID of a running wvdial process
124 &foreign_require("proc", "proc-lib.pl");
125 foreach $p (&proc::list_processes()) {
126 if ($p->{'args'} =~ /^\S*wvdial($|\s)/ && $p->{'args'} !~ /\[/) {
135 return $_[0] =~ /^Dialer Defaults$/i ?
136 $text{'index_defaults'} :
137 $_[0] =~ /^Dialer\s+(.*)$/i ?
138 &text('index_dialer', "$1") : $_[0];
141 # ppp_connect(section-name, textmode)
144 # Get this dialer's configuration
145 local $conf = &get_config();
146 local ($dialer) = grep { lc($_->{'name'}) eq lc($_[0]) } @$conf;
147 local ($ddialer) = grep { lc($_->{'name'}) eq 'dialer defaults' } @$conf;
148 local ($inherits, $parent, $autodns);
149 if ($inherits = $dialer->{'values'}->{'inherits'}) {
150 ($parent) = grep { lc($_->{'name'}) eq lc($inherits) } @$conf;
152 $autodns = $dialer->{'values'}->{'auto dns'} ?
153 ($dialer->{'values'}->{'auto dns'} =~ /on|yes|1/ ? 1 : 0) :
154 $parent->{'values'}->{'auto dns'} ?
155 ($parent->{'values'}->{'auto dns'} =~ /on|yes|1/ ? 1 : 0) :
156 $ddialer->{'values'}->{'auto dns'} ?
157 ($ddialer->{'values'}->{'auto dns'} =~ /on|yes|1/ ? 1 : 0) :
160 # Run wvdial, writing to a temp file in the background
161 local $stime = time();
163 $sect =~ s/^Dialer\s+//;
164 local $temp = &tempname();
165 if (!($pid = fork())) {
169 open(STDOUT, ">$temp");
170 open(STDERR, ">&STDOUT");
171 open(STDIN, "/dev/null");
172 exec($config{'wvdial'}, $sect);
175 while (!open(TEMP, $temp)) { }
176 &additional_log("exec", undef, "$config{'wvdial'} $sect");
179 # Read output until success or failure
181 print &text('connect_cmd', "$config{'wvdial'} $sect"),"\n\n";
184 print "<b>",&text('connect_cmd',
185 "<tt>$config{'wvdial'} $sect</tt>"),"</b><br>\n";
188 local ($connected, $failed);
190 local $line = &wait_for_line();
195 print &html_escape($line);
197 if ($line =~ /IP\s+address\s+is\s+(\d+\.\d+\.\d+\.\d+)/i) {
199 $connected = $1 eq "0.0.0.0" ? "*" : $1;
202 elsif ($line =~ /starting\s+ppp/i) {
203 # Connected in stupid mode
208 # Program terminated .. must have failed!
213 print "</pre>\n" if ($_[1] == 0);
215 # If OK, save the PID for later
218 print "<b>",$connected eq '*' ? $text{'connect_noip'} :
219 &text('connect_ip', "<tt>$connected</tt>"),"</b><p>\n";
222 print $connected eq '*' ? $text{'connect_noip'} :
223 &text('connect_ip', $connected),"\n";
225 &save_connect_details($connected, $pid, $_[0]);
229 print "<b>$text{'connect_failed'}</b><p>\n";
232 print "$text{'connect_failed'}\n\n";
235 $config{'dialer'} = $_[0];
236 &lock_file("$module_config_directory/config");
237 &write_file("$module_config_directory/config", \%config);
238 &unlock_file("$module_config_directory/config");
240 if ($connected && $autodns) {
241 # If the resolv.conf file has not been modified, and the PPP
242 # resolv.conf has, copy it into place
245 local ($ip, $pid, $sect) = &get_connect_details();
246 if (!$pid || !kill(0, $pid)) {
247 # Connection is down .. DNS will never be updated
249 print "<b>$text{'connect_dnsdown'}</b><p>\n";
252 print "$text{'connect_dnsdown'}\n\n";
257 if ($now > $stime+60) {
258 # Took too long to update DNS
260 print "<b>$text{'connect_dnsto'}</b><p>\n";
263 print "$text{'connect_dnsto'}\n\n";
267 local @pst = stat($ppp_resolv_conf);
268 local @ost = stat($resolv_conf);
269 if ($ost[9] >= $stime) {
270 # Something else has update the DNS config ..
272 print "<b>$text{'connect_dns2'}</b><p>\n";
275 print "$text{'connect_dns2'}\n\n";
279 if ($pst[9] >= $stime) {
280 # A PPP DNS config has been created .. use it
281 &system_logged("cp $resolv_conf $save_resolv_conf")
282 if (!-l $resolv_conf);
283 unlink($resolv_conf);
284 &system_logged("cp $ppp_resolv_conf $resolv_conf");
286 print "<b>$text{'connect_dns'}</b><p>\n";
289 print "$text{'connect_dns'}\n\n";
299 # Reads a line from the temp file, or waits if there is no more to read. Only
300 # returns undef if the process has died
303 local $line = <TEMP>;
304 return $line if ($line);
306 return undef if (!kill(0, $pid));
308 return &wait_for_line();
311 # ppp_disconnect(mode, text-mode)
312 # Shuts down the active PPP connection
315 local ($ip, $pid, $sect);
317 ($ip, $pid, $sect) = &get_connect_details();
320 $pid = &get_wvdial_pid();
322 if ($pid && &kill_logged('TERM', $pid)) {
323 # Tell the user that the connection is now down
326 print &text('disc_ok1', "<tt>$ip</tt>", &dialer_name($sect)),"<p>\n";
329 print &text('disc_ok1', $ip, &dialer_name($sect)),"\n\n";
334 print $text{'disc_ok2'},"<p>\n";
337 print $text{'disc_ok2'},"\n\n";
341 # Restore the saved DNS config file, if it hasn't been done
343 local @ost = stat($resolv_conf);
344 if (!-l $resolv_conf && $ost[9] < time()-5 && -r $save_resolv_conf) {
345 &system_logged("mv $save_resolv_conf $resolv_conf");
347 print "$text{'disc_dns'}<p>\n";
350 print "$text{'disc_dns'}\n\n";
353 &system_logged("rm -f $ppp_resolv_conf");
357 # Wasn't even active .. tell the user
359 print "$text{'disc_edown'}<p>\n";
362 print "$text{'disc_edown'}\n\n";