|
20 | 20 | #include <stdint.h> |
21 | 21 | #include <fcntl.h> |
22 | 22 | #include <unistd.h> |
| 23 | +#include <time.h> |
23 | 24 | #include <utime.h> |
24 | 25 | #include <errno.h> |
25 | | -#include <glib.h> |
| 26 | +#include <pwd.h> |
26 | 27 | #include <gio/gio.h> |
27 | 28 | #include <curl/curl.h> |
28 | 29 | #include <openssl/sha.h> |
|
44 | 45 | #define UPDATE_THRESHOLD 7200 |
45 | 46 | #define UPDATE_DB_MARKER_SUFFIX "cve.update_db" |
46 | 47 |
|
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) |
48 | 69 | { |
49 | 70 | 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; |
51 | 73 |
|
52 | 74 | 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; |
55 | 88 | } else { |
56 | | - dir = (gchar *) path; |
| 89 | + dir = path; |
57 | 90 | } |
58 | 91 |
|
59 | 92 | if (mkdir(dir, mode)) { |
60 | | - struct stat st = { .st_ino = 0, }; |
61 | | - |
62 | 93 | if (errno != EEXIST) { |
63 | | - goto end; |
| 94 | + return NULL; |
64 | 95 | } |
65 | 96 |
|
66 | | - if (stat(dir, &st) || !S_ISDIR(st.st_mode)) { |
67 | | - goto end; |
| 97 | + if (!cve_is_dir(dir)) { |
| 98 | + return NULL; |
68 | 99 | } |
69 | 100 | } |
70 | 101 |
|
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); |
77 | 103 | } |
78 | 104 |
|
79 | 105 | 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, |
366 | 392 |
|
367 | 393 | bool update_db(bool quiet, const char *db_file) |
368 | 394 | { |
369 | | - autofree(gchar) *db_dir = NULL; |
| 395 | + autofree(char) *db_dir = NULL; |
370 | 396 | autofree(CveDB) *cve_db = NULL; |
371 | | - autofree(GDateTime) *date = NULL; |
372 | 397 | autofree(cve_string) *u_fname = NULL; |
| 398 | + struct tm *tm; |
| 399 | + time_t t; |
373 | 400 | int u_handle = -1; |
374 | 401 | int year; |
375 | 402 | bool ret = false; |
376 | 403 | bool db_exist = false; |
377 | 404 | bool db_locked = false; |
378 | 405 |
|
| 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 | + |
379 | 418 | u_fname = make_db_dot_fname(db_file, UPDATE_DB_MARKER_SUFFIX); |
380 | 419 | if (!u_fname) { |
381 | 420 | goto oom; |
382 | 421 | } |
383 | 422 |
|
384 | | - db_dir = g_path_get_dirname(db_file); |
| 423 | + db_dir = cve_get_file_parent(db_file); |
385 | 424 | if (!db_dir) { |
386 | 425 | goto oom; |
387 | 426 | } |
@@ -412,9 +451,6 @@ bool update_db(bool quiet, const char *db_file) |
412 | 451 | goto end; |
413 | 452 | } |
414 | 453 |
|
415 | | - date = g_date_time_new_now_local(); |
416 | | - year = g_date_time_get_year(date); |
417 | | - |
418 | 454 | if (!cve_db_begin(cve_db)) { |
419 | 455 | fprintf(stderr, "Failed to initialise DB\n"); |
420 | 456 | goto end; |
@@ -460,6 +496,9 @@ bool update_db(bool quiet, const char *db_file) |
460 | 496 | oom: |
461 | 497 | fputs("update_db(): Out of memory\n", stderr); |
462 | 498 | goto end; |
| 499 | +time: |
| 500 | + fputs("Can't get local time\n", stderr); |
| 501 | + goto end; |
463 | 502 | } |
464 | 503 |
|
465 | 504 | /* |
|
0 commit comments