Skip to content

Commit

Permalink
Merge pull request #1453 from timopollmeier/remove-c-sql-functions
Browse files Browse the repository at this point in the history
Use pg-gvm extension for C PostgreSQL functions
  • Loading branch information
bjoernricks authored Mar 18, 2021
2 parents a74b57e + 22c7bda commit 311a3a8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 53 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## [21.10] (unreleased)

### Added

### Changed
- Use pg-gvm extension for C PostgreSQL functions [#1400](https://github.com/greenbone/gvmd/pull/1400), [#1453](https://github.com/greenbone/gvmd/pull/1453)

### Fixed

### Removed

[21.4]: https://github.com/greenbone/gvmd/compare/gvmd-21.04...master
Expand Down
26 changes: 16 additions & 10 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,41 +125,47 @@ Certificates`.
apt install postgresql postgresql-contrib postgresql-server-dev-all
```

2. Run cmake and build gvmd as usual.
2. Install the pg-gvm extension.

3. Setup Postgres User and DB (`/usr/share/doc/postgresql-common/README.Debian.gz`)
Install the pg-gvm extension library (https://github.com/greenbone/pg-gvm).
For instructions on how to do this, see the README file there.

3. Run cmake and build gvmd as usual.

4. Setup Postgres User and DB (`/usr/share/doc/postgresql-common/README.Debian.gz`)

```sh
sudo -u postgres bash
createuser -DRS mattm # mattm is your OS login name
createdb -O mattm gvmd
```

4. Setup permissions.
5. Setup permissions.

```sh
sudo -u postgres bash # if you logged out after step 3
sudo -u postgres bash # if you logged out after step 4
psql gvmd
create role dba with superuser noinherit;
grant dba to mattm; # mattm is the user created in step 3
grant dba to mattm; # mattm is the user created in step 4
```

5. Create DB extensions (also necessary when the database got dropped).
6. Create DB extensions (also necessary when the database got dropped).

```sh
sudo -u postgres bash # if you logged out after step 4
sudo -u postgres bash # if you logged out after step 5
psql gvmd
create extension "uuid-ossp";
create extension "pgcrypto";
create extension "pg-gvm"; # if it is not installed in step 2.
```

6. Make Postgres aware of the gvm libraries if not installed
7. Make Postgres aware of the gvm libraries if not installed
in a ld-aware directory. For example create file `/etc/ld.so.conf.d/gvm.conf`
with appropriate path and then run `ldconfig`.

7. Run Manager as usual.
8. Run Manager as usual.

8. To run SQL on the database.
9. To run SQL on the database.

```sh
psql gvmd
Expand Down
58 changes: 15 additions & 43 deletions src/manage_pg.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,22 +194,23 @@ manage_create_sql_functions ()
return -1;
}

/* Functions in C. */
/* Functions in C have been moved to the "pg-gvm" extension. */

/* Operators */

sql ("SET role dba;");

sql ("CREATE OR REPLACE FUNCTION hosts_contains (text, text)"
" RETURNS boolean"
" AS '%s/libgvm-pg-server', 'sql_hosts_contains'"
" LANGUAGE C"
" IMMUTABLE;",
GVM_LIB_INSTALL_DIR);
if (sql_int ("SELECT count(*) FROM pg_operator"
" WHERE oprname = '?~#';")
== 0)
{
sql ("CREATE OPERATOR ?~#"
" (PROCEDURE = regexp, LEFTARG = text, RIGHTARG = text);");
}

sql ("RESET role;");

sql ("CREATE OR REPLACE FUNCTION max_hosts (text, text)"
" RETURNS integer"
" AS '%s/libgvm-pg-server', 'sql_max_hosts'"
" LANGUAGE C;",
GVM_LIB_INSTALL_DIR);
/* Functions in pl/pgsql. */

/*
* This database function is a duplicate of 'level_max_severity' from manage_utils.c
Expand Down Expand Up @@ -275,18 +276,6 @@ manage_create_sql_functions ()
"END;"
"$$ LANGUAGE plpgsql;");

sql ("CREATE OR REPLACE FUNCTION next_time_ical (text, text)"
" RETURNS integer"
" AS '%s/libgvm-pg-server', 'sql_next_time_ical'"
" LANGUAGE C;",
GVM_LIB_INSTALL_DIR);

sql ("CREATE OR REPLACE FUNCTION next_time_ical (text, text, integer)"
" RETURNS integer"
" AS '%s/libgvm-pg-server', 'sql_next_time_ical'"
" LANGUAGE C;",
GVM_LIB_INSTALL_DIR);

sql ("CREATE OR REPLACE FUNCTION severity_matches_ov (a double precision,"
" b double precision)"
"RETURNS BOOLEAN AS $$"
Expand All @@ -300,24 +289,6 @@ manage_create_sql_functions ()
"END;"
"$$ LANGUAGE plpgsql IMMUTABLE;");

sql ("CREATE OR REPLACE FUNCTION regexp (text, text)"
" RETURNS boolean"
" AS '%s/libgvm-pg-server', 'sql_regexp'"
" LANGUAGE C;",
GVM_LIB_INSTALL_DIR);

if (sql_int ("SELECT count(*) FROM pg_operator"
" WHERE oprname = '?~#';")
== 0)
{
sql ("CREATE OPERATOR ?~#"
" (PROCEDURE = regexp, LEFTARG = text, RIGHTARG = text);");
}

sql ("RESET role;");

/* Functions in pl/pgsql. */

/* Helper function for quoting the individual parts of multi-part
* identifiers like "scap", "cpes" and "id" in "scap.cpes.id" where
* necessary.
Expand Down Expand Up @@ -2946,7 +2917,8 @@ int
check_db_extensions ()
{
if (db_extension_installed ("uuid-ossp")
&& db_extension_installed ("pgcrypto"))
&& db_extension_installed ("pgcrypto")
&& db_extension_installed ("pg-gvm"))
{
g_debug ("%s: All required extensions are installed.", __func__);
return 0;
Expand Down

0 comments on commit 311a3a8

Please sign in to comment.