]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/session.c
Get session to switch display servers
[sojka/lightdm.git] / src / session.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 <config.h>
13
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <unistd.h>
18 #include <sys/wait.h>
19 #include <fcntl.h>
20 #include <glib/gstdio.h>
21 #include <grp.h>
22 #include <pwd.h>
23
24 #include "session.h"
25 #include "configuration.h"
26 #include "console-kit.h"
27 #include "login1.h"
28 #include "guest-account.h"
29
30 enum {
31     GOT_MESSAGES,
32     AUTHENTICATION_COMPLETE,
33     STOPPED,
34     LAST_SIGNAL
35 };
36 static guint signals[LAST_SIGNAL] = { 0 };
37
38 struct SessionPrivate
39 {
40     /* Display server running on */
41     DisplayServer *display_server;
42
43     /* PID of child process */
44     GPid pid;
45
46     /* Pipes to talk to child */
47     int to_child_input;
48     int from_child_output;
49     GIOChannel *from_child_channel;
50     guint from_child_watch;
51     guint child_watch;
52
53     /* User to authenticate as */
54     gchar *username;
55
56     /* TRUE if is a guest account */
57     gboolean is_guest;
58
59     /* User object that matches the current username */
60     User *user;
61
62     /* Messages being requested by PAM */
63     int messages_length;
64     struct pam_message *messages;
65
66     /* Authentication result from PAM */
67     gboolean authentication_started;
68     gboolean authentication_complete;
69     int authentication_result;
70     gchar *authentication_result_string;
71
72     /* File to log to */
73     gchar *log_filename;
74
75     /* Seat class */
76     gchar *class;
77
78     /* tty this session is running on */
79     gchar *tty;
80
81     /* X display connected to */
82     gchar *xdisplay;
83     XAuthority *xauthority;
84     gboolean xauth_use_system_location;
85
86     /* Remote host this session is being controlled from */
87     gchar *remote_host_name;
88
89     /* Console kit cookie */
90     gchar *console_kit_cookie;
91
92     /* login1 session */
93     gchar *login1_session;
94
95     /* Environment to set in child */
96     GList *env;
97 };
98
99 /* Maximum length of a string to pass between daemon and session */
100 #define MAX_STRING_LENGTH 65535
101
102 G_DEFINE_TYPE (Session, session, G_TYPE_OBJECT);
103
104 void
105 session_set_log_file (Session *session, const gchar *filename)
106 {
107     g_return_if_fail (session != NULL);
108     g_free (session->priv->log_filename);
109     session->priv->log_filename = g_strdup (filename);
110 }
111
112 void
113 session_set_class (Session *session, const gchar *class)
114 {
115     g_return_if_fail (session != NULL);
116     g_free (session->priv->class);
117     session->priv->class = g_strdup (class);
118 }
119
120 static void
121 session_real_set_display_server (Session *session, DisplayServer *display_server)
122 {
123     if (session->priv->display_server)
124         g_object_unref (session->priv->display_server);
125     session->priv->display_server = g_object_ref (display_server);
126 }
127
128 void
129 session_set_display_server (Session *session, DisplayServer *display_server)
130 {
131     g_return_if_fail (session != NULL);
132     g_return_if_fail (display_server != NULL);
133     SESSION_GET_CLASS (session)->set_display_server (session, display_server);
134 }
135
136 void
137 session_set_tty (Session *session, const gchar *tty)
138 {
139     g_return_if_fail (session != NULL);
140     g_free (session->priv->tty);
141     session->priv->tty = g_strdup (tty);
142 }
143
144 void
145 session_set_xdisplay (Session *session, const gchar *xdisplay)
146 {
147     g_return_if_fail (session != NULL);
148     g_free (session->priv->xdisplay);
149     session->priv->xdisplay = g_strdup (xdisplay);
150 }
151
152 void
153 session_set_xauthority (Session *session, XAuthority *authority, gboolean use_system_location)
154 {
155     g_return_if_fail (session != NULL);
156     if (session->priv->xauthority)
157         g_object_unref (session->priv->xauthority);
158     session->priv->xauthority = g_object_ref (authority);
159     session->priv->xauth_use_system_location = use_system_location;
160 }
161
162 void
163 session_set_remote_host_name (Session *session, const gchar *remote_host_name)
164 {
165     g_return_if_fail (session != NULL);
166     g_free (session->priv->remote_host_name);
167     session->priv->remote_host_name = g_strdup (remote_host_name);
168 }
169
170 void
171 session_set_env (Session *session, const gchar *name, const gchar *value)
172 {
173     g_return_if_fail (session != NULL);
174     session->priv->env = g_list_append (session->priv->env, g_strdup_printf ("%s=%s", name, value));
175 }
176
177 User *
178 session_get_user (Session *session)
179 {
180     g_return_val_if_fail (session != NULL, NULL);
181
182     if (session->priv->username == NULL)
183         return NULL;
184
185     if (!session->priv->user)
186         session->priv->user = accounts_get_user_by_name (session->priv->username);
187
188     return session->priv->user;
189 }
190
191 static void
192 write_data (Session *session, const void *buf, size_t count)
193 {
194     if (write (session->priv->to_child_input, buf, count) != count)
195         g_warning ("Error writing to session: %s", strerror (errno));
196 }
197
198 static void
199 write_string (Session *session, const char *value)
200 {
201     int length;
202
203     length = value ? strlen (value) : -1;
204     write_data (session, &length, sizeof (length));
205     if (value)
206         write_data (session, value, sizeof (char) * length);
207 }
208
209 static ssize_t
210 read_from_child (Session *session, void *buf, size_t count)
211 {
212     ssize_t n_read;
213     n_read = read (session->priv->from_child_output, buf, count);
214     if (n_read < 0)
215         g_warning ("Error reading from session: %s", strerror (errno));
216     return n_read;
217 }
218
219 static gchar *
220 read_string_from_child (Session *session)
221 {
222     int length;
223     char *value;
224
225     if (read_from_child (session, &length, sizeof (length)) <= 0)
226         return NULL;
227     if (length < 0)
228         return NULL;
229     if (length > MAX_STRING_LENGTH)
230     {
231         g_warning ("Invalid string length %d from child", length);
232         return NULL;
233     }
234
235     value = g_malloc (sizeof (char) * (length + 1));
236     read_from_child (session, value, length);
237     value[length] = '\0';
238
239     return value;
240 }
241
242 static void
243 session_watch_cb (GPid pid, gint status, gpointer data)
244 {
245     Session *session = data;
246
247     session->priv->pid = 0;
248
249     if (WIFEXITED (status))
250         g_debug ("Session %d exited with return value %d", pid, WEXITSTATUS (status));
251     else if (WIFSIGNALED (status))
252         g_debug ("Session %d terminated with signal %d", pid, WTERMSIG (status));
253
254     /* If failed during authentication then report this as an authentication failure */
255     if (session->priv->authentication_started && !session->priv->authentication_complete)
256     {
257         g_debug ("Session %d failed during authentication", pid);
258         session->priv->authentication_complete = TRUE;
259         session->priv->authentication_result = PAM_CONV_ERR;
260         g_free (session->priv->authentication_result_string);
261         session->priv->authentication_result_string = g_strdup ("Authentication stopped before completion");
262         g_signal_emit (G_OBJECT (session), signals[AUTHENTICATION_COMPLETE], 0);
263     }
264
265     g_signal_emit (G_OBJECT (session), signals[STOPPED], 0);
266
267     /* Delete account if it is a guest one */
268     if (session->priv->is_guest)
269         guest_account_cleanup (session->priv->username);
270
271     /* Drop our reference on the child process, it has terminated */
272     g_object_unref (session);
273 }
274
275 static gboolean
276 from_child_cb (GIOChannel *source, GIOCondition condition, gpointer data)
277 {
278     Session *session = data;
279     gchar *username;
280     ssize_t n_read;
281     gboolean auth_complete;
282
283     /* Remote end gone */
284     if (condition == G_IO_HUP)
285     {
286         session->priv->from_child_watch = 0;
287         return FALSE;
288     }
289
290     /* Get the username currently being authenticated (may change during authentication) */
291     username = read_string_from_child (session);
292     if (g_strcmp0 (username, session->priv->username) != 0)
293     {
294         g_free (session->priv->username);
295         session->priv->username = username;
296         if (session->priv->user)
297             g_object_unref (session->priv->user);
298         session->priv->user = NULL;
299     }
300     else
301         g_free (username);
302
303     /* Check if authentication completed */
304     n_read = read_from_child (session, &auth_complete, sizeof (auth_complete));
305     if (n_read < 0)
306         g_debug ("Error reading from child: %s", strerror (errno));
307     if (n_read <= 0)
308     {
309         session->priv->from_child_watch = 0;
310         return FALSE;
311     }
312
313     if (auth_complete)
314     {
315         session->priv->authentication_complete = TRUE;
316         read_from_child (session, &session->priv->authentication_result, sizeof (session->priv->authentication_result));
317         g_free (session->priv->authentication_result_string);
318         session->priv->authentication_result_string = read_string_from_child (session);
319
320         g_debug ("Session %d authentication complete with return value %d: %s", session->priv->pid, session->priv->authentication_result, session->priv->authentication_result_string);
321
322         /* No longer expect any more messages */
323         session->priv->from_child_watch = 0;
324
325         g_signal_emit (G_OBJECT (session), signals[AUTHENTICATION_COMPLETE], 0);
326
327         return FALSE;
328     }
329     else
330     {
331         int i;
332
333         session->priv->messages_length = 0;
334         read_from_child (session, &session->priv->messages_length, sizeof (session->priv->messages_length));
335         session->priv->messages = calloc (session->priv->messages_length, sizeof (struct pam_message));
336         for (i = 0; i < session->priv->messages_length; i++)
337         {
338             struct pam_message *m = &session->priv->messages[i];
339             read_from_child (session, &m->msg_style, sizeof (m->msg_style));
340             m->msg = read_string_from_child (session);
341         }
342
343         g_debug ("Session %d got %d message(s) from PAM", session->priv->pid, session->priv->messages_length);
344
345         g_signal_emit (G_OBJECT (session), signals[GOT_MESSAGES], 0);
346     }
347
348     return TRUE;
349 }
350
351 gboolean
352 session_start (Session *session, const gchar *service, const gchar *username, gboolean do_authenticate, gboolean is_interactive, gboolean is_guest)
353 {
354     int version;
355     int to_child_pipe[2], from_child_pipe[2];
356     int to_child_output, from_child_input;
357
358     g_return_val_if_fail (session != NULL, FALSE);
359     g_return_val_if_fail (service != NULL, FALSE);
360     g_return_val_if_fail (session->priv->pid == 0, FALSE);
361
362     /* Create pipes to talk to the child */
363     if (pipe (to_child_pipe) < 0 || pipe (from_child_pipe) < 0)
364     {
365         g_warning ("Failed to create pipe to communicate with session process: %s", strerror (errno));
366         return FALSE;
367     }
368     to_child_output = to_child_pipe[0];
369     session->priv->to_child_input = to_child_pipe[1];
370     session->priv->from_child_output = from_child_pipe[0];
371     from_child_input = from_child_pipe[1];
372     session->priv->from_child_channel = g_io_channel_unix_new (session->priv->from_child_output);
373     session->priv->from_child_watch = g_io_add_watch (session->priv->from_child_channel, G_IO_IN | G_IO_HUP, from_child_cb, session);
374
375     /* Don't allow the daemon end of the pipes to be accessed in child processes */
376     fcntl (session->priv->to_child_input, F_SETFD, FD_CLOEXEC);
377     fcntl (session->priv->from_child_output, F_SETFD, FD_CLOEXEC);
378
379     /* Create the guest account if it is one */
380     session->priv->is_guest = is_guest;
381     if (is_guest && username == NULL)
382         username = guest_account_setup ();
383
384     /* Remember what username we started with - it will be updated by PAM during authentication */
385     session->priv->username = g_strdup (username);
386
387     /* Run the child */
388     session->priv->pid = fork ();
389     if (session->priv->pid < 0)
390     {
391         g_debug ("Failed to fork session child process: %s", strerror (errno));
392         return FALSE;
393     }
394
395     if (session->priv->pid == 0)
396     {
397         /* Run us again in session child mode */
398         execlp ("lightdm",
399                 "lightdm",
400                 "--session-child",
401                 g_strdup_printf ("%d", to_child_output),
402                 g_strdup_printf ("%d", from_child_input),
403                 NULL);
404         _exit (EXIT_FAILURE);
405     }
406
407     /* Hold a reference on this object until the child process terminates so we
408      * can handle the watch callback even if it is no longer used. Otherwise a
409      * zombie process will remain */
410     g_object_ref (session);
411
412     /* Listen for session termination */
413     session->priv->authentication_started = TRUE;
414     session->priv->child_watch = g_child_watch_add (session->priv->pid, session_watch_cb, session);
415
416     /* Close the ends of the pipes we don't need */
417     close (to_child_output);
418     close (from_child_input);
419
420     /* Indicate what version of the protocol we are using */
421     version = 0;
422     write_data (session, &version, sizeof (version));
423
424     /* Send configuration */
425     write_string (session, service);
426     write_string (session, username);
427     write_data (session, &do_authenticate, sizeof (do_authenticate));
428     write_data (session, &is_interactive, sizeof (is_interactive));
429     write_string (session, session->priv->class);
430     write_string (session, session->priv->tty);
431     write_string (session, session->priv->remote_host_name);
432     write_string (session, session->priv->xdisplay);
433     if (session->priv->xauthority)
434     {
435         guint16 family;
436         gsize length;
437
438         write_string (session, xauth_get_authorization_name (session->priv->xauthority));
439         family = xauth_get_family (session->priv->xauthority);
440         write_data (session, &family, sizeof (family));
441         length = xauth_get_address_length (session->priv->xauthority);
442         write_data (session, &length, sizeof (length));
443         write_data (session, xauth_get_address (session->priv->xauthority), length);
444         write_string (session, xauth_get_number (session->priv->xauthority));
445         length = xauth_get_authorization_data_length (session->priv->xauthority);
446         write_data (session, &length, sizeof (length));
447         write_data (session, xauth_get_authorization_data (session->priv->xauthority), length);
448     }
449     else
450         write_string (session, NULL);
451
452     g_debug ("Started session %d with service '%s', username '%s'", session->priv->pid, service, username);
453
454     return TRUE;
455 }
456
457 const gchar *
458 session_get_username (Session *session)
459 {
460     g_return_val_if_fail (session != NULL, NULL);
461     return session->priv->username;
462 }
463
464 const gchar *
465 session_get_console_kit_cookie (Session *session)
466 {
467     g_return_val_if_fail (session != NULL, NULL);
468     return session->priv->console_kit_cookie;
469 }
470
471 void
472 session_respond (Session *session, struct pam_response *response)
473 {
474     int error = PAM_SUCCESS;
475     int i;
476
477     g_return_if_fail (session != NULL);
478
479     write_data (session, &error, sizeof (error));
480     for (i = 0; i < session->priv->messages_length; i++)
481     {
482         write_string (session, response[i].resp);
483         write_data (session, &response[i].resp_retcode, sizeof (response[i].resp_retcode));
484     }
485
486     /* Delete the old messages */
487     for (i = 0; i < session->priv->messages_length; i++)
488         g_free ((char *) session->priv->messages[i].msg);
489     g_free (session->priv->messages);
490     session->priv->messages = NULL;
491     session->priv->messages_length = 0;
492 }
493
494 void
495 session_respond_error (Session *session, int error)
496 {
497     g_return_if_fail (session != NULL);
498     g_return_if_fail (error != PAM_SUCCESS);
499
500     write_data (session, &error, sizeof (error));
501 }
502
503 int
504 session_get_messages_length (Session *session)
505 {
506     g_return_val_if_fail (session != NULL, 0);
507     return session->priv->messages_length;
508 }
509
510 const struct pam_message *
511 session_get_messages (Session *session)
512 {
513     g_return_val_if_fail (session != NULL, NULL);
514     return session->priv->messages;
515 }
516
517 gboolean
518 session_get_is_authenticated (Session *session)
519 {
520     g_return_val_if_fail (session != NULL, FALSE);
521     return session->priv->authentication_complete && session->priv->authentication_result == PAM_SUCCESS;
522 }
523
524 int
525 session_get_authentication_result (Session *session)
526 {
527     g_return_val_if_fail (session != NULL, 0);
528     return session->priv->authentication_result;
529 }
530
531 const gchar *
532 session_get_authentication_result_string (Session *session)
533 {
534     g_return_val_if_fail (session != NULL, NULL);
535     return session->priv->authentication_result_string;
536 }
537
538 void
539 session_run (Session *session, gchar **argv)
540 {
541     gsize i, argc;
542     gchar *command, *filename;
543     GList *link;
544
545     g_return_if_fail (session != NULL);
546     g_return_if_fail (session_get_is_authenticated (session));
547
548     command = g_strjoinv (" ", argv);
549     g_debug ("Session %d running command %s", session->priv->pid, command);
550     g_free (command);
551
552     /* Create authority location */
553     if (session->priv->xauth_use_system_location)
554     {
555         gchar *run_dir, *dir;
556
557         run_dir = config_get_string (config_get_instance (), "LightDM", "run-directory");
558         dir = g_build_filename (run_dir, session->priv->username, NULL);
559         g_free (run_dir);
560
561         if (g_mkdir_with_parents (dir, S_IRWXU) < 0)
562             g_warning ("Failed to set create system authority dir %s: %s", dir, strerror (errno));          
563         if (getuid () == 0)
564         {
565             if (chown (dir, user_get_uid (session_get_user (session)), user_get_gid (session_get_user (session))) < 0)
566                 g_warning ("Failed to set ownership of user authority dir: %s", strerror (errno));
567         }
568
569         filename = g_build_filename (dir, "xauthority", NULL);
570         g_free (dir);
571     }
572     else
573         filename = g_build_filename (user_get_home_directory (session_get_user (session)), ".Xauthority", NULL);
574
575     write_string (session, session->priv->log_filename);
576     write_string (session, filename);
577     g_free (filename);
578     argc = g_list_length (session->priv->env);
579     write_data (session, &argc, sizeof (argc));
580     for (link = session->priv->env; link; link = link->next)
581         write_string (session, (gchar *) link->data);
582     argc = g_strv_length (argv);
583     write_data (session, &argc, sizeof (argc));
584     for (i = 0; i < argc; i++)
585         write_string (session, argv[i]);
586
587     if (login1_is_running ())
588       session->priv->login1_session = read_string_from_child (session);
589     if (!session->priv->login1_session)
590       session->priv->console_kit_cookie = read_string_from_child (session);
591 }
592
593 void
594 session_lock (Session *session)
595 {
596     g_return_if_fail (session != NULL);
597     if (getuid () == 0)
598     {
599         if (login1_is_running ())
600             login1_lock_session (session->priv->login1_session);
601         if (!session->priv->login1_session)
602             ck_lock_session (session->priv->console_kit_cookie);
603     }
604 }
605
606 void
607 session_unlock (Session *session)
608 {
609     g_return_if_fail (session != NULL);
610     if (getuid () == 0)
611     {
612         if (login1_is_running ())
613             login1_unlock_session (session->priv->login1_session);
614         if (!session->priv->login1_session)
615             ck_unlock_session (session->priv->console_kit_cookie);
616     }
617 }
618
619 void
620 session_stop (Session *session)
621 {
622     g_return_if_fail (session != NULL);
623
624     if (session->priv->pid > 0)
625     {
626         g_debug ("Session %d: Sending SIGTERM", session->priv->pid);
627         kill (session->priv->pid, SIGTERM);
628         // FIXME: Handle timeout
629     }
630 }
631
632 gboolean
633 session_get_is_stopped (Session *session)
634 {
635     g_return_val_if_fail (session != NULL, TRUE);
636     return session->priv->pid == 0;
637 }
638
639 static void
640 session_init (Session *session)
641 {
642     session->priv = G_TYPE_INSTANCE_GET_PRIVATE (session, SESSION_TYPE, SessionPrivate);
643 }
644
645 static void
646 session_finalize (GObject *object)
647 {
648     Session *self = SESSION (object);
649     int i;
650
651     if (self->priv->display_server)
652         g_object_unref (self->priv->display_server);
653     if (self->priv->pid)
654         kill (self->priv->pid, SIGKILL);
655     if (self->priv->from_child_channel)
656         g_io_channel_unref (self->priv->from_child_channel);
657     if (self->priv->from_child_watch)
658         g_source_remove (self->priv->from_child_watch);
659     if (self->priv->child_watch)
660         g_source_remove (self->priv->child_watch);
661     g_free (self->priv->username);
662     if (self->priv->user)
663         g_object_unref (self->priv->user);
664     for (i = 0; i < self->priv->messages_length; i++)
665         g_free ((char *) self->priv->messages[i].msg);
666     g_free (self->priv->messages);
667     g_free (self->priv->authentication_result_string);
668     g_free (self->priv->log_filename);
669     g_free (self->priv->class);
670     g_free (self->priv->tty);
671     g_free (self->priv->xdisplay);
672     if (self->priv->xauthority)
673         g_object_unref (self->priv->xauthority);
674     g_free (self->priv->remote_host_name);
675     g_free (self->priv->login1_session);
676     g_free (self->priv->console_kit_cookie);
677     g_list_free_full (self->priv->env, g_free);
678
679     G_OBJECT_CLASS (session_parent_class)->finalize (object);
680 }
681
682 static void
683 session_class_init (SessionClass *klass)
684 {
685     GObjectClass *object_class = G_OBJECT_CLASS (klass);
686
687     klass->set_display_server = session_real_set_display_server;
688     object_class->finalize = session_finalize;
689
690     g_type_class_add_private (klass, sizeof (SessionPrivate));
691
692     signals[GOT_MESSAGES] =
693         g_signal_new ("got-messages",
694                       G_TYPE_FROM_CLASS (klass),
695                       G_SIGNAL_RUN_LAST,
696                       G_STRUCT_OFFSET (SessionClass, got_messages),
697                       NULL, NULL,
698                       g_cclosure_marshal_VOID__VOID,
699                       G_TYPE_NONE, 0);
700
701     signals[AUTHENTICATION_COMPLETE] =
702         g_signal_new ("authentication-complete",
703                       G_TYPE_FROM_CLASS (klass),
704                       G_SIGNAL_RUN_LAST,
705                       G_STRUCT_OFFSET (SessionClass, authentication_complete),
706                       NULL, NULL,
707                       g_cclosure_marshal_VOID__VOID,
708                       G_TYPE_NONE, 0);
709
710     signals[STOPPED] =
711         g_signal_new ("stopped",
712                       G_TYPE_FROM_CLASS (klass),
713                       G_SIGNAL_RUN_LAST,
714                       G_STRUCT_OFFSET (SessionClass, stopped),
715                       NULL, NULL,
716                       g_cclosure_marshal_VOID__VOID,
717                       G_TYPE_NONE, 0);
718 }