15 #include <security/pam_appl.h>
20 #include "configuration.h"
21 #include "session-child.h"
23 #include "console-kit.h"
25 #include "privileges.h"
26 #include "x-authority.h"
27 #include "configuration.h"
29 /* Child process being run */
30 static GPid child_pid = 0;
32 /* Pipe to communicate with daemon */
33 static int from_daemon_output = 0;
34 static int to_daemon_input = 0;
36 static gboolean is_interactive;
37 static gboolean do_authenticate;
38 static gboolean authentication_complete = FALSE;
39 static pam_handle_t *pam_handle;
41 /* Maximum length of a string to pass between daemon and session */
42 #define MAX_STRING_LENGTH 65535
45 write_data (const void *buf, size_t count)
47 if (write (to_daemon_input, buf, count) != count)
48 g_printerr ("Error writing to daemon: %s\n", strerror (errno));
52 write_string (const char *value)
56 length = value ? strlen (value) : -1;
57 write_data (&length, sizeof (length));
59 write_data (value, sizeof (char) * length);
63 read_data (void *buf, size_t count)
67 n_read = read (from_daemon_output, buf, count);
69 g_printerr ("Error reading from daemon: %s\n", strerror (errno));
75 read_string_full (void* (*alloc_fn)(size_t n))
80 if (read_data (&length, sizeof (length)) <= 0)
84 if (length > MAX_STRING_LENGTH)
86 g_printerr ("Invalid string length %d from daemon\n", length);
90 value = (*alloc_fn) (sizeof (char) * (length + 1));
91 read_data (value, length);
100 return read_string_full (g_malloc);
104 pam_conv_cb (int msg_length, const struct pam_message **msg, struct pam_response **resp, void *app_data)
107 gboolean auth_complete = FALSE;
108 struct pam_response *response;
109 gchar *username = NULL;
111 /* FIXME: We don't support communication after pam_authenticate completes */
112 if (authentication_complete)
115 /* Cancel authentication if requiring input */
118 for (i = 0; i < msg_length; i++)
120 if (msg[i]->msg_style == PAM_PROMPT_ECHO_ON || msg[i]->msg_style == PAM_PROMPT_ECHO_OFF)
122 g_printerr ("Stopping PAM conversation, interaction requested but not supported\n");
127 /* Ignore informational messages */
131 /* Check if we changed user */
132 pam_get_item (pam_handle, PAM_USER, (const void **) &username);
134 /* Notify the daemon */
135 write_string (username);
136 write_data (&auth_complete, sizeof (auth_complete));
137 write_data (&msg_length, sizeof (msg_length));
138 for (i = 0; i < msg_length; i++)
140 const struct pam_message *m = msg[i];
141 write_data (&m->msg_style, sizeof (m->msg_style));
142 write_string (m->msg);
146 read_data (&error, sizeof (error));
147 if (error != PAM_SUCCESS)
149 response = calloc (msg_length, sizeof (struct pam_response));
150 for (i = 0; i < msg_length; i++)
152 struct pam_response *r = &response[i];
153 // callers of this function inside pam will expect to be able to call
154 // free() on the strings we give back. So alloc with malloc.
155 r->resp = read_string_full (malloc);
156 read_data (&r->resp_retcode, sizeof (r->resp_retcode));
164 signal_cb (int signum)
166 /* Pass on signal to child, otherwise just quit */
168 kill (child_pid, signum);
176 gchar *x_authority_name;
177 guint16 x_authority_family;
178 guint8 *x_authority_address;
179 gsize x_authority_address_length;
180 gchar *x_authority_number;
181 guint8 *x_authority_data;
182 gsize x_authority_data_length;
184 x_authority_name = read_string ();
185 if (!x_authority_name)
188 read_data (&x_authority_family, sizeof (x_authority_family));
189 read_data (&x_authority_address_length, sizeof (x_authority_address_length));
190 x_authority_address = g_malloc (x_authority_address_length);
191 read_data (x_authority_address, x_authority_address_length);
192 x_authority_number = read_string ();
193 read_data (&x_authority_data_length, sizeof (x_authority_data_length));
194 x_authority_data = g_malloc (x_authority_data_length);
195 read_data (x_authority_data, x_authority_data_length);
197 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 /* GNU provides this but we can't rely on that so let's make our own version */
202 updwtmpx (const gchar *wtmp_file, struct utmpx *ut)
206 memset (&u, 0, sizeof (u));
207 u.ut_type = ut->ut_type;
208 u.ut_pid = ut->ut_pid;
210 strncpy (u.ut_line, ut->ut_line, sizeof (u.ut_line));
212 strncpy (u.ut_id, ut->ut_id, sizeof (u.ut_id));
214 strncpy (u.ut_user, ut->ut_user, sizeof (u.ut_user));
216 strncpy (u.ut_host, ut->ut_host, sizeof (u.ut_host));
217 u.ut_tv.tv_sec = ut->ut_tv.tv_sec;
218 u.ut_tv.tv_usec = ut->ut_tv.tv_usec;
220 updwtmp (wtmp_file, &u);
224 session_child_run (int argc, char **argv)
226 struct pam_conv conversation = { pam_conv_cb, NULL };
227 int i, version, fd, result;
228 gboolean auth_complete = TRUE;
230 gchar *log_filename, *log_backup_filename = NULL;
233 gchar **command_argv;
234 GVariantBuilder ck_parameters;
236 int authentication_result;
237 gchar *authentication_result_string;
241 gchar *remote_host_name;
243 XAuthority *x_authority = NULL;
244 gchar *x_authority_filename;
245 GDBusConnection *bus;
246 const gchar *login1_session_id = NULL;
247 gchar *console_kit_cookie = NULL;
248 const gchar *locale_value;
250 static const gchar * const locale_var_names[] = {
263 const gchar *home_directory;
264 GError *error = NULL;
266 #if !defined(GLIB_VERSION_2_36)
270 if (config_get_boolean (config_get_instance (), "LightDM", "lock-memory"))
272 /* Protect memory from being paged to disk, as we deal with passwords */
273 mlockall (MCL_CURRENT | MCL_FUTURE);
276 /* Make input non-blocking */
277 fd = open ("/dev/null", O_RDONLY);
278 dup2 (fd, STDIN_FILENO);
282 fd = open ("/dev/null", O_WRONLY);
283 dup2 (fd, STDOUT_FILENO);
286 /* Get the pipe from the daemon */
289 g_printerr ("Usage: lightdm --session-child INPUTFD OUTPUTFD\n");
292 from_daemon_output = atoi (argv[2]);
293 to_daemon_input = atoi (argv[3]);
294 if (from_daemon_output == 0 || to_daemon_input == 0)
296 g_printerr ("Invalid file descriptors %s %s\n", argv[2], argv[3]);
300 /* Don't let these pipes leak to the command we will run */
301 fcntl (from_daemon_output, F_SETFD, FD_CLOEXEC);
302 fcntl (to_daemon_input, F_SETFD, FD_CLOEXEC);
304 /* Read a version number so we can handle upgrades (i.e. a newer version of session child is run for an old daemon */
305 read_data (&version, sizeof (version));
307 service = read_string ();
308 username = read_string ();
309 read_data (&do_authenticate, sizeof (do_authenticate));
310 read_data (&is_interactive, sizeof (is_interactive));
311 read_string (); /* Used to be class, now we just use the environment variable */
312 tty = read_string ();
313 remote_host_name = read_string ();
314 xdisplay = read_string ();
315 x_authority = read_xauth ();
318 result = pam_start (service, username, &conversation, &pam_handle);
319 if (result != PAM_SUCCESS)
321 g_printerr ("Failed to start PAM: %s", pam_strerror (NULL, result));
327 pam_set_item (pam_handle, PAM_XDISPLAY, xdisplay);
329 pam_set_item (pam_handle, PAM_TTY, xdisplay);
332 pam_set_item (pam_handle, PAM_TTY, tty);
337 struct pam_xauth_data value;
339 value.name = (char *) x_authority_get_authorization_name (x_authority);
340 value.namelen = strlen (x_authority_get_authorization_name (x_authority));
341 value.data = (char *) x_authority_get_authorization_data (x_authority);
342 value.datalen = x_authority_get_authorization_data_length (x_authority);
343 pam_set_item (pam_handle, PAM_XAUTHDATA, &value);
350 const gchar *new_username;
352 authentication_result = pam_authenticate (pam_handle, 0);
354 /* See what user we ended up as */
355 if (pam_get_item (pam_handle, PAM_USER, (const void **) &new_username) != PAM_SUCCESS)
357 pam_end (pam_handle, 0);
361 username = g_strdup (new_username);
363 /* Write record to btmp database */
364 if (authentication_result == PAM_AUTH_ERR)
369 memset (&ut, 0, sizeof (ut));
370 ut.ut_type = USER_PROCESS;
371 ut.ut_pid = getpid ();
374 strncpy (ut.ut_line, xdisplay, sizeof (ut.ut_line));
375 strncpy (ut.ut_id, xdisplay, sizeof (ut.ut_id));
378 strncpy (ut.ut_line, tty + strlen ("/dev/"), sizeof (ut.ut_line));
379 strncpy (ut.ut_user, username, sizeof (ut.ut_user));
381 strncpy (ut.ut_host, xdisplay, sizeof (ut.ut_host));
382 else if (remote_host_name)
383 strncpy (ut.ut_host, remote_host_name, sizeof (ut.ut_host));
384 gettimeofday (&tv, NULL);
385 ut.ut_tv.tv_sec = tv.tv_sec;
386 ut.ut_tv.tv_usec = tv.tv_usec;
388 updwtmpx ("/var/log/btmp", &ut);
391 /* Check account is valid */
392 if (authentication_result == PAM_SUCCESS)
393 authentication_result = pam_acct_mgmt (pam_handle, 0);
394 if (authentication_result == PAM_NEW_AUTHTOK_REQD)
395 authentication_result = pam_chauthtok (pam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
398 authentication_result = PAM_SUCCESS;
399 authentication_complete = TRUE;
401 if (authentication_result == PAM_SUCCESS)
403 /* Fail authentication if user doesn't actually exist */
404 user = accounts_get_user_by_name (username);
407 g_printerr ("Failed to get information on user %s: %s\n", username, strerror (errno));
408 authentication_result = PAM_USER_UNKNOWN;
412 /* Set POSIX variables */
413 pam_putenv (pam_handle, "PATH=/usr/local/bin:/usr/bin:/bin");
414 pam_putenv (pam_handle, g_strdup_printf ("USER=%s", username));
415 pam_putenv (pam_handle, g_strdup_printf ("LOGNAME=%s", username));
416 pam_putenv (pam_handle, g_strdup_printf ("HOME=%s", user_get_home_directory (user)));
417 pam_putenv (pam_handle, g_strdup_printf ("SHELL=%s", user_get_shell (user)));
419 /* Let the greeter and user session inherit the system default locale */
420 for (i = 0; locale_var_names[i] != NULL; i++)
422 if ((locale_value = g_getenv (locale_var_names[i])) != NULL)
424 locale_var = g_strdup_printf ("%s=%s", locale_var_names[i], locale_value);
425 pam_putenv (pam_handle, locale_var);
432 authentication_result_string = g_strdup (pam_strerror (pam_handle, authentication_result));
434 /* Report authentication result */
435 write_string (username);
436 write_data (&auth_complete, sizeof (auth_complete));
437 write_data (&authentication_result, sizeof (authentication_result));
438 write_string (authentication_result_string);
440 /* Check we got a valid user */
443 g_printerr ("No user selected during authentication\n");
444 pam_end (pam_handle, 0);
448 /* Stop if we didn't authenticated */
449 if (authentication_result != PAM_SUCCESS)
451 pam_end (pam_handle, 0);
455 /* Get the command to run (blocks) */
456 log_filename = read_string ();
460 tty = read_string ();
462 x_authority_filename = read_string ();
466 xdisplay = read_string ();
468 g_object_unref (x_authority);
469 x_authority = read_xauth ();
471 read_data (&env_length, sizeof (env_length));
472 for (i = 0; i < env_length; i++)
473 pam_putenv (pam_handle, read_string ());
474 read_data (&command_argc, sizeof (command_argc));
475 command_argv = g_malloc (sizeof (gchar *) * (command_argc + 1));
476 for (i = 0; i < command_argc; i++)
477 command_argv[i] = read_string ();
478 command_argv[i] = NULL;
480 /* If nothing to run just refresh credentials because we successfully authenticated */
481 if (command_argc == 0)
483 pam_setcred (pam_handle, PAM_REINITIALIZE_CRED);
484 pam_end (pam_handle, 0);
488 /* Redirect stderr to a log file */
491 log_backup_filename = g_strdup_printf ("%s.old", log_filename);
492 if (g_path_is_absolute (log_filename))
494 rename (log_filename, log_backup_filename);
495 fd = open (log_filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
496 dup2 (fd, STDERR_FILENO);
498 g_free (log_filename);
504 fd = open ("/dev/null", O_WRONLY);
505 dup2 (fd, STDERR_FILENO);
509 /* Set group membership - these can be overriden in pam_setcred */
512 if (initgroups (username, user_get_gid (user)) < 0)
514 g_printerr ("Failed to initialize supplementary groups for %s: %s\n", username, strerror (errno));
515 _exit (EXIT_FAILURE);
519 /* Set credentials */
520 result = pam_setcred (pam_handle, PAM_ESTABLISH_CRED);
521 if (result != PAM_SUCCESS)
523 g_printerr ("Failed to establish PAM credentials: %s\n", pam_strerror (pam_handle, result));
524 pam_end (pam_handle, 0);
528 /* Open the session */
529 result = pam_open_session (pam_handle, 0);
530 if (result != PAM_SUCCESS)
532 g_printerr ("Failed to open PAM session: %s\n", pam_strerror (pam_handle, result));
533 pam_end (pam_handle, 0);
537 /* Open a connection to the system bus for ConsoleKit - we must keep it open or CK will close the session */
538 bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
540 g_printerr ("Unable to contact system bus: %s", error->message);
543 pam_end (pam_handle, 0);
547 /* Check what logind session we are, or fallback to ConsoleKit */
548 login1_session_id = pam_getenv (pam_handle, "XDG_SESSION_ID");
549 if (login1_session_id)
551 write_string (login1_session_id);
557 g_variant_builder_init (&ck_parameters, G_VARIANT_TYPE ("(a(sv))"));
558 g_variant_builder_open (&ck_parameters, G_VARIANT_TYPE ("a(sv)"));
559 g_variant_builder_add (&ck_parameters, "(sv)", "unix-user", g_variant_new_int32 (user_get_uid (user)));
560 if (g_strcmp0 (pam_getenv (pam_handle, "XDG_SESSION_CLASS"), "greeter") == 0)
561 g_variant_builder_add (&ck_parameters, "(sv)", "session-type", g_variant_new_string ("LoginWindow"));
564 g_variant_builder_add (&ck_parameters, "(sv)", "x11-display", g_variant_new_string (xdisplay));
566 g_variant_builder_add (&ck_parameters, "(sv)", "x11-display-device", g_variant_new_string (tty));
568 if (remote_host_name)
570 g_variant_builder_add (&ck_parameters, "(sv)", "is-local", g_variant_new_boolean (FALSE));
571 g_variant_builder_add (&ck_parameters, "(sv)", "remote-host-name", g_variant_new_string (remote_host_name));
574 g_variant_builder_add (&ck_parameters, "(sv)", "is-local", g_variant_new_boolean (TRUE));
575 console_kit_cookie = ck_open_session (&ck_parameters);
578 write_string (console_kit_cookie);
579 if (console_kit_cookie)
582 value = g_strdup_printf ("XDG_SESSION_COOKIE=%s", console_kit_cookie);
583 pam_putenv (pam_handle, value);
588 /* Write X authority */
591 gboolean drop_privileges, result;
593 GError *error = NULL;
595 drop_privileges = geteuid () == 0;
597 privileges_drop (user_get_uid (user), user_get_gid (user));
598 result = x_authority_write (x_authority, XAUTH_WRITE_MODE_REPLACE, x_authority_filename, &error);
600 privileges_reclaim ();
603 g_printerr ("Error writing X authority: %s\n", error->message);
604 g_clear_error (&error);
607 pam_end (pam_handle, 0);
611 value = g_strdup_printf ("XAUTHORITY=%s", x_authority_filename);
612 pam_putenv (pam_handle, value);
616 /* Catch terminate signal and pass it to the child */
617 signal (SIGTERM, signal_cb);
619 /* Run the command as the authenticated user */
620 uid = user_get_uid (user);
621 gid = user_get_gid (user);
622 home_directory = user_get_home_directory (user);
626 /* Make this process its own session */
630 /* Change to this user */
633 if (setgid (gid) != 0)
636 if (setuid (uid) != 0)
640 /* Change working directory */
641 /* NOTE: This must be done after the permissions are changed because NFS filesystems can
642 * be setup so the local root user accesses the NFS files as 'nobody'. If the home directories
643 * are not system readable then the chdir can fail */
644 if (chdir (home_directory) != 0)
649 rename (log_filename, log_backup_filename);
650 fd = open (log_filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
653 dup2 (fd, STDERR_FILENO);
658 /* Run the command */
659 execve (command_argv[0], command_argv, pam_getenvlist (pam_handle));
660 _exit (EXIT_FAILURE);
663 /* Bail out if failed to fork */
666 g_printerr ("Failed to fork session child process: %s\n", strerror (errno));
667 return_code = EXIT_FAILURE;
670 /* Wait for the command to complete (blocks) */
674 if (g_strcmp0 (pam_getenv (pam_handle, "XDG_SESSION_CLASS"), "greeter") != 0)
679 memset (&ut, 0, sizeof (ut));
680 ut.ut_type = USER_PROCESS;
681 ut.ut_pid = child_pid;
684 strncpy (ut.ut_line, xdisplay, sizeof (ut.ut_line));
685 strncpy (ut.ut_id, xdisplay, sizeof (ut.ut_id));
688 strncpy (ut.ut_line, tty + strlen ("/dev/"), sizeof (ut.ut_line));
689 strncpy (ut.ut_user, username, sizeof (ut.ut_user));
691 strncpy (ut.ut_host, xdisplay, sizeof (ut.ut_host));
692 else if (remote_host_name)
693 strncpy (ut.ut_host, remote_host_name, sizeof (ut.ut_host));
694 gettimeofday (&tv, NULL);
695 ut.ut_tv.tv_sec = tv.tv_sec;
696 ut.ut_tv.tv_usec = tv.tv_usec;
698 /* Write records to utmp/wtmp databases */
700 if (!pututxline (&ut))
701 g_printerr ("Failed to write utmpx: %s\n", strerror (errno));
703 updwtmpx ("/var/log/wtmp", &ut);
706 waitpid (child_pid, &return_code, 0);
710 if (g_strcmp0 (pam_getenv (pam_handle, "XDG_SESSION_CLASS"), "greeter") != 0)
715 memset (&ut, 0, sizeof (ut));
716 ut.ut_type = DEAD_PROCESS;
717 ut.ut_pid = child_pid;
720 strncpy (ut.ut_line, xdisplay, sizeof (ut.ut_line));
721 strncpy (ut.ut_id, xdisplay, sizeof (ut.ut_id));
724 strncpy (ut.ut_line, tty + strlen ("/dev/"), sizeof (ut.ut_line));
725 strncpy (ut.ut_user, username, sizeof (ut.ut_user));
727 strncpy (ut.ut_host, xdisplay, sizeof (ut.ut_host));
728 else if (remote_host_name)
729 strncpy (ut.ut_host, remote_host_name, sizeof (ut.ut_host));
730 gettimeofday (&tv, NULL);
731 ut.ut_tv.tv_sec = tv.tv_sec;
732 ut.ut_tv.tv_usec = tv.tv_usec;
734 /* Write records to utmp/wtmp databases */
736 if (!pututxline (&ut))
737 g_printerr ("Failed to write utmpx: %s\n", strerror (errno));
739 updwtmpx ("/var/log/wtmp", &ut);
743 /* Remove X authority */
746 gboolean drop_privileges, result;
747 GError *error = NULL;
749 drop_privileges = geteuid () == 0;
751 privileges_drop (user_get_uid (user), user_get_gid (user));
752 result = x_authority_write (x_authority, XAUTH_WRITE_MODE_REMOVE, x_authority_filename, &error);
754 privileges_reclaim ();
757 g_printerr ("Error removing X authority: %s\n", error->message);
758 g_clear_error (&error);
760 _exit (EXIT_FAILURE);
763 /* Close the Console Kit session */
764 if (console_kit_cookie)
765 ck_close_session (console_kit_cookie);
767 /* Close the session */
768 pam_close_session (pam_handle, 0);
770 /* Remove credentials */
771 pam_setcred (pam_handle, PAM_DELETE_CRED);
773 pam_end (pam_handle, 0);
776 /* Return result of session process to the daemon */