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
38 bool check_stack(SList<string> stack, string[] reference) {
40 if (stack.length() < reference.length)
43 for (int i = 0; i < reference.length; i++)
45 if (stack.nth_data(i) != reference[i])
52 bool always_confirm_handler(SList<string> stack)
54 string[] always_confirm_path = {"always-confirm", "rule", "selection-rules", "identity", "identities"};
56 return check_stack(stack, always_confirm_path);
60 pattern_handler(SList<string> stack)
62 string[] pattern_path = {"pattern", "rule", "selection-rules", "identity", "identities"};
64 return check_stack(stack, pattern_path);
67 bool server_cert_handler(SList<string> stack)
69 string[] server_cert_path = {"server-cert", "trust-anchor", "identity", "identities"};
71 return check_stack(stack, server_cert_path);
74 bool subject_alt_handler(SList<string> stack)
76 string[] subject_alt_path = {"subject-alt", "trust-anchor", "identity", "identities"};
78 return check_stack(stack, subject_alt_path);
81 bool subject_handler(SList<string> stack)
83 string[] subject_path = {"subject", "trust-anchor", "identity", "identities"};
85 return check_stack(stack, subject_path);
88 bool ca_cert_handler(SList<string> stack)
90 string[] ca_path = {"ca-cert", "trust-anchor", "identity", "identities"};
92 return check_stack(stack, ca_path);
95 bool realm_handler(SList<string> stack)
97 string[] realm_path = {"realm", "identity", "identities"};
99 return check_stack(stack, realm_path);
102 bool password_handler(SList<string> stack)
104 string[] password_path = {"password", "identity", "identities"};
106 return check_stack(stack, password_path);
109 bool user_handler(SList<string> stack)
111 string[] user_path = {"user", "identity", "identities"};
113 return check_stack(stack, user_path);
116 bool display_name_handler(SList<string> stack)
118 string[] display_name_path = {"display-name", "identity", "identities"};
120 return check_stack(stack, display_name_path);
123 public void start_element_func(MarkupParseContext context,
125 string[] attribute_names,
126 string[] attribute_values) throws MarkupError
128 if (element_name == "identity")
130 IdCard[] tmp_cards = cards;
132 cards = new IdCard[tmp_cards.length + 1];
133 for (int i = 0; i < tmp_cards.length; i++)
135 cards[i] = tmp_cards[i];
138 cards[tmp_cards.length] = card;
140 else if (element_name == "rule")
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++)
146 card.rules[i] = tmp_rules[i];
149 card.rules[tmp_rules.length] = Rule();
154 text_element_func(MarkupParseContext context,
156 size_t text_len) throws MarkupError
158 unowned SList<string> stack = context.get_element_stack();
163 if (stack.nth_data(0) == "display-name" && display_name_handler(stack))
165 card.display_name = text;
167 else if (stack.nth_data(0) == "user" && user_handler(stack))
169 card.username = text;
171 else if (stack.nth_data(0) == "password" && password_handler(stack))
173 card.password = text;
175 else if (stack.nth_data(0) == "realm" && realm_handler(stack))
179 else if (stack.nth_data(0) == "service")
181 string[] services = card.services;
182 card.services = new string[services.length + 1];
183 for (int i = 0; i < services.length; i++)
185 card.services[i] = services[i];
187 card.services[services.length] = text;
190 else if (stack.nth_data(0) == "pattern" && pattern_handler(stack))
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;
196 else if (stack.nth_data(0) == "always-confirm" && always_confirm_handler(stack))
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;
205 else if (stack.nth_data(0) == "ca-cert" && ca_cert_handler(stack))
207 card.trust_anchor.ca_cert = text;
209 else if (stack.nth_data(0) == "subject" && subject_handler(stack))
211 card.trust_anchor.subject = text;
213 else if (stack.nth_data(0) == "subject-alt" && subject_alt_handler(stack))
215 card.trust_anchor.subject_alt = text;
217 else if (stack.nth_data(0) == "server-cert" && server_cert_handler(stack))
219 card.trust_anchor.server_cert = text;
225 private MarkupParser parser;
228 public Parser(string path) {
232 var file = File.new_for_path(path);
236 var dis = new DataInputStream(file.read());
238 while ((line = dis.read_line(null)) != null)
243 error("Could not retreive file size");
246 parser = {start_element_func, null, text_element_func, null, null};
249 public void parse() {
250 var ctx = new MarkupParseContext(parser, 0, null, null);
254 ctx.parse(text, text.length);
258 error("Could not parse %s, invalid content", path);