Skip to content

Commit d621909

Browse files
authored
Merge pull request #1980 from evgenyz/fix-systemd-probes
OVAL/probes/systemd*: Use ListUnitFiles D-Bus method to fetch all units
2 parents acdbe77 + ba71d89 commit d621909

File tree

6 files changed

+128
-8
lines changed

6 files changed

+128
-8
lines changed

src/OVAL/probes/unix/linux/systemdshared.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ static char *get_path_by_unit(DBusConnection *conn, const char *unit)
8585
// if it hasn't been loaded yet.
8686
"LoadUnit"
8787
);
88+
dD("LoadUnit: %s", unit);
89+
8890
if (msg == NULL) {
8991
dD("Failed to create dbus_message via dbus_message_new_method_call!");
9092
goto cleanup;
@@ -124,7 +126,7 @@ static char *get_path_by_unit(DBusConnection *conn, const char *unit)
124126
}
125127

126128
if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_OBJECT_PATH) {
127-
dD("Expected string argument in reply. Instead received: %s.", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&args)));
129+
dD("Expected object path argument in reply. Instead received: %s.", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&args)));
128130
goto cleanup;
129131
}
130132

@@ -152,7 +154,7 @@ static int get_all_systemd_units(DBusConnection* conn, int(*callback)(const char
152154
"org.freedesktop.systemd1",
153155
"/org/freedesktop/systemd1",
154156
"org.freedesktop.systemd1.Manager",
155-
"ListUnits"
157+
"ListUnitFiles"
156158
);
157159
if (msg == NULL) {
158160
dD("Failed to create dbus_message via dbus_message_new_method_call!");
@@ -201,17 +203,18 @@ static int get_all_systemd_units(DBusConnection* conn, int(*callback)(const char
201203
goto cleanup;
202204
}
203205

204-
DBusMessageIter unit_name;
205-
dbus_message_iter_recurse(&unit_iter, &unit_name);
206+
DBusMessageIter unit_full_path_and_name;
207+
dbus_message_iter_recurse(&unit_iter, &unit_full_path_and_name);
206208

207-
if (dbus_message_iter_get_arg_type(&unit_name) != DBUS_TYPE_STRING) {
208-
dD("Expected string as the first element in the unit struct. Instead received: %s.", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&unit_name)));
209+
if (dbus_message_iter_get_arg_type(&unit_full_path_and_name) != DBUS_TYPE_STRING) {
210+
dD("Expected string as the first element in the unit struct. Instead received: %s.", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&unit_full_path_and_name)));
209211
goto cleanup;
210212
}
211213

212214
_DBusBasicValue value;
213-
dbus_message_iter_get_basic(&unit_name, &value);
214-
char *unit_name_s = oscap_strdup(value.str);
215+
dbus_message_iter_get_basic(&unit_full_path_and_name, &value);
216+
char *unit_name_s = oscap_strdup(basename(value.str));
217+
oscap_strrm(unit_name_s, "@");
215218
int cbret = callback(unit_name_s, cbarg);
216219
free(unit_name_s);
217220
if (cbret != 0) {

src/common/util.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,24 @@ static inline char *oscap_strdup(const char *str) {
313313
#endif
314314
}
315315

316+
/**
317+
* Removes all occurrences of substr from str by shifting the contents to
318+
* the left. If string is NULL, does nothing.
319+
* @param str String we want to process
320+
* @param substr Sub-string we want to get rid of
321+
*/
322+
static inline void oscap_strrm(char *str, const char *substr) {
323+
if (str == NULL)
324+
return;
325+
326+
size_t sublen = strlen(substr);
327+
char *ptr = strstr(str, substr);
328+
while (ptr != NULL) {
329+
memmove(ptr, ptr+sublen, strlen(ptr) - sublen + 1);
330+
ptr = strstr(str, substr);
331+
}
332+
}
333+
316334
/// Just like strcmp except it's NULL-safe. Use the standard strcmp directly if possible.
317335
static inline int oscap_strcmp(const char *s1, const char *s2) {
318336
if (s1 == NULL) s1 = "";

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ add_subdirectory("sce")
3939
add_subdirectory("schemas")
4040
add_subdirectory("sources")
4141
add_subdirectory("utils")
42+
add_subdirectory("common")

tests/common/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
add_oscap_test_executable(test_oscap_util
2+
"test_oscap_util.c"
3+
)
4+
5+
add_oscap_test("test_oscap_util.sh")

tests/common/test_oscap_util.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2023 Red Hat Inc., Durham, North Carolina.
3+
* All Rights Reserved.
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*
19+
* Authors:
20+
* Evgenii Kolesnikov <[email protected]>
21+
*/
22+
23+
#ifdef HAVE_CONFIG_H
24+
#include <config.h>
25+
#endif
26+
27+
#include <stdio.h>
28+
#include <stdlib.h>
29+
#include <string.h>
30+
#include "common/util.h"
31+
32+
int test_oscap_strrm(void);
33+
34+
int test_oscap_strrm()
35+
{
36+
char str[] = "abcdef12345678def90";
37+
size_t len = strlen(str);
38+
39+
oscap_strrm(str, "0");
40+
if (strncmp(str, "abcdef12345678def9", len) != 0)
41+
return 1;
42+
43+
oscap_strrm(str, "abc");
44+
if (strcmp(str, "def12345678def9") != 0)
45+
return 2;
46+
47+
oscap_strrm(str, "def");
48+
if (strcmp(str, "123456789") != 0)
49+
return 3;
50+
51+
oscap_strrm(str, "5");
52+
if (strcmp(str, "12346789") != 0)
53+
return 4;
54+
55+
return 0;
56+
}
57+
58+
int main (int argc, char *argv[])
59+
{
60+
int retval = 0;
61+
62+
if ((retval = test_oscap_strrm()) != 0)
63+
return retval;
64+
65+
return retval;
66+
}

tests/common/test_oscap_util.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2023 Red Hat Inc., Durham, North Carolina.
4+
# All Rights Reserved.
5+
#
6+
# OpenScap Test Suite
7+
#
8+
# Authors:
9+
# Evgenii Kolesnikov <[email protected]>
10+
11+
. $builddir/tests/test_common.sh
12+
13+
# Test cases.
14+
15+
function test_oscap_util {
16+
./test_oscap_util
17+
}
18+
19+
# Testing.
20+
21+
test_init
22+
23+
if [ -z ${CUSTOM_OSCAP+x} ] ; then
24+
test_run "test_oscap_util" test_oscap_util
25+
fi
26+
27+
test_exit

0 commit comments

Comments
 (0)