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