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