Added the MoonshotLogger class, which by default, does nothing. But if the --enable...
[moonshot-ui.git] / src / moonshot-server.vala
1 /*
2  * Copyright (c) 2011-2014, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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
30  * SUCH DAMAGE.
31 */
32 #if IPC_DBUS
33
34 [DBus (name = "org.janet.Moonshot")]
35 public class MoonshotServer : Object {
36
37     static MoonshotLogger logger = get_logger("MoonshotServer");
38
39     private string app_name = "Moonshot";
40
41     private IdentityManagerApp parent_app;
42
43     public MoonshotServer(IdentityManagerApp app)
44     {
45         logger.trace("MoonshotServer.<constructor>; app=" + (app == null ? "null" : "non-null"));
46         this.parent_app = app;
47     }
48
49     public bool show_ui()
50     {
51         logger.trace("MoonshotServer.show_ui");
52
53         if (parent_app.view == null) {
54             stderr.printf(app_name, "show_ui: parent_app.view is null!\n");
55             logger.warn("show_ui: parent_app.view is null!");
56             return false;
57         }
58         parent_app.show();
59         parent_app.explicitly_launched = true;
60         logger.trace("MoonshotServer.show_ui: returning true");
61         return true;
62     }
63
64     public async bool get_identity(string nai,
65                                    string password,
66                                    string service,
67                                    out string nai_out,
68                                    out string password_out,
69                                    out string server_certificate_hash,
70                                    out string ca_certificate,
71                                    out string subject_name_constraint,
72                                    out string subject_alt_name_constraint)
73     {
74         logger.trace(@"MoonshotServer.get_identity: nai='$nai'; service='$service'");
75         var request = new IdentityRequest(parent_app,
76                                           nai,
77                                           password,
78                                           service);
79         logger.trace(@"MoonshotServer.get_identity: Calling request.execute()");
80         request.set_callback((IdentityRequest) => get_identity.callback());
81         request.execute();
82         logger.trace(@"MoonshotServer.get_identity: Back from request.execute()");
83         yield;
84         logger.trace(@"MoonshotServer.get_identity: back from yield");
85
86         nai_out = "";
87         password_out = "";
88         server_certificate_hash = "";
89         ca_certificate = "";
90         subject_name_constraint = "";
91         subject_alt_name_constraint = "";
92
93         var id_card = request.id_card;
94
95         if ((id_card != null) && (id_card.display_name != IdCard.NO_IDENTITY)) {
96             nai_out = id_card.nai;
97             if ((request.password != null) && (request.password != ""))
98                 password_out = request.password;
99             else
100                 password_out = id_card.password;
101
102             server_certificate_hash = id_card.trust_anchor.server_cert;
103             ca_certificate = id_card.trust_anchor.ca_cert;
104             subject_name_constraint = id_card.trust_anchor.subject;
105             subject_alt_name_constraint = id_card.trust_anchor.subject_alt;
106
107             if (nai_out == null)
108                 nai_out = "";
109             if (password_out == null)
110                 password_out = "";
111             if (server_certificate_hash == null)
112                 server_certificate_hash = "";
113             if (ca_certificate == null)
114                 ca_certificate = "";
115             if (subject_name_constraint == null)
116                 subject_name_constraint = "";
117             if (subject_alt_name_constraint == null)
118                 subject_alt_name_constraint = "";
119
120             logger.trace("MoonshotServer.get_identity: returning true");
121
122             return true;
123         }
124
125         logger.trace("MoonshotServer.get_identity: returning false");
126         return false;
127     }
128
129     public async bool get_default_identity(out string nai_out,
130                                            out string password_out,
131                                            out string server_certificate_hash,
132                                            out string ca_certificate,
133                                            out string subject_name_constraint,
134                                            out string subject_alt_name_constraint)
135     {
136         logger.trace("MoonshotServer.get_default_identity");
137         var request = new IdentityRequest.default(parent_app);
138         request.set_callback((IdentityRequest) => get_default_identity.callback());
139         request.execute();
140         yield;
141
142         nai_out = "";
143         password_out = "";
144         server_certificate_hash = "";
145         ca_certificate = "";
146         subject_name_constraint = "";
147         subject_alt_name_constraint = "";
148
149         if (request.id_card != null)
150         {
151             nai_out = request.id_card.nai;
152             password_out = request.id_card.password;
153
154             server_certificate_hash = request.id_card.trust_anchor.server_cert;
155             ca_certificate = request.id_card.trust_anchor.ca_cert;
156             subject_name_constraint = request.id_card.trust_anchor.subject;
157             subject_alt_name_constraint = request.id_card.trust_anchor.subject_alt;
158
159             if (nai_out == null)
160                 nai_out = "";
161             if (password_out == null)
162                 password_out = "";
163             if (server_certificate_hash == null)
164                 server_certificate_hash = "";
165             if (ca_certificate == null)
166                 ca_certificate = "";
167             if (subject_name_constraint == null)
168                 subject_name_constraint = "";
169             if (subject_alt_name_constraint == null)
170                 subject_alt_name_constraint = "";
171
172             logger.trace("MoonshotServer.get_default_identity: returning true");
173             return true;
174         }
175
176         return false;
177     }
178
179     public bool install_id_card(string   display_name,
180                                 string   user_name,
181                                 string   ?password,
182                                 string   ?realm,
183                                 string[] ?rules_patterns,
184                                 string[] ?rules_always_confirm,
185                                 string[] ?services,
186                                 string   ?ca_cert,
187                                 string   ?subject,
188                                 string   ?subject_alt,
189                                 string   ?server_cert,
190                                 int      force_flat_file_store)
191     {
192         IdCard idcard = new IdCard();
193
194         idcard.display_name = display_name;
195         idcard.username = user_name;
196         idcard.password = password;
197         if ((password != null) && (password != ""))
198             idcard.store_password = true;
199         idcard.issuer = realm;
200         idcard.services = services;
201         idcard.trust_anchor.ca_cert = ca_cert;
202         idcard.trust_anchor.subject = subject;
203         idcard.trust_anchor.subject_alt = subject_alt;
204         idcard.trust_anchor.server_cert = server_cert;
205
206         if (rules_patterns.length == rules_always_confirm.length)
207         {
208             /* workaround Centos vala array property bug: use temp array */
209             Rule[] rules = new Rule[rules_patterns.length];
210          
211             for (int i = 0; i < rules.length; i++)
212             { 
213                 rules[i].pattern = rules_patterns[i];
214                 rules[i].always_confirm = rules_always_confirm[i];
215             }
216             idcard.rules = rules;
217         }
218
219         return parent_app.add_identity(idcard, force_flat_file_store!=0);
220     }
221
222
223     public int install_from_file(string file_name)
224     {
225         var webp = new WebProvisioning.Parser(file_name);
226
227         webp.parse();
228         bool result = false;
229         int installed_cards = 0;
230         foreach (IdCard card in WebProvisioning.cards)
231         {
232             string[] rules_patterns = {};
233             string[] rules_always_confirm = {};
234         
235             if (card.rules.length > 0)
236             {
237                 int i = 0;
238                 rules_patterns = new string[card.rules.length];
239                 rules_always_confirm = new string[card.rules.length];
240                 foreach (Rule r in card.rules)
241                 {
242                     rules_patterns[i] = r.pattern;
243                     rules_always_confirm[i] = r.always_confirm;
244                     i++;
245                 }
246             } 
247
248             result = install_id_card(card.display_name,
249                                      card.username,
250                                      card.password,
251                                      card.issuer,
252                                      rules_patterns,
253                                      rules_always_confirm,
254                                      card.services,
255                                      card.trust_anchor.ca_cert,
256                                      card.trust_anchor.subject,
257                                      card.trust_anchor.subject_alt,
258                                      card.trust_anchor.server_cert,
259                                      0);
260             if (result) {
261                 installed_cards++;
262             }
263         }
264         return installed_cards;
265     }
266 }
267
268
269 #elif IPC_MSRPC
270
271 using Rpc;
272 using MoonshotRpcInterface;
273
274 /* This class must be a singleton, because we use a global RPC
275  * binding handle. I cannot picture a situation where more than
276  * one instance of the same interface would be needed so this
277  * shouldn't be a problem.
278  *
279  * Shutdown is automatically done by the RPC runtime when the
280  * process ends
281  */
282 public class MoonshotServer : Object {
283     private static IdentityManagerApp parent_app;
284
285     private static MoonshotServer instance = null;
286
287     public static void start(IdentityManagerApp app)
288     {
289         parent_app = app;
290         Rpc.server_start(MoonshotRpcInterface.spec, "/org/janet/Moonshot", Rpc.Flags.PER_USER);
291     }
292
293     public static MoonshotServer get_instance()
294     {
295         if (instance == null)
296             instance = new MoonshotServer();
297         return instance;
298     }
299
300     [CCode (cname = "moonshot_get_identity_rpc")]
301     public static void get_identity(Rpc.AsyncCall call,
302                                     string nai,
303                                     string password,
304                                     string service,
305                                     ref string nai_out,
306                                     ref string password_out,
307                                     ref string server_certificate_hash,
308                                     ref string ca_certificate,
309                                     ref string subject_name_constraint,
310                                     ref string subject_alt_name_constraint)
311     {
312         logger.trace("(static) get_identity");
313
314         bool result = false;
315
316         var request = new IdentityRequest(parent_app,
317                                           nai,
318                                           password,
319                                           service);
320
321         // Pass execution to the main loop and block the RPC thread
322         request.mutex = new Mutex();
323         request.cond = new Cond();
324         request.set_callback(return_identity_cb);
325
326         request.mutex.lock();
327         Idle.add(request.execute);
328
329         while (request.complete == false)
330             request.cond.wait(request.mutex);
331
332         nai_out = "";
333         password_out = "";
334         server_certificate_hash = "";
335         ca_certificate = "";
336         subject_name_constraint = "";
337         subject_alt_name_constraint = "";
338
339         var id_card = request.id_card;
340
341         if (id_card != null) {
342             // The strings are freed by the RPC runtime
343             nai_out = id_card.nai;
344             password_out = id_card.password;
345             server_certificate_hash = id_card.trust_anchor.server_cert;
346             ca_certificate = id_card.trust_anchor.ca_cert;
347             subject_name_constraint = id_card.trust_anchor.subject;
348             subject_alt_name_constraint = id_card.trust_anchor.subject_alt;
349
350             return_if_fail(nai_out != null);
351             return_if_fail(password_out != null);
352             return_if_fail(server_certificate_hash != null);
353             return_if_fail(ca_certificate != null);
354             return_if_fail(subject_name_constraint != null);
355             return_if_fail(subject_alt_name_constraint != null);
356
357             result = true;
358         }
359
360         // The outputs must be set before this function is called. For this
361         // reason they are 'ref' not 'out' parameters - Vala assigns to the
362         // 'out' parameters only at the end of the function, which is too
363         // late.
364         call.return(&result);
365
366         request.cond.signal();
367         request.mutex.unlock();
368     }
369
370     [CCode (cname = "moonshot_get_default_identity_rpc")]
371     public static void get_default_identity(Rpc.AsyncCall call,
372                                             ref string nai_out,
373                                             ref string password_out,
374                                             ref string server_certificate_hash,
375                                             ref string ca_certificate,
376                                             ref string subject_name_constraint,
377                                             ref string subject_alt_name_constraint)
378     {
379         logger.trace("(static) get_default_identity");
380
381         bool result;
382
383         var request = new IdentityRequest.default(parent_app);
384         request.mutex = new Mutex();
385         request.cond = new Cond();
386         request.set_callback(return_identity_cb);
387
388         request.mutex.lock();
389         Idle.add(request.execute);
390
391         while (request.complete == false)
392             request.cond.wait(request.mutex);
393
394         nai_out = "";
395         password_out = "";
396         server_certificate_hash = "";
397         ca_certificate = "";
398         subject_name_constraint = "";
399         subject_alt_name_constraint = "";
400
401         if (request.id_card != null)
402         {
403             nai_out = request.id_card.nai;
404             password_out = request.id_card.password;
405             server_certificate_hash = "certificate";
406
407             return_if_fail(nai_out != null);
408             return_if_fail(password_out != null);
409             return_if_fail(server_certificate_hash != null);
410             return_if_fail(ca_certificate != null);
411             return_if_fail(subject_name_constraint != null);
412             return_if_fail(subject_alt_name_constraint != null);
413
414             result = true;
415         }
416         else
417         {
418             result = false;
419         }
420
421         call.return(&result);
422
423         request.cond.signal();
424         request.mutex.unlock();
425     }
426
427     // Called from the main loop thread when an identity has
428     // been selected
429     static void return_identity_cb(IdentityRequest request) {
430         // Notify the RPC thread that the request is complete
431         request.mutex.lock();
432         request.cond.signal();
433
434         // Block the main loop until the RPC call has returned
435         // to avoid any races
436         request.cond.wait(request.mutex);
437         request.mutex.unlock();
438     }
439
440     [CCode (cname = "moonshot_install_id_card_rpc")]
441     public static bool install_id_card(string     display_name,
442                                        string     user_name,
443                                        string     password,
444                                        string     realm,
445                                        string[]   rules_patterns,
446                                        string[]   rules_always_confirm,
447                                        string[]   services,
448                                        string     ca_cert,
449                                        string     subject,
450                                        string     subject_alt,
451                                        string     server_cert,
452                                        bool       force_flat_file_store)
453     {
454         logger.trace("(static) install_id_card");
455         IdCard idcard = new IdCard();
456
457         bool success = false;
458         Mutex mutex = new Mutex();
459         Cond cond = new Cond();
460
461         idcard.display_name = display_name;
462         idcard.username = user_name;
463         idcard.password = password;
464         idcard.issuer = realm;
465         idcard.services = services;
466         idcard.trust_anchor.ca_cert = ca_cert;
467         idcard.trust_anchor.subject = subject;
468         idcard.trust_anchor.subject_alt = subject_alt;
469         idcard.trust_anchor.server_cert = server_cert;
470
471         if (rules_patterns.length == rules_always_confirm.length)
472         {
473             idcard.rules = new Rule[rules_patterns.length];
474          
475             for (int i = 0; i < idcard.rules.length; i++)
476             { 
477                 idcard.rules[i].pattern = rules_patterns[i];
478                 idcard.rules[i].always_confirm = rules_always_confirm[i];
479             }
480         }
481
482         mutex.lock();
483
484         // Defer addition to the main loop thread.
485         Idle.add(() => {
486                 mutex.lock();
487                 success = parent_app.add_identity(idcard, force_flat_file_store);
488                 cond.signal();
489                 mutex.unlock();
490                 return false;
491             });
492
493         cond.wait(mutex);
494         mutex.unlock();
495
496         return success;
497     }
498
499 }
500
501
502 #endif