Skip to content

Commit b50e1ba

Browse files
committed
Move a function to utils
1 parent 8cb70c5 commit b50e1ba

File tree

3 files changed

+41
-33
lines changed

3 files changed

+41
-33
lines changed

src/OVAL/probes/probe-api.c

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,45 +1795,13 @@ SEXP_t *probe_obj_getmask(SEXP_t *obj)
17951795
return (mask);
17961796
}
17971797

1798-
static bool path_startswith(const char *path, const char *prefix)
1799-
{
1800-
bool res = true;
1801-
const char *del = "/";
1802-
char *path_dup = oscap_strdup(path);
1803-
char **path_split = oscap_split(path_dup, del);
1804-
char *prefix_dup = oscap_strdup(prefix);
1805-
char **prefix_split = oscap_split(prefix_dup, del);
1806-
int i = 0, j = 0;
1807-
while (prefix_split[i] && path_split[j]) {
1808-
if (!strcmp(prefix_split[i], "")) {
1809-
++i;
1810-
continue;
1811-
}
1812-
if (!strcmp(path_split[j], "")) {
1813-
++j;
1814-
continue;
1815-
}
1816-
if (strcmp(prefix_split[i], path_split[j])) {
1817-
res = false;
1818-
break;
1819-
}
1820-
++i;
1821-
++j;
1822-
}
1823-
free(path_dup);
1824-
free(path_split);
1825-
free(prefix_dup);
1826-
free(prefix_split);
1827-
return res;
1828-
}
1829-
18301798
bool probe_path_is_blocked(const char *path, struct oscap_list *blocked_paths)
18311799
{
18321800
bool res = false;
18331801
struct oscap_iterator *it = oscap_iterator_new(blocked_paths);
18341802
while (oscap_iterator_has_more(it)) {
18351803
const char *item = oscap_iterator_next(it);
1836-
if (path_startswith(path, item)) {
1804+
if (oscap_path_startswith(path, item)) {
18371805
res = true;
18381806
break;
18391807
}

src/common/util.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,3 +442,35 @@ int oscap_open_writable(const char *filename)
442442
}
443443
return fd;
444444
}
445+
446+
bool oscap_path_startswith(const char *path, const char *prefix)
447+
{
448+
bool res = true;
449+
const char *del = "/";
450+
char *path_dup = oscap_strdup(path);
451+
char **path_split = oscap_split(path_dup, del);
452+
char *prefix_dup = oscap_strdup(prefix);
453+
char **prefix_split = oscap_split(prefix_dup, del);
454+
int i = 0, j = 0;
455+
while (prefix_split[i] && path_split[j]) {
456+
if (!strcmp(prefix_split[i], "")) {
457+
++i;
458+
continue;
459+
}
460+
if (!strcmp(path_split[j], "")) {
461+
++j;
462+
continue;
463+
}
464+
if (strcmp(prefix_split[i], path_split[j])) {
465+
res = false;
466+
break;
467+
}
468+
++i;
469+
++j;
470+
}
471+
free(path_dup);
472+
free(path_split);
473+
free(prefix_dup);
474+
free(prefix_split);
475+
return res;
476+
}

src/common/util.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,4 +540,12 @@ char *oscap_windows_error_message(unsigned long error_code);
540540
*/
541541
int oscap_open_writable(const char *filename);
542542

543+
/**
544+
* Check if a path starts with the given prefix
545+
* @param path file system path
546+
* @param prefix file system path that will be tested if it's a prefix of the path parameter
547+
* @return true or false
548+
*/
549+
bool oscap_path_startswith(const char *path, const char *prefix);
550+
543551
#endif /* OSCAP_UTIL_H_ */

0 commit comments

Comments
 (0)