Allow xml output with leases to use output file
[debian/dhcpd-pools.git] / src / getdata.c
1 /* http://dhcpd-pools.sourceforge.net/
2 ** Copyright 2006- Sami Kerola <kerolasa@iki.fi>
3 **
4 ** This program is free software: you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation, either version 3 of the License, or
7 ** (at your option) any later version.
8 **
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ** GNU General Public License for more details.
13 **
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21
22 #ifdef HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif
25
26 #ifdef  HAVE_STDLIB_H
27 #include <stdlib.h>
28 #else                           /* Not STDC_HEADERS */
29 extern char *malloc();
30 #define EXIT_FAILURE    1       /* Failing exit status.  */
31 #define EXIT_SUCCESS    0       /* Successful exit status.  */
32 #endif                          /* STDC_HEADERS */
33
34 #ifdef  HAVE_STRING_H
35 #include <string.h>
36 #else
37 #include <strings.h>
38 #endif
39
40 #include <stdio.h>
41 #include <arpa/inet.h>
42 #include <netinet/in.h>
43 #include <sys/socket.h>
44 #include <sys/stat.h>
45 #include <sys/types.h>
46 #ifndef _XOPEN_SOURCE
47 #define _XOPEN_SOURCE 600
48 #endif
49 #include <fcntl.h>
50 #include <errno.h>
51 #include <err.h>
52 #include <ctype.h>
53 #include <assert.h>
54
55 #include "dhcpd-pools.h"
56 #include "defaults.h"
57
58 /* Parse dhcpd.leases file. All performance boosts for this function are
59  * wellcome */
60 int parse_leases(void)
61 {
62         FILE *dhcpd_leases;
63         char *line, *ipstring, *macstring;
64         struct in_addr inp;
65         struct stat lease_file_stats;
66         struct macaddr_t *macaddr_p;
67         unsigned long leasesmallocsize;
68         unsigned long touchesmallocsize;
69         unsigned long backupsmallocsize;
70         int sw_active_lease = 0;
71
72         num_touches = num_leases = num_backups = 0;
73
74         dhcpd_leases = fopen(config.dhcpdlease_file, "r");
75         if (dhcpd_leases == NULL) {
76                 err(EXIT_FAILURE, "parse_leases: %s",
77                     config.dhcpdlease_file);
78         }
79 #ifdef POSIX_FADV_WILLNEED
80         posix_fadvise((long) dhcpd_leases, 0, 0, POSIX_FADV_WILLNEED);
81         if (errno) {
82                 err(EXIT_FAILURE, "parse_leases: fadvise %s",
83                     config.dhcpdlease_file);
84         }
85 #endif                          /* POSIX_FADV_WILLNEED */
86 #ifdef POSIX_FADV_SEQUENTIAL
87         posix_fadvise((long) dhcpd_leases, 0, 0, POSIX_FADV_SEQUENTIAL);
88         if (errno) {
89                 err(EXIT_FAILURE, "parse_leases: fadvise %s",
90                     config.dhcpdlease_file);
91         }
92 #endif                          /* POSIX_FADV_SEQUENTIAL */
93
94         /* I found out that there's one lease address per 300 bytes in
95          * dhcpd.leases file. Malloc is little bit pessimistic and uses 250.
96          * If someone has higher density in lease file I'm interested to
97          * hear about that. */
98         if (stat(config.dhcpdlease_file, &lease_file_stats)) {
99                 err(EXIT_FAILURE, "parse_leases: %s",
100                     config.dhcpdlease_file);
101         }
102         leasesmallocsize = (lease_file_stats.st_size / 250) + MAXLEN - 2;
103         touchesmallocsize = (lease_file_stats.st_size / 250) + MAXLEN - 2;
104         backupsmallocsize = (lease_file_stats.st_size / 120) + MAXLEN - 2;
105         leases = safe_malloc(sizeof(long int) * leasesmallocsize);
106         touches =
107             safe_malloc((size_t) sizeof(long int) * touchesmallocsize);
108
109         line = safe_malloc(sizeof(long int) * MAXLEN);
110         ipstring = safe_malloc(sizeof(long int) * MAXLEN);
111         if (config.output_format[0] == 'X') {
112                 macstring = safe_malloc(sizeof(char) * 18);
113                 macaddr = safe_malloc(sizeof(struct macaddr_t));
114                 macaddr_p = macaddr;
115                 macaddr_p->next = NULL;
116         }
117
118         while (!feof(dhcpd_leases)) {
119                 fgets(line, MAXLEN, dhcpd_leases);
120                 /* It's a lease, save IP */
121                 if (strstr(line, "lease") == line) {
122                         strncpy(ipstring, line, (size_t) MAXLEN);
123                         nth_field(2, ipstring, ipstring);
124                         inet_aton(ipstring, &inp);
125                         sw_active_lease = 0;
126                 }
127                 /* Copy IP to correct array */
128                 else if (strstr(line, "binding state active")) {
129                         leases[num_leases] = htonl(inp.s_addr);
130                         num_leases++;
131                         assert(!(leasesmallocsize < num_leases));
132                         sw_active_lease = 1;
133                 } else if (strstr(line, "  binding state free")) {
134                         touches[num_touches] = htonl(inp.s_addr);
135                         num_touches++;
136                         assert(!(touchesmallocsize < num_touches));
137                 } else if (strstr(line, "  binding state backup")) {
138                         if (num_backups == 0) {
139                                 backups =
140                                     safe_malloc((size_t) sizeof(long int) *
141                                                 backupsmallocsize);
142                         }
143                         backups[num_backups] = htonl(inp.s_addr);
144                         num_backups++;
145                         assert(!(backupsmallocsize < num_backups));
146                 }
147
148                 if ((macaddr != NULL)
149                     && (sw_active_lease == 1)
150                     && (strstr(line, "hardware ethernet"))) {
151                         nth_field(3, macstring, line);
152                         macstring[17] = '\0';
153                         macaddr_p->ethernet = safe_strdup(macstring);
154                         macaddr_p->ip = safe_strdup(ipstring);
155                         macaddr_p->next =
156                             safe_malloc(sizeof(struct macaddr_t));
157                         macaddr_p = macaddr_p->next;
158                         macaddr_p->next = NULL;
159                 }
160         }
161         free(line);
162         free(ipstring);
163         if (macaddr != NULL) {
164                 free(macstring);
165         }
166         return 0;
167 }
168
169 /* Like strcpy but for field which is separated by white spaces. Number of
170  * first field is 1 and not 0 like C programs should have. Question of
171  * semantics, send mail to author if this annoys. All performance boosts for
172  * this function are well come. */
173 int nth_field(int n, char *dest, const char *src)
174 {
175         int i, j = 0, wordn = 0, len;
176
177         len = strlen(src);
178
179         for (i = 0; i < len; i++) {
180                 if (isspace(src[i])) {
181                         if (!(wordn < n)) {
182                                 dest[j] = '\0';
183                                 break;
184                         }
185                         j = 0;
186                 } else {
187                         if (j == 0) {
188                                 wordn++;
189                         }
190                         if (wordn == n) {
191                                 dest[j] = src[i];
192                         }
193                         j++;
194                 }
195         }
196         return 0;
197 }
198
199 /* dhcpd.conf interesting words */
200 int is_interesting_config_clause(char *s)
201 {
202         if (strstr(s, "range")) {
203                 return 3;
204         } else if (strstr(s, "shared-network")) {
205                 return 1;
206         } else if (strstr(s, "include")) {
207                 return 4;
208         } else {
209                 return 0;
210         }
211 }
212
213 /* FIXME: This spagetti monster function need to be rewrote at least ones. */
214 char *parse_config(int is_include, char *config_file,
215                    char *current_shared_name,
216                    char *next_free_shared_name,
217                    struct shared_network_t *shared_p)
218 {
219         FILE *dhcpd_config;
220         int i = 0, newclause = true, argument = false, comment =
221             false, braces = 0, quote = false;
222         char *word, c;
223         int braces_shared = 1000;
224         struct in_addr inp;
225         struct range_t *range_p;
226
227         char *last_shared_name;
228         last_shared_name = SHARED_NETWORKS_NAMES + shared_net_names;
229
230         word = safe_malloc(sizeof(char) * MAXLEN);
231
232         if (is_include) {
233                 /* Default place holder for ranges "All networks". */
234                 shared_p->name = current_shared_name;
235         }
236
237         /* Open configuration file */
238         dhcpd_config = fopen(config_file, "r");
239         if (dhcpd_config == NULL) {
240                 err(EXIT_FAILURE, "parse_config: %s", config_file);
241         }
242 #ifdef POSIX_FADV_WILLNEED
243         posix_fadvise((long) dhcpd_config, 0, 0, POSIX_FADV_WILLNEED);
244         if (errno) {
245                 err(EXIT_FAILURE, "parse_config: fadvise %s", config_file);
246         }
247 #endif                          /* POSIX_FADV_WILLNEED */
248 #ifdef POSIX_FADV_SEQUENTIAL
249         posix_fadvise((long) dhcpd_config, 0, 0, POSIX_FADV_SEQUENTIAL);
250         if (errno) {
251                 err(EXIT_FAILURE, "parse_config: fadvise %s", config_file);
252         }
253 #endif                          /* POSIX_FADV_SEQUENTIAL */
254
255         /* Very hairy stuff begins. */
256         while (!feof(dhcpd_config)) {
257                 c = fgetc(dhcpd_config);
258                 /* Certain characters are magical */
259                 switch (c) {
260                         /* Handle comments if they are not quoted */
261                 case '#':
262                         if (quote == false) {
263                                 comment = true;
264                         }
265                         continue;
266                 case '"':
267                         if (comment == false) {
268                                 quote++;
269                                 /* Either one or zero */
270                                 quote = quote % 2;
271                         }
272                         continue;
273                 case '\n':
274                         /* New line resets comment section, but
275                          * not if quoted */
276                         if (quote == false) {
277                                 comment = false;
278                         }
279                         break;
280                 case ';':
281                         /* Quoted colon does not mean new clause */
282                         if (quote == true) {
283                                 break;
284                         }
285                         if (comment == false && argument != 2
286                             && argument != 4) {
287                                 newclause = true;
288                                 i = 0;
289                         } else if (argument == 2) {
290                                 /* Range ends to ; and this hair in code
291                                  * make two ranges wrote to gether like...
292                                  *
293                                  * range 10.20.30.40 10.20.30.41;range 10.20.30.42 10.20.30.43;
294                                  *
295                                  * ...to be interpreted correctly. */
296                                 c = ' ';
297                         }
298                         continue;
299                 case '{':
300                         if (quote == true) {
301                                 break;
302                         }
303                         if (comment == false) {
304                                 braces++;
305                         }
306                         /* i == 0 detects word that ends to brace like:
307                          *
308                          * shared-network DSL{ ... */
309                         if (i == 0) {
310                                 newclause = true;
311                         }
312                         continue;
313                 case '}':
314                         if (quote == true) {
315                                 break;
316                         }
317                         if (comment == false) {
318                                 braces--;
319                                 /* End of shared-network */
320                                 if (braces_shared == braces) {
321                                         current_shared_name =
322                                             shared_net_names;
323                                         /* FIXME: Using 1000 is lame, but
324                                          * works. */
325                                         braces_shared = 1000;
326                                         shared_p = shared_networks;
327                                 }
328                                 /* Not literally true, but works for this
329                                  * program */
330                                 newclause = true;
331                         }
332                         continue;
333                 default:
334                         break;
335                 }
336
337                 /* Either inside comment or Nth word of clause. */
338                 if (comment == true
339                     || (newclause == false && argument == 0)) {
340                         continue;
341                 }
342                 /* Strip white spaces before new clause word. */
343                 if ((newclause == true || argument != 0) && isspace(c)
344                     && i == 0) {
345                         continue;
346                 }
347                 /* Save to word which clause this is. */
348                 if ((newclause == true || argument != 0)
349                     && (!isspace(c) || quote == true)) {
350                         word[i] = c;
351                         i++;
352                         /* Long word which is almost causing overflow. None
353                          * of words are this long which the program is
354                          * searching. */
355                         if (MAXLEN < i) {
356                                 newclause = false;
357                                 i = 0;
358                                 continue;
359                         }
360                 }
361                 /* See if clause is something that parser is looking for. */
362                 else if (newclause == true) {
363                         /* Insert string end & set state */
364                         word[i] = '\0';
365                         newclause = false;
366                         i = 0;
367
368                         argument = is_interesting_config_clause(word);
369                 }
370                 /* words after range, shared-network or include */
371                 else if (argument != 0) {
372                         word[i] = '\0';
373                         newclause = false;
374                         i = 0;
375
376                         switch (argument) {
377                         case 2:
378                                 /* printf ("range 2nd ip: %s\n", word); */
379                                 range_p = ranges + num_ranges;
380                                 inet_aton(word, &inp);
381                                 argument = 0;
382                                 range_p->last_ip = htonl(inp.s_addr) + 1;
383                                 range_p->count = 0;
384                                 range_p->touched = 0;
385                                 range_p->backups = 0;
386                                 range_p->shared_net = shared_p;
387                                 num_ranges++;
388                                 if (RANGES < num_ranges) {
389                                         errx(EXIT_FAILURE,
390                                              "parse_config: Range space full! Increase RANGES and recompile.");
391                                 }
392                                 newclause = true;
393                                 break;
394                         case 3:
395                                 /* printf ("range 1nd ip: %s\n", word); */
396                                 range_p = ranges + num_ranges;
397                                 if (!(inet_aton(word, &inp))) {
398                                         /* word was not ip, try
399                                          * again */
400                                         break;
401                                 }
402                                 range_p->first_ip = htonl(inp.s_addr) - 1;
403                                 argument = 2;
404                                 break;
405                         case 1:
406                                 /* printf ("shared-network named: %s\n", word); */
407                                 strcpy(next_free_shared_name, word);
408                                 shared_p =
409                                     shared_networks + num_shared_networks;
410                                 num_shared_networks++;
411                                 shared_p++;
412                                 shared_p->name = next_free_shared_name;
413                                 shared_p->available = 0;
414                                 shared_p->used = 0;
415                                 shared_p->touched = 0;
416                                 shared_p->backups = 0;
417                                 /* Temporary abuse of argument
418                                  * variable */
419                                 argument =
420                                     strlen(next_free_shared_name) + 1;
421                                 if (next_free_shared_name + argument <
422                                     last_shared_name) {
423                                         next_free_shared_name += argument;
424                                 } else {
425                                         /* FIXME: make this go
426                                          * away by reallocationg
427                                          * more space. */
428                                         errx(EXIT_FAILURE,
429                                              "parse_config: End of shared-network space, increase SHARED_NETWORKS_NAMES and recompile");
430                                 }
431                                 argument = 0;
432                                 braces_shared = braces;
433                                 break;
434                         case 4:
435                                 /* printf ("include file: %s\n", word); */
436                                 argument = 0;
437                                 next_free_shared_name =
438                                     parse_config(false, word,
439                                                  current_shared_name,
440                                                  next_free_shared_name,
441                                                  shared_p);
442                                 newclause = true;
443                                 break;
444                         case 0:
445                                 /* printf ("nothing interesting: %s\n", word); */
446                                 argument = 0;
447                                 break;
448                         default:
449                                 warnx("impossible occurred, report a bug");
450                                 assert(0);
451                         }
452                 }
453         }
454         free(word);
455         return next_free_shared_name;
456 }