| 1 |
/* |
| 2 |
* Copyright (C) 2008-2012 Red Hat, Inc. |
| 3 |
* Copyright (C) 2015 Tangent Space <[email protected]> |
| 4 |
* Copyright (C) 2019 Wu Xiaotian <[email protected]> |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General |
| 17 |
* Public License along with this library; if not, write to the |
| 18 |
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
| 19 |
* Boston, MA 02111-1307, USA. |
| 20 |
* |
| 21 |
* Author: David Zeuthen <[email protected]> |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <pthread.h> |
| 25 |
|
| 26 |
#include "polkitbackendcommon.h" |
| 27 |
|
| 28 |
#include "duktape.h" |
| 29 |
|
| 30 |
/* Built source and not too big to worry about deduplication */ |
| 31 |
#include "initjs.h" /* init.js */ |
| 32 |
|
| 33 |
/** |
| 34 |
* SECTION:polkitbackendjsauthority |
| 35 |
* @title: PolkitBackendJsAuthority |
| 36 |
* @short_description: JS Authority |
| 37 |
* @stability: Unstable |
| 38 |
* |
| 39 |
* An (Duktape-based) implementation of #PolkitBackendAuthority that reads and |
| 40 |
* evaluates Javascript files and supports interaction with authentication |
| 41 |
* agents (virtue of being based on #PolkitBackendInteractiveAuthority). |
| 42 |
*/ |
| 43 |
|
| 44 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 45 |
|
| 46 |
struct _PolkitBackendJsAuthorityPrivate |
| 47 |
{ |
| 48 |
gchar **rules_dirs; |
| 49 |
GFileMonitor **dir_monitors; /* NULL-terminated array of GFileMonitor instances */ |
| 50 |
|
| 51 |
duk_context *cx; |
| 52 |
|
| 53 |
pthread_t runaway_killer_thread; |
| 54 |
}; |
| 55 |
|
| 56 |
enum |
| 57 |
{ |
| 58 |
RUNAWAY_KILLER_THREAD_EXIT_STATUS_UNSET, |
| 59 |
RUNAWAY_KILLER_THREAD_EXIT_STATUS_SUCCESS, |
| 60 |
RUNAWAY_KILLER_THREAD_EXIT_STATUS_FAILURE, |
| 61 |
}; |
| 62 |
|
| 63 |
static gboolean execute_script_with_runaway_killer(PolkitBackendJsAuthority *authority, |
| 64 |
const gchar *filename); |
| 65 |
|
| 66 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 67 |
|
| 68 |
G_DEFINE_TYPE_WITH_PRIVATE (PolkitBackendJsAuthority, polkit_backend_js_authority, POLKIT_BACKEND_TYPE_INTERACTIVE_AUTHORITY); |
| 69 |
|
| 70 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 71 |
|
| 72 |
static duk_ret_t js_polkit_log (duk_context *cx); |
| 73 |
static duk_ret_t js_polkit_spawn (duk_context *cx); |
| 74 |
static duk_ret_t js_polkit_user_is_in_netgroup (duk_context *cx); |
| 75 |
|
| 76 |
static const duk_function_list_entry js_polkit_functions[] = |
| 77 |
{ |
| 78 |
{ "log", js_polkit_log, 1 }, |
| 79 |
{ "spawn", js_polkit_spawn, 1 }, |
| 80 |
{ "_userIsInNetGroup", js_polkit_user_is_in_netgroup, 2 }, |
| 81 |
{ NULL, NULL, 0 }, |
| 82 |
}; |
| 83 |
|
| 84 |
static void report_error (void *udata, |
| 85 |
const char *msg) |
| 86 |
{ |
| 87 |
PolkitBackendJsAuthority *authority = udata; |
| 88 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 89 |
LOG_LEVEL_ERROR, |
| 90 |
"fatal Duktape JS backend error: %s", |
| 91 |
(msg ? msg : "no message")); |
| 92 |
} |
| 93 |
|
| 94 |
static void |
| 95 |
polkit_backend_js_authority_init (PolkitBackendJsAuthority *authority) |
| 96 |
{ |
| 97 |
authority->priv = polkit_backend_js_authority_get_instance_private (authority); |
| 98 |
} |
| 99 |
|
| 100 |
static void |
| 101 |
load_scripts (PolkitBackendJsAuthority *authority) |
| 102 |
{ |
| 103 |
GList *files = NULL; |
| 104 |
GList *l; |
| 105 |
guint num_scripts = 0; |
| 106 |
GError *error = NULL; |
| 107 |
guint n; |
| 108 |
|
| 109 |
files = NULL; |
| 110 |
|
| 111 |
for (n = 0; authority->priv->rules_dirs != NULL && authority->priv->rules_dirs[n] != NULL; n++) |
| 112 |
{ |
| 113 |
const gchar *dir_name = authority->priv->rules_dirs[n]; |
| 114 |
GDir *dir = NULL; |
| 115 |
|
| 116 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 117 |
LOG_LEVEL_INFO, |
| 118 |
"Loading rules from directory %s", |
| 119 |
dir_name); |
| 120 |
|
| 121 |
dir = g_dir_open (dir_name, |
| 122 |
0, |
| 123 |
&error); |
| 124 |
if (dir != NULL) |
| 125 |
{ |
| 126 |
const gchar *name; |
| 127 |
while ((name = g_dir_read_name (dir)) != NULL) |
| 128 |
{ |
| 129 |
if (g_str_has_suffix (name, ".rules")) |
| 130 |
files = g_list_prepend (files, g_strdup_printf ("%s/%s", dir_name, name)); |
| 131 |
} |
| 132 |
g_dir_close (dir); |
| 133 |
} |
| 134 |
else |
| 135 |
{ |
| 136 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 137 |
(error->code == G_FILE_ERROR_NOENT) ? LOG_LEVEL_INFO : LOG_LEVEL_WARNING, |
| 138 |
"Error opening rules directory: %s (%s, %d)", |
| 139 |
error->message, g_quark_to_string (error->domain), error->code); |
| 140 |
g_clear_error (&error); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
files = g_list_sort (files, (GCompareFunc) polkit_backend_common_rules_file_name_cmp); |
| 145 |
|
| 146 |
for (l = files; l != NULL; l = l->next) |
| 147 |
{ |
| 148 |
const gchar *filename = (gchar *)l->data; |
| 149 |
|
| 150 |
if (!execute_script_with_runaway_killer(authority, filename)) |
| 151 |
continue; |
| 152 |
num_scripts++; |
| 153 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 154 |
LOG_LEVEL_DEBUG, |
| 155 |
"Loaded and executed script in file %s", |
| 156 |
filename); |
| 157 |
} |
| 158 |
|
| 159 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 160 |
LOG_LEVEL_INFO, |
| 161 |
"Finished loading, compiling and executing %d rules", |
| 162 |
num_scripts); |
| 163 |
g_list_free_full (files, g_free); |
| 164 |
} |
| 165 |
|
| 166 |
void |
| 167 |
polkit_backend_common_reload_scripts (PolkitBackendJsAuthority *authority) |
| 168 |
{ |
| 169 |
duk_context *cx = authority->priv->cx; |
| 170 |
|
| 171 |
duk_set_top (cx, 0); |
| 172 |
if (!duk_get_global_string (cx, "polkit")) { |
| 173 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 174 |
LOG_LEVEL_ERROR, |
| 175 |
"Error deleting old rules, not loading new ones"); |
| 176 |
return; |
| 177 |
} |
| 178 |
duk_push_string (cx, "_deleteRules"); |
| 179 |
|
| 180 |
duk_call_prop (cx, 0, 0); |
| 181 |
|
| 182 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 183 |
LOG_LEVEL_INFO, |
| 184 |
"Collecting garbage unconditionally..."); |
| 185 |
|
| 186 |
load_scripts (authority); |
| 187 |
|
| 188 |
/* Let applications know we have new rules... */ |
| 189 |
g_signal_emit_by_name (authority, "changed"); |
| 190 |
} |
| 191 |
|
| 192 |
static void |
| 193 |
setup_file_monitors (PolkitBackendJsAuthority *authority) |
| 194 |
{ |
| 195 |
guint n; |
| 196 |
GPtrArray *p; |
| 197 |
|
| 198 |
p = g_ptr_array_new (); |
| 199 |
for (n = 0; authority->priv->rules_dirs != NULL && authority->priv->rules_dirs[n] != NULL; n++) |
| 200 |
{ |
| 201 |
GFile *file; |
| 202 |
GError *error; |
| 203 |
GFileMonitor *monitor; |
| 204 |
|
| 205 |
file = g_file_new_for_path (authority->priv->rules_dirs[n]); |
| 206 |
error = NULL; |
| 207 |
monitor = g_file_monitor_directory (file, |
| 208 |
G_FILE_MONITOR_NONE, |
| 209 |
NULL, |
| 210 |
&error); |
| 211 |
g_object_unref (file); |
| 212 |
if (monitor == NULL) |
| 213 |
{ |
| 214 |
g_warning ("Error monitoring directory %s: %s", |
| 215 |
authority->priv->rules_dirs[n], |
| 216 |
error->message); |
| 217 |
g_clear_error (&error); |
| 218 |
} |
| 219 |
else |
| 220 |
{ |
| 221 |
g_signal_connect (monitor, |
| 222 |
"changed", |
| 223 |
G_CALLBACK (polkit_backend_common_on_dir_monitor_changed), |
| 224 |
authority); |
| 225 |
g_ptr_array_add (p, monitor); |
| 226 |
} |
| 227 |
} |
| 228 |
g_ptr_array_add (p, NULL); |
| 229 |
authority->priv->dir_monitors = (GFileMonitor**) g_ptr_array_free (p, FALSE); |
| 230 |
} |
| 231 |
|
| 232 |
void |
| 233 |
polkit_backend_common_js_authority_constructed (GObject *object) |
| 234 |
{ |
| 235 |
PolkitBackendJsAuthority *authority = POLKIT_BACKEND_JS_AUTHORITY (object); |
| 236 |
duk_context *cx; |
| 237 |
|
| 238 |
cx = duk_create_heap (NULL, NULL, NULL, authority, report_error); |
| 239 |
if (cx == NULL) |
| 240 |
goto fail; |
| 241 |
|
| 242 |
authority->priv->cx = cx; |
| 243 |
|
| 244 |
duk_push_global_object (cx); |
| 245 |
duk_push_object (cx); |
| 246 |
duk_put_function_list (cx, -1, js_polkit_functions); |
| 247 |
duk_put_prop_string (cx, -2, "polkit"); |
| 248 |
|
| 249 |
/* load polkit objects/functions into JS context (e.g. addRule(), |
| 250 |
* _deleteRules(), _runRules() et al) |
| 251 |
*/ |
| 252 |
duk_eval_string (cx, init_js); |
| 253 |
|
| 254 |
if (authority->priv->rules_dirs == NULL) |
| 255 |
{ |
| 256 |
authority->priv->rules_dirs = g_new0 (gchar *, 5); |
| 257 |
authority->priv->rules_dirs[0] = g_strdup (PACKAGE_SYSCONF_DIR "/polkit-1/rules.d"); |
| 258 |
authority->priv->rules_dirs[1] = g_strdup ("/run/polkit-1/rules.d"); |
| 259 |
authority->priv->rules_dirs[2] = g_strdup ("/usr/local/share/polkit-1/rules.d"); |
| 260 |
if (g_strcmp0 (PACKAGE_DATA_DIR, "/usr/local/share") != 0) |
| 261 |
{ |
| 262 |
authority->priv->rules_dirs[3] = g_strdup (PACKAGE_DATA_DIR "/polkit-1/rules.d"); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
setup_file_monitors (authority); |
| 267 |
load_scripts (authority); |
| 268 |
|
| 269 |
G_OBJECT_CLASS (polkit_backend_js_authority_parent_class)->constructed (object); |
| 270 |
return; |
| 271 |
|
| 272 |
fail: |
| 273 |
g_critical ("Error initializing JavaScript environment"); |
| 274 |
g_assert_not_reached (); |
| 275 |
} |
| 276 |
|
| 277 |
void |
| 278 |
polkit_backend_common_js_authority_finalize (GObject *object) |
| 279 |
{ |
| 280 |
PolkitBackendJsAuthority *authority = POLKIT_BACKEND_JS_AUTHORITY (object); |
| 281 |
guint n; |
| 282 |
|
| 283 |
for (n = 0; authority->priv->dir_monitors != NULL && authority->priv->dir_monitors[n] != NULL; n++) |
| 284 |
{ |
| 285 |
GFileMonitor *monitor = authority->priv->dir_monitors[n]; |
| 286 |
g_signal_handlers_disconnect_by_func (monitor, |
| 287 |
G_CALLBACK (polkit_backend_common_on_dir_monitor_changed), |
| 288 |
authority); |
| 289 |
g_object_unref (monitor); |
| 290 |
} |
| 291 |
g_free (authority->priv->dir_monitors); |
| 292 |
g_strfreev (authority->priv->rules_dirs); |
| 293 |
|
| 294 |
duk_destroy_heap (authority->priv->cx); |
| 295 |
|
| 296 |
G_OBJECT_CLASS (polkit_backend_js_authority_parent_class)->finalize (object); |
| 297 |
} |
| 298 |
|
| 299 |
void |
| 300 |
polkit_backend_common_js_authority_set_property (GObject *object, |
| 301 |
guint property_id, |
| 302 |
const GValue *value, |
| 303 |
GParamSpec *pspec) |
| 304 |
{ |
| 305 |
PolkitBackendJsAuthority *authority = POLKIT_BACKEND_JS_AUTHORITY (object); |
| 306 |
|
| 307 |
switch (property_id) |
| 308 |
{ |
| 309 |
case PROP_RULES_DIRS: |
| 310 |
g_assert (authority->priv->rules_dirs == NULL); |
| 311 |
authority->priv->rules_dirs = (gchar **) g_value_dup_boxed (value); |
| 312 |
break; |
| 313 |
|
| 314 |
default: |
| 315 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
| 316 |
break; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
static void |
| 321 |
polkit_backend_js_authority_class_init (PolkitBackendJsAuthorityClass *klass) |
| 322 |
{ |
| 323 |
polkit_backend_common_js_authority_class_init_common (klass); |
| 324 |
} |
| 325 |
|
| 326 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 327 |
|
| 328 |
static void |
| 329 |
set_property_str (duk_context *cx, |
| 330 |
const gchar *name, |
| 331 |
const gchar *value) |
| 332 |
{ |
| 333 |
duk_push_string (cx, value); |
| 334 |
duk_put_prop_string (cx, -2, name); |
| 335 |
} |
| 336 |
|
| 337 |
static void |
| 338 |
set_property_strv (duk_context *cx, |
| 339 |
const gchar *name, |
| 340 |
GPtrArray *value) |
| 341 |
{ |
| 342 |
guint n; |
| 343 |
duk_push_array (cx); |
| 344 |
for (n = 0; n < value->len; n++) |
| 345 |
{ |
| 346 |
duk_push_string (cx, g_ptr_array_index (value, n)); |
| 347 |
duk_put_prop_index (cx, -2, n); |
| 348 |
} |
| 349 |
duk_put_prop_string (cx, -2, name); |
| 350 |
} |
| 351 |
|
| 352 |
static void |
| 353 |
set_property_int32 (duk_context *cx, |
| 354 |
const gchar *name, |
| 355 |
gint32 value) |
| 356 |
{ |
| 357 |
duk_push_int (cx, value); |
| 358 |
duk_put_prop_string (cx, -2, name); |
| 359 |
} |
| 360 |
|
| 361 |
static void |
| 362 |
set_property_bool (duk_context *cx, |
| 363 |
const char *name, |
| 364 |
gboolean value) |
| 365 |
{ |
| 366 |
duk_push_boolean (cx, value); |
| 367 |
duk_put_prop_string (cx, -2, name); |
| 368 |
} |
| 369 |
|
| 370 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 371 |
|
| 372 |
static gboolean |
| 373 |
push_subject (duk_context *cx, |
| 374 |
PolkitSubject *subject, |
| 375 |
PolkitIdentity *user_for_subject, |
| 376 |
gboolean subject_is_local, |
| 377 |
gboolean subject_is_active, |
| 378 |
GError **error) |
| 379 |
{ |
| 380 |
gboolean ret = FALSE; |
| 381 |
gboolean no_new_privs = FALSE; |
| 382 |
gint pidfd = -1; |
| 383 |
pid_t pid_early, pid_late; |
| 384 |
uid_t uid; |
| 385 |
PolkitSubject *process = NULL; |
| 386 |
gchar *user_name = NULL; |
| 387 |
GPtrArray *groups = NULL; |
| 388 |
GArray *gids_from_dbus = NULL; |
| 389 |
struct passwd *passwd; |
| 390 |
char *seat_str = NULL; |
| 391 |
char *session_str = NULL; |
| 392 |
char *system_unit = NULL; |
| 393 |
|
| 394 |
if (!duk_get_global_string (cx, "Subject")) { |
| 395 |
return FALSE; |
| 396 |
} |
| 397 |
|
| 398 |
duk_new (cx, 0); |
| 399 |
|
| 400 |
if (POLKIT_IS_UNIX_PROCESS (subject)) |
| 401 |
{ |
| 402 |
process = subject; |
| 403 |
} |
| 404 |
else if (POLKIT_IS_SYSTEM_BUS_NAME (subject)) |
| 405 |
{ |
| 406 |
process = polkit_system_bus_name_get_process_sync (POLKIT_SYSTEM_BUS_NAME (subject), NULL, error); |
| 407 |
if (process == NULL) |
| 408 |
goto out; |
| 409 |
} |
| 410 |
else |
| 411 |
{ |
| 412 |
g_assert_not_reached (); |
| 413 |
} |
| 414 |
|
| 415 |
pid_early = polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (process)); |
| 416 |
pidfd = polkit_unix_process_get_pidfd (POLKIT_UNIX_PROCESS (process)); |
| 417 |
|
| 418 |
#ifdef HAVE_LIBSYSTEMD |
| 419 |
#if HAVE_SD_PIDFD_GET_SESSION |
| 420 |
if (pidfd >= 0) |
| 421 |
sd_pidfd_get_session (pidfd, &session_str); |
| 422 |
else |
| 423 |
#endif /* HAVE_SD_PIDFD_GET_SESSION */ |
| 424 |
sd_pid_get_session (pid_early, &session_str); |
| 425 |
if (session_str) |
| 426 |
sd_session_get_seat (session_str, &seat_str); |
| 427 |
#endif /* HAVE_LIBSYSTEMD */ |
| 428 |
|
| 429 |
g_assert (POLKIT_IS_UNIX_USER (user_for_subject)); |
| 430 |
uid = polkit_unix_user_get_uid (POLKIT_UNIX_USER (user_for_subject)); |
| 431 |
|
| 432 |
groups = g_ptr_array_new_with_free_func (g_free); |
| 433 |
gids_from_dbus = polkit_unix_process_get_gids (POLKIT_UNIX_PROCESS (process)); |
| 434 |
|
| 435 |
passwd = getpwuid (uid); |
| 436 |
if (passwd == NULL) |
| 437 |
{ |
| 438 |
user_name = g_strdup_printf ("%d", (gint) uid); |
| 439 |
g_warning ("Error looking up info for uid %d: %m", (gint) uid); |
| 440 |
} |
| 441 |
else |
| 442 |
{ |
| 443 |
user_name = g_strdup (passwd->pw_name); |
| 444 |
} |
| 445 |
|
| 446 |
/* D-Bus will give us supplementary groups too, so prefer that to looking up |
| 447 |
* the group from the uid. */ |
| 448 |
if (gids_from_dbus && gids_from_dbus->len > 0) |
| 449 |
{ |
| 450 |
gint n; |
| 451 |
for (n = 0; n < gids_from_dbus->len; n++) |
| 452 |
{ |
| 453 |
struct group *group; |
| 454 |
group = getgrgid (g_array_index (gids_from_dbus, gid_t, n)); |
| 455 |
if (group == NULL) |
| 456 |
{ |
| 457 |
g_ptr_array_add (groups, g_strdup_printf ("%d", (gint) g_array_index (gids_from_dbus, gid_t, n))); |
| 458 |
} |
| 459 |
else |
| 460 |
{ |
| 461 |
g_ptr_array_add (groups, g_strdup (group->gr_name)); |
| 462 |
} |
| 463 |
} |
| 464 |
} |
| 465 |
else |
| 466 |
{ |
| 467 |
if (passwd != NULL) |
| 468 |
{ |
| 469 |
gid_t gids[512]; |
| 470 |
int num_gids = 512; |
| 471 |
|
| 472 |
if (getgrouplist (passwd->pw_name, |
| 473 |
passwd->pw_gid, |
| 474 |
gids, |
| 475 |
&num_gids) < 0) |
| 476 |
{ |
| 477 |
g_warning ("Error looking up groups for uid %d: %m", (gint) uid); |
| 478 |
} |
| 479 |
else |
| 480 |
{ |
| 481 |
gint n; |
| 482 |
for (n = 0; n < num_gids; n++) |
| 483 |
{ |
| 484 |
struct group *group; |
| 485 |
group = getgrgid (gids[n]); |
| 486 |
if (group == NULL) |
| 487 |
{ |
| 488 |
g_ptr_array_add (groups, g_strdup_printf ("%d", (gint) gids[n])); |
| 489 |
} |
| 490 |
else |
| 491 |
{ |
| 492 |
g_ptr_array_add (groups, g_strdup (group->gr_name)); |
| 493 |
} |
| 494 |
} |
| 495 |
} |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
/* Query the unit, will work only if we got the pidfd from dbus-daemon/broker. |
| 500 |
* Best-effort operation, will log on failure, but we don't bail here. But |
| 501 |
* only do so if the pidfd was marked as safe, i.e.: we got it from D-Bus so |
| 502 |
* it can be trusted end-to-end, with no reuse attack window. */ |
| 503 |
if (polkit_unix_process_get_pidfd_is_safe (POLKIT_UNIX_PROCESS (process))) |
| 504 |
polkit_backend_common_pidfd_to_systemd_unit (pidfd, &system_unit, &no_new_privs); |
| 505 |
|
| 506 |
/* In case we are using PIDFDs, check that the PID still matches to avoid race |
| 507 |
* conditions and PID recycle attacks. |
| 508 |
*/ |
| 509 |
pid_late = polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (process)); |
| 510 |
if (pid_late != pid_early) |
| 511 |
{ |
| 512 |
if (pid_late <= 0) |
| 513 |
{ |
| 514 |
g_warning ("Process %d terminated", (gint) pid_early); |
| 515 |
g_set_error (error, |
| 516 |
POLKIT_ERROR, |
| 517 |
POLKIT_ERROR_FAILED, |
| 518 |
"Process %d terminated", (gint) pid_early); |
| 519 |
} |
| 520 |
else |
| 521 |
{ |
| 522 |
g_warning ("Process changed pid from %d to %d", (gint) pid_early, (gint) pid_late); |
| 523 |
g_set_error (error, |
| 524 |
POLKIT_ERROR, |
| 525 |
POLKIT_ERROR_FAILED, |
| 526 |
"Process changed pid from %d to %d", (gint) pid_early, (gint) pid_late); |
| 527 |
} |
| 528 |
goto out; |
| 529 |
} |
| 530 |
|
| 531 |
set_property_int32 (cx, "uid", uid); |
| 532 |
set_property_int32 (cx, "pid", pid_early); |
| 533 |
set_property_str (cx, "user", user_name); |
| 534 |
set_property_strv (cx, "groups", groups); |
| 535 |
set_property_str (cx, "seat", seat_str); |
| 536 |
set_property_str (cx, "session", session_str); |
| 537 |
set_property_str (cx, "system_unit", system_unit); |
| 538 |
/* If we have a unit, also record if it has the NoNewPrivileges setting enabled */ |
| 539 |
if (system_unit) |
| 540 |
set_property_bool (cx, "no_new_privileges", no_new_privs); |
| 541 |
set_property_bool (cx, "local", subject_is_local); |
| 542 |
set_property_bool (cx, "active", subject_is_active); |
| 543 |
|
| 544 |
ret = TRUE; |
| 545 |
|
| 546 |
out: |
| 547 |
if (POLKIT_IS_SYSTEM_BUS_NAME (subject)) |
| 548 |
g_object_unref (process); |
| 549 |
free (session_str); |
| 550 |
free (seat_str); |
| 551 |
free (system_unit); |
| 552 |
g_free (user_name); |
| 553 |
if (groups != NULL) |
| 554 |
g_ptr_array_unref (groups); |
| 555 |
if (gids_from_dbus != NULL) |
| 556 |
g_array_unref (gids_from_dbus); |
| 557 |
|
| 558 |
return ret; |
| 559 |
} |
| 560 |
|
| 561 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 562 |
|
| 563 |
static gboolean |
| 564 |
push_action_and_details (duk_context *cx, |
| 565 |
const gchar *action_id, |
| 566 |
PolkitDetails *details) |
| 567 |
{ |
| 568 |
gchar **keys; |
| 569 |
guint n; |
| 570 |
|
| 571 |
if (!duk_get_global_string (cx, "Action")) { |
| 572 |
return FALSE; |
| 573 |
} |
| 574 |
|
| 575 |
duk_new (cx, 0); |
| 576 |
|
| 577 |
set_property_str (cx, "id", action_id); |
| 578 |
|
| 579 |
keys = polkit_details_get_keys (details); |
| 580 |
for (n = 0; keys != NULL && keys[n] != NULL; n++) |
| 581 |
{ |
| 582 |
gchar *key; |
| 583 |
const gchar *value; |
| 584 |
key = g_strdup_printf ("_detail_%s", keys[n]); |
| 585 |
value = polkit_details_lookup (details, keys[n]); |
| 586 |
set_property_str (cx, key, value); |
| 587 |
g_free (key); |
| 588 |
} |
| 589 |
g_strfreev (keys); |
| 590 |
|
| 591 |
return TRUE; |
| 592 |
} |
| 593 |
|
| 594 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 595 |
|
| 596 |
typedef struct { |
| 597 |
PolkitBackendJsAuthority *authority; |
| 598 |
const gchar *filename; |
| 599 |
pthread_cond_t cond; |
| 600 |
pthread_mutex_t mutex; |
| 601 |
gint ret; |
| 602 |
} RunawayKillerCtx; |
| 603 |
|
| 604 |
static gpointer |
| 605 |
runaway_killer_thread_execute_js (gpointer user_data) |
| 606 |
{ |
| 607 |
RunawayKillerCtx *ctx = user_data; |
| 608 |
duk_context *cx = ctx->authority->priv->cx; |
| 609 |
|
| 610 |
int oldtype, pthread_err; |
| 611 |
|
| 612 |
if ((pthread_err = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype))) { |
| 613 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (ctx->authority), |
| 614 |
LOG_LEVEL_ERROR, |
| 615 |
"Error setting thread cancel type: %s", |
| 616 |
strerror(pthread_err)); |
| 617 |
goto err; |
| 618 |
} |
| 619 |
|
| 620 |
GFile *file = g_file_new_for_path(ctx->filename); |
| 621 |
char *contents; |
| 622 |
gsize len; |
| 623 |
|
| 624 |
if (!g_file_load_contents(file, NULL, &contents, &len, NULL, NULL)) { |
| 625 |
polkit_backend_authority_log(POLKIT_BACKEND_AUTHORITY(ctx->authority), |
| 626 |
LOG_LEVEL_ERROR, |
| 627 |
"Error loading script %s", ctx->filename); |
| 628 |
g_object_unref(file); |
| 629 |
goto err; |
| 630 |
} |
| 631 |
|
| 632 |
g_object_unref(file); |
| 633 |
|
| 634 |
/* evaluate the script, trying to print context in any syntax errors |
| 635 |
found */ |
| 636 |
if (duk_peval_lstring(cx, contents, len) != 0) |
| 637 |
{ |
| 638 |
polkit_backend_authority_log(POLKIT_BACKEND_AUTHORITY(ctx->authority), |
| 639 |
LOG_LEVEL_ERROR, |
| 640 |
"Error compiling script %s: %s", ctx->filename, |
| 641 |
duk_safe_to_string(cx, -1)); |
| 642 |
duk_pop(cx); |
| 643 |
goto free_err; |
| 644 |
} |
| 645 |
g_free(contents); |
| 646 |
|
| 647 |
if ((pthread_err = pthread_mutex_lock(&ctx->mutex))) { |
| 648 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (ctx->authority), |
| 649 |
LOG_LEVEL_ERROR, |
| 650 |
"Error locking mutex: %s", |
| 651 |
strerror(pthread_err)); |
| 652 |
return NULL; |
| 653 |
} |
| 654 |
|
| 655 |
ctx->ret = RUNAWAY_KILLER_THREAD_EXIT_STATUS_SUCCESS; |
| 656 |
goto end; |
| 657 |
|
| 658 |
free_err: |
| 659 |
g_free(contents); |
| 660 |
err: |
| 661 |
if ((pthread_err = pthread_mutex_lock(&ctx->mutex))) { |
| 662 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (ctx->authority), |
| 663 |
LOG_LEVEL_ERROR, |
| 664 |
"Error locking mutex: %s", |
| 665 |
strerror(pthread_err)); |
| 666 |
return NULL; |
| 667 |
} |
| 668 |
ctx->ret = RUNAWAY_KILLER_THREAD_EXIT_STATUS_FAILURE; |
| 669 |
end: |
| 670 |
if ((pthread_err = pthread_cond_signal(&ctx->cond))) { |
| 671 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (ctx->authority), |
| 672 |
LOG_LEVEL_ERROR, |
| 673 |
"Error signaling on condition variable: %s", |
| 674 |
strerror(pthread_err)); |
| 675 |
ctx->ret = RUNAWAY_KILLER_THREAD_EXIT_STATUS_FAILURE; |
| 676 |
} |
| 677 |
if ((pthread_err = pthread_mutex_unlock(&ctx->mutex))) { |
| 678 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (ctx->authority), |
| 679 |
LOG_LEVEL_ERROR, |
| 680 |
"Error unlocking mutex: %s", |
| 681 |
strerror(pthread_err)); |
| 682 |
ctx->ret = RUNAWAY_KILLER_THREAD_EXIT_STATUS_FAILURE; |
| 683 |
} |
| 684 |
return NULL; |
| 685 |
} |
| 686 |
|
| 687 |
static gpointer |
| 688 |
runaway_killer_thread_call_js (gpointer user_data) |
| 689 |
{ |
| 690 |
RunawayKillerCtx *ctx = user_data; |
| 691 |
duk_context *cx = ctx->authority->priv->cx; |
| 692 |
int oldtype, pthread_err; |
| 693 |
|
| 694 |
if ((pthread_err = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype))) { |
| 695 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (ctx->authority), |
| 696 |
LOG_LEVEL_ERROR, |
| 697 |
"Error setting thread cancel type: %s", |
| 698 |
strerror(pthread_err)); |
| 699 |
goto err; |
| 700 |
} |
| 701 |
|
| 702 |
if (duk_pcall_prop (cx, 0, 2) != DUK_EXEC_SUCCESS) |
| 703 |
{ |
| 704 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (ctx->authority), |
| 705 |
LOG_LEVEL_ERROR, |
| 706 |
"Error evaluating admin rules: %s", |
| 707 |
duk_safe_to_string (cx, -1)); |
| 708 |
goto err; |
| 709 |
} |
| 710 |
|
| 711 |
if ((pthread_err = pthread_mutex_lock(&ctx->mutex))) { |
| 712 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (ctx->authority), |
| 713 |
LOG_LEVEL_ERROR, |
| 714 |
"Error locking mutex: %s", |
| 715 |
strerror(pthread_err)); |
| 716 |
return NULL; |
| 717 |
} |
| 718 |
|
| 719 |
ctx->ret = RUNAWAY_KILLER_THREAD_EXIT_STATUS_SUCCESS; |
| 720 |
goto end; |
| 721 |
|
| 722 |
err: |
| 723 |
if ((pthread_err = pthread_mutex_lock(&ctx->mutex))) { |
| 724 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (ctx->authority), |
| 725 |
LOG_LEVEL_ERROR, |
| 726 |
"Error locking mutex: %s", |
| 727 |
strerror(pthread_err)); |
| 728 |
return NULL; |
| 729 |
} |
| 730 |
ctx->ret = RUNAWAY_KILLER_THREAD_EXIT_STATUS_FAILURE; |
| 731 |
end: |
| 732 |
if ((pthread_err = pthread_cond_signal(&ctx->cond))) { |
| 733 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (ctx->authority), |
| 734 |
LOG_LEVEL_ERROR, |
| 735 |
"Error signaling on condition variable: %s", |
| 736 |
strerror(pthread_err)); |
| 737 |
ctx->ret = RUNAWAY_KILLER_THREAD_EXIT_STATUS_FAILURE; |
| 738 |
} |
| 739 |
if ((pthread_err = pthread_mutex_unlock(&ctx->mutex))) { |
| 740 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (ctx->authority), |
| 741 |
LOG_LEVEL_ERROR, |
| 742 |
"Error unlocking mutex: %s", |
| 743 |
strerror(pthread_err)); |
| 744 |
ctx->ret = RUNAWAY_KILLER_THREAD_EXIT_STATUS_FAILURE; |
| 745 |
} |
| 746 |
|
| 747 |
return NULL; |
| 748 |
} |
| 749 |
|
| 750 |
#if defined (HAVE_PTHREAD_CONDATTR_SETCLOCK) |
| 751 |
# if defined(CLOCK_MONOTONIC) |
| 752 |
# define PK_CLOCK CLOCK_MONOTONIC |
| 753 |
# elif defined(CLOCK_BOOTTIME) |
| 754 |
# define PK_CLOCK CLOCK_BOOTTIME |
| 755 |
# else |
| 756 |
/* No suitable clock */ |
| 757 |
# undef HAVE_PTHREAD_CONDATTR_SETCLOCK |
| 758 |
# define PK_CLOCK CLOCK_REALTIME |
| 759 |
# endif |
| 760 |
#else /* ! HAVE_PTHREAD_CONDATTR_SETCLOCK */ |
| 761 |
# define PK_CLOCK CLOCK_REALTIME |
| 762 |
#endif /* ! HAVE_PTHREAD_CONDATTR_SETCLOCK */ |
| 763 |
|
| 764 |
static gboolean |
| 765 |
runaway_killer_common(PolkitBackendJsAuthority *authority, RunawayKillerCtx *ctx, void *js_context_cb (void *user_data)) |
| 766 |
{ |
| 767 |
int pthread_err; |
| 768 |
gboolean cancel = FALSE; |
| 769 |
#ifdef HAVE_PTHREAD_CONDATTR_SETCLOCK |
| 770 |
pthread_condattr_t attr; |
| 771 |
#endif |
| 772 |
struct timespec abs_time; |
| 773 |
|
| 774 |
#ifdef HAVE_PTHREAD_CONDATTR_SETCLOCK |
| 775 |
if ((pthread_err = pthread_condattr_init(&attr))) { |
| 776 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 777 |
LOG_LEVEL_ERROR, |
| 778 |
"Error initializing condition variable attributes: %s", |
| 779 |
strerror(pthread_err)); |
| 780 |
return FALSE; |
| 781 |
} |
| 782 |
if ((pthread_err = pthread_condattr_setclock(&attr, PK_CLOCK))) { |
| 783 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 784 |
LOG_LEVEL_ERROR, |
| 785 |
"Error setting condition variable attributes: %s", |
| 786 |
strerror(pthread_err)); |
| 787 |
goto err_clean_condattr; |
| 788 |
} |
| 789 |
/* Init again, with needed attr */ |
| 790 |
if ((pthread_err = pthread_cond_init(&ctx->cond, &attr))) { |
| 791 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 792 |
LOG_LEVEL_ERROR, |
| 793 |
"Error initializing condition variable: %s", |
| 794 |
strerror(pthread_err)); |
| 795 |
goto err_clean_condattr; |
| 796 |
} |
| 797 |
#endif |
| 798 |
|
| 799 |
if ((pthread_err = pthread_mutex_lock(&ctx->mutex))) { |
| 800 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 801 |
LOG_LEVEL_ERROR, |
| 802 |
"Error locking mutex: %s", |
| 803 |
strerror(pthread_err)); |
| 804 |
goto err_clean_cond; |
| 805 |
} |
| 806 |
|
| 807 |
if (clock_gettime(PK_CLOCK, &abs_time)) { |
| 808 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 809 |
LOG_LEVEL_ERROR, |
| 810 |
"Error getting system's monotonic time: %s", |
| 811 |
strerror(errno)); |
| 812 |
goto err_clean_cond; |
| 813 |
} |
| 814 |
abs_time.tv_sec += RUNAWAY_KILLER_TIMEOUT; |
| 815 |
|
| 816 |
if ((pthread_err = pthread_create(&authority->priv->runaway_killer_thread, NULL, |
| 817 |
js_context_cb, ctx))) { |
| 818 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 819 |
LOG_LEVEL_ERROR, |
| 820 |
"Error creating runaway JS killer thread: %s", |
| 821 |
strerror(pthread_err)); |
| 822 |
goto err_clean_cond; |
| 823 |
} |
| 824 |
|
| 825 |
while (ctx->ret == RUNAWAY_KILLER_THREAD_EXIT_STATUS_UNSET) /* loop to treat spurious wakeups */ |
| 826 |
if (pthread_cond_timedwait(&ctx->cond, &ctx->mutex, &abs_time) == ETIMEDOUT) { |
| 827 |
cancel = TRUE; |
| 828 |
|
| 829 |
/* Log that we are terminating the script */ |
| 830 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 831 |
LOG_LEVEL_WARNING, |
| 832 |
"Terminating runaway script after %d seconds", |
| 833 |
RUNAWAY_KILLER_TIMEOUT); |
| 834 |
|
| 835 |
break; |
| 836 |
} |
| 837 |
|
| 838 |
if ((pthread_err = pthread_mutex_unlock(&ctx->mutex))) { |
| 839 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 840 |
LOG_LEVEL_ERROR, |
| 841 |
"Error unlocking mutex: %s", |
| 842 |
strerror(pthread_err)); |
| 843 |
goto err_clean_cond; |
| 844 |
} |
| 845 |
|
| 846 |
if (cancel) { |
| 847 |
if ((pthread_err = pthread_cancel (authority->priv->runaway_killer_thread))) { |
| 848 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 849 |
LOG_LEVEL_ERROR, |
| 850 |
"Error cancelling runaway JS killer thread: %s", |
| 851 |
strerror(pthread_err)); |
| 852 |
goto err_clean_cond; |
| 853 |
} |
| 854 |
} |
| 855 |
if ((pthread_err = pthread_join (authority->priv->runaway_killer_thread, NULL))) { |
| 856 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 857 |
LOG_LEVEL_ERROR, |
| 858 |
"Error joining runaway JS killer thread: %s", |
| 859 |
strerror(pthread_err)); |
| 860 |
goto err_clean_cond; |
| 861 |
} |
| 862 |
|
| 863 |
return ctx->ret == RUNAWAY_KILLER_THREAD_EXIT_STATUS_SUCCESS; |
| 864 |
|
| 865 |
err_clean_cond: |
| 866 |
#ifdef HAVE_PTHREAD_CONDATTR_SETCLOCK |
| 867 |
pthread_cond_destroy(&ctx->cond); |
| 868 |
err_clean_condattr: |
| 869 |
pthread_condattr_destroy(&attr); |
| 870 |
#endif |
| 871 |
return FALSE; |
| 872 |
} |
| 873 |
|
| 874 |
/* Blocking for at most RUNAWAY_KILLER_TIMEOUT */ |
| 875 |
static gboolean |
| 876 |
execute_script_with_runaway_killer(PolkitBackendJsAuthority *authority, |
| 877 |
const gchar *filename) |
| 878 |
{ |
| 879 |
RunawayKillerCtx ctx = {.authority = authority, .filename = filename, |
| 880 |
.ret = RUNAWAY_KILLER_THREAD_EXIT_STATUS_UNSET, |
| 881 |
.mutex = PTHREAD_MUTEX_INITIALIZER, |
| 882 |
.cond = PTHREAD_COND_INITIALIZER}; |
| 883 |
|
| 884 |
return runaway_killer_common(authority, &ctx, &runaway_killer_thread_execute_js); |
| 885 |
} |
| 886 |
|
| 887 |
/* Calls already stacked function and args. Blocking for at most |
| 888 |
* RUNAWAY_KILLER_TIMEOUT. If timeout is the case, ctx.ret will be |
| 889 |
* RUNAWAY_KILLER_THREAD_EXIT_STATUS_UNSET, thus returning FALSE. |
| 890 |
*/ |
| 891 |
static gboolean |
| 892 |
call_js_function_with_runaway_killer(PolkitBackendJsAuthority *authority) |
| 893 |
{ |
| 894 |
RunawayKillerCtx ctx = {.authority = authority, |
| 895 |
.ret = RUNAWAY_KILLER_THREAD_EXIT_STATUS_UNSET, |
| 896 |
.mutex = PTHREAD_MUTEX_INITIALIZER, |
| 897 |
.cond = PTHREAD_COND_INITIALIZER}; |
| 898 |
|
| 899 |
return runaway_killer_common(authority, &ctx, &runaway_killer_thread_call_js); |
| 900 |
} |
| 901 |
|
| 902 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 903 |
|
| 904 |
GList * |
| 905 |
polkit_backend_common_js_authority_get_admin_auth_identities (PolkitBackendInteractiveAuthority *_authority, |
| 906 |
PolkitSubject *caller, |
| 907 |
PolkitSubject *subject, |
| 908 |
PolkitIdentity *user_for_subject, |
| 909 |
gboolean subject_is_local, |
| 910 |
gboolean subject_is_active, |
| 911 |
const gchar *action_id, |
| 912 |
PolkitDetails *details) |
| 913 |
{ |
| 914 |
PolkitBackendJsAuthority *authority = POLKIT_BACKEND_JS_AUTHORITY (_authority); |
| 915 |
GList *ret = NULL; |
| 916 |
guint n; |
| 917 |
GError *error = NULL; |
| 918 |
const char *ret_str = NULL; |
| 919 |
gchar **ret_strs = NULL; |
| 920 |
duk_context *cx = authority->priv->cx; |
| 921 |
|
| 922 |
duk_set_top (cx, 0); |
| 923 |
if (!duk_get_global_string (cx, "polkit")) { |
| 924 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 925 |
LOG_LEVEL_ERROR, |
| 926 |
"Error deleting old rules, not loading new ones"); |
| 927 |
goto out; |
| 928 |
} |
| 929 |
|
| 930 |
duk_push_string (cx, "_runAdminRules"); |
| 931 |
|
| 932 |
if (!push_action_and_details (cx, action_id, details)) |
| 933 |
{ |
| 934 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 935 |
LOG_LEVEL_ERROR, |
| 936 |
"Error converting action and details to JS object"); |
| 937 |
goto out; |
| 938 |
} |
| 939 |
|
| 940 |
if (!push_subject (cx, subject, user_for_subject, subject_is_local, subject_is_active, &error)) |
| 941 |
{ |
| 942 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 943 |
LOG_LEVEL_ERROR, |
| 944 |
"Error converting subject to JS object: %s", |
| 945 |
error->message); |
| 946 |
g_clear_error (&error); |
| 947 |
goto out; |
| 948 |
} |
| 949 |
|
| 950 |
if (!call_js_function_with_runaway_killer (authority)) |
| 951 |
goto out; |
| 952 |
|
| 953 |
ret_str = duk_require_string (cx, -1); |
| 954 |
|
| 955 |
ret_strs = g_strsplit (ret_str, ",", -1); |
| 956 |
for (n = 0; ret_strs != NULL && ret_strs[n] != NULL; n++) |
| 957 |
{ |
| 958 |
const gchar *identity_str = ret_strs[n]; |
| 959 |
PolkitIdentity *identity; |
| 960 |
|
| 961 |
error = NULL; |
| 962 |
identity = polkit_identity_from_string (identity_str, &error); |
| 963 |
if (identity == NULL) |
| 964 |
{ |
| 965 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 966 |
LOG_LEVEL_WARNING, |
| 967 |
"Identity `%s' is not valid, ignoring: %s", |
| 968 |
identity_str, error->message); |
| 969 |
g_clear_error (&error); |
| 970 |
} |
| 971 |
else |
| 972 |
{ |
| 973 |
ret = g_list_prepend (ret, identity); |
| 974 |
} |
| 975 |
} |
| 976 |
ret = g_list_reverse (ret); |
| 977 |
|
| 978 |
out: |
| 979 |
g_strfreev (ret_strs); |
| 980 |
/* fallback to root password auth */ |
| 981 |
if (ret == NULL) |
| 982 |
ret = g_list_prepend (ret, polkit_unix_user_new (0)); |
| 983 |
|
| 984 |
return ret; |
| 985 |
} |
| 986 |
|
| 987 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 988 |
|
| 989 |
PolkitImplicitAuthorization |
| 990 |
polkit_backend_common_js_authority_check_authorization_sync (PolkitBackendInteractiveAuthority *_authority, |
| 991 |
PolkitSubject *caller, |
| 992 |
PolkitSubject *subject, |
| 993 |
PolkitIdentity *user_for_subject, |
| 994 |
gboolean subject_is_local, |
| 995 |
gboolean subject_is_active, |
| 996 |
const gchar *action_id, |
| 997 |
PolkitDetails *details, |
| 998 |
PolkitImplicitAuthorization implicit) |
| 999 |
{ |
| 1000 |
PolkitBackendJsAuthority *authority = POLKIT_BACKEND_JS_AUTHORITY (_authority); |
| 1001 |
PolkitImplicitAuthorization ret = implicit; |
| 1002 |
GError *error = NULL; |
| 1003 |
gchar *ret_str = NULL; |
| 1004 |
gboolean good = FALSE; |
| 1005 |
duk_context *cx = authority->priv->cx; |
| 1006 |
|
| 1007 |
duk_set_top (cx, 0); |
| 1008 |
if (!duk_get_global_string (cx, "polkit")) { |
| 1009 |
goto out; |
| 1010 |
} |
| 1011 |
|
| 1012 |
duk_push_string (cx, "_runRules"); |
| 1013 |
|
| 1014 |
if (!push_action_and_details (cx, action_id, details)) |
| 1015 |
{ |
| 1016 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 1017 |
LOG_LEVEL_ERROR, |
| 1018 |
"Error converting action and details to JS object"); |
| 1019 |
goto out; |
| 1020 |
} |
| 1021 |
|
| 1022 |
if (!push_subject (cx, subject, user_for_subject, subject_is_local, subject_is_active, &error)) |
| 1023 |
{ |
| 1024 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 1025 |
LOG_LEVEL_ERROR, |
| 1026 |
"Error converting subject to JS object: %s", |
| 1027 |
error->message); |
| 1028 |
g_clear_error (&error); |
| 1029 |
goto out; |
| 1030 |
} |
| 1031 |
|
| 1032 |
// If any error is the js context happened (ctx.ret == |
| 1033 |
// RUNAWAY_KILLER_THREAD_EXIT_STATUS_FAILURE) or it never properly returned |
| 1034 |
// (runaway scripts or ctx.ret == RUNAWAY_KILLER_THREAD_EXIT_STATUS_UNSET), |
| 1035 |
// unauthorize |
| 1036 |
if (!call_js_function_with_runaway_killer (authority)) |
| 1037 |
goto out; |
| 1038 |
|
| 1039 |
if (duk_is_null(cx, -1)) { |
| 1040 |
/* this is fine, means there was no match, use implicit authorizations */ |
| 1041 |
good = TRUE; |
| 1042 |
goto out; |
| 1043 |
} |
| 1044 |
ret_str = g_strdup (duk_require_string (cx, -1)); |
| 1045 |
if (!polkit_implicit_authorization_from_string (ret_str, &ret)) |
| 1046 |
{ |
| 1047 |
polkit_backend_authority_log (POLKIT_BACKEND_AUTHORITY (authority), |
| 1048 |
LOG_LEVEL_WARNING, |
| 1049 |
"Returned result `%s' is not valid", |
| 1050 |
ret_str); |
| 1051 |
goto out; |
| 1052 |
} |
| 1053 |
|
| 1054 |
polkit_details_insert (details, "polkit.result", ret_str); |
| 1055 |
|
| 1056 |
good = TRUE; |
| 1057 |
|
| 1058 |
out: |
| 1059 |
if (!good) |
| 1060 |
ret = POLKIT_IMPLICIT_AUTHORIZATION_NOT_AUTHORIZED; |
| 1061 |
if (ret_str != NULL) |
| 1062 |
g_free (ret_str); |
| 1063 |
|
| 1064 |
return ret; |
| 1065 |
} |
| 1066 |
|
| 1067 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 1068 |
|
| 1069 |
static duk_ret_t |
| 1070 |
js_polkit_log (duk_context *cx) |
| 1071 |
{ |
| 1072 |
const char *str = duk_require_string (cx, 0); |
| 1073 |
fprintf (stderr, "%s\n", str); |
| 1074 |
return 0; |
| 1075 |
} |
| 1076 |
|
| 1077 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 1078 |
|
| 1079 |
static duk_ret_t |
| 1080 |
js_polkit_spawn (duk_context *cx) |
| 1081 |
{ |
| 1082 |
duk_ret_t ret = DUK_RET_ERROR; |
| 1083 |
gchar *standard_output = NULL; |
| 1084 |
gchar *standard_error = NULL; |
| 1085 |
gint exit_status; |
| 1086 |
GError *error = NULL; |
| 1087 |
guint32 array_len; |
| 1088 |
gchar **argv = NULL; |
| 1089 |
GMainContext *context = NULL; |
| 1090 |
GMainLoop *loop = NULL; |
| 1091 |
SpawnData data = {0}; |
| 1092 |
char *err_str = NULL; |
| 1093 |
guint n; |
| 1094 |
|
| 1095 |
if (!duk_is_array (cx, 0)) |
| 1096 |
goto out; |
| 1097 |
|
| 1098 |
array_len = duk_get_length (cx, 0); |
| 1099 |
|
| 1100 |
argv = g_new0 (gchar*, array_len + 1); |
| 1101 |
for (n = 0; n < array_len; n++) |
| 1102 |
{ |
| 1103 |
duk_get_prop_index (cx, 0, n); |
| 1104 |
argv[n] = g_strdup (duk_to_string (cx, -1)); |
| 1105 |
duk_pop (cx); |
| 1106 |
} |
| 1107 |
|
| 1108 |
context = g_main_context_new (); |
| 1109 |
loop = g_main_loop_new (context, FALSE); |
| 1110 |
|
| 1111 |
g_main_context_push_thread_default (context); |
| 1112 |
|
| 1113 |
data.loop = loop; |
| 1114 |
polkit_backend_common_spawn ((const gchar *const *) argv, |
| 1115 |
10, /* timeout_seconds */ |
| 1116 |
NULL, /* cancellable */ |
| 1117 |
polkit_backend_common_spawn_cb, |
| 1118 |
&data); |
| 1119 |
|
| 1120 |
g_main_loop_run (loop); |
| 1121 |
|
| 1122 |
g_main_context_pop_thread_default (context); |
| 1123 |
|
| 1124 |
if (!polkit_backend_common_spawn_finish (data.res, |
| 1125 |
&exit_status, |
| 1126 |
&standard_output, |
| 1127 |
&standard_error, |
| 1128 |
&error)) |
| 1129 |
{ |
| 1130 |
err_str = g_strdup_printf ("Error spawning helper: %s (%s, %d)", |
| 1131 |
error->message, g_quark_to_string (error->domain), error->code); |
| 1132 |
g_clear_error (&error); |
| 1133 |
goto out; |
| 1134 |
} |
| 1135 |
|
| 1136 |
if (!(WIFEXITED (exit_status) && WEXITSTATUS (exit_status) == 0)) |
| 1137 |
{ |
| 1138 |
GString *gstr; |
| 1139 |
gstr = g_string_new (NULL); |
| 1140 |
if (WIFEXITED (exit_status)) |
| 1141 |
{ |
| 1142 |
g_string_append_printf (gstr, |
| 1143 |
"Helper exited with non-zero exit status %d", |
| 1144 |
WEXITSTATUS (exit_status)); |
| 1145 |
} |
| 1146 |
else if (WIFSIGNALED (exit_status)) |
| 1147 |
{ |
| 1148 |
g_string_append_printf (gstr, |
| 1149 |
"Helper was signaled with signal %s (%d)", |
| 1150 |
polkit_backend_common_get_signal_name (WTERMSIG (exit_status)), |
| 1151 |
WTERMSIG (exit_status)); |
| 1152 |
} |
| 1153 |
g_string_append_printf (gstr, ", stdout=`%s', stderr=`%s'", |
| 1154 |
standard_output, standard_error); |
| 1155 |
err_str = g_string_free (gstr, FALSE); |
| 1156 |
goto out; |
| 1157 |
} |
| 1158 |
|
| 1159 |
duk_push_string (cx, standard_output); |
| 1160 |
ret = 1; |
| 1161 |
|
| 1162 |
out: |
| 1163 |
g_strfreev (argv); |
| 1164 |
g_free (standard_output); |
| 1165 |
g_free (standard_error); |
| 1166 |
g_clear_object (&data.res); |
| 1167 |
if (loop != NULL) |
| 1168 |
g_main_loop_unref (loop); |
| 1169 |
if (context != NULL) |
| 1170 |
g_main_context_unref (context); |
| 1171 |
|
| 1172 |
if (err_str) |
| 1173 |
{ |
| 1174 |
duk_push_error_object (cx, DUK_ERR_ERROR, err_str); |
| 1175 |
free (err_str); |
| 1176 |
duk_throw (cx); |
| 1177 |
g_assert_not_reached (); |
| 1178 |
} |
| 1179 |
|
| 1180 |
return ret; |
| 1181 |
} |
| 1182 |
|
| 1183 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 1184 |
|
| 1185 |
|
| 1186 |
static duk_ret_t |
| 1187 |
js_polkit_user_is_in_netgroup (duk_context *cx) |
| 1188 |
{ |
| 1189 |
gboolean is_in_netgroup = FALSE; |
| 1190 |
#ifdef HAVE_SETNETGRENT |
| 1191 |
const char *user; |
| 1192 |
const char *netgroup; |
| 1193 |
|
| 1194 |
user = duk_require_string (cx, 0); |
| 1195 |
netgroup = duk_require_string (cx, 1); |
| 1196 |
if (innetgr (netgroup, |
| 1197 |
NULL, /* host */ |
| 1198 |
user, |
| 1199 |
NULL)) /* domain */ |
| 1200 |
{ |
| 1201 |
is_in_netgroup = TRUE; |
| 1202 |
} |
| 1203 |
#endif |
| 1204 |
duk_push_boolean (cx, is_in_netgroup); |
| 1205 |
return 1; |
| 1206 |
} |
| 1207 |
|
| 1208 |
/* ---------------------------------------------------------------------------------------------------- */ |