polkitsubject.c 14.9 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
/*
 * 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

26
#include <string.h>
27
#include <stdio.h>
28

29
#include "polkitsubject.h"
30
#include "polkitunixprocess.h"
31
#include "polkitunixsession.h"
32
#include "polkitsystembusname.h"
33
#include "polkiterror.h"
34
#include "polkitprivate.h"
35

David Zeuthen's avatar
David Zeuthen committed
36 37 38
/**
 * SECTION:polkitsubject
 * @title: PolkitSubject
David Zeuthen's avatar
David Zeuthen committed
39
 * @short_description: Type for representing subjects
David Zeuthen's avatar
David Zeuthen committed
40
 *
David Zeuthen's avatar
David Zeuthen committed
41 42
 * #PolkitSubject is an abstract type for representing one or more
 * processes.
David Zeuthen's avatar
David Zeuthen committed
43 44
 */

45 46 47 48 49 50 51 52
static void
base_init (gpointer g_iface)
{
}

GType
polkit_subject_get_type (void)
{
53
  static volatile gsize g_define_type_id__volatile = 0;
54

55
  if (g_once_init_enter (&g_define_type_id__volatile))
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    {
      static const GTypeInfo info =
      {
        sizeof (PolkitSubjectIface),
        base_init,              /* base_init      */
        NULL,                   /* base_finalize  */
        NULL,                   /* class_init     */
        NULL,                   /* class_finalize */
        NULL,                   /* class_data     */
        0,                      /* instance_size  */
        0,                      /* n_preallocs    */
        NULL,                   /* instance_init  */
        NULL                    /* value_table    */
      };

71 72
      GType iface_type =
        g_type_register_static (G_TYPE_INTERFACE, "PolkitSubject", &info, 0);
73 74

      g_type_interface_add_prerequisite (iface_type, G_TYPE_OBJECT);
75
      g_once_init_leave (&g_define_type_id__volatile, iface_type);
76 77
    }

78
  return g_define_type_id__volatile;
79 80
}

David Zeuthen's avatar
David Zeuthen committed
81 82 83 84 85 86 87 88
/**
 * polkit_subject_hash:
 * @subject: A #PolkitSubject.
 *
 * Gets a hash code for @subject that can be used with e.g. g_hash_table_new().
 *
 * Returns: A hash code.
 */
89 90 91
guint
polkit_subject_hash (PolkitSubject *subject)
{
92
  g_return_val_if_fail (POLKIT_IS_SUBJECT (subject), 0);
93 94 95
  return POLKIT_SUBJECT_GET_IFACE (subject)->hash (subject);
}

David Zeuthen's avatar
David Zeuthen committed
96 97 98 99 100 101 102 103 104 105 106
/**
 * polkit_subject_equal:
 * @a: A #PolkitSubject.
 * @b: A #PolkitSubject.
 *
 * Checks if @a and @b are equal, ie. represent the same subject.
 *
 * This function can be used in e.g. g_hash_table_new().
 *
 * Returns: %TRUE if @a and @b are equal, %FALSE otherwise.
 */
107 108 109 110
gboolean
polkit_subject_equal (PolkitSubject *a,
                      PolkitSubject *b)
{
111 112 113
  g_return_val_if_fail (POLKIT_IS_SUBJECT (a), FALSE);
  g_return_val_if_fail (POLKIT_IS_SUBJECT (b), FALSE);

114 115 116 117 118 119
  if (!g_type_is_a (G_TYPE_FROM_INSTANCE (a), G_TYPE_FROM_INSTANCE (b)))
    return FALSE;

  return POLKIT_SUBJECT_GET_IFACE (a)->equal (a, b);
}

David Zeuthen's avatar
David Zeuthen committed
120 121 122 123 124 125 126 127 128
/**
 * polkit_subject_to_string:
 * @subject: A #PolkitSubject.
 *
 * Serializes @subject to a string that can be used in
 * polkit_subject_from_string().
 *
 * Returns: A string representing @subject. Free with g_free().
 */
129 130 131
gchar *
polkit_subject_to_string (PolkitSubject *subject)
{
132
  g_return_val_if_fail (POLKIT_IS_SUBJECT (subject), NULL);
133 134 135
  return POLKIT_SUBJECT_GET_IFACE (subject)->to_string (subject);
}

136 137 138
/**
 * polkit_subject_exists:
 * @subject: A #PolkitSubject.
David Zeuthen's avatar
David Zeuthen committed
139
 * @cancellable: (allow-none): A #GCancellable or %NULL.
140 141 142 143 144
 * @callback: A #GAsyncReadyCallback to call when the request is satisfied
 * @user_data: The data to pass to @callback.
 *
 * Asynchronously checks if @subject exists.
 *
David Zeuthen's avatar
David Zeuthen committed
145 146 147 148 149
 * 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_subject_exists_finish() to get the
 * result of the operation.
150 151 152 153 154 155 156
 **/
void
polkit_subject_exists (PolkitSubject       *subject,
                       GCancellable        *cancellable,
                       GAsyncReadyCallback  callback,
                       gpointer             user_data)
{
157 158
  g_return_if_fail (POLKIT_IS_SUBJECT (subject));
  g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
159 160 161 162 163 164 165 166 167 168
  POLKIT_SUBJECT_GET_IFACE (subject)->exists (subject,
                                              cancellable,
                                              callback,
                                              user_data);
}

/**
 * polkit_subject_exists_finish:
 * @subject: A #PolkitSubject.
 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to polkit_subject_exists().
David Zeuthen's avatar
David Zeuthen committed
169
 * @error: (allow-none): Return location for error or %NULL.
170 171 172 173 174 175 176 177 178 179
 *
 * Finishes checking whether a subject exists.
 *
 * Returns: %TRUE if the subject exists, %FALSE if not or @error is set.
 */
gboolean
polkit_subject_exists_finish (PolkitSubject   *subject,
                              GAsyncResult    *res,
                              GError         **error)
{
180 181 182
  g_return_val_if_fail (POLKIT_IS_SUBJECT (subject), FALSE);
  g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
183 184 185 186 187 188 189 190
  return POLKIT_SUBJECT_GET_IFACE (subject)->exists_finish (subject,
                                                            res,
                                                            error);
}

/**
 * polkit_subject_exists_sync:
 * @subject: A #PolkitSubject.
David Zeuthen's avatar
David Zeuthen committed
191 192
 * @cancellable: (allow-none): A #GCancellable or %NULL.
 * @error: (allow-none): Return location for error or %NULL.
193 194 195
 *
 * Checks if @subject exists.
 *
David Zeuthen's avatar
David Zeuthen committed
196 197 198
 * This is a synchronous blocking call - the calling thread is blocked
 * until a reply is received. See polkit_subject_exists() for the
 * asynchronous version.
199 200 201 202 203 204 205 206
 *
 * Returns: %TRUE if the subject exists, %FALSE if not or @error is set.
 */
gboolean
polkit_subject_exists_sync   (PolkitSubject  *subject,
                              GCancellable   *cancellable,
                              GError        **error)
{
207 208 209
  g_return_val_if_fail (POLKIT_IS_SUBJECT (subject), FALSE);
  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
210 211 212 213 214
  return POLKIT_SUBJECT_GET_IFACE (subject)->exists_sync (subject,
                                                          cancellable,
                                                          error);
}

David Zeuthen's avatar
David Zeuthen committed
215 216 217
/**
 * polkit_subject_from_string:
 * @str: A string obtained from polkit_subject_to_string().
David Zeuthen's avatar
David Zeuthen committed
218
 * @error: (allow-none): Return location for error or %NULL.
David Zeuthen's avatar
David Zeuthen committed
219 220 221 222
 *
 * Creates an object from @str that implements the #PolkitSubject
 * interface.
 *
223 224
 * Returns: (transfer full): A #PolkitSubject or %NULL if @error is
 * set. Free with g_object_unref().
David Zeuthen's avatar
David Zeuthen committed
225
 */
226 227 228 229 230 231 232
PolkitSubject *
polkit_subject_from_string  (const gchar   *str,
                             GError       **error)
{
  PolkitSubject *subject;

  g_return_val_if_fail (str != NULL, NULL);
233
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
234 235 236 237 238

  /* TODO: we could do something with VFuncs like in g_icon_from_string() */

  subject = NULL;

239
  if (g_str_has_prefix (str, "unix-process:"))
240
    {
241 242
      gint scanned_pid;
      guint64 scanned_starttime;
243 244 245 246 247 248
      gint scanned_uid;
      if (sscanf (str, "unix-process:%d:%" G_GUINT64_FORMAT ":%d", &scanned_pid, &scanned_starttime, &scanned_uid) == 3)
        {
          subject = polkit_unix_process_new_for_owner (scanned_pid, scanned_starttime, scanned_uid);
        }
      else if (sscanf (str, "unix-process:%d:%" G_GUINT64_FORMAT, &scanned_pid, &scanned_starttime) == 2)
249
        {
250 251 252 253
          subject = polkit_unix_process_new_full (scanned_pid, scanned_starttime);
        }
      else if (sscanf (str, "unix-process:%d", &scanned_pid) == 1)
        {
254
          subject = polkit_unix_process_new (scanned_pid);
255 256 257 258 259 260 261
          if (polkit_unix_process_get_start_time (POLKIT_UNIX_PROCESS (subject)) == 0)
            {
              g_object_unref (subject);
              subject = NULL;
              g_set_error (error,
                           POLKIT_ERROR,
                           POLKIT_ERROR_FAILED,
262 263
                           "Unable to determine start time for process with pid %d",
                           scanned_pid);
264 265 266
            }
        }
    }
267 268 269 270
  else if (g_str_has_prefix (str, "unix-session:"))
    {
      subject = polkit_unix_session_new (str + sizeof "unix-session:" - 1);
    }
271 272 273 274 275 276 277 278 279 280
  else if (g_str_has_prefix (str, "system-bus-name:"))
    {
      subject = polkit_system_bus_name_new (str + sizeof "system-bus-name:" - 1);
    }

  if (subject == NULL && (error != NULL && *error == NULL))
    {
      g_set_error (error,
                   POLKIT_ERROR,
                   POLKIT_ERROR_FAILED,
281
                   "Malformed subject string `%s'",
282 283 284 285 286 287 288
                   str);
    }


  return subject;
}

David Zeuthen's avatar
David Zeuthen committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
GVariant *
polkit_subject_to_gvariant (PolkitSubject *subject)
{
  GVariantBuilder builder;
  GVariant *dict;
  GVariant *ret;
  const gchar *kind;

  kind = "";

  g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
  if (POLKIT_IS_UNIX_PROCESS (subject))
    {
      kind = "unix-process";
      g_variant_builder_add (&builder, "{sv}", "pid",
                             g_variant_new_uint32 (polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject))));
      g_variant_builder_add (&builder, "{sv}", "start-time",
                             g_variant_new_uint64 (polkit_unix_process_get_start_time (POLKIT_UNIX_PROCESS (subject))));
307 308
      g_variant_builder_add (&builder, "{sv}", "uid",
                             g_variant_new_int32 (polkit_unix_process_get_uid (POLKIT_UNIX_PROCESS (subject))));
David Zeuthen's avatar
David Zeuthen committed
309 310 311
    }
  else if (POLKIT_IS_UNIX_SESSION (subject))
    {
312
      kind = "unix-session";
David Zeuthen's avatar
David Zeuthen committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 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 398 399 400 401 402 403 404 405 406
      g_variant_builder_add (&builder, "{sv}", "session-id",
                             g_variant_new_string (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject))));
    }
  else if (POLKIT_IS_SYSTEM_BUS_NAME (subject))
    {
      kind = "system-bus-name";
      g_variant_builder_add (&builder, "{sv}", "name",
                             g_variant_new_string (polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (subject))));
    }
  else
    {
      g_warning ("Unknown class %s implementing PolkitSubject", g_type_name (G_TYPE_FROM_INSTANCE (subject)));
    }

  dict = g_variant_builder_end (&builder);
  ret = g_variant_new ("(s@a{sv})", kind, dict);
  return ret;
}

static GVariant *
lookup_asv (GVariant            *dict,
            const gchar         *given_key,
            const GVariantType  *given_type,
            GError             **error)
{
  GVariantIter iter;
  const gchar *key;
  GVariant *value;
  GVariant *ret;

  ret = NULL;

  g_variant_iter_init (&iter, dict);
  while (g_variant_iter_next (&iter, "{&sv}", &key, &value))
    {
      if (g_strcmp0 (key, given_key) == 0)
        {
          if (!g_variant_is_of_type (value, given_type))
            {
              gchar *type_string;
              type_string = g_variant_type_dup_string (given_type);
              g_set_error (error,
                           POLKIT_ERROR,
                           POLKIT_ERROR_FAILED,
                           "Value for key `%s' found but is of type %s and type %s was expected",
                           given_key,
                           g_variant_get_type_string (value),
                           type_string);
              g_free (type_string);
              goto out;
            }
          ret = value;
          goto out;
        }
      g_variant_unref (value);
    }

 out:
  if (ret == NULL)
    {
      gchar *type_string;
      type_string = g_variant_type_dup_string (given_type);
      g_set_error (error,
                   POLKIT_ERROR,
                   POLKIT_ERROR_FAILED,
                   "Didn't find value for key `%s' of type %s",
                   given_key,
                   type_string);
      g_free (type_string);
    }

  return ret;
}

PolkitSubject *
polkit_subject_new_for_gvariant (GVariant  *variant,
                                 GError    **error)
{
  PolkitSubject *ret;
  const gchar *kind;
  GVariant *details_gvariant;

  ret = NULL;

  g_variant_get (variant,
                 "(&s@a{sv})",
                 &kind,
                 &details_gvariant);

  if (g_strcmp0 (kind, "unix-process") == 0)
    {
      GVariant *v;
      guint32 pid;
      guint64 start_time;
407
      gint32 uid;
David Zeuthen's avatar
David Zeuthen committed
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426

      v = lookup_asv (details_gvariant, "pid", G_VARIANT_TYPE_UINT32, error);
      if (v == NULL)
        {
          g_prefix_error (error, "Error parsing unix-process subject: ");
          goto out;
        }
      pid = g_variant_get_uint32 (v);
      g_variant_unref (v);

      v = lookup_asv (details_gvariant, "start-time", G_VARIANT_TYPE_UINT64, error);
      if (v == NULL)
        {
          g_prefix_error (error, "Error parsing unix-process subject: ");
          goto out;
        }
      start_time = g_variant_get_uint64 (v);
      g_variant_unref (v);

427 428 429 430 431 432 433 434 435 436 437 438
      v = lookup_asv (details_gvariant, "uid", G_VARIANT_TYPE_INT32, error);
      if (v != NULL)
        {
          uid = g_variant_get_int32 (v);
          g_variant_unref (v);
        }
      else
        {
          uid = -1;
        }

      ret = polkit_unix_process_new_for_owner (pid, start_time, uid);
David Zeuthen's avatar
David Zeuthen committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
    }
  else if (g_strcmp0 (kind, "unix-session") == 0)
    {
      GVariant *v;
      const gchar *session_id;

      v = lookup_asv (details_gvariant, "session-id", G_VARIANT_TYPE_STRING, error);
      if (v == NULL)
        {
          g_prefix_error (error, "Error parsing unix-session subject: ");
          goto out;
        }
      session_id = g_variant_get_string (v, NULL);
      ret = polkit_unix_session_new (session_id);
      g_variant_unref (v);
    }
  else if (g_strcmp0 (kind, "system-bus-name") == 0)
    {
      GVariant *v;
      const gchar *name;

      v = lookup_asv (details_gvariant, "name", G_VARIANT_TYPE_STRING, error);
      if (v == NULL)
        {
          g_prefix_error (error, "Error parsing system-bus-name subject: ");
          goto out;
        }
      name = g_variant_get_string (v, NULL);
      if (!g_dbus_is_unique_name (name))
        {
          g_set_error (error,
                       POLKIT_ERROR,
                       POLKIT_ERROR_FAILED,
                       "Error parsing system-bus-name subject: `%s' is not a valid unique name",
                       name);
          goto out;
        }
      ret = polkit_system_bus_name_new (name);
      g_variant_unref (v);
    }
  else
    {
      g_set_error (error,
                   POLKIT_ERROR,
                   POLKIT_ERROR_FAILED,
                   "Unknown subject of kind `%s'",
                   kind);
    }

 out:
  g_variant_unref (details_gvariant);
  return ret;
}