Support import in the Moonshot UI
authorDan Breslau <dbreslau@painless-security.com>
Tue, 23 Aug 2016 16:35:14 +0000 (12:35 -0400)
committerDan Breslau <dbreslau@painless-security.com>
Tue, 23 Aug 2016 16:35:14 +0000 (12:35 -0400)
src/moonshot-identity-management-view.vala
src/moonshot-identity-manager-app.vala
src/moonshot-provisioning-common.vala
src/moonshot-server.vala

index b10ff0c..9f4e6bc 100644 (file)
 */
 using Gee;
 using Gtk;
+using WebProvisioning;
 
 public class IdentityManagerView : Window {
     static MoonshotLogger logger = get_logger("IdentityManagerView");
 
+    bool use_flat_file_store = false;
+
     // The latest year in which Moonshot sources were modified.
     private static int LATEST_EDIT_YEAR = 2016;
 
@@ -67,6 +70,8 @@ public class IdentityManagerView : Window {
 
     private IdCard selected_idcard = null;
 
+    private string import_directory = null;
+
     private enum Columns
     {
         IDCARD_COL,
@@ -84,8 +89,10 @@ public class IdentityManagerView : Window {
     "        </menu>" +
     "</menubar>";
 
-    public IdentityManagerView(IdentityManagerApp app) {
+    public IdentityManagerView(IdentityManagerApp app, bool use_flat_file_store) {
         parent_app = app;
+        this.use_flat_file_store = use_flat_file_store;
+
         #if OS_MACOS
             osxApp = OSXApplication.get_instance();
         #endif
@@ -247,28 +254,6 @@ public class IdentityManagerView : Window {
                        Columns.PASSWORD_COL, id_card.password);
     }
 
-    // private void remove_id_card_data(IdCard id_card)
-    // {
-    //     TreeIter iter;
-    //     string issuer;
-
-    //     if (listmodel->get_iter_first(out iter))
-    //     {
-    //         do
-    //         {
-    //             listmodel->get(iter,
-    //                            Columns.ISSUER_COL, out issuer);
-
-    //             if (id_card.issuer == issuer)
-    //             {
-    //                 listmodel->remove(iter);
-    //                 break;
-    //             }
-    //         }
-    //         while (listmodel->iter_next(ref iter));
-    //     }
-    // }
-
     private IdCardWidget add_id_card_widget(IdCard id_card)
     {
         logger.trace("add_id_card_widget: id_card.nai='%s'; selected nai='%s'"
@@ -790,7 +775,11 @@ SUCH DAMAGE.
         var add_button = new Button.with_label(_("Add"));
         add_button.clicked.connect((w) => {add_identity_cb();});
         top_table.attach(make_rigid(add_button), num_cols - button_width, num_cols, row, row + 1, fill, fill, 0, 0);
-        logger.trace("build_ui: row spacing for row %d is %u".printf(row, top_table.get_row_spacing(row)));
+        row++;
+
+        var import_button = new Button.with_label(_("Import"));
+        import_button.clicked.connect((w) => {import_identities_cb();});
+        top_table.attach(make_rigid(import_button), num_cols - button_width, num_cols, row, row + 1, fill, fill, 0, 0);
         row++;
 
         this.edit_button = new Button.with_label(_("Edit"));
@@ -893,4 +882,57 @@ SUCH DAMAGE.
         return fixed_height;
     }
 
+    private void import_identities_cb() {
+        var dialog = new FileChooserDialog("Import File",
+                                           this,
+                                           FileChooserAction.OPEN,
+                                           _("Cancel"),ResponseType.CANCEL,
+                                           _("Save"), ResponseType.ACCEPT,
+                                           null);
+
+        if (import_directory != null) {
+            dialog.set_current_folder(import_directory);
+        }
+
+        if (dialog.run() == ResponseType.ACCEPT)
+        {
+            // Save the parent directory to use as default for next save
+            string filename = dialog.get_filename();
+            var file  = File.new_for_path(filename);
+            import_directory = file.get_parent().get_path();
+
+            int import_count = 0;
+
+            var webp = new Parser(filename);
+            dialog.destroy();
+            webp.parse();
+            logger.trace(@"import_identities_cb: Have $(webp.cards.length) IdCards");
+            foreach (IdCard card in webp.cards)
+            {
+
+                if (card == null) {
+                    logger.trace(@"import_identities_cb: Skipping null IdCard");
+                    continue;
+                }
+
+                bool result = add_identity(card, use_flat_file_store);
+                if (result) {
+                    logger.trace(@"import_identities_cb: Added or updated '$(card.display_name)'");
+                    import_count++;
+                }
+                else {
+                    logger.trace(@"import_identities_cb: Did not add or update '$(card.display_name)'");
+                }
+            }
+            var msg_dialog = new Gtk.MessageDialog(this,
+                                               Gtk.DialogFlags.DESTROY_WITH_PARENT,
+                                               Gtk.MessageType.INFO,
+                                               Gtk.ButtonsType.OK,
+                                               _("Import completed. %d Identities were added or updated."),
+                                               import_count);
+            msg_dialog.run();
+            msg_dialog.destroy();
+        }
+    }
+
 }
index 4de64be..507ca8c 100644 (file)
@@ -108,7 +108,7 @@ public class IdentityManagerApp {
             model.set_store_type(IIdentityCardStore.StoreType.KEYRING);
 
         if (!headless)
-            view = new IdentityManagerView(this);
+            view = new IdentityManagerView(this, use_flat_file_store);
         LinkedList<IdCard> card_list = model.get_card_list();
         if (card_list.size > 0)
             this.default_id_card = card_list.last();
index d780eeb..98b52df 100644 (file)
@@ -121,107 +121,114 @@ namespace WebProvisioning
 
     public class Parser : Object
     {
-        // private static MoonshotLogger logger = new MoonshotLogger("WebProvisioning");
+        private static MoonshotLogger logger = new MoonshotLogger("WebProvisioning");
 
         private void start_element_func(MarkupParseContext context,
                                         string element_name,
                                         string[] attribute_names,
                                         string[] attribute_values) throws MarkupError
+        {
+            if (element_name == "identity")
             {
-                if (element_name == "identity")
-                {
-                    card = new IdCard();
-                    _cards += card;
-
-                    ta_ca_cert = "";
-                    ta_server_cert = "";
-                    ta_subject = "";
-                    ta_subject_alt = "";
-                }
-                else if (element_name == "rule")
-                {
-                    card.add_rule(Rule());
-                }
+                card = new IdCard();
+                _cards += card;
+
+                ta_ca_cert = "";
+                ta_server_cert = "";
+                ta_subject = "";
+                ta_subject_alt = "";
+            }
+            else if (element_name == "rule")
+            {
+                card.add_rule(Rule());
             }
+        }
 
-            private void end_element_func(MarkupParseContext context,
-                                          string element_name) throws MarkupError
+        private void end_element_func(MarkupParseContext context,
+                                      string element_name) throws MarkupError
+        {
+            if (element_name == "identity")
             {
-                if (element_name == "identity")
-                {
-                    if (ta_ca_cert != "" || ta_server_cert != "") {
-                        var ta = new TrustAnchor(ta_ca_cert,
-                                                 ta_server_cert,
-                                                 ta_subject,
-                                                 ta_subject_alt,
-                                                 false);
+                if (ta_ca_cert != "" || ta_server_cert != "") {
+                    var ta = new TrustAnchor(ta_ca_cert,
+                                             ta_server_cert,
+                                             ta_subject,
+                                             ta_subject_alt,
+                                             false);
+                    if (!ta.is_empty()) {
+                        string ta_datetime_added = TrustAnchor.format_datetime_now();
+                        ta.set_datetime_added(ta_datetime_added);
+                        logger.trace("end_element_func : Set ta_datetime_added for '%s' to '%s'".printf(card.display_name, ta_datetime_added));
                         card.set_trust_anchor_from_store(ta);
                     }
+
+                    card.set_trust_anchor_from_store(ta);
                 }
             }
+        }
 
-            private void
-            text_element_func(MarkupParseContext context,
-                              string             text,
-                              size_t             text_len) throws MarkupError {
-                unowned SList<string> stack = context.get_element_stack();
+        private void
+        text_element_func(MarkupParseContext context,
+                          string             text,
+                          size_t             text_len) throws MarkupError {
+            unowned SList<string> stack = context.get_element_stack();
 
-                if (text_len < 1)
-                    return;
+            if (text_len < 1)
+                return;
 
-                if (stack.nth_data(0) == "display-name" && display_name_handler(stack))
-                {
-                    card.display_name = text;
-                }
-                else if (stack.nth_data(0) == "user" && user_handler(stack))
-                {
-                    card.username = text;
-                }
-                else if (stack.nth_data(0) == "password" && password_handler(stack))
-                {
-                    card.password = text;
-                }
-                else if (stack.nth_data(0) == "realm" && realm_handler(stack))
-                {
-                    card.issuer = text;
-                }
-                else if (stack.nth_data(0) == "service")
-                {
-                    card.services.add(text);
-                }
+            if (stack.nth_data(0) == "display-name" && display_name_handler(stack))
+            {
+                card.display_name = text;
+            }
+            else if (stack.nth_data(0) == "user" && user_handler(stack))
+            {
+                card.username = text;
+            }
+            else if (stack.nth_data(0) == "password" && password_handler(stack))
+            {
+                card.password = text;
+            }
+            else if (stack.nth_data(0) == "realm" && realm_handler(stack))
+            {
+                card.issuer = text;
+            }
+            else if (stack.nth_data(0) == "service")
+            {
+                card.services.add(text);
+            }
 
-                /* Rules */
-                else if (stack.nth_data(0) == "pattern" && pattern_handler(stack))
-                {
-                    /* use temp array to workaround valac 0.10 bug accessing array property length */
+            /* Rules */
+            else if (stack.nth_data(0) == "pattern" && pattern_handler(stack))
+            {
+                /* use temp array to workaround valac 0.10 bug accessing array property length */
+                var temp = card.rules;
+                card.rules[temp.length - 1].pattern = text;
+            }
+            else if (stack.nth_data(0) == "always-confirm" && always_confirm_handler(stack))
+            {
+                if (text == "true" || text == "false") {
+                    /* use temp array to workaround valac 0.10 bug accessing array property length*/
                     var temp = card.rules;
-                    card.rules[temp.length - 1].pattern = text;
-                }
-                else if (stack.nth_data(0) == "always-confirm" && always_confirm_handler(stack))
-                {
-                    if (text == "true" || text == "false") {
-                        /* use temp array to workaround valac 0.10 bug accessing array property length*/
-                        var temp = card.rules;
-                        card.rules[temp.length - 1].always_confirm = text;
-                    }
-                }
-                else if (stack.nth_data(0) == "ca-cert" && ca_cert_handler(stack))
-                {
-                    ta_ca_cert = text ?? "";
-                }
-                else if (stack.nth_data(0) == "server-cert" && server_cert_handler(stack))
-                {
-                    ta_server_cert = text ?? "";
-                }
-                else if (stack.nth_data(0) == "subject" && subject_handler(stack))
-                {
-                    ta_subject = text;
-                }
-                else if (stack.nth_data(0) == "subject-alt" && subject_alt_handler(stack))
-                {
-                    ta_subject_alt = text;
+                    card.rules[temp.length - 1].always_confirm = text;
                 }
             }
+            else if (stack.nth_data(0) == "ca-cert" && ca_cert_handler(stack))
+            {
+                ta_ca_cert = text ?? "";
+            }
+            else if (stack.nth_data(0) == "server-cert" && server_cert_handler(stack))
+            {
+                ta_server_cert = text ?? "";
+            }
+            else if (stack.nth_data(0) == "subject" && subject_handler(stack))
+            {
+                ta_subject = text;
+            }
+            else if (stack.nth_data(0) == "subject-alt" && subject_alt_handler(stack))
+            {
+                ta_subject_alt = text;
+            }
+        }
 
         private const MarkupParser parser = {
             start_element_func, end_element_func, text_element_func, null, null
index 7c95ff7..54a5bff 100644 (file)
@@ -199,12 +199,6 @@ public class MoonshotServer : Object {
         idcard.issuer = realm;
         idcard.update_services(services);
         var ta = new TrustAnchor(ca_cert, server_cert, subject, subject_alt, false);
-        if (!ta.is_empty()) {
-            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'".printf(idcard.display_name, ta_datetime_added));
-            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("; ")));