| 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 <grp.h> |
| 25 |
#include <string.h> |
| 26 |
#include <glib/gstdio.h> |
| 27 |
|
| 28 |
#include <polkit/polkit.h> |
| 29 |
#include <polkit/polkitprivate.h> |
| 30 |
#include "polkitbackendsessionmonitor.h" |
| 31 |
|
| 32 |
#define CKDB_PATH "/var/run/ConsoleKit/database" |
| 33 |
|
| 34 |
/* <internal> |
| 35 |
* SECTION:polkitbackendsessionmonitor |
| 36 |
* @title: PolkitBackendSessionMonitor |
| 37 |
* @short_description: Monitor sessions |
| 38 |
* |
| 39 |
* The #PolkitBackendSessionMonitor class is a utility class to track and monitor sessions. |
| 40 |
*/ |
| 41 |
|
| 42 |
struct _PolkitBackendSessionMonitor |
| 43 |
{ |
| 44 |
GObject parent_instance; |
| 45 |
|
| 46 |
GDBusConnection *system_bus; |
| 47 |
|
| 48 |
GKeyFile *database; |
| 49 |
GFileMonitor *database_monitor; |
| 50 |
time_t database_mtime; |
| 51 |
}; |
| 52 |
|
| 53 |
struct _PolkitBackendSessionMonitorClass |
| 54 |
{ |
| 55 |
GObjectClass parent_class; |
| 56 |
|
| 57 |
void (*changed) (PolkitBackendSessionMonitor *monitor); |
| 58 |
}; |
| 59 |
|
| 60 |
|
| 61 |
enum |
| 62 |
{ |
| 63 |
CHANGED_SIGNAL, |
| 64 |
LAST_SIGNAL, |
| 65 |
}; |
| 66 |
|
| 67 |
static guint signals[LAST_SIGNAL] = {0}; |
| 68 |
|
| 69 |
G_DEFINE_TYPE (PolkitBackendSessionMonitor, polkit_backend_session_monitor, G_TYPE_OBJECT); |
| 70 |
|
| 71 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 72 |
|
| 73 |
static gboolean |
| 74 |
reload_database (PolkitBackendSessionMonitor *monitor, |
| 75 |
GError **error) |
| 76 |
{ |
| 77 |
gboolean ret; |
| 78 |
struct stat statbuf; |
| 79 |
|
| 80 |
ret = FALSE; |
| 81 |
|
| 82 |
if (monitor->database != NULL) |
| 83 |
{ |
| 84 |
g_key_file_free (monitor->database); |
| 85 |
monitor->database = NULL; |
| 86 |
} |
| 87 |
|
| 88 |
if (stat (CKDB_PATH, &statbuf) != 0) |
| 89 |
{ |
| 90 |
g_set_error (error, |
| 91 |
G_IO_ERROR, |
| 92 |
g_io_error_from_errno (errno), |
| 93 |
"Error statting file " CKDB_PATH ": %s", |
| 94 |
strerror (errno)); |
| 95 |
goto out; |
| 96 |
} |
| 97 |
|
| 98 |
monitor->database_mtime = statbuf.st_mtime; |
| 99 |
|
| 100 |
monitor->database = g_key_file_new (); |
| 101 |
if (!g_key_file_load_from_file (monitor->database, |
| 102 |
CKDB_PATH, |
| 103 |
G_KEY_FILE_NONE, |
| 104 |
error)) |
| 105 |
{ |
| 106 |
goto out; |
| 107 |
} |
| 108 |
|
| 109 |
ret = TRUE; |
| 110 |
|
| 111 |
out: |
| 112 |
return ret; |
| 113 |
} |
| 114 |
|
| 115 |
static gboolean |
| 116 |
ensure_database (PolkitBackendSessionMonitor *monitor, |
| 117 |
GError **error) |
| 118 |
{ |
| 119 |
gboolean ret = FALSE; |
| 120 |
|
| 121 |
if (monitor->database != NULL) |
| 122 |
{ |
| 123 |
struct stat statbuf; |
| 124 |
|
| 125 |
if (stat (CKDB_PATH, &statbuf) != 0) |
| 126 |
{ |
| 127 |
g_set_error (error, |
| 128 |
G_IO_ERROR, |
| 129 |
g_io_error_from_errno (errno), |
| 130 |
"Error statting file " CKDB_PATH " to check timestamp: %s", |
| 131 |
strerror (errno)); |
| 132 |
goto out; |
| 133 |
} |
| 134 |
if (statbuf.st_mtime == monitor->database_mtime) |
| 135 |
{ |
| 136 |
ret = TRUE; |
| 137 |
goto out; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
ret = reload_database (monitor, error); |
| 142 |
|
| 143 |
out: |
| 144 |
return ret; |
| 145 |
} |
| 146 |
|
| 147 |
static void |
| 148 |
on_file_monitor_changed (GFileMonitor *file_monitor, |
| 149 |
GFile *file, |
| 150 |
GFile *other_file, |
| 151 |
GFileMonitorEvent event_type, |
| 152 |
gpointer user_data) |
| 153 |
{ |
| 154 |
PolkitBackendSessionMonitor *monitor = POLKIT_BACKEND_SESSION_MONITOR (user_data); |
| 155 |
|
| 156 |
/* throw away cache */ |
| 157 |
if (monitor->database != NULL) |
| 158 |
{ |
| 159 |
g_key_file_free (monitor->database); |
| 160 |
monitor->database = NULL; |
| 161 |
} |
| 162 |
g_signal_emit (monitor, signals[CHANGED_SIGNAL], 0); |
| 163 |
} |
| 164 |
|
| 165 |
static void |
| 166 |
polkit_backend_session_monitor_init (PolkitBackendSessionMonitor *monitor) |
| 167 |
{ |
| 168 |
GError *error; |
| 169 |
GFile *file; |
| 170 |
|
| 171 |
error = NULL; |
| 172 |
monitor->system_bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); |
| 173 |
if (monitor->system_bus == NULL) |
| 174 |
{ |
| 175 |
g_printerr ("Error getting system bus: %s", error->message); |
| 176 |
g_error_free (error); |
| 177 |
} |
| 178 |
|
| 179 |
error = NULL; |
| 180 |
if (!ensure_database (monitor, &error)) |
| 181 |
{ |
| 182 |
g_printerr ("Error loading " CKDB_PATH ": %s", error->message); |
| 183 |
g_error_free (error); |
| 184 |
} |
| 185 |
|
| 186 |
error = NULL; |
| 187 |
file = g_file_new_for_path (CKDB_PATH); |
| 188 |
monitor->database_monitor = g_file_monitor_file (file, |
| 189 |
G_FILE_MONITOR_NONE, |
| 190 |
NULL, |
| 191 |
&error); |
| 192 |
g_object_unref (file); |
| 193 |
if (monitor->database_monitor == NULL) |
| 194 |
{ |
| 195 |
g_printerr ("Error monitoring " CKDB_PATH ": %s", error->message); |
| 196 |
g_error_free (error); |
| 197 |
} |
| 198 |
else |
| 199 |
{ |
| 200 |
g_signal_connect (monitor->database_monitor, |
| 201 |
"changed", |
| 202 |
G_CALLBACK (on_file_monitor_changed), |
| 203 |
monitor); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
static void |
| 208 |
polkit_backend_session_monitor_finalize (GObject *object) |
| 209 |
{ |
| 210 |
PolkitBackendSessionMonitor *monitor = POLKIT_BACKEND_SESSION_MONITOR (object); |
| 211 |
|
| 212 |
if (monitor->system_bus != NULL) |
| 213 |
g_object_unref (monitor->system_bus); |
| 214 |
|
| 215 |
if (monitor->database_monitor != NULL) |
| 216 |
g_object_unref (monitor->database_monitor); |
| 217 |
|
| 218 |
if (monitor->database != NULL) |
| 219 |
g_key_file_free (monitor->database); |
| 220 |
|
| 221 |
if (G_OBJECT_CLASS (polkit_backend_session_monitor_parent_class)->finalize != NULL) |
| 222 |
G_OBJECT_CLASS (polkit_backend_session_monitor_parent_class)->finalize (object); |
| 223 |
} |
| 224 |
|
| 225 |
static void |
| 226 |
polkit_backend_session_monitor_class_init (PolkitBackendSessionMonitorClass *klass) |
| 227 |
{ |
| 228 |
GObjectClass *gobject_class; |
| 229 |
|
| 230 |
gobject_class = G_OBJECT_CLASS (klass); |
| 231 |
|
| 232 |
gobject_class->finalize = polkit_backend_session_monitor_finalize; |
| 233 |
|
| 234 |
/** |
| 235 |
* PolkitBackendSessionMonitor::changed: |
| 236 |
* @monitor: A #PolkitBackendSessionMonitor |
| 237 |
* |
| 238 |
* Emitted when something changes. |
| 239 |
*/ |
| 240 |
signals[CHANGED_SIGNAL] = g_signal_new ("changed", |
| 241 |
POLKIT_BACKEND_TYPE_SESSION_MONITOR, |
| 242 |
G_SIGNAL_RUN_LAST, |
| 243 |
G_STRUCT_OFFSET (PolkitBackendSessionMonitorClass, changed), |
| 244 |
NULL, /* accumulator */ |
| 245 |
NULL, /* accumulator data */ |
| 246 |
g_cclosure_marshal_VOID__VOID, |
| 247 |
G_TYPE_NONE, |
| 248 |
0); |
| 249 |
} |
| 250 |
|
| 251 |
PolkitBackendSessionMonitor * |
| 252 |
polkit_backend_session_monitor_new (void) |
| 253 |
{ |
| 254 |
PolkitBackendSessionMonitor *monitor; |
| 255 |
|
| 256 |
monitor = POLKIT_BACKEND_SESSION_MONITOR (g_object_new (POLKIT_BACKEND_TYPE_SESSION_MONITOR, NULL)); |
| 257 |
|
| 258 |
return monitor; |
| 259 |
} |
| 260 |
|
| 261 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 262 |
|
| 263 |
GList * |
| 264 |
polkit_backend_session_monitor_get_sessions (PolkitBackendSessionMonitor *monitor) |
| 265 |
{ |
| 266 |
/* TODO */ |
| 267 |
return NULL; |
| 268 |
} |
| 269 |
|
| 270 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 271 |
|
| 272 |
/** |
| 273 |
* polkit_backend_session_monitor_get_user: |
| 274 |
* @monitor: A #PolkitBackendSessionMonitor. |
| 275 |
* @subject: A #PolkitSubject. |
| 276 |
* @result_matches: If not %NULL, set to indicate whether the return value matches current (RACY) state. |
| 277 |
* @error: Return location for error. |
| 278 |
* |
| 279 |
* Gets the user corresponding to @subject or %NULL if no user exists. |
| 280 |
* |
| 281 |
* NOTE: For a #PolkitUnixProcess, the UID is read from @subject (which may |
| 282 |
* come from e.g. a D-Bus client), so it may not correspond to the actual UID |
| 283 |
* of the referenced process (at any point in time). This is indicated by |
| 284 |
* setting @result_matches to %FALSE; the caller may reject such subjects or |
| 285 |
* require additional privileges. @result_matches == %TRUE only indicates that |
| 286 |
* the UID matched the underlying process at ONE point in time, it may not match |
| 287 |
* later. |
| 288 |
* |
| 289 |
* Returns: %NULL if @error is set otherwise a #PolkitUnixUser that should be freed with g_object_unref(). |
| 290 |
*/ |
| 291 |
PolkitIdentity * |
| 292 |
polkit_backend_session_monitor_get_user_for_subject (PolkitBackendSessionMonitor *monitor, |
| 293 |
PolkitSubject *subject, |
| 294 |
gboolean *result_matches, |
| 295 |
GError **error) |
| 296 |
{ |
| 297 |
PolkitIdentity *ret; |
| 298 |
gboolean matches; |
| 299 |
GError *local_error; |
| 300 |
|
| 301 |
ret = NULL; |
| 302 |
matches = FALSE; |
| 303 |
|
| 304 |
if (POLKIT_IS_UNIX_PROCESS (subject)) |
| 305 |
{ |
| 306 |
gint subject_uid, current_uid; |
| 307 |
|
| 308 |
subject_uid = polkit_unix_process_get_uid (POLKIT_UNIX_PROCESS (subject)); |
| 309 |
if (subject_uid == -1) |
| 310 |
{ |
| 311 |
g_set_error (error, |
| 312 |
POLKIT_ERROR, |
| 313 |
POLKIT_ERROR_FAILED, |
| 314 |
"Unix process subject does not have uid set"); |
| 315 |
goto out; |
| 316 |
} |
| 317 |
local_error = NULL; |
| 318 |
current_uid = polkit_unix_process_get_racy_uid__ (POLKIT_UNIX_PROCESS (subject), &local_error); |
| 319 |
if (local_error != NULL) |
| 320 |
{ |
| 321 |
g_propagate_error (error, local_error); |
| 322 |
goto out; |
| 323 |
} |
| 324 |
ret = polkit_unix_user_new (subject_uid); |
| 325 |
matches = (subject_uid == current_uid); |
| 326 |
} |
| 327 |
else if (POLKIT_IS_SYSTEM_BUS_NAME (subject)) |
| 328 |
{ |
| 329 |
ret = (PolkitIdentity*)polkit_system_bus_name_get_user_sync (POLKIT_SYSTEM_BUS_NAME (subject), NULL, error); |
| 330 |
matches = TRUE; |
| 331 |
} |
| 332 |
else if (POLKIT_IS_UNIX_SESSION (subject)) |
| 333 |
{ |
| 334 |
gint uid; |
| 335 |
gchar *group; |
| 336 |
|
| 337 |
if (!ensure_database (monitor, error)) |
| 338 |
{ |
| 339 |
g_prefix_error (error, "Error getting user for session: Error ensuring CK database at " CKDB_PATH ": "); |
| 340 |
goto out; |
| 341 |
} |
| 342 |
|
| 343 |
group = g_strdup_printf ("Session %s", polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (subject))); |
| 344 |
local_error = NULL; |
| 345 |
uid = g_key_file_get_integer (monitor->database, group, "uid", &local_error); |
| 346 |
if (local_error != NULL) |
| 347 |
{ |
| 348 |
g_propagate_prefixed_error (error, local_error, "Error getting uid using " CKDB_PATH ": "); |
| 349 |
g_free (group); |
| 350 |
goto out; |
| 351 |
} |
| 352 |
g_free (group); |
| 353 |
|
| 354 |
ret = polkit_unix_user_new (uid); |
| 355 |
matches = TRUE; |
| 356 |
} |
| 357 |
|
| 358 |
out: |
| 359 |
if (result_matches != NULL) |
| 360 |
{ |
| 361 |
*result_matches = matches; |
| 362 |
} |
| 363 |
return ret; |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* polkit_backend_session_monitor_get_session_for_subject: |
| 368 |
* @monitor: A #PolkitBackendSessionMonitor. |
| 369 |
* @subject: A #PolkitSubject. |
| 370 |
* @error: Return location for error. |
| 371 |
* |
| 372 |
* Gets the session corresponding to @subject or %NULL if no session exists. |
| 373 |
* |
| 374 |
* Returns: %NULL if @error is set otherwise a #PolkitUnixSession that should be freed with g_object_unref(). |
| 375 |
*/ |
| 376 |
PolkitSubject * |
| 377 |
polkit_backend_session_monitor_get_session_for_subject (PolkitBackendSessionMonitor *monitor, |
| 378 |
PolkitSubject *subject, |
| 379 |
GError **error) |
| 380 |
{ |
| 381 |
PolkitSubject *session; |
| 382 |
|
| 383 |
session = NULL; |
| 384 |
|
| 385 |
if (POLKIT_IS_UNIX_PROCESS (subject)) |
| 386 |
{ |
| 387 |
const gchar *session_id; |
| 388 |
GVariant *result; |
| 389 |
result = g_dbus_connection_call_sync (monitor->system_bus, |
| 390 |
"org.freedesktop.ConsoleKit", |
| 391 |
"/org/freedesktop/ConsoleKit/Manager", |
| 392 |
"org.freedesktop.ConsoleKit.Manager", |
| 393 |
"GetSessionForUnixProcess", |
| 394 |
g_variant_new ("(u)", polkit_unix_process_get_pid (POLKIT_UNIX_PROCESS (subject))), |
| 395 |
G_VARIANT_TYPE ("(o)"), |
| 396 |
G_DBUS_CALL_FLAGS_NONE, |
| 397 |
-1, /* timeout_msec */ |
| 398 |
NULL, /* GCancellable */ |
| 399 |
error); |
| 400 |
if (result == NULL) |
| 401 |
goto out; |
| 402 |
g_variant_get (result, "(&o)", &session_id); |
| 403 |
session = polkit_unix_session_new (session_id); |
| 404 |
g_variant_unref (result); |
| 405 |
} |
| 406 |
else if (POLKIT_IS_SYSTEM_BUS_NAME (subject)) |
| 407 |
{ |
| 408 |
guint32 pid; |
| 409 |
const gchar *session_id; |
| 410 |
GVariant *result; |
| 411 |
|
| 412 |
result = g_dbus_connection_call_sync (monitor->system_bus, |
| 413 |
"org.freedesktop.DBus", |
| 414 |
"/org/freedesktop/DBus", |
| 415 |
"org.freedesktop.DBus", |
| 416 |
"GetConnectionUnixProcessID", |
| 417 |
g_variant_new ("(s)", polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (subject))), |
| 418 |
G_VARIANT_TYPE ("(u)"), |
| 419 |
G_DBUS_CALL_FLAGS_NONE, |
| 420 |
-1, /* timeout_msec */ |
| 421 |
NULL, /* GCancellable */ |
| 422 |
error); |
| 423 |
if (result == NULL) |
| 424 |
goto out; |
| 425 |
g_variant_get (result, "(u)", &pid); |
| 426 |
g_variant_unref (result); |
| 427 |
|
| 428 |
result = g_dbus_connection_call_sync (monitor->system_bus, |
| 429 |
"org.freedesktop.ConsoleKit", |
| 430 |
"/org/freedesktop/ConsoleKit/Manager", |
| 431 |
"org.freedesktop.ConsoleKit.Manager", |
| 432 |
"GetSessionForUnixProcess", |
| 433 |
g_variant_new ("(u)", pid), |
| 434 |
G_VARIANT_TYPE ("(o)"), |
| 435 |
G_DBUS_CALL_FLAGS_NONE, |
| 436 |
-1, /* timeout_msec */ |
| 437 |
NULL, /* GCancellable */ |
| 438 |
error); |
| 439 |
if (result == NULL) |
| 440 |
goto out; |
| 441 |
g_variant_get (result, "(&o)", &session_id); |
| 442 |
session = polkit_unix_session_new (session_id); |
| 443 |
g_variant_unref (result); |
| 444 |
} |
| 445 |
else |
| 446 |
{ |
| 447 |
g_set_error (error, |
| 448 |
POLKIT_ERROR, |
| 449 |
POLKIT_ERROR_NOT_SUPPORTED, |
| 450 |
"Cannot get user for subject of type %s", |
| 451 |
g_type_name (G_TYPE_FROM_INSTANCE (subject))); |
| 452 |
} |
| 453 |
|
| 454 |
out: |
| 455 |
|
| 456 |
return session; |
| 457 |
} |
| 458 |
|
| 459 |
static gboolean |
| 460 |
get_boolean (PolkitBackendSessionMonitor *monitor, |
| 461 |
PolkitSubject *session, |
| 462 |
const gchar *key_name) |
| 463 |
{ |
| 464 |
gboolean ret; |
| 465 |
gchar *group; |
| 466 |
GError *error; |
| 467 |
|
| 468 |
ret = FALSE; |
| 469 |
|
| 470 |
group = g_strdup_printf ("Session %s", polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session))); |
| 471 |
|
| 472 |
error = NULL; |
| 473 |
if (!ensure_database (monitor, &error)) |
| 474 |
{ |
| 475 |
g_printerr ("Error getting boolean `%s' in group `%s': Error ensuring CK database at " CKDB_PATH ": %s", |
| 476 |
key_name, |
| 477 |
group, |
| 478 |
error->message); |
| 479 |
g_error_free (error); |
| 480 |
goto out; |
| 481 |
} |
| 482 |
|
| 483 |
error = NULL; |
| 484 |
ret = g_key_file_get_boolean (monitor->database, group, key_name, &error); |
| 485 |
if (error != NULL) |
| 486 |
{ |
| 487 |
g_printerr ("Error looking %s using " CKDB_PATH " for %s: %s\n", |
| 488 |
key_name, |
| 489 |
group, |
| 490 |
error->message); |
| 491 |
g_error_free (error); |
| 492 |
goto out; |
| 493 |
} |
| 494 |
|
| 495 |
out: |
| 496 |
g_free (group); |
| 497 |
return ret; |
| 498 |
} |
| 499 |
|
| 500 |
gboolean |
| 501 |
polkit_backend_session_monitor_is_session_local (PolkitBackendSessionMonitor *monitor, |
| 502 |
PolkitSubject *session) |
| 503 |
{ |
| 504 |
return get_boolean (monitor, session, "is_local"); |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
gboolean |
| 509 |
polkit_backend_session_monitor_is_session_active (PolkitBackendSessionMonitor *monitor, |
| 510 |
PolkitSubject *session) |
| 511 |
{ |
| 512 |
return get_boolean (monitor, session, "is_active"); |
| 513 |
} |
| 514 |
|