Refactored the IdCard services list to fix new bugs and (hopefully) prevent even...
[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             for (i = 0; i < entry.attributes.len; i++) {
109                 var attribute = ((GnomeKeyring.Attribute *) entry.attributes.data)[i];
110                 string value = attribute.string_value;
111                 if (attribute.name == "Issuer") {
112                     id_card.issuer = value;
113                 } else if (attribute.name == "Username") {
114                     id_card.username = value;
115                 } else if (attribute.name == "DisplayName") {
116                     id_card.display_name = value;
117                 } else if (attribute.name == "Services") {
118                     id_card.update_services(value.split(";"));
119                 } else if (attribute.name == "Rules-Pattern") {
120                     rules_patterns_index = i;
121                 } else if (attribute.name == "Rules-AlwaysConfirm") {
122                     rules_always_confirm_index = i;
123                 } else if (attribute.name == "CA-Cert") {
124                     id_card.trust_anchor.ca_cert = value.strip();
125                 } else if (attribute.name == "Server-Cert") {
126                     id_card.trust_anchor.server_cert = value;
127                 } else if (attribute.name == "Subject") {
128                     id_card.trust_anchor.subject = value;
129                 } else if (attribute.name == "Subject-Alt") {
130                     id_card.trust_anchor.subject_alt = value;
131                 } else if (attribute.name == "StorePassword") {
132                     store_password = value;
133                 }
134             }
135             if ((rules_always_confirm_index != -1) && (rules_patterns_index != -1)) {
136                 string rules_patterns_all = ((GnomeKeyring.Attribute *) entry.attributes.data)[rules_patterns_index].string_value;
137                 string rules_always_confirm_all = ((GnomeKeyring.Attribute *) entry.attributes.data)[rules_always_confirm_index].string_value;
138                 string [] rules_always_confirm = rules_always_confirm_all.split(";");
139                 string [] rules_patterns = rules_patterns_all.split(";");
140                 if (rules_patterns.length == rules_always_confirm.length) {
141                     Rule[] rules = new Rule[rules_patterns.length];
142                     for (int j = 0; j < rules_patterns.length; j++) {
143                         rules[j].pattern = rules_patterns[j];
144                         rules[j].always_confirm = rules_always_confirm[j];
145                     }
146                     id_card.rules = rules;
147                 }
148             }
149
150             if (store_password != null)
151                 id_card.store_password = (store_password == "yes");
152             else
153                 id_card.store_password = ((entry.secret != null) && (entry.secret != ""));
154
155             if (id_card.store_password)
156                 id_card.password = entry.secret;
157             else
158                 id_card.password = null;
159             id_card_list.add(id_card);
160         }
161     }
162
163     public void store_id_cards() {
164         logger.trace("store_id_cards");
165         clear_keyring();
166         foreach (IdCard id_card in this.id_card_list) {
167             /* workaround for Centos vala array property bug: use temp array */
168             var rules = id_card.rules;
169             string[] rules_patterns = new string[rules.length];
170             string[] rules_always_conf = new string[rules.length];
171             
172             for (int i = 0; i < rules.length; i++) {
173                 rules_patterns[i] = rules[i].pattern;
174                 rules_always_conf[i] = rules[i].always_confirm;
175             }
176             string patterns = string.joinv(";", rules_patterns);
177             string always_conf = string.joinv(";", rules_always_conf);
178             string services = id_card.get_services_string(";");
179             GnomeKeyring.AttributeList attributes = new GnomeKeyring.AttributeList();
180             uint32 item_id;
181             attributes.append_string(keyring_store_attribute, keyring_store_version);
182             attributes.append_string("Issuer", id_card.issuer);
183             attributes.append_string("Username", id_card.username);
184             attributes.append_string("DisplayName", id_card.display_name);
185             attributes.append_string("Services", services);
186             attributes.append_string("Rules-Pattern", patterns);
187             attributes.append_string("Rules-AlwaysConfirm", always_conf);
188             attributes.append_string("CA-Cert", id_card.trust_anchor.ca_cert);
189             attributes.append_string("Server-Cert", id_card.trust_anchor.server_cert);
190             attributes.append_string("Subject", id_card.trust_anchor.subject);
191             attributes.append_string("Subject-Alt", id_card.trust_anchor.subject_alt);
192             attributes.append_string("StorePassword", id_card.store_password ? "yes" : "no");
193
194             GnomeKeyring.Result result = GnomeKeyring.item_create_sync(null,
195                                                                        item_type, id_card.display_name, attributes,
196                                                                        id_card.store_password ? id_card.password : "",
197                                                                        true, out item_id);
198             if (result != GnomeKeyring.Result.OK) {
199                 stdout.printf("GnomeKeyring.item_create_sync() failed. result: %d", result);
200             }
201         }
202         load_id_cards();
203     }
204
205     public KeyringStore() {
206         id_card_list = new LinkedList<IdCard>();
207         load_id_cards();
208     }
209 }
210
211 #endif