00e09f5570a5f8a54ef812cabd6885318a7f4ac0
[moonshot-ui.git] / src / moonshot-keyring-store.vala
1 using Gee;
2
3 #if GNOME_KEYRING
4 public class KeyringStore : Object, IIdentityCardStore {
5     private LinkedList<IdCard> id_card_list;
6     private const string keyring_store_attribute = "Moonshot";
7     private const string keyring_store_version = "1.0";
8     private const GnomeKeyring.ItemType item_type = GnomeKeyring.ItemType.GENERIC_SECRET;
9
10     public void add_card(IdCard card) {
11         id_card_list.add(card);
12         store_id_cards ();
13     }
14
15     public IdCard? update_card(IdCard card) {
16         id_card_list.remove(card);
17         id_card_list.add(card);
18         store_id_cards ();
19         foreach (IdCard idcard in id_card_list)
20             if (idcard.display_name == card.display_name)
21                 return idcard;
22         return null;
23     }
24
25     public void remove_card(IdCard card) {
26         id_card_list.remove(card);
27         store_id_cards ();
28     }
29
30     public IIdentityCardStore.StoreType get_store_type() {
31         return IIdentityCardStore.StoreType.KEYRING;
32     }
33
34     public LinkedList<IdCard> get_card_list() {
35         return id_card_list;
36     }
37
38     /* clear all keyring-stored ids (in preparation to store current list) */
39     private void clear_keyring() {
40         GnomeKeyring.AttributeList match = new GnomeKeyring.AttributeList();
41         match.append_string(keyring_store_attribute, keyring_store_version);
42         GLib.List<GnomeKeyring.Found> items;
43         GnomeKeyring.find_items_sync(item_type, match, out items);
44         foreach(unowned GnomeKeyring.Found entry in items) {
45             GnomeKeyring.Result result = GnomeKeyring.item_delete_sync(null, entry.item_id);
46             if (result != GnomeKeyring.Result.OK) {
47                 stdout.printf("GnomeKeyring.item_delete_sync() failed. result: %d", result);
48             }
49         }
50     }
51      
52     private void load_id_cards() {
53         id_card_list.clear();
54
55         GnomeKeyring.AttributeList match = new GnomeKeyring.AttributeList();
56         match.append_string(keyring_store_attribute, keyring_store_version);
57         GLib.List<GnomeKeyring.Found> items;
58         GnomeKeyring.find_items_sync(item_type, match, out items);
59         foreach(unowned GnomeKeyring.Found entry in items) {
60             IdCard id_card = new IdCard ();
61             int i;
62             int rules_patterns_index = -1;
63             int rules_always_confirm_index = -1;
64             string store_password = null;
65             for (i=0; i<entry.attributes.len; i++) {
66                 var attribute = ((GnomeKeyring.Attribute *) entry.attributes.data)[i];
67                 string value = attribute.string_value;
68                 if (attribute.name == "Issuer") {
69                     id_card.issuer = value;
70                 } else if (attribute.name == "Username") {
71                     id_card.username = value;
72                 } else if (attribute.name == "DisplayName") {
73                     id_card.display_name = value;
74                 } else if (attribute.name == "Services") {
75                     id_card.services = value.split(";");
76                 } else if (attribute.name == "Rules-Pattern") {
77                     rules_patterns_index = i;
78                 } else if (attribute.name == "Rules-AlwaysConfirm") {
79                     rules_always_confirm_index = i;
80                 } else if (attribute.name == "CA-Cert") {
81                     id_card.trust_anchor.ca_cert = value;
82                 } else if (attribute.name == "Server-Cert") {
83                     id_card.trust_anchor.server_cert = value;
84                 } else if (attribute.name == "Subject") {
85                     id_card.trust_anchor.subject = value;
86                 } else if (attribute.name == "Subject-Alt") {
87                     id_card.trust_anchor.subject_alt = value;
88                 } else if (attribute.name == "StorePassword") {
89                     store_password = value;
90                 }
91             }
92             if ((rules_always_confirm_index != -1) && (rules_patterns_index != -1)) {
93                 string rules_patterns_all = ((GnomeKeyring.Attribute *) entry.attributes.data)[rules_patterns_index].string_value;
94                 string rules_always_confirm_all = ((GnomeKeyring.Attribute *) entry.attributes.data)[rules_always_confirm_index].string_value;
95                 string [] rules_always_confirm = rules_always_confirm_all.split(";");
96                 string [] rules_patterns = rules_patterns_all.split(";");
97                 if (rules_patterns.length == rules_always_confirm.length) {
98                    Rule[] rules = new Rule[rules_patterns.length];
99                    for (int j=0; j<rules_patterns.length; j++) {
100                        rules[j].pattern = rules_patterns[j];
101                        rules[j].always_confirm = rules_always_confirm[j];
102                    }
103                    id_card.rules = rules;
104                 }
105             }
106
107             if (store_password != null)
108                 id_card.store_password = (store_password == "yes");
109             else
110                 id_card.store_password = ((entry.secret != null) && (entry.secret != ""));
111
112             if (id_card.store_password)
113                 id_card.password = entry.secret;
114             else
115                 id_card.password = null;
116             id_card_list.add(id_card);
117         }
118     }
119
120     public void store_id_cards () {
121         clear_keyring();
122         foreach (IdCard id_card in this.id_card_list) {
123             string[] rules_patterns = new string[id_card.rules.length];
124             string[] rules_always_conf = new string[id_card.rules.length];
125             
126             for (int i=0; i<id_card.rules.length; i++) {
127                 rules_patterns[i] = id_card.rules[i].pattern;
128                 rules_always_conf[i] = id_card.rules[i].always_confirm;
129             }
130             string patterns = string.joinv(";", rules_patterns);
131             string always_conf = string.joinv(";", rules_always_conf);
132             string services = string.joinv(";", id_card.services);
133             GnomeKeyring.AttributeList attributes = new GnomeKeyring.AttributeList();
134             uint32 item_id;
135             attributes.append_string(keyring_store_attribute, keyring_store_version);
136             attributes.append_string("Issuer", id_card.issuer);
137             attributes.append_string("Username", id_card.username);
138             attributes.append_string("DisplayName", id_card.display_name);
139             attributes.append_string("Services", services);
140             attributes.append_string("Rules-Pattern", patterns);
141             attributes.append_string("Rules-AlwaysConfirm", always_conf);
142             attributes.append_string("CA-Cert", id_card.trust_anchor.ca_cert);
143             attributes.append_string("Server-Cert", id_card.trust_anchor.server_cert);
144             attributes.append_string("Subject", id_card.trust_anchor.subject);
145             attributes.append_string("Subject-Alt", id_card.trust_anchor.subject_alt);
146             attributes.append_string("StorePassword", id_card.store_password ? "yes" : "no");
147
148             GnomeKeyring.Result result = GnomeKeyring.item_create_sync(null,
149                 item_type, id_card.display_name, attributes,
150                 id_card.store_password ? id_card.password : "",
151                 true, out item_id);
152             if (result != GnomeKeyring.Result.OK) {
153                 stdout.printf("GnomeKeyring.item_create_sync() failed. result: %d", result);
154             }
155         }
156         load_id_cards();
157     }
158
159     public KeyringStore () {
160         id_card_list = new LinkedList<IdCard>();
161         load_id_cards();
162     }
163 }
164
165 #endif