forked from flatpak/xdg-desktop-portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate-icon.c
285 lines (241 loc) · 7.8 KB
/
validate-icon.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* Copyright © 2018 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:
* Matthias Clasen <[email protected]>
*/
/* The canonical copy of this file is in:
* - https://github.com/flatpak/flatpak at icon-validator/validate-icon.c
* Known copies of this file are in:
* - https://github.com/flatpak/xdg-desktop-portal at src/validate-icon.c
*/
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <glib/gstdio.h>
#include <errno.h>
#include <unistd.h>
#ifdef __FreeBSD__
#define execvpe exect
#endif
#define ICON_VALIDATOR_GROUP "Icon Validator"
static int
validate_icon (const char *arg_width,
const char *arg_height,
const char *filename)
{
GdkPixbufFormat *format;
int max_width, max_height;
int width, height;
g_autofree char *name = NULL;
const char *allowed_formats[] = { "png", "jpeg", "svg", NULL };
g_autoptr(GdkPixbuf) pixbuf = NULL;
g_autoptr(GError) error = NULL;
g_autoptr(GKeyFile) key_file = NULL;
g_autofree char *key_file_data = NULL;
format = gdk_pixbuf_get_file_info (filename, &width, &height);
if (format == NULL)
{
g_printerr ("Format not recognized\n");
return 1;
}
name = gdk_pixbuf_format_get_name (format);
if (!g_strv_contains (allowed_formats, name))
{
g_printerr ("Format %s not accepted\n", name);
return 1;
}
if (!g_str_equal (name, "svg"))
{
max_width = g_ascii_strtoll (arg_width, NULL, 10);
if (max_width < 16 || max_width > 4096)
{
g_printerr ("Bad width limit: %s\n", arg_width);
return 1;
}
max_height = g_ascii_strtoll (arg_height, NULL, 10);
if (max_height < 16 || max_height > 4096)
{
g_printerr ("Bad height limit: %s\n", arg_height);
return 1;
}
}
else
{
/* Sanity check for vector files */
max_height = max_width = 4096;
}
if (width > max_width || height > max_height)
{
g_printerr ("Image too large (%dx%d). Max. size %dx%d\n", width, height, max_width, max_height);
return 1;
}
pixbuf = gdk_pixbuf_new_from_file (filename, &error);
if (pixbuf == NULL)
{
g_printerr ("Failed to load image: %s\n", error->message);
return 1;
}
if (width != height)
{
g_printerr ("Expected a square icon but got: %dx%d\n", width, height);
return 1;
}
/* Print the format and size for consumption by (at least) the dynamic
* launcher portal. xdg-desktop-portal has a copy of this file. Use a
* GKeyFile so the output can be easily extended in the future in a backwards
* compatible way.
*/
key_file = g_key_file_new ();
g_key_file_set_string (key_file, ICON_VALIDATOR_GROUP, "format", name);
g_key_file_set_integer (key_file, ICON_VALIDATOR_GROUP, "width", width);
key_file_data = g_key_file_to_data (key_file, NULL, NULL);
g_print ("%s", key_file_data);
return 0;
}
#ifdef HELPER
G_GNUC_NULL_TERMINATED
static void
add_args (GPtrArray *argv_array, ...)
{
va_list args;
const char *arg;
va_start (args, argv_array);
while ((arg = va_arg (args, const gchar *)))
g_ptr_array_add (argv_array, g_strdup (arg));
va_end (args);
}
static gboolean
path_is_usrmerged (const char *dir)
{
/* does /dir point to /usr/dir? */
g_autofree char *target = NULL;
GStatBuf stat_buf_src, stat_buf_target;
if (g_stat (dir, &stat_buf_src) < 0)
return FALSE;
target = g_strdup_printf ("/usr/%s", dir);
if (g_stat (target, &stat_buf_target) < 0)
return FALSE;
return (stat_buf_src.st_dev == stat_buf_target.st_dev) &&
(stat_buf_src.st_ino == stat_buf_target.st_ino);
}
const char *
flatpak_get_bwrap (void)
{
const char *e = g_getenv ("FLATPAK_BWRAP");
if (e != NULL)
return e;
return HELPER;
}
static int
rerun_in_sandbox (const char *arg_width,
const char *arg_height,
const char *filename)
{
const char * const usrmerged_dirs[] = { "bin", "lib32", "lib64", "lib", "sbin" };
int i;
g_autoptr(GPtrArray) args = g_ptr_array_new_with_free_func (g_free);
char validate_icon[PATH_MAX + 1];
ssize_t symlink_size;
symlink_size = readlink ("/proc/self/exe", validate_icon, sizeof (validate_icon) - 1);
if (symlink_size < 0 || (size_t) symlink_size >= sizeof (validate_icon))
{
g_printerr ("Error: failed to read /proc/self/exe\n");
return 1;
}
validate_icon[symlink_size] = 0;
add_args (args,
flatpak_get_bwrap (),
"--unshare-ipc",
"--unshare-net",
"--unshare-pid",
"--ro-bind", "/usr", "/usr",
"--ro-bind-try", "/etc/ld.so.cache", "/etc/ld.so.cache",
"--ro-bind", validate_icon, validate_icon,
NULL);
/* These directories might be symlinks into /usr/... */
for (i = 0; i < G_N_ELEMENTS (usrmerged_dirs); i++)
{
g_autofree char *absolute_dir = g_strdup_printf ("/%s", usrmerged_dirs[i]);
if (!g_file_test (absolute_dir, G_FILE_TEST_EXISTS))
continue;
if (path_is_usrmerged (absolute_dir))
{
g_autofree char *symlink_target = g_strdup_printf ("/usr/%s", absolute_dir);
add_args (args,
"--symlink", symlink_target, absolute_dir,
NULL);
}
else
{
add_args (args,
"--ro-bind", absolute_dir, absolute_dir,
NULL);
}
}
add_args (args,
"--tmpfs", "/tmp",
"--proc", "/proc",
"--dev", "/dev",
"--chdir", "/",
"--setenv", "GIO_USE_VFS", "local",
"--unsetenv", "TMPDIR",
"--die-with-parent",
"--ro-bind", filename, filename,
NULL);
if (g_getenv ("G_MESSAGES_DEBUG"))
add_args (args, "--setenv", "G_MESSAGES_DEBUG", g_getenv ("G_MESSAGES_DEBUG"), NULL);
if (g_getenv ("G_MESSAGES_PREFIXED"))
add_args (args, "--setenv", "G_MESSAGES_PREFIXED", g_getenv ("G_MESSAGES_PREFIXED"), NULL);
add_args (args, validate_icon, arg_width, arg_height, filename, NULL);
g_ptr_array_add (args, NULL);
{
g_autofree char *cmdline = g_strjoinv (" ", (char **) args->pdata);
g_debug ("Icon validation: Spawning %s", cmdline);
}
execvpe (flatpak_get_bwrap (), (char **) args->pdata, NULL);
/* If we get here, then execvpe() failed. */
g_printerr ("Icon validation: execvpe %s: %s\n", flatpak_get_bwrap (), g_strerror (errno));
return 1;
}
#endif
static gboolean opt_sandbox;
static GOptionEntry entries[] = {
{ "sandbox", 0, 0, G_OPTION_ARG_NONE, &opt_sandbox, "Run in a sandbox", NULL },
{ NULL }
};
int
main (int argc, char *argv[])
{
g_autoptr(GOptionContext) context = NULL;
g_autoptr(GError) error = NULL;
context = g_option_context_new ("WIDTH HEIGHT PATH");
g_option_context_add_main_entries (context, entries, NULL);
if (!g_option_context_parse (context, &argc, &argv, &error))
{
g_printerr ("Error: %s\n", error->message);
return 1;
}
if (argc != 4)
{
g_printerr ("Usage: %s [OPTION…] WIDTH HEIGHT PATH\n", argv[0]);
return 1;
}
#ifdef HELPER
if (opt_sandbox)
return rerun_in_sandbox (argv[1], argv[2], argv[3]);
else
#endif
return validate_icon (argv[1], argv[2], argv[3]);
}