summaryrefslogtreecommitdiff
path: root/common/flatpak-run-cups.c
diff options
context:
space:
mode:
authorSimon McVittie <smcv@collabora.com>2023-05-15 17:33:01 +0100
committerSimon McVittie <smcv@collabora.com>2023-05-15 19:54:51 +0100
commitdb7a8bb598d34b65cecc32d7af9056f9e75ac611 (patch)
treee02ee06705695dd377d74cd39a9b229e84a6d5c6 /common/flatpak-run-cups.c
parent1cbff35386f4e6584646199a26fdfe82e72d732b (diff)
downloadflatpak-db7a8bb598d34b65cecc32d7af9056f9e75ac611.tar.gz
common: Split up socket setup from flatpak-run into multiple files
flatpak-run is large enough to be getting unwieldy, so separate it out into various smaller modules. A side benefit of these is that they'll be easier to reuse in other projects, like Steam's pressure-vessel tool. Signed-off-by: Simon McVittie <smcv@collabora.com>
Diffstat (limited to 'common/flatpak-run-cups.c')
-rw-r--r--common/flatpak-run-cups.c127
1 files changed, 127 insertions, 0 deletions
diff --git a/common/flatpak-run-cups.c b/common/flatpak-run-cups.c
new file mode 100644
index 00000000..c355e0f8
--- /dev/null
+++ b/common/flatpak-run-cups.c
@@ -0,0 +1,127 @@
+/* vi:set et sw=2 sts=2 cin cino=t0,f0,(0,{s,>2s,n-s,^-s,e-s:
+ * Copyright © 2014-2019 Red Hat, Inc
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ * Alexander Larsson <alexl@redhat.com>
+ */
+
+#include "config.h"
+#include "flatpak-run-cups-private.h"
+
+#include "flatpak-utils-private.h"
+
+static gboolean
+flatpak_run_cups_check_server_is_socket (const char *server)
+{
+ if (g_str_has_prefix (server, "/") && strstr (server, ":") == NULL)
+ return TRUE;
+
+ return FALSE;
+}
+
+/* Try to find a default server from a cups confguration file */
+static char *
+flatpak_run_get_cups_server_name_config (const char *path)
+{
+ g_autoptr(GFile) file = g_file_new_for_path (path);
+ g_autoptr(GError) my_error = NULL;
+ g_autoptr(GFileInputStream) input_stream = NULL;
+ g_autoptr(GDataInputStream) data_stream = NULL;
+ size_t len;
+
+ input_stream = g_file_read (file, NULL, &my_error);
+ if (my_error)
+ {
+ g_info ("CUPS configuration file '%s': %s", path, my_error->message);
+ return NULL;
+ }
+
+ data_stream = g_data_input_stream_new (G_INPUT_STREAM (input_stream));
+
+ while (TRUE)
+ {
+ g_autofree char *line = g_data_input_stream_read_line (data_stream, &len, NULL, NULL);
+ if (line == NULL)
+ break;
+
+ g_strchug (line);
+
+ if ((*line == '\0') || (*line == '#'))
+ continue;
+
+ g_auto(GStrv) tokens = g_strsplit (line, " ", 2);
+
+ if ((tokens[0] != NULL) && (tokens[1] != NULL))
+ {
+ if (strcmp ("ServerName", tokens[0]) == 0)
+ {
+ g_strchug (tokens[1]);
+
+ if (flatpak_run_cups_check_server_is_socket (tokens[1]))
+ return g_strdup (tokens[1]);
+ }
+ }
+ }
+
+ return NULL;
+}
+
+static char *
+flatpak_run_get_cups_server_name (void)
+{
+ g_autofree char * cups_server = NULL;
+ g_autofree char * cups_config_path = NULL;
+
+ /* TODO
+ * we don't currently support cups servers located on the network, if such
+ * server is detected, we simply ignore it and in the worst case we fallback
+ * to the default socket
+ */
+ cups_server = g_strdup (g_getenv ("CUPS_SERVER"));
+ if (cups_server && flatpak_run_cups_check_server_is_socket (cups_server))
+ return g_steal_pointer (&cups_server);
+ g_clear_pointer (&cups_server, g_free);
+
+ cups_config_path = g_build_filename (g_get_home_dir (), ".cups/client.conf", NULL);
+ cups_server = flatpak_run_get_cups_server_name_config (cups_config_path);
+ if (cups_server && flatpak_run_cups_check_server_is_socket (cups_server))
+ return g_steal_pointer (&cups_server);
+ g_clear_pointer (&cups_server, g_free);
+
+ cups_server = flatpak_run_get_cups_server_name_config ("/etc/cups/client.conf");
+ if (cups_server && flatpak_run_cups_check_server_is_socket (cups_server))
+ return g_steal_pointer (&cups_server);
+
+ // Fallback to default socket
+ return g_strdup ("/var/run/cups/cups.sock");
+}
+
+void
+flatpak_run_add_cups_args (FlatpakBwrap *bwrap)
+{
+ g_autofree char * sandbox_server_name = g_strdup ("/var/run/cups/cups.sock");
+ g_autofree char * cups_server_name = flatpak_run_get_cups_server_name ();
+
+ if (!g_file_test (cups_server_name, G_FILE_TEST_EXISTS))
+ {
+ g_info ("Could not find CUPS server");
+ return;
+ }
+
+ flatpak_bwrap_add_args (bwrap,
+ "--ro-bind", cups_server_name, sandbox_server_name,
+ NULL);
+}