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
|
/*
* Copyright (C) 2014-2018 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"
// return 1 if the program is found
static int find(const char *program, const char *directory) {
int retval = 0;
char *fname;
if (asprintf(&fname, "/%s/%s", directory, program) == -1)
errExit("asprintf");
struct stat s;
if (stat(fname, &s) == 0) {
if (arg_debug)
printf("found %s in directory %s\n", program, directory);
retval = 1;
}
free(fname);
return retval;
}
// return 1 if program is installed on the system
int which(const char *program) {
// check some well-known paths
if (find(program, "/bin") || find(program, "/usr/bin") ||
find(program, "/sbin") || find(program, "/usr/sbin") ||
find(program, "/usr/games"))
return 1;
// check environment
char *path1 = getenv("PATH");
if (path1) {
char *path2 = strdup(path1);
if (!path2)
errExit("strdup");
// use path2 to count the entries
char *ptr = strtok(path2, ":");
while (ptr) {
// Ubuntu 18.04 is adding /snap/bin to PATH;
// they populate /snap/bin with simbolic links to /usr/bin/ programs;
// most simlinked programs are not installed by default.
// Removing /snap/bin from our search
if (strcmp(ptr, "/snap/bin") != 0) {
if (find(program, ptr)) {
free(path2);
return 1;
}
}
ptr = strtok(NULL, ":");
}
free(path2);
}
return 0;
}
// return 1 if the file is a link
int is_link(const char *fname) {
assert(fname);
if (*fname == '\0')
return 0;
struct stat s;
if (lstat(fname, &s) == 0) {
if (S_ISLNK(s.st_mode))
return 1;
}
return 0;
}
|