Minor formatting changes
[moonshot-ui.git] / src / moonshot-identities-manager.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 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     private const string FILE_NAME = "identities.txt";
90     private PasswordHashTable password_table;
91     private IIdentityCardStore store;
92     public LinkedList<IdCard>  get_card_list() {
93         var identities = store.get_card_list();
94         identities.sort((a, b) => {
95                 IdCard id_a = (IdCard )a;
96                 IdCard id_b = (IdCard )b;
97                 if (id_a.IsNoIdentity() && !id_b.IsNoIdentity()) {
98                     return -1;
99                 } else if (id_b.IsNoIdentity() && !id_a.IsNoIdentity()) {
100                     return 1;
101                 }
102                 return strcmp(id_a.display_name, id_b.display_name);
103             });
104         if (identities.is_empty || !identities[0].IsNoIdentity())
105             identities.insert(0, IdCard.NewNoIdentity());
106         foreach (IdCard id_card in identities) {
107             if (!id_card.store_password) {
108                 password_table.RetrievePassword(id_card, store);
109             }
110         }
111         return identities;
112     }
113     public signal void card_list_changed();
114
115     /* This method finds a valid display name */
116     public bool display_name_is_valid(string name,
117                                       out string? candidate)
118     {
119         if (&candidate != null)
120             candidate = null;
121         foreach (IdCard id_card in this.store.get_card_list())
122         {
123             if (id_card.display_name == name)
124             {
125                 if (&candidate != null)
126                 {
127                     for (int i = 0; i < 1000; i++)
128                     {
129                         string tmp = "%s %d".printf(name, i);
130                         if (display_name_is_valid(tmp, null))
131                         {
132                             candidate = tmp;
133                             break;
134                         }
135                     }
136                 }
137                 return false;
138             }
139         }
140         return true;
141     }
142
143     private bool remove_duplicates(IdCard card)
144     {
145         bool duplicate_found = false;
146         bool found = false;
147         do {
148             var cards = this.store.get_card_list();
149             found = false;
150             foreach (IdCard id_card in cards) {
151                 if ((card != id_card) && (id_card.nai == card.nai)) {
152                     stdout.printf("removing duplicate id for '%s'\n", card.nai);
153                     remove_card_internal(id_card);
154                     found = duplicate_found = true;
155                     break;
156                 }
157             }
158         } while (found);
159         return duplicate_found;
160     }
161
162     public IdCard? find_id_card(string nai, bool force_flat_file_store) {
163         IdCard? retval = null;
164         IIdentityCardStore.StoreType saved_store_type = get_store_type();
165         if (force_flat_file_store)
166             set_store_type(IIdentityCardStore.StoreType.FLAT_FILE);
167
168         foreach (IdCard id in get_card_list()) {
169             if (id.nai == nai) {
170                 retval = id;
171                 break;
172             }
173         }
174         set_store_type(saved_store_type);
175         if (force_flat_file_store && 
176             (saved_store_type != IIdentityCardStore.StoreType.FLAT_FILE))
177             card_list_changed();
178         return retval;
179     }
180
181     public void add_card(IdCard card, bool force_flat_file_store) {
182         if (card.temporary)
183             return;
184
185         string candidate;
186         IIdentityCardStore.StoreType saved_store_type = get_store_type();
187
188         if (force_flat_file_store)
189             set_store_type(IIdentityCardStore.StoreType.FLAT_FILE);
190
191         remove_duplicates(card);
192
193         if (!display_name_is_valid(card.display_name, out candidate))
194         {
195             card.display_name = candidate;
196         }
197
198         if (!card.store_password)
199             password_table.CachePassword(card, store);
200         store.add_card(card);
201         set_store_type(saved_store_type);
202         card_list_changed();
203     }
204
205     public IdCard update_card(IdCard card) {
206         IdCard retval;
207         if (card.temporary) {
208             retval = card;
209             return retval;
210         }
211             
212         if (!card.store_password)
213             password_table.CachePassword(card, store);
214         else
215             password_table.RemovePassword(card, store);
216         retval = store.update_card(card);
217         card_list_changed();
218         return retval;
219     }
220
221     private bool remove_card_internal(IdCard card) {
222         if (card.temporary)
223             return false;
224         password_table.RemovePassword(card, store);
225         return store.remove_card(card);
226     }
227
228     public bool remove_card(IdCard card) {
229         if (remove_card_internal(card)) {
230             card_list_changed();
231             return true;
232         }
233         return false;
234     }
235
236     public void set_store_type(IIdentityCardStore.StoreType type) {
237         if ((store != null) && (store.get_store_type() == type))
238             return;
239         switch (type) {
240             #if GNOME_KEYRING
241         case IIdentityCardStore.StoreType.KEYRING:
242             store = new KeyringStore();
243             break;
244             #endif
245         case IIdentityCardStore.StoreType.FLAT_FILE:
246         default:
247             store = new LocalFlatFileStore();
248             break;
249         }
250     }
251
252     public IIdentityCardStore.StoreType get_store_type() {
253         return store.get_store_type();
254     }
255
256     public bool HasNonTrivialIdentities() {
257         foreach (IdCard card in this.store.get_card_list()) {
258             // The 'NoIdentity' card is non-trivial if it has services or rules.
259             // All other cards are automatically non-trivial.
260             if ((!card.IsNoIdentity()) || 
261                 (card.services.length > 0) ||
262                 (card.rules.length > 0)) {
263                 return true;
264             }
265         }
266         return false;
267     }
268
269
270     private IdentityManagerApp parent;
271
272     public IdentityManagerModel(IdentityManagerApp parent_app, IIdentityCardStore.StoreType store_type) {
273         parent = parent_app;
274         password_table = new PasswordHashTable();
275         set_store_type(store_type);
276     }
277 }