diff --git a/.clang-format b/.clang-format index a494e22..4884c7f 100644 --- a/.clang-format +++ b/.clang-format @@ -29,7 +29,7 @@ BreakBeforeTernaryOperators: true BreakConstructorInitializersBeforeComma: false #uncomment for clang 3.9 #BreakStringLiterals: false -ColumnLimit: 100 +ColumnLimit: 120 CommentPragmas: '\*\<' ConstructorInitializerAllOnOneLineOrOnePerLine: false ConstructorInitializerIndentWidth: 4 diff --git a/.travis.yml b/.travis.yml index 8e58d9a..6b2d555 100644 --- a/.travis.yml +++ b/.travis.yml @@ -39,7 +39,7 @@ before_install: script: - lcov --version | grep "1.10" - export CC="gcc-5" - - ./configure --enable-coverage && make && make check && make distcheck && make check-valgrind + - ./configure --enable-coverage && make && make check && make distcheck - clang-format-3.8 -i $(find . -name '*.[ch]') && git diff --exit-code diff --git a/Makefile.am b/Makefile.am index 5257fc2..456b924 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,6 +2,10 @@ include $(top_srcdir)/common.mk ACLOCAL_AMFLAGS = -I m4 +if COVERAGE +AM_CFLAGS += --coverage +endif + EXTRA_DIST = ${top_srcdir}/README.md \ ${top_srcdir}/LICENSE \ ${top_srcdir}/common.mk \ @@ -18,15 +22,20 @@ EXTRA_DIST = ${top_srcdir}/README.md \ ${top_srcdir}/tests/dummy_data/eopkg/files/security/cve-2013-4459.nopatch \ ${top_srcdir}/tests/dummy_data/pkgbuild/PKGBUILD \ ${top_srcdir}/tests/dummy_data/pkgbuild/cve-2014-5461.patch \ - ${top_srcdir}/test-init.sh + ${top_srcdir}/test-init.sh \ + ${top_srcdir}/data/packages.template NULL = CLEANFILES = -SUBDIRS = \ - data \ - src \ - docs +dist_man_MANS = \ + docs/cve-check-tool.1 + +# Install the template file to disk +templatedir=$(datadir)/cve-check-tool/ +template_DATA = data/packages.template + +include src/Makefile.am if COVERAGE coverage: diff --git a/configure.ac b/configure.ac index 79c3542..9dab298 100644 --- a/configure.ac +++ b/configure.ac @@ -83,11 +83,7 @@ fi AM_CONDITIONAL([RELATIVE_PLUGINS], [test "$enable_relative_plugins" = "yes"]) -AC_CONFIG_FILES([Makefile - data/Makefile - docs/Makefile - src/Makefile - src/plugins/Makefile]) +AC_CONFIG_FILES([Makefile]) AC_OUTPUT diff --git a/data/Makefile.am b/data/Makefile.am deleted file mode 100644 index 4535218..0000000 --- a/data/Makefile.am +++ /dev/null @@ -1,9 +0,0 @@ -TEMPLATE_FILES = \ - packages.template - -EXTRA_DIST = \ - $(TEMPLATE_FILES) \ - $(DEFAULT_FILES) - -templatedir=$(datadir)/cve-check-tool/ -template_DATA = $(TEMPLATE_FILES) diff --git a/docs/Makefile.am b/docs/Makefile.am deleted file mode 100644 index 5ff869c..0000000 --- a/docs/Makefile.am +++ /dev/null @@ -1,5 +0,0 @@ -include $(top_srcdir)/common.mk - - -dist_man_MANS = \ - cve-check-tool.1 diff --git a/src/Makefile.am b/src/Makefile.am index 00f206c..252ae1f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,12 +1,3 @@ -include $(top_srcdir)/common.mk - -if COVERAGE -AM_CFLAGS += --coverage -endif - -SUBDIRS = . \ - plugins - bin_PROGRAMS = \ cve-check-tool \ cve-check-update @@ -15,23 +6,23 @@ noinst_LTLIBRARIES = \ libcve.la libcve_la_SOURCES = \ - library/common.h \ - library/cve-check-tool.h \ - library/cve-string.h \ - library/cve-string.c \ - library/hashmap.h \ - library/hashmap.c \ - library/cve-db-lock.h \ - library/cve-db-lock.c \ - library/fetch.c \ - library/fetch.h \ - library/template.c \ - library/template.h \ - library/util.c \ - library/util.h \ - plugins/packaging/faux/faux.h \ - core.h \ - core.c + src/library/common.h \ + src/library/cve-check-tool.h \ + src/library/cve-string.h \ + src/library/cve-string.c \ + src/library/hashmap.h \ + src/library/hashmap.c \ + src/library/cve-db-lock.h \ + src/library/cve-db-lock.c \ + src/library/fetch.c \ + src/library/fetch.h \ + src/library/template.c \ + src/library/template.h \ + src/library/util.c \ + src/library/util.h \ + src/plugins/packaging/faux/faux.h \ + src/core.h \ + src/core.c libcve_la_CFLAGS = \ $(CVE_CHECK_TOOL_CFLAGS) \ @@ -41,12 +32,12 @@ libcve_la_LIBADD = \ $(CVE_CHECK_TOOL_LIBS) cve_check_tool_SOURCES = \ - main.c \ - update.h \ - update.c \ - plugin-manager.c \ - plugin-manager.h \ - plugin.h + src/main.c \ + src/update.h \ + src/update.c \ + src/plugin-manager.c \ + src/plugin-manager.h \ + src/plugin.h cve_check_tool_CFLAGS = \ @@ -60,9 +51,9 @@ cve_check_tool_LDADD = \ -ldl cve_check_update_SOURCES = \ - update.c \ - update.h \ - update-main.c + src/update.c \ + src/update.h \ + src/update-main.c cve_check_update_CFLAGS = \ @@ -74,3 +65,5 @@ cve_check_update_LDADD = \ $(CVE_CHECK_TOOL_LIBS) \ libcve.la \ -ldl + +include src/plugins/Makefile.am diff --git a/src/core.c b/src/core.c index 727ab8c..4389d9b 100644 --- a/src/core.c +++ b/src/core.c @@ -12,25 +12,25 @@ #define _GNU_SOURCE #include "config.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include -#include #include -#include -#include -#include -#include -#include #include -#include -#include -#include -#include "cve-check-tool.h" #include "core.h" +#include "cve-check-tool.h" -#include "util.h" #include "cve-string.h" +#include "util.h" #include @@ -87,7 +87,8 @@ static bool ensure_table(CveDB *self) return false; } - query = "CREATE TABLE IF NOT EXISTS " TABLE_NAME " " + query = "CREATE TABLE IF NOT EXISTS " TABLE_NAME + " " "(ID TEXT UNIQUE, SUMMARY TEXT, SCORE TEXT, MODIFIED INTEGER, VECTOR TEXT);"; rc = sqlite3_exec(self->db, query, NULL, NULL, &err); if (rc != SQLITE_OK) { @@ -96,7 +97,9 @@ static bool ensure_table(CveDB *self) return false; } - query = "CREATE TABLE IF NOT EXISTS PRODUCTS (HASH INTEGER UNIQUE, ID TEXT, VENDOR TEXT, PRODUCT TEXT, VERSION TEXT);"; + query = + "CREATE TABLE IF NOT EXISTS PRODUCTS (HASH INTEGER UNIQUE, ID TEXT, VENDOR TEXT, PRODUCT TEXT, VERSION " + "TEXT);"; rc = sqlite3_exec(self->db, query, NULL, NULL, &err); if (rc != SQLITE_OK) { fprintf(stderr, "ensure_table(): %s\n", err); @@ -139,11 +142,11 @@ struct cve_entry_t *cve_db_get_cve(CveDB *self, char *id) return NULL; } - t->id = g_strdup((const char*)sqlite3_column_text(self->get_cve, 0)); - t->summary = g_strdup((const char*)sqlite3_column_text(self->get_cve, 1)); - t->score = g_strdup((const char*)sqlite3_column_text(self->get_cve, 2)); + t->id = g_strdup((const char *)sqlite3_column_text(self->get_cve, 0)); + t->summary = g_strdup((const char *)sqlite3_column_text(self->get_cve, 1)); + t->score = g_strdup((const char *)sqlite3_column_text(self->get_cve, 2)); t->modified = sqlite3_column_int64(self->get_cve, 3); - t->vector = g_strdup((const char*)sqlite3_column_text(self->get_cve, 4)); + t->vector = g_strdup((const char *)sqlite3_column_text(self->get_cve, 4)); return t; } @@ -173,7 +176,8 @@ GList *cve_db_get_issues_frac_compare(CveDB *self, char *product, char *version) ret = strverscmp(version, (const char *)sqlite3_column_text(self->search_product, 2)); if (ret <= 0) { /* our version <= NVD version */ - list = g_list_append(list, g_strdup((const char*)sqlite3_column_text(self->search_product, 0))); + list = + g_list_append(list, g_strdup((const char *)sqlite3_column_text(self->search_product, 0))); } } @@ -187,7 +191,7 @@ GList *cve_db_get_issues_frac_compare(CveDB *self, char *product, char *version) return list; } -GList *cve_db_get_issues(CveDB *self, char *product, char *version) +GList *cve_db_get_issues(CveDB *self, char *product, char *version) { int rc = 0; GList *list = NULL; @@ -210,7 +214,7 @@ GList *cve_db_get_issues(CveDB *self, char *product, char *version) } while ((rc = sqlite3_step(self->search_product) == SQLITE_ROW)) { - list = g_list_append(list, g_strdup((const gchar*)sqlite3_column_text(self->search_product, 0))); + list = g_list_append(list, g_strdup((const gchar *)sqlite3_column_text(self->search_product, 0))); } if (rc != SQLITE_OK) { fprintf(stderr, "cve_db_get_issues(): %s\n", sqlite3_errmsg(self->db)); @@ -246,7 +250,7 @@ static inline void free_vuln(struct vulnerability_t *t) * @param vuln Where to store the resulting vulnerability data * @return a boolean value, true if the operation succeeded */ -static bool parse_vuln(char *cve_id, const xmlChar* inp, struct vulnerability_t *vuln) +static bool parse_vuln(char *cve_id, const xmlChar *inp, struct vulnerability_t *vuln) { gchar *product = NULL; gchar *vendor = NULL; @@ -255,7 +259,7 @@ static bool parse_vuln(char *cve_id, const xmlChar* inp, struct vulnerability_t int len = 0; /* Example: cpe:/a:oracle:siebel_crm:8.1.1 */ - gchar **splits = g_strsplit((const gchar*)inp, ":", 10); + gchar **splits = g_strsplit((const gchar *)inp, ":", 10); if ((len = g_strv_length(splits)) < 4) { g_strfreev(splits); return false; @@ -321,7 +325,7 @@ static void process_node(CveDB *self, xmlTextReaderPtr r) { const xmlChar *name = NULL; const xmlChar *value = NULL; - struct vulnerability_t vuln = {.vendor = 0}; + struct vulnerability_t vuln = {.vendor = 0 }; xmlChar *uri = NULL; int64_t last_mod = -1; @@ -334,22 +338,25 @@ static void process_node(CveDB *self, xmlTextReaderPtr r) self->in_entry = !self->in_entry; if (!self->in_entry) { int rc = 0; - last_mod = parse_xml_date((char*)self->modified); + last_mod = parse_xml_date((char *)self->modified); sqlite3_reset(self->insert); /* ID */ - if (sqlite3_bind_text(self->insert, 1, (const char*)self->cur_id, -1, SQLITE_STATIC) != SQLITE_OK) { + if (sqlite3_bind_text(self->insert, 1, (const char *)self->cur_id, -1, SQLITE_STATIC) != + SQLITE_OK) { fprintf(stderr, "process_node(): %s\n", sqlite3_errmsg(self->db)); goto next; } /* SUMMARY */ - if (sqlite3_bind_text(self->insert, 2, (const char*)self->summary, -1, SQLITE_STATIC) != SQLITE_OK) { + if (sqlite3_bind_text(self->insert, 2, (const char *)self->summary, -1, SQLITE_STATIC) != + SQLITE_OK) { fprintf(stderr, "process_node(): %s\n", sqlite3_errmsg(self->db)); goto next; } /* SCORE */ - if (sqlite3_bind_text(self->insert, 3, (const char*)self->score, -1, SQLITE_STATIC) != SQLITE_OK) { + if (sqlite3_bind_text(self->insert, 3, (const char *)self->score, -1, SQLITE_STATIC) != + SQLITE_OK) { fprintf(stderr, "process_node(): %s\n", sqlite3_errmsg(self->db)); goto next; } @@ -359,7 +366,8 @@ static void process_node(CveDB *self, xmlTextReaderPtr r) goto next; } /* VECTOR */ - if (sqlite3_bind_text(self->insert, 5, (const char*)self->vector, -1, SQLITE_STATIC) != SQLITE_OK) { + if (sqlite3_bind_text(self->insert, 5, (const char *)self->vector, -1, SQLITE_STATIC) != + SQLITE_OK) { fprintf(stderr, "process_node(): %s\n", sqlite3_errmsg(self->db)); goto next; } @@ -368,14 +376,14 @@ static void process_node(CveDB *self, xmlTextReaderPtr r) if (rc != SQLITE_DONE) { fprintf(stderr, "process_node(): %s\n", sqlite3_errmsg(self->db)); } -next: + next: _cve_db_clean(self); return; } if (self->cur_id) { xmlFree(self->cur_id); } - self->cur_id = xmlTextReaderGetAttribute(r, BAD_CAST "id"); + self->cur_id = xmlTextReaderGetAttribute(r, BAD_CAST "id"); return; } if (xmlStrEqual(name, BAD_CAST "vuln:references")) { @@ -446,7 +454,7 @@ static void process_node(CveDB *self, xmlTextReaderPtr r) if (!value) { return; } - if (!parse_vuln((char*) self->cur_id, value, &vuln)) { + if (!parse_vuln((char *)self->cur_id, value, &vuln)) { return; } @@ -459,7 +467,8 @@ static void process_node(CveDB *self, xmlTextReaderPtr r) } /* ID */ - if (sqlite3_bind_text(self->insert_product, 2, (const char*)self->cur_id, -1, SQLITE_STATIC) != SQLITE_OK) { + if (sqlite3_bind_text(self->insert_product, 2, (const char *)self->cur_id, -1, SQLITE_STATIC) != + SQLITE_OK) { fprintf(stderr, "process_node(): %s\n", sqlite3_errmsg(self->db)); goto end_product; } @@ -485,7 +494,7 @@ static void process_node(CveDB *self, xmlTextReaderPtr r) fprintf(stderr, "process_node(): %s\n", sqlite3_errmsg(self->db)); } -end_product: + end_product: free_vuln(&vuln); return; } @@ -512,7 +521,7 @@ static void process_node(CveDB *self, xmlTextReaderPtr r) bool cve_db_load(CveDB *self, const char *fname) { bool b = false; - __attribute__ ((unused)) int rc; + __attribute__((unused)) int rc; if (!self || !fname) { return false; @@ -664,26 +673,26 @@ CveDB *cve_db_new(const char *path) void cve_db_free(CveDB *self) { - if (!self) { - return; - } - if (self->insert) { - sqlite3_finalize(self->insert); - } - if (self->insert_product) { - sqlite3_finalize(self->insert_product); - } - if (self->search_product) { - sqlite3_finalize(self->search_product); - } - if (self->get_cve) { - sqlite3_finalize(self->get_cve); - } - if (self->db) { - sqlite3_close(self->db); - } - _cve_db_clean(self); - free(self); + if (!self) { + return; + } + if (self->insert) { + sqlite3_finalize(self->insert); + } + if (self->insert_product) { + sqlite3_finalize(self->insert_product); + } + if (self->search_product) { + sqlite3_finalize(self->search_product); + } + if (self->get_cve) { + sqlite3_finalize(self->get_cve); + } + if (self->db) { + sqlite3_close(self->db); + } + _cve_db_clean(self); + free(self); } /* diff --git a/src/core.h b/src/core.h index 48e295d..69c4b0b 100644 --- a/src/core.h +++ b/src/core.h @@ -10,11 +10,11 @@ */ #pragma once -#include #include "common.h" +#include -extern const char *nvd_file; /* nvd.db */ -extern const char *nvd_dir; /* NVDS */ +extern const char *nvd_file; /* nvd.db */ +extern const char *nvd_dir; /* NVDS */ extern bool use_frac_compare; /** @@ -54,7 +54,6 @@ bool cve_db_finalize(CveDB *db); */ bool cve_db_begin(CveDB *db); - /** * Load the NVD XML file into this DB * diff --git a/src/library/common.h b/src/library/common.h index ee5663c..eeedabf 100644 --- a/src/library/common.h +++ b/src/library/common.h @@ -43,13 +43,13 @@ struct vulnerability_t { * Simple representation of a CVE */ struct cve_entry_t { - gchar *id; /** #include #include -#include #include "common.h" @@ -27,39 +27,39 @@ * * @param path Full legal path to the source package */ - + typedef void (*cve_add_callback)(const char *); /** * Instance tracking */ typedef struct CveCheckTool { - int64_t modified; /** -#include +#include +#include +#include #include #include #include #include -#include -#include -#include #include -#include +#include +#include +#include #include "core.h" -#include "util.h" -#include "cve-string.h" #include "cve-db-lock.h" +#include "cve-string.h" +#include "util.h" static const short int locktype2l_type[LT_MAX + 1] = { - [LT_READ] = F_RDLCK, - [LT_WRITE] = F_WRLCK, + [LT_READ] = F_RDLCK, [LT_WRITE] = F_WRLCK, }; static const char locktype2string[LT_MAX + 1][sizeof("write")] = { - [LT_READ] = "read", - [LT_WRITE] = "write", + [LT_READ] = "read", [LT_WRITE] = "write", }; static int db_lock_fd = -1; static cve_string *db_lock_fname; #ifndef O_NOFOLLOW -#define O_NOFOLLOW 0 +#define O_NOFOLLOW 0 #endif bool cve_db_lock_init(const char *db_path) { - const int flags = O_RDWR|O_CREAT|O_NONBLOCK|O_NOFOLLOW; - const mode_t mode = S_IRUSR|S_IWUSR; + const int flags = O_RDWR | O_CREAT | O_NONBLOCK | O_NOFOLLOW; + const mode_t mode = S_IRUSR | S_IWUSR; assert(db_lock_fd < 0); assert(db_lock_fname == NULL); @@ -100,8 +98,7 @@ bool cve_db_lock(locktype lt, int wait) do { struct flock fl = { - .l_type = locktype2l_type[lt], - .l_whence = SEEK_SET, + .l_type = locktype2l_type[lt], .l_whence = SEEK_SET, }; int ret; @@ -110,9 +107,7 @@ bool cve_db_lock(locktype lt, int wait) return true; } if (errno != EAGAIN && errno != EACCES) { - fprintf(stderr, - "Error acquiring database lock: %s\n", - strerror(errno)); + fprintf(stderr, "Error acquiring database lock: %s\n", strerror(errno)); break; } @@ -123,21 +118,18 @@ bool cve_db_lock(locktype lt, int wait) if (wait) { int remaining = wait - waited; if (remaining <= 0) { - fprintf(stderr, - "; %s lock is not acquired\n", lt_str); + fprintf(stderr, "; %s lock is not acquired\n", lt_str); break; } - fprintf(stderr, - "; acquiring %s lock within %ds ...", - lt_str, remaining); + fprintf(stderr, "; acquiring %s lock within %ds ...", lt_str, remaining); } else { fputs("; waiting indefinitely", stderr); } fputc('\n', stderr); -sleep: + sleep: sleep(1); waited++; - if (wait && waited >= (unsigned int) wait) { + if (wait && waited >= (unsigned int)wait) { /* last round: make it even */ waited = (wait + 1) & ~1; } @@ -149,8 +141,7 @@ bool cve_db_lock(locktype lt, int wait) void cve_db_unlock(void) { struct flock fl = { - .l_type = F_UNLCK, - .l_whence = SEEK_SET, + .l_type = F_UNLCK, .l_whence = SEEK_SET, }; int ret; diff --git a/src/library/cve-db-lock.h b/src/library/cve-db-lock.h index d95d123..dfb4c52 100644 --- a/src/library/cve-db-lock.h +++ b/src/library/cve-db-lock.h @@ -16,15 +16,14 @@ * database file */ typedef enum { - LT_READ = 0, /** -#include #include #include +#include +#include #include "cve-string.h" @@ -94,4 +94,3 @@ bool cve_string_cat(cve_string *s, const char *append) * vi: set shiftwidth=8 tabstop=8 expandtab: * :indentSize=8:tabSize=8:noTabs=true: */ - diff --git a/src/library/cve-string.h b/src/library/cve-string.h index 1b35946..c0091fb 100644 --- a/src/library/cve-string.h +++ b/src/library/cve-string.h @@ -13,16 +13,17 @@ #define _GNU_SOURCE +#include #include #include -#include +#include /** * Safely represent and store a buffer as a string */ typedef struct cve_string_t { - char *str; /** str->len || len <= 0) { return false; } - return (strncmp(str->str+(str->len-(len)), suffix, len) == 0); + return (strncmp(str->str + (str->len - (len)), suffix, len) == 0); } /** @@ -182,21 +181,20 @@ static inline char *cve_string_strip(cve_string *str) break; } } - for (int j = str->len-1; j > i; j--) { + for (int j = str->len - 1; j > i; j--) { if (str->str[j] != ' ') { - str->str[j+1] = '\0'; + str->str[j + 1] = '\0'; break; } } - return str->str+i; + return str->str + i; } /** * To be used only with compile time constants. */ -#define cve_string_has_suffix(a, suff) cve_string_has_suffix_const(a, suff, (sizeof(suff)-1)) -#define cve_string_has_prefix(a, pref) cve_string_has_prefix_const(a, pref, (sizeof(pref)-1)) - +#define cve_string_has_suffix(a, suff) cve_string_has_suffix_const(a, suff, (sizeof(suff) - 1)) +#define cve_string_has_prefix(a, pref) cve_string_has_prefix_const(a, pref, (sizeof(pref) - 1)) /* * Editor modelines - https://www.wireshark.org/tools/modelines.html diff --git a/src/library/fetch.c b/src/library/fetch.c index ed9bca9..49c3e91 100644 --- a/src/library/fetch.c +++ b/src/library/fetch.c @@ -11,12 +11,12 @@ #define _GNU_SOURCE -#include #include #include #include -#include #include +#include +#include #include "fetch.h" #include "util.h" @@ -150,7 +150,7 @@ bool gunzip_file(const char *path) return NULL; } ret = g_output_stream_splice(cos, G_INPUT_STREAM(fis), G_OUTPUT_STREAM_SPLICE_NONE, NULL, NULL); - return (ret > 0 ? true : false ); + return (ret > 0 ? true : false); } /* diff --git a/src/library/fetch.h b/src/library/fetch.h index bc3355e..8acce79 100644 --- a/src/library/fetch.h +++ b/src/library/fetch.h @@ -13,11 +13,7 @@ #include -typedef enum { - FETCH_STATUS_FAIL, - FETCH_STATUS_UPDATE, - FETCH_STATUS_OK -} FetchStatus; +typedef enum { FETCH_STATUS_FAIL, FETCH_STATUS_UPDATE, FETCH_STATUS_OK } FetchStatus; /** * Fetch the given URI to the target directory, only if it is newer than diff --git a/src/library/hashmap.c b/src/library/hashmap.c index af971f2..4513bb8 100644 --- a/src/library/hashmap.c +++ b/src/library/hashmap.c @@ -10,10 +10,10 @@ */ #define _GNU_SOURCE +#include #include #include #include -#include #include "hashmap.h" @@ -29,34 +29,34 @@ * An bucket/chain within the hashmap */ typedef struct CveHashmapEntry { - void *hash; /**hash = (void*)key; + row->hash = (void *)key; row->value = value; row->occ = true; if (parent != row) { @@ -313,7 +316,6 @@ void cve_hashmap_free(CveHashmap *self) } free(self); - } static void cve_hashmap_update_next_size(CveHashmap *self) @@ -321,7 +323,7 @@ static void cve_hashmap_update_next_size(CveHashmap *self) if (!self) { return; } - self->next_size = (int) (self->n_buckets * FULL_FACTOR); + self->next_size = (int)(self->n_buckets * FULL_FACTOR); } int cve_hashmap_size(CveHashmap *self) @@ -359,7 +361,12 @@ static bool cve_hashmap_resize(CveHashmap *self) while (entry) { if (entry->occ) { unsigned hash = cve_hashmap_get_hash(self, entry->hash); - if ((incr = cve_hashmap_insert_bucket(self, new_buckets, new_size, hash, entry->hash, entry->value)) > 0) { + if ((incr = cve_hashmap_insert_bucket(self, + new_buckets, + new_size, + hash, + entry->hash, + entry->value)) > 0) { items += incr; } else { /* Likely a memory issue */ @@ -390,18 +397,15 @@ static bool cve_hashmap_resize(CveHashmap *self) return false; } - void cve_hashmap_iter_init(CveHashmap *map, CveHashmapIter *citer) { _CveHashmapIter *iter = NULL; if (!map || !citer) { return; } - iter = (_CveHashmapIter*)citer; + iter = (_CveHashmapIter *)citer; _CveHashmapIter it = { - .bucket = -1, - .map = map, - .item = NULL, + .bucket = -1, .map = map, .item = NULL, }; *iter = it; } @@ -415,7 +419,7 @@ bool cve_hashmap_iter_next(CveHashmapIter *citer, void **key, void **value) return false; } - iter = (_CveHashmapIter*)citer; + iter = (_CveHashmapIter *)citer; if (!iter->map) { return false; } @@ -430,7 +434,7 @@ bool cve_hashmap_iter_next(CveHashmapIter *citer, void **key, void **value) } if (!item) { iter->bucket++; - if (iter->bucket > iter->map->n_buckets-1) { + if (iter->bucket > iter->map->n_buckets - 1) { return false; } item = &(iter->map->buckets[iter->bucket]); @@ -452,7 +456,7 @@ bool cve_hashmap_iter_next(CveHashmapIter *citer, void **key, void **value) } return true; -} +} /* * Editor modelines - https://www.wireshark.org/tools/modelines.html diff --git a/src/library/hashmap.h b/src/library/hashmap.h index 237e690..ef6fa35 100644 --- a/src/library/hashmap.h +++ b/src/library/hashmap.h @@ -28,7 +28,7 @@ #include /* Convert between uint and void* */ -#define HASH_KEY(x) ((void*)((uintptr_t)(x))) +#define HASH_KEY(x) ((void *)((uintptr_t)(x))) #define HASH_VALUE(x) HASH_KEY(x) #define UNHASH_KEY(x) ((unsigned int)((uintptr_t)(x))) #define UNHASH_VALUE(x) UNHASH_KEY(x) @@ -44,7 +44,6 @@ typedef struct CveHashmapIter { void *n2; } CveHashmapIter; - /** * Hash comparison function definition * @@ -108,7 +107,7 @@ static inline bool string_compare(const void *l, const void *r) if (!l || !r) { return false; } - return (strcmp(l,r) == 0); + return (strcmp(l, r) == 0); } /** @@ -119,7 +118,6 @@ static inline bool simple_compare(const void *l, const void *r) return (l == r); } - /** * Create a new CveHashmap * @@ -140,7 +138,8 @@ CveHashmap *cve_hashmap_new(hash_create_func hash, hash_compare_func compare); * * @return A newly allocated CveHashmap */ -CveHashmap *cve_hashmap_new_full(hash_create_func hash, hash_compare_func compare, hash_free_func key_free, hash_free_func value_free); +CveHashmap *cve_hashmap_new_full(hash_create_func hash, hash_compare_func compare, hash_free_func key_free, + hash_free_func value_free); /** * Store a key/value pair in the hashmap diff --git a/src/library/template.c b/src/library/template.c index fc61477..df4cd4c 100644 --- a/src/library/template.c +++ b/src/library/template.c @@ -14,7 +14,6 @@ #include "template.h" #include "util.h" - #include struct TemplateContext { @@ -30,9 +29,9 @@ struct TemplateContext { typedef enum { TC_VALUE_TYPE_MIN, - TC_VALUE_TYPE_BOOL, /**values = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)template_context_value_free); + ret->values = + g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)template_context_value_free); ret->sects = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify)template_context_free); ret->emit = true; return ret; @@ -211,7 +211,7 @@ static void template_context_destroy_subcontext(TemplateContext *ctx, const char */ static inline TCValue *find_context_value(TemplateContext *ctx, const char *key) { - __attribute__ ((unused)) TCValue *val = NULL; + __attribute__((unused)) TCValue *val = NULL; if (!ctx || !key) { return NULL; } @@ -237,10 +237,10 @@ static inline TCValue *find_context_value(TemplateContext *ctx, const char *key) * will be expanded to enable iterables, and the subcontext handling * will likely become private, switching to a type based API */ -static inline TemplateContext* get_context(TemplateContext *ctx, const char *key) +static inline TemplateContext *get_context(TemplateContext *ctx, const char *key) { - __attribute__ ((unused)) TemplateContext *val = NULL; - __attribute__ ((unused)) TCValue *tval = NULL; + __attribute__((unused)) TemplateContext *val = NULL; + __attribute__((unused)) TCValue *tval = NULL; if (!ctx || !ctx->sects || !key) { return NULL; } @@ -299,17 +299,16 @@ cve_string *template_context_process_line(TemplateContext *self, const char *ori return input; } - while ((c = memchr(input->str+offset, '{', input->len-offset))) { + while ((c = memchr(input->str + offset, '{', input->len - offset))) { autofree(cve_string) *newstr = NULL; TCValue *val = NULL; int oldoffset = offset; offset = (c - input->str); - - if (ctx->emit && offset-oldoffset >= 1) { - char *strstart = input->str+oldoffset; - strstart[offset-oldoffset] = '\0'; + if (ctx->emit && offset - oldoffset >= 1) { + char *strstart = input->str + oldoffset; + strstart[offset - oldoffset] = '\0'; if (!ctx->block) { ctx->block = cve_string_dup(strstart); @@ -318,7 +317,7 @@ cve_string *template_context_process_line(TemplateContext *self, const char *ori } } - if (*(c+1) != '{') { + if (*(c + 1) != '{') { insert_missing(ctx, &ctx->block); ++offset; goto bail; @@ -330,28 +329,28 @@ cve_string *template_context_process_line(TemplateContext *self, const char *ori ++offset; goto bail; } - if (*(c+1) != '}') { + if (*(c + 1) != '}') { insert_missing(ctx, &ctx->block); ++offset; goto bail; } - int start = (s-input->str); - start+=2; - int end = (c-input->str); + int start = (s - input->str); + start += 2; + int end = (c - input->str); int length = end - start; - newstr = cve_string_dup(input->str+start); + newstr = cve_string_dup(input->str + start); newstr->str[length] = '\0'; newstr->len = length; offset += length + 4; if (newstr->str[0] == '#') { - TemplateContext *child = get_context(ctx, newstr->str+1); + TemplateContext *child = get_context(ctx, newstr->str + 1); if (ctx->sect_key) { g_free(ctx->sect_key); } - ctx->sect_key = g_strdup(newstr->str+1); + ctx->sect_key = g_strdup(newstr->str + 1); if (!child) { ctx->emit = false; } else { @@ -363,7 +362,7 @@ cve_string *template_context_process_line(TemplateContext *self, const char *ori } } } else if (newstr->str[0] == '/') { - gchar *sect = newstr->str+1; + gchar *sect = newstr->str + 1; if (ctx->name && g_str_equal(sect, ctx->name)) { if (ctx->block) { autofree(cve_string) *contents = ctx->block; @@ -375,7 +374,8 @@ cve_string *template_context_process_line(TemplateContext *self, const char *ori GList *root = val->value, *child = NULL; for (child = root; child; child = child->next) { TemplateContext *cctx = child->data; - complete = template_context_process_line(cctx, contents->str, true); + complete = + template_context_process_line(cctx, contents->str, true); if (!ctx->parent->block) { ctx->parent->block = complete; } else { @@ -397,8 +397,10 @@ cve_string *template_context_process_line(TemplateContext *self, const char *ori ctx = ctx->parent; ctx->in_list = false; } else { - if (ctx->sect_key && !g_str_equal(ctx->sect_key, sect)) { - g_warning("Ending section without starting one: %s (ctx: %s)", sect, ctx->sect_key); + if (ctx->sect_key && !g_str_equal(ctx->sect_key, sect)) { + g_warning("Ending section without starting one: %s (ctx: %s)", + sect, + ctx->sect_key); return NULL; } } @@ -426,19 +428,27 @@ cve_string *template_context_process_line(TemplateContext *self, const char *ori if (val) { if (!ctx->block) { if (val->type == TC_VALUE_TYPE_STRING) { - ctx->block = cve_string_dup(((cve_string*)val->value)->str); + ctx->block = + cve_string_dup(((cve_string *)val->value)->str); } else if (val->type == TC_VALUE_TYPE_BOOL) { - ctx->block = cve_string_dup(val->value ? "true" : "false"); + ctx->block = + cve_string_dup(val->value ? "true" : "false"); } else { - g_warning("Cannot render value of '%s' - which is a list", newstr->str); + g_warning( + "Cannot render value of '%s' - which is a list", + newstr->str); } } else { if (val->type == TC_VALUE_TYPE_STRING) { - cve_string_cat(ctx->block, ((cve_string*)val->value)->str); + cve_string_cat(ctx->block, + ((cve_string *)val->value)->str); } else if (val->type == TC_VALUE_TYPE_BOOL) { - cve_string_cat(ctx->block, val->value ? "true" : "false"); + cve_string_cat(ctx->block, + val->value ? "true" : "false"); } else { - g_warning("Cannot render value of '%s' - which is a list", newstr->str); + g_warning( + "Cannot render value of '%s' - which is a list", + newstr->str); } } } else { @@ -455,7 +465,7 @@ cve_string *template_context_process_line(TemplateContext *self, const char *ori } } } -bail: + bail: if (offset >= input->len) { break; } @@ -463,9 +473,9 @@ cve_string *template_context_process_line(TemplateContext *self, const char *ori if (offset < input->len) { if (!ctx->block) { - ctx->block = cve_string_dup(input->str+offset); + ctx->block = cve_string_dup(input->str + offset); } else { - cve_string_cat(ctx->block, input->str+offset); + cve_string_cat(ctx->block, input->str + offset); } } @@ -498,7 +508,7 @@ cve_string *template_string(const char *original, GHashTable *keys) context = template_context_new(); g_hash_table_iter_init(&iter, keys); - while (g_hash_table_iter_next(&iter, (void**)&key, (void**)&value)) { + while (g_hash_table_iter_next(&iter, (void **)&key, (void **)&value)) { template_context_add_string(context, key, value); } diff --git a/src/library/util.c b/src/library/util.c index 4d4a576..251652d 100644 --- a/src/library/util.c +++ b/src/library/util.c @@ -10,26 +10,24 @@ */ #define _GNU_SOURCE -#include +#include #include -#include -#include +#include #include -#include +#include +#include +#include #include #include -#include -#include +#include -#include "util.h" #include "cve-check-tool.h" #include "cve-string.h" - - +#include "util.h" bool find_sources(const char *path, package_match_func match, bool recurse, cve_add_callback cb) { - struct stat st = {.st_ino = 0}; + struct stat st = {.st_ino = 0 }; bool ret = false; DIR *dir = NULL; struct dirent *ent = NULL; @@ -87,7 +85,6 @@ bool is_package_list(cve_string *path) return false; } - gchar *demacro(CveHashmap *macros, gchar *str) { gchar *key = NULL, *value = NULL; @@ -101,7 +98,7 @@ gchar *demacro(CveHashmap *macros, gchar *str) CveHashmapIter iter; cve_hashmap_iter_init(macros, &iter); - while (cve_hashmap_iter_next(&iter, (void**)&key, (void**)&value)) { + while (cve_hashmap_iter_next(&iter, (void **)&key, (void **)&value)) { if (str_contains(str, key)) { hit = true; str = str_replace(str, key, value); @@ -131,12 +128,11 @@ int64_t parse_xml_date(const char *date) if (!(c = memchr(tmp->str, 'T', tmp->len))) { return -1; } - if (!(c = memchr(c, '-', tmp->len - (tmp->str -c)))) { + if (!(c = memchr(c, '-', tmp->len - (tmp->str - c)))) { return -1; } gint y, m, d, h, min, s; - if (sscanf(date, "%4d-%2d-%2dT%2d:%2d:%2d", &y, &m, &d, - &h, &min, &s) != 6) { + if (sscanf(date, "%4d-%2d-%2dT%2d:%2d:%2d", &y, &m, &d, &h, &min, &s) != 6) { return -1; } tz = g_time_zone_new(c); @@ -169,13 +165,13 @@ gchar *str_replace(gchar *source, const gchar *word, const gchar *replace) bool cve_file_exists(const char *p) { - struct stat st = {.st_ino = 0}; - return (stat(p,&st) == 0); + struct stat st = {.st_ino = 0 }; + return (stat(p, &st) == 0); } bool cve_is_dir(const char *p) { - struct stat st = {.st_ino = 0}; + struct stat st = {.st_ino = 0 }; if (stat(p, &st) != 0) { return false; } @@ -227,7 +223,7 @@ cve_string *make_db_dot_fname(const char *db_path, const char *suffix) if (file) { *file++ = '\0'; if (!*file) { - file = (char *) nvd_file; + file = (char *)nvd_file; } dir = *path ? path : "."; } else { diff --git a/src/library/util.h b/src/library/util.h index 2d549e9..9f7e8fa 100644 --- a/src/library/util.h +++ b/src/library/util.h @@ -13,15 +13,15 @@ #define _GNU_SOURCE -#include -#include #include -#include -#include +#include #include -#include +#include #include +#include #include +#include +#include #include "cve-check-tool.h" #include "cve-string.h" @@ -32,13 +32,12 @@ */ bool is_package_list(cve_string *path); - /** * Determine if a file is one the enumerator is interested in * * @note Paths are relative basename, not absolute */ -typedef bool (*package_match_func)(const gchar*); +typedef bool (*package_match_func)(const gchar *); /** * Recursively replace macros within a string @@ -53,7 +52,6 @@ typedef bool (*package_match_func)(const gchar*); */ gchar *demacro(CveHashmap *macros, gchar *str); - /** * Convert an XML formatted date into unix seconds * @@ -76,22 +74,23 @@ bool find_sources(const char *directory, package_match_func match, bool recurse, * Implemented in a *similar* fashion to how g_autoptr is intended to * work in future, but without the concerns of MSVC, etc.. */ -#define DEF_AUTOFREE(N,C) \ - static inline void _autofree_func_##N (void *p) {\ - if (p && *(N**)p) { \ - /* To debug: printf("Freeing %s\n", #N); */\ - C (*(N**)p);\ - (*(void**)p) = NULL;\ - } \ +#define DEF_AUTOFREE(N, C) \ + static inline void _autofree_func_##N(void *p) \ + { \ + if (p && *(N **)p) { \ + /* To debug: printf("Freeing %s\n", #N); */ \ + C(*(N **)p); \ + (*(void **)p) = NULL; \ + } \ } -#define autofree(N) __attribute__ ((cleanup( _autofree_func_##N ))) N +#define autofree(N) __attribute__((cleanup(_autofree_func_##N))) N /** * Enable easier integration with autofree. Note this is still a single * pointer, you need to use it like: gstrv*, NOT gstrv. */ -typedef gchar* gstrv; +typedef gchar *gstrv; /** * Autofree helper: Cleanup a GFileEnumerator diff --git a/src/main.c b/src/main.c index 7899f91..05548ff 100644 --- a/src/main.c +++ b/src/main.c @@ -10,36 +10,36 @@ */ #define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include #include #include -#include #include -#include -#include -#include -#include #include -#include -#include -#include #include #include "cve-check-tool.h" -#include "plugins/packaging/faux/faux.h" -#include "util.h" #include "config.h" -#include "cve-string.h" -#include "cve-db-lock.h" #include "core.h" +#include "cve-db-lock.h" +#include "cve-string.h" +#include "plugins/packaging/faux/faux.h" +#include "util.h" #include "update.h" #include "plugin-manager.h" typedef struct CveToolInstance { - CveCheckTool shared; /*path); } if (t->xml) { - xmlFree((xmlChar*)t->name); - xmlFree((xmlChar*)t->version); + xmlFree((xmlChar *)t->name); + xmlFree((xmlChar *)t->version); } else { - g_free((gchar*)t->name); - g_free((gchar*)t->version); + g_free((gchar *)t->name); + g_free((gchar *)t->version); } free(t); @@ -119,13 +118,13 @@ static void cve_add_package_internal(struct source_package_t *pkg) * linux_kernel,4.3 = my_own_linux_kernel_name * */ - gchar** temp = g_strsplit(q, ",", -1); + gchar **temp = g_strsplit(q, ",", -1); if (temp[1] != NULL) { - g_free(q); - q = g_strndup(temp[0], strlen(temp[0])); - g_free(pkg->version); - pkg->version = g_strndup(temp[1], strlen(temp[1])); - g_strfreev(temp); + g_free(q); + q = g_strndup(temp[0], strlen(temp[0])); + g_free(pkg->version); + pkg->version = g_strndup(temp[1], strlen(temp[1])); + g_strfreev(temp); } } @@ -143,17 +142,19 @@ static void cve_add_package_internal(struct source_package_t *pkg) cur_id = em->data; if (!cur_id) { - fprintf(stderr, "Fatal configuration detected (null immutable item): Please report this issue to: https://github.com/ikeydoherty/cve-check-tool/issues/20\n"); + fprintf(stderr, + "Fatal configuration detected (null immutable item): Please report this issue to: " + "https://github.com/ikeydoherty/cve-check-tool/issues/20\n"); continue; } - if (pkg_plugin->is_ignored && pkg_plugin->is_ignored(pkg, (gchar*)cur_id)) { + if (pkg_plugin->is_ignored && pkg_plugin->is_ignored(pkg, (gchar *)cur_id)) { continue; } - if (pkg_plugin->is_patched && pkg_plugin->is_patched(pkg, (gchar*)cur_id)) { + if (pkg_plugin->is_patched && pkg_plugin->is_patched(pkg, (gchar *)cur_id)) { if (!g_list_find_custom(pkg->patched, cur_id, (GCompareFunc)strcmp)) { - gchar *tmp = g_strdup((const gchar*)cur_id); + gchar *tmp = g_strdup((const gchar *)cur_id); if (!tmp) { abort(); } @@ -161,7 +162,7 @@ static void cve_add_package_internal(struct source_package_t *pkg) } } else { if (!g_list_find_custom(pkg->issues, cur_id, (GCompareFunc)strcmp)) { - gchar *tmp = g_strdup((const gchar*)cur_id); + gchar *tmp = g_strdup((const gchar *)cur_id); if (!tmp) { abort(); } @@ -257,7 +258,7 @@ static bool load_faux(const char *path) t->extra = d; cve_add_package_internal(t); -next: + next: free(buf); buf = NULL; ++line; @@ -289,10 +290,13 @@ static void cve_add_package(const char *path) static void show_version(void) { - const gchar *msg = "\ -" PACKAGE " " PACKAGE_VERSION "\n\ + const gchar *msg = + "\ +" PACKAGE " " PACKAGE_VERSION + "\n\ Copyright (C) 2015 Intel Corporation\n\ -" PACKAGE_NAME " is free software; you can redistribute it and/or modify\n\ +" PACKAGE_NAME + " is free software; you can redistribute it and/or modify\n\ it under the terms of the GNU General Public License as published by\n\ the Free Software Foundation; either version 2 of the License, or\n\ (at your option) any later version."; @@ -312,23 +316,40 @@ static gchar *mapping_file = NULL; static gchar *output_file = NULL; static gchar *cacert_file = NULL; -static GOptionEntry _entries[] = { - { "not-patched", 'n', 0, G_OPTION_ARG_NONE, &hide_patched, "Hide patched/addressed CVEs", NULL }, - { "not-affected", 'a', 0, G_OPTION_ARG_NONE, &show_unaffected, "Show unaffected items", NULL }, - { "skip-update", 'u', 0, G_OPTION_ARG_NONE, &skip_update, "Bypass forced updates", NULL }, - { "nvd-dir", 'd', 0, G_OPTION_ARG_STRING, &nvds, "NVD directory in filesystem", NULL }, - { "version", 'v', 0, G_OPTION_ARG_NONE, &_show_version, "Show version", NULL }, - { "type", 't', 0, G_OPTION_ARG_STRING, &forced_type, "Set package type to T", "T" }, - { "no-html", 'N', 0, G_OPTION_ARG_NONE, &no_html, "Disable HTML report", NULL }, - { "modified", 'm', 0, G_OPTION_ARG_STRING, &modified_stamp, "Ignore reports after modification date", "D" }, - { "srpm-dir", 's', 0, G_OPTION_ARG_STRING, &srpm_dir, "Source RPM directory", "S" }, - { "csv", 'c', 0, G_OPTION_ARG_NONE, &csv_mode, "Output CSV formatted data only", NULL }, - { "mapping", 'M', 0, G_OPTION_ARG_STRING, &mapping_file, "Path to a mapping file", NULL}, - { "output-file", 'o', 0, G_OPTION_ARG_STRING, &output_file, "Path to the output file (output plugin specific)", NULL}, - { "use-fractional-compare", 'f', 0, G_OPTION_ARG_NONE, &use_frac_compare, "CVE version string fractional compare", NULL }, - { "cacert", 'C', 0, G_OPTION_ARG_STRING, &cacert_file, "Path to the combined SSL certificates file (system default is used if not set)", NULL}, - { .short_name = 0 } -}; +static GOptionEntry _entries[] = + { { "not-patched", 'n', 0, G_OPTION_ARG_NONE, &hide_patched, "Hide patched/addressed CVEs", NULL }, + { "not-affected", 'a', 0, G_OPTION_ARG_NONE, &show_unaffected, "Show unaffected items", NULL }, + { "skip-update", 'u', 0, G_OPTION_ARG_NONE, &skip_update, "Bypass forced updates", NULL }, + { "nvd-dir", 'd', 0, G_OPTION_ARG_STRING, &nvds, "NVD directory in filesystem", NULL }, + { "version", 'v', 0, G_OPTION_ARG_NONE, &_show_version, "Show version", NULL }, + { "type", 't', 0, G_OPTION_ARG_STRING, &forced_type, "Set package type to T", "T" }, + { "no-html", 'N', 0, G_OPTION_ARG_NONE, &no_html, "Disable HTML report", NULL }, + { "modified", 'm', 0, G_OPTION_ARG_STRING, &modified_stamp, "Ignore reports after modification date", "D" }, + { "srpm-dir", 's', 0, G_OPTION_ARG_STRING, &srpm_dir, "Source RPM directory", "S" }, + { "csv", 'c', 0, G_OPTION_ARG_NONE, &csv_mode, "Output CSV formatted data only", NULL }, + { "mapping", 'M', 0, G_OPTION_ARG_STRING, &mapping_file, "Path to a mapping file", NULL }, + { "output-file", + 'o', + 0, + G_OPTION_ARG_STRING, + &output_file, + "Path to the output file (output plugin specific)", + NULL }, + { "use-fractional-compare", + 'f', + 0, + G_OPTION_ARG_NONE, + &use_frac_compare, + "CVE version string fractional compare", + NULL }, + { "cacert", + 'C', + 0, + G_OPTION_ARG_STRING, + &cacert_file, + "Path to the combined SSL certificates file (system default is used if not set)", + NULL }, + {.short_name = 0 } }; /** * Attempt to gain the correct packaging plugin for the given path @@ -360,7 +381,7 @@ static CvePlugin *plugin_for_path(GList *plugins, const char *path, bool recurse DIR *dir = NULL; struct dirent *ent = NULL; CvePlugin *ret = NULL; - struct stat st = {.st_ino = 0}, stc = {.st_ino = 0}; + struct stat st = {.st_ino = 0 }, stc = {.st_ino = 0 }; char *p = realpath(path, NULL); if (!p) { @@ -442,7 +463,7 @@ static gchar *supported_packages(GList *plugins) static bool cve_locate(const char *path, bool recurse) { - struct stat st = {.st_ino = 0}; + struct stat st = {.st_ino = 0 }; bool ret = false; DIR *dir = NULL; struct dirent *ent = NULL; @@ -502,7 +523,7 @@ int main(int argc, char **argv) autofree(CveDB) *cve_db = NULL; GList *pkg_plugins = NULL; int ret = EXIT_FAILURE; - CveToolInstance instance = { .pkg_plugin = NULL }; + CveToolInstance instance = {.pkg_plugin = NULL }; time_t ti; CvePlugin *report = NULL; CvePlugin *package = NULL; @@ -777,7 +798,7 @@ int main(int argc, char **argv) self_priv->pkg_plugin = package; } cve_locate(path, false); -clean: + clean: free(buf); buf = NULL; } @@ -866,4 +887,3 @@ int main(int argc, char **argv) * vi: set shiftwidth=8 tabstop=8 expandtab: * :indentSize=8:tabSize=8:noTabs=true: */ - diff --git a/src/plugin-manager.c b/src/plugin-manager.c index 456b2f1..b670705 100644 --- a/src/plugin-manager.c +++ b/src/plugin-manager.c @@ -12,25 +12,24 @@ #define _GNU_SOURCE #include "config.h" -#include -#include +#include #include +#include +#include #include -#include +#include "plugin-manager.h" #include "plugin.h" #include "util.h" -#include "plugin-manager.h" - static CveHashmap *_plugins; static char *get_plugins_path(void) { #ifdef TEST_SUITE_BUILD - return strdup(TOP_BUILD_DIR"/tests/dummy_install"); + return strdup(TOP_BUILD_DIR "/tests/dummy_install"); #elif defined(INTREE) - return strdup(TOP_BUILD_DIR"/src/plugins/.libs/"); + return strdup(TOP_BUILD_DIR "/src/plugins/.libs/"); #elif !defined(RELATIVE_PLUGINS) /* Return built in path when relative plugins disabled */ return strdup(MODULE_DIR); @@ -46,7 +45,6 @@ static char *get_plugins_path(void) /* relative plugins */ const char *libpaths[] = { "lib/cve-check-tool", "lib64/cve-check-tool" }; - autofree(char) *cp = strdup(binname); if (!cp) { return NULL; @@ -98,7 +96,6 @@ static void load_module(const char *name) cve_plugin_init init_func; CvePlugin *plugin = NULL; - path = build_module_path(name); if (!path) { fprintf(stderr, "Out of memory\n"); @@ -132,7 +129,7 @@ static void load_module(const char *name) abort(); } plugin->handle = handle; - cve_hashmap_put(_plugins, (char*)plugin->name, plugin); + cve_hashmap_put(_plugins, (char *)plugin->name, plugin); } static void destroy_plugin(CvePlugin *plugin) @@ -150,7 +147,7 @@ void cve_plugin_manager_init() { DIR *dir = NULL; struct dirent *ent = NULL; - autofree(char) *mod_path =NULL; + autofree(char) *mod_path = NULL; if (_plugins) { return; @@ -187,7 +184,6 @@ void cve_plugin_manager_destroy() _plugins = NULL; } - CvePlugin *cve_plugin_get_by_name(const char *name) { CvePlugin *ret = NULL; @@ -210,7 +206,7 @@ GList *cve_plugin_get_by_cap(int cap) } cve_hashmap_iter_init(_plugins, &iter); - while (cve_hashmap_iter_next(&iter, NULL, (void**)&plug)) { + while (cve_hashmap_iter_next(&iter, NULL, (void **)&plug)) { if (plug->flags & cap) { ret = g_list_append(ret, plug); } diff --git a/src/plugin-manager.h b/src/plugin-manager.h index 6c9e868..d84bf2e 100644 --- a/src/plugin-manager.h +++ b/src/plugin-manager.h @@ -9,8 +9,8 @@ * (at your option) any later version. */ -#include #include "plugin.h" +#include /** * Initialise the plugin manager, loading all available plugins @@ -22,7 +22,6 @@ void cve_plugin_manager_init(void); */ void cve_plugin_manager_destroy(void); - /** * Get a plugin with a given name if it exists * @@ -35,7 +34,7 @@ CvePlugin *cve_plugin_get_by_name(const char *name); * Get a list of plugins for a given capability * * @note The allocated list must be freed, but not its contents - * + * * @param cap Capibility as exposed via CvePluginType flags * @return a list of matching plugins, or NULL */ diff --git a/src/plugin.h b/src/plugin.h index d1fe49c..ab62bcb 100644 --- a/src/plugin.h +++ b/src/plugin.h @@ -9,60 +9,59 @@ * (at your option) any later version. */ -#include -#include "cve-check-tool.h" #include "common.h" +#include "cve-check-tool.h" +#include #pragma once typedef enum { - PLUGIN_TYPE_MIN = 1 << 0, - PLUGIN_TYPE_PACKAGE = 1 << 1, /**timeout_secs) { curl_easy_setopt(curl, CURLOPT_TIMEOUT, g_jira_cfg->timeout_secs); } @@ -159,7 +161,7 @@ bool add_new_jira_issue(const gchar *jira_json) } ret = check_jira_response(response.data); if (ret) { - /* Perhaps a double check with new jql query here */ + /* Perhaps a double check with new jql query here */ } } g_free(response.data); @@ -195,37 +197,60 @@ bool build_new_jira_issue(const gchar *summary, const gchar *description, bool a if (!jira_cfg_field->is_array) { if (as_template) { json_new_issue = g_strdup_printf("%s\"%s\":{\"%s\":\"{{%s}}\"},", - json_new_issue, jira_cfg_field->name, jira_cfg_field->attribute, jira_cfg_field->name); + json_new_issue, + jira_cfg_field->name, + jira_cfg_field->attribute, + jira_cfg_field->name); } else { json_new_issue = g_strdup_printf("%s\"%s\":{\"%s\":\"%s\"},", - json_new_issue, jira_cfg_field->name, jira_cfg_field->attribute, jira_cfg_field->value); + json_new_issue, + jira_cfg_field->name, + jira_cfg_field->attribute, + jira_cfg_field->value); } } else { if (as_template) { json_new_issue = g_strdup_printf("%s\"%s\":[{\"%s\":\"{{%s}}\"}],", - json_new_issue, jira_cfg_field->name, jira_cfg_field->attribute, jira_cfg_field->name); + json_new_issue, + jira_cfg_field->name, + jira_cfg_field->attribute, + jira_cfg_field->name); } else { json_new_issue = g_strdup_printf("%s\"%s\":[{\"%s\":\"%s\"}],", - json_new_issue, jira_cfg_field->name, jira_cfg_field->attribute, jira_cfg_field->value); + json_new_issue, + jira_cfg_field->name, + jira_cfg_field->attribute, + jira_cfg_field->value); } } } else { if (as_template) { - json_new_issue = g_strdup_printf("%s\"%s\":\"{{%s}}\",", json_new_issue, jira_cfg_field->name, jira_cfg_field->name); + json_new_issue = g_strdup_printf("%s\"%s\":\"{{%s}}\",", + json_new_issue, + jira_cfg_field->name, + jira_cfg_field->name); } else { - json_new_issue = g_strdup_printf("%s\"%s\":\"%s\",", json_new_issue, jira_cfg_field->name, jira_cfg_field->value); + json_new_issue = g_strdup_printf("%s\"%s\":\"%s\",", + json_new_issue, + jira_cfg_field->name, + jira_cfg_field->value); } } } iter = iter->next; } - if (g_str_has_suffix(json_new_issue,",")) { - json_new_issue = g_string_erase(g_string_new(json_new_issue), get_str_length(json_new_issue)-1, 1)->str; + if (g_str_has_suffix(json_new_issue, ",")) { + json_new_issue = + g_string_erase(g_string_new(json_new_issue), get_str_length(json_new_issue) - 1, 1)->str; } if (as_template) { - json_new_issue = g_strdup_printf("%s,\"summary\":\"{{summary}}\",\"description\":\"{{description}}\"",json_new_issue); + json_new_issue = g_strdup_printf("%s,\"summary\":\"{{summary}}\",\"description\":\"{{description}}\"", + json_new_issue); } else { - json_new_issue = g_strdup_printf("%s,\"summary\":\"%s\",\"description\":\"%s\"", json_new_issue, summary, description); + json_new_issue = g_strdup_printf("%s,\"summary\":\"%s\",\"description\":\"%s\"", + json_new_issue, + summary, + description); } *jira_json = g_strdup_printf("%s}}", json_new_issue); return true; @@ -247,18 +272,18 @@ bool build_new_jira_issue_file(const gchar *path, const gchar *summary, const gc return false; } if (summary_tmp != NULL) { - g_hash_table_insert(g_jira_cfg->template,"summary", summary_tmp); + g_hash_table_insert(g_jira_cfg->template, "summary", summary_tmp); } if (description_tmp != NULL) { - g_hash_table_insert(g_jira_cfg->template,"description", summary_tmp); + g_hash_table_insert(g_jira_cfg->template, "description", summary_tmp); } j_json = template_string(data, g_jira_cfg->template); *jira_json = g_strdup(j_json->str); if (summary_tmp != NULL) { - g_hash_table_remove(g_jira_cfg->template,"summary"); + g_hash_table_remove(g_jira_cfg->template, "summary"); } if (description_tmp != NULL) { - g_hash_table_remove(g_jira_cfg->template,"description"); + g_hash_table_remove(g_jira_cfg->template, "description"); } if (is_empty(*jira_json)) { fprintf(stderr, "Error: Template translation failed\n"); @@ -298,12 +323,12 @@ bool check_jira_response(const gchar *jira_json) return false; } node = json_parser_get_root(parser); - status = json_object_get_member(json_node_get_object(node),"errorMessages"); + status = json_object_get_member(json_node_get_object(node), "errorMessages"); if (status != NULL) { if (JSON_NODE_HOLDS_ARRAY(status)) { records = json_node_get_array(status); if (json_array_get_length(records)) { - fprintf(stderr, "Error: %s\n", json_array_get_string_element(records,0)); + fprintf(stderr, "Error: %s\n", json_array_get_string_element(records, 0)); } else { fprintf(stderr, "Error: %s\n", jira_json); } @@ -317,7 +342,8 @@ bool check_jira_response(const gchar *jira_json) return ret; } -static void destroy_template(__attribute__ ((unused)) gpointer key, __attribute__ ((unused)) gpointer value, gpointer data) +static void destroy_template(__attribute__((unused)) gpointer key, __attribute__((unused)) gpointer value, + gpointer data) { g_free(data); } @@ -336,7 +362,7 @@ void destroy_jira_plugin(void) g_free(g_jira_cfg->jql); g_free(g_jira_cfg->search_filter); iter = g_jira_cfg->fields; - while(iter) { + while (iter) { jira_cfg_field = (struct jira_cfg_field_t *)(iter->data); g_free(jira_cfg_field->name); g_free(jira_cfg_field->attribute); @@ -358,7 +384,7 @@ void free_jira_issues(GSList **jira_issues) return; } iter = *jira_issues; - while(iter) { + while (iter) { jira_issue = iter->data; g_free(jira_issue->key); g_free(jira_issue->summary); @@ -383,7 +409,7 @@ struct jira_issue_t *get_jira_issue(const GSList *jira_issues, const gchar *summ return NULL; } iter = jira_issues; - while(iter) { + while (iter) { jira_issue = iter->data; if (!g_strcmp0(jira_issue->summary, summary)) { return jira_issue; @@ -420,7 +446,7 @@ int get_jira_issues_count(const GSList *jira_issues) int jira_issues_count = 0; const GSList *iter = jira_issues; - while(iter) { + while (iter) { ++jira_issues_count; iter = iter->next; } @@ -523,7 +549,10 @@ bool is_jira_alive(void) fprintf(stderr, "Error: Curl failed to initialize for searching issues\n"); return false; } - fprintf(stderr, "Checking JIRA server %s with timeout set at %i sec(s)\n",g_jira_cfg->url, g_jira_cfg->timeout_secs); + fprintf(stderr, + "Checking JIRA server %s with timeout set at %i sec(s)\n", + g_jira_cfg->url, + g_jira_cfg->timeout_secs); response.data = g_malloc(1); if (response.data == NULL) { curl_global_cleanup(); @@ -531,15 +560,15 @@ bool is_jira_alive(void) return false; } response.size = 0; - headers = curl_slist_append(NULL,"Accept: application/json"); - headers = curl_slist_append(headers,"Content-Type: application/json"); - curl_easy_setopt(curl,CURLOPT_HTTPHEADER,headers); - curl_easy_setopt(curl,CURLOPT_USERAGENT,"libcurl-agent/1.0"); - curl_easy_setopt(curl,CURLOPT_URL,g_jira_cfg->url); - curl_easy_setopt(curl,CURLOPT_NOPROGRESS,1L); - curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,0L); - curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_jira_cb); - curl_easy_setopt(curl,CURLOPT_WRITEDATA,(void *)&response); + headers = curl_slist_append(NULL, "Accept: application/json"); + headers = curl_slist_append(headers, "Content-Type: application/json"); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0"); + curl_easy_setopt(curl, CURLOPT_URL, g_jira_cfg->url); + curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L); + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_jira_cb); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response); if (g_jira_cfg->timeout_secs) { curl_easy_setopt(curl, CURLOPT_TIMEOUT, g_jira_cfg->timeout_secs); } @@ -560,14 +589,15 @@ bool is_jira_alive(void) return ret; } -static bool parse_jira_cfg_bool(const gchar*path, GKeyFile *key_file, const gchar *group, const gchar *key, bool *assignee,bool ignore_error, bool default_value) +static bool parse_jira_cfg_bool(const gchar *path, GKeyFile *key_file, const gchar *group, const gchar *key, + bool *assignee, bool ignore_error, bool default_value) { autofree(GError) *error = NULL; - *assignee = g_key_file_get_boolean(key_file,group,key,&error); + *assignee = g_key_file_get_boolean(key_file, group, key, &error); if (error) { if (!ignore_error) { - fprintf(stderr, "Error: Parsing JIRA %s %s value (%s): %s\n",group, key, error->message, path); + fprintf(stderr, "Error: Parsing JIRA %s %s value (%s): %s\n", group, key, error->message, path); return false; } else { *assignee = default_value; @@ -578,7 +608,7 @@ static bool parse_jira_cfg_bool(const gchar*path, GKeyFile *key_file, const gcha static bool parse_jira_cfg_field(const gchar *key, const gchar *value) { - int token_cnt,i = 0; + int token_cnt, i = 0; struct jira_cfg_field_t *jira_cfg_field = NULL; gchar **tokens = NULL; @@ -605,7 +635,7 @@ static bool parse_jira_cfg_field(const gchar *key, const gchar *value) fprintf(stderr, "Error: The reserved field 'description' is not allowed in cfg file\n"); return false; } - if ((token_cnt > 1) && (g_str_has_prefix(tokens[1],"[")) && (g_str_has_suffix(tokens[1],"]"))) { + if ((token_cnt > 1) && (g_str_has_prefix(tokens[1], "[")) && (g_str_has_suffix(tokens[1], "]"))) { jira_cfg_field->is_array = true; } else { jira_cfg_field->is_array = false; @@ -614,17 +644,21 @@ static bool parse_jira_cfg_field(const gchar *key, const gchar *value) if (!jira_cfg_field->is_array) { jira_cfg_field->attribute = g_strdup_printf("%s", tokens[1]); } else { - jira_cfg_field->attribute = g_string_erase(g_string_erase(g_string_new(tokens[1]), get_str_length(tokens[1])-1, 1),0,1)->str; + jira_cfg_field->attribute = + g_string_erase(g_string_erase(g_string_new(tokens[1]), get_str_length(tokens[1]) - 1, 1), + 0, + 1) + ->str; } } else { jira_cfg_field->attribute = NULL; } if (is_empty(value)) { jira_cfg_field->value = NULL; - g_hash_table_insert(g_jira_cfg->template,jira_cfg_field->name,""); + g_hash_table_insert(g_jira_cfg->template, jira_cfg_field->name, ""); } else { jira_cfg_field->value = g_strdup(value); - g_hash_table_insert(g_jira_cfg->template,jira_cfg_field->name, g_strdup(value)); + g_hash_table_insert(g_jira_cfg->template, jira_cfg_field->name, g_strdup(value)); } g_jira_cfg->fields = g_slist_append(g_jira_cfg->fields, jira_cfg_field); g_strfreev(tokens); @@ -633,7 +667,7 @@ static bool parse_jira_cfg_field(const gchar *key, const gchar *value) static bool parse_jira_cfg_file(GKeyFile *config, const gchar *path) { - autofree(GKeyFile) *key_file = g_key_file_new(); + autofree(GKeyFile) *key_file = g_key_file_new(); GKeyFileFlags flags = G_KEY_FILE_KEEP_TRANSLATIONS; autofree(GError) *error = NULL; autofree(gchar) *value = NULL; @@ -658,19 +692,19 @@ static bool parse_jira_cfg_file(GKeyFile *config, const gchar *path) } } group = "JIRA"; - if (!parse_jira_cfg_str(path, key_file, group,"url",&g_jira_cfg->url,false, NULL)) { + if (!parse_jira_cfg_str(path, key_file, group, "url", &g_jira_cfg->url, false, NULL)) { return false; } - if (!parse_jira_cfg_str(path, key_file, group,"user",&g_jira_cfg->user,false, NULL)) { + if (!parse_jira_cfg_str(path, key_file, group, "user", &g_jira_cfg->user, false, NULL)) { return false; } - if (!parse_jira_cfg_str(path, key_file, group,"password",&g_jira_cfg->password,false, NULL)) { + if (!parse_jira_cfg_str(path, key_file, group, "password", &g_jira_cfg->password, false, NULL)) { return false; } - if (!parse_jira_cfg_int(path, key_file, group,"timeout_secs",&g_jira_cfg->timeout_secs,false, 0)) { + if (!parse_jira_cfg_int(path, key_file, group, "timeout_secs", &g_jira_cfg->timeout_secs, false, 0)) { return false; } - if (!parse_jira_cfg_bool(path, key_file, group,"verbose",&g_jira_cfg->verbose,false, false)) { + if (!parse_jira_cfg_bool(path, key_file, group, "verbose", &g_jira_cfg->verbose, false, false)) { return false; } group = "JIRA-New-Issue"; @@ -704,20 +738,21 @@ static bool parse_jira_cfg_file(GKeyFile *config, const gchar *path) } g_strfreev(keys); group = "JIRA-Search-Issues"; - if (!parse_jira_cfg_str(path, key_file, group,"jql",&g_jira_cfg->jql, false, NULL)) { + if (!parse_jira_cfg_str(path, key_file, group, "jql", &g_jira_cfg->jql, false, NULL)) { return false; } - if (!parse_jira_cfg_str(path, key_file, group,"search_filter", &g_jira_cfg->search_filter, false, NULL)) { + if (!parse_jira_cfg_str(path, key_file, group, "search_filter", &g_jira_cfg->search_filter, false, NULL)) { return false; } return true; } -static bool parse_jira_cfg_int(const gchar*path, GKeyFile *key_file, const gchar *group, const gchar *key,int *assignee, bool ignore_error, int default_value) +static bool parse_jira_cfg_int(const gchar *path, GKeyFile *key_file, const gchar *group, const gchar *key, + int *assignee, bool ignore_error, int default_value) { autofree(GError) *error = NULL; - *assignee = g_key_file_get_integer(key_file,group,key,&error); + *assignee = g_key_file_get_integer(key_file, group, key, &error); if (error) { if (!ignore_error) { fprintf(stderr, "Error: Parsing JIRA %s %s value (%s): %s\n", group, key, error->message, path); @@ -734,10 +769,11 @@ static bool parse_jira_cfg_int(const gchar*path, GKeyFile *key_file, const gchar return true; } -static bool parse_jira_cfg_str(const gchar *path, GKeyFile *key_file, const gchar *group, const gchar *key, gchar **assignee, bool ignore_error, gchar *default_value) +static bool parse_jira_cfg_str(const gchar *path, GKeyFile *key_file, const gchar *group, const gchar *key, + gchar **assignee, bool ignore_error, gchar *default_value) { autofree(GError) *error = NULL; - *assignee = g_key_file_get_string(key_file,group,key, &error); + *assignee = g_key_file_get_string(key_file, group, key, &error); if (error) { if (!ignore_error) { fprintf(stderr, "Error: Parsing JIRA %s %s value (%s): %s\n", group, key, error->message, path); @@ -775,7 +811,7 @@ bool parse_jira_issues(const gchar *jira_issues_json, GSList **jira_issues) fprintf(stderr, "Error: %s\n", error->message); return false; } - node = json_object_get_member(json_node_get_object(json_parser_get_root(parser)),"issues"); + node = json_object_get_member(json_node_get_object(json_parser_get_root(parser)), "issues"); if (node == NULL || !JSON_NODE_HOLDS_ARRAY(node)) { fprintf(stderr, "Error: Can't find issues field from JIRA response\n"); return false; @@ -788,33 +824,34 @@ bool parse_jira_issues(const gchar *jira_issues_json, GSList **jira_issues) } for (i = 0; i < issues_cnt; i++) { record = json_array_get_object_element(records, i); - node = json_object_get_member(record,"fields"); + node = json_object_get_member(record, "fields"); object = json_node_get_object(node); - summary = json_object_get_member(object,"summary"); - if ((is_empty(g_jira_cfg->search_filter)) || (g_pattern_match_simple(g_jira_cfg->search_filter,json_node_get_string(summary)))) { + summary = json_object_get_member(object, "summary"); + if ((is_empty(g_jira_cfg->search_filter)) || + (g_pattern_match_simple(g_jira_cfg->search_filter, json_node_get_string(summary)))) { jira_issue = g_slice_new(struct jira_issue_t); - description = json_object_get_member(object,"description"); - key = json_object_get_member(record,"key"); - node = json_object_get_member(object,"status"); + description = json_object_get_member(object, "description"); + key = json_object_get_member(record, "key"); + node = json_object_get_member(object, "status"); field = json_node_get_object(node); - status = json_object_get_member(field,"name"); + status = json_object_get_member(field, "name"); jira_issue->key = g_strdup(json_node_get_string(key)); jira_issue->summary = g_strdup(json_node_get_string(summary)); jira_issue->description = g_strdup(json_node_get_string(description)); jira_issue->status = g_strdup(json_node_get_string(status)); - node = json_object_get_member(object,"resolution"); + node = json_object_get_member(object, "resolution"); if (json_node_is_null(node)) { jira_issue->resolution = g_strdup(""); } else { field = json_node_get_object(node); - resolution = json_object_get_member(field,"name"); + resolution = json_object_get_member(field, "name"); jira_issue->resolution = g_strdup(json_node_get_string(resolution)); } *jira_issues = g_slist_append(*jira_issues, jira_issue); } } if (!g_slist_length(*jira_issues)) { - fprintf(stderr, "Error: No matching JIRA issues found with filter: %s\n",g_jira_cfg->search_filter); + fprintf(stderr, "Error: No matching JIRA issues found with filter: %s\n", g_jira_cfg->search_filter); return false; } return true; @@ -863,9 +900,15 @@ bool save_jira_issues_csv(const GSList *jira_issues, const gchar *path) return false; } fprintf(fp, "JIRA Key,Summary,Status,Description,Resolution\n"); - while(iter) { + while (iter) { jira_issue = iter->data; - fprintf(fp, "%s,%s,%s,\"%s\",\"%s\"\n",jira_issue->key,jira_issue->summary,jira_issue->status,jira_issue->description,jira_issue->resolution); + fprintf(fp, + "%s,%s,%s,\"%s\",\"%s\"\n", + jira_issue->key, + jira_issue->summary, + jira_issue->status, + jira_issue->description, + jira_issue->resolution); iter = iter->next; } fclose(fp); @@ -893,16 +936,16 @@ bool save_jira_issues_xml(const GSList *jira_issues, const gchar *path) fprintf(stderr, "Error: Can't open file for XML writing: %s\n", path); return false; } - fprintf(fp," \n"); - fprintf(fp,"\n", jira_issues_count); - while(iter) { + fprintf(fp, " \n"); + fprintf(fp, "\n", jira_issues_count); + while (iter) { jira_issue = iter->data; fprintf(fp, "%s\n", sp); - fprintf(fp, "%s%s%s\n", sp,sp,jira_issue->key); - fprintf(fp, "%s%s%s\n", sp,sp,jira_issue->summary); - fprintf(fp, "%s%s%s\n", sp,sp,jira_issue->status); - fprintf(fp, "%s%s%s\n",sp,sp,jira_issue->resolution); - fprintf(fp, "%s%s%s\n",sp,sp,jira_issue->description); + fprintf(fp, "%s%s%s\n", sp, sp, jira_issue->key); + fprintf(fp, "%s%s%s\n", sp, sp, jira_issue->summary); + fprintf(fp, "%s%s%s\n", sp, sp, jira_issue->status); + fprintf(fp, "%s%s%s\n", sp, sp, jira_issue->resolution); + fprintf(fp, "%s%s%s\n", sp, sp, jira_issue->description); fprintf(fp, "%s\n", sp); iter = iter->next; } @@ -954,18 +997,18 @@ bool search_jira_issues(const gchar *jira_json, gchar **jira_issues_json) return false; } response.size = 0; - headers = curl_slist_append(NULL,"Accept: application/json"); - headers = curl_slist_append(headers,"Content-Type: application/json"); - curl_easy_setopt(curl,CURLOPT_HTTPHEADER,headers); - curl_easy_setopt(curl,CURLOPT_USERAGENT,"libcurl-agent/1.0"); - curl_easy_setopt(curl,CURLOPT_URL,url); - curl_easy_setopt(curl,CURLOPT_NOPROGRESS,1L); - curl_easy_setopt(curl,CURLOPT_USERPWD,secret); - curl_easy_setopt(curl,CURLOPT_POSTFIELDS,jira_json); - curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,(long)get_str_length(jira_json)); - curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,0L); - curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_jira_cb); - curl_easy_setopt(curl,CURLOPT_WRITEDATA,(void *)&response); + headers = curl_slist_append(NULL, "Accept: application/json"); + headers = curl_slist_append(headers, "Content-Type: application/json"); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0"); + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L); + curl_easy_setopt(curl, CURLOPT_USERPWD, secret); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jira_json); + curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)get_str_length(jira_json)); + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_jira_cb); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response); if (g_jira_cfg->timeout_secs) { curl_easy_setopt(curl, CURLOPT_TIMEOUT, g_jira_cfg->timeout_secs); } @@ -1041,7 +1084,7 @@ void show_jira_issues(const GSList *jira_issues) return; } iter = jira_issues; - while(iter) { + while (iter) { jira_issue = iter->data; fprintf(stderr, "JIRA Key: %s\n", jira_issue->key); fprintf(stderr, "Summary: %s\n", jira_issue->summary); @@ -1059,7 +1102,7 @@ static size_t write_jira_cb(void *ptr, size_t size, size_t memb, void *data) struct jira_data_t *mem = (struct jira_data_t *)data; mem->data = realloc(mem->data, mem->size + new_size + 1); - if(mem->data == NULL) { + if (mem->data == NULL) { fprintf(stderr, "Error: Out of memory\n"); return 0; } diff --git a/src/plugins/jira/jira.h b/src/plugins/jira/jira.h index ef32f8f..1d1d3cb 100644 --- a/src/plugins/jira/jira.h +++ b/src/plugins/jira/jira.h @@ -24,11 +24,11 @@ * A JIRA issue */ struct jira_issue_t { - gchar *key; /** #include +#include +#include "cli.h" #include "config.h" -#include "util.h" #include "cve-check-tool.h" -#include "cli.h" #include "plugin.h" +#include "util.h" static bool cli_write_report(CveCheckTool *self) { @@ -41,30 +41,34 @@ static bool cli_write_report(CveCheckTool *self) } g_hash_table_iter_init(&iter, self->db); - while (g_hash_table_iter_next(&iter, (void**)&key, (void**)&v)) { + while (g_hash_table_iter_next(&iter, (void **)&key, (void **)&v)) { if (!v->issues && !v->patched && !self->show_unaffected) { continue; } if (!v->issues && self->hide_patched) { continue; } - if (fprintf(fd, "%s %s (%u patched, %u issues)\n"C_WHITE"------------"C_RESET"\n", - key, (char*)v->version, g_list_length(v->patched), g_list_length(v->issues)) < 0) { + if (fprintf(fd, + "%s %s (%u patched, %u issues)\n" C_WHITE "------------" C_RESET "\n", + key, + (char *)v->version, + g_list_length(v->patched), + g_list_length(v->issues)) < 0) { goto io_error; } for (c = v->issues; c; c = c->next) { - entry = cve_db_get_cve(self->cve_db, (gchar*)c->data); + entry = cve_db_get_cve(self->cve_db, (gchar *)c->data); if (self->modified > 0 && entry->modified > self->modified) { cve_free(entry); continue; } - if (fprintf(fd, " * "C_RED"%s"C_RESET" : %s\n\n", (char*)c->data, entry->summary) < 0) { + if (fprintf(fd, " * " C_RED "%s" C_RESET " : %s\n\n", (char *)c->data, entry->summary) < 0) { goto io_error; } /* Print links.. */ bool p = false; for (t = entry->uris; t; t = t->next) { - if (fprintf(fd, " - %s\n", (char*)t->data) < 0) { + if (fprintf(fd, " - %s\n", (char *)t->data) < 0) { goto io_error; } p = true; @@ -78,12 +82,15 @@ static bool cli_write_report(CveCheckTool *self) } if (!self->hide_patched) { for (c = v->patched; c; c = c->next) { - entry = cve_db_get_cve(self->cve_db, (gchar*)c->data); + entry = cve_db_get_cve(self->cve_db, (gchar *)c->data); if (self->modified > 0 && entry->modified > self->modified) { cve_free(entry); continue; } - if (fprintf(fd, " * "C_BLUE"%s [PATCHED]"C_RESET" : %s\n\n", (char*)c->data, entry->summary) < 0) { + if (fprintf(fd, + " * " C_BLUE "%s [PATCHED]" C_RESET " : %s\n\n", + (char *)c->data, + entry->summary) < 0) { goto io_error; } cve_free(entry); @@ -107,7 +114,6 @@ static bool cli_write_report(CveCheckTool *self) return ret; } - _module_export_ bool cve_plugin_module_init(CvePlugin *self) { self->report = cli_write_report; diff --git a/src/plugins/output/cli/cli.h b/src/plugins/output/cli/cli.h index de00fb3..d7a0a71 100644 --- a/src/plugins/output/cli/cli.h +++ b/src/plugins/output/cli/cli.h @@ -12,11 +12,11 @@ #define _GNU_SOURCE #include "config.h" -#include "util.h" #include "cve-check-tool.h" +#include "util.h" #define COL "\x1B[" -#define COLOR(x) COL"3"#x"m" +#define COLOR(x) COL "3" #x "m" #define C_RESET "\033[0m" #define C_RED COLOR(1) diff --git a/src/plugins/output/csv/Makefile.am b/src/plugins/output/csv/Makefile.am index 89fb062..1f98003 100644 --- a/src/plugins/output/csv/Makefile.am +++ b/src/plugins/output/csv/Makefile.am @@ -2,11 +2,11 @@ pkglib_LTLIBRARIES += \ csv.la csv_la_SOURCES = \ - output/csv/csv.c + src/plugins/output/csv/csv.c csv_la_LIBADD = \ $(MODULE_COMMON_LIBS) \ - ${top_builddir}/src/libcve.la + libcve.la csv_la_CFLAGS = \ $(MODULE_COMMON_CFLAGS) \ diff --git a/src/plugins/output/csv/csv.c b/src/plugins/output/csv/csv.c index 9d84f84..174ed32 100644 --- a/src/plugins/output/csv/csv.c +++ b/src/plugins/output/csv/csv.c @@ -11,17 +11,17 @@ #define _GNU_SOURCE -#include #include +#include #include "config.h" -#include "util.h" #include "cve-check-tool.h" #include "plugin.h" +#include "util.h" -static inline bool filter_item(CveCheckTool *self, __attribute__ ((unused)) GList *item) +static inline bool filter_item(CveCheckTool *self, __attribute__((unused)) GList *item) { - struct cve_entry_t *c_entry = cve_db_get_cve(self->cve_db, (gchar*)item->data); + struct cve_entry_t *c_entry = cve_db_get_cve(self->cve_db, (gchar *)item->data); bool ret = false; if (self->modified > 0 && c_entry->modified > self->modified) { ret = true; @@ -39,7 +39,7 @@ static gchar *list_as_string(CveCheckTool *self, GList *list) } if (!filter_item(self, list)) { - ret = g_strdup_printf("%s", (gchar*)list->data); + ret = g_strdup_printf("%s", (gchar *)list->data); } if (!list->next) { @@ -51,7 +51,7 @@ static gchar *list_as_string(CveCheckTool *self, GList *list) continue; } gchar *next = NULL; - next = g_strdup_printf("%s %s", ret, (gchar*)it->data); + next = g_strdup_printf("%s %s", ret, (gchar *)it->data); if (ret) { g_free(ret); } @@ -103,7 +103,7 @@ static bool csv_write_report(CveCheckTool *self) /* package,version,unpatched CVE numbers space delimited,patched CVE numbers space delimited, open bug count */ g_hash_table_iter_init(&iter, self->db); - while (g_hash_table_iter_next(&iter, (void**)&key, (void**)&v)) { + while (g_hash_table_iter_next(&iter, (void **)&key, (void **)&v)) { autofree(gchar) *issues = NULL; autofree(gchar) *patched = NULL; autofree(gchar) *open_bug_count = NULL; @@ -127,11 +127,12 @@ static bool csv_write_report(CveCheckTool *self) continue; } if (self->bdb) { - open_bug_count = g_strdup_printf("%i", get_open_bug_count(self, is) + get_open_bug_count(self, pa)); + open_bug_count = + g_strdup_printf("%i", get_open_bug_count(self, is) + get_open_bug_count(self, pa)); } else { open_bug_count = g_strdup("0"); } - if (fprintf(fd, "%s,%s,%s,%s,%s\n", key, (char*)v->version, is, pa, open_bug_count) < 0) { + if (fprintf(fd, "%s,%s,%s,%s,%s\n", key, (char *)v->version, is, pa, open_bug_count) < 0) { goto io_error; } } diff --git a/src/plugins/output/html/Makefile.am b/src/plugins/output/html/Makefile.am index ec8cb61..7fe7e37 100644 --- a/src/plugins/output/html/Makefile.am +++ b/src/plugins/output/html/Makefile.am @@ -2,11 +2,11 @@ pkglib_LTLIBRARIES += \ html.la html_la_SOURCES = \ - output/html/html.c + src/plugins/output/html/html.c html_la_LIBADD = \ $(MODULE_COMMON_LIBS) \ - ${top_builddir}/src/libcve.la + libcve.la html_la_CFLAGS = \ $(MODULE_COMMON_CFLAGS) \ diff --git a/src/plugins/output/html/html.c b/src/plugins/output/html/html.c index 421553f..dc20dd1 100644 --- a/src/plugins/output/html/html.c +++ b/src/plugins/output/html/html.c @@ -11,19 +11,19 @@ #define _GNU_SOURCE -#include #include +#include #include "config.h" -#include "util.h" -#include "template.h" #include "cve-check-tool.h" #include "cve-string.h" #include "plugin.h" +#include "template.h" +#include "util.h" #define TMPL(X) DATA_DIRECTORY G_DIR_SEPARATOR_S X ".template" -#define TMPL_TOTAL TMPL("packages") +#define TMPL_TOTAL TMPL("packages") #define NVD_CVE_URI "http://web.nvd.nist.gov/view/vuln/detail?vulnId=" @@ -72,7 +72,10 @@ static inline gchar *status_map(ReportStatus status) } } -#define LOAD_TEMPLATE(name,ret) if (!load_template(name,ret)) { return false; } +#define LOAD_TEMPLATE(name, ret) \ + if (!load_template(name, ret)) { \ + return false; \ + } static bool write_report(CveCheckTool *self) { @@ -96,7 +99,7 @@ static bool write_report(CveCheckTool *self) template_context_add_string(context, "cve_uri_root", NVD_CVE_URI); g_hash_table_iter_init(&iter, self->db); - while (g_hash_table_iter_next(&iter, (void**)&key, (void**)&v)) { + while (g_hash_table_iter_next(&iter, (void **)&key, (void **)&v)) { bool hit = false; if (!v->issues && !v->patched && !self->show_unaffected) { continue; @@ -106,7 +109,7 @@ static bool write_report(CveCheckTool *self) } for (c = v->issues; c; c = c->next) { - c_entry = cve_db_get_cve(self->cve_db, (gchar*)c->data); + c_entry = cve_db_get_cve(self->cve_db, (gchar *)c->data); if (self->modified > 0 && c_entry->modified > self->modified) { cve_free(c_entry); continue; @@ -136,7 +139,7 @@ static bool write_report(CveCheckTool *self) } if (!self->hide_patched && v->patched) { for (c = v->patched; c; c = c->next) { - c_entry = cve_db_get_cve(self->cve_db, (gchar*)c->data); + c_entry = cve_db_get_cve(self->cve_db, (gchar *)c->data); if (self->modified > 0 && c_entry->modified > self->modified) { cve_free(c_entry); @@ -167,8 +170,7 @@ static bool write_report(CveCheckTool *self) } } - aff = g_strdup_printf("CVE Report for %d package%s", affected, - affected > 1 ? "s" : ""); + aff = g_strdup_printf("CVE Report for %d package%s", affected, affected > 1 ? "s" : ""); template_context_add_string(context, "affected_string", aff); report = template_context_process_line(context, body, false); diff --git a/src/plugins/packaging/eopkg/Makefile.am b/src/plugins/packaging/eopkg/Makefile.am index 5bdcac6..170885b 100644 --- a/src/plugins/packaging/eopkg/Makefile.am +++ b/src/plugins/packaging/eopkg/Makefile.am @@ -2,12 +2,12 @@ pkglib_LTLIBRARIES += \ eopkg.la eopkg_la_SOURCES = \ - packaging/eopkg/eopkg.c \ - packaging/eopkg/eopkg.h + src/plugins/packaging/eopkg/eopkg.c \ + src/plugins/packaging/eopkg/eopkg.h eopkg_la_LIBADD = \ $(MODULE_COMMON_LIBS) \ - ${top_builddir}/src/libcve.la + libcve.la eopkg_la_CFLAGS = \ $(MODULE_COMMON_CFLAGS) \ diff --git a/src/plugins/packaging/eopkg/eopkg.c b/src/plugins/packaging/eopkg/eopkg.c index aec49b6..95205c7 100644 --- a/src/plugins/packaging/eopkg/eopkg.c +++ b/src/plugins/packaging/eopkg/eopkg.c @@ -11,15 +11,15 @@ #define _GNU_SOURCE #include -#include #include -#include +#include #include #include +#include #include "eopkg.h" -#include "util.h" #include "plugin.h" +#include "util.h" #define PATCH_PREFIX "files/security/" @@ -83,14 +83,15 @@ struct source_package_t *eopkg_inspect_pspec(const char *filename) g_warning("Missing release property"); continue; } - int t_release = atoi((const char*)tmp); + int t_release = atoi((const char *)tmp); if (t_release > release) { release = t_release; if (version) { version = NULL; } for (xmlNodePtr sub = child->children; sub; sub = sub->next) { - if (!(sub->type == XML_ELEMENT_NODE && xmlStrEqual(sub->name, BAD_CAST "Version"))) { + if (!(sub->type == XML_ELEMENT_NODE && + xmlStrEqual(sub->name, BAD_CAST "Version"))) { continue; } if (sub->children && sub->children->type == XML_TEXT_NODE) { @@ -133,7 +134,7 @@ struct source_package_t *eopkg_inspect_pspec(const char *filename) bool eopkg_is_patched(struct source_package_t *pkg, char *id) { /* Determine if its patched. */ - autofree(gchar) *pnamet = g_ascii_strdown((gchar*)id, -1); + autofree(gchar) *pnamet = g_ascii_strdown((gchar *)id, -1); autofree(gchar) *pnamel = g_strdup_printf("%s/%s/%s.patch", pkg->path, PATCH_PREFIX, pnamet); autofree(gchar) *pname = g_strdup_printf("%s/%s/%s.patch", pkg->path, PATCH_PREFIX, id); @@ -147,7 +148,7 @@ bool eopkg_is_patched(struct source_package_t *pkg, char *id) bool eopkg_is_ignored(struct source_package_t *pkg, char *id) { /* Determine if its patched. */ - autofree(gchar) *pnamet = g_ascii_strdown((gchar*)id, -1); + autofree(gchar) *pnamet = g_ascii_strdown((gchar *)id, -1); autofree(gchar) *pnamel = g_strdup_printf("%s/%s/%s.nopatch", pkg->path, PATCH_PREFIX, pnamet); autofree(gchar) *pname = g_strdup_printf("%s/%s/%s.nopatch", pkg->path, PATCH_PREFIX, id); @@ -160,7 +161,8 @@ bool eopkg_is_ignored(struct source_package_t *pkg, char *id) bool eopkg_is_package(const char *filename) { - return g_str_has_suffix((const gchar*)filename, "pspec.xml") || g_str_has_suffix((const gchar*)filename, "pspec_x86_64.xml"); + return g_str_has_suffix((const gchar *)filename, "pspec.xml") || + g_str_has_suffix((const gchar *)filename, "pspec_x86_64.xml"); } void eopkg_locate_sources(const char *directory, bool recurse, cve_add_callback cb) diff --git a/src/plugins/packaging/faux/Makefile.am b/src/plugins/packaging/faux/Makefile.am index 42e22b6..6596110 100644 --- a/src/plugins/packaging/faux/Makefile.am +++ b/src/plugins/packaging/faux/Makefile.am @@ -2,12 +2,12 @@ pkglib_LTLIBRARIES += \ faux.la faux_la_SOURCES = \ - packaging/faux/faux.c \ - packaging/faux/faux.h + src/plugins/packaging/faux/faux.c \ + src/plugins/packaging/faux/faux.h faux_la_LIBADD = \ $(MODULE_COMMON_LIBS) \ - ${top_builddir}/src/libcve.la + libcve.la faux_la_CFLAGS = \ $(MODULE_COMMON_CFLAGS) \ diff --git a/src/plugins/packaging/faux/faux.c b/src/plugins/packaging/faux/faux.c index 13f1a35..4dc1ced 100644 --- a/src/plugins/packaging/faux/faux.c +++ b/src/plugins/packaging/faux/faux.c @@ -11,14 +11,14 @@ #define _GNU_SOURCE #include #include -#include #include #include +#include -#include "faux.h" -#include "util.h" #include "cve-check-tool.h" +#include "faux.h" #include "plugin.h" +#include "util.h" void faux_free(struct source_package_t *pkg) { @@ -77,7 +77,7 @@ bool faux_is_ignored(struct source_package_t *t, char *id) bool faux_is_package(const char *filename) { - return g_str_has_suffix((const gchar*)filename, ".csv"); + return g_str_has_suffix((const gchar *)filename, ".csv"); } _module_export_ bool cve_plugin_module_init(CvePlugin *self) diff --git a/src/plugins/packaging/pkgbuild/Makefile.am b/src/plugins/packaging/pkgbuild/Makefile.am index 9200354..301e36d 100644 --- a/src/plugins/packaging/pkgbuild/Makefile.am +++ b/src/plugins/packaging/pkgbuild/Makefile.am @@ -2,12 +2,12 @@ pkglib_LTLIBRARIES += \ pkgbuild.la pkgbuild_la_SOURCES = \ - packaging/pkgbuild/pkgbuild.c \ - packaging/pkgbuild/pkgbuild.h + src/plugins/packaging/pkgbuild/pkgbuild.c \ + src/plugins/packaging/pkgbuild/pkgbuild.h pkgbuild_la_LIBADD = \ $(MODULE_COMMON_LIBS) \ - ${top_builddir}/src/libcve.la + libcve.la pkgbuild_la_CFLAGS = \ $(MODULE_COMMON_CFLAGS) \ diff --git a/src/plugins/packaging/pkgbuild/pkgbuild.c b/src/plugins/packaging/pkgbuild/pkgbuild.c index b3931d5..80e06de 100644 --- a/src/plugins/packaging/pkgbuild/pkgbuild.c +++ b/src/plugins/packaging/pkgbuild/pkgbuild.c @@ -12,13 +12,13 @@ #define _GNU_SOURCE #include #include -#include #include #include +#include #include "pkgbuild.h" -#include "util.h" #include "plugin.h" +#include "util.h" struct source_package_t *pkgbuild_inspect_spec(const char *filename) { @@ -57,7 +57,7 @@ struct source_package_t *pkgbuild_inspect_spec(const char *filename) goto clean; } key = g_strstrip(strv[0]); - value = g_strjoinv("=", strv+1); + value = g_strjoinv("=", strv + 1); value = g_strstrip(value); if (g_str_equal(key, "pkgname")) { @@ -72,7 +72,7 @@ struct source_package_t *pkgbuild_inspect_spec(const char *filename) g_free(read); break; } -clean: + clean: g_free(read); } @@ -101,7 +101,7 @@ struct source_package_t *pkgbuild_inspect_spec(const char *filename) bool pkgbuild_is_patched(struct source_package_t *pkg, char *id) { /* Determine if its patched. */ - autofree(gchar) *pnamet = g_ascii_strdown((gchar*)id, -1); + autofree(gchar) *pnamet = g_ascii_strdown((gchar *)id, -1); autofree(gchar) *pnamel = g_strdup_printf("%s/%s.patch", pkg->path, pnamet); autofree(gchar) *pname = g_strdup_printf("%s/%s.patch", pkg->path, id); @@ -114,7 +114,7 @@ bool pkgbuild_is_patched(struct source_package_t *pkg, char *id) bool pkgbuild_is_package(const char *filename) { - return g_str_has_suffix((const gchar*)filename, "PKGBUILD"); + return g_str_has_suffix((const gchar *)filename, "PKGBUILD"); } void pkgbuild_locate_sources(const char *directory, bool recurse, cve_add_callback cb) diff --git a/src/plugins/packaging/rpm/Makefile.am b/src/plugins/packaging/rpm/Makefile.am index bf06360..dc62ee9 100644 --- a/src/plugins/packaging/rpm/Makefile.am +++ b/src/plugins/packaging/rpm/Makefile.am @@ -3,12 +3,12 @@ pkglib_LTLIBRARIES += \ srpm.la rpm_la_SOURCES = \ - packaging/rpm/rpm.c \ - packaging/rpm/rpm.h + src/plugins/packaging/rpm/rpm.c \ + src/plugins/packaging/rpm/rpm.h rpm_la_LIBADD = \ $(MODULE_COMMON_LIBS) \ - ${top_builddir}/src/libcve.la + libcve.la rpm_la_CFLAGS = \ $(MODULE_COMMON_CFLAGS) \ @@ -18,11 +18,11 @@ rpm_la_LDFLAGS = \ $(MODULE_FLAGS) srpm_la_SOURCES = \ - packaging/rpm/srpm.c + src/plugins/packaging/rpm/srpm.c srpm_la_LIBADD = \ $(MODULE_COMMON_LIBS) \ - ${top_builddir}/src/libcve.la + libcve.la srpm_la_CFLAGS = \ $(MODULE_COMMON_CFLAGS) \ @@ -31,5 +31,5 @@ srpm_la_CFLAGS = \ srpm_la_LDFLAGS = \ $(MODULE_FLAGS) -EXTRA_DIST = \ - packaging/rpm/rpm_common.c +EXTRA_DIST += \ + src/plugins/packaging/rpm/rpm_common.c diff --git a/src/plugins/packaging/rpm/rpm.c b/src/plugins/packaging/rpm/rpm.c index 8b2cc01..ef6a085 100644 --- a/src/plugins/packaging/rpm/rpm.c +++ b/src/plugins/packaging/rpm/rpm.c @@ -10,17 +10,17 @@ */ #define _GNU_SOURCE #include -#include #include -#include +#include #include #include +#include -#include "rpm.h" -#include "util.h" #include "cve-check-tool.h" -#include "rpm_common.c" #include "plugin.h" +#include "rpm.h" +#include "rpm_common.c" +#include "util.h" void rpm_extra_free(struct source_package_t *pkg) { @@ -112,7 +112,10 @@ struct source_package_t *rpm_inspect_spec(const char *filename) } val = cve_hashmap_get(patches, key); if (!val) { - fprintf(stderr, "%s is broken - applying \"patch%s\" which isn't declared\n", filename, key); + fprintf(stderr, + "%s is broken - applying \"patch%s\" which isn't declared\n", + filename, + key); continue; } lpatches = g_list_append(lpatches, g_ascii_strdown(val, -1)); @@ -127,7 +130,7 @@ struct source_package_t *rpm_inspect_spec(const char *filename) goto clean; } key = g_strstrip(strv[0]); - value = g_strjoinv(":", strv+1); + value = g_strjoinv(":", strv + 1); value = g_strstrip(value); if (str_iequal(key, "Name")) { @@ -162,9 +165,8 @@ struct source_package_t *rpm_inspect_spec(const char *filename) if (str_has_isuffix(value, ".nopatch")) { lpatches = g_list_append(lpatches, g_ascii_strdown(value, -1)); } - } -clean: + clean: g_free(read); } @@ -201,7 +203,7 @@ struct source_package_t *rpm_inspect_spec(const char *filename) bool rpm_is_patched(struct source_package_t *pkg, char *id) { /* Determine if its patched. */ - autofree(gchar) *pnamet = g_ascii_strdown((gchar*)id, -1); + autofree(gchar) *pnamet = g_ascii_strdown((gchar *)id, -1); autofree(gchar) *pname = g_strdup_printf("%s.patch", pnamet); if (pkg->extra) { /* Patch validation */ @@ -213,7 +215,7 @@ bool rpm_is_patched(struct source_package_t *pkg, char *id) bool rpm_is_ignored(struct source_package_t *pkg, char *id) { /* Determine if its ignored. */ - autofree(gchar) *pnamet = g_ascii_strdown((gchar*)id, -1); + autofree(gchar) *pnamet = g_ascii_strdown((gchar *)id, -1); autofree(gchar) *pname = g_strdup_printf("%s.nopatch", pnamet); if (pkg->extra) { /* Patch validation */ @@ -224,7 +226,7 @@ bool rpm_is_ignored(struct source_package_t *pkg, char *id) bool rpm_is_package(const char *filename) { - return g_str_has_suffix((const gchar*)filename, ".spec"); + return g_str_has_suffix((const gchar *)filename, ".spec"); } _module_export_ bool cve_plugin_module_init(CvePlugin *self) diff --git a/src/plugins/packaging/rpm/rpm.h b/src/plugins/packaging/rpm/rpm.h index c7326bc..2e9bbd3 100644 --- a/src/plugins/packaging/rpm/rpm.h +++ b/src/plugins/packaging/rpm/rpm.h @@ -32,8 +32,7 @@ struct source_package_t *srpm_examine(const char *filename); * @return a struct source_package_t if successful, otherwise NULL */ -struct source_package_t *rpm_inspect_srpm(const char *dir, const char *name, - const char *version, const char *release); +struct source_package_t *rpm_inspect_srpm(const char *dir, const char *name, const char *version, const char *release); bool srpm_is_patched(struct source_package_t *t, char *id); bool srpm_is_ignored(struct source_package_t *t, char *id); diff --git a/src/plugins/packaging/rpm/rpm_common.c b/src/plugins/packaging/rpm/rpm_common.c index 63a33f1..bfe5227 100644 --- a/src/plugins/packaging/rpm/rpm_common.c +++ b/src/plugins/packaging/rpm/rpm_common.c @@ -10,23 +10,22 @@ */ #define _GNU_SOURCE #include -#include #include -#include +#include #include #include +#include +#include "cve-check-tool.h" #include "rpm.h" #include "util.h" -#include "cve-check-tool.h" - static inline gchar *firstword(gchar *input) { - autofree(gstrv) *splits = NULL; + autofree(gstrv) *splits = NULL; - splits = g_strsplit(input, " ", 2); - return g_strdup(splits[0]); + splits = g_strsplit(input, " ", 2); + return g_strdup(splits[0]); } /** @@ -71,7 +70,6 @@ static inline bool str_has_isuffix(const gchar *inp, const gchar *cmp) return false; } - /* * Editor modelines - https://www.wireshark.org/tools/modelines.html * diff --git a/src/plugins/packaging/rpm/srpm.c b/src/plugins/packaging/rpm/srpm.c index 465cfb3..2c57f48 100644 --- a/src/plugins/packaging/rpm/srpm.c +++ b/src/plugins/packaging/rpm/srpm.c @@ -10,17 +10,17 @@ */ #define _GNU_SOURCE #include -#include #include -#include +#include #include #include +#include -#include "rpm.h" -#include "util.h" #include "cve-check-tool.h" -#include "rpm_common.c" #include "plugin.h" +#include "rpm.h" +#include "rpm_common.c" +#include "util.h" static bool srpm_patch_check(struct source_package_t *t, char *id, bool ignore) { @@ -28,12 +28,12 @@ static bool srpm_patch_check(struct source_package_t *t, char *id, bool ignore) return false; } - autofree(gchar) *pnamet = g_ascii_strdown((gchar*)id, -1); + autofree(gchar) *pnamet = g_ascii_strdown((gchar *)id, -1); autofree(gchar) *pname = g_strdup_printf(ignore ? "%s.nopatch" : "%s.patch", pnamet); gchar **list = t->extra; for (uint i = 0; i < g_strv_length(list); i++) { - autofree(gchar) *comp = g_strchomp(g_ascii_strdown((gchar*)list[i], -1)); + autofree(gchar) *comp = g_strchomp(g_ascii_strdown((gchar *)list[i], -1)); if (g_str_equal(comp, "")) { continue; } @@ -53,7 +53,6 @@ void srpm_extra_free(struct source_package_t *pkg) } } - struct source_package_t *srpm_examine(const char *filename) { struct source_package_t *t = NULL; @@ -99,24 +98,22 @@ struct source_package_t *srpm_examine(const char *filename) if (len > 2) { /* TODO: De-glibificate */ GPtrArray *arr = g_ptr_array_new_with_free_func(NULL); - for (int i = 0; i < len-2; i++) { - g_ptr_array_add(arr, g_strdup(splits[i+2])); + for (int i = 0; i < len - 2; i++) { + g_ptr_array_add(arr, g_strdup(splits[i + 2])); } g_ptr_array_add(arr, NULL); - t->extra = (gchar**)arr->pdata; + t->extra = (gchar **)arr->pdata; g_ptr_array_free(arr, FALSE); } return t; } -struct source_package_t *srpm_scan_archive(const char *dir, const char *name, - const char *version, const char *release) +struct source_package_t *srpm_scan_archive(const char *dir, const char *name, const char *version, const char *release) { autofree(gchar) *path = NULL; - path = g_strdup_printf("%s%s%s-%s-%s.src.rpm", dir, G_DIR_SEPARATOR_S, - name, version, release); + path = g_strdup_printf("%s%s%s-%s-%s.src.rpm", dir, G_DIR_SEPARATOR_S, name, version, release); if (!path) { return NULL; @@ -135,10 +132,9 @@ bool srpm_is_ignored(struct source_package_t *t, char *id) return srpm_patch_check(t, id, true); } - bool srpm_is_package(const char *filename) { - return g_str_has_suffix((const gchar*)filename, ".src.rpm"); + return g_str_has_suffix((const gchar *)filename, ".src.rpm"); } _module_export_ bool cve_plugin_module_init(CvePlugin *self) diff --git a/src/update-main.c b/src/update-main.c index c52d9d0..fdf7f27 100644 --- a/src/update-main.c +++ b/src/update-main.c @@ -10,30 +10,33 @@ */ #define _GNU_SOURCE +#include +#include +#include +#include #include #include -#include #include -#include -#include -#include #include "cve-check-tool.h" -#include "util.h" #include "config.h" -#include "cve-string.h" -#include "cve-db-lock.h" #include "core.h" +#include "cve-db-lock.h" +#include "cve-string.h" +#include "util.h" #include "update.h" static void show_version(void) { - const gchar *msg = "\ -" PACKAGE " " PACKAGE_VERSION "\n\ + const gchar *msg = + "\ +" PACKAGE " " PACKAGE_VERSION + "\n\ Copyright (C) 2015 Intel Corporation\n\ -" PACKAGE_NAME " is free software; you can redistribute it and/or modify\n\ +" PACKAGE_NAME + " is free software; you can redistribute it and/or modify\n\ it under the terms of the GNU General Public License as published by\n\ the Free Software Foundation; either version 2 of the License, or\n\ (at your option) any later version."; @@ -45,13 +48,18 @@ static bool _show_version = false; static bool _quiet = false; static const char *_cacert_file = NULL; -static GOptionEntry _entries[] = { - { "nvd-dir", 'd', 0, G_OPTION_ARG_STRING, &nvds, "NVD directory in filesystem", NULL }, - { "version", 'v', 0, G_OPTION_ARG_NONE, &_show_version, "Show version", NULL }, - { "quiet", 'q', 0, G_OPTION_ARG_NONE, &_quiet, "Run silently", NULL }, - { "cacert", 'C', 0, G_OPTION_ARG_STRING, &_cacert_file, "Path to the combined SSL certificates file (system default is used if not set)", NULL}, - { .short_name = 0 } -}; +static GOptionEntry _entries[] = + { { "nvd-dir", 'd', 0, G_OPTION_ARG_STRING, &nvds, "NVD directory in filesystem", NULL }, + { "version", 'v', 0, G_OPTION_ARG_NONE, &_show_version, "Show version", NULL }, + { "quiet", 'q', 0, G_OPTION_ARG_NONE, &_quiet, "Run silently", NULL }, + { "cacert", + 'C', + 0, + G_OPTION_ARG_STRING, + &_cacert_file, + "Path to the combined SSL certificates file (system default is used if not set)", + NULL }, + {.short_name = 0 } }; /** * Main entry. diff --git a/src/update.c b/src/update.c index 7c4d635..37f229c 100644 --- a/src/update.c +++ b/src/update.c @@ -10,31 +10,31 @@ */ #define _GNU_SOURCE -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include -#include #include -#include -#include -#include +#include +#include +#include #include +#include #include -#include -#include -#include -#include -#include #include "cve-check-tool.h" -#include "util.h" #include "config.h" -#include "cve-string.h" -#include "cve-db-lock.h" #include "core.h" +#include "cve-db-lock.h" +#include "cve-string.h" +#include "util.h" #include "update.h" @@ -43,7 +43,7 @@ #include "fetch.h" #define UPDATE_THRESHOLD 7200 -#define UPDATE_DB_MARKER_SUFFIX "cve.update_db" +#define UPDATE_DB_MARKER_SUFFIX "cve.update_db" static const char *get_home_dir(void) { @@ -67,7 +67,7 @@ static const char *get_home_dir(void) cve_string *get_db_path(const char *path) { - const mode_t mode = S_IRWXU|S_IRWXG|S_IRWXO; + const mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO; const char *dir; autofree(cve_string) *d = NULL; @@ -117,8 +117,7 @@ static char *nvdcve_meta_get_val(FILE *f, const char *field) char field_name[256], field_value[256]; int ret; - ret = fscanf(f, " %255[^: \f\n\r\t\v] :%255s", - field_name, field_value); + ret = fscanf(f, " %255[^: \f\n\r\t\v] :%255s", field_name, field_value); if (ret != 2) { if (ret != EOF) { continue; @@ -198,7 +197,6 @@ static bool nvdcve_data_ok(const char *meta, const char *data) fclose(fmeta); err_out: return ret; - } static bool __update_required(const char *db_file, const char *update_fname) @@ -246,13 +244,13 @@ int update_required(const char *db_file) } #ifndef O_NOFOLLOW -#define O_NOFOLLOW 0 +#define O_NOFOLLOW 0 #endif static inline int update_begin(const char *update_fname) { - const int flags = O_RDONLY|O_CREAT|O_NONBLOCK|O_NOFOLLOW; - const mode_t mode = S_IRUSR|S_IWUSR; + const int flags = O_RDONLY | O_CREAT | O_NONBLOCK | O_NOFOLLOW; + const mode_t mode = S_IRUSR | S_IWUSR; return open(update_fname, flags, mode); } @@ -265,8 +263,8 @@ static inline void update_end(int fd, const char *update_fname, bool ok) } } -static int do_fetch_update(int year, const char *db_dir, CveDB *cve_db, - bool db_exist, bool verbose, const char *cacert_file) +static int do_fetch_update(int year, const char *db_dir, CveDB *cve_db, bool db_exist, bool verbose, + const char *cacert_file) { const char nvd_uri[] = URI_PREFIX; autofree(cve_string) *uri_meta = NULL; @@ -357,8 +355,7 @@ static int do_fetch_update(int year, const char *db_dir, CveDB *cve_db, refetched = true; goto refetch; } - fprintf(stderr, "Unable to extract %s\n", - nvdcve_data_gz->str); + fprintf(stderr, "Unable to extract %s\n", nvdcve_data_gz->str); return -1; } if (!nvdcve_data_ok(nvdcve_meta->str, nvdcve_data->str)) { @@ -366,8 +363,7 @@ static int do_fetch_update(int year, const char *db_dir, CveDB *cve_db, refetched = true; goto refetch; } - fprintf(stderr, "Unpacked data %s is not consistent\n", - nvdcve_data->str); + fprintf(stderr, "Unpacked data %s is not consistent\n", nvdcve_data->str); return -1; } } @@ -381,8 +377,7 @@ static int do_fetch_update(int year, const char *db_dir, CveDB *cve_db, if (verbose) { static const char data_report_msg[][sizeof("Skipp")] = { - [false] = "Skipp", - [true] = "Load", + [false] = "Skipp", [true] = "Load", }; fprintf(stderr, "%sed: %s\n", data_report_msg[load], nvd_xml_gz->str); } @@ -404,7 +399,7 @@ bool update_db(bool quiet, const char *db_file, const char *cacert_file) bool db_locked = false; t = time(NULL); - if (t == (time_t) -1) { + if (t == (time_t)-1) { goto time; } @@ -456,7 +451,7 @@ bool update_db(bool quiet, const char *db_file, const char *cacert_file) goto end; } - for (int i = YEAR_START; i <= year+1; i++) { + for (int i = YEAR_START; i <= year + 1; i++) { int y = i > year ? -1 : i; int rc; diff --git a/src/update.h b/src/update.h index ceea0c3..bfc9811 100644 --- a/src/update.h +++ b/src/update.h @@ -17,7 +17,6 @@ int update_required(const char *db_file); bool update_db(bool quiet, const char *db_file, const char *cacert_file); - /* * Editor modelines - https://www.wireshark.org/tools/modelines.html * diff --git a/test-init.sh b/test-init.sh index b5e14e2..3703517 100755 --- a/test-init.sh +++ b/test-init.sh @@ -11,4 +11,4 @@ fi mkdir -p "${top_builddir}/tests/dummy_install/" -find "${top_builddir}/src/plugins/" -name "*.so" | xargs -I{} cp {} "${top_builddir}/tests/dummy_install/." +cp ${top_builddir}/.libs/*.so "${top_builddir}/tests/dummy_install/." diff --git a/tests/check-core.c b/tests/check-core.c index 0e4768a..0520382 100644 --- a/tests/check-core.c +++ b/tests/check-core.c @@ -10,13 +10,13 @@ #define _GNU_SOURCE #include -#include #include +#include #include "cve-string.c" -#include "util.h" -#include "util.c" #include "hashmap.c" +#include "util.c" +#include "util.h" #include "config.h" @@ -76,20 +76,17 @@ START_TEST(cve_string_test) str = cve_string_dup_printf("Test String #%d", 1); fail_if(!str, "Failed to allocate string"); - fail_if(!g_str_equal(str->str, "Test String #1"), - "Invalid formatted string"); + fail_if(!g_str_equal(str->str, "Test String #1"), "Invalid formatted string"); fail_if(cstrlen(str) != 14, "Incorrect string length"); fail_if(!cve_string_has_suffix(str, "g #1"), "String has incorrect suffix"); fail_if(!cve_string_has_prefix(str, "T"), "String has incorrect prefix"); - fail_if(!cve_string_cat(str, "append"), - "Failed to append string"); - fail_if(cstrlen(str) != 20, "Incorrect string length after append"); + fail_if(!cve_string_cat(str, "append"), "Failed to append string"); + fail_if(cstrlen(str) != 20, "Incorrect string length after append"); str2 = cve_string_dup(str->str); - fail_if(!g_str_equal(str->str, str2->str), - "Strings do not match"); + fail_if(!g_str_equal(str->str, str2->str), "Strings do not match"); fail_if(cstrlen(str) != cstrlen(str2), "Invalid string length after dup"); cve_string_free(str); @@ -102,18 +99,15 @@ START_TEST(cve_string_test) str = cve_string_dup_printf("Test String #%d", 2); fail_if(!str, "Failed to allocate string"); - fail_if(!cve_string_const_equal(str, "Test String #2"), - "Const String Compare failed"); + fail_if(!cve_string_const_equal(str, "Test String #2"), "Const String Compare failed"); /* Ensure its not failing completely. */ - fail_if(cve_string_const_equal(str, "Test String #1"), - "Const String Compare should not match"); + fail_if(cve_string_const_equal(str, "Test String #1"), "Const String Compare should not match"); str2 = cve_string_dup(str->str); fail_if(!str2, "Failed to dup string"); fail_if(!cve_string_equal(str, str2), "Identical strings not matching"); - fail_if(!cve_string_const_equal(str2, (const char*)str->str), - "Direct const compare fail!"); + fail_if(!cve_string_const_equal(str2, (const char *)str->str), "Direct const compare fail!"); cve_string_free(str); cve_string_free(str2); @@ -126,7 +120,7 @@ START_TEST(cve_string_test) fail_if(cve_string_const_equal(NULL, NULL), "const_equal on NULL string"); /* Forced empty ->str tests */ - cve_string st = { .len = 0 }; + cve_string st = {.len = 0 }; fail_if(cve_string_equal(&st, &st), "equal on NULL ->str"); fail_if(cve_string_const_equal(&st, "TEST"), "const_equal on NULL ->str"); } diff --git a/tests/check-database.c b/tests/check-database.c index 9987945..ad55b88 100644 --- a/tests/check-database.c +++ b/tests/check-database.c @@ -10,14 +10,14 @@ #define _GNU_SOURCE #include -#include #include +#include -#include "util.h" -#include "util.c" -#include "cve-string.c" #include "core.c" +#include "cve-string.c" #include "hashmap.c" +#include "util.c" +#include "util.h" #include "config.h" @@ -38,8 +38,7 @@ START_TEST(cve_database_load) db = cve_db_new(":memory:"); fail_if(!db, "Failed to create CveDB"); - fail_if(!cve_db_load(db, TOP_DIR "/tests/dummy_data/nvdcve-2.0-2002.xml"), - "Failed to load database"); + fail_if(!cve_db_load(db, TOP_DIR "/tests/dummy_data/nvdcve-2.0-2002.xml"), "Failed to load database"); cve_db_free(db); } @@ -49,14 +48,13 @@ START_TEST(cve_database_fetch) { CveDB *db = NULL; GList *ret = NULL; - __attribute__ ((unused)) gchar *cve = NULL; + __attribute__((unused)) gchar *cve = NULL; struct cve_entry_t *t = NULL; db = cve_db_new(":memory:"); fail_if(!db, "Failed to create CveDB"); - fail_if(!cve_db_load(db, TOP_DIR "/tests/dummy_data/nvdcve-2.0-2002.xml"), - "Failed to load database"); + fail_if(!cve_db_load(db, TOP_DIR "/tests/dummy_data/nvdcve-2.0-2002.xml"), "Failed to load database"); ret = cve_db_get_issues(db, "ssleay", "0.9"); fail_if(!ret, "Failed to get issues list for ssleay"); @@ -76,7 +74,6 @@ START_TEST(cve_database_fetch) fail_if(!g_str_equal(t->id, cve), "Mismatched CVE return"); cve_free(t); t = NULL; - g_list_free_full(ret, g_free); diff --git a/tests/check-hashmap.c b/tests/check-hashmap.c index 198856f..384e13e 100644 --- a/tests/check-hashmap.c +++ b/tests/check-hashmap.c @@ -10,9 +10,9 @@ */ #define _GNU_SOURCE +#include "hashmap.c" #include #include -#include "hashmap.c" START_TEST(cve_hashmap_new_check) { @@ -53,7 +53,7 @@ START_TEST(cve_hashmap_simple_check) val = cve_hashmap_get(map, HASH_KEY(802)); fail_if(!val, "Value should be returned from hashmap for known key"); fail_if(UNHASH_VALUE(val) != 802, "Value returned from hashmap was incorrect"); - + cve_hashmap_free(map); map = NULL; @@ -96,7 +96,6 @@ START_TEST(cve_hashmap_string_check) fail_if(UNHASH_VALUE(val) != 83, "Failed to get correct value from hashmap"); cve_hashmap_free(map); - } END_TEST @@ -120,9 +119,8 @@ START_TEST(cve_hashmap_iter_check) fail_if(cve_hashmap_size(map) != 5000, "Invalid hashmap size after 5000 elements"); cve_hashmap_iter_init(map, &iter); - while (cve_hashmap_iter_next(&iter, (void**)&key, (void**)&value)) { - fail_if(UNHASH_KEY(key) != UNHASH_VALUE(value), - "Mismatched key/value pair in iteration"); + while (cve_hashmap_iter_next(&iter, (void **)&key, (void **)&value)) { + fail_if(UNHASH_KEY(key) != UNHASH_VALUE(value), "Mismatched key/value pair in iteration"); ++count; } fail_if(count != 5000, "Did not iterate all hashmap elements"); @@ -137,8 +135,7 @@ START_TEST(cve_hashmap_iter_check) key = value = NULL; while (cve_hashmap_iter_next(&iter, &key, &value)) { unsigned int k = UNHASH_KEY(key); - fail_if(UNHASH_VALUE(value) != k, - "Mismatched post-removal key/value pair in iteration"); + fail_if(UNHASH_VALUE(value) != k, "Mismatched post-removal key/value pair in iteration"); fail_if(k >= 2000 && k < 4000, "Key/value not removed from hashtable"); ++count; } @@ -153,7 +150,7 @@ static int free_count = 0; static inline void free_helper(void *p) { - free((void*)p); + free((void *)p); ++free_count; } @@ -190,37 +187,37 @@ START_TEST(cve_hashmap_alloc_check) fail_if(free_count != 2, "Failed to free last element from hashmap"); } END_TEST - + static Suite *cve_hashmap_suite(void) { - Suite *s = NULL; - TCase *tc = NULL; - - s = suite_create("cve_array"); - tc = tcase_create("cve_hashmap_functions"); - tcase_add_test(tc, cve_hashmap_new_check); - tcase_add_test(tc, cve_hashmap_simple_check); - tcase_add_test(tc, cve_hashmap_string_check); - tcase_add_test(tc, cve_hashmap_iter_check); - tcase_add_test(tc, cve_hashmap_alloc_check); - suite_add_tcase(s, tc); - - return s; + Suite *s = NULL; + TCase *tc = NULL; + + s = suite_create("cve_array"); + tc = tcase_create("cve_hashmap_functions"); + tcase_add_test(tc, cve_hashmap_new_check); + tcase_add_test(tc, cve_hashmap_simple_check); + tcase_add_test(tc, cve_hashmap_string_check); + tcase_add_test(tc, cve_hashmap_iter_check); + tcase_add_test(tc, cve_hashmap_alloc_check); + suite_add_tcase(s, tc); + + return s; } int main(void) { - int number_failed; - Suite *s; - SRunner *sr; + int number_failed; + Suite *s; + SRunner *sr; - s = cve_hashmap_suite(); - sr = srunner_create(s); - srunner_run_all(sr, CK_VERBOSE); - number_failed = srunner_ntests_failed(sr); - srunner_free(sr); + s = cve_hashmap_suite(); + sr = srunner_create(s); + srunner_run_all(sr, CK_VERBOSE); + number_failed = srunner_ntests_failed(sr); + srunner_free(sr); - return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; + return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; } /* diff --git a/tests/check-jira-plugin.c b/tests/check-jira-plugin.c index 753d543..dcd9677 100644 --- a/tests/check-jira-plugin.c +++ b/tests/check-jira-plugin.c @@ -10,22 +10,22 @@ #define _GNU_SOURCE #include -#include #include +#include #include "cve-string.c" -#include "util.h" #include "util.c" +#include "util.h" #include "config.h" -#include "plugins/jira/jira.h" +#include "hashmap.c" #include "plugins/jira/jira.c" +#include "plugins/jira/jira.h" #include "template.c" -#include "hashmap.c" const char *nvd_file = "nvd.db"; -#define DEFAULT_CONFIG_FILE DEFAULT_PATH "/cve-check-tool.conf" +#define DEFAULT_CONFIG_FILE DEFAULT_PATH "/cve-check-tool.conf" #define TEST_CONFIG_FILE TOP_DIR "/tests/dummy_data/plugins/jira/cve-check-tool.conf" #define TEST_JSON_FILE TOP_DIR "/tests/dummy_data/plugins/jira/jira_test_issues.json" @@ -65,7 +65,7 @@ START_TEST(cve_jira_plugin_set_functions) int i = 0; ret = init_jira_plugin(NULL, TEST_CONFIG_FILE); fail_if(!ret, "Failed to initialize JIRA plugin"); - for(i = 0;i < 2; i++) { + for (i = 0; i < 2; i++) { if (i == 1) { destroy_jira_plugin(); } @@ -111,29 +111,29 @@ START_TEST(cve_jira_plugin_save_functions) remove("test_jira_issues.xml"); } ret = save_jira_issues_csv(jira_issues, ""); - fail_if(ret,"Negative save to CSV file test should have failed"); + fail_if(ret, "Negative save to CSV file test should have failed"); ret = save_jira_issues_csv(jira_issues, NULL); - fail_if(ret,"Negative save to CSV file test should have failed"); + fail_if(ret, "Negative save to CSV file test should have failed"); ret = save_jira_issues_csv(NULL, ""); - fail_if(ret,"Negative save to CSV file test should have failed"); + fail_if(ret, "Negative save to CSV file test should have failed"); ret = save_jira_issues_csv(NULL, NULL); - fail_if(ret,"Negative save to CSV file test should have failed"); + fail_if(ret, "Negative save to CSV file test should have failed"); ret = save_jira_issues_csv(NULL, "test_jira_issues.csv"); - fail_if(ret,"Negative save to CSV file test should have failed"); + fail_if(ret, "Negative save to CSV file test should have failed"); if (cve_file_exists("test_jira_issues.csv")) { remove("test_jira_issues.csv"); fail("Negative save to CSV file test should not have not create file"); } ret = save_jira_issues_xml(jira_issues, ""); - fail_if(ret,"Negative save to XML file test should have failed"); + fail_if(ret, "Negative save to XML file test should have failed"); ret = save_jira_issues_xml(jira_issues, NULL); - fail_if(ret,"Negative save to XML file test should have failed"); + fail_if(ret, "Negative save to XML file test should have failed"); ret = save_jira_issues_xml(NULL, ""); - fail_if(ret,"Negative save to XML file test should have failed"); + fail_if(ret, "Negative save to XML file test should have failed"); ret = save_jira_issues_xml(NULL, NULL); - fail_if(ret,"Negative save to XML file test should have failed"); + fail_if(ret, "Negative save to XML file test should have failed"); ret = save_jira_issues_xml(NULL, "test_jira_issues.xml"); - fail_if(ret,"Negative save to XML file test should have failed"); + fail_if(ret, "Negative save to XML file test should have failed"); if (cve_file_exists("test_jira_issues.xml")) { remove("test_jira_issues.xml"); fail("Negative save to XML file test should not have not create file"); @@ -145,11 +145,11 @@ START_TEST(cve_jira_plugin_save_functions) remove("test_jira_save.txt"); } ret = save("", ""); - fail_if(ret,"Negative save to text file test should have failed"); + fail_if(ret, "Negative save to text file test should have failed"); ret = save(NULL, ""); - fail_if(ret,"Negative save to text file test should have failed"); + fail_if(ret, "Negative save to text file test should have failed"); ret = save("", NULL); - fail_if(ret,"Negative save to text file test should have failed"); + fail_if(ret, "Negative save to text file test should have failed"); ret = save("", "test_jira_save.txt"); if (cve_file_exists("test_jira_save.txt")) { remove("test_jira_save.txt"); @@ -171,11 +171,11 @@ static Suite *core_suite(void) s = suite_create("cve_jira_plugin"); tc = tcase_create("cve_jira_plugin_functions"); - tcase_add_test(tc,cve_jira_plugin_init_function); - tcase_add_test(tc,cve_jira_plugin_jira_issues_functions); - tcase_add_test(tc,cve_jira_plugin_set_functions); - tcase_add_test(tc,cve_jira_plugin_save_functions); - suite_add_tcase(s,tc); + tcase_add_test(tc, cve_jira_plugin_init_function); + tcase_add_test(tc, cve_jira_plugin_jira_issues_functions); + tcase_add_test(tc, cve_jira_plugin_set_functions); + tcase_add_test(tc, cve_jira_plugin_save_functions); + suite_add_tcase(s, tc); return s; } diff --git a/tests/check-packaging.c b/tests/check-packaging.c index dcd08d4..d6f70c7 100644 --- a/tests/check-packaging.c +++ b/tests/check-packaging.c @@ -10,13 +10,13 @@ #define _GNU_SOURCE #include -#include #include +#include #include "cve-string.c" -#include "util.h" -#include "util.c" #include "hashmap.c" +#include "util.c" +#include "util.h" #define TEST_SUITE_BUILD 1 @@ -26,7 +26,6 @@ const char *nvd_file = "nvd.db"; - static int add_count = 0; void cve_add_package(__attribute__((unused)) const char *path) @@ -45,7 +44,7 @@ static inline void package_free(void *p, CvePlugin *pkg_plugin) pkg_plugin->free_package(t); t->extra = NULL; } - + if (t->issues) { /* bless you */ g_list_free_full(t->issues, xmlFree); } @@ -56,11 +55,11 @@ static inline void package_free(void *p, CvePlugin *pkg_plugin) free(t->path); } if (t->xml) { - xmlFree((xmlChar*)t->name); - xmlFree((xmlChar*)t->version); + xmlFree((xmlChar *)t->name); + xmlFree((xmlChar *)t->version); } else { - g_free((gchar*)t->name); - g_free((gchar*)t->version); + g_free((gchar *)t->name); + g_free((gchar *)t->version); } free(t); @@ -87,19 +86,14 @@ START_TEST(cve_rpm_test) pkg = plugin->scan_package(TOP_DIR "/tests/dummy_data/rpm/package.spec"); fail_if(!pkg, "Failed to inspect RPM spec!"); - fail_if(!g_str_equal(pkg->name, "test-package"), - "Invalid RPM package name"); - fail_if(!g_str_equal(pkg->version, "1.1.0"), - "Invalid RPM package version"); + fail_if(!g_str_equal(pkg->name, "test-package"), "Invalid RPM package name"); + fail_if(!g_str_equal(pkg->version, "1.1.0"), "Invalid RPM package version"); fail_if(pkg->release != 5, "Invalid RPM package release"); - fail_if(!plugin->is_patched(pkg, "CVE-2014-5461"), - "Failed to detect RPM CVE patch"); + fail_if(!plugin->is_patched(pkg, "CVE-2014-5461"), "Failed to detect RPM CVE patch"); - fail_if(!plugin->is_ignored(pkg, "CVE-2013-4459"), - "Failed to detect ignored CVE"); - fail_if(plugin->is_ignored(pkg, "CVE-2013-0012"), - "Incorrectly detected non-ignored CVE"); + fail_if(!plugin->is_ignored(pkg, "CVE-2013-4459"), "Failed to detect ignored CVE"); + fail_if(plugin->is_ignored(pkg, "CVE-2013-0012"), "Incorrectly detected non-ignored CVE"); package_free(pkg, plugin); } @@ -121,25 +115,21 @@ START_TEST(cve_srpm_test) fail_if(!plugin, "srpm plugin not found!"); - pkg = plugin->scan_archive(TOP_DIR "/tests/dummy_data/rpm/", "package", "1.", "5" ); + pkg = plugin->scan_archive(TOP_DIR "/tests/dummy_data/rpm/", "package", "1.", "5"); fail_if(!pkg, "Failed to inspect source RPM!"); fail_if(!plugin->is_patched(pkg, "CVE-2014-5461"), "SRPM patch test failed"); - fail_if(!g_str_equal(pkg->name, "test-package"), - "Invalid SRPM package name"); - fail_if(!g_str_equal(pkg->version, "1."), - "Invalid SRPM package version"); + fail_if(!g_str_equal(pkg->name, "test-package"), "Invalid SRPM package name"); + fail_if(!g_str_equal(pkg->version, "1."), "Invalid SRPM package version"); - fail_if(plugin->is_ignored(pkg, "CVE-2013-0012"), - "Incorrectly detected non-ignored CVE"); + fail_if(plugin->is_ignored(pkg, "CVE-2013-0012"), "Incorrectly detected non-ignored CVE"); package_free(pkg, plugin); - pkg = plugin->scan_archive(TOP_DIR "/tests/dummy_data/rpm/", "invalid_package", "1.", "5" ); + pkg = plugin->scan_archive(TOP_DIR "/tests/dummy_data/rpm/", "invalid_package", "1.", "5"); fail_if(pkg, "Incorrectly succeeded at missing source RPM!"); } END_TEST - /** * Solus eopkg test */ @@ -156,19 +146,14 @@ START_TEST(cve_eopkg_test) pkg = plugin->scan_package(TOP_DIR "/tests/dummy_data/eopkg/pspec.xml"); fail_if(!pkg, "Failed to inspect eopkg spec!"); - fail_if(!g_str_equal(pkg->name, "budgie-desktop"), - "Invalid eopkg package name"); - fail_if(!g_str_equal(pkg->version, "8.1"), - "Invalid eopkg package version"); + fail_if(!g_str_equal(pkg->name, "budgie-desktop"), "Invalid eopkg package name"); + fail_if(!g_str_equal(pkg->version, "8.1"), "Invalid eopkg package version"); fail_if(pkg->release != 41, "Invalid eopkg package release"); - fail_if(!plugin->is_patched(pkg, "CVE-2014-5461"), - "Failed to detect eopkg CVE patch"); + fail_if(!plugin->is_patched(pkg, "CVE-2014-5461"), "Failed to detect eopkg CVE patch"); - fail_if(!plugin->is_ignored(pkg, "CVE-2013-4459"), - "Failed to detect ignored CVE"); - fail_if(plugin->is_ignored(pkg, "CVE-2013-0012"), - "Incorrectly detected non-ignored CVE"); + fail_if(!plugin->is_ignored(pkg, "CVE-2013-4459"), "Failed to detect ignored CVE"); + fail_if(plugin->is_ignored(pkg, "CVE-2013-0012"), "Incorrectly detected non-ignored CVE"); package_free(pkg, plugin); } @@ -190,14 +175,11 @@ START_TEST(cve_pkgbuild_test) pkg = plugin->scan_package(TOP_DIR "/tests/dummy_data/pkgbuild/PKGBUILD"); fail_if(!pkg, "Failed to inspect PKGBUILD spec!"); - fail_if(!g_str_equal(pkg->name, "my-test-package"), - "Invalid PKGBUILD package name"); - fail_if(!g_str_equal(pkg->version, "1.0.3"), - "Invalid PKGBUILD package version"); + fail_if(!g_str_equal(pkg->name, "my-test-package"), "Invalid PKGBUILD package name"); + fail_if(!g_str_equal(pkg->version, "1.0.3"), "Invalid PKGBUILD package version"); fail_if(pkg->release != 10, "Invalid PKGBUILD package release"); - fail_if(!plugin->is_patched(pkg, "CVE-2014-5461"), - "Failed to detect pkgbuild CVE patch"); + fail_if(!plugin->is_patched(pkg, "CVE-2014-5461"), "Failed to detect pkgbuild CVE patch"); package_free(pkg, plugin); } diff --git a/tests/check-template.c b/tests/check-template.c index 2fbddfb..7ac6b81 100644 --- a/tests/check-template.c +++ b/tests/check-template.c @@ -10,12 +10,12 @@ #define _GNU_SOURCE #include -#include #include +#include #include "cve-string.c" -#include "template.c" #include "hashmap.c" +#include "template.c" #include "config.h" @@ -38,8 +38,10 @@ START_TEST(cve_template_basic) g_hash_table_insert(table, "key3", "Hobbit"); g_hash_table_insert(table, "location", "Mordor"); - const char *str = "{{key1}}{{key2}} was a curious {{key3}}. With hindsight it is likely he would have avoided {{location}}."; - const char *exp = "Frodo Baggins was a curious Hobbit. With hindsight it is likely he would have avoided Mordor."; + const char *str = + "{{key1}}{{key2}} was a curious {{key3}}. With hindsight it is likely he would have avoided {{location}}."; + const char *exp = + "Frodo Baggins was a curious Hobbit. With hindsight it is likely he would have avoided Mordor."; ret = template_string(str, table); fail_if(!ret, "Unable to allocate string"); fail_if(!cve_string_const_equal(ret, exp), "Built string does not match expected template output"); @@ -64,7 +66,8 @@ START_TEST(cve_template_context) template_context_add_string(child, "number", "42"); template_context_add_subcontext(top, "section", child); - const char *input = "The name is {{name}}{{#norender}}Output{{/norender}}{{#section}} {{name}} {{number}}{{/section}}"; + const char *input = + "The name is {{name}}{{#norender}}Output{{/norender}}{{#section}} {{name}} {{number}}{{/section}}"; const char *exp = "The name is correctname correctname 42"; ret = template_context_process_line(top, input, false); @@ -75,14 +78,16 @@ START_TEST(cve_template_context) /* HTMLish */ input = "table a:visited { color: #999999 ; }"; ret = template_context_process_line(top, input, false); - fail_if(!cve_string_const_equal(ret, input), "Returned context string does not match expected Style template output"); + fail_if(!cve_string_const_equal(ret, input), + "Returned context string does not match expected Style template output"); cve_string_free(ret); template_context_free(top); /* bool checks */ top = template_context_new(); - input = "{{#bool1}}Hello Universe{{/bool1}}{{#bool2}}Hello World{{/bool2}}{{#ignored}}Should not emit{{/ignored}}"; + input = + "{{#bool1}}Hello Universe{{/bool1}}{{#bool2}}Hello World{{/bool2}}{{#ignored}}Should not emit{{/ignored}}"; exp = "Hello World"; template_context_add_bool(top, "bool1", false); template_context_add_bool(top, "bool2", true); @@ -142,18 +147,15 @@ START_TEST(cve_template_list) node = template_context_new(); template_context_add_string(node, "key1", "val1"); - fail_if(!template_context_add_list(top, "list", node), - "Failed to add node to list"); + fail_if(!template_context_add_list(top, "list", node), "Failed to add node to list"); node = template_context_new(); template_context_add_string(node, "key1", "val2"); - fail_if(!template_context_add_list(top, "list", node), - "Failed to add node to list"); + fail_if(!template_context_add_list(top, "list", node), "Failed to add node to list"); node = template_context_new(); template_context_add_string(node, "key1", "val3"); - fail_if(!template_context_add_list(top, "list", node), - "Failed to add node to list"); + fail_if(!template_context_add_list(top, "list", node), "Failed to add node to list"); exp = "list items: val1 val2 val3 "; ret = template_context_process_line(top, "list items: {{#list}}{{key1}} {{/list}}", false); @@ -178,8 +180,10 @@ START_TEST(cve_template_bool) template_context_add_string(top, "name", "Frodo"); exp = "His name was Frodo: true"; - ret = template_context_process_line(top, - "His name was {{#conditiontrue}}{{name}}{{/conditiontrue}}{{#nocond}}Jimbob{{/nocond}}: {{conditiontrue}}", false); + ret = template_context_process_line( + top, + "His name was {{#conditiontrue}}{{name}}{{/conditiontrue}}{{#nocond}}Jimbob{{/nocond}}: {{conditiontrue}}", + false); fail_if(!ret, "No return from template_context_process_line"); fail_if(!cve_string_const_equal(ret, exp), "Returned bool-test string does not match expected output"); cve_string_free(ret);