Skip to content

Commit

Permalink
Change: Extend get_feeds GMP command.
Browse files Browse the repository at this point in the history
Added information on whether the feed owner and feed import roles
are set and whether the user has access to feed resources.
  • Loading branch information
a-h-abdelsalam committed Oct 8, 2024
1 parent 04071fa commit 5a7e62c
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -12976,6 +12976,7 @@ static void
handle_get_feeds (gmp_parser_t *gmp_parser, GError **error)
{
assert (current_credentials.username);
assert (current_credentials.uuid);

Check warning on line 12979 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L12979

Added line #L12979 was not covered by tests

if (acl_user_may ("get_feeds") == 0)
{
Expand All @@ -12986,10 +12987,59 @@ handle_get_feeds (gmp_parser_t *gmp_parser, GError **error)
return;
}

char *feed_owner_uuid, *feed_roles;
gboolean feed_owner_set, feed_import_roles_set, feed_resources_access;

feed_owner_set = feed_import_roles_set = feed_resources_access = FALSE;

Check warning on line 12993 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L12993

Added line #L12993 was not covered by tests

setting_value (SETTING_UUID_FEED_IMPORT_OWNER, &feed_owner_uuid);

Check warning on line 12995 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L12995

Added line #L12995 was not covered by tests

if (feed_owner_uuid != NULL && strlen (feed_owner_uuid) > 0)
feed_owner_set = TRUE;

Check warning on line 12998 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L12998

Added line #L12998 was not covered by tests
else
g_warning ("%s: No feed owner set.", __func__);

Check warning on line 13000 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13000

Added line #L13000 was not covered by tests

setting_value (SETTING_UUID_FEED_IMPORT_ROLES, &feed_roles);

Check warning on line 13002 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13002

Added line #L13002 was not covered by tests

if (feed_roles != NULL && strlen (feed_roles) > 0)
feed_import_roles_set = TRUE;

Check warning on line 13005 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13005

Added line #L13005 was not covered by tests
else
g_warning ("%s: No feed import roles set.", __func__);

Check warning on line 13007 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13007

Added line #L13007 was not covered by tests

if (feed_owner_uuid != NULL && strcmp (feed_owner_uuid, current_credentials.uuid) == 0)
feed_resources_access = TRUE;
else if (feed_roles != NULL)

Check warning on line 13011 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13010-L13011

Added lines #L13010 - L13011 were not covered by tests
{
gchar **roles = g_strsplit (feed_roles, ",", -1);
gchar **role = roles;

Check warning on line 13014 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13013-L13014

Added lines #L13013 - L13014 were not covered by tests
while (*role)
{
if (acl_user_has_role (current_credentials.uuid, *role))
{
feed_resources_access = TRUE;
break;

Check warning on line 13020 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13019-L13020

Added lines #L13019 - L13020 were not covered by tests
}
role++;

Check warning on line 13022 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13022

Added line #L13022 was not covered by tests
}
g_strfreev (roles);

Check warning on line 13024 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13024

Added line #L13024 was not covered by tests
}

free (feed_roles);
free (feed_owner_uuid);

Check warning on line 13028 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13027-L13028

Added lines #L13027 - L13028 were not covered by tests

SEND_TO_CLIENT_OR_FAIL ("<get_feeds_response"
" status=\"" STATUS_OK "\""
" status_text=\"" STATUS_OK_TEXT "\">");

SENDF_TO_CLIENT_OR_FAIL ("<feed_owner_set>%s</feed_owner_set>",

Check warning on line 13034 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13034

Added line #L13034 was not covered by tests
feed_owner_set ? "1" : "0");

SENDF_TO_CLIENT_OR_FAIL ("<feed_roles_set>%s</feed_roles_set>",

Check warning on line 13037 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13037

Added line #L13037 was not covered by tests
feed_import_roles_set ? "1" : "0");

SENDF_TO_CLIENT_OR_FAIL ("<feed_resources_access>%s</feed_resources_access>",

Check warning on line 13040 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13040

Added line #L13040 was not covered by tests
feed_resources_access ? "1" : "0");

if ((get_feeds_data->type == NULL)
|| (strcasecmp (get_feeds_data->type, "nvt") == 0))
get_feed (gmp_parser, error, NVT_FEED);
Expand Down
29 changes: 29 additions & 0 deletions src/manage_acl.c
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,35 @@ acl_user_is_user (const char *uuid)
return ret;
}

/**
* @brief Check whether a user has a given role.
*
* @param[in] user_uuid UUID of the user.
* @param[in] role_uuid UUID of the role.
*
* @return 1 if user has the given role, else 0.
*/
int
acl_user_has_role (const char *user_uuid, const char *role_uuid)

Check warning on line 474 in src/manage_acl.c

View check run for this annotation

Codecov / codecov/patch

src/manage_acl.c#L474

Added line #L474 was not covered by tests
{
int ret;
gchar *quoted_role_uuid, *quoted_user_uuid;

quoted_role_uuid = sql_quote (role_uuid);
quoted_user_uuid = sql_quote (user_uuid);

Check warning on line 480 in src/manage_acl.c

View check run for this annotation

Codecov / codecov/patch

src/manage_acl.c#L479-L480

Added lines #L479 - L480 were not covered by tests

ret = sql_int ("SELECT count (*) FROM role_users"

Check warning on line 482 in src/manage_acl.c

View check run for this annotation

Codecov / codecov/patch

src/manage_acl.c#L482

Added line #L482 was not covered by tests
" WHERE role = (SELECT id FROM roles"
" WHERE uuid = '%s')"
" AND \"user\" = (SELECT id FROM users WHERE uuid = '%s');",
quoted_role_uuid, quoted_user_uuid);

g_free (quoted_role_uuid);
g_free (quoted_user_uuid);
return ret;

Check warning on line 490 in src/manage_acl.c

View check run for this annotation

Codecov / codecov/patch

src/manage_acl.c#L488-L490

Added lines #L488 - L490 were not covered by tests
}


/* TODO This is only predicatable for unique fields like "id". If the field
* is "name" then "SELECT ... format" will choose arbitrarily between
* the resources that have the same name. */
Expand Down
3 changes: 3 additions & 0 deletions src/manage_acl.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ acl_user_is_super_admin (const char *);
int
acl_user_is_observer (const char *);

int
acl_user_has_role (const char *, const char *);

int
acl_user_owns (const char *, resource_t, int);

Expand Down
21 changes: 21 additions & 0 deletions src/schema_formats/XML/GMP.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -11525,8 +11525,26 @@ END:VCALENDAR
<type>text</type>
<required>1</required>
</attrib>
<e>feed_owner_set</e>
<e>feed_roles_set</e>
<e>feed_resources_access</e>
<any><e>feed</e></any>
</pattern>
<ele>
<name>feed_owner_set</name>
<summary>Whether the feed owner is set</summary>
<pattern><t>boolean</t></pattern>
</ele>
<ele>
<name>feed_roles_set</name>
<summary>Whether the feed roles are set</summary>
<pattern><t>boolean</t></pattern>
</ele>
<ele>
<name>feed_resources_access</name>
<summary>Whether the user has access to feed resources</summary>
<pattern><t>boolean</t></pattern>
</ele>
<ele>
<name>feed</name>
<pattern>
Expand Down Expand Up @@ -11590,6 +11608,9 @@ END:VCALENDAR
</request>
<response>
<get_feeds_response status_text="OK" status="200">
<feed_owner_set>1</feed_owner_set>
<feed_roles_set>1</feed_roles_set>
<feed_resources_access>1</feed_resources_access>
<feed>
<type>NVT</type>
<name>Greenbone Security Feed</name>
Expand Down

0 comments on commit 5a7e62c

Please sign in to comment.