Skip to content

Commit 3e69deb

Browse files
author
netblue30
committed
fix firecfg
1 parent 94bb788 commit 3e69deb

File tree

1 file changed

+74
-44
lines changed

1 file changed

+74
-44
lines changed

src/firecfg/desktop_files.c

+74-44
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,15 @@
1919
*/
2020

2121
#include "firecfg.h"
22+
#include <ctype.h>
2223

23-
// look for a profile file in /etc/firejail diectory and in homedir/.config/firejail directory
24-
static int have_profile(const char *filename, const char *homedir) {
25-
assert(filename);
26-
assert(homedir);
27-
28-
if (arg_debug)
29-
printf("checking profile for %s\n", filename);
30-
31-
// remove .desktop extension; if file name starts with org.gnome... remove it
32-
char *f1;
33-
if (strncmp(filename, "org.gnome.", 10) == 0)
34-
f1 = strdup(filename + 10);
35-
else
36-
f1 = strdup(filename);
37-
if (!f1)
38-
errExit("strdup");
39-
f1[strlen(f1) - 8] = '\0';
40-
if (arg_debug)
41-
printf("looking for a profile for %s - %s\n", filename, f1);
42-
24+
static int check_profile(const char *name, const char *homedir) {
4325
// build profile name
4426
char *profname1;
4527
char *profname2;
46-
if (asprintf(&profname1, "%s/%s.profile", SYSCONFDIR, f1) == -1)
28+
if (asprintf(&profname1, "%s/%s.profile", SYSCONFDIR, name) == -1)
4729
errExit("asprintf");
48-
if (asprintf(&profname2, "%s/.config/firejail/%s.profile", homedir, f1) == -1)
30+
if (asprintf(&profname2, "%s/.config/firejail/%s.profile", homedir, name) == -1)
4931
errExit("asprintf");
5032

5133
int rv = 0;
@@ -60,14 +42,57 @@ static int have_profile(const char *filename, const char *homedir) {
6042
rv = 1;
6143
}
6244

63-
if (arg_debug)
64-
printf("Profile for %s %s\n", f1, (rv)? "found": "not found");
65-
free(f1);
6645
free(profname1);
6746
free(profname2);
6847
return rv;
6948
}
7049

50+
51+
// look for a profile file in /etc/firejail diectory and in homedir/.config/firejail directory
52+
static int have_profile(const char *filename, const char *homedir) {
53+
assert(filename);
54+
assert(homedir);
55+
56+
if (arg_debug)
57+
printf("checking profile for %s\n", filename);
58+
59+
// we get strange names here, such as .org.gnom.gedit.desktop, com.uploadedlobster.peek.desktop,
60+
// or io.github.Pithos.desktop; extract the word before .desktop
61+
62+
char *tmpfname = strdup(filename);
63+
if (!tmpfname)
64+
errExit("strdup");
65+
66+
// check .desktop extension
67+
int len = strlen(tmpfname);
68+
if (len <= 8)
69+
return 0;
70+
if (strcmp(tmpfname + len - 8, ".desktop"))
71+
return 0;
72+
tmpfname[len - 8] = '\0';
73+
74+
// extract last word
75+
char *last_word = strrchr(tmpfname, '.');
76+
if (last_word)
77+
last_word++;
78+
else
79+
last_word = tmpfname;
80+
81+
// try lowercase
82+
last_word[0] = tolower(last_word[0]);
83+
int rv = check_profile(last_word, homedir);
84+
if (rv) {
85+
free(tmpfname);
86+
return rv;
87+
}
88+
89+
// try uppercase
90+
last_word[0] = toupper(last_word[0]);
91+
rv = check_profile(last_word, homedir);
92+
free(tmpfname);
93+
return rv;
94+
}
95+
7196
void fix_desktop_files(char *homedir) {
7297
assert(homedir);
7398
struct stat sb;
@@ -174,34 +199,36 @@ void fix_desktop_files(char *homedir) {
174199
continue;
175200
}
176201

177-
178202
// try to decide if we need to covert this file
179203
char *change_exec = NULL;
180204
int change_dbus = 0;
181205

206+
if (strstr(buf, "\nDBusActivatable=true"))
207+
change_dbus = 1;
208+
182209
// https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s06.html
183210
// The executable program can either be specified with its full path
184211
// or with the name of the executable only
185212
if (execname[0] == '/') {
186-
char *end_name = strchr(execname, ' ');
187-
if (end_name) {
188-
*end_name = '\0';
189-
char *start_name = strrchr(execname, '/');
190-
if (start_name) {
191-
start_name++;
192-
// check if we have the executable on the regular path
193-
if (which(start_name)) {
194-
change_exec = strdup(start_name);
195-
if (!change_exec)
196-
errExit("strdup");
197-
}
213+
// mark end of line
214+
char *end = strchr(execname, '\n');
215+
if (end)
216+
*end = '\0';
217+
end = strchr(execname, ' ');
218+
if (end)
219+
*end = '\0';
220+
char *start_name = strrchr(execname, '/');
221+
if (start_name) {
222+
start_name++;
223+
// check if we have the executable on the regular path
224+
if (which(start_name)) {
225+
change_exec = strdup(start_name);
226+
if (!change_exec)
227+
errExit("strdup");
198228
}
199229
}
200230
}
201231

202-
if (strstr(buf, "\nDBusActivatable=true"))
203-
change_dbus = 1;
204-
205232
if (change_exec == NULL && change_dbus == 0) {
206233
munmap(buf, sb.st_size + 1);
207234
continue;
@@ -242,9 +269,12 @@ void fix_desktop_files(char *homedir) {
242269
fprintf(fpout, "DBusActivatable=false\n");
243270
else if (change_exec && strncmp(fbuf, "Exec=", 5) == 0) {
244271
char *start_params = strchr(fbuf + 5, ' ');
245-
assert(start_params);
246-
start_params++;
247-
fprintf(fpout, "Exec=%s %s", change_exec, start_params);
272+
if (start_params) {
273+
start_params++;
274+
fprintf(fpout, "Exec=%s %s", change_exec, start_params);
275+
}
276+
else
277+
fprintf(fpout, "Exec=%s\n", change_exec);
248278
}
249279
else
250280
fprintf(fpout, "%s", fbuf);

0 commit comments

Comments
 (0)