2 * Copyright (c) 2011-2016, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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
37 [DBus (name = "org.janet.Moonshot")]
38 public class MoonshotServer : Object {
40 static MoonshotLogger logger = get_logger("MoonshotServer");
42 private string app_name = "Moonshot";
44 private IdentityManagerApp parent_app;
46 public MoonshotServer(IdentityManagerApp app)
48 logger.trace("MoonshotServer.<constructor>; app=" + (app == null ? "null" : "non-null"));
49 this.parent_app = app;
54 logger.trace("MoonshotServer.show_ui");
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!");
62 parent_app.explicitly_launched = true;
63 logger.trace("MoonshotServer.show_ui: returning true");
67 public async bool get_identity(string nai,
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)
77 logger.trace(@"MoonshotServer.get_identity: nai='$nai'; service='$service'");
78 var request = new IdentityRequest(parent_app,
82 logger.trace(@"MoonshotServer.get_identity: Calling request.execute()");
83 request.set_callback((IdentityRequest) => get_identity.callback());
85 logger.trace(@"MoonshotServer.get_identity: Back from request.execute()");
87 logger.trace(@"MoonshotServer.get_identity: back from yield");
91 server_certificate_hash = "";
93 subject_name_constraint = "";
94 subject_alt_name_constraint = "";
96 var id_card = request.id_card;
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;
103 password_out = id_card.password;
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;
112 if (password_out == null)
114 if (server_certificate_hash == null)
115 server_certificate_hash = "";
116 if (ca_certificate == null)
118 if (subject_name_constraint == null)
119 subject_name_constraint = "";
120 if (subject_alt_name_constraint == null)
121 subject_alt_name_constraint = "";
123 logger.trace(@"MoonshotServer.get_identity: returning with nai_out=$nai_out");
128 logger.trace("MoonshotServer.get_identity: returning false");
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)
139 logger.trace("MoonshotServer.get_default_identity");
140 var request = new IdentityRequest.default(parent_app);
141 request.set_callback((IdentityRequest) => get_default_identity.callback());
147 server_certificate_hash = "";
149 subject_name_constraint = "";
150 subject_alt_name_constraint = "";
152 if (request.id_card != null)
154 nai_out = request.id_card.nai;
155 password_out = request.id_card.password;
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;
164 if (password_out == null)
166 if (server_certificate_hash == null)
167 server_certificate_hash = "";
168 if (ca_certificate == null)
170 if (subject_name_constraint == null)
171 subject_name_constraint = "";
172 if (subject_alt_name_constraint == null)
173 subject_alt_name_constraint = "";
175 logger.trace("MoonshotServer.get_default_identity: returning true");
182 public bool install_id_card(string display_name,
186 string[] ?rules_patterns,
187 string[] ?rules_always_confirm,
193 int force_flat_file_store)
195 IdCard idcard = new IdCard();
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);
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));
212 idcard.set_trust_anchor_from_store(ta);
214 logger.trace("install_id_card: Card '%s' has services: '%s'"
215 .printf(idcard.display_name, idcard.get_services_string("; ")));
217 logger.trace(@"Installing IdCard named '$(idcard.display_name)'; ca_cert='$(idcard.trust_anchor.ca_cert)'; server_cert='$(idcard.trust_anchor.server_cert)'");
220 if (rules_patterns.length == rules_always_confirm.length)
222 /* workaround Centos vala array property bug: use temp array */
223 Rule[] rules = new Rule[rules_patterns.length];
225 for (int i = 0; i < rules.length; i++)
227 rules[i].pattern = rules_patterns[i];
228 rules[i].always_confirm = rules_always_confirm[i];
230 idcard.rules = rules;
233 ArrayList<IdCard>? old_duplicates = null;
234 var ret = parent_app.add_identity(idcard, (force_flat_file_store != 0), out old_duplicates);
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);
247 public int install_from_file(string file_name)
249 var webp = new WebProvisioning.Parser(file_name);
253 int installed_cards = 0;
254 foreach (IdCard card in webp.cards)
256 string[] rules_patterns = {};
257 string[] rules_always_confirm = {};
259 if (card.rules.length > 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)
266 rules_patterns[i] = r.pattern;
267 rules_always_confirm[i] = r.always_confirm;
273 // prevent a crash by holding the reference to otherwise
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];
283 logger.trace(@"install_from_file: Adding card with display name '$(card.display_name)'");
284 result = install_id_card(card.display_name,
289 rules_always_confirm,
291 card.trust_anchor.ca_cert,
292 card.trust_anchor.subject,
293 card.trust_anchor.subject_alt,
294 card.trust_anchor.server_cert,
300 return installed_cards;
308 using MoonshotRpcInterface;
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.
315 * Shutdown is automatically done by the RPC runtime when the
318 public class MoonshotServer : Object {
319 private static IdentityManagerApp parent_app;
321 private static MoonshotServer instance = null;
323 public static void start(IdentityManagerApp app)
326 Rpc.server_start(MoonshotRpcInterface.spec, "/org/janet/Moonshot", Rpc.Flags.PER_USER);
329 public static MoonshotServer get_instance()
331 if (instance == null)
332 instance = new MoonshotServer();
336 [CCode (cname = "moonshot_get_identity_rpc")]
337 public static void get_identity(Rpc.AsyncCall call,
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)
348 logger.trace("(static) get_identity");
352 var request = new IdentityRequest(parent_app,
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);
362 request.mutex.lock();
363 Idle.add(request.execute);
365 while (request.complete == false)
366 request.cond.wait(request.mutex);
370 server_certificate_hash = "";
372 subject_name_constraint = "";
373 subject_alt_name_constraint = "";
375 var id_card = request.id_card;
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;
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);
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
400 call.return(&result);
402 request.cond.signal();
403 request.mutex.unlock();
406 [CCode (cname = "moonshot_get_default_identity_rpc")]
407 public static void get_default_identity(Rpc.AsyncCall call,
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)
415 logger.trace("(static) get_default_identity");
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);
424 request.mutex.lock();
425 Idle.add(request.execute);
427 while (request.complete == false)
428 request.cond.wait(request.mutex);
432 server_certificate_hash = "";
434 subject_name_constraint = "";
435 subject_alt_name_constraint = "";
437 if (request.id_card != null)
439 nai_out = request.id_card.nai;
440 password_out = request.id_card.password;
441 server_certificate_hash = "certificate";
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);
457 call.return(&result);
459 request.cond.signal();
460 request.mutex.unlock();
463 // Called from the main loop thread when an identity has
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();
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();
476 [CCode (cname = "moonshot_install_id_card_rpc")]
477 public static bool install_id_card(string display_name,
481 string[] rules_patterns,
482 string[] rules_always_confirm,
488 bool force_flat_file_store)
490 logger.trace("(static) install_id_card");
491 IdCard idcard = new IdCard();
493 bool success = false;
494 Mutex mutex = new Mutex();
495 Cond cond = new Cond();
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;
507 if (rules_patterns.length == rules_always_confirm.length)
509 idcard.rules = new Rule[rules_patterns.length];
511 for (int i = 0; i < idcard.rules.length; i++)
513 idcard.rules[i].pattern = rules_patterns[i];
514 idcard.rules[i].always_confirm = rules_always_confirm[i];
520 ArrayList<IdCard>? old_duplicates = null;
521 // Defer addition to the main loop thread.
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);