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