Set device creation date if it hasn't been set yet
authorAndreas Steffen <andreas.steffen@strongswan.org>
Mon, 24 Jun 2013 18:18:16 +0000 (20:18 +0200)
committerAndreas Steffen <andreas.steffen@strongswan.org>
Mon, 24 Jun 2013 18:18:16 +0000 (20:18 +0200)
src/libimcv/imv/imv_policy_manager.c

index 1a74da2..8379105 100644 (file)
@@ -21,6 +21,7 @@
 
 #include <stdlib.h>
 #include <stdio.h>
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <time.h>
 
 /**
  * global debug output variables
 
 /**
  * global debug output variables
@@ -52,13 +53,15 @@ bool policy_start(database_t *db, int session_id)
        enumerator_t *e;
        int id, gid, device_id, product_id, group_id = 0, parent;
        int type, file, dir, arg_int, rec_fail, rec_noresult;
        enumerator_t *e;
        int id, gid, device_id, product_id, group_id = 0, parent;
        int type, file, dir, arg_int, rec_fail, rec_noresult;
+       u_int created;
        char *argument;
 
        /* get session data */
        e = db->query(db,
        char *argument;
 
        /* get session data */
        e = db->query(db,
-                       "SELECT device, product FROM sessions WHERE id = ? ",
-                        DB_INT, session_id, DB_INT, DB_INT);
-       if (!e || !e->enumerate(e, &device_id, &product_id))
+                       "SELECT s.device, s.product, d.created FROM sessions AS s "
+                       "LEFT JOIN devices AS d ON s.device = d.id WHERE s.id = ?",
+                        DB_INT, session_id, DB_INT, DB_INT, DB_UINT);
+       if (!e || !e->enumerate(e, &device_id, &product_id, &created))
        {
                DESTROY_IF(e);
                fprintf(stderr, "session %d not found\n", session_id);
        {
                DESTROY_IF(e);
                fprintf(stderr, "session %d not found\n", session_id);
@@ -80,6 +83,18 @@ bool policy_start(database_t *db, int session_id)
                        }
                        e->destroy(e);
                }
                        }
                        e->destroy(e);
                }
+
+               /* set the creation date if hasn't been set yet */
+               if (!created)
+               {
+                       if (db->execute(db, NULL,
+                                       "UPDATE devices SET created = ? WHERE id = ?",
+                                       DB_UINT, time(NULL), DB_INT, device_id) != 1)
+                       {
+                               fprintf(stderr, "creation date of device could not be set\n");
+                               return FALSE;
+                       }
+               }
        }
 
        /* if no group membership found, try default product group */
        }
 
        /* if no group membership found, try default product group */