Minor refactoring of XML import code
[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 public class TrustAnchor : Object
33 {
34     public string ca_cert {get; set; default = "";}
35     public string subject {get; set; default = "";}
36     public string subject_alt  {get; set; default = "";}
37     public string server_cert  {get; set; default = "";}
38     public int Compare(TrustAnchor other)
39     {
40         if (this.ca_cert != other.ca_cert)
41             return 1;
42         if (this.subject != other.subject)
43             return 1;
44         if (this.subject_alt != other.subject_alt)
45             return 1;
46         if (this.server_cert != other.server_cert)
47             return 1;
48         return 0;
49     }
50 }
51
52 public struct Rule
53 {
54     public string pattern;
55     public string always_confirm;
56     public int Compare(Rule other) {
57         if (this.pattern != other.pattern)
58             return 1;
59         if (this.always_confirm != other.always_confirm)
60             return 1;
61         return 0;
62     }
63 }
64
65 public class IdCard : Object
66 {
67     public const string NO_IDENTITY = "No Identity";
68
69     private string _nai;
70   
71     public string display_name { get; set; default = ""; }
72   
73     public string username { get; set; default = ""; }
74 #if GNOME_KEYRING
75     private unowned string _password;
76     public string password {
77         get {
78             return (_password!=null) ? _password : "";
79         }
80         set {
81             if (_password != null) {
82                 GnomeKeyring.memory_free((void *)_password);
83                 _password = null;
84             }
85             if (value != null)
86                 _password = GnomeKeyring.memory_strdup(value); 
87         }
88     }
89 #else
90     public string password { get; set; default = null; }
91 #endif
92
93     public string issuer { get; set; default = ""; }
94   
95     private Rule[] _rules = {};
96     public Rule[] rules {
97         get {return _rules;}
98         internal set {_rules = value ?? {};}
99     }
100
101     private string[] _services = {};
102     public string[] services {
103         get {return _services;}
104         internal set {_services = value ?? {};}
105     }
106
107     public bool temporary {get; set; default = false; }
108
109     public TrustAnchor trust_anchor  { get; set; default = new TrustAnchor (); }
110   
111     public unowned string nai { get {  _nai = username + "@" + issuer; return _nai;}}
112
113     public bool store_password { get; set; default = false; }
114
115     public bool IsNoIdentity() 
116     {
117         return (display_name == NO_IDENTITY);
118     }
119
120     public enum DiffFlags {
121         DISPLAY_NAME,
122         USERNAME,
123         PASSWORD,
124         ISSUER,
125         RULES,
126         SERVICES,
127         TRUST_ANCHOR;
128     }
129
130     public int Compare(IdCard other)
131     {
132         int diff = 0;
133         if (this.display_name != other.display_name)
134             diff |= 1 << DiffFlags.DISPLAY_NAME;
135
136         if (this.username != other.username)
137             diff |= 1 << DiffFlags.USERNAME;
138
139         if (this.password != other.password)
140             diff |= 1 << DiffFlags.PASSWORD;
141
142         if (this.issuer != other.issuer)
143             diff |= 1 << DiffFlags.ISSUER;
144
145         if (CompareRules(this.rules, other.rules)!=0)
146             diff |= 1 << DiffFlags.RULES;
147
148         if (CompareStringArray(this.services, other.services)!=0)
149             diff |= 1 << DiffFlags.SERVICES;
150
151         if (this.trust_anchor.Compare(other.trust_anchor)!=0)
152             diff |= 1 << DiffFlags.TRUST_ANCHOR;
153
154         // stdout.printf("Diff Flags: %x\n", diff);
155         return diff;
156     }
157
158     public static IdCard NewNoIdentity() 
159     { 
160         IdCard card = new IdCard();
161         card.display_name = NO_IDENTITY;
162         return card;
163     }
164
165     ~IdCard() {
166         password = null;
167     }
168
169     internal void add_rule(Rule rule) {
170         _rules += rule;
171     }
172
173     internal void add_service(string service) {
174         _services += service;
175     }
176 }
177
178 public int CompareRules(Rule[] a, Rule[] b)
179 {
180     if (a.length != b.length) {
181         return 1;
182     }
183
184     for (int i = 0; i < a.length; i++) {
185         if (a[i].Compare(b[i]) != 0) {
186             return 1;
187         }
188     }
189     return 0;
190 }
191
192 public int CompareStringArray(string[] a, string [] b)
193 {
194     if (a.length != b.length) {
195         return 1;
196     }
197
198     for (int i = 0; i < a.length; i++) {
199         if (a[i] != b[i]) {
200             return 1;
201         }
202     }
203     return 0;
204 }