2 * Copyright (c) 2011-2014, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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
34 public class Password {
36 private unowned string _password;
37 public string password {
42 if (_password != null) {
43 GnomeKeyring.memory_free((void *)_password);
47 _password = GnomeKeyring.memory_strdup(value);
51 public string password { get; set; default = null; }
54 public Password(string in_password) {
55 password = in_password;
63 public class PasswordHashTable : Object {
64 private HashTable<string, Password> password_table;
66 private static string ComputeHashKey(IdCard card, IIdentityCardStore store) {
67 return "%s_store_%d".printf( card.display_name, store.get_store_type() );
70 public void CachePassword(IdCard card, IIdentityCardStore store) {
71 password_table.replace(ComputeHashKey(card, store), new Password(card.password));
74 public void RemovePassword(IdCard card, IIdentityCardStore store) {
75 password_table.remove(ComputeHashKey(card, store));
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;
83 public PasswordHashTable() {
84 password_table = new HashTable<string, Password>(GLib.str_hash, GLib.str_equal);
88 public class IdentityManagerModel : Object {
89 static MoonshotLogger logger = get_logger("IdentityManagerModel");
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()) {
101 } else if (id_b.is_no_identity() && !id_a.is_no_identity()) {
104 return strcmp(id_a.display_name, id_b.display_name);
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);
115 public signal void card_list_changed();
117 /* This method finds a valid display name */
118 public bool display_name_is_valid(string name,
119 out string? candidate)
121 if (&candidate != null)
123 foreach (IdCard id_card in this.store.get_card_list())
125 if (id_card.display_name == name)
127 if (&candidate != null)
129 for (int i = 0; i < 1000; i++)
131 string tmp = "%s %d".printf(name, i);
132 if (display_name_is_valid(tmp, null))
145 private bool remove_duplicates(IdCard new_card)
147 bool duplicate_found = false;
150 var cards = this.store.get_card_list();
152 foreach (IdCard id_card in cards) {
153 if ((new_card != id_card) && (id_card.nai == new_card.nai)) {
154 stdout.printf("removing duplicate id for '%s'\n", new_card.nai);
155 logger.trace("removing duplicate id for '%s'\n".printf(new_card.nai));
156 remove_card_internal(id_card);
157 found = duplicate_found = true;
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 and user_verified fields from the old card.");
161 new_card.trust_anchor.set_datetime_added(id_card.trust_anchor.datetime_added);
162 new_card.trust_anchor.user_verified = id_card.trust_anchor.user_verified;
168 return duplicate_found;
171 public IdCard? find_id_card(string nai, bool force_flat_file_store) {
172 IdCard? retval = null;
173 IIdentityCardStore.StoreType saved_store_type = get_store_type();
174 if (force_flat_file_store)
175 set_store_type(IIdentityCardStore.StoreType.FLAT_FILE);
177 foreach (IdCard id in get_card_list()) {
183 set_store_type(saved_store_type);
184 if (force_flat_file_store &&
185 (saved_store_type != IIdentityCardStore.StoreType.FLAT_FILE))
190 public void add_card(IdCard card, bool force_flat_file_store) {
191 if (card.temporary) {
192 logger.trace("add_card: card is temporary; returning.");
197 IIdentityCardStore.StoreType saved_store_type = get_store_type();
199 if (force_flat_file_store)
200 set_store_type(IIdentityCardStore.StoreType.FLAT_FILE);
202 remove_duplicates(card);
204 if (!display_name_is_valid(card.display_name, out candidate))
206 card.display_name = candidate;
209 if (!card.store_password)
210 password_table.CachePassword(card, store);
212 logger.trace("add_card: Adding card '%s' with services: '%s'"
213 .printf(card.display_name, card.get_services_string("; ")));
215 store.add_card(card);
216 set_store_type(saved_store_type);
220 public IdCard update_card(IdCard card) {
221 logger.trace("update_card");
224 if (card.temporary) {
229 if (!card.store_password)
230 password_table.CachePassword(card, store);
232 password_table.RemovePassword(card, store);
233 retval = store.update_card(card);
238 private bool remove_card_internal(IdCard card) {
241 password_table.RemovePassword(card, store);
242 return store.remove_card(card);
245 public bool remove_card(IdCard card) {
246 if (remove_card_internal(card)) {
253 // The name is misleading: This not only sets the store type,
254 // it also creates a new store instance, which loads the card data.
255 public void set_store_type(IIdentityCardStore.StoreType type) {
256 if ((store != null) && (store.get_store_type() == type))
260 case IIdentityCardStore.StoreType.KEYRING:
261 store = new KeyringStore();
264 case IIdentityCardStore.StoreType.FLAT_FILE:
266 store = new LocalFlatFileStore();
270 // Loop through the loaded IDs. If any trust anchors are old enough that we didn't record
271 // the datetime_added, add it now.
272 string before_now = _("Before ") + TrustAnchor.format_datetime_now();
273 bool save_needed = false;
274 foreach (IdCard id in this.store.get_card_list()) {
275 if (!id.trust_anchor.is_empty() && id.trust_anchor.datetime_added == "") {
276 logger.trace("set_store_type : Set ta_datetime_added for old trust anchor on '%s' to '%s'".printf(id.display_name, before_now));
277 id.trust_anchor.set_datetime_added(before_now);
282 this.store.store_id_cards();
286 public IIdentityCardStore.StoreType get_store_type() {
287 return store.get_store_type();
290 public bool HasNonTrivialIdentities() {
291 foreach (IdCard card in this.store.get_card_list()) {
292 // The 'NoIdentity' card is non-trivial if it has services or rules.
293 // All other cards are automatically non-trivial.
294 if ((!card.is_no_identity()) ||
295 (card.services.size > 0) ||
296 (card.rules.length > 0)) {
304 private IdentityManagerApp parent;
306 public IdentityManagerModel(IdentityManagerApp parent_app, IIdentityCardStore.StoreType store_type) {
307 logger.trace("IdentityManagerModel: store_type=" + store_type.to_string());
309 password_table = new PasswordHashTable();
310 set_store_type(store_type);