Skip to content

Commit

Permalink
Add UI for setting custom routes and adding IPv4/IPv6 addresses to de…
Browse files Browse the repository at this point in the history
…vices
  • Loading branch information
lattera committed May 15, 2012
1 parent abfe2c8 commit 28b2b20
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 15 deletions.
10 changes: 10 additions & 0 deletions jailadmin.helpers.inc
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,13 @@ function get_all_mounts_for_select($jail) {

return $mounts;
}

function get_all_routes_for_select($jail) {
$routes = array();

$i=0;
foreach ($jail->routes as $route)
$routes[$route['source'] . '^' . $route['destination'] . '^' . $i++] = $route['source'] . '->' . $route['destination'];

return $routes;
}
10 changes: 10 additions & 0 deletions jailadmin.module
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ function jailadmin_menu() {
'type' => MENU_NORMAL_ITEM,
);

$items['jailadmin/%/network'] = array(
'title' => 'Configure Jail Network',
'page callback' => 'drupal_get_form',
'page arguments' => array('jailadmin_config_network', 1),
'file' => 'jailnetwork.inc',
'access callback' => 'jail_config_access',
'access arguments' => array(1),
'type' => MENU_NORMAL_ITEM,
);

return $items;
}

Expand Down
89 changes: 74 additions & 15 deletions jailconfig.inc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function jailadmin_config($form, &$form_state) {
$network_devices = get_all_network_devices_for_select($jail);
$services = get_all_services_for_select($jail);
$mounts = get_all_mounts_for_select($jail);
$routes = get_all_routes_for_select($jail);
$readonly = FALSE;

if (user_access('config ' . $jail->name) == FALSE)
Expand Down Expand Up @@ -56,6 +57,13 @@ function jailadmin_config($form, &$form_state) {
);

if (count($network_devices)) {
$form['network_devices']['devconfig'] = array(
'#type' => 'markup',
'#prefix' => '<div>',
'#markup' => l('Configure existing devices', "jailadmin/{$jail->name}/network"),
'#suffix' => '</div>',
);

$form['network_devices']['devices'] = array(
'#type' => 'select',
'#title' => t('Network Devices'),
Expand Down Expand Up @@ -88,6 +96,43 @@ function jailadmin_config($form, &$form_state) {
'#default_value' => FALSE,
);

$form['routes'] = array(
'#type' => 'fieldset',
'#title' => t('Routes'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);

if (count($routes)) {
$form['routes']['existing_routes'] = array(
'#type' => 'select',
'#title' => t('Existing Routes'),
'#description' => t('Select one or more to delete'),
'#multiple' => TRUE,
'#options' => $routes,
'#disabled' => $jail->IsOnline() || $readonly,
);

$form['routes']['new_route'] = array(
'#type' => 'fieldset',
'#title' => t('New Route'),
);

$form['routes']['new_route']['new_route_source'] = array(
'#type' => 'textfield',
'#title' => t('Source'),
'#size' => 60,
'#disabled' => $jail->IsOnline() || $readonly,
);

$form['routes']['new_route']['new_route_destination'] = array(
'#type' => 'textfield',
'#title' => t('Destination'),
'#size' => 60,
'#disabled' => $jail->IsOnline() || $readonly,
);
}

$form['services'] = array(
'#type' => 'fieldset',
'#title' => t('Services'),
Expand Down Expand Up @@ -231,13 +276,6 @@ function jailadmin_config($form, &$form_state) {
'#disabled' => $jail->IsOnline() || $readonly,
);

$form['jail_actions']['setup_services'] = array(
'#type' => 'submit',
'#value' => t('Setup Services'),
'#submit' => array('jail_actions_setup_services'),
'#disabled' => $jail->IsOnline() || $readonly,
);

$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Configuration'),
Expand Down Expand Up @@ -279,6 +317,21 @@ function jailadmin_config_submit($form, &$form_state) {
}
}

if (isset($form_state['values']['existing_routes'])) {
foreach(array_filter($form_state['values']['existing_routes']) as $route) {
$arr = explode('^', $route);
if (count($arr) != 3) {
drupal_set_message(t('Improper route object sent'), 'error');
} else {
db_delete('jailadmin_routes')
->condition('jail', $jail->name)
->condition('source', $arr[0])
->condition('destination', $arr[1])
->execute();
}
}
}

if (isset($form_state['values']['existing_mounts'])) {
foreach (array_filter($form_state['values']['existing_mounts']) as $target) {
$mount = Mount::LoadByTarget($jail, $target);
Expand Down Expand Up @@ -324,6 +377,18 @@ function jailadmin_config_submit($form, &$form_state) {
$form_state['input']['new_network_device'] = '';
}

if (isset($form_state['values']['new_route_source']) && strlen($form_state['values']['new_route_source'])) {
db_insert('jailadmin_routes')
->fields(array(
'jail' => $jail->name,
'source' => $form_state['values']['new_route_source'],
'destination' => $form_state['values']['new_route_destination'],
))->execute();

$form_state['input']['new_route_source'] = '';
$form_state['input']['new_route_destination'] = '';
}

if (isset($form_state['values']['services'])) {
foreach (array_filter($form_state['values']['services']) as $raw_service) {
$service = new Service;
Expand All @@ -347,6 +412,8 @@ function jailadmin_config_submit($form, &$form_state) {
if ($dirty)
$jail->Persist();

drupal_set_message(t('Settings saved!'));

$form_state['rebuild'] = TRUE;
}

Expand Down Expand Up @@ -440,11 +507,3 @@ function jail_actions_upgrade($form, &$form_state) {

$form_state['rebuild'] = TRUE;
}

function jail_actions_setup_services($form, &$form_state) {
$jail = Jail::Load($form_state['build_info']['args'][0]);

$jail->SetupServices();

$form_state['rebuild'] = TRUE;
}
122 changes: 122 additions & 0 deletions jailnetwork.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

require_once('jailadmin.helpers.inc');
require_once('classes/Mount.php');
require_once('classes/Service.php');
require_once('classes/Network.php');
require_once('classes/NetworkDevice.php');
require_once('classes/Jail.php');

function check_input($form_state=array(), $name) {
if (isset($form_state['values'][$name]) && strlen($form_state['values'][$name]))
return $form_state['values'][$name];

if (isset($form_state['input'][$name]) && strlen($form_state['input'][$name]))
return $form_state['input'][$name];

return FALSE;
}

function jailadmin_config_network($form, &$form_state) {
$jail = Jail::Load($form_state['build_info']['args'][0]);
$devices = get_all_network_devices_for_select($jail);
$devicename = check_input($form_state, 'device');
$readonly = FALSE;

if (user_access('config ' . $jail->name) == FALSE)
$readonly = TRUE;

if ($readonly) {
if (user_access('view ' . $jail->name . ' config') == FALSE) {
drupal_set_message(t('Access Denied'), 'error');
return $form;
}
}

if ($jail->IsOnline())
drupal_set_message(t('Jail is online. All settings are read-only.'), 'status');

$form['devices'] = array(
'#type' => 'fieldset',
'#title' => t('Devices'),
);

if (count($devices)) {
$form['devices']['device'] = array(
'#type' => 'select',
'#title' => t('Existing Devices'),
'#description' => t('Select one or more to modify'),
'#required' => TRUE,
'#options' => $devices,
);

if ($devicename !== FALSE)
$form['devices']['device']['#default_value'] = $devicename;
}

if ($devicename !== FALSE) {
$form['devices'][$devicename]['settings'] = array(
'#type' => 'fieldset',
'#title' => t('@device settings', array('@device' => $devicename)),
);

$device = NetworkDevice::LoadByDeviceName($jail, $devicename);
$form['devices'][$devicename]['settings']['existing'] = array(
'#type' => 'select',
'#title' => t('Existing IPs'),
'#description' => t('Select one or more to delete'),
'#multiple' => TRUE,
'#options' => drupal_map_assoc($device->ips),
'#disabled' => $jail->IsOnline() || $readonly,
);

$form['devices'][$devicename]['new_ip'] = array(
'#type' => 'textfield',
'#title' => t('Add New IP'),
'#size' => 60,
'#diabled' => $jail->IsOnline() || $readonly,
);
}

$form['save'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);

$form_state['rebuild'] = TRUE;

return $form;
}

function jailadmin_config_network_submit($form, &$form_state) {
$jail = Jail::Load($form_state['build_info']['args'][0]);
$form_state['rebuild'] = TRUE;

$devicename = check_input($form_state, 'device');
if ($devicename === FALSE)
return $form;

if (isset($form_state['values']['existing']) && is_array($form_state['values']['existing']) && count($form_state['values']['existing'])) {
foreach (array_filter($form_state['values']['existing']) as $todelete) {
db_delete('jailadmin_epair_aliases')
->condition('device', $devicename)
->condition('ip', $todelete)
->execute();
}
}

$new_ip = check_input($form_state, 'new_ip');
if ($new_ip !== FALSE && strlen($new_ip)) {
db_insert('jailadmin_epair_aliases')
->fields(array(
'device' => $devicename,
'ip' => $new_ip,
))->execute();

$form_state['input']['new_ip'] = '';
}

drupal_set_message(t('Settings saved!'));

return $form;
}

0 comments on commit 28b2b20

Please sign in to comment.