Greenbone Vulnerability Manager  22.4.0~dev1
Macros | Functions
manage_sql_port_lists.c File Reference

GVM management layer: Port list SQL. More...

#include "manage_sql_port_lists.h"
#include "manage_acl.h"
#include "manage_port_lists.h"
#include "sql.h"
#include <errno.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

Macros

#define G_LOG_DOMAIN   "md manage"
 GLib log domain.
 
#define RANGE(type, start, end)
 Insert a port range. More...
 
#define PORT_LIST_ITERATOR_FILTER_COLUMNS    { GET_ITERATOR_FILTER_COLUMNS, "total", "tcp", "udp", "predefined", NULL }
 Filter columns for Port List iterator.
 
#define PORT_LIST_ITERATOR_COLUMNS
 Port List iterator columns.
 
#define PORT_LIST_ITERATOR_TRASH_COLUMNS
 Port List iterator columns for trash case.
 

Functions

int sync_port_lists_with_feed (gboolean)
 Sync all port lists with the feed. More...
 
static void make_port_ranges_openvas_default (port_list_t list)
 Make port ranges. More...
 
gboolean find_port_list (const char *uuid, port_list_t *port_list)
 Find a port list given a UUID. More...
 
gboolean find_port_list_no_acl (const char *uuid, port_list_t *port_list)
 Find a port list given a UUID. More...
 
gboolean find_port_list_with_permission (const char *uuid, port_list_t *port_list, const char *permission)
 Find a port list for a specific permission, given a UUID. More...
 
gboolean find_trash_port_list_no_acl (const char *uuid, port_list_t *port_list)
 Find a trash port list given a UUID. More...
 
int port_list_predefined (port_list_t port_list)
 Return whether a port list is predefined. More...
 
int trash_port_list_predefined (port_list_t port_list)
 Return whether a trash port list is predefined. More...
 
static char * port_range_port_list_uuid (const char *port_range)
 Return the UUID of the port list of a port_range. More...
 
static gboolean find_port_range_with_permission (const char *uuid, port_range_t *port_range, const char *permission)
 Find a port range given a UUID. More...
 
static int range_compare (gconstpointer one, gconstpointer two)
 Compare two ranges by type then start. More...
 
static void ranges_sort_merge (array_t *ranges)
 Sort and merge ranges. More...
 
static int create_port_list_lock (const char *quoted_id, const char *quoted_name, const char *comment, array_t *ranges, int predefined, port_list_t *port_list)
 Create a port list, with database locked. More...
 
int create_port_list_unique (const char *name, const char *comment, const char *port_range, port_list_t *port_list)
 Create a port list having a unique name. More...
 
static int create_port_list_internal (int check_access, const char *id, const char *name, const char *comment, const char *port_ranges, array_t *ranges, int predefined, port_list_t *port_list_return)
 Create a port list. More...
 
int create_port_list (const char *id, const char *name, const char *comment, const char *port_ranges, array_t *ranges, port_list_t *port_list_return)
 Create a port list. More...
 
int create_port_list_no_acl (const char *id, const char *name, const char *comment, const char *port_ranges, array_t *ranges, port_list_t *port_list_return)
 Create a port list. More...
 
int copy_port_list (const char *name, const char *comment, const char *port_list_id, port_list_t *new_port_list)
 Create Port List from an existing Port List. More...
 
static int port_list_predefined_uuid (const gchar *port_list_id)
 Return whether a port list is predefined. More...
 
int modify_port_list (const char *port_list_id, const char *name, const char *comment)
 Modify a Port List. More...
 
int create_port_range (const char *port_list_id, const char *type, const char *start, const char *end, const char *comment, port_range_t *port_range_return)
 Create a port range in a port list. More...
 
int delete_port_list (const char *port_list_id, int ultimate)
 Delete a port list. More...
 
void insert_port_range (port_list_t port_list, port_protocol_t type, int start, int end)
 Create a port range. More...
 
int delete_port_range (const char *port_range_id, int dummy)
 Delete a port range. More...
 
const char ** port_list_filter_columns ()
 Get filter columns. More...
 
column_tport_list_select_columns ()
 Get select columns. More...
 
int port_list_count (const get_data_t *get)
 Count the number of Port Lists. More...
 
int init_port_list_iterator (iterator_t *iterator, const get_data_t *get)
 Initialise a Port List iterator, including observed Port Lists. More...
 
int port_list_iterator_count_all (iterator_t *iterator)
 Get the port count from a port_list iterator. More...
 
int port_list_iterator_count_tcp (iterator_t *iterator)
 Get the TCP port count from a port_list iterator. More...
 
int port_list_iterator_count_udp (iterator_t *iterator)
 Get the UDP port count from a port_list iterator. More...
 
int port_list_iterator_predefined (iterator_t *iterator)
 Get predefined status from a port_list iterator. More...
 
char * port_list_uuid (port_list_t port_list)
 Return the UUID of a port_list. More...
 
char * port_range_uuid (port_range_t port_range)
 Return the UUID of a port_range. More...
 
int port_list_in_use (port_list_t port_list)
 Return whether a port_list is in use by a task. More...
 
int trash_port_list_in_use (port_list_t port_list)
 Check whether a trashcan Port List is in use. More...
 
int port_list_writable (port_list_t port_list)
 Check whether a Port List is writable. More...
 
int trash_port_list_writable (port_list_t port_list)
 Check whether a trashcan Port List is writable. More...
 
int trash_port_list_readable_uuid (const gchar *port_list_id)
 Return whether a trashcan port list is readable. More...
 
void init_port_range_iterator (iterator_t *iterator, port_list_t port_list, int trash, int ascending, const char *sort_field)
 Initialise a port_range iterator. More...
 
const char * port_range_iterator_comment (iterator_t *iterator)
 Get the comment from a port range iterator. More...
 
const char * port_range_iterator_type (iterator_t *iterator)
 Get the type from a port range iterator. More...
 
port_protocol_t port_range_iterator_type_int (iterator_t *iterator)
 Get the type from a port range iterator. More...
 
void init_port_list_target_iterator (iterator_t *iterator, port_list_t port_list, int ascending)
 Initialise a port list target iterator. More...
 
int port_list_target_iterator_readable (iterator_t *iterator)
 Get the read permission status from a GET iterator. More...
 
int restore_port_list (const char *port_list_id)
 Try restore a port list. More...
 
void empty_trashcan_port_lists ()
 Empty trashcan.
 
void inherit_port_lists (user_t user, user_t inheritor)
 Change ownership of port lists, for user deletion. More...
 
void delete_port_lists_user (user_t user)
 Delete all port lists owned by a user. More...
 
void migrate_predefined_port_lists ()
 Migrate old ownerless port lists to the Feed Owner.
 
int port_list_updated_in_feed (port_list_t port_list, const gchar *path)
 Check if a port list has been updated in the feed. More...
 
void update_port_list (port_list_t port_list, const gchar *name, const gchar *comment, array_t *ranges)
 Update a port list from an XML file. More...
 
void check_db_port_lists ()
 Check port lists, for startup.
 
int cleanup_port_list_sequences ()
 Cleans up port list related id sequences likely to run out. More...
 

Detailed Description

GVM management layer: Port list SQL.

The Port List SQL for the GVM management layer.

Macro Definition Documentation

◆ RANGE

#define RANGE (   type,
  start,
  end 
)
Value:
sql ("INSERT INTO port_ranges" \
" (uuid, port_list, type, start, \"end\", comment, exclude)" \
" VALUES" \
" (make_uuid (), %llu, %i," \
" '" G_STRINGIFY (start) "'," \
" '" G_STRINGIFY (end) "'," \
" '', 0)", \
list, \
type)
void sql(char *sql,...)
Perform an SQL statement, retrying if database is busy or locked.
Definition: sql.c:269

Insert a port range.

Function Documentation

◆ cleanup_port_list_sequences()

int cleanup_port_list_sequences ( )

Cleans up port list related id sequences likely to run out.

Returns
0 success, -1 error.

◆ copy_port_list()

int copy_port_list ( const char *  name,
const char *  comment,
const char *  port_list_id,
port_list_t *  new_port_list 
)

Create Port List from an existing Port List.

Parameters
[in]nameName of new Port List. NULL to copy from existing.
[in]commentComment on new Port List. NULL to copy from existing.
[in]port_list_idUUID of existing Port List.
[out]new_port_listNew Port List.
Returns
0 success, 1 Port List exists already, 2 failed to find existing Port List, 99 permission denied, -1 error.

◆ create_port_list()

int create_port_list ( const char *  id,
const char *  name,
const char *  comment,
const char *  port_ranges,
array_t *  ranges,
port_list_t *  port_list_return 
)

Create a port list.

Parameters
[in]idID of port list. Only used with ranges.
[in]nameName of port list.
[in]commentComment on port list.
[in]port_rangesGMP port range string.
[in]rangesArray of port ranges of type range_t. Overrides port_ranges.
[out]port_list_returnCreated port list.
Returns
0 success, 1 port list exists already, 4 error in port_ranges, 99 permission denied, -1 error.

◆ create_port_list_internal()

static int create_port_list_internal ( int  check_access,
const char *  id,
const char *  name,
const char *  comment,
const char *  port_ranges,
array_t *  ranges,
int  predefined,
port_list_t *  port_list_return 
)
static

Create a port list.

Parameters
[in]check_accessWhether to check for create_config permission.
[in]idID of port list. Only used with ranges.
[in]nameName of port list.
[in]commentComment on port list.
[in]port_rangesGMP port range string.
[in]rangesArray of port ranges of type range_t. Overrides port_ranges.
[in]predefinedWhether port list is predefined.
[out]port_list_returnCreated port list.
Returns
0 success, 1 port list exists already, 4 error in port_ranges, 99 permission denied, -1 error.

◆ create_port_list_lock()

static int create_port_list_lock ( const char *  quoted_id,
const char *  quoted_name,
const char *  comment,
array_t *  ranges,
int  predefined,
port_list_t *  port_list 
)
static

Create a port list, with database locked.

Caller must lock the database.

Parameters
[in]quoted_idSQL quoted UUID, or NULL.
[in]quoted_nameSQL quoted name of port list.
[in]commentComment on port list.
[in]rangesPort ranges of port list.
[in]predefinedWhether port list is predefined.
[out]port_listCreated port list.
Returns
0 success.

◆ create_port_list_no_acl()

int create_port_list_no_acl ( const char *  id,
const char *  name,
const char *  comment,
const char *  port_ranges,
array_t *  ranges,
port_list_t *  port_list_return 
)

Create a port list.

Parameters
[in]idID of port list. Only used with ranges.
[in]nameName of port list.
[in]commentComment on port list.
[in]port_rangesGMP port range string.
[in]rangesArray of port ranges of type range_t. Overrides port_ranges.
[out]port_list_returnCreated port list.
Returns
0 success, 1 port list exists already, 4 error in port_ranges, 99 permission denied, -1 error.

◆ create_port_list_unique()

int create_port_list_unique ( const char *  name,
const char *  comment,
const char *  port_range,
port_list_t *  port_list 
)

Create a port list having a unique name.

Caller must provide transaction.

Parameters
[in]nameName of port list.
[in]commentComment on port list.
[in]port_rangeGMP style port range list.
[out]port_listCreated port list.
Returns
0 success, 4 error in port range.

◆ create_port_range()

int create_port_range ( const char *  port_list_id,
const char *  type,
const char *  start,
const char *  end,
const char *  comment,
port_range_t *  port_range_return 
)

Create a port range in a port list.

Parameters
[in]port_list_idPort list UUID.
[in]typeType.
[in]startStart port.
[in]endEnd port.
[in]commentComment.
[out]port_range_returnCreated port range.
Returns
0 success, 1 syntax error in start, 2 syntax error in end, 3 failed to find port list, 4 syntax error in type, 5 port list in use, 6 new range overlaps an existing range, 99 permission denied, -1 error.

◆ delete_port_list()

int delete_port_list ( const char *  port_list_id,
int  ultimate 
)

Delete a port list.

Parameters
[in]port_list_idUUID of port_list.
[in]ultimateWhether to remove entirely, or to trashcan.
Returns
0 success, 1 fail because a target refers to the port list, 2 failed to find port list, 99 permission denied, -1 error.

◆ delete_port_lists_user()

void delete_port_lists_user ( user_t  user)

Delete all port lists owned by a user.

Parameters
[in]userThe user.

◆ delete_port_range()

int delete_port_range ( const char *  port_range_id,
int  dummy 
)

Delete a port range.

Parameters
[in]port_range_idUUID of port_range.
[in]dummyDummy arg to match other delete functions.
Returns
0 success, 2 failed to find port range, 99 permission denied, -1 error.

◆ find_port_list()

gboolean find_port_list ( const char *  uuid,
port_list_t *  port_list 
)

Find a port list given a UUID.

Parameters
[in]uuidUUID of port_list.
[out]port_listPort_List return, 0 if successfully failed to find port_list.
Returns
FALSE on success (including if failed to find port_list), TRUE on error.

◆ find_port_list_no_acl()

gboolean find_port_list_no_acl ( const char *  uuid,
port_list_t *  port_list 
)

Find a port list given a UUID.

This does not do any permission checks.

Parameters
[in]uuidUUID of resource.
[out]port_listPort list return, 0 if no such port list.
Returns
FALSE on success (including if no such port list), TRUE on error.

◆ find_port_list_with_permission()

gboolean find_port_list_with_permission ( const char *  uuid,
port_list_t *  port_list,
const char *  permission 
)

Find a port list for a specific permission, given a UUID.

Parameters
[in]uuidUUID of port list.
[out]port_listPort list return, 0 if successfully failed to find port list.
[in]permissionPermission.
Returns
FALSE on success (including if failed to find port_list), TRUE on error.

◆ find_port_range_with_permission()

static gboolean find_port_range_with_permission ( const char *  uuid,
port_range_t *  port_range,
const char *  permission 
)
static

Find a port range given a UUID.

Parameters
[in]uuidUUID of port_range.
[out]port_rangePort range return, 0 if successfully failed to find port range.
[in]permissionUUID of port_range.
Returns
FALSE on success (including if failed to find port range), TRUE on error.

◆ find_trash_port_list_no_acl()

gboolean find_trash_port_list_no_acl ( const char *  uuid,
port_list_t *  port_list 
)

Find a trash port list given a UUID.

This does not do any permission checks.

Parameters
[in]uuidUUID of resource.
[out]port_listPort list return, 0 if no such port list.
Returns
FALSE on success (including if no such port list), TRUE on error.

◆ inherit_port_lists()

void inherit_port_lists ( user_t  user,
user_t  inheritor 
)

Change ownership of port lists, for user deletion.

Parameters
[in]userCurrent owner.
[in]inheritorNew owner.

◆ init_port_list_iterator()

int init_port_list_iterator ( iterator_t iterator,
const get_data_t get 
)

Initialise a Port List iterator, including observed Port Lists.

Parameters
[in]iteratorIterator.
[in]getGET data.
Returns
0 success, 1 failed to find Port List, 2 failed to find filter, -1 error.

◆ init_port_list_target_iterator()

void init_port_list_target_iterator ( iterator_t iterator,
port_list_t  port_list,
int  ascending 
)

Initialise a port list target iterator.

Parameters
[in]iteratorIterator.
[in]port_listPort list.
[in]ascendingWhether to sort ascending or descending.

◆ init_port_range_iterator()

void init_port_range_iterator ( iterator_t iterator,
port_list_t  port_list,
int  trash,
int  ascending,
const char *  sort_field 
)

Initialise a port_range iterator.

Parameters
[in]iteratorIterator.
[in]port_listPort list.
[in]trashWhether port_list is in the trashcan.
[in]ascendingWhether to sort ascending or descending.
[in]sort_fieldField to sort on, or NULL for type then start.

◆ insert_port_range()

void insert_port_range ( port_list_t  port_list,
port_protocol_t  type,
int  start,
int  end 
)

Create a port range.

Parameters
[in]port_listPort list to insert into.
[in]typeProtocol: PORT_PROTOCOL_UDP or PORT_PROTOCOL_TCP.
[in]startStart of range.
[in]endEnd of range.

◆ make_port_ranges_openvas_default()

static void make_port_ranges_openvas_default ( port_list_t  list)
static

Make port ranges.

Caller must lock the db.

Parameters
[in]listPort list.

◆ modify_port_list()

int modify_port_list ( const char *  port_list_id,
const char *  name,
const char *  comment 
)

Modify a Port List.

Parameters
[in]port_list_idUUID of Port List.
[in]nameName of Port List.
[in]commentComment on Port List.
Returns
0 success, 1 failed to find port list, 2 port list with new name, exists, 3 port_list_id required, 99 permission denied, -1 internal error.

◆ port_list_count()

int port_list_count ( const get_data_t get)

Count the number of Port Lists.

Parameters
[in]getGET params.
Returns
Total number of Port Lists filtered set.

◆ port_list_filter_columns()

const char** port_list_filter_columns ( )

Get filter columns.

Returns
Constant array of filter columns.

◆ port_list_in_use()

int port_list_in_use ( port_list_t  port_list)

Return whether a port_list is in use by a task.

Parameters
[in]port_listPort_List.
Returns
1 if in use, else 0.

◆ port_list_iterator_count_all()

int port_list_iterator_count_all ( iterator_t iterator)

Get the port count from a port_list iterator.

Parameters
[in]iteratorIterator.
Returns
Port count.

◆ port_list_iterator_count_tcp()

int port_list_iterator_count_tcp ( iterator_t iterator)

Get the TCP port count from a port_list iterator.

Parameters
[in]iteratorIterator.
Returns
TCP port count.

◆ port_list_iterator_count_udp()

int port_list_iterator_count_udp ( iterator_t iterator)

Get the UDP port count from a port_list iterator.

Parameters
[in]iteratorIterator.
Returns
UDP port count.

◆ port_list_iterator_predefined()

int port_list_iterator_predefined ( iterator_t iterator)

Get predefined status from a port_list iterator.

Parameters
[in]iteratorIterator.
Returns
1 if predefined, else 0.

◆ port_list_predefined()

int port_list_predefined ( port_list_t  port_list)

Return whether a port list is predefined.

Parameters
[in]port_listPort list.
Returns
1 if predefined, else 0.

◆ port_list_predefined_uuid()

static int port_list_predefined_uuid ( const gchar *  port_list_id)
static

Return whether a port list is predefined.

Parameters
[in]port_list_idUUID of port list.
Returns
1 if predefined, else 0.

◆ port_list_select_columns()

column_t* port_list_select_columns ( )

Get select columns.

Returns
Constant array of select columns.

◆ port_list_target_iterator_readable()

int port_list_target_iterator_readable ( iterator_t iterator)

Get the read permission status from a GET iterator.

Parameters
[in]iteratorIterator.
Returns
1 if may read, else 0.

◆ port_list_updated_in_feed()

int port_list_updated_in_feed ( port_list_t  port_list,
const gchar *  path 
)

Check if a port list has been updated in the feed.

Parameters
[in]pathFull path to port list XML in feed.
[in]port_listPort List.
Returns
1 if updated in feed, else 0.

◆ port_list_uuid()

char* port_list_uuid ( port_list_t  port_list)

Return the UUID of a port_list.

Parameters
[in]port_listPort_List.
Returns
Newly allocated UUID if available, else NULL.

◆ port_list_writable()

int port_list_writable ( port_list_t  port_list)

Check whether a Port List is writable.

Parameters
[in]port_listPort List.
Returns
1 yes, 0 no.

◆ port_range_iterator_comment()

const char* port_range_iterator_comment ( iterator_t iterator)

Get the comment from a port range iterator.

Parameters
[in]iteratorIterator.
Returns
The comment of the range, or NULL if iteration is complete. Freed by cleanup_iterator.

◆ port_range_iterator_type()

const char* port_range_iterator_type ( iterator_t iterator)

Get the type from a port range iterator.

Parameters
[in]iteratorIterator.
Returns
The type of the range, or NULL if iteration is complete. Freed by cleanup_iterator.

◆ port_range_iterator_type_int()

port_protocol_t port_range_iterator_type_int ( iterator_t iterator)

Get the type from a port range iterator.

Parameters
[in]iteratorIterator.
Returns
The type of the range, or NULL if iteration is complete. Freed by cleanup_iterator.

◆ port_range_port_list_uuid()

static char* port_range_port_list_uuid ( const char *  port_range)
static

Return the UUID of the port list of a port_range.

Parameters
[in]port_rangePort Range UUID.
Returns
Newly allocated UUID if available, else NULL.

◆ port_range_uuid()

char* port_range_uuid ( port_range_t  port_range)

Return the UUID of a port_range.

Parameters
[in]port_rangePort Range.
Returns
Newly allocated UUID if available, else NULL.

◆ range_compare()

static int range_compare ( gconstpointer  one,
gconstpointer  two 
)
static

Compare two ranges by type then start.

Parameters
[in]oneFirst range.
[in]twoSecond range.
Returns
0 equal, 1 one greater, -1 two greater.

◆ ranges_sort_merge()

static void ranges_sort_merge ( array_t *  ranges)
static

Sort and merge ranges.

Parameters
[in]rangesArray of port ranges of type range_t.

◆ restore_port_list()

int restore_port_list ( const char *  port_list_id)

Try restore a port list.

If success, ends transaction for caller before exiting.

Parameters
[in]port_list_idUUID of resource.
Returns
0 success, 1 fail because port list is in use, 2 failed to find port list, -1 error.

◆ sync_port_lists_with_feed()

int sync_port_lists_with_feed ( gboolean  rebuild)

Sync all port lists with the feed.

Create port lists that exists in the feed but not in the db. Update port lists in the db that have changed on the feed. Do nothing to db port lists that have been removed from the feed.

Parameters
[in]rebuildWhether ignore timestamps to force a rebuild.
Returns
0 success, 1 no feed directory, 2 no feed owner, -1 error.

◆ trash_port_list_in_use()

int trash_port_list_in_use ( port_list_t  port_list)

Check whether a trashcan Port List is in use.

Parameters
[in]port_listPort List.
Returns
1 yes, 0 no.

◆ trash_port_list_predefined()

int trash_port_list_predefined ( port_list_t  port_list)

Return whether a trash port list is predefined.

Parameters
[in]port_listPort list.
Returns
1 if predefined, else 0.

◆ trash_port_list_readable_uuid()

int trash_port_list_readable_uuid ( const gchar *  port_list_id)

Return whether a trashcan port list is readable.

Parameters
[in]port_list_idPort list UUID.
Returns
1 if readable, else 0.

◆ trash_port_list_writable()

int trash_port_list_writable ( port_list_t  port_list)

Check whether a trashcan Port List is writable.

Parameters
[in]port_listPort List.
Returns
1 yes, 0 no.

◆ update_port_list()

void update_port_list ( port_list_t  port_list,
const gchar *  name,
const gchar *  comment,
array_t *  ranges 
)

Update a port list from an XML file.

Parameters
[in]port_listExisting port list.
[in]nameNew name.
[in]commentNew comment.
[in]rangesNew port ranges.