Squashed merge of many commits, including (but not limited to) :
[moonshot-ui.git] / src / moonshot-identities-manager.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 public class Password {
35 #if GNOME_KEYRING
36     private unowned string _password;
37     public string password {
38         get {
39             return _password;
40         }
41         set {
42             if (_password != null) {
43                 GnomeKeyring.memory_free((void *)_password);
44                 _password = null;
45             }
46             if (value != null)
47                 _password = GnomeKeyring.memory_strdup(value); 
48         }
49     }
50 #else
51     public string password { get; set; default = null; }
52 #endif
53
54     public Password(string in_password) {
55         password = in_password;
56     }
57
58     ~Password() {
59         password = null;
60     }
61 }
62
63 public class PasswordHashTable : Object {
64     private HashTable<string, Password> password_table;
65
66     private static string ComputeHashKey(IdCard card, IIdentityCardStore store) {
67         return "%s_store_%d".printf( card.display_name, store.get_store_type() );
68     }
69
70     public void CachePassword(IdCard card, IIdentityCardStore store) {
71         password_table.replace(ComputeHashKey(card, store), new Password(card.password));
72     }
73
74     public void RemovePassword(IdCard card, IIdentityCardStore store) {
75         password_table.remove(ComputeHashKey(card, store));
76     }
77     public void RetrievePassword(IdCard card, IIdentityCardStore store) {
78         weak Password password = password_table.lookup(ComputeHashKey(card, store));
79         if (password != null) {
80             card.password = password.password;
81         }
82     }
83     public PasswordHashTable() {
84         password_table = new HashTable<string, Password>(GLib.str_hash, GLib.str_equal);
85     }
86 }
87
88 public class IdentityManagerModel : Object {
89     static MoonshotLogger logger = get_logger("IdentityManagerModel");
90
91     private const string FILE_NAME = "identities.txt";
92     private PasswordHashTable password_table;
93     private IIdentityCardStore store;
94     public LinkedList<IdCard>  get_card_list() {
95         var identities = store.get_card_list();
96         identities.sort((a, b) => {
97                 IdCard id_a = (IdCard )a;
98                 IdCard id_b = (IdCard )b;
99                 if (id_a.is_no_identity() && !id_b.is_no_identity()) {
100                     return -1;
101                 } else if (id_b.is_no_identity() && !id_a.is_no_identity()) {
102                     return 1;
103                 }
104                 return strcmp(id_a.display_name, id_b.display_name);
105             });
106         if (identities.is_empty || !identities[0].is_no_identity())
107             identities.insert(0, IdCard.NewNoIdentity());
108         foreach (IdCard id_card in identities) {
109             if (!id_card.store_password) {
110                 password_table.RetrievePassword(id_card, store);
111             }
112         }
113         return identities;
114     }
115     public signal void card_list_changed();
116
117     /* This method finds a valid display name */
118     public bool display_name_is_valid(string name,
119                                       out string? candidate)
120     {
121         if (&candidate != null)
122             candidate = null;
123         foreach (IdCard id_card in this.store.get_card_list())
124         {
125             if (id_card.display_name == name)
126             {
127                 if (&candidate != null)
128                 {
129                     for (int i = 0; i < 1000; i++)
130                     {
131                         string tmp = "%s %d".printf(name, i);
132                         if (display_name_is_valid(tmp, null))
133                         {
134                             candidate = tmp;
135                             break;
136                         }
137                     }
138                 }
139                 return false;
140             }
141         }
142         return true;
143     }
144
145     private bool remove_duplicates(IdCard new_card, out ArrayList<IdCard>? old_duplicates)
146     {
147         ArrayList<IdCard> dups = new ArrayList<IdCard>();
148         var cards = this.store.get_card_list();
149         foreach (IdCard id_card in cards) {
150             if ((new_card != id_card) && (id_card.nai == new_card.nai)) {
151                 dups.add(id_card);
152             }
153         }
154
155         foreach (IdCard id_card in dups) {
156             logger.trace("removing duplicate id for '%s'\n".printf(new_card.nai));
157             remove_card_internal(id_card);
158
159             if (new_card.trust_anchor.Compare(id_card.trust_anchor) == 0) {
160                 logger.trace("Old and new cards have same trust anchor. Re-using the datetime_added field from the old card.");
161                 new_card.trust_anchor.set_datetime_added(id_card.trust_anchor.datetime_added);
162             }
163         }
164
165         if (&old_duplicates != null) {
166             old_duplicates = dups;
167         }
168
169         return (dups.size > 0);
170     }
171
172
173     public bool find_duplicate_nai_sets(out ArrayList<ArrayList<IdCard>> duplicates)
174     {
175         var nais = new HashMap<string, ArrayList<IdCard>>();
176
177         duplicates = new ArrayList<ArrayList<IdCard>>();
178         LinkedList<IdCard> card_list = get_card_list() ;
179         if (card_list == null) {
180             return false;
181         }
182
183         bool found = false;
184         foreach (IdCard id_card in card_list) {
185             logger.trace(@"load_id_cards: Loading card with display name '$(id_card.display_name)'");
186
187             //!!TODO: This uniqueness check really belongs somewhere else -- like where we add
188             // IDs, and/or read them from storage. However, we should never hit this.
189
190             if (nais.has_key(id_card.nai)) {
191                 ArrayList<IdCard> list = nais.get(id_card.nai);
192                 list.add(id_card);
193             }
194             else {
195                 ArrayList<IdCard> list = new ArrayList<IdCard>();
196                 list.add(id_card);
197                 nais.set(id_card.nai, list);
198             }
199         }
200
201         duplicates = new ArrayList<ArrayList<IdCard>>();
202         foreach (Map.Entry<string, ArrayList<IdCard>> entry in nais.entries) {
203             var list = entry.value;
204             if (list.size > 1) {
205                 duplicates.add(list);
206                 found = true;
207             }
208         }
209         return found;
210     }
211
212
213     public IdCard? find_id_card(string nai, bool force_flat_file_store) {
214         IdCard? retval = null;
215         IIdentityCardStore.StoreType saved_store_type = get_store_type();
216         if (force_flat_file_store)
217             set_store_type(IIdentityCardStore.StoreType.FLAT_FILE);
218
219         foreach (IdCard id in get_card_list()) {
220             if (id.nai == nai) {
221                 retval = id;
222                 break;
223             }
224         }
225         set_store_type(saved_store_type);
226         if (force_flat_file_store && 
227             (saved_store_type != IIdentityCardStore.StoreType.FLAT_FILE))
228             card_list_changed();
229         return retval;
230     }
231
232     public void add_card(IdCard card, bool force_flat_file_store, out ArrayList<IdCard>? old_duplicates=null) {
233         if (card.temporary) {
234             logger.trace("add_card: card is temporary; returning.");
235             return;
236         }
237
238         string candidate;
239         IIdentityCardStore.StoreType saved_store_type = get_store_type();
240
241         if (force_flat_file_store)
242             set_store_type(IIdentityCardStore.StoreType.FLAT_FILE);
243
244         remove_duplicates(card, out old_duplicates);
245
246         if (!display_name_is_valid(card.display_name, out candidate))
247         {
248             card.display_name = candidate;
249         }
250
251         if (!card.store_password)
252             password_table.CachePassword(card, store);
253
254         logger.trace("add_card: Adding card '%s' with services: '%s'"
255                      .printf(card.display_name, card.get_services_string("; ")));
256
257         store.add_card(card);
258         set_store_type(saved_store_type);
259         card_list_changed();
260     }
261
262     public IdCard update_card(IdCard card) {
263         logger.trace("update_card");
264
265         IdCard retval;
266         if (card.temporary) {
267             retval = card;
268             return retval;
269         }
270             
271         if (!card.store_password)
272             password_table.CachePassword(card, store);
273         else
274             password_table.RemovePassword(card, store);
275         retval = store.update_card(card);
276         card_list_changed();
277         return retval;
278     }
279
280     private bool remove_card_internal(IdCard card) {
281         if (card.temporary)
282             return false;
283         password_table.RemovePassword(card, store);
284         return store.remove_card(card);
285     }
286
287     public bool remove_card(IdCard card) {
288         if (remove_card_internal(card)) {
289             logger.trace(@"remove_card: Removed '$(card.display_name)'");
290             card_list_changed();
291             return true;
292         }
293         logger.warn(@"remove_card: Couldn't remove '$(card.display_name)'");
294         return false;
295     }
296
297     // The name is misleading: This not only sets the store type,
298     // it also creates a new store instance, which loads the card data.
299     public void set_store_type(IIdentityCardStore.StoreType type) {
300         if ((store != null) && (store.get_store_type() == type))
301             return;
302         switch (type) {
303             #if GNOME_KEYRING
304         case IIdentityCardStore.StoreType.KEYRING:
305             store = new KeyringStore();
306             break;
307             #endif
308         case IIdentityCardStore.StoreType.FLAT_FILE:
309         default:
310             store = new LocalFlatFileStore();
311             break;
312         }
313
314         // Loop through the loaded IDs. If any trust anchors are old enough that we didn't record
315         // the datetime_added, add it now.
316         string before_now = _("Before ") + TrustAnchor.format_datetime_now();
317         bool save_needed = false;
318         foreach (IdCard id in this.store.get_card_list()) {
319             if (!id.trust_anchor.is_empty() && id.trust_anchor.datetime_added == "") {
320                 logger.trace("set_store_type : Set ta_datetime_added for old trust anchor on '%s' to '%s'".printf(id.display_name, before_now));
321                 id.trust_anchor.set_datetime_added(before_now);
322                 save_needed = true;
323             }
324         }
325         if (save_needed) {
326             this.store.store_id_cards();
327         }
328     }
329
330     public IIdentityCardStore.StoreType get_store_type() {
331         return store.get_store_type();
332     }
333
334     public bool HasNonTrivialIdentities() {
335         foreach (IdCard card in this.store.get_card_list()) {
336             // The 'NoIdentity' card is non-trivial if it has services or rules.
337             // All other cards are automatically non-trivial.
338             if ((!card.is_no_identity()) || 
339                 (card.services.size > 0) ||
340                 (card.rules.length > 0)) {
341                 return true;
342             }
343         }
344         return false;
345     }
346
347
348     private IdentityManagerApp parent;
349
350     public IdentityManagerModel(IdentityManagerApp parent_app, IIdentityCardStore.StoreType store_type) {
351         logger.trace("IdentityManagerModel: store_type=" + store_type.to_string());
352         parent = parent_app;
353         password_table = new PasswordHashTable();
354         set_store_type(store_type);
355     }
356 }