3 [DBus (name = "org.janet.Moonshot")]
4 public class MoonshotServer : Object {
6 private IdentityManagerApp parent_app;
8 public MoonshotServer (IdentityManagerApp app)
10 this.parent_app = app;
15 if (parent_app.view == null) {
19 parent_app.explicitly_launched = true;
23 public async bool get_identity (string nai,
27 out string password_out,
28 out string server_certificate_hash,
29 out string ca_certificate,
30 out string subject_name_constraint,
31 out string subject_alt_name_constraint)
33 var request = new IdentityRequest (parent_app,
37 request.set_callback ((IdentityRequest) => get_identity.callback());
43 server_certificate_hash = "";
45 subject_name_constraint = "";
46 subject_alt_name_constraint = "";
48 var id_card = request.id_card;
50 if ((id_card != null) && (id_card.display_name != IdCard.NO_IDENTITY)) {
51 nai_out = id_card.nai;
52 password_out = id_card.password;
54 server_certificate_hash = id_card.trust_anchor.server_cert;
55 ca_certificate = id_card.trust_anchor.ca_cert;
56 subject_name_constraint = id_card.trust_anchor.subject;
57 subject_alt_name_constraint = id_card.trust_anchor.subject_alt;
61 if (password_out == null)
63 if (server_certificate_hash == null)
64 server_certificate_hash = "";
65 if (ca_certificate == null)
67 if (subject_name_constraint == null)
68 subject_name_constraint = "";
69 if (subject_alt_name_constraint == null)
70 subject_alt_name_constraint = "";
78 public async bool get_default_identity (out string nai_out,
79 out string password_out,
80 out string server_certificate_hash,
81 out string ca_certificate,
82 out string subject_name_constraint,
83 out string subject_alt_name_constraint)
85 var request = new IdentityRequest.default (parent_app);
86 request.set_callback ((IdentityRequest) => get_default_identity.callback());
92 server_certificate_hash = "";
94 subject_name_constraint = "";
95 subject_alt_name_constraint = "";
97 if (request.id_card != null)
99 nai_out = request.id_card.nai;
100 password_out = request.id_card.password;
102 server_certificate_hash = request.id_card.trust_anchor.server_cert;
103 ca_certificate = request.id_card.trust_anchor.ca_cert;
104 subject_name_constraint = request.id_card.trust_anchor.subject;
105 subject_alt_name_constraint = request.id_card.trust_anchor.subject_alt;
109 if (password_out == null)
111 if (server_certificate_hash == null)
112 server_certificate_hash = "";
113 if (ca_certificate == null)
115 if (subject_name_constraint == null)
116 subject_name_constraint = "";
117 if (subject_alt_name_constraint == null)
118 subject_alt_name_constraint = "";
126 public bool install_id_card (string display_name,
130 string[] ?rules_patterns,
131 string[] ?rules_always_confirm,
138 IdCard idcard = new IdCard ();
140 idcard.display_name = display_name;
141 idcard.username = user_name;
142 idcard.password = password;
143 idcard.issuer = realm;
144 idcard.services = services;
145 idcard.trust_anchor.ca_cert = ca_cert;
146 idcard.trust_anchor.subject = subject;
147 idcard.trust_anchor.subject_alt = subject_alt;
148 idcard.trust_anchor.server_cert = server_cert;
150 if (rules_patterns.length == rules_always_confirm.length)
152 idcard.rules = new Rule[rules_patterns.length];
154 for (int i=0; i<idcard.rules.length; i++)
156 idcard.rules[i].pattern = rules_patterns[i];
157 idcard.rules[i].always_confirm = rules_always_confirm[i];
161 return parent_app.add_identity (idcard);
165 public int install_from_file (string file_name)
167 var webp = new WebProvisioning.Parser (file_name);
171 int installed_cards = 0;
172 foreach (IdCard card in WebProvisioning.cards)
174 string[] rules_patterns = {};
175 string[] rules_always_confirm = {};
177 if (card.rules.length > 0)
180 rules_patterns = new string[card.rules.length];
181 rules_always_confirm = new string[card.rules.length];
182 foreach (Rule r in card.rules)
184 rules_patterns[i] = r.pattern;
185 rules_always_confirm[i] = r.always_confirm;
190 result = install_id_card (card.display_name,
195 rules_always_confirm,
197 card.trust_anchor.ca_cert,
198 card.trust_anchor.subject,
199 card.trust_anchor.subject_alt,
200 card.trust_anchor.server_cert);
205 return installed_cards;
213 using MoonshotRpcInterface;
215 /* This class must be a singleton, because we use a global RPC
216 * binding handle. I cannot picture a situation where more than
217 * one instance of the same interface would be needed so this
218 * shouldn't be a problem.
220 * Shutdown is automatically done by the RPC runtime when the
223 public class MoonshotServer : Object {
224 private static IdentityManagerView main_window;
226 private static MoonshotServer instance = null;
228 public static void start (Gtk.Window window)
230 main_window = (IdentityManagerView) window;
231 Rpc.server_start (MoonshotRpcInterface.spec, "/org/janet/Moonshot", Rpc.Flags.PER_USER);
234 public static MoonshotServer get_instance ()
236 if (instance == null)
237 instance = new MoonshotServer ();
241 [CCode (cname = "moonshot_get_identity_rpc")]
242 public static void get_identity (Rpc.AsyncCall call,
247 ref string password_out,
248 ref string server_certificate_hash,
249 ref string ca_certificate,
250 ref string subject_name_constraint,
251 ref string subject_alt_name_constraint)
255 var request = new IdentityRequest (main_window,
260 // Pass execution to the main loop and block the RPC thread
261 request.mutex = new Mutex ();
262 request.cond = new Cond ();
263 request.set_callback (return_identity_cb);
265 request.mutex.lock ();
266 Idle.add (request.execute);
268 while (request.complete == false)
269 request.cond.wait (request.mutex);
273 server_certificate_hash = "";
275 subject_name_constraint = "";
276 subject_alt_name_constraint = "";
278 var id_card = request.id_card;
280 if (id_card != null) {
281 // The strings are freed by the RPC runtime
282 nai_out = id_card.nai;
283 password_out = id_card.password;
284 server_certificate_hash = "certificate";
286 return_if_fail (nai_out != null);
287 return_if_fail (password_out != null);
288 return_if_fail (server_certificate_hash != null);
289 return_if_fail (ca_certificate != null);
290 return_if_fail (subject_name_constraint != null);
291 return_if_fail (subject_alt_name_constraint != null);
296 // The outputs must be set before this function is called. For this
297 // reason they are 'ref' not 'out' parameters - Vala assigns to the
298 // 'out' parameters only at the end of the function, which is too
300 call.return (&result);
302 request.cond.signal ();
303 request.mutex.unlock ();
306 [CCode (cname = "moonshot_get_default_identity_rpc")]
307 public static void get_default_identity (Rpc.AsyncCall call,
309 ref string password_out,
310 ref string server_certificate_hash,
311 ref string ca_certificate,
312 ref string subject_name_constraint,
313 ref string subject_alt_name_constraint)
317 var request = new IdentityRequest.default (main_window);
318 request.mutex = new Mutex ();
319 request.cond = new Cond ();
320 request.set_callback (return_identity_cb);
322 request.mutex.lock ();
323 Idle.add (request.execute);
325 while (request.complete == false)
326 request.cond.wait (request.mutex);
330 server_certificate_hash = "";
332 subject_name_constraint = "";
333 subject_alt_name_constraint = "";
335 if (request.id_card != null)
337 nai_out = request.id_card.nai;
338 password_out = request.id_card.password;
339 server_certificate_hash = "certificate";
341 return_if_fail (nai_out != null);
342 return_if_fail (password_out != null);
343 return_if_fail (server_certificate_hash != null);
344 return_if_fail (ca_certificate != null);
345 return_if_fail (subject_name_constraint != null);
346 return_if_fail (subject_alt_name_constraint != null);
355 call.return (&result);
357 request.cond.signal ();
358 request.mutex.unlock ();
361 // Called from the main loop thread when an identity has
363 static void return_identity_cb (IdentityRequest request) {
364 // Notify the RPC thread that the request is complete
365 request.mutex.lock ();
366 request.cond.signal ();
368 // Block the main loop until the RPC call has returned
369 // to avoid any races
370 request.cond.wait (request.mutex);
371 request.mutex.unlock ();
374 [CCode (cname = "moonshot_install_id_card_rpc")]
375 public static bool install_id_card (string display_name,
379 string[] rules_patterns,
380 string[] rules_always_confirm,
387 IdCard idcard = new IdCard ();
388 bool success = false;
389 Mutex mutex = new Mutex();
390 Cond cond = new Cond();
392 idcard.display_name = display_name;
393 idcard.username = user_name;
394 idcard.password = password;
395 idcard.issuer = realm;
396 idcard.services = services;
397 idcard.trust_anchor.ca_cert = ca_cert;
398 idcard.trust_anchor.subject = subject;
399 idcard.trust_anchor.subject_alt = subject_alt;
400 idcard.trust_anchor.server_cert = server_cert;
402 if (rules_patterns.length == rules_always_confirm.length)
404 idcard.rules = new Rule[rules_patterns.length];
406 for (int i=0; i<idcard.rules.length; i++)
408 idcard.rules[i].pattern = rules_patterns[i];
409 idcard.rules[i].always_confirm = rules_always_confirm[i];
415 // Defer addition to the main loop thread.
418 success = main_window.add_identity (idcard);