19
19
*/
20
20
21
21
#include "firecfg.h"
22
+ #include <ctype.h>
22
23
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 ) {
43
25
// build profile name
44
26
char * profname1 ;
45
27
char * profname2 ;
46
- if (asprintf (& profname1 , "%s/%s.profile" , SYSCONFDIR , f1 ) == -1 )
28
+ if (asprintf (& profname1 , "%s/%s.profile" , SYSCONFDIR , name ) == -1 )
47
29
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 )
49
31
errExit ("asprintf" );
50
32
51
33
int rv = 0 ;
@@ -60,14 +42,57 @@ static int have_profile(const char *filename, const char *homedir) {
60
42
rv = 1 ;
61
43
}
62
44
63
- if (arg_debug )
64
- printf ("Profile for %s %s\n" , f1 , (rv )? "found" : "not found" );
65
- free (f1 );
66
45
free (profname1 );
67
46
free (profname2 );
68
47
return rv ;
69
48
}
70
49
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
+
71
96
void fix_desktop_files (char * homedir ) {
72
97
assert (homedir );
73
98
struct stat sb ;
@@ -174,34 +199,36 @@ void fix_desktop_files(char *homedir) {
174
199
continue ;
175
200
}
176
201
177
-
178
202
// try to decide if we need to covert this file
179
203
char * change_exec = NULL ;
180
204
int change_dbus = 0 ;
181
205
206
+ if (strstr (buf , "\nDBusActivatable=true" ))
207
+ change_dbus = 1 ;
208
+
182
209
// https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s06.html
183
210
// The executable program can either be specified with its full path
184
211
// or with the name of the executable only
185
212
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" );
198
228
}
199
229
}
200
230
}
201
231
202
- if (strstr (buf , "\nDBusActivatable=true" ))
203
- change_dbus = 1 ;
204
-
205
232
if (change_exec == NULL && change_dbus == 0 ) {
206
233
munmap (buf , sb .st_size + 1 );
207
234
continue ;
@@ -242,9 +269,12 @@ void fix_desktop_files(char *homedir) {
242
269
fprintf (fpout , "DBusActivatable=false\n" );
243
270
else if (change_exec && strncmp (fbuf , "Exec=" , 5 ) == 0 ) {
244
271
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 );
248
278
}
249
279
else
250
280
fprintf (fpout , "%s" , fbuf );
0 commit comments