comparison 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, 19 Jun 2018 15:44:36 +0200
parents a4020e99e550
children adba37525ee5
comparison
equal deleted inserted replaced
5:a4020e99e550 6:2477a6151087
22 */ 22 */
23 23
24 #include <errno.h> 24 #include <errno.h>
25 #include <fcntl.h> 25 #include <fcntl.h>
26 #include <packagekit-glib2/packagekit.h> 26 #include <packagekit-glib2/packagekit.h>
27 #include <polkit/polkit.h>
27 #include <string.h> 28 #include <string.h>
28 #include <sys/stat.h> 29 #include <sys/stat.h>
29 #include <sys/types.h> 30 #include <sys/types.h>
30 #include <upower.h> 31 #include <upower.h>
31 #include <utime.h> 32 #include <utime.h>
40 GObject parent_instance; 41 GObject parent_instance;
41 PkControl *pk_control; 42 PkControl *pk_control;
42 GCancellable *cancellable; 43 GCancellable *cancellable;
43 UpClient *up_client; 44 UpClient *up_client;
44 UpDevice *up_device; 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;
45 gint64 last_check; 52 gint64 last_check;
46 PkNetworkEnum network_state; 53 PkNetworkEnum network_state;
47 gboolean inhibited; 54 gboolean inhibited;
48 gboolean is_battery_low; 55 gboolean is_battery_low;
49 guint periodic_check_id; 56 guint periodic_check_id;
310 317
311 G_OBJECT_CLASS(pui_backend_parent_class)->dispose(object); 318 G_OBJECT_CLASS(pui_backend_parent_class)->dispose(object);
312 } 319 }
313 320
314 static void 321 static void
322 pui_backend_finalize(GObject *object)
323 {
324 PuiBackend *self = PUI_BACKEND(object);
325
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);
332
333 G_OBJECT_CLASS(pui_backend_parent_class)->finalize(object);
334 }
335
336 static void
315 pui_backend_class_init(PuiBackendClass *klass) 337 pui_backend_class_init(PuiBackendClass *klass)
316 { 338 {
317 GObjectClass *object_class = G_OBJECT_CLASS(klass); 339 GObjectClass *object_class = G_OBJECT_CLASS(klass);
318 340
319 object_class->set_property = pui_backend_set_property; 341 object_class->set_property = pui_backend_set_property;
320 object_class->get_property = pui_backend_get_property; 342 object_class->get_property = pui_backend_get_property;
321 object_class->dispose = pui_backend_dispose; 343 object_class->dispose = pui_backend_dispose;
344 object_class->finalize = pui_backend_finalize;
322 345
323 properties[PROP_IMPORTANT_UPDATES] = 346 properties[PROP_IMPORTANT_UPDATES] =
324 g_param_spec_uint("important-updates", "Important updates", 347 g_param_spec_uint("important-updates", "Important updates",
325 "Number of available important updates", 0, G_MAXUINT, 0, 348 "Number of available important updates", 0, G_MAXUINT, 0,
326 G_PARAM_READABLE); 349 G_PARAM_READABLE);
557 result, errorp); 580 result, errorp);
558 g_object_unref(source_object); 581 g_object_unref(source_object);
559 582
560 return ((object != NULL) ? PUI_BACKEND(object) : NULL); 583 return ((object != NULL) ? PUI_BACKEND(object) : NULL);
561 } 584 }
585
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;
592
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 }
598
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;
606
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 }
614
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 }
620
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 }
638
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;
658
659 polkit_permission_new("org.freedesktop.packagekit."
660 "system-network-proxy-configure", NULL, NULL,
661 on_polkit_permission_finished, self);
662 }