Cast first argument for %.*s to int
[strongswan.git] / src / libimcv / plugins / imv_os / pacman.c
index 7a3a53c..f5f5288 100644 (file)
@@ -20,6 +20,9 @@
 #include <string.h>
 #include <errno.h>
 #include <syslog.h>
+#include <time.h>
+
+#include "imv_os_state.h"
 
 #include <library.h>
 #include <utils/debug.h>
@@ -81,17 +84,53 @@ static void cleanup(void)
 static void usage(void)
 {
        printf("Usage:\n"
-                  "ipsec pacman --file <filename> --package <name>\n");
+                  "ipsec pacman --product <name> --file <filename> [--update]\n");
+}
+
+/**
+ * Extract the time the package file was generated
+ */
+static time_t extract_time(char *line)
+{
+       struct tm t;
+       char wday[4], mon[4];
+       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;
+       }
+       t.tm_isdst = 0;
+       t.tm_year -= 1900;
+       t.tm_mon = 12;
+
+       for (i = 0; i < countof(months); i++)
+       {
+               if (streq(mon, months[i]))
+               {
+                       t.tm_mon = i;
+                       break;
+               }
+       }
+       if (t.tm_mon == 12)
+       {
+               return UNDEFINED_TIME;
+       }
+
+       return mktime(&t) - timezone;
 }
 
 /**
  * Process a package file and store updates in the database
  */
-static void process_packages(char *filename, char *product)
+static void process_packages(char *filename, char *product, bool update)
 {
-       char *uri, line[1024], *pos;
-       int count = 0, errored = 0, vulnerable = 0;
-       int new_packages = 0, new_versions = 0, updates = 0, reverted = 0;
+       char *uri, line[12288], *pos;
+       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;
@@ -102,7 +141,7 @@ static void process_packages(char *filename, char *product)
        file = fopen(filename, "r");
        if (!file)
        {
-               fprintf(stderr, "could not open \"%s\"", filename);
+               fprintf(stderr, "could not open \"%s\"\n", filename);
                exit(EXIT_FAILURE);
        }
 
@@ -134,7 +173,7 @@ static void process_packages(char *filename, char *product)
                e->destroy(e);
        }
        if (!pid)
-       {       
+       {
                if (db->execute(db, &pid, "INSERT INTO products (name) VALUES (?)",
                                                DB_TEXT, product) != 1)
                {
@@ -149,15 +188,30 @@ static void process_packages(char *filename, char *product)
        while (fgets(line, sizeof(line), file))
        {
                char *package, *version;
-               bool security;
-               int current_security;
-               u_int32_t gid = 0, vid = 0;
+               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 == 3)
+               if (count == 1)
                {
                        printf("%s", line);
                }
+               if (count == 3)
+               {
+                       gen_time = extract_time(line);
+
+                       if (gen_time == UNDEFINED_TIME)
+                       {
+                               fprintf(stderr, "could not extract generation time\n");
+                               fclose(file);
+                               db->destroy(db);
+                               exit(EXIT_FAILURE);
+                       }
+                       printf("Generated: %T\n", &gen_time, TRUE);
+               }
                if (count < 7)
                {
                        continue;
@@ -167,14 +221,47 @@ static void process_packages(char *filename, char *product)
                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;
                }
                *pos++ = '\0';
                package = line;
-               version = "";
+
+               /* look for version string in parentheses */
+               if (*pos == '(')
+               {
+                       version = ++pos;
+                       pos = strchr(pos, ')');
+                       if (pos)
+                       {
+                               *pos++ = '\0';
+                       }
+                       else
+                       {
+                               fprintf(stderr, "could not extract package version from "
+                                               "'%.*s'\n", (int)(strlen(line)-1), line);
+                               errored++;
+                               continue;
+                       }
+               }
+               else
+               {
+                       /* no version information, skip entry */
+                       continue;
+               }
+               security = (strstr(pos, "[security]") != NULL);
+               if (security)
+               {
+                       vulnerable++;
+               }
+
+               /* handle non-security packages in update mode only */
+               if (!update && !security)
+               {
+                       continue;
+               }
 
                /* check if package is already in database */
                e = db->query(db, "SELECT id FROM packages WHERE name = ?",
@@ -187,8 +274,8 @@ static void process_packages(char *filename, char *product)
                        }
                        e->destroy(e);
                }
-               if (!gid)
-               {       
+               if (!gid && security)
+               {
                        if (db->execute(db, &gid, "INSERT INTO packages (name) VALUES (?)",
                                                                DB_TEXT, package) != 1)
                        {
@@ -201,92 +288,143 @@ static void process_packages(char *filename, char *product)
                        new_packages++;
                }
 
-               /* look for version string in parentheses */
-               if (*pos == '(')
+               /* check for package versions already in database */
+               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)
                {
-                       version = ++pos;
-                       pos = strchr(pos, ')');
-                       if (pos)
+                       break;
+               }
+               while (e->enumerate(e, &vid, &cur_version, &cur_security, &cur_time))
+               {
+                       if (streq(version, cur_version))
                        {
-                               *pos++ = '\0'; 
+                               /* 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
                        {
-                               fprintf(stderr, "could not extract package version from '%.*s'",
-                                       strlen(line)-1, line);
-                               errored++;
-                               continue;
+                               if (security == cur_security)
+                               {
+                                       add_version = FALSE;
+                               }
                        }
                }
-               security = (strstr(pos, "[security]") != NULL);
-               if (security)
+               e->destroy(e);
+
+               if ((!vid && !security) || (vid && !add_version))
                {
-                       vulnerable++;
+                       free(version_update);
+                       free(version_delete);
+                       continue;
                }
 
-               /* check if version is already in database */
-               e = db->query(db, "SELECT id, security FROM versions "
-                                                 "WHERE release = ? AND package = ? AND product = ?",
-                                                 DB_TEXT, version, DB_INT, pid, DB_INT, gid,
-                                                 DB_INT, DB_INT);
-               if (e)
+               if ((!vid && security) || (vid && !vid_update))
                {
-                       if (!e->enumerate(e, &vid, &current_security))
-                       {
-                               vid = 0;
-                       }
-                       e->destroy(e);
-               }
-               if (!vid)
-               {       
-                       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)
+                       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);
+                               free(version_update);
+                               free(version_delete);
                                fclose(file);
                                db->destroy(db);
                                exit(EXIT_FAILURE);
                        }
                        new_versions++;
                }
-               else if (current_security != security)
+               else
                {
-                       printf("'%s' (%s) %s\n", package, version, security ? "[s]" : "");
+                       printf("%s (%s) %s updated by\n",
+                                  package, version_update, security_update ? "[s]" : "");
+                       printf("%s (%s) %s\n", package, version, security ? "[s]" : "");
 
-                       if (security)
+                       if (db->execute(db, NULL,
+                               "UPDATE versions SET release = ?, time = ? WHERE id = ?",
+                               DB_TEXT, version, DB_INT, gen_time, DB_INT, vid_update) <= 0)
                        {
-                               if (db->execute(db, NULL,
-                                       "UPDATE versions SET security = ? WHERE vid = ?",
-                                       DB_INT, security, DB_INT, vid)  < 0)
-                               {
-                                       fprintf(stderr, "could not store update security field\n");
-                                       fclose(file);
-                                       db->destroy(db);
-                                       exit(EXIT_FAILURE);
-                               }
-                               updates++;
+                               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);
                        }
-                       else
+                       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)
                        {
-                               reverted++;
+                               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);
-       printf("processed %d packages, %d vulnerable, %d errored, "
-                  "%d new packages, %d new versions, %d updates, %d reverted\n",
-                       count - 6, vulnerable, errored, new_packages, new_versions,
-                       updates, reverted);     
+
+       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[])
 {
        char *filename = NULL, *product = NULL;
+       bool update = FALSE;
 
        /* reinit getopt state */
        optind = 0;
@@ -299,6 +437,7 @@ static void do_args(int argc, char *argv[])
                        { "help", no_argument, NULL, 'h' },
                        { "file", required_argument, NULL, 'f' },
                        { "product", required_argument, NULL, 'p' },
+                       { "update", no_argument, NULL, 'u' },
                        { 0,0,0,0 }
                };
 
@@ -316,13 +455,16 @@ static void do_args(int argc, char *argv[])
                        case 'p':
                                product = optarg;
                                continue;
+                       case 'u':
+                               update = TRUE;
+                               continue;
                }
                break;
        }
 
        if (filename && product)
        {
-               process_packages(filename, product);
+               process_packages(filename, product, update);
        }
        else
        {