Implement web provisioning on Windows
[moonshot-ui.git] / src / moonshot-webp-parser.vala
1 using Moonshot;
2
3 namespace WebProvisioning
4
5   IdCard card;
6   IdCard[] cards;
7
8   bool
9   check_stack (SList<string> stack, string[] reference)
10   {
11     if (stack.length () < reference.length)
12       return false;
13     
14     for (int i = 0; i<reference.length; i++)
15     {
16       if (stack.nth_data(i) != reference[i])
17         return false;
18     }
19
20     return true;
21   }
22
23   bool
24   always_confirm_handler (SList<string> stack)
25   {
26     string[] always_confirm_path = {"always-confirm", "rule", "selection-rules", "identity", "identities"};
27     
28     return check_stack (stack, always_confirm_path);
29   }
30   
31   bool
32   pattern_handler (SList<string> stack)
33   {
34     string[] pattern_path = {"pattern", "rule", "selection-rules", "identity", "identities"};
35     
36     return check_stack (stack, pattern_path);
37   }
38
39   bool
40   server_cert_handler (SList<string> stack)
41   {
42     string[] server_cert_path = {"server-cert", "trust-anchor", "identity", "identities"};
43     
44     return check_stack (stack, server_cert_path);
45   }
46
47   bool
48   subject_alt_handler (SList<string> stack)
49   {
50     string[] subject_alt_path = {"subject-alt", "trust-anchor", "identity", "identities"};
51     
52     return check_stack (stack, subject_alt_path);
53   }
54
55   bool
56   subject_handler (SList<string> stack)
57   {
58     string[] subject_path = {"subject", "trust-anchor", "identity", "identities"};
59     
60     return check_stack (stack, subject_path);
61   }
62   
63   bool
64   ca_cert_handler (SList<string> stack)
65   {
66     string[] ca_path = {"ca-cert", "trust-anchor", "identity", "identities"};
67     
68     return check_stack (stack, ca_path);
69   }
70
71   bool
72   realm_handler (SList<string> stack)
73   {
74     string[] realm_path = {"realm", "identity", "identities"};
75     
76     return check_stack (stack, realm_path);
77   }
78
79   bool
80   password_handler (SList<string> stack)
81   {
82     string[] password_path = {"password", "identity", "identities"};
83     
84     return check_stack (stack, password_path);
85   }
86
87   bool
88   user_handler (SList<string> stack)
89   {
90     string[] user_path = {"user", "identity", "identities"};
91     
92     return check_stack (stack, user_path);
93   }
94
95   bool
96   display_name_handler (SList<string> stack)
97   {
98     string[] display_name_path = {"display-name", "identity", "identities"};
99     
100     return check_stack (stack, display_name_path);
101   }
102   
103   public void
104   start_element_func (MarkupParseContext context,
105                       string element_name,
106                       string[] attribute_names,
107                       string[] attribute_values) throws MarkupError
108   {
109     if (element_name == "identity")
110     {
111       IdCard[] tmp_cards = cards;
112
113       cards = new IdCard[tmp_cards.length + 1];
114       for (int i=0; i<tmp_cards.length; i++)
115       {
116         cards[i] = tmp_cards[i];
117       }
118       card = new IdCard();
119       cards[tmp_cards.length] = card;
120     }
121     else if (element_name == "rule")
122     {
123       Rule[] tmp_rules = card.rules;
124       card.rules = new Rule[tmp_rules.length + 1];
125       for (int i=0; i<tmp_rules.length; i++)
126       {
127         card.rules[i] = tmp_rules[i];
128       }
129       
130       card.rules[tmp_rules.length] = Rule();
131     }
132   }
133
134   public void
135   text_element_func (MarkupParseContext context,
136                      string             text,
137                      size_t             text_len) throws MarkupError
138   {
139     unowned SList<string> stack = context.get_element_stack ();
140     
141     if (text_len < 1)
142       return;
143     
144     if (stack.nth_data(0) == "display-name" && display_name_handler (stack))
145     {
146       card.display_name = text;
147     }
148     else if (stack.nth_data(0) == "user" && user_handler (stack))
149     {
150       card.username = text;
151     }
152     else if (stack.nth_data(0) == "password" && password_handler (stack))
153     {
154       card.password = text;
155     }
156     else if (stack.nth_data(0) == "realm" && realm_handler (stack))
157     {
158       card.issuer = text;
159     }
160     else if (stack.nth_data(0) == "service")
161     {
162       string[] services = card.services;
163       card.services = new string[services.length + 1];
164       for (int i = 0; i<services.length; i++)
165       {
166         card.services[i] = services[i];
167       }
168       card.services[services.length] = text;
169     }
170     /* Rules */
171     else if (stack.nth_data(0) == "pattern" && pattern_handler (stack))
172     {
173       card.rules[card.rules.length - 1].pattern = text;
174     }
175     else if (stack.nth_data(0) == "always-confirm" && always_confirm_handler (stack))
176     {
177       if (text == "true" || text == "false")
178         card.rules[card.rules.length - 1].always_confirm = text;
179     }
180     /*Trust anchor*/
181     else if (stack.nth_data(0) == "ca-cert" && ca_cert_handler (stack))
182     {
183       card.trust_anchor.ca_cert = text;
184     }
185     else if (stack.nth_data(0) == "subject" && subject_handler (stack))
186     {
187       card.trust_anchor.subject = text;
188     }
189     else if (stack.nth_data(0) == "subject-alt" && subject_alt_handler (stack))
190     {
191       card.trust_anchor.subject_alt = text;
192     }
193     else if (stack.nth_data(0) == "server-cert" && server_cert_handler (stack))
194     {
195       card.trust_anchor.server_cert = text;
196     }
197   }
198
199   class Parser
200   {
201     private MarkupParser parser;
202     private string       text;
203     private string       path;
204     public Parser (string path)
205     {
206       text = "";
207       this.path = path;
208
209       var file = File.new_for_path (path);
210     
211       try
212       {
213         var dis = new DataInputStream (file.read ());
214         string line;
215         while ((line = dis.read_line (null)) != null)
216           text += line;
217       }
218       catch (GLib.Error e)
219       {
220         error ("Could not retreive file size");
221       }
222       
223       parser = {start_element_func, null, text_element_func, null, null};
224     }
225     
226     public void
227     parse ()
228     {
229       var ctx = new MarkupParseContext(parser, 0, null, null);
230       
231       try
232       {
233         ctx.parse (text, text.length);
234       }
235       catch (GLib.Error e)
236       {
237         error ("Could not parse %s, invalid content", path);
238       } 
239     }
240   }
241
242   public static int main (string[] args)
243   {
244     if (args.length < 2)
245     {
246       error ("Usage %s [-a] WEB_PROVISIONING_FILE", args[0]);
247     }
248     
249     if (!FileUtils.test (args[1], FileTest.EXISTS | FileTest.IS_REGULAR))
250     {
251       error ("%s does not exist", args[1]);
252     }
253     
254     var webp = new Parser (args[1]);
255     webp.parse();
256     
257     foreach (IdCard card in cards)
258     {
259       Moonshot.Error error;
260       string[] rules_patterns = {};
261       string[] rules_always_confirm = {};
262         
263       if (card.rules.length > 0)
264       {
265         int i = 0;
266         rules_patterns = new string[card.rules.length];
267         rules_always_confirm = new string[card.rules.length];
268         foreach (Rule r in card.rules)
269         {
270           rules_patterns[i] = r.pattern;
271           rules_always_confirm[i] = r.always_confirm;
272           i++;
273         }
274       }
275
276       Moonshot.install_id_card (card.display_name,
277                                 card.username,
278                                 card.password,
279                                 card.issuer,
280                                 rules_patterns,
281                                 rules_always_confirm,
282                                 card.services,
283                                 card.trust_anchor.ca_cert,
284                                 card.trust_anchor.subject,
285                                 card.trust_anchor.subject_alt,
286                                 card.trust_anchor.server_cert,
287                                 out error);
288
289       if (error != null)
290       {
291         stderr.printf ("Error: %s", error.message);
292         continue;
293       }
294     }
295     
296     return 0;
297   }
298 }