| 1 |
/* |
| 2 |
* Copyright (C) 2008 Red Hat, Inc. |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General |
| 15 |
* Public License along with this library; if not, write to the |
| 16 |
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
| 17 |
* Boston, MA 02111-1307, USA. |
| 18 |
* |
| 19 |
* Author: David Zeuthen <[email protected]> |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <string.h> |
| 23 |
#include "polkitunixsession.h" |
| 24 |
#include "polkitsubject.h" |
| 25 |
#include "polkiterror.h" |
| 26 |
#include "polkitprivate.h" |
| 27 |
|
| 28 |
/** |
| 29 |
* SECTION:polkitunixsession |
| 30 |
* @title: PolkitUnixSession |
| 31 |
* @short_description: Unix sessions |
| 32 |
* |
| 33 |
* An object that represents an user session. |
| 34 |
* |
| 35 |
* The session id is an opaque string obtained from ConsoleKit. |
| 36 |
*/ |
| 37 |
|
| 38 |
/** |
| 39 |
* PolkitUnixSession: |
| 40 |
* |
| 41 |
* The #PolkitUnixSession struct should not be accessed directly. |
| 42 |
*/ |
| 43 |
struct _PolkitUnixSession |
| 44 |
{ |
| 45 |
GObject parent_instance; |
| 46 |
|
| 47 |
gchar *session_id; |
| 48 |
|
| 49 |
gint pid; |
| 50 |
}; |
| 51 |
|
| 52 |
struct _PolkitUnixSessionClass |
| 53 |
{ |
| 54 |
GObjectClass parent_class; |
| 55 |
}; |
| 56 |
|
| 57 |
enum |
| 58 |
{ |
| 59 |
PROP_0, |
| 60 |
PROP_SESSION_ID, |
| 61 |
PROP_PID, |
| 62 |
}; |
| 63 |
|
| 64 |
static void subject_iface_init (PolkitSubjectIface *subject_iface); |
| 65 |
static void initable_iface_init (GInitableIface *initable_iface); |
| 66 |
static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface); |
| 67 |
|
| 68 |
G_DEFINE_TYPE_WITH_CODE (PolkitUnixSession, polkit_unix_session, G_TYPE_OBJECT, |
| 69 |
G_IMPLEMENT_INTERFACE (POLKIT_TYPE_SUBJECT, subject_iface_init) |
| 70 |
G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init) |
| 71 |
G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init) |
| 72 |
); |
| 73 |
|
| 74 |
static void |
| 75 |
polkit_unix_session_init (PolkitUnixSession *session) |
| 76 |
{ |
| 77 |
} |
| 78 |
|
| 79 |
static void |
| 80 |
polkit_unix_session_finalize (GObject *object) |
| 81 |
{ |
| 82 |
PolkitUnixSession *session = POLKIT_UNIX_SESSION (object); |
| 83 |
|
| 84 |
g_free (session->session_id); |
| 85 |
|
| 86 |
if (G_OBJECT_CLASS (polkit_unix_session_parent_class)->finalize != NULL) |
| 87 |
G_OBJECT_CLASS (polkit_unix_session_parent_class)->finalize (object); |
| 88 |
} |
| 89 |
|
| 90 |
static void |
| 91 |
polkit_unix_session_get_property (GObject *object, |
| 92 |
guint prop_id, |
| 93 |
GValue *value, |
| 94 |
GParamSpec *pspec) |
| 95 |
{ |
| 96 |
PolkitUnixSession *session = POLKIT_UNIX_SESSION (object); |
| 97 |
|
| 98 |
switch (prop_id) |
| 99 |
{ |
| 100 |
case PROP_SESSION_ID: |
| 101 |
g_value_set_string (value, session->session_id); |
| 102 |
break; |
| 103 |
|
| 104 |
default: |
| 105 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 106 |
break; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
static void |
| 111 |
polkit_unix_session_set_property (GObject *object, |
| 112 |
guint prop_id, |
| 113 |
const GValue *value, |
| 114 |
GParamSpec *pspec) |
| 115 |
{ |
| 116 |
PolkitUnixSession *session = POLKIT_UNIX_SESSION (object); |
| 117 |
|
| 118 |
switch (prop_id) |
| 119 |
{ |
| 120 |
case PROP_SESSION_ID: |
| 121 |
polkit_unix_session_set_session_id (session, g_value_get_string (value)); |
| 122 |
break; |
| 123 |
|
| 124 |
case PROP_PID: |
| 125 |
session->pid = g_value_get_int (value); |
| 126 |
break; |
| 127 |
|
| 128 |
default: |
| 129 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 130 |
break; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
static void |
| 135 |
polkit_unix_session_class_init (PolkitUnixSessionClass *klass) |
| 136 |
{ |
| 137 |
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
| 138 |
|
| 139 |
gobject_class->finalize = polkit_unix_session_finalize; |
| 140 |
gobject_class->get_property = polkit_unix_session_get_property; |
| 141 |
gobject_class->set_property = polkit_unix_session_set_property; |
| 142 |
|
| 143 |
/** |
| 144 |
* PolkitUnixSession:session-id: |
| 145 |
* |
| 146 |
* The UNIX session id. |
| 147 |
*/ |
| 148 |
g_object_class_install_property (gobject_class, |
| 149 |
PROP_SESSION_ID, |
| 150 |
g_param_spec_string ("session-id", |
| 151 |
"Session ID", |
| 152 |
"The UNIX session ID", |
| 153 |
NULL, |
| 154 |
G_PARAM_CONSTRUCT | |
| 155 |
G_PARAM_READWRITE | |
| 156 |
G_PARAM_STATIC_NAME | |
| 157 |
G_PARAM_STATIC_BLURB | |
| 158 |
G_PARAM_STATIC_NICK)); |
| 159 |
|
| 160 |
|
| 161 |
/** |
| 162 |
* PolkitUnixSession:pid: |
| 163 |
* |
| 164 |
* The UNIX process id to look up the session. |
| 165 |
*/ |
| 166 |
g_object_class_install_property (gobject_class, |
| 167 |
PROP_PID, |
| 168 |
g_param_spec_int ("pid", |
| 169 |
"Process ID", |
| 170 |
"Process ID to use for looking up the session", |
| 171 |
0, |
| 172 |
G_MAXINT, |
| 173 |
0, |
| 174 |
G_PARAM_CONSTRUCT_ONLY | |
| 175 |
G_PARAM_WRITABLE | |
| 176 |
G_PARAM_STATIC_NAME | |
| 177 |
G_PARAM_STATIC_BLURB | |
| 178 |
G_PARAM_STATIC_NICK)); |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* polkit_unix_session_get_session_id: |
| 184 |
* @session: A #PolkitUnixSession. |
| 185 |
* |
| 186 |
* Gets the session id for @session. |
| 187 |
* |
| 188 |
* Returns: The session id for @session. Do not free this string, it |
| 189 |
* is owned by @session. |
| 190 |
**/ |
| 191 |
const gchar * |
| 192 |
polkit_unix_session_get_session_id (PolkitUnixSession *session) |
| 193 |
{ |
| 194 |
g_return_val_if_fail (POLKIT_IS_UNIX_SESSION (session), NULL); |
| 195 |
return session->session_id; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* polkit_unix_session_set_session_id: |
| 200 |
* @session: A #PolkitUnixSession. |
| 201 |
* @session_id: The session id. |
| 202 |
* |
| 203 |
* Sets the session id for @session to @session_id. |
| 204 |
**/ |
| 205 |
void |
| 206 |
polkit_unix_session_set_session_id (PolkitUnixSession *session, |
| 207 |
const gchar *session_id) |
| 208 |
{ |
| 209 |
g_return_if_fail (POLKIT_IS_UNIX_SESSION (session)); |
| 210 |
/*g_return_if_fail (session_id != NULL);*/ |
| 211 |
g_free (session->session_id); |
| 212 |
session->session_id = g_strdup (session_id); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* polkit_unix_session_new: |
| 217 |
* @session_id: The session id. |
| 218 |
* |
| 219 |
* Creates a new #PolkitUnixSession for @session_id. |
| 220 |
* |
| 221 |
* Returns: (transfer full): A #PolkitUnixSession. Free with g_object_unref(). |
| 222 |
**/ |
| 223 |
PolkitSubject * |
| 224 |
polkit_unix_session_new (const gchar *session_id) |
| 225 |
{ |
| 226 |
return POLKIT_SUBJECT (g_object_new (POLKIT_TYPE_UNIX_SESSION, |
| 227 |
"session-id", session_id, |
| 228 |
NULL)); |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* polkit_unix_session_new_for_process: |
| 233 |
* @pid: The process id of the process to get the session for. |
| 234 |
* @cancellable: (allow-none): A #GCancellable or %NULL. |
| 235 |
* @callback: A #GAsyncReadyCallback to call when the request is satisfied |
| 236 |
* @user_data: The data to pass to @callback. |
| 237 |
* |
| 238 |
* Asynchronously creates a new #PolkitUnixSession object for the |
| 239 |
* process with process id @pid. |
| 240 |
* |
| 241 |
* When the operation is finished, @callback will be invoked in the |
| 242 |
* <link linkend="g-main-context-push-thread-default">thread-default |
| 243 |
* main loop</link> of the thread you are calling this method |
| 244 |
* from. You can then call |
| 245 |
* polkit_unix_session_new_for_process_finish() to get the result of |
| 246 |
* the operation. |
| 247 |
* |
| 248 |
* This method constructs the object asynchronously, for the synchronous and blocking version |
| 249 |
* use polkit_unix_session_new_for_process_sync(). |
| 250 |
**/ |
| 251 |
void |
| 252 |
polkit_unix_session_new_for_process (gint pid, |
| 253 |
GCancellable *cancellable, |
| 254 |
GAsyncReadyCallback callback, |
| 255 |
gpointer user_data) |
| 256 |
{ |
| 257 |
g_async_initable_new_async (POLKIT_TYPE_UNIX_SESSION, |
| 258 |
G_PRIORITY_DEFAULT, |
| 259 |
cancellable, |
| 260 |
callback, |
| 261 |
user_data, |
| 262 |
"pid", pid, |
| 263 |
NULL); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* polkit_unix_session_new_for_process_finish: |
| 268 |
* @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to polkit_unix_session_new_for_process(). |
| 269 |
* @error: (allow-none): Return location for error. |
| 270 |
* |
| 271 |
* Finishes constructing a #PolkitSubject for a process id. |
| 272 |
* |
| 273 |
* Returns: (transfer full) (allow-none): A #PolkitUnixSession for the @pid passed to |
| 274 |
* polkit_unix_session_new_for_process() or %NULL if @error is |
| 275 |
* set. Free with g_object_unref(). |
| 276 |
**/ |
| 277 |
PolkitSubject * |
| 278 |
polkit_unix_session_new_for_process_finish (GAsyncResult *res, |
| 279 |
GError **error) |
| 280 |
{ |
| 281 |
GObject *object; |
| 282 |
GObject *source_object; |
| 283 |
|
| 284 |
source_object = g_async_result_get_source_object (res); |
| 285 |
g_assert (source_object != NULL); |
| 286 |
|
| 287 |
object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object), |
| 288 |
res, |
| 289 |
error); |
| 290 |
g_object_unref (source_object); |
| 291 |
|
| 292 |
if (object != NULL) |
| 293 |
return POLKIT_SUBJECT (object); |
| 294 |
else |
| 295 |
return NULL; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
/** |
| 300 |
* polkit_unix_session_new_for_process_sync: |
| 301 |
* @pid: The process id of the process to get the session for. |
| 302 |
* @cancellable: (allow-none): A #GCancellable or %NULL. |
| 303 |
* @error: (allow-none): Return location for error. |
| 304 |
* |
| 305 |
* Creates a new #PolkitUnixSession for the process with process id @pid. |
| 306 |
* |
| 307 |
* This is a synchronous call - the calling thread is blocked until a |
| 308 |
* reply is received. For the asynchronous version, see |
| 309 |
* polkit_unix_session_new_for_process(). |
| 310 |
* |
| 311 |
* Returns: (allow-none) (transfer full): A #PolkitUnixSession for |
| 312 |
* @pid or %NULL if @error is set. Free with g_object_unref(). |
| 313 |
**/ |
| 314 |
PolkitSubject * |
| 315 |
polkit_unix_session_new_for_process_sync (gint pid, |
| 316 |
GCancellable *cancellable, |
| 317 |
GError **error) |
| 318 |
{ |
| 319 |
return POLKIT_SUBJECT (g_initable_new (POLKIT_TYPE_UNIX_SESSION, |
| 320 |
cancellable, |
| 321 |
error, |
| 322 |
"pid", pid, |
| 323 |
NULL)); |
| 324 |
} |
| 325 |
|
| 326 |
static guint |
| 327 |
polkit_unix_session_hash (PolkitSubject *subject) |
| 328 |
{ |
| 329 |
PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject); |
| 330 |
|
| 331 |
return g_str_hash (session->session_id); |
| 332 |
} |
| 333 |
|
| 334 |
static gboolean |
| 335 |
polkit_unix_session_equal (PolkitSubject *a, |
| 336 |
PolkitSubject *b) |
| 337 |
{ |
| 338 |
PolkitUnixSession *session_a; |
| 339 |
PolkitUnixSession *session_b; |
| 340 |
|
| 341 |
session_a = POLKIT_UNIX_SESSION (a); |
| 342 |
session_b = POLKIT_UNIX_SESSION (b); |
| 343 |
|
| 344 |
return g_strcmp0 (session_a->session_id, session_b->session_id) == 0; |
| 345 |
} |
| 346 |
|
| 347 |
static gchar * |
| 348 |
polkit_unix_session_to_string (PolkitSubject *subject) |
| 349 |
{ |
| 350 |
PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject); |
| 351 |
|
| 352 |
return g_strdup_printf ("unix-session:%s", session->session_id); |
| 353 |
} |
| 354 |
|
| 355 |
static gboolean |
| 356 |
polkit_unix_session_exists_sync (PolkitSubject *subject, |
| 357 |
GCancellable *cancellable, |
| 358 |
GError **error) |
| 359 |
{ |
| 360 |
PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject); |
| 361 |
GDBusConnection *connection; |
| 362 |
GVariant *result; |
| 363 |
gboolean ret; |
| 364 |
|
| 365 |
ret = FALSE; |
| 366 |
|
| 367 |
connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error); |
| 368 |
if (connection == NULL) |
| 369 |
goto out; |
| 370 |
|
| 371 |
result = g_dbus_connection_call_sync (connection, |
| 372 |
"org.freedesktop.ConsoleKit", /* name */ |
| 373 |
session->session_id, /* object path */ |
| 374 |
"org.freedesktop.ConsoleKit.Session", /* interface name */ |
| 375 |
"GetUser", /* method */ |
| 376 |
NULL, /* parameters */ |
| 377 |
G_VARIANT_TYPE ("(u)"), |
| 378 |
G_DBUS_CALL_FLAGS_NONE, |
| 379 |
-1, |
| 380 |
cancellable, |
| 381 |
error); |
| 382 |
if (result == NULL) |
| 383 |
goto out; |
| 384 |
|
| 385 |
ret = TRUE; |
| 386 |
g_variant_unref (result); |
| 387 |
|
| 388 |
out: |
| 389 |
if (connection != NULL) |
| 390 |
g_object_unref (connection); |
| 391 |
return ret; |
| 392 |
} |
| 393 |
|
| 394 |
static void |
| 395 |
exists_in_thread_func (GSimpleAsyncResult *res, |
| 396 |
GObject *object, |
| 397 |
GCancellable *cancellable) |
| 398 |
{ |
| 399 |
GError *error; |
| 400 |
error = NULL; |
| 401 |
if (!polkit_unix_session_exists_sync (POLKIT_SUBJECT (object), |
| 402 |
cancellable, |
| 403 |
&error)) |
| 404 |
{ |
| 405 |
g_simple_async_result_set_from_error (res, error); |
| 406 |
g_error_free (error); |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
static void |
| 411 |
polkit_unix_session_exists (PolkitSubject *subject, |
| 412 |
GCancellable *cancellable, |
| 413 |
GAsyncReadyCallback callback, |
| 414 |
gpointer user_data) |
| 415 |
{ |
| 416 |
GSimpleAsyncResult *simple; |
| 417 |
|
| 418 |
g_return_if_fail (POLKIT_IS_UNIX_SESSION (subject)); |
| 419 |
|
| 420 |
simple = g_simple_async_result_new (G_OBJECT (subject), |
| 421 |
callback, |
| 422 |
user_data, |
| 423 |
polkit_unix_session_exists); |
| 424 |
g_simple_async_result_run_in_thread (simple, |
| 425 |
exists_in_thread_func, |
| 426 |
G_PRIORITY_DEFAULT, |
| 427 |
cancellable); |
| 428 |
g_object_unref (simple); |
| 429 |
} |
| 430 |
|
| 431 |
static gboolean |
| 432 |
polkit_unix_session_exists_finish (PolkitSubject *subject, |
| 433 |
GAsyncResult *res, |
| 434 |
GError **error) |
| 435 |
{ |
| 436 |
GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res); |
| 437 |
gboolean ret; |
| 438 |
|
| 439 |
g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == polkit_unix_session_exists); |
| 440 |
|
| 441 |
ret = FALSE; |
| 442 |
|
| 443 |
if (g_simple_async_result_propagate_error (simple, error)) |
| 444 |
goto out; |
| 445 |
|
| 446 |
ret = g_simple_async_result_get_op_res_gboolean (simple); |
| 447 |
|
| 448 |
out: |
| 449 |
return ret; |
| 450 |
} |
| 451 |
|
| 452 |
static void |
| 453 |
subject_iface_init (PolkitSubjectIface *subject_iface) |
| 454 |
{ |
| 455 |
subject_iface->hash = polkit_unix_session_hash; |
| 456 |
subject_iface->equal = polkit_unix_session_equal; |
| 457 |
subject_iface->to_string = polkit_unix_session_to_string; |
| 458 |
subject_iface->exists = polkit_unix_session_exists; |
| 459 |
subject_iface->exists_finish = polkit_unix_session_exists_finish; |
| 460 |
subject_iface->exists_sync = polkit_unix_session_exists_sync; |
| 461 |
} |
| 462 |
|
| 463 |
static gboolean |
| 464 |
polkit_unix_session_initable_init (GInitable *initable, |
| 465 |
GCancellable *cancellable, |
| 466 |
GError **error) |
| 467 |
{ |
| 468 |
PolkitUnixSession *session = POLKIT_UNIX_SESSION (initable); |
| 469 |
GDBusConnection *connection; |
| 470 |
GVariant *result; |
| 471 |
gboolean ret; |
| 472 |
|
| 473 |
connection = NULL; |
| 474 |
ret = FALSE; |
| 475 |
|
| 476 |
if (session->session_id != NULL) |
| 477 |
{ |
| 478 |
/* already set, nothing to do */ |
| 479 |
ret = TRUE; |
| 480 |
goto out; |
| 481 |
} |
| 482 |
|
| 483 |
connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error); |
| 484 |
if (connection == NULL) |
| 485 |
goto out; |
| 486 |
|
| 487 |
result = g_dbus_connection_call_sync (connection, |
| 488 |
"org.freedesktop.ConsoleKit", /* name */ |
| 489 |
"/org/freedesktop/ConsoleKit/Manager", /* object path */ |
| 490 |
"org.freedesktop.ConsoleKit.Manager", /* interface name */ |
| 491 |
"GetSessionForUnixProcess", /* method */ |
| 492 |
g_variant_new ("(u)", session->pid), /* parameters */ |
| 493 |
G_VARIANT_TYPE ("(o)"), |
| 494 |
G_DBUS_CALL_FLAGS_NONE, |
| 495 |
-1, |
| 496 |
cancellable, |
| 497 |
error); |
| 498 |
if (result == NULL) |
| 499 |
goto out; |
| 500 |
|
| 501 |
g_variant_get (result, "(o)", &session->session_id); |
| 502 |
g_variant_unref (result); |
| 503 |
|
| 504 |
ret = TRUE; |
| 505 |
|
| 506 |
out: |
| 507 |
if (connection != NULL) |
| 508 |
g_object_unref (connection); |
| 509 |
|
| 510 |
return ret; |
| 511 |
} |
| 512 |
|
| 513 |
static void |
| 514 |
initable_iface_init (GInitableIface *initable_iface) |
| 515 |
{ |
| 516 |
initable_iface->init = polkit_unix_session_initable_init; |
| 517 |
} |
| 518 |
|
| 519 |
static void |
| 520 |
async_initable_iface_init (GAsyncInitableIface *async_initable_iface) |
| 521 |
{ |
| 522 |
/* use default implementation to run GInitable code in a thread */ |
| 523 |
} |