polkitunixsession.c 16.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * Copyright (C) 2008 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: David Zeuthen <davidz@redhat.com>
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <string.h>
#include "polkitunixsession.h"
#include "polkitsubject.h"
29
#include "polkiterror.h"
30 31 32 33 34 35 36
#include "polkitprivate.h"

/**
 * SECTION:polkitunixsession
 * @title: PolkitUnixSession
 * @short_description: Unix sessions
 *
David Zeuthen's avatar
David Zeuthen committed
37 38 39
 * An object that represents an user session.
 *
 * The session id is an opaque string obtained from ConsoleKit.
40 41
 */

David Zeuthen's avatar
David Zeuthen committed
42 43 44 45 46
/**
 * PolkitUnixSession:
 *
 * The #PolkitUnixSession struct should not be accessed directly.
 */
47 48 49 50 51
struct _PolkitUnixSession
{
  GObject parent_instance;

  gchar *session_id;
52

53
  gint pid;
54 55 56 57 58 59 60 61 62 63 64
};

struct _PolkitUnixSessionClass
{
  GObjectClass parent_class;
};

enum
{
  PROP_0,
  PROP_SESSION_ID,
65
  PROP_PID,
66 67
};

68 69 70
static void subject_iface_init        (PolkitSubjectIface *subject_iface);
static void initable_iface_init       (GInitableIface *initable_iface);
static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
71 72 73

G_DEFINE_TYPE_WITH_CODE (PolkitUnixSession, polkit_unix_session, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (POLKIT_TYPE_SUBJECT, subject_iface_init)
74 75
                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
                         G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
76 77 78
                         );

static void
79
polkit_unix_session_init (PolkitUnixSession *session)
80 81 82
{
}

83 84 85 86 87 88 89 90 91 92 93
static void
polkit_unix_session_finalize (GObject *object)
{
  PolkitUnixSession *session = POLKIT_UNIX_SESSION (object);

  g_free (session->session_id);

  if (G_OBJECT_CLASS (polkit_unix_session_parent_class)->finalize != NULL)
    G_OBJECT_CLASS (polkit_unix_session_parent_class)->finalize (object);
}

94 95
static void
polkit_unix_session_get_property (GObject    *object,
96 97 98
                                  guint       prop_id,
                                  GValue     *value,
                                  GParamSpec *pspec)
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
{
  PolkitUnixSession *session = POLKIT_UNIX_SESSION (object);

  switch (prop_id)
    {
    case PROP_SESSION_ID:
      g_value_set_string (value, session->session_id);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
polkit_unix_session_set_property (GObject      *object,
116 117 118
                                  guint         prop_id,
                                  const GValue *value,
                                  GParamSpec   *pspec)
119 120 121 122 123 124 125 126 127
{
  PolkitUnixSession *session = POLKIT_UNIX_SESSION (object);

  switch (prop_id)
    {
    case PROP_SESSION_ID:
      polkit_unix_session_set_session_id (session, g_value_get_string (value));
      break;

128
    case PROP_PID:
129
      session->pid = g_value_get_int (value);
130 131
      break;

132 133 134 135 136 137 138 139 140 141 142
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
polkit_unix_session_class_init (PolkitUnixSessionClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

143
  gobject_class->finalize     = polkit_unix_session_finalize;
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
  gobject_class->get_property = polkit_unix_session_get_property;
  gobject_class->set_property = polkit_unix_session_set_property;

  /**
   * PolkitUnixSession:session-id:
   *
   * The UNIX session id.
   */
  g_object_class_install_property (gobject_class,
                                   PROP_SESSION_ID,
                                   g_param_spec_string ("session-id",
                                                        "Session ID",
                                                        "The UNIX session ID",
                                                        NULL,
                                                        G_PARAM_CONSTRUCT |
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_STATIC_NAME |
                                                        G_PARAM_STATIC_BLURB |
                                                        G_PARAM_STATIC_NICK));

164 165 166 167 168 169 170 171

  /**
   * PolkitUnixSession:pid:
   *
   * The UNIX process id to look up the session.
   */
  g_object_class_install_property (gobject_class,
                                   PROP_PID,
172 173 174 175 176 177 178 179 180 181 182
                                   g_param_spec_int ("pid",
                                                     "Process ID",
                                                     "Process ID to use for looking up the session",
                                                     0,
                                                     G_MAXINT,
                                                     0,
                                                     G_PARAM_CONSTRUCT_ONLY |
                                                     G_PARAM_WRITABLE |
                                                     G_PARAM_STATIC_NAME |
                                                     G_PARAM_STATIC_BLURB |
                                                     G_PARAM_STATIC_NICK));
183

184 185
}

David Zeuthen's avatar
David Zeuthen committed
186 187 188 189 190 191 192 193 194
/**
 * polkit_unix_session_get_session_id:
 * @session: A #PolkitUnixSession.
 *
 * Gets the session id for @session.
 *
 * Returns: The session id for @session. Do not free this string, it
 * is owned by @session.
 **/
195 196 197
const gchar *
polkit_unix_session_get_session_id (PolkitUnixSession *session)
{
198
  g_return_val_if_fail (POLKIT_IS_UNIX_SESSION (session), NULL);
199 200 201
  return session->session_id;
}

David Zeuthen's avatar
David Zeuthen committed
202 203 204 205 206 207 208
/**
 * polkit_unix_session_set_session_id:
 * @session: A #PolkitUnixSession.
 * @session_id: The session id.
 *
 * Sets the session id for @session to @session_id.
 **/
209 210 211 212
void
polkit_unix_session_set_session_id (PolkitUnixSession *session,
                                    const gchar       *session_id)
{
213 214
  g_return_if_fail (POLKIT_IS_UNIX_SESSION (session));
  /*g_return_if_fail (session_id != NULL);*/
215 216 217 218
  g_free (session->session_id);
  session->session_id = g_strdup (session_id);
}

David Zeuthen's avatar
David Zeuthen committed
219 220 221 222 223 224
/**
 * polkit_unix_session_new:
 * @session_id: The session id.
 *
 * Creates a new #PolkitUnixSession for @session_id.
 *
225
 * Returns: (transfer full): A #PolkitUnixSession. Free with g_object_unref().
David Zeuthen's avatar
David Zeuthen committed
226
 **/
227 228 229 230 231 232 233 234
PolkitSubject *
polkit_unix_session_new (const gchar *session_id)
{
  return POLKIT_SUBJECT (g_object_new (POLKIT_TYPE_UNIX_SESSION,
                                       "session-id", session_id,
                                       NULL));
}

235 236 237
/**
 * polkit_unix_session_new_for_process:
 * @pid: The process id of the process to get the session for.
David Zeuthen's avatar
David Zeuthen committed
238
 * @cancellable: (allow-none): A #GCancellable or %NULL.
239 240 241 242 243 244
 * @callback: A #GAsyncReadyCallback to call when the request is satisfied
 * @user_data: The data to pass to @callback.
 *
 * Asynchronously creates a new #PolkitUnixSession object for the
 * process with process id @pid.
 *
David Zeuthen's avatar
David Zeuthen committed
245 246 247 248 249 250
 * When the operation is finished, @callback will be invoked in the
 * <link linkend="g-main-context-push-thread-default">thread-default
 * main loop</link> of the thread you are calling this method
 * from. You can then call
 * polkit_unix_session_new_for_process_finish() to get the result of
 * the operation.
251 252 253 254 255
 *
 * This method constructs the object asynchronously, for the synchronous and blocking version
 * use polkit_unix_session_new_for_process_sync().
 **/
void
256
polkit_unix_session_new_for_process (gint                pid,
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
                                     GCancellable       *cancellable,
                                     GAsyncReadyCallback callback,
                                     gpointer            user_data)
{
  g_async_initable_new_async (POLKIT_TYPE_UNIX_SESSION,
                              G_PRIORITY_DEFAULT,
                              cancellable,
                              callback,
                              user_data,
                              "pid", pid,
                              NULL);
}

/**
 * polkit_unix_session_new_for_process_finish:
 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to polkit_unix_session_new_for_process().
David Zeuthen's avatar
David Zeuthen committed
273
 * @error: (allow-none): Return location for error.
274 275 276
 *
 * Finishes constructing a #PolkitSubject for a process id.
 *
277
 * Returns: (transfer full) (allow-none): A #PolkitUnixSession for the @pid passed to
David Zeuthen's avatar
David Zeuthen committed
278 279
 *     polkit_unix_session_new_for_process() or %NULL if @error is
 *     set. Free with g_object_unref().
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
 **/
PolkitSubject *
polkit_unix_session_new_for_process_finish (GAsyncResult   *res,
                                            GError        **error)
{
  GObject *object;
  GObject *source_object;

  source_object = g_async_result_get_source_object (res);
  g_assert (source_object != NULL);

  object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
                                        res,
                                        error);
  g_object_unref (source_object);

  if (object != NULL)
    return POLKIT_SUBJECT (object);
  else
    return NULL;
}


/**
 * polkit_unix_session_new_for_process_sync:
 * @pid: The process id of the process to get the session for.
David Zeuthen's avatar
David Zeuthen committed
306 307
 * @cancellable: (allow-none): A #GCancellable or %NULL.
 * @error: (allow-none): Return location for error.
308 309 310
 *
 * Creates a new #PolkitUnixSession for the process with process id @pid.
 *
David Zeuthen's avatar
David Zeuthen committed
311 312
 * This is a synchronous call - the calling thread is blocked until a
 * reply is received. For the asynchronous version, see
313 314
 * polkit_unix_session_new_for_process().
 *
315 316
 * Returns: (allow-none) (transfer full): A #PolkitUnixSession for
 * @pid or %NULL if @error is set. Free with g_object_unref().
317 318
 **/
PolkitSubject *
319
polkit_unix_session_new_for_process_sync (gint           pid,
320 321 322 323 324 325 326 327 328 329
                                          GCancellable  *cancellable,
                                          GError       **error)
{
  return POLKIT_SUBJECT (g_initable_new (POLKIT_TYPE_UNIX_SESSION,
                                         cancellable,
                                         error,
                                         "pid", pid,
                                         NULL));
}

330 331 332 333 334 335 336 337
static guint
polkit_unix_session_hash (PolkitSubject *subject)
{
  PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);

  return g_str_hash (session->session_id);
}

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
static gboolean
polkit_unix_session_equal (PolkitSubject *a,
                           PolkitSubject *b)
{
  PolkitUnixSession *session_a;
  PolkitUnixSession *session_b;

  session_a = POLKIT_UNIX_SESSION (a);
  session_b = POLKIT_UNIX_SESSION (b);

  return g_strcmp0 (session_a->session_id, session_b->session_id) == 0;
}

static gchar *
polkit_unix_session_to_string (PolkitSubject *subject)
{
  PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);

  return g_strdup_printf ("unix-session:%s", session->session_id);
}

David Zeuthen's avatar
David Zeuthen committed
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
static gboolean
polkit_unix_session_exists_sync (PolkitSubject   *subject,
                                    GCancellable    *cancellable,
                                    GError         **error)
{
  PolkitUnixSession *session = POLKIT_UNIX_SESSION (subject);
  GDBusConnection *connection;
  GVariant *result;
  gboolean ret;

  ret = FALSE;

  connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
  if (connection == NULL)
    goto out;

  result = g_dbus_connection_call_sync (connection,
                                        "org.freedesktop.ConsoleKit",           /* name */
                                        session->session_id,                    /* object path */
                                        "org.freedesktop.ConsoleKit.Session",   /* interface name */
                                        "GetUser",                              /* method */
                                        NULL, /* parameters */
                                        G_VARIANT_TYPE ("(u)"),
                                        G_DBUS_CALL_FLAGS_NONE,
                                        -1,
                                        cancellable,
                                        error);
  if (result == NULL)
    goto out;

  ret = TRUE;
  g_variant_unref (result);

 out:
  if (connection != NULL)
    g_object_unref (connection);
  return ret;
}

398
static void
David Zeuthen's avatar
David Zeuthen committed
399 400 401
exists_in_thread_func (GSimpleAsyncResult *res,
                       GObject            *object,
                       GCancellable       *cancellable)
402 403 404
{
  GError *error;
  error = NULL;
David Zeuthen's avatar
David Zeuthen committed
405 406 407
  if (!polkit_unix_session_exists_sync (POLKIT_SUBJECT (object),
                                        cancellable,
                                        &error))
408
    {
David Zeuthen's avatar
David Zeuthen committed
409
      g_simple_async_result_set_from_error (res, error);
410 411 412 413 414 415 416 417 418 419 420 421
      g_error_free (error);
    }
}

static void
polkit_unix_session_exists (PolkitSubject       *subject,
                            GCancellable        *cancellable,
                            GAsyncReadyCallback  callback,
                            gpointer             user_data)
{
  GSimpleAsyncResult *simple;

David Zeuthen's avatar
David Zeuthen committed
422
  g_return_if_fail (POLKIT_IS_UNIX_SESSION (subject));
423

David Zeuthen's avatar
David Zeuthen committed
424
  simple = g_simple_async_result_new (G_OBJECT (subject),
425 426 427
                                      callback,
                                      user_data,
                                      polkit_unix_session_exists);
David Zeuthen's avatar
David Zeuthen committed
428 429 430 431 432
  g_simple_async_result_run_in_thread (simple,
                                       exists_in_thread_func,
                                       G_PRIORITY_DEFAULT,
                                       cancellable);
  g_object_unref (simple);
433 434 435 436
}

static gboolean
polkit_unix_session_exists_finish (PolkitSubject  *subject,
David Zeuthen's avatar
David Zeuthen committed
437 438
                                      GAsyncResult   *res,
                                      GError        **error)
439 440 441 442 443 444 445 446 447 448 449
{
  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
  gboolean ret;

  g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == polkit_unix_session_exists);

  ret = FALSE;

  if (g_simple_async_result_propagate_error (simple, error))
    goto out;

David Zeuthen's avatar
David Zeuthen committed
450
  ret = g_simple_async_result_get_op_res_gboolean (simple);
451 452 453 454 455

 out:
  return ret;
}

456 457 458
static void
subject_iface_init (PolkitSubjectIface *subject_iface)
{
459 460 461 462 463 464 465 466 467 468 469 470 471 472
  subject_iface->hash          = polkit_unix_session_hash;
  subject_iface->equal         = polkit_unix_session_equal;
  subject_iface->to_string     = polkit_unix_session_to_string;
  subject_iface->exists        = polkit_unix_session_exists;
  subject_iface->exists_finish = polkit_unix_session_exists_finish;
  subject_iface->exists_sync   = polkit_unix_session_exists_sync;
}

static gboolean
polkit_unix_session_initable_init (GInitable     *initable,
                                   GCancellable  *cancellable,
                                   GError       **error)
{
  PolkitUnixSession *session = POLKIT_UNIX_SESSION (initable);
David Zeuthen's avatar
David Zeuthen committed
473 474
  GDBusConnection *connection;
  GVariant *result;
475 476 477 478 479
  gboolean ret;

  connection = NULL;
  ret = FALSE;

David Zeuthen's avatar
David Zeuthen committed
480 481 482 483 484 485 486 487 488
  if (session->session_id != NULL)
    {
      /* already set, nothing to do */
      ret = TRUE;
      goto out;
    }

  connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
  if (connection == NULL)
489 490
    goto out;

David Zeuthen's avatar
David Zeuthen committed
491 492 493 494 495 496 497 498 499 500 501 502
  result = g_dbus_connection_call_sync (connection,
                                        "org.freedesktop.ConsoleKit",           /* name */
                                        "/org/freedesktop/ConsoleKit/Manager",  /* object path */
                                        "org.freedesktop.ConsoleKit.Manager",   /* interface name */
                                        "GetSessionForUnixProcess",             /* method */
                                        g_variant_new ("(u)", session->pid),    /* parameters */
                                        G_VARIANT_TYPE ("(o)"),
                                        G_DBUS_CALL_FLAGS_NONE,
                                        -1,
                                        cancellable,
                                        error);
  if (result == NULL)
503 504
    goto out;

David Zeuthen's avatar
David Zeuthen committed
505 506 507
  g_variant_get (result, "(o)", &session->session_id);
  g_variant_unref (result);

508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
  ret = TRUE;

 out:
  if (connection != NULL)
    g_object_unref (connection);

  return ret;
}

static void
initable_iface_init (GInitableIface *initable_iface)
{
  initable_iface->init = polkit_unix_session_initable_init;
}

static void
async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
{
David Zeuthen's avatar
David Zeuthen committed
526
  /* use default implementation to run GInitable code in a thread */
527
}