Debian Codegraph @

src/mustach-dhcpd-pools.c

raw symbols SCIP
1 /*
2 * The dhcpd-pools has BSD 2-clause license which also known as "Simplified
3 * BSD License" or "FreeBSD License".
4 *
5 * Copyright 2006- Sami Kerola. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the
17 * distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * The views and conclusions contained in the software and documentation are
32 * those of the authors and should not be interpreted as representing
33 * official policies, either expressed or implied, of Sami Kerola.
34 */
35
36 /*! \file mustach-dhcpd-pools.c
37 * \brief Mustache templating specific functions.
38 */
39
40 #include <config.h>
41
42 #include <fcntl.h>
43 #include <errno.h>
44 #include <error.h>
45 #include <stdlib.h>
46 #include <sys/stat.h>
47 #include <sys/time.h>
48 #include <unistd.h>
49
50 #include "close-stream.h"
51 #include "dhcpd-pools.h"
52 #include "error.h"
53 #include "mustach.h"
54 #include "xalloc.h"
55
56 /*! \struct expl
57 * \brief A structure that travels through mustach via closure void pointer.
58 */
59 struct expl {
60 struct conf_t *state;
61 struct leases_t *lease_p;
62 struct range_t *range_p;
63 struct shared_network_t *shnet_p;
64 struct output_helper_t oh;
65 int current;
66 uint32_t pad;
67 };
68
69 static int must_enter(void *closure, const char *name);
70 static int must_leave(void *closure);
71
72 /*! \brief Template base level tag parser and printer. */
73 static int must_put_base(void *closure, const char *name, int escape
74 __attribute__ ((unused)), FILE *file)
75 {
76 struct expl *e = closure;
77
78 if (!strcmp(name, "localtime")) {
79 dp_time_tool(file, NULL, 0);
80 return 0;
81 }
82 if (!strcmp(name, "number_of_ranges")) {
83 fprintf(file, "%u", e->state->num_ranges);
84 return 0;
85 }
86 if (!strcmp(name, "number_of_ranges_warning")) {
87 struct status_counts_t stat = { 0 };
88
89 range_alarms(e->state, &stat);
90 fprintf(file, "%u", stat.warning);
91 return 0;
92 }
93 if (!strcmp(name, "number_of_ranges_critical")) {
94 struct status_counts_t stat = { 0 };
95
96 range_alarms(e->state, &stat);
97 fprintf(file, "%u", stat.critical);
98 return 0;
99 }
100 if (!strcmp(name, "number_of_shared_networks")) {
101 uint32_t num = 0;
102 struct shared_network_t *shared_p;
103
104 for (shared_p = e->state->shared_net_root->next; shared_p;
105 shared_p = shared_p->next) {
106 num++;
107 }
108 fprintf(file, "%u", num);
109 return 0;
110 }
111 if (!strcmp(name, "number_of_shared_networks_warning")) {
112 struct status_counts_t stat = { 0 };
113
114 shared_net_alarms(e->state, &stat);
115 fprintf(file, "%u", stat.warning);
116 return 0;
117 }
118 if (!strcmp(name, "number_of_shared_networks_critical")) {
119 struct status_counts_t stat = { 0 };
120
121 shared_net_alarms(e->state, &stat);
122 fprintf(file, "%u", stat.critical);
123 return 0;
124 }
125 if (!strcmp(name, "version")) {
126 fprintf(file, "%s", PACKAGE_VERSION);
127 return 0;
128 }
129 /* lease file */
130 if (!strcmp(name, "lease_file_path")) {
131 fprintf(file, "%s", e->state->dhcpdlease_file);
132 return 0;
133 }
134 if (!strcmp(name, "lease_file_local_mtime")) {
135 dp_time_tool(file, e->state->dhcpdlease_file, 0);
136 return 0;
137 }
138 if (!strcmp(name, "lease_file_epoch_mtime")) {
139 dp_time_tool(file, e->state->dhcpdlease_file, 1);
140 return 0;
141 }
142 /* conf file */
143 if (!strcmp(name, "conf_file_path")) {
144 fprintf(file, "%s", e->state->dhcpdconf_file);
145 return 0;
146 }
147 if (!strcmp(name, "conf_file_local_mtime")) {
148 dp_time_tool(file, e->state->dhcpdconf_file, 0);
149 return 0;
150 }
151 if (!strcmp(name, "conf_file_epoch_mtime")) {
152 dp_time_tool(file, e->state->dhcpdconf_file, 1);
153 return 0;
154 }
155 /* template file */
156 if (!strcmp(name, "template_file_path")) {
157 fprintf(file, "%s", e->state->mustach_template);
158 return 0;
159 }
160 if (!strcmp(name, "template_file_local_mtime")) {
161 dp_time_tool(file, e->state->mustach_template, 0);
162 return 0;
163 }
164 if (!strcmp(name, "template_file_epoch_mtime")) {
165 dp_time_tool(file, e->state->mustach_template, 1);
166 return 0;
167 }
168 error(EXIT_FAILURE, 0, "mustach_dhcpd_pools: fmustach: unexpected tag: %s", name);
169 return 1;
170 }
171
172 /*! \struct mustach_itf
173 * \brief Mustach function pointers. */
174 static struct mustach_itf itf = {
175 .start = NULL,
176 .enter = must_enter,
177 .put = must_put_base,
178 .next = NULL,
179 .leave = must_leave
180 };
181
182 /*! \brief Mustach active lease aka {{#active_lease}} tag parser and printer. */
183 static int must_put_active_lease(void *closure, const char *name, int escape
184 __attribute__((unused)), FILE *file)
185 {
186 struct expl *e = closure;
187
188 if (!strcmp(name, "ip")) {
189 fprintf(file, "%s", ntop_ipaddr(&e->lease_p->ip));
190 return 0;
191 }
192 if (!strcmp(name, "macaddress")) {
193 fprintf(file, "%s", e->lease_p->ethernet);
194 return 0;
195 }
196 error(EXIT_FAILURE, 0, "mustach_dhcpd_pools: fmustach: unexpected tag: %s", name);
197 return 1;
198 }
199
200 /*! \brief A function to move to next lease when {{/active_lease}} is encountered. */
201 static int must_next_active_lease(void *closure)
202 {
203 struct expl *e = closure;
204
205 e->lease_p = e->lease_p->hh.next;
206 if (e->lease_p == NULL)
207 return 0;
208 return 1;
209 }
210
211
212 /*! \brief Mustach range aka {{#subnets}} tag parser and printer. */
213 static int must_put_range(void *closure, const char *name, int escape
214 __attribute__ ((unused)), FILE *file)
215 {
216 struct expl *e = closure;
217
218 if (!strcmp(name, "location")) {
219 fprintf(file, "%s", e->range_p->shared_net->name);
220 return 0;
221 }
222 if (!strcmp(name, "range")) {
223 fprintf(file, "%s - ", ntop_ipaddr(&e->range_p->first_ip));
224 fprintf(file, "%s", ntop_ipaddr(&e->range_p->last_ip));
225 return 0;
226 }
227 if (!strcmp(name, "first_ip")) {
228 fprintf(file, "%s", ntop_ipaddr(&e->range_p->first_ip));
229 return 0;
230 }
231 if (!strcmp(name, "last_ip")) {
232 fprintf(file, "%s", ntop_ipaddr(&e->range_p->last_ip));
233 return 0;
234 }
235 if (!strcmp(name, "used")) {
236 fprintf(file, "%g", e->range_p->count);
237 return 0;
238 }
239 if (!strcmp(name, "touched")) {
240 fprintf(file, "%g", e->range_p->touched);
241 return 0;
242 }
243 if (!strcmp(name, "defined")) {
244 fprintf(file, "%g", e->oh.range_size);
245 return 0;
246 }
247 if (!strcmp(name, "free")) {
248 fprintf(file, "%g", e->oh.range_size - e->range_p->count);
249 return 0;
250 }
251 if (!strcmp(name, "percent")) {
252 fprintf(file, "%g", e->oh.percent);
253 return 0;
254 }
255 if (!strcmp(name, "touch_count")) {
256 fprintf(file, "%g", e->oh.tc);
257 return 0;
258 }
259 if (!strcmp(name, "touch_percent")) {
260 fprintf(file, "%g", e->oh.tcp);
261 return 0;
262 }
263 if (e->state->backups_found == 1) {
264 if (!strcmp(name, "backup_count")) {
265 fprintf(file, "%g", e->range_p->backups);
266 return 0;
267 }
268 if (!strcmp(name, "backup_percent")) {
269 fprintf(file, "%g", e->oh.bup);
270 return 0;
271 }
272 }
273 if (!strcmp(name, "status")) {
274 fprintf(file, "%d", e->oh.status);
275 return 0;
276 }
277 if (!strcmp(name, "gettimeofday")) {
278 dp_time_tool(file, NULL, 1);
279 return 0;
280 }
281 if (!strcmp(name, "lease_file_epoch_mtime")) {
282 dp_time_tool(file, e->state->dhcpdlease_file, 1);
283 return 0;
284 }
285 error(EXIT_FAILURE, 0, "mustach_dhcpd_pools: fmustach: unexpected tag: %s", name);
286 return 1;
287 }
288
289 /*! \brief Mustach shared networks aka {{#shared-networks}} tag parser and printer. */
290 static int must_put_shnet(void *closure, const char *name, int escape
291 __attribute__ ((unused)), FILE *file)
292 {
293 struct expl *e = closure;
294
295 if (!strcmp(name, "location")) {
296 fprintf(file, "%s", e->shnet_p->name);
297 return 0;
298 }
299 if (!strcmp(name, "defined")) {
300 fprintf(file, "%g", e->shnet_p->available);
301 return 0;
302 }
303 if (!strcmp(name, "used")) {
304 fprintf(file, "%g", e->shnet_p->used);
305 return 0;
306 }
307 if (!strcmp(name, "touched")) {
308 fprintf(file, "%g", e->shnet_p->touched);
309 return 0;
310 }
311 if (!strcmp(name, "free")) {
312 fprintf(file, "%g", e->shnet_p->available - e->shnet_p->used);
313 return 0;
314 }
315 if (!strcmp(name, "percent")) {
316 fprintf(file, "%g", e->oh.percent);
317 return 0;
318 }
319 if (!strcmp(name, "touch_count")) {
320 fprintf(file, "%g", e->oh.tc);
321 return 0;
322 }
323 if (!strcmp(name, "touch_percent")) {
324 fprintf(file, "%g", e->oh.tcp);
325 return 0;
326 }
327 if (e->state->backups_found == 1) {
328 if (!strcmp(name, "backup_count")) {
329 fprintf(file, "%g", e->shnet_p->backups);
330 return 0;
331 }
332 if (!strcmp(name, "backup_percent")) {
333 fprintf(file, "%g", e->oh.bup);
334 return 0;
335 }
336 }
337 if (!strcmp(name, "status")) {
338 fprintf(file, "%d", e->oh.status);
339 return 0;
340 }
341 if (!strcmp(name, "gettimeofday")) {
342 dp_time_tool(file, NULL, 1);
343 return 0;
344 }
345 if (!strcmp(name, "lease_file_epoch_mtime")) {
346 dp_time_tool(file, e->state->dhcpdlease_file, 1);
347 return 0;
348 }
349 error(EXIT_FAILURE, 0, "mustach_dhcpd_pools: fmustach: unexpected tag: %s", name);
350 return 1;
351 }
352
353 /*! \brief A function to move to next range when {{/subnets}} is encountered. */
354 static int must_next_range(void *closure)
355 {
356 struct expl *e = closure;
357
358 do {
359 e->range_p++;
360 e->current--;
361 if (e->current <= 0)
362 return 0;
363 } while (range_output_helper(e->state, &e->oh, e->range_p));
364 return 1;
365 }
366
367 /*! \brief A function to move to next shared network when {{/shared-networks}}
368 * is encountered. */
369 static int must_next_shnet(void *closure)
370 {
371 struct expl *e = closure;
372
373 if (e->current == 1 || e->shnet_p == NULL)
374 return 0;
375 while (1) {
376 e->shnet_p = e->shnet_p->next;
377 if (e->shnet_p == NULL)
378 break;
379 if (shnet_output_helper(e->state, &e->oh, e->shnet_p))
380 continue;
381 else
382 return 1;
383 }
384 return 0;
385 }
386
387 /*! \brief Function that is called when mustach is searching output loops from
388 * template file. */
389 static int must_enter(void *closure, const char *name)
390 {
391 struct expl *e = closure;
392
393 if (!strcmp(name, "active_lease")) {
394 itf.put = must_put_active_lease;
395 itf.next = must_next_active_lease;
396 e->current = 0;
397 e->lease_p = e->state->leases;
398 return must_next_active_lease(closure);
399 }
400 if (!strcmp(name, "subnets")) {
401 itf.put = must_put_range;
402 itf.next = must_next_range;
403 e->current = e->state->num_ranges + 1;
404 e->range_p = e->state->ranges;
405 /* must_next_range() will skip_ok when needed */
406 e->range_p--;
407 return must_next_range(closure);
408 }
409 if (!strcmp(name, "shared-networks")) {
410 itf.put = must_put_shnet;
411 itf.next = must_next_shnet;
412 e->shnet_p = e->state->shared_net_root;
413 e->current = 0;
414 return must_next_shnet(closure);
415 }
416 if (!strcmp(name, "summary")) {
417 itf.put = must_put_shnet;
418 itf.next = must_next_shnet;
419 e->shnet_p = e->state->shared_net_root;
420 e->current = 1;
421 shnet_output_helper(e->state, &e->oh, e->shnet_p);
422 return 1;
423 }
424 error(EXIT_FAILURE, 0, "mustach_dhcpd_pools: fmustach: unexpected tag: %s", name);
425 return 1;
426 }
427
428 /*! \brief Function that is called when all elements within a print loop are outputted. */
429 static int must_leave(void *closure)
430 {
431 struct expl *e = closure;
432
433 e->shnet_p = e->state->shared_net_root;
434 e->range_p = e->state->ranges;
435 itf.put = must_put_base;
436 return 0;
437 }
438
439 /*! \brief Read mustach template to memory. */
440 static char *must_read_template(const char *filename)
441 {
442 int f;
443 struct stat s;
444 char *result;
445
446 if (filename == NULL)
447 error(EXIT_FAILURE, 0, "must_read_template: --mustach argument missing");
448 if ((f = open(filename, O_RDONLY)) < 0) {
449 error(EXIT_FAILURE, errno, "must_read_template: open: %s", filename);
450 }
451 fstat(f, &s);
452 result = xmalloc(s.st_size + 1);
453 if (read(f, result, s.st_size) != s.st_size) {
454 error(EXIT_FAILURE, errno, "must_read_template: read: %s", filename);
455 }
456 close(f);
457 result[s.st_size] = 0;
458 return result;
459 }
460
461 /*! \brief Start mustach processing. */
462 int mustach_dhcpd_pools(struct conf_t *state)
463 {
464 struct expl e = { .state = state };
465 char *template;
466 FILE *outfile;
467 int ret;
468
469 template = must_read_template(state->mustach_template);
470 if (state->output_file) {
471 outfile = fopen(state->output_file, "w+");
472 if (outfile == NULL) {
473 error(EXIT_FAILURE, errno, "mustach_dhcpd_pools: fopen: %s",
474 state->output_file);
475 }
476 } else {
477 outfile = stdout;
478 }
479 ret = fmustach(template, &itf, &e, outfile);
480 free(template);
481 if (outfile == stdout) {
482 if (fflush(stdout))
483 error(EXIT_FAILURE, errno, "mustach_dhcpd_pools: fflush");
484 } else {
485 if (close_stream(outfile))
486 error(EXIT_FAILURE, errno, "mustach_dhcpd_pools: fclose");
487 }
488 switch (ret) {
489 case MUSTACH_OK:
490 return 0;
491 case MUSTACH_ERROR_SYSTEM:
492 error(EXIT_FAILURE, 0, "mustach_dhcpd_pools: fmustach: system error");
493 break;
494 case MUSTACH_ERROR_UNEXPECTED_END:
495 error(EXIT_FAILURE, 0, "mustach_dhcpd_pools: fmustach: unexpected end");
496 break;
497 case MUSTACH_ERROR_EMPTY_TAG:
498 error(EXIT_FAILURE, 0, "mustach_dhcpd_pools: fmustach: empty tag");
499 break;
500 case MUSTACH_ERROR_TAG_TOO_LONG:
501 error(EXIT_FAILURE, 0, "mustach_dhcpd_pools: fmustach: too long tag");
502 break;
503 case MUSTACH_ERROR_BAD_SEPARATORS:
504 error(EXIT_FAILURE, 0, "mustach_dhcpd_pools: fmustach: bad separator");
505 break;
506 case MUSTACH_ERROR_TOO_DEEP:
507 error(EXIT_FAILURE, 0, "mustach_dhcpd_pools: fmustach: too deep");
508 break;
509 case MUSTACH_ERROR_CLOSING:
510 error(EXIT_FAILURE, 0, "mustach_dhcpd_pools: fmustach: closing");
511 break;
512 case MUSTACH_ERROR_BAD_UNESCAPE_TAG:
513 error(EXIT_FAILURE, 0, "mustach_dhcpd_pools: fmustach: bad escape tag");
514 break;
515 default:
516 error(EXIT_FAILURE, 0, "mustach_dhcpd_pools: fmustach: unknown error");
517 }
518 return 1;
519 }