| 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 "polkitbackendcommon.h" |
| 23 |
|
| 24 |
static void |
| 25 |
utils_child_watch_from_release_cb (GPid pid, |
| 26 |
gint status, |
| 27 |
gpointer user_data) |
| 28 |
{ |
| 29 |
} |
| 30 |
|
| 31 |
static void |
| 32 |
utils_spawn_data_free (UtilsSpawnData *data) |
| 33 |
{ |
| 34 |
if (data->timeout_source != NULL) |
| 35 |
{ |
| 36 |
g_source_destroy (data->timeout_source); |
| 37 |
data->timeout_source = NULL; |
| 38 |
} |
| 39 |
|
| 40 |
/* Nuke the child, if necessary */ |
| 41 |
if (data->child_watch_source != NULL) |
| 42 |
{ |
| 43 |
g_source_destroy (data->child_watch_source); |
| 44 |
data->child_watch_source = NULL; |
| 45 |
} |
| 46 |
|
| 47 |
if (data->child_pid != 0) |
| 48 |
{ |
| 49 |
GSource *source; |
| 50 |
kill (data->child_pid, SIGTERM); |
| 51 |
/* OK, we need to reap for the child ourselves - we don't want |
| 52 |
* to use waitpid() because that might block the calling |
| 53 |
* thread (the child might handle SIGTERM and use several |
| 54 |
* seconds for cleanup/rollback). |
| 55 |
* |
| 56 |
* So we use GChildWatch instead. |
| 57 |
* |
| 58 |
* Avoid taking a references to ourselves. but note that we need |
| 59 |
* to pass the GSource so we can nuke it once handled. |
| 60 |
*/ |
| 61 |
source = g_child_watch_source_new (data->child_pid); |
| 62 |
g_source_set_callback (source, |
| 63 |
(GSourceFunc) utils_child_watch_from_release_cb, |
| 64 |
source, |
| 65 |
(GDestroyNotify) g_source_destroy); |
| 66 |
/* attach source to the global default main context */ |
| 67 |
g_source_attach (source, NULL); |
| 68 |
g_source_unref (source); |
| 69 |
data->child_pid = 0; |
| 70 |
} |
| 71 |
|
| 72 |
if (data->child_stdout != NULL) |
| 73 |
{ |
| 74 |
g_string_free (data->child_stdout, TRUE); |
| 75 |
data->child_stdout = NULL; |
| 76 |
} |
| 77 |
|
| 78 |
if (data->child_stderr != NULL) |
| 79 |
{ |
| 80 |
g_string_free (data->child_stderr, TRUE); |
| 81 |
data->child_stderr = NULL; |
| 82 |
} |
| 83 |
|
| 84 |
if (data->child_stdout_channel != NULL) |
| 85 |
{ |
| 86 |
g_io_channel_unref (data->child_stdout_channel); |
| 87 |
data->child_stdout_channel = NULL; |
| 88 |
} |
| 89 |
if (data->child_stderr_channel != NULL) |
| 90 |
{ |
| 91 |
g_io_channel_unref (data->child_stderr_channel); |
| 92 |
data->child_stderr_channel = NULL; |
| 93 |
} |
| 94 |
|
| 95 |
if (data->child_stdout_source != NULL) |
| 96 |
{ |
| 97 |
g_source_destroy (data->child_stdout_source); |
| 98 |
data->child_stdout_source = NULL; |
| 99 |
} |
| 100 |
if (data->child_stderr_source != NULL) |
| 101 |
{ |
| 102 |
g_source_destroy (data->child_stderr_source); |
| 103 |
data->child_stderr_source = NULL; |
| 104 |
} |
| 105 |
|
| 106 |
if (data->child_stdout_fd != -1) |
| 107 |
{ |
| 108 |
g_warn_if_fail (close (data->child_stdout_fd) == 0); |
| 109 |
data->child_stdout_fd = -1; |
| 110 |
} |
| 111 |
if (data->child_stderr_fd != -1) |
| 112 |
{ |
| 113 |
g_warn_if_fail (close (data->child_stderr_fd) == 0); |
| 114 |
data->child_stderr_fd = -1; |
| 115 |
} |
| 116 |
|
| 117 |
if (data->cancellable_handler_id > 0) |
| 118 |
{ |
| 119 |
g_cancellable_disconnect (data->cancellable, data->cancellable_handler_id); |
| 120 |
data->cancellable_handler_id = 0; |
| 121 |
} |
| 122 |
|
| 123 |
if (data->main_context != NULL) |
| 124 |
g_main_context_unref (data->main_context); |
| 125 |
|
| 126 |
if (data->cancellable != NULL) |
| 127 |
g_object_unref (data->cancellable); |
| 128 |
|
| 129 |
g_slice_free (UtilsSpawnData, data); |
| 130 |
} |
| 131 |
|
| 132 |
/* called in the thread where @cancellable was cancelled */ |
| 133 |
static void |
| 134 |
utils_on_cancelled (GCancellable *cancellable, |
| 135 |
gpointer user_data) |
| 136 |
{ |
| 137 |
UtilsSpawnData *data = (UtilsSpawnData *)user_data; |
| 138 |
GError *error; |
| 139 |
|
| 140 |
error = NULL; |
| 141 |
g_warn_if_fail (g_cancellable_set_error_if_cancelled (cancellable, &error)); |
| 142 |
g_simple_async_result_take_error (data->simple, error); |
| 143 |
g_simple_async_result_complete_in_idle (data->simple); |
| 144 |
g_object_unref (data->simple); |
| 145 |
} |
| 146 |
|
| 147 |
static gboolean |
| 148 |
utils_timeout_cb (gpointer user_data) |
| 149 |
{ |
| 150 |
UtilsSpawnData *data = (UtilsSpawnData *)user_data; |
| 151 |
|
| 152 |
data->timed_out = TRUE; |
| 153 |
|
| 154 |
/* ok, timeout is history, make sure we don't free it in spawn_data_free() */ |
| 155 |
data->timeout_source = NULL; |
| 156 |
|
| 157 |
/* we're done */ |
| 158 |
g_simple_async_result_complete_in_idle (data->simple); |
| 159 |
g_object_unref (data->simple); |
| 160 |
|
| 161 |
return FALSE; /* remove source */ |
| 162 |
} |
| 163 |
|
| 164 |
static void |
| 165 |
utils_child_watch_cb (GPid pid, |
| 166 |
gint status, |
| 167 |
gpointer user_data) |
| 168 |
{ |
| 169 |
UtilsSpawnData *data = (UtilsSpawnData *)user_data; |
| 170 |
gchar *buf; |
| 171 |
gsize buf_size; |
| 172 |
|
| 173 |
if (g_io_channel_read_to_end (data->child_stdout_channel, &buf, &buf_size, NULL) == G_IO_STATUS_NORMAL) |
| 174 |
{ |
| 175 |
g_string_append_len (data->child_stdout, buf, buf_size); |
| 176 |
} |
| 177 |
g_free (buf); |
| 178 |
|
| 179 |
if (g_io_channel_read_to_end (data->child_stderr_channel, &buf, &buf_size, NULL) == G_IO_STATUS_NORMAL) |
| 180 |
{ |
| 181 |
g_string_append_len (data->child_stderr, buf, buf_size); |
| 182 |
} |
| 183 |
g_free (buf); |
| 184 |
|
| 185 |
data->exit_status = status; |
| 186 |
|
| 187 |
/* ok, child watch is history, make sure we don't free it in spawn_data_free() */ |
| 188 |
data->child_pid = 0; |
| 189 |
data->child_watch_source = NULL; |
| 190 |
|
| 191 |
/* we're done */ |
| 192 |
g_simple_async_result_complete_in_idle (data->simple); |
| 193 |
g_object_unref (data->simple); |
| 194 |
} |
| 195 |
|
| 196 |
static gboolean |
| 197 |
utils_read_child_stderr (GIOChannel *channel, |
| 198 |
GIOCondition condition, |
| 199 |
gpointer user_data) |
| 200 |
{ |
| 201 |
UtilsSpawnData *data = (UtilsSpawnData *)user_data; |
| 202 |
gchar buf[1024]; |
| 203 |
gsize bytes_read; |
| 204 |
|
| 205 |
g_io_channel_read_chars (channel, buf, sizeof buf, &bytes_read, NULL); |
| 206 |
g_string_append_len (data->child_stderr, buf, bytes_read); |
| 207 |
return TRUE; |
| 208 |
} |
| 209 |
|
| 210 |
static gboolean |
| 211 |
utils_read_child_stdout (GIOChannel *channel, |
| 212 |
GIOCondition condition, |
| 213 |
gpointer user_data) |
| 214 |
{ |
| 215 |
UtilsSpawnData *data = (UtilsSpawnData *)user_data; |
| 216 |
gchar buf[1024]; |
| 217 |
gsize bytes_read; |
| 218 |
|
| 219 |
g_io_channel_read_chars (channel, buf, sizeof buf, &bytes_read, NULL); |
| 220 |
g_string_append_len (data->child_stdout, buf, bytes_read); |
| 221 |
return TRUE; |
| 222 |
} |
| 223 |
|
| 224 |
void |
| 225 |
polkit_backend_common_spawn (const gchar *const *argv, |
| 226 |
guint timeout_seconds, |
| 227 |
GCancellable *cancellable, |
| 228 |
GAsyncReadyCallback callback, |
| 229 |
gpointer user_data) |
| 230 |
{ |
| 231 |
UtilsSpawnData *data; |
| 232 |
GError *error; |
| 233 |
|
| 234 |
data = g_slice_new0 (UtilsSpawnData); |
| 235 |
data->timeout_seconds = timeout_seconds; |
| 236 |
data->simple = g_simple_async_result_new (NULL, |
| 237 |
callback, |
| 238 |
user_data, |
| 239 |
(gpointer*)polkit_backend_common_spawn); |
| 240 |
data->main_context = g_main_context_get_thread_default (); |
| 241 |
if (data->main_context != NULL) |
| 242 |
g_main_context_ref (data->main_context); |
| 243 |
|
| 244 |
data->cancellable = cancellable != NULL ? (GCancellable*)g_object_ref (cancellable) : NULL; |
| 245 |
|
| 246 |
data->child_stdout = g_string_new (NULL); |
| 247 |
data->child_stderr = g_string_new (NULL); |
| 248 |
data->child_stdout_fd = -1; |
| 249 |
data->child_stderr_fd = -1; |
| 250 |
|
| 251 |
/* the life-cycle of UtilsSpawnData is tied to its GSimpleAsyncResult */ |
| 252 |
g_simple_async_result_set_op_res_gpointer (data->simple, data, (GDestroyNotify) utils_spawn_data_free); |
| 253 |
|
| 254 |
error = NULL; |
| 255 |
if (data->cancellable != NULL) |
| 256 |
{ |
| 257 |
/* could already be cancelled */ |
| 258 |
error = NULL; |
| 259 |
if (g_cancellable_set_error_if_cancelled (data->cancellable, &error)) |
| 260 |
{ |
| 261 |
g_simple_async_result_take_error (data->simple, error); |
| 262 |
g_simple_async_result_complete_in_idle (data->simple); |
| 263 |
g_object_unref (data->simple); |
| 264 |
goto out; |
| 265 |
} |
| 266 |
|
| 267 |
data->cancellable_handler_id = g_cancellable_connect (data->cancellable, |
| 268 |
G_CALLBACK (utils_on_cancelled), |
| 269 |
data, |
| 270 |
NULL); |
| 271 |
} |
| 272 |
|
| 273 |
error = NULL; |
| 274 |
if (!g_spawn_async_with_pipes (NULL, /* working directory */ |
| 275 |
(gchar **) argv, |
| 276 |
NULL, /* envp */ |
| 277 |
G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD, |
| 278 |
NULL, /* child_setup */ |
| 279 |
NULL, /* child_setup's user_data */ |
| 280 |
&(data->child_pid), |
| 281 |
NULL, /* gint *stdin_fd */ |
| 282 |
&(data->child_stdout_fd), |
| 283 |
&(data->child_stderr_fd), |
| 284 |
&error)) |
| 285 |
{ |
| 286 |
g_prefix_error (&error, "Error spawning: "); |
| 287 |
g_simple_async_result_take_error (data->simple, error); |
| 288 |
g_simple_async_result_complete_in_idle (data->simple); |
| 289 |
g_object_unref (data->simple); |
| 290 |
goto out; |
| 291 |
} |
| 292 |
|
| 293 |
if (timeout_seconds > 0) |
| 294 |
{ |
| 295 |
data->timeout_source = g_timeout_source_new_seconds (timeout_seconds); |
| 296 |
g_source_set_priority (data->timeout_source, G_PRIORITY_DEFAULT); |
| 297 |
g_source_set_callback (data->timeout_source, utils_timeout_cb, data, NULL); |
| 298 |
g_source_attach (data->timeout_source, data->main_context); |
| 299 |
g_source_unref (data->timeout_source); |
| 300 |
} |
| 301 |
|
| 302 |
data->child_watch_source = g_child_watch_source_new (data->child_pid); |
| 303 |
g_source_set_callback (data->child_watch_source, (GSourceFunc) utils_child_watch_cb, data, NULL); |
| 304 |
g_source_attach (data->child_watch_source, data->main_context); |
| 305 |
g_source_unref (data->child_watch_source); |
| 306 |
|
| 307 |
data->child_stdout_channel = g_io_channel_unix_new (data->child_stdout_fd); |
| 308 |
g_io_channel_set_flags (data->child_stdout_channel, G_IO_FLAG_NONBLOCK, NULL); |
| 309 |
data->child_stdout_source = g_io_create_watch (data->child_stdout_channel, G_IO_IN); |
| 310 |
g_source_set_callback (data->child_stdout_source, (GSourceFunc) utils_read_child_stdout, data, NULL); |
| 311 |
g_source_attach (data->child_stdout_source, data->main_context); |
| 312 |
g_source_unref (data->child_stdout_source); |
| 313 |
|
| 314 |
data->child_stderr_channel = g_io_channel_unix_new (data->child_stderr_fd); |
| 315 |
g_io_channel_set_flags (data->child_stderr_channel, G_IO_FLAG_NONBLOCK, NULL); |
| 316 |
data->child_stderr_source = g_io_create_watch (data->child_stderr_channel, G_IO_IN); |
| 317 |
g_source_set_callback (data->child_stderr_source, (GSourceFunc) utils_read_child_stderr, data, NULL); |
| 318 |
g_source_attach (data->child_stderr_source, data->main_context); |
| 319 |
g_source_unref (data->child_stderr_source); |
| 320 |
|
| 321 |
out: |
| 322 |
; |
| 323 |
} |
| 324 |
|
| 325 |
void |
| 326 |
polkit_backend_common_on_dir_monitor_changed (GFileMonitor *monitor, |
| 327 |
GFile *file, |
| 328 |
GFile *other_file, |
| 329 |
GFileMonitorEvent event_type, |
| 330 |
gpointer user_data) |
| 331 |
{ |
| 332 |
PolkitBackendJsAuthority *authority = POLKIT_BACKEND_JS_AUTHORITY (user_data); |
| 333 |
|
| 334 |
/* TODO: maybe rate-limit so storms of events are collapsed into one with a 500ms resolution? |
| 335 |
* Because when editing a file with emacs we get 4-8 events.. |
| 336 |
*/ |
| 337 |
|
| 338 |
if (file != NULL) |
| 339 |
{ |
| 340 |
gchar *name; |
| 341 |
|
| 342 |
name = g_file_get_basename (file); |
| 343 |
|
| 344 |
/* g_print ("event_type=%d file=%p name=%s\n", event_type, file, name); */ |
| 345 |
if (!g_str_has_prefix (name, ".") && |
| 346 |
!g_str_has_prefix (name, "#") && |
| 347 |
g_str_has_suffix (name, ".rules") && |
| 348 |
(event_type == G_FILE_MONITOR_EVENT_CREATED || |
| 349 |
event_type == G_FILE_MONITOR_EVENT_DELETED || |
| 350 |
event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)) |
| 351 |
{ |
| 352 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 353 |
LOG_LEVEL_INFO, |
| 354 |
"Reloading rules"); |
| 355 |
polkit_backend_common_reload_scripts (authority); |
| 356 |
} |
| 357 |
g_free (name); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
gboolean |
| 362 |
polkit_backend_common_spawn_finish (GAsyncResult *res, |
| 363 |
gint *out_exit_status, |
| 364 |
gchar **out_standard_output, |
| 365 |
gchar **out_standard_error, |
| 366 |
GError **error) |
| 367 |
{ |
| 368 |
GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res); |
| 369 |
UtilsSpawnData *data; |
| 370 |
gboolean ret = FALSE; |
| 371 |
|
| 372 |
g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE); |
| 373 |
g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 374 |
|
| 375 |
g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == polkit_backend_common_spawn); |
| 376 |
|
| 377 |
if (g_simple_async_result_propagate_error (simple, error)) |
| 378 |
goto out; |
| 379 |
|
| 380 |
data = (UtilsSpawnData*)g_simple_async_result_get_op_res_gpointer (simple); |
| 381 |
|
| 382 |
if (data->timed_out) |
| 383 |
{ |
| 384 |
g_set_error (error, |
| 385 |
G_IO_ERROR, |
| 386 |
G_IO_ERROR_TIMED_OUT, |
| 387 |
"Timed out after %d seconds", |
| 388 |
data->timeout_seconds); |
| 389 |
goto out; |
| 390 |
} |
| 391 |
|
| 392 |
if (out_exit_status != NULL) |
| 393 |
*out_exit_status = data->exit_status; |
| 394 |
|
| 395 |
if (out_standard_output != NULL) |
| 396 |
*out_standard_output = g_strdup (data->child_stdout->str); |
| 397 |
|
| 398 |
if (out_standard_error != NULL) |
| 399 |
*out_standard_error = g_strdup (data->child_stderr->str); |
| 400 |
|
| 401 |
ret = TRUE; |
| 402 |
|
| 403 |
out: |
| 404 |
return ret; |
| 405 |
} |
| 406 |
|
| 407 |
static const gchar * |
| 408 |
polkit_backend_js_authority_get_name (PolkitBackendAuthority *authority) |
| 409 |
{ |
| 410 |
return "js"; |
| 411 |
} |
| 412 |
|
| 413 |
static const gchar * |
| 414 |
polkit_backend_js_authority_get_version (PolkitBackendAuthority *authority) |
| 415 |
{ |
| 416 |
return PACKAGE_VERSION; |
| 417 |
} |
| 418 |
|
| 419 |
static PolkitAuthorityFeatures |
| 420 |
polkit_backend_js_authority_get_features (PolkitBackendAuthority *authority) |
| 421 |
{ |
| 422 |
return POLKIT_AUTHORITY_FEATURES_TEMPORARY_AUTHORIZATION; |
| 423 |
} |
| 424 |
|
| 425 |
void |
| 426 |
polkit_backend_common_js_authority_class_init_common (PolkitBackendJsAuthorityClass *klass) |
| 427 |
{ |
| 428 |
GObjectClass *gobject_class; |
| 429 |
PolkitBackendAuthorityClass *authority_class; |
| 430 |
PolkitBackendInteractiveAuthorityClass *interactive_authority_class; |
| 431 |
|
| 432 |
gobject_class = G_OBJECT_CLASS (klass); |
| 433 |
gobject_class->finalize = polkit_backend_common_js_authority_finalize; |
| 434 |
gobject_class->set_property = polkit_backend_common_js_authority_set_property; |
| 435 |
gobject_class->constructed = polkit_backend_common_js_authority_constructed; |
| 436 |
|
| 437 |
authority_class = POLKIT_BACKEND_AUTHORITY_CLASS (klass); |
| 438 |
authority_class->get_name = polkit_backend_js_authority_get_name; |
| 439 |
authority_class->get_version = polkit_backend_js_authority_get_version; |
| 440 |
authority_class->get_features = polkit_backend_js_authority_get_features; |
| 441 |
|
| 442 |
interactive_authority_class = POLKIT_BACKEND_INTERACTIVE_AUTHORITY_CLASS (klass); |
| 443 |
interactive_authority_class->get_admin_identities = polkit_backend_common_js_authority_get_admin_auth_identities; |
| 444 |
interactive_authority_class->check_authorization_sync = polkit_backend_common_js_authority_check_authorization_sync; |
| 445 |
|
| 446 |
g_object_class_install_property (gobject_class, |
| 447 |
PROP_RULES_DIRS, |
| 448 |
g_param_spec_boxed ("rules-dirs", |
| 449 |
NULL, |
| 450 |
NULL, |
| 451 |
G_TYPE_STRV, |
| 452 |
G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE)); |
| 453 |
} |
| 454 |
|
| 455 |
gint |
| 456 |
polkit_backend_common_rules_file_name_cmp (const gchar *a, |
| 457 |
const gchar *b) |
| 458 |
{ |
| 459 |
gint ret; |
| 460 |
const gchar *a_base; |
| 461 |
const gchar *b_base; |
| 462 |
|
| 463 |
a_base = strrchr (a, '/'); |
| 464 |
b_base = strrchr (b, '/'); |
| 465 |
|
| 466 |
g_assert (a_base != NULL); |
| 467 |
g_assert (b_base != NULL); |
| 468 |
a_base += 1; |
| 469 |
b_base += 1; |
| 470 |
|
| 471 |
ret = g_strcmp0 (a_base, b_base); |
| 472 |
if (ret == 0) |
| 473 |
{ |
| 474 |
/* /etc wins over /usr */ |
| 475 |
ret = g_strcmp0 (a, b); |
| 476 |
g_assert (ret != 0); |
| 477 |
} |
| 478 |
|
| 479 |
return ret; |
| 480 |
} |
| 481 |
|
| 482 |
const gchar * |
| 483 |
polkit_backend_common_get_signal_name (gint signal_number) |
| 484 |
{ |
| 485 |
switch (signal_number) |
| 486 |
{ |
| 487 |
#define _HANDLE_SIG(sig) case sig: return #sig; |
| 488 |
_HANDLE_SIG (SIGHUP); |
| 489 |
_HANDLE_SIG (SIGINT); |
| 490 |
_HANDLE_SIG (SIGQUIT); |
| 491 |
_HANDLE_SIG (SIGILL); |
| 492 |
_HANDLE_SIG (SIGABRT); |
| 493 |
_HANDLE_SIG (SIGFPE); |
| 494 |
_HANDLE_SIG (SIGKILL); |
| 495 |
_HANDLE_SIG (SIGSEGV); |
| 496 |
_HANDLE_SIG (SIGPIPE); |
| 497 |
_HANDLE_SIG (SIGALRM); |
| 498 |
_HANDLE_SIG (SIGTERM); |
| 499 |
_HANDLE_SIG (SIGUSR1); |
| 500 |
_HANDLE_SIG (SIGUSR2); |
| 501 |
_HANDLE_SIG (SIGCHLD); |
| 502 |
_HANDLE_SIG (SIGCONT); |
| 503 |
_HANDLE_SIG (SIGSTOP); |
| 504 |
_HANDLE_SIG (SIGTSTP); |
| 505 |
_HANDLE_SIG (SIGTTIN); |
| 506 |
_HANDLE_SIG (SIGTTOU); |
| 507 |
_HANDLE_SIG (SIGBUS); |
| 508 |
#ifdef SIGPOLL |
| 509 |
_HANDLE_SIG (SIGPOLL); |
| 510 |
#endif |
| 511 |
_HANDLE_SIG (SIGPROF); |
| 512 |
_HANDLE_SIG (SIGSYS); |
| 513 |
_HANDLE_SIG (SIGTRAP); |
| 514 |
_HANDLE_SIG (SIGURG); |
| 515 |
_HANDLE_SIG (SIGVTALRM); |
| 516 |
_HANDLE_SIG (SIGXCPU); |
| 517 |
_HANDLE_SIG (SIGXFSZ); |
| 518 |
#undef _HANDLE_SIG |
| 519 |
default: |
| 520 |
break; |
| 521 |
} |
| 522 |
return "UNKNOWN_SIGNAL"; |
| 523 |
} |
| 524 |
|
| 525 |
void |
| 526 |
polkit_backend_common_spawn_cb (GObject *source_object, |
| 527 |
GAsyncResult *res, |
| 528 |
gpointer user_data) |
| 529 |
{ |
| 530 |
SpawnData *data = (SpawnData *)user_data; |
| 531 |
data->res = (GAsyncResult*)g_object_ref (res); |
| 532 |
g_main_loop_quit (data->loop); |
| 533 |
} |
| 534 |
|
| 535 |
void |
| 536 |
polkit_backend_common_pidfd_to_systemd_unit (gint pidfd, |
| 537 |
gchar **ret_unit, |
| 538 |
gboolean *ret_no_new_privs) |
| 539 |
{ |
| 540 |
static int cached_has_pidfd_support = -1; |
| 541 |
GError *error = NULL; |
| 542 |
GDBusConnection *connection = NULL; |
| 543 |
GMainContext *tmp_context = NULL; |
| 544 |
GVariant *result = NULL, *no_new_privs_result = NULL, *no_new_privis_value; |
| 545 |
GUnixFDList *fd_list = NULL; |
| 546 |
const char *unit_path, *unit; |
| 547 |
int fd_id; |
| 548 |
|
| 549 |
/* Try to lookup using a PIDFD, so that we do not have issues with PIDs being |
| 550 |
* recycled under our nose. For that we need both a kernel that supports the |
| 551 |
* PIDFD syscalls (no wrapper from glibc, so need to call it directly) and a |
| 552 |
* version of systemd with the new GetUnitByPIDFD method. If either are not |
| 553 |
* available, then return nothing, as we don't want to be open to PID recycle |
| 554 |
* attacks. |
| 555 |
*/ |
| 556 |
|
| 557 |
g_assert (ret_unit != NULL); |
| 558 |
g_assert (ret_no_new_privs != NULL); |
| 559 |
|
| 560 |
if (pidfd < 0 || cached_has_pidfd_support == 0) |
| 561 |
return; |
| 562 |
|
| 563 |
connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); |
| 564 |
if (connection == NULL) |
| 565 |
{ |
| 566 |
g_warning ("Error getting system bus: %s", error->message); |
| 567 |
goto out; |
| 568 |
} |
| 569 |
|
| 570 |
tmp_context = g_main_context_new (); |
| 571 |
g_main_context_push_thread_default (tmp_context); |
| 572 |
|
| 573 |
fd_list = g_unix_fd_list_new (); |
| 574 |
if (fd_list == NULL) |
| 575 |
goto out; |
| 576 |
|
| 577 |
fd_id = g_unix_fd_list_append (fd_list, pidfd, &error); |
| 578 |
if (fd_id < 0) |
| 579 |
{ |
| 580 |
g_warning ("Error appending PID FD to fd list: %s", error->message); |
| 581 |
goto out; |
| 582 |
} |
| 583 |
|
| 584 |
result = g_dbus_connection_call_with_unix_fd_list_sync (connection, |
| 585 |
"org.freedesktop.systemd1", /* name */ |
| 586 |
"/org/freedesktop/systemd1", /* object path */ |
| 587 |
"org.freedesktop.systemd1.Manager", /* interface name */ |
| 588 |
"GetUnitByPIDFD", /* method */ |
| 589 |
g_variant_new ("(h)", fd_id), |
| 590 |
G_VARIANT_TYPE ("(osay)"), |
| 591 |
G_DBUS_CALL_FLAGS_NONE, |
| 592 |
-1, |
| 593 |
fd_list, |
| 594 |
NULL, |
| 595 |
NULL, |
| 596 |
&error); |
| 597 |
|
| 598 |
if (result == NULL) |
| 599 |
{ |
| 600 |
if (g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD)) |
| 601 |
cached_has_pidfd_support = 0; |
| 602 |
|
| 603 |
g_warning ("Error calling GetUnitByPIDFD: %s", error->message); |
| 604 |
goto out; |
| 605 |
} |
| 606 |
|
| 607 |
g_variant_get (result, "(&o&say)", &unit_path, &unit, NULL); |
| 608 |
if (unit == NULL) |
| 609 |
goto out; |
| 610 |
|
| 611 |
/* Check for NoNewPrivileges property being set on the unit via D-Bus, and |
| 612 |
* return if it is not. This protects against PID changes, as if unset the |
| 613 |
* unit could use a setuid binary. It is only valid for service units. */ |
| 614 |
if (g_str_has_suffix (unit, ".service")) |
| 615 |
{ |
| 616 |
no_new_privs_result = g_dbus_connection_call_sync (connection, |
| 617 |
"org.freedesktop.systemd1", /* name */ |
| 618 |
unit_path, /* object path */ |
| 619 |
"org.freedesktop.DBus.Properties", /* interface name */ |
| 620 |
"Get", /* method */ |
| 621 |
g_variant_new ("(ss)", "org.freedesktop.systemd1.Service", "NoNewPrivileges"), |
| 622 |
G_VARIANT_TYPE ("(v)"), |
| 623 |
G_DBUS_CALL_FLAGS_NONE, |
| 624 |
-1, |
| 625 |
NULL, |
| 626 |
&error); |
| 627 |
|
| 628 |
if (no_new_privs_result == NULL) |
| 629 |
{ |
| 630 |
g_warning ("Error calling Get on NoNewPrivileges property for unit %s: %s", unit, error->message); |
| 631 |
goto out; |
| 632 |
} |
| 633 |
|
| 634 |
g_variant_get (no_new_privs_result, "(v)", &no_new_privis_value); |
| 635 |
if (no_new_privis_value == NULL) |
| 636 |
{ |
| 637 |
g_warning ("Error getting value for NoNewPrivileges property for unit %s", unit); |
| 638 |
goto out; |
| 639 |
} |
| 640 |
|
| 641 |
*ret_no_new_privs = g_variant_get_boolean (no_new_privis_value); |
| 642 |
} |
| 643 |
else |
| 644 |
*ret_no_new_privs = FALSE; |
| 645 |
|
| 646 |
*ret_unit = strdup (unit); |
| 647 |
if (!*ret_unit) |
| 648 |
{ |
| 649 |
g_warning ("Failed to allocate memory for systemd unit ID"); |
| 650 |
goto out; |
| 651 |
} |
| 652 |
|
| 653 |
out: |
| 654 |
if (tmp_context) |
| 655 |
{ |
| 656 |
g_main_context_pop_thread_default (tmp_context); |
| 657 |
g_main_context_unref (tmp_context); |
| 658 |
} |
| 659 |
if (connection != NULL) |
| 660 |
g_object_unref (connection); |
| 661 |
if (result != NULL) |
| 662 |
g_variant_unref (result); |
| 663 |
if (no_new_privs_result != NULL) |
| 664 |
g_variant_unref (no_new_privs_result); |
| 665 |
if (fd_list != NULL) |
| 666 |
g_object_unref (fd_list); |
| 667 |
if (error) |
| 668 |
g_error_free (error); |
| 669 |
} |