Cast first argument for %.*s to int
[strongswan.git] / src / libimcv / plugins / imv_os / pacman.c
index 1b8e53d..f5f5288 100644 (file)
@@ -22,6 +22,8 @@
 #include <syslog.h>
 #include <time.h>
 
 #include <syslog.h>
 #include <time.h>
 
+#include "imv_os_state.h"
+
 #include <library.h>
 #include <utils/debug.h>
 
 #include <library.h>
 #include <utils/debug.h>
 
@@ -82,7 +84,7 @@ static void cleanup(void)
 static void usage(void)
 {
        printf("Usage:\n"
 static void usage(void)
 {
        printf("Usage:\n"
-                  "ipsec pacman --file <filename> --package <name>\n");
+                  "ipsec pacman --product <name> --file <filename> [--update]\n");
 }
 
 /**
 }
 
 /**
@@ -95,13 +97,13 @@ static time_t extract_time(char *line)
        char* months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                                           "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
        int i;
        char* months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                                           "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
        int i;
-       
+
        if (sscanf(line, "Generated: %3s %3s %2d %2d:%2d:%2d %4d UTC", wday, mon,
                           &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec, &t.tm_year) != 7)
        {
                return UNDEFINED_TIME;
        }
        if (sscanf(line, "Generated: %3s %3s %2d %2d:%2d:%2d %4d UTC", wday, mon,
                           &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec, &t.tm_year) != 7)
        {
                return UNDEFINED_TIME;
        }
-       t.tm_isdst = 0; 
+       t.tm_isdst = 0;
        t.tm_year -= 1900;
        t.tm_mon = 12;
 
        t.tm_year -= 1900;
        t.tm_mon = 12;
 
@@ -127,8 +129,8 @@ static time_t extract_time(char *line)
 static void process_packages(char *filename, char *product, bool update)
 {
        char *uri, line[12288], *pos;
 static void process_packages(char *filename, char *product, bool update)
 {
        char *uri, line[12288], *pos;
-       int count = 0, errored = 0, vulnerable = 0;
-       int new_packages = 0, new_versions = 0;
+       int count = 0, errored = 0, vulnerable = 0, new_packages = 0;
+       int new_versions = 0, updated_versions = 0, deleted_versions = 0;
        u_int32_t pid = 0;
        enumerator_t *e;
        database_t *db;
        u_int32_t pid = 0;
        enumerator_t *e;
        database_t *db;
@@ -171,7 +173,7 @@ static void process_packages(char *filename, char *product, bool update)
                e->destroy(e);
        }
        if (!pid)
                e->destroy(e);
        }
        if (!pid)
-       {       
+       {
                if (db->execute(db, &pid, "INSERT INTO products (name) VALUES (?)",
                                                DB_TEXT, product) != 1)
                {
                if (db->execute(db, &pid, "INSERT INTO products (name) VALUES (?)",
                                                DB_TEXT, product) != 1)
                {
@@ -185,11 +187,12 @@ static void process_packages(char *filename, char *product, bool update)
 
        while (fgets(line, sizeof(line), file))
        {
 
        while (fgets(line, sizeof(line), file))
        {
-               char *package, *version, *current_version;
-               bool security, update_version = TRUE;
-               int current_security;
-               u_int32_t gid = 0, vid = 0;
-               time_t generation_time;
+               char *package, *version;
+               char *cur_version, *version_update = NULL, *version_delete = NULL;
+               bool security, add_version = TRUE;
+               int cur_security, security_update = 0, security_delete = 0;
+               u_int32_t gid = 0, vid = 0, vid_update = 0, vid_delete = 0;
+               time_t gen_time, cur_time;
 
                count++;
                if (count == 1)
 
                count++;
                if (count == 1)
@@ -198,16 +201,16 @@ static void process_packages(char *filename, char *product, bool update)
                }
                if (count == 3)
                {
                }
                if (count == 3)
                {
-                       generation_time = extract_time(line);
+                       gen_time = extract_time(line);
 
 
-                       if (generation_time == UNDEFINED_TIME)
+                       if (gen_time == UNDEFINED_TIME)
                        {
                                fprintf(stderr, "could not extract generation time\n");
                                fclose(file);
                                db->destroy(db);
                                exit(EXIT_FAILURE);
                        }
                        {
                                fprintf(stderr, "could not extract generation time\n");
                                fclose(file);
                                db->destroy(db);
                                exit(EXIT_FAILURE);
                        }
-                       printf("Generated: %T\n", &generation_time, TRUE);
+                       printf("Generated: %T\n", &gen_time, TRUE);
                }
                if (count < 7)
                {
                }
                if (count < 7)
                {
@@ -218,8 +221,8 @@ static void process_packages(char *filename, char *product, bool update)
                pos = strchr(line, ' ');
                if (!pos)
                {
                pos = strchr(line, ' ');
                if (!pos)
                {
-                       fprintf(stderr, "could not extract package name from '%.*s'",
-                                       strlen(line)-1, line);
+                       fprintf(stderr, "could not extract package name from '%.*s'\n",
+                                       (int)(strlen(line)-1), line);
                        errored++;
                        continue;
                }
                        errored++;
                        continue;
                }
@@ -237,8 +240,8 @@ static void process_packages(char *filename, char *product, bool update)
                        }
                        else
                        {
                        }
                        else
                        {
-                               fprintf(stderr, "could not extract package version from '%.*s'\n",
-                                       strlen(line)-1, line);
+                               fprintf(stderr, "could not extract package version from "
+                                               "'%.*s'\n", (int)(strlen(line)-1), line);
                                errored++;
                                continue;
                        }
                                errored++;
                                continue;
                        }
@@ -272,7 +275,7 @@ static void process_packages(char *filename, char *product, bool update)
                        e->destroy(e);
                }
                if (!gid && security)
                        e->destroy(e);
                }
                if (!gid && security)
-               {       
+               {
                        if (db->execute(db, &gid, "INSERT INTO packages (name) VALUES (?)",
                                                                DB_TEXT, package) != 1)
                        {
                        if (db->execute(db, &gid, "INSERT INTO packages (name) VALUES (?)",
                                                                DB_TEXT, package) != 1)
                        {
@@ -286,44 +289,136 @@ static void process_packages(char *filename, char *product, bool update)
                }
 
                /* check for package versions already in database */
                }
 
                /* check for package versions already in database */
-               e = db->query(db, "SELECT id, release, security FROM versions "
-                                                 "WHERE package = ? AND product = ?",
-                                                 DB_INT, gid, DB_INT, pid, DB_INT, DB_TEXT, DB_INT);
-               if (e)
+               e = db->query(db,
+                               "SELECT id, release, security, time FROM versions "
+                               "WHERE package = ? AND product = ?",
+                               DB_INT, gid, DB_INT, pid, DB_INT, DB_TEXT, DB_INT, DB_INT);
+               if (!e)
+               {
+                       break;
+               }
+               while (e->enumerate(e, &vid, &cur_version, &cur_security, &cur_time))
                {
                {
-                       while (e->enumerate(e, &vid, &current_version, &current_security))
+                       if (streq(version, cur_version))
+                       {
+                               /* already in data base */
+                               add_version = FALSE;
+                               break;
+                       }
+                       else if (gen_time > cur_time)
+                       {
+                               if (security)
+                               {
+                                       if (cur_security)
+                                       {
+                                               vid_update = vid;
+                                               version_update = strdup(cur_version);
+                                               security_update = cur_security;
+                                       }
+                                       else
+                                       {
+                                               vid_delete = vid;
+                                               version_delete = strdup(cur_version);
+                                               security_delete = cur_security;
+                                       }
+                               }
+                               else
+                               {
+                                       if (!cur_security)
+                                       {
+                                               vid_update = vid;
+                                               version_update = strdup(cur_version);
+                                               security_update = cur_security;
+                                       }
+                               }
+                       }
+                       else
                        {
                        {
-                               if (streq(version, current_version))
+                               if (security == cur_security)
                                {
                                {
-                                       update_version = FALSE;
+                                       add_version = FALSE;
                                }
                        }
                                }
                        }
-                       e->destroy(e);
                }
                }
-               if ((!vid && security) || (vid && update_version))
-               {       
-                       printf("'%s' (%s) %s\n", package, version, security ? "[s]" : "");
-
-                       if (db->execute(db, &gid,
-                               "INSERT INTO versions (package, product, release, security) "
-                               "VALUES (?, ?, ?, ?)", DB_INT, gid, DB_INT, pid,
-                               DB_TEXT, version, DB_INT, security) != 1)
+               e->destroy(e);
+
+               if ((!vid && !security) || (vid && !add_version))
+               {
+                       free(version_update);
+                       free(version_delete);
+                       continue;
+               }
+
+               if ((!vid && security) || (vid && !vid_update))
+               {
+                       printf("%s (%s) %s\n", package, version, security ? "[s]" : "");
+
+                       if (db->execute(db, &vid,
+                               "INSERT INTO versions "
+                               "(package, product, release, security, time) "
+                               "VALUES (?, ?, ?, ?, ?)", DB_INT, gid, DB_INT, pid,
+                               DB_TEXT, version, DB_INT, security, DB_INT, gen_time) != 1)
                        {
                                fprintf(stderr, "could not store version '%s' to database\n",
                                                                 version);
                        {
                                fprintf(stderr, "could not store version '%s' to database\n",
                                                                 version);
+                               free(version_update);
+                               free(version_delete);
                                fclose(file);
                                db->destroy(db);
                                exit(EXIT_FAILURE);
                        }
                        new_versions++;
                }
                                fclose(file);
                                db->destroy(db);
                                exit(EXIT_FAILURE);
                        }
                        new_versions++;
                }
-       }
+               else
+               {
+                       printf("%s (%s) %s updated by\n",
+                                  package, version_update, security_update ? "[s]" : "");
+                       printf("%s (%s) %s\n", package, version, security ? "[s]" : "");
+
+                       if (db->execute(db, NULL,
+                               "UPDATE versions SET release = ?, time = ? WHERE id = ?",
+                               DB_TEXT, version, DB_INT, gen_time, DB_INT, vid_update) <= 0)
+                       {
+                               fprintf(stderr, "could not update version '%s' to database\n",
+                                                                version);
+                               free(version_update);
+                               free(version_delete);
+                               fclose(file);
+                               db->destroy(db);
+                               exit(EXIT_FAILURE);
+                       }
+                       updated_versions++;
+               }
+
+               if (vid_delete)
+               {
+                       printf("%s (%s) %s deleted\n",
+                                  package, version_delete, security_delete ? "[s]" : "");
 
 
+                       if (db->execute(db, NULL,
+                               "DELETE FROM  versions WHERE id = ?",
+                               DB_INT, vid_delete) <= 0)
+                       {
+                               fprintf(stderr, "could not delete version '%s' from database\n",
+                                                                version_delete);
+                               free(version_update);
+                               free(version_delete);
+                               fclose(file);
+                               db->destroy(db);
+                               exit(EXIT_FAILURE);
+                       }
+                       deleted_versions++;
+               }
+               free(version_update);
+               free(version_delete);
+       }
        fclose(file);
        db->destroy(db);
        fclose(file);
        db->destroy(db);
-       printf("processed %d packages, %d vulnerable, %d errored, "
-                  "%d new packages, %d new versions\n", count - 6, vulnerable,
-                       errored, new_packages, new_versions);
+
+       printf("processed %d packages, %d security, %d new packages, "
+                  "%d new versions, %d updated versions, %d deleted versions, "
+                  "%d errored\n", count - 6, vulnerable, new_packages, new_versions,
+                  updated_versions, deleted_versions, errored);
 }
 
 static void do_args(int argc, char *argv[])
 }
 
 static void do_args(int argc, char *argv[])