summaryrefslogtreecommitdiff
path: root/consolekit.c
diff options
context:
space:
mode:
Diffstat (limited to 'consolekit.c')
-rw-r--r--consolekit.c241
1 files changed, 241 insertions, 0 deletions
diff --git a/consolekit.c b/consolekit.c
new file mode 100644
index 000000000..0266f06a2
--- /dev/null
+++ b/consolekit.c
@@ -0,0 +1,241 @@
1/*
2 * Copyright (c) 2008 Colin Watson. All rights reserved.
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16/*
17 * Loosely based on pam-ck-connector, which is:
18 *
19 * Copyright (c) 2007 David Zeuthen <davidz@redhat.com>
20 *
21 * Permission is hereby granted, free of charge, to any person
22 * obtaining a copy of this software and associated documentation
23 * files (the "Software"), to deal in the Software without
24 * restriction, including without limitation the rights to use,
25 * copy, modify, merge, publish, distribute, sublicense, and/or sell
26 * copies of the Software, and to permit persons to whom the
27 * Software is furnished to do so, subject to the following
28 * conditions:
29 *
30 * The above copyright notice and this permission notice shall be
31 * included in all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
34 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
35 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
36 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
37 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
38 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
39 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
40 * OTHER DEALINGS IN THE SOFTWARE.
41 */
42
43#include "includes.h"
44
45#ifdef USE_CONSOLEKIT
46
47#include <ck-connector.h>
48
49#include "openbsd-compat/sys-queue.h"
50#include "xmalloc.h"
51#include "channels.h"
52#include "key.h"
53#include "hostfile.h"
54#include "auth.h"
55#include "log.h"
56#include "misc.h"
57#include "servconf.h"
58#include "canohost.h"
59#include "session.h"
60#include "consolekit.h"
61
62extern ServerOptions options;
63extern u_int utmp_len;
64
65void
66set_active(const char *cookie)
67{
68 DBusError err;
69 DBusConnection *connection;
70 DBusMessage *message = NULL, *reply = NULL;
71 char *sid;
72 DBusMessageIter iter, subiter;
73 const char *interface, *property;
74 dbus_bool_t active;
75
76 dbus_error_init(&err);
77 connection = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
78 if (!connection) {
79 if (dbus_error_is_set(&err)) {
80 error("unable to open DBus connection: %s",
81 err.message);
82 dbus_error_free(&err);
83 }
84 goto out;
85 }
86 dbus_connection_set_exit_on_disconnect(connection, FALSE);
87
88 message = dbus_message_new_method_call("org.freedesktop.ConsoleKit",
89 "/org/freedesktop/ConsoleKit/Manager",
90 "org.freedesktop.ConsoleKit.Manager",
91 "GetSessionForCookie");
92 if (!message)
93 goto out;
94 if (!dbus_message_append_args(message, DBUS_TYPE_STRING, &cookie,
95 DBUS_TYPE_INVALID)) {
96 if (dbus_error_is_set(&err)) {
97 error("unable to get current session: %s",
98 err.message);
99 dbus_error_free(&err);
100 }
101 goto out;
102 }
103
104 dbus_error_init(&err);
105 reply = dbus_connection_send_with_reply_and_block(connection, message,
106 -1, &err);
107 if (!reply) {
108 if (dbus_error_is_set(&err)) {
109 error("unable to get current session: %s",
110 err.message);
111 dbus_error_free(&err);
112 }
113 goto out;
114 }
115
116 dbus_error_init(&err);
117 if (!dbus_message_get_args(reply, &err,
118 DBUS_TYPE_OBJECT_PATH, &sid,
119 DBUS_TYPE_INVALID)) {
120 if (dbus_error_is_set(&err)) {
121 error("unable to get current session: %s",
122 err.message);
123 dbus_error_free(&err);
124 }
125 goto out;
126 }
127 dbus_message_unref(reply);
128 dbus_message_unref(message);
129 message = reply = NULL;
130
131 message = dbus_message_new_method_call("org.freedesktop.ConsoleKit",
132 sid, "org.freedesktop.DBus.Properties", "Set");
133 if (!message)
134 goto out;
135 interface = "org.freedesktop.ConsoleKit.Session";
136 property = "active";
137 if (!dbus_message_append_args(message,
138 DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &property,
139 DBUS_TYPE_INVALID))
140 goto out;
141 dbus_message_iter_init_append(message, &iter);
142 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
143 DBUS_TYPE_BOOLEAN_AS_STRING, &subiter))
144 goto out;
145 active = TRUE;
146 if (!dbus_message_iter_append_basic(&subiter, DBUS_TYPE_BOOLEAN,
147 &active))
148 goto out;
149 if (!dbus_message_iter_close_container(&iter, &subiter))
150 goto out;
151
152 dbus_error_init(&err);
153 reply = dbus_connection_send_with_reply_and_block(connection, message,
154 -1, &err);
155 if (!reply) {
156 if (dbus_error_is_set(&err)) {
157 error("unable to make current session active: %s",
158 err.message);
159 dbus_error_free(&err);
160 }
161 goto out;
162 }
163
164out:
165 if (reply)
166 dbus_message_unref(reply);
167 if (message)
168 dbus_message_unref(message);
169}
170
171/*
172 * We pass display separately rather than using s->display because the
173 * latter is not available in the monitor when using privsep.
174 */
175
176char *
177consolekit_register(Session *s, const char *display)
178{
179 DBusError err;
180 const char *tty = s->tty;
181 const char *remote_host_name;
182 dbus_bool_t is_local = FALSE;
183 const char *cookie = NULL;
184
185 if (s->ckc) {
186 debug("already registered with ConsoleKit");
187 return xstrdup(ck_connector_get_cookie(s->ckc));
188 }
189
190 s->ckc = ck_connector_new();
191 if (!s->ckc) {
192 error("ck_connector_new failed");
193 return NULL;
194 }
195
196 if (!tty)
197 tty = "";
198 if (!display)
199 display = "";
200 remote_host_name = get_remote_name_or_ip(utmp_len, options.use_dns);
201 if (!remote_host_name)
202 remote_host_name = "";
203
204 dbus_error_init(&err);
205 if (!ck_connector_open_session_with_parameters(s->ckc, &err,
206 "unix-user", &s->pw->pw_uid,
207 "display-device", &tty,
208 "x11-display", &display,
209 "remote-host-name", &remote_host_name,
210 "is-local", &is_local,
211 NULL)) {
212 if (dbus_error_is_set(&err)) {
213 debug("%s", err.message);
214 dbus_error_free(&err);
215 } else {
216 debug("insufficient privileges or D-Bus / ConsoleKit "
217 "not available");
218 }
219 return NULL;
220 }
221
222 debug("registered uid=%d on tty='%s' with ConsoleKit",
223 s->pw->pw_uid, s->tty);
224
225 cookie = ck_connector_get_cookie(s->ckc);
226 set_active(cookie);
227 return xstrdup(cookie);
228}
229
230void
231consolekit_unregister(Session *s)
232{
233 if (s->ckc) {
234 debug("unregistering ConsoleKit session %s",
235 ck_connector_get_cookie(s->ckc));
236 ck_connector_unref(s->ckc);
237 s->ckc = NULL;
238 }
239}
240
241#endif /* USE_CONSOLEKIT */