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