15 #include <security/pam_appl.h>
24 #include "configuration.h"
25 #include "session-child.h"
27 #include "console-kit.h"
29 #include "privileges.h"
30 #include "x-authority.h"
31 #include "configuration.h"
33 /* Child process being run */
34 static GPid child_pid = 0;
36 /* Pipe to communicate with daemon */
37 static int from_daemon_output = 0;
38 static int to_daemon_input = 0;
40 static gboolean is_interactive;
41 static gboolean do_authenticate;
42 static gboolean authentication_complete = FALSE;
43 static pam_handle_t *pam_handle;
45 /* Maximum length of a string to pass between daemon and session */
46 #define MAX_STRING_LENGTH 65535
49 write_data (const void *buf, size_t count)
51 if (write (to_daemon_input, buf, count) != count)
52 g_printerr ("Error writing to daemon: %s\n", strerror (errno));
56 write_string (const char *value)
60 length = value ? strlen (value) : -1;
61 write_data (&length, sizeof (length));
63 write_data (value, sizeof (char) * length);
67 read_data (void *buf, size_t count)
71 n_read = read (from_daemon_output, buf, count);
73 g_printerr ("Error reading from daemon: %s\n", strerror (errno));
79 read_string_full (void* (*alloc_fn)(size_t n))
84 if (read_data (&length, sizeof (length)) <= 0)
88 if (length > MAX_STRING_LENGTH)
90 g_printerr ("Invalid string length %d from daemon\n", length);
94 value = (*alloc_fn) (sizeof (char) * (length + 1));
95 read_data (value, length);
104 return read_string_full (g_malloc);
108 pam_conv_cb (int msg_length, const struct pam_message **msg, struct pam_response **resp, void *app_data)
111 gboolean auth_complete = FALSE;
112 struct pam_response *response;
113 gchar *username = NULL;
115 /* FIXME: We don't support communication after pam_authenticate completes */
116 if (authentication_complete)
119 /* Cancel authentication if requiring input */
122 for (i = 0; i < msg_length; i++)
124 if (msg[i]->msg_style == PAM_PROMPT_ECHO_ON || msg[i]->msg_style == PAM_PROMPT_ECHO_OFF)
126 g_printerr ("Stopping PAM conversation, interaction requested but not supported\n");
131 /* Ignore informational messages */
135 /* Check if we changed user */
136 pam_get_item (pam_handle, PAM_USER, (const void **) &username);
138 /* Notify the daemon */
139 write_string (username);
140 write_data (&auth_complete, sizeof (auth_complete));
141 write_data (&msg_length, sizeof (msg_length));
142 for (i = 0; i < msg_length; i++)
144 const struct pam_message *m = msg[i];
145 write_data (&m->msg_style, sizeof (m->msg_style));
146 write_string (m->msg);
150 read_data (&error, sizeof (error));
151 if (error != PAM_SUCCESS)
153 response = calloc (msg_length, sizeof (struct pam_response));
154 for (i = 0; i < msg_length; i++)
156 struct pam_response *r = &response[i];
157 // callers of this function inside pam will expect to be able to call
158 // free() on the strings we give back. So alloc with malloc.
159 r->resp = read_string_full (malloc);
160 read_data (&r->resp_retcode, sizeof (r->resp_retcode));
168 signal_cb (int signum)
170 /* Pass on signal to child, otherwise just quit */
172 kill (child_pid, signum);
180 gchar *x_authority_name;
181 guint16 x_authority_family;
182 guint8 *x_authority_address;
183 gsize x_authority_address_length;
184 gchar *x_authority_number;
185 guint8 *x_authority_data;
186 gsize x_authority_data_length;
188 x_authority_name = read_string ();
189 if (!x_authority_name)
192 read_data (&x_authority_family, sizeof (x_authority_family));
193 read_data (&x_authority_address_length, sizeof (x_authority_address_length));
194 x_authority_address = g_malloc (x_authority_address_length);
195 read_data (x_authority_address, x_authority_address_length);
196 x_authority_number = read_string ();
197 read_data (&x_authority_data_length, sizeof (x_authority_data_length));
198 x_authority_data = g_malloc (x_authority_data_length);
199 read_data (x_authority_data, x_authority_data_length);
201 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);
204 /* GNU provides this but we can't rely on that so let's make our own version */
206 updwtmpx (const gchar *wtmp_file, struct utmpx *ut)
210 memset (&u, 0, sizeof (u));
211 u.ut_type = ut->ut_type;
212 u.ut_pid = ut->ut_pid;
214 strncpy (u.ut_line, ut->ut_line, sizeof (u.ut_line));
216 strncpy (u.ut_id, ut->ut_id, sizeof (u.ut_id));
218 strncpy (u.ut_user, ut->ut_user, sizeof (u.ut_user));
220 strncpy (u.ut_host, ut->ut_host, sizeof (u.ut_host));
221 u.ut_tv.tv_sec = ut->ut_tv.tv_sec;
222 u.ut_tv.tv_usec = ut->ut_tv.tv_usec;
224 updwtmp (wtmp_file, &u);
228 audit_event (int type, const gchar *username, uid_t uid, const gchar *remote_host_name, const gchar *tty, gboolean success)
232 const char *op = NULL;
234 auditfd = audit_open ();
236 g_printerr ("Error opening audit socket: %s\n", strerror (errno));
240 if (type == AUDIT_USER_LOGIN)
242 else if (type == AUDIT_USER_LOGOUT)
244 result = success == TRUE ? 1 : 0;
246 if (audit_log_acct_message (auditfd, type, NULL, op, username, uid, remote_host_name, NULL, tty, result) <= 0)
247 g_printerr ("Error writing audit message: %s\n", strerror (errno));
254 session_child_run (int argc, char **argv)
256 struct pam_conv conversation = { pam_conv_cb, NULL };
257 int i, version, fd, result;
258 gboolean auth_complete = TRUE;
260 gchar *log_filename, *log_backup_filename = NULL;
263 gchar **command_argv;
264 GVariantBuilder ck_parameters;
266 int authentication_result;
267 gchar *authentication_result_string;
271 gchar *remote_host_name;
273 XAuthority *x_authority = NULL;
274 gchar *x_authority_filename;
275 GDBusConnection *bus;
276 const gchar *login1_session_id = NULL;
277 gchar *console_kit_cookie = NULL;
278 const gchar *locale_value;
280 static const gchar * const locale_var_names[] = {
293 const gchar *home_directory;
294 GError *error = NULL;
296 #if !defined(GLIB_VERSION_2_36)
300 if (config_get_boolean (config_get_instance (), "LightDM", "lock-memory"))
302 /* Protect memory from being paged to disk, as we deal with passwords */
303 mlockall (MCL_CURRENT | MCL_FUTURE);
306 /* Make input non-blocking */
307 fd = open ("/dev/null", O_RDONLY);
308 dup2 (fd, STDIN_FILENO);
312 fd = open ("/dev/null", O_WRONLY);
313 dup2 (fd, STDOUT_FILENO);
316 /* Get the pipe from the daemon */
319 g_printerr ("Usage: lightdm --session-child INPUTFD OUTPUTFD\n");
322 from_daemon_output = atoi (argv[2]);
323 to_daemon_input = atoi (argv[3]);
324 if (from_daemon_output == 0 || to_daemon_input == 0)
326 g_printerr ("Invalid file descriptors %s %s\n", argv[2], argv[3]);
330 /* Don't let these pipes leak to the command we will run */
331 fcntl (from_daemon_output, F_SETFD, FD_CLOEXEC);
332 fcntl (to_daemon_input, F_SETFD, FD_CLOEXEC);
334 /* Read a version number so we can handle upgrades (i.e. a newer version of session child is run for an old daemon */
335 read_data (&version, sizeof (version));
337 service = read_string ();
338 username = read_string ();
339 read_data (&do_authenticate, sizeof (do_authenticate));
340 read_data (&is_interactive, sizeof (is_interactive));
341 read_string (); /* Used to be class, now we just use the environment variable */
342 tty = read_string ();
343 remote_host_name = read_string ();
344 xdisplay = read_string ();
345 x_authority = read_xauth ();
348 result = pam_start (service, username, &conversation, &pam_handle);
349 if (result != PAM_SUCCESS)
351 g_printerr ("Failed to start PAM: %s", pam_strerror (NULL, result));
357 pam_set_item (pam_handle, PAM_XDISPLAY, xdisplay);
359 pam_set_item (pam_handle, PAM_TTY, xdisplay);
362 pam_set_item (pam_handle, PAM_TTY, tty);
367 struct pam_xauth_data value;
369 value.name = (char *) x_authority_get_authorization_name (x_authority);
370 value.namelen = strlen (x_authority_get_authorization_name (x_authority));
371 value.data = (char *) x_authority_get_authorization_data (x_authority);
372 value.datalen = x_authority_get_authorization_data_length (x_authority);
373 pam_set_item (pam_handle, PAM_XAUTHDATA, &value);
380 const gchar *new_username;
382 authentication_result = pam_authenticate (pam_handle, 0);
384 /* See what user we ended up as */
385 if (pam_get_item (pam_handle, PAM_USER, (const void **) &new_username) != PAM_SUCCESS)
387 pam_end (pam_handle, 0);
391 username = g_strdup (new_username);
393 /* Write record to btmp database */
394 if (authentication_result == PAM_AUTH_ERR)
399 memset (&ut, 0, sizeof (ut));
400 ut.ut_type = USER_PROCESS;
401 ut.ut_pid = getpid ();
404 strncpy (ut.ut_line, xdisplay, sizeof (ut.ut_line));
405 strncpy (ut.ut_id, xdisplay, sizeof (ut.ut_id));
408 strncpy (ut.ut_line, tty + strlen ("/dev/"), sizeof (ut.ut_line));
409 strncpy (ut.ut_user, username, sizeof (ut.ut_user));
411 strncpy (ut.ut_host, xdisplay, sizeof (ut.ut_host));
412 else if (remote_host_name)
413 strncpy (ut.ut_host, remote_host_name, sizeof (ut.ut_host));
414 gettimeofday (&tv, NULL);
415 ut.ut_tv.tv_sec = tv.tv_sec;
416 ut.ut_tv.tv_usec = tv.tv_usec;
418 updwtmpx ("/var/log/btmp", &ut);
420 audit_event (AUDIT_USER_LOGIN, username, -1, remote_host_name, tty, FALSE);
423 /* Check account is valid */
424 if (authentication_result == PAM_SUCCESS)
425 authentication_result = pam_acct_mgmt (pam_handle, 0);
426 if (authentication_result == PAM_NEW_AUTHTOK_REQD)
427 authentication_result = pam_chauthtok (pam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
430 authentication_result = PAM_SUCCESS;
431 authentication_complete = TRUE;
433 if (authentication_result == PAM_SUCCESS)
435 /* Fail authentication if user doesn't actually exist */
436 user = accounts_get_user_by_name (username);
439 g_printerr ("Failed to get information on user %s: %s\n", username, strerror (errno));
440 authentication_result = PAM_USER_UNKNOWN;
444 /* Set POSIX variables */
445 pam_putenv (pam_handle, "PATH=/usr/local/bin:/usr/bin:/bin");
446 pam_putenv (pam_handle, g_strdup_printf ("USER=%s", username));
447 pam_putenv (pam_handle, g_strdup_printf ("LOGNAME=%s", username));
448 pam_putenv (pam_handle, g_strdup_printf ("HOME=%s", user_get_home_directory (user)));
449 pam_putenv (pam_handle, g_strdup_printf ("SHELL=%s", user_get_shell (user)));
451 /* Let the greeter and user session inherit the system default locale */
452 for (i = 0; locale_var_names[i] != NULL; i++)
454 if ((locale_value = g_getenv (locale_var_names[i])) != NULL)
456 locale_var = g_strdup_printf ("%s=%s", locale_var_names[i], locale_value);
457 pam_putenv (pam_handle, locale_var);
464 authentication_result_string = g_strdup (pam_strerror (pam_handle, authentication_result));
466 /* Report authentication result */
467 write_string (username);
468 write_data (&auth_complete, sizeof (auth_complete));
469 write_data (&authentication_result, sizeof (authentication_result));
470 write_string (authentication_result_string);
472 /* Check we got a valid user */
475 g_printerr ("No user selected during authentication\n");
476 pam_end (pam_handle, 0);
480 /* Stop if we didn't authenticated */
481 if (authentication_result != PAM_SUCCESS)
483 pam_end (pam_handle, 0);
487 /* Get the command to run (blocks) */
488 log_filename = read_string ();
492 tty = read_string ();
494 x_authority_filename = read_string ();
498 xdisplay = read_string ();
500 g_object_unref (x_authority);
501 x_authority = read_xauth ();
503 read_data (&env_length, sizeof (env_length));
504 for (i = 0; i < env_length; i++)
505 pam_putenv (pam_handle, read_string ());
506 read_data (&command_argc, sizeof (command_argc));
507 command_argv = g_malloc (sizeof (gchar *) * (command_argc + 1));
508 for (i = 0; i < command_argc; i++)
509 command_argv[i] = read_string ();
510 command_argv[i] = NULL;
512 /* If nothing to run just refresh credentials because we successfully authenticated */
513 if (command_argc == 0)
515 pam_setcred (pam_handle, PAM_REINITIALIZE_CRED);
516 pam_end (pam_handle, 0);
520 /* Redirect stderr to a log file */
523 log_backup_filename = g_strdup_printf ("%s.old", log_filename);
524 if (g_path_is_absolute (log_filename))
526 rename (log_filename, log_backup_filename);
527 fd = open (log_filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
528 dup2 (fd, STDERR_FILENO);
530 g_free (log_filename);
536 fd = open ("/dev/null", O_WRONLY);
537 dup2 (fd, STDERR_FILENO);
541 /* Set group membership - these can be overriden in pam_setcred */
544 if (initgroups (username, user_get_gid (user)) < 0)
546 g_printerr ("Failed to initialize supplementary groups for %s: %s\n", username, strerror (errno));
547 _exit (EXIT_FAILURE);
551 /* Set credentials */
552 result = pam_setcred (pam_handle, PAM_ESTABLISH_CRED);
553 if (result != PAM_SUCCESS)
555 g_printerr ("Failed to establish PAM credentials: %s\n", pam_strerror (pam_handle, result));
556 pam_end (pam_handle, 0);
560 /* Open the session */
561 result = pam_open_session (pam_handle, 0);
562 if (result != PAM_SUCCESS)
564 g_printerr ("Failed to open PAM session: %s\n", pam_strerror (pam_handle, result));
565 pam_end (pam_handle, 0);
569 /* Open a connection to the system bus for ConsoleKit - we must keep it open or CK will close the session */
570 bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
572 g_printerr ("Unable to contact system bus: %s", error->message);
575 pam_end (pam_handle, 0);
579 /* Check what logind session we are, or fallback to ConsoleKit */
580 login1_session_id = pam_getenv (pam_handle, "XDG_SESSION_ID");
581 if (login1_session_id)
583 write_string (login1_session_id);
589 g_variant_builder_init (&ck_parameters, G_VARIANT_TYPE ("(a(sv))"));
590 g_variant_builder_open (&ck_parameters, G_VARIANT_TYPE ("a(sv)"));
591 g_variant_builder_add (&ck_parameters, "(sv)", "unix-user", g_variant_new_int32 (user_get_uid (user)));
592 if (g_strcmp0 (pam_getenv (pam_handle, "XDG_SESSION_CLASS"), "greeter") == 0)
593 g_variant_builder_add (&ck_parameters, "(sv)", "session-type", g_variant_new_string ("LoginWindow"));
596 g_variant_builder_add (&ck_parameters, "(sv)", "x11-display", g_variant_new_string (xdisplay));
598 g_variant_builder_add (&ck_parameters, "(sv)", "x11-display-device", g_variant_new_string (tty));
600 if (remote_host_name)
602 g_variant_builder_add (&ck_parameters, "(sv)", "is-local", g_variant_new_boolean (FALSE));
603 g_variant_builder_add (&ck_parameters, "(sv)", "remote-host-name", g_variant_new_string (remote_host_name));
606 g_variant_builder_add (&ck_parameters, "(sv)", "is-local", g_variant_new_boolean (TRUE));
607 console_kit_cookie = ck_open_session (&ck_parameters);
610 write_string (console_kit_cookie);
611 if (console_kit_cookie)
614 value = g_strdup_printf ("XDG_SESSION_COOKIE=%s", console_kit_cookie);
615 pam_putenv (pam_handle, value);
620 /* Write X authority */
623 gboolean drop_privileges, result;
625 GError *error = NULL;
627 drop_privileges = geteuid () == 0;
629 privileges_drop (user_get_uid (user), user_get_gid (user));
630 result = x_authority_write (x_authority, XAUTH_WRITE_MODE_REPLACE, x_authority_filename, &error);
632 privileges_reclaim ();
635 g_printerr ("Error writing X authority: %s\n", error->message);
636 g_clear_error (&error);
639 pam_end (pam_handle, 0);
643 value = g_strdup_printf ("XAUTHORITY=%s", x_authority_filename);
644 pam_putenv (pam_handle, value);
648 /* Catch terminate signal and pass it to the child */
649 signal (SIGTERM, signal_cb);
651 /* Run the command as the authenticated user */
652 uid = user_get_uid (user);
653 gid = user_get_gid (user);
654 home_directory = user_get_home_directory (user);
658 /* Make this process its own session */
662 /* Change to this user */
665 if (setgid (gid) != 0)
668 if (setuid (uid) != 0)
672 /* Change working directory */
673 /* NOTE: This must be done after the permissions are changed because NFS filesystems can
674 * be setup so the local root user accesses the NFS files as 'nobody'. If the home directories
675 * are not system readable then the chdir can fail */
676 if (chdir (home_directory) != 0)
681 rename (log_filename, log_backup_filename);
682 fd = open (log_filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
685 dup2 (fd, STDERR_FILENO);
690 /* Run the command */
691 execve (command_argv[0], command_argv, pam_getenvlist (pam_handle));
692 _exit (EXIT_FAILURE);
695 /* Bail out if failed to fork */
698 g_printerr ("Failed to fork session child process: %s\n", strerror (errno));
699 return_code = EXIT_FAILURE;
702 /* Wait for the command to complete (blocks) */
706 if (g_strcmp0 (pam_getenv (pam_handle, "XDG_SESSION_CLASS"), "greeter") != 0)
711 memset (&ut, 0, sizeof (ut));
712 ut.ut_type = USER_PROCESS;
713 ut.ut_pid = child_pid;
716 strncpy (ut.ut_line, xdisplay, sizeof (ut.ut_line));
717 strncpy (ut.ut_id, xdisplay, sizeof (ut.ut_id));
720 strncpy (ut.ut_line, tty + strlen ("/dev/"), sizeof (ut.ut_line));
721 strncpy (ut.ut_user, username, sizeof (ut.ut_user));
723 strncpy (ut.ut_host, xdisplay, sizeof (ut.ut_host));
724 else if (remote_host_name)
725 strncpy (ut.ut_host, remote_host_name, sizeof (ut.ut_host));
726 gettimeofday (&tv, NULL);
727 ut.ut_tv.tv_sec = tv.tv_sec;
728 ut.ut_tv.tv_usec = tv.tv_usec;
730 /* Write records to utmp/wtmp databases */
732 if (!pututxline (&ut))
733 g_printerr ("Failed to write utmpx: %s\n", strerror (errno));
735 updwtmpx ("/var/log/wtmp", &ut);
737 audit_event (AUDIT_USER_LOGIN, username, uid, remote_host_name, tty, TRUE);
740 waitpid (child_pid, &return_code, 0);
744 if (g_strcmp0 (pam_getenv (pam_handle, "XDG_SESSION_CLASS"), "greeter") != 0)
749 memset (&ut, 0, sizeof (ut));
750 ut.ut_type = DEAD_PROCESS;
751 ut.ut_pid = child_pid;
754 strncpy (ut.ut_line, xdisplay, sizeof (ut.ut_line));
755 strncpy (ut.ut_id, xdisplay, sizeof (ut.ut_id));
758 strncpy (ut.ut_line, tty + strlen ("/dev/"), sizeof (ut.ut_line));
759 strncpy (ut.ut_user, username, sizeof (ut.ut_user));
761 strncpy (ut.ut_host, xdisplay, sizeof (ut.ut_host));
762 else if (remote_host_name)
763 strncpy (ut.ut_host, remote_host_name, sizeof (ut.ut_host));
764 gettimeofday (&tv, NULL);
765 ut.ut_tv.tv_sec = tv.tv_sec;
766 ut.ut_tv.tv_usec = tv.tv_usec;
768 /* Write records to utmp/wtmp databases */
770 if (!pututxline (&ut))
771 g_printerr ("Failed to write utmpx: %s\n", strerror (errno));
773 updwtmpx ("/var/log/wtmp", &ut);
775 audit_event (AUDIT_USER_LOGOUT, username, uid, remote_host_name, tty, TRUE);
779 /* Remove X authority */
782 gboolean drop_privileges, result;
783 GError *error = NULL;
785 drop_privileges = geteuid () == 0;
787 privileges_drop (user_get_uid (user), user_get_gid (user));
788 result = x_authority_write (x_authority, XAUTH_WRITE_MODE_REMOVE, x_authority_filename, &error);
790 privileges_reclaim ();
793 g_printerr ("Error removing X authority: %s\n", error->message);
794 g_clear_error (&error);
796 _exit (EXIT_FAILURE);
799 /* Close the Console Kit session */
800 if (console_kit_cookie)
801 ck_close_session (console_kit_cookie);
803 /* Close the session */
804 pam_close_session (pam_handle, 0);
806 /* Remove credentials */
807 pam_setcred (pam_handle, PAM_DELETE_CRED);
809 pam_end (pam_handle, 0);
812 /* Return result of session process to the daemon */