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