15 #include <security/pam_appl.h>
19 #include "configuration.h"
20 #include "session-child.h"
22 #include "console-kit.h"
24 #include "privileges.h"
25 #include "x-authority.h"
26 #include "configuration.h"
28 /* Child process being run */
29 static GPid child_pid = 0;
31 /* Pipe to communicate with daemon */
32 static int from_daemon_output = 0;
33 static int to_daemon_input = 0;
35 static gboolean is_interactive;
36 static gboolean do_authenticate;
37 static gboolean authentication_complete = FALSE;
38 static pam_handle_t *pam_handle;
40 /* Maximum length of a string to pass between daemon and session */
41 #define MAX_STRING_LENGTH 65535
44 write_data (const void *buf, size_t count)
46 if (write (to_daemon_input, buf, count) != count)
47 g_printerr ("Error writing to daemon: %s\n", strerror (errno));
51 write_string (const char *value)
55 length = value ? strlen (value) : -1;
56 write_data (&length, sizeof (length));
58 write_data (value, sizeof (char) * length);
62 read_data (void *buf, size_t count)
66 n_read = read (from_daemon_output, buf, count);
68 g_printerr ("Error reading from daemon: %s\n", strerror (errno));
74 read_string_full (void* (*alloc_fn)(size_t n))
79 if (read_data (&length, sizeof (length)) <= 0)
83 if (length > MAX_STRING_LENGTH)
85 g_printerr ("Invalid string length %d from daemon\n", length);
89 value = (*alloc_fn) (sizeof (char) * (length + 1));
90 read_data (value, length);
99 return read_string_full (g_malloc);
103 pam_conv_cb (int msg_length, const struct pam_message **msg, struct pam_response **resp, void *app_data)
106 gboolean auth_complete = FALSE;
107 struct pam_response *response;
108 gchar *username = NULL;
110 /* FIXME: We don't support communication after pam_authenticate completes */
111 if (authentication_complete)
114 /* Cancel authentication if requiring input */
117 for (i = 0; i < msg_length; i++)
119 if (msg[i]->msg_style == PAM_PROMPT_ECHO_ON || msg[i]->msg_style == PAM_PROMPT_ECHO_OFF)
121 g_printerr ("Stopping PAM conversation, interaction requested but not supported\n");
126 /* Ignore informational messages */
130 /* Check if we changed user */
131 pam_get_item (pam_handle, PAM_USER, (const void **) &username);
133 /* Notify the daemon */
134 write_string (username);
135 write_data (&auth_complete, sizeof (auth_complete));
136 write_data (&msg_length, sizeof (msg_length));
137 for (i = 0; i < msg_length; i++)
139 const struct pam_message *m = msg[i];
140 write_data (&m->msg_style, sizeof (m->msg_style));
141 write_string (m->msg);
145 read_data (&error, sizeof (error));
146 if (error != PAM_SUCCESS)
148 response = calloc (msg_length, sizeof (struct pam_response));
149 for (i = 0; i < msg_length; i++)
151 struct pam_response *r = &response[i];
152 // callers of this function inside pam will expect to be able to call
153 // free() on the strings we give back. So alloc with malloc.
154 r->resp = read_string_full (malloc);
155 read_data (&r->resp_retcode, sizeof (r->resp_retcode));
163 signal_cb (int signum)
165 /* Pass on signal to child, otherwise just quit */
167 kill (child_pid, signum);
175 gchar *x_authority_name;
176 guint16 x_authority_family;
177 guint8 *x_authority_address;
178 gsize x_authority_address_length;
179 gchar *x_authority_number;
180 guint8 *x_authority_data;
181 gsize x_authority_data_length;
183 x_authority_name = read_string ();
184 if (!x_authority_name)
187 read_data (&x_authority_family, sizeof (x_authority_family));
188 read_data (&x_authority_address_length, sizeof (x_authority_address_length));
189 x_authority_address = g_malloc (x_authority_address_length);
190 read_data (x_authority_address, x_authority_address_length);
191 x_authority_number = read_string ();
192 read_data (&x_authority_data_length, sizeof (x_authority_data_length));
193 x_authority_data = g_malloc (x_authority_data_length);
194 read_data (x_authority_data, x_authority_data_length);
196 return x_authority_new (x_authority_family, x_authority_address, x_authority_address_length, x_authority_number, x_authority_name, x_authority_data, x_authority_data_length);
200 session_child_run (int argc, char **argv)
202 struct pam_conv conversation = { pam_conv_cb, NULL };
203 int i, version, fd, result;
204 gboolean auth_complete = TRUE;
206 gchar *log_filename, *log_backup_filename = NULL;
209 gchar **command_argv;
210 GVariantBuilder ck_parameters;
212 int authentication_result;
213 gchar *authentication_result_string;
217 gchar *remote_host_name;
219 XAuthority *x_authority = NULL;
220 gchar *x_authority_filename;
221 GDBusConnection *bus;
222 gchar *console_kit_cookie = NULL;
223 gchar *login1_session = NULL;
224 const gchar *locale_value;
226 static const gchar * const locale_var_names[] = {
237 GError *error = NULL;
239 #if !defined(GLIB_VERSION_2_36)
243 if (config_get_boolean (config_get_instance (), "LightDM", "lock-memory"))
245 /* Protect memory from being paged to disk, as we deal with passwords */
246 mlockall (MCL_CURRENT | MCL_FUTURE);
249 /* Make input non-blocking */
250 fd = open ("/dev/null", O_RDONLY);
251 dup2 (fd, STDIN_FILENO);
255 fd = open ("/dev/null", O_WRONLY);
256 dup2 (fd, STDOUT_FILENO);
259 /* Get the pipe from the daemon */
262 g_printerr ("Usage: lightdm --session-child INPUTFD OUTPUTFD\n");
265 from_daemon_output = atoi (argv[2]);
266 to_daemon_input = atoi (argv[3]);
267 if (from_daemon_output == 0 || to_daemon_input == 0)
269 g_printerr ("Invalid file descriptors %s %s\n", argv[2], argv[3]);
273 /* Don't let these pipes leak to the command we will run */
274 fcntl (from_daemon_output, F_SETFD, FD_CLOEXEC);
275 fcntl (to_daemon_input, F_SETFD, FD_CLOEXEC);
277 /* Read a version number so we can handle upgrades (i.e. a newer version of session child is run for an old daemon */
278 read_data (&version, sizeof (version));
280 service = read_string ();
281 username = read_string ();
282 read_data (&do_authenticate, sizeof (do_authenticate));
283 read_data (&is_interactive, sizeof (is_interactive));
284 read_string (); /* Used to be class, now we just use the environment variable */
285 tty = read_string ();
286 remote_host_name = read_string ();
287 xdisplay = read_string ();
288 x_authority = read_xauth ();
291 result = pam_start (service, username, &conversation, &pam_handle);
292 if (result != PAM_SUCCESS)
294 g_printerr ("Failed to start PAM: %s", pam_strerror (NULL, result));
300 pam_set_item (pam_handle, PAM_XDISPLAY, xdisplay);
302 pam_set_item (pam_handle, PAM_TTY, xdisplay);
305 pam_set_item (pam_handle, PAM_TTY, tty);
310 struct pam_xauth_data value;
312 value.name = (char *) x_authority_get_authorization_name (x_authority);
313 value.namelen = strlen (x_authority_get_authorization_name (x_authority));
314 value.data = (char *) x_authority_get_authorization_data (x_authority);
315 value.datalen = x_authority_get_authorization_data_length (x_authority);
316 pam_set_item (pam_handle, PAM_XAUTHDATA, &value);
323 const gchar *new_username;
325 authentication_result = pam_authenticate (pam_handle, 0);
327 /* See what user we ended up as */
328 if (pam_get_item (pam_handle, PAM_USER, (const void **) &new_username) != PAM_SUCCESS)
331 username = g_strdup (new_username);
333 /* Check account is valid */
334 if (authentication_result == PAM_SUCCESS)
335 authentication_result = pam_acct_mgmt (pam_handle, 0);
336 if (authentication_result == PAM_NEW_AUTHTOK_REQD)
337 authentication_result = pam_chauthtok (pam_handle, 0);
340 authentication_result = PAM_SUCCESS;
341 authentication_complete = TRUE;
343 if (authentication_result == PAM_SUCCESS)
345 /* Fail authentication if user doesn't actually exist */
346 user = accounts_get_user_by_name (username);
349 g_printerr ("Failed to get information on user %s: %s\n", username, strerror (errno));
350 authentication_result = PAM_USER_UNKNOWN;
354 /* Set POSIX variables */
355 pam_putenv (pam_handle, "PATH=/usr/local/bin:/usr/bin:/bin");
356 pam_putenv (pam_handle, g_strdup_printf ("USER=%s", username));
357 pam_putenv (pam_handle, g_strdup_printf ("LOGNAME=%s", username));
358 pam_putenv (pam_handle, g_strdup_printf ("HOME=%s", user_get_home_directory (user)));
359 pam_putenv (pam_handle, g_strdup_printf ("SHELL=%s", user_get_shell (user)));
361 /* Let the greeter and user session inherit the system default locale */
362 for (i = 0; locale_var_names[i] != NULL; i++)
364 if ((locale_value = g_getenv (locale_var_names[i])) != NULL)
366 locale_var = g_strdup_printf ("%s=%s", locale_var_names[i], locale_value);
367 pam_putenv (pam_handle, locale_var);
374 authentication_result_string = g_strdup (pam_strerror (pam_handle, authentication_result));
376 /* Report authentication result */
377 write_string (username);
378 write_data (&auth_complete, sizeof (auth_complete));
379 write_data (&authentication_result, sizeof (authentication_result));
380 write_string (authentication_result_string);
382 /* Check we got a valid user */
385 g_printerr ("No user selected during authentication\n");
389 /* Stop if we didn't authenticated */
390 if (authentication_result != PAM_SUCCESS)
393 /* Get the command to run (blocks) */
394 log_filename = read_string ();
398 tty = read_string ();
400 x_authority_filename = read_string ();
404 xdisplay = read_string ();
406 g_object_unref (x_authority);
407 x_authority = read_xauth ();
409 read_data (&env_length, sizeof (env_length));
410 for (i = 0; i < env_length; i++)
411 pam_putenv (pam_handle, read_string ());
412 read_data (&command_argc, sizeof (command_argc));
413 command_argv = g_malloc (sizeof (gchar *) * (command_argc + 1));
414 for (i = 0; i < command_argc; i++)
415 command_argv[i] = read_string ();
416 command_argv[i] = NULL;
418 /* Redirect stderr to a log file */
420 log_backup_filename = g_strdup_printf ("%s.old", log_filename);
423 fd = open ("/dev/null", O_WRONLY);
424 dup2 (fd, STDERR_FILENO);
427 else if (g_path_is_absolute (log_filename))
429 rename (log_filename, log_backup_filename);
430 fd = open (log_filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
431 dup2 (fd, STDERR_FILENO);
435 /* Set group membership - these can be overriden in pam_setcred */
438 if (initgroups (username, user_get_gid (user)) < 0)
440 g_printerr ("Failed to initialize supplementary groups for %s: %s\n", username, strerror (errno));
441 _exit (EXIT_FAILURE);
445 /* Set credentials */
446 result = pam_setcred (pam_handle, PAM_ESTABLISH_CRED);
447 if (result != PAM_SUCCESS)
449 g_printerr ("Failed to establish PAM credentials: %s\n", pam_strerror (pam_handle, result));
453 /* Open the session */
454 result = pam_open_session (pam_handle, 0);
455 if (result != PAM_SUCCESS)
457 g_printerr ("Failed to open PAM session: %s\n", pam_strerror (pam_handle, result));
461 /* Open a connection to the system bus for ConsoleKit - we must keep it open or CK will close the session */
462 bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
464 g_printerr ("Unable to contact system bus: %s", error->message);
468 if (login1_is_running ())
470 login1_session = login1_get_session_id ();
471 write_string (login1_session);
476 /* Open a Console Kit session */
477 g_variant_builder_init (&ck_parameters, G_VARIANT_TYPE ("(a(sv))"));
478 g_variant_builder_open (&ck_parameters, G_VARIANT_TYPE ("a(sv)"));
479 g_variant_builder_add (&ck_parameters, "(sv)", "unix-user", g_variant_new_int32 (user_get_uid (user)));
480 if (g_strcmp0 (pam_getenv (pam_handle, "XDG_SESSION_CLASS"), "greeter") == 0)
481 g_variant_builder_add (&ck_parameters, "(sv)", "session-type", g_variant_new_string ("LoginWindow"));
484 g_variant_builder_add (&ck_parameters, "(sv)", "x11-display", g_variant_new_string (xdisplay));
486 g_variant_builder_add (&ck_parameters, "(sv)", "x11-display-device", g_variant_new_string (tty));
488 if (remote_host_name)
490 g_variant_builder_add (&ck_parameters, "(sv)", "is-local", g_variant_new_boolean (FALSE));
491 g_variant_builder_add (&ck_parameters, "(sv)", "remote-host-name", g_variant_new_string (remote_host_name));
494 g_variant_builder_add (&ck_parameters, "(sv)", "is-local", g_variant_new_boolean (TRUE));
495 console_kit_cookie = ck_open_session (&ck_parameters);
496 write_string (console_kit_cookie);
497 if (console_kit_cookie)
500 value = g_strdup_printf ("XDG_SESSION_COOKIE=%s", console_kit_cookie);
501 pam_putenv (pam_handle, value);
506 /* Write X authority */
509 gboolean drop_privileges, result;
511 GError *error = NULL;
513 drop_privileges = geteuid () == 0;
515 privileges_drop (user);
516 result = x_authority_write (x_authority, XAUTH_WRITE_MODE_REPLACE, x_authority_filename, &error);
518 privileges_reclaim ();
521 g_printerr ("Error writing X authority: %s\n", error->message);
522 g_clear_error (&error);
526 value = g_strdup_printf ("XAUTHORITY=%s", x_authority_filename);
527 pam_putenv (pam_handle, value);
531 /* Catch terminate signal and pass it to the child */
532 signal (SIGTERM, signal_cb);
534 /* Run the command as the authenticated user */
538 // FIXME: This is not thread safe (particularly the printfs)
540 /* Make this process its own session */
542 g_printerr ("Failed to make process a new session: %s\n", strerror (errno));
544 /* Change to this user */
547 if (setgid (user_get_gid (user)) != 0)
549 g_printerr ("Failed to set group ID to %d: %s\n", user_get_gid (user), strerror (errno));
550 _exit (EXIT_FAILURE);
553 if (setuid (user_get_uid (user)) != 0)
555 g_printerr ("Failed to set user ID to %d: %s\n", user_get_uid (user), strerror (errno));
556 _exit (EXIT_FAILURE);
560 /* Change working directory */
561 /* NOTE: This must be done after the permissions are changed because NFS filesystems can
562 * be setup so the local root user accesses the NFS files as 'nobody'. If the home directories
563 * are not system readable then the chdir can fail */
564 if (chdir (user_get_home_directory (user)) != 0)
566 g_printerr ("Failed to change to home directory %s: %s\n", user_get_home_directory (user), strerror (errno));
567 _exit (EXIT_FAILURE);
570 /* Redirect stderr to a log file */
571 if (log_filename && !g_path_is_absolute (log_filename))
573 rename (log_filename, log_backup_filename);
574 fd = open (log_filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
575 dup2 (fd, STDERR_FILENO);
579 /* Run the command */
580 execve (command_argv[0], command_argv, pam_getenvlist (pam_handle));
581 g_printerr ("Failed to run command: %s\n", strerror (errno));
582 _exit (EXIT_FAILURE);
585 /* Bail out if failed to fork */
588 g_printerr ("Failed to fork session child process: %s\n", strerror (errno));
589 return_code = EXIT_FAILURE;
592 /* Wait for the command to complete (blocks) */
596 if (g_strcmp0 (pam_getenv (pam_handle, "XDG_SESSION_CLASS"), "greeter") != 0)
601 memset (&ut, 0, sizeof (ut));
602 ut.ut_type = USER_PROCESS;
603 ut.ut_pid = child_pid;
605 strncpy (ut.ut_line, tty + strlen ("/dev/"), sizeof (ut.ut_line));
607 strncpy (ut.ut_id, xdisplay, sizeof (ut.ut_id));
608 strncpy (ut.ut_user, username, sizeof (ut.ut_user));
610 strncpy (ut.ut_host, xdisplay, sizeof (ut.ut_host));
611 else if (remote_host_name)
612 strncpy (ut.ut_host, remote_host_name, sizeof (ut.ut_host));
613 gettimeofday (&tv, NULL);
614 ut.ut_tv.tv_sec = tv.tv_sec;
615 ut.ut_tv.tv_usec = tv.tv_usec;
618 if (!pututxline (&ut))
619 g_printerr ("Failed to write utmpx: %s\n", strerror (errno));
623 waitpid (child_pid, &return_code, 0);
627 if (g_strcmp0 (pam_getenv (pam_handle, "XDG_SESSION_CLASS"), "greeter") != 0)
632 memset (&ut, 0, sizeof (ut));
633 ut.ut_type = DEAD_PROCESS;
634 ut.ut_pid = child_pid;
636 strncpy (ut.ut_line, tty + strlen ("/dev/"), sizeof (ut.ut_line));
638 strncpy (ut.ut_id, xdisplay, sizeof (ut.ut_id));
639 strncpy (ut.ut_user, username, sizeof (ut.ut_user));
641 strncpy (ut.ut_host, xdisplay, sizeof (ut.ut_host));
642 else if (remote_host_name)
643 strncpy (ut.ut_host, remote_host_name, sizeof (ut.ut_host));
644 gettimeofday (&tv, NULL);
645 ut.ut_tv.tv_sec = tv.tv_sec;
646 ut.ut_tv.tv_usec = tv.tv_usec;
649 if (!pututxline (&ut))
650 g_printerr ("Failed to write utmpx: %s\n", strerror (errno));
655 /* Remove X authority */
658 gboolean drop_privileges, result;
659 GError *error = NULL;
661 drop_privileges = geteuid () == 0;
663 privileges_drop (user);
664 result = x_authority_write (x_authority, XAUTH_WRITE_MODE_REMOVE, x_authority_filename, &error);
666 privileges_reclaim ();
669 g_printerr ("Error removing X authority: %s\n", error->message);
670 g_clear_error (&error);
672 _exit (EXIT_FAILURE);
675 /* Close the Console Kit session */
676 if (console_kit_cookie)
677 ck_close_session (console_kit_cookie);
679 /* Close the session */
680 pam_close_session (pam_handle, 0);
682 /* Remove credentials */
683 result = pam_setcred (pam_handle, PAM_DELETE_CRED);
685 pam_end (pam_handle, 0);
688 /* Return result of session process to the daemon */