2 * Copyright (c) 2011-2014, 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
36 [DBus (name = "org.janet.Moonshot")]
37 interface IIdentityManager : GLib.Object {
39 public abstract bool show_ui() throws DBus.Error;
41 public abstract bool show_ui() throws IOError;
46 public class IdentityManagerApp {
47 public IdentityManagerModel model;
48 public IdCard default_id_card;
49 public bool explicitly_launched;
50 public IdentityManagerView view;
51 private MoonshotServer ipc_server;
52 private bool name_is_owned;
53 private bool show_requested;
56 public OSXApplication osxApp;
58 // the signal handler function.
59 // the current instance of our app class is passed in the
60 // id_manager_app_instanceparameter
61 public static bool on_osx_open_files(OSXApplication osx_app_instance,
63 IdentityManagerApp id_manager_app_instance ) {
64 int added_cards = id_manager_app_instance.ipc_server.install_from_file(file_name);
69 private const int WINDOW_WIDTH = 400;
70 private const int WINDOW_HEIGHT = 500;
73 /** If we're successfully registered with DBus, then show the UI. Otherwise, wait until we're registered. */
81 show_requested = true;
85 public IdentityManagerApp(bool headless, bool use_flat_file_store) {
86 use_flat_file_store |= UserForcesFlatFileStore();
89 bool keyring_available = (!use_flat_file_store) && GnomeKeyring.is_available();
91 bool keyring_available = false;
94 IIdentityCardStore.StoreType store_type;
95 if (headless || use_flat_file_store || !keyring_available)
96 store_type = IIdentityCardStore.StoreType.FLAT_FILE;
98 store_type = IIdentityCardStore.StoreType.KEYRING;
100 model = new IdentityManagerModel(this, store_type);
101 /* if headless, but we have nothing in the flat file store
102 * and keyring is available, switch to keyring */
103 if (headless && keyring_available && !use_flat_file_store && !model.HasNonTrivialIdentities())
104 model.set_store_type(IIdentityCardStore.StoreType.KEYRING);
107 view = new IdentityManagerView(this);
108 LinkedList<IdCard> card_list = model.get_card_list();
109 if (card_list.size > 0)
110 this.default_id_card = card_list.last();
115 osxApp = OSXApplication.get_instance();
116 // The 'correct' way of connecting won't work in Mac OS with Vala 0.12; e.g.
117 // osxApp.ns_application_open_file.connect(install_from_file);
118 // so we have to use this old way
119 Signal.connect(osxApp, "NSApplicationOpenFile", (GLib.Callback)(on_osx_open_files), this);
123 public bool add_identity(IdCard id, bool force_flat_file_store) {
124 if (view != null) return view.add_identity(id, force_flat_file_store);
125 model.add_card(id, force_flat_file_store);
129 public void select_identity(IdentityRequest request) {
131 IdCard identity = null;
133 if (request.select_default)
135 identity = default_id_card;
138 if (identity == null)
140 bool has_nai = request.nai != null && request.nai != "";
141 bool has_srv = request.service != null && request.service != "";
142 bool confirm = false;
144 foreach (IdCard id in model.get_card_list())
146 /* If NAI matches, use this id card */
147 if (has_nai && request.nai == id.nai)
153 /* If any service matches we add id card to the candidate list */
156 foreach (string srv in id.services)
158 if (request.service == srv)
160 request.candidates.append(id);
167 /* If more than one candidate we dissasociate service from all ids */
168 if ((identity == null) && has_srv && request.candidates.length() > 1)
170 foreach (IdCard id in request.candidates)
173 SList<string> services_list = null;
174 bool has_service = false;
176 foreach (string srv in id.services)
178 if (srv == request.service)
183 services_list.append(srv);
189 if (services_list.length() == 0)
195 string[] services = new string[services_list.length()];
196 foreach (string srv in services_list)
202 id.services = services;
206 /* If there are no candidates we use the service matching rules */
207 if ((identity == null) && (request.candidates.length() == 0))
209 foreach (IdCard id in model.get_card_list())
211 foreach (Rule rule in id.rules)
213 if (!match_service_pattern(request.service, rule.pattern))
216 request.candidates.append(id);
218 if (rule.always_confirm == "true")
224 if ((identity == null) && has_nai) {
225 // create a temp identity
226 string[] components = request.nai.split("@", 2);
227 identity = new IdCard();
228 identity.display_name = request.nai;
229 identity.username = components[0];
230 if (components.length > 1)
231 identity.issuer = components[1];
232 identity.password = request.password;
233 identity.temporary = true;
235 if (identity == null) {
236 if (request.candidates.length() != 1) {
239 identity = request.candidates.nth_data(0);
243 if (confirm && (view != null))
245 if (!explicitly_launched)
247 view.queue_identity_request(request);
251 // Send back the identity (we can't directly run the
252 // callback because we may be being called from a 'yield')
256 identity = view.check_add_password(identity, request, model);
258 request.return_identity(identity);
259 // The following occasionally causes the app to exit without sending the dbus
260 // reply, so for now we just don't exit
261 // if (!explicitly_launched)
262 // Idle.add(() => { Gtk.main_quit(); return false; } );
269 private bool match_service_pattern(string service, string pattern)
271 var pspec = new PatternSpec(pattern);
272 return pspec.match_string(service);
276 private void init_ipc_server() {
277 // Errors will currently be sent via g_log - ie. to an
278 // obtrusive message box, on Windows
280 this.ipc_server = MoonshotServer.get_instance();
281 MoonshotServer.start(this);
284 private void init_ipc_server() {
286 var conn = DBus.Bus.get(DBus.BusType.SESSION);
287 dynamic DBus.Object bus = conn.get_object("org.freedesktop.DBus",
288 "/org/freedesktop/DBus",
289 "org.freedesktop.DBus");
291 // try to register service in session bus
292 uint reply = bus.request_name("org.janet.Moonshot", (uint) 0);
293 if (reply == DBus.RequestNameReply.PRIMARY_OWNER)
295 this.ipc_server = new MoonshotServer(this);
296 conn.register_object("/org/janet/moonshot", ipc_server);
300 DBus.Object manager_proxy = conn.get_object("org.janet.Moonshot",
301 "/org/janet/moonshot",
302 "org.janet.Moonshot");
303 if (manager_proxy != null)
304 manager_proxy.call("ShowUi", out e, GLib.Type.INVALID, typeof(bool), out shown, GLib.Type.INVALID);
307 GLib.error("Couldn't own name org.janet.Moonshot on dbus or show previously launched identity manager.");
309 stdout.printf("Showed previously launched identity manager.\n");
310 GLib.Process.exit(0);
316 stderr.printf("%s\n", e.message);
320 private void bus_acquired_cb(DBusConnection conn) {
322 conn.register_object("/org/janet/moonshot", ipc_server);
326 stderr.printf("%s\n", e.message);
330 private void init_ipc_server() {
331 this.ipc_server = new MoonshotServer(this);
333 GLib.Bus.own_name(GLib.BusType.SESSION,
334 "org.janet.Moonshot",
335 GLib.BusNameOwnerFlags.NONE,
338 // Name acquired callback:
341 name_is_owned = true;
343 // Now that we know that we own the name, it's safe to show the UI.
344 if (show_requested) {
346 show_requested = false;
351 // Name lost callback:
354 // This callback usually means that another moonshot is already running.
355 // But it *might* mean that we lost the name for some other reason
356 // (though it's unclear to me yet what those reasons are.)
357 // Clearing these flags seems like a good idea for that case. -- dbreslau
358 name_is_owned = false;
359 show_requested = false;
363 IIdentityManager manager = Bus.get_proxy_sync(BusType.SESSION, name, "/org/janet/moonshot");
364 shown = manager.show_ui();
366 } catch (IOError e) {
369 GLib.error("Couldn't own name %s on dbus or show previously launched identity manager.", name);
371 stdout.printf("Showed previously launched identity manager.\n");
372 GLib.Process.exit(0);
379 static bool explicitly_launched = true;
380 static bool use_flat_file_store = false;
381 const GLib.OptionEntry[] options = {
382 {"dbus-launched", 0, GLib.OptionFlags.REVERSE, GLib.OptionArg.NONE,
383 ref explicitly_launched, "launch for dbus rpc use", null},
384 {"flat-file-store", 0, 0, GLib.OptionArg.NONE,
385 ref use_flat_file_store, "force use of flat file identity store (used by default only for headless operation)", null},
390 public static int main(string[] args) {
392 bool headless = false;
394 bool headless = (GLib.Environment.get_variable("DISPLAY") == null);
399 var opt_context = new OptionContext(null);
400 opt_context.set_help_enabled(true);
401 opt_context.add_main_entries(options, null);
402 opt_context.parse(ref args);
403 } catch (OptionError e) {
404 stdout.printf(_("error: %s\n"),e.message);
405 stdout.printf(_("Run '%s --help' to see a full list of available options\n"), args[0]);
408 explicitly_launched = false;
411 if (!Gtk.init_with_args(ref args, _(""), options, null)) {
412 stdout.printf(_("unable to initialize window\n"));
415 } catch (GLib.Error e) {
416 stdout.printf(_("error: %s\n"),e.message);
417 stdout.printf(_("Run '%s --help' to see a full list of available options\n"), args[0]);
420 gtk_available = true;
424 // Force specific theme settings on Windows without requiring a gtkrc file
425 Gtk.Settings settings = Gtk.Settings.get_default();
426 settings.set_string_property("gtk-theme-name", "ms-windows", "moonshot");
427 settings.set_long_property("gtk-menu-images", 0, "moonshot");
430 Intl.bindtextdomain(Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
431 Intl.bind_textdomain_codeset(Config.GETTEXT_PACKAGE, "UTF-8");
432 Intl.textdomain(Config.GETTEXT_PACKAGE);
434 var app = new IdentityManagerApp(headless, use_flat_file_store);
435 app.explicitly_launched = explicitly_launched;
437 if (app.explicitly_launched) {
443 MainLoop loop = new MainLoop();