2 * Copyright (c) 2011-2014, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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
33 namespace WebProvisioning
39 check_stack (SList<string> stack, string[] reference)
41 if (stack.length () < reference.length)
44 for (int i = 0; i<reference.length; i++)
46 if (stack.nth_data(i) != reference[i])
54 always_confirm_handler (SList<string> stack)
56 string[] always_confirm_path = {"always-confirm", "rule", "selection-rules", "identity", "identities"};
58 return check_stack (stack, always_confirm_path);
62 pattern_handler (SList<string> stack)
64 string[] pattern_path = {"pattern", "rule", "selection-rules", "identity", "identities"};
66 return check_stack (stack, pattern_path);
70 server_cert_handler (SList<string> stack)
72 string[] server_cert_path = {"server-cert", "trust-anchor", "identity", "identities"};
74 return check_stack (stack, server_cert_path);
78 subject_alt_handler (SList<string> stack)
80 string[] subject_alt_path = {"subject-alt", "trust-anchor", "identity", "identities"};
82 return check_stack (stack, subject_alt_path);
86 subject_handler (SList<string> stack)
88 string[] subject_path = {"subject", "trust-anchor", "identity", "identities"};
90 return check_stack (stack, subject_path);
94 ca_cert_handler (SList<string> stack)
96 string[] ca_path = {"ca-cert", "trust-anchor", "identity", "identities"};
98 return check_stack (stack, ca_path);
102 realm_handler (SList<string> stack)
104 string[] realm_path = {"realm", "identity", "identities"};
106 return check_stack (stack, realm_path);
110 password_handler (SList<string> stack)
112 string[] password_path = {"password", "identity", "identities"};
114 return check_stack (stack, password_path);
118 user_handler (SList<string> stack)
120 string[] user_path = {"user", "identity", "identities"};
122 return check_stack (stack, user_path);
126 display_name_handler (SList<string> stack)
128 string[] display_name_path = {"display-name", "identity", "identities"};
130 return check_stack (stack, display_name_path);
134 start_element_func (MarkupParseContext context,
136 string[] attribute_names,
137 string[] attribute_values) throws MarkupError
139 if (element_name == "identity")
141 IdCard[] tmp_cards = cards;
143 cards = new IdCard[tmp_cards.length + 1];
144 for (int i=0; i<tmp_cards.length; i++)
146 cards[i] = tmp_cards[i];
149 cards[tmp_cards.length] = card;
151 else if (element_name == "rule")
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++)
157 card.rules[i] = tmp_rules[i];
160 card.rules[tmp_rules.length] = Rule();
165 text_element_func (MarkupParseContext context,
167 size_t text_len) throws MarkupError
169 unowned SList<string> stack = context.get_element_stack ();
174 if (stack.nth_data(0) == "display-name" && display_name_handler (stack))
176 card.display_name = text;
178 else if (stack.nth_data(0) == "user" && user_handler (stack))
180 card.username = text;
182 else if (stack.nth_data(0) == "password" && password_handler (stack))
184 card.password = text;
186 else if (stack.nth_data(0) == "realm" && realm_handler (stack))
190 else if (stack.nth_data(0) == "service")
192 string[] services = card.services;
193 card.services = new string[services.length + 1];
194 for (int i = 0; i<services.length; i++)
196 card.services[i] = services[i];
198 card.services[services.length] = text;
201 else if (stack.nth_data(0) == "pattern" && pattern_handler (stack))
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;
207 else if (stack.nth_data(0) == "always-confirm" && always_confirm_handler (stack))
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;
216 else if (stack.nth_data(0) == "ca-cert" && ca_cert_handler (stack))
218 card.trust_anchor.ca_cert = text;
220 else if (stack.nth_data(0) == "subject" && subject_handler (stack))
222 card.trust_anchor.subject = text;
224 else if (stack.nth_data(0) == "subject-alt" && subject_alt_handler (stack))
226 card.trust_anchor.subject_alt = text;
228 else if (stack.nth_data(0) == "server-cert" && server_cert_handler (stack))
230 card.trust_anchor.server_cert = text;
236 private MarkupParser parser;
239 public Parser (string path)
244 var file = File.new_for_path (path);
248 var dis = new DataInputStream (file.read ());
250 while ((line = dis.read_line (null)) != null)
255 error ("Could not retreive file size");
258 parser = {start_element_func, null, text_element_func, null, null};
264 var ctx = new MarkupParseContext(parser, 0, null, null);
268 ctx.parse (text, text.length);
272 error ("Could not parse %s, invalid content", path);