| 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 <errno.h> |
| 23 |
#include <pwd.h> |
| 24 |
#include <string.h> |
| 25 |
#include <expat.h> |
| 26 |
|
| 27 |
#ifdef ENABLE_GETTEXT |
| 28 |
#include <locale.h> |
| 29 |
#include <glib/gi18n.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
#include <polkit/polkit.h> |
| 33 |
#include <polkit/polkitprivate.h> |
| 34 |
|
| 35 |
#include "polkitbackendactionpool.h" |
| 36 |
|
| 37 |
/* <internal> |
| 38 |
* SECTION:polkitbackendactionpool |
| 39 |
* @title: PolkitBackendActionPool |
| 40 |
* @short_description: Registered actions |
| 41 |
* |
| 42 |
* The #PolkitBackendActionPool class is a utility class to look up registered PolicyKit actions. |
| 43 |
*/ |
| 44 |
|
| 45 |
typedef struct |
| 46 |
{ |
| 47 |
gchar *vendor_name; |
| 48 |
gchar *vendor_url; |
| 49 |
gchar *icon_name; |
| 50 |
gchar *description; |
| 51 |
gchar *description_domain; |
| 52 |
gchar *message; |
| 53 |
gchar *message_domain; |
| 54 |
|
| 55 |
PolkitImplicitAuthorization implicit_authorization_any; |
| 56 |
PolkitImplicitAuthorization implicit_authorization_inactive; |
| 57 |
PolkitImplicitAuthorization implicit_authorization_active; |
| 58 |
|
| 59 |
/* each of these map from the locale identifer (e.g. da_DK) to the localized value */ |
| 60 |
GHashTable *localized_description; |
| 61 |
GHashTable *localized_message; |
| 62 |
|
| 63 |
/* this maps from annotation key (string) to annotation value (also a string) */ |
| 64 |
GHashTable *annotations; |
| 65 |
} ParsedAction; |
| 66 |
|
| 67 |
static void |
| 68 |
parsed_action_free (ParsedAction *action) |
| 69 |
{ |
| 70 |
g_free (action->vendor_name); |
| 71 |
g_free (action->vendor_url); |
| 72 |
g_free (action->icon_name); |
| 73 |
g_free (action->description); |
| 74 |
g_free (action->description_domain); |
| 75 |
g_free (action->message); |
| 76 |
g_free (action->message_domain); |
| 77 |
|
| 78 |
g_hash_table_unref (action->localized_description); |
| 79 |
g_hash_table_unref (action->localized_message); |
| 80 |
|
| 81 |
g_hash_table_unref (action->annotations); |
| 82 |
g_free (action); |
| 83 |
} |
| 84 |
|
| 85 |
static gboolean process_policy_file (PolkitBackendActionPool *pool, |
| 86 |
const gchar *xml, |
| 87 |
GError **error); |
| 88 |
|
| 89 |
static void ensure_file (PolkitBackendActionPool *pool, |
| 90 |
GFile *file); |
| 91 |
|
| 92 |
static void ensure_all_files (PolkitBackendActionPool *pool); |
| 93 |
|
| 94 |
static const gchar *_localize (GHashTable *translations, |
| 95 |
const gchar *untranslated, |
| 96 |
const gchar *domain, |
| 97 |
const gchar *lang); |
| 98 |
|
| 99 |
typedef struct |
| 100 |
{ |
| 101 |
/* directories with .policy files, e.g. /usr/share/polkit-1/actions */ |
| 102 |
GList *directories; |
| 103 |
|
| 104 |
/* GFileMonitor instances for directories */ |
| 105 |
GList *dir_monitors; |
| 106 |
|
| 107 |
/* maps from action_id to a ParsedAction struct */ |
| 108 |
GHashTable *parsed_actions; |
| 109 |
|
| 110 |
/* maps from basename of parsed file to nothing */ |
| 111 |
GHashTable *parsed_files; |
| 112 |
|
| 113 |
/* is TRUE only when we've read all files */ |
| 114 |
gboolean has_loaded_all_files; |
| 115 |
|
| 116 |
} PolkitBackendActionPoolPrivate; |
| 117 |
|
| 118 |
enum |
| 119 |
{ |
| 120 |
PROP_0, |
| 121 |
PROP_DIRECTORIES, |
| 122 |
}; |
| 123 |
|
| 124 |
enum |
| 125 |
{ |
| 126 |
CHANGED_SIGNAL, |
| 127 |
LAST_SIGNAL, |
| 128 |
}; |
| 129 |
|
| 130 |
static guint signals[LAST_SIGNAL] = {0}; |
| 131 |
|
| 132 |
G_DEFINE_TYPE_WITH_PRIVATE (PolkitBackendActionPool, polkit_backend_action_pool, G_TYPE_OBJECT); |
| 133 |
|
| 134 |
static void |
| 135 |
polkit_backend_action_pool_init (PolkitBackendActionPool *pool) |
| 136 |
{ |
| 137 |
PolkitBackendActionPoolPrivate *priv; |
| 138 |
|
| 139 |
priv = polkit_backend_action_pool_get_instance_private (pool); |
| 140 |
|
| 141 |
priv->parsed_actions = g_hash_table_new_full (g_str_hash, |
| 142 |
g_str_equal, |
| 143 |
g_free, |
| 144 |
(GDestroyNotify) parsed_action_free); |
| 145 |
|
| 146 |
priv->parsed_files = g_hash_table_new_full (g_str_hash, |
| 147 |
g_str_equal, |
| 148 |
g_free, |
| 149 |
NULL); |
| 150 |
} |
| 151 |
|
| 152 |
static void |
| 153 |
polkit_backend_action_pool_finalize (GObject *object) |
| 154 |
{ |
| 155 |
PolkitBackendActionPool *pool; |
| 156 |
PolkitBackendActionPoolPrivate *priv; |
| 157 |
|
| 158 |
pool = POLKIT_BACKEND_ACTION_POOL (object); |
| 159 |
priv = polkit_backend_action_pool_get_instance_private (pool); |
| 160 |
|
| 161 |
if (priv->directories != NULL) |
| 162 |
g_list_free_full (priv->directories, g_object_unref); |
| 163 |
|
| 164 |
if (priv->dir_monitors != NULL) |
| 165 |
g_list_free_full (priv->dir_monitors, g_object_unref); |
| 166 |
|
| 167 |
if (priv->parsed_actions != NULL) |
| 168 |
g_hash_table_unref (priv->parsed_actions); |
| 169 |
|
| 170 |
if (priv->parsed_files != NULL) |
| 171 |
g_hash_table_unref (priv->parsed_files); |
| 172 |
|
| 173 |
G_OBJECT_CLASS (polkit_backend_action_pool_parent_class)->finalize (object); |
| 174 |
} |
| 175 |
|
| 176 |
static void |
| 177 |
polkit_backend_action_pool_get_property (GObject *object, |
| 178 |
guint prop_id, |
| 179 |
GValue *value, |
| 180 |
GParamSpec *pspec) |
| 181 |
{ |
| 182 |
PolkitBackendActionPool *pool; |
| 183 |
PolkitBackendActionPoolPrivate *priv; |
| 184 |
|
| 185 |
pool = POLKIT_BACKEND_ACTION_POOL (object); |
| 186 |
priv = polkit_backend_action_pool_get_instance_private (pool); |
| 187 |
|
| 188 |
switch (prop_id) |
| 189 |
{ |
| 190 |
case PROP_DIRECTORIES: |
| 191 |
g_value_set_object (value, priv->directories); |
| 192 |
break; |
| 193 |
|
| 194 |
default: |
| 195 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 196 |
break; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
static void |
| 201 |
dir_monitor_changed (GFileMonitor *monitor, |
| 202 |
GFile *file, |
| 203 |
GFile *other_file, |
| 204 |
GFileMonitorEvent event_type, |
| 205 |
gpointer user_data) |
| 206 |
{ |
| 207 |
PolkitBackendActionPool *pool; |
| 208 |
PolkitBackendActionPoolPrivate *priv; |
| 209 |
|
| 210 |
pool = POLKIT_BACKEND_ACTION_POOL (user_data); |
| 211 |
priv = polkit_backend_action_pool_get_instance_private (pool); |
| 212 |
|
| 213 |
/* TODO: maybe rate-limit so storms of events are collapsed into one with a 500ms resolution? |
| 214 |
* Because when editing a file with emacs we get 4-8 events.. |
| 215 |
*/ |
| 216 |
|
| 217 |
if (file != NULL) |
| 218 |
{ |
| 219 |
gchar *name; |
| 220 |
|
| 221 |
name = g_file_get_basename (file); |
| 222 |
|
| 223 |
//g_debug ("event_type=%d file=%p name=%s", event_type, file, name); |
| 224 |
|
| 225 |
if (!g_str_has_prefix (name, ".") && |
| 226 |
!g_str_has_prefix (name, "#") && |
| 227 |
g_str_has_suffix (name, ".policy") && |
| 228 |
(event_type == G_FILE_MONITOR_EVENT_CREATED || |
| 229 |
event_type == G_FILE_MONITOR_EVENT_DELETED || |
| 230 |
event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)) |
| 231 |
{ |
| 232 |
|
| 233 |
//g_debug ("match"); |
| 234 |
|
| 235 |
/* now throw away all caches */ |
| 236 |
g_hash_table_remove_all (priv->parsed_files); |
| 237 |
g_hash_table_remove_all (priv->parsed_actions); |
| 238 |
priv->has_loaded_all_files = FALSE; |
| 239 |
|
| 240 |
g_signal_emit_by_name (pool, "changed"); |
| 241 |
} |
| 242 |
|
| 243 |
g_free (name); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
static void |
| 249 |
polkit_backend_action_pool_set_property (GObject *object, |
| 250 |
guint prop_id, |
| 251 |
const GValue *value, |
| 252 |
GParamSpec *pspec) |
| 253 |
{ |
| 254 |
PolkitBackendActionPool *pool; |
| 255 |
PolkitBackendActionPoolPrivate *priv; |
| 256 |
|
| 257 |
pool = POLKIT_BACKEND_ACTION_POOL (object); |
| 258 |
priv = polkit_backend_action_pool_get_instance_private (pool); |
| 259 |
|
| 260 |
switch (prop_id) |
| 261 |
{ |
| 262 |
case PROP_DIRECTORIES: |
| 263 |
{ |
| 264 |
const gchar **dir_names = (const gchar**) g_value_get_boxed (value); |
| 265 |
|
| 266 |
for (int n = 0; dir_names[n] != NULL; n++) |
| 267 |
{ |
| 268 |
GFile *file; |
| 269 |
GFileMonitor *monitor; |
| 270 |
GError *error = NULL; |
| 271 |
|
| 272 |
const gchar *dir_name = dir_names[n]; |
| 273 |
|
| 274 |
file = g_file_new_for_path (dir_name); |
| 275 |
priv->directories = g_list_prepend (priv->directories, file); |
| 276 |
|
| 277 |
monitor = g_file_monitor_directory (file, |
| 278 |
G_FILE_MONITOR_NONE, |
| 279 |
NULL, |
| 280 |
&error); |
| 281 |
if (monitor == NULL) |
| 282 |
{ |
| 283 |
g_warning ("Error monitoring actions directory: %s", error->message); |
| 284 |
g_error_free (error); |
| 285 |
} |
| 286 |
else |
| 287 |
{ |
| 288 |
g_signal_connect (monitor, |
| 289 |
"changed", |
| 290 |
(GCallback) dir_monitor_changed, |
| 291 |
pool); |
| 292 |
priv->dir_monitors = g_list_prepend (priv->dir_monitors, monitor); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
priv->directories = g_list_reverse(priv->directories); |
| 297 |
priv->dir_monitors = g_list_reverse(priv->dir_monitors); |
| 298 |
|
| 299 |
break; |
| 300 |
} |
| 301 |
default: |
| 302 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 303 |
break; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
static void |
| 308 |
polkit_backend_action_pool_class_init (PolkitBackendActionPoolClass *klass) |
| 309 |
{ |
| 310 |
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
| 311 |
|
| 312 |
gobject_class->get_property = polkit_backend_action_pool_get_property; |
| 313 |
gobject_class->set_property = polkit_backend_action_pool_set_property; |
| 314 |
gobject_class->finalize = polkit_backend_action_pool_finalize; |
| 315 |
|
| 316 |
/** |
| 317 |
* PolkitBackendActionPool:directory: |
| 318 |
* |
| 319 |
* The directory to load action description files from. |
| 320 |
*/ |
| 321 |
g_object_class_install_property (gobject_class, |
| 322 |
PROP_DIRECTORIES, |
| 323 |
g_param_spec_boxed ("directories", |
| 324 |
"Directories", |
| 325 |
"Directories to load action description files from", |
| 326 |
G_TYPE_STRV, |
| 327 |
G_PARAM_READWRITE | |
| 328 |
G_PARAM_CONSTRUCT_ONLY | |
| 329 |
G_PARAM_STATIC_NAME | |
| 330 |
G_PARAM_STATIC_NICK | |
| 331 |
G_PARAM_STATIC_BLURB)); |
| 332 |
|
| 333 |
/** |
| 334 |
* PolkitBackendActionPool::changed: |
| 335 |
* @action_pool: A #PolkitBackendActionPool. |
| 336 |
* |
| 337 |
* Emitted when action files in the supplied directory changes. |
| 338 |
*/ |
| 339 |
signals[CHANGED_SIGNAL] = g_signal_new ("changed", |
| 340 |
POLKIT_BACKEND_TYPE_ACTION_POOL, |
| 341 |
G_SIGNAL_RUN_LAST, |
| 342 |
0, /* class offset */ |
| 343 |
NULL, /* accumulator */ |
| 344 |
NULL, /* accumulator data */ |
| 345 |
g_cclosure_marshal_VOID__VOID, |
| 346 |
G_TYPE_NONE, |
| 347 |
0); |
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* polkit_backend_action_pool_new: |
| 352 |
* @directory: A #GFile for the directory holding PolicyKit action description files. |
| 353 |
* |
| 354 |
* Creates a new #PolkitBackendPool that can be used for looking up #PolkitActionDescription objects. |
| 355 |
* |
| 356 |
* Returns: A #PolkitBackendActionPool. Free with g_object_unref(). |
| 357 |
**/ |
| 358 |
PolkitBackendActionPool * |
| 359 |
polkit_backend_action_pool_new (const gchar **directories) |
| 360 |
{ |
| 361 |
PolkitBackendActionPool *pool; |
| 362 |
|
| 363 |
pool = POLKIT_BACKEND_ACTION_POOL (g_object_new (POLKIT_BACKEND_TYPE_ACTION_POOL, |
| 364 |
"directories", directories, |
| 365 |
NULL)); |
| 366 |
|
| 367 |
return pool; |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* polkit_backend_action_pool_get_action: |
| 372 |
* @pool: A #PolkitBackendActionPool. |
| 373 |
* @action_id: A PolicyKit action identifier. |
| 374 |
* @locale: The locale to get descriptions for or %NULL for system locale. |
| 375 |
* |
| 376 |
* Gets a #PolkitActionDescription object describing the action with identifier @action_id. |
| 377 |
* |
| 378 |
* Returns: A #PolkitActionDescription (free with g_object_unref()) or %NULL |
| 379 |
* if @action_id isn't registered or valid. |
| 380 |
**/ |
| 381 |
PolkitActionDescription * |
| 382 |
polkit_backend_action_pool_get_action (PolkitBackendActionPool *pool, |
| 383 |
const gchar *action_id, |
| 384 |
const gchar *locale) |
| 385 |
{ |
| 386 |
PolkitBackendActionPoolPrivate *priv; |
| 387 |
PolkitActionDescription *ret; |
| 388 |
ParsedAction *parsed_action; |
| 389 |
const gchar *description; |
| 390 |
const gchar *message; |
| 391 |
|
| 392 |
g_return_val_if_fail (POLKIT_BACKEND_IS_ACTION_POOL (pool), NULL); |
| 393 |
|
| 394 |
priv = polkit_backend_action_pool_get_instance_private (pool); |
| 395 |
|
| 396 |
/* TODO: just compute the name of the expected file and ensure it's parsed */ |
| 397 |
ensure_all_files (pool); |
| 398 |
|
| 399 |
ret = NULL; |
| 400 |
|
| 401 |
parsed_action = g_hash_table_lookup (priv->parsed_actions, action_id); |
| 402 |
if (parsed_action == NULL) |
| 403 |
{ |
| 404 |
g_warning ("Unknown action_id '%s'", action_id); |
| 405 |
goto out; |
| 406 |
} |
| 407 |
|
| 408 |
description = _localize (parsed_action->localized_description, |
| 409 |
parsed_action->description, |
| 410 |
parsed_action->description_domain, |
| 411 |
locale); |
| 412 |
message = _localize (parsed_action->localized_message, |
| 413 |
parsed_action->message, |
| 414 |
parsed_action->message_domain, |
| 415 |
locale); |
| 416 |
|
| 417 |
ret = polkit_action_description_new (action_id, |
| 418 |
description, |
| 419 |
message, |
| 420 |
parsed_action->vendor_name, |
| 421 |
parsed_action->vendor_url, |
| 422 |
parsed_action->icon_name, |
| 423 |
parsed_action->implicit_authorization_any, |
| 424 |
parsed_action->implicit_authorization_inactive, |
| 425 |
parsed_action->implicit_authorization_active, |
| 426 |
parsed_action->annotations); |
| 427 |
|
| 428 |
out: |
| 429 |
return ret; |
| 430 |
} |
| 431 |
|
| 432 |
/** |
| 433 |
* polkit_backend_action_pool_get_all_actions: |
| 434 |
* @pool: A #PolkitBackendActionPool. |
| 435 |
* @locale: The locale to get descriptions for or %NULL for system locale. |
| 436 |
* |
| 437 |
* Gets all registered PolicyKit action descriptions from @pool with strings for @locale. |
| 438 |
* |
| 439 |
* Returns: A #GList of #PolkitActionDescription objects. This list |
| 440 |
* should be freed with g_list_free() after each element have |
| 441 |
* been unreffed with g_object_unref(). |
| 442 |
**/ |
| 443 |
GList * |
| 444 |
polkit_backend_action_pool_get_all_actions (PolkitBackendActionPool *pool, |
| 445 |
const gchar *locale) |
| 446 |
{ |
| 447 |
GList *ret; |
| 448 |
PolkitBackendActionPoolPrivate *priv; |
| 449 |
GHashTableIter hash_iter; |
| 450 |
const gchar *action_id; |
| 451 |
|
| 452 |
g_return_val_if_fail (POLKIT_BACKEND_IS_ACTION_POOL (pool), NULL); |
| 453 |
|
| 454 |
priv = polkit_backend_action_pool_get_instance_private (pool); |
| 455 |
|
| 456 |
ensure_all_files (pool); |
| 457 |
|
| 458 |
ret = NULL; |
| 459 |
|
| 460 |
g_hash_table_iter_init (&hash_iter, priv->parsed_actions); |
| 461 |
while (g_hash_table_iter_next (&hash_iter, (gpointer) &action_id, NULL)) |
| 462 |
{ |
| 463 |
PolkitActionDescription *action_desc; |
| 464 |
|
| 465 |
action_desc = polkit_backend_action_pool_get_action (pool, |
| 466 |
action_id, |
| 467 |
locale); |
| 468 |
|
| 469 |
if (action_desc != NULL) |
| 470 |
ret = g_list_prepend (ret, action_desc); |
| 471 |
} |
| 472 |
|
| 473 |
ret = g_list_reverse (ret); |
| 474 |
|
| 475 |
return ret; |
| 476 |
} |
| 477 |
|
| 478 |
/** |
| 479 |
* polkit_backend_action_pool_reload: |
| 480 |
* @pool: A #PolkitBackendActionPool. |
| 481 |
* |
| 482 |
* Reload all PolicyKit actions in @pool. |
| 483 |
**/ |
| 484 |
void |
| 485 |
polkit_backend_action_pool_reload (PolkitBackendActionPool *pool) |
| 486 |
{ |
| 487 |
PolkitBackendActionPoolPrivate *priv; |
| 488 |
|
| 489 |
if (!POLKIT_BACKEND_IS_ACTION_POOL (pool)) |
| 490 |
return; |
| 491 |
|
| 492 |
priv = polkit_backend_action_pool_get_instance_private (pool); |
| 493 |
|
| 494 |
g_hash_table_remove_all (priv->parsed_files); |
| 495 |
g_hash_table_remove_all (priv->parsed_actions); |
| 496 |
priv->has_loaded_all_files = FALSE; |
| 497 |
ensure_all_files (pool); |
| 498 |
} |
| 499 |
|
| 500 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 501 |
|
| 502 |
static void |
| 503 |
ensure_file (PolkitBackendActionPool *pool, |
| 504 |
GFile *file) |
| 505 |
{ |
| 506 |
PolkitBackendActionPoolPrivate *priv; |
| 507 |
gchar *contents; |
| 508 |
GError *error = NULL; |
| 509 |
gchar *path; |
| 510 |
gchar *basename; |
| 511 |
|
| 512 |
priv = polkit_backend_action_pool_get_instance_private (pool); |
| 513 |
|
| 514 |
path = g_file_get_path (file); |
| 515 |
basename = g_file_get_basename (file); |
| 516 |
|
| 517 |
if (g_hash_table_lookup_extended (priv->parsed_files, basename, NULL, NULL) == TRUE) |
| 518 |
goto out; |
| 519 |
|
| 520 |
if (!g_file_load_contents (file, |
| 521 |
NULL, |
| 522 |
&contents, |
| 523 |
NULL, |
| 524 |
NULL, |
| 525 |
&error)) |
| 526 |
{ |
| 527 |
g_warning ("Error loading file with path '%s': %s", path, error->message); |
| 528 |
goto out; |
| 529 |
} |
| 530 |
|
| 531 |
if (!process_policy_file (pool, |
| 532 |
contents, |
| 533 |
&error)) |
| 534 |
{ |
| 535 |
g_warning ("Error parsing file with path '%s': %s", path, error->message); |
| 536 |
g_free (contents); |
| 537 |
goto out; |
| 538 |
} |
| 539 |
|
| 540 |
g_free (contents); |
| 541 |
|
| 542 |
/* steal basename */ |
| 543 |
g_hash_table_insert (priv->parsed_files, basename, NULL); |
| 544 |
basename = NULL; |
| 545 |
|
| 546 |
out: |
| 547 |
g_free (basename); |
| 548 |
g_free (path); |
| 549 |
} |
| 550 |
|
| 551 |
static void |
| 552 |
ensure_all_files (PolkitBackendActionPool *pool) |
| 553 |
{ |
| 554 |
PolkitBackendActionPoolPrivate *priv; |
| 555 |
|
| 556 |
GList *files = NULL; |
| 557 |
|
| 558 |
priv = polkit_backend_action_pool_get_instance_private (pool); |
| 559 |
|
| 560 |
if (priv->has_loaded_all_files) |
| 561 |
return; |
| 562 |
|
| 563 |
for (GList *l = priv->directories; l != NULL; l = l->next) |
| 564 |
{ |
| 565 |
GError *error = NULL; |
| 566 |
GFileEnumerator *enumerator; |
| 567 |
|
| 568 |
GFile* file = l->data; |
| 569 |
|
| 570 |
char *dir_name = g_file_get_path(file); |
| 571 |
|
| 572 |
enumerator = g_file_enumerate_children (file, |
| 573 |
G_FILE_ATTRIBUTE_STANDARD_NAME, |
| 574 |
G_FILE_QUERY_INFO_NONE, |
| 575 |
NULL, |
| 576 |
&error); |
| 577 |
if (error != NULL) |
| 578 |
{ |
| 579 |
/* These directories are not required, do not print a warning if they are missing */ |
| 580 |
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND) || |
| 581 |
!g_str_has_suffix(dir_name, "actions")) |
| 582 |
g_warning ("Error enumerating files in %s: %s", dir_name, error->message); |
| 583 |
g_error_free (error); |
| 584 |
} |
| 585 |
else |
| 586 |
{ |
| 587 |
GFileInfo *file_info; |
| 588 |
while ((file_info = g_file_enumerator_next_file (enumerator, NULL, &error)) != NULL) |
| 589 |
{ |
| 590 |
const gchar *name = g_file_info_get_name (file_info); |
| 591 |
/* only consider files with the right suffix */ |
| 592 |
if (g_str_has_suffix (name, ".policy")) |
| 593 |
files = g_list_prepend (files, g_strdup_printf ("%s/%s", dir_name, name)); |
| 594 |
|
| 595 |
g_object_unref (file_info); |
| 596 |
} /* for all files */ |
| 597 |
} |
| 598 |
|
| 599 |
if (enumerator) |
| 600 |
g_object_unref (enumerator); |
| 601 |
g_free (dir_name); |
| 602 |
} |
| 603 |
|
| 604 |
/* standard sorting places /etc before /usr as desired */ |
| 605 |
files = g_list_sort (files, (GCompareFunc) g_strcmp0); |
| 606 |
|
| 607 |
for (GList *l = files; l != NULL; l = l->next) |
| 608 |
{ |
| 609 |
GFile *file = g_file_new_for_path((gchar *)l->data); |
| 610 |
ensure_file (pool, file); |
| 611 |
g_object_unref (file); |
| 612 |
} |
| 613 |
|
| 614 |
g_list_free_full (files, g_free); |
| 615 |
|
| 616 |
priv->has_loaded_all_files = TRUE; |
| 617 |
} |
| 618 |
|
| 619 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 620 |
|
| 621 |
enum { |
| 622 |
STATE_NONE, |
| 623 |
STATE_UNKNOWN_TAG, |
| 624 |
STATE_IN_POLICY_CONFIG, |
| 625 |
STATE_IN_POLICY_VENDOR, |
| 626 |
STATE_IN_POLICY_VENDOR_URL, |
| 627 |
STATE_IN_POLICY_ICON_NAME, |
| 628 |
STATE_IN_ACTION, |
| 629 |
STATE_IN_ACTION_DESCRIPTION, |
| 630 |
STATE_IN_ACTION_MESSAGE, |
| 631 |
STATE_IN_ACTION_VENDOR, |
| 632 |
STATE_IN_ACTION_VENDOR_URL, |
| 633 |
STATE_IN_ACTION_ICON_NAME, |
| 634 |
STATE_IN_DEFAULTS, |
| 635 |
STATE_IN_DEFAULTS_ALLOW_ANY, |
| 636 |
STATE_IN_DEFAULTS_ALLOW_INACTIVE, |
| 637 |
STATE_IN_DEFAULTS_ALLOW_ACTIVE, |
| 638 |
STATE_IN_ANNOTATE |
| 639 |
}; |
| 640 |
|
| 641 |
#define PARSER_MAX_DEPTH 32 |
| 642 |
|
| 643 |
typedef struct { |
| 644 |
XML_Parser parser; |
| 645 |
int state; |
| 646 |
int state_stack[PARSER_MAX_DEPTH]; |
| 647 |
int stack_depth; |
| 648 |
|
| 649 |
char *global_vendor; |
| 650 |
char *global_vendor_url; |
| 651 |
char *global_icon_name; |
| 652 |
|
| 653 |
char *action_id; |
| 654 |
char *vendor; |
| 655 |
char *vendor_url; |
| 656 |
char *icon_name; |
| 657 |
|
| 658 |
PolkitImplicitAuthorization implicit_authorization_any; |
| 659 |
PolkitImplicitAuthorization implicit_authorization_inactive; |
| 660 |
PolkitImplicitAuthorization implicit_authorization_active; |
| 661 |
|
| 662 |
GHashTable *policy_descriptions; |
| 663 |
GHashTable *policy_messages; |
| 664 |
|
| 665 |
char *policy_description_nolang; |
| 666 |
char *policy_description_domain; |
| 667 |
char *policy_message_nolang; |
| 668 |
char *policy_message_domain; |
| 669 |
|
| 670 |
/* the value of xml:lang for the thing we're reading in _cdata() */ |
| 671 |
char *elem_lang; |
| 672 |
|
| 673 |
/* the value of gettext-domain for the thing we're reading in _cdata() */ |
| 674 |
char *elem_domain; |
| 675 |
|
| 676 |
char *annotate_key; |
| 677 |
GHashTable *annotations; |
| 678 |
|
| 679 |
PolkitBackendActionPool *pool; |
| 680 |
} ParserData; |
| 681 |
|
| 682 |
static void |
| 683 |
pd_unref_action_data (ParserData *pd) |
| 684 |
{ |
| 685 |
g_free (pd->action_id); |
| 686 |
pd->action_id = NULL; |
| 687 |
|
| 688 |
g_free (pd->vendor); |
| 689 |
pd->vendor = NULL; |
| 690 |
g_free (pd->vendor_url); |
| 691 |
pd->vendor_url = NULL; |
| 692 |
g_free (pd->icon_name); |
| 693 |
pd->icon_name = NULL; |
| 694 |
|
| 695 |
g_free (pd->policy_description_nolang); |
| 696 |
pd->policy_description_nolang = NULL; |
| 697 |
g_free (pd->policy_description_domain); |
| 698 |
pd->policy_description_domain = NULL; |
| 699 |
g_free (pd->policy_message_nolang); |
| 700 |
pd->policy_message_nolang = NULL; |
| 701 |
g_free (pd->policy_message_domain); |
| 702 |
pd->policy_message_domain = NULL; |
| 703 |
if (pd->policy_descriptions != NULL) |
| 704 |
{ |
| 705 |
g_hash_table_unref (pd->policy_descriptions); |
| 706 |
pd->policy_descriptions = NULL; |
| 707 |
} |
| 708 |
if (pd->policy_messages != NULL) |
| 709 |
{ |
| 710 |
g_hash_table_unref (pd->policy_messages); |
| 711 |
pd->policy_messages = NULL; |
| 712 |
} |
| 713 |
g_free (pd->annotate_key); |
| 714 |
pd->annotate_key = NULL; |
| 715 |
if (pd->annotations != NULL) |
| 716 |
{ |
| 717 |
g_hash_table_unref (pd->annotations); |
| 718 |
pd->annotations = NULL; |
| 719 |
} |
| 720 |
g_free (pd->elem_lang); |
| 721 |
pd->elem_lang = NULL; |
| 722 |
g_free (pd->elem_domain); |
| 723 |
pd->elem_domain = NULL; |
| 724 |
} |
| 725 |
|
| 726 |
static void |
| 727 |
pd_unref_data (ParserData *pd) |
| 728 |
{ |
| 729 |
pd_unref_action_data (pd); |
| 730 |
|
| 731 |
g_free (pd->global_vendor); |
| 732 |
pd->global_vendor = NULL; |
| 733 |
g_free (pd->global_vendor_url); |
| 734 |
pd->global_vendor_url = NULL; |
| 735 |
g_free (pd->global_icon_name); |
| 736 |
pd->global_icon_name = NULL; |
| 737 |
} |
| 738 |
|
| 739 |
static void |
| 740 |
_start (void *data, const char *el, const char **attr) |
| 741 |
{ |
| 742 |
guint state; |
| 743 |
guint num_attr; |
| 744 |
ParserData *pd = data; |
| 745 |
|
| 746 |
if (pd->stack_depth < 0 || pd->stack_depth >= PARSER_MAX_DEPTH) |
| 747 |
{ |
| 748 |
g_warning ("XML parsing reached max depth?"); |
| 749 |
goto error; |
| 750 |
} |
| 751 |
|
| 752 |
for (num_attr = 0; attr[num_attr] != NULL; num_attr++) |
| 753 |
; |
| 754 |
|
| 755 |
state = STATE_NONE; |
| 756 |
|
| 757 |
switch (pd->state) |
| 758 |
{ |
| 759 |
case STATE_NONE: |
| 760 |
if (strcmp (el, "policyconfig") == 0) |
| 761 |
{ |
| 762 |
state = STATE_IN_POLICY_CONFIG; |
| 763 |
} |
| 764 |
break; |
| 765 |
|
| 766 |
case STATE_IN_POLICY_CONFIG: |
| 767 |
if (strcmp (el, "action") == 0) |
| 768 |
{ |
| 769 |
if (num_attr != 2 || strcmp (attr[0], "id") != 0) |
| 770 |
goto error; |
| 771 |
state = STATE_IN_ACTION; |
| 772 |
|
| 773 |
//if (!polkit_action_validate_id (attr[1])) |
| 774 |
// goto error; |
| 775 |
|
| 776 |
pd_unref_action_data (pd); |
| 777 |
pd->action_id = g_strdup (attr[1]); |
| 778 |
pd->policy_descriptions = g_hash_table_new_full (g_str_hash, |
| 779 |
g_str_equal, |
| 780 |
g_free, |
| 781 |
g_free); |
| 782 |
pd->policy_messages = g_hash_table_new_full (g_str_hash, |
| 783 |
g_str_equal, |
| 784 |
g_free, |
| 785 |
g_free); |
| 786 |
pd->annotations = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); |
| 787 |
/* initialize defaults */ |
| 788 |
pd->implicit_authorization_any = POLKIT_IMPLICIT_AUTHORIZATION_NOT_AUTHORIZED; |
| 789 |
pd->implicit_authorization_inactive = POLKIT_IMPLICIT_AUTHORIZATION_NOT_AUTHORIZED; |
| 790 |
pd->implicit_authorization_active = POLKIT_IMPLICIT_AUTHORIZATION_NOT_AUTHORIZED; |
| 791 |
} |
| 792 |
else if (strcmp (el, "vendor") == 0 && num_attr == 0) |
| 793 |
{ |
| 794 |
state = STATE_IN_POLICY_VENDOR; |
| 795 |
} |
| 796 |
else if (strcmp (el, "vendor_url") == 0 && num_attr == 0) |
| 797 |
{ |
| 798 |
state = STATE_IN_POLICY_VENDOR_URL; |
| 799 |
} |
| 800 |
else if (strcmp (el, "icon_name") == 0 && num_attr == 0) |
| 801 |
{ |
| 802 |
state = STATE_IN_POLICY_ICON_NAME; |
| 803 |
} |
| 804 |
break; |
| 805 |
|
| 806 |
case STATE_IN_ACTION: |
| 807 |
if (strcmp (el, "defaults") == 0) |
| 808 |
{ |
| 809 |
state = STATE_IN_DEFAULTS; |
| 810 |
} |
| 811 |
else if (strcmp (el, "description") == 0) |
| 812 |
{ |
| 813 |
if (num_attr == 2 && strcmp (attr[0], "xml:lang") == 0) |
| 814 |
{ |
| 815 |
pd->elem_lang = g_strdup (attr[1]); |
| 816 |
} |
| 817 |
if (num_attr == 2 && strcmp (attr[0], "gettext-domain") == 0) |
| 818 |
{ |
| 819 |
pd->elem_domain = g_strdup (attr[1]); |
| 820 |
} |
| 821 |
state = STATE_IN_ACTION_DESCRIPTION; |
| 822 |
} |
| 823 |
else if (strcmp (el, "message") == 0) |
| 824 |
{ |
| 825 |
if (num_attr == 2 && strcmp (attr[0], "xml:lang") == 0) |
| 826 |
{ |
| 827 |
pd->elem_lang = g_strdup (attr[1]); |
| 828 |
} |
| 829 |
if (num_attr == 2 && strcmp (attr[0], "gettext-domain") == 0) |
| 830 |
{ |
| 831 |
pd->elem_domain = g_strdup (attr[1]); |
| 832 |
} |
| 833 |
state = STATE_IN_ACTION_MESSAGE; |
| 834 |
} |
| 835 |
else if (strcmp (el, "vendor") == 0 && num_attr == 0) |
| 836 |
{ |
| 837 |
state = STATE_IN_ACTION_VENDOR; |
| 838 |
} |
| 839 |
else if (strcmp (el, "vendor_url") == 0 && num_attr == 0) |
| 840 |
{ |
| 841 |
state = STATE_IN_ACTION_VENDOR_URL; |
| 842 |
} |
| 843 |
else if (strcmp (el, "icon_name") == 0 && num_attr == 0) |
| 844 |
{ |
| 845 |
state = STATE_IN_ACTION_ICON_NAME; |
| 846 |
} |
| 847 |
else if (strcmp (el, "annotate") == 0) |
| 848 |
{ |
| 849 |
if (num_attr != 2 || strcmp (attr[0], "key") != 0) |
| 850 |
goto error; |
| 851 |
|
| 852 |
state = STATE_IN_ANNOTATE; |
| 853 |
|
| 854 |
g_free (pd->annotate_key); |
| 855 |
pd->annotate_key = g_strdup (attr[1]); |
| 856 |
} |
| 857 |
break; |
| 858 |
|
| 859 |
case STATE_IN_DEFAULTS: |
| 860 |
if (strcmp (el, "allow_any") == 0) |
| 861 |
state = STATE_IN_DEFAULTS_ALLOW_ANY; |
| 862 |
else if (strcmp (el, "allow_inactive") == 0) |
| 863 |
state = STATE_IN_DEFAULTS_ALLOW_INACTIVE; |
| 864 |
else if (strcmp (el, "allow_active") == 0) |
| 865 |
state = STATE_IN_DEFAULTS_ALLOW_ACTIVE; |
| 866 |
break; |
| 867 |
|
| 868 |
default: |
| 869 |
break; |
| 870 |
} |
| 871 |
|
| 872 |
if (state == STATE_NONE) |
| 873 |
{ |
| 874 |
g_warning ("skipping unknown tag <%s> at line %d", |
| 875 |
el, (int) XML_GetCurrentLineNumber (pd->parser)); |
| 876 |
state = STATE_UNKNOWN_TAG; |
| 877 |
} |
| 878 |
|
| 879 |
pd->state = state; |
| 880 |
pd->state_stack[pd->stack_depth] = pd->state; |
| 881 |
pd->stack_depth++; |
| 882 |
return; |
| 883 |
|
| 884 |
error: |
| 885 |
XML_StopParser (pd->parser, FALSE); |
| 886 |
} |
| 887 |
|
| 888 |
static gboolean |
| 889 |
_validate_icon_name (const gchar *icon_name) |
| 890 |
{ |
| 891 |
guint n; |
| 892 |
gboolean ret; |
| 893 |
gsize len; |
| 894 |
|
| 895 |
ret = FALSE; |
| 896 |
|
| 897 |
len = strlen (icon_name); |
| 898 |
|
| 899 |
/* check for common suffixes */ |
| 900 |
if (g_str_has_suffix (icon_name, ".png")) |
| 901 |
goto out; |
| 902 |
if (g_str_has_suffix (icon_name, ".jpg")) |
| 903 |
goto out; |
| 904 |
|
| 905 |
/* icon name cannot be a path */ |
| 906 |
for (n = 0; n < len; n++) |
| 907 |
{ |
| 908 |
if (icon_name [n] == '/') |
| 909 |
{ |
| 910 |
goto out; |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
ret = TRUE; |
| 915 |
|
| 916 |
out: |
| 917 |
return ret; |
| 918 |
} |
| 919 |
|
| 920 |
static void |
| 921 |
_cdata (void *data, const char *s, int len) |
| 922 |
{ |
| 923 |
gchar *str; |
| 924 |
ParserData *pd = data; |
| 925 |
|
| 926 |
str = g_strndup (s, len); |
| 927 |
|
| 928 |
switch (pd->state) |
| 929 |
{ |
| 930 |
case STATE_IN_ACTION_DESCRIPTION: |
| 931 |
if (pd->elem_lang == NULL) |
| 932 |
{ |
| 933 |
g_free (pd->policy_description_nolang); |
| 934 |
pd->policy_description_nolang = str; |
| 935 |
pd->policy_description_domain = g_strdup (pd->elem_domain); |
| 936 |
str = NULL; |
| 937 |
} |
| 938 |
else |
| 939 |
{ |
| 940 |
g_hash_table_insert (pd->policy_descriptions, |
| 941 |
g_strdup (pd->elem_lang), |
| 942 |
str); |
| 943 |
str = NULL; |
| 944 |
} |
| 945 |
break; |
| 946 |
|
| 947 |
case STATE_IN_ACTION_MESSAGE: |
| 948 |
if (pd->elem_lang == NULL) |
| 949 |
{ |
| 950 |
g_free (pd->policy_message_nolang); |
| 951 |
pd->policy_message_nolang = str; |
| 952 |
pd->policy_message_domain = g_strdup (pd->elem_domain); |
| 953 |
str = NULL; |
| 954 |
} |
| 955 |
else |
| 956 |
{ |
| 957 |
g_hash_table_insert (pd->policy_messages, |
| 958 |
g_strdup (pd->elem_lang), |
| 959 |
str); |
| 960 |
str = NULL; |
| 961 |
} |
| 962 |
break; |
| 963 |
|
| 964 |
case STATE_IN_POLICY_VENDOR: |
| 965 |
g_free (pd->global_vendor); |
| 966 |
pd->global_vendor = str; |
| 967 |
str = NULL; |
| 968 |
break; |
| 969 |
|
| 970 |
case STATE_IN_POLICY_VENDOR_URL: |
| 971 |
g_free (pd->global_vendor_url); |
| 972 |
pd->global_vendor_url = str; |
| 973 |
str = NULL; |
| 974 |
break; |
| 975 |
|
| 976 |
case STATE_IN_POLICY_ICON_NAME: |
| 977 |
if (! _validate_icon_name (str)) |
| 978 |
{ |
| 979 |
g_warning ("Icon name '%s' is invalid", str); |
| 980 |
goto error; |
| 981 |
} |
| 982 |
g_free (pd->global_icon_name); |
| 983 |
pd->global_icon_name = str; |
| 984 |
str = NULL; |
| 985 |
break; |
| 986 |
|
| 987 |
case STATE_IN_ACTION_VENDOR: |
| 988 |
g_free (pd->vendor); |
| 989 |
pd->vendor = str; |
| 990 |
str = NULL; |
| 991 |
break; |
| 992 |
|
| 993 |
case STATE_IN_ACTION_VENDOR_URL: |
| 994 |
g_free (pd->vendor_url); |
| 995 |
pd->vendor_url = str; |
| 996 |
str = NULL; |
| 997 |
break; |
| 998 |
|
| 999 |
case STATE_IN_ACTION_ICON_NAME: |
| 1000 |
if (! _validate_icon_name (str)) |
| 1001 |
{ |
| 1002 |
g_warning ("Icon name '%s' is invalid", str); |
| 1003 |
goto error; |
| 1004 |
} |
| 1005 |
|
| 1006 |
g_free (pd->icon_name); |
| 1007 |
pd->icon_name = str; |
| 1008 |
str = NULL; |
| 1009 |
break; |
| 1010 |
|
| 1011 |
case STATE_IN_DEFAULTS_ALLOW_ANY: |
| 1012 |
if (!polkit_implicit_authorization_from_string (str, &pd->implicit_authorization_any)) |
| 1013 |
goto error; |
| 1014 |
break; |
| 1015 |
|
| 1016 |
case STATE_IN_DEFAULTS_ALLOW_INACTIVE: |
| 1017 |
if (!polkit_implicit_authorization_from_string (str, &pd->implicit_authorization_inactive)) |
| 1018 |
goto error; |
| 1019 |
break; |
| 1020 |
|
| 1021 |
case STATE_IN_DEFAULTS_ALLOW_ACTIVE: |
| 1022 |
if (!polkit_implicit_authorization_from_string (str, &pd->implicit_authorization_active)) |
| 1023 |
goto error; |
| 1024 |
break; |
| 1025 |
|
| 1026 |
case STATE_IN_ANNOTATE: |
| 1027 |
g_hash_table_insert (pd->annotations, g_strdup (pd->annotate_key), str); |
| 1028 |
str = NULL; |
| 1029 |
break; |
| 1030 |
|
| 1031 |
default: |
| 1032 |
break; |
| 1033 |
} |
| 1034 |
|
| 1035 |
g_free (str); |
| 1036 |
return; |
| 1037 |
|
| 1038 |
error: |
| 1039 |
g_free (str); |
| 1040 |
XML_StopParser (pd->parser, FALSE); |
| 1041 |
} |
| 1042 |
|
| 1043 |
static void |
| 1044 |
_end (void *data, const char *el) |
| 1045 |
{ |
| 1046 |
ParserData *pd = data; |
| 1047 |
|
| 1048 |
g_free (pd->elem_lang); |
| 1049 |
pd->elem_lang = NULL; |
| 1050 |
g_free (pd->elem_domain); |
| 1051 |
pd->elem_domain = NULL; |
| 1052 |
|
| 1053 |
switch (pd->state) |
| 1054 |
{ |
| 1055 |
case STATE_IN_ACTION: |
| 1056 |
{ |
| 1057 |
gchar *vendor; |
| 1058 |
gchar *vendor_url; |
| 1059 |
gchar *icon_name; |
| 1060 |
ParsedAction *action; |
| 1061 |
PolkitBackendActionPoolPrivate *priv; |
| 1062 |
|
| 1063 |
priv = polkit_backend_action_pool_get_instance_private (pd->pool); |
| 1064 |
|
| 1065 |
vendor = pd->vendor; |
| 1066 |
if (vendor == NULL) |
| 1067 |
vendor = pd->global_vendor; |
| 1068 |
|
| 1069 |
vendor_url = pd->vendor_url; |
| 1070 |
if (vendor_url == NULL) |
| 1071 |
vendor_url = pd->global_vendor_url; |
| 1072 |
|
| 1073 |
icon_name = pd->icon_name; |
| 1074 |
if (icon_name == NULL) |
| 1075 |
icon_name = pd->global_icon_name; |
| 1076 |
|
| 1077 |
action = g_new0 (ParsedAction, 1); |
| 1078 |
action->vendor_name = g_strdup (vendor); |
| 1079 |
action->vendor_url = g_strdup (vendor_url); |
| 1080 |
action->icon_name = g_strdup (icon_name); |
| 1081 |
action->description = g_strdup (pd->policy_description_nolang); |
| 1082 |
action->description_domain = g_strdup (pd->policy_description_domain); |
| 1083 |
action->message = g_strdup (pd->policy_message_nolang); |
| 1084 |
action->message_domain = g_strdup (pd->policy_message_domain); |
| 1085 |
|
| 1086 |
action->localized_description = pd->policy_descriptions; |
| 1087 |
action->localized_message = pd->policy_messages; |
| 1088 |
action->annotations = pd->annotations; |
| 1089 |
|
| 1090 |
action->implicit_authorization_any = pd->implicit_authorization_any; |
| 1091 |
action->implicit_authorization_inactive = pd->implicit_authorization_inactive; |
| 1092 |
action->implicit_authorization_active = pd->implicit_authorization_active; |
| 1093 |
|
| 1094 |
g_hash_table_insert (priv->parsed_actions, g_strdup (pd->action_id), |
| 1095 |
action); |
| 1096 |
|
| 1097 |
/* we steal these hash tables */ |
| 1098 |
pd->annotations = NULL; |
| 1099 |
pd->policy_descriptions = NULL; |
| 1100 |
pd->policy_messages = NULL; |
| 1101 |
|
| 1102 |
break; |
| 1103 |
} |
| 1104 |
|
| 1105 |
default: |
| 1106 |
break; |
| 1107 |
} |
| 1108 |
|
| 1109 |
--pd->stack_depth; |
| 1110 |
if (pd->stack_depth < 0 || pd->stack_depth >= PARSER_MAX_DEPTH) |
| 1111 |
{ |
| 1112 |
g_warning ("reached max depth?"); |
| 1113 |
goto error; |
| 1114 |
} |
| 1115 |
|
| 1116 |
if (pd->stack_depth > 0) |
| 1117 |
pd->state = pd->state_stack[pd->stack_depth - 1]; |
| 1118 |
else |
| 1119 |
pd->state = STATE_NONE; |
| 1120 |
|
| 1121 |
return; |
| 1122 |
|
| 1123 |
error: |
| 1124 |
XML_StopParser (pd->parser, FALSE); |
| 1125 |
} |
| 1126 |
|
| 1127 |
|
| 1128 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 1129 |
|
| 1130 |
static gboolean |
| 1131 |
process_policy_file (PolkitBackendActionPool *pool, |
| 1132 |
const gchar *xml, |
| 1133 |
GError **error) |
| 1134 |
{ |
| 1135 |
ParserData pd; |
| 1136 |
int xml_res; |
| 1137 |
|
| 1138 |
/* clear parser data */ |
| 1139 |
memset (&pd, 0, sizeof (ParserData)); |
| 1140 |
|
| 1141 |
pd.pool = pool; |
| 1142 |
|
| 1143 |
pd.parser = XML_ParserCreate (NULL); |
| 1144 |
pd.stack_depth = 0; |
| 1145 |
XML_SetUserData (pd.parser, &pd); |
| 1146 |
XML_SetElementHandler (pd.parser, _start, _end); |
| 1147 |
XML_SetCharacterDataHandler (pd.parser, _cdata); |
| 1148 |
|
| 1149 |
/* init parser data */ |
| 1150 |
pd.state = STATE_NONE; |
| 1151 |
|
| 1152 |
xml_res = XML_Parse (pd.parser, xml, strlen (xml), 1); |
| 1153 |
|
| 1154 |
if (xml_res == 0) |
| 1155 |
{ |
| 1156 |
if (XML_GetErrorCode (pd.parser) == XML_ERROR_NO_MEMORY) |
| 1157 |
{ |
| 1158 |
abort (); |
| 1159 |
} |
| 1160 |
else |
| 1161 |
{ |
| 1162 |
g_set_error (error, |
| 1163 |
POLKIT_ERROR, |
| 1164 |
POLKIT_ERROR_FAILED, |
| 1165 |
"%d: parse error: %s", |
| 1166 |
(int) XML_GetCurrentLineNumber (pd.parser), |
| 1167 |
XML_ErrorString (XML_GetErrorCode (pd.parser))); |
| 1168 |
} |
| 1169 |
XML_ParserFree (pd.parser); |
| 1170 |
goto error; |
| 1171 |
} |
| 1172 |
|
| 1173 |
XML_ParserFree (pd.parser); |
| 1174 |
|
| 1175 |
pd_unref_data (&pd); |
| 1176 |
return TRUE; |
| 1177 |
|
| 1178 |
error: |
| 1179 |
pd_unref_data (&pd); |
| 1180 |
return FALSE; |
| 1181 |
} |
| 1182 |
|
| 1183 |
/** |
| 1184 |
* _localize: |
| 1185 |
* @translations: a mapping from xml:lang to the value, e.g. 'da' -> 'Smadre', 'en_CA' -> 'Punch, Aye!' |
| 1186 |
* @untranslated: the untranslated value, e.g. 'Punch' |
| 1187 |
* @domain: the gettext domain for this string. May be NULL. |
| 1188 |
* @lang: the locale we're interested in, e.g. 'da_DK', 'da', 'en_CA', 'en_US'; basically just $LANG |
| 1189 |
* with the encoding cut off. Maybe be NULL. |
| 1190 |
* |
| 1191 |
* Pick the correct translation to use. |
| 1192 |
* |
| 1193 |
* Returns: the localized string to use |
| 1194 |
*/ |
| 1195 |
static const gchar * |
| 1196 |
_localize (GHashTable *translations, |
| 1197 |
const gchar *untranslated, |
| 1198 |
const gchar *domain, |
| 1199 |
const gchar *lang) |
| 1200 |
{ |
| 1201 |
const gchar *result; |
| 1202 |
gchar **langs; |
| 1203 |
guint n; |
| 1204 |
|
| 1205 |
if (lang == NULL) |
| 1206 |
{ |
| 1207 |
result = untranslated; |
| 1208 |
goto out; |
| 1209 |
} |
| 1210 |
|
| 1211 |
/* If configured at build time, check gettext */ |
| 1212 |
#ifdef ENABLE_GETTEXT |
| 1213 |
if (domain != NULL) |
| 1214 |
{ |
| 1215 |
gchar *old_locale; |
| 1216 |
|
| 1217 |
old_locale = g_strdup (setlocale (LC_ALL, NULL)); |
| 1218 |
setlocale (LC_ALL, lang); |
| 1219 |
result = dgettext (domain, untranslated); |
| 1220 |
setlocale (LC_ALL, old_locale); |
| 1221 |
g_free (old_locale); |
| 1222 |
|
| 1223 |
if (result != NULL) |
| 1224 |
goto out; |
| 1225 |
} |
| 1226 |
#endif |
| 1227 |
|
| 1228 |
/* first see if we have the translation */ |
| 1229 |
result = (const char *) g_hash_table_lookup (translations, (void *) lang); |
| 1230 |
if (result != NULL) |
| 1231 |
goto out; |
| 1232 |
|
| 1233 |
/* we could have a translation for 'da' but lang=='da_DK'; cut off the last part and try again */ |
| 1234 |
langs = g_get_locale_variants (lang); |
| 1235 |
for (n = 0; langs[n] != NULL; n++) |
| 1236 |
{ |
| 1237 |
result = (const char *) g_hash_table_lookup (translations, (void *) langs[n]); |
| 1238 |
if (result != NULL) |
| 1239 |
break; |
| 1240 |
} |
| 1241 |
g_strfreev (langs); |
| 1242 |
if (result != NULL) |
| 1243 |
goto out; |
| 1244 |
|
| 1245 |
/* fall back to untranslated */ |
| 1246 |
result = untranslated; |
| 1247 |
|
| 1248 |
out: |
| 1249 |
return result; |
| 1250 |
} |