Skip to content
This repository was archived by the owner on Jan 6, 2023. It is now read-only.

Commit ad8011a

Browse files
committed
Merge pull request #33 from popovich-sergei/next
Next
2 parents f956b1e + 832cc26 commit ad8011a

File tree

8 files changed

+89
-49
lines changed

8 files changed

+89
-49
lines changed

src/library/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ typedef enum {
7272
typedef struct source_package_t {
7373
void *name; /**<Our name for the package */
7474
void *version; /**<Our current version */
75-
gchar *path; /**<Filesystem path of source path */
75+
char *path; /**<Filesystem path of source path */
7676
int release; /**<Highest (available) release number */
7777
GList *issues; /**<List of applicable (non-patched) CVEs */
7878
GList *patched; /**<List of *patched* applicable CVEs */

src/main.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ int main(int argc, char **argv)
457457
autofree(GOptionContext) *context = NULL;
458458
autofree(char) *target_sz = NULL;
459459
autofree(cve_string) *target = NULL;
460-
autofree(gchar) *db_path = NULL;
460+
autofree(cve_string) *db_path = NULL;
461461
autofree(CveDB) *cve_db = NULL;
462462
GList *pkg_plugins = NULL;
463463
int ret = EXIT_FAILURE;
@@ -492,11 +492,11 @@ int main(int argc, char **argv)
492492

493493
db_path = get_db_path(nvds);
494494
if (!db_path) {
495-
fprintf(stderr, "main(): Out of memory\n");
495+
fprintf(stderr, "main(): Can't get db path\n");
496496
goto cleanup_no_lock;
497497
}
498498

499-
db_locked = cve_db_lock_init(db_path);
499+
db_locked = cve_db_lock_init(db_path->str);
500500
if (!db_locked) {
501501
fprintf(stderr, "Not continuing without a database %s\n", "lock");
502502
goto cleanup_no_lock;
@@ -509,27 +509,27 @@ int main(int argc, char **argv)
509509
}
510510

511511
if (!skip_update) {
512-
int status = update_required(db_path);
512+
int status = update_required(db_path->str);
513513
if (status < 0) {
514514
fprintf(stderr, "Failed to check if db requires update\n");
515515
goto cleanup;
516516
}
517517
if (status) {
518518
fprintf(stderr, "Update of db forced\n");
519519
cve_db_unlock();
520-
if (!update_db(quiet, db_path)) {
520+
if (!update_db(quiet, db_path->str)) {
521521
fprintf(stderr, "DB update failure\n");
522522
goto cleanup;
523523
}
524524
}
525525
} else {
526-
if (!cve_file_exists(db_path)) {
526+
if (!cve_file_exists(db_path->str)) {
527527
fprintf(stderr, "Not continuing without a database %s\n", "file");
528528
goto cleanup;
529529
}
530530
}
531531

532-
cve_db = cve_db_new(db_path);
532+
cve_db = cve_db_new(db_path->str);
533533
if (!cve_db) {
534534
fprintf(stderr, "main(): DB initialisation issue\n");
535535
goto cleanup;

src/plugins/packaging/eopkg/eopkg.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct source_package_t *eopkg_inspect_pspec(const char *filename)
3535
xmlChar *source_name = NULL;
3636
int release = -1;
3737
xmlChar *version = NULL;
38-
autofree(gchar) *fpath = NULL;
38+
char *fpath = NULL;
3939

4040
doc = xmlReadFile(filename, NULL, 0);
4141
if (!doc) {
@@ -107,19 +107,21 @@ struct source_package_t *eopkg_inspect_pspec(const char *filename)
107107
if (!version || !source_name) {
108108
goto clean;
109109
}
110-
fpath = g_path_get_dirname(filename);
110+
111+
fpath = cve_get_file_parent(filename);
111112
if (!fpath) {
112113
goto clean;
113114
}
114115

115116
t = calloc(1, sizeof(struct source_package_t));
116117
if (!t) {
118+
free(fpath);
117119
goto clean;
118120
}
119121
t->name = xmlStrdup(source_name);
120122
t->version = xmlStrdup(version);
121123
t->release = release;
122-
t->path = realpath(fpath, NULL);
124+
t->path = fpath;
123125
t->xml = true; /* Ensure xmlFree is used */
124126
t->type = PACKAGE_TYPE_EOPKG;
125127

src/plugins/packaging/pkgbuild/pkgbuild.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ struct source_package_t *pkgbuild_inspect_spec(const char *filename)
3636

3737
autofree(GDataInputStream) *dis = g_data_input_stream_new(G_INPUT_STREAM(fis));
3838
char *read = NULL;
39+
char *fpath = NULL;
3940
autofree(gchar) *name = NULL;
4041
autofree(gchar) *version = NULL;
4142
autofree(gchar) *release = NULL;
42-
autofree(gchar) *fpath = NULL;
4343

4444
while ((read = g_data_input_stream_read_line(dis, NULL, NULL, NULL)) != NULL) {
4545
autofree(gstrv) *strv = NULL;
@@ -80,19 +80,20 @@ struct source_package_t *pkgbuild_inspect_spec(const char *filename)
8080
return NULL;
8181
}
8282

83-
fpath = g_path_get_dirname(filename);
83+
fpath = cve_get_file_parent(filename);
8484
if (!fpath) {
8585
return NULL;
8686
}
8787

8888
t = calloc(1, sizeof(struct source_package_t));
8989
if (!t) {
90+
free(fpath);
9091
return NULL;
9192
}
9293
t->name = g_strdup(name);
9394
t->version = g_strdup(version);
9495
t->release = atoi(release);
95-
t->path = realpath(fpath, NULL);
96+
t->path = fpath;
9697
t->type = PACKAGE_TYPE_PKGBUILD;
9798
return t;
9899
}

src/plugins/packaging/rpm/rpm.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ struct source_package_t *rpm_inspect_spec(const char *filename)
4747

4848
autofree(GDataInputStream) *dis = g_data_input_stream_new(G_INPUT_STREAM(fis));
4949
char *read = NULL;
50+
char *fpath = NULL;
5051
autofree(gchar) *name = NULL;
5152
autofree(gchar) *version = NULL;
5253
autofree(gchar) *release = NULL;
5354
autofree(CveHashmap) *macros = NULL;
54-
autofree(gchar) *fpath = NULL;
5555
GList *lpatches = NULL;
5656

5757
while ((read = g_data_input_stream_read_line(dis, NULL, NULL, NULL)) != NULL) {
@@ -172,7 +172,7 @@ struct source_package_t *rpm_inspect_spec(const char *filename)
172172
return NULL;
173173
}
174174

175-
fpath = g_path_get_dirname(filename);
175+
fpath = cve_get_file_parent(filename);
176176
if (!fpath) {
177177
return NULL;
178178
}
@@ -185,12 +185,13 @@ struct source_package_t *rpm_inspect_spec(const char *filename)
185185

186186
t = calloc(1, sizeof(struct source_package_t));
187187
if (!t) {
188+
free(fpath);
188189
return NULL;
189190
}
190191
t->name = g_strdup(name);
191192
t->version = g_strdup(version);
192193
t->release = atoi(release);
193-
t->path = realpath(fpath, NULL);
194+
t->path = fpath;
194195
t->type = PACKAGE_TYPE_RPM;
195196
t->extra = lpatches;
196197

src/update-main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ int main(int argc, char **argv)
5858
{
5959
autofree(GError) *error = NULL;
6060
autofree(GOptionContext) *context = NULL;
61-
autofree(gchar) *db_path = NULL;
61+
autofree(cve_string) *db_path = NULL;
6262
int ret = EXIT_FAILURE;
6363
bool db_locked;
6464

@@ -78,17 +78,17 @@ int main(int argc, char **argv)
7878

7979
db_path = get_db_path(nvds);
8080
if (!db_path) {
81-
fprintf(stderr, "main(): Out of memory\n");
81+
fprintf(stderr, "main(): Can't get db path\n");
8282
goto end;
8383
}
8484

85-
db_locked = cve_db_lock_init(db_path);
85+
db_locked = cve_db_lock_init(db_path->str);
8686
if (!db_locked) {
8787
fputs("Not continuing without a database lock\n", stderr);
8888
goto end;
8989
}
9090

91-
if (update_db(_quiet, db_path)) {
91+
if (update_db(_quiet, db_path->str)) {
9292
ret = EXIT_SUCCESS;
9393
} else {
9494
fprintf(stderr, "Failed to update database\n");

src/update.c

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
#include <stdint.h>
2121
#include <fcntl.h>
2222
#include <unistd.h>
23+
#include <time.h>
2324
#include <utime.h>
2425
#include <errno.h>
25-
#include <glib.h>
26+
#include <pwd.h>
2627
#include <gio/gio.h>
2728
#include <curl/curl.h>
2829
#include <openssl/sha.h>
@@ -44,36 +45,61 @@
4445
#define UPDATE_THRESHOLD 7200
4546
#define UPDATE_DB_MARKER_SUFFIX "cve.update_db"
4647

47-
gchar *get_db_path(const gchar *path)
48+
static const char *get_home_dir(void)
49+
{
50+
const char *home;
51+
52+
home = getenv("HOME");
53+
if (!home) {
54+
struct passwd *p;
55+
56+
p = getpwuid(getuid());
57+
if (p) {
58+
home = p->pw_dir;
59+
if (home && !*home) {
60+
home = NULL;
61+
}
62+
}
63+
}
64+
65+
return home;
66+
}
67+
68+
cve_string *get_db_path(const char *path)
4869
{
4970
const mode_t mode = S_IRWXU|S_IRWXG|S_IRWXO;
50-
gchar *dir, *ret = NULL;
71+
const char *dir;
72+
autofree(cve_string) *d = NULL;
5173

5274
if (!path || !*path) {
53-
const gchar *home = g_get_home_dir();
54-
dir = g_strdup_printf("%s/%s", home, nvd_dir);
75+
const char *home;
76+
77+
home = get_home_dir();
78+
if (!home) {
79+
return NULL;
80+
}
81+
82+
d = cve_string_dup_printf("%s/%s", home, nvd_dir);
83+
if (!d) {
84+
return NULL;
85+
}
86+
87+
dir = d->str;
5588
} else {
56-
dir = (gchar *) path;
89+
dir = path;
5790
}
5891

5992
if (mkdir(dir, mode)) {
60-
struct stat st = { .st_ino = 0, };
61-
6293
if (errno != EEXIST) {
63-
goto end;
94+
return NULL;
6495
}
6596

66-
if (stat(dir, &st) || !S_ISDIR(st.st_mode)) {
67-
goto end;
97+
if (!cve_is_dir(dir)) {
98+
return NULL;
6899
}
69100
}
70101

71-
ret = g_strdup_printf("%s/%s", dir, nvd_file);
72-
end:
73-
if (dir != path) {
74-
g_free(dir);
75-
}
76-
return ret;
102+
return cve_string_dup_printf("%s/%s", dir, nvd_file);
77103
}
78104

79105
static cve_string *nvdcve_make_fname(int year, const char *fext)
@@ -366,22 +392,35 @@ static int do_fetch_update(int year, const char *db_dir, CveDB *cve_db,
366392

367393
bool update_db(bool quiet, const char *db_file)
368394
{
369-
autofree(gchar) *db_dir = NULL;
395+
autofree(char) *db_dir = NULL;
370396
autofree(CveDB) *cve_db = NULL;
371-
autofree(GDateTime) *date = NULL;
372397
autofree(cve_string) *u_fname = NULL;
398+
struct tm *tm;
399+
time_t t;
373400
int u_handle = -1;
374401
int year;
375402
bool ret = false;
376403
bool db_exist = false;
377404
bool db_locked = false;
378405

406+
t = time(NULL);
407+
if (t == (time_t) -1) {
408+
goto time;
409+
}
410+
411+
tm = localtime(&t);
412+
if (!tm) {
413+
goto time;
414+
}
415+
416+
year = tm->tm_year + 1900;
417+
379418
u_fname = make_db_dot_fname(db_file, UPDATE_DB_MARKER_SUFFIX);
380419
if (!u_fname) {
381420
goto oom;
382421
}
383422

384-
db_dir = g_path_get_dirname(db_file);
423+
db_dir = cve_get_file_parent(db_file);
385424
if (!db_dir) {
386425
goto oom;
387426
}
@@ -412,9 +451,6 @@ bool update_db(bool quiet, const char *db_file)
412451
goto end;
413452
}
414453

415-
date = g_date_time_new_now_local();
416-
year = g_date_time_get_year(date);
417-
418454
if (!cve_db_begin(cve_db)) {
419455
fprintf(stderr, "Failed to initialise DB\n");
420456
goto end;
@@ -460,6 +496,9 @@ bool update_db(bool quiet, const char *db_file)
460496
oom:
461497
fputs("update_db(): Out of memory\n", stderr);
462498
goto end;
499+
time:
500+
fputs("Can't get local time\n", stderr);
501+
goto end;
463502
}
464503

465504
/*

src/update.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@
99
* (at your option) any later version.
1010
*/
1111

12-
#define _GNU_SOURCE
13-
#include <stdio.h>
14-
#include <stdlib.h>
15-
#include <stdbool.h>
12+
#include "cve-string.h"
1613

17-
gchar *get_db_path(const gchar *path);
14+
cve_string *get_db_path(const char *path);
1815

1916
int update_required(const char *db_file);
2017

0 commit comments

Comments
 (0)