|
From: James C. <qu...@us...> - 2006-07-19 06:13:09
|
Update of /cvsroot/pptpclient/pptp-extras/pptpconfig In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25983 Modified Files: ChangeLog NEWS pptpconfig.php Log Message: add command line options Index: ChangeLog =================================================================== RCS file: /cvsroot/pptpclient/pptp-extras/pptpconfig/ChangeLog,v retrieving revision 1.14 retrieving revision 1.15 diff -u -d -r1.14 -r1.15 --- ChangeLog 19 Apr 2006 23:17:42 -0000 1.14 +++ ChangeLog 19 Jul 2006 06:13:06 -0000 1.15 @@ -1,3 +1,18 @@ +Wed Jul 19 15:19:35 2006 Stephen DiVerdi <sdi...@cs...> + + * pptpconfig.php: add start, stop and status options. + + pptpconfig [-q] [tunnel {start|stop|status}] + + If no command line arguments are given, the normal GUI behavior is + executed. '-q' optionally specified quiet behavior, in which case + no output is written and the exit code indicates success or + failure. The name of a previously configured tunnel is specified, + followed by the command to execute. 'start' and 'stop' are the + same as clicking the start or stop buttons in the GUI, and + 'status' returns a string and error code to indicate if the tunnel + is currently active or not. + Thu Apr 20 09:12:53 2006 James Cameron <qu...@us...> * pptpconfig.php: call setsid() to prevent pppd process group kill Index: NEWS =================================================================== RCS file: /cvsroot/pptpclient/pptp-extras/pptpconfig/NEWS,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- NEWS 19 Apr 2006 23:17:42 -0000 1.10 +++ NEWS 19 Jul 2006 06:13:06 -0000 1.11 @@ -1,3 +1,4 @@ + - add command line start, stop and status options [DiVerdi] - call setsid() to prevent process group kill hitting parent [Cameron] 2006-04-10 fix resolv.conf undo Index: pptpconfig.php =================================================================== RCS file: /cvsroot/pptpclient/pptp-extras/pptpconfig/pptpconfig.php,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- pptpconfig.php 19 Apr 2006 23:17:42 -0000 1.9 +++ pptpconfig.php 19 Jul 2006 06:13:06 -0000 1.10 @@ -4,7 +4,7 @@ # $Id$ # # pptpconfig.php, PPTP configuration and management GUI -# Copyright (C) 2002-2004 James Cameron (qu...@us...) +# Copyright (C) 2002-2006 James Cameron (qu...@us...) # # 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 @@ -115,6 +115,61 @@ $xml = $me.'.xml'; if (!@is_readable($xml)) $xml = PATH_LIB.$me.'.xml'; +$use_gui = TRUE; +$quiet = FALSE; + +# check for the optional quiet flag +$argidx = 1; +if ($argv[$argidx]=='-q') { + quiet = TRUE; + $argidx++; +} + +# basic CLI - if there's nothing on the command line, proceed with the +# GUI. otherwise, parse the args and execute the command +if ($argv[$argidx]!='') { + $use_gui = FALSE; + + # get the tunnel name and command + $name = $argv[$argidx++]; + if ($argv[$argidx]=='') + usage(); + $command = $argv[$argidx++]; + + tunnels_load(); + + # make sure the named tunnel has been configured + $found = FALSE; + foreach ($tunnels as $tunnel) { + if ($name==$tunnel['name']) + $found = TRUE; + } + if (!$found) { + echo "couldn't find tunnel $name!\n"; + exit(1); + } + + if ($command=='start') { + start($name); + } else if ($command=='stop') { + stop($name); + } else if ($command=='status') { + $status = pid_state($name); + echo "tunnel $name is $status\n"; + if ($status=='stopped') + exit(1); + } else { + usage(); + } + + exit(0); +} + +function usage() { + echo "usage: $argv[0] [-q] [tunnel {start|stop|status}]\n"; + exit(1); +} + # open the main window on_main_setup_activate(0); @@ -1285,7 +1340,7 @@ # colour and display messages in text widget function scribe($context, $line) { - global $colours, $me; + global $colours, $me, $use_gui, $quiet; $colour = $colours['no']; if (ereg('^sent', $line)) $colour = $colours['tx']; @@ -1302,38 +1357,60 @@ if (ereg('^anon warn', $line)) $colour = $colours['uh']; if (ereg('^anon fatal', $line)) $colour = $colours['uh']; if (ereg('^'.$me, $line)) $colour = $colours['me']; - - $context['text']->insert(NULL, $colour, NULL, $line); + + if ($use_gui) + $context['text']->insert(NULL, $colour, NULL, $line); + else if (!$quiet) + echo $line; } # input handler for pppd pipe, displays what pppd says, with our comments function reader($ignore, $ignore, $context) { - global $me, $gtk_inputs; + global $me, $gtk_inputs, $use_gui, $quiet; $line = fgets($context['pipe'], 1024); - if (!($line === false)) { + if ($use_gui) { + # in the gui, the reader is a callback that processes on line at a time + if (!($line == false)) { + scribe($context, $line); + + if (ereg("Using interface (ppp[0-9]*)", $line, $regs)) { + $timeout = gtk::timeout_add(500, 'stats', $context); + $window = $context['window']; + $window->set_data('timeout', $timeout); + $window->set_data('interface', $regs[1]); + # todo, if end of $line is not newline, add one here + scribe($context, "$me: monitoring interface ".$regs[1]."\n"); + } + + if (ereg('^remote IP address ([0-9.]*)', $line, $regs)) { + $window = $context['window']; + $window->set_data('remoteip', $regs[1]); + } - scribe($context, $line); + return TRUE; + } else { + gtk::input_remove($gtk_inputs[$context['pipe']]); + } + } else { + # in the CLI, reader should read all input right away + while (!($line === false)) { + scribe($context, $line); - if (ereg("Using interface (ppp[0-9]*)", $line, $regs)) { - $timeout = gtk::timeout_add(500, 'stats', $context); - $window = $context['window']; - $window->set_data('timeout', $timeout); - $window->set_data('interface', $regs[1]); - # todo, if end of $line is not newline, add one here - scribe($context, "$me: monitoring interface ".$regs[1]."\n"); - } + if (ereg("Using interface (ppp[0-9]*)", $line, $regs)) { + $context['interface'] = $regs[1]; + # todo, if end of $line is not newline, add one here + scribe($context, "$me: monitoring interface ".$regs[1]."\n"); + } - if (ereg('^remote IP address ([0-9.]*)', $line, $regs)) { - $window = $context['window']; - $window->set_data('remoteip', $regs[1]); - } + if (ereg('^remote IP address ([0-9.]*)', $line, $regs)) { + $context['remoteip'] = $regs[1]; + } - return TRUE; + $line = fgets($context['pipe'], 1024); + } } - gtk::input_remove($gtk_inputs[$context['pipe']]); - $status = pclose($context['pipe']); # decode status, per "man wait" @@ -1359,16 +1436,23 @@ $diagnosis = ($status == 0) ? "started" : "failed"; scribe($context, "$me: pppd process $reason $status ($diagnosis)\n"); - $window = $context['window']; - $window->set_data('exit', $status); - $window->set_data('reason', $reason); - $statusbar = $window->get_data('statusbar'); - $statusbar->pop($window->get_data('id')); - $stop = $window->get_data('stop'); - $ping = $window->get_data('ping'); - $start = $window->get_data('start'); + if ($use_gui) { + $window = $context['window']; + $window->set_data('exit', $status); + $window->set_data('reason', $reason); + $statusbar = $window->get_data('statusbar'); + $statusbar->pop($window->get_data('id')); + $stop = $window->get_data('stop'); + $ping = $window->get_data('ping'); + $start = $window->get_data('start'); + } + if ($status == 0) { - $state = $window->get_data('state'); + if ($use_gui) + $state = $window->get_data('state'); + else + $state = 'initialising'; + if ($state == 'stopping') { $statusbar->push($window->get_data('id'), 'Stopped'); $window->set_data('state', 'stopped'); @@ -1378,16 +1462,22 @@ $ping->set_sensitive(0); $start->set_sensitive(1); } else { - $interface = $window->get_data('interface'); + if ($use_gui ) + $interface = $window->get_data('interface'); + else + $interface = $context['interface']; + routing_start($context, $interface); resolv_start($context, $interface); undo_save($context['name']); - $statusbar->push($window->get_data('id'), 'Connected'); - $window->set_data('state', 'running'); - setup_list_set_state($context['name'], 'running'); scribe($context, "$me: connected\n"); - $ping->set_sensitive(1); - iconify_start($context, $window); + if ($use_gui) { + $statusbar->push($window->get_data('id'), 'Connected'); + $window->set_data('state', 'running'); + setup_list_set_state($context['name'], 'running'); + $ping->set_sensitive(1); + iconify_start($context, $window); + } } } else { if ($reason == 'exit status') { @@ -1396,12 +1486,18 @@ $text = strsignal($status); } scribe($context, "$me: $text\n"); - $window->set_data('state', 'stopped'); - setup_list_set_state($context['name'], 'stopped'); - $statusbar->push($window->get_data('id'), 'Stopped, '.$text); - $stop->set_sensitive(0); - $ping->set_sensitive(0); - $start->set_sensitive(1); + if ($use_gui) { + $window->set_data('state', 'stopped'); + setup_list_set_state($context['name'], 'stopped'); + $statusbar->push($window->get_data('id'), 'Stopped, '.$text); + $stop->set_sensitive(0); + $ping->set_sensitive(0); + $start->set_sensitive(1); + } else { + if (!$quiet) + echo "start failed: $text\n"; + exit(1); + } } if (tunnel_name_to_debug($context['name'])) { @@ -1535,58 +1631,69 @@ kill() system call returned an error for PID '$pid'"); return 0; } -} + return 1; +} # user requests stop tunnel function stop($name) { - global $gx; + global $gx, $use_gui; - kill_name($name); + $killed = kill_name($name); + if (!$use_gui && !$killed) { + if (!$quiet) + echo "stop failed\n"; + exit(1); + } - # create per-tunnel window if not yet existing - if (!isset($gx[$name])) { - $window = tunnel_window_create($name); - $window->set_data('state', 'running'); - setup_list_set_state($name, 'running'); - } else { - # otherwise, just show the widget (may have been hidden by user) - $window = $gx[$name]->get_widget('pptpconfig-tunnel'); - $window->show(); + if ($use_gui) { + # create per-tunnel window if not yet existing + if (!isset($gx[$name])) { + $window = tunnel_window_create($name); + $window->set_data('state', 'running'); + setup_list_set_state($name, 'running'); + } else { + # otherwise, just show the widget (may have been hidden by user) + $window = $gx[$name]->get_widget('pptpconfig-tunnel'); + $window->show(); + } } # restore default route and DNS configuration - $context['text'] = $gx[$name]->get_widget('text'); + if ($use_gui) + $context['text'] = $gx[$name]->get_widget('text'); undo_load($name); undo_execute($context); undo_remove($name); - $timeout = $window->get_data('timeout'); - if (is_int($timeout)) gtk::timeout_remove($window->get_data('timeout')); - $start = $gx[$name]->get_widget('start'); - $ping = $gx[$name]->get_widget('ping'); - $stop = $gx[$name]->get_widget('stop'); - $state = $window->get_data('state'); - $statusbar = $window->get_data('statusbar'); - $statusbar->pop($window->get_data('id')); - if ($state == 'starting') { - $window->set_data('state', 'stopping'); - setup_list_set_state($name, 'stopping'); - $statusbar->push($window->get_data('id'), 'Stopping'); - $stop->set_sensitive(1); - $ping->set_sensitive(1); - $start->set_sensitive(0); - } - if ($state == 'running') { - $window->set_data('state', 'stopped'); - setup_list_set_state($name, 'stopped'); - $statusbar->push($window->get_data('id'), 'Stopped'); - $stop->set_sensitive(0); - $ping->set_sensitive(0); - $start->set_sensitive(1); + if ($use_gui) { + $timeout = $window->get_data('timeout'); + if (is_int($timeout)) gtk::timeout_remove($window->get_data('timeout')); + $start = $gx[$name]->get_widget('start'); + $ping = $gx[$name]->get_widget('ping'); + $stop = $gx[$name]->get_widget('stop'); + $state = $window->get_data('state'); + $statusbar = $window->get_data('statusbar'); + $statusbar->pop($window->get_data('id')); + if ($state == 'starting') { + $window->set_data('state', 'stopping'); + setup_list_set_state($name, 'stopping'); + $statusbar->push($window->get_data('id'), 'Stopping'); + $stop->set_sensitive(1); + $ping->set_sensitive(1); + $start->set_sensitive(0); + } + if ($state == 'running') { + $window->set_data('state', 'stopped'); + setup_list_set_state($name, 'stopped'); + $statusbar->push($window->get_data('id'), 'Stopped'); + $stop->set_sensitive(0); + $ping->set_sensitive(0); + $start->set_sensitive(1); + } + setup_unselect_all(); } - setup_unselect_all(); } # erase security critical portions when dumping tunnel configuration @@ -1607,8 +1714,9 @@ return $tunnel['debug']; } +#sjd modified to respect the use_gui flag for CLI version function start($name) { - global $selected, $tunnels, $gx, $me, $gtk_inputs; + global $selected, $tunnels, $gx, $me, $gtk_inputs, $use_gui; # check for an existing tunnel $pidfile = pid_name($name); @@ -1618,17 +1726,22 @@ return 0; } - # create per-tunnel window if not yet existing - if (!isset($gx[$name])) { - $window = tunnel_window_create($name); - } else { - # otherwise, just show the widget (may have been hidden by user) - $window = $gx[$name]->get_widget('pptpconfig-tunnel'); - $window->show(); + if ($use_gui) { + # create per-tunnel window if not yet existing + if (!isset($gx[$name])) { + $window = tunnel_window_create($name); + } else { + # otherwise, just show the widget (may have been hidden by user) + $window = $gx[$name]->get_widget('pptpconfig-tunnel'); + $window->show(); + } } # check the tunnel state - $state = $window->get_data('state'); + if ($use_gui) + $state = $window->get_data('state'); + else + $state = 'initialising'; # if it was stopped, change to initialising, clear the old log if ($state == 'stopped') { @@ -1641,8 +1754,13 @@ if ($state == 'initialising') { # build a context array - $text = $gx[$name]->get_widget('text'); - $context = array('name' => $name, 'tree' => $gx[$name], 'window' => $window, 'text' => $text); + $context = array('name' => $name); + if ($use_gui) { + $text = $gx[$name]->get_widget('text'); + $context['tree'] = $gx[$name]; + $context['window'] = $window; + $context['text'] = $text; + } # obtain the route to the server before pppd starts, so that # it can be used to add a host route after pppd has connected @@ -1684,28 +1802,32 @@ $context['pipe'] = popen($command.' 2>&1', 'r'); socket_set_blocking($context['pipe'], FALSE); - $window->set_data('state', 'starting'); - setup_list_set_state($name, 'starting'); + if ($use_gui) { + $window->set_data('state', 'starting'); + setup_list_set_state($name, 'starting'); - $statusbar = $window->get_data('statusbar'); - $statusbar->pop($window->get_data('id')); - $statusbar->push($window->get_data('id'), 'Starting'); + $statusbar = $window->get_data('statusbar'); + $statusbar->pop($window->get_data('id')); + $statusbar->push($window->get_data('id'), 'Starting'); - $gtk_inputs[$context['pipe']] = gtk::input_add($context['pipe'], GDK_INPUT_READ, 'reader', $context); - $window->set_data('pipe', $context['pipe']); + $gtk_inputs[$context['pipe']] = gtk::input_add($context['pipe'], GDK_INPUT_READ, 'reader', $context); + $window->set_data('pipe', $context['pipe']); - # allow the stop button, disallow the start button - $stop = $window->get_data('stop'); - $stop->set_sensitive(1); + # allow the stop button, disallow the start button + $stop = $window->get_data('stop'); + $stop->set_sensitive(1); - $ping = $window->get_data('ping'); - $ping->set_sensitive(0); + $ping = $window->get_data('ping'); + $ping->set_sensitive(0); - $start = $window->get_data('start'); - $start->set_sensitive(0); + $start = $window->get_data('start'); + $start->set_sensitive(0); - notebook_reset(); - setup_unselect_all(); + notebook_reset(); + setup_unselect_all(); + } else { + reader(0, 0, $context); + } } else { message('start already', "Cannot Start. Tunnel is already running."); @@ -2074,17 +2196,24 @@ # window pptpconfig-message function message($title, $text) { - $gx = open_window('pptpconfig-message'); - # todo: when window opens, text is blank, when we change text below - # the window enlarges, try (a) setting text before title, (b) - # creating entire widget window inline rather than use glade - # definition. - $window = $gx->get_widget('pptpconfig-message'); - $window->set_title('pptpconfig '.$title); - $label = $gx->get_widget('message_label'); - $label->set_text($text); - $ok = $gx->get_widget('message_ok'); - $ok->hide(); + global $use_gui, $quiet; + if ($use_gui) { + $gx = open_window('pptpconfig-message'); + # todo: when window opens, text is blank, when we change text below + # the window enlarges, try (a) setting text before title, (b) + # creating entire widget window inline rather than use glade + # definition. + $window = $gx->get_widget('pptpconfig-message'); + $window->set_title('pptpconfig '.$title); + $label = $gx->get_widget('message_label'); + $label->set_text($text); + $ok = $gx->get_widget('message_ok'); + $ok->hide(); + } else if (!$quiet) { + echo "$title\n"; + echo " $text\n"; + echo "\n"; + } } function on_message_ok_clicked($widget) { |