/
flatpak-run-cups.c
127 lines (105 loc) · 3.92 KB
/
flatpak-run-cups.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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:https://www.gnu.org/licenses/>.
*
* Authors:
* Alexander Larsson <[email protected]>
*/
#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);
}