rename flag fields in structures
[project/firewall3.git] / utils.c
1 /*
2 * firewall3 - 3rd OpenWrt UCI firewall implementation
3 *
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include "utils.h"
20 #include "options.h"
21
22 static int lock_fd = -1;
23 static pid_t pipe_pid = -1;
24 static FILE *pipe_fd = NULL;
25
26 static void
27 warn_elem_section_name(struct uci_section *s, bool find_name)
28 {
29 int i = 0;
30 struct uci_option *o;
31 struct uci_element *tmp;
32
33 if (s->anonymous)
34 {
35 uci_foreach_element(&s->package->sections, tmp)
36 {
37 if (strcmp(uci_to_section(tmp)->type, s->type))
38 continue;
39
40 if (&s->e == tmp)
41 break;
42
43 i++;
44 }
45
46 fprintf(stderr, "@%s[%d]", s->type, i);
47
48 if (find_name)
49 {
50 uci_foreach_element(&s->options, tmp)
51 {
52 o = uci_to_option(tmp);
53
54 if (!strcmp(tmp->name, "name") && (o->type == UCI_TYPE_STRING))
55 {
56 fprintf(stderr, " (%s)", o->v.string);
57 break;
58 }
59 }
60 }
61 }
62 else
63 {
64 fprintf(stderr, "'%s'", s->e.name);
65 }
66
67 if (find_name)
68 fprintf(stderr, " ");
69 }
70
71 void
72 warn_elem(struct uci_element *e, const char *format, ...)
73 {
74 if (e->type == UCI_TYPE_SECTION)
75 {
76 fprintf(stderr, "Warning: Section ");
77 warn_elem_section_name(uci_to_section(e), true);
78 }
79 else if (e->type == UCI_TYPE_OPTION)
80 {
81 fprintf(stderr, "Warning: Option ");
82 warn_elem_section_name(uci_to_option(e)->section, false);
83 fprintf(stderr, ".%s ", e->name);
84 }
85
86 va_list argptr;
87 va_start(argptr, format);
88 vfprintf(stderr, format, argptr);
89 va_end(argptr);
90
91 fprintf(stderr, "\n");
92 }
93
94 void
95 warn(const char* format, ...)
96 {
97 fprintf(stderr, "Warning: ");
98 va_list argptr;
99 va_start(argptr, format);
100 vfprintf(stderr, format, argptr);
101 va_end(argptr);
102 fprintf(stderr, "\n");
103 }
104
105 void
106 error(const char* format, ...)
107 {
108 fprintf(stderr, "Error: ");
109 va_list argptr;
110 va_start(argptr, format);
111 vfprintf(stderr, format, argptr);
112 va_end(argptr);
113 fprintf(stderr, "\n");
114
115 exit(1);
116 }
117
118 void
119 info(const char* format, ...)
120 {
121 va_list argptr;
122 va_start(argptr, format);
123 vfprintf(stderr, format, argptr);
124 va_end(argptr);
125 fprintf(stderr, "\n");
126 }
127
128 const char *
129 fw3_find_command(const char *cmd)
130 {
131 struct stat s;
132 int plen = 0, clen = strlen(cmd) + 1;
133 char *search, *p;
134 static char path[PATH_MAX];
135
136 if (!stat(cmd, &s) && S_ISREG(s.st_mode))
137 return cmd;
138
139 search = getenv("PATH");
140
141 if (!search)
142 search = "/bin:/usr/bin:/sbin:/usr/sbin";
143
144 p = search;
145
146 do
147 {
148 if (*p != ':' && *p != '\0')
149 continue;
150
151 plen = p - search;
152
153 if ((plen + clen) >= sizeof(path))
154 continue;
155
156 strncpy(path, search, plen);
157 sprintf(path + plen, "/%s", cmd);
158
159 if (!stat(path, &s) && S_ISREG(s.st_mode))
160 return path;
161
162 search = p + 1;
163 }
164 while (*p++);
165
166 return NULL;
167 }
168
169 bool
170 fw3_stdout_pipe(void)
171 {
172 pipe_fd = stdout;
173 return true;
174 }
175
176 bool
177 __fw3_command_pipe(bool silent, const char *command, ...)
178 {
179 pid_t pid;
180 va_list argp;
181 int pfds[2];
182 int argn;
183 char *arg, **args, **tmp;
184
185 command = fw3_find_command(command);
186
187 if (!command)
188 return false;
189
190 if (pipe(pfds))
191 return false;
192
193 argn = 2;
194 args = malloc(argn * sizeof(arg));
195
196 if (!args)
197 return false;
198
199 args[0] = (char *)command;
200 args[1] = NULL;
201
202 va_start(argp, command);
203
204 while ((arg = va_arg(argp, char *)) != NULL)
205 {
206 tmp = realloc(args, ++argn * sizeof(arg));
207
208 if (!tmp)
209 break;
210
211 args = tmp;
212 args[argn-2] = arg;
213 args[argn-1] = NULL;
214 }
215
216 va_end(argp);
217
218 switch ((pid = fork()))
219 {
220 case -1:
221 return false;
222
223 case 0:
224 dup2(pfds[0], 0);
225
226 close(pfds[0]);
227 close(pfds[1]);
228
229 close(1);
230
231 if (silent)
232 close(2);
233
234 execv(command, args);
235
236 default:
237 signal(SIGPIPE, SIG_IGN);
238 pipe_pid = pid;
239 close(pfds[0]);
240 }
241
242 pipe_fd = fdopen(pfds[1], "w");
243 return true;
244 }
245
246 void
247 fw3_pr(const char *fmt, ...)
248 {
249 va_list args;
250 va_start(args, fmt);
251 vfprintf(pipe_fd, fmt, args);
252 va_end(args);
253 }
254
255 void
256 fw3_command_close(void)
257 {
258 if (pipe_fd && pipe_fd != stdout)
259 fclose(pipe_fd);
260
261 if (pipe_pid > -1)
262 waitpid(pipe_pid, NULL, 0);
263
264 signal(SIGPIPE, SIG_DFL);
265
266 pipe_fd = NULL;
267 pipe_pid = -1;
268 }
269
270 bool
271 fw3_has_table(bool ipv6, const char *table)
272 {
273 FILE *f;
274
275 char line[12];
276 bool seen = false;
277
278 const char *path = ipv6
279 ? "/proc/net/ip6_tables_names" : "/proc/net/ip_tables_names";
280
281 if (!(f = fopen(path, "r")))
282 return false;
283
284 while (fgets(line, sizeof(line), f))
285 {
286 if (!strncmp(line, table, strlen(table)))
287 {
288 seen = true;
289 break;
290 }
291 }
292
293 fclose(f);
294
295 return seen;
296 }
297
298
299 bool
300 fw3_lock(void)
301 {
302 lock_fd = open(FW3_LOCKFILE, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
303
304 if (lock_fd < 0)
305 {
306 warn("Cannot create lock file %s: %s", FW3_LOCKFILE, strerror(errno));
307 return false;
308 }
309
310 if (flock(lock_fd, LOCK_EX))
311 {
312 warn("Cannot acquire exclusive lock: %s", strerror(errno));
313 return false;
314 }
315
316 return true;
317 }
318
319 void
320 fw3_unlock(void)
321 {
322 if (lock_fd < 0)
323 return;
324
325 if (flock(lock_fd, LOCK_UN))
326 warn("Cannot release exclusive lock: %s", strerror(errno));
327
328 close(lock_fd);
329 unlink(FW3_LOCKFILE);
330
331 lock_fd = -1;
332 }
333
334
335 struct list_head *
336 fw3_read_statefile(void)
337 {
338 FILE *sf;
339
340 int n;
341 char line[128];
342 const char *p;
343
344 struct list_head *state;
345 struct fw3_statefile_entry *entry;
346
347 sf = fopen(FW3_STATEFILE, "r");
348
349 if (!sf)
350 return NULL;
351
352 state = malloc(sizeof(*state));
353
354 if (!state)
355 return NULL;
356
357 INIT_LIST_HEAD(state);
358
359 while (fgets(line, sizeof(line), sf))
360 {
361 entry = malloc(sizeof(*entry));
362
363 if (!entry)
364 continue;
365
366 memset(entry, 0, sizeof(*entry));
367
368 p = strtok(line, " \t\n");
369
370 if (!p)
371 continue;
372
373 entry->type = strtoul(p, NULL, 10);
374
375 p = strtok(NULL, " \t\n");
376
377 if (!p)
378 continue;
379
380 entry->name = strdup(p);
381
382 for (n = 0, p = strtok(NULL, " \t\n");
383 n < ARRAY_SIZE(entry->flags) && p != NULL;
384 n++, p = strtok(NULL, " \t\n"))
385 {
386 entry->flags[n] = strtoul(p, NULL, 10);
387 }
388
389 list_add_tail(&entry->list, state);
390 }
391
392 fclose(sf);
393
394 return state;
395 }
396
397 void
398 fw3_write_statefile(void *state)
399 {
400 FILE *sf;
401 struct fw3_state *s = state;
402 struct fw3_defaults *d = &s->defaults;
403 struct fw3_zone *z;
404 struct fw3_ipset *i;
405
406 int mask = (1 << FW3_DEFAULT_IPV4_LOADED) | (1 << FW3_DEFAULT_IPV6_LOADED);
407
408 if (!(d->flags & mask))
409 {
410 if (unlink(FW3_STATEFILE))
411 warn("Unable to remove state %s: %s",
412 FW3_STATEFILE, strerror(errno));
413
414 return;
415 }
416
417 sf = fopen(FW3_STATEFILE, "w");
418
419 if (!sf)
420 {
421 warn("Cannot create state %s: %s", FW3_STATEFILE, strerror(errno));
422 return;
423 }
424
425 fprintf(sf, "%u - %u\n", FW3_TYPE_DEFAULTS, d->flags);
426
427 list_for_each_entry(z, &s->zones, list)
428 {
429 fprintf(sf, "%u %s %u %u\n", FW3_TYPE_ZONE,
430 z->name, z->src_flags, z->dst_flags);
431 }
432
433 list_for_each_entry(i, &s->ipsets, list)
434 {
435 if (i->external && *i->external)
436 continue;
437
438 fprintf(sf, "%u %s\n", FW3_TYPE_IPSET, i->name);
439 }
440
441 fclose(sf);
442 }
443
444 void
445 fw3_free_statefile(struct list_head *statefile)
446 {
447 struct fw3_statefile_entry *e, *tmp;
448
449 if (!statefile)
450 return;
451
452 list_for_each_entry_safe(e, tmp, statefile, list)
453 {
454 list_del(&e->list);
455 free(e->name);
456 free(e);
457 }
458
459 free(statefile);
460 }