]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/seat.c
Add back the greeter guest account hint
[sojka/lightdm.git] / src / seat.c
1 /*
2  * Copyright (C) 2010-2011 Robert Ancell.
3  * Author: Robert Ancell <robert.ancell@canonical.com>
4  * 
5  * This program is free software: you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License as published by the Free Software
7  * Foundation, either version 3 of the License, or (at your option) any later
8  * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
9  * license.
10  */
11
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/wait.h>
15
16 #include "seat.h"
17 #include "configuration.h"
18 #include "guest-account.h"
19 #include "greeter.h"
20
21 enum {
22     SESSION_ADDED,
23     RUNNING_USER_SESSION,
24     SESSION_REMOVED,
25     STOPPED,
26     LAST_SIGNAL
27 };
28 static guint signals[LAST_SIGNAL] = { 0 };
29
30 struct SeatPrivate
31 {
32     /* Configuration for this seat */
33     GHashTable *properties;
34
35     /* TRUE if able to switch users */
36     gboolean can_switch;
37
38     /* TRUE if display server can be shared for sessions */
39     gboolean share_display_server;
40
41     /* The display servers on this seat */
42     GList *display_servers;
43
44     /* The sessions on this seat */
45     GList *sessions;
46
47     /* The session to set active when it starts */
48     Session *session_to_activate;
49
50     /* TRUE if stopping this seat (waiting for displays to stop) */
51     gboolean stopping;
52
53     /* TRUE if stopped */
54     gboolean stopped;
55 };
56
57 /* PAM services to use */
58 #define GREETER_SERVICE   "lightdm-greeter"
59 #define USER_SERVICE      "lightdm"
60 #define AUTOLOGIN_SERVICE "lightdm-autologin"
61
62 G_DEFINE_TYPE (Seat, seat, G_TYPE_OBJECT);
63
64 typedef struct
65 {
66     const gchar *name;
67     GType type;
68 } SeatModule;
69 static GHashTable *seat_modules = NULL;
70
71 // FIXME: Make a get_display_server() that re-uses display servers if supported
72 static DisplayServer *create_display_server (Seat *seat);
73 static Greeter *create_greeter_session (Seat *seat);
74 static void start_session (Seat *seat, Session *session);
75
76 void
77 seat_register_module (const gchar *name, GType type)
78 {
79     SeatModule *module;
80
81     if (!seat_modules)
82         seat_modules = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
83
84     g_debug ("Registered seat module %s", name);
85
86     module = g_malloc0 (sizeof (SeatModule));
87     module->name = g_strdup (name);
88     module->type = type;
89     g_hash_table_insert (seat_modules, g_strdup (name), module);
90 }
91
92 Seat *
93 seat_new (const gchar *module_name)
94 {
95     Seat *seat;
96     SeatModule *m = NULL;
97   
98     g_return_val_if_fail (module_name != NULL, NULL);
99
100     if (seat_modules)
101         m = g_hash_table_lookup (seat_modules, module_name);
102     if (!m)
103         return NULL;
104
105     seat = g_object_new (m->type, NULL);
106
107     return seat;
108 }
109
110 void
111 seat_set_property (Seat *seat, const gchar *name, const gchar *value)
112 {
113     g_return_if_fail (seat != NULL);
114     g_hash_table_insert (seat->priv->properties, g_strdup (name), g_strdup (value));
115 }
116
117 const gchar *
118 seat_get_string_property (Seat *seat, const gchar *name)
119 {
120     g_return_val_if_fail (seat != NULL, NULL);
121     return g_hash_table_lookup (seat->priv->properties, name);
122 }
123
124 gboolean
125 seat_get_boolean_property (Seat *seat, const gchar *name)
126 {
127     return g_strcmp0 (seat_get_string_property (seat, name), "true") == 0;
128 }
129
130 gint
131 seat_get_integer_property (Seat *seat, const gchar *name)
132 {
133     const gchar *value;
134
135     value = seat_get_string_property (seat, name);
136     return value ? atoi (value) : 0;
137 }
138
139 void
140 seat_set_can_switch (Seat *seat, gboolean can_switch)
141 {
142     g_return_if_fail (seat != NULL);
143
144     seat->priv->can_switch = can_switch;
145 }
146
147 void
148 seat_set_share_display_server (Seat *seat, gboolean share_display_server)
149 {
150     g_return_if_fail (seat != NULL);
151
152     seat->priv->share_display_server = share_display_server;
153 }
154
155 gboolean
156 seat_start (Seat *seat)
157 {
158     g_return_val_if_fail (seat != NULL, FALSE);
159   
160     SEAT_GET_CLASS (seat)->setup (seat);
161     return SEAT_GET_CLASS (seat)->start (seat);
162 }
163
164 GList *
165 seat_get_sessions (Seat *seat)
166 {
167     g_return_val_if_fail (seat != NULL, NULL);
168     return seat->priv->sessions;
169 }
170
171 void
172 seat_set_active_session (Seat *seat, Session *session)
173 {
174     GList *link;
175
176     g_return_if_fail (seat != NULL);
177
178     SEAT_GET_CLASS (seat)->set_active_session (seat, session);
179
180     /* Stop any greeters */
181     for (link = seat->priv->sessions; link; link = link->next)
182     {
183         Session *s = link->data;
184
185         if (s == session || session_get_is_stopping (s))
186             continue;
187
188         if (IS_GREETER (s))
189         {
190             g_debug ("Stopping greeter");
191             session_stop (s);
192         }
193     }
194 }
195
196 Session *
197 seat_get_active_session (Seat *seat)
198 {
199     g_return_val_if_fail (seat != NULL, NULL);
200     return SEAT_GET_CLASS (seat)->get_active_session (seat);
201 }
202
203 gboolean
204 seat_get_can_switch (Seat *seat)
205 {
206     g_return_val_if_fail (seat != NULL, FALSE);
207     return seat->priv->can_switch;
208 }
209
210 gboolean
211 seat_get_allow_guest (Seat *seat)
212 {
213     g_return_val_if_fail (seat != NULL, FALSE);
214     return seat_get_boolean_property (seat, "allow-guest") && guest_account_is_installed ();
215 }
216
217 static gboolean
218 run_script (Seat *seat, DisplayServer *display_server, const gchar *script_name, User *user)
219 {
220     Process *script;
221     gboolean result = FALSE;
222   
223     script = process_new ();
224
225     process_set_command (script, script_name);
226
227     /* Set POSIX variables */
228     process_set_clear_environment (script, TRUE);
229     process_set_env (script, "SHELL", "/bin/sh");
230
231     /* Variables required for regression tests */
232     if (g_getenv ("LIGHTDM_TEST_ROOT"))
233     {
234         process_set_env (script, "LIGHTDM_TEST_ROOT", g_getenv ("LIGHTDM_TEST_ROOT"));
235         process_set_env (script, "LD_PRELOAD", g_getenv ("LD_PRELOAD"));
236         process_set_env (script, "LD_LIBRARY_PATH", g_getenv ("LD_LIBRARY_PATH"));
237         process_set_env (script, "PATH", g_getenv ("PATH"));
238     }
239     else
240         process_set_env (script, "PATH", "/usr/local/bin:/usr/bin:/bin");
241
242     if (user)
243     {
244         process_set_env (script, "USER", user_get_name (user));
245         process_set_env (script, "LOGNAME", user_get_name (user));
246         process_set_env (script, "HOME", user_get_home_directory (user));
247     }
248     else
249         process_set_env (script, "HOME", "/");
250
251     SEAT_GET_CLASS (seat)->run_script (seat, display_server, script);
252
253     if (process_start (script, TRUE))
254     {
255         int exit_status;
256
257         exit_status = process_get_exit_status (script);
258         if (WIFEXITED (exit_status))
259         {
260             g_debug ("Exit status of %s: %d", script_name, WEXITSTATUS (exit_status));
261             result = WEXITSTATUS (exit_status) == EXIT_SUCCESS;
262         }
263     }
264
265     g_object_unref (script);
266
267     return result;
268 }
269
270 static void
271 seat_real_run_script (Seat *seat, DisplayServer *display_server, Process *process)
272 {  
273 }
274
275 static void
276 emit_upstart_signal (const gchar *signal)
277 {
278     g_return_if_fail (signal != NULL);
279     g_return_if_fail (signal[0] != 0);
280
281     if (getuid () != 0)
282         return;
283
284     gchar *cmd = g_strdup_printf ("initctl -q emit %s DISPLAY_MANAGER=lightdm", signal);
285     g_spawn_command_line_async (cmd, NULL); /* OK if it fails, probably not installed */
286     g_free (cmd);
287 }
288
289 static void
290 check_stopped (Seat *seat)
291 {
292     if (seat->priv->stopping &&
293         !seat->priv->stopped &&
294         g_list_length (seat->priv->display_servers) == 0 &&
295         g_list_length (seat->priv->sessions) == 0)
296     {
297         seat->priv->stopped = TRUE;
298         g_debug ("Seat stopped");
299         g_signal_emit (seat, signals[STOPPED], 0);
300     }
301 }
302
303 static void
304 display_server_stopped_cb (DisplayServer *display_server, Seat *seat)
305 {
306     GList *list, *link;
307     Session *active_session;
308
309     g_debug ("Display server stopped");
310
311     g_signal_handlers_disconnect_matched (display_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
312     seat->priv->display_servers = g_list_remove (seat->priv->display_servers, display_server);
313
314     if (seat->priv->stopping)
315     {
316         check_stopped (seat);
317         g_object_unref (display_server);
318         return;
319     }
320
321     /* Stop all sessions on this display server */
322     list = g_list_copy (seat->priv->sessions);
323     for (link = list; link; link = link->next)
324         g_object_ref (link->data);
325     for (link = list; link; link = link->next)
326     {
327         Session *session = link->data;
328
329         if (session_get_display_server (session) != display_server || session_get_is_stopping (session))
330             continue;
331
332         /* Stop seat if this is the only display server and it failed to start a greeter */
333         if (IS_GREETER (session) &&
334             !session_get_is_started (session) &&
335             g_list_length (seat->priv->display_servers) == 0)
336         {
337             g_debug ("Stopping seat, greeter display server failed to start");
338             seat_stop (seat);
339         }
340
341         g_debug ("Stopping session");
342         session_stop (session);
343     }
344     g_list_free_full (list, g_object_unref);
345
346     if (!seat->priv->stopping && display_server_get_start_local_sessions (display_server))
347     {
348         /* If we were the active session, switch to a greeter */
349         active_session = seat_get_active_session (seat);
350         if (!active_session || session_get_display_server (active_session) == display_server)
351         {
352             g_debug ("Active display server stopped, starting greeter");
353             seat_switch_to_greeter (seat);
354         }
355     }
356
357     g_object_unref (display_server);
358 }
359
360 static void
361 switch_to_greeter_from_failed_session (Seat *seat, Session *session)
362 {
363     Greeter *greeter_session;
364     DisplayServer *display_server;
365
366     greeter_session = create_greeter_session (seat);
367     if (session_get_is_guest (session))
368         greeter_set_hint (greeter_session, "select-guest", "true");
369     else
370         greeter_set_hint (greeter_session, "select-user", session_get_username (session));
371     if (seat->priv->session_to_activate)
372         g_object_unref (seat->priv->session_to_activate);
373     seat->priv->session_to_activate = g_object_ref (greeter_session);
374
375     if (seat->priv->share_display_server)
376         session_set_display_server (SESSION (greeter_session), session_get_display_server (session));
377     else
378     {
379         DisplayServer *display_server;
380
381         display_server = create_display_server (seat);
382         if (!display_server_start (display_server))
383         {
384             g_debug ("Failed to start display server for greeter");
385             seat_stop (seat);
386         }
387
388         session_set_display_server (session, display_server);
389     }
390
391     start_session (seat, SESSION (greeter_session));
392
393     /* Stop failed session */
394     session_stop (session);
395 }
396
397 static void
398 start_session (Seat *seat, Session *session)
399 {
400     Greeter *greeter_session;
401
402     if (session_start (session))
403         return;
404
405     if (IS_GREETER (session))
406     {
407         g_debug ("Failed to start greeter");
408         display_server_stop (session_get_display_server (session));
409         return;
410     }
411
412     g_debug ("Failed to start session, starting greeter");
413     switch_to_greeter_from_failed_session (seat, session);
414 }
415
416 static void
417 run_session (Seat *seat, Session *session)
418 {
419     const gchar *script;
420
421     if (IS_GREETER (session))
422         script = seat_get_string_property (seat, "greeter-setup-script");
423     else
424         script = seat_get_string_property (seat, "session-setup-script");
425     if (script && !run_script (seat, session_get_display_server (session), script, NULL))
426     {
427         g_debug ("Switching to greeter due to failed setup script");
428         switch_to_greeter_from_failed_session (seat, session);
429         return;
430     }
431
432     if (!IS_GREETER (session))
433         g_signal_emit (seat, signals[RUNNING_USER_SESSION], 0, session);
434
435     session_run (session);
436
437     // FIXME: Wait until the session is ready
438
439     if (session == seat->priv->session_to_activate)
440     {
441         seat_set_active_session (seat, session);
442         g_object_unref (seat->priv->session_to_activate);
443         seat->priv->session_to_activate = NULL;
444     }
445 }
446
447 static void
448 session_authentication_complete_cb (Session *session, Seat *seat)
449 {
450     if (session_get_is_authenticated (session))
451     {
452         g_debug ("Session authenticated, running command");
453         run_session (seat, session);
454     }
455     else if (!IS_GREETER (session))
456     {
457         g_debug ("Switching to greeter due to failed authentication");
458         switch_to_greeter_from_failed_session (seat, session);
459     }
460     else
461     {
462         g_debug ("Stopping session that failed authentication");
463         session_stop (session);
464     }
465 }
466
467 static void
468 session_stopped_cb (Session *session, Seat *seat)
469 {
470     DisplayServer *display_server;
471
472     g_debug ("Session stopped");
473
474     display_server = session_get_display_server (session);
475
476     /* Cleanup */
477     if (!IS_GREETER (session))
478     {
479         const gchar *script;
480         script = seat_get_string_property (seat, "session-cleanup-script");
481         if (script)
482             run_script (seat, display_server, script, session_get_user (session));
483     }
484
485     g_signal_handlers_disconnect_matched (session, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
486     seat->priv->sessions = g_list_remove (seat->priv->sessions, session);
487
488     /* We were waiting for this session, but it didn't start :( */
489     // FIXME: Start a greeter on this?
490     if (session == seat->priv->session_to_activate)
491     {
492         g_object_unref (seat->priv->session_to_activate);
493         seat->priv->session_to_activate = NULL;
494     }
495
496     if (seat->priv->stopping)
497     {
498         check_stopped (seat);
499         g_object_unref (session);
500         return;
501     }
502
503     /* If this is the greeter session then re-use this display server */
504     if (IS_GREETER (session) && seat->priv->share_display_server &&
505         greeter_get_start_session (GREETER (session)))
506     {
507         GList *link;
508
509         for (link = seat->priv->sessions; link; link = link->next)
510         {
511             Session *s = link->data;
512
513             /* Skip this session and sessions on other display servers */
514             if (s == session || session_get_display_server (s) != display_server || session_get_is_stopping (s))
515                 continue;
516
517             if (session_get_is_authenticated (s))
518             {
519                 g_debug ("Greeter stopped, running session");
520                 run_session (seat, s);
521             }
522             else
523             {
524                 g_debug ("Greeter stopped, starting session authentication");
525                 start_session (seat, s);
526             }
527             break;
528         }
529     }
530     /* If this is the greeter and nothing else is running then stop the seat */
531     else if (IS_GREETER (session) &&
532         !greeter_get_start_session (GREETER (session)) &&
533         g_list_length (seat->priv->display_servers) == 1 &&
534         g_list_nth_data (seat->priv->display_servers, 0) == display_server)
535     {
536         g_debug ("Stopping seat, failed to start a greeter");
537         seat_stop (seat);
538     }
539     /* If we were the active session, switch to a greeter */
540     else if (!IS_GREETER (session) && session == seat_get_active_session (seat))
541     {
542         g_debug ("Active session stopped, starting greeter");
543         seat_switch_to_greeter (seat);
544     }
545
546     /* Stop the display server if no-longer required */
547     if (display_server && !display_server_get_is_stopping (display_server))
548     {
549         GList *link;
550         int n_sessions = 0;
551
552         for (link = seat->priv->sessions; link; link = link->next)
553         {
554             Session *s = link->data;
555             if (s == session)
556                 continue;
557             if (session_get_display_server (s) == display_server)
558                 n_sessions++;
559         }
560         if (n_sessions == 0)
561         {
562             g_debug ("Stopping display server, no sessions require it");
563             display_server_stop (display_server);
564         }
565     }
566
567     g_signal_emit (seat, signals[SESSION_REMOVED], 0, session);
568     g_object_unref (session);
569 }
570
571 static void
572 set_session_env (Session *session)
573 {
574     /* Connect using the session bus */
575     if (getuid () != 0)
576     {
577         if (g_getenv ("DBUS_SESSION_BUS_ADDRESS"))
578             session_set_env (session, "DBUS_SESSION_BUS_ADDRESS", g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
579         session_set_env (session, "LDM_BUS", "SESSION");
580         if (g_getenv ("LD_PRELOAD"))
581             session_set_env (session, "LD_PRELOAD", g_getenv ("LD_PRELOAD"));
582         if (g_getenv ("LD_LIBRARY_PATH"))
583             session_set_env (session, "LD_LIBRARY_PATH", g_getenv ("LD_LIBRARY_PATH"));
584         if (g_getenv ("PATH"))
585             session_set_env (session, "PATH", g_getenv ("PATH"));
586     }
587
588     /* Variables required for regression tests */
589     if (g_getenv ("LIGHTDM_TEST_ROOT"))
590     {
591         session_set_env (session, "LIGHTDM_TEST_ROOT", g_getenv ("LIGHTDM_TEST_ROOT"));
592         session_set_env (session, "DBUS_SYSTEM_BUS_ADDRESS", g_getenv ("DBUS_SYSTEM_BUS_ADDRESS"));
593         session_set_env (session, "DBUS_SESSION_BUS_ADDRESS", g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
594         session_set_env (session, "LD_PRELOAD", g_getenv ("LD_PRELOAD"));
595         session_set_env (session, "LD_LIBRARY_PATH", g_getenv ("LD_LIBRARY_PATH"));
596         session_set_env (session, "GI_TYPELIB_PATH", g_getenv ("GI_TYPELIB_PATH"));
597     }
598 }
599
600 static Session *
601 create_session (Seat *seat, gboolean autostart)
602 {
603     Session *session;
604
605     session = SEAT_GET_CLASS (seat)->create_session (seat);
606     seat->priv->sessions = g_list_append (seat->priv->sessions, session);
607     if (autostart)
608         g_signal_connect (session, "authentication-complete", G_CALLBACK (session_authentication_complete_cb), seat);
609     g_signal_connect (session, "stopped", G_CALLBACK (session_stopped_cb), seat);
610
611     set_session_env (session);
612
613     g_signal_emit (seat, signals[SESSION_ADDED], 0, session);
614
615     return session;
616 }
617
618 static gchar **
619 get_session_argv_from_filename (const gchar *filename, const gchar *session_wrapper)
620 {
621     GKeyFile *session_desktop_file;
622     gboolean result;
623     int argc;
624     gchar *command = NULL, **argv, *path;
625     GError *error = NULL;
626
627     /* Read the command from the .desktop file */
628     session_desktop_file = g_key_file_new ();
629     result = g_key_file_load_from_file (session_desktop_file, filename, G_KEY_FILE_NONE, &error);
630     if (error)
631         g_debug ("Failed to load session file %s: %s", filename, error->message);
632     g_clear_error (&error);
633     if (result)
634     {
635         command = g_key_file_get_string (session_desktop_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
636         if (!command)
637             g_debug ("No command in session file %s", filename);
638     }
639     g_key_file_free (session_desktop_file);
640
641     if (!command)
642         return NULL;
643
644     /* If configured, run sessions through a wrapper */
645     if (session_wrapper)
646     {
647         argv = g_malloc (sizeof (gchar *) * 3);
648         path = g_find_program_in_path (session_wrapper);
649         argv[0] = path ? path : g_strdup (session_wrapper);
650         argv[1] = command;
651         argv[2] = NULL;
652         return argv;
653     }
654
655     /* Split command into an array listing and make command absolute */
656     result = g_shell_parse_argv (command, &argc, &argv, &error);
657     if (error)
658         g_debug ("Invalid session command '%s': %s", command, error->message);
659     g_clear_error (&error);
660     g_free (command);
661     if (!result)
662         return NULL;
663     path = g_find_program_in_path (argv[0]);
664     if (path)
665     {
666         g_free (argv[0]);
667         argv[0] = path;
668     }
669   
670     return argv;
671 }
672
673 static gchar **
674 get_session_argv (const gchar *sessions_dir, const gchar *session_name, const gchar *session_wrapper)
675 {
676     gchar **dirs, **argv;
677     int i;
678
679     g_return_val_if_fail (sessions_dir != NULL, NULL);
680     g_return_val_if_fail (session_name != NULL, NULL);
681
682     dirs = g_strsplit (sessions_dir, ":", -1);
683     for (i = 0; dirs[i]; i++)
684     {
685         gchar *filename, *path;
686
687         filename = g_strdup_printf ("%s.desktop", session_name);
688         path = g_build_filename (dirs[i], filename, NULL);
689         g_free (filename);
690         argv = get_session_argv_from_filename (path, session_wrapper);
691         g_free (path);
692         if (argv)
693             break;
694     }
695     g_strfreev (dirs);
696
697     return argv;
698 }
699
700 static Session *
701 create_user_session (Seat *seat, const gchar *username)
702 {
703     User *user;
704     gchar *sessions_dir, **argv;
705     const gchar *session_name, *language;
706     Session *session = NULL;
707
708     /* Load user preferences */
709     user = accounts_get_user_by_name (username);
710     if (!user)
711     {
712         g_debug ("Can't login unknown user '%s'", username);
713         return NULL;
714     }
715     session_name = user_get_xsession (user);
716     language = user_get_language (user);
717
718     if (!session_name)
719         session_name = seat_get_string_property (seat, "user-session");
720     sessions_dir = config_get_string (config_get_instance (), "LightDM", "sessions-directory");
721     argv = get_session_argv (sessions_dir, session_name, seat_get_string_property (seat, "session-wrapper"));
722     g_free (sessions_dir);
723     if (argv)
724     {
725         session = create_session (seat, TRUE);
726         session_set_env (session, "DESKTOP_SESSION", session_name);
727         session_set_env (session, "GDMSESSION", session_name);
728         if (language && language[0] != '\0')
729         {
730             session_set_env (session, "LANG", language);
731             session_set_env (session, "GDM_LANG", language);
732         }
733         session_set_pam_service (session, AUTOLOGIN_SERVICE);
734         session_set_username (session, username);
735         session_set_do_authenticate (session, TRUE);
736         session_set_argv (session, argv);     
737     }
738     else
739         g_debug ("Can't find session '%s'", seat_get_string_property (seat, "user-session"));
740
741     g_object_unref (user);
742
743     return session;
744 }
745
746 static Session *
747 create_guest_session (Seat *seat)
748 {
749     gchar *sessions_dir, **argv;
750     Session *session;
751
752     sessions_dir = config_get_string (config_get_instance (), "LightDM", "sessions-directory");
753     argv = get_session_argv (sessions_dir,
754                              seat_get_string_property (seat, "user-session"),
755                              seat_get_string_property (seat, "session-wrapper"));
756     g_free (sessions_dir);
757     if (!argv)
758     {
759         g_debug ("Can't find session '%s'", seat_get_string_property (seat, "user-session"));
760         return NULL;
761     }
762
763     session = create_session (seat, TRUE);
764     session_set_do_authenticate (session, TRUE);
765     session_set_is_guest (session, TRUE);
766     session_set_argv (session, argv);
767   
768     return session;
769 }
770
771 static Session *
772 greeter_create_session_cb (Greeter *greeter, Seat *seat)
773 {
774     Session *session;
775
776     session = create_session (seat, FALSE);
777     session_set_display_server (session, session_get_display_server (SESSION (greeter)));
778
779     return g_object_ref (session);
780 }
781
782 static void
783 prepend_argv (gchar ***argv, const gchar *value)
784 {
785     gchar **old_argv, **new_argv;
786     gint i;
787
788     old_argv = *argv;
789     new_argv = g_malloc (sizeof (gchar *) * (g_strv_length (*argv) + 2));
790     new_argv[0] = g_strdup (value);
791     for (i = 0; old_argv[i]; i++)
792         new_argv[i + 1] = old_argv[i];
793     new_argv[i + 1] = NULL;
794
795     g_free (*argv);
796     *argv = new_argv;
797 }
798
799 static Session *
800 find_user_session (Seat *seat, const gchar *username)
801 {
802     GList *link;
803
804     if (!username)
805         return NULL;
806
807     for (link = seat->priv->sessions; link; link = link->next)
808     {
809         Session *session = link->data;
810
811         if (!session_get_is_stopping (session) && strcmp (session_get_username (session), username) == 0)
812             return session;
813     }
814
815     return NULL;
816 }
817
818 static gboolean
819 greeter_start_session_cb (Greeter *greeter, SessionType type, const gchar *session_name, Seat *seat)
820 {
821     Session *session, *existing_session;
822     const gchar *username, *language = NULL;
823     User *user;
824     gchar *sessions_dir = NULL;
825     gchar **argv;
826
827     /* Get the session to use */
828     if (greeter_get_guest_authenticated (greeter))
829     {
830         session = create_guest_session (seat);
831         if (!session)
832             return FALSE;
833         session_set_pam_service (session, AUTOLOGIN_SERVICE);
834     }
835     else
836         session = greeter_get_authentication_session (greeter);
837
838     /* Switch to this session when it is ready */
839     if (seat->priv->session_to_activate)
840         g_object_unref (seat->priv->session_to_activate);
841     seat->priv->session_to_activate = g_object_ref (session);
842
843     /* Return to existing session if it is open */
844     username = session_get_username (session);
845     existing_session = find_user_session (seat, username);
846     if (existing_session && session != existing_session)
847     {
848         g_debug ("Returning to existing user session %s", username);
849         session_stop (session);
850         seat_set_active_session (seat, existing_session);
851         return TRUE;
852     }
853
854     /* Get session command to run */
855     switch (type)
856     {
857     case SESSION_TYPE_LOCAL:
858         sessions_dir = config_get_string (config_get_instance (), "LightDM", "sessions-directory");
859         break;
860     case SESSION_TYPE_REMOTE:
861         sessions_dir = config_get_string (config_get_instance (), "LightDM", "remote-sessions-directory");
862         break;
863     }
864
865     /* Load user preferences */
866     user = session_get_user (session);
867     if (user)
868     {
869         if (!session_name)
870             session_name = user_get_xsession (user);
871         language = user_get_language (user);
872     }
873
874     if (!session_name)
875         session_name = seat_get_string_property (seat, "user-session");
876     if (user)
877         user_set_xsession (session_get_user (session), session_name);
878
879     argv = get_session_argv (sessions_dir, session_name, seat_get_string_property (seat, "session-wrapper"));
880     g_free (sessions_dir);
881   
882     if (!argv)
883     {
884         g_debug ("Can't find session '%s'", seat_get_string_property (seat, "user-session"));
885         return FALSE;
886     }
887
888     session_set_argv (session, argv);
889     g_strfreev (argv);
890     session_set_env (session, "DESKTOP_SESSION", session_name);
891     session_set_env (session, "GDMSESSION", session_name);
892     if (language && language[0] != '\0')
893     {
894         session_set_env (session, "LANG", language);
895         session_set_env (session, "GDM_LANG", language);
896     }
897
898     /* If can re-use the display server, stop the greeter first */
899     if (seat->priv->share_display_server)
900     {
901         /* Run on the same display server after the greeter has stopped */
902         session_set_display_server (session, session_get_display_server (SESSION (greeter)));
903
904         g_debug ("Stopping greeter");
905         session_stop (SESSION (greeter));
906
907         return TRUE;
908     }
909     /* Otherwise start a new display server for this session */
910     else
911     {
912         DisplayServer *display_server;
913
914         display_server = create_display_server (seat);
915         if (!display_server_start (display_server))
916             return FALSE;
917
918         session_set_display_server (session, display_server);
919
920         return TRUE;
921     }
922 }
923
924 static Greeter *
925 create_greeter_session (Seat *seat)
926 {
927     gchar *sessions_dir, **argv;
928     Greeter *greeter_session;
929     gchar *greeter_user;
930     const gchar *greeter_wrapper;
931
932     sessions_dir = config_get_string (config_get_instance (), "LightDM", "greeters-directory");
933     argv = get_session_argv (sessions_dir,
934                              seat_get_string_property (seat, "greeter-session"),
935                              seat_get_string_property (seat, "session-wrapper"));
936     g_free (sessions_dir);
937     if (!argv)
938         return NULL;
939
940     greeter_wrapper = seat_get_string_property (seat, "greeter-wrapper");
941     if (greeter_wrapper)
942     {
943         gchar *path;
944         path = g_find_program_in_path (greeter_wrapper);
945         prepend_argv (&argv, path ? path : greeter_wrapper);
946         g_free (path);
947     }
948
949     greeter_session = SEAT_GET_CLASS (seat)->create_greeter_session (seat);
950     seat->priv->sessions = g_list_append (seat->priv->sessions, SESSION (greeter_session));
951     g_signal_connect (greeter_session, "authentication-complete", G_CALLBACK (session_authentication_complete_cb), seat);
952     g_signal_connect (greeter_session, "stopped", G_CALLBACK (session_stopped_cb), seat);
953   
954     set_session_env (SESSION (greeter_session));
955
956     session_set_pam_service (SESSION (greeter_session), GREETER_SERVICE);
957     greeter_user = config_get_string (config_get_instance (), "LightDM", "greeter-user");
958     session_set_username (SESSION (greeter_session), greeter_user);
959     g_free (greeter_user);
960     session_set_argv (SESSION (greeter_session), argv);
961
962     greeter_set_pam_services (greeter_session, USER_SERVICE, AUTOLOGIN_SERVICE);
963     g_signal_connect (greeter_session, "create-session", G_CALLBACK (greeter_create_session_cb), seat);
964     g_signal_connect (greeter_session, "start-session", G_CALLBACK (greeter_start_session_cb), seat);
965
966     /* Set hints to greeter */
967     greeter_set_hint (greeter_session, "default-session", seat_get_string_property (seat, "user-session"));
968     greeter_set_allow_guest (greeter_session, seat_get_allow_guest (seat));
969     greeter_set_hint (greeter_session, "hide-users", seat_get_boolean_property (seat, "greeter-hide-users") ? "true" : "false");
970     greeter_set_hint (greeter_session, "show-manual-login", seat_get_boolean_property (seat, "greeter-show-manual-login") ? "true" : "false");
971     greeter_set_hint (greeter_session, "show-remote-login", seat_get_boolean_property (seat, "greeter-show-remote-login") ? "true" : "false");
972     greeter_set_hint (greeter_session, "has-guest-account", seat_get_allow_guest (seat) && seat_get_boolean_property (seat, "greeter-allow-guest") ? "true" : "false");
973
974     return greeter_session;
975 }
976
977 static Session *
978 find_session_for_display_server (Seat *seat, DisplayServer *display_server)
979 {
980     GList *link;
981
982     for (link = seat->priv->sessions; link; link = link->next)
983     {
984         Session *session = link->data;
985         if (session_get_display_server (session) == display_server && !session_get_is_stopping (session))
986             return session;
987     }
988
989     return NULL;
990 }
991
992 static void
993 display_server_ready_cb (DisplayServer *display_server, Seat *seat)
994 {
995     const gchar *script;
996     Session *session;
997
998     /* Run setup script */
999     script = seat_get_string_property (seat, "display-setup-script");
1000     if (script && !run_script (seat, display_server, script, NULL))
1001     {
1002         g_debug ("Stopping display server due to failed setup script");
1003         display_server_stop (display_server);
1004         return;
1005     }
1006
1007     /* Stop if don't need to run a session */
1008     if (!display_server_get_start_local_sessions (display_server))
1009         return;
1010
1011     emit_upstart_signal ("login-session-start");
1012
1013     /* Start the session waiting for this display server */
1014     session = find_session_for_display_server (seat, display_server);
1015     if (session)
1016     {
1017         if (session_get_is_authenticated (session))
1018         {
1019             g_debug ("Display server ready, running session");
1020             run_session (seat, session);
1021         }
1022         else
1023         {
1024             g_debug ("Display server ready, starting session authentication");
1025             start_session (seat, session);
1026         }
1027     }
1028     else
1029     {
1030         g_debug ("Stopping not required display server");
1031         display_server_stop (display_server);
1032     }
1033 }
1034
1035 static DisplayServer *
1036 create_display_server (Seat *seat)
1037 {
1038     DisplayServer *display_server;
1039
1040     display_server = SEAT_GET_CLASS (seat)->create_display_server (seat);
1041     seat->priv->display_servers = g_list_append (seat->priv->display_servers, display_server);
1042     g_signal_connect (display_server, "ready", G_CALLBACK (display_server_ready_cb), seat);
1043     g_signal_connect (display_server, "stopped", G_CALLBACK (display_server_stopped_cb), seat);
1044
1045     return display_server;
1046 }
1047
1048 static Greeter *
1049 find_greeter_session (Seat *seat)
1050 {
1051     GList *link;
1052
1053     for (link = seat->priv->sessions; link; link = link->next)
1054     {
1055         Session *session = link->data;
1056         if (!session_get_is_stopping (session) && IS_GREETER (session))
1057             return GREETER (session);
1058     }
1059
1060     return NULL;
1061 }
1062
1063 gboolean
1064 seat_switch_to_greeter (Seat *seat)
1065 {
1066     Greeter *greeter_session;
1067     DisplayServer *display_server;
1068
1069     g_return_val_if_fail (seat != NULL, FALSE);
1070
1071     if (!seat->priv->can_switch)
1072         return FALSE;
1073
1074     /* Switch to greeter if one open (shouldn't be though) */
1075     greeter_session = find_greeter_session (seat);
1076     if (greeter_session)
1077     {
1078         g_debug ("Switching to existing greeter");
1079         seat_set_active_session (seat, SESSION (greeter_session));
1080         return TRUE;
1081     }
1082
1083     greeter_session = create_greeter_session (seat);
1084     if (seat->priv->session_to_activate)
1085         g_object_unref (seat->priv->session_to_activate);
1086     seat->priv->session_to_activate = g_object_ref (greeter_session);
1087
1088     display_server = create_display_server (seat);
1089     session_set_display_server (SESSION (greeter_session), display_server);
1090     if (!display_server_start (display_server))
1091         return FALSE;
1092
1093     return TRUE;
1094 }
1095
1096 gboolean
1097 seat_switch_to_user (Seat *seat, const gchar *username, const gchar *session_name)
1098 {
1099     Session *session;
1100     DisplayServer *display_server;
1101
1102     g_return_val_if_fail (seat != NULL, FALSE);
1103     g_return_val_if_fail (username != NULL, FALSE);
1104
1105     if (!seat->priv->can_switch)
1106         return FALSE;
1107
1108     g_debug ("Switching to user %s", username);
1109
1110     session = find_user_session (seat, username);
1111     if (session)
1112     {
1113         g_debug ("Switching to existing user session %s", username);
1114         seat_set_active_session (seat, session);
1115         return TRUE;
1116     }
1117
1118     session = create_user_session (seat, username);
1119     if (!session)
1120         return FALSE;
1121     if (seat->priv->session_to_activate)
1122         g_object_unref (seat->priv->session_to_activate);
1123     seat->priv->session_to_activate = g_object_ref (session);
1124     session_set_pam_service (session, USER_SERVICE);
1125
1126     display_server = create_display_server (seat);
1127     session_set_display_server (session, display_server);
1128     if (!display_server_start (display_server))
1129         return FALSE;
1130
1131     return FALSE;
1132 }
1133
1134 static Session *
1135 find_guest_session (Seat *seat)
1136 {
1137     GList *link;
1138
1139     for (link = seat->priv->sessions; link; link = link->next)
1140     {
1141         Session *session = link->data;
1142         if (!session_get_is_stopping (session) && session_get_is_guest (session))
1143             return session;
1144     }
1145
1146     return NULL;
1147 }
1148
1149 gboolean
1150 seat_switch_to_guest (Seat *seat, const gchar *session_name)
1151 {
1152     Session *session;
1153     DisplayServer *display_server;
1154     GList *link;
1155
1156     g_return_val_if_fail (seat != NULL, FALSE);
1157
1158     if (!seat->priv->can_switch || !seat_get_allow_guest (seat))
1159         return FALSE;
1160
1161     /* Switch to session if one open */
1162     session = find_guest_session (seat);
1163     if (session)
1164     {
1165         g_debug ("Switching to existing guest account %s", session_get_username (session));
1166         seat_set_active_session (seat, session);
1167         return TRUE;
1168     }
1169
1170     display_server = create_display_server (seat);
1171     if (!display_server_start (display_server))
1172         return FALSE;
1173
1174     session = create_guest_session (seat);
1175     if (!session)
1176         return FALSE;
1177     if (seat->priv->session_to_activate)
1178         g_object_unref (seat->priv->session_to_activate);
1179     seat->priv->session_to_activate = g_object_ref (session);
1180     session_set_pam_service (session, AUTOLOGIN_SERVICE);
1181     session_set_display_server (session, display_server);
1182
1183     return TRUE;
1184 }
1185
1186 gboolean
1187 seat_lock (Seat *seat, const gchar *username)
1188 {
1189     Greeter *greeter_session;
1190     DisplayServer *display_server;
1191
1192     g_return_val_if_fail (seat != NULL, FALSE);
1193
1194     if (!seat->priv->can_switch)
1195         return FALSE;
1196
1197     g_debug ("Locking seat");
1198
1199     /* Switch to greeter if one open (shouldn't be though) */
1200     greeter_session = find_greeter_session (seat);
1201     if (greeter_session)
1202     {
1203         g_debug ("Switching to existing greeter");
1204         seat_set_active_session (seat, SESSION (greeter_session));
1205         return TRUE;
1206     }
1207
1208     display_server = create_display_server (seat);
1209     if (!display_server_start (display_server))
1210         return FALSE;
1211
1212     greeter_session = create_greeter_session (seat);
1213     if (seat->priv->session_to_activate)
1214         g_object_unref (seat->priv->session_to_activate);
1215     seat->priv->session_to_activate = g_object_ref (greeter_session);
1216     greeter_set_hint (greeter_session, "lock-screen", "true");
1217     if (username)
1218         greeter_set_hint (greeter_session, "select-user", username);
1219     session_set_display_server (SESSION (greeter_session), display_server);
1220
1221     return TRUE;
1222 }
1223
1224 void
1225 seat_stop (Seat *seat)
1226 {
1227     g_return_if_fail (seat != NULL);
1228
1229     if (seat->priv->stopping)
1230         return;
1231
1232     g_debug ("Stopping seat");
1233     seat->priv->stopping = TRUE;
1234     SEAT_GET_CLASS (seat)->stop (seat);
1235 }
1236
1237 gboolean
1238 seat_get_is_stopping (Seat *seat)
1239 {
1240     g_return_val_if_fail (seat != NULL, FALSE);
1241     return seat->priv->stopping;
1242 }
1243
1244 static void
1245 seat_real_setup (Seat *seat)
1246 {
1247 }
1248
1249 static gboolean
1250 seat_real_start (Seat *seat)
1251 {
1252     const gchar *autologin_username;
1253     int autologin_timeout;
1254     gboolean autologin_guest;
1255     gboolean autologin_in_background;
1256     const gchar *user_session;
1257     Session *session = NULL;
1258     DisplayServer *display_server;
1259     User *user;
1260     gchar *sessions_dir;
1261     const gchar *wrapper = NULL;
1262     const gchar *session_name = NULL;
1263
1264     g_debug ("Starting seat");
1265
1266     display_server = create_display_server (seat);
1267
1268     /* If this display server doesn't have a session running on it, just start it */
1269     if (!display_server_get_start_local_sessions (display_server))
1270         return display_server_start (display_server);
1271
1272     /* Get autologin settings */
1273     autologin_username = seat_get_string_property (seat, "autologin-user");
1274     if (g_strcmp0 (autologin_username, "") == 0)
1275         autologin_username = NULL;
1276     autologin_timeout = seat_get_integer_property (seat, "autologin-user-timeout");
1277     autologin_guest = seat_get_boolean_property (seat, "autologin-guest");
1278     autologin_in_background = seat_get_boolean_property (seat, "autologin-in-background");
1279
1280     /* Autologin if configured */
1281     if (autologin_timeout == 0 || autologin_in_background)
1282     {
1283         if (autologin_guest)
1284             session = create_guest_session (seat);
1285         else if (autologin_username != NULL)
1286             session = create_user_session (seat, autologin_username);
1287       
1288         if (session)
1289         {
1290             if (seat->priv->session_to_activate)
1291                 g_object_unref (seat->priv->session_to_activate);
1292             seat->priv->session_to_activate = g_object_ref (session);
1293             session_set_pam_service (session, AUTOLOGIN_SERVICE);
1294         }
1295
1296         /* Load in background if required */
1297         if (autologin_in_background && session)
1298         {
1299             DisplayServer *background_display_server;
1300
1301             background_display_server = create_display_server (seat);
1302             session_set_display_server (session, background_display_server);
1303             if (!display_server_start (background_display_server))
1304                 return FALSE;
1305
1306             /* Start a greeter as well */
1307             session = NULL;
1308         }
1309     }
1310
1311     /* Fallback to a greeter */
1312     if (!session)
1313     {
1314         Greeter *greeter_session;
1315
1316         greeter_session = create_greeter_session (seat);
1317         if (seat->priv->session_to_activate)
1318             g_object_unref (seat->priv->session_to_activate);
1319         seat->priv->session_to_activate = g_object_ref (greeter_session);
1320         session = SESSION (greeter_session);
1321
1322         if (autologin_timeout)
1323         {
1324             gchar *value;
1325
1326             value = g_strdup_printf ("%d", autologin_timeout);
1327             greeter_set_hint (greeter_session, "autologin-timeout", value);
1328             g_free (value);
1329             if (autologin_username)
1330                 greeter_set_hint (greeter_session, "autologin-user", autologin_username);
1331             if (autologin_guest)
1332                 greeter_set_hint (greeter_session, "autologin-guest", "true");
1333         }
1334     }
1335
1336     /* Fail if can't start a session */
1337     if (!session)
1338     {
1339         seat_stop (seat);
1340         return FALSE;
1341     }
1342
1343     /* Start display server to show session on */
1344     session_set_display_server (session, display_server);
1345     if (!display_server_start (display_server))
1346         return FALSE;
1347
1348     return TRUE;
1349 }
1350
1351 static void
1352 seat_real_set_active_session (Seat *seat, Session *session)
1353 {
1354 }
1355
1356 static Session *
1357 seat_real_get_active_session (Seat *seat)
1358 {
1359     return NULL;
1360 }
1361
1362 static void
1363 seat_real_stop (Seat *seat)
1364 {
1365     GList *list, *link;
1366
1367     check_stopped (seat);
1368     if (seat->priv->stopped)
1369         return;
1370
1371     /* Stop all the display servers and sessions on the seat. Copy the list as
1372      * it might be modified if a display server / session stops during this loop */
1373     list = g_list_copy (seat->priv->display_servers);
1374     for (link = list; link; link = link->next)
1375         g_object_ref (link->data);
1376     for (link = list; link; link = link->next)
1377     {
1378         DisplayServer *display_server = link->data;
1379         if (!display_server_get_is_stopping (display_server))
1380         {
1381             g_debug ("Stopping display server");
1382             display_server_stop (display_server);
1383         }
1384     }
1385     g_list_free_full (list, g_object_unref);
1386     list = g_list_copy (seat->priv->sessions);
1387     for (link = list; link; link = link->next)
1388         g_object_ref (link->data);
1389     for (link = list; link; link = link->next)
1390     {
1391         Session *session = link->data;
1392         if (!session_get_is_stopping (session))
1393         {
1394             g_debug ("Stopping session");
1395             session_stop (session);
1396         }
1397     }
1398     g_list_free_full (list, g_object_unref);
1399 }
1400
1401 static void
1402 seat_init (Seat *seat)
1403 {
1404     seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_TYPE, SeatPrivate);
1405     seat->priv->properties = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
1406     seat->priv->share_display_server = TRUE;
1407 }
1408
1409 static void
1410 seat_finalize (GObject *object)
1411 {
1412     Seat *self;
1413     GList *link;
1414
1415     self = SEAT (object);
1416
1417     g_hash_table_unref (self->priv->properties);
1418     for (link = self->priv->display_servers; link; link = link->next)
1419     {
1420         DisplayServer *display_server = link->data;
1421         g_signal_handlers_disconnect_matched (display_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self);
1422     }  
1423     g_list_free_full (self->priv->display_servers, g_object_unref);
1424     for (link = self->priv->sessions; link; link = link->next)
1425     {
1426         Session *session = link->data;
1427         g_signal_handlers_disconnect_matched (session, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self);
1428     }
1429     g_list_free_full (self->priv->sessions, g_object_unref);
1430     if (self->priv->session_to_activate)
1431         g_object_unref (self->priv->session_to_activate);
1432
1433     G_OBJECT_CLASS (seat_parent_class)->finalize (object);
1434 }
1435
1436 static void
1437 seat_class_init (SeatClass *klass)
1438 {
1439     GObjectClass *object_class = G_OBJECT_CLASS (klass);
1440
1441     klass->setup = seat_real_setup;
1442     klass->start = seat_real_start;
1443     klass->set_active_session = seat_real_set_active_session;
1444     klass->get_active_session = seat_real_get_active_session;
1445     klass->run_script = seat_real_run_script;
1446     klass->stop = seat_real_stop;
1447
1448     object_class->finalize = seat_finalize;
1449
1450     g_type_class_add_private (klass, sizeof (SeatPrivate));
1451
1452     signals[SESSION_ADDED] =
1453         g_signal_new ("session-added",
1454                       G_TYPE_FROM_CLASS (klass),
1455                       G_SIGNAL_RUN_LAST,
1456                       G_STRUCT_OFFSET (SeatClass, session_added),
1457                       NULL, NULL,
1458                       NULL,
1459                       G_TYPE_NONE, 1, SESSION_TYPE);
1460     signals[RUNNING_USER_SESSION] =
1461         g_signal_new ("running-user-session",
1462                       G_TYPE_FROM_CLASS (klass),
1463                       G_SIGNAL_RUN_LAST,
1464                       G_STRUCT_OFFSET (SeatClass, running_user_session),
1465                       NULL, NULL,
1466                       NULL,
1467                       G_TYPE_NONE, 1, SESSION_TYPE);
1468     signals[SESSION_REMOVED] =
1469         g_signal_new ("session-removed",
1470                       G_TYPE_FROM_CLASS (klass),
1471                       G_SIGNAL_RUN_LAST,
1472                       G_STRUCT_OFFSET (SeatClass, session_removed),
1473                       NULL, NULL,
1474                       NULL,
1475                       G_TYPE_NONE, 1, SESSION_TYPE);
1476     signals[STOPPED] =
1477         g_signal_new ("stopped",
1478                       G_TYPE_FROM_CLASS (klass),
1479                       G_SIGNAL_RUN_LAST,
1480                       G_STRUCT_OFFSET (SeatClass, stopped),
1481                       NULL, NULL,
1482                       NULL,
1483                       G_TYPE_NONE, 0);
1484 }