2 * Copyright (c) 2011-2016, 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
35 namespace WebProvisioning
37 bool check_stack(SList<string> stack, string[] reference) {
39 if (stack.length() < reference.length)
42 for (int i = 0; i < reference.length; i++)
44 if (stack.nth_data(i) != reference[i])
51 bool always_confirm_handler(SList<string> stack)
53 string[] always_confirm_path = {"always-confirm", "rule", "selection-rules", "identity", "identities"};
55 return check_stack(stack, always_confirm_path);
59 pattern_handler(SList<string> stack)
61 string[] pattern_path = {"pattern", "rule", "selection-rules", "identity", "identities"};
63 return check_stack(stack, pattern_path);
66 bool server_cert_handler(SList<string> stack)
68 string[] server_cert_path = {"server-cert", "trust-anchor", "identity", "identities"};
70 return check_stack(stack, server_cert_path);
73 bool subject_alt_handler(SList<string> stack)
75 string[] subject_alt_path = {"subject-alt", "trust-anchor", "identity", "identities"};
77 return check_stack(stack, subject_alt_path);
80 bool subject_handler(SList<string> stack)
82 string[] subject_path = {"subject", "trust-anchor", "identity", "identities"};
84 return check_stack(stack, subject_path);
87 bool ca_cert_handler(SList<string> stack)
89 string[] ca_path = {"ca-cert", "trust-anchor", "identity", "identities"};
91 return check_stack(stack, ca_path);
94 bool realm_handler(SList<string> stack)
96 string[] realm_path = {"realm", "identity", "identities"};
98 return check_stack(stack, realm_path);
101 bool password_handler(SList<string> stack)
103 string[] password_path = {"password", "identity", "identities"};
105 return check_stack(stack, password_path);
108 bool user_handler(SList<string> stack)
110 string[] user_path = {"user", "identity", "identities"};
112 return check_stack(stack, user_path);
115 bool display_name_handler(SList<string> stack)
117 string[] display_name_path = {"display-name", "identity", "identities"};
119 return check_stack(stack, display_name_path);
122 public class Parser : Object
124 private static MoonshotLogger logger = new MoonshotLogger("WebProvisioning");
126 private void start_element_func(MarkupParseContext context,
128 string[] attribute_names,
129 string[] attribute_values) throws MarkupError
131 if (element_name == "identity")
141 else if (element_name == "rule")
143 card.add_rule(Rule());
147 private void end_element_func(MarkupParseContext context,
148 string element_name) throws MarkupError
150 if (element_name == "identity")
152 if (ta_ca_cert != "" || ta_server_cert != "") {
153 var ta = new TrustAnchor(ta_ca_cert,
158 // Set the datetime_added in moonshot-server.vala, since it doesn't get sent via IPC
159 card.set_trust_anchor_from_store(ta);
165 text_element_func(MarkupParseContext context,
167 size_t text_len) throws MarkupError {
168 unowned SList<string> stack = context.get_element_stack();
173 if (stack.nth_data(0) == "display-name" && display_name_handler(stack))
175 card.display_name = text;
177 else if (stack.nth_data(0) == "user" && user_handler(stack))
179 card.username = text;
181 else if (stack.nth_data(0) == "password" && password_handler(stack))
183 card.password = text;
185 else if (stack.nth_data(0) == "realm" && realm_handler(stack))
189 else if (stack.nth_data(0) == "service")
191 card.services.add(text);
195 else if (stack.nth_data(0) == "pattern" && pattern_handler(stack))
197 /* use temp array to workaround valac 0.10 bug accessing array property length */
198 var temp = card.rules;
199 card.rules[temp.length - 1].pattern = text;
201 else if (stack.nth_data(0) == "always-confirm" && always_confirm_handler(stack))
203 if (text == "true" || text == "false") {
204 /* use temp array to workaround valac 0.10 bug accessing array property length*/
205 var temp = card.rules;
206 card.rules[temp.length - 1].always_confirm = text;
209 else if (stack.nth_data(0) == "ca-cert" && ca_cert_handler(stack))
211 ta_ca_cert = text ?? "";
213 else if (stack.nth_data(0) == "server-cert" && server_cert_handler(stack))
215 ta_server_cert = text ?? "";
217 else if (stack.nth_data(0) == "subject" && subject_handler(stack))
221 else if (stack.nth_data(0) == "subject-alt" && subject_alt_handler(stack))
223 ta_subject_alt = text;
227 private const MarkupParser parser = {
228 start_element_func, end_element_func, text_element_func, null, null
231 private MarkupParseContext ctx;
236 private string ta_ca_cert;
237 private string ta_server_cert;
238 private string ta_subject;
239 private string ta_subject_alt;
242 private IdCard[] _cards = {};
244 public IdCard[] cards {
246 private set {_cards = value ?? new IdCard[0] ;}
249 public Parser(string path) {
251 ctx = new MarkupParseContext(parser, 0, this, null);
256 var file = File.new_for_path(path);
260 var dis = new DataInputStream(file.read());
262 while ((line = dis.read_line(null)) != null) {
265 // Preserve newlines.
267 // This may add an extra newline at EOF. Maybe use
268 // dis.read_upto("\n", ...) followed by dis.read_byte() instead?
274 error("Could not retreive file size");
278 public void parse() {
281 ctx.parse(text, text.length);
285 error("Could not parse %s, invalid content", path);