2 * Copyright (C) 2010-2011 Robert Ancell.
3 * Author: Robert Ancell <robert.ancell@canonical.com>
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
20 #include <glib/gstdio.h>
25 #include "configuration.h"
26 #include "console-kit.h"
28 #include "guest-account.h"
32 AUTHENTICATION_COMPLETE,
36 static guint signals[LAST_SIGNAL] = { 0 };
40 /* Display server running on */
41 DisplayServer *display_server;
43 /* PID of child process */
46 /* Pipes to talk to child */
48 int from_child_output;
49 GIOChannel *from_child_channel;
50 guint from_child_watch;
53 /* User to authenticate as */
56 /* TRUE if is a guest account */
59 /* User object that matches the current username */
62 /* Messages being requested by PAM */
64 struct pam_message *messages;
66 /* Authentication result from PAM */
67 gboolean authentication_started;
68 gboolean authentication_complete;
69 int authentication_result;
70 gchar *authentication_result_string;
78 /* tty this session is running on */
81 /* X display connected to */
83 XAuthority *xauthority;
84 gboolean xauth_use_system_location;
86 /* Remote host this session is being controlled from */
87 gchar *remote_host_name;
89 /* Console kit cookie */
90 gchar *console_kit_cookie;
93 gchar *login1_session;
95 /* Environment to set in child */
99 /* Maximum length of a string to pass between daemon and session */
100 #define MAX_STRING_LENGTH 65535
102 G_DEFINE_TYPE (Session, session, G_TYPE_OBJECT);
105 session_set_log_file (Session *session, const gchar *filename)
107 g_return_if_fail (session != NULL);
108 g_free (session->priv->log_filename);
109 session->priv->log_filename = g_strdup (filename);
113 session_set_class (Session *session, const gchar *class)
115 g_return_if_fail (session != NULL);
116 g_free (session->priv->class);
117 session->priv->class = g_strdup (class);
121 session_real_set_display_server (Session *session, DisplayServer *display_server)
123 if (session->priv->display_server)
124 g_object_unref (session->priv->display_server);
125 session->priv->display_server = g_object_ref (display_server);
129 session_set_display_server (Session *session, DisplayServer *display_server)
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);
137 session_set_tty (Session *session, const gchar *tty)
139 g_return_if_fail (session != NULL);
140 g_free (session->priv->tty);
141 session->priv->tty = g_strdup (tty);
145 session_set_xdisplay (Session *session, const gchar *xdisplay)
147 g_return_if_fail (session != NULL);
148 g_free (session->priv->xdisplay);
149 session->priv->xdisplay = g_strdup (xdisplay);
153 session_set_xauthority (Session *session, XAuthority *authority, gboolean use_system_location)
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;
163 session_set_remote_host_name (Session *session, const gchar *remote_host_name)
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);
171 session_set_env (Session *session, const gchar *name, const gchar *value)
173 g_return_if_fail (session != NULL);
174 session->priv->env = g_list_append (session->priv->env, g_strdup_printf ("%s=%s", name, value));
178 session_get_user (Session *session)
180 g_return_val_if_fail (session != NULL, NULL);
182 if (session->priv->username == NULL)
185 if (!session->priv->user)
186 session->priv->user = accounts_get_user_by_name (session->priv->username);
188 return session->priv->user;
192 write_data (Session *session, const void *buf, size_t count)
194 if (write (session->priv->to_child_input, buf, count) != count)
195 g_warning ("Error writing to session: %s", strerror (errno));
199 write_string (Session *session, const char *value)
203 length = value ? strlen (value) : -1;
204 write_data (session, &length, sizeof (length));
206 write_data (session, value, sizeof (char) * length);
210 read_from_child (Session *session, void *buf, size_t count)
213 n_read = read (session->priv->from_child_output, buf, count);
215 g_warning ("Error reading from session: %s", strerror (errno));
220 read_string_from_child (Session *session)
225 if (read_from_child (session, &length, sizeof (length)) <= 0)
229 if (length > MAX_STRING_LENGTH)
231 g_warning ("Invalid string length %d from child", length);
235 value = g_malloc (sizeof (char) * (length + 1));
236 read_from_child (session, value, length);
237 value[length] = '\0';
243 session_watch_cb (GPid pid, gint status, gpointer data)
245 Session *session = data;
247 session->priv->pid = 0;
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));
254 /* If failed during authentication then report this as an authentication failure */
255 if (session->priv->authentication_started && !session->priv->authentication_complete)
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);
265 g_signal_emit (G_OBJECT (session), signals[STOPPED], 0);
267 /* Delete account if it is a guest one */
268 if (session->priv->is_guest)
269 guest_account_cleanup (session->priv->username);
271 /* Drop our reference on the child process, it has terminated */
272 g_object_unref (session);
276 from_child_cb (GIOChannel *source, GIOCondition condition, gpointer data)
278 Session *session = data;
281 gboolean auth_complete;
283 /* Remote end gone */
284 if (condition == G_IO_HUP)
286 session->priv->from_child_watch = 0;
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)
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;
303 /* Check if authentication completed */
304 n_read = read_from_child (session, &auth_complete, sizeof (auth_complete));
306 g_debug ("Error reading from child: %s", strerror (errno));
309 session->priv->from_child_watch = 0;
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);
320 g_debug ("Session %d authentication complete with return value %d: %s", session->priv->pid, session->priv->authentication_result, session->priv->authentication_result_string);
322 /* No longer expect any more messages */
323 session->priv->from_child_watch = 0;
325 g_signal_emit (G_OBJECT (session), signals[AUTHENTICATION_COMPLETE], 0);
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++)
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);
343 g_debug ("Session %d got %d message(s) from PAM", session->priv->pid, session->priv->messages_length);
345 g_signal_emit (G_OBJECT (session), signals[GOT_MESSAGES], 0);
352 session_start (Session *session, const gchar *service, const gchar *username, gboolean do_authenticate, gboolean is_interactive, gboolean is_guest)
355 int to_child_pipe[2], from_child_pipe[2];
356 int to_child_output, from_child_input;
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);
362 /* Create pipes to talk to the child */
363 if (pipe (to_child_pipe) < 0 || pipe (from_child_pipe) < 0)
365 g_warning ("Failed to create pipe to communicate with session process: %s", strerror (errno));
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);
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);
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 ();
384 /* Remember what username we started with - it will be updated by PAM during authentication */
385 session->priv->username = g_strdup (username);
388 session->priv->pid = fork ();
389 if (session->priv->pid < 0)
391 g_debug ("Failed to fork session child process: %s", strerror (errno));
395 if (session->priv->pid == 0)
397 /* Run us again in session child mode */
401 g_strdup_printf ("%d", to_child_output),
402 g_strdup_printf ("%d", from_child_input),
404 _exit (EXIT_FAILURE);
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);
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);
416 /* Close the ends of the pipes we don't need */
417 close (to_child_output);
418 close (from_child_input);
420 /* Indicate what version of the protocol we are using */
422 write_data (session, &version, sizeof (version));
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)
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);
450 write_string (session, NULL);
452 g_debug ("Started session %d with service '%s', username '%s'", session->priv->pid, service, username);
458 session_get_username (Session *session)
460 g_return_val_if_fail (session != NULL, NULL);
461 return session->priv->username;
465 session_get_console_kit_cookie (Session *session)
467 g_return_val_if_fail (session != NULL, NULL);
468 return session->priv->console_kit_cookie;
472 session_respond (Session *session, struct pam_response *response)
474 int error = PAM_SUCCESS;
477 g_return_if_fail (session != NULL);
479 write_data (session, &error, sizeof (error));
480 for (i = 0; i < session->priv->messages_length; i++)
482 write_string (session, response[i].resp);
483 write_data (session, &response[i].resp_retcode, sizeof (response[i].resp_retcode));
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;
495 session_respond_error (Session *session, int error)
497 g_return_if_fail (session != NULL);
498 g_return_if_fail (error != PAM_SUCCESS);
500 write_data (session, &error, sizeof (error));
504 session_get_messages_length (Session *session)
506 g_return_val_if_fail (session != NULL, 0);
507 return session->priv->messages_length;
510 const struct pam_message *
511 session_get_messages (Session *session)
513 g_return_val_if_fail (session != NULL, NULL);
514 return session->priv->messages;
518 session_get_is_authenticated (Session *session)
520 g_return_val_if_fail (session != NULL, FALSE);
521 return session->priv->authentication_complete && session->priv->authentication_result == PAM_SUCCESS;
525 session_get_authentication_result (Session *session)
527 g_return_val_if_fail (session != NULL, 0);
528 return session->priv->authentication_result;
532 session_get_authentication_result_string (Session *session)
534 g_return_val_if_fail (session != NULL, NULL);
535 return session->priv->authentication_result_string;
539 session_run (Session *session, gchar **argv)
542 gchar *command, *filename;
545 g_return_if_fail (session != NULL);
546 g_return_if_fail (session_get_is_authenticated (session));
548 command = g_strjoinv (" ", argv);
549 g_debug ("Session %d running command %s", session->priv->pid, command);
552 /* Create authority location */
553 if (session->priv->xauth_use_system_location)
555 gchar *run_dir, *dir;
557 run_dir = config_get_string (config_get_instance (), "LightDM", "run-directory");
558 dir = g_build_filename (run_dir, session->priv->username, NULL);
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));
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));
569 filename = g_build_filename (dir, "xauthority", NULL);
573 filename = g_build_filename (user_get_home_directory (session_get_user (session)), ".Xauthority", NULL);
575 write_string (session, session->priv->log_filename);
576 write_string (session, 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]);
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);
594 session_lock (Session *session)
596 g_return_if_fail (session != NULL);
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);
607 session_unlock (Session *session)
609 g_return_if_fail (session != NULL);
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);
620 session_stop (Session *session)
622 g_return_if_fail (session != NULL);
624 if (session->priv->pid > 0)
626 g_debug ("Session %d: Sending SIGTERM", session->priv->pid);
627 kill (session->priv->pid, SIGTERM);
628 // FIXME: Handle timeout
633 session_get_is_stopped (Session *session)
635 g_return_val_if_fail (session != NULL, TRUE);
636 return session->priv->pid == 0;
640 session_init (Session *session)
642 session->priv = G_TYPE_INSTANCE_GET_PRIVATE (session, SESSION_TYPE, SessionPrivate);
646 session_finalize (GObject *object)
648 Session *self = SESSION (object);
651 if (self->priv->display_server)
652 g_object_unref (self->priv->display_server);
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);
679 G_OBJECT_CLASS (session_parent_class)->finalize (object);
683 session_class_init (SessionClass *klass)
685 GObjectClass *object_class = G_OBJECT_CLASS (klass);
687 klass->set_display_server = session_real_set_display_server;
688 object_class->finalize = session_finalize;
690 g_type_class_add_private (klass, sizeof (SessionPrivate));
692 signals[GOT_MESSAGES] =
693 g_signal_new ("got-messages",
694 G_TYPE_FROM_CLASS (klass),
696 G_STRUCT_OFFSET (SessionClass, got_messages),
698 g_cclosure_marshal_VOID__VOID,
701 signals[AUTHENTICATION_COMPLETE] =
702 g_signal_new ("authentication-complete",
703 G_TYPE_FROM_CLASS (klass),
705 G_STRUCT_OFFSET (SessionClass, authentication_complete),
707 g_cclosure_marshal_VOID__VOID,
711 g_signal_new ("stopped",
712 G_TYPE_FROM_CLASS (klass),
714 G_STRUCT_OFFSET (SessionClass, stopped),
716 g_cclosure_marshal_VOID__VOID,