Refactored the IdCard services list to fix new bugs and (hopefully) prevent even...
[moonshot-ui.git] / src / moonshot-id.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
33 using Gee;
34
35 public class TrustAnchor : Object
36 {
37     public string ca_cert {get; set; default = "";}
38     public string subject {get; set; default = "";}
39     public string subject_alt  {get; set; default = "";}
40     public string server_cert  {get; set; default = "";}
41     public int Compare(TrustAnchor other)
42     {
43         if (this.ca_cert != other.ca_cert)
44             return 1;
45         if (this.subject != other.subject)
46             return 1;
47         if (this.subject_alt != other.subject_alt)
48             return 1;
49         if (this.server_cert != other.server_cert)
50             return 1;
51         return 0;
52     }
53 }
54
55 public struct Rule
56 {
57     public string pattern;
58     public string always_confirm;
59     public int Compare(Rule other) {
60         if (this.pattern != other.pattern)
61             return 1;
62         if (this.always_confirm != other.always_confirm)
63             return 1;
64         return 0;
65     }
66 }
67
68 public class IdCard : Object
69 {
70     static MoonshotLogger logger = get_logger("IdCard");
71
72     public const string NO_IDENTITY = "No Identity";
73
74     private string _nai;
75   
76     public string display_name { get; set; default = ""; }
77   
78     public string username { get; set; default = ""; }
79 #if GNOME_KEYRING
80     private unowned string _password;
81     public string password {
82         get {
83             return (_password!=null) ? _password : "";
84         }
85         set {
86             if (_password != null) {
87                 GnomeKeyring.memory_free((void *)_password);
88                 _password = null;
89             }
90             if (value != null)
91                 _password = GnomeKeyring.memory_strdup(value); 
92         }
93     }
94 #else
95     public string password { get; set; default = null; }
96 #endif
97
98     public string issuer { get; set; default = ""; }
99   
100     private Rule[] _rules = new Rule[0];
101     public Rule[] rules {
102         get {return _rules;}
103         internal set {_rules = value ?? new Rule[0] ;}
104     }
105
106     private ArrayList<string> _services = new ArrayList<string>();
107
108     internal ArrayList<string> services {
109          get {return  _services;}
110     }
111
112     // Returns the list of services as a string, using the given separator.
113     internal string get_services_string(string sep) {
114         if (_services.is_empty) {
115             return "";
116         }
117
118         // ArrayList.to_array() seems to be unreliable -- it causes segfaults 
119         // semi-randomly. (Possibly because it returns an unowned ref?)
120         // return string.joinv(sep, _services.to_array());
121         // 
122         // This problem may be related to the one noted elsewhere as the
123         // "Centos vala array property bug".
124
125         string[] svcs = new string[_services.size];
126         for (int i = 0; i < _services.size; i++) {
127             svcs[i] = _services[i];
128         }
129
130         return string.joinv(sep, svcs);
131     }
132
133     internal void update_services(string[] services) {
134         _services.clear();
135
136         // Doesn't exist in older versions of libgee:
137         // _services.add_all_array(services);
138
139         if (services != null) {
140             foreach (string s in services) {
141                 _services.add(s);
142             }
143         }
144     } 
145
146     internal void update_services_from_list(ArrayList<string> services) {
147         if (services == this._services) {
148             // Don't try to update from self.
149             return;
150         }
151
152         _services.clear();
153
154         if (services != null) {
155             _services.add_all(services);
156         }
157     } 
158
159
160     public bool temporary {get; set; default = false; }
161
162     public TrustAnchor trust_anchor  { get; set; default = new TrustAnchor (); }
163   
164     public unowned string nai { get {  _nai = username + "@" + issuer; return _nai;}}
165
166     public bool store_password { get; set; default = false; }
167
168     public bool is_no_identity() 
169     {
170         return (display_name == NO_IDENTITY);
171     }
172
173     public enum DiffFlags {
174         DISPLAY_NAME,
175         USERNAME,
176         PASSWORD,
177         ISSUER,
178         RULES,
179         SERVICES,
180         TRUST_ANCHOR;
181     }
182
183     public int Compare(IdCard other)
184     {
185         int diff = 0;
186         if (this.display_name != other.display_name)
187             diff |= 1 << DiffFlags.DISPLAY_NAME;
188
189         if (this.username != other.username)
190             diff |= 1 << DiffFlags.USERNAME;
191
192         if (this.password != other.password)
193             diff |= 1 << DiffFlags.PASSWORD;
194
195         if (this.issuer != other.issuer)
196             diff |= 1 << DiffFlags.ISSUER;
197
198         if (CompareRules(this.rules, other.rules)!=0)
199             diff |= 1 << DiffFlags.RULES;
200
201         if (CompareStringArrayList(this._services, other._services)!=0)
202             diff |= 1 << DiffFlags.SERVICES;
203
204         if (this.trust_anchor.Compare(other.trust_anchor)!=0)
205             diff |= 1 << DiffFlags.TRUST_ANCHOR;
206
207         // stdout.printf("Diff Flags: %x\n", diff);
208         return diff;
209     }
210
211     public static IdCard NewNoIdentity() 
212     { 
213         IdCard card = new IdCard();
214         card.display_name = NO_IDENTITY;
215         return card;
216     }
217
218     ~IdCard() {
219         password = null;
220     }
221
222     internal void add_rule(Rule rule) {
223         _rules += rule;
224     }
225 }
226
227 public int CompareRules(Rule[] a, Rule[] b)
228 {
229     if (a.length != b.length) {
230         return 1;
231     }
232
233     for (int i = 0; i < a.length; i++) {
234         if (a[i].Compare(b[i]) != 0) {
235             return 1;
236         }
237     }
238     return 0;
239 }
240
241 public int CompareStringArrayList(ArrayList<string> a, ArrayList<string> b)
242 {
243     if (a.size != b.size) {
244         return 1;
245     }
246
247     for (int i = 0; i < a.size; i++) {
248         if (a[i] != b[i]) {
249             return 1;
250         }
251     }
252     return 0;
253 }