Skip to content
Browse files

Add ability to add/remove servers from server groups.

  • Loading branch information...
1 parent e1bb611 commit 930f9599e469d25af69d153e041fe03f4ca7d289 @lattera lattera committed Jan 13, 2012
View
100 sites/all/modules/wayfair/vulnscan/helpers.inc
@@ -1,5 +1,7 @@
<?php
+/* Server functions */
+
function get_all_servers() {
return db_query('SELECT * FROM {vulnscan_server}');
}
@@ -17,8 +19,106 @@ function get_all_servers_assoc() {
function delete_servers($servers = array()) {
/* $servers array needs to be an associative array (e.g. array('servername' => 'servername') */
foreach ($servers as $key => $value) {
+ db_delete('vulnscan_servergroupmapping')
+ ->condition('servername', $value)
+ ->execute();
+
db_delete('vulnscan_server')
->condition('name', $value)
->execute();
}
}
+
+function add_server($server) {
+ if (strlen($server) == 0)
+ return false;
+
+ $name = strtoupper($server);
+ $servers = get_all_servers_assoc();
+
+ if (array_key_exists($name, $servers))
+ return false;
+
+ db_insert('vulnscan_server')
+ ->fields(array(
+ 'name' => $name,
+ ))->execute();
+
+ return true;
+}
+
+/* Server Group functions */
+
+function get_all_server_groups() {
+ return db_query('SELECT * FROM {vulnscan_servergroup}');
+}
+
+function get_all_server_groups_assoc() {
+ $groups = array();
+ $result = get_all_server_groups();
+
+ foreach ($result as $record)
+ $groups[$record->groupname] = $record->groupname;
+
+ return $groups;
+}
+
+function add_server_group($groupname) {
+ if (strlen($groupname) == 0)
+ return false;
+
+ $groups = get_all_server_groups_assoc();
+ if (array_key_exists($groupname, $groups))
+ return false;
+
+ db_insert('vulnscan_servergroup')
+ ->fields(array(
+ 'groupname' => $groupname,
+ ))->execute();
+
+ return true;
+}
+
+function get_all_servers_in_server_group($groupname) {
+ return db_query('SELECT servername FROM {vulnscan_servergroupmapping} WHERE groupname = :groupname', array(':groupname' => $groupname));
+}
+
+function get_all_servers_in_server_group_assoc($groupname) {
+ $servers = array();
+ $result = get_all_servers_in_server_group($groupname);
+
+ foreach ($result as $record)
+ $servers[$record->servername] = $record->servername;
+
+ return $servers;
+}
+
+function get_all_servers_in_server_group_for_default($groupname) {
+ $servers = array();
+ $result = get_all_servers_in_server_group($groupname);
+
+ foreach ($result as $record)
+ $servers[$record->servername] = $record->servername;
+
+ return $servers;
+}
+
+function remove_servers_in_server_group($groupname, $servers=array()) {
+ foreach ($servers as $key => $value) {
+ db_delete('vulnscan_servergroupmapping')
+ ->condition('groupname', $groupname)
+ ->condition('servername', $value)
+ ->execute();
+ }
+}
+
+function add_servers_in_server_group($groupname, $servers=array()) {
+ foreach ($servers as $server)
+ db_insert('vulnscan_servergroupmapping')
+ ->fields(array(
+ 'groupname' => $groupname,
+ 'servername' => $server,
+ ))->execute();
+
+ return true;
+}
View
119 sites/all/modules/wayfair/vulnscan/vulnscan.admin.inc
@@ -56,21 +56,114 @@ function vulnscan_admin_settings_submit($form, &$form_state) {
variable_set('vulnscan_skipfish_location', $form_state['values']['skipfish_location']);
/* Save the new server */
- if (isset($form_state['values']['new_server']) && strlen($form_state['values']['new_server'])) {
- $name = strtoupper($form_state['values']['new_server']);
- $servers = get_all_servers_assoc();
-
- if (array_key_exists($name, $servers)) {
- form_set_error('new_server', t('Server @server already exists.', array('@server' => $name)));
- } else {
- db_insert('vulnscan_server')->fields(array(
- 'name' => $name
- ))->execute();
- }
- }
+ if (isset($form_state['values']['new_server']) && strlen($form_state['values']['new_server']))
+ if (add_server($form_state['values']['new_server']) == false)
+ form_set_error('new_server', t('An error occurred adding the server @server', array('@server' => $form_state['values']['new_server'])));
/* Delete selected servers */
- if (isset($form_state['values']['servers'])) {
+ if (isset($form_state['values']['servers']))
delete_servers(array_filter($form_state['values']['servers']));
+}
+
+function vulnscan_admin_settings_servergroup($form, &$form_state) {
+// form_set_error('new_servergroup', t('form_state: @state', array('@state' => var_export($form_state, TRUE))));
+// form_set_error('new_servergroup', t('form_state: @state', array('@state' => var_export($form_state, TRUE))));
+ $groups = get_all_server_groups_assoc();
+
+ $form['vulnscan_servergroup'] = array(
+ '#type' => 'fieldset',
+ '#title' => t('Server Group'),
+ );
+
+ if (count($groups) > 0) {
+ $form['vulnscan_servergroup']['servergroup'] = array(
+ '#type' => 'select',
+ '#title' => t('Server Group'),
+ '#options' => $groups,
+ );
}
+
+ $form['vulnscan_servergroup']['new_servergroup'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Add New Server Group'),
+ '#size' => 30,
+ );
+
+ if (isset($form_state['input']['servergroup'])) {
+ $existing_servers = get_all_servers_in_server_group_assoc($form_state['input']['servergroup']);
+ $all = get_all_servers_assoc();
+
+ /* Only show existing/add fieldsets if servers exist */
+ if (count($all)) {
+
+ $new_servers = array();
+ foreach ($all as $key => $value)
+ if (array_key_exists($key, $existing_servers) == false)
+ $new_servers[$key] = $value;
+
+ if (count($existing_servers)) {
+ $form['vulnscan_existing_servers'] = array(
+ '#type' => 'fieldset',
+ '#title' => t('Assigned Servers'),
+ '#collapsible' => TRUE,
+ '#collapsed' => FALSE,
+ );
+
+ $form['vulnscan_existing_servers']['existing_servers'] = array(
+ '#type' => 'select',
+ '#multiple' => TRUE,
+ '#description' => t('Select one or more to delete'),
+ '#options' => $existing_servers
+ );
+ }
+
+ $form['vulnscan_new_servers'] = array(
+ '#type' => 'fieldset',
+ '#title' => t('Add New Servers'),
+ '#collapsible' => TRUE,
+ '#collapsed' => FALSE,
+ );
+
+ $form['vulnscan_new_servers']['new_server'] = array(
+ '#type' => 'select',
+ '#multiple' => TRUE,
+ '#options' => $new_servers,
+ );
+ }
+ }
+
+ $form['select_servergroup'] = array(
+ '#type' => 'submit',
+ '#submit' => array('vulnscan_admin_settings_servergroup_setservergroup'),
+ '#value' => t('Select Server Group'),
+ );
+
+ $form['submit'] = array(
+ '#type' => 'submit',
+ '#value' => t('Save Settings'),
+ );
+
+ return $form;
+}
+
+function vulnscan_admin_settings_servergroup_setservergroup($form, &$form_state) {
+ $form_state['rebuild'] = TRUE;
+}
+
+function vulnscan_admin_settings_servergroup_submit($form, &$form_state) {
+ if (isset($form_state['values']['new_servergroup']) && strlen($form_state['values']['new_servergroup']))
+ if (add_server_group($form_state['values']['new_servergroup']) == false)
+ form_set_error('new_servergroup', t('Could not add Server Group @group', array('@group' => $form['values']['new_servergroup'])));
+
+ if (isset($form_state['values']['existing_servers'])) {
+ remove_servers_in_server_group($form_state['values']['servergroup'], array_filter($form_state['values']['existing_servers']));
+ }
+
+ if (isset($form_state['values']['new_server'])) {
+ if (add_servers_in_server_group($form_state['values']['servergroup'], array_filter($form_state['values']['new_server'])) == false) {
+ form_set_error('new_server', t('Could not assign servers to group.'));
+ }
+ }
+
+ $form_state['rebuild'] = TRUE;
}
View
28 sites/all/modules/wayfair/vulnscan/vulnscan.module
@@ -4,15 +4,15 @@
* Wayfair Vulnerability Scanner module
*/
+require_once('helpers.inc');
+
/**
* Implements hook_permission().
*/
function vulnscan_permission() {
- /*
- * TODO:
- * 1) Add permission for scanning each server group
- */
- return array(
+ $groups = get_all_server_groups_assoc();
+
+ $perms = array(
'administer servers' => array(
'title' => t('Administer server groups'),
'description' => t('Create/Modify/Delete Server Groups'),
@@ -38,6 +38,14 @@ function vulnscan_permission() {
'description' => t('View other\'s scans'),
),
);
+
+ foreach ($groups as $key => $value) {
+ $perms['scan ' . $value] = array(
+ 'title' => t('Run scan for Server Group @group', array('@group' => $value)),
+ );
+ }
+
+ return $perms;
}
/**
@@ -66,6 +74,16 @@ function vulnscan_menu() {
'file' => 'vulnscan.admin.inc',
);
+ $items['admin/config/vulnscan/servergroups'] = array(
+ 'title' => 'Server Group settings.',
+ 'description' => 'Add/Modify/Delete Server Groups',
+ 'page callback' => 'drupal_get_form',
+ 'page arguments' => array('vulnscan_admin_settings_servergroup'),
+ 'access arguments' => array('administer site configuration'),
+ 'type' => MENU_NORMAL_ITEM,
+ 'file' => 'vulnscan.admin.inc',
+ );
+
/* Actions */
$items['vulnscan/scan'] = array(
'title' => 'Run Scan',

0 comments on commit 930f959

Please sign in to comment.
Something went wrong with that request. Please try again.