projects/package-update-indicator

view pui-backend.c @ 6:2477a6151087

Make PackagKit use the user's network proxies

Pick up network proxies from the user's environment and make PackagKit use
them. Setting network proxies is a privileged operation, so it is only
attempted if the user is allowed to use the polkit action
org.freedesktop.packagekit.system-network-proxy-configure without
authentication.
author Guido Berhoerster <guido+pui@berhoerster.name>
date Tue Jun 19 15:44:36 2018 +0200 (2018-06-19)
parents a4020e99e550
children adba37525ee5
line source
1 /*
2 * Copyright (C) 2018 Guido Berhoerster <guido+pui@berhoerster.name>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <packagekit-glib2/packagekit.h>
27 #include <polkit/polkit.h>
28 #include <string.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <upower.h>
32 #include <utime.h>
34 #include "pui-common.h"
35 #include "pui-backend.h"
36 #include "pui-get-updates.h"
38 #define LOW_BATTERY_THRESHOLD 10.0
40 struct _PuiBackend {
41 GObject parent_instance;
42 PkControl *pk_control;
43 GCancellable *cancellable;
44 UpClient *up_client;
45 UpDevice *up_device;
46 gchar *proxy_http;
47 gchar *proxy_https;
48 gchar *proxy_ftp;
49 gchar *proxy_socks;
50 gchar *no_proxy;
51 gchar *pac;
52 gint64 last_check;
53 PkNetworkEnum network_state;
54 gboolean inhibited;
55 gboolean is_battery_low;
56 guint periodic_check_id;
57 guint refresh_interval;
58 gboolean use_mobile_connection;
59 guint important_updates;
60 guint normal_updates;
61 };
63 static void pui_backend_async_initable_iface_init(gpointer, gpointer);
65 G_DEFINE_TYPE_WITH_CODE(PuiBackend, pui_backend, G_TYPE_OBJECT,
66 G_IMPLEMENT_INTERFACE(G_TYPE_ASYNC_INITABLE,
67 pui_backend_async_initable_iface_init))
69 enum {
70 STATE_CHANGED,
71 RESTART_REQUIRED,
72 SIGNAL_LAST
73 };
75 enum {
76 PROP_0,
77 PROP_IMPORTANT_UPDATES,
78 PROP_NORMAL_UPDATES,
79 PROP_REFRESH_INTERVAL,
80 PROP_USE_MOBILE_CONNECTION,
81 PROP_LAST
82 };
84 static guint signals[SIGNAL_LAST] = { 0 };
85 static GParamSpec *properties[PROP_LAST] = { NULL };
87 static gboolean periodic_check(gpointer);
89 GQuark
90 pui_backend_error_quark(void)
91 {
92 return (g_quark_from_static_string("pui-backend-error-quark"));
93 }
95 static void
96 process_pk_package(gpointer data, gpointer user_data)
97 {
98 PkPackage *package = data;
99 PuiBackend *self = user_data;
100 PkInfoEnum type_info = pk_package_get_info(package);
102 switch (type_info) {
103 case PK_INFO_ENUM_LOW: /* FALLTHROUGH */
104 case PK_INFO_ENUM_ENHANCEMENT: /* FALLTHROUGH */
105 case PK_INFO_ENUM_NORMAL:
106 self->normal_updates++;
107 break;
108 case PK_INFO_ENUM_BUGFIX: /* FALLTHROUGH */
109 case PK_INFO_ENUM_IMPORTANT: /* FALLTHROUGH */
110 case PK_INFO_ENUM_SECURITY:
111 self->important_updates++;
112 break;
113 default:
114 break;
115 }
116 }
118 static void
119 on_get_updates_finished(GObject *source_object, GAsyncResult *async_result,
120 gpointer user_data)
121 {
122 PuiBackend *self = user_data;
123 GPtrArray *package_list = NULL;
124 GError *error = NULL;
125 guint prev_normal_updates = self->normal_updates;
126 guint prev_important_updates = self->important_updates;
128 package_list = pui_get_updates_finish(async_result, &error);
129 if (package_list == NULL) {
130 if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED) ||
131 g_error_matches(error, PUI_GET_UPDATES_ERROR,
132 PUI_GET_UPDATES_ERROR_CANCELLED)) {
133 /* cancelled */
134 g_debug("cancelled checking for updates");
135 } else {
136 g_warning("failed to check for updates: %s",
137 error->message);
138 }
139 g_error_free(error);
140 goto out;
141 }
143 self->normal_updates = 0;
144 self->important_updates = 0;
145 g_ptr_array_foreach(package_list, process_pk_package, self);
146 g_debug("normal updates: %u, important updates: %u",
147 self->normal_updates, self->important_updates);
148 if (self->normal_updates != prev_normal_updates) {
149 g_object_notify_by_pspec(G_OBJECT(self),
150 properties[PROP_NORMAL_UPDATES]);
151 }
152 if (self->important_updates != prev_important_updates) {
153 g_object_notify_by_pspec(G_OBJECT(self),
154 properties[PROP_IMPORTANT_UPDATES]);
155 }
156 if ((self->normal_updates != prev_normal_updates) ||
157 (self->important_updates != prev_important_updates)) {
158 g_debug("emitting signal state-changed");
159 g_signal_emit(self, signals[STATE_CHANGED], 0);
160 }
162 /* last successful check */
163 self->last_check = g_get_monotonic_time();
165 out:
166 g_clear_object(&self->cancellable);
168 /* reschedule periodic check */
169 if (!self->inhibited) {
170 self->periodic_check_id =
171 g_timeout_add_seconds(PUI_CHECK_UPDATES_INTERVAL,
172 periodic_check, self);
173 }
175 if (package_list != NULL) {
176 g_ptr_array_unref(package_list);
177 }
178 }
180 static gboolean
181 periodic_check(gpointer user_data)
182 {
183 PuiBackend *self = user_data;
185 g_debug("running periodic check");
187 self->cancellable = g_cancellable_new();
188 pui_get_updates_async(self->pk_control, self->refresh_interval,
189 self->cancellable, on_get_updates_finished, self);
191 /* next periodic check will be scheduled after completion */
192 self->periodic_check_id = 0;
194 return (G_SOURCE_REMOVE);
195 }
197 static void
198 check_inhibit(PuiBackend *self)
199 {
200 gboolean inhibited;
201 guint elapsed_time;
202 guint remaining_time;
204 inhibited = ((self->network_state == PK_NETWORK_ENUM_OFFLINE) ||
205 (!self->use_mobile_connection &&
206 (self->network_state == PK_NETWORK_ENUM_MOBILE)) ||
207 self->is_battery_low);
208 if (self->inhibited == inhibited) {
209 return;
210 }
212 self->inhibited = inhibited;
213 if (inhibited) {
214 /* cancel periodic checks */
215 if (self->periodic_check_id != 0) {
216 g_source_remove(self->periodic_check_id);
217 }
219 /* cancel running operation */
220 if ((self->cancellable != NULL) &&
221 !g_cancellable_is_cancelled(self->cancellable)) {
222 g_cancellable_cancel(self->cancellable);
223 g_clear_object(&self->cancellable);
224 }
225 } else {
226 /* schedule periodic checks when no longer inhibited */
227 elapsed_time = (g_get_monotonic_time() - self->last_check) /
228 G_USEC_PER_SEC;
229 /*
230 * if more time that the check interval has passed since the
231 * last check, schedule a check after a short delay, otherwise
232 * wait until the interval has passed
233 */
234 remaining_time = (elapsed_time < PUI_CHECK_UPDATES_INTERVAL) ?
235 PUI_CHECK_UPDATES_INTERVAL - elapsed_time :
236 PUI_STARTUP_DELAY;
237 self->periodic_check_id = g_timeout_add_seconds(remaining_time,
238 periodic_check, self);
239 }
240 }
242 static void
243 pui_backend_set_property(GObject *object, guint property_id,
244 const GValue *value, GParamSpec *pspec)
245 {
246 PuiBackend *self = PUI_BACKEND(object);
248 switch (property_id) {
249 case PROP_REFRESH_INTERVAL:
250 self->refresh_interval = g_value_get_uint(value);
251 g_debug("property \"refresh-interval\" set to %u",
252 self->refresh_interval);
253 break;
254 case PROP_USE_MOBILE_CONNECTION:
255 self->use_mobile_connection = g_value_get_boolean(value);
256 g_debug("property \"use-mobile-connection\" set to %s",
257 self->use_mobile_connection ? "true" : "false");
258 check_inhibit(self);
259 break;
260 default:
261 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
262 break;
263 }
264 }
266 static void
267 pui_backend_get_property(GObject *object, guint property_id, GValue *value,
268 GParamSpec *pspec)
269 {
270 PuiBackend *self = PUI_BACKEND(object);
272 switch (property_id) {
273 case PROP_IMPORTANT_UPDATES:
274 g_value_set_uint(value, self->important_updates);
275 break;
276 case PROP_NORMAL_UPDATES:
277 g_value_set_uint(value, self->normal_updates);
278 break;
279 case PROP_REFRESH_INTERVAL:
280 g_value_set_uint(value, self->refresh_interval);
281 break;
282 case PROP_USE_MOBILE_CONNECTION:
283 g_value_set_boolean(value, self->use_mobile_connection);
284 break;
285 default:
286 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
287 break;
288 }
289 }
291 static void
292 pui_backend_dispose(GObject *object)
293 {
294 PuiBackend *self = PUI_BACKEND(object);
296 if (self->periodic_check_id != 0) {
297 g_source_remove(self->periodic_check_id);
298 self->periodic_check_id = 0;
299 }
301 if (self->cancellable != NULL) {
302 g_cancellable_cancel(self->cancellable);
303 g_clear_object(&self->cancellable);
304 }
306 if (self->pk_control != NULL) {
307 g_clear_object(&self->pk_control);
308 }
310 if (self->up_device != NULL) {
311 g_clear_object(&self->up_device);
312 }
314 if (self->up_client != NULL) {
315 g_clear_object(&self->up_client);
316 }
318 G_OBJECT_CLASS(pui_backend_parent_class)->dispose(object);
319 }
321 static void
322 pui_backend_finalize(GObject *object)
323 {
324 PuiBackend *self = PUI_BACKEND(object);
326 g_free(self->proxy_http);
327 g_free(self->proxy_https);
328 g_free(self->proxy_ftp);
329 g_free(self->proxy_socks);
330 g_free(self->no_proxy);
331 g_free(self->pac);
333 G_OBJECT_CLASS(pui_backend_parent_class)->finalize(object);
334 }
336 static void
337 pui_backend_class_init(PuiBackendClass *klass)
338 {
339 GObjectClass *object_class = G_OBJECT_CLASS(klass);
341 object_class->set_property = pui_backend_set_property;
342 object_class->get_property = pui_backend_get_property;
343 object_class->dispose = pui_backend_dispose;
344 object_class->finalize = pui_backend_finalize;
346 properties[PROP_IMPORTANT_UPDATES] =
347 g_param_spec_uint("important-updates", "Important updates",
348 "Number of available important updates", 0, G_MAXUINT, 0,
349 G_PARAM_READABLE);
351 properties[PROP_NORMAL_UPDATES] =
352 g_param_spec_uint("normal-updates", "Normal updates",
353 "Number of available normal updates", 0, G_MAXUINT, 0,
354 G_PARAM_READABLE);
356 properties[PROP_REFRESH_INTERVAL] =
357 g_param_spec_uint("refresh-interval", "Refresh interval",
358 "Interval in seconds for refreshing the package cache", 0,
359 G_MAXUINT, PUI_DEFAULT_REFRESH_INTERVAL, G_PARAM_READWRITE);
361 properties[PROP_USE_MOBILE_CONNECTION] =
362 g_param_spec_boolean("use-mobile-connection",
363 "Whether to use a mobile connection", "Whether to use a mobile "
364 "connection for refreshing the package cache", FALSE,
365 G_PARAM_READWRITE);
367 g_object_class_install_properties(object_class, PROP_LAST, properties);
369 signals[STATE_CHANGED] = g_signal_new("state-changed",
370 G_TYPE_FROM_CLASS(object_class),
371 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0,
372 NULL, NULL, NULL, G_TYPE_NONE, 0);
374 signals[RESTART_REQUIRED] = g_signal_new("restart-required",
375 G_TYPE_FROM_CLASS(object_class),
376 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0,
377 NULL, NULL, NULL, G_TYPE_NONE, 0);
378 }
380 static void
381 pui_backend_init(PuiBackend *self)
382 {
383 self->pk_control = pk_control_new();
385 self->inhibited = TRUE;
387 self->up_client = up_client_new();
388 if (self->up_client) {
389 self->up_device = up_client_get_display_device(self->up_client);
390 }
391 }
393 static void
394 on_get_properties_finished(GObject *object, GAsyncResult *result,
395 gpointer user_data)
396 {
397 PkControl *control = PK_CONTROL(object);
398 PuiBackend *self;
399 GTask *task = user_data;
400 GError *error = NULL;
401 gchar *backend_name = NULL;
402 PkBitfield roles = 0;
403 gchar *roles_str = NULL;
405 self = g_task_get_task_data(task);
407 if (!pk_control_get_properties_finish(control, result, &error)) {
408 g_task_return_error(task, error);
409 goto out;
410 }
412 /* check whether the backend supports GetUpdates */
413 g_object_get(control, "backend-name", &backend_name, "roles", &roles,
414 "network-state", &self->network_state, NULL);
415 g_debug("backend: %s", backend_name);
416 roles_str = pk_role_bitfield_to_string(roles);
417 g_debug("roles: %s", roles_str);
418 g_debug("network-state: %s",
419 pk_network_enum_to_string(self->network_state));
420 if (!pk_bitfield_contain(roles, PK_ROLE_ENUM_GET_UPDATES)) {
421 error = g_error_new(PUI_BACKEND_ERROR,
422 PUI_BACKEND_ERROR_GET_UPDATES_NOT_IMPLEMENTED,
423 "Getting updates is not implemented in the %s PackageKit "
424 "backend", backend_name);
425 g_task_return_error(task, error);
426 goto out;
427 }
429 g_task_return_boolean(task, TRUE);
430 out:
431 g_free(roles_str);
432 g_free(backend_name);
433 g_object_unref(task);
434 }
436 static void
437 on_notify_device_charge_percentage(UpDevice *device, GParamSpec *pspec,
438 gpointer user_data)
439 {
440 PuiBackend *self = user_data;
441 UpDeviceKind kind;
442 gdouble percentage;
444 g_object_get(device, "kind", &kind, "percentage", &percentage, NULL);
445 if ((kind != UP_DEVICE_KIND_BATTERY) && (kind != UP_DEVICE_KIND_UPS)) {
446 return;
447 }
448 g_debug("charge percentage changed: %.0f%%\n", percentage);
449 if ((self->is_battery_low && (percentage > LOW_BATTERY_THRESHOLD)) ||
450 (!self->is_battery_low && (percentage < LOW_BATTERY_THRESHOLD))) {
451 self->is_battery_low = !self->is_battery_low;
452 check_inhibit(self);
453 }
454 }
456 static void
457 on_notify_network_state(PkControl *pk_control, GParamSpec *pspec,
458 gpointer user_data)
459 {
460 PuiBackend *self = user_data;
462 g_object_get(pk_control, "network-state", &self->network_state, NULL);
463 g_debug("network state changed: %s",
464 pk_network_enum_to_string(self->network_state));
465 check_inhibit(self);
466 }
468 static void
469 on_updates_changed(PkControl *control, gpointer user_data)
470 {
471 PuiBackend *self = user_data;
473 /*
474 * schedule a check after a short delay so that a rapid succession of
475 * signals is coalesced
476 */
477 if (!self->inhibited) {
478 if (self->periodic_check_id != 0) {
479 g_source_remove(self->periodic_check_id);
480 }
481 self->periodic_check_id =
482 g_timeout_add_seconds(PUI_STARTUP_DELAY, periodic_check,
483 self);
484 }
485 }
487 static void
488 on_restart_schedule(PkControl *control, gpointer user_data)
489 {
490 PuiBackend *self = user_data;
492 g_debug("emitting signal restart-required");
493 g_signal_emit(self, signals[RESTART_REQUIRED], 0);
494 }
496 static void
497 pui_backend_init_async(GAsyncInitable *initable, int io_priority,
498 GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
499 {
500 PuiBackend *self = PUI_BACKEND(initable);
501 GTask *task;
503 task = g_task_new(G_OBJECT(initable), cancellable, callback, user_data);
504 g_task_set_priority(task, io_priority);
505 g_task_set_task_data(task, g_object_ref(self),
506 (GDestroyNotify)g_object_unref);
508 pk_control_get_properties_async(self->pk_control, cancellable,
509 on_get_properties_finished, task);
510 }
512 static gboolean
513 pui_backend_init_finish(GAsyncInitable *initable, GAsyncResult *result,
514 GError **errorp)
515 {
516 PuiBackend *self = PUI_BACKEND(initable);
517 GTask *task = G_TASK(result);
518 UpDeviceKind kind;
519 gdouble percentage;
521 if (!g_task_propagate_boolean(task, errorp)) {
522 return (FALSE);
523 }
525 if (self->up_device != NULL) {
526 /* get the kind of device and charge percentage */
527 g_object_get(self->up_device, "kind", &kind, "percentage",
528 &percentage, NULL);
529 if ((kind == UP_DEVICE_KIND_BATTERY) ||
530 (kind == UP_DEVICE_KIND_UPS)) {
531 self->is_battery_low =
532 (percentage < LOW_BATTERY_THRESHOLD);
533 }
535 /* get notification if the charge percentage changes */
536 g_signal_connect(self->up_device, "notify::percentage",
537 G_CALLBACK(on_notify_device_charge_percentage), self);
538 }
540 /* get notification when the network state changes */
541 g_signal_connect(self->pk_control, "notify::network-state",
542 G_CALLBACK(on_notify_network_state), self);
543 /* get notifications when the available package updates change */
544 g_signal_connect(self->pk_control, "updates-changed",
545 G_CALLBACK(on_updates_changed), self);
546 /* get notifications when an application restart is required */
547 g_signal_connect(self->pk_control, "restart-schedule",
548 G_CALLBACK(on_restart_schedule), self);
550 check_inhibit(self);
552 return (TRUE);
553 }
555 static void
556 pui_backend_async_initable_iface_init(gpointer g_iface, gpointer iface_data)
557 {
558 GAsyncInitableIface *iface = g_iface;
560 iface->init_async = pui_backend_init_async;
561 iface->init_finish = pui_backend_init_finish;
562 }
564 void
565 pui_backend_new_async(GCancellable *cancellable, GAsyncReadyCallback callback,
566 gpointer user_data)
567 {
568 g_async_initable_new_async(PUI_TYPE_BACKEND, G_PRIORITY_DEFAULT,
569 cancellable, callback, user_data, NULL);
570 }
572 PuiBackend *
573 pui_backend_new_finish(GAsyncResult *result, GError **errorp)
574 {
575 GObject *object;
576 GObject *source_object;
578 source_object = g_async_result_get_source_object(result);
579 object = g_async_initable_new_finish(G_ASYNC_INITABLE(source_object),
580 result, errorp);
581 g_object_unref(source_object);
583 return ((object != NULL) ? PUI_BACKEND(object) : NULL);
584 }
586 static void
587 on_set_proxy_finished(GObject *source_object, GAsyncResult *result,
588 gpointer user_data)
589 {
590 PuiBackend *self = user_data;
591 GError *error = NULL;
593 if (!pk_control_set_proxy_finish(self->pk_control, result, &error)) {
594 g_warning("failed to set proxies: %s", error->message);
595 g_error_free(error);
596 }
597 }
599 static void
600 on_polkit_permission_finished(GObject *source_object, GAsyncResult *result,
601 gpointer user_data)
602 {
603 PuiBackend *self = user_data;
604 GPermission *permission;
605 GError *error = NULL;
607 permission = polkit_permission_new_finish(result, &error);
608 if (permission == NULL) {
609 g_warning("failed to create PolKit permission for setting the "
610 "network proxies: %s", error->message);
611 g_error_free(error);
612 return;
613 }
615 if (!g_permission_get_allowed(permission)) {
616 /* setting the proxy requires authentication or is disallowed */
617 g_debug("setting the network proxy is not allowed");
618 return;
619 }
621 g_debug("setting HTTP proxy to \"%s\"", (self->proxy_http != NULL) ?
622 self->proxy_http : "(null)");
623 g_debug("setting HTTPS proxy to \"%s\"", (self->proxy_https != NULL) ?
624 self->proxy_https : "(null)");
625 g_debug("setting FTP proxy to \"%s\"", (self->proxy_ftp != NULL) ?
626 self->proxy_ftp : "(null)");
627 g_debug("setting SOCKS proxy to \"%s\"", (self->proxy_socks != NULL) ?
628 self->proxy_socks : "(null)");
629 g_debug("setting the list of download IPs which should not go through "
630 "a proxy to \"%s\"", (self->no_proxy != NULL) ? self->no_proxy :
631 "(null)");
632 g_debug("setting the PAC string to \"%s\"", (self->pac != NULL) ?
633 self->pac : "(null)");
634 pk_control_set_proxy2_async(self->pk_control, self->proxy_http,
635 self->proxy_https, self->proxy_ftp, self->proxy_socks,
636 self->no_proxy, self->pac, NULL, on_set_proxy_finished, self);
637 }
639 void
640 pui_backend_set_proxy(PuiBackend *self, const gchar *proxy_http,
641 const gchar *proxy_https, const gchar *proxy_ftp, const gchar *proxy_socks,
642 const gchar *no_proxy, const gchar *pac)
643 {
644 g_free(self->proxy_http);
645 self->proxy_http = (proxy_http != NULL) ? g_strdup(proxy_http) : NULL;
646 g_free(self->proxy_https);
647 self->proxy_https = (proxy_https != NULL) ? g_strdup(proxy_https) :
648 NULL;
649 g_free(self->proxy_ftp);
650 self->proxy_ftp = (proxy_ftp != NULL) ? g_strdup(proxy_ftp) : NULL;
651 g_free(self->proxy_socks);
652 self->proxy_socks = (proxy_socks != NULL) ? g_strdup(proxy_socks) :
653 NULL;
654 g_free(self->no_proxy);
655 self->no_proxy = (no_proxy != NULL) ? g_strdup(no_proxy) : NULL;
656 g_free(self->pac);
657 self->pac = (pac != NULL) ? g_strdup(pac) : NULL;
659 polkit_permission_new("org.freedesktop.packagekit."
660 "system-network-proxy-configure", NULL, NULL,
661 on_polkit_permission_finished, self);
662 }