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 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()) {
99 } else if (id_b.IsNoIdentity() && !id_a.IsNoIdentity()) {
102 return strcmp(id_a.display_name, id_b.display_name);
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);
113 public signal void card_list_changed();
115 /* This method finds a valid display name */
116 public bool display_name_is_valid (string name,
117 out string? candidate)
119 if (&candidate != null)
121 foreach (IdCard id_card in this.get_card_list())
123 if (id_card.display_name == name)
125 if (&candidate != null)
127 for (int i=0; i<1000; i++)
129 string tmp = "%s %d".printf (name, i);
130 if (display_name_is_valid (tmp, null))
143 private bool remove_duplicates(IdCard card)
145 bool duplicate_found = false;
148 var cards = get_card_list();
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;
159 return duplicate_found;
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);
168 foreach (IdCard id in get_card_list()) {
174 set_store_type(saved_store_type);
175 if (force_flat_file_store &&
176 (saved_store_type != IIdentityCardStore.StoreType.FLAT_FILE))
181 public void add_card(IdCard card, bool force_flat_file_store) {
186 IIdentityCardStore.StoreType saved_store_type = get_store_type();
188 if (force_flat_file_store)
189 set_store_type(IIdentityCardStore.StoreType.FLAT_FILE);
191 remove_duplicates(card);
193 if (!display_name_is_valid (card.display_name, out candidate))
195 card.display_name = candidate;
198 if (!card.store_password)
199 password_table.CachePassword(card, store);
200 store.add_card(card);
201 set_store_type(saved_store_type);
205 public IdCard update_card(IdCard card) {
207 if (card.temporary) {
212 if (!card.store_password)
213 password_table.CachePassword(card, store);
215 password_table.RemovePassword(card, store);
216 retval = store.update_card(card);
221 private bool remove_card_internal(IdCard card) {
224 password_table.RemovePassword(card, store);
225 return store.remove_card(card);
228 public bool remove_card(IdCard card) {
229 if (remove_card_internal(card)) {
236 public void set_store_type(IIdentityCardStore.StoreType type) {
237 if ((store != null) && (store.get_store_type() == type))
241 case IIdentityCardStore.StoreType.KEYRING:
242 store = new KeyringStore();
245 case IIdentityCardStore.StoreType.FLAT_FILE:
247 store = new LocalFlatFileStore();
252 public IIdentityCardStore.StoreType get_store_type() {
253 return store.get_store_type();
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)) {
270 private IdentityManagerApp parent;
272 public IdentityManagerModel(IdentityManagerApp parent_app, IIdentityCardStore.StoreType store_type) {
274 password_table = new PasswordHashTable();
275 set_store_type(store_type);