15 #include <security/pam_appl.h>
21 #include "configuration.h"
22 #include "session-child.h"
24 #include "console-kit.h"
26 #include "privileges.h"
27 #include "x-authority.h"
28 #include "configuration.h"
30 /* Child process being run */
31 static GPid child_pid = 0;
33 /* Pipe to communicate with daemon */
34 static int from_daemon_output = 0;
35 static int to_daemon_input = 0;
37 static gboolean is_interactive;
38 static gboolean do_authenticate;
39 static gboolean authentication_complete = FALSE;
40 static pam_handle_t *pam_handle;
42 /* Maximum length of a string to pass between daemon and session */
43 #define MAX_STRING_LENGTH 65535
46 write_data (const void *buf, size_t count)
48 if (write (to_daemon_input, buf, count) != count)
49 g_printerr ("Error writing to daemon: %s\n", strerror (errno));
53 write_string (const char *value)
57 length = value ? strlen (value) : -1;
58 write_data (&length, sizeof (length));
60 write_data (value, sizeof (char) * length);
64 read_data (void *buf, size_t count)
68 n_read = read (from_daemon_output, buf, count);
70 g_printerr ("Error reading from daemon: %s\n", strerror (errno));
76 read_string_full (void* (*alloc_fn)(size_t n))
81 if (read_data (&length, sizeof (length)) <= 0)
85 if (length > MAX_STRING_LENGTH)
87 g_printerr ("Invalid string length %d from daemon\n", length);
91 value = (*alloc_fn) (sizeof (char) * (length + 1));
92 read_data (value, length);
101 return read_string_full (g_malloc);
105 pam_conv_cb (int msg_length, const struct pam_message **msg, struct pam_response **resp, void *app_data)
108 gboolean auth_complete = FALSE;
109 struct pam_response *response;
110 gchar *username = NULL;
112 /* FIXME: We don't support communication after pam_authenticate completes */
113 if (authentication_complete)
116 /* Cancel authentication if requiring input */
119 for (i = 0; i < msg_length; i++)
121 if (msg[i]->msg_style == PAM_PROMPT_ECHO_ON || msg[i]->msg_style == PAM_PROMPT_ECHO_OFF)
123 g_printerr ("Stopping PAM conversation, interaction requested but not supported\n");
128 /* Ignore informational messages */
132 /* Check if we changed user */
133 pam_get_item (pam_handle, PAM_USER, (const void **) &username);
135 /* Notify the daemon */
136 write_string (username);
137 write_data (&auth_complete, sizeof (auth_complete));
138 write_data (&msg_length, sizeof (msg_length));
139 for (i = 0; i < msg_length; i++)
141 const struct pam_message *m = msg[i];
142 write_data (&m->msg_style, sizeof (m->msg_style));
143 write_string (m->msg);
147 read_data (&error, sizeof (error));
148 if (error != PAM_SUCCESS)
150 response = calloc (msg_length, sizeof (struct pam_response));
151 for (i = 0; i < msg_length; i++)
153 struct pam_response *r = &response[i];
154 // callers of this function inside pam will expect to be able to call
155 // free() on the strings we give back. So alloc with malloc.
156 r->resp = read_string_full (malloc);
157 read_data (&r->resp_retcode, sizeof (r->resp_retcode));
165 signal_cb (int signum)
167 /* Pass on signal to child, otherwise just quit */
169 kill (child_pid, signum);
177 gchar *x_authority_name;
178 guint16 x_authority_family;
179 guint8 *x_authority_address;
180 gsize x_authority_address_length;
181 gchar *x_authority_number;
182 guint8 *x_authority_data;
183 gsize x_authority_data_length;
185 x_authority_name = read_string ();
186 if (!x_authority_name)
189 read_data (&x_authority_family, sizeof (x_authority_family));
190 read_data (&x_authority_address_length, sizeof (x_authority_address_length));
191 x_authority_address = g_malloc (x_authority_address_length);
192 read_data (x_authority_address, x_authority_address_length);
193 x_authority_number = read_string ();
194 read_data (&x_authority_data_length, sizeof (x_authority_data_length));
195 x_authority_data = g_malloc (x_authority_data_length);
196 read_data (x_authority_data, x_authority_data_length);
198 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);
201 /* GNU provides this but we can't rely on that so let's make our own version */
203 updwtmpx (const gchar *wtmp_file, struct utmpx *ut)
207 memset (&u, 0, sizeof (u));
208 u.ut_type = ut->ut_type;
209 u.ut_pid = ut->ut_pid;
211 strncpy (u.ut_line, ut->ut_line, sizeof (u.ut_line));
213 strncpy (u.ut_id, ut->ut_id, sizeof (u.ut_id));
215 strncpy (u.ut_user, ut->ut_user, sizeof (u.ut_user));
217 strncpy (u.ut_host, ut->ut_host, sizeof (u.ut_host));
218 u.ut_tv.tv_sec = ut->ut_tv.tv_sec;
219 u.ut_tv.tv_usec = ut->ut_tv.tv_usec;
221 updwtmp (wtmp_file, &u);
225 audit_event (int type, const gchar *username, uid_t uid, const gchar *remote_host_name, const gchar *tty, gboolean success)
229 const char *op = NULL;
231 auditfd = audit_open ();
233 g_printerr ("Error opening audit socket: %s\n", strerror (errno));
237 if (type == AUDIT_USER_LOGIN)
239 else if (type == AUDIT_USER_LOGOUT)
241 result = success == TRUE ? 1 : 0;
243 if (audit_log_acct_message (auditfd, type, NULL, op, username, uid, remote_host_name, NULL, tty, result) <= 0)
244 g_printerr ("Error writing audit message: %s\n", strerror (errno));
251 session_child_run (int argc, char **argv)
253 struct pam_conv conversation = { pam_conv_cb, NULL };
254 int i, version, fd, result;
255 gboolean auth_complete = TRUE;
257 gchar *log_filename, *log_backup_filename = NULL;
260 gchar **command_argv;
261 GVariantBuilder ck_parameters;
263 int authentication_result;
264 gchar *authentication_result_string;
268 gchar *remote_host_name;
270 XAuthority *x_authority = NULL;
271 gchar *x_authority_filename;
272 GDBusConnection *bus;
273 const gchar *login1_session_id = NULL;
274 gchar *console_kit_cookie = NULL;
275 const gchar *locale_value;
277 static const gchar * const locale_var_names[] = {
290 const gchar *home_directory;
291 GError *error = NULL;
293 #if !defined(GLIB_VERSION_2_36)
297 if (config_get_boolean (config_get_instance (), "LightDM", "lock-memory"))
299 /* Protect memory from being paged to disk, as we deal with passwords */
300 mlockall (MCL_CURRENT | MCL_FUTURE);
303 /* Make input non-blocking */
304 fd = open ("/dev/null", O_RDONLY);
305 dup2 (fd, STDIN_FILENO);
309 fd = open ("/dev/null", O_WRONLY);
310 dup2 (fd, STDOUT_FILENO);
313 /* Get the pipe from the daemon */
316 g_printerr ("Usage: lightdm --session-child INPUTFD OUTPUTFD\n");
319 from_daemon_output = atoi (argv[2]);
320 to_daemon_input = atoi (argv[3]);
321 if (from_daemon_output == 0 || to_daemon_input == 0)
323 g_printerr ("Invalid file descriptors %s %s\n", argv[2], argv[3]);
327 /* Don't let these pipes leak to the command we will run */
328 fcntl (from_daemon_output, F_SETFD, FD_CLOEXEC);
329 fcntl (to_daemon_input, F_SETFD, FD_CLOEXEC);
331 /* Read a version number so we can handle upgrades (i.e. a newer version of session child is run for an old daemon */
332 read_data (&version, sizeof (version));
334 service = read_string ();
335 username = read_string ();
336 read_data (&do_authenticate, sizeof (do_authenticate));
337 read_data (&is_interactive, sizeof (is_interactive));
338 read_string (); /* Used to be class, now we just use the environment variable */
339 tty = read_string ();
340 remote_host_name = read_string ();
341 xdisplay = read_string ();
342 x_authority = read_xauth ();
345 result = pam_start (service, username, &conversation, &pam_handle);
346 if (result != PAM_SUCCESS)
348 g_printerr ("Failed to start PAM: %s", pam_strerror (NULL, result));
354 pam_set_item (pam_handle, PAM_XDISPLAY, xdisplay);
356 pam_set_item (pam_handle, PAM_TTY, xdisplay);
359 pam_set_item (pam_handle, PAM_TTY, tty);
364 struct pam_xauth_data value;
366 value.name = (char *) x_authority_get_authorization_name (x_authority);
367 value.namelen = strlen (x_authority_get_authorization_name (x_authority));
368 value.data = (char *) x_authority_get_authorization_data (x_authority);
369 value.datalen = x_authority_get_authorization_data_length (x_authority);
370 pam_set_item (pam_handle, PAM_XAUTHDATA, &value);
377 const gchar *new_username;
379 authentication_result = pam_authenticate (pam_handle, 0);
381 /* See what user we ended up as */
382 if (pam_get_item (pam_handle, PAM_USER, (const void **) &new_username) != PAM_SUCCESS)
384 pam_end (pam_handle, 0);
388 username = g_strdup (new_username);
390 /* Write record to btmp database */
391 if (authentication_result == PAM_AUTH_ERR)
396 memset (&ut, 0, sizeof (ut));
397 ut.ut_type = USER_PROCESS;
398 ut.ut_pid = getpid ();
401 strncpy (ut.ut_line, xdisplay, sizeof (ut.ut_line));
402 strncpy (ut.ut_id, xdisplay, sizeof (ut.ut_id));
405 strncpy (ut.ut_line, tty + strlen ("/dev/"), sizeof (ut.ut_line));
406 strncpy (ut.ut_user, username, sizeof (ut.ut_user));
408 strncpy (ut.ut_host, xdisplay, sizeof (ut.ut_host));
409 else if (remote_host_name)
410 strncpy (ut.ut_host, remote_host_name, sizeof (ut.ut_host));
411 gettimeofday (&tv, NULL);
412 ut.ut_tv.tv_sec = tv.tv_sec;
413 ut.ut_tv.tv_usec = tv.tv_usec;
415 updwtmpx ("/var/log/btmp", &ut);
417 audit_event (AUDIT_USER_LOGIN, username, -1, remote_host_name, tty, FALSE);
420 /* Check account is valid */
421 if (authentication_result == PAM_SUCCESS)
422 authentication_result = pam_acct_mgmt (pam_handle, 0);
423 if (authentication_result == PAM_NEW_AUTHTOK_REQD)
424 authentication_result = pam_chauthtok (pam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
427 authentication_result = PAM_SUCCESS;
428 authentication_complete = TRUE;
430 if (authentication_result == PAM_SUCCESS)
432 /* Fail authentication if user doesn't actually exist */
433 user = accounts_get_user_by_name (username);
436 g_printerr ("Failed to get information on user %s: %s\n", username, strerror (errno));
437 authentication_result = PAM_USER_UNKNOWN;
441 /* Set POSIX variables */
442 pam_putenv (pam_handle, "PATH=/usr/local/bin:/usr/bin:/bin");
443 pam_putenv (pam_handle, g_strdup_printf ("USER=%s", username));
444 pam_putenv (pam_handle, g_strdup_printf ("LOGNAME=%s", username));
445 pam_putenv (pam_handle, g_strdup_printf ("HOME=%s", user_get_home_directory (user)));
446 pam_putenv (pam_handle, g_strdup_printf ("SHELL=%s", user_get_shell (user)));
448 /* Let the greeter and user session inherit the system default locale */
449 for (i = 0; locale_var_names[i] != NULL; i++)
451 if ((locale_value = g_getenv (locale_var_names[i])) != NULL)
453 locale_var = g_strdup_printf ("%s=%s", locale_var_names[i], locale_value);
454 pam_putenv (pam_handle, locale_var);
461 authentication_result_string = g_strdup (pam_strerror (pam_handle, authentication_result));
463 /* Report authentication result */
464 write_string (username);
465 write_data (&auth_complete, sizeof (auth_complete));
466 write_data (&authentication_result, sizeof (authentication_result));
467 write_string (authentication_result_string);
469 /* Check we got a valid user */
472 g_printerr ("No user selected during authentication\n");
473 pam_end (pam_handle, 0);
477 /* Stop if we didn't authenticated */
478 if (authentication_result != PAM_SUCCESS)
480 pam_end (pam_handle, 0);
484 /* Get the command to run (blocks) */
485 log_filename = read_string ();
489 tty = read_string ();
491 x_authority_filename = read_string ();
495 xdisplay = read_string ();
497 g_object_unref (x_authority);
498 x_authority = read_xauth ();
500 read_data (&env_length, sizeof (env_length));
501 for (i = 0; i < env_length; i++)
502 pam_putenv (pam_handle, read_string ());
503 read_data (&command_argc, sizeof (command_argc));
504 command_argv = g_malloc (sizeof (gchar *) * (command_argc + 1));
505 for (i = 0; i < command_argc; i++)
506 command_argv[i] = read_string ();
507 command_argv[i] = NULL;
509 /* If nothing to run just refresh credentials because we successfully authenticated */
510 if (command_argc == 0)
512 pam_setcred (pam_handle, PAM_REINITIALIZE_CRED);
513 pam_end (pam_handle, 0);
517 /* Redirect stderr to a log file */
520 log_backup_filename = g_strdup_printf ("%s.old", log_filename);
521 if (g_path_is_absolute (log_filename))
523 rename (log_filename, log_backup_filename);
524 fd = open (log_filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
525 dup2 (fd, STDERR_FILENO);
527 g_free (log_filename);
533 fd = open ("/dev/null", O_WRONLY);
534 dup2 (fd, STDERR_FILENO);
538 /* Set group membership - these can be overriden in pam_setcred */
541 if (initgroups (username, user_get_gid (user)) < 0)
543 g_printerr ("Failed to initialize supplementary groups for %s: %s\n", username, strerror (errno));
544 _exit (EXIT_FAILURE);
548 /* Set credentials */
549 result = pam_setcred (pam_handle, PAM_ESTABLISH_CRED);
550 if (result != PAM_SUCCESS)
552 g_printerr ("Failed to establish PAM credentials: %s\n", pam_strerror (pam_handle, result));
553 pam_end (pam_handle, 0);
557 /* Open the session */
558 result = pam_open_session (pam_handle, 0);
559 if (result != PAM_SUCCESS)
561 g_printerr ("Failed to open PAM session: %s\n", pam_strerror (pam_handle, result));
562 pam_end (pam_handle, 0);
566 /* Open a connection to the system bus for ConsoleKit - we must keep it open or CK will close the session */
567 bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
569 g_printerr ("Unable to contact system bus: %s", error->message);
572 pam_end (pam_handle, 0);
576 /* Check what logind session we are, or fallback to ConsoleKit */
577 login1_session_id = pam_getenv (pam_handle, "XDG_SESSION_ID");
578 if (login1_session_id)
580 write_string (login1_session_id);
586 g_variant_builder_init (&ck_parameters, G_VARIANT_TYPE ("(a(sv))"));
587 g_variant_builder_open (&ck_parameters, G_VARIANT_TYPE ("a(sv)"));
588 g_variant_builder_add (&ck_parameters, "(sv)", "unix-user", g_variant_new_int32 (user_get_uid (user)));
589 if (g_strcmp0 (pam_getenv (pam_handle, "XDG_SESSION_CLASS"), "greeter") == 0)
590 g_variant_builder_add (&ck_parameters, "(sv)", "session-type", g_variant_new_string ("LoginWindow"));
593 g_variant_builder_add (&ck_parameters, "(sv)", "x11-display", g_variant_new_string (xdisplay));
595 g_variant_builder_add (&ck_parameters, "(sv)", "x11-display-device", g_variant_new_string (tty));
597 if (remote_host_name)
599 g_variant_builder_add (&ck_parameters, "(sv)", "is-local", g_variant_new_boolean (FALSE));
600 g_variant_builder_add (&ck_parameters, "(sv)", "remote-host-name", g_variant_new_string (remote_host_name));
603 g_variant_builder_add (&ck_parameters, "(sv)", "is-local", g_variant_new_boolean (TRUE));
604 console_kit_cookie = ck_open_session (&ck_parameters);
607 write_string (console_kit_cookie);
608 if (console_kit_cookie)
611 value = g_strdup_printf ("XDG_SESSION_COOKIE=%s", console_kit_cookie);
612 pam_putenv (pam_handle, value);
617 /* Write X authority */
620 gboolean drop_privileges, result;
622 GError *error = NULL;
624 drop_privileges = geteuid () == 0;
626 privileges_drop (user_get_uid (user), user_get_gid (user));
627 result = x_authority_write (x_authority, XAUTH_WRITE_MODE_REPLACE, x_authority_filename, &error);
629 privileges_reclaim ();
632 g_printerr ("Error writing X authority: %s\n", error->message);
633 g_clear_error (&error);
636 pam_end (pam_handle, 0);
640 value = g_strdup_printf ("XAUTHORITY=%s", x_authority_filename);
641 pam_putenv (pam_handle, value);
645 /* Catch terminate signal and pass it to the child */
646 signal (SIGTERM, signal_cb);
648 /* Run the command as the authenticated user */
649 uid = user_get_uid (user);
650 gid = user_get_gid (user);
651 home_directory = user_get_home_directory (user);
655 /* Make this process its own session */
659 /* Change to this user */
662 if (setgid (gid) != 0)
665 if (setuid (uid) != 0)
669 /* Change working directory */
670 /* NOTE: This must be done after the permissions are changed because NFS filesystems can
671 * be setup so the local root user accesses the NFS files as 'nobody'. If the home directories
672 * are not system readable then the chdir can fail */
673 if (chdir (home_directory) != 0)
678 rename (log_filename, log_backup_filename);
679 fd = open (log_filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
682 dup2 (fd, STDERR_FILENO);
687 /* Run the command */
688 execve (command_argv[0], command_argv, pam_getenvlist (pam_handle));
689 _exit (EXIT_FAILURE);
692 /* Bail out if failed to fork */
695 g_printerr ("Failed to fork session child process: %s\n", strerror (errno));
696 return_code = EXIT_FAILURE;
699 /* Wait for the command to complete (blocks) */
703 if (g_strcmp0 (pam_getenv (pam_handle, "XDG_SESSION_CLASS"), "greeter") != 0)
708 memset (&ut, 0, sizeof (ut));
709 ut.ut_type = USER_PROCESS;
710 ut.ut_pid = child_pid;
713 strncpy (ut.ut_line, xdisplay, sizeof (ut.ut_line));
714 strncpy (ut.ut_id, xdisplay, sizeof (ut.ut_id));
717 strncpy (ut.ut_line, tty + strlen ("/dev/"), sizeof (ut.ut_line));
718 strncpy (ut.ut_user, username, sizeof (ut.ut_user));
720 strncpy (ut.ut_host, xdisplay, sizeof (ut.ut_host));
721 else if (remote_host_name)
722 strncpy (ut.ut_host, remote_host_name, sizeof (ut.ut_host));
723 gettimeofday (&tv, NULL);
724 ut.ut_tv.tv_sec = tv.tv_sec;
725 ut.ut_tv.tv_usec = tv.tv_usec;
727 /* Write records to utmp/wtmp databases */
729 if (!pututxline (&ut))
730 g_printerr ("Failed to write utmpx: %s\n", strerror (errno));
732 updwtmpx ("/var/log/wtmp", &ut);
734 audit_event (AUDIT_USER_LOGIN, username, uid, remote_host_name, tty, TRUE);
737 waitpid (child_pid, &return_code, 0);
741 if (g_strcmp0 (pam_getenv (pam_handle, "XDG_SESSION_CLASS"), "greeter") != 0)
746 memset (&ut, 0, sizeof (ut));
747 ut.ut_type = DEAD_PROCESS;
748 ut.ut_pid = child_pid;
751 strncpy (ut.ut_line, xdisplay, sizeof (ut.ut_line));
752 strncpy (ut.ut_id, xdisplay, sizeof (ut.ut_id));
755 strncpy (ut.ut_line, tty + strlen ("/dev/"), sizeof (ut.ut_line));
756 strncpy (ut.ut_user, username, sizeof (ut.ut_user));
758 strncpy (ut.ut_host, xdisplay, sizeof (ut.ut_host));
759 else if (remote_host_name)
760 strncpy (ut.ut_host, remote_host_name, sizeof (ut.ut_host));
761 gettimeofday (&tv, NULL);
762 ut.ut_tv.tv_sec = tv.tv_sec;
763 ut.ut_tv.tv_usec = tv.tv_usec;
765 /* Write records to utmp/wtmp databases */
767 if (!pututxline (&ut))
768 g_printerr ("Failed to write utmpx: %s\n", strerror (errno));
770 updwtmpx ("/var/log/wtmp", &ut);
772 audit_event (AUDIT_USER_LOGOUT, username, uid, remote_host_name, tty, TRUE);
776 /* Remove X authority */
779 gboolean drop_privileges, result;
780 GError *error = NULL;
782 drop_privileges = geteuid () == 0;
784 privileges_drop (user_get_uid (user), user_get_gid (user));
785 result = x_authority_write (x_authority, XAUTH_WRITE_MODE_REMOVE, x_authority_filename, &error);
787 privileges_reclaim ();
790 g_printerr ("Error removing X authority: %s\n", error->message);
791 g_clear_error (&error);
793 _exit (EXIT_FAILURE);
796 /* Close the Console Kit session */
797 if (console_kit_cookie)
798 ck_close_session (console_kit_cookie);
800 /* Close the session */
801 pam_close_session (pam_handle, 0);
803 /* Remove credentials */
804 pam_setcred (pam_handle, PAM_DELETE_CRED);
806 pam_end (pam_handle, 0);
809 /* Return result of session process to the daemon */