Working libsecret keystore
[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
57     protected static Attributes match_attributes;
58
59     protected static IdCard deserialize(GLib.HashTable<string,string> attrs, string? secret)
60     {
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"];
68
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(";"));
75         }
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);
79             }
80         id_card.set_trust_anchor_from_store(ta);
81
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];
92                 }
93                 id_card.rules = rules;
94             }
95         }
96         
97         if (store_password != null)
98             id_card.store_password = (store_password == "yes");
99         else
100             id_card.store_password = ((secret != null) && (secret != ""));
101
102         if (id_card.store_password)
103             id_card.password = secret;
104         else
105             id_card.password = null;
106
107
108             
109
110         return id_card;
111     }
112
113     internal static Attributes serialize(IdCard id_card)
114     {
115         /* workaround for Centos vala array property bug: use temp array */
116         var rules = id_card.rules;
117         string[] rules_patterns = new string[rules.length];
118         string[] rules_always_conf = new string[rules.length];
119             
120         for (int i = 0; i < rules.length; i++) {
121             rules_patterns[i] = rules[i].pattern;
122             rules_always_conf[i] = rules[i].always_confirm;
123         }
124         string patterns = string.joinv(";", rules_patterns);
125         string always_conf = string.joinv(";", rules_always_conf);
126         string services = id_card.get_services_string(";");
127         Attributes attributes = new Attributes();
128         attributes.insert(keyring_store_attribute, keyring_store_version);
129         attributes.insert("Issuer", id_card.issuer);
130         attributes.insert("Username", id_card.username);
131         attributes.insert("DisplayName", id_card.display_name);
132         attributes.insert("Services", services);
133         attributes.insert("Rules-Pattern", patterns);
134         attributes.insert("Rules-AlwaysConfirm", always_conf);
135         attributes.insert("CA-Cert", id_card.trust_anchor.ca_cert);
136         attributes.insert("Server-Cert", id_card.trust_anchor.server_cert);
137         attributes.insert("Subject", id_card.trust_anchor.subject);
138         attributes.insert("Subject-Alt", id_card.trust_anchor.subject_alt);
139         attributes.insert("TA_DateTime_Added", id_card.trust_anchor.datetime_added);
140         attributes.insert("StorePassword", id_card.store_password ? "yes" : "no");
141         return attributes;
142     }
143
144     class construct {
145         match_attributes = new Attributes();
146         match_attributes.insert(keyring_store_attribute, keyring_store_version);
147     }
148
149     public void add_card(IdCard card) {
150         logger.trace("add_card: Adding card '%s' with services: '%s'"
151                      .printf(card.display_name, card.get_services_string("; ")));
152
153         id_card_list.add(card);
154         store_id_cards();
155     }
156
157     public IdCard? update_card(IdCard card) {
158         logger.trace("update_card");
159
160         id_card_list.remove(card);
161         id_card_list.add(card);
162
163         store_id_cards();
164         foreach (IdCard idcard in id_card_list) {
165             if (idcard.display_name == card.display_name) {
166                 return idcard;
167             }
168         }
169
170         logger.error(@"update_card: card '$(card.display_name)' was not found after re-loading!");
171         return null;
172     }
173
174     public bool remove_card(IdCard card) {
175         bool retval = id_card_list.remove(card);
176         if (retval)
177             store_id_cards();
178         return retval;
179     }
180
181     public IIdentityCardStore.StoreType get_store_type() {
182         return IIdentityCardStore.StoreType.KEYRING;
183     }
184
185     public LinkedList<IdCard> get_card_list() {
186         return id_card_list;
187     }
188
189     protected abstract void clear_keyring();     
190     protected abstract void load_id_cards() throws GLib.Error;
191     internal abstract void store_id_cards();
192
193     
194
195
196     public KeyringStoreBase() {
197         id_card_list = new LinkedList<IdCard>();
198         try {
199             load_id_cards();
200         } catch( GLib.Error e) {
201             stdout.printf("Unable to load ID cards: %s\n", e.message);
202         }
203         
204     }
205 }
206
207 #endif