polkitbackendlocalauthorizationstore.c 27.3 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
/*
 * 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>
 */

#include "config.h"

24
#include <netdb.h>
25
#include <string.h>
26 27 28
#include <polkit/polkit.h>
#include "polkitbackendlocalauthorizationstore.h"

David Zeuthen's avatar
David Zeuthen committed
29
/* <internal>
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
 * SECTION:polkitbackendlocalauthorizationstore
 * @title: PolkitBackendLocalAuthorizationStore
 * @short_description: Watches a directory for authorization files
 *
 * #PolkitBackendLocalAuthorizationStore is a utility class to watch
 * and read authorization files from a directory.
 */

struct _PolkitBackendLocalAuthorizationStorePrivate
{
  GFile *directory;
  gchar *extension;

  GFileMonitor *directory_monitor;

  /* List of LocalAuthorization objects */
  GList *authorizations;

  gboolean has_data;
};

enum
{
  PROP_0,
  PROP_DIRECTORY,
  PROP_EXTENSION,
};

enum
{
  CHANGED_SIGNAL,
  LAST_SIGNAL,
};

static guint signals[LAST_SIGNAL] = {0};

static void polkit_backend_local_authorization_store_purge (PolkitBackendLocalAuthorizationStore *store);

static void polkit_backend_local_authorization_store_ensure (PolkitBackendLocalAuthorizationStore *store);

G_DEFINE_TYPE (PolkitBackendLocalAuthorizationStore, polkit_backend_local_authorization_store, G_TYPE_OBJECT);

/* ---------------------------------------------------------------------------------------------------- */

typedef struct
{
  gchar *id;

78
  /* Identities with glob support */
79
  GList *identity_specs;
80 81 82 83

  /* Netgroup identity strings, which can not support glob syntax */
  GList *netgroup_identities;

84 85
  GList *action_specs;

86 87 88
  PolkitImplicitAuthorization result_any;
  PolkitImplicitAuthorization result_inactive;
  PolkitImplicitAuthorization result_active;
89 90

  GHashTable *return_value;
91 92 93 94 95 96 97 98
} LocalAuthorization;

static void
local_authorization_free (LocalAuthorization *authorization)
{
  g_free (authorization->id);
  g_list_foreach (authorization->identity_specs, (GFunc) g_pattern_spec_free, NULL);
  g_list_free (authorization->identity_specs);
99
  g_list_free_full (authorization->netgroup_identities, g_free);
100 101
  g_list_foreach (authorization->action_specs, (GFunc) g_pattern_spec_free, NULL);
  g_list_free (authorization->action_specs);
102 103
  if (authorization->return_value != NULL)
    g_hash_table_unref (authorization->return_value);
104 105 106 107 108 109 110 111 112 113 114 115 116
  g_free (authorization);
}


static LocalAuthorization *
local_authorization_new (GKeyFile      *key_file,
                         const gchar   *filename,
                         const gchar   *group,
                         GError       **error)
{
  LocalAuthorization *authorization;
  gchar **identity_strings;
  gchar **action_strings;
117 118 119
  gchar *result_any_string;
  gchar *result_inactive_string;
  gchar *result_active_string;
120
  gchar **return_value_strings;
121 122 123 124
  guint n;

  identity_strings = NULL;
  action_strings = NULL;
125 126 127
  result_any_string = NULL;
  result_inactive_string = NULL;
  result_active_string = NULL;
128
  return_value_strings = NULL;
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

  authorization = g_new0 (LocalAuthorization, 1);

  identity_strings = g_key_file_get_string_list (key_file,
                                                 group,
                                                 "Identity",
                                                 NULL,
                                                 error);
  if (identity_strings == NULL)
    {
      local_authorization_free (authorization);
      authorization = NULL;
      goto out;
    }
  for (n = 0; identity_strings[n] != NULL; n++)
    {
145 146 147 148 149 150 151
      /* Put netgroup entries in a seperate list from other identities who support glob syntax */
      if (g_str_has_prefix (identity_strings[n], "unix-netgroup:"))
        authorization->netgroup_identities = g_list_prepend (authorization->netgroup_identities,
                                                             g_strdup (identity_strings[n] + sizeof "unix-netgroup:" - 1));
      else
        authorization->identity_specs = g_list_prepend (authorization->identity_specs,
                                                        g_pattern_spec_new (identity_strings[n]));
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    }

  action_strings = g_key_file_get_string_list (key_file,
                                                 group,
                                                 "Action",
                                                 NULL,
                                                 error);
  if (action_strings == NULL)
    {
      local_authorization_free (authorization);
      authorization = NULL;
      goto out;
    }
  for (n = 0; action_strings[n] != NULL; n++)
    {
      authorization->action_specs = g_list_prepend (authorization->action_specs,
                                                    g_pattern_spec_new (action_strings[n]));
    }

171 172 173 174 175 176 177 178 179
  authorization->result_any = POLKIT_IMPLICIT_AUTHORIZATION_UNKNOWN;
  authorization->result_inactive = POLKIT_IMPLICIT_AUTHORIZATION_UNKNOWN;
  authorization->result_active = POLKIT_IMPLICIT_AUTHORIZATION_UNKNOWN;

  result_any_string = g_key_file_get_string (key_file,
                                             group,
                                             "ResultAny",
                                             NULL);
  if (result_any_string != NULL)
180
    {
181 182 183 184 185 186 187 188 189 190 191
      if (!polkit_implicit_authorization_from_string (result_any_string,
                                                      &authorization->result_any))
        {
          g_set_error (error,
                       POLKIT_ERROR,
                       POLKIT_ERROR_FAILED,
                       "Cannot parse ResultAny string `%s'", result_any_string);
          local_authorization_free (authorization);
          authorization = NULL;
          goto out;
        }
192 193
    }

194 195 196 197 198
  result_inactive_string = g_key_file_get_string (key_file,
                                                  group,
                                                  "ResultInactive",
                                                  NULL);
  if (result_inactive_string != NULL)
199
    {
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
      if (!polkit_implicit_authorization_from_string (result_inactive_string,
                                                      &authorization->result_inactive))
        {
          g_set_error (error,
                       POLKIT_ERROR,
                       POLKIT_ERROR_FAILED,
                       "Cannot parse ResultInactive string `%s'", result_inactive_string);
          local_authorization_free (authorization);
          authorization = NULL;
          goto out;
        }
    }

  result_active_string = g_key_file_get_string (key_file,
                                                group,
                                                "ResultActive",
                                                NULL);
  if (result_active_string != NULL)
    {
      if (!polkit_implicit_authorization_from_string (result_active_string,
                                                      &authorization->result_active))
        {
          g_set_error (error,
                       POLKIT_ERROR,
                       POLKIT_ERROR_FAILED,
                       "Cannot parse ResultActive string `%s'", result_active_string);
          local_authorization_free (authorization);
          authorization = NULL;
          goto out;
        }
    }

  if (result_any_string == NULL && result_inactive_string == NULL && result_active_string == NULL)
    {
          g_set_error (error,
                       POLKIT_ERROR,
                       POLKIT_ERROR_FAILED,
                       "Must have at least one of ResultAny, ResultInactive and ResultActive");
          local_authorization_free (authorization);
          authorization = NULL;
          goto out;
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
  return_value_strings = g_key_file_get_string_list (key_file,
                                                     group,
                                                     "ReturnValue",
                                                     NULL,
                                                     error);
  if (return_value_strings != NULL)
    {
      for (n = 0; return_value_strings[n] != NULL; n++)
        {
          gchar *p;
          const gchar *key;
          const gchar *value;

          p = strchr (return_value_strings[n], '=');
          if (p == NULL)
            {
              g_warning ("Item `%s' in ReturnValue is malformed. Ignoring.",
                         return_value_strings[n]);
              continue;
            }

          *p = '\0';
          key = return_value_strings[n];
          value = p + 1;

          if (authorization->return_value == NULL)
            {
              authorization->return_value = g_hash_table_new_full (g_str_hash,
                                                                   g_str_equal,
                                                                   g_free,
                                                                   g_free);
            }
          g_hash_table_insert (authorization->return_value, g_strdup (key), g_strdup (value));
        }
    }

279 280 281 282 283
  authorization->id = g_strdup_printf ("%s::%s", filename, group);

 out:
  g_strfreev (identity_strings);
  g_free (action_strings);
284 285 286
  g_free (result_any_string);
  g_free (result_inactive_string);
  g_free (result_active_string);
287
  g_strfreev (return_value_strings);
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 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 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 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 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
  return authorization;
}

/* ---------------------------------------------------------------------------------------------------- */

static void
polkit_backend_local_authorization_store_init (PolkitBackendLocalAuthorizationStore *store)
{
  store->priv = G_TYPE_INSTANCE_GET_PRIVATE (store,
                                             POLKIT_BACKEND_TYPE_LOCAL_AUTHORIZATION_STORE,
                                             PolkitBackendLocalAuthorizationStorePrivate);
}

static void
polkit_backend_local_authorization_store_finalize (GObject *object)
{
  PolkitBackendLocalAuthorizationStore *store = POLKIT_BACKEND_LOCAL_AUTHORIZATION_STORE (object);

  if (store->priv->directory != NULL)
    g_object_unref (store->priv->directory);
  g_free (store->priv->extension);

  if (store->priv->directory_monitor != NULL)
    g_object_unref (store->priv->directory_monitor);

  g_list_foreach (store->priv->authorizations, (GFunc) local_authorization_free, NULL);
  g_list_free (store->priv->authorizations);

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


static void
polkit_backend_local_authorization_store_get_property (GObject    *object,
                                                       guint       prop_id,
                                                       GValue     *value,
                                                       GParamSpec *pspec)
{
  PolkitBackendLocalAuthorizationStore *store = POLKIT_BACKEND_LOCAL_AUTHORIZATION_STORE (object);

  switch (prop_id)
    {
    case PROP_DIRECTORY:
      g_value_set_object (value, store->priv->directory);
      break;

    case PROP_EXTENSION:
      g_value_set_string (value, store->priv->extension);
      break;

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

static void
polkit_backend_local_authorization_store_set_property (GObject      *object,
                                                       guint         prop_id,
                                                       const GValue *value,
                                                       GParamSpec   *pspec)
{
  PolkitBackendLocalAuthorizationStore *store = POLKIT_BACKEND_LOCAL_AUTHORIZATION_STORE (object);

  switch (prop_id)
    {
    case PROP_DIRECTORY:
      store->priv->directory = g_value_dup_object (value);
      break;

    case PROP_EXTENSION:
      store->priv->extension = g_value_dup_string (value);
      break;

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

static void
directory_monitor_changed (GFileMonitor     *monitor,
                           GFile            *file,
                           GFile            *other_file,
                           GFileMonitorEvent event_type,
                           gpointer          user_data)
{
  PolkitBackendLocalAuthorizationStore *store;

  store = POLKIT_BACKEND_LOCAL_AUTHORIZATION_STORE (user_data);

  if (file != NULL)
    {
      gchar *name;

      name = g_file_get_basename (file);

      //g_debug ("event_type=%d file=%p name=%s", event_type, file, name);

      if (!g_str_has_prefix (name, ".") &&
          !g_str_has_prefix (name, "#") &&
          g_str_has_suffix (name, store->priv->extension) &&
          (event_type == G_FILE_MONITOR_EVENT_CREATED ||
           event_type == G_FILE_MONITOR_EVENT_DELETED ||
           event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT))
        {

          //g_debug ("match");

          /* now throw away all caches */
          polkit_backend_local_authorization_store_purge (store);
          g_signal_emit_by_name (store, "changed");
        }

      g_free (name);
    }
}

static void
polkit_backend_local_authorization_store_constructed (GObject *object)
{
  PolkitBackendLocalAuthorizationStore *store = POLKIT_BACKEND_LOCAL_AUTHORIZATION_STORE (object);
  GError *error;

  error = NULL;
  store->priv->directory_monitor = g_file_monitor_directory (store->priv->directory,
                                                              G_FILE_MONITOR_NONE,
                                                              NULL,
                                                              &error);
  if (store->priv->directory_monitor == NULL)
    {
      gchar *dir_name;
      dir_name = g_file_get_uri (store->priv->directory);
      g_warning ("Error monitoring directory %s: %s", dir_name, error->message);
      g_free (dir_name);
      g_error_free (error);
    }
  else
    {
      g_signal_connect (store->priv->directory_monitor,
                        "changed",
                        (GCallback) directory_monitor_changed,
                        store);
    }

  if (G_OBJECT_CLASS (polkit_backend_local_authorization_store_parent_class)->constructed != NULL)
    G_OBJECT_CLASS (polkit_backend_local_authorization_store_parent_class)->constructed (object);
}

static void
polkit_backend_local_authorization_store_class_init (PolkitBackendLocalAuthorizationStoreClass *klass)
{
  GObjectClass *gobject_class;

  gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->get_property = polkit_backend_local_authorization_store_get_property;
  gobject_class->set_property = polkit_backend_local_authorization_store_set_property;
  gobject_class->constructed  = polkit_backend_local_authorization_store_constructed;
  gobject_class->finalize     = polkit_backend_local_authorization_store_finalize;

  g_type_class_add_private (klass, sizeof (PolkitBackendLocalAuthorizationStorePrivate));

  /**
   * PolkitBackendLocalAuthorizationStore:directory:
   *
   * The directory to watch for authorization files.
   */
  g_object_class_install_property (gobject_class,
                                   PROP_DIRECTORY,
                                   g_param_spec_object ("directory",
                                                        "Directory",
                                                        "The directory to watch for configuration files",
                                                        G_TYPE_FILE,
                                                        G_PARAM_CONSTRUCT_ONLY |
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_STATIC_NAME |
                                                        G_PARAM_STATIC_BLURB |
                                                        G_PARAM_STATIC_NICK));

  /**
   * PolkitBackendLocalAuthorizationStore:extension:
   *
   * The file extension for files to consider, e.g. <quote>.pkla</quote>.
   */
  g_object_class_install_property (gobject_class,
                                   PROP_EXTENSION,
                                   g_param_spec_string ("extension",
                                                        "Extension",
                                                        "The extension of files to consider",
                                                        NULL,
                                                        G_PARAM_CONSTRUCT_ONLY |
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_STATIC_NAME |
                                                        G_PARAM_STATIC_BLURB |
                                                        G_PARAM_STATIC_NICK));

  /**
   * PolkitBackendConfiguStore::changed:
   * @store: A #PolkitBackendLocalAuthorizationStore.
   *
   * Emitted when configuration files in #PolkitBackendConfiguStore:directory changes.
   */
  signals[CHANGED_SIGNAL] = g_signal_new ("changed",
                                          POLKIT_BACKEND_TYPE_LOCAL_AUTHORIZATION_STORE,
                                          G_SIGNAL_RUN_LAST,
                                          G_STRUCT_OFFSET (PolkitBackendLocalAuthorizationStoreClass, changed),
                                          NULL,
                                          NULL,
                                          g_cclosure_marshal_VOID__VOID,
                                          G_TYPE_NONE,
                                          0);
}

/**
 * polkit_backend_local_authorization_store_new:
 * @directory: The directory to watch.
 * @extension: The extension of files to consider e.g. <quote>.pkla</quote>.
 *
 * Creates a new #PolkitBackendLocalAuthorizationStore object that
 * reads authorizations from @directory with file extension
 * @extension. To watch for configuration changes, connect to the
 * #PolkitBackendLocalAuthorizationStore::changed signal.
 *
 * Returns: A #PolkitBackendLocalAuthorizationStore. Free with
 * g_object_unref().
 **/
PolkitBackendLocalAuthorizationStore *
polkit_backend_local_authorization_store_new (GFile       *directory,
                                              const gchar *extension)
{
  PolkitBackendLocalAuthorizationStore *store;

  store = POLKIT_BACKEND_LOCAL_AUTHORIZATION_STORE (g_object_new (POLKIT_BACKEND_TYPE_LOCAL_AUTHORIZATION_STORE,
                                                                  "directory", directory,
                                                                  "extension", extension,
                                                                  NULL));

  return store;
}

static void
polkit_backend_local_authorization_store_purge (PolkitBackendLocalAuthorizationStore *store)
{
533 534 535 536 537 538
  gchar *path;

  path = g_file_get_path (store->priv->directory);
  g_debug ("Dropping all .pkla caches for directory `%s'", path);
  g_free (path);

539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
  g_list_foreach (store->priv->authorizations, (GFunc) local_authorization_free, NULL);
  g_list_free (store->priv->authorizations);
  store->priv->authorizations = NULL;

  store->priv->has_data = FALSE;
}

static void
polkit_backend_local_authorization_store_ensure (PolkitBackendLocalAuthorizationStore *store)
{
  GFileEnumerator *enumerator;
  GFileInfo *file_info;
  GError *error;
  GList *files;
  GList *l;

  files = NULL;

  if (store->priv->has_data)
    goto out;

  polkit_backend_local_authorization_store_purge (store);

  error = NULL;
  enumerator = g_file_enumerate_children (store->priv->directory,
564
                                          "standard::name",
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
                                          G_FILE_QUERY_INFO_NONE,
                                          NULL,
                                          &error);
  if (enumerator == NULL)
    {
      gchar *dir_name;
      dir_name = g_file_get_uri (store->priv->directory);
      g_warning ("Error enumerating files in %s: %s", dir_name, error->message);
      g_free (dir_name);
      g_error_free (error);
      goto out;
    }

  while ((file_info = g_file_enumerator_next_file (enumerator, NULL, &error)) != NULL)
    {
      const gchar *name;

      name = g_file_info_get_name (file_info);

      /* only consider files with the appropriate extension */
585
      if (g_str_has_suffix (name, store->priv->extension) && name[0] != '.')
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
        files = g_list_prepend (files, g_file_get_child (store->priv->directory, name));

      g_object_unref (file_info);
    }
  g_object_unref (enumerator);
  if (error != NULL)
    {
      g_warning ("Error enumerating files: %s", error->message);
      g_error_free (error);
      goto out;
    }

  /* process files; highest priority comes first */
  for (l = files; l != NULL; l = l->next)
    {
      GFile *file = G_FILE (l->data);
      gchar *filename;
      GKeyFile *key_file;

      filename = g_file_get_path (file);

      key_file = g_key_file_new ();

      error = NULL;
      if (!g_key_file_load_from_file (key_file,
                                      filename,
                                      G_KEY_FILE_NONE,
613
                                      &error))
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
        {
          g_warning ("Error loading key-file %s: %s", filename, error->message);
          g_error_free (error);
          error = NULL;
          g_key_file_free (key_file);
        }
      else
        {
          gchar **groups;
          guint n;

          groups = g_key_file_get_groups (key_file, NULL);
          for (n = 0; groups[n] != NULL; n++)
            {
              LocalAuthorization *authorization;

              error = NULL;
              authorization = local_authorization_new (key_file, filename, groups[n], &error);
              if (authorization == NULL)
                {
                  g_warning ("Error parsing group `%s' in file `%s': %s",
                             groups[n],
                             filename,
                             error->message);
                  g_error_free (error);
                }
              else
                {
                  store->priv->authorizations = g_list_prepend (store->priv->authorizations,
                                                                authorization);
                }
            }
          g_strfreev (groups);

          store->priv->authorizations = g_list_reverse (store->priv->authorizations);

          g_key_file_free (key_file);
        }

      g_free (filename);
    }

656 657
  store->priv->has_data = TRUE;

658 659 660 661 662 663 664 665 666 667 668
 out:
  g_list_foreach (files, (GFunc) g_object_unref, NULL);
  g_list_free (files);
}

/**
 * polkit_backend_local_authorization_store_lookup:
 * @store: A #PolkitBackendLocalAuthorizationStore.
 * @identity: The identity to check for.
 * @action_id: The action id to check for.
 * @details: Details for @action.
669 670 671
 * @out_result_any: Return location for the result for any subjects if the look up matched.
 * @out_result_inactive: Return location for the result for subjects in local inactive sessions if the look up matched.
 * @out_result_active: Return location for the result for subjects in local active sessions if the look up matched.
672
 * @out_details: %NULL or a #PolkitDetails object to append key/value pairs to on a positive match.
673 674 675 676 677 678 679 680 681 682 683
 *
 * Checks if an authorization entry from @store matches @identity, @action_id and @details.
 *
 * Returns: %TRUE if @store has an authorization entry that matches
 *     @identity, @action_id and @details. Otherwise %FALSE.
 */
gboolean
polkit_backend_local_authorization_store_lookup (PolkitBackendLocalAuthorizationStore *store,
                                                 PolkitIdentity                       *identity,
                                                 const gchar                          *action_id,
                                                 PolkitDetails                        *details,
684 685
                                                 PolkitImplicitAuthorization          *out_result_any,
                                                 PolkitImplicitAuthorization          *out_result_inactive,
686 687
                                                 PolkitImplicitAuthorization          *out_result_active,
                                                 PolkitDetails                        *out_details)
688 689 690 691 692 693 694 695 696
{
  GList *l, *ll;
  gboolean ret;
  gchar *identity_string;

  g_return_val_if_fail (POLKIT_BACKEND_IS_LOCAL_AUTHORIZATION_STORE (store), FALSE);
  g_return_val_if_fail (POLKIT_IS_IDENTITY (identity), FALSE);
  g_return_val_if_fail (action_id != NULL, FALSE);
  g_return_val_if_fail (POLKIT_IS_DETAILS (details), FALSE);
697 698 699
  g_return_val_if_fail (out_result_any != NULL, FALSE);
  g_return_val_if_fail (out_result_inactive != NULL, FALSE);
  g_return_val_if_fail (out_result_active != NULL, FALSE);
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718

  ret = FALSE;
  identity_string = NULL;

  polkit_backend_local_authorization_store_ensure (store);

  for (l = store->priv->authorizations; l != NULL; l = l->next)
    {
      LocalAuthorization *authorization = l->data;

      /* first match the action */
      for (ll = authorization->action_specs; ll != NULL; ll = ll->next)
        {
          if (g_pattern_match_string ((GPatternSpec *) ll->data, action_id))
            break;
        }
      if (ll == NULL)
        continue;

719
      /* then match the identity against identity specs */
720 721 722 723 724 725 726
      if (identity_string == NULL)
        identity_string = polkit_identity_to_string (identity);
      for (ll = authorization->identity_specs; ll != NULL; ll = ll->next)
        {
          if (g_pattern_match_string ((GPatternSpec *) ll->data, identity_string))
            break;
        }
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742

      /* if no identity specs matched and identity is a user, match against netgroups */
      if (ll == NULL && POLKIT_IS_UNIX_USER (identity))
        {
          PolkitUnixUser *user_identity = POLKIT_UNIX_USER (identity);
          const gchar *user_name = polkit_unix_user_get_name (user_identity);
          if (!user_name)
            continue;

          for (ll = authorization->netgroup_identities; ll != NULL; ll = ll->next)
            {
              if (innetgr ((const gchar *) ll->data, NULL, user_name, NULL))
                break;
            }
        }

743 744 745 746
      if (ll == NULL)
        continue;

      /* Yay, a match! However, keep going since subsequent authorization entries may modify the result */
747 748 749
      *out_result_any = authorization->result_any;
      *out_result_inactive = authorization->result_inactive;
      *out_result_active = authorization->result_active;
750 751
      ret = TRUE;

752 753 754 755 756 757 758 759 760 761 762 763 764
      if (out_details != NULL && authorization->return_value != NULL)
        {
          GHashTableIter iter;
          const gchar *key;
          const gchar *value;

          g_hash_table_iter_init (&iter, authorization->return_value);
          while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value))
            {
              polkit_details_insert (out_details, key, value);
            }
        }

765 766 767 768 769 770 771 772 773 774 775 776
#if 0
      g_debug ("authorization with id `%s' matched action_id `%s' for identity `%s'",
               authorization->id,
               action_id,
               polkit_identity_to_string (identity));
#endif
    }

  g_free (identity_string);

  return ret;
}