Some formatting changes
[moonshot-ui.git] / src / moonshot-keyring-store-base.vala
1 /*
2  * Copyright (c) 2011-2016, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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
30  * SUCH DAMAGE.
31 */
32 using Gee;
33
34 #if GNOME_KEYRING || LIBSECRET_KEYRING
35
36 public abstract class KeyringStoreBase : Object, IIdentityCardStore {
37     protected static MoonshotLogger logger = get_logger("KeyringStore");
38
39     protected LinkedList<IdCard> id_card_list;
40     internal const string keyring_store_attribute = "Moonshot";
41     internal const string keyring_store_version = "1.0";
42
43     /*
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
48      * gnome keyring code
49     */
50     protected class Attributes: GLib.HashTable<string, string> {
51         public Attributes() {
52             base.full(GLib.str_hash, GLib.str_equal, GLib.g_free, GLib.g_free);
53         }
54     }
55
56     protected static Attributes match_attributes;
57
58     protected static IdCard deserialize(GLib.HashTable<string,string> attrs, string? secret)
59     {
60         IdCard id_card = new IdCard();
61         unowned string store_password = attrs.lookup("StorePassword");
62         unowned string ca_cert = attrs.lookup("CA-Cert") ?? "";
63         unowned string server_cert = attrs.lookup("Server-Cert") ?? "";
64         unowned string subject = attrs.lookup("Subject") ?? "";
65         unowned string subject_alt = attrs.lookup("Subject-Alt") ?? "";
66         unowned string ta_datetime_added = attrs.lookup("TA_DateTime_Added");
67
68         id_card.issuer = attrs.lookup("Issuer");
69         id_card.username = attrs.lookup("Username");
70         id_card.display_name = attrs.lookup("DisplayName");
71         unowned string services = attrs.lookup("Services");
72         if ((services != null) && services != "") {
73             id_card.update_services(services.split(";"));
74         }
75         var ta = new TrustAnchor(ca_cert, server_cert, subject, subject_alt);
76         if (ta_datetime_added != null) {
77             ta.set_datetime_added(ta_datetime_added);
78         }
79         id_card.set_trust_anchor_from_store(ta);
80
81         unowned string rules_pattern_all = attrs.lookup("Rules-Pattern");
82         unowned string rules_always_confirm_all = attrs.lookup("Rules-AlwaysConfirm");
83         if ((rules_pattern_all != null) && (rules_always_confirm_all != null)) {
84             string[] rules_patterns = rules_pattern_all.split(";");
85             string[] rules_always_confirm = rules_always_confirm_all.split(";");
86             if (rules_patterns.length == rules_always_confirm.length) {
87                 Rule[] rules = new Rule[rules_patterns.length];
88                 for (int i = 0; i < rules_patterns.length; i++) {
89                     rules[i].pattern = (owned) rules_patterns[i];
90                     rules[i].always_confirm = (owned) rules_always_confirm[i];
91                 }
92                 id_card.rules = rules;
93             }
94         }
95
96         if (store_password != null)
97             id_card.store_password = (store_password == "yes");
98         else
99             id_card.store_password = ((secret != null) && (secret != ""));
100
101         if (id_card.store_password)
102             id_card.password = secret;
103         else
104             id_card.password = null;
105
106         return id_card;
107     }
108
109     internal static Attributes serialize(IdCard id_card)
110     {
111         /* workaround for Centos vala array property bug: use temp array */
112         var rules = id_card.rules;
113         string[] rules_patterns = new string[rules.length];
114         string[] rules_always_conf = new string[rules.length];
115
116         for (int i = 0; i < rules.length; i++) {
117             rules_patterns[i] = rules[i].pattern;
118             rules_always_conf[i] = rules[i].always_confirm;
119         }
120         string patterns = string.joinv(";", rules_patterns);
121         string always_conf = string.joinv(";", rules_always_conf);
122         string services = id_card.get_services_string(";");
123         Attributes attributes = new Attributes();
124         attributes.insert(keyring_store_attribute, keyring_store_version);
125         attributes.insert("Issuer", id_card.issuer);
126         attributes.insert("Username", id_card.username);
127         attributes.insert("DisplayName", id_card.display_name);
128         attributes.insert("Services", services);
129         attributes.insert("Rules-Pattern", patterns);
130         attributes.insert("Rules-AlwaysConfirm", always_conf);
131         attributes.insert("CA-Cert", id_card.trust_anchor.ca_cert);
132         attributes.insert("Server-Cert", id_card.trust_anchor.server_cert);
133         attributes.insert("Subject", id_card.trust_anchor.subject);
134         attributes.insert("Subject-Alt", id_card.trust_anchor.subject_alt);
135         attributes.insert("TA_DateTime_Added", id_card.trust_anchor.datetime_added);
136         attributes.insert("StorePassword", id_card.store_password ? "yes" : "no");
137         return attributes;
138     }
139
140     class construct {
141         match_attributes = new Attributes();
142         match_attributes.insert(keyring_store_attribute, keyring_store_version);
143     }
144
145     public void add_card(IdCard card) {
146         logger.trace("add_card: Adding card '%s' with services: '%s'"
147                      .printf(card.display_name, card.get_services_string("; ")));
148         id_card_list.add(card);
149         store_id_cards();
150     }
151
152     public IdCard? update_card(IdCard card) {
153         logger.trace("update_card");
154
155         id_card_list.remove(card);
156         id_card_list.add(card);
157
158         store_id_cards();
159         foreach (IdCard idcard in id_card_list) {
160             if (idcard.display_name == card.display_name) {
161                 return idcard;
162             }
163         }
164
165         logger.error(@"update_card: card '$(card.display_name)' was not found after re-loading!");
166         return null;
167     }
168
169     public bool remove_card(IdCard card) {
170         bool retval = id_card_list.remove(card);
171         if (retval)
172             store_id_cards();
173         return retval;
174     }
175
176     public IIdentityCardStore.StoreType get_store_type() {
177         return IIdentityCardStore.StoreType.KEYRING;
178     }
179
180     public LinkedList<IdCard> get_card_list() {
181         return id_card_list;
182     }
183
184     protected abstract void clear_keyring();
185     protected abstract void load_id_cards() throws GLib.Error;
186     internal abstract void store_id_cards();
187
188     public KeyringStoreBase() {
189         id_card_list = new LinkedList<IdCard>();
190         try {
191             load_id_cards();
192         } catch( GLib.Error e) {
193             stdout.printf("Unable to load ID cards: %s\n", e.message);
194         }
195     }
196 }
197
198 #endif