| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- |
| 2 |
* vim: set et ts=8 sw=8: |
| 3 |
* |
| 4 |
* Copyright (C) 2008 Novell, Inc. |
| 5 |
* |
| 6 |
* Authors: Vincent Untz |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation; either version 2 of the License, or |
| 11 |
* (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 21 |
* |
| 22 |
* The code is originally based on gnome-clock-applet-mechanism.c, which |
| 23 |
* is under the same license and with the following copyright: |
| 24 |
* |
| 25 |
* Copyright (C) 2007 David Zeuthen <[email protected]> |
| 26 |
* |
| 27 |
*/ |
| 28 |
|
| 29 |
#ifdef HAVE_CONFIG_H |
| 30 |
# include "config.h" |
| 31 |
#endif |
| 32 |
|
| 33 |
#include <stdlib.h> |
| 34 |
#include <stdio.h> |
| 35 |
#include <fcntl.h> |
| 36 |
#include <unistd.h> |
| 37 |
#include <string.h> |
| 38 |
#include <sys/wait.h> |
| 39 |
#include <errno.h> |
| 40 |
#include <sys/time.h> |
| 41 |
|
| 42 |
#include <glib.h> |
| 43 |
#include <glib-object.h> |
| 44 |
#include <gio/gio.h> |
| 45 |
|
| 46 |
#include <polkit/polkit.h> |
| 47 |
|
| 48 |
#include <pwd.h> |
| 49 |
|
| 50 |
#include "cups-pk-helper-mechanism.h" |
| 51 |
#include "cph-iface-mechanism.h" |
| 52 |
#include "cups.h" |
| 53 |
|
| 54 |
#define CPH_SERVICE_DBUS "org.freedesktop.DBus" |
| 55 |
#define CPH_PATH_DBUS "/org/freedesktop/DBus" |
| 56 |
#define CPH_INTERFACE_DBUS "org.freedesktop.DBus" |
| 57 |
|
| 58 |
/* error */ |
| 59 |
|
| 60 |
static const GDBusErrorEntry cph_error_entries[] = |
| 61 |
{ |
| 62 |
{ CPH_MECHANISM_ERROR_GENERAL, "org.opensuse.CupsPkHelper.Mechanism.GeneralError" }, |
| 63 |
{ CPH_MECHANISM_ERROR_NOT_PRIVILEGED, "org.opensuse.CupsPkHelper.Mechanism.NotPrivileged" } |
| 64 |
}; |
| 65 |
|
| 66 |
GQuark |
| 67 |
cph_mechanism_error_quark (void) |
| 68 |
{ |
| 69 |
static gsize ret = 0; |
| 70 |
|
| 71 |
if (ret == 0) { |
| 72 |
g_assert (CPH_MECHANISM_NUM_ERRORS == G_N_ELEMENTS (cph_error_entries)); |
| 73 |
|
| 74 |
g_dbus_error_register_error_domain ("cph-mechanism-error", |
| 75 |
&ret, |
| 76 |
cph_error_entries, |
| 77 |
G_N_ELEMENTS (cph_error_entries)); |
| 78 |
} |
| 79 |
|
| 80 |
return ret; |
| 81 |
} |
| 82 |
|
| 83 |
/* mechanism object */ |
| 84 |
|
| 85 |
G_DEFINE_TYPE (CphMechanism, cph_mechanism, CPH_IFACE_TYPE_MECHANISM_SKELETON) |
| 86 |
|
| 87 |
#define CPH_MECHANISM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CPH_TYPE_MECHANISM, CphMechanismPrivate)) |
| 88 |
|
| 89 |
struct CphMechanismPrivate |
| 90 |
{ |
| 91 |
gboolean exported; |
| 92 |
gboolean connected; |
| 93 |
PolkitAuthority *pol_auth; |
| 94 |
CphCups *cups; |
| 95 |
GDBusProxy *dbus_proxy; |
| 96 |
}; |
| 97 |
|
| 98 |
enum { |
| 99 |
CALLED, |
| 100 |
LAST_SIGNAL |
| 101 |
}; |
| 102 |
|
| 103 |
static guint signals[LAST_SIGNAL] = { 0 }; |
| 104 |
|
| 105 |
static GObject *cph_mechanism_constructor (GType type, |
| 106 |
guint n_construct_properties, |
| 107 |
GObjectConstructParam *construct_properties); |
| 108 |
static void cph_mechanism_dispose (GObject *object); |
| 109 |
static void cph_mechanism_finalize (GObject *object); |
| 110 |
|
| 111 |
static void cph_mechanism_connect_signals (CphMechanism *mechanism); |
| 112 |
|
| 113 |
static void |
| 114 |
cph_mechanism_class_init (CphMechanismClass *klass) |
| 115 |
{ |
| 116 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 117 |
|
| 118 |
object_class->constructor = cph_mechanism_constructor; |
| 119 |
object_class->dispose = cph_mechanism_dispose; |
| 120 |
object_class->finalize = cph_mechanism_finalize; |
| 121 |
|
| 122 |
signals[CALLED] = |
| 123 |
g_signal_new ("called", |
| 124 |
G_OBJECT_CLASS_TYPE (object_class), |
| 125 |
G_SIGNAL_RUN_LAST, |
| 126 |
G_STRUCT_OFFSET (CphMechanismClass, called), |
| 127 |
NULL, NULL, |
| 128 |
g_cclosure_marshal_VOID__VOID, |
| 129 |
G_TYPE_NONE, |
| 130 |
0); |
| 131 |
|
| 132 |
g_type_class_add_private (klass, sizeof (CphMechanismPrivate)); |
| 133 |
} |
| 134 |
|
| 135 |
static GObject * |
| 136 |
cph_mechanism_constructor (GType type, |
| 137 |
guint n_construct_properties, |
| 138 |
GObjectConstructParam *construct_properties) |
| 139 |
{ |
| 140 |
GObject *obj; |
| 141 |
CphMechanism *mechanism; |
| 142 |
|
| 143 |
obj = G_OBJECT_CLASS (cph_mechanism_parent_class)->constructor ( |
| 144 |
type, |
| 145 |
n_construct_properties, |
| 146 |
construct_properties); |
| 147 |
|
| 148 |
mechanism = CPH_MECHANISM (obj); |
| 149 |
mechanism->priv->cups = cph_cups_new (); |
| 150 |
|
| 151 |
if (!mechanism->priv->cups) { |
| 152 |
g_object_unref (mechanism); |
| 153 |
return NULL; |
| 154 |
} |
| 155 |
|
| 156 |
return obj; |
| 157 |
} |
| 158 |
|
| 159 |
static void |
| 160 |
cph_mechanism_init (CphMechanism *mechanism) |
| 161 |
{ |
| 162 |
mechanism->priv = CPH_MECHANISM_GET_PRIVATE (mechanism); |
| 163 |
|
| 164 |
mechanism->priv->exported = FALSE; |
| 165 |
mechanism->priv->connected = FALSE; |
| 166 |
mechanism->priv->pol_auth = NULL; |
| 167 |
mechanism->priv->cups = NULL; |
| 168 |
mechanism->priv->dbus_proxy = NULL; |
| 169 |
} |
| 170 |
|
| 171 |
static void |
| 172 |
cph_mechanism_dispose (GObject *object) |
| 173 |
{ |
| 174 |
CphMechanism *mechanism; |
| 175 |
|
| 176 |
g_return_if_fail (object != NULL); |
| 177 |
g_return_if_fail (CPH_IS_MECHANISM (object)); |
| 178 |
|
| 179 |
mechanism = CPH_MECHANISM (object); |
| 180 |
|
| 181 |
if (mechanism->priv->pol_auth != NULL) |
| 182 |
g_object_unref (mechanism->priv->pol_auth); |
| 183 |
mechanism->priv->pol_auth = NULL; |
| 184 |
|
| 185 |
if (mechanism->priv->cups != NULL) |
| 186 |
g_object_unref (mechanism->priv->cups); |
| 187 |
mechanism->priv->cups = NULL; |
| 188 |
|
| 189 |
if (mechanism->priv->dbus_proxy != NULL) |
| 190 |
g_object_unref (mechanism->priv->dbus_proxy); |
| 191 |
mechanism->priv->dbus_proxy = NULL; |
| 192 |
|
| 193 |
G_OBJECT_CLASS (cph_mechanism_parent_class)->dispose (object); |
| 194 |
} |
| 195 |
|
| 196 |
static void |
| 197 |
cph_mechanism_finalize (GObject *object) |
| 198 |
{ |
| 199 |
CphMechanism *mechanism; |
| 200 |
|
| 201 |
g_return_if_fail (object != NULL); |
| 202 |
g_return_if_fail (CPH_IS_MECHANISM (object)); |
| 203 |
|
| 204 |
mechanism = CPH_MECHANISM (object); |
| 205 |
|
| 206 |
if (mechanism->priv->exported) |
| 207 |
g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (mechanism)); |
| 208 |
mechanism->priv->exported = FALSE; |
| 209 |
|
| 210 |
G_OBJECT_CLASS (cph_mechanism_parent_class)->finalize (object); |
| 211 |
} |
| 212 |
|
| 213 |
CphMechanism * |
| 214 |
cph_mechanism_new (void) |
| 215 |
{ |
| 216 |
GObject *object; |
| 217 |
|
| 218 |
object = g_object_new (CPH_TYPE_MECHANISM, NULL); |
| 219 |
|
| 220 |
return CPH_MECHANISM (object); |
| 221 |
} |
| 222 |
|
| 223 |
gboolean |
| 224 |
cph_mechanism_register (CphMechanism *mechanism, |
| 225 |
GDBusConnection *connection, |
| 226 |
const char *object_path, |
| 227 |
GError **error) |
| 228 |
{ |
| 229 |
gboolean ret; |
| 230 |
|
| 231 |
g_return_val_if_fail (CPH_IS_MECHANISM (mechanism), FALSE); |
| 232 |
g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 233 |
|
| 234 |
if (mechanism->priv->exported && mechanism->priv->pol_auth != NULL) |
| 235 |
return TRUE; |
| 236 |
|
| 237 |
if (!mechanism->priv->exported) { |
| 238 |
ret = g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (mechanism), |
| 239 |
connection, |
| 240 |
object_path, |
| 241 |
error); |
| 242 |
if (!ret) |
| 243 |
return FALSE; |
| 244 |
|
| 245 |
mechanism->priv->exported = TRUE; |
| 246 |
} |
| 247 |
|
| 248 |
if (mechanism->priv->pol_auth == NULL) { |
| 249 |
mechanism->priv->pol_auth = polkit_authority_get_sync (NULL, error); |
| 250 |
if (mechanism->priv->pol_auth == NULL) |
| 251 |
return FALSE; |
| 252 |
} |
| 253 |
|
| 254 |
cph_mechanism_connect_signals (mechanism); |
| 255 |
|
| 256 |
return TRUE; |
| 257 |
} |
| 258 |
|
| 259 |
/* polkit helpers */ |
| 260 |
|
| 261 |
static gboolean |
| 262 |
_check_polkit_for_action_internal (CphMechanism *mechanism, |
| 263 |
GDBusMethodInvocation *context, |
| 264 |
const char *action_method, |
| 265 |
gboolean allow_user_interaction, |
| 266 |
GError **error) |
| 267 |
{ |
| 268 |
const char *sender; |
| 269 |
PolkitSubject *subject; |
| 270 |
PolkitAuthorizationResult *pk_result; |
| 271 |
char *action; |
| 272 |
GError *local_error; |
| 273 |
|
| 274 |
g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 275 |
|
| 276 |
local_error = NULL; |
| 277 |
|
| 278 |
action = g_strdup_printf ("org.opensuse.cupspkhelper.mechanism.%s", |
| 279 |
action_method); |
| 280 |
|
| 281 |
/* Check that caller is privileged */ |
| 282 |
sender = g_dbus_method_invocation_get_sender (context); |
| 283 |
subject = polkit_system_bus_name_new (sender); |
| 284 |
|
| 285 |
pk_result = polkit_authority_check_authorization_sync (mechanism->priv->pol_auth, |
| 286 |
subject, |
| 287 |
action, |
| 288 |
NULL, |
| 289 |
allow_user_interaction ? |
| 290 |
POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION : |
| 291 |
POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE, |
| 292 |
NULL, |
| 293 |
&local_error); |
| 294 |
g_object_unref (subject); |
| 295 |
|
| 296 |
if (local_error) { |
| 297 |
g_propagate_error (error, local_error); |
| 298 |
g_free (action); |
| 299 |
|
| 300 |
return FALSE; |
| 301 |
} |
| 302 |
|
| 303 |
if (!polkit_authorization_result_get_is_authorized (pk_result)) { |
| 304 |
g_set_error (error, |
| 305 |
CPH_MECHANISM_ERROR, |
| 306 |
CPH_MECHANISM_ERROR_NOT_PRIVILEGED, |
| 307 |
"Not Authorized for action: %s", action); |
| 308 |
g_free (action); |
| 309 |
g_object_unref (pk_result); |
| 310 |
|
| 311 |
return FALSE; |
| 312 |
} |
| 313 |
|
| 314 |
g_free (action); |
| 315 |
g_object_unref (pk_result); |
| 316 |
|
| 317 |
return TRUE; |
| 318 |
} |
| 319 |
|
| 320 |
static gboolean |
| 321 |
_check_polkit_for_action_v (CphMechanism *mechanism, |
| 322 |
GDBusMethodInvocation *context, |
| 323 |
const char *first_action_method, |
| 324 |
...) |
| 325 |
{ |
| 326 |
gboolean retval; |
| 327 |
GError *error; |
| 328 |
va_list var_args; |
| 329 |
const char *action_method; |
| 330 |
|
| 331 |
retval = FALSE; |
| 332 |
error = NULL; |
| 333 |
|
| 334 |
/* We check if the user is authorized for any of the specificed action |
| 335 |
* methods. We only allow user interaction for the last one. Therefore, |
| 336 |
* callers of this function should choose with care the order, |
| 337 |
* especially if we don't want to prompt for a password too often and |
| 338 |
* if we don't want to authorize too many things at once. */ |
| 339 |
va_start (var_args, first_action_method); |
| 340 |
action_method = first_action_method; |
| 341 |
|
| 342 |
while (action_method) { |
| 343 |
char *next_action_method; |
| 344 |
|
| 345 |
if (error != NULL) { |
| 346 |
g_error_free (error); |
| 347 |
error = NULL; |
| 348 |
} |
| 349 |
|
| 350 |
next_action_method = va_arg (var_args, char *); |
| 351 |
|
| 352 |
retval = _check_polkit_for_action_internal (mechanism, context, |
| 353 |
action_method, |
| 354 |
next_action_method == NULL, |
| 355 |
&error); |
| 356 |
if (retval) |
| 357 |
break; |
| 358 |
|
| 359 |
action_method = next_action_method; |
| 360 |
} |
| 361 |
|
| 362 |
va_end (var_args); |
| 363 |
|
| 364 |
if (!retval) { |
| 365 |
if (!error) { |
| 366 |
/* This should never happen, but let's be paranoid */ |
| 367 |
error = g_error_new (CPH_MECHANISM_ERROR, |
| 368 |
CPH_MECHANISM_ERROR_GENERAL, |
| 369 |
"Unknown error when checking for " |
| 370 |
"authorization"); |
| 371 |
} |
| 372 |
|
| 373 |
g_dbus_method_invocation_return_gerror (context, error); |
| 374 |
g_error_free (error); |
| 375 |
} |
| 376 |
|
| 377 |
return retval; |
| 378 |
} |
| 379 |
|
| 380 |
static gboolean |
| 381 |
_check_polkit_for_action (CphMechanism *mechanism, |
| 382 |
GDBusMethodInvocation *context, |
| 383 |
const char *action_method) |
| 384 |
{ |
| 385 |
return _check_polkit_for_action_v (mechanism, context, |
| 386 |
action_method, NULL); |
| 387 |
} |
| 388 |
|
| 389 |
static gboolean |
| 390 |
_check_polkit_for_printer (CphMechanism *mechanism, |
| 391 |
GDBusMethodInvocation *context, |
| 392 |
const char *printer_name, |
| 393 |
const char *uri) |
| 394 |
{ |
| 395 |
gboolean is_local; |
| 396 |
|
| 397 |
is_local = cph_cups_is_printer_local (mechanism->priv->cups, |
| 398 |
printer_name) && |
| 399 |
(!uri || cph_cups_is_printer_uri_local (uri)); |
| 400 |
|
| 401 |
return _check_polkit_for_action_v (mechanism, context, |
| 402 |
"all-edit", |
| 403 |
"printeraddremove", |
| 404 |
is_local ? "printer-local-edit" |
| 405 |
: "printer-remote-edit", |
| 406 |
NULL); |
| 407 |
} |
| 408 |
|
| 409 |
static gboolean |
| 410 |
_check_polkit_for_printer_class (CphMechanism *mechanism, |
| 411 |
GDBusMethodInvocation *context, |
| 412 |
const char *printer_name) |
| 413 |
{ |
| 414 |
if (cph_cups_is_class (mechanism->priv->cups, printer_name)) { |
| 415 |
return _check_polkit_for_action_v (mechanism, context, |
| 416 |
"all-edit", |
| 417 |
"printeraddremove", |
| 418 |
"class-edit", NULL); |
| 419 |
} else { |
| 420 |
return _check_polkit_for_printer (mechanism, context, |
| 421 |
printer_name, NULL); |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
static const char * |
| 426 |
_cph_mechanism_get_action_for_name (CphMechanism *mechanism, |
| 427 |
const char *name) |
| 428 |
{ |
| 429 |
if (cph_cups_is_class (mechanism->priv->cups, name)) |
| 430 |
return "class-edit"; |
| 431 |
|
| 432 |
if (cph_cups_is_printer_local (mechanism->priv->cups, name)) |
| 433 |
return "printer-local-edit"; |
| 434 |
|
| 435 |
return "printer-remote-edit"; |
| 436 |
} |
| 437 |
|
| 438 |
static void |
| 439 |
_cph_mechanism_ensure_dbus_proxy (CphMechanism *mechanism, |
| 440 |
GDBusConnection *connection) |
| 441 |
{ |
| 442 |
GError *error = NULL; |
| 443 |
|
| 444 |
if (mechanism->priv->dbus_proxy != NULL) |
| 445 |
return; |
| 446 |
|
| 447 |
mechanism->priv->dbus_proxy = g_dbus_proxy_new_sync (connection, |
| 448 |
G_DBUS_PROXY_FLAGS_NONE, |
| 449 |
NULL, |
| 450 |
CPH_SERVICE_DBUS, |
| 451 |
CPH_PATH_DBUS, |
| 452 |
CPH_INTERFACE_DBUS, |
| 453 |
NULL, |
| 454 |
&error); |
| 455 |
|
| 456 |
if (mechanism->priv->dbus_proxy == NULL) { |
| 457 |
if (error) |
| 458 |
g_warning ("Could not get proxy to dbus service: %s", error->message); |
| 459 |
else |
| 460 |
g_warning ("Could not get proxy to dbus service user"); |
| 461 |
|
| 462 |
g_error_free (error); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
static gboolean |
| 467 |
_cph_mechanism_get_sender_uid (CphMechanism *mechanism, |
| 468 |
GDBusMethodInvocation *context, |
| 469 |
unsigned int *sender_uid) |
| 470 |
{ |
| 471 |
GError *error; |
| 472 |
GDBusConnection *connection; |
| 473 |
const char *sender; |
| 474 |
GVariant *result; |
| 475 |
|
| 476 |
*sender_uid = 0; |
| 477 |
|
| 478 |
connection = g_dbus_method_invocation_get_connection (context); |
| 479 |
_cph_mechanism_ensure_dbus_proxy (mechanism, connection); |
| 480 |
if (mechanism->priv->dbus_proxy == NULL) |
| 481 |
return FALSE; |
| 482 |
|
| 483 |
sender = g_dbus_method_invocation_get_sender (context); |
| 484 |
|
| 485 |
error = NULL; |
| 486 |
result = g_dbus_proxy_call_sync (mechanism->priv->dbus_proxy, |
| 487 |
"GetConnectionUnixUser", |
| 488 |
g_variant_new ("(s)", sender), |
| 489 |
G_DBUS_CALL_FLAGS_NONE, |
| 490 |
-1, |
| 491 |
NULL, |
| 492 |
&error); |
| 493 |
|
| 494 |
if (result == NULL) { |
| 495 |
if (error) |
| 496 |
g_warning ("Could not get unix user: %s", error->message); |
| 497 |
else |
| 498 |
g_warning ("Could not get unix user"); |
| 499 |
|
| 500 |
g_error_free (error); |
| 501 |
return FALSE; |
| 502 |
} |
| 503 |
|
| 504 |
g_variant_get (result, "(u)", sender_uid); |
| 505 |
g_variant_unref (result); |
| 506 |
|
| 507 |
return TRUE; |
| 508 |
} |
| 509 |
|
| 510 |
static char * |
| 511 |
_cph_mechanism_get_sender_user_name (CphMechanism *mechanism, |
| 512 |
GDBusMethodInvocation *context) |
| 513 |
{ |
| 514 |
unsigned int sender_uid; |
| 515 |
struct passwd *password_entry; |
| 516 |
char *user_name = NULL; |
| 517 |
|
| 518 |
if (!_cph_mechanism_get_sender_uid (mechanism, context, &sender_uid)) |
| 519 |
return NULL; |
| 520 |
|
| 521 |
password_entry = getpwuid ((uid_t) sender_uid); |
| 522 |
|
| 523 |
if (password_entry != NULL) |
| 524 |
user_name = g_strdup (password_entry->pw_name); |
| 525 |
|
| 526 |
return user_name; |
| 527 |
} |
| 528 |
|
| 529 |
/* helpers */ |
| 530 |
|
| 531 |
static const char * |
| 532 |
_cph_mechanism_return_error (CphMechanism *mechanism, |
| 533 |
gboolean failed) |
| 534 |
{ |
| 535 |
const char *error; |
| 536 |
|
| 537 |
if (failed) { |
| 538 |
error = cph_cups_last_status_to_string (mechanism->priv->cups); |
| 539 |
if (!error || error[0] == '\0') |
| 540 |
error = "Unknown error"; |
| 541 |
} else |
| 542 |
error = ""; |
| 543 |
|
| 544 |
return error; |
| 545 |
} |
| 546 |
|
| 547 |
static void |
| 548 |
_cph_mechanism_emit_called (CphMechanism *mechanism) |
| 549 |
{ |
| 550 |
g_signal_emit (mechanism, signals[CALLED], 0); |
| 551 |
} |
| 552 |
|
| 553 |
/* exported methods */ |
| 554 |
|
| 555 |
static gboolean |
| 556 |
cph_mechanism_file_get (CphIfaceMechanism *object, |
| 557 |
GDBusMethodInvocation *context, |
| 558 |
const char *resource, |
| 559 |
const char *filename) |
| 560 |
{ |
| 561 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 562 |
unsigned int sender_uid; |
| 563 |
gboolean ret; |
| 564 |
|
| 565 |
_cph_mechanism_emit_called (mechanism); |
| 566 |
|
| 567 |
if (!_cph_mechanism_get_sender_uid (mechanism, context, &sender_uid)) { |
| 568 |
GError *error; |
| 569 |
|
| 570 |
error = g_error_new (CPH_MECHANISM_ERROR, |
| 571 |
CPH_MECHANISM_ERROR_GENERAL, |
| 572 |
"Cannot determine sender UID"); |
| 573 |
g_dbus_method_invocation_return_gerror (context, error); |
| 574 |
g_error_free (error); |
| 575 |
|
| 576 |
return TRUE; |
| 577 |
} |
| 578 |
|
| 579 |
if (!_check_polkit_for_action (mechanism, context, "server-settings")) |
| 580 |
return TRUE; |
| 581 |
|
| 582 |
ret = cph_cups_file_get (mechanism->priv->cups, |
| 583 |
resource, filename, sender_uid); |
| 584 |
|
| 585 |
cph_iface_mechanism_complete_file_get ( |
| 586 |
object, context, |
| 587 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 588 |
return TRUE; |
| 589 |
} |
| 590 |
|
| 591 |
static gboolean |
| 592 |
cph_mechanism_file_put (CphIfaceMechanism *object, |
| 593 |
GDBusMethodInvocation *context, |
| 594 |
const char *resource, |
| 595 |
const char *filename) |
| 596 |
{ |
| 597 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 598 |
unsigned int sender_uid; |
| 599 |
gboolean ret; |
| 600 |
|
| 601 |
_cph_mechanism_emit_called (mechanism); |
| 602 |
|
| 603 |
if (!_cph_mechanism_get_sender_uid (mechanism, context, &sender_uid)) { |
| 604 |
GError *error; |
| 605 |
|
| 606 |
error = g_error_new (CPH_MECHANISM_ERROR, |
| 607 |
CPH_MECHANISM_ERROR_GENERAL, |
| 608 |
"Cannot determine sender UID"); |
| 609 |
g_dbus_method_invocation_return_gerror (context, error); |
| 610 |
g_error_free (error); |
| 611 |
|
| 612 |
return TRUE; |
| 613 |
} |
| 614 |
|
| 615 |
if (!_check_polkit_for_action (mechanism, context, "server-settings")) |
| 616 |
return TRUE; |
| 617 |
|
| 618 |
ret = cph_cups_file_put (mechanism->priv->cups, |
| 619 |
resource, filename, sender_uid); |
| 620 |
|
| 621 |
cph_iface_mechanism_complete_file_put ( |
| 622 |
object, context, |
| 623 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 624 |
return TRUE; |
| 625 |
} |
| 626 |
|
| 627 |
static gboolean |
| 628 |
cph_mechanism_server_get_settings (CphIfaceMechanism *object, |
| 629 |
GDBusMethodInvocation *context) |
| 630 |
{ |
| 631 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 632 |
gboolean ret; |
| 633 |
GVariant *settings = NULL; |
| 634 |
|
| 635 |
_cph_mechanism_emit_called (mechanism); |
| 636 |
|
| 637 |
if (!_check_polkit_for_action (mechanism, context, "server-settings")) |
| 638 |
return TRUE; |
| 639 |
|
| 640 |
ret = cph_cups_server_get_settings (mechanism->priv->cups, |
| 641 |
&settings); |
| 642 |
|
| 643 |
if (settings == NULL) |
| 644 |
settings = g_variant_new_array (G_VARIANT_TYPE_DICT_ENTRY, NULL, 0); |
| 645 |
|
| 646 |
cph_iface_mechanism_complete_server_get_settings ( |
| 647 |
object, context, |
| 648 |
_cph_mechanism_return_error (mechanism, !ret), |
| 649 |
settings); |
| 650 |
return TRUE; |
| 651 |
} |
| 652 |
|
| 653 |
static gboolean |
| 654 |
cph_mechanism_server_set_settings (CphIfaceMechanism *object, |
| 655 |
GDBusMethodInvocation *context, |
| 656 |
GVariant *settings) |
| 657 |
{ |
| 658 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 659 |
gboolean ret; |
| 660 |
|
| 661 |
_cph_mechanism_emit_called (mechanism); |
| 662 |
|
| 663 |
if (!_check_polkit_for_action (mechanism, context, "server-settings")) |
| 664 |
return TRUE; |
| 665 |
|
| 666 |
ret = cph_cups_server_set_settings (mechanism->priv->cups, settings); |
| 667 |
|
| 668 |
cph_iface_mechanism_complete_server_set_settings ( |
| 669 |
object, context, |
| 670 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 671 |
return TRUE; |
| 672 |
} |
| 673 |
|
| 674 |
static gboolean |
| 675 |
cph_mechanism_devices_get (CphIfaceMechanism *object, |
| 676 |
GDBusMethodInvocation *context, |
| 677 |
int timeout, |
| 678 |
int limit, |
| 679 |
const char *const *include_schemes, |
| 680 |
const char *const *exclude_schemes) |
| 681 |
{ |
| 682 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 683 |
gboolean ret; |
| 684 |
GVariant *devices = NULL; |
| 685 |
|
| 686 |
_cph_mechanism_emit_called (mechanism); |
| 687 |
|
| 688 |
if (!_check_polkit_for_action_v (mechanism, context, |
| 689 |
"all-edit", |
| 690 |
"devices-get", |
| 691 |
NULL)) |
| 692 |
return TRUE; |
| 693 |
|
| 694 |
ret = cph_cups_devices_get (mechanism->priv->cups, |
| 695 |
timeout, |
| 696 |
limit, |
| 697 |
include_schemes, |
| 698 |
exclude_schemes, |
| 699 |
&devices); |
| 700 |
|
| 701 |
if (devices == NULL) |
| 702 |
devices = g_variant_new_array (G_VARIANT_TYPE_DICT_ENTRY, NULL, 0); |
| 703 |
|
| 704 |
cph_iface_mechanism_complete_devices_get ( |
| 705 |
object, context, |
| 706 |
_cph_mechanism_return_error (mechanism, !ret), |
| 707 |
devices); |
| 708 |
return TRUE; |
| 709 |
} |
| 710 |
|
| 711 |
static gboolean |
| 712 |
cph_mechanism_printer_add (CphIfaceMechanism *object, |
| 713 |
GDBusMethodInvocation *context, |
| 714 |
const char *name, |
| 715 |
const char *uri, |
| 716 |
const char *ppd, |
| 717 |
const char *info, |
| 718 |
const char *location) |
| 719 |
{ |
| 720 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 721 |
gboolean ret; |
| 722 |
|
| 723 |
_cph_mechanism_emit_called (mechanism); |
| 724 |
|
| 725 |
if (!_check_polkit_for_printer (mechanism, context, name, uri)) |
| 726 |
return TRUE; |
| 727 |
|
| 728 |
ret = cph_cups_printer_add (mechanism->priv->cups, |
| 729 |
name, uri, ppd, info, location); |
| 730 |
|
| 731 |
cph_iface_mechanism_complete_printer_add ( |
| 732 |
object, context, |
| 733 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 734 |
return TRUE; |
| 735 |
} |
| 736 |
|
| 737 |
static gboolean |
| 738 |
cph_mechanism_printer_add_with_ppd_file (CphIfaceMechanism *object, |
| 739 |
GDBusMethodInvocation *context, |
| 740 |
const char *name, |
| 741 |
const char *uri, |
| 742 |
const char *ppdfile, |
| 743 |
const char *info, |
| 744 |
const char *location) |
| 745 |
{ |
| 746 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 747 |
gboolean ret; |
| 748 |
|
| 749 |
_cph_mechanism_emit_called (mechanism); |
| 750 |
|
| 751 |
if (!_check_polkit_for_printer (mechanism, context, name, uri)) |
| 752 |
return TRUE; |
| 753 |
|
| 754 |
ret = cph_cups_printer_add_with_ppd_file (mechanism->priv->cups, |
| 755 |
name, uri, ppdfile, |
| 756 |
info, location); |
| 757 |
|
| 758 |
cph_iface_mechanism_complete_printer_add_with_ppd_file ( |
| 759 |
object, context, |
| 760 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 761 |
return TRUE; |
| 762 |
} |
| 763 |
|
| 764 |
static gboolean |
| 765 |
cph_mechanism_printer_set_device (CphIfaceMechanism *object, |
| 766 |
GDBusMethodInvocation *context, |
| 767 |
const char *name, |
| 768 |
const char *device) |
| 769 |
{ |
| 770 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 771 |
gboolean ret; |
| 772 |
|
| 773 |
_cph_mechanism_emit_called (mechanism); |
| 774 |
|
| 775 |
if (!_check_polkit_for_printer (mechanism, context, name, device)) |
| 776 |
return TRUE; |
| 777 |
|
| 778 |
ret = cph_cups_printer_set_uri (mechanism->priv->cups, |
| 779 |
name, device); |
| 780 |
|
| 781 |
cph_iface_mechanism_complete_printer_set_device ( |
| 782 |
object, context, |
| 783 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 784 |
return TRUE; |
| 785 |
} |
| 786 |
|
| 787 |
static gboolean |
| 788 |
cph_mechanism_printer_set_default (CphIfaceMechanism *object, |
| 789 |
GDBusMethodInvocation *context, |
| 790 |
const char *name) |
| 791 |
{ |
| 792 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 793 |
gboolean ret; |
| 794 |
const char *last_action; |
| 795 |
|
| 796 |
_cph_mechanism_emit_called (mechanism); |
| 797 |
|
| 798 |
last_action = _cph_mechanism_get_action_for_name (mechanism, name); |
| 799 |
if (!_check_polkit_for_action_v (mechanism, context, |
| 800 |
"all-edit", |
| 801 |
"printeraddremove", |
| 802 |
/* this is not the last check because |
| 803 |
* it's likely most useful to the user |
| 804 |
* to give "printer-X-edit" powers */ |
| 805 |
"printer-default", |
| 806 |
/* quite important, since it's |
| 807 |
* automatically called after adding a |
| 808 |
* printer */ |
| 809 |
last_action, |
| 810 |
NULL)) |
| 811 |
return TRUE; |
| 812 |
|
| 813 |
ret = cph_cups_printer_set_default (mechanism->priv->cups, name); |
| 814 |
|
| 815 |
cph_iface_mechanism_complete_printer_set_default ( |
| 816 |
object, context, |
| 817 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 818 |
return TRUE; |
| 819 |
} |
| 820 |
|
| 821 |
static gboolean |
| 822 |
cph_mechanism_printer_set_enabled (CphIfaceMechanism *object, |
| 823 |
GDBusMethodInvocation *context, |
| 824 |
const char *name, |
| 825 |
gboolean enabled) |
| 826 |
{ |
| 827 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 828 |
gboolean ret; |
| 829 |
const char *last_action; |
| 830 |
|
| 831 |
_cph_mechanism_emit_called (mechanism); |
| 832 |
|
| 833 |
last_action = _cph_mechanism_get_action_for_name (mechanism, name); |
| 834 |
if (!_check_polkit_for_action_v (mechanism, context, |
| 835 |
"all-edit", |
| 836 |
"printeraddremove", |
| 837 |
/* this is not the last check because |
| 838 |
* it's likely most useful to the user |
| 839 |
* to give "printer-X-edit" powers */ |
| 840 |
"printer-enable", |
| 841 |
/* quite important, since it's |
| 842 |
* automatically called after adding a |
| 843 |
* printer */ |
| 844 |
last_action, |
| 845 |
NULL)) |
| 846 |
return TRUE; |
| 847 |
|
| 848 |
ret = cph_cups_printer_set_enabled (mechanism->priv->cups, |
| 849 |
name, enabled); |
| 850 |
|
| 851 |
cph_iface_mechanism_complete_printer_set_enabled ( |
| 852 |
object, context, |
| 853 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 854 |
return TRUE; |
| 855 |
} |
| 856 |
|
| 857 |
static gboolean |
| 858 |
cph_mechanism_printer_set_accept_jobs (CphIfaceMechanism *object, |
| 859 |
GDBusMethodInvocation *context, |
| 860 |
const char *name, |
| 861 |
gboolean enabled, |
| 862 |
const char *reason) |
| 863 |
{ |
| 864 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 865 |
gboolean ret; |
| 866 |
|
| 867 |
_cph_mechanism_emit_called (mechanism); |
| 868 |
|
| 869 |
if (!_check_polkit_for_printer (mechanism, context, name, NULL)) |
| 870 |
return TRUE; |
| 871 |
|
| 872 |
if (reason && reason[0] == '\0') |
| 873 |
reason = NULL; |
| 874 |
|
| 875 |
ret = cph_cups_printer_set_accept_jobs (mechanism->priv->cups, |
| 876 |
name, enabled, reason); |
| 877 |
|
| 878 |
cph_iface_mechanism_complete_printer_set_accept_jobs ( |
| 879 |
object, context, |
| 880 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 881 |
return TRUE; |
| 882 |
} |
| 883 |
|
| 884 |
static gboolean |
| 885 |
cph_mechanism_printer_delete (CphIfaceMechanism *object, |
| 886 |
GDBusMethodInvocation *context, |
| 887 |
const char *name) |
| 888 |
{ |
| 889 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 890 |
gboolean ret; |
| 891 |
|
| 892 |
_cph_mechanism_emit_called (mechanism); |
| 893 |
|
| 894 |
if (!_check_polkit_for_printer (mechanism, context, name, NULL)) |
| 895 |
return TRUE; |
| 896 |
|
| 897 |
ret = cph_cups_printer_delete (mechanism->priv->cups, name); |
| 898 |
|
| 899 |
cph_iface_mechanism_complete_printer_delete ( |
| 900 |
object, context, |
| 901 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 902 |
return TRUE; |
| 903 |
} |
| 904 |
|
| 905 |
static gboolean |
| 906 |
cph_mechanism_printer_class_rename (CphIfaceMechanism *object, |
| 907 |
GDBusMethodInvocation *context, |
| 908 |
const char *old_printer_name, |
| 909 |
const char *new_printer_name) |
| 910 |
{ |
| 911 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 912 |
gboolean ret; |
| 913 |
|
| 914 |
_cph_mechanism_emit_called (mechanism); |
| 915 |
|
| 916 |
if (!_check_polkit_for_printer_class (mechanism, context, old_printer_name)) |
| 917 |
return TRUE; |
| 918 |
|
| 919 |
ret = cph_cups_printer_class_rename (mechanism->priv->cups, old_printer_name, new_printer_name); |
| 920 |
|
| 921 |
cph_iface_mechanism_complete_printer_rename ( |
| 922 |
object, context, |
| 923 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 924 |
return TRUE; |
| 925 |
} |
| 926 |
|
| 927 |
static gboolean |
| 928 |
cph_mechanism_class_add_printer (CphIfaceMechanism *object, |
| 929 |
GDBusMethodInvocation *context, |
| 930 |
const char *name, |
| 931 |
const char *printer) |
| 932 |
{ |
| 933 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 934 |
gboolean ret; |
| 935 |
|
| 936 |
_cph_mechanism_emit_called (mechanism); |
| 937 |
|
| 938 |
if (!_check_polkit_for_action_v (mechanism, context, |
| 939 |
"all-edit", |
| 940 |
"printeraddremove", |
| 941 |
"class-edit", |
| 942 |
NULL)) |
| 943 |
return TRUE; |
| 944 |
|
| 945 |
ret = cph_cups_class_add_printer (mechanism->priv->cups, |
| 946 |
name, printer); |
| 947 |
|
| 948 |
cph_iface_mechanism_complete_class_add_printer ( |
| 949 |
object, context, |
| 950 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 951 |
return TRUE; |
| 952 |
} |
| 953 |
|
| 954 |
static gboolean |
| 955 |
cph_mechanism_class_delete_printer (CphIfaceMechanism *object, |
| 956 |
GDBusMethodInvocation *context, |
| 957 |
const char *name, |
| 958 |
const char *printer) |
| 959 |
{ |
| 960 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 961 |
gboolean ret; |
| 962 |
|
| 963 |
_cph_mechanism_emit_called (mechanism); |
| 964 |
|
| 965 |
if (!_check_polkit_for_action_v (mechanism, context, |
| 966 |
"all-edit", |
| 967 |
"printeraddremove", |
| 968 |
"class-edit", |
| 969 |
NULL)) |
| 970 |
return TRUE; |
| 971 |
|
| 972 |
ret = cph_cups_class_delete_printer (mechanism->priv->cups, |
| 973 |
name, printer); |
| 974 |
|
| 975 |
cph_iface_mechanism_complete_class_delete_printer ( |
| 976 |
object, context, |
| 977 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 978 |
return TRUE; |
| 979 |
} |
| 980 |
|
| 981 |
static gboolean |
| 982 |
cph_mechanism_class_delete (CphIfaceMechanism *object, |
| 983 |
GDBusMethodInvocation *context, |
| 984 |
const char *name) |
| 985 |
{ |
| 986 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 987 |
gboolean ret; |
| 988 |
|
| 989 |
_cph_mechanism_emit_called (mechanism); |
| 990 |
|
| 991 |
if (!_check_polkit_for_action_v (mechanism, context, |
| 992 |
"all-edit", |
| 993 |
"printeraddremove", |
| 994 |
"class-edit", |
| 995 |
NULL)) |
| 996 |
return TRUE; |
| 997 |
|
| 998 |
ret = cph_cups_class_delete (mechanism->priv->cups, name); |
| 999 |
|
| 1000 |
cph_iface_mechanism_complete_class_delete ( |
| 1001 |
object, context, |
| 1002 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 1003 |
return TRUE; |
| 1004 |
} |
| 1005 |
|
| 1006 |
static gboolean |
| 1007 |
cph_mechanism_printer_set_info (CphIfaceMechanism *object, |
| 1008 |
GDBusMethodInvocation *context, |
| 1009 |
const char *name, |
| 1010 |
const char *info) |
| 1011 |
{ |
| 1012 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 1013 |
gboolean ret; |
| 1014 |
|
| 1015 |
_cph_mechanism_emit_called (mechanism); |
| 1016 |
|
| 1017 |
if (!_check_polkit_for_printer_class (mechanism, context, name)) |
| 1018 |
return TRUE; |
| 1019 |
|
| 1020 |
ret = cph_cups_printer_class_set_info (mechanism->priv->cups, |
| 1021 |
name, info); |
| 1022 |
|
| 1023 |
cph_iface_mechanism_complete_printer_set_info ( |
| 1024 |
object, context, |
| 1025 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 1026 |
return TRUE; |
| 1027 |
} |
| 1028 |
|
| 1029 |
static gboolean |
| 1030 |
cph_mechanism_printer_set_location (CphIfaceMechanism *object, |
| 1031 |
GDBusMethodInvocation *context, |
| 1032 |
const char *name, |
| 1033 |
const char *location) |
| 1034 |
{ |
| 1035 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 1036 |
gboolean ret; |
| 1037 |
|
| 1038 |
_cph_mechanism_emit_called (mechanism); |
| 1039 |
|
| 1040 |
if (!_check_polkit_for_printer_class (mechanism, context, name)) |
| 1041 |
return TRUE; |
| 1042 |
|
| 1043 |
ret = cph_cups_printer_class_set_location (mechanism->priv->cups, |
| 1044 |
name, location); |
| 1045 |
|
| 1046 |
cph_iface_mechanism_complete_printer_set_location ( |
| 1047 |
object, context, |
| 1048 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 1049 |
return TRUE; |
| 1050 |
} |
| 1051 |
|
| 1052 |
static gboolean |
| 1053 |
cph_mechanism_printer_set_shared (CphIfaceMechanism *object, |
| 1054 |
GDBusMethodInvocation *context, |
| 1055 |
const char *name, |
| 1056 |
gboolean shared) |
| 1057 |
{ |
| 1058 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 1059 |
gboolean ret; |
| 1060 |
|
| 1061 |
_cph_mechanism_emit_called (mechanism); |
| 1062 |
|
| 1063 |
if (!_check_polkit_for_printer_class (mechanism, context, name)) |
| 1064 |
return TRUE; |
| 1065 |
|
| 1066 |
ret = cph_cups_printer_class_set_shared (mechanism->priv->cups, |
| 1067 |
name, shared); |
| 1068 |
|
| 1069 |
cph_iface_mechanism_complete_printer_set_shared ( |
| 1070 |
object, context, |
| 1071 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 1072 |
return TRUE; |
| 1073 |
} |
| 1074 |
|
| 1075 |
static gboolean |
| 1076 |
cph_mechanism_printer_set_job_sheets (CphIfaceMechanism *object, |
| 1077 |
GDBusMethodInvocation *context, |
| 1078 |
const char *name, |
| 1079 |
const char *start, |
| 1080 |
const char *end) |
| 1081 |
{ |
| 1082 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 1083 |
gboolean ret; |
| 1084 |
|
| 1085 |
_cph_mechanism_emit_called (mechanism); |
| 1086 |
|
| 1087 |
if (!_check_polkit_for_printer_class (mechanism, context, name)) |
| 1088 |
return TRUE; |
| 1089 |
|
| 1090 |
ret = cph_cups_printer_class_set_job_sheets (mechanism->priv->cups, |
| 1091 |
name, start, end); |
| 1092 |
|
| 1093 |
cph_iface_mechanism_complete_printer_set_job_sheets ( |
| 1094 |
object, context, |
| 1095 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 1096 |
return TRUE; |
| 1097 |
} |
| 1098 |
|
| 1099 |
static gboolean |
| 1100 |
cph_mechanism_printer_set_error_policy (CphIfaceMechanism *object, |
| 1101 |
GDBusMethodInvocation *context, |
| 1102 |
const char *name, |
| 1103 |
const char *policy) |
| 1104 |
{ |
| 1105 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 1106 |
gboolean ret; |
| 1107 |
|
| 1108 |
_cph_mechanism_emit_called (mechanism); |
| 1109 |
|
| 1110 |
if (!_check_polkit_for_printer_class (mechanism, context, name)) |
| 1111 |
return TRUE; |
| 1112 |
|
| 1113 |
ret = cph_cups_printer_class_set_error_policy (mechanism->priv->cups, |
| 1114 |
name, policy); |
| 1115 |
|
| 1116 |
cph_iface_mechanism_complete_printer_set_error_policy ( |
| 1117 |
object, context, |
| 1118 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 1119 |
return TRUE; |
| 1120 |
} |
| 1121 |
|
| 1122 |
static gboolean |
| 1123 |
cph_mechanism_printer_set_op_policy (CphIfaceMechanism *object, |
| 1124 |
GDBusMethodInvocation *context, |
| 1125 |
const char *name, |
| 1126 |
const char *policy) |
| 1127 |
{ |
| 1128 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 1129 |
gboolean ret; |
| 1130 |
|
| 1131 |
_cph_mechanism_emit_called (mechanism); |
| 1132 |
|
| 1133 |
if (!_check_polkit_for_printer_class (mechanism, context, name)) |
| 1134 |
return TRUE; |
| 1135 |
|
| 1136 |
ret = cph_cups_printer_class_set_op_policy (mechanism->priv->cups, |
| 1137 |
name, policy); |
| 1138 |
|
| 1139 |
cph_iface_mechanism_complete_printer_set_op_policy ( |
| 1140 |
object, context, |
| 1141 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 1142 |
return TRUE; |
| 1143 |
} |
| 1144 |
|
| 1145 |
static gboolean |
| 1146 |
cph_mechanism_printer_set_users_allowed (CphIfaceMechanism *object, |
| 1147 |
GDBusMethodInvocation *context, |
| 1148 |
const char *name, |
| 1149 |
const char *const *users) |
| 1150 |
{ |
| 1151 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 1152 |
gboolean ret; |
| 1153 |
|
| 1154 |
_cph_mechanism_emit_called (mechanism); |
| 1155 |
|
| 1156 |
if (!_check_polkit_for_printer_class (mechanism, context, name)) |
| 1157 |
return TRUE; |
| 1158 |
|
| 1159 |
ret = cph_cups_printer_class_set_users_allowed (mechanism->priv->cups, |
| 1160 |
name, users); |
| 1161 |
|
| 1162 |
cph_iface_mechanism_complete_printer_set_users_allowed ( |
| 1163 |
object, context, |
| 1164 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 1165 |
return TRUE; |
| 1166 |
} |
| 1167 |
|
| 1168 |
static gboolean |
| 1169 |
cph_mechanism_printer_set_users_denied (CphIfaceMechanism *object, |
| 1170 |
GDBusMethodInvocation *context, |
| 1171 |
const char *name, |
| 1172 |
const char *const *users) |
| 1173 |
{ |
| 1174 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 1175 |
gboolean ret; |
| 1176 |
|
| 1177 |
_cph_mechanism_emit_called (mechanism); |
| 1178 |
|
| 1179 |
if (!_check_polkit_for_printer_class (mechanism, context, name)) |
| 1180 |
return TRUE; |
| 1181 |
|
| 1182 |
ret = cph_cups_printer_class_set_users_denied (mechanism->priv->cups, |
| 1183 |
name, users); |
| 1184 |
|
| 1185 |
cph_iface_mechanism_complete_printer_set_users_denied ( |
| 1186 |
object, context, |
| 1187 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 1188 |
return TRUE; |
| 1189 |
} |
| 1190 |
|
| 1191 |
static gboolean |
| 1192 |
cph_mechanism_printer_add_option_default (CphIfaceMechanism *object, |
| 1193 |
GDBusMethodInvocation *context, |
| 1194 |
const char *name, |
| 1195 |
const char *option, |
| 1196 |
const char *const *values) |
| 1197 |
{ |
| 1198 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 1199 |
gboolean ret; |
| 1200 |
|
| 1201 |
_cph_mechanism_emit_called (mechanism); |
| 1202 |
|
| 1203 |
if (!_check_polkit_for_printer_class (mechanism, context, name)) |
| 1204 |
return TRUE; |
| 1205 |
|
| 1206 |
ret = cph_cups_printer_class_set_option_default (mechanism->priv->cups, |
| 1207 |
name, option, values); |
| 1208 |
|
| 1209 |
cph_iface_mechanism_complete_printer_add_option_default ( |
| 1210 |
object, context, |
| 1211 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 1212 |
return TRUE; |
| 1213 |
} |
| 1214 |
|
| 1215 |
static gboolean |
| 1216 |
cph_mechanism_printer_delete_option_default (CphIfaceMechanism *object, |
| 1217 |
GDBusMethodInvocation *context, |
| 1218 |
const char *name, |
| 1219 |
const char *option) |
| 1220 |
{ |
| 1221 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 1222 |
gboolean ret; |
| 1223 |
|
| 1224 |
_cph_mechanism_emit_called (mechanism); |
| 1225 |
|
| 1226 |
if (!_check_polkit_for_printer_class (mechanism, context, name)) |
| 1227 |
return TRUE; |
| 1228 |
|
| 1229 |
ret = cph_cups_printer_class_set_option_default (mechanism->priv->cups, |
| 1230 |
name, option, NULL); |
| 1231 |
|
| 1232 |
cph_iface_mechanism_complete_printer_delete_option_default ( |
| 1233 |
object, context, |
| 1234 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 1235 |
return TRUE; |
| 1236 |
} |
| 1237 |
|
| 1238 |
static gboolean |
| 1239 |
cph_mechanism_printer_add_option (CphIfaceMechanism *object, |
| 1240 |
GDBusMethodInvocation *context, |
| 1241 |
const char *name, |
| 1242 |
const char *option, |
| 1243 |
const char *const *values) |
| 1244 |
{ |
| 1245 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 1246 |
gboolean ret; |
| 1247 |
|
| 1248 |
_cph_mechanism_emit_called (mechanism); |
| 1249 |
|
| 1250 |
if (!_check_polkit_for_printer_class (mechanism, context, name)) |
| 1251 |
return TRUE; |
| 1252 |
|
| 1253 |
ret = cph_cups_printer_class_set_option (mechanism->priv->cups, |
| 1254 |
name, option, values); |
| 1255 |
|
| 1256 |
cph_iface_mechanism_complete_printer_add_option ( |
| 1257 |
object, context, |
| 1258 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 1259 |
return TRUE; |
| 1260 |
} |
| 1261 |
|
| 1262 |
static gboolean |
| 1263 |
cph_mechanism_job_cancel_purge (CphIfaceMechanism *object, |
| 1264 |
GDBusMethodInvocation *context, |
| 1265 |
int id, |
| 1266 |
gboolean purge) |
| 1267 |
{ |
| 1268 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 1269 |
CphJobStatus job_status; |
| 1270 |
gboolean ret; |
| 1271 |
char *user_name; |
| 1272 |
|
| 1273 |
_cph_mechanism_emit_called (mechanism); |
| 1274 |
|
| 1275 |
user_name = _cph_mechanism_get_sender_user_name (mechanism, context); |
| 1276 |
job_status = cph_cups_job_get_status (mechanism->priv->cups, |
| 1277 |
id, user_name); |
| 1278 |
|
| 1279 |
switch (job_status) { |
| 1280 |
case CPH_JOB_STATUS_OWNED_BY_USER: { |
| 1281 |
if (!_check_polkit_for_action_v (mechanism, context, |
| 1282 |
"all-edit", |
| 1283 |
"job-not-owned-edit", |
| 1284 |
"job-edit", |
| 1285 |
NULL)) |
| 1286 |
goto out; |
| 1287 |
break; |
| 1288 |
} |
| 1289 |
case CPH_JOB_STATUS_NOT_OWNED_BY_USER: { |
| 1290 |
if (!_check_polkit_for_action_v (mechanism, context, |
| 1291 |
"all-edit", |
| 1292 |
"job-not-owned-edit", |
| 1293 |
NULL)) |
| 1294 |
goto out; |
| 1295 |
break; |
| 1296 |
} |
| 1297 |
case CPH_JOB_STATUS_INVALID: { |
| 1298 |
cph_iface_mechanism_complete_job_cancel_purge ( |
| 1299 |
object, context, |
| 1300 |
_cph_mechanism_return_error (mechanism, TRUE)); |
| 1301 |
goto out; |
| 1302 |
} |
| 1303 |
} |
| 1304 |
|
| 1305 |
ret = cph_cups_job_cancel (mechanism->priv->cups, id, purge, user_name); |
| 1306 |
|
| 1307 |
cph_iface_mechanism_complete_job_cancel_purge ( |
| 1308 |
object, context, |
| 1309 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 1310 |
|
| 1311 |
out: |
| 1312 |
g_free (user_name); |
| 1313 |
|
| 1314 |
return TRUE; |
| 1315 |
} |
| 1316 |
|
| 1317 |
static gboolean |
| 1318 |
cph_mechanism_job_cancel (CphIfaceMechanism *object, |
| 1319 |
GDBusMethodInvocation *context, |
| 1320 |
int id) |
| 1321 |
{ |
| 1322 |
/* This only works because cph_iface_mechanism_complete_job_cancel and |
| 1323 |
* cph_iface_mechanism_complete_job_cancel_purge do the same thing. */ |
| 1324 |
return cph_mechanism_job_cancel_purge (object, context, id, FALSE); |
| 1325 |
} |
| 1326 |
|
| 1327 |
static gboolean |
| 1328 |
cph_mechanism_job_restart (CphIfaceMechanism *object, |
| 1329 |
GDBusMethodInvocation *context, |
| 1330 |
int id) |
| 1331 |
{ |
| 1332 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 1333 |
CphJobStatus job_status; |
| 1334 |
gboolean ret; |
| 1335 |
char *user_name; |
| 1336 |
|
| 1337 |
_cph_mechanism_emit_called (mechanism); |
| 1338 |
|
| 1339 |
user_name = _cph_mechanism_get_sender_user_name (mechanism, context); |
| 1340 |
job_status = cph_cups_job_get_status (mechanism->priv->cups, |
| 1341 |
id, user_name); |
| 1342 |
|
| 1343 |
switch (job_status) { |
| 1344 |
case CPH_JOB_STATUS_OWNED_BY_USER: { |
| 1345 |
if (!_check_polkit_for_action_v (mechanism, context, |
| 1346 |
"all-edit", |
| 1347 |
"job-not-owned-edit", |
| 1348 |
"job-edit", |
| 1349 |
NULL)) |
| 1350 |
goto out; |
| 1351 |
break; |
| 1352 |
} |
| 1353 |
case CPH_JOB_STATUS_NOT_OWNED_BY_USER: { |
| 1354 |
if (!_check_polkit_for_action_v (mechanism, context, |
| 1355 |
"all-edit", |
| 1356 |
"job-not-owned-edit", |
| 1357 |
NULL)) |
| 1358 |
goto out; |
| 1359 |
break; |
| 1360 |
} |
| 1361 |
case CPH_JOB_STATUS_INVALID: { |
| 1362 |
cph_iface_mechanism_complete_job_restart ( |
| 1363 |
object, context, |
| 1364 |
_cph_mechanism_return_error (mechanism, TRUE)); |
| 1365 |
goto out; |
| 1366 |
} |
| 1367 |
} |
| 1368 |
|
| 1369 |
ret = cph_cups_job_restart (mechanism->priv->cups, id, user_name); |
| 1370 |
|
| 1371 |
cph_iface_mechanism_complete_job_restart ( |
| 1372 |
object, context, |
| 1373 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 1374 |
|
| 1375 |
out: |
| 1376 |
g_free (user_name); |
| 1377 |
|
| 1378 |
return TRUE; |
| 1379 |
} |
| 1380 |
|
| 1381 |
static gboolean |
| 1382 |
cph_mechanism_job_set_hold_until (CphIfaceMechanism *object, |
| 1383 |
GDBusMethodInvocation *context, |
| 1384 |
int id, |
| 1385 |
const char *job_hold_until) |
| 1386 |
{ |
| 1387 |
CphMechanism *mechanism = CPH_MECHANISM (object); |
| 1388 |
CphJobStatus job_status; |
| 1389 |
gboolean ret; |
| 1390 |
char *user_name; |
| 1391 |
|
| 1392 |
_cph_mechanism_emit_called (mechanism); |
| 1393 |
|
| 1394 |
user_name = _cph_mechanism_get_sender_user_name (mechanism, context); |
| 1395 |
job_status = cph_cups_job_get_status (mechanism->priv->cups, |
| 1396 |
id, user_name); |
| 1397 |
|
| 1398 |
switch (job_status) { |
| 1399 |
case CPH_JOB_STATUS_OWNED_BY_USER: { |
| 1400 |
if (!_check_polkit_for_action_v (mechanism, context, |
| 1401 |
"all-edit", |
| 1402 |
"job-not-owned-edit", |
| 1403 |
"job-edit", |
| 1404 |
NULL)) |
| 1405 |
goto out; |
| 1406 |
break; |
| 1407 |
} |
| 1408 |
case CPH_JOB_STATUS_NOT_OWNED_BY_USER: { |
| 1409 |
if (!_check_polkit_for_action_v (mechanism, context, |
| 1410 |
"all-edit", |
| 1411 |
"job-not-owned-edit", |
| 1412 |
NULL)) |
| 1413 |
goto out; |
| 1414 |
break; |
| 1415 |
} |
| 1416 |
case CPH_JOB_STATUS_INVALID: { |
| 1417 |
cph_iface_mechanism_complete_job_set_hold_until ( |
| 1418 |
object, context, |
| 1419 |
_cph_mechanism_return_error (mechanism, TRUE)); |
| 1420 |
goto out; |
| 1421 |
} |
| 1422 |
} |
| 1423 |
|
| 1424 |
ret = cph_cups_job_set_hold_until (mechanism->priv->cups, id, job_hold_until, user_name); |
| 1425 |
|
| 1426 |
cph_iface_mechanism_complete_job_set_hold_until ( |
| 1427 |
object, context, |
| 1428 |
_cph_mechanism_return_error (mechanism, !ret)); |
| 1429 |
|
| 1430 |
out: |
| 1431 |
g_free (user_name); |
| 1432 |
|
| 1433 |
return TRUE; |
| 1434 |
} |
| 1435 |
|
| 1436 |
/* connect methors */ |
| 1437 |
|
| 1438 |
static void |
| 1439 |
cph_mechanism_connect_signals (CphMechanism *mechanism) |
| 1440 |
{ |
| 1441 |
g_return_if_fail (CPH_IS_MECHANISM (mechanism)); |
| 1442 |
|
| 1443 |
if (mechanism->priv->connected) |
| 1444 |
return; |
| 1445 |
|
| 1446 |
mechanism->priv->connected = TRUE; |
| 1447 |
|
| 1448 |
g_signal_connect (mechanism, |
| 1449 |
"handle-class-add-printer", |
| 1450 |
G_CALLBACK (cph_mechanism_class_add_printer), |
| 1451 |
NULL); |
| 1452 |
g_signal_connect (mechanism, |
| 1453 |
"handle-class-delete", |
| 1454 |
G_CALLBACK (cph_mechanism_class_delete), |
| 1455 |
NULL); |
| 1456 |
g_signal_connect (mechanism, |
| 1457 |
"handle-class-delete-printer", |
| 1458 |
G_CALLBACK (cph_mechanism_class_delete_printer), |
| 1459 |
NULL); |
| 1460 |
g_signal_connect (mechanism, |
| 1461 |
"handle-devices-get", |
| 1462 |
G_CALLBACK (cph_mechanism_devices_get), |
| 1463 |
NULL); |
| 1464 |
g_signal_connect (mechanism, |
| 1465 |
"handle-file-get", |
| 1466 |
G_CALLBACK (cph_mechanism_file_get), |
| 1467 |
NULL); |
| 1468 |
g_signal_connect (mechanism, |
| 1469 |
"handle-file-put", |
| 1470 |
G_CALLBACK (cph_mechanism_file_put), |
| 1471 |
NULL); |
| 1472 |
g_signal_connect (mechanism, |
| 1473 |
"handle-job-cancel", |
| 1474 |
G_CALLBACK (cph_mechanism_job_cancel), |
| 1475 |
NULL); |
| 1476 |
g_signal_connect (mechanism, |
| 1477 |
"handle-job-cancel-purge", |
| 1478 |
G_CALLBACK (cph_mechanism_job_cancel_purge), |
| 1479 |
NULL); |
| 1480 |
g_signal_connect (mechanism, |
| 1481 |
"handle-job-restart", |
| 1482 |
G_CALLBACK (cph_mechanism_job_restart), |
| 1483 |
NULL); |
| 1484 |
g_signal_connect (mechanism, |
| 1485 |
"handle-job-set-hold-until", |
| 1486 |
G_CALLBACK (cph_mechanism_job_set_hold_until), |
| 1487 |
NULL); |
| 1488 |
g_signal_connect (mechanism, |
| 1489 |
"handle-printer-add", |
| 1490 |
G_CALLBACK (cph_mechanism_printer_add), |
| 1491 |
NULL); |
| 1492 |
g_signal_connect (mechanism, |
| 1493 |
"handle-printer-add-option", |
| 1494 |
G_CALLBACK (cph_mechanism_printer_add_option), |
| 1495 |
NULL); |
| 1496 |
g_signal_connect (mechanism, |
| 1497 |
"handle-printer-add-option-default", |
| 1498 |
G_CALLBACK (cph_mechanism_printer_add_option_default), |
| 1499 |
NULL); |
| 1500 |
g_signal_connect (mechanism, |
| 1501 |
"handle-printer-add-with-ppd-file", |
| 1502 |
G_CALLBACK (cph_mechanism_printer_add_with_ppd_file), |
| 1503 |
NULL); |
| 1504 |
g_signal_connect (mechanism, |
| 1505 |
"handle-printer-delete", |
| 1506 |
G_CALLBACK (cph_mechanism_printer_delete), |
| 1507 |
NULL); |
| 1508 |
g_signal_connect (mechanism, |
| 1509 |
"handle-printer-delete-option-default", |
| 1510 |
G_CALLBACK (cph_mechanism_printer_delete_option_default), |
| 1511 |
NULL); |
| 1512 |
g_signal_connect (mechanism, |
| 1513 |
"handle-printer-rename", |
| 1514 |
G_CALLBACK (cph_mechanism_printer_class_rename), |
| 1515 |
NULL); |
| 1516 |
g_signal_connect (mechanism, |
| 1517 |
"handle-printer-set-accept-jobs", |
| 1518 |
G_CALLBACK (cph_mechanism_printer_set_accept_jobs), |
| 1519 |
NULL); |
| 1520 |
g_signal_connect (mechanism, |
| 1521 |
"handle-printer-set-default", |
| 1522 |
G_CALLBACK (cph_mechanism_printer_set_default), |
| 1523 |
NULL); |
| 1524 |
g_signal_connect (mechanism, |
| 1525 |
"handle-printer-set-device", |
| 1526 |
G_CALLBACK (cph_mechanism_printer_set_device), |
| 1527 |
NULL); |
| 1528 |
g_signal_connect (mechanism, |
| 1529 |
"handle-printer-set-enabled", |
| 1530 |
G_CALLBACK (cph_mechanism_printer_set_enabled), |
| 1531 |
NULL); |
| 1532 |
g_signal_connect (mechanism, |
| 1533 |
"handle-printer-set-error-policy", |
| 1534 |
G_CALLBACK (cph_mechanism_printer_set_error_policy), |
| 1535 |
NULL); |
| 1536 |
g_signal_connect (mechanism, |
| 1537 |
"handle-printer-set-info", |
| 1538 |
G_CALLBACK (cph_mechanism_printer_set_info), |
| 1539 |
NULL); |
| 1540 |
g_signal_connect (mechanism, |
| 1541 |
"handle-printer-set-job-sheets", |
| 1542 |
G_CALLBACK (cph_mechanism_printer_set_job_sheets), |
| 1543 |
NULL); |
| 1544 |
g_signal_connect (mechanism, |
| 1545 |
"handle-printer-set-location", |
| 1546 |
G_CALLBACK (cph_mechanism_printer_set_location), |
| 1547 |
NULL); |
| 1548 |
g_signal_connect (mechanism, |
| 1549 |
"handle-printer-set-op-policy", |
| 1550 |
G_CALLBACK (cph_mechanism_printer_set_op_policy), |
| 1551 |
NULL); |
| 1552 |
g_signal_connect (mechanism, |
| 1553 |
"handle-printer-set-shared", |
| 1554 |
G_CALLBACK (cph_mechanism_printer_set_shared), |
| 1555 |
NULL); |
| 1556 |
g_signal_connect (mechanism, |
| 1557 |
"handle-printer-set-users-allowed", |
| 1558 |
G_CALLBACK (cph_mechanism_printer_set_users_allowed), |
| 1559 |
NULL); |
| 1560 |
g_signal_connect (mechanism, |
| 1561 |
"handle-printer-set-users-denied", |
| 1562 |
G_CALLBACK (cph_mechanism_printer_set_users_denied), |
| 1563 |
NULL); |
| 1564 |
g_signal_connect (mechanism, |
| 1565 |
"handle-server-get-settings", |
| 1566 |
G_CALLBACK (cph_mechanism_server_get_settings), |
| 1567 |
NULL); |
| 1568 |
g_signal_connect (mechanism, |
| 1569 |
"handle-server-set-settings", |
| 1570 |
G_CALLBACK (cph_mechanism_server_set_settings), |
| 1571 |
NULL); |
| 1572 |
} |