Reskinned Identity Selector per the JANET Wireframes documents.
[moonshot-ui.git] / src / moonshot-server.vala
index 67c22ab..4fbcd2a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011-2014, JANET(UK)
+ * Copyright (c) 2011-2016, JANET(UK)
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
 */
+
+using Gee;
+
 #if IPC_DBUS
 
 [DBus (name = "org.janet.Moonshot")]
 public class MoonshotServer : Object {
 
+    static MoonshotLogger logger = get_logger("MoonshotServer");
+
+    private string app_name = "Moonshot";
+
     private IdentityManagerApp parent_app;
 
     public MoonshotServer(IdentityManagerApp app)
     {
+        logger.trace("MoonshotServer.<constructor>; app=" + (app == null ? "null" : "non-null"));
         this.parent_app = app;
     }
 
     public bool show_ui()
     {
+        logger.trace("MoonshotServer.show_ui");
+
         if (parent_app.view == null) {
+            stderr.printf(app_name, "show_ui: parent_app.view is null!\n");
+            logger.warn("show_ui: parent_app.view is null!");
             return false;
         }
         parent_app.show();
         parent_app.explicitly_launched = true;
+        logger.trace("MoonshotServer.show_ui: returning true");
         return true;
     }
 
@@ -61,13 +74,17 @@ public class MoonshotServer : Object {
                                    out string subject_name_constraint,
                                    out string subject_alt_name_constraint)
     {
+        logger.trace(@"MoonshotServer.get_identity: nai='$nai'; service='$service'");
         var request = new IdentityRequest(parent_app,
                                           nai,
                                           password,
                                           service);
+        logger.trace(@"MoonshotServer.get_identity: Calling request.execute()");
         request.set_callback((IdentityRequest) => get_identity.callback());
         request.execute();
+        logger.trace(@"MoonshotServer.get_identity: Back from request.execute()");
         yield;
+        logger.trace(@"MoonshotServer.get_identity: back from yield");
 
         nai_out = "";
         password_out = "";
@@ -78,9 +95,9 @@ public class MoonshotServer : Object {
 
         var id_card = request.id_card;
 
-        if ((id_card != null) && (id_card.display_name != IdCard.NO_IDENTITY)) {
+        if ((id_card != null) && (!id_card.is_no_identity())) {
             nai_out = id_card.nai;
-            if ((request.password!=null) && (request.password != ""))
+            if ((request.password != null) && (request.password != ""))
                 password_out = request.password;
             else
                 password_out = id_card.password;
@@ -103,9 +120,12 @@ public class MoonshotServer : Object {
             if (subject_alt_name_constraint == null)
                 subject_alt_name_constraint = "";
 
+            logger.trace(@"MoonshotServer.get_identity: returning with nai_out=$nai_out");
+
             return true;
         }
 
+        logger.trace("MoonshotServer.get_identity: returning false");
         return false;
     }
 
@@ -116,6 +136,7 @@ public class MoonshotServer : Object {
                                            out string subject_name_constraint,
                                            out string subject_alt_name_constraint)
     {
+        logger.trace("MoonshotServer.get_default_identity");
         var request = new IdentityRequest.default(parent_app);
         request.set_callback((IdentityRequest) => get_default_identity.callback());
         request.execute();
@@ -151,24 +172,25 @@ public class MoonshotServer : Object {
             if (subject_alt_name_constraint == null)
                 subject_alt_name_constraint = "";
 
+            logger.trace("MoonshotServer.get_default_identity: returning true");
             return true;
         }
 
         return false;
     }
 
-    public bool install_id_card (string   display_name,
-                                 string   user_name,
-                                 string   ?password,
-                                 string   ?realm,
-                                 string[] ?rules_patterns,
-                                 string[] ?rules_always_confirm,
-                                 string[] ?services,
-                                 string   ?ca_cert,
-                                 string   ?subject,
-                                 string   ?subject_alt,
-                                 string   ?server_cert,
-                                 int      force_flat_file_store)
+    public bool install_id_card(string   display_name,
+                                string   user_name,
+                                string   ?password,
+                                string   ?realm,
+                                string[] ?rules_patterns,
+                                string[] ?rules_always_confirm,
+                                string[] ?services,
+                                string   ?ca_cert,
+                                string   ?subject,
+                                string   ?subject_alt,
+                                string   ?server_cert,
+                                int      force_flat_file_store)
     {
         IdCard idcard = new IdCard();
 
@@ -178,11 +200,22 @@ public class MoonshotServer : Object {
         if ((password != null) && (password != ""))
             idcard.store_password = true;
         idcard.issuer = realm;
-        idcard.services = services;
-        idcard.trust_anchor.ca_cert = ca_cert;
-        idcard.trust_anchor.subject = subject;
-        idcard.trust_anchor.subject_alt = subject_alt;
-        idcard.trust_anchor.server_cert = server_cert;
+        idcard.update_services(services);
+        var ta = new TrustAnchor(ca_cert, server_cert, subject, subject_alt, false);
+
+        if (!ta.is_empty()) {
+            // We have to set the datetime_added here, because it isn't delivered via IPC.
+            string ta_datetime_added = TrustAnchor.format_datetime_now();
+            ta.set_datetime_added(ta_datetime_added);
+            logger.trace("install_id_card : Set ta_datetime_added for '%s' to '%s'; ca_cert='%s'; server_cert='%s'".printf(idcard.display_name, ta.datetime_added, ta.ca_cert, ta.server_cert));
+        }
+        idcard.set_trust_anchor_from_store(ta);
+
+        logger.trace("install_id_card: Card '%s' has services: '%s'"
+                     .printf(idcard.display_name, idcard.get_services_string("; ")));
+
+        logger.trace(@"Installing IdCard named '$(idcard.display_name)'; ca_cert='$(idcard.trust_anchor.ca_cert)'; server_cert='$(idcard.trust_anchor.server_cert)'");
+
 
         if (rules_patterns.length == rules_always_confirm.length)
         {
@@ -197,7 +230,17 @@ public class MoonshotServer : Object {
             idcard.rules = rules;
         }
 
-        return parent_app.add_identity(idcard, force_flat_file_store!=0);
+        ArrayList<IdCard>? old_duplicates = null;
+        var ret = parent_app.add_identity(idcard, (force_flat_file_store != 0), out old_duplicates);
+
+        if (old_duplicates != null) {
+            // Printing to stdout here is ugly behavior; but it's old behavior that
+            // may be expected. (TODO: Do we need to keep this?)
+            foreach (IdCard id_card in old_duplicates) {
+                stdout.printf("removed duplicate id for '%s'\n", id_card.nai);
+            }
+        }
+        return ret;
     }
 
 
@@ -208,7 +251,7 @@ public class MoonshotServer : Object {
         webp.parse();
         bool result = false;
         int installed_cards = 0;
-        foreach (IdCard card in WebProvisioning.cards)
+        foreach (IdCard card in webp.cards)
         {
             string[] rules_patterns = {};
             string[] rules_always_confirm = {};
@@ -226,13 +269,25 @@ public class MoonshotServer : Object {
                 }
             } 
 
+
+            // prevent a crash by holding the reference to otherwise
+            // unowned array(?)
+
+            // string[] svcs = card.services.to_array();
+            // string[] svcs = card.services.to_array()[:];
+            string[] svcs = new string[card.services.size];
+            for (int i = 0; i < card.services.size; i++) {
+                svcs[i] = card.services[i];
+            }
+
+            logger.trace(@"install_from_file: Adding card with display name '$(card.display_name)'");
             result = install_id_card(card.display_name,
                                      card.username,
                                      card.password,
                                      card.issuer,
                                      rules_patterns,
                                      rules_always_confirm,
-                                     card.services,
+                                     svcs,
                                      card.trust_anchor.ca_cert,
                                      card.trust_anchor.subject,
                                      card.trust_anchor.subject_alt,
@@ -290,6 +345,8 @@ public class MoonshotServer : Object {
                                     ref string subject_name_constraint,
                                     ref string subject_alt_name_constraint)
     {
+        logger.trace("(static) get_identity");
+
         bool result = false;
 
         var request = new IdentityRequest(parent_app,
@@ -355,6 +412,8 @@ public class MoonshotServer : Object {
                                             ref string subject_name_constraint,
                                             ref string subject_alt_name_constraint)
     {
+        logger.trace("(static) get_default_identity");
+
         bool result;
 
         var request = new IdentityRequest.default(parent_app);
@@ -428,7 +487,9 @@ public class MoonshotServer : Object {
                                        string     server_cert,
                                        bool       force_flat_file_store)
     {
+        logger.trace("(static) install_id_card");
         IdCard idcard = new IdCard();
+
         bool success = false;
         Mutex mutex = new Mutex();
         Cond cond = new Cond();
@@ -456,10 +517,14 @@ public class MoonshotServer : Object {
 
         mutex.lock();
 
+        ArrayList<IdCard>? old_duplicates = null;
         // Defer addition to the main loop thread.
         Idle.add(() => {
                 mutex.lock();
-                success = parent_app.add_identity(idcard, force_flat_file_store);
+                success = parent_app.add_identity(idcard, force_flat_file_store, out old_duplicates);
+                foreach (IdCard id_card in old_duplicates) {
+                    stdout.printf("removing duplicate id for '%s'\n", new_card.nai);
+                }
                 cond.signal();
                 mutex.unlock();
                 return false;