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;
54 public OSXApplication osxApp;
56 // the signal handler function.
57 // the current instance of our app class is passed in the
58 // id_manager_app_instanceparameter
59 public static bool on_osx_open_files (OSXApplication osx_app_instance,
61 IdentityManagerApp id_manager_app_instance ) {
62 int added_cards = id_manager_app_instance.ipc_server.install_from_file(file_name);
67 private const int WINDOW_WIDTH = 400;
68 private const int WINDOW_HEIGHT = 500;
70 if (view != null) view.show();
73 public IdentityManagerApp (bool headless, bool use_flat_file_store) {
74 use_flat_file_store |= UserForcesFlatFileStore();
76 bool keyring_available = (!use_flat_file_store) && GnomeKeyring.is_available();
78 bool keyring_available = false;
80 IIdentityCardStore.StoreType store_type;
81 if (headless || use_flat_file_store || !keyring_available)
82 store_type = IIdentityCardStore.StoreType.FLAT_FILE;
84 store_type = IIdentityCardStore.StoreType.KEYRING;
86 model = new IdentityManagerModel(this, store_type);
87 /* if headless, but we have nothing in the flat file store
88 * and keyring is available, switch to keyring */
89 if (headless && keyring_available && !use_flat_file_store && !model.HasNonTrivialIdentities())
90 model.set_store_type(IIdentityCardStore.StoreType.KEYRING);
93 view = new IdentityManagerView(this);
94 LinkedList<IdCard> card_list = model.get_card_list() ;
95 if (card_list.size > 0)
96 this.default_id_card = card_list.last();
102 osxApp = OSXApplication.get_instance();
103 // The 'correct' way of connrcting wont work in Mac OS with Vala 0.12 e.g.
104 // osxApp.ns_application_open_file.connect(install_from_file);
105 // so we have to use this old way
106 Signal.connect(osxApp, "NSApplicationOpenFile", (GLib.Callback)(on_osx_open_files), this);
111 public bool add_identity (IdCard id, bool force_flat_file_store) {
112 if (view != null) return view.add_identity(id, force_flat_file_store);
113 model.add_card(id, force_flat_file_store);
117 public void select_identity (IdentityRequest request) {
118 IdCard identity = null;
120 if (request.select_default)
122 identity = default_id_card;
125 if (identity == null)
127 bool has_nai = request.nai != null && request.nai != "";
128 bool has_srv = request.service != null && request.service != "";
129 bool confirm = false;
131 foreach (IdCard id in model.get_card_list())
133 /* If NAI matches, use this id card */
134 if (has_nai && request.nai == id.nai)
140 /* If any service matches we add id card to the candidate list */
143 foreach (string srv in id.services)
145 if (request.service == srv)
147 request.candidates.append (id);
154 /* If more than one candidate we dissasociate service from all ids */
155 if ((identity == null) && has_srv && request.candidates.length() > 1)
157 foreach (IdCard id in request.candidates)
160 SList<string> services_list = null;
161 bool has_service = false;
163 foreach (string srv in id.services)
165 if (srv == request.service)
170 services_list.append (srv);
176 if (services_list.length () == 0)
182 string[] services = new string[services_list.length ()];
183 foreach (string srv in services_list)
189 id.services = services;
193 /* If there are no candidates we use the service matching rules */
194 if ((identity==null) && (request.candidates.length () == 0))
196 foreach (IdCard id in model.get_card_list())
198 foreach (Rule rule in id.rules)
200 if (!match_service_pattern (request.service, rule.pattern))
203 request.candidates.append (id);
205 if (rule.always_confirm == "true")
211 if ((identity == null) && has_nai) {
212 // create a temp identity
213 string[] components = request.nai.split("@", 2);
214 identity = new IdCard();
215 identity.display_name = request.nai;
216 identity.username = components[0];
217 if (components.length > 1)
218 identity.issuer = components[1];
219 identity.password = request.password;
220 identity.temporary = true;
222 if (identity == null) {
223 if (request.candidates.length () != 1) {
226 identity = request.candidates.nth_data (0);
230 if (confirm && (view != null))
232 if (!explicitly_launched)
234 view.queue_identity_request(request);
238 // Send back the identity (we can't directly run the
239 // callback because we may be being called from a 'yield')
243 identity = view.check_add_password(identity, request, model);
245 request.return_identity (identity);
246 // The following occasionally causes the app to exit without sending the dbus
247 // reply, so for now we just don't exit
248 // if (!explicitly_launched)
249 // Idle.add( () => { Gtk.main_quit(); return false; } );
256 private bool match_service_pattern (string service, string pattern)
258 var pspec = new PatternSpec (pattern);
259 return pspec.match_string (service);
263 private void init_ipc_server ()
265 // Errors will currently be sent via g_log - ie. to an
266 // obtrusive message box, on Windows
268 this.ipc_server = MoonshotServer.get_instance ();
269 MoonshotServer.start (this);
272 private void init_ipc_server ()
275 var conn = DBus.Bus.get (DBus.BusType.SESSION);
276 dynamic DBus.Object bus = conn.get_object ("org.freedesktop.DBus",
277 "/org/freedesktop/DBus",
278 "org.freedesktop.DBus");
280 // try to register service in session bus
281 uint reply = bus.request_name ("org.janet.Moonshot", (uint) 0);
282 if (reply == DBus.RequestNameReply.PRIMARY_OWNER)
284 this.ipc_server = new MoonshotServer (this);
285 conn.register_object ("/org/janet/moonshot", ipc_server);
289 DBus.Object manager_proxy = conn.get_object ("org.janet.Moonshot",
290 "/org/janet/moonshot",
291 "org.janet.Moonshot");
292 if (manager_proxy != null)
293 manager_proxy.call("ShowUi", out e, GLib.Type.INVALID, typeof(bool), out shown, GLib.Type.INVALID);
296 GLib.error ("Couldn't own name org.janet.Moonshot on dbus or show previously launched identity manager.");
298 stdout.printf("Showed previously launched identity manager.\n");
299 GLib.Process.exit(0);
305 stderr.printf ("%s\n", e.message);
309 private void bus_acquired_cb (DBusConnection conn)
312 conn.register_object ("/org/janet/moonshot", ipc_server);
316 stderr.printf ("%s\n", e.message);
320 private void init_ipc_server ()
322 this.ipc_server = new MoonshotServer (this);
323 GLib.Bus.own_name (GLib.BusType.SESSION,
324 "org.janet.Moonshot",
325 GLib.BusNameOwnerFlags.NONE,
331 IIdentityManager manager = Bus.get_proxy_sync (BusType.SESSION, name, "/org/janet/moonshot");
332 shown = manager.show_ui();
333 } catch (IOError e) {
336 GLib.error ("Couldn't own name %s on dbus or show previously launched identity manager.", name);
338 stdout.printf("Showed previously launched identity manager.\n");
339 GLib.Process.exit(0);
346 static bool explicitly_launched = true;
347 static bool use_flat_file_store = false;
348 static bool attach_console = false;
349 const GLib.OptionEntry[] options = {
350 {"attach-console",0,0,GLib.OptionArg.NONE,
351 ref attach_console,"attach console",null},
352 {"dbus-launched",0,GLib.OptionFlags.REVERSE,GLib.OptionArg.NONE,
353 ref explicitly_launched,"launch for dbus rpc use",null},
354 {"flat-file-store",0,0,GLib.OptionArg.NONE,
355 ref use_flat_file_store,"force use of flat file identity store (used by default only for headless operation)",null},
359 public extern int moonshot_attach_console();
361 public static int main(string[] args){
363 bool headless = false;
365 bool headless = GLib.Environment.get_variable("DISPLAY") == null;
370 var opt_context = new OptionContext(null);
371 opt_context.set_help_enabled (true);
372 opt_context.add_main_entries (options, null);
373 opt_context.parse(ref args);
374 } catch (OptionError e) {
375 stdout.printf(_("error: %s\n"),e.message);
376 stdout.printf(_("Run '%s --help' to see a full list of available options\n"), args[0]);
379 explicitly_launched = false;
382 if (!Gtk.init_with_args(ref args, _(""), options, null)) {
383 stdout.printf(_("unable to initialize window\n"));
386 } catch (GLib.Error e) {
387 stdout.printf(_("error: %s\n"),e.message);
388 stdout.printf(_("Run '%s --help' to see a full list of available options\n"), args[0]);
391 gtk_available = true;
395 // Force specific theme settings on Windows without requiring a gtkrc file
396 Gtk.Settings settings = Gtk.Settings.get_default ();
397 settings.set_string_property ("gtk-theme-name", "ms-windows", "moonshot");
398 settings.set_long_property ("gtk-menu-images", 0, "moonshot");
399 if (attach_console) {
400 if (moonshot_attach_console() != 0) {
401 stdout.printf(_("Attached console\n"));
406 stdout.printf("Standard output\n");
411 Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
412 Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
413 Intl.textdomain (Config.GETTEXT_PACKAGE);
416 var app = new IdentityManagerApp(headless, use_flat_file_store);
417 app.explicitly_launched = explicitly_launched;
419 if (app.explicitly_launched) {
425 MainLoop loop = new MainLoop();