ck-xinit-session.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright Red Hat, Inc. 2007,2009.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Red Hat, Inc., nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  19. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  20. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  21. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  22. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * Gate a process inside of a ConsoleKit session.
  31. *
  32. * We want to do this instead of doing it from inside of xinit because at the
  33. * point we're doing it, we've already added the user's UID to the list of
  34. * allowed clients for the X server, so the ConsoleKit daemon, which assumes
  35. * the user's UID, will be able to connect without needing to be able to read
  36. * the user's X cookies.
  37. */
  38. #include <sys/types.h>
  39. #include <sys/wait.h>
  40. #include <paths.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <syslog.h>
  44. #include <unistd.h>
  45. #include <ck-connector.h>
  46. #include <dbus/dbus.h>
  47. static void
  48. setbusenv(const char *var, const char *val)
  49. {
  50. DBusConnection *conn;
  51. DBusMessage *req, *rep;
  52. DBusMessageIter iter, sub, subsub;
  53. DBusError error;
  54. dbus_error_init (&error);
  55. conn = dbus_bus_get(DBUS_BUS_SESSION, &error);
  56. if (conn == NULL) {
  57. return;
  58. }
  59. req = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
  60. if (req == NULL) {
  61. return;
  62. }
  63. memset(&iter, 0, sizeof(iter));
  64. memset(&sub, 0, sizeof(sub));
  65. memset(&subsub, 0, sizeof(subsub));
  66. dbus_message_iter_init_append(req, &iter);
  67. if (!dbus_message_set_destination(req, DBUS_SERVICE_DBUS) ||
  68. !dbus_message_set_path(req, DBUS_PATH_DBUS) ||
  69. !dbus_message_set_interface(req, DBUS_INTERFACE_DBUS) ||
  70. !dbus_message_set_member(req, "UpdateActivationEnvironment") ||
  71. !dbus_message_iter_open_container(&iter,
  72. DBUS_TYPE_ARRAY,
  73. DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
  74. DBUS_TYPE_STRING_AS_STRING
  75. DBUS_TYPE_STRING_AS_STRING
  76. DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
  77. &sub) ||
  78. !dbus_message_iter_open_container(&sub,
  79. DBUS_TYPE_DICT_ENTRY,
  80. NULL,
  81. &subsub) ||
  82. !dbus_message_iter_append_basic(&subsub, DBUS_TYPE_STRING, &var) ||
  83. !dbus_message_iter_append_basic(&subsub, DBUS_TYPE_STRING, &val) ||
  84. !dbus_message_iter_close_container(&sub, &subsub) ||
  85. !dbus_message_iter_close_container(&iter, &sub)) {
  86. dbus_message_unref(req);
  87. return;
  88. }
  89. rep = dbus_connection_send_with_reply_and_block(conn, req,
  90. 30000, &error);
  91. dbus_message_unref(req);
  92. if (rep) {
  93. dbus_message_unref(rep);
  94. }
  95. }
  96. int
  97. main(int argc, char **argv)
  98. {
  99. CkConnector *ckc = NULL;
  100. DBusError error;
  101. const char *shell, *cookie;
  102. pid_t pid;
  103. int status;
  104. ckc = ck_connector_new();
  105. if (ckc != NULL) {
  106. dbus_error_init (&error);
  107. if (ck_connector_open_session(ckc, &error)) {
  108. pid = fork();
  109. switch (pid) {
  110. case -1:
  111. syslog(LOG_ERR, "error forking child");
  112. break;
  113. case 0:
  114. cookie = ck_connector_get_cookie(ckc);
  115. setenv("XDG_SESSION_COOKIE", cookie, 1);
  116. setbusenv("XDG_SESSION_COOKIE", cookie);
  117. break;
  118. default:
  119. waitpid(pid, &status, 0);
  120. exit(status);
  121. break;
  122. }
  123. } else {
  124. syslog(LOG_ERR, "error connecting to console-kit");
  125. }
  126. } else {
  127. syslog(LOG_ERR, "error setting up to connect to console-kit");
  128. }
  129. if (argc > 1) {
  130. execvp(argv[1], argv + 1);
  131. } else {
  132. shell = getenv("SHELL");
  133. if (shell == NULL) {
  134. shell = _PATH_BSHELL;
  135. }
  136. execlp(shell, shell, NULL);
  137. }
  138. _exit(1);
  139. }