chiark / gitweb /
unit: introduce ConditionFileIsExecutable= and use it where we check for a binary...
[elogind.git] / src / condition.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #ifdef HAVE_SELINUX
28 #include <selinux/selinux.h>
29 #endif
30
31 #include "util.h"
32 #include "condition.h"
33
34 Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate) {
35         Condition *c;
36
37         assert(type < _CONDITION_TYPE_MAX);
38
39         if (!(c = new0(Condition, 1)))
40                 return NULL;
41
42         c->type = type;
43         c->trigger = trigger;
44         c->negate = negate;
45
46         if (parameter)
47                 if (!(c->parameter = strdup(parameter))) {
48                         free(c);
49                         return NULL;
50                 }
51
52         return c;
53 }
54
55 void condition_free(Condition *c) {
56         assert(c);
57
58         free(c->parameter);
59         free(c);
60 }
61
62 void condition_free_list(Condition *first) {
63         Condition *c, *n;
64
65         LIST_FOREACH_SAFE(conditions, c, n, first)
66                 condition_free(c);
67 }
68
69 static bool test_kernel_command_line(const char *parameter) {
70         char *line, *w, *state, *word = NULL;
71         bool equal;
72         int r;
73         size_t l, pl;
74         bool found = false;
75
76         assert(parameter);
77
78         if (detect_virtualization(NULL) > 0)
79                 return false;
80
81         if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
82                 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
83                 return false;
84         }
85
86         equal = !!strchr(parameter, '=');
87         pl = strlen(parameter);
88
89         FOREACH_WORD_QUOTED(w, l, line, state) {
90
91                 free(word);
92                 if (!(word = strndup(w, l)))
93                         break;
94
95                 if (equal) {
96                         if (streq(word, parameter)) {
97                                 found = true;
98                                 break;
99                         }
100                 } else {
101                         if (startswith(word, parameter) && (word[pl] == '=' || word[pl] == 0)) {
102                                 found = true;
103                                 break;
104                         }
105                 }
106
107         }
108
109         free(word);
110         free(line);
111
112         return found;
113 }
114
115 static bool test_virtualization(const char *parameter) {
116         int r, b;
117         const char *id;
118
119         assert(parameter);
120
121         if ((r = detect_virtualization(&id)) < 0) {
122                 log_warning("Failed to detect virtualization, ignoring: %s", strerror(-r));
123                 return false;
124         }
125
126         b = parse_boolean(parameter);
127
128         if (r > 0 && b > 0)
129                 return true;
130
131         if (r == 0 && b == 0)
132                 return true;
133
134         return streq(parameter, id);
135 }
136
137 static bool test_security(const char *parameter) {
138 #ifdef HAVE_SELINUX
139         if (streq(parameter, "selinux"))
140                 return is_selinux_enabled() > 0;
141 #endif
142         return false;
143 }
144
145 bool condition_test(Condition *c) {
146         assert(c);
147
148         switch(c->type) {
149
150         case CONDITION_PATH_EXISTS:
151                 return (access(c->parameter, F_OK) >= 0) == !c->negate;
152
153         case CONDITION_PATH_EXISTS_GLOB:
154                 return (glob_exists(c->parameter) > 0) == !c->negate;
155
156         case CONDITION_PATH_IS_DIRECTORY: {
157                 struct stat st;
158
159                 if (lstat(c->parameter, &st) < 0)
160                         return !c->negate;
161                 return S_ISDIR(st.st_mode) == !c->negate;
162         }
163
164         case CONDITION_DIRECTORY_NOT_EMPTY: {
165                 int k;
166
167                 k = dir_is_empty(c->parameter);
168                 return !(k == -ENOENT || k > 0) == !c->negate;
169         }
170
171         case CONDITION_FILE_IS_EXECUTABLE: {
172                 struct stat st;
173
174                 if (lstat(c->parameter, &st) < 0)
175                         return !c->negate;
176
177                 return (S_ISREG(st.st_mode) && (st.st_mode & 0111)) == !c->negate;
178         }
179
180         case CONDITION_KERNEL_COMMAND_LINE:
181                 return test_kernel_command_line(c->parameter) == !c->negate;
182
183         case CONDITION_VIRTUALIZATION:
184                 return test_virtualization(c->parameter) == !c->negate;
185
186         case CONDITION_SECURITY:
187                 return test_security(c->parameter) == !c->negate;
188
189         case CONDITION_NULL:
190                 return !c->negate;
191
192         default:
193                 assert_not_reached("Invalid condition type.");
194         }
195 }
196
197 bool condition_test_list(Condition *first) {
198         Condition *c;
199         int triggered = -1;
200
201         /* If the condition list is empty, then it is true */
202         if (!first)
203                 return true;
204
205         /* Otherwise, if all of the non-trigger conditions apply and
206          * if any of the trigger conditions apply (unless there are
207          * none) we return true */
208         LIST_FOREACH(conditions, c, first) {
209                 bool b;
210
211                 b = condition_test(c);
212
213                 if (!c->trigger && !b)
214                         return false;
215
216                 if (c->trigger && triggered <= 0)
217                         triggered = b;
218         }
219
220         return triggered != 0;
221 }
222
223 void condition_dump(Condition *c, FILE *f, const char *prefix) {
224         assert(c);
225         assert(f);
226
227         if (!prefix)
228                 prefix = "";
229
230         fprintf(f,
231                 "%s\t%s: %s%s%s\n",
232                 prefix,
233                 condition_type_to_string(c->type),
234                 c->trigger ? "|" : "",
235                 c->negate ? "!" : "",
236                 c->parameter);
237 }
238
239 void condition_dump_list(Condition *first, FILE *f, const char *prefix) {
240         Condition *c;
241
242         LIST_FOREACH(conditions, c, first)
243                 condition_dump(c, f, prefix);
244 }
245
246 static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
247         [CONDITION_PATH_EXISTS] = "ConditionPathExists",
248         [CONDITION_PATH_EXISTS_GLOB] = "ConditionPathExistsGlob",
249         [CONDITION_PATH_IS_DIRECTORY] = "ConditionPathIsDirectory",
250         [CONDITION_DIRECTORY_NOT_EMPTY] = "ConditionDirectoryNotEmpty",
251         [CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
252         [CONDITION_VIRTUALIZATION] = "ConditionVirtualization",
253         [CONDITION_SECURITY] = "ConditionSecurity",
254         [CONDITION_NULL] = "ConditionNull"
255 };
256
257 DEFINE_STRING_TABLE_LOOKUP(condition_type, ConditionType);