3 [DBus (name = "org.janet.Moonshot")]
4 public class MoonshotServer : Object {
6 private IdentityManagerView main_window;
8 public MoonshotServer (Gtk.Window window)
10 this.main_window = (IdentityManagerView) window;
13 public async bool get_identity (string nai,
17 out string password_out,
18 out string server_certificate_hash,
19 out string ca_certificate,
20 out string subject_name_constraint,
21 out string subject_alt_name_constraint)
23 var request = new IdentityRequest (main_window,
27 request.set_callback ((IdentityRequest) => get_identity.callback());
33 server_certificate_hash = "";
35 subject_name_constraint = "";
36 subject_alt_name_constraint = "";
38 var id_card = request.id_card;
40 if (id_card != null) {
41 nai_out = id_card.nai;
42 password_out = id_card.password;
44 server_certificate_hash = id_card.trust_anchor.server_cert;
45 ca_certificate = id_card.trust_anchor.ca_cert;
46 subject_name_constraint = id_card.trust_anchor.subject;
47 subject_alt_name_constraint = id_card.trust_anchor.subject_alt;
51 if (password_out == null)
53 if (server_certificate_hash == null)
54 server_certificate_hash = "";
55 if (ca_certificate == null)
57 if (subject_name_constraint == null)
58 subject_name_constraint = "";
59 if (subject_alt_name_constraint == null)
60 subject_alt_name_constraint = "";
68 public async bool get_default_identity (out string nai_out,
69 out string password_out,
70 out string server_certificate_hash,
71 out string ca_certificate,
72 out string subject_name_constraint,
73 out string subject_alt_name_constraint)
75 var request = new IdentityRequest.default (main_window);
76 request.set_callback ((IdentityRequest) => get_default_identity.callback());
82 server_certificate_hash = "";
84 subject_name_constraint = "";
85 subject_alt_name_constraint = "";
87 if (request.id_card != null)
89 nai_out = request.id_card.nai;
90 password_out = request.id_card.password;
92 server_certificate_hash = request.id_card.trust_anchor.server_cert;
93 ca_certificate = request.id_card.trust_anchor.ca_cert;
94 subject_name_constraint = request.id_card.trust_anchor.subject;
95 subject_alt_name_constraint = request.id_card.trust_anchor.subject_alt;
99 if (password_out == null)
101 if (server_certificate_hash == null)
102 server_certificate_hash = "";
103 if (ca_certificate == null)
105 if (subject_name_constraint == null)
106 subject_name_constraint = "";
107 if (subject_alt_name_constraint == null)
108 subject_alt_name_constraint = "";
116 public bool install_id_card (string display_name,
120 string[] rules_patterns,
121 string[] rules_always_confirm,
128 IdCard idcard = new IdCard ();
130 idcard.display_name = display_name;
131 idcard.username = user_name;
132 idcard.password = password;
133 idcard.issuer = realm;
134 idcard.services = services;
135 idcard.trust_anchor.ca_cert = ca_cert;
136 idcard.trust_anchor.subject = subject;
137 idcard.trust_anchor.subject_alt = subject_alt;
138 idcard.trust_anchor.server_cert = server_cert;
140 if (rules_patterns.length == rules_always_confirm.length)
142 idcard.rules = new Rule[rules_patterns.length];
144 for (int i=0; i<idcard.rules.length; i++)
146 idcard.rules[i].pattern = rules_patterns[i];
147 idcard.rules[i].always_confirm = rules_always_confirm[i];
151 return this.main_window.add_identity (idcard);
158 using MoonshotRpcInterface;
160 /* This class must be a singleton, because we use a global RPC
161 * binding handle. I cannot picture a situation where more than
162 * one instance of the same interface would be needed so this
163 * shouldn't be a problem.
165 * Shutdown is automatically done by the RPC runtime when the
168 public class MoonshotServer : Object {
169 private static IdentityManagerView main_window;
171 private static MoonshotServer instance = null;
173 public static void start (Gtk.Window window)
175 main_window = (IdentityManagerView) window;
176 Rpc.server_start (MoonshotRpcInterface.spec, "/org/janet/Moonshot", Rpc.Flags.PER_USER);
179 public static MoonshotServer get_instance ()
181 if (instance == null)
182 instance = new MoonshotServer ();
186 [CCode (cname = "moonshot_get_identity_rpc")]
187 public static void get_identity (Rpc.AsyncCall call,
192 ref string password_out,
193 ref string server_certificate_hash,
194 ref string ca_certificate,
195 ref string subject_name_constraint,
196 ref string subject_alt_name_constraint)
200 var request = new IdentityRequest (main_window,
205 // Pass execution to the main loop and block the RPC thread
206 request.mutex = new Mutex ();
207 request.cond = new Cond ();
208 request.set_callback (return_identity_cb);
210 request.mutex.lock ();
211 Idle.add (request.execute);
213 while (request.complete == false)
214 request.cond.wait (request.mutex);
218 server_certificate_hash = "";
220 subject_name_constraint = "";
221 subject_alt_name_constraint = "";
223 var id_card = request.id_card;
225 if (id_card == null) {
226 // The strings are freed by the RPC runtime
227 nai_out = id_card.nai;
228 password_out = id_card.password;
229 server_certificate_hash = "certificate";
231 return_if_fail (nai_out != null);
232 return_if_fail (password_out != null);
233 return_if_fail (server_certificate_hash != null);
234 return_if_fail (ca_certificate != null);
235 return_if_fail (subject_name_constraint != null);
236 return_if_fail (subject_alt_name_constraint != null);
241 // The outputs must be set before this function is called. For this
242 // reason they are 'ref' not 'out' parameters - Vala assigns to the
243 // 'out' parameters only at the end of the function, which is too
245 call.return (&result);
247 request.cond.signal ();
248 request.mutex.unlock ();
251 [CCode (cname = "moonshot_get_default_identity_rpc")]
252 public static void get_default_identity (Rpc.AsyncCall call,
254 ref string password_out,
255 ref string server_certificate_hash,
256 ref string ca_certificate,
257 ref string subject_name_constraint,
258 ref string subject_alt_name_constraint)
262 var request = new IdentityRequest.default (main_window);
263 request.mutex = new Mutex ();
264 request.cond = new Cond ();
265 request.set_callback (return_identity_cb);
267 request.mutex.lock ();
268 Idle.add (request.execute);
270 while (request.complete == false)
271 request.cond.wait (request.mutex);
275 server_certificate_hash = "";
277 subject_name_constraint = "";
278 subject_alt_name_constraint = "";
280 if (request.id_card != null)
282 nai_out = request.id_card.nai;
283 password_out = request.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);
300 call.return (&result);
302 request.cond.signal ();
303 request.mutex.unlock ();
306 // Called from the main loop thread when an identity has
308 static void return_identity_cb (IdentityRequest request) {
309 // Notify the RPC thread that the request is complete
310 request.mutex.lock ();
311 request.cond.signal ();
313 // Block the main loop until the RPC call has returned
314 // to avoid any races
315 request.cond.wait (request.mutex);
316 request.mutex.unlock ();
319 [CCode (cname = "moonshot_install_id_card_rpc")]
320 public static bool install_id_card (string display_name,
324 string[] rules_patterns,
325 string[] rules_always_confirm,
332 IdCard idcard = new IdCard ();
333 bool success = false;
334 Mutex mutex = new Mutex();
335 Cond cond = new Cond();
337 idcard.display_name = display_name;
338 idcard.username = user_name;
339 idcard.password = password;
340 idcard.issuer = realm;
341 idcard.services = services;
342 idcard.trust_anchor.ca_cert = ca_cert;
343 idcard.trust_anchor.subject = subject;
344 idcard.trust_anchor.subject_alt = subject_alt;
345 idcard.trust_anchor.server_cert = server_cert;
347 if (rules_patterns.length == rules_always_confirm.length)
349 idcard.rules = new Rule[rules_patterns.length];
351 for (int i=0; i<idcard.rules.length; i++)
353 idcard.rules[i].pattern = rules_patterns[i];
354 idcard.rules[i].always_confirm = rules_always_confirm[i];
360 // Defer addition to the main loop thread.
363 success = main_window.add_identity (idcard);