polkitidentity.c 10.1 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 29 30
/*
 * 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 "polkitidentity.h"
#include "polkitunixuser.h"
#include "polkitunixgroup.h"
31
#include "polkitunixnetgroup.h"
32 33 34
#include "polkiterror.h"
#include "polkitprivate.h"

David Zeuthen's avatar
David Zeuthen committed
35 36 37
/**
 * SECTION:polkitidentity
 * @title: PolkitIdentity
David Zeuthen's avatar
David Zeuthen committed
38
 * @short_description: Type for representing identities
David Zeuthen's avatar
David Zeuthen committed
39
 *
David Zeuthen's avatar
David Zeuthen committed
40 41
 * #PolkitIdentity is an abstract type for representing one or more
 * identities.
David Zeuthen's avatar
David Zeuthen committed
42 43
 */

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

GType
polkit_identity_get_type (void)
{
52
  static volatile gsize g_define_type_id__volatile = 0;
53

54
  if (g_once_init_enter (&g_define_type_id__volatile))
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    {
      static const GTypeInfo info =
      {
        sizeof (PolkitIdentityIface),
        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    */
      };

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

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

77
  return g_define_type_id__volatile;
78 79
}

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

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

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

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

David Zeuthen's avatar
David Zeuthen committed
119 120 121 122 123 124 125 126 127
/**
 * polkit_identity_to_string:
 * @identity: A #PolkitIdentity.
 *
 * Serializes @identity to a string that can be used in
 * polkit_identity_from_string().
 *
 * Returns: A string representing @identity. Free with g_free().
 */
128 129 130
gchar *
polkit_identity_to_string (PolkitIdentity *identity)
{
131
  g_return_val_if_fail (POLKIT_IS_IDENTITY (identity), NULL);
132 133 134
  return POLKIT_IDENTITY_GET_IFACE (identity)->to_string (identity);
}

David Zeuthen's avatar
David Zeuthen committed
135 136 137 138 139 140 141 142
/**
 * polkit_identity_from_string:
 * @str: A string obtained from polkit_identity_to_string().
 * @error: Return location for error.
 *
 * Creates an object from @str that implements the #PolkitIdentity
 * interface.
 *
143 144
 * Returns: (allow-none) (transfer full): A #PolkitIdentity or %NULL
 * if @error is set. Free with g_object_unref().
David Zeuthen's avatar
David Zeuthen committed
145
 */
146 147 148 149 150 151 152 153 154
PolkitIdentity *
polkit_identity_from_string  (const gchar   *str,
                             GError       **error)
{
  PolkitIdentity *identity;
  guint64 val;
  gchar *endptr;

  g_return_val_if_fail (str != NULL, NULL);
155
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
156 157 158 159 160 161 162 163 164 165 166

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

  identity = NULL;

  if (g_str_has_prefix (str, "unix-user:"))
    {
      val = g_ascii_strtoull (str + sizeof "unix-user:" - 1,
                              &endptr,
                              10);
      if (*endptr == '\0')
167
        identity = polkit_unix_user_new ((gint) val);
168 169 170 171 172 173 174 175 176 177
      else
        identity = polkit_unix_user_new_for_name (str + sizeof "unix-user:" - 1,
                                                 error);
    }
  else if (g_str_has_prefix (str, "unix-group:"))
    {
      val = g_ascii_strtoull (str + sizeof "unix-group:" - 1,
                              &endptr,
                              10);
      if (*endptr == '\0')
178
        identity = polkit_unix_group_new ((gint) val);
179 180 181 182
      else
        identity = polkit_unix_group_new_for_name (str + sizeof "unix-group:" - 1,
                                                  error);
    }
183 184 185 186
  else if (g_str_has_prefix (str, "unix-netgroup:"))
    {
      identity = polkit_unix_netgroup_new (str + sizeof "unix-netgroup:" - 1);
    }
187 188 189 190 191 192 193 194 195 196 197 198 199 200

  if (identity == NULL && (error != NULL && *error == NULL))
    {
      g_set_error (error,
                   POLKIT_ERROR,
                   POLKIT_ERROR_FAILED,
                   "Malformed identity string '%s'",
                   str);
    }


  return identity;
}

David Zeuthen's avatar
David Zeuthen committed
201 202 203
GVariant *
polkit_identity_to_gvariant (PolkitIdentity *identity)
{
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
  GVariantBuilder builder;
  GVariant *dict;
  GVariant *ret;
  const gchar *kind;

  kind = "";

  g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
  if (POLKIT_IS_UNIX_USER (identity))
    {
      kind = "unix-user";
      g_variant_builder_add (&builder, "{sv}", "uid",
                             g_variant_new_uint32 (polkit_unix_user_get_uid (POLKIT_UNIX_USER (identity))));
    }
  else if (POLKIT_IS_UNIX_GROUP (identity))
    {
      kind = "unix-group";
      g_variant_builder_add (&builder, "{sv}", "gid",
                             g_variant_new_uint32 (polkit_unix_group_get_gid (POLKIT_UNIX_GROUP (identity))));
    }
224 225 226 227 228 229
  else if (POLKIT_IS_UNIX_NETGROUP (identity))
    {
      kind = "unix-netgroup";
      g_variant_builder_add (&builder, "{sv}", "name",
                             g_variant_new_string (polkit_unix_netgroup_get_name (POLKIT_UNIX_NETGROUP (identity))));
    }
230 231 232 233 234 235 236 237
  else
    {
      g_warning ("Unknown class %s implementing PolkitIdentity", g_type_name (G_TYPE_FROM_INSTANCE (identity)));
    }

  dict = g_variant_builder_end (&builder);
  ret = g_variant_new ("(s@a{sv})", kind, dict);
  return ret;
David Zeuthen's avatar
David Zeuthen committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 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 306 307 308 309 310 311 312 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
}

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;
}

PolkitIdentity *
polkit_identity_new_for_gvariant (GVariant  *variant,
                                  GError    **error)
{
  PolkitIdentity *ret;
  const gchar *kind;
  GVariant *details_gvariant;

  ret = NULL;

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

  if (g_strcmp0 (kind, "unix-user") == 0)
    {
      GVariant *v;
      guint32 uid;

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

      ret = polkit_unix_user_new (uid);
    }
  else if (g_strcmp0 (kind, "unix-group") == 0)
    {
      GVariant *v;
      guint32 gid;

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

      ret = polkit_unix_group_new (gid);
    }
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
  else if (g_strcmp0 (kind, "unix-netgroup") == 0)
    {
      GVariant *v;
      const char *name;

      v = lookup_asv (details_gvariant, "name", G_VARIANT_TYPE_STRING, error);
      if (v == NULL)
        {
          g_prefix_error (error, "Error parsing net identity: ");
          goto out;
        }
      name = g_variant_get_string (v, NULL);
      ret = polkit_unix_netgroup_new (name);
      g_variant_unref (v);
    }
David Zeuthen's avatar
David Zeuthen committed
357 358 359 360 361 362 363 364 365 366 367 368 369
  else
    {
      g_set_error (error,
                   POLKIT_ERROR,
                   POLKIT_ERROR_FAILED,
                   "Unknown identity of kind `%s'",
                   kind);
    }

 out:
  g_variant_unref (details_gvariant);
  return ret;
}