projects/package-update-indicator

changeset 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 (23 months ago)
parents a4020e99e550
children 9d961e513af0
files Makefile README package-update-indicator.1.xml pui-application.c pui-backend.c pui-backend.h
line diff
     1.1 --- a/Makefile	Sun Jun 17 17:05:16 2018 +0200
     1.2 +++ b/Makefile	Tue Jun 19 15:44:36 2018 +0200
     1.3 @@ -143,9 +143,11 @@
     1.4  			-DSETTINGS_SCHEMA_ID=\"$(APPLICATION_ID)\" \
     1.5  			-DI_KNOW_THE_PACKAGEKIT_GLIB2_API_IS_SUBJECT_TO_CHANGE
     1.6  $(PACKAGE): XCFLAGS =	$(shell pkg-config --cflags gtk+-3.0 \
     1.7 -			    appindicator3-0.1 packagekit-glib2 upower-glib)
     1.8 +			    appindicator3-0.1 packagekit-glib2 \
     1.9 +			    polkit-gobject-1 upower-glib)
    1.10  $(PACKAGE): LDLIBS =	$(shell pkg-config --libs gtk+-3.0 \
    1.11 -			    appindicator3-0.1 packagekit-glib2 upower-glib)
    1.12 +			    appindicator3-0.1 packagekit-glib2 \
    1.13 +			    polkit-gobject-1 upower-glib)
    1.14  
    1.15  $(PACKAGE)-prefs: XCPPFLAGS = -DPACKAGE=\"$(PACKAGE)\" \
    1.16  			-DAPPLICATION_ID=\"$(PREFS_APPLICATION_ID)\" \
     2.1 --- a/README	Sun Jun 17 17:05:16 2018 +0200
     2.2 +++ b/README	Tue Jun 19 15:44:36 2018 +0200
     2.3 @@ -24,6 +24,7 @@
     2.4  - libappindicator 12.10.0 or later
     2.5  - PackageKit-glib2 0.8.17 or later
     2.6  - upower-glib 0.99.0 or later
     2.7 +- polkit 0.105 or later
     2.8  - the xsltproc tool from libxml2
     2.9  
    2.10  Before building package-update-indicator check the commented macros in the
     3.1 --- a/package-update-indicator.1.xml	Sun Jun 17 17:05:16 2018 +0200
     3.2 +++ b/package-update-indicator.1.xml	Tue Jun 19 15:44:36 2018 +0200
     3.3 @@ -33,7 +33,7 @@
     3.4        <email>guido+pui@berhoerster.name</email>
     3.5        <personblurb/>
     3.6      </author>
     3.7 -    <date>5 June, 2018</date>
     3.8 +    <date>19 June, 2018</date>
     3.9    </info>
    3.10    <refmeta>
    3.11      <refentrytitle>package-update-indicator</refentrytitle>
    3.12 @@ -73,6 +73,11 @@
    3.13      checks for software updates and notifies the user about available updates
    3.14      using desktop notifications and either a status notifier icon or a system
    3.15      tray icon.</para>
    3.16 +    <para>Protocol-specific proxies specified through the corresponding
    3.17 +    environment variables will be used by PackagKit when refreshing metadata if
    3.18 +    the user is allowed to use the polkit action
    3.19 +    <literal>org.freedesktop.packagekit.system-network-proxy-configure</literal>
    3.20 +    without authentication.</para>
    3.21    </refsect1>
    3.22    <refsect1>
    3.23      <title>Options</title>
    3.24 @@ -104,6 +109,42 @@
    3.25      </variablelist>
    3.26    </refsect1>
    3.27    <refsect1>
    3.28 +    <title>Environment Variables</title>
    3.29 +    <variablelist>
    3.30 +      <varlistentry>
    3.31 +        <term>http_proxy</term>
    3.32 +        <listitem>
    3.33 +          <para>Network proxy using the HTTP protcol.</para>
    3.34 +        </listitem>
    3.35 +      </varlistentry>
    3.36 +      <varlistentry>
    3.37 +        <term>https_proxy</term>
    3.38 +        <listitem>
    3.39 +          <para>Network proxy using the HTTPS protcol.</para>
    3.40 +        </listitem>
    3.41 +      </varlistentry>
    3.42 +      <varlistentry>
    3.43 +        <term>ftp_proxy</term>
    3.44 +        <listitem>
    3.45 +          <para>Network proxy using the FTP protcol.</para>
    3.46 +        </listitem>
    3.47 +      </varlistentry>
    3.48 +      <varlistentry>
    3.49 +        <term>socks_proxy</term>
    3.50 +        <listitem>
    3.51 +          <para>Network proxy using the SOCKS protcol.</para>
    3.52 +        </listitem>
    3.53 +      </varlistentry>
    3.54 +      <varlistentry>
    3.55 +        <term>no_proxy</term>
    3.56 +        <listitem>
    3.57 +          <para>Comma-separated list of host for which no proxy should be
    3.58 +            used.</para>
    3.59 +        </listitem>
    3.60 +      </varlistentry>
    3.61 +    </variablelist>
    3.62 +  </refsect1>
    3.63 +  <refsect1>
    3.64      <title>Exit Status</title>
    3.65      <para>The following exit values are returned:</para>
    3.66      <variablelist>
    3.67 @@ -131,6 +172,8 @@
    3.68      <title>See Also</title>
    3.69      <para><citerefentry>
    3.70        <refentrytitle>package-update-indicator-prefs</refentrytitle>
    3.71 -      <manvolnum>1</manvolnum></citerefentry></para>
    3.72 +      <manvolnum>1</manvolnum></citerefentry>,
    3.73 +      <citerefentry><refentrytitle>polkit</refentrytitle>
    3.74 +      <manvolnum>8</manvolnum></citerefentry></para>
    3.75    </refsect1>
    3.76  </refentry>
     4.1 --- a/pui-application.c	Sun Jun 17 17:05:16 2018 +0200
     4.2 +++ b/pui-application.c	Tue Jun 19 15:44:36 2018 +0200
     4.3 @@ -406,6 +406,10 @@
     4.4  		return;
     4.5  	}
     4.6  
     4.7 +	pui_backend_set_proxy(self->backend, g_getenv("http_proxy"),
     4.8 +	    g_getenv("https_proxy"), g_getenv("ftp_proxy"),
     4.9 +	    g_getenv("socks_proxy"), g_getenv("no_proxy"), NULL);
    4.10 +
    4.11  	g_settings_bind(self->settings, "refresh-interval", self->backend,
    4.12  	    "refresh-interval", G_SETTINGS_BIND_GET);
    4.13  	g_settings_bind(self->settings, "use-mobile-connection", self->backend,
     5.1 --- a/pui-backend.c	Sun Jun 17 17:05:16 2018 +0200
     5.2 +++ b/pui-backend.c	Tue Jun 19 15:44:36 2018 +0200
     5.3 @@ -24,6 +24,7 @@
     5.4  #include <errno.h>
     5.5  #include <fcntl.h>
     5.6  #include <packagekit-glib2/packagekit.h>
     5.7 +#include <polkit/polkit.h>
     5.8  #include <string.h>
     5.9  #include <sys/stat.h>
    5.10  #include <sys/types.h>
    5.11 @@ -42,6 +43,12 @@
    5.12  	GCancellable	*cancellable;
    5.13  	UpClient	*up_client;
    5.14  	UpDevice	*up_device;
    5.15 +	gchar		*proxy_http;
    5.16 +	gchar		*proxy_https;
    5.17 +	gchar		*proxy_ftp;
    5.18 +	gchar		*proxy_socks;
    5.19 +	gchar		*no_proxy;
    5.20 +	gchar		*pac;
    5.21  	gint64		last_check;
    5.22  	PkNetworkEnum	network_state;
    5.23  	gboolean	inhibited;
    5.24 @@ -312,6 +319,21 @@
    5.25  }
    5.26  
    5.27  static void
    5.28 +pui_backend_finalize(GObject *object)
    5.29 +{
    5.30 +	PuiBackend	*self = PUI_BACKEND(object);
    5.31 +
    5.32 +	g_free(self->proxy_http);
    5.33 +	g_free(self->proxy_https);
    5.34 +	g_free(self->proxy_ftp);
    5.35 +	g_free(self->proxy_socks);
    5.36 +	g_free(self->no_proxy);
    5.37 +	g_free(self->pac);
    5.38 +
    5.39 +	G_OBJECT_CLASS(pui_backend_parent_class)->finalize(object);
    5.40 +}
    5.41 +
    5.42 +static void
    5.43  pui_backend_class_init(PuiBackendClass *klass)
    5.44  {
    5.45  	GObjectClass	*object_class = G_OBJECT_CLASS(klass);
    5.46 @@ -319,6 +341,7 @@
    5.47  	object_class->set_property = pui_backend_set_property;
    5.48  	object_class->get_property = pui_backend_get_property;
    5.49  	object_class->dispose = pui_backend_dispose;
    5.50 +	object_class->finalize = pui_backend_finalize;
    5.51  
    5.52  	properties[PROP_IMPORTANT_UPDATES] =
    5.53  	    g_param_spec_uint("important-updates", "Important updates",
    5.54 @@ -559,3 +582,81 @@
    5.55  
    5.56  	return ((object != NULL) ? PUI_BACKEND(object) : NULL);
    5.57  }
    5.58 +
    5.59 +static void
    5.60 +on_set_proxy_finished(GObject *source_object, GAsyncResult *result,
    5.61 +    gpointer user_data)
    5.62 +{
    5.63 +	PuiBackend	*self = user_data;
    5.64 +	GError		*error = NULL;
    5.65 +
    5.66 +	if (!pk_control_set_proxy_finish(self->pk_control, result, &error)) {
    5.67 +		g_warning("failed to set proxies: %s", error->message);
    5.68 +		g_error_free(error);
    5.69 +	}
    5.70 +}
    5.71 +
    5.72 +static void
    5.73 +on_polkit_permission_finished(GObject *source_object, GAsyncResult *result,
    5.74 +    gpointer user_data)
    5.75 +{
    5.76 +	PuiBackend	*self = user_data;
    5.77 +	GPermission	*permission;
    5.78 +	GError		*error = NULL;
    5.79 +
    5.80 +	permission = polkit_permission_new_finish(result, &error);
    5.81 +	if (permission == NULL) {
    5.82 +		g_warning("failed to create PolKit permission for setting the "
    5.83 +		    "network proxies: %s", error->message);
    5.84 +		g_error_free(error);
    5.85 +		return;
    5.86 +	}
    5.87 +
    5.88 +	if (!g_permission_get_allowed(permission)) {
    5.89 +		/* setting the proxy requires authentication or is disallowed */
    5.90 +		g_debug("setting the network proxy is not allowed");
    5.91 +		return;
    5.92 +	}
    5.93 +
    5.94 +	g_debug("setting HTTP proxy to \"%s\"", (self->proxy_http != NULL) ?
    5.95 +	    self->proxy_http : "(null)");
    5.96 +	g_debug("setting HTTPS proxy to \"%s\"", (self->proxy_https != NULL) ?
    5.97 +	    self->proxy_https : "(null)");
    5.98 +	g_debug("setting FTP proxy to \"%s\"", (self->proxy_ftp != NULL) ?
    5.99 +	    self->proxy_ftp : "(null)");
   5.100 +	g_debug("setting SOCKS proxy to \"%s\"", (self->proxy_socks != NULL) ?
   5.101 +	    self->proxy_socks : "(null)");
   5.102 +	g_debug("setting the list of download IPs which should not go through "
   5.103 +	    "a proxy to \"%s\"", (self->no_proxy != NULL) ? self->no_proxy :
   5.104 +	    "(null)");
   5.105 +	g_debug("setting the PAC string to \"%s\"", (self->pac != NULL) ?
   5.106 +	    self->pac : "(null)");
   5.107 +	pk_control_set_proxy2_async(self->pk_control, self->proxy_http,
   5.108 +	    self->proxy_https, self->proxy_ftp, self->proxy_socks,
   5.109 +	    self->no_proxy, self->pac, NULL, on_set_proxy_finished, self);
   5.110 +}
   5.111 +
   5.112 +void
   5.113 +pui_backend_set_proxy(PuiBackend *self, const gchar *proxy_http,
   5.114 +    const gchar *proxy_https, const gchar *proxy_ftp, const gchar *proxy_socks,
   5.115 +    const gchar *no_proxy, const gchar *pac)
   5.116 +{
   5.117 +	g_free(self->proxy_http);
   5.118 +	self->proxy_http = (proxy_http != NULL) ? g_strdup(proxy_http) : NULL;
   5.119 +	g_free(self->proxy_https);
   5.120 +	self->proxy_https = (proxy_https != NULL) ? g_strdup(proxy_https) :
   5.121 +	    NULL;
   5.122 +	g_free(self->proxy_ftp);
   5.123 +	self->proxy_ftp = (proxy_ftp != NULL) ? g_strdup(proxy_ftp) : NULL;
   5.124 +	g_free(self->proxy_socks);
   5.125 +	self->proxy_socks = (proxy_socks != NULL) ? g_strdup(proxy_socks) :
   5.126 +	    NULL;
   5.127 +	g_free(self->no_proxy);
   5.128 +	self->no_proxy = (no_proxy != NULL) ? g_strdup(no_proxy) : NULL;
   5.129 +	g_free(self->pac);
   5.130 +	self->pac = (pac != NULL) ? g_strdup(pac) : NULL;
   5.131 +
   5.132 +	polkit_permission_new("org.freedesktop.packagekit."
   5.133 +	    "system-network-proxy-configure", NULL, NULL,
   5.134 +	    on_polkit_permission_finished, self);
   5.135 +}
     6.1 --- a/pui-backend.h	Sun Jun 17 17:05:16 2018 +0200
     6.2 +++ b/pui-backend.h	Tue Jun 19 15:44:36 2018 +0200
     6.3 @@ -43,6 +43,8 @@
     6.4  void		pui_backend_new_async(GCancellable *, GAsyncReadyCallback,
     6.5      gpointer);
     6.6  PuiBackend *	pui_backend_new_finish(GAsyncResult *, GError **);
     6.7 +void		pui_backend_set_proxy(PuiBackend *, const gchar *,
     6.8 +    const gchar *, const gchar *, const gchar *, const gchar *, const gchar *);
     6.9  
    6.10  G_END_DECLS
    6.11