2 * Copyright (c) 2011-2016, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #if GNOME_KEYRING || LIBSECRET_KEYRING
36 public abstract class KeyringStoreBase : Object, IIdentityCardStore {
37 protected static MoonshotLogger logger = get_logger("KeyringStore");
39 protected LinkedList<IdCard> id_card_list;
40 internal const string keyring_store_attribute = "Moonshot";
41 internal const string keyring_store_version = "1.0";
44 * This class is directly useful for the libsecret implementation.
45 * However, we convert the gnome keyring attributes into a HashTable
46 * so we can share the serialization code between the two
47 * implementations. This ends up decreasing complexity even of the
50 protected class Attributes: GLib.HashTable<string, string> {
52 base.full(GLib.str_hash, GLib.str_equal, GLib.g_free, GLib.g_free);
57 protected static Attributes match_attributes;
59 protected IdCard deserialize(GLib.HashTable<string,string> attrs, string? secret)
61 IdCard id_card = new IdCard();
62 unowned string store_password = attrs["StorePassword"];
63 unowned string ca_cert = attrs["CA-Cert"] ?? "";
64 unowned string server_cert = attrs["Server-Cert"] ?? "";
65 unowned string subject = attrs["Subject"] ?? "";
66 unowned string subject_alt = attrs["Subject-Alt"] ?? "";
67 unowned string ta_datetime_added = attrs["TA_DateTime_Added"];
69 id_card.issuer = attrs["Issuer"];
70 id_card.username = attrs["Username"];
71 id_card.display_name = attrs["DisplayName"];
72 unowned string services = attrs["Services"];
73 if ((services != null) && services != "") {
74 id_card.update_services(services.split(";"));
76 var ta = new TrustAnchor(ca_cert, server_cert, subject, subject_alt);
77 if (ta_datetime_added != null) {
78 ta.set_datetime_added(ta_datetime_added);
80 id_card.set_trust_anchor_from_store(ta);
82 unowned string rules_pattern_all = attrs["Rules-Pattern"];
83 unowned string rules_always_confirm_all = attrs["Rules-AlwaysConfirm"];
84 if ((rules_pattern_all != null) && (rules_always_confirm_all != null)) {
85 string[] rules_patterns = rules_pattern_all.split(";");
86 string[] rules_always_confirm = rules_always_confirm_all.split(";");
87 if (rules_patterns.length == rules_always_confirm.length) {
88 Rule[] rules = new Rule[rules_patterns.length];
89 for (int i = 0; i < rules_patterns.length; i++) {
90 rules[i].pattern = (owned) rules_patterns[i];
91 rules[i].always_confirm = (owned) rules_always_confirm[i];
93 id_card.rules = rules;
97 if (store_password != null)
98 id_card.store_password = (store_password == "yes");
100 id_card.store_password = ((secret != null) && (secret != ""));
102 if (id_card.store_password)
103 id_card.password = secret;
105 id_card.password = null;
114 match_attributes = new Attributes();
115 match_attributes.insert(keyring_store_attribute, keyring_store_version);
118 public void add_card(IdCard card) {
119 logger.trace("add_card: Adding card '%s' with services: '%s'"
120 .printf(card.display_name, card.get_services_string("; ")));
122 id_card_list.add(card);
126 public IdCard? update_card(IdCard card) {
127 logger.trace("update_card");
129 id_card_list.remove(card);
130 id_card_list.add(card);
133 foreach (IdCard idcard in id_card_list) {
134 if (idcard.display_name == card.display_name) {
139 logger.error(@"update_card: card '$(card.display_name)' was not found after re-loading!");
143 public bool remove_card(IdCard card) {
144 bool retval = id_card_list.remove(card);
150 public IIdentityCardStore.StoreType get_store_type() {
151 return IIdentityCardStore.StoreType.KEYRING;
154 public LinkedList<IdCard> get_card_list() {
158 protected abstract void clear_keyring();
159 protected abstract void load_id_cards() throws GLib.Error;
160 internal abstract void store_id_cards();
164 public KeyringStoreBase() {
165 id_card_list = new LinkedList<IdCard>();
168 } catch( GLib.Error e) {
169 stdout.printf("Unable to load ID cards: %s\n", e.message);