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