Add copyright to source code
[moonshot-ui.git] / src / moonshot-provisioning-common.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 namespace WebProvisioning
34
35   IdCard card;
36   IdCard[] cards;
37
38   bool
39   check_stack (SList<string> stack, string[] reference)
40   {
41     if (stack.length () < reference.length)
42       return false;
43     
44     for (int i = 0; i<reference.length; i++)
45     {
46       if (stack.nth_data(i) != reference[i])
47         return false;
48     }
49
50     return true;
51   }
52
53   bool
54   always_confirm_handler (SList<string> stack)
55   {
56     string[] always_confirm_path = {"always-confirm", "rule", "selection-rules", "identity", "identities"};
57     
58     return check_stack (stack, always_confirm_path);
59   }
60   
61   bool
62   pattern_handler (SList<string> stack)
63   {
64     string[] pattern_path = {"pattern", "rule", "selection-rules", "identity", "identities"};
65     
66     return check_stack (stack, pattern_path);
67   }
68
69   bool
70   server_cert_handler (SList<string> stack)
71   {
72     string[] server_cert_path = {"server-cert", "trust-anchor", "identity", "identities"};
73     
74     return check_stack (stack, server_cert_path);
75   }
76
77   bool
78   subject_alt_handler (SList<string> stack)
79   {
80     string[] subject_alt_path = {"subject-alt", "trust-anchor", "identity", "identities"};
81     
82     return check_stack (stack, subject_alt_path);
83   }
84
85   bool
86   subject_handler (SList<string> stack)
87   {
88     string[] subject_path = {"subject", "trust-anchor", "identity", "identities"};
89     
90     return check_stack (stack, subject_path);
91   }
92   
93   bool
94   ca_cert_handler (SList<string> stack)
95   {
96     string[] ca_path = {"ca-cert", "trust-anchor", "identity", "identities"};
97     
98     return check_stack (stack, ca_path);
99   }
100
101   bool
102   realm_handler (SList<string> stack)
103   {
104     string[] realm_path = {"realm", "identity", "identities"};
105     
106     return check_stack (stack, realm_path);
107   }
108
109   bool
110   password_handler (SList<string> stack)
111   {
112     string[] password_path = {"password", "identity", "identities"};
113     
114     return check_stack (stack, password_path);
115   }
116
117   bool
118   user_handler (SList<string> stack)
119   {
120     string[] user_path = {"user", "identity", "identities"};
121     
122     return check_stack (stack, user_path);
123   }
124
125   bool
126   display_name_handler (SList<string> stack)
127   {
128     string[] display_name_path = {"display-name", "identity", "identities"};
129     
130     return check_stack (stack, display_name_path);
131   }
132   
133   public void
134   start_element_func (MarkupParseContext context,
135                       string element_name,
136                       string[] attribute_names,
137                       string[] attribute_values) throws MarkupError
138   {
139     if (element_name == "identity")
140     {
141       IdCard[] tmp_cards = cards;
142
143       cards = new IdCard[tmp_cards.length + 1];
144       for (int i=0; i<tmp_cards.length; i++)
145       {
146         cards[i] = tmp_cards[i];
147       }
148       card = new IdCard();
149       cards[tmp_cards.length] = card;
150     }
151     else if (element_name == "rule")
152     {
153       Rule[] tmp_rules = card.rules;
154       card.rules = new Rule[tmp_rules.length + 1];
155       for (int i=0; i<tmp_rules.length; i++)
156       {
157         card.rules[i] = tmp_rules[i];
158       }
159       
160       card.rules[tmp_rules.length] = Rule();
161     }
162   }
163
164   public void
165   text_element_func (MarkupParseContext context,
166                      string             text,
167                      size_t             text_len) throws MarkupError
168   {
169     unowned SList<string> stack = context.get_element_stack ();
170     
171     if (text_len < 1)
172       return;
173     
174     if (stack.nth_data(0) == "display-name" && display_name_handler (stack))
175     {
176       card.display_name = text;
177     }
178     else if (stack.nth_data(0) == "user" && user_handler (stack))
179     {
180       card.username = text;
181     }
182     else if (stack.nth_data(0) == "password" && password_handler (stack))
183     {
184       card.password = text;
185     }
186     else if (stack.nth_data(0) == "realm" && realm_handler (stack))
187     {
188       card.issuer = text;
189     }
190     else if (stack.nth_data(0) == "service")
191     {
192       string[] services = card.services;
193       card.services = new string[services.length + 1];
194       for (int i = 0; i<services.length; i++)
195       {
196         card.services[i] = services[i];
197       }
198       card.services[services.length] = text;
199     }
200     /* Rules */
201     else if (stack.nth_data(0) == "pattern" && pattern_handler (stack))
202     {
203       /* use temp array to workaround valac 0.10 bug accessing array property length */ 
204       var temp = card.rules;
205       card.rules[temp.length - 1].pattern = text;
206     }
207     else if (stack.nth_data(0) == "always-confirm" && always_confirm_handler (stack))
208     {
209       if (text == "true" || text == "false") {
210         /* use temp array to workaround valac 0.10 bug accessing array property length*/
211         var temp = card.rules;
212         card.rules[temp.length - 1].always_confirm = text;
213       }
214     }
215     /*Trust anchor*/
216     else if (stack.nth_data(0) == "ca-cert" && ca_cert_handler (stack))
217     {
218       card.trust_anchor.ca_cert = text;
219     }
220     else if (stack.nth_data(0) == "subject" && subject_handler (stack))
221     {
222       card.trust_anchor.subject = text;
223     }
224     else if (stack.nth_data(0) == "subject-alt" && subject_alt_handler (stack))
225     {
226       card.trust_anchor.subject_alt = text;
227     }
228     else if (stack.nth_data(0) == "server-cert" && server_cert_handler (stack))
229     {
230       card.trust_anchor.server_cert = text;
231     }
232   }
233
234   class Parser
235   {
236     private MarkupParser parser;
237     private string       text;
238     private string       path;
239     public Parser (string path)
240     {
241       text = "";
242       this.path = path;
243
244       var file = File.new_for_path (path);
245     
246       try
247       {
248         var dis = new DataInputStream (file.read ());
249         string line;
250         while ((line = dis.read_line (null)) != null)
251           text += line;
252       }
253       catch (GLib.Error e)
254       {
255         error ("Could not retreive file size");
256       }
257       
258       parser = {start_element_func, null, text_element_func, null, null};
259     }
260     
261     public void
262     parse ()
263     {
264       var ctx = new MarkupParseContext(parser, 0, null, null);
265       
266       try
267       {
268         ctx.parse (text, text.length);
269       }
270       catch (GLib.Error e)
271       {
272         error ("Could not parse %s, invalid content", path);
273       } 
274     }
275   }
276 }