-
Notifications
You must be signed in to change notification settings - Fork 573
/
desktop_files.c
357 lines (311 loc) · 9 KB
/
desktop_files.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* Copyright (C) 2014-2024 Firejail Authors
*
* This file is part of firejail project
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "firecfg.h"
#include <ctype.h>
static int check_profile(const char *name, const char *homedir) {
// build profile name
char *profname1;
#ifndef HAVE_ONLY_SYSCFG_PROFILES
char *profname2;
#endif
if (asprintf(&profname1, "%s/%s.profile", SYSCONFDIR, name) == -1)
errExit("asprintf");
#ifndef HAVE_ONLY_SYSCFG_PROFILES
if (asprintf(&profname2, "%s/.config/firejail/%s.profile", homedir, name) == -1)
errExit("asprintf");
#endif
int rv = 0;
if (access(profname1, R_OK) == 0) {
if (arg_debug)
printf("found %s\n", profname1);
rv = 1;
}
#ifndef HAVE_ONLY_SYSCFG_PROFILES
else if (access(profname2, R_OK) == 0) {
if (arg_debug)
printf("found %s\n", profname2);
rv = 1;
}
#endif
free(profname1);
#ifndef HAVE_ONLY_SYSCFG_PROFILES
free(profname2);
#endif
return rv;
}
// look for a profile file in /etc/firejail and ~/.config/firejail
static int have_profile(const char *filename, const char *homedir) {
assert(filename);
assert(homedir);
if (arg_debug)
printf("checking profile for %s\n", filename);
// we get strange names here, such as .org.gnome.gedit.desktop, com.uploadedlobster.peek.desktop,
// or io.github.Pithos.desktop; extract the word before .desktop
// TODO: implement proper fix for #2624 (names like org.gnome.Logs.desktop fall thru
// the 'last word' logic and don't get installed to ~/.local/share/applications
char *tmpfname = strdup(filename);
if (!tmpfname)
errExit("strdup");
// check .desktop extension
int len = strlen(tmpfname);
if (len <= 8) {
free(tmpfname);
return 0;
}
if (strcmp(tmpfname + len - 8, ".desktop")) {
free(tmpfname);
return 0;
}
tmpfname[len - 8] = '\0';
// extract last word
char *last_word = strrchr(tmpfname, '.');
if (last_word)
last_word++;
else
last_word = tmpfname;
// try lowercase
last_word[0] = tolower(last_word[0]);
int rv = check_profile(last_word, homedir);
if (rv) {
free(tmpfname);
return rv;
}
// try uppercase
last_word[0] = toupper(last_word[0]);
rv = check_profile(last_word, homedir);
free(tmpfname);
return rv;
}
void fix_desktop_files(const char *homedir) {
assert(homedir);
struct stat sb;
// check user
if (getuid() == 0) {
fprintf(stderr, "Error: this option is not supported for root user; please run as a regular user.\n");
exit(1);
}
// build ignorelist
parse_config_all(0);
// destination
// create ~/.local/share/applications directory if necessary
char *user_apps_dir;
if (asprintf(&user_apps_dir, "%s/.local/share/applications", homedir) == -1)
errExit("asprintf");
printf("\nFixing desktop files in %s\n", user_apps_dir);
if (stat(user_apps_dir, &sb) == -1) {
char *tmp;
if (asprintf(&tmp, "%s/.local", homedir) == -1)
errExit("asprintf");
int rv = mkdir(tmp, 0755);
(void) rv;
free(tmp);
if (asprintf(&tmp, "%s/.local/share", homedir) == -1)
errExit("asprintf");
rv = mkdir(tmp, 0755);
(void) rv;
free(tmp);
rv = mkdir(user_apps_dir, 0700);
if (rv) {
perror("mkdir");
fprintf(stderr, "Warning: cannot create ~/.local/share/application directory, desktop files fixing skipped...\n");
free(user_apps_dir);
return;
}
rv = chmod(user_apps_dir, 0700);
(void) rv;
}
// source
DIR *dir = opendir("/usr/share/applications");
if (!dir || chdir("/usr/share/applications")) {
perror("opendir");
fprintf(stderr, "Warning: cannot access /usr/share/applications directory, desktop files fixing skipped...\n");
free(user_apps_dir);
if (dir)
closedir(dir);
return;
}
// copy
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
const char *filename = entry->d_name;
if (strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0)
continue;
// skip if not regular file or link
// d_type is not available on some file systems
if (entry->d_type != DT_REG && entry->d_type != DT_LNK && entry->d_type != DT_UNKNOWN)
continue;
// skip if not .desktop file
char *exec = strdup(filename);
if (!exec)
errExit("strdup");
char *ptr = strstr(exec, ".desktop");
if (ptr == NULL || *(ptr + 8) != '\0') {
printf(" %s skipped (not a .desktop file)\n", exec);
free(exec);
continue;
}
// skip if program is in ignorelist
*ptr = '\0';
if (in_ignorelist(exec)) {
printf(" %s ignored\n", exec);
free(exec);
continue;
}
free(exec);
// skip links - Discord on Arch #4235 seems to be a symlink to /opt directory
// if (is_link(filename))
// continue;
// no profile in /etc/firejail, no desktop file fixing
if (!have_profile(filename, homedir))
continue;
//****************************************************
// load the file in memory and do some basic checking
//****************************************************
FILE *fp = fopen(filename, "r");
if (!fp) {
fprintf(stderr, "Warning: cannot open /usr/share/applications/%s\n", filename);
continue;
}
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
if (size == -1)
errExit("ftell");
fseek(fp, 0, SEEK_SET);
char *buf = malloc(size + 1);
if (!buf)
errExit("malloc");
size_t loaded = fread(buf, size, 1, fp);
fclose(fp);
if (loaded != 1) {
fprintf(stderr, "Warning: cannot read /usr/share/applications/%s\n", filename);
free(buf);
continue;
}
buf[size] = '\0';
// check format
if (strstr(buf, "[Desktop Entry]\n") == NULL) {
if (arg_debug)
printf(" %s - skipped: wrong format?\n", filename);
free(buf);
continue;
}
// get executable name
ptr = strstr(buf,"\nExec=");
if (!ptr || strlen(ptr) < 7) {
if (arg_debug)
printf(" %s - skipped: wrong format?\n", filename);
free(buf);
continue;
}
char *execname = ptr + 6;
// executable name can be quoted, this is rare and currently unsupported, TODO
if (execname[0] == '"') {
if (arg_debug)
printf(" %s - skipped: path quoting unsupported\n", filename);
free(buf);
continue;
}
// try to decide if we need to convert this file
char *change_exec = NULL;
int change_dbus = 0;
if (strstr(buf, "\nDBusActivatable=true"))
change_dbus = 1;
// https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s06.html
// The executable program can either be specified with its full path
// or with the name of the executable only
if (execname[0] == '/') {
// mark end of line
char *end = strchr(execname, '\n');
if (end)
*end = '\0';
end = strchr(execname, ' ');
if (end)
*end = '\0';
char *start_name = strrchr(execname, '/');
if (start_name) {
start_name++;
// check if we have the executable on the regular path
if (which(start_name)) {
change_exec = strdup(start_name);
if (!change_exec)
errExit("strdup");
}
}
}
free(buf);
if (change_exec == NULL && change_dbus == 0)
continue;
//****************************************************
// generate output file
//****************************************************
char *outname;
if (asprintf(&outname ,"%s/%s", user_apps_dir, filename) == -1)
errExit("asprintf");
if (stat(outname, &sb) == 0) {
printf(" %s skipped: file exists\n", filename);
free(outname);
if (change_exec)
free(change_exec);
continue;
}
FILE *fpin = fopen(filename, "r");
if (!fpin) {
fprintf(stderr, "Warning: cannot open /usr/share/applications/%s\n", filename);
free(outname);
if (change_exec)
free(change_exec);
continue;
}
FILE *fpout = fopen(outname, "w");
if (!fpout) {
fprintf(stderr, "Warning: cannot open ~/.local/share/applications/%s\n", outname);
fclose(fpin);
free(outname);
if (change_exec)
free(change_exec);
continue;
}
fprintf(fpout, "# converted by firecfg\n");
free(outname);
char fbuf[MAX_BUF];
while (fgets(fbuf, MAX_BUF, fpin)) {
if (change_dbus && strcmp(fbuf, "DBusActivatable=true\n") == 0)
fprintf(fpout, "DBusActivatable=false\n");
else if (change_exec && strncmp(fbuf, "Exec=", 5) == 0) {
char *start_params = strchr(fbuf + 5, ' ');
if (start_params) {
start_params++;
fprintf(fpout, "Exec=%s %s", change_exec, start_params);
}
else
fprintf(fpout, "Exec=%s\n", change_exec);
}
else
fprintf(fpout, "%s", fbuf);
}
if (change_exec)
free(change_exec);
fclose(fpin);
fclose(fpout);
printf(" %s created\n", filename);
}
closedir(dir);
free(user_apps_dir);
}