summaryrefslogtreecommitdiff
path: root/consolekit.c
diff options
context:
space:
mode:
Diffstat (limited to 'consolekit.c')
-rw-r--r--consolekit.c240
1 files changed, 240 insertions, 0 deletions
diff --git a/consolekit.c b/consolekit.c
new file mode 100644
index 000000000..f1039e652
--- /dev/null
+++ b/consolekit.c
@@ -0,0 +1,240 @@
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 "servconf.h"
57#include "canohost.h"
58#include "session.h"
59#include "consolekit.h"
60
61extern ServerOptions options;
62extern u_int utmp_len;
63
64void
65set_active(const char *cookie)
66{
67 DBusError err;
68 DBusConnection *connection;
69 DBusMessage *message = NULL, *reply = NULL;
70 char *sid;
71 DBusMessageIter iter, subiter;
72 const char *interface, *property;
73 dbus_bool_t active;
74
75 dbus_error_init(&err);
76 connection = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
77 if (!connection) {
78 if (dbus_error_is_set(&err)) {
79 error("unable to open DBus connection: %s",
80 err.message);
81 dbus_error_free(&err);
82 }
83 goto out;
84 }
85 dbus_connection_set_exit_on_disconnect(connection, FALSE);
86
87 message = dbus_message_new_method_call("org.freedesktop.ConsoleKit",
88 "/org/freedesktop/ConsoleKit/Manager",
89 "org.freedesktop.ConsoleKit.Manager",
90 "GetSessionForCookie");
91 if (!message)
92 goto out;
93 if (!dbus_message_append_args(message, DBUS_TYPE_STRING, &cookie,
94 DBUS_TYPE_INVALID)) {
95 if (dbus_error_is_set(&err)) {
96 error("unable to get current session: %s",
97 err.message);
98 dbus_error_free(&err);
99 }
100 goto out;
101 }
102
103 dbus_error_init(&err);
104 reply = dbus_connection_send_with_reply_and_block(connection, message,
105 -1, &err);
106 if (!reply) {
107 if (dbus_error_is_set(&err)) {
108 error("unable to get current session: %s",
109 err.message);
110 dbus_error_free(&err);
111 }
112 goto out;
113 }
114
115 dbus_error_init(&err);
116 if (!dbus_message_get_args(reply, &err,
117 DBUS_TYPE_OBJECT_PATH, &sid,
118 DBUS_TYPE_INVALID)) {
119 if (dbus_error_is_set(&err)) {
120 error("unable to get current session: %s",
121 err.message);
122 dbus_error_free(&err);
123 }
124 goto out;
125 }
126 dbus_message_unref(reply);
127 dbus_message_unref(message);
128 message = reply = NULL;
129
130 message = dbus_message_new_method_call("org.freedesktop.ConsoleKit",
131 sid, "org.freedesktop.DBus.Properties", "Set");
132 if (!message)
133 goto out;
134 interface = "org.freedesktop.ConsoleKit.Session";
135 property = "active";
136 if (!dbus_message_append_args(message,
137 DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &property,
138 DBUS_TYPE_INVALID))
139 goto out;
140 dbus_message_iter_init_append(message, &iter);
141 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
142 DBUS_TYPE_BOOLEAN_AS_STRING, &subiter))
143 goto out;
144 active = TRUE;
145 if (!dbus_message_iter_append_basic(&subiter, DBUS_TYPE_BOOLEAN,
146 &active))
147 goto out;
148 if (!dbus_message_iter_close_container(&iter, &subiter))
149 goto out;
150
151 dbus_error_init(&err);
152 reply = dbus_connection_send_with_reply_and_block(connection, message,
153 -1, &err);
154 if (!reply) {
155 if (dbus_error_is_set(&err)) {
156 error("unable to make current session active: %s",
157 err.message);
158 dbus_error_free(&err);
159 }
160 goto out;
161 }
162
163out:
164 if (reply)
165 dbus_message_unref(reply);
166 if (message)
167 dbus_message_unref(message);
168}
169
170/*
171 * We pass display separately rather than using s->display because the
172 * latter is not available in the monitor when using privsep.
173 */
174
175char *
176consolekit_register(Session *s, const char *display)
177{
178 DBusError err;
179 const char *tty = s->tty;
180 const char *remote_host_name;
181 dbus_bool_t is_local = FALSE;
182 const char *cookie = NULL;
183
184 if (s->ckc) {
185 debug("already registered with ConsoleKit");
186 return xstrdup(ck_connector_get_cookie(s->ckc));
187 }
188
189 s->ckc = ck_connector_new();
190 if (!s->ckc) {
191 error("ck_connector_new failed");
192 return NULL;
193 }
194
195 if (!tty)
196 tty = "";
197 if (!display)
198 display = "";
199 remote_host_name = get_remote_name_or_ip(utmp_len, options.use_dns);
200 if (!remote_host_name)
201 remote_host_name = "";
202
203 dbus_error_init(&err);
204 if (!ck_connector_open_session_with_parameters(s->ckc, &err,
205 "unix-user", &s->pw->pw_uid,
206 "display-device", &tty,
207 "x11-display", &display,
208 "remote-host-name", &remote_host_name,
209 "is-local", &is_local,
210 NULL)) {
211 if (dbus_error_is_set(&err)) {
212 debug("%s", err.message);
213 dbus_error_free(&err);
214 } else {
215 debug("insufficient privileges or D-Bus / ConsoleKit "
216 "not available");
217 }
218 return NULL;
219 }
220
221 debug("registered uid=%d on tty='%s' with ConsoleKit",
222 s->pw->pw_uid, s->tty);
223
224 cookie = ck_connector_get_cookie(s->ckc);
225 set_active(cookie);
226 return xstrdup(cookie);
227}
228
229void
230consolekit_unregister(Session *s)
231{
232 if (s->ckc) {
233 debug("unregistering ConsoleKit session %s",
234 ck_connector_get_cookie(s->ckc));
235 ck_connector_unref(s->ckc);
236 s->ckc = NULL;
237 }
238}
239
240#endif /* USE_CONSOLEKIT */