Implemented "Clear Trust Anchor" button in Edit Identity Dialog.
[moonshot-ui.git] / src / moonshot-keyring-store.vala
1 /*
2  * Copyright (c) 2011-2014, 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
35 public class KeyringStore : Object, IIdentityCardStore {
36     static MoonshotLogger logger = get_logger("KeyringStore");
37
38     private LinkedList<IdCard> id_card_list;
39     private const string keyring_store_attribute = "Moonshot";
40     private const string keyring_store_version = "1.0";
41     private const GnomeKeyring.ItemType item_type = GnomeKeyring.ItemType.GENERIC_SECRET;
42
43     public void add_card(IdCard card) {
44         logger.trace("add_card: Adding card '%s' with services: '%s'"
45                      .printf(card.display_name, card.get_services_string("; ")));
46
47         id_card_list.add(card);
48         store_id_cards();
49     }
50
51     public IdCard? update_card(IdCard card) {
52         logger.trace("update_card");
53
54         id_card_list.remove(card);
55         id_card_list.add(card);
56
57         store_id_cards();
58         foreach (IdCard idcard in id_card_list) {
59             if (idcard.display_name == card.display_name) {
60                 return idcard;
61             }
62         }
63         return null;
64     }
65
66     public bool remove_card(IdCard card) {
67         bool retval = id_card_list.remove(card);
68         if (retval)
69             store_id_cards();
70         return retval;
71     }
72
73     public IIdentityCardStore.StoreType get_store_type() {
74         return IIdentityCardStore.StoreType.KEYRING;
75     }
76
77     public LinkedList<IdCard> get_card_list() {
78         return id_card_list;
79     }
80
81     /* clear all keyring-stored ids (in preparation to store current list) */
82     private void clear_keyring() {
83         GnomeKeyring.AttributeList match = new GnomeKeyring.AttributeList();
84         match.append_string(keyring_store_attribute, keyring_store_version);
85         GLib.List<GnomeKeyring.Found> items;
86         GnomeKeyring.find_items_sync(item_type, match, out items);
87         foreach(unowned GnomeKeyring.Found entry in items) {
88             GnomeKeyring.Result result = GnomeKeyring.item_delete_sync(null, entry.item_id);
89             if (result != GnomeKeyring.Result.OK) {
90                 stdout.printf("GnomeKeyring.item_delete_sync() failed. result: %d", result);
91             }
92         }
93     }
94      
95     private void load_id_cards() {
96         id_card_list.clear();
97
98         GnomeKeyring.AttributeList match = new GnomeKeyring.AttributeList();
99         match.append_string(keyring_store_attribute, keyring_store_version);
100         GLib.List<GnomeKeyring.Found> items;
101         GnomeKeyring.find_items_sync(item_type, match, out items);
102         foreach(unowned GnomeKeyring.Found entry in items) {
103             IdCard id_card = new IdCard();
104             int i;
105             int rules_patterns_index = -1;
106             int rules_always_confirm_index = -1;
107             string store_password = null;
108             string ca_cert = "";
109             string server_cert = "";
110             string subject = "";
111             string subject_alt = "";
112             bool   user_verified = false;
113             for (i = 0; i < entry.attributes.len; i++) {
114                 var attribute = ((GnomeKeyring.Attribute *) entry.attributes.data)[i];
115                 string value = "";
116                 if (attribute.type == GnomeKeyring.AttributeType.STRING) {
117                     value = attribute.string_value;
118                 }
119
120                 if (attribute.name == "Issuer") {
121                     id_card.issuer = value;
122                 } else if (attribute.name == "Username") {
123                     id_card.username = value;
124                 } else if (attribute.name == "DisplayName") {
125                     id_card.display_name = value;
126                 } else if (attribute.name == "Services") {
127                     id_card.update_services(value.split(";"));
128                 } else if (attribute.name == "Rules-Pattern") {
129                     rules_patterns_index = i;
130                 } else if (attribute.name == "Rules-AlwaysConfirm") {
131                     rules_always_confirm_index = i;
132                 } else if (attribute.name == "CA-Cert") {
133                     ca_cert = value.strip();
134                 } else if (attribute.name == "Server-Cert") {
135                     server_cert = value;
136                 } else if (attribute.name == "Subject") {
137                     subject = value;
138                 } else if (attribute.name == "Subject-Alt") {
139                     subject_alt = value;
140                 } else if (attribute.name == "StorePassword") {
141                     store_password = value;
142                 } else if (attribute.name == "CACert_User_Verified") {
143                     user_verified = (value == "true");
144                 }
145             }
146
147             var ta = new TrustAnchor(ca_cert, server_cert, subject, subject_alt, user_verified);
148             id_card.set_trust_anchor_from_store(ta);
149
150             if ((rules_always_confirm_index != -1) && (rules_patterns_index != -1)) {
151                 string rules_patterns_all = ((GnomeKeyring.Attribute *) entry.attributes.data)[rules_patterns_index].string_value;
152                 string rules_always_confirm_all = ((GnomeKeyring.Attribute *) entry.attributes.data)[rules_always_confirm_index].string_value;
153                 string [] rules_always_confirm = rules_always_confirm_all.split(";");
154                 string [] rules_patterns = rules_patterns_all.split(";");
155                 if (rules_patterns.length == rules_always_confirm.length) {
156                     Rule[] rules = new Rule[rules_patterns.length];
157                     for (int j = 0; j < rules_patterns.length; j++) {
158                         rules[j].pattern = rules_patterns[j];
159                         rules[j].always_confirm = rules_always_confirm[j];
160                     }
161                     id_card.rules = rules;
162                 }
163             }
164
165             if (store_password != null)
166                 id_card.store_password = (store_password == "yes");
167             else
168                 id_card.store_password = ((entry.secret != null) && (entry.secret != ""));
169
170             if (id_card.store_password)
171                 id_card.password = entry.secret;
172             else
173                 id_card.password = null;
174             id_card_list.add(id_card);
175         }
176     }
177
178     public void store_id_cards() {
179         logger.trace("store_id_cards");
180         clear_keyring();
181         foreach (IdCard id_card in this.id_card_list) {
182             /* workaround for Centos vala array property bug: use temp array */
183             var rules = id_card.rules;
184             string[] rules_patterns = new string[rules.length];
185             string[] rules_always_conf = new string[rules.length];
186             
187             for (int i = 0; i < rules.length; i++) {
188                 rules_patterns[i] = rules[i].pattern;
189                 rules_always_conf[i] = rules[i].always_confirm;
190             }
191             string patterns = string.joinv(";", rules_patterns);
192             string always_conf = string.joinv(";", rules_always_conf);
193             string services = id_card.get_services_string(";");
194             GnomeKeyring.AttributeList attributes = new GnomeKeyring.AttributeList();
195             uint32 item_id;
196             attributes.append_string(keyring_store_attribute, keyring_store_version);
197             attributes.append_string("Issuer", id_card.issuer);
198             attributes.append_string("Username", id_card.username);
199             attributes.append_string("DisplayName", id_card.display_name);
200             attributes.append_string("Services", services);
201             attributes.append_string("Rules-Pattern", patterns);
202             attributes.append_string("Rules-AlwaysConfirm", always_conf);
203             attributes.append_string("CA-Cert", id_card.trust_anchor.ca_cert);
204             attributes.append_string("Server-Cert", id_card.trust_anchor.server_cert);
205             attributes.append_string("Subject", id_card.trust_anchor.subject);
206             attributes.append_string("Subject-Alt", id_card.trust_anchor.subject_alt);
207             attributes.append_string("CACert_User_Verified", id_card.trust_anchor.user_verified ? "true" : "false");
208             attributes.append_string("StorePassword", id_card.store_password ? "yes" : "no");
209
210             GnomeKeyring.Result result = GnomeKeyring.item_create_sync(null,
211                                                                        item_type, id_card.display_name, attributes,
212                                                                        id_card.store_password ? id_card.password : "",
213                                                                        true, out item_id);
214             if (result != GnomeKeyring.Result.OK) {
215                 stdout.printf("GnomeKeyring.item_create_sync() failed. result: %d", result);
216             }
217         }
218         load_id_cards();
219     }
220
221     public KeyringStore() {
222         id_card_list = new LinkedList<IdCard>();
223         load_id_cards();
224     }
225 }
226
227 #endif